MySensors Library & Examples  2.3.2-62-ge298769
PJON_CRC32.h
1 #pragma once
2 
3 /* CRC32 table-less implementation
4  See: http://www.hackersdelight.org/hdcodetxt/crc.c.txt
5 
6  Polynomial
7 
8  0x82608edb = x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1
9  (0x82608edb; 0x104c11db7) <=> (0xedb88320; 0x1db710641)
10  |
11  bit-reversed polynomial implicit +1 notation
12  or reverse reciprocal notation */
13 
14 struct PJON_crc32 {
15 
16  static inline uint32_t compute(
17  const uint8_t *data,
18  uint16_t length,
19  uint32_t previousCrc32 = 0
20  )
21  {
22  uint8_t bits;
23  uint32_t crc = ~previousCrc32; // same as previousCrc32 ^ 0xFFFFFFFF
24  uint8_t * current = (uint8_t*) data;
25  while(length--) {
26  crc ^= *current++;
27  bits = 8;
28  while(bits--) {
29  if(crc & 1) {
30  crc = (crc >> 1) ^ 0xEDB88320;
31  } else {
32  crc = crc >> 1;
33  }
34  }
35  }
36  return ~crc; // same as crc ^ 0xFFFFFFFF
37  };
38 
39 
40  static inline bool compare(
41  const uint32_t computed,
42  const uint8_t *received
43  )
44  {
45  for(uint8_t i = 4; i > 0; i--)
46  if(
47  (uint8_t)(computed >> (8 * (i - 1))) !=
48  (uint8_t)(received[3 - (i - 1)])
49  ) {
50  return false;
51  }
52  return true;
53  };
54 
55 };
data
char data[MAX_PAYLOAD_SIZE+1]
Buffer for raw payload data.
Definition: MyMessage.h:654
PJON_crc32
Definition: PJON_CRC32.h:14