循环冗余校验(CRC)算法入门

时间:2023-03-10 04:29:17
循环冗余校验(CRC)算法入门

http://blog.csdn.net/liyuanbhu/article/details/7882789

前言

CRC校验(循环冗余校验)是数据通讯中最常采用的校验方式。在嵌入式软件开发中,经常要用到CRC 算法对各种数据进行校验。因此,掌握基本的CRC算法应是嵌入式程序员的基本技能。可是,我认识的嵌入式程序员中能真正掌握CRC算法的人却很少,平常在项目中见到的CRC的代码多数都是那种效率非常低下的实现方式。

其实,在网上有一篇介绍CRC 算法的非常好的文章,作者是Ross Williams,题目叫:“A PAINLESS GUIDE TO CRC ERROR DETECTION ALGORITHMS”。我常将这篇文章推荐给向我询问CRC算法的朋友,但不少朋友向我抱怨原文太长了,而且是英文的。希望我能写篇短点的文章,因此就有了本文。不过,我的水平比不了Ross Williams,我的文章肯定也没Ross Williams的写的好。因此,阅读英文没有障碍的朋友还是去读Ross Williams的原文吧。

本文的读者群设定为软件开发人员,尤其是从事嵌入式软件开发的程序员,而不是专业从事数学或通讯领域研究的学者(我也没有这个水平写的这么高深)。因此,本文的目标是介绍CRC算法的基本原理和实现方式,用到的数学尽量控制在高中生可以理解的深度。

另外,鉴于大多数嵌入式程序员都是半路出家转行过来的,不少人只会C语言。因此,文中的示例代码全部采用C语言来实现。作为一篇入门短文,文中给出的代码更注重于示范性,尽可能的保持易读性。因此,文中的代码并不追求最高效的实现,但对于一般的应用却也足够快速了。

从奇偶校验说起

所谓通讯过程的校验是指在通讯数据后加上一些附加信息,通过这些附加信息来判断接收到的数据是否和发送出的数据相同。比如说RS232串行通讯可以设置奇偶校验位,所谓奇偶校验就是在发送的每一个字节后都加上一位,使得每个字节中1的个数为奇数个或偶数个。比如我们要发送的字节是0x1a,二进制表示为0001 1010。

采用奇校验,则在数据后补上个0,数据变为0001 1010 0,数据中1的个数为奇数个(3个)

采用偶校验,则在数据后补上个1,数据变为0001 1010 1,数据中1的个数为偶数个(4个)

接收方通过计算数据中1个数是否满足奇偶性来确定数据是否有错。

奇偶校验的缺点也很明显,首先,它对错误的检测概率大约只有50%。也就是只有一半的错误它能够检测出来。另外,每传输一个字节都要附加一位校验位,对传输效率的影响很大。因此,在高速数据通讯中很少采用奇偶校验。奇偶校验优点也很明显,它很简单,因此可以用硬件来实现,这样可以减少软件的负担。因此,奇偶校验也被广泛的应用着。

奇偶校验就先介绍到这来,之所以从奇偶校验说起,是因为这种校验方式最简单,而且后面将会知道奇偶校验其实就是CRC 校验的一种(CRC-1)。

累加和校验

另一种常见的校验方式是累加和校验。所谓累加和校验实现方式有很多种,最常用的一种是在一次通讯数据包的最后加入一个字节的校验数据。这个字节内容为前面数据包中全部数据的忽略进位的按字节累加和。比如下面的例子:

我们要传输的信息为: 6、23、4

加上校验和后的数据包:6、23、4、33

这里 33 为前三个字节的校验和。接收方收到全部数据后对前三个数据进行同样的累加计算,如果累加和与最后一个字节相同的话就认为传输的数据没有错误。

累加和校验由于实现起来非常简单,也被广泛的采用。但是这种校验方式的检错能力也比较一般,对于单字节的校验和大概有1/256 的概率将原本是错误的通讯数据误判为正确数据。之所以这里介绍这种校验,是因为CRC校验在传输数据的形式上与累加和校验是相同的,都可以表示为:通讯数据 校验字节(也可能是多个字节)

初识 CRC 算法

CRC 算法的基本思想是将传输的数据当做一个位数很长的数。将这个数除以另一个数。得到的余数作为校验数据附加到原数据后面。还以上面例子中的数据为例:

6、23、4 可以看做一个2进制数: 0000011000010111 00000010

假如被除数选9,二进制表示为:1001

则除法运算可以表示为:

循环冗余校验(CRC)算法入门

可以看到,最后的余数为1。如果我们将这个余数作为校验和的话,传输的数据则是:6、23、4、1

