VSM C++ SDK
Vehicle Specific Modules SDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
crc32.h
1 // Copyright (c) 2018, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
5 #ifndef _UGCS_VSM_CRC32_H_
6 #define _UGCS_VSM_CRC32_H_
7 
8 #include <stdint.h>
9 #include <stddef.h>
10 
11 namespace ugcs {
12 namespace vsm {
13 
14 class Crc32 {
15 public:
16  void
17  Reset()
18  {
19  crc = 0xFFFFFFFF;
20  }
21 
22  uint32_t
23  Add_byte(uint8_t b)
24  {
25  crc = crc_tab[(crc ^ b) & 0xff] ^ (crc >> 8);
26  return ~crc;
27  }
28 
29  uint32_t
30  Add_short(uint16_t b)
31  {
32  return Add_buffer(&b, 2);
33  }
34 
35  uint32_t
36  Add_int(uint32_t b)
37  {
38  return Add_buffer(&b, 4);
39  }
40 
41  uint32_t
42  Add_buffer(void* b, size_t len)
43  {
44  auto buf = static_cast<uint8_t*>(b);
45  for (; len; --len, ++buf) {
46  crc = crc_tab[(crc ^ (*buf)) & 0xff] ^ (crc >> 8);
47  }
48  return ~crc;
49  }
50 
51  uint32_t
52  Get()
53  {
54  return ~crc;
55  }
56 
57 private:
58  uint32_t crc = 0xFFFFFFFF;
59  static uint32_t crc_tab[];
60 };
61 
62 } /* namespace vsm */
63 } /* namespace ugcs */
64 
65 #endif /* _UGCS_VSM_CRC32_H_ */
Definition: crc32.h:14