2 * derived from crctester.c
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;
103 if (refin) crc = reflect(crc, order);
105 if (!refin) while (len--) crc = (crc << 8) ^ crctab[ ((crc >> (order-8)) & 0xff) ^ *p++];
106 else while (len--) crc = (crc >> 8) ^ crctab[ (crc & 0xff) ^ *p++];
108 if (refout^refin) crc = reflect(crc, order);
115 static unsigned long crctable (unsigned char* p, unsigned long len)
117 // normal lookup table algorithm with augmented zero bytes.
118 // only usable with polynom orders of 8, 16, 24 or 32.
120 unsigned long crc = crcinit_nondirect;
123 crc = reflect(crc, order);
127 crc = ((crc << 8) | *p++) ^ crctab[ (crc >> (order-8)) & 0xff];
130 crc = ((crc >> 8) | (*p++ << (order-8))) ^ crctab[ crc & 0xff];
133 while (++len < order/8)
134 crc = (crc << 8) ^ crctab[ (crc >> (order-8)) & 0xff];
136 while (++len < order/8)
137 crc = (crc >> 8) ^ crctab[crc & 0xff];
140 crc = reflect(crc, order);
149 unsigned long crcbitbybit(unsigned char* p, unsigned long len) {
151 // bit by bit algorithm with augmented zero bytes.
152 // does not use lookup table, suited for polynom orders between 1...32.
154 unsigned long i, j, c, bit;
155 unsigned long crc = crcinit_nondirect;
157 for (i=0; i<len; i++) {
159 c = (unsigned long)*p++;
163 for (j=0x80; j; j>>=1) {
164 bit = crc & crchighbit;
173 for (i=0; i<order; i++) {
174 bit = crc & crchighbit;
181 crc=reflect(crc, order);
190 unsigned long crcbitbybitfast(unsigned char* p, unsigned long len) {
192 // fast bit by bit algorithm without augmented zero bytes.
193 // does not use lookup table, suited for polynom orders between 1...32.
195 unsigned long i, j, c, bit;
196 unsigned long crc = crcinit_direct;
198 for (i=0; i<len; i++) {
199 c = (unsigned long)*p++;
203 for (j=0x80; j; j>>=1) {
204 bit = crc & crchighbit;
214 crc=reflect(crc, order);
221 void crc16ccitt_init(void)
224 unsigned long bit, crc;
226 crcmask = ((((unsigned long)1<<(order-1))-1)<<1)|1;
227 crchighbit = (unsigned long)1<<(order-1);
229 generate_crc_table();
232 crcinit_nondirect = crcinit;
234 for (i=0; i<order; i++) {
235 bit = crc & crchighbit;
241 crcinit_direct = crc;
243 crcinit_direct = crcinit;
245 for (i=0; i<order; i++) {
253 crcinit_nondirect = crc;
257 unsigned short crc16ccitt (unsigned char *data, int len)
259 // call CRC algorithms using the CRC parameters above and print result to the console
260 return crcbitbybitfast((unsigned char *)data, len);
266 // test program for checking four different CRC computing types that are:
267 // crcbit(), crcbitfast(), crctable() and crctablefast(), see above.
268 // parameters are at the top of this program.
269 // Result will be printed on the console.
272 unsigned long bit, crc;
275 // at first, compute constant bit masks for whole CRC and CRC high bit
277 crcmask = ((((unsigned long)1<<(order-1))-1)<<1)|1;
278 crchighbit = (unsigned long)1<<(order-1);
283 if (order < 1 || order > 32) {
284 printf("ERROR, invalid order, it must be between 1..32.\n");
288 if (polynom != (polynom & crcmask)) {
289 printf("ERROR, invalid polynom.\n");
293 if (crcinit != (crcinit & crcmask)) {
294 printf("ERROR, invalid crcinit.\n");
298 if (crcxor != (crcxor & crcmask)) {
299 printf("ERROR, invalid crcxor.\n");
304 // generate lookup table
306 generate_crc_table();
309 // compute missing initial CRC value
313 crcinit_nondirect = crcinit;
315 for (i=0; i<order; i++) {
317 bit = crc & crchighbit;
319 if (bit) crc^= polynom;
322 crcinit_direct = crc;
327 crcinit_direct = crcinit;
329 for (i=0; i<order; i++) {
332 if (bit) crc^= polynom;
334 if (bit) crc|= crchighbit;
336 crcinit_nondirect = crc;
340 // call CRC algorithms using the CRC parameters above and print result to the console
343 printf("CRC tester v1.1 written on 13/01/2003 by Sven Reifegerste (zorc/reflex)\n");
344 printf("-----------------------------------------------------------------------\n");
346 printf("Parameters:\n");
348 printf(" polynom : 0x%x\n", polynom);
349 printf(" order : %d\n", order);
350 printf(" crcinit : 0x%x direct, 0x%x nondirect\n", crcinit_direct, crcinit_nondirect);
351 printf(" crcxor : 0x%x\n", crcxor);
352 printf(" refin : %d\n", refin);
353 printf(" refout : %d\n", refout);
355 printf(" data string : '%s' (%d bytes)\n", string, strlen(string));
357 printf("Results:\n");
360 printf(" crc bit by bit : 0x%x\n", crcbitbybit((unsigned char *)string, strlen(string)));
361 printf(" crc bit by bit fast : 0x%x\n", crcbitbybitfast((unsigned char *)string, strlen(string)));
362 if (!(order&7)) printf(" crc table : 0x%x\n", crctable((unsigned char *)string, strlen(string)));
363 if (!(order&7)) printf(" crc table fast : 0x%x\n", crctablefast((unsigned char *)string, strlen(string)));