CRC 算法和这个过程有点类似,不过采用的不是上面例子中的通常的这种除法。在CRC算法中,将二进制数据流作为多项式的系数,然后进行的是多项式的乘除法。还是举个例子吧。

比如说我们有两个二进制数,分别为:1101 和1011。

1101 与如下的多项式相联系:1x3+1x2+0x1+1x0=x3+x2+x0

1011与如下的多项式相联系:1x3+0x2+1x1+1x0=x3+x1+x0

两个多项式的乘法:(x3+x2+x0)(x3+x1+x0)=x6+x5+x4+x3+x3+x3+x2+x1+x0

得到结果后,合并同类项时采用模2运算。也就是说乘除法采用正常的多项式乘除法,而加减法都采用模2运算。所谓模2运算就是结果除以2后取余数。比如3 mod 2 = 1。因此,上面最终得到的多项式为:x6+x5+x4+x3+x2+x1+x0,对应的二进制数:111111

加减法采用模2运算后其实就成了一种运算了,就是我们通常所说的异或运算:

0+0=0

0+1=1

1+0=1

1+1=0

0-0=0

1-0=1

0-1=1

1-1=0

上面说了半天多项式,其实就算是不引入多项式乘除法的概念也可以说明这些运算的特殊之处。只不过几乎所有讲解 CRC 算法的文献中都会提到多项式,因此这里也简单的写了一点基本的概念。不过总用这种多项式表示也很罗嗦,下面的讲解中将尽量采用更简洁的写法。

除法运算与上面给出的乘法概念类似,还是遇到加减的地方都用异或运算来代替。下面是一个例子:

要传输的数据为:1101011011

除数设为:10011

在计算前先将原始数据后面填上4个0:11010110110000,之所以要补0,后面再做解释。

循环冗余校验(CRC)算法入门

从这个例子可以看出,采用了模2的加减法后,不需要考虑借位的问题,所以除法变简单了。最后得到的余数就是CRC 校验字。为了进行CRC运算,也就是这种特殊的除法运算,必须要指定个被除数,在CRC算法中,这个被除数有一个专有名称叫做“生成多项式”。生成多项式的选取是个很有难度的问题,如果选的不好,那么检出错误的概率就会低很多。好在这个问题已经被专家们研究了很长一段时间了,对于我们这些使用者来说,只要把现成的成果拿来用就行了。

最常用的几种生成多项式如下:

CRC8=X8+X5+X4+X0

CRC-CCITT=X16+X12+X5+X0

CRC16=X16+X15+X2+X0

CRC12=X12+X11+X3+X2+X0

CRC32=X32+X26+X23+X22+X16+X12+X11+X10+X8+X7+X5+X4+X2+X1+X0

有一点要特别注意,文献中提到的生成多项式经常会说到多项式的位宽(Width,简记为W),这个位宽不是多项式对应的二进制数的位数,而是位数减1。比如CRC8中用到的位宽为8的生成多项式,其实对应得二进制数有九位:100110001。另外一点,多项式表示和二进制表示都很繁琐,交流起来不方便,因此,文献中多用16进制简写法来表示,因为生成多项式的最高位肯定为1,最高位的位置由位宽可知,故在简记式中,将最高的1统一去掉了,如CRC32的生成多项式简记为04C11DB7实际上表示的是104C11DB7。当然,这样简记除了方便外,在编程计算时也有它的用处。

对于上面的例子,位宽为4(W=4),按照CRC算法的要求,计算前要在原始数据后填上W个0,也就是4个0。

位宽W=1的生成多项式(CRC1)有两种,分别是X1和X1+X0,读者可以自己证明10 对应的就是奇偶校验中的奇校验,而11对应则是偶校验。因此,写到这里我们知道了奇偶校验其实就是CRC校验的一种特例,这也是我要以奇偶校验作为开篇介绍的原因了。

CRC算法的编程实现

说了这么多总算到了核心部分了。从前面的介绍我们知道CRC校验核心就是实现无借位的除法运算。下面还是通过一个例子来说明如何实现CRC校验。

假设我们的生成多项式为:100110001(简记为0x31),也就是CRC-8

则计算步骤如下:

(1)      将CRC寄存器(8-bits,比生成多项式少1bit)赋初值0

(2)      在待传输信息流后面加入8个0

(3)      While (数据未处理完)

(4)      Begin

(5)          If (CRC寄存器首位是1)

(6)              reg = reg XOR 0x31

(7)          CRC寄存器左移一位,读入一个新的数据于CRC寄存器的0 bit的位置。

(8)      End

(9)      CRC寄存器就是我们所要求的余数。

