一、概述

关于crc的原理网上由很多了,讲的都比较详细。具体crc原理可参见
https://www.cnblogs.com/esestt/archive/2007/08/09/848856.html。看来看去,关于代码实现部分网上都是互相拷贝,没有一个完整的crc实现,因此想着把所有crc8/16/32基于查表法的实现在该博客写下来,想着说一句crc你如果只是使用,只看我的代码就够了。万一发现有bug,上一句话当我没说…

1.CRC与哪些配置有关

关于crc,有5个配置项决定了crc的计算方式,他们分别是多项式、余数初始值、结果异或值、输入数据正/反序、输出数据正/反序五个配置。这其中任何一个配置不同,得到的crc结果都将不同。

1.1. 多项式又称为简记式,生成多项式的最高幂次项系数是固定的1,在简记式计算中,将最高的1统一去掉了,比如多项式CRC-8 x8+x5+x4+1的简记式为 0x31。

1.2 余数初始值(有的叫初始值),参看crc计算模2除法原理我们知道,如果crc计算数据前加若干0是不影响crc最终计算结果的,这是不可接受的,因此引入余数初始值。以0xFFFFFFFF作为余数初值为例,其相当于在 假设DATA = (D1, D2, … Dn), 那么其等于 FF^D1, FF^D2, FF^D3, FF^D4, D5, … Dn 的 CRC校验 。实际上,我们crc的迭代计算,我们计算出前一部分的crc1,将crc1继续迭代得到最终的crc,本身这个ctc1和这里的余数初始值是相同的效果,换句话说,迭代的crc接口前一部分的crc结果作为余数初始值传入,便可得到最终的crc值,这在某些为了省内存空间的情况下是等效的。

1.3 结果异或值,就是当我们crc计算得出结果后,和某个值再进行异或操作,获得最终结果(不进行异或,等价于异或0)。显然,结果异或值的计算只是在最后一次计算时使用,并不参与迭代。

1.4 输入数据正/反序,我们要计算的一组数据一般情况下将左边的位看成高位,从左到右排列进行计算,但是实际上我们也可能将要计算的数据左边作为低位,右边作为高位,而这就是将输入数据进行反序计算。

1.5 输出数据正/反序,当我们得到crc计算结果后后,也可能将crc的计算结果进行以下高低位颠倒再进行输出,这就是对应的输出数据反序。显然,输出数据的反序也是只计算一次,并不参与迭代。

常见的crc算法有如下:

2.代码实现

这里将crc8/16/32的接口按照正入正出/正入反出/反入正出/反入反出提供了四类接口,当然你可以合成一个接口实现。另外,再次情调的是,在嵌入式某些内存紧张的条件下,你可以计算部分数据的crc,然后将该crc作为余数初始值继续计算获得最终的crc计算结果。
这里说crc查表法的两个规律可参考:

  • 1.相同简记式且相同输入数据序的crc码表是一样的
  • 2.正序表crc表规律:生成的第二个元素正好对应相应的简记式

在这里插入代码片
/**

  • crc 查表算法实现文件

*/
#include “crc.h”
#include <stdio.h>
#include <string.h>

#define CRC_8_WIDTH 8 //crc8 位宽始终为8
#define CRC_16_WIDTH 16 //crc16 位宽始终为16
#define CRC_32_WIDTH 32 //crc16 位宽始终为32

unsigned char reflected_c(unsigned char d);
unsigned short reflected_w(unsigned short d);
unsigned int reflected_dw(unsigned int d);

