首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C中的CRC表算法,宽度限制

C中的CRC表算法,宽度限制
EN

Stack Overflow用户
提问于 2016-10-19 21:03:53
回答 1查看 1.2K关注 0票数 0

我发现了一个易于使用的CRC算法实现这里。它包括基于表和位的算法。代码看起来很好,但是基于表的算法有一个重要的限制。以下是有关守则:

代码语言:javascript
复制
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的倍数)?多么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-20 05:48:10

是的,您可以为任何宽度多项式实现基于表的CRCs。请参阅克拉卡尼的输出,例如基于表的实现,例如5位、13位和31位的CRCs。

这件事没什么棘手的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40141505

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档