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

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

#include <crc.hpp>

Static Public Member Functions

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

Static Public Attributes

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

Static Private Attributes

static const uint32_t INIT = 0xFFFFFFFF
 CRC32 初始值 / CRC32 initial value.
 

Detailed Description

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

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

Definition at line 183 of file crc.hpp.

Constructor & Destructor Documentation

◆ CRC32()

LibXR::CRC32::CRC32 ( )
inline

Definition at line 193 of file crc.hpp.

193{}

Member Function Documentation

◆ Calculate()

uint32_t LibXR::CRC32::Calculate ( const void * raw,
size_t len )
static

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

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

Definition at line 35 of file crc_o3.cpp.

36{
37 const uint8_t* buf = reinterpret_cast<const uint8_t*>(raw);
38 if (!inited_)
39 {
41 }
42
43 uint32_t crc = INIT;
44 while (len--)
45 {
46 crc = tab_[(crc ^ *buf++) & 0xff] ^ (crc >> 8);
47 }
48 return crc;
49}
static bool inited_
查找表是否已初始化 / Whether the lookup table is initialized
Definition crc.hpp:190
static const uint32_t INIT
CRC32 初始值 / CRC32 initial value.
Definition crc.hpp:186
static uint32_t tab_[256]
CRC32 查找表 / CRC32 lookup table.
Definition crc.hpp:189
static void GenerateTable()
生成 CRC32 查找表 / Generates the CRC32 lookup table
Definition crc.hpp:198

◆ GenerateTable()

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

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

Definition at line 198 of file crc.hpp.

199 {
200 uint32_t crc = 0;
201 for (int i = 0; i < 256; ++i)
202 {
203 crc = i;
204 for (int j = 0; j < 8; ++j)
205 {
206 if (crc & 1)
207 {
208 crc = (crc >> 1) ^ 0xEDB88320;
209 }
210 else
211 {
212 crc >>= 1;
213 }
214 }
215 tab_[i] = crc;
216 }
217 inited_ = true;
218 }

◆ Verify()

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

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

236 {
237 const uint8_t* buf = reinterpret_cast<const uint8_t*>(raw);
238 if (!inited_)
239 {
241 }
242
243 if (len < 2)
244 {
245 return false;
246 }
247
248 uint32_t actual = 0;
249 std::memcpy(&actual, buf + len - sizeof(actual), sizeof(actual));
250
251 uint32_t expected = Calculate(buf, len - sizeof(uint32_t));
252 return expected == actual;
253 }
static uint32_t Calculate(const void *raw, size_t len)
计算数据的 CRC32 校验码 / Computes the CRC32 checksum for the given data
Definition crc_o3.cpp:35

Field Documentation

◆ INIT

const uint32_t LibXR::CRC32::INIT = 0xFFFFFFFF
staticprivate

CRC32 初始值 / CRC32 initial value.

Definition at line 186 of file crc.hpp.

◆ inited_

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

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

Definition at line 190 of file crc.hpp.

◆ tab_

uint32_t LibXR::CRC32::tab_[256]
inlinestatic

CRC32 查找表 / CRC32 lookup table.

Definition at line 189 of file crc.hpp.


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