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

Constructor & Destructor Documentation

◆ CRC32()

LibXR::CRC32::CRC32 ( )
inline

Definition at line 218 of file crc.hpp.

218{}

Member Function Documentation

◆ Calculate()

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

计算数据的 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 251 of file crc.hpp.

252 {
253 const uint8_t *buf = reinterpret_cast<const uint8_t *>(raw);
254 if (!inited_)
255 {
257 }
258
259 uint32_t crc = INIT;
260 while (len--)
261 {
262 crc = tab_[(crc ^ *buf++) & 0xff] ^ (crc >> 8);
263 }
264 return crc;
265 }
static bool inited_
查找表是否已初始化 / Whether the lookup table is initialized
Definition crc.hpp:215
static const uint32_t INIT
CRC32 初始值 / CRC32 initial value.
Definition crc.hpp:211
static uint32_t tab_[256]
CRC32 查找表 / CRC32 lookup table.
Definition crc.hpp:214
static void GenerateTable()
生成 CRC32 查找表 / Generates the CRC32 lookup table
Definition crc.hpp:223
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

◆ GenerateTable()

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

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

Definition at line 223 of file crc.hpp.

224 {
225 uint32_t crc = 0;
226 for (int i = 0; i < 256; ++i)
227 {
228 crc = i;
229 for (int j = 0; j < 8; ++j)
230 {
231 if (crc & 1)
232 {
233 crc = (crc >> 1) ^ 0xEDB88320;
234 }
235 else
236 {
237 crc >>= 1;
238 }
239 }
240 tab_[i] = crc;
241 }
242 inited_ = true;
243 }

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

275 {
276 const uint8_t *buf = reinterpret_cast<const uint8_t *>(raw);
277 if (!inited_)
278 {
280 }
281
282 if (len < 2)
283 {
284 return false;
285 }
286
288 return expected == (reinterpret_cast<const uint32_t *>(
289 buf + (len % 4)))[len / sizeof(uint32_t) - 1];
290 }
static uint32_t Calculate(const void *raw, size_t len)
计算数据的 CRC32 校验码 / Computes the CRC32 checksum for the given data
Definition crc.hpp:251

Field Documentation

◆ INIT

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

CRC32 初始值 / CRC32 initial value.

Definition at line 211 of file crc.hpp.

◆ inited_

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

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

Definition at line 215 of file crc.hpp.

◆ tab_

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

CRC32 查找表 / CRC32 lookup table.

Definition at line 214 of file crc.hpp.


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