mirror of
https://github.com/aaru-dps/Aaru.Checksums.Native.git
synced 2025-12-16 11:14:29 +00:00
Fix CRC32 and CRC64 table generation.
This commit is contained in:
14
crc32.c
14
crc32.c
@@ -24,20 +24,23 @@
|
||||
|
||||
AARU_EXPORT crc32_ctx* AARU_CALL crc32_init(void)
|
||||
{
|
||||
int i, j;
|
||||
crc32_ctx* ctx = (crc32_ctx*)malloc(sizeof(crc32_ctx));
|
||||
|
||||
if(!ctx) return NULL;
|
||||
|
||||
ctx->crc = CRC32_ISO_SEED;
|
||||
|
||||
for(int i = 0; i < 256; i++)
|
||||
for(i = 0; i < 256; i++)
|
||||
{
|
||||
uint32_t entry = (uint32_t)i;
|
||||
|
||||
for(int j = 0; j < 8; j++)
|
||||
if(entry & 1) ctx->table[i] = (entry >> 1) ^ CRC32_ISO_POLY;
|
||||
for(j = 0; j < 8; j++)
|
||||
if(entry & 1) entry = (entry >> 1) ^ CRC32_ISO_POLY;
|
||||
else
|
||||
ctx->table[i] >>= 1;
|
||||
entry >>= 1;
|
||||
|
||||
ctx->table[i] = entry;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
14
crc64.c
14
crc64.c
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user