Skip to content

Commit

Permalink
fix crc test
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyMusatkin committed Oct 7, 2024
1 parent 60dab57 commit 0697afb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
14 changes: 14 additions & 0 deletions include/aws/checksums/private/crc_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,18 @@
return val; \
}

/* helper function to reverse byte order on big-endian platforms*/
static inline uint32_t aws_swap_bytes_if_needed_32(uint32_t x) {
if (!aws_is_big_endian()) {
return x;
}

uint8_t c1 = x & 0xFF;
uint8_t c2 = (x >> 8) & 0xFF;
uint8_t c3 = (x >> 16) & 0xFF;
uint8_t c4 = (x >> 24) & 0xFF;

return ((uint32_t)c1 << 24) + ((uint32_t)c2 << 16) + ((uint32_t)c3 << 8) + c4;
}

#endif /* AWS_CHECKSUMS_PRIVATE_CRC_UTIL_H */
28 changes: 7 additions & 21 deletions source/crc_sw.c
Original file line number Diff line number Diff line change
Expand Up @@ -1106,20 +1106,6 @@ const uint32_t CRC32C_TABLE[16][256] = {
0x5A26B1E2, 0xA82ABC1C, 0xBBD2DCEF, 0x49DED111, 0x9C221D09, 0x6E2E10F7, 0x7DD67004, 0x8FDA7DFA /* [15][0x100]*/
}};

/* helper function to reverse byte order on big-endian platforms*/
static inline uint32_t swap_bytes_if_needed(uint32_t x) {
if (!aws_is_big_endian()) {
return x;
}

uint8_t c1 = x & 0xFF;
uint8_t c2 = (x >> 8) & 0xFF;
uint8_t c3 = (x >> 16) & 0xFF;
uint8_t c4 = (x >> 24) & 0xFF;

return ((uint32_t)c1 << 24) + ((uint32_t)c2 << 16) + ((uint32_t)c3 << 8) + c4;
}

/* private (static) function factoring out byte-by-byte CRC computation using just one slice of the lookup table*/
static uint32_t s_crc_generic_sb1(const uint8_t *input, int length, uint32_t crc, const uint32_t *table_ptr) {
uint32_t(*table)[16][256] = (uint32_t(*)[16][256])table_ptr;
Expand Down Expand Up @@ -1164,7 +1150,7 @@ static uint32_t s_crc_generic_sb4(const uint8_t *input, int length, uint32_t crc
uint32_t(*table)[16][256] = (uint32_t(*)[16][256])table_ptr;

while (remaining >= 4) {
crc ^= swap_bytes_if_needed(*current++);
crc ^= aws_swap_bytes_if_needed_32(*current++);
crc = (*table)[3][crc & 0xff] ^ (*table)[2][(crc >> 8) & 0xff] ^ (*table)[1][(crc >> 16) & 0xff] ^
(*table)[0][crc >> 24];
remaining -= 4;
Expand All @@ -1180,8 +1166,8 @@ static uint32_t s_crc_generic_sb8(const uint8_t *input, int length, uint32_t crc
uint32_t(*table)[16][256] = (uint32_t(*)[16][256])table_ptr;

while (remaining >= 8) {
uint32_t c1 = swap_bytes_if_needed(*current++) ^ crc;
uint32_t c2 = swap_bytes_if_needed(*current++);
uint32_t c1 = aws_swap_bytes_if_needed_32(*current++) ^ crc;
uint32_t c2 = aws_swap_bytes_if_needed_32(*current++);
uint32_t t1 = (*table)[7][c1 & 0xff] ^ (*table)[6][(c1 >> 8) & 0xff] ^ (*table)[5][(c1 >> 16) & 0xff] ^
(*table)[4][(c1 >> 24) & 0xff];
uint32_t t2 = (*table)[3][c2 & 0xff] ^ (*table)[2][(c2 >> 8) & 0xff] ^ (*table)[1][(c2 >> 16) & 0xff] ^
Expand All @@ -1200,10 +1186,10 @@ static uint32_t s_crc_generic_sb16(const uint8_t *input, int length, uint32_t cr
uint32_t(*table)[16][256] = (uint32_t(*)[16][256])table_ptr;

while (remaining >= 16) {
uint32_t c1 = swap_bytes_if_needed(*current++) ^ crc;
uint32_t c2 = swap_bytes_if_needed(*current++);
uint32_t c3 = swap_bytes_if_needed(*current++);
uint32_t c4 = swap_bytes_if_needed(*current++);
uint32_t c1 = aws_swap_bytes_if_needed_32(*current++) ^ crc;
uint32_t c2 = aws_swap_bytes_if_needed_32(*current++);
uint32_t c3 = aws_swap_bytes_if_needed_32(*current++);
uint32_t c4 = aws_swap_bytes_if_needed_32(*current++);
uint32_t t1 = (*table)[15][c1 & 0xff] ^ (*table)[14][(c1 >> 8) & 0xff] ^ (*table)[13][(c1 >> 16) & 0xff] ^
(*table)[12][(c1 >> 24) & 0xff];
uint32_t t2 = (*table)[11][c2 & 0xff] ^ (*table)[10][(c2 >> 8) & 0xff] ^ (*table)[9][(c2 >> 16) & 0xff] ^
Expand Down
4 changes: 2 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ file(GLOB TEST_SRC "*.c")

file(GLOB TESTS ${TEST_HDRS} ${TEST_SRC})

#add_test_case(test_crc32c)
add_test_case(test_crc32c)
add_test_case(test_crc32)
#add_test_case(test_large_buffer_crc32)
add_test_case(test_large_buffer_crc32)
#add_test_case(test_crc64nvme)
#add_test_case(test_large_buffer_crc64)

Expand Down
6 changes: 4 additions & 2 deletions tests/crc_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <aws/checksums/crc.h>
#include <aws/checksums/private/crc_priv.h>
#include <aws/checksums/private/crc_util.h>

#include <aws/common/device_random.h>

Expand Down Expand Up @@ -72,9 +73,10 @@ static int s_test_known_crc_32(
uint32_t result = func(input, (int)length, 0);
ASSERT_HEX_EQUALS(expected_crc, result, "%s(%s)", func_name, data_name);

uint32_t result_swapped = aws_swap_bytes_if_needed_32(result);
// Compute the residue of the buffer (the CRC of the buffer plus its CRC) - will always be a constant value
// uint32_t residue = (uint32_t)func((const uint8_t *)&result, 4, result); // assuming little endian
//ASSERT_HEX_EQUALS(expected_residue, residue, "len %d residue %s(%s)", length, func_name, data_name);
uint32_t residue = (uint32_t)func((const uint8_t *)&result_swapped, 4, result); // assuming little endian
ASSERT_HEX_EQUALS(expected_residue, residue, "len %d residue %s(%s)", length, func_name, data_name);

// chain the crc computation so 2 calls each operate on about 1/2 of the buffer
uint32_t crc1 = func(input, (int)(length / 2), 0);
Expand Down

0 comments on commit 0697afb

Please sign in to comment.