crc32
This commit is contained in:
parent
7383d6e0f2
commit
8f4ea26303
4 changed files with 147 additions and 0 deletions
33
crc32/crc32_nolookup.h
Normal file
33
crc32/crc32_nolookup.h
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#ifndef CRC32_H
|
||||
#define CRC32_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
|
||||
Loading…
Add table
Add a link
Reference in a new issue