Skip to content

Commit

Permalink
fix reference impl
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyMusatkin committed Oct 7, 2024
1 parent 4853bc6 commit 2fb5fcc
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions tests/crc_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,34 @@ typedef uint32_t(crc_fn)(const uint8_t *input, int length, uint32_t previousCrc3

// Slow reference implementation that computes a 32-bit bit-reflected/bit-inverted CRC using the provided polynomial.
static uint32_t s_crc_32_reference(const uint8_t *input, int length, const uint32_t previousCrc, uint32_t polynomial) {

uint32_t crc = ~previousCrc;
while (length-- > 0) {
crc ^= *input++;
for (int j = 8; j > 0; --j) {
crc = (crc >> 1) ^ ((((crc & 1) ^ 1) - 1) & polynomial);
if (aws_is_big_endian()) {
uint32_t crc = 0xFFFFFFFF;

for (size_t i = 0; i < length; i++) {
uint8_t byte = input[i];

// Process each bit of the byte, starting from MSB
for (int j = 7; j >= 0; j--) {
uint32_t bit = (byte >> j) & 1;
uint32_t c = (crc >> 31) & 1;
crc = crc << 1;
if (c ^ bit) {
crc = crc ^ polynomial;
}
}
}

return ~crc;
} else {
uint32_t crc = ~previousCrc;
while (length-- > 0) {
crc ^= *input++;
for (int j = 8; j > 0; --j) {
crc = (crc >> 1) ^ ((((crc & 1) ^ 1) - 1) & polynomial);
}
}
return ~crc;
}
return ~crc;
}

// Very, very slow reference implementation that computes a CRC32.
Expand Down

0 comments on commit 2fb5fcc

Please sign in to comment.