//反序crc8 对应生成的crc码表
static unsigned char Crc8_Table_Reflected[256] = { 0 };
//crc8 对应生成的crc码表(注:当前表值是简记式0x07、数据正序时对应表)
static unsigned char Crc8_Table[256] = {
0x00,0x07,0x0e,0x09,0x1c,0x1b,0x12,0x15,0x38,0x3f,0x36,0x31,0x24,0x23,0x2a,0x2d,
0x70,0x77,0x7e,0x79,0x6c,0x6b,0x62,0x65,0x48,0x4f,0x46,0x41,0x54,0x53,0x5a,0x5d,
0xe0,0xe7,0xee,0xe9,0xfc,0xfb,0xf2,0xf5,0xd8,0xdf,0xd6,0xd1,0xc4,0xc3,0xca,0xcd,
0x90,0x97,0x9e,0x99,0x8c,0x8b,0x82,0x85,0xa8,0xaf,0xa6,0xa1,0xb4,0xb3,0xba,0xbd,
0xc7,0xc0,0xc9,0xce,0xdb,0xdc,0xd5,0xd2,0xff,0xf8,0xf1,0xf6,0xe3,0xe4,0xed,0xea,
0xb7,0xb0,0xb9,0xbe,0xab,0xac,0xa5,0xa2,0x8f,0x88,0x81,0x86,0x93,0x94,0x9d,0x9a,
0x27,0x20,0x29,0x2e,0x3b,0x3c,0x35,0x32,0x1f,0x18,0x11,0x16,0x03,0x04,0x0d,0x0a,
0x57,0x50,0x59,0x5e,0x4b,0x4c,0x45,0x42,0x6f,0x68,0x61,0x66,0x73,0x74,0x7d,0x7a,
0x89,0x8e,0x87,0x80,0x95,0x92,0x9b,0x9c,0xb1,0xb6,0xbf,0xb8,0xad,0xaa,0xa3,0xa4,
0xf9,0xfe,0xf7,0xf0,0xe5,0xe2,0xeb,0xec,0xc1,0xc6,0xcf,0xc8,0xdd,0xda,0xd3,0xd4,
0x69,0x6e,0x67,0x60,0x75,0x72,0x7b,0x7c,0x51,0x56,0x5f,0x58,0x4d,0x4a,0x43,0x44,
0x19,0x1e,0x17,0x10,0x05,0x02,0x0b,0x0c,0x21,0x26,0x2f,0x28,0x3d,0x3a,0x33,0x34,
0x4e,0x49,0x40,0x47,0x52,0x55,0x5c,0x5b,0x76,0x71,0x78,0x7f,0x6a,0x6d,0x64,0x63,
0x3e,0x39,0x30,0x37,0x22,0x25,0x2c,0x2b,0x06,0x01,0x08,0x0f,0x1a,0x1d,0x14,0x13,
0xae,0xa9,0xa0,0xa7,0xb2,0xb5,0xbc,0xbb,0x96,0x91,0x98,0x9f,0x8a,0x8d,0x84,0x83,
0xde,0xd9,0xd0,0xd7,0xc2,0xc5,0xcc,0xcb,0xe6,0xe1,0xe8,0xef,0xfa,0xfd,0xf4,0xf3,
};

/* 函数名:生成crc8 的256字节的crc码表

  • crctable_8: 生成的crc8校验表首地址

  • simple_formula: crc8使用的简记式

  • note:正序表接口
    /
    static void Mk_crc8_table_256(unsigned char
    crctable_8, unsigned char simple_formula)
    {
    unsigned int i;
    unsigned char crc_8 = 0;
    int bit;

    for (i = 0; i < 256; i++)
    {
    /* Initialize the remainder. /
    crc_8 = i << (CRC_8_WIDTH - 8);
    /
    Shift and XOR with the polynomial. /
    for (bit = 0; bit < 8; bit++)
    {
    /
    Try to divide the current data bit. /
    if (crc_8 & (1 << (CRC_8_WIDTH - 1)))
    {
    crc_8 = (crc_8 << 1) ^ simple_formula;
    }
    else
    {
    crc_8 = crc_8 << 1;
    }
    }
    /
    Save the result in the table. */
    crctable_8[i] = crc_8;
    }
    }

/**

  • 函数名:正序crc8获取一组数据的crc校验值
  • @para
  •   message :用来计算的数据地址   nBytes:数据的长度  remainder_init:余数初始值  final_xor_value:结果异或值
    

/
unsigned char Crc8_calculate(const unsigned char
message, unsigned int nBytes, unsigned char remainder_init, unsigned char final_xor_value)
{
unsigned int offset;
unsigned char byte;
unsigned char remainder = remainder_init;
/* Divide the message by the polynomial, a byte at a time. /
for (offset = 0; offset < nBytes; offset++)
{
byte = (remainder >> (CRC_8_WIDTH - 8)) ^ message[offset];
remainder = Crc8_Table[byte] ^ (remainder << 8);
}
/
The final remainder is the CRC result. /
return (remainder ^ final_xor_value);
} /
crcCompute() */

/**

  • 函数名:输入正序 输出反序 crc8获取一组数据的crc校验值
  • @para
  •   message :用来计算的数据地址   nBytes:数据的长度  remainder_init:余数初始值,如果没有,则初始值为0  final_xor_value:结果异或值,如果没有异或,则为0
    

/
unsigned char Crc8_calculate_refout(const unsigned char
message, unsigned int nBytes, unsigned char remainder_init, unsigned char final_xor_value)
{
unsigned char remainder = 0;

remainder = Crc8_calculate(message,nBytes,remainder_init,0);
/* The final remainder is the CRC result. */
return (reflected_c(remainder) ^ final_xor_value);

} /* crcCompute() */

/**

  • 函数名:输入反序 输出正序 crc8获取一组数据的crc校验值
  • @para
  •   message :用来计算的数据地址   nBytes:数据的长度  remainder_init:余数初始值  final_xor_value:结果异或值
    

/
unsigned char Crc8_calculate_refin(const unsigned char
message, unsigned int nBytes, unsigned char remainder_init, unsigned char final_xor_value)
{
unsigned char remainder = 0;

remainder = Crc8_calculate_refinout(message, nBytes, remainder_init, 0);
/* The final remainder is the CRC result. */
return (reflected_c(remainder) ^ final_xor_value);

}

