You are on page 1of 2

Crc-16

 /**************************************************************
 * *
 * Fichier : crc16.c *
 * Fonction pour calculer le CRC16 *
 * *
 **************************************************************/


 /* Table of CRCs of all 8-bit messages. */
 unsigned short crc16_table[256];

 /* Flag: has the table been computed? Initially false. */
 int crc16_table_computed = 0;

 /* Make the table for a fast CRC. */
 void make_crc16_table(int poly)
 {
 unsigned short c;
 int n, k;
 for (n = 0; n < 256; n++) {
 c = (unsigned short) n;
 for (k = 0; k < 8; k++) {
 if (c & 1) {
 c = poly ^ (c >> 1);
 } else {
 c = c >> 1;
 }
 }
 crc16_table[n] = c;
 }
 crc16_table_computed = poly;
 }

 // poly
 // 0xA001 CRC16
 // 0x8408 CRC-CCITT
 // 0x1021 CRC16.XMODEM
 unsigned int Update_CRC16(int poly, unsigned int crc, unsigned
char *buf, unsigned int len)
 {
 register unsigned int i;
 if (crc16_table_computed != poly)
 make_crc16_table(poly);
 for (i=0; i<len; i++)
 crc = ((crc >> 8) & 0xff) ^ crc16_table[(crc ^ *buf++) &
0xff];
 //crc = crc16_table[(unsigned char)(crc >> 8) ^ *buf++] ^ (crc
<< 8);
 return (crc);
 }

You might also like