This commit is contained in:
Chris 2023-12-24 21:42:43 +01:00
parent 6703a07e0f
commit 56b996cfd5
4 changed files with 14 additions and 9 deletions

View File

@ -1,5 +1,4 @@
.PHONY: all .PHONY: all
all: tinycrc.a crctest all: tinycrc.a crctest

View File

@ -1,3 +1,7 @@
# TinyCRC # TinyCRC
This is a library for calculating CRC32, CRC16 and CRC8. This is a library for calculating CRC32, CRC16 and CRC8. I did not write these, but
they are collected from various places on the interweb, and as such available under
the MIT license.
The library should be portable enough to work on embedded systems.

View File

@ -10,29 +10,31 @@ void test(const uint8_t* buffer)
uint16_t crc16 = tinycrc_crc16(buffer, strlen((char*)buffer)); uint16_t crc16 = tinycrc_crc16(buffer, strlen((char*)buffer));
uint8_t crc8 = tinycrc_crc8(buffer, strlen((char*)buffer)); uint8_t crc8 = tinycrc_crc8(buffer, strlen((char*)buffer));
printf("%-32s | crc32 | %08x\n", (char*)buffer, crc32); printf("%-32s | %08x | %04x | %02x\n", (char*)buffer, crc32, crc16, crc8);
printf("%-32s | crc16 | %04x\n", (char*)buffer, crc16);
printf("%-32s | crc8 | %02x\n", (char*)buffer, crc8);
} }
int main() int main()
{ {
uint8_t* buffer = (uint8_t*)malloc(32); uint8_t* buffer = (uint8_t*)malloc(32);
printf("%-32s | %8d | %4d | %2d\n", (char*)buffer, 32, 16, 8);
memset((char*)buffer, 0, 32);
strcpy((char*)buffer, "Hello World\0"); strcpy((char*)buffer, "Hello World\0");
test(buffer); test(buffer);
memset((char*)buffer, 0, 32);
strcpy((char*)buffer, "Hello World!\0"); strcpy((char*)buffer, "Hello World!\0");
test(buffer); test(buffer);
memset((char*)buffer, 0, 32);
strcpy((char*)buffer, "Hello Worlds\0"); strcpy((char*)buffer, "Hello Worlds\0");
test(buffer); test(buffer);
memset((char*)buffer, 0, 32);
strcpy((char*)buffer, "Testing\0"); strcpy((char*)buffer, "Testing\0");
test(buffer); test(buffer);
free(buffer); free(buffer);
} }

View File

@ -1,7 +1,7 @@
#include "tinycrc.h" #include "tinycrc.h"
/* /*
CRC16 CRC16 (from https://stackoverflow.com/questions/10564491/function-to-calculate-a-crc16-checksum)
*/ */
uint16_t tinycrc_crc16(const uint8_t *data, uint16_t size) uint16_t tinycrc_crc16(const uint8_t *data, uint16_t size)
@ -19,4 +19,4 @@ uint16_t tinycrc_crc16(const uint8_t *data, uint16_t size)
crc = (crc << 8) ^ ((unsigned short)(x << 12)) ^ ((unsigned short)(x <<5)) ^ ((unsigned short)x); crc = (crc << 8) ^ ((unsigned short)(x << 12)) ^ ((unsigned short)(x <<5)) ^ ((unsigned short)x);
} }
return crc & 0xFFFF; return crc & 0xFFFF;
} }