/**

  • 函数名:输入反序 输出反序 crc8获取一组数据的crc校验值
  • @para
  •   message :用来计算的数据地址   nBytes:数据的长度  remainder_init:余数初始值  final_xor_value:结果异或值
    

/
unsigned char Crc8_calculate_refinout(const unsigned char
message, unsigned int nBytes, unsigned char remainder_init, unsigned char final_xor_value)
{
unsigned char remainder = remainder_init;
/* Divide the message by the polynomial, a byte at a time. */
while (nBytes–)
{
remainder = Crc8_Table_Reflected[(remainder & 0xff) ^ message++];
}
/
The final remainder is the CRC result. */
return (remainder ^ final_xor_value);

} /* crcCompute() */

//反序crc16 对应生成的crc码表
static unsigned short Crc16_Table_Reflected[256] = { 0 };
//crc16 对应生成的crc码表(注:当前表值是简记式0x1021、数据正序时对应表)
static unsigned short Crc16_Table[256] = {
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
};

/* 函数名:生成crc16 的256字节的crc码表

  • crctable_16: 生成的crc16校验表首地址

  • simple_formula: crc16使用的简记式
    */
    static void Mk_crc16_table_256(unsigned short * crctable_16, unsigned short simple_formula)
    {
    unsigned int i;
    unsigned short cRc_16 = 0;
    int bit;

    for (i = 0; i < 256; i++)
    {
    /* Initialize the remainder. /
    cRc_16 = i << (CRC_16_WIDTH - 8);
    /
    Shift and XOR with the polynomial. /
    for (bit = 0; bit < 8; bit++)
    {
    /
    Try to divide the current data bit. /
    if (cRc_16 & (1 << (CRC_16_WIDTH - 1)))
    {
    cRc_16 = (cRc_16 << 1) ^ simple_formula;
    }
    else
    {
    cRc_16 = cRc_16 << 1;
    }
    }
    /
    Save the result in the table. /
    crctable_16[i] = cRc_16;
    }
    }
    /
    *

  • 函数名:输入正序 输出正序 crc16获取一组数据的crc校验值

  • @para

  •   message :用来计算的数据地址   nBytes:数据的长度  remainder_init:余数初始值,如果没有,则初始值为0  final_xor_value:结果异或值,如果没有异或,则为0
    

/
unsigned short Crc16_calculate(const unsigned char * message, unsigned int nBytes, unsigned short remainder_init, unsigned short final_xor_value)
{
unsigned int offset;
unsigned char byte;
unsigned short remainder = remainder_init;
/
Divide the message by the polynomial, a byte at a time. /
for (offset = 0; offset < nBytes; offset++)
{
byte = (remainder >> (CRC_16_WIDTH - 8)) ^ message[offset];
remainder = Crc16_Table[byte] ^ (remainder << 8);
}
/
The final remainder is the CRC result. /
return (remainder ^ final_xor_value);
} /
crcCompute() */

/**

  • 函数名:输入正序 输出反序 crc16获取一组数据的crc校验值
  • @para
  •   message :用来计算的数据地址   nBytes:数据的长度  remainder_init:余数初始值,如果没有,则初始值为0  final_xor_value:结果异或值,如果没有异或,则为0
    

/
unsigned short Crc16_calculate_refout(const unsigned char
message, unsigned int nBytes, unsigned short remainder_init, unsigned short final_xor_value)
{
unsigned int offset;
unsigned char byte;
unsigned short remainder = remainder_init;
/* Divide the message by the polynomial, a byte at a time. /
for (offset = 0; offset < nBytes; offset++)
{
byte = (remainder >> (CRC_16_WIDTH - 8)) ^ message[offset];
remainder = Crc16_Table[byte] ^ (remainder << 8);
}
/
The final remainder is the CRC result. /
return (reflected_w(remainder) ^ final_xor_value);
} /
crcCompute() */

/**

  • 函数名:输入反序 输出正序 crc16获取一组数据的crc校验值
  • @para
  • message :用来计算的数据地址   nBytes:数据的长度  remainder_init:余数初始值  final_xor_value:结果异或值
    

/
unsigned short Crc16_calculate_refin(const unsigned char
message, unsigned int nBytes, unsigned short remainder_init, unsigned short final_xor_value)
{
unsigned short remainder = 0;

remainder = Crc16_calculate_refinout(message, nBytes,remainder_init,0);
return (reflected_w(remainder) ^ final_xor_value);

} /* crcCompute() */

/**

  • 函数名:输入反序 输出反序 crc16获取一组数据的crc校验值
  • @para
  • message :用来计算的数据地址   nBytes:数据的长度  remainder_init:余数初始值  final_xor_value:结果异或值
    

/
unsigned short Crc16_calculate_refinout(const unsigned char * message, unsigned int nBytes, unsigned short remainder_init, unsigned short final_xor_value)
{
unsigned short remainder = remainder_init;
/
Divide the message by the polynomial, a byte at a time. */
while (nBytes–)
{
remainder = (remainder >> 8) ^ Crc16_Table_Reflected[(remainder & 0xff) ^ message++];
}
/
The final remainder is the CRC result. */
return (remainder ^ final_xor_value);

} /* crcCompute() */

//反序crc32 对应生成的crc码表
static unsigned int Crc32_Table_Reflected[256] = { 0 };
//正序crc32 对应生成的crc码表(注:当前表值简记式0x04c11db7、数据正序时对应表)
static unsigned int Crc32_Table[256] = {
0X00000000L, 0X04c11db7L, 0X09823b6eL, 0X0d4326d9L, 0X130476dcL, 0X17c56b6bL, 0X1a864db2L,
0X1e475005L, 0X2608edb8L, 0X22c9f00fL, 0X2f8ad6d6L, 0X2b4bcb61L, 0X350c9b64L, 0X31cd86d3L,
0X3c8ea00aL, 0X384fbdbdL, 0X4c11db70L, 0X48d0c6c7L, 0X4593e01eL, 0X4152fda9L, 0X5f15adacL,
0X5bd4b01bL, 0X569796c2L, 0X52568b75L, 0X6a1936c8L, 0X6ed82b7fL, 0X639b0da6L, 0X675a1011L,
0X791d4014L, 0X7ddc5da3L, 0X709f7b7aL, 0X745e66cdL, 0X9823b6e0L, 0X9ce2ab57L, 0X91a18d8eL,
0X95609039L, 0X8b27c03cL, 0X8fe6dd8bL, 0X82a5fb52L, 0X8664e6e5L, 0Xbe2b5b58L, 0Xbaea46efL,
0Xb7a96036L, 0Xb3687d81L, 0Xad2f2d84L, 0Xa9ee3033L, 0Xa4ad16eaL, 0Xa06c0b5dL, 0Xd4326d90L,
0Xd0f37027L, 0Xddb056feL, 0Xd9714b49L, 0Xc7361b4cL, 0Xc3f706fbL, 0Xceb42022L, 0Xca753d95L,
0Xf23a8028L, 0Xf6fb9d9fL, 0Xfbb8bb46L, 0Xff79a6f1L, 0Xe13ef6f4L, 0Xe5ffeb43L, 0Xe8bccd9aL,
0Xec7dd02dL, 0X34867077L, 0X30476dc0L, 0X3d044b19L, 0X39c556aeL, 0X278206abL, 0X23431b1cL,
0X2e003dc5L, 0X2ac12072L, 0X128e9dcfL, 0X164f8078L, 0X1b0ca6a1L, 0X1fcdbb16L, 0X018aeb13L,
0X054bf6a4L, 0X0808d07dL, 0X0cc9cdcaL, 0X7897ab07L, 0X7c56b6b0L, 0X71159069L, 0X75d48ddeL,
0X6b93dddbL, 0X6f52c06cL, 0X6211e6b5L, 0X66d0fb02L, 0X5e9f46bfL, 0X5a5e5b08L, 0X571d7dd1L,
0X53dc6066L, 0X4d9b3063L, 0X495a2dd4L, 0X44190b0dL, 0X40d816baL, 0Xaca5c697L, 0Xa864db20L,
0Xa527fdf9L, 0Xa1e6e04eL, 0Xbfa1b04bL, 0Xbb60adfcL, 0Xb6238b25L, 0Xb2e29692L, 0X8aad2b2fL,
0X8e6c3698L, 0X832f1041L, 0X87ee0df6L, 0X99a95df3L, 0X9d684044L, 0X902b669dL, 0X94ea7b2aL,
0Xe0b41de7L, 0Xe4750050L, 0Xe9362689L, 0Xedf73b3eL, 0Xf3b06b3bL, 0Xf771768cL, 0Xfa325055L,
0Xfef34de2L, 0Xc6bcf05fL, 0Xc27dede8L, 0Xcf3ecb31L, 0Xcbffd686L, 0Xd5b88683L, 0Xd1799b34L,
0Xdc3abdedL, 0Xd8fba05aL, 0X690ce0eeL, 0X6dcdfd59L, 0X608edb80L, 0X644fc637L, 0X7a089632L,
0X7ec98b85L, 0X738aad5cL, 0X774bb0ebL, 0X4f040d56L, 0X4bc510e1L, 0X46863638L, 0X42472b8fL,
0X5c007b8aL, 0X58c1663dL, 0X558240e4L, 0X51435d53L, 0X251d3b9eL, 0X21dc2629L, 0X2c9f00f0L,
0X285e1d47L, 0X36194d42L, 0X32d850f5L, 0X3f9b762cL, 0X3b5a6b9bL, 0X0315d626L, 0X07d4cb91L,
0X0a97ed48L, 0X0e56f0ffL, 0X1011a0faL, 0X14d0bd4dL, 0X19939b94L, 0X1d528623L, 0Xf12f560eL,
0Xf5ee4bb9L, 0Xf8ad6d60L, 0Xfc6c70d7L, 0Xe22b20d2L, 0Xe6ea3d65L, 0Xeba91bbcL, 0Xef68060bL,
0Xd727bbb6L, 0Xd3e6a601L, 0Xdea580d8L, 0Xda649d6fL, 0Xc423cd6aL, 0Xc0e2d0ddL, 0Xcda1f604L,
0Xc960ebb3L, 0Xbd3e8d7eL, 0Xb9ff90c9L, 0Xb4bcb610L, 0Xb07daba7L, 0Xae3afba2L, 0Xaafbe615L,
0Xa7b8c0ccL, 0Xa379dd7bL, 0X9b3660c6L, 0X9ff77d71L, 0X92b45ba8L, 0X9675461fL, 0X8832161aL,
0X8cf30badL, 0X81b02d74L, 0X857130c3L, 0X5d8a9099L, 0X594b8d2eL, 0X5408abf7L, 0X50c9b640L,
0X4e8ee645L, 0X4a4ffbf2L, 0X470cdd2bL, 0X43cdc09cL, 0X7b827d21L, 0X7f436096L, 0X7200464fL,
0X76c15bf8L, 0X68860bfdL, 0X6c47164aL, 0X61043093L, 0X65c52d24L, 0X119b4be9L, 0X155a565eL,
0X18197087L, 0X1cd86d30L, 0X029f3d35L, 0X065e2082L, 0X0b1d065bL, 0X0fdc1becL, 0X3793a651L,
0X3352bbe6L, 0X3e119d3fL, 0X3ad08088L, 0X2497d08dL, 0X2056cd3aL, 0X2d15ebe3L, 0X29d4f654L,
0Xc5a92679L, 0Xc1683bceL, 0Xcc2b1d17L, 0Xc8ea00a0L, 0Xd6ad50a5L, 0Xd26c4d12L, 0Xdf2f6bcbL,
0Xdbee767cL, 0Xe3a1cbc1L, 0Xe760d676L, 0Xea23f0afL, 0Xeee2ed18L, 0Xf0a5bd1dL, 0Xf464a0aaL,
0Xf9278673L, 0Xfde69bc4L, 0X89b8fd09L, 0X8d79e0beL, 0X803ac667L, 0X84fbdbd0L, 0X9abc8bd5L,
0X9e7d9662L, 0X933eb0bbL, 0X97ffad0cL, 0Xafb010b1L, 0Xab710d06L, 0Xa6322bdfL, 0Xa2f33668L,
0Xbcb4666dL, 0Xb8757bdaL, 0Xb5365d03L, 0Xb1f740b4L };

static void Mk_crc32_table_256(unsigned int* crctable_32, unsigned int simple_formula)
{
unsigned int i;
unsigned int cRc_32 = 0;
int bit;

for (i = 0; i < 256; i++)
{/* Initialize the remainder.  */cRc_32 = i << (CRC_32_WIDTH - 8);/* Shift and XOR with the polynomial.   */for (bit = 0; bit < 8; bit++){/* Try to divide the current data bit.  */if (cRc_32 & (1 << (CRC_32_WIDTH - 1))){cRc_32 = (cRc_32 << 1) ^ simple_formula;}else{cRc_32 = cRc_32 << 1;}}/* Save the result in the table. */crctable_32[i] = cRc_32;
}

}

/**

  • 函数名:输入正序 输出正序 crc32获取一组数据的crc校验值
  • @para
  •   message :用来计算的数据地址   nBytes:数据的长度  remainder_init:余数初始值  final_xor_value:结果异或值
    

/
unsigned int Crc32_calculate(const unsigned char
message, unsigned int nBytes, unsigned int remainder_init, unsigned int final_xor_value)
{
unsigned int offset;
unsigned char byte;
unsigned int remainder = remainder_init;
/* Divide the message by the polynomial, a byte at a time. /
for (offset = 0; offset < nBytes; offset++)
{
byte = (remainder >> (CRC_32_WIDTH - 8)) ^ message[offset];
remainder = Crc32_Table[byte] ^ (remainder << 8);
}
/
The final remainder is the CRC result. /
return (remainder ^ final_xor_value);
} /
crcCompute() */

/**

  • 函数名:输入正序 输出反序 crc32获取一组数据的crc校验值
  • @para
  •   message :用来计算的数据地址   nBytes:数据的长度  remainder_init:余数初始值  final_xor_value:结果异或值
    

/
unsigned int Crc32_calculate_refout(const unsigned char
message, unsigned int nBytes, unsigned int remainder_init, unsigned int final_xor_value)
{
unsigned int remainder = 0;

remainder = Crc32_calculate(message, nBytes, remainder_init, 0);
/* The final remainder is the CRC result. */
return (reflected_dw(remainder) ^ final_xor_value);

}