实际上,真正的CRC 计算通常与上面描述的还有些出入。这是因为这种最基本的CRC除法有个很明显的缺陷,就是数据流的开头添加一些0并不影响最后校验字的结果。这个问题很让人恼火啊,因此真正应用的CRC 算法基本都在原始的CRC算法的基础上做了些小的改动。

所谓的改动,也就是增加了两个概念,第一个是“余数初始值”,第二个是“结果异或值”。

所谓的“余数初始值”就是在计算CRC值的开始,给CRC寄存器一个初始值。“结果异或值”是在其余计算完成后将CRC寄存器的值在与这个值进行一下异或操作作为最后的校验值。

常见的三种CRC 标准用到个各个参数如下表。

CCITT

CRC16

CRC32

校验和位宽W

16

16

32

生成多项式

x16+x12+x5+1

x16+x15+x2+1

x32+x26+x23+x22+x16+

x12+x11+x10+x8+x7+x5+

x4+x2+x1+1

除数(多项式)

0x1021

0x8005

0x04C11DB7

余数初始值

0xFFFF

0x0000

0xFFFFFFFF

结果异或值

0x0000

0x0000

0xFFFFFFFF

加入这些变形后,常见的算法描述形式就成了这个样子了:

(1)      设置CRC寄存器,并给其赋值为“余数初始值”。

(2)      将数据的第一个8-bit字符与CRC寄存器进行异或,并把结果存入CRC寄存器。

(3)      CRC寄存器向右移一位,MSB补零,移出并检查LSB。

(4)      如果LSB为0,重复第三步;若LSB为1,CRC寄存器与0x31相异或。

(5)      重复第3与第4步直到8次移位全部完成。此时一个8-bit数据处理完毕。

(6)      重复第2至第5步直到所有数据全部处理完成。

(7)      最终CRC寄存器的内容与“结果异或值”进行或非操作后即为CRC值。

示例性的C代码如下所示,因为效率很低,项目中如对计算时间有要求应该避免采用这样的代码。不过这个代码已经比网上常见的计算代码要好了,因为这个代码有一个crc的参数,可以将上次计算的crc结果传入函数中作为这次计算的初始值,这对大数据块的CRC计算是很有用的,不需要一次将所有数据读入内存,而是读一部分算一次,全读完后就计算完了。这对内存受限系统还是很有用的。

  1. #define POLY        0x1021
  2. /**
  3. * Calculating CRC-16 in 'C'
  4. * @para addr, start of data
  5. * @para num, length of data
  6. * @para crc, incoming CRC
  7. */
  8. uint16_t crc16(unsigned char *addr, int num, uint16_t crc)
  9. {
  10. int i;
  11. for (; num > 0; num--)              /* Step through bytes in memory */
  12. {
  13. crc = crc ^ (*addr++ << 8);     /* Fetch byte from memory, XOR into CRC top byte*/
  14. for (i = 0; i < 8; i++)             /* Prepare to rotate 8 bits */
  15. {
  16. if (crc & 0x8000)            /* b15 is set... */
  17. crc = (crc << 1) ^ POLY;    /* rotate and XOR with polynomic */
  18. else                          /* b15 is clear... */
  19. crc <<= 1;                  /* just rotate */
  20. }                             /* Loop for 8 bits */
  21. crc &= 0xFFFF;                  /* Ensure CRC remains 16-bit value */
  22. }                               /* Loop until num=0 */
  23. return(crc);                    /* Return updated CRC */
  24. }

上面的代码是我从http://mdfs.net/Info/Comp/Comms/CRC16.htm找到的,不过原始代码有错误,我做了些小的修改。

