今天看了一个简单的crc校验代码,挺简单的:
static uint32_t crc32_tab[256];
void make_crc_table(void)
{
uint32_t c;
int n, k;
uint32_t poly; /* polynomial exclusive-or pattern */
//长整形数 c, poly;
/* terms of polynomial defining this crc (except x^32): */
static const uint8_t p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};
/* make exclusive-or pattern from polynomial (0xedb88320L) */
poly = 0L; //通过p[], 计算出poly值
for (n = 0; n < sizeof(p); n++)
poly |= 1L << (31 - p[n]);
for (n = 0; n < 256; n++) //再通过poly 算出 crc32_tab[],一共256个值。
{
c = (uint32_t)n;
for (k = 0; k < 8; k++)
c = c & 1 ? poly ^ (c >> 1) : c >> 1;