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

Constructor & Destructor Documentation

◆ CRC16()

LibXR::CRC16::CRC16 ( )
inline

Definition at line 112 of file crc.hpp.

112{}

Member Function Documentation

◆ Calculate()

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

计算数据的 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 19 of file crc_o3.cpp.

20{
21 const uint8_t* buf = reinterpret_cast<const uint8_t*>(raw);
22 if (!inited_)
23 {
25 }
26
27 uint16_t crc = INIT;
28 while (len--)
29 {
30 crc = tab_[(crc ^ *buf++) & 0xff] ^ (crc >> 8);
31 }
32 return crc;
33}
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

◆ GenerateTable()

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

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

Definition at line 117 of file crc.hpp.

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 }

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

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

Field Documentation

◆ INIT

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

CRC16 初始值 / CRC16 initial value.

Definition at line 106 of file crc.hpp.

◆ inited_

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

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

Definition at line 110 of file crc.hpp.

◆ tab_

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

CRC16 查找表 / CRC16 lookup table.

Definition at line 109 of file crc.hpp.


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