]>
Commit | Line | Data |
---|---|---|
f38a1528 | 1 | #include "crc32.h" |
2 | ||
3 | #define htole32(x) (x) | |
4 | #define CRC32_PRESET 0xFFFFFFFF | |
5 | ||
f38a1528 | 6 | static void crc32_byte (uint32_t *crc, const uint8_t value); |
7 | ||
8 | static void crc32_byte (uint32_t *crc, const uint8_t value) { | |
9 | /* x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1 */ | |
10 | const uint32_t poly = 0xEDB88320; | |
11 | ||
12 | *crc ^= value; | |
13 | for (int current_bit = 7; current_bit >= 0; current_bit--) { | |
14 | int bit_out = (*crc) & 0x00000001; | |
15 | *crc >>= 1; | |
16 | if (bit_out) | |
17 | *crc ^= poly; | |
18 | } | |
19 | } | |
20 | ||
e36b07ef | 21 | void crc32_ex (const uint8_t *data, const size_t len, uint8_t *crc) { |
f38a1528 | 22 | uint32_t desfire_crc = CRC32_PRESET; |
23 | for (size_t i = 0; i < len; i++) { | |
24 | crc32_byte (&desfire_crc, data[i]); | |
25 | } | |
26 | ||
27 | *((uint32_t *)(crc)) = htole32 (desfire_crc); | |
28 | } | |
29 | ||
30 | void crc32_append (uint8_t *data, const size_t len) { | |
e36b07ef | 31 | crc32_ex (data, len, data + len); |
1b75698c | 32 | } |