/**

  • 函数名:输入反序 输出正序 crc32获取一组数据的crc校验值
  • @para
  •   message :用来计算的数据地址   nBytes:数据的长度  remainder_init:余数初始值  final_xor_value:结果异或值
    

/
unsigned int Crc32_calculate_refin(const unsigned char
message, unsigned int nBytes, unsigned int remainder_init, unsigned int final_xor_value)
{
unsigned int remainder = 0;

remainder = Crc32_calculate_refinout(message, nBytes, remainder_init, 0);
return (reflected_dw(remainder) ^ final_xor_value);

} /* crcCompute() */

/**

  • 函数名:输入反序 输出反序 crc32获取一组数据的crc校验值
  • @para
  •   message :用来计算的数据地址   nBytes:数据的长度  remainder_init:余数初始值  final_xor_value:结果异或值
    

/
unsigned int Crc32_calculate_refinout(const unsigned char
message, unsigned int nBytes, unsigned int remainder_init, unsigned int final_xor_value)
{
unsigned int remainder = remainder_init;
/* Divide the message by the polynomial, a byte at a time. */
while (nBytes–)
{
remainder = (remainder >> 8) ^ Crc32_Table_Reflected[(remainder & 0xff) ^ message++];
}
/
The final remainder is the CRC result. /
return (remainder ^ final_xor_value);
} /
crcCompute() */

