Initial commit

This commit is contained in:
Chris 2023-12-19 03:01:07 +01:00
commit 4fd5bc05c9
8 changed files with 172 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*.o
*.a
crctest

19
Makefile Normal file
View File

@ -0,0 +1,19 @@
.PHONY: all
all: tinycrc.a crctest
.PHONY: clean
clean:
rm -f crctest.cpp.o tinycrc.a
$(MAKE) -C src clean
crctest: crctest.cpp.o tinycrc.a
gcc -g -o $@ $< tinycrc.a
tinycrc.a:
$(MAKE) -C src tinycrc.a
cp src/tinycrc.a .
%.cpp.o: %.cpp
gcc -c -o $@ $<

46
crctest.cpp Normal file
View File

@ -0,0 +1,46 @@
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <cstring>
#include "src/tinycrc.h"
int main()
{
uint8_t* buffer = (uint8_t*)malloc(32);
sprintf((char*)buffer, "Hello %s!", "World");
uint32_t crc32 = tinycrc_crc32(buffer, 32);
uint16_t crc16 = tinycrc_crc16(buffer, 32);
uint8_t crc8 = tinycrc_crc8(buffer, 32);
printf("%-32s | crc32 | %08x\n", (char*)buffer, crc32);
printf("%-32s | crc16 | %04x\n", (char*)buffer, crc16);
printf("%-32s | crc8 | %02x\n", (char*)buffer, crc8);
sprintf((char*)buffer, "Hello %s!", "Worlds");
crc32 = tinycrc_crc32(buffer, 32);
crc16 = tinycrc_crc16(buffer, 32);
crc8 = tinycrc_crc8(buffer, 32);
printf("%-32s | crc32 | %08x\n", (char*)buffer, crc32);
printf("%-32s | crc16 | %04x\n", (char*)buffer, crc16);
printf("%-32s | crc8 | %02x\n", (char*)buffer, crc8);
sprintf((char*)buffer, "%s %d", "foobarxyzzy", 42);
crc32 = tinycrc_crc32(buffer, 32);
crc16 = tinycrc_crc16(buffer, 32);
crc8 = tinycrc_crc8(buffer, 32);
printf("%-32s | crc32 | %08x\n", (char*)buffer, crc32);
printf("%-32s | crc16 | %04x\n", (char*)buffer, crc16);
printf("%-32s | crc8 | %02x\n", (char*)buffer, crc8);
free(buffer);
}

15
src/Makefile Normal file
View File

@ -0,0 +1,15 @@
OBJS=crc32.o crc16.o crc8.o
.PHONY: all
all: tinycrc.a
.PHONY: clean
clean:
rm -f tinycrc.a $(OBJS)
%.o: %.cpp
g++ -g -c -o $@ $<
tinycrc.a: $(OBJS)
ar qc $@ $(OBJS)

22
src/crc16.cpp Normal file
View File

@ -0,0 +1,22 @@
#include "tinycrc.h"
/*
CRC16
*/
uint16_t tinycrc_crc16(const uint8_t *data, uint16_t size)
{
unsigned char x;
unsigned short crc = 0xFFFF;
if (data == nullptr) {
return 0;
}
for (int index = 0 ; index < size ; ++index) {
x = crc >> 8 ^ *data++;
x ^= x>>4;
crc = (crc << 8) ^ ((unsigned short)(x << 12)) ^ ((unsigned short)(x <<5)) ^ ((unsigned short)x);
}
return crc & 0xFFFF;
}

33
src/crc32.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <stdio.h>
#include <ctype.h>
#include "tinycrc.h"
/*
CRC32
*/
uint32_t tinycrc_crc32(const uint8_t* data, uint16_t size)
{
const unsigned long crc_table[16] = {
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
};
if (data == nullptr) {
return 0;
}
unsigned long crc = ~0L;
for (int index = 0 ; index < size ; ++index) {
crc = crc_table[(crc ^ data[index]) & 0x0f] ^ (crc >> 4);
crc = crc_table[(crc ^ (data[index] >> 4)) & 0x0f] ^ (crc >> 4);
crc = ~crc;
}
return crc & 0xFFFFFFFF;
};

26
src/crc8.cpp Normal file
View File

@ -0,0 +1,26 @@
#include "tinycrc.h"
/*
CRC8 (from https://devcoons.com/crc8/)
*/
uint8_t tinycrc_crc8(const uint8_t* data, uint16_t length)
{
char crc = 0x00;
char extract;
char sum;
for(int i=0;i<length;i++)
{
extract = *data;
for (char tempI = 8; tempI; tempI--)
{
sum = (crc ^ extract) & 0x01;
crc >>= 1;
if (sum)
crc ^= 0x8C;
extract >>= 1;
}
data++;
}
return crc;
}

8
src/tinycrc.h Normal file
View File

@ -0,0 +1,8 @@
#pragma once
#include <stdint.h>
uint32_t tinycrc_crc32(const uint8_t* data, uint16_t size);
uint16_t tinycrc_crc16(const uint8_t* data, uint16_t size);
uint8_t tinycrc_crc8(const uint8_t* data, uint16_t length);