libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
crc.hpp
1#pragma once
2
3#include <cstdint>
4#include <cstdio>
5
6namespace LibXR
7{
16class CRC8
17{
18 private:
19 static const uint8_t INIT = 0xFF;
20
21 public:
22 static inline uint8_t tab_[256];
23 static inline bool inited_ =
24 false;
25
26 CRC8() {}
27
31 static void GenerateTable()
32 {
33 uint8_t crc = 0;
34
35 for (int i = 0; i < 256; i++)
36 {
37 tab_[i] = i;
38 }
39
40 for (int i = 0; i < 256; i++)
41 {
42 for (int j = 7; j >= 0; j--)
43 {
44 crc = tab_[i] & 0x01;
45
46 if (crc)
47 {
48 tab_[i] = tab_[i] >> 1;
49 tab_[i] ^= 0x8c;
50 }
51 else
52 {
53 tab_[i] = tab_[i] >> 1;
54 }
55 }
56 }
57 inited_ = true;
58 }
59
66 static uint8_t Calculate(const void *raw, size_t len)
67 {
68 const uint8_t *buf = reinterpret_cast<const uint8_t *>(raw);
69 if (!inited_)
70 {
72 }
73
75
76 while (len-- > 0)
77 {
78 crc = tab_[(crc ^ *buf++) & 0xff];
79 }
80
81 return crc;
82 }
83
91 static bool Verify(const void *raw, size_t len)
92 {
93 const uint8_t *buf = reinterpret_cast<const uint8_t *>(raw);
94 if (!inited_)
95 {
97 }
98
99 if (len < 2)
100 {
101 return false;
102 }
103 uint8_t expected = Calculate(buf, len - sizeof(uint8_t));
104 return expected == buf[len - sizeof(uint8_t)];
105 }
106};
107
116class CRC16
117{
118 private:
119 static const uint16_t INIT = 0xFFFF;
120
121 public:
122 static inline uint16_t tab_[256];
123 static inline bool inited_ =
124 false;
125 CRC16() {}
126
130 static void GenerateTable()
131 {
132 uint16_t crc = 0;
133 for (int i = 0; i < 256; ++i)
134 {
135 crc = i;
136 for (int j = 0; j < 8; ++j)
137 {
138 if (crc & 1)
139 {
140 crc = (crc >> 1) ^ 0x8408;
141 }
142 else
143 {
144 crc >>= 1;
145 }
146 }
147 tab_[i] = crc;
148 }
149 inited_ = true;
150 }
151
158 static uint16_t Calculate(const void *raw, size_t len)
159 {
160 const uint8_t *buf = reinterpret_cast<const uint8_t *>(raw);
161 if (!inited_)
162 {
164 }
165
166 uint16_t crc = INIT;
167 while (len--)
168 {
169 crc = tab_[(crc ^ *buf++) & 0xff] ^ (crc >> 8);
170 }
171 return crc;
172 }
173
181 static bool Verify(const void *raw, size_t len)
182 {
183 const uint8_t *buf = reinterpret_cast<const uint8_t *>(raw);
184 if (!inited_)
185 {
187 }
188
189 if (len < 2)
190 {
191 return false;
192 }
193
195 return expected == (reinterpret_cast<const uint16_t *>(
196 buf + (len % 2)))[len / sizeof(uint16_t) - 1];
197 }
198};
199
208class CRC32
209{
210 private:
211 static const uint32_t INIT = 0xFFFFFFFF;
212
213 public:
214 static inline uint32_t tab_[256];
215 static inline bool inited_ =
216 false;
217
218 CRC32() {}
219
223 static void GenerateTable()
224 {
225 uint32_t crc = 0;
226 for (int i = 0; i < 256; ++i)
227 {
228 crc = i;
229 for (int j = 0; j < 8; ++j)
230 {
231 if (crc & 1)
232 {
233 crc = (crc >> 1) ^ 0xEDB88320;
234 }
235 else
236 {
237 crc >>= 1;
238 }
239 }
240 tab_[i] = crc;
241 }
242 inited_ = true;
243 }
244
251 static uint32_t Calculate(const void *raw, size_t len)
252 {
253 const uint8_t *buf = reinterpret_cast<const uint8_t *>(raw);
254 if (!inited_)
255 {
257 }
258
259 uint32_t crc = INIT;
260 while (len--)
261 {
262 crc = tab_[(crc ^ *buf++) & 0xff] ^ (crc >> 8);
263 }
264 return crc;
265 }
266
274 static bool Verify(const void *raw, size_t len)
275 {
276 const uint8_t *buf = reinterpret_cast<const uint8_t *>(raw);
277 if (!inited_)
278 {
280 }
281
282 if (len < 2)
283 {
284 return false;
285 }
286
288 return expected == (reinterpret_cast<const uint32_t *>(
289 buf + (len % 4)))[len / sizeof(uint32_t) - 1];
290 }
291};
292} // namespace LibXR
16 位循环冗余校验(CRC-16)计算类 / CRC-16 checksum computation class
Definition crc.hpp:117
static bool Verify(const void *raw, size_t len)
验证数据的 CRC16 校验码 / Verifies the CRC16 checksum of the given data
Definition crc.hpp:181
static uint16_t Calculate(const void *raw, size_t len)
计算数据的 CRC16 校验码 / Computes the CRC16 checksum for the given data
Definition crc.hpp:158
static bool inited_
查找表是否已初始化 / Whether the lookup table is initialized
Definition crc.hpp:123
static void GenerateTable()
生成 CRC16 查找表 / Generates the CRC16 lookup table
Definition crc.hpp:130
static uint16_t tab_[256]
CRC16 查找表 / CRC16 lookup table.
Definition crc.hpp:122
static const uint16_t INIT
CRC16 初始值 / CRC16 initial value.
Definition crc.hpp:119
32 位循环冗余校验(CRC-32)计算类 / CRC-32 checksum computation class
Definition crc.hpp:209
static bool inited_
查找表是否已初始化 / Whether the lookup table is initialized
Definition crc.hpp:215
static uint32_t Calculate(const void *raw, size_t len)
计算数据的 CRC32 校验码 / Computes the CRC32 checksum for the given data
Definition crc.hpp:251
static const uint32_t INIT
CRC32 初始值 / CRC32 initial value.
Definition crc.hpp:211
static uint32_t tab_[256]
CRC32 查找表 / CRC32 lookup table.
Definition crc.hpp:214
static bool Verify(const void *raw, size_t len)
验证数据的 CRC32 校验码 / Verifies the CRC32 checksum of the given data
Definition crc.hpp:274
static void GenerateTable()
生成 CRC32 查找表 / Generates the CRC32 lookup table
Definition crc.hpp:223
8 位循环冗余校验(CRC-8)计算类 / CRC-8 checksum computation class
Definition crc.hpp:17
static const uint8_t INIT
CRC8 初始值 / CRC8 initial value.
Definition crc.hpp:19
static void GenerateTable()
生成 CRC8 查找表 / Generates the CRC8 lookup table
Definition crc.hpp:31
static bool inited_
查找表是否已初始化 / Whether the lookup table is initialized
Definition crc.hpp:23
static uint8_t Calculate(const void *raw, size_t len)
计算数据的 CRC8 校验码 / Computes the CRC8 checksum for the given data
Definition crc.hpp:66
static uint8_t tab_[256]
CRC8 查找表 / CRC8 lookup table.
Definition crc.hpp:22
static bool Verify(const void *raw, size_t len)
验证数据的 CRC8 校验码 / Verifies the CRC8 checksum of the given data
Definition crc.hpp:91
LibXR Color Control Library / LibXR终端颜色控制库
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值