📄 gen_crc32_table.c
字号:
#include <stdio.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define CRC32POLY 0xedb88320
#define CRC32POLY_REV 0x04c11db7
#define FCSTABLE crc32_table
#define FCS(crcnum, c) (((crcnum) << 8) ^ FCSTABLE[(((crcnum) >> 8) ^ (c)) & 0xff])
#define FCS_REV(crcnum, c) (((crcnum) >> 8) ^ FCSTABLE[((crcnum) ^ (c)) & 0xff])
#define REVERSE 1
unsigned int crc32_table[256];
unsigned long int Reflect(unsigned long int ref, char ch)
{
unsigned long int value = 0;
int i;
// 交换bit0和bit7,bit1和bit6,类推
for(i = 1; i < (ch + 1); i++)
{
if(ref & 1)
value |= 1 << (ch - i);
ref >>= 1;
}
return value;
}
init_crc32_table(unsigned int *crc32_table, const unsigned int ulPolynomial)
{
unsigned long int crc,temp;
int i,j;
// 256个值
for(i = 0; i <= 0xFF; i++)
{
temp=Reflect(i, 8);
crc32_table[i]= temp<< 24;
for (j = 0; j < 8; j++)
{
unsigned long int t1,t2;
unsigned long int 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);
}
/*
for(i=0; i<0xff; i++)
{
temp = (i << 24) & 0xff000000;
crc32_table[i] = 0;
for(j=0; j<8; j++)
{
if( (temp ^ crc32_table[i]) & 0x80000000)
crc32_table[i] = (crc32_table[i] << 1) ^ ulPolynomial;
else
crc32_table[i] <<= 1;
temp <<= 1;
}
}
*/
}
unsigned int Trid_CRC32_Gen(unsigned char *buf, int len)
{
unsigned int i = 0;
unsigned int crc32 = 0xFFFFFFFF;
//unsigned int crc32 = 0x00000000;
unsigned char *pD, c;
pD = buf;
/*
for(i=0; i<len; i++)
{
crc32=crc32_table[(crc32^(*(pD+i)))&0xff] ^ (crc32>>8);
}
return ~crc32;
*/
while(len--)
{
c = pD[i++];
#if REVERSE
crc32 = FCS_REV(crc32, c);
#else
crc32 = FCS(crc32, c);
#endif
}
#if REVERSE
return ~crc32;
//return crc32;
#else
return crc32;
#endif
}
void main()
{
int i;
unsigned char data = 0x10;
unsigned char j=0;
//int fd;
//fd = open("table.txt", O_CREAT | O_RDWR);
//if(fd == -1)
//{
// printf("open file error!!\n");
// exit(-1);
//}
FILE *fp;
fp = fopen("table.txt", "w+");
if(fp == NULL)
{
printf("error\n");
exit(-1);
}
init_crc32_table(crc32_table, CRC32POLY_REV);
for(i=0; i<256; i++)
{
//fprintf(fp, "0x%x, ", crc32_table[i]);
printf("0x%08x ", crc32_table[i]);
if((i % 8) == 7)
//fprintf(fp, "\n");
printf("\n");
}
printf("\n\n0x%08x\n\n", Trid_CRC32_Gen(&data, 1));
for(i=0; i<256; i++)
{
printf("0x%08x ", Trid_CRC32_Gen(&j, 1));
if((i%8) == 7)
printf("\n");
j++;
}
fclose(fp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -