64,637
社区成员
发帖
与我相关
我的任务
分享
/* ****************************** CRC校验 ****************************** */
unsigned long int crc32_table[256];
unsigned long int ulPolynomial = 0x04c11db7;
unsigned long int Reflect(unsigned long int ref, char ch){
unsigned long int value=0;
int i;
for(i = 1; i < (ch + 1); i++){
if(ref & 1)
value |= 1 << (ch - i);
ref >>= 1;
}
return value;
}
void init_crc32_table(){
unsigned long int crc,temp;
unsigned long int t1,t2;
unsigned long int flag;
int i,j;
for(i = 0; i <= 0xFF; i++){
temp=Reflect(i, 8);
crc32_table[i]= temp<< 24;
for (j = 0; j < 8; j++){
flag=crc32_table[i]&0x80000000;
t1=(crc32_table[i] << 1);
if(flag==0)
t2=0;
else
t2=ulPolynomial;
crc32_table[i] =t1^t2 ;
}
crc=crc32_table[i];
crc32_table[i] = Reflect(crc32_table[i], 32);
}
return;
}
unsigned long GenerateCRC32(char* DataBuf,unsigned long len) //crc32生成函数
{
unsigned long oldcrc32;
unsigned long crc32;
unsigned long oldcrc;
unsigned int charcnt;
char c,t;
oldcrc32=0x00000000; //初值为0
charcnt=0;
while(len--)
{
t= (oldcrc32>>24)&0xFF; //要移出的字节的值
oldcrc=crc32_table[t]; //根据移出的字节的值查表
c=DataBuf[charcnt]; //新移进来的字节值
oldcrc32=(oldcrc32<<8)|c; //将新移进来的字节值添在寄存器末字节中
oldcrc32=oldcrc32^oldcrc; //将寄存器与查出的值进行xor运算
charcnt++;
}
crc32=oldcrc32;
return crc32;
}
/* *************************************************************************************** */