libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::CRC8 Class Reference

8 位循环冗余校验(CRC-8)计算类 / CRC-8 checksum computation class More...

#include <crc.hpp>

Static Public Member Functions

static void GenerateTable ()
 生成 CRC8 查找表 / Generates the CRC8 lookup table
 
static uint8_t Calculate (const void *raw, size_t len)
 计算数据的 CRC8 校验码 / Computes the CRC8 checksum for the given data
 
static bool Verify (const void *raw, size_t len)
 验证数据的 CRC8 校验码 / Verifies the CRC8 checksum of the given data
 

Static Public Attributes

static uint8_t tab_ [256]
 CRC8 查找表 / CRC8 lookup table.
 
static bool inited_
 查找表是否已初始化 / Whether the lookup table is initialized
 

Static Private Attributes

static const uint8_t INIT = 0xFF
 CRC8 初始值 / CRC8 initial value.
 

Detailed Description

8 位循环冗余校验(CRC-8)计算类 / CRC-8 checksum computation class

该类实现了 CRC-8 校验算法,支持计算和验证数据的 CRC8 校验码。 This class implements the CRC-8 checksum algorithm, supporting computation and verification.

Definition at line 16 of file crc.hpp.

Constructor & Destructor Documentation

◆ CRC8()

LibXR::CRC8::CRC8 ( )
inline

Definition at line 26 of file crc.hpp.

26{}

Member Function Documentation

◆ Calculate()

static uint8_t LibXR::CRC8::Calculate ( const void raw,
size_t  len 
)
inlinestatic

计算数据的 CRC8 校验码 / Computes the CRC8 checksum for the given data

Parameters
raw输入数据指针 / Pointer to input data
len数据长度 / Length of the data
Returns
计算得到的 CRC8 值 / Computed CRC8 value

Definition at line 66 of file crc.hpp.

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 }
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 tab_[256]
CRC8 查找表 / CRC8 lookup table.
Definition crc.hpp:22
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

◆ GenerateTable()

static void LibXR::CRC8::GenerateTable ( )
inlinestatic

生成 CRC8 查找表 / Generates the CRC8 lookup table

Definition at line 31 of file crc.hpp.

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 }

◆ Verify()

static bool LibXR::CRC8::Verify ( const void raw,
size_t  len 
)
inlinestatic

验证数据的 CRC8 校验码 / Verifies the CRC8 checksum of the given data

Parameters
raw输入数据指针 / Pointer to input data
len数据长度 / Length of the data
Returns
校验成功返回 true,否则返回 false / Returns true if the checksum is valid, otherwise returns false

Definition at line 91 of file crc.hpp.

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 }
static uint8_t Calculate(const void *raw, size_t len)
计算数据的 CRC8 校验码 / Computes the CRC8 checksum for the given data
Definition crc.hpp:66

Field Documentation

◆ INIT

const uint8_t LibXR::CRC8::INIT = 0xFF
staticprivate

CRC8 初始值 / CRC8 initial value.

Definition at line 19 of file crc.hpp.

◆ inited_

bool LibXR::CRC8::inited_
inlinestatic
Initial value:
=
false

查找表是否已初始化 / Whether the lookup table is initialized

Definition at line 23 of file crc.hpp.

◆ tab_

uint8_t LibXR::CRC8::tab_[256]
inlinestatic

CRC8 查找表 / CRC8 lookup table.

Definition at line 22 of file crc.hpp.


The documentation for this class was generated from the following file: