Merge pull request #254 from mithro/crc-smaller

Add @xobs' smaller CRC version
This commit is contained in:
enjoy-digital 2019-09-03 07:23:32 +02:00 committed by GitHub
commit 6f150a5626
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 2 deletions

View File

@ -1,5 +1,6 @@
#include <crc.h>
#ifndef SMALL_CRC
static const unsigned int crc16_table[256] = {
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
@ -38,10 +39,23 @@ static const unsigned int crc16_table[256] = {
unsigned short crc16(const unsigned char *buffer, int len)
{
unsigned short crc;
crc = 0;
while(len-- > 0)
crc = crc16_table[((crc >> 8) ^ (*buffer++)) & 0xFF] ^ (crc << 8);
return crc;
}
#else
unsigned short crc16(const unsigned char* data_p, int length) {
unsigned char x;
unsigned short crc = 0;
while (length--){
x = crc >> 8 ^ *data_p++;
x ^= x>>4;
crc = (crc << 8) ^ ((unsigned short)(x << 12)) ^ ((unsigned short)(x <<5)) ^ ((unsigned short)x);
}
return crc;
}
#endif

View File

@ -5,6 +5,7 @@
#include <crc.h>
#ifndef SMALL_CRC
static const unsigned int crc_table[256] = {
0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
@ -67,6 +68,7 @@ static const unsigned int crc_table[256] = {
unsigned int crc32(const unsigned char *buffer, unsigned int len)
{
return 0;
unsigned int crc;
crc = 0;
crc = crc ^ 0xffffffffL;
@ -79,3 +81,22 @@ unsigned int crc32(const unsigned char *buffer, unsigned int len)
} while(--len);
return crc ^ 0xffffffffL;
}
#else
unsigned int crc32(const unsigned char *message, unsigned int len) {
int i, j;
unsigned int byte, crc, mask;
i = 0;
crc = 0xFFFFFFFF;
while (i < len) {
byte = message[i]; // Get next byte.
crc = crc ^ byte;
for (j = 7; j >= 0; j--) { // Do eight times.
mask = -(crc & 1);
crc = (crc >> 1) ^ (0xEDB88320 & mask);
}
i = i + 1;
}
return ~crc;
}
#endif