//由正序表得到反序表接口
unsigned char reflected(unsigned char b)
{
unsigned char c = 0;
unsigned char i ;
for ( i = 0; i<8; i++)
{
c <<= 1;
if (b & 1) c |= 1;
b >>= 1;
}
return c;
}

unsigned char reflected_c(unsigned char d)
{
unsigned char c = 0;
unsigned int i;
for (i = 0; i < 8; i++)
{
c <<= 1;
if (d & 1) c |= 1;
d >>= 1;
}
return c;
}

unsigned short reflected_w(unsigned short d)
{
unsigned short c = 0;
unsigned int i;
for ( i = 0; i<16; i++)
{
c <<= 1;
if (d & 1) c |= 1;
d >>= 1;
}
return c;
}

unsigned int reflected_dw(unsigned int d)
{
unsigned int c = 0;
unsigned int i;
for (i = 0; i < 32; i++)
{
c <<= 1;
if (d & 1) c |= 1;
d >>= 1;
}
return c;
}

//由正序表得到反序表
void RefTable_Init()
{
unsigned int i;
for (i = 0; i<256; i++)
{
Crc16_Table_Reflected[i] = reflected_w(Crc16_Table[reflected(i)]);
Crc32_Table_Reflected[i] = reflected_dw(Crc32_Table[reflected(i)]);
Crc8_Table_Reflected[i] = reflected_c(Crc8_Table[reflected(i)]);
}
}

