diff --git a/common/omv_crc.c b/common/omv_crc.c index 02b3ef312..6ed143fb4 100644 --- a/common/omv_crc.c +++ b/common/omv_crc.c @@ -60,7 +60,7 @@ static const uint16_t crc16_table[256] = { }; // CRC32 lookup table for polynomial 0xFA567D89 -const uint32_t crc32_table[256] = { +static const uint32_t crc32_table[256] = { 0x00000000, 0xFA567D89, 0x0EFA869B, 0xF4ACFB12, 0x1DF50D36, 0xE7A370BF, 0x130F8BAD, 0xE959F624, 0x3BEA1A6C, 0xC1BC67E5, 0x35109CF7, 0xCF46E17E, 0x261F175A, 0xDC496AD3, 0x28E591C1, 0xD2B3EC48, 0x77D434D8, 0x8D824951, 0x792EB243, 0x8378CFCA, 0x6A2139EE, 0x90774467, 0x64DBBF75, 0x9E8DC2FC, diff --git a/ports/alif/omv_crc.c b/ports/alif/omv_crc.c index 527c63fb1..cb523d5fc 100644 --- a/ports/alif/omv_crc.c +++ b/ports/alif/omv_crc.c @@ -28,8 +28,6 @@ #define CRC0 ((CRC_Type *) CRC0_BASE) static bool crc32_initialized = false; -extern const uint32_t crc32_table[256]; - static void crc_calculate(CRC_Type *crc, const void *buf, uint32_t len, uint32_t *value); static void omv_crc32_init(void) { @@ -89,14 +87,20 @@ static void crc_calculate(CRC_Type *crc, const void *buf, uint32_t len, uint32_t *value = crc->CRC_OUT; } - // Process remaining bytes using software CRC with lookup table + // Process remaining bytes using software CRC calculation if (remainder > 0) { uint32_t result = *value; const uint8_t *remaining_data = data + aligned_len; for (uint32_t i = 0; i < remainder; i++) { - uint8_t index = (result >> 24) ^ remaining_data[i]; - result = (result << 8) ^ crc32_table[index]; + result ^= (uint32_t) remaining_data[i] << 24; + for (int bit = 0; bit < 8; bit++) { + if (result & 0x80000000) { + result = (result << 1) ^ OMV_CRC32_POLY; + } else { + result <<= 1; + } + } } *value = result; }