Uses the CRC_IV directly in the crc32c() function.

This commit is contained in:
Andrea Mazzoleni 2013-08-19 12:45:12 +02:00
parent 5136eb3719
commit 9d00435b42
4 changed files with 21 additions and 13 deletions

View file

@ -262,7 +262,7 @@ static void crc32ctest(void)
for(i=0;TEST_CRC32C[i].data;++i) { for(i=0;TEST_CRC32C[i].data;++i) {
uint32_t digest; 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) { if (digest != TEST_CRC32C[i].digest) {
fprintf(stderr, "Failed CRC32C test vector %u\n", i); fprintf(stderr, "Failed CRC32C test vector %u\n", i);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);

View file

@ -2103,7 +2103,7 @@ static void decoding_error(const char* path, STREAM* f)
crc_computed = scrc(f); crc_computed = scrc(f);
/* adjust the stored crc to include itself */ /* 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) { if (crc_computed != crc_stored) {
fprintf(stderr, "Mismatching CRC in '%s'\n", path); fprintf(stderr, "Mismatching CRC in '%s'\n", path);

24
util.c
View file

@ -84,8 +84,8 @@ STREAM* sopen_read(const char* file)
s->state_index = 0; s->state_index = 0;
s->offset = 0; s->offset = 0;
s->offset_uncached = 0; s->offset_uncached = 0;
s->crc = CRC_IV; s->crc = 0;
s->crc_uncached = CRC_IV; s->crc_uncached = 0;
return s; return s;
} }
@ -109,8 +109,8 @@ STREAM* sopen_multi_write(unsigned count)
s->state_index = 0; s->state_index = 0;
s->offset = 0; s->offset = 0;
s->offset_uncached = 0; s->offset_uncached = 0;
s->crc = CRC_IV; s->crc = 0;
s->crc_uncached = CRC_IV; s->crc_uncached = 0;
return s; return s;
} }
@ -251,7 +251,7 @@ int64_t stell(STREAM* s)
uint32_t scrc(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) int sgettok(STREAM* f, char* str, int size)
@ -1061,8 +1061,16 @@ static uint32_t CRC32C_3[256] = {
0x4a21617b, 0x9764cbc3, 0xf54642fa, 0x2803e842 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) static uint32_t crc32c_gen(uint32_t crc, const unsigned char* ptr, unsigned size)
{ {
crc ^= CRC_IV;
while (size >= 4) { while (size >= 4) {
crc ^= ptr[0] | (uint32_t)ptr[1] << 8 | (uint32_t)ptr[2] << 16 | (uint32_t)ptr[3] << 24; 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]; 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; --size;
} }
crc ^= CRC_IV;
return crc; 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) static uint32_t crc32c_x86(uint32_t crc, const unsigned char* ptr, unsigned size)
{ {
crc ^= CRC_IV;
while (size >= 4) { while (size >= 4) {
asm volatile ("crc32l %1, %0\n" : "+r" (crc) : "rm" (*(uint32_t*)ptr)); asm volatile ("crc32l %1, %0\n" : "+r" (crc) : "rm" (*(uint32_t*)ptr));
ptr += 4; ptr += 4;
@ -1095,6 +1107,8 @@ static uint32_t crc32c_x86(uint32_t crc, const unsigned char* ptr, unsigned size
--size; --size;
} }
crc ^= CRC_IV;
return crc; return crc;
} }

6
util.h
View file

@ -496,12 +496,6 @@ void randomize(void* ptr, unsigned size);
/****************************************************************************/ /****************************************************************************/
/* crc */ /* 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) * Computes the CRC-32 (Castagnoli)
*/ */