unsigned char tmp[] = { 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38 };

int main(void)
{

unsigned char dataB[4] = { 0 };
/* crc8正序验证 (输入正序、 输出正序) */
Mk_crc8_table_256(Crc8_Table, 0x07);
Mk_crc16_table_256(Crc16_Table, 0x1021);
Mk_crc32_table_256(Crc32_Table, 0X04c11db7);
RefTable_Init();dataB[0] = Crc8_calculate(tmp, sizeof(tmp), 0, 0);
printf("%x\n", dataB[0]);dataB[1] = Crc8_calculate(tmp, sizeof(tmp), 0, 0xff);
printf("%x\n", dataB[1]);dataB[2] = Crc8_calculate(tmp, sizeof(tmp), 0xff, 0);
printf("%x\n", dataB[2]);dataB[3] = Crc8_calculate(tmp, sizeof(tmp), 0xff, 0xff);
printf("%x\n", dataB[3]);/*crc8正序验证 (输入正序 输出反序)*/
dataB[0] = Crc8_calculate_refout(tmp, sizeof(tmp), 0, 0);
printf("%x\n", dataB[0]);dataB[1] = Crc8_calculate_refout(tmp, sizeof(tmp), 0, 0xff);
printf("%x\n", dataB[1]);dataB[2] = Crc8_calculate_refout(tmp, sizeof(tmp), 0xff, 0);
printf("%x\n", dataB[2]);dataB[3] = Crc8_calculate_refout(tmp, sizeof(tmp), 0xff, 0xff);
printf("%x\n", dataB[3]);/* crc8反序验证 (输入反序、 输出反序) */
dataB[0] = Crc8_calculate_refinout(tmp, sizeof(tmp), 0, 0);
printf("%x\n", dataB[0]);dataB[1] = Crc8_calculate_refinout(tmp, sizeof(tmp), 0, 0xff);
printf("%x\n", dataB[1]);dataB[2] = Crc8_calculate_refinout(tmp, sizeof(tmp), 0xff, 0);
printf("%x\n", dataB[2]);dataB[3] = Crc8_calculate_refinout(tmp, sizeof(tmp), 0xff, 0xff);
printf("%x\n", dataB[3]);/* crc8反序验证 (输入反序、 输出正序) */
dataB[0] = Crc8_calculate_refin(tmp, sizeof(tmp), 0, 0);
printf("%x\n", dataB[0]);dataB[1] = Crc8_calculate_refin(tmp, sizeof(tmp), 0, 0xff);
printf("%x\n", dataB[1]);dataB[2] = Crc8_calculate_refin(tmp, sizeof(tmp), 0xff, 0);
printf("%x\n", dataB[2]);dataB[3] = Crc8_calculate_refin(tmp, sizeof(tmp), 0xff, 0xff);
printf("%x\n", dataB[3]);printf("\n\n");unsigned short ret[4] = { 0 };/* crc16正序验证 (输入正序、 输出正序) */ret[0] = Crc16_calculate(tmp, sizeof(tmp), 0, 0);
printf("%x\n", ret[0]);ret[1] = Crc16_calculate(tmp, sizeof(tmp), 0, 0xffff);
printf("%x\n", ret[1]);ret[2] = Crc16_calculate(tmp, sizeof(tmp), 0xffff, 0);
printf("%x\n", ret[2]);ret[3] = Crc16_calculate(tmp, sizeof(tmp), 0xffff, 0xffff);
printf("%x\n", ret[3]);/*crc16正序验证 (输入正序 输出反序)*/
ret[0] = Crc16_calculate_refout(tmp, sizeof(tmp), 0, 0);
printf("%x\n", ret[0]);ret[1] = Crc16_calculate_refout(tmp, sizeof(tmp), 0, 0xffff);
printf("%x\n", ret[1]);ret[2] = Crc16_calculate_refout(tmp, sizeof(tmp), 0xffff, 0);
printf("%x\n", ret[2]);ret[3] = Crc16_calculate_refout(tmp, sizeof(tmp), 0xffff, 0xffff);
printf("%x\n", ret[3]);/* crc16反序验证 (输入反序、 输出反序) */ret[0] = Crc16_calculate_refinout(tmp, sizeof(tmp), 0, 0);
printf("%x\n", ret[0]);ret[1] = Crc16_calculate_refinout(tmp, sizeof(tmp), 0, 0xffff);
printf("%x\n", ret[1]);ret[2] = Crc16_calculate_refinout(tmp, sizeof(tmp), 0xffff, 0);
printf("%x\n", ret[2]);ret[3] = Crc16_calculate_refinout(tmp, sizeof(tmp), 0xffff, 0xffff);
printf("%x\n", ret[3]);/* crc16反序验证 (输入反序、 输出正序) */
ret[0] = Crc16_calculate_refin(tmp, sizeof(tmp), 0, 0);
printf("%x\n", ret[0]);ret[1] = Crc16_calculate_refin(tmp, sizeof(tmp), 0, 0xffff);
printf("%x\n", ret[1]);ret[2] = Crc16_calculate_refin(tmp, sizeof(tmp), 0xffff, 0);
printf("%x\n", ret[2]);ret[3] = Crc16_calculate_refin(tmp, sizeof(tmp), 0xffff, 0xffff);
printf("%x\n", ret[3]);printf("\n\n");unsigned int dataDw[4] = { 0 };
/* crc32正序验证 (输入正序、 输出正序) */dataDw[0] = Crc32_calculate(tmp, sizeof(tmp), 0, 0);
printf("%x\n", dataDw[0]);dataDw[1] = Crc32_calculate(tmp, sizeof(tmp), 0, 0xffffffff);
printf("%x\n", dataDw[1]);dataDw[2] = Crc32_calculate(tmp, sizeof(tmp), 0xffffffff, 0);
printf("%x\n", dataDw[2]);dataDw[3] = Crc32_calculate(tmp, sizeof(tmp), 0xffffffff, 0xffffffff);
printf("%x\n", dataDw[3]);/*crc32正序验证 (输入正序 输出反序)*/
dataDw[0] = Crc32_calculate_refout(tmp, sizeof(tmp), 0, 0);
printf("%x\n", dataDw[0]);dataDw[1] = Crc32_calculate_refout(tmp, sizeof(tmp), 0, 0xffffffff);
printf("%x\n", dataDw[1]);dataDw[2] = Crc32_calculate_refout(tmp, sizeof(tmp), 0xffffffff, 0);
printf("%x\n", dataDw[2]);dataDw[3] = Crc32_calculate_refout(tmp, sizeof(tmp), 0xffffffff, 0xffffffff);
printf("%x\n", dataDw[3]);/* crc32反序验证 (输入反序、 输出反序) */dataDw[0] = Crc32_calculate_refinout(tmp, sizeof(tmp), 0, 0);
printf("%x\n", dataDw[0]);dataDw[1] = Crc32_calculate_refinout(tmp, sizeof(tmp), 0, 0xffffffff);
printf("%x\n", dataDw[1]);dataDw[2] = Crc32_calculate_refinout(tmp, sizeof(tmp), 0xffffffff, 0);
printf("%x\n", dataDw[2]);dataDw[3] = Crc32_calculate_refinout(tmp, sizeof(tmp), 0xffffffff, 0xffffffff);
printf("%x\n", dataDw[3]);/* crc32反序验证 (输入反序、 输出正序) */
dataDw[0] = Crc32_calculate_refin(tmp, sizeof(tmp), 0, 0);
printf("%x\n", dataDw[0]);dataDw[1] = Crc32_calculate_refin(tmp, sizeof(tmp), 0, 0xffffffff);
printf("%x\n", dataDw[1]);dataDw[2] = Crc32_calculate_refin(tmp, sizeof(tmp), 0xffffffff, 0);
printf("%x\n", dataDw[2]);dataDw[3] = Crc32_calculate_refin(tmp, sizeof(tmp), 0xffffffff, 0xffffffff);
printf("%x\n", dataDw[3]);return getchar();

}

