📄 crc.h
字号:
#ifndef _CRC_H_
#define _CRC_H_
//-----------------------------------------------------------
//#include "CRC.h"
/********************************************************************
purpose: 处理CRC循环校验所需要的方法,可选择临时计算,
或者一次计算出数值查表以增加速度.
*********************************************************************/
#define CRC16_CCITT 0x1021
#define CRC16_DEFAULT 0x8005
#define CRC32_DEFAULT 0x04C10DB7
void BuildTable16(unsigned short aPoly , unsigned long* Table_CRC);
void BuildTable32(unsigned long aPoly , unsigned long* Table_CRC);
class CCRC
{
public:
CCRC()
{
memset(Table_CRC,0,256);
bTableExist=false;
}
virtual ~CCRC()
{
}
CCRC(const unsigned long& aPoly)
{
if((unsigned short)aPoly==CRC16_CCITT
||(unsigned short)aPoly==CRC16_DEFAULT)
BuildTable16((unsigned short)aPoly, Table_CRC );
else if(aPoly==CRC32_DEFAULT)
BuildTable32((unsigned long)aPoly, Table_CRC );
bTableExist=true;
}
private:
unsigned long Table_CRC[256]; // CRC 表
bool bTableExist;
public:
/*******************************************************************/
/*函 数 名 称: RunCRC16
功 能 描 述: 执行对数据段的CRC16循环冗余校验
参 数 说 明: aData[in]:待校验数据
aSize[in]:待校验数据长度
aPoly[in]:创建表所需要的多项式
返回值 说明: 循环冗余校验结果
/*******************************************************************/
unsigned short RunCRC16( const char * aData, unsigned long aSize, unsigned short aPoly );
/*******************************************************************/
/*函 数 名 称: RunCRC32
功 能 描 述: 执行对数据段的CRC32循环冗余校验
参 数 说 明: aData[in]:待校验数据
aSize[in]:待校验数据长度
aPoly[in]:创建表所需要的多项式
返回值 说明: 循环冗余校验结果
/*******************************************************************/
unsigned long RunCRC32( const char * aData, unsigned long aSize, unsigned long aPoly );
};
/*******************************************************************/
/* 函 数 名 称: BuildTable16
功 能 描 述: 创建CRC16所需要的Table
参 数 说 明: aPoly[in]:创建表所需要的多项式
Table_CRC[in][out]:Table表的buff
返回值 说明: void
更 新 日 期: 2003.12.19
/*******************************************************************/
void BuildTable16( unsigned short aPoly , unsigned long* Table_CRC )
{
unsigned short i, j;
unsigned short nData;
unsigned short nAccum;
for ( i = 0; i < 256; i++ )
{
nData = ( unsigned short )( i << 8 );
nAccum = 0;
for ( j = 0; j < 8; j++ )
{
if ( ( nData ^ nAccum ) & 0x8000 )
nAccum = ( nAccum << 1 ) ^ aPoly;
else
nAccum <<= 1;
nData <<= 1;
}
Table_CRC[i] = ( unsigned long )nAccum;
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -