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

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

#include <crc.hpp>

Static Public Member Functions

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

Static Public Attributes

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

Static Private Attributes

static const uint16_t INIT = 0xFFFF
 CRC16 初始值 / CRC16 initial value.
 

Detailed Description

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

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

Definition at line 116 of file crc.hpp.

Constructor & Destructor Documentation

◆ CRC16()

LibXR::CRC16::CRC16 ( )
inline

Definition at line 125 of file crc.hpp.

125{}

Member Function Documentation

◆ Calculate()

static uint16_t LibXR::CRC16::Calculate ( const void raw,
size_t  len 
)
inlinestatic

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

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

Definition at line 158 of file crc.hpp.

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 }
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
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

◆ GenerateTable()

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

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

Definition at line 130 of file crc.hpp.

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 }

◆ Verify()

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

验证数据的 CRC16 校验码 / Verifies the CRC16 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 181 of file crc.hpp.

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

Field Documentation

◆ INIT

const uint16_t LibXR::CRC16::INIT = 0xFFFF
staticprivate

CRC16 初始值 / CRC16 initial value.

Definition at line 119 of file crc.hpp.

◆ inited_

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

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

Definition at line 123 of file crc.hpp.

◆ tab_

uint16_t LibXR::CRC16::tab_[256]
inlinestatic

CRC16 查找表 / CRC16 lookup table.

Definition at line 122 of file crc.hpp.


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