#include #include u_int16_t crc_16_tab[256]; void make_crc16(u_int16_t pol) /* Generate a table for a byte-wise 32-bit CRC calculation on the polynomial: x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1. Polynomials over GF(2) are represented in binary, one bit per coefficient, with the lowest powers in the most significant bit. Then adding polynomials is just exclusive-or, and multiplying a polynomial by x is a right shift by one. If we call the above polynomial p, and represent a byte as the polynomial q, also with the lowest power in the most significant bit (so the byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p, where a mod b means the remainder after dividing a by b. This calculation is done using the shift-register method of multiplying and taking the remainder. The register is initialized to zero, and for each incoming bit, x^32 is added mod p to the register if the bit is a one (where x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by x (which is shifting right by one and adding x^32 mod p if the bit shifted out is a one). We start with the highest power (least significant bit) of q and repeat for all eight bits of q. The table is simply the CRC of all possible eight bit values. This is all the information needed to generate CRC's on data a byte at a time for all combinations of CRC register values and incoming bytes. The table is written to stdout as 256 long hexadecimal values in C language format. */ /* * Note: * * The backpack CRC is slightly different. * * It seems that input data bytes correspond to polynomial with the * highest power stored in the MOST significant bit, so the entire * data stream seems to be of the form * * (a_0 x^0 + ... + a_7 x^7) + x^8 (b_0 x^0 + ... + b_7 x^7) * * instead of * * (a_0 x^7 + ... + a_7 x^0) + x^8 (b_0 x^7 + ... + b_7 x^0) * * (example for a two byte data stream) * * Therefore the crc tab has to be computed the other way round, and * the shift register algorithm has to be modified accordingly * * * Probably also the generating polynomal has its highest power in the * MSB, but this really doesn't matter, it's just a matter of * interpretation and doesn't affect the computation of the CRC * */ { u_int16_t c; /* crc shift register */ u_int16_t e; /* polynomial exclusive-or pattern */ int i; /* counter for all possible eight bit values */ int k; /* byte being shifted into crc apparatus */ /* terms of polynomial defining this crc (except x^32): */ int p[16]; /* = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; */ int nc; /* number of non-zero coeff. */ c = pol; nc = 0; k = 0; while (c) { if (c & 0x0001) { p[nc++] = k; } k++; c >>= 1; } #if 1 printf("Polynomial: x^%d", p[0]); for (i = 1; i < nc; i++) { printf(" + x^%d", p[i]); } printf("\n"); #endif /* Make exclusive-or pattern from polynomial (0xedb88320) */ e = 0; for (i = 0; i < nc; i++) { e |= 1L << p[i]; } printf("Polynomial pattern: 0x%04x\n", e); crc_16_tab[0] = 0x0000; for (i = 1; i < 256; i++) { c = i << 8; /* The idea to initialize the register with the byte instead of * zero was stolen from Haruhiko Okumura's ar002 */ for (k = 0; k < 8; k++) { c = c & 0x8000 ? (c << 1) ^ e : c << 1; } crc_16_tab[i] = c; printf("crc[0x%02x] = 0x%04x\n", i, c); } } /* =========================================================================== * Run a set of bytes through the crc shift register. If s is a NULL * pointer, then initialize the crc shift register contents instead. * Return the current crc in either case. */ static u_int16_t crc = (u_int16_t)0xffff; /* shift register contents */ u_int16_t updcrc(s, n) u_int8_t *s; /* pointer to bytes to pump through */ unsigned n; /* number of bytes in s[] */ { register u_int16_t c; /* temporary variable */ if (s == NULL) { c = 0; } else { c = crc; if (n) do { #if 0 c = crc_16_tab[((int)c ^ (*s++)) & 0xff] ^ (c >> 8); #else c = crc_16_tab[(((unsigned int)c >> 8) ^ (*s++)) & 0xff] ^ (c << 8); #endif } while (--n); } crc = c; return c; } void main(int argc, char *argv[]) { u_int16_t str[32]; u_int16_t pattern; u_int16_t pol = strtol(argv[1], NULL, 16); int i, j; printf("Polynomial: 0x%04x\n", pol); /* * create the CRC lookup table */ make_crc16(pol); /* * compute some crc patterns. */ for (j = 0; j < 16; j ++) { pattern = 0xff00 << j; pattern |= 0xff >> (15 - j); for (i = 0; i < 16; i ++) { str[i] = pattern; } crc = (u_int16_t)0xffff; /* shift register contents */ printf("Pattern: 0x%04x, Count: %d, CRC: 0x%04x\n", (unsigned int)str[0], 32, updcrc(str, 32)); } }