crc原理及c代码实现相关推荐

  1. 蓝牙:CRC原理详解(附crc16校验代码)

    CRC原理详解(附crc16校验代码) 参考链接: https://www.cnblogs.com/esestt/archive/2007/08/09/848856.html Cyclic Redun ...

  2. CRC爆破png图片宽度和高度原理以及python代码

    CRC爆破png图片宽度和高度原理以及python代码 文章目录 CRC爆破png图片宽度和高度原理以及python代码 1.PNG图片的格式 2.CRC 3.Python爆破图片宽度和高度 参考文章 ...

  3. CRC原理详解(附crc16校验代码)

    CRC原理详解 算法原理 查表法 反向算法 附录1:crc16校验表及用法 算法原理 Cyclic Redundancy Check循环冗余检验,是基于数据计算一组效验码,用于核对数据传输过程中是否被 ...

  4. CRC原理及其逆向分析方法

    CRC原理及其逆向破解方法: 介绍: 这篇短文包含CRC原理介绍和其逆向分析方法,很多程序员和破解者不是很清楚了解 CRC的工作原理,而且几乎没人知道如何逆向分析它的方法,事实上它是非常有用的. 首先 ...

  5. 我学习CRC32、CRC16、CRC原理和算法的总结(与WINRAR结果一致)

    原文地址:http://wenku.baidu.com/view/fb791c0203d8ce2f006623f5.html 我学习CRC32.CRC16.CRC原理和算法的总结(与WINRAR结果一 ...

  6. 不要跑,CRC没这么难!(简单易懂的CRC原理阐述)

    不要跑,CRC没这么难!(简单易懂的CRC原理阐述) 网上大多的教材都是面向大佬的很多细节原理都没有讲清楚,对于我们这些新萌菜鸟们实在太不友好了.于是我写一篇相对轻松易懂的博客,希望能对学习CRC的朋 ...

  7. 简单易懂的CRC原理阐述

    转载: https://segmentfault.com/a/1190000018094567 网上大多的教材都是面向大佬的很多细节原理都没有讲清楚,对于我们这些新萌菜鸟们实在太不友好了.于是我写一篇 ...

  8. 【转】CRC原理及其逆向破解方法

    转自:http://blog.163.com/j_drew/blog/static/11601844200692681123935/ 介绍: 这篇短文包含CRC原理介绍和其逆向分析方法,很多程序员和破 ...

  9. 色彩(颜色)空间原理(实现代码)

    色彩(颜色)空间原理(实现代码) 编写代码 对于代码示例,我将展示生成线性变换矩阵的算法和在sRGB空间和XYZ空间之间进行完全转换的示例.为了实现其他RGB空间,您只需要实现适当的gamma校正曲线 ...

  10. 彻底搞透视觉三维重建:原理剖析、代码讲解、及优化改进

    视觉三维重建 = 定位定姿 + 稠密重建 + surface reconstruction +纹理贴图.三维重建技术是计算机视觉的重要技术之一,基于视觉的三维重建技术通过深度数据获取.预处理.点云配准 ...

