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

Constructor & Destructor Documentation

◆ CRC16()

LibXR::CRC16::CRC16 ( )
inline

Definition at line 128 of file crc.hpp.

128{}

Member Function Documentation

◆ Calculate()

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

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

◆ GenerateTable()

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

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

Definition at line 133 of file crc.hpp.

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 }

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

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

Field Documentation

◆ INIT

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

CRC16 初始值 / CRC16 initial value.

Definition at line 122 of file crc.hpp.

◆ inited_

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

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

Definition at line 126 of file crc.hpp.

◆ tab_

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

CRC16 查找表 / CRC16 lookup table.

Definition at line 125 of file crc.hpp.


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