Fix CRC32 and CRC64 table generation.

This commit is contained in:
2021-09-22 04:09:39 +01:00
parent c17752dbf0
commit 7553a3ce40
2 changed files with 18 additions and 10 deletions

14
crc32.c
View File

@@ -24,20 +24,23 @@
AARU_EXPORT crc32_ctx* AARU_CALL crc32_init(void) AARU_EXPORT crc32_ctx* AARU_CALL crc32_init(void)
{ {
int i, j;
crc32_ctx* ctx = (crc32_ctx*)malloc(sizeof(crc32_ctx)); crc32_ctx* ctx = (crc32_ctx*)malloc(sizeof(crc32_ctx));
if(!ctx) return NULL; if(!ctx) return NULL;
ctx->crc = CRC32_ISO_SEED; ctx->crc = CRC32_ISO_SEED;
for(int i = 0; i < 256; i++) for(i = 0; i < 256; i++)
{ {
uint32_t entry = (uint32_t)i; uint32_t entry = (uint32_t)i;
for(int j = 0; j < 8; j++) for(j = 0; j < 8; j++)
if(entry & 1) ctx->table[i] = (entry >> 1) ^ CRC32_ISO_POLY; if(entry & 1) entry = (entry >> 1) ^ CRC32_ISO_POLY;
else else
ctx->table[i] >>= 1; entry >>= 1;
ctx->table[i] = entry;
} }
return ctx; return ctx;
@@ -45,9 +48,10 @@ AARU_EXPORT crc32_ctx* AARU_CALL crc32_init(void)
AARU_EXPORT int AARU_CALL crc32_update(crc32_ctx* ctx, const uint8_t* data, uint32_t len) AARU_EXPORT int AARU_CALL crc32_update(crc32_ctx* ctx, const uint8_t* data, uint32_t len)
{ {
uint32_t i;
if(!ctx || !data) return -1; if(!ctx || !data) return -1;
for(uint32_t i = 0; i < len; i++) ctx->crc = (ctx->crc >> 8) ^ ctx->table[data[i] ^ (ctx->crc & 0xff)]; for( i = 0; i < len; i++) ctx->crc = (ctx->crc >> 8) ^ ctx->table[data[i] ^ (ctx->crc & 0xff)];
return 0; return 0;
} }

14
crc64.c
View File

@@ -24,20 +24,23 @@
AARU_EXPORT crc64_ctx* AARU_CALL crc64_init(void) AARU_EXPORT crc64_ctx* AARU_CALL crc64_init(void)
{ {
int i, j;
crc64_ctx* ctx = (crc64_ctx*)malloc(sizeof(crc64_ctx)); crc64_ctx* ctx = (crc64_ctx*)malloc(sizeof(crc64_ctx));
if(!ctx) return NULL; if(!ctx) return NULL;
ctx->crc = CRC64_ECMA_SEED; ctx->crc = CRC64_ECMA_SEED;
for(int i = 0; i < 256; i++) for(i = 0; i < 256; i++)
{ {
uint64_t entry = (uint64_t)i; uint64_t entry = (uint64_t)i;
for(int j = 0; j < 8; j++) for(j = 0; j < 8; j++)
if(entry & 1) ctx->table[i] = (entry >> 1) ^ CRC64_ECMA_POLY; if(entry & 1) entry = (entry >> 1) ^ CRC64_ECMA_POLY;
else else
ctx->table[i] >>= 1; entry >>= 1;
ctx->table[i]=entry;
} }
return ctx; return ctx;
@@ -45,9 +48,10 @@ AARU_EXPORT crc64_ctx* AARU_CALL crc64_init(void)
AARU_EXPORT int AARU_CALL crc64_update(crc64_ctx* ctx, const uint8_t* data, uint32_t len) AARU_EXPORT int AARU_CALL crc64_update(crc64_ctx* ctx, const uint8_t* data, uint32_t len)
{ {
uint32_t i;
if(!ctx || !data) return -1; if(!ctx || !data) return -1;
for(uint32_t i = 0; i < len; i++) ctx->crc = (ctx->crc >> 8) ^ ctx->table[data[i] ^ (ctx->crc & 0xff)]; for( i = 0; i < len; i++) ctx->crc = (ctx->crc >> 8) ^ ctx->table[data[i] ^ (ctx->crc & 0xff)];
return 0; return 0;
} }