最新文章

  1. java中保存图片到本地_java保存网络图片到本地
  2. 马斯克矩阵模拟错了?这个试验证明人类不是「缸中之脑」
  3. Mybatis集成二级缓存与同时使用缓存与事务存在的坑
  4. c#switch语句判断成绩_C#程序流程控制 知多少?
  5. webapi 设置参数可为空_Web API系列(二):灵活多样的路由配置
  6. Oracle11g新特性:在线操作功能增强-表增加包含默认值的字段(转载)
  7. cmmi写文档工作教训
  8. Java类与类,类与接口,接口与接口关系
  9. ---Ubuntu 下安装oracle Java
  10. 目标检测:Object Detection in 20 Years: A Survey
  11. web逻辑思维题目_Java Web面试题整理(思维导图)
  12. 10分钟教你用python做个打飞机(超详细超入门教程)附源代码下载
  13. latex系列---Latex参考文献的引用
  14. Leveraging Long-Range Temporal Relationships Between Proposals for Video Object Detection论文详读
  15. RT-Thread柿饼控件(1)-- AnimatedImage
  16. 游戏开发九宫格战斗架构解析具体解释
  17. 等待事件统计视图 | 全方位认识 sys 系统库
  18. mac 苹果鼠标 magic mouse2 当触摸代替点击当触摸板教程
  19. 故障树手册(Fault Tree handbook)(4)
  20. ANSYS workbench 根据坐标施加载荷- external data载荷映射

热门文章

  1. https://download.csdn.net/download/kuyu27537830/1322930#comment
  2. homelede软路由设置方法_软路由LEDE折腾overlay分区扩容之路
  3. VIVADO除法IP注意事项
  4. ARM开发经典学习网站推荐 (转)
  5. SSL2812 2017年10月30日提高组T2 凤凰院凶真(dp,LCIS)
  6. MATLAB角度转换为弧度
  7. html如何发送语音,浏览器实现HTML5发送语音功能
  8. 基于CC2430的基础实验4-----定时器中断
  9. MFC API 设置Excel单元格格式
  10. 计算机退出域后无法加域,win10退出域后无法再加入域