JaiaBot  1.20.0
JaiaBot micro-AUV software
crc32.h
Go to the documentation of this file.
1 
2 #ifndef JAIABOT_CORE_SRC_LIB_CRC_CRC32_H
3 #define JAIABOT_CORE_SRC_LIB_CRC_CRC32_H
4 
5 #include <stdint.h>
6 #include <stdlib.h>
7 
8 using namespace std;
9 
10 namespace crc
11 {
13 {
14  public:
15  uint32_t table[256];
16 
17  CRC32Table() { generate_table(); }
18 
20  {
21  uint32_t polynomial = 0xEDB88320;
22  for (uint32_t i = 0; i < 256; i++)
23  {
24  uint32_t c = i;
25  for (size_t j = 0; j < 8; j++)
26  {
27  if (c & 1)
28  {
29  c = polynomial ^ (c >> 1);
30  }
31  else
32  {
33  c >>= 1;
34  }
35  }
36  table[i] = c;
37  }
38  }
39 };
40 
42 
43 uint32_t calculate_crc32(const void* buf, size_t len, uint32_t initial = 0)
44 {
45  const uint32_t* table = crc32_table.table;
46 
47  uint32_t c = initial ^ 0xFFFFFFFF;
48  const uint8_t* u = static_cast<const uint8_t*>(buf);
49  for (size_t i = 0; i < len; ++i) { c = table[(c ^ u[i]) & 0xFF] ^ (c >> 8); }
50  return c ^ 0xFFFFFFFF;
51 }
52 }; // namespace crc
53 
54 #endif
void generate_table()
Definition: crc32.h:19
uint32_t table[256]
Definition: crc32.h:15
Definition: crc32.h:11
uint32_t calculate_crc32(const void *buf, size_t len, uint32_t initial=0)
Definition: crc32.h:43
const CRC32Table crc32_table
Definition: crc32.h:41