2 * derived from crctester.c 2011 by Nils Faerber <nils.faerber@kernelconcepts.de>
4 * ----------------------------------------------------------------------------
5 * CRC tester v1.3 written on 4th of February 2003 by Sven Reifegerste (zorc/reflex)
6 * This is the complete compilable C program, consisting only of this .c file.
7 * No guarantee for any mistakes.
9 * changes to CRC tester v1.2:
11 * - remove unneccessary (!(polynom&1)) test for invalid polynoms
12 * (now also XMODEM parameters 0x8408 work in c-code as they should)
14 * changes to CRC tester v1.1:
16 * - include an crc&0crcmask after converting non-direct to direct initial
17 * value to avoid overflow
19 * changes to CRC tester v1.0:
21 * - most int's were replaced by unsigned long's to allow longer input strings
22 * and avoid overflows and unnecessary type-casting's
23 * ----------------------------------------------------------------------------
26 static const int order = 16;
27 static const unsigned long polynom = 0x1021;
28 static const int direct = 1;
29 static const unsigned long crcinit = 0xffff;
30 static const unsigned long crcxor = 0x0000;
31 static const int refin = 1;
32 static const int refout = 0;
34 // 'order' [1..32] is the CRC polynom order, counted without the leading '1' bit
35 // 'polynom' is the CRC polynom without leading '1' bit
36 // 'direct' [0,1] specifies the kind of algorithm: 1=direct, no augmented zero bits
37 // 'crcinit' is the initial CRC value belonging to that algorithm
38 // 'crcxor' is the final XOR value
39 // 'refin' [0,1] specifies if a data byte is reflected before processing (UART) or not
40 // 'refout' [0,1] specifies if the CRC will be reflected before XOR
43 // internal global values:
45 static unsigned long crcmask;
46 static unsigned long crchighbit;
47 static unsigned long crcinit_direct;
48 static unsigned long crcinit_nondirect;
49 static unsigned long crctab[256];
54 static unsigned long reflect (unsigned long crc, int bitnum) {
56 // reflects the lower 'bitnum' bits of 'crc'
58 unsigned long i, j=1, crcout=0;
60 for (i=(unsigned long)1<<(bitnum-1); i; i>>=1) {
61 if (crc & i) crcout|=j;
69 static void generate_crc_table(void)
71 // make CRC lookup table used by table algorithms
74 unsigned long bit, crc;
76 for (i=0; i<256; i++) {
79 if (refin) crc=reflect(crc, 8);
84 bit = crc & crchighbit;
86 if (bit) crc^= polynom;
89 if (refin) crc = reflect(crc, order);
96 static unsigned long crctablefast (unsigned char* p, unsigned long len)
98 // fast lookup table algorithm without augmented zero bytes, e.g. used in pkzip.
99 // only usable with polynom orders of 8, 16, 24 or 32.
101 unsigned long crc = crcinit_direct;
104 crc = reflect(crc, order);
108 crc = (crc << 8) ^ crctab[ ((crc >> (order-8)) & 0xff) ^ *p++];
111 crc = (crc >> 8) ^ crctab[ (crc & 0xff) ^ *p++];
114 crc = reflect(crc, order);
122 static unsigned long crctable (unsigned char* p, unsigned long len)
124 /* normal lookup table algorithm with augmented zero bytes. */
125 /* only usable with polynom orders of 8, 16, 24 or 32. */
127 unsigned long crc = crcinit_nondirect;
130 crc = reflect(crc, order);
134 crc = ((crc << 8) | *p++) ^ crctab[ (crc >> (order-8)) & 0xff];
137 crc = ((crc >> 8) | (*p++ << (order-8))) ^ crctab[ crc & 0xff];
140 while (++len < order/8)
141 crc = (crc << 8) ^ crctab[ (crc >> (order-8)) & 0xff];
143 while (++len < order/8)
144 crc = (crc >> 8) ^ crctab[crc & 0xff];
147 crc = reflect(crc, order);
157 unsigned long crcbitbybit(unsigned char* p, unsigned long len) {
159 /* bit by bit algorithm with augmented zero bytes. */
160 /* does not use lookup table, suited for polynom orders between 1...32. */
162 unsigned long i, j, c, bit;
163 unsigned long crc = crcinit_nondirect;
165 for (i=0; i<len; i++) {
167 c = (unsigned long)*p++;
171 for (j=0x80; j; j>>=1) {
172 bit = crc & crchighbit;
181 for (i=0; i<order; i++) {
182 bit = crc & crchighbit;
189 crc=reflect(crc, order);
199 unsigned long crcbitbybitfast(unsigned char* p, unsigned long len) {
201 // fast bit by bit algorithm without augmented zero bytes.
202 // does not use lookup table, suited for polynom orders between 1...32.
204 unsigned long i, j, c, bit;
205 unsigned long crc = crcinit_direct;
207 for (i=0; i<len; i++) {
208 c = (unsigned long)*p++;
212 for (j=0x80; j; j>>=1) {
213 bit = crc & crchighbit;
223 crc=reflect(crc, order);
230 void crc16ccitt_init(void)
233 unsigned long bit, crc;
235 crcmask = ((((unsigned long)1<<(order-1))-1)<<1)|1;
236 crchighbit = (unsigned long)1<<(order-1);
238 generate_crc_table();
241 crcinit_nondirect = crcinit;
243 for (i=0; i<order; i++) {
244 bit = crc & crchighbit;
250 crcinit_direct = crc;
252 crcinit_direct = crcinit;
254 for (i=0; i<order; i++) {
262 crcinit_nondirect = crc;
266 unsigned short crc16ccitt (unsigned char *data, int len)
268 // call CRC algorithms using the CRC parameters above and print result to the console
269 return crcbitbybitfast((unsigned char *)data, len);
275 // test program for checking four different CRC computing types that are:
276 // crcbit(), crcbitfast(), crctable() and crctablefast(), see above.
277 // parameters are at the top of this program.
278 // Result will be printed on the console.
281 unsigned long bit, crc;
284 // at first, compute constant bit masks for whole CRC and CRC high bit
286 crcmask = ((((unsigned long)1<<(order-1))-1)<<1)|1;
287 crchighbit = (unsigned long)1<<(order-1);
292 if (order < 1 || order > 32) {
293 printf("ERROR, invalid order, it must be between 1..32.\n");
297 if (polynom != (polynom & crcmask)) {
298 printf("ERROR, invalid polynom.\n");
302 if (crcinit != (crcinit & crcmask)) {
303 printf("ERROR, invalid crcinit.\n");
307 if (crcxor != (crcxor & crcmask)) {
308 printf("ERROR, invalid crcxor.\n");
313 // generate lookup table
315 generate_crc_table();
318 // compute missing initial CRC value
322 crcinit_nondirect = crcinit;
324 for (i=0; i<order; i++) {
326 bit = crc & crchighbit;
328 if (bit) crc^= polynom;
331 crcinit_direct = crc;
336 crcinit_direct = crcinit;
338 for (i=0; i<order; i++) {
341 if (bit) crc^= polynom;
343 if (bit) crc|= crchighbit;
345 crcinit_nondirect = crc;
349 // call CRC algorithms using the CRC parameters above and print result to the console
352 printf("CRC tester v1.1 written on 13/01/2003 by Sven Reifegerste (zorc/reflex)\n");
353 printf("-----------------------------------------------------------------------\n");
355 printf("Parameters:\n");
357 printf(" polynom : 0x%x\n", polynom);
358 printf(" order : %d\n", order);
359 printf(" crcinit : 0x%x direct, 0x%x nondirect\n", crcinit_direct, crcinit_nondirect);
360 printf(" crcxor : 0x%x\n", crcxor);
361 printf(" refin : %d\n", refin);
362 printf(" refout : %d\n", refout);
364 printf(" data string : '%s' (%d bytes)\n", string, strlen(string));
366 printf("Results:\n");
369 printf(" crc bit by bit : 0x%x\n", crcbitbybit((unsigned char *)string, strlen(string)));
370 printf(" crc bit by bit fast : 0x%x\n", crcbitbybitfast((unsigned char *)string, strlen(string)));
371 if (!(order&7)) printf(" crc table : 0x%x\n", crctable((unsigned char *)string, strlen(string)));
372 if (!(order&7)) printf(" crc table fast : 0x%x\n", crctablefast((unsigned char *)string, strlen(string)));