From 9d00435b42ef7173099397f7a4bd1f4ded1a96e1 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Mon, 19 Aug 2013 12:45:12 +0200 Subject: [PATCH] Uses the CRC_IV directly in the crc32c() function. --- selftest.c | 2 +- state.c | 2 +- util.c | 24 +++++++++++++++++++----- util.h | 6 ------ 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/selftest.c b/selftest.c index 61552686..a5a01c4e 100644 --- a/selftest.c +++ b/selftest.c @@ -262,7 +262,7 @@ static void crc32ctest(void) for(i=0;TEST_CRC32C[i].data;++i) { uint32_t digest; - digest = CRC_IV ^ crc32c(CRC_IV, (const unsigned char*)TEST_CRC32C[i].data, TEST_CRC32C[i].len); + digest = crc32c(0, (const unsigned char*)TEST_CRC32C[i].data, TEST_CRC32C[i].len); if (digest != TEST_CRC32C[i].digest) { fprintf(stderr, "Failed CRC32C test vector %u\n", i); exit(EXIT_FAILURE); diff --git a/state.c b/state.c index 3ad30ebc..afec0cd1 100644 --- a/state.c +++ b/state.c @@ -2103,7 +2103,7 @@ static void decoding_error(const char* path, STREAM* f) crc_computed = scrc(f); /* adjust the stored crc to include itself */ - crc_stored = crc32c(crc_stored ^ CRC_IV, buf, 4) ^ CRC_IV; + crc_stored = crc32c(crc_stored, buf, 4); if (crc_computed != crc_stored) { fprintf(stderr, "Mismatching CRC in '%s'\n", path); diff --git a/util.c b/util.c index 4d343c9c..3523004c 100644 --- a/util.c +++ b/util.c @@ -84,8 +84,8 @@ STREAM* sopen_read(const char* file) s->state_index = 0; s->offset = 0; s->offset_uncached = 0; - s->crc = CRC_IV; - s->crc_uncached = CRC_IV; + s->crc = 0; + s->crc_uncached = 0; return s; } @@ -109,8 +109,8 @@ STREAM* sopen_multi_write(unsigned count) s->state_index = 0; s->offset = 0; s->offset_uncached = 0; - s->crc = CRC_IV; - s->crc_uncached = CRC_IV; + s->crc = 0; + s->crc_uncached = 0; return s; } @@ -251,7 +251,7 @@ int64_t stell(STREAM* s) uint32_t scrc(STREAM*s) { - return crc32c(s->crc_uncached, s->buffer, s->pos - s->buffer) ^ CRC_IV; + return crc32c(s->crc_uncached, s->buffer, s->pos - s->buffer); } int sgettok(STREAM* f, char* str, int size) @@ -1061,8 +1061,16 @@ static uint32_t CRC32C_3[256] = { 0x4a21617b, 0x9764cbc3, 0xf54642fa, 0x2803e842 }; +/** + * CRC initial value. + * Using a not zero value allows to detect a leading run of zeros. + */ +#define CRC_IV 0xffffffffU + static uint32_t crc32c_gen(uint32_t crc, const unsigned char* ptr, unsigned size) { + crc ^= CRC_IV; + while (size >= 4) { crc ^= ptr[0] | (uint32_t)ptr[1] << 8 | (uint32_t)ptr[2] << 16 | (uint32_t)ptr[3] << 24; crc = CRC32C_3[crc & 0xff] ^ CRC32C_2[(crc >> 8) & 0xff] ^ CRC32C_1[(crc >> 16) & 0xff] ^ CRC32C_0[crc >> 24]; @@ -1076,6 +1084,8 @@ static uint32_t crc32c_gen(uint32_t crc, const unsigned char* ptr, unsigned size --size; } + crc ^= CRC_IV; + return crc; } @@ -1083,6 +1093,8 @@ static uint32_t crc32c_gen(uint32_t crc, const unsigned char* ptr, unsigned size static uint32_t crc32c_x86(uint32_t crc, const unsigned char* ptr, unsigned size) { + crc ^= CRC_IV; + while (size >= 4) { asm volatile ("crc32l %1, %0\n" : "+r" (crc) : "rm" (*(uint32_t*)ptr)); ptr += 4; @@ -1095,6 +1107,8 @@ static uint32_t crc32c_x86(uint32_t crc, const unsigned char* ptr, unsigned size --size; } + crc ^= CRC_IV; + return crc; } diff --git a/util.h b/util.h index 7938e653..9821a793 100644 --- a/util.h +++ b/util.h @@ -496,12 +496,6 @@ void randomize(void* ptr, unsigned size); /****************************************************************************/ /* crc */ -/** - * CRC initial value. - * Using a not zero value allows to detect trailing zeros at the end. - */ -#define CRC_IV 0xffffffffU - /** * Computes the CRC-32 (Castagnoli) */