33 lines
691 B
C
33 lines
691 B
C
#ifndef CRC32_NOLOOKUP_H
|
|
#define CRC32_NOLOOKUP_H
|
|
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
/*
|
|
* CRC-32 (IEEE 802.3) implementation
|
|
* Polynomial: 0xEDB88320 (reflected)
|
|
* Initial value: 0xFFFFFFFF
|
|
* Final XOR: 0xFFFFFFFF
|
|
* Input reflected: Yes
|
|
* Output reflected: Yes
|
|
*/
|
|
|
|
static inline uint32_t crc32_ieee(const void *data, size_t len) {
|
|
const uint8_t *p = (const uint8_t *)data;
|
|
uint32_t crc = 0xFFFFFFFF;
|
|
|
|
while (len--) {
|
|
crc ^= *p++;
|
|
for (int i = 0; i < 8; i++) {
|
|
if (crc & 1)
|
|
crc = (crc >> 1) ^ 0xEDB88320;
|
|
else
|
|
crc >>= 1;
|
|
}
|
|
}
|
|
|
|
return crc ^ 0xFFFFFFFF;
|
|
}
|
|
|
|
#endif // CRC32_H
|