diff --git a/selftest.c b/selftest.c index 14f10940..61552686 100644 --- a/selftest.c +++ b/selftest.c @@ -236,6 +236,7 @@ struct crc_test_vector { * Test vectors for CRC32C (Castagnoli) */ static struct crc_test_vector TEST_CRC32C[] = { +{ "", 0, 0 }, { "\x61", 1, 0xc1d04330 }, { "\x66\x6f\x6f", 3, 0xcfc4ae1d }, { "\x68\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64", 11, 0xc99465aa }, @@ -261,7 +262,7 @@ static void crc32ctest(void) for(i=0;TEST_CRC32C[i].data;++i) { uint32_t digest; - digest = 0xffffffffU ^ crc32c(0xffffffffU, (const unsigned char*)TEST_CRC32C[i].data, TEST_CRC32C[i].len); + digest = CRC_IV ^ crc32c(CRC_IV, (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 afec0cd1..3ad30ebc 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, buf, 4); + crc_stored = crc32c(crc_stored ^ CRC_IV, buf, 4) ^ CRC_IV; if (crc_computed != crc_stored) { fprintf(stderr, "Mismatching CRC in '%s'\n", path); diff --git a/util.c b/util.c index 15faf912..4d343c9c 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 = 0; - s->crc_uncached = 0; + s->crc = CRC_IV; + s->crc_uncached = CRC_IV; 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 = 0; - s->crc_uncached = 0; + s->crc = CRC_IV; + s->crc_uncached = CRC_IV; 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); + return crc32c(s->crc_uncached, s->buffer, s->pos - s->buffer) ^ CRC_IV; } int sgettok(STREAM* f, char* str, int size) diff --git a/util.h b/util.h index 9821a793..7938e653 100644 --- a/util.h +++ b/util.h @@ -496,6 +496,12 @@ 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) */