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 static uint8_t Calculate(const void* raw, size_t len);
70
78 static bool Verify(const void* raw, size_t len)
79 {
80 const uint8_t* buf = reinterpret_cast<const uint8_t*>(raw);
81 if (!inited_)
82 {
84 }
85
86 if (len < 2)
87 {
88 return false;
89 }
90 uint8_t expected = Calculate(buf, len - sizeof(uint8_t));
91 return expected == buf[len - sizeof(uint8_t)];
92 }
93};
94
103class CRC16
104{
105 private:
106 static const uint16_t INIT = 0xFFFF;
107
108 public:
109 static inline uint16_t tab_[256];
110 static inline bool inited_ =
111 false;
112 CRC16() {}
113
117 static void GenerateTable()
118 {
119 uint16_t crc = 0;
120 for (int i = 0; i < 256; ++i)
121 {
122 crc = i;
123 for (int j = 0; j < 8; ++j)
124 {
125 if (crc & 1)
126 {
127 crc = (crc >> 1) ^ 0x8408;
128 }
129 else
130 {
131 crc >>= 1;
132 }
133 }
134 tab_[i] = crc;
135 }
136 inited_ = true;
137 }
138
145 static uint16_t Calculate(const void* raw, size_t len);
146
154 static bool Verify(const void* raw, size_t len)
155 {
156 const uint8_t* buf = reinterpret_cast<const uint8_t*>(raw);
157 if (!inited_)
158 {
160 }
161
162 if (len < 2)
163 {
164 return false;
165 }
166
167 uint16_t actual = 0;
168 std::memcpy(&actual, buf + len - sizeof(actual), sizeof(actual));
169
170 uint16_t expected = Calculate(buf, len - sizeof(uint16_t));
171 return expected == actual;
172 }
173};
174
183class CRC32
184{
185 private:
186 static const uint32_t INIT = 0xFFFFFFFF;
187
188 public:
189 static inline uint32_t tab_[256];
190 static inline bool inited_ =
191 false;
192
193 CRC32() {}
194
198 static void GenerateTable()
199 {
200 uint32_t crc = 0;
201 for (int i = 0; i < 256; ++i)
202 {
203 crc = i;
204 for (int j = 0; j < 8; ++j)
205 {
206 if (crc & 1)
207 {
208 crc = (crc >> 1) ^ 0xEDB88320;
209 }
210 else
211 {
212 crc >>= 1;
213 }
214 }
215 tab_[i] = crc;
216 }
217 inited_ = true;
218 }
219
226 static uint32_t Calculate(const void* raw, size_t len);
227
235 static bool Verify(const void* raw, size_t len)
236 {
237 const uint8_t* buf = reinterpret_cast<const uint8_t*>(raw);
238 if (!inited_)
239 {
241 }
242
243 if (len < 2)
244 {
245 return false;
246 }
247
248 uint32_t actual = 0;
249 std::memcpy(&actual, buf + len - sizeof(actual), sizeof(actual));
250
251 uint32_t expected = Calculate(buf, len - sizeof(uint32_t));
252 return expected == actual;
253 }
254};
255
264class CRC64
265{
266 private:
267 static const uint64_t INIT =
268 0xFFFFFFFFFFFFFFFFULL;
269
270 public:
271 static inline uint64_t tab_[256];
272 static inline bool inited_ =
273 false;
274
275 CRC64() {}
276
280 static void GenerateTable()
281 {
282 uint64_t crc = 0;
283 for (int i = 0; i < 256; ++i)
284 {
285 crc = static_cast<uint64_t>(i);
286 for (int j = 0; j < 8; ++j)
287 {
288 if (crc & 1ULL)
289 {
290 crc = (crc >> 1U) ^ 0xC96C5795D7870F42ULL;
291 }
292 else
293 {
294 crc >>= 1U;
295 }
296 }
297 tab_[i] = crc;
298 }
299 inited_ = true;
300 }
301
308 static uint64_t Calculate(const void* raw, size_t len);
309};
310} // namespace LibXR
16 位循环冗余校验(CRC-16)计算类 / CRC-16 checksum computation class
Definition crc.hpp:104
static bool Verify(const void *raw, size_t len)
验证数据的 CRC16 校验码 / Verifies the CRC16 checksum of the given data
Definition crc.hpp:154
static bool inited_
查找表是否已初始化 / Whether the lookup table is initialized
Definition crc.hpp:110
static void GenerateTable()
生成 CRC16 查找表 / Generates the CRC16 lookup table
Definition crc.hpp:117
static uint16_t tab_[256]
CRC16 查找表 / CRC16 lookup table.
Definition crc.hpp:109
static const uint16_t INIT
CRC16 初始值 / CRC16 initial value.
Definition crc.hpp:106
static uint16_t Calculate(const void *raw, size_t len)
计算数据的 CRC16 校验码 / Computes the CRC16 checksum for the given data
Definition crc_o3.cpp:19
32 位循环冗余校验(CRC-32)计算类 / CRC-32 checksum computation class
Definition crc.hpp:184
static bool inited_
查找表是否已初始化 / Whether the lookup table is initialized
Definition crc.hpp:190
static uint32_t Calculate(const void *raw, size_t len)
计算数据的 CRC32 校验码 / Computes the CRC32 checksum for the given data
Definition crc_o3.cpp:35
static const uint32_t INIT
CRC32 初始值 / CRC32 initial value.
Definition crc.hpp:186
static uint32_t tab_[256]
CRC32 查找表 / CRC32 lookup table.
Definition crc.hpp:189
static bool Verify(const void *raw, size_t len)
验证数据的 CRC32 校验码 / Verifies the CRC32 checksum of the given data
Definition crc.hpp:235
static void GenerateTable()
生成 CRC32 查找表 / Generates the CRC32 lookup table
Definition crc.hpp:198
64 位循环冗余校验(CRC-64)计算类 / CRC-64 checksum computation class
Definition crc.hpp:265
static const uint64_t INIT
CRC64 初始值 / CRC64 initial value.
Definition crc.hpp:267
static bool inited_
查找表是否已初始化 / Whether the lookup table is initialized
Definition crc.hpp:272
static void GenerateTable()
生成 CRC64 查找表 / Generates the CRC64 lookup table
Definition crc.hpp:280
static uint64_t Calculate(const void *raw, size_t len)
计算数据的 CRC64 校验码 / Computes the CRC64 checksum for the given data
Definition crc_o3.cpp:51
static uint64_t tab_[256]
CRC64 查找表 / CRC64 lookup table.
Definition crc.hpp:271
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 bool inited_
查找表是否已初始化 / Whether the lookup table is initialized
Definition crc.hpp:26
static uint8_t Calculate(const void *raw, size_t len)
计算数据的 CRC8 校验码 / Computes the CRC8 checksum for the given data
Definition crc_o3.cpp:3
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:78
LibXR 命名空间
Definition ch32_can.hpp:14