1 #include <lib/cksum.h>
2 /*
3  * Computes the CRC for transmitted and received data using
4  * the CCITT 16bit algorithm (X^16 + X^12 + X^5 + 1) with
5  * a 0xFFFF initialization vector.
6  */
7 
8 #define CRC16_INIT_VALUE  0xFFFF
9 
10  /*
11   * Computes an updated version of the CRC from existing CRC.
12   * crc: the previous values of the CRC
13   * buf: the data on which to apply the checksum
14   * length: the number of bytes of data in 'buf' to be calculated.
15   */
update_crc16(unsigned short crc,const unsigned char * buf,unsigned int length)16 unsigned short update_crc16(unsigned short crc, const unsigned char *buf,
17 			   unsigned int length)
18 {
19 	unsigned int i;
20 	for (i = 0; i < length; i++) {
21 		crc = (unsigned char) (crc >> 8) | (crc << 8);
22 		crc ^= buf[i];
23 		crc ^= (unsigned char) (crc & 0xff) >> 4;
24 		crc ^= (crc << 8) << 4;
25 		crc ^= ((crc & 0xff) << 4) << 1;
26 	}
27 	return crc;
28 }
29 
30  /*
31   * Computes a CRC, starting with an initialization value.
32   * buf: the data on which to apply the checksum
33   * length: the number of bytes of data in 'buf' to be calculated.
34   */
crc16(const unsigned char * buf,unsigned int length)35 unsigned short crc16(const unsigned char *buf, unsigned int length)
36 {
37 	unsigned short crc = CRC16_INIT_VALUE;
38 	return update_crc16(crc, buf, length);
39 }
40