在 C/C++ 中计算 32 位 CRC 查找表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26049150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:30:02  来源:igfitidea点击:

Calculate a 32-bit CRC lookup table in C/C++

c++clookup-tablescrc32

提问by Programmer_D

I want to calculate a 32-bit CRC lookup table. One way I tried is by using the following code from this website:

我想计算一个 32 位的 CRC 查找表。我尝试的一种方法是使用本网站的以下代码:

#include <iostream>
#include <stdint.h>

void make_crc_table()
{
    unsigned long POLYNOMIAL = 0x04c11db7;
    unsigned long WIDTH = 8 * sizeof(unsigned long);
    unsigned long TOPBIT = 1 << (WIDTH - 1);
    unsigned long crcTable[256];
    unsigned long remainder;
    // Compute the remainder of each possible dividend
    for (int dividend = 0; dividend < 256; ++dividend)
    {
        // Start with the dividend followed by zeros
        remainder = dividend << (WIDTH - 8);

        // Perform modulo-2 division, a bit at a time
        for (unsigned long bit = 8; bit > 0; --bit)
        {
            // Try to divide the current data bit
            if (remainder & TOPBIT)
            {
                remainder = (remainder << 1) ^ POLYNOMIAL;
            }
            else
            {
                remainder = (remainder << 1);
            }
        }
        crcTable[dividend] = remainder;
    }

    // Print the CRC table
    for (int i = 0; i < 256; i++)
    {
        if (i % 4 == 0)
        {
            std::cout <<"\n";
        }
        std::cout << std::hex << crcTable[i];
        std::cout << ", ";
    }
}

int main()
{
    make_crc_table();
    return 0;
}

Another way I tried is by using the following code that I found from this StackOverflow question, and the code can be downloaded from here in a file called CRC Calculator.zip

我尝试的另一种方法是使用我从这个 StackOverflow question 中找到的以下代码,该代码可以从这里下载到名为 CRC Calculator.zip 的文件中

#include <iostream>
#include <stdint.h>

#define POLYNOMIAL      0x04C11DB7
uint32_t A_crcLookupTable[256] = {0};
#define WIDTH    (8 * sizeof(uint32_t))
#define TOPBIT   (((uint32_t)1) << (WIDTH - 1))

#define FP_reflect_DATA(_DATO)                      (_DATO)
#define FP_reflect_CRCTableValue(_CRCTableValue)    (_CRCTableValue)

uint32_t F_CRC_ObtenValorDeTabla(uint8_t VP_Pos_Tabla)
{
    uint32_t VP_CRCTableValue = 0;
    uint8_t VP_Pos_bit = 0;

    VP_CRCTableValue = ((uint32_t) FP_reflect_DATA(VP_Pos_Tabla)) << (WIDTH - 8);

    for (VP_Pos_bit = 0; VP_Pos_bit < 8; VP_Pos_bit++)
    {
        if (VP_CRCTableValue & TOPBIT)
        {
            VP_CRCTableValue = (VP_CRCTableValue << 1) ^ POLYNOMIAL;
        }
        else
        {
            VP_CRCTableValue = (VP_CRCTableValue << 1);
        }
    }
    return (FP_reflect_CRCTableValue(VP_CRCTableValue));
}

void F_CRC_InicializaTabla(void)
{
    uint16_t VP_Pos_Array = 0;

    for (VP_Pos_Array = 0; VP_Pos_Array < 256; VP_Pos_Array++)
    {
        A_crcLookupTable[VP_Pos_Array] = F_CRC_ObtenValorDeTabla((uint8_t)(VP_Pos_Array &0xFF));

    }

}


void make_crc_table()
{
    F_CRC_InicializaTabla();

    // Print the CRC table
    for (int i = 0; i < 256; i++)
    {
        if (i % 4 == 0)
        {
            std::cout <<"\n";
        }
        std::cout << std::hex << A_crcLookupTable[i];
        std::cout << ", ";
    }
}

int main()
{
    make_crc_table();
    return 0;
}

Here is what the correctfinal table should be, based on this link:

以下是正确的决赛桌应该是什么,基于此链接

// The constants here are for the CRC-32 generator 
// polynomial, as defined in the Microsoft 
// Systems Journal, March 1995, pp. 107-108
CONST
  table: ARRAY[0..255] OF DWORD =
 (
0, 4c11db7, 9823b6e, d4326d9, 
130476dc, 17c56b6b, 1a864db2, 1e475005, 
2608edb8, 22c9f00f, 2f8ad6d6, 2b4bcb61, 
350c9b64, 31cd86d3, 3c8ea00a, 384fbdbd, 
4c11db70, 48d0c6c7, 4593e01e, 4152fda9, 
5f15adac, 5bd4b01b, 569796c2, 52568b75, 
6a1936c8, 6ed82b7f, 639b0da6, 675a1011, 
791d4014, 7ddc5da3, 709f7b7a, 745e66cd, 
9823b6e0, 9ce2ab57, 91a18d8e, 95609039, 
8b27c03c, 8fe6dd8b, 82a5fb52, 8664e6e5, 
be2b5b58, baea46ef, b7a96036, b3687d81, 
ad2f2d84, a9ee3033, a4ad16ea, a06c0b5d, 
d4326d90, d0f37027, ddb056fe, d9714b49, 
c7361b4c, c3f706fb, ceb42022, ca753d95, 
f23a8028, f6fb9d9f, fbb8bb46, ff79a6f1, 
e13ef6f4, e5ffeb43, e8bccd9a, ec7dd02d, 
34867077, 30476dc0, 3d044b19, 39c556ae, 
278206ab, 23431b1c, 2e003dc5, 2ac12072, 
128e9dcf, 164f8078, 1b0ca6a1, 1fcdbb16, 
18aeb13, 54bf6a4, 808d07d, cc9cdca, 
7897ab07, 7c56b6b0, 71159069, 75d48dde, 
6b93dddb, 6f52c06c, 6211e6b5, 66d0fb02, 
5e9f46bf, 5a5e5b08, 571d7dd1, 53dc6066, 
4d9b3063, 495a2dd4, 44190b0d, 40d816ba, 
aca5c697, a864db20, a527fdf9, a1e6e04e, 
bfa1b04b, bb60adfc, b6238b25, b2e29692, 
8aad2b2f, 8e6c3698, 832f1041, 87ee0df6, 
99a95df3, 9d684044, 902b669d, 94ea7b2a, 
e0b41de7, e4750050, e9362689, edf73b3e, 
f3b06b3b, f771768c, fa325055, fef34de2, 
c6bcf05f, c27dede8, cf3ecb31, cbffd686, 
d5b88683, d1799b34, dc3abded, d8fba05a, 
690ce0ee, 6dcdfd59, 608edb80, 644fc637, 
7a089632, 7ec98b85, 738aad5c, 774bb0eb, 
4f040d56, 4bc510e1, 46863638, 42472b8f, 
5c007b8a, 58c1663d, 558240e4, 51435d53, 
251d3b9e, 21dc2629, 2c9f00f0, 285e1d47, 
36194d42, 32d850f5, 3f9b762c, 3b5a6b9b, 
315d626, 7d4cb91, a97ed48, e56f0ff, 
1011a0fa, 14d0bd4d, 19939b94, 1d528623, 
f12f560e, f5ee4bb9, f8ad6d60, fc6c70d7, 
e22b20d2, e6ea3d65, eba91bbc, ef68060b, 
d727bbb6, d3e6a601, dea580d8, da649d6f, 
c423cd6a, c0e2d0dd, cda1f604, c960ebb3, 
bd3e8d7e, b9ff90c9, b4bcb610, b07daba7, 
ae3afba2, aafbe615, a7b8c0cc, a379dd7b, 
9b3660c6, 9ff77d71, 92b45ba8, 9675461f, 
8832161a, 8cf30bad, 81b02d74, 857130c3, 
5d8a9099, 594b8d2e, 5408abf7, 50c9b640, 
4e8ee645, 4a4ffbf2, 470cdd2b, 43cdc09c, 
7b827d21, 7f436096, 7200464f, 76c15bf8, 
68860bfd, 6c47164a, 61043093, 65c52d24, 
119b4be9, 155a565e, 18197087, 1cd86d30, 
29f3d35, 65e2082, b1d065b, fdc1bec, 
3793a651, 3352bbe6, 3e119d3f, 3ad08088, 
2497d08d, 2056cd3a, 2d15ebe3, 29d4f654, 
c5a92679, c1683bce, cc2b1d17, c8ea00a0, 
d6ad50a5, d26c4d12, df2f6bcb, dbee767c, 
e3a1cbc1, e760d676, ea23f0af, eee2ed18, 
f0a5bd1d, f464a0aa, f9278673, fde69bc4, 
89b8fd09, 8d79e0be, 803ac667, 84fbdbd0, 
9abc8bd5, 9e7d9662, 933eb0bb, 97ffad0c, 
afb010b1, ab710d06, a6322bdf, a2f33668, 
bcb4666d, b8757bda, b5365d03, b1f740b4,
000000, 073096, $EE0E612C, 0951BA, 6DC419, 6AF48F, $E963A535, E6495A3,
#include <iostream>
#include <iomanip>

void make_crc_table(unsigned long crcTable[]) {
    unsigned long POLYNOMIAL = 0xEDB88320;
    unsigned long remainder;
    unsigned char b = 0;
    do {
        // Start with the data byte
        remainder = b;
        for (unsigned long bit = 8; bit > 0; --bit) {
            if (remainder & 1)
                remainder = (remainder >> 1) ^ POLYNOMIAL;
            else
                remainder = (remainder >> 1);
        }
        crcTable[(size_t)b] = remainder;
    } while(0 != ++b);
}

unsigned long gen_crc(unsigned char *p, size_t n, unsigned long crcTable[]) {
    unsigned long crc = 0xfffffffful;
    size_t i;
    for(i = 0; i < n; i++)
        crc = crcTable[*p++ ^ (crc&0xff)] ^ (crc>>8);
    return(~crc);
}

int main() {
    unsigned long crcTable[256];
    make_crc_table(crcTable);
    // Print the CRC table
    for (size_t i = 0; i < 256; i++) {
        std::cout << std::setfill('0') << std::setw(8) << std::hex << crcTable[i];
        if (i % 4 == 3)
            std::cout << std::endl;
        else
            std::cout << ", ";
    }
    return 0;
}
EDB8832, DCB8A4, $E0D5E91E, D2D988, B64C2B, EB17CBD, $E7B82D07, BF1D91, DB71064, AB020F2, $F3B97148, BE41DE, ADAD47D, DDDE4EB, $F4D4B551, D385C7, 6C9856, 6BA8C0, $FD62F97A, A65C9EC, 015C4F, 066CD9, $FA0F3D63, D080DF5, B6E20C8, C69105E, $D56041E4, $A2677172, C03E4D1, B04D447, $D20D85FD, $A50AB56B, B5A8FA, B2986C, $DBBBC9D6, $ACBCF940, D86CE3, DF5C75, $DCD60DCF, $ABD13D59, D930AC, DE003A, $C8D75180, $BFD06116, B4F4B5, B3C423, $CFBA9599, $B8BDA50F, 02B89E, F058808, $C60CD9B2, $B10BE924, F6F7C87, 684C11, $C1611DAB, $B6662D3D, DC4190, DB7106, D220BC, $EFD5102A, B18589, B6B51F, FBFE4A5, $E8B8D433, 07C9A2,
uint32_t CRC32_function(uint8_t *buf, uint32_t len){

    uint32_t val, crc;
    uint8_t i;

    crc = 0xFFFFFFFF;
    while(len--){
        val=(crc^*buf++)&0xFF;
        for(i=0; i<8; i++){
            val = val & 1 ? (val>>1)^0xEDB88320 : val>>1;
        }
        crc = val^crc>>8;
    }
    return crc^0xFFFFFFFF;
}
F00F934, 09A88E, $E10E9818, F6A0DBB, 6D3D2D, 646C97, $E6635C01, B6B51F4, C6C6162, 6530D8, $F262004E, C0695ED, B01A57B, 08F4C1, $F50FC457, B0D9C6, B7E950, BBEB8EA, $FCB9887C, DD1DDF, DA2D49, CD37CF3, $FBD44C65, DB26158, AB551CE, $A3BC0074, $D4BB30E2, ADFA541, DD895D7, $A4D1C46D, $D3D6F4FB, 69E96A, 6ED9FC, $AD678846, $DA60B8D0, 042D73, 031DE5, $AA0A4C5F, $DD0D7CC9, 05713C, 0241AA, $BE0B1010, $C90C2086, 68B525, 6F85B3, $B966D409, $CE61E49F, EDEF90E, D9C998, $B0D09822, $C7D7A8B4, B33D17, EB40D81, $B7BD5C3B, $C0BA6CAD, $EDB88320, ABFB3B6, B6E20C, B1D29A, $EAD54739, DD277AF, DB2615, DC1683, $E3630B12, 643B84, ##代码##D6D6A3E, A6A5AA8, $E40ECF0B, 09FF9D, ##代码##A00AE27, D079EB1, $F00F9344, 08A3D2, E01F268, 06C2FE, $F762575D, 6567CB, 6C3671, E6B06E7, $FED41B76, D32BE0, DA7A5A, DD4ACC, $F9B9DF6F, EBEEFF9, B7BE43, B08ED5, $D6D6A3E8, $A1D1937E, D8C2C4, FDFF252, $D1BB67F1, $A6BC5767, FB506DD, B2364B, $D80D2BDA, $AF0A1B4C, 034AF6, 047A60, $DF60EFC3, $A867DF55, 6E8EEF, 69BE79, $CB61B38C, $BC66831A, 6FD2A0, 68E236, $CC0C7795, $BB0B4703, 0216B9, 05262F, $C5BA3BBE, $B2BD0B28, BB45A92, CB36A04, $C2D7FFA7, $B5D0CF31, CD99E8B, BDEAE1D, B64C2B0, $EC63F226, 6AA39C, 6D930A, C0906A9, $EB0E363F, 076785, 005713, BF4A82, $E2B87A14, BB12BAE, ##代码##CB61B38, D28E9B, $E5D5BE0D, CDCEFB7, ##代码##BDBDF21, D3D2D4, $F1D4E242, DDB3F8, FDA836E, BE16CD, $F6B9265B, FB077E1, B74777, 085AE6, $FF0F6A70, 063BCA, 010B5C, F659EFF, $F862AE69, 6BFFD3, 6CCF45, $A00AE278, $D70DD2EE, E048354, 03B3C2, $A7672661, $D06016F7, 69474D, E6E77DB, $AED16A4A, $D9D65ADC, DF0B66, D83BF0, $A9BCAE53, $DEBB9EC5, B2CF7F, B5FFE9, $BDBDF21C, $CABAC28A, B39330, B4A3A6, $BAD03605, $CDD70693, DE5729, D967BF, $B3667A2E, $C4614AB8, D681B02, A6F2B94, $B40BBE37, $C30C8EA1, A05DF1B, D02EF8D);

However, this is what my output is from both programs (I diffed the output, and it's the same for both of them), and it's incorrect:

但是,这就是我来自两个程序的输出(我对输出进行了差异化,并且它们都相同),这是不正确的

##代码##

回答by rcgldr

The bits are reversed. Note that the table entry for array[0x80](0x80 is 0x01 reversed) = 0xEDB88320, which is 0x04C11DB7reversed.

这些位是相反的。请注意,array[0x80](0x80 is 0x01 reversed)的表条目= 0xEDB883200x04C11DB7颠倒的。

Example code:

示例代码:

##代码##

回答by 3r6

If there is not enough Flash and RAM (embedded software), you can use this function:

如果没有足够的 Flash 和 RAM(嵌入式软件),可以使用此功能:

##代码##

But CRC calculation takes more time.

但是CRC计算需要更多的时间。