3 // CRC code from http://www.hackersdelight.org/hdcodetxt/crc.c.txt
5 // Reverses (reflects) bits in a 32-bit word.
6 unsigned reverse(unsigned x
) {
7 x
= ((x
& 0x55555555) << 1) | ((x
>> 1) & 0x55555555);
8 x
= ((x
& 0x33333333) << 2) | ((x
>> 2) & 0x33333333);
9 x
= ((x
& 0x0F0F0F0F) << 4) | ((x
>> 4) & 0x0F0F0F0F);
10 x
= (x
<< 24) | ((x
& 0xFF00) << 8) |
11 ((x
>> 8) & 0xFF00) | (x
>> 24);
15 // ----------------------------- crc32a --------------------------------
17 /* This is the basic CRC algorithm with no optimizations. It follows the
18 logic circuit as closely as possible. */
20 unsigned int crc32a(uint8_t *message
, unsigned int size
) {
22 unsigned int byte
, crc
;
27 byte
= message
[i
]; // Get next byte.
28 byte
= reverse(byte
); // 32-bit reversal.
29 for (j
= 0; j
<= 7; j
++) { // Do eight times.
30 if ((int)(crc
^ byte
) < 0)
31 crc
= (crc
<< 1) ^ 0x04C11DB7;
33 byte
= byte
<< 1; // Ready next msg bit.
41 extern uint32_t length
;
45 /* Compute a simple checksum. */
46 return crc32a(data
, length
);