我发现了一个易于使用的CRC算法实现这里。它包括基于表和位的算法。代码看起来很好,但是基于表的算法有一个重要的限制。以下是有关守则:
unsigned long reflect (unsigned long crc, int bitnum) {
unsigned long i, j=1, crcout=0;
for (i=(unsigned long)1<<(bitnum-1); i; i>>=1) {
if (crc & i) crcout|=j;
j<<= 1;
}
return (crcout);
}
void generate_crc_table() {
// make CRC lookup table used by table algorithms
int i, j;
unsigned long bit, crc;
for (i=0; i<256; i++) {
crc=(unsigned long)i;
if (refin) crc=reflect(crc, 8);
crc<<= order-8;
for (j=0; j<8; j++) {
bit = crc & crchighbit;
crc<<= 1;
if (bit) crc^= polynom;
}
if (refin) crc = reflect(crc, order);
crc&= crcmask;
crctab[i]= crc;
}
}
unsigned long crctablefast (unsigned char* p, unsigned long len) {
// fast lookup table algorithm without augmented zero bytes, e.g. used in pkzip.
// only usable with polynom orders of 8, 16, 24 or 32.
unsigned long crc = crcinit_direct;
if (refin) crc = reflect(crc, order);
if (!refin) while (len--) crc = (crc << 8) ^ crctab[ ((crc >> (order-8)) & 0xff) ^ *p++];
else while (len--) crc = (crc >> 8) ^ crctab[ (crc & 0xff) ^ *p++];
if (refout^refin) crc = reflect(crc, order);
crc^= crcxor;
crc&= crcmask;
return(crc);
}请注意表函数的代码注释如下:
只适用于8,16,24或32的多数点。
基于表的算法通常仅限于8倍的宽度(特别是使用16位和32位表的表算法)吗?
是否有可能实现一个基于表的CRC算法,它接受任何CRC宽度(不仅仅是8的倍数)?多么?
发布于 2016-10-20 05:48:10
是的,您可以为任何宽度多项式实现基于表的CRCs。请参阅克拉卡尼的输出,例如基于表的实现,例如5位、13位和31位的CRCs。
这件事没什么棘手的。
https://stackoverflow.com/questions/40141505
复制相似问题