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
crc64.c
View File

@@ -24,20 +24,23 @@
AARU_EXPORT crc64_ctx* AARU_CALL crc64_init(void)
{
int i, j;
crc64_ctx* ctx = (crc64_ctx*)malloc(sizeof(crc64_ctx));
if(!ctx) return NULL;
ctx->crc = CRC64_ECMA_SEED;
for(int i = 0; i < 256; i++)
for(i = 0; i < 256; i++)
{
uint64_t entry = (uint64_t)i;
for(int j = 0; j < 8; j++)
if(entry & 1) ctx->table[i] = (entry >> 1) ^ CRC64_ECMA_POLY;
for(j = 0; j < 8; j++)
if(entry & 1) entry = (entry >> 1) ^ CRC64_ECMA_POLY;
else
ctx->table[i] >>= 1;
entry >>= 1;
ctx->table[i]=entry;
}
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)
{
uint32_t i;
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;
}