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 LIBXR_FORCE_OPTIMIZE_O3 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 19 of file crc.hpp.

Constructor & Destructor Documentation

◆ CRC8()

LibXR::CRC8::CRC8 ( )
inline

Definition at line 29 of file crc.hpp.

29{}

Member Function Documentation

◆ Calculate()

static LIBXR_FORCE_OPTIMIZE_O3 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 69 of file crc.hpp.

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 }
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 tab_[256]
CRC8 查找表 / CRC8 lookup table.
Definition crc.hpp:25

◆ GenerateTable()

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

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

Definition at line 34 of file crc.hpp.

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 }

◆ 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 94 of file crc.hpp.

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 }
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

Field Documentation

◆ INIT

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

CRC8 初始值 / CRC8 initial value.

Definition at line 22 of file crc.hpp.

◆ inited_

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

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

Definition at line 26 of file crc.hpp.

◆ tab_

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

CRC8 查找表 / CRC8 lookup table.

Definition at line 25 of file crc.hpp.


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