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

Constructor & Destructor Documentation

◆ CRC32()

LibXR::CRC32::CRC32 ( )
inline

Definition at line 223 of file crc.hpp.

223{}

Member Function Documentation

◆ Calculate()

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

257 {
258 const uint8_t* buf = reinterpret_cast<const uint8_t*>(raw);
259 if (!inited_)
260 {
262 }
263
264 uint32_t crc = INIT;
265 while (len--)
266 {
267 crc = tab_[(crc ^ *buf++) & 0xff] ^ (crc >> 8);
268 }
269 return crc;
270 }
static bool inited_
查找表是否已初始化 / Whether the lookup table is initialized
Definition crc.hpp:220
static const uint32_t INIT
CRC32 初始值 / CRC32 initial value.
Definition crc.hpp:216
static uint32_t tab_[256]
CRC32 查找表 / CRC32 lookup table.
Definition crc.hpp:219
static void GenerateTable()
生成 CRC32 查找表 / Generates the CRC32 lookup table
Definition crc.hpp:228

◆ GenerateTable()

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

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

Definition at line 228 of file crc.hpp.

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

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

280 {
281 const uint8_t* buf = reinterpret_cast<const uint8_t*>(raw);
282 if (!inited_)
283 {
285 }
286
287 if (len < 2)
288 {
289 return false;
290 }
291
292 uint32_t actual = 0;
293 std::memcpy(&actual, buf + len - sizeof(actual), sizeof(actual));
294
295 uint32_t expected = Calculate(buf, len - sizeof(uint32_t));
296 return expected == actual;
297 }
static LIBXR_FORCE_OPTIMIZE_O3 uint32_t Calculate(const void *raw, size_t len)
计算数据的 CRC32 校验码 / Computes the CRC32 checksum for the given data
Definition crc.hpp:256

Field Documentation

◆ INIT

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

CRC32 初始值 / CRC32 initial value.

Definition at line 216 of file crc.hpp.

◆ inited_

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

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

Definition at line 220 of file crc.hpp.

◆ tab_

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

CRC32 查找表 / CRC32 lookup table.

Definition at line 219 of file crc.hpp.


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