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#include <cstring>
6
7#include "libxr_def.hpp"
8
9namespace LibXR
10{
19class CRC8
20{
21 private:
22 static const uint8_t INIT = 0xFF;
23
24 public:
25 static inline uint8_t tab_[256];
26 static inline bool inited_ =
27 false;
28
29 CRC8() {}
30
34 static void GenerateTable()
35 {
36 uint8_t crc = 0;
37
38 for (int i = 0; i < 256; i++)
39 {
40 tab_[i] = i;
41 }
42
43 for (int i = 0; i < 256; i++)
44 {
45 for (int j = 7; j >= 0; j--)
46 {
47 crc = tab_[i] & 0x01;
48
49 if (crc)
50 {
51 tab_[i] = tab_[i] >> 1;
52 tab_[i] ^= 0x8c;
53 }
54 else
55 {
56 tab_[i] = tab_[i] >> 1;
57 }
58 }
59 }
60 inited_ = true;
61 }
62
69 LIBXR_FORCE_OPTIMIZE_O3 static uint8_t Calculate(const void* raw, size_t len)
70 {
71 const uint8_t* buf = reinterpret_cast<const uint8_t*>(raw);
72 if (!inited_)
73 {
75 }
76
77 uint8_t crc = INIT;
78
79 while (len-- > 0)
80 {
81 crc = tab_[(crc ^ *buf++) & 0xff];
82 }
83
84 return crc;
85 }
86
94 static bool Verify(const void* raw, size_t len)
95 {
96 const uint8_t* buf = reinterpret_cast<const uint8_t*>(raw);
97 if (!inited_)
98 {
100 }
101
102 if (len < 2)
103 {
104 return false;
105 }
106 uint8_t expected = Calculate(buf, len - sizeof(uint8_t));
107 return expected == buf[len - sizeof(uint8_t)];
108 }
109};
110
119class CRC16
120{
121 private:
122 static const uint16_t INIT = 0xFFFF;
123
124 public:
125 static inline uint16_t tab_[256];
126 static inline bool inited_ =
127 false;
128 CRC16() {}
129
133 static void GenerateTable()
134 {
135 uint16_t crc = 0;
136 for (int i = 0; i < 256; ++i)
137 {
138 crc = i;
139 for (int j = 0; j < 8; ++j)
140 {
141 if (crc & 1)
142 {
143 crc = (crc >> 1) ^ 0x8408;
144 }
145 else
146 {
147 crc >>= 1;
148 }
149 }
150 tab_[i] = crc;
151 }
152 inited_ = true;
153 }
154
161 LIBXR_FORCE_OPTIMIZE_O3 static uint16_t Calculate(const void* raw, size_t len)
162 {
163 const uint8_t* buf = reinterpret_cast<const uint8_t*>(raw);
164 if (!inited_)
165 {
167 }
168
169 uint16_t crc = INIT;
170 while (len--)
171 {
172 crc = tab_[(crc ^ *buf++) & 0xff] ^ (crc >> 8);
173 }
174 return crc;
175 }
176
184 static bool Verify(const void* raw, size_t len)
185 {
186 const uint8_t* buf = reinterpret_cast<const uint8_t*>(raw);
187 if (!inited_)
188 {
190 }
191
192 if (len < 2)
193 {
194 return false;
195 }
196
197 uint16_t actual = 0;
198 std::memcpy(&actual, buf + len - sizeof(actual), sizeof(actual));
199
200 uint16_t expected = Calculate(buf, len - sizeof(uint16_t));
201 return expected == actual;
202 }
203};
204
213class CRC32
214{
215 private:
216 static const uint32_t INIT = 0xFFFFFFFF;
217
218 public:
219 static inline uint32_t tab_[256];
220 static inline bool inited_ =
221 false;
222
223 CRC32() {}
224
228 static void GenerateTable()
229 {
230 uint32_t crc = 0;
231 for (int i = 0; i < 256; ++i)
232 {
233 crc = i;
234 for (int j = 0; j < 8; ++j)
235 {
236 if (crc & 1)
237 {
238 crc = (crc >> 1) ^ 0xEDB88320;
239 }
240 else
241 {
242 crc >>= 1;
243 }
244 }
245 tab_[i] = crc;
246 }
247 inited_ = true;
248 }
249
256 LIBXR_FORCE_OPTIMIZE_O3 static uint32_t Calculate(const void* raw, size_t len)
257 {
258 const uint8_t* buf = reinterpret_cast<const uint8_t*>(raw);
259 if (!inited_)
260 {
262 }
263
264 uint32_t crc = INIT;
265 while (len--)
266 {
267 crc = tab_[(crc ^ *buf++) & 0xff] ^ (crc >> 8);
268 }
269 return crc;
270 }
271
279 static bool Verify(const void* raw, size_t len)
280 {
281 const uint8_t* buf = reinterpret_cast<const uint8_t*>(raw);
282 if (!inited_)
283 {
285 }
286
287 if (len < 2)
288 {
289 return false;
290 }
291
292 uint32_t actual = 0;
293 std::memcpy(&actual, buf + len - sizeof(actual), sizeof(actual));
294
295 uint32_t expected = Calculate(buf, len - sizeof(uint32_t));
296 return expected == actual;
297 }
298};
299
308class CRC64
309{
310 private:
311 static const uint64_t INIT =
312 0xFFFFFFFFFFFFFFFFULL;
313
314 public:
315 static inline uint64_t tab_[256];
316 static inline bool inited_ =
317 false;
318
319 CRC64() {}
320
324 static void GenerateTable()
325 {
326 uint64_t crc = 0;
327 for (int i = 0; i < 256; ++i)
328 {
329 crc = static_cast<uint64_t>(i);
330 for (int j = 0; j < 8; ++j)
331 {
332 if (crc & 1ULL)
333 {
334 crc = (crc >> 1U) ^ 0xC96C5795D7870F42ULL;
335 }
336 else
337 {
338 crc >>= 1U;
339 }
340 }
341 tab_[i] = crc;
342 }
343 inited_ = true;
344 }
345
352 LIBXR_FORCE_OPTIMIZE_O3 static uint64_t Calculate(const void* raw, size_t len)
353 {
354 const uint8_t* buf = reinterpret_cast<const uint8_t*>(raw);
355 if (!inited_)
356 {
358 }
359
360 uint64_t crc = INIT;
361 while (len--)
362 {
363 crc = tab_[(crc ^ *buf++) & 0xff] ^ (crc >> 8U);
364 }
365 return crc;
366 }
367};
368} // namespace LibXR
16 位循环冗余校验(CRC-16)计算类 / CRC-16 checksum computation class
Definition crc.hpp:120
static bool Verify(const void *raw, size_t len)
验证数据的 CRC16 校验码 / Verifies the CRC16 checksum of the given data
Definition crc.hpp:184
static LIBXR_FORCE_OPTIMIZE_O3 uint16_t Calculate(const void *raw, size_t len)
计算数据的 CRC16 校验码 / Computes the CRC16 checksum for the given data
Definition crc.hpp:161
static bool inited_
查找表是否已初始化 / Whether the lookup table is initialized
Definition crc.hpp:126
static void GenerateTable()
生成 CRC16 查找表 / Generates the CRC16 lookup table
Definition crc.hpp:133
static uint16_t tab_[256]
CRC16 查找表 / CRC16 lookup table.
Definition crc.hpp:125
static const uint16_t INIT
CRC16 初始值 / CRC16 initial value.
Definition crc.hpp:122
32 位循环冗余校验(CRC-32)计算类 / CRC-32 checksum computation class
Definition crc.hpp:214
static LIBXR_FORCE_OPTIMIZE_O3 uint32_t Calculate(const void *raw, size_t len)
计算数据的 CRC32 校验码 / Computes the CRC32 checksum for the given data
Definition crc.hpp:256
static bool inited_
查找表是否已初始化 / Whether the lookup table is initialized
Definition crc.hpp:220
static const uint32_t INIT
CRC32 初始值 / CRC32 initial value.
Definition crc.hpp:216
static uint32_t tab_[256]
CRC32 查找表 / CRC32 lookup table.
Definition crc.hpp:219
static bool Verify(const void *raw, size_t len)
验证数据的 CRC32 校验码 / Verifies the CRC32 checksum of the given data
Definition crc.hpp:279
static void GenerateTable()
生成 CRC32 查找表 / Generates the CRC32 lookup table
Definition crc.hpp:228
64 位循环冗余校验(CRC-64)计算类 / CRC-64 checksum computation class
Definition crc.hpp:309
static const uint64_t INIT
CRC64 初始值 / CRC64 initial value.
Definition crc.hpp:311
static LIBXR_FORCE_OPTIMIZE_O3 uint64_t Calculate(const void *raw, size_t len)
计算数据的 CRC64 校验码 / Computes the CRC64 checksum for the given data
Definition crc.hpp:352
static bool inited_
查找表是否已初始化 / Whether the lookup table is initialized
Definition crc.hpp:316
static void GenerateTable()
生成 CRC64 查找表 / Generates the CRC64 lookup table
Definition crc.hpp:324
static uint64_t tab_[256]
CRC64 查找表 / CRC64 lookup table.
Definition crc.hpp:315
8 位循环冗余校验(CRC-8)计算类 / CRC-8 checksum computation class
Definition crc.hpp:20
static const uint8_t INIT
CRC8 初始值 / CRC8 initial value.
Definition crc.hpp:22
static void GenerateTable()
生成 CRC8 查找表 / Generates the CRC8 lookup table
Definition crc.hpp:34
static LIBXR_FORCE_OPTIMIZE_O3 uint8_t Calculate(const void *raw, size_t len)
计算数据的 CRC8 校验码 / Computes the CRC8 checksum for the given data
Definition crc.hpp:69
static bool inited_
查找表是否已初始化 / Whether the lookup table is initialized
Definition crc.hpp:26
static uint8_t tab_[256]
CRC8 查找表 / CRC8 lookup table.
Definition crc.hpp:25
static bool Verify(const void *raw, size_t len)
验证数据的 CRC8 校验码 / Verifies the CRC8 checksum of the given data
Definition crc.hpp:94
LibXR 命名空间
Definition ch32_can.hpp:14