下面对这个函数给出个例子片段代码:

  1. unsigned char data1[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
  2. unsigned char data2[] = {'5', '6', '7', '8', '9'};
  3. unsigned short c1, c2;
  4. c1 = crc16(data1, 9, 0xffff);
  5. c2 = crc16(data1, 4, 0xffff);
  6. c2 = crc16(data2, 5, c2);
  7. printf("%04x\n", c1);
  8. printf("%04x\n", c2);

读者可以验算,c1、c2 的结果都为 29b1。上面代码中crc 的初始值之所以为0xffff,是因为CCITT标准要求的除数初始值就是0xffff。

上面的算法对数据流逐位进行计算,效率很低。实际上仔细分析CRC计算的数学性质后我们可以多位多位计算,最常用的是一种按字节查表的快速算法。该算法基于这样一个事实:计算本字节后的CRC码,等于上一字节余式CRC码的低8位左移8位,加上上一字节CRC右移 8位和本字节之和后所求得的CRC码。如果我们把8位二进制序列数的CRC(共256个)全部计算出来,放在一个表里,编码时只要从表中查找对应的值进行处理即可。

按照这个方法,可以有如下的代码(这个代码也不是我写的,是我在Micbael Barr的书“Programming Embedded Systems in C and C++” 中找到的,同样,我做了点小小的改动。):

  1. /*
  2. crc.h
  3. */
  4. #ifndef CRC_H_INCLUDED
  5. #define CRC_H_INCLUDED
  6. /*
  7. * The CRC parameters. Currently configured for CCITT.
  8. * Simply modify these to switch to another CRC Standard.
  9. */
  10. /*
  11. #define POLYNOMIAL          0x8005
  12. #define INITIAL_REMAINDER   0x0000
  13. #define FINAL_XOR_VALUE     0x0000
  14. */
  15. #define POLYNOMIAL          0x1021
  16. #define INITIAL_REMAINDER   0xFFFF
  17. #define FINAL_XOR_VALUE     0x0000
  18. /*
  19. #define POLYNOMIAL          0x1021
  20. #define POLYNOMIAL          0xA001
  21. #define INITIAL_REMAINDER   0xFFFF
  22. #define FINAL_XOR_VALUE     0x0000
  23. */
  24. /*
  25. * The width of the CRC calculation and result.
  26. * Modify the typedef for an 8 or 32-bit CRC standard.
  27. */
  28. typedef unsigned short width_t;
  29. #define WIDTH (8 * sizeof(width_t))
  30. #define TOPBIT (1 << (WIDTH - 1))
  31. /**
  32. * Initialize the CRC lookup table.
  33. * This table is used by crcCompute() to make CRC computation faster.
  34. */
  35. void crcInit(void);
  36. /**
  37. * Compute the CRC checksum of a binary message block.
  38. * @para message, 用来计算的数据
  39. * @para nBytes, 数据的长度
  40. * @note This function expects that crcInit() has been called
  41. *       first to initialize the CRC lookup table.
  42. */
  43. width_t crcCompute(unsigned char * message, unsigned int nBytes);
  44. #endif // CRC_H_INCLUDED
  1. /*
  2. *crc.c
  3. */
  4. #include "crc.h"
  5. /*
  6. * An array containing the pre-computed intermediate result for each
  7. * possible byte of input. This is used to speed up the computation.
  8. */
  9. static width_t crcTable[256];
  10. /**
  11. * Initialize the CRC lookup table.
  12. * This table is used by crcCompute() to make CRC computation faster.
  13. */
  14. void crcInit(void)
  15. {
  16. width_t remainder;
  17. width_t dividend;
  18. int bit;
  19. /* Perform binary long division, a bit at a time. */
  20. for(dividend = 0; dividend < 256; dividend++)
  21. {
  22. /* Initialize the remainder.  */
  23. remainder = dividend << (WIDTH - 8);
  24. /* Shift and XOR with the polynomial.   */
  25. for(bit = 0; bit < 8; bit++)
  26. {
  27. /* Try to divide the current data bit.  */
  28. if(remainder & TOPBIT)
  29. {
  30. remainder = (remainder << 1) ^ POLYNOMIAL;
  31. }
  32. else
  33. {
  34. remainder = remainder << 1;
  35. }
  36. }
  37. /* Save the result in the table. */
  38. crcTable[dividend] = remainder;
  39. }
  40. } /* crcInit() */
  41. /**
  42. * Compute the CRC checksum of a binary message block.
  43. * @para message, 用来计算的数据
  44. * @para nBytes, 数据的长度
  45. * @note This function expects that crcInit() has been called
  46. *       first to initialize the CRC lookup table.
  47. */
  48. width_t crcCompute(unsigned char * message, unsigned int nBytes)
  49. {
  50. unsigned int offset;
  51. unsigned char byte;
  52. width_t remainder = INITIAL_REMAINDER;
  53. /* Divide the message by the polynomial, a byte at a time. */
  54. for( offset = 0; offset < nBytes; offset++)
  55. {
  56. byte = (remainder >> (WIDTH - 8)) ^ message[offset];
  57. remainder = crcTable[byte] ^ (remainder << 8);
  58. }
  59. /* The final remainder is the CRC result. */
  60. return (remainder ^ FINAL_XOR_VALUE);
  61. } /* crcCompute() */

上面代码中crcInit() 函数用来计算crcTable,因此在调用 crcCompute 前必须先调用 crcInit()。不过,对于嵌入式系统,RAM是很紧张的,最好将 crcTable 提前算好,作为常量数据存到程序存储区而不占用RAM空间。CRC 计算实际上还有很多内容可以介绍,不过对于一般的程序员来说,知道这些也就差不多了。余下的部分以后有时间了我再写篇文章来介绍吧。

最后,给出个 C++ 代码,实现了 CRC8、CRC16 和 CRC32 的计算。收集了常见的各种 CRC 系数。 代码可以从这里下载:https://code.csdn.net/liyuanbhu/crc_compute/tree/master

  1. #ifndef CRCCOMPUTE_H
  2. #define CRCCOMPUTE_H
  3. #include <stdint.h>
  4. template <typename TYPE> class CRC
  5. {
  6. public:
  7. CRC();
  8. CRC(TYPE polynomial, TYPE init_remainder, TYPE final_xor_value);
  9. void build(TYPE polynomial, TYPE init_remainder, TYPE final_xor_value);
  10. /**
  11. * Compute the CRC checksum of a binary message block.
  12. * @para message, 用来计算的数据
  13. * @para nBytes, 数据的长度
  14. */
  15. TYPE crcCompute(char * message, unsigned int nBytes);
  16. TYPE crcCompute(char * message, unsigned int nBytes, bool reinit);
  17. protected:
  18. TYPE m_polynomial;
  19. TYPE m_initial_remainder;
  20. TYPE m_final_xor_value;
  21. TYPE m_remainder;
  22. TYPE crcTable[256];
  23. int m_width;
  24. int m_topbit;
  25. /**
  26. * Initialize the CRC lookup table.
  27. * This table is used by crcCompute() to make CRC computation faster.
  28. */
  29. void crcInit(void);
  30. };
  31. template <typename TYPE>
  32. CRC<TYPE>::CRC()
  33. {
  34. m_width = 8 * sizeof(TYPE);
  35. m_topbit = 1 << (m_width - 1);
  36. }
  37. template <typename TYPE>
  38. CRC<TYPE>::CRC(TYPE polynomial, TYPE init_remainder, TYPE final_xor_value)
  39. {
  40. m_width = 8 * sizeof(TYPE);
  41. m_topbit = 1 << (m_width - 1);
  42. m_polynomial = polynomial;
  43. m_initial_remainder = init_remainder;
  44. m_final_xor_value = final_xor_value;
  45. crcInit();
  46. }
  47. template <typename TYPE>
  48. void CRC<TYPE>::build(TYPE polynomial, TYPE init_remainder, TYPE final_xor_value)
  49. {
  50. m_polynomial = polynomial;
  51. m_initial_remainder = init_remainder;
  52. m_final_xor_value = final_xor_value;
  53. crcInit();
  54. }
  55. template <typename TYPE>
  56. TYPE CRC<TYPE>::crcCompute(char * message, unsigned int nBytes)
  57. {
  58. unsigned int offset;
  59. unsigned char byte;
  60. TYPE remainder = m_initial_remainder;
  61. /* Divide the message by the polynomial, a byte at a time. */
  62. for( offset = 0; offset < nBytes; offset++)
  63. {
  64. byte = (remainder >> (m_width - 8)) ^ message[offset];
  65. remainder = crcTable[byte] ^ (remainder << 8);
  66. }
  67. /* The final remainder is the CRC result. */
  68. return (remainder ^ m_final_xor_value);
  69. }
  70. template <typename TYPE>
  71. TYPE CRC<TYPE>::crcCompute(char * message, unsigned int nBytes, bool reinit)
  72. {
  73. unsigned int offset;
  74. unsigned char byte;
  75. if(reinit)
  76. {
  77. m_remainder = m_initial_remainder;
  78. }
  79. /* Divide the message by the polynomial, a byte at a time. */
  80. for( offset = 0; offset < nBytes; offset++)
  81. {
  82. byte = (m_remainder >> (m_width - 8)) ^ message[offset];
  83. m_remainder = crcTable[byte] ^ (m_remainder << 8);
  84. }
  85. /* The final remainder is the CRC result. */
  86. return (m_remainder ^ m_final_xor_value);
  87. }
  88. class CRC8 : public CRC<uint8_t>
  89. {
  90. public:
  91. enum CRC8_TYPE {eCRC8, eAUTOSAR, eCDMA2000, eDARC, eDVB_S2, eEBU, eAES, eGSM_A, eGSM_B, eI_CODE,
  92. eITU, eLTE, eMAXIM, eOPENSAFETY, eROHC, eSAE_J1850, eWCDMA};
  93. CRC8(CRC8_TYPE type);
  94. CRC8(uint8_t polynomial, uint8_t init_remainder, uint8_t final_xor_value)
  95. :CRC<uint8_t>(polynomial, init_remainder, final_xor_value){}
  96. };
  97. class CRC16 : public CRC<uint16_t>
  98. {
  99. public:
  100. enum CRC16_TYPE {eCCITT, eKERMIT, eCCITT_FALSE, eIBM, eARC, eLHA, eSPI_FUJITSU,
  101. eBUYPASS, eVERIFONE, eUMTS, eCDMA2000, eCMS, eDDS_110, eDECT_R,
  102. eDECT_X, eDNP, eEN_13757, eGENIBUS, eEPC, eDARC, eI_CODE, eGSM,
  103. eLJ1200, eMAXIM, eMCRF4XX, eOPENSAFETY_A, eOPENSAFETY_B, ePROFIBUS,
  104. eIEC_61158_2, eRIELLO, eT10_DIF, eTELEDISK, eTMS37157, eUSB,
  105. eCRC_A, eMODBUS, eX_25, eCRC_B, eISO_HDLC, eIBM_SDLC, eXMODEM,
  106. eZMODEM, eACORN, eLTE};
  107. CRC16(CRC16_TYPE type);
  108. CRC16(uint16_t polynomial, uint16_t init_remainder, uint16_t final_xor_value)
  109. :CRC<uint16_t>(polynomial, init_remainder, final_xor_value){}
  110. };
  111. class CRC32 : public CRC<uint32_t>
  112. {
  113. public:
  114. enum CRC32_TYPE {eADCCP, ePKZIP, eCRC32, eAAL5, eDECT_B, eB_CRC32, eBZIP2, eAUTOSAR,
  115. eCRC32C, eCRC32D, eMPEG2, ePOSIX, eCKSUM, eCRC32Q, eJAMCRC, eXFER};
  116. CRC32(CRC32_TYPE type);
  117. };
  118. #endif // CRCCOMPUTE_H
  1. #include "crcCompute.h"
  2. template <typename TYPE>
  3. void CRC<TYPE>::crcInit(void)
  4. {
  5. TYPE remainder;
  6. TYPE dividend;
  7. int bit;
  8. /* Perform binary long division, a bit at a time. */
  9. for(dividend = 0; dividend < 256; dividend++)
  10. {
  11. /* Initialize the remainder.  */
  12. remainder = dividend << (m_width - 8);
  13. /* Shift and XOR with the polynomial.   */
  14. for(bit = 0; bit < 8; bit++)
  15. {
  16. /* Try to divide the current data bit.  */
  17. if(remainder & m_topbit)
  18. {
  19. remainder = (remainder << 1) ^ m_polynomial;
  20. }
  21. else
  22. {
  23. remainder = remainder << 1;
  24. }
  25. }
  26. /* Save the result in the table. */
  27. crcTable[dividend] = remainder;
  28. }
  29. }
  30. CRC8::CRC8(CRC8_TYPE type)
  31. {
  32. switch (type)
  33. {
  34. case eCRC8:
  35. m_polynomial = 0x07; //http://reveng.sourceforge.net/crc-catalogue/all.htm
  36. m_initial_remainder = 0x00;
  37. m_final_xor_value = 0x00;
  38. break;
  39. case eAUTOSAR:
  40. m_polynomial = 0x2f;
  41. m_initial_remainder = 0xff;
  42. m_final_xor_value = 0xff;
  43. break;
  44. case eCDMA2000:
  45. m_polynomial = 0x9b;
  46. m_initial_remainder = 0xFF;
  47. m_final_xor_value = 0x00;
  48. break;
  49. case eDARC:
  50. m_polynomial = 0x39;
  51. m_initial_remainder = 0x00;
  52. m_final_xor_value = 0x00;
  53. break;
  54. case eDVB_S2:
  55. m_polynomial = 0xd5;
  56. m_initial_remainder = 0x00;
  57. m_final_xor_value = 0x00;
  58. break;
  59. case eEBU:
  60. case eAES:
  61. m_polynomial = 0x1d;
  62. m_initial_remainder = 0xFF;
  63. m_final_xor_value = 0x00;
  64. break;
  65. case eGSM_A:
  66. m_polynomial = 0x1d;
  67. m_initial_remainder = 0x00;
  68. m_final_xor_value = 0x00;
  69. break;
  70. case eGSM_B:
  71. m_polynomial = 0x49;
  72. m_initial_remainder = 0x00;
  73. m_final_xor_value = 0xFF;
  74. break;
  75. case eI_CODE:
  76. m_polynomial = 0x1d;
  77. m_initial_remainder = 0xFD;
  78. m_final_xor_value = 0x00;
  79. break;
  80. case eITU:
  81. m_polynomial = 0x07;
  82. m_initial_remainder = 0x00;
  83. m_final_xor_value = 0x55;
  84. break;
  85. case eLTE:
  86. m_polynomial = 0x9b;
  87. m_initial_remainder = 0x00;
  88. m_final_xor_value = 0x00;
  89. break;
  90. case eMAXIM:
  91. m_polynomial = 0x31;
  92. m_initial_remainder = 0x00;
  93. m_final_xor_value = 0x00;
  94. break;
  95. case eOPENSAFETY:
  96. m_polynomial = 0x2f;
  97. m_initial_remainder = 0x00;
  98. m_final_xor_value = 0x00;
  99. break;
  100. case eROHC:
  101. m_polynomial = 0x07;
  102. m_initial_remainder = 0xff;
  103. m_final_xor_value = 0x00;
  104. break;
  105. case eSAE_J1850:
  106. m_polynomial = 0x1d;
  107. m_initial_remainder = 0xff;
  108. m_final_xor_value = 0xff;
  109. break;
  110. case eWCDMA:
  111. m_polynomial = 0x9b;
  112. m_initial_remainder = 0x00;
  113. m_final_xor_value = 0x00;
  114. break;
  115. default:
  116. m_polynomial = 0x07;
  117. m_initial_remainder = 0x00;
  118. m_final_xor_value = 0x00;
  119. break;
  120. }
  121. crcInit();
  122. }
  123. CRC16::CRC16(CRC16_TYPE type)
  124. {
  125. switch (type)
  126. {
  127. case eCCITT_FALSE:
  128. case eMCRF4XX:
  129. m_polynomial = 0x1021;
  130. m_initial_remainder = 0xFFFF;
  131. m_final_xor_value = 0x0000;
  132. break;
  133. case eIBM:
  134. case eARC:
  135. case eLHA:
  136. case eBUYPASS:
  137. case eVERIFONE:
  138. case eUMTS:
  139. m_polynomial = 0x8005;
  140. m_initial_remainder = 0x0000;
  141. m_final_xor_value = 0x0000;
  142. break;
  143. case eSPI_FUJITSU:
  144. m_polynomial = 0x1021;
  145. m_initial_remainder = 0x1d0f;
  146. m_final_xor_value = 0x0000;
  147. break;
  148. case eCCITT:
  149. case eKERMIT:
  150. case eXMODEM:
  151. case eZMODEM:
  152. case eACORN:
  153. case eLTE:
  154. m_polynomial = 0x1021;
  155. m_initial_remainder = 0x0000;
  156. m_final_xor_value = 0x0000;
  157. break;
  158. case eCDMA2000:
  159. m_polynomial = 0xc867;
  160. m_initial_remainder = 0xffff;
  161. m_final_xor_value = 0x0000;
  162. break;
  163. case eCMS:
  164. case eMODBUS:
  165. m_polynomial = 0x8005;
  166. m_initial_remainder = 0xffff;
  167. m_final_xor_value = 0x0000;
  168. break;
  169. case eDDS_110:
  170. m_polynomial = 0x8005;
  171. m_initial_remainder = 0x800d;
  172. m_final_xor_value = 0x0000;
  173. break;
  174. case eDECT_R:
  175. m_polynomial = 0x0589;
  176. m_initial_remainder = 0x0000;
  177. m_final_xor_value = 0x0001;
  178. break;
  179. case eDECT_X:
  180. m_polynomial = 0x0589;
  181. m_initial_remainder = 0x0000;
  182. m_final_xor_value = 0x0000;
  183. break;
  184. case eDNP:
  185. case eEN_13757:
  186. m_polynomial = 0x3d65;
  187. m_initial_remainder = 0x0000;
  188. m_final_xor_value = 0xffff;
  189. break;
  190. case eGENIBUS:
  191. case eEPC:
  192. case eDARC:
  193. case eI_CODE:
  194. case eX_25:
  195. case eCRC_B:
  196. case eISO_HDLC:
  197. case eIBM_SDLC:
  198. m_polynomial = 0x1021;
  199. m_initial_remainder = 0xffff;
  200. m_final_xor_value = 0xffff;
  201. break;
  202. case eGSM:
  203. m_polynomial = 0x1021;
  204. m_initial_remainder = 0x0000;
  205. m_final_xor_value = 0xffff;
  206. break;
  207. case eLJ1200:
  208. m_polynomial = 0x6f63;
  209. m_initial_remainder = 0x0000;
  210. m_final_xor_value = 0x0000;
  211. break;
  212. case eMAXIM:
  213. m_polynomial = 0x8005;
  214. m_initial_remainder = 0x0000;
  215. m_final_xor_value = 0xffff;
  216. break;
  217. case eOPENSAFETY_A:
  218. m_polynomial = 0x5935;
  219. m_initial_remainder = 0x0000;
  220. m_final_xor_value = 0x0000;
  221. break;
  222. case eOPENSAFETY_B:
  223. m_polynomial = 0x755b;
  224. m_initial_remainder = 0x0000;
  225. m_final_xor_value = 0x0000;
  226. break;
  227. case ePROFIBUS:
  228. case eIEC_61158_2:
  229. m_polynomial = 0x1dcf;
  230. m_initial_remainder = 0xffff;
  231. m_final_xor_value = 0xffff;
  232. break;
  233. case eRIELLO:
  234. m_polynomial = 0x1021;
  235. m_initial_remainder = 0xb2aa;
  236. m_final_xor_value = 0x0000;
  237. break;
  238. case eT10_DIF:
  239. m_polynomial = 0x8bb7;
  240. m_initial_remainder = 0x0000;
  241. m_final_xor_value = 0x0000;
  242. break;
  243. case eTELEDISK:
  244. m_polynomial = 0xa097;
  245. m_initial_remainder = 0x0000;
  246. m_final_xor_value = 0x0000;
  247. break;
  248. case eTMS37157:
  249. m_polynomial = 0x1021;
  250. m_initial_remainder = 0x89ec;
  251. m_final_xor_value = 0x0000;
  252. break;
  253. case eUSB:
  254. m_polynomial = 0x8005;
  255. m_initial_remainder = 0xffff;
  256. m_final_xor_value = 0xffff;
  257. break;
  258. case eCRC_A:
  259. m_polynomial = 0x1021;
  260. m_initial_remainder = 0xc6c6;
  261. m_final_xor_value = 0x0000;
  262. break;
  263. default:
  264. m_polynomial = 0x8005;
  265. m_initial_remainder = 0x0000;
  266. m_final_xor_value = 0x0000;
  267. break;
  268. }
  269. crcInit();
  270. }
  271. CRC32::CRC32(CRC32_TYPE type)
  272. {
  273. switch (type)
  274. {
  275. case eADCCP:
  276. case ePKZIP:
  277. case eCRC32:
  278. case eBZIP2:
  279. case eAAL5:
  280. case eDECT_B:
  281. case eB_CRC32:
  282. m_polynomial = 0x04c11db7;
  283. m_initial_remainder = 0xFFFFFFFF;
  284. m_final_xor_value = 0xFFFFFFFF;
  285. break;
  286. case eAUTOSAR:
  287. m_polynomial = 0xf4acfb13;
  288. m_initial_remainder = 0xFFFFFFFF;
  289. m_final_xor_value = 0xFFFFFFFF;
  290. break;
  291. case eCRC32C:
  292. m_polynomial = 0x1edc6f41;
  293. m_initial_remainder = 0xFFFFFFFF;
  294. m_final_xor_value = 0xFFFFFFFF;
  295. break;
  296. case eCRC32D:
  297. m_polynomial = 0xa833982b;
  298. m_initial_remainder = 0xFFFFFFFF;
  299. m_final_xor_value = 0xFFFFFFFF;
  300. break;
  301. case eMPEG2:
  302. case eJAMCRC:
  303. m_polynomial = 0x04c11db7;
  304. m_initial_remainder = 0xFFFFFFFF;
  305. m_final_xor_value = 0x00000000;
  306. break;
  307. case ePOSIX:
  308. case eCKSUM:
  309. m_polynomial = 0x04c11db7;
  310. m_initial_remainder = 0x00000000;
  311. m_final_xor_value = 0xFFFFFFFF;
  312. break;
  313. case eCRC32Q:
  314. m_polynomial = 0x814141ab;
  315. m_initial_remainder = 0x00000000;
  316. m_final_xor_value = 0x00000000;
  317. break;
  318. case eXFER:
  319. m_polynomial = 0x000000af;
  320. m_initial_remainder = 0x00000000;
  321. m_final_xor_value = 0x00000000;
  322. break;
  323. default:
  324. m_polynomial = 0x04C11DB7;
  325. m_initial_remainder = 0xFFFFFFFF;
  326. m_final_xor_value = 0xFFFFFFFF;
  327. break;
  328. }
  329. crcInit();
  330. }
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include "crcCompute.h"
  4. using namespace std;
  5. int main(int argc, char *argv[])
  6. {
  7. CRC16 crc16(CRC16::eCCITT_FALSE);
  8. char data1[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
  9. char data2[] = {'5', '6', '7', '8', '9'};
  10. unsigned short c1, c2;
  11. c1 = crc16.crcCompute(data1, 9);
  12. c2 = crc16.crcCompute(data1, 4, true);
  13. c2 = crc16.crcCompute(data2, 5, false);
  14. printf("%04x\n", c1);
  15. printf("%04x\n", c2);
  16. return 0;
  17. }