fork(2) download
  1. // ----------------------------------------------------------------------------
  2. // CRC tester v1.3 written on 4th of February 2003 by Sven Reifegerste (zorc/reflex)
  3. // This is the complete compilable C program, consisting only of this .c file.
  4. // No guarantee for any mistakes.
  5. //
  6. // changes to CRC tester v1.2:
  7. //
  8. // - remove unneccessary (!(polynom&1)) test for invalid polynoms
  9. // (now also XMODEM parameters 0x8408 work in c-code as they should)
  10. //
  11. // changes to CRC tester v1.1:
  12. //
  13. // - include an crc&0crcmask after converting non-direct to direct initial
  14. // value to avoid overflow
  15. //
  16. // changes to CRC tester v1.0:
  17. //
  18. // - most int's were replaced by unsigned long's to allow longer input strings
  19. // and avoid overflows and unnecessary type-casting's
  20. // ----------------------------------------------------------------------------
  21.  
  22. // includes:
  23.  
  24. #include <string.h>
  25. #include <stdio.h>
  26.  
  27.  
  28. // CRC parameters (default values are for CRC-32):
  29.  
  30. const int order = 8;
  31. const unsigned long polynom = 0x31;
  32. const int direct = 1;
  33. const unsigned long crcinit = 0;
  34. const unsigned long crcxor = 0;
  35. const int refin = 1;
  36. const int refout = 1;
  37.  
  38. // 'order' [1..32] is the CRC polynom order, counted without the leading '1' bit
  39. // 'polynom' is the CRC polynom without leading '1' bit
  40. // 'direct' [0,1] specifies the kind of algorithm: 1=direct, no augmented zero bits
  41. // 'crcinit' is the initial CRC value belonging to that algorithm
  42. // 'crcxor' is the final XOR value
  43. // 'refin' [0,1] specifies if a data byte is reflected before processing (UART) or not
  44. // 'refout' [0,1] specifies if the CRC will be reflected before XOR
  45.  
  46.  
  47. // Data character string
  48.  
  49. const unsigned char string[] = {"123456789"};
  50.  
  51. // internal global values:
  52.  
  53. unsigned long crcmask;
  54. unsigned long crchighbit;
  55. unsigned long crcinit_direct;
  56. unsigned long crcinit_nondirect;
  57. unsigned long crctab[256];
  58.  
  59.  
  60. // subroutines
  61.  
  62. unsigned long reflect (unsigned long crc, int bitnum) {
  63.  
  64. // reflects the lower 'bitnum' bits of 'crc'
  65.  
  66. unsigned long i, j=1, crcout=0;
  67.  
  68. for (i=(unsigned long)1<<(bitnum-1); i; i>>=1) {
  69. if (crc & i) crcout|=j;
  70. j<<= 1;
  71. }
  72. return (crcout);
  73. }
  74.  
  75.  
  76.  
  77. void generate_crc_table() {
  78.  
  79. // make CRC lookup table used by table algorithms
  80.  
  81. int i, j;
  82. unsigned long bit, crc;
  83.  
  84. for (i=0; i<256; i++) {
  85.  
  86. crc=(unsigned long)i;
  87. if (refin) crc=reflect(crc, 8);
  88. crc<<= order-8;
  89.  
  90. for (j=0; j<8; j++) {
  91.  
  92. bit = crc & crchighbit;
  93. crc<<= 1;
  94. if (bit) crc^= polynom;
  95. }
  96.  
  97. if (refin) crc = reflect(crc, order);
  98. crc&= crcmask;
  99. crctab[i]= crc;
  100.  
  101. }
  102. }
  103.  
  104.  
  105.  
  106. unsigned long crctablefast (unsigned char* p, unsigned long len) {
  107.  
  108. // fast lookup table algorithm without augmented zero bytes, e.g. used in pkzip.
  109. // only usable with polynom orders of 8, 16, 24 or 32.
  110.  
  111. unsigned long crc = crcinit_direct;
  112.  
  113. if (refin) crc = reflect(crc, order);
  114.  
  115. if (!refin) while (len--) crc = (crc << 8) ^ crctab[ ((crc >> (order-8)) & 0xff) ^ *p++];
  116. else while (len--) crc = (crc >> 8) ^ crctab[ (crc & 0xff) ^ *p++];
  117.  
  118. if (refout^refin) crc = reflect(crc, order);
  119. crc^= crcxor;
  120. crc&= crcmask;
  121.  
  122. return(crc);
  123. }
  124.  
  125.  
  126.  
  127. unsigned long crctable (unsigned char* p, unsigned long len) {
  128.  
  129. // normal lookup table algorithm with augmented zero bytes.
  130. // only usable with polynom orders of 8, 16, 24 or 32.
  131.  
  132. unsigned long crc = crcinit_nondirect;
  133.  
  134. if (refin) crc = reflect(crc, order);
  135.  
  136. if (!refin) while (len--) crc = ((crc << 8) | *p++) ^ crctab[ (crc >> (order-8)) & 0xff];
  137. else while (len--) crc = ((crc >> 8) | (*p++ << (order-8))) ^ crctab[ crc & 0xff];
  138.  
  139. if (!refin) while (++len < order/8) crc = (crc << 8) ^ crctab[ (crc >> (order-8)) & 0xff];
  140. else while (++len < order/8) crc = (crc >> 8) ^ crctab[crc & 0xff];
  141.  
  142. if (refout^refin) crc = reflect(crc, order);
  143. crc^= crcxor;
  144. crc&= crcmask;
  145.  
  146. return(crc);
  147. }
  148.  
  149.  
  150.  
  151. unsigned long crcbitbybit(unsigned char* p, unsigned long len) {
  152.  
  153. // bit by bit algorithm with augmented zero bytes.
  154. // does not use lookup table, suited for polynom orders between 1...32.
  155.  
  156. unsigned long i, j, c, bit;
  157. unsigned long crc = crcinit_nondirect;
  158.  
  159. for (i=0; i<len; i++) {
  160.  
  161. c = (unsigned long)*p++;
  162. if (refin) c = reflect(c, 8);
  163.  
  164. for (j=0x80; j; j>>=1) {
  165.  
  166. bit = crc & crchighbit;
  167. crc<<= 1;
  168. if (c & j) crc|= 1;
  169. if (bit) crc^= polynom;
  170. }
  171. }
  172.  
  173. for (i=0; i<order; i++) {
  174.  
  175. bit = crc & crchighbit;
  176. crc<<= 1;
  177. if (bit) crc^= polynom;
  178. }
  179.  
  180. if (refout) crc=reflect(crc, order);
  181. crc^= crcxor;
  182. crc&= crcmask;
  183.  
  184. return(crc);
  185. }
  186.  
  187.  
  188.  
  189. unsigned long crcbitbybitfast(unsigned char* p, unsigned long len) {
  190.  
  191. // fast bit by bit algorithm without augmented zero bytes.
  192. // does not use lookup table, suited for polynom orders between 1...32.
  193.  
  194. unsigned long i, j, c, bit;
  195. unsigned long crc = crcinit_direct;
  196.  
  197. for (i=0; i<len; i++) {
  198.  
  199. c = (unsigned long)*p++;
  200. if (refin) c = reflect(c, 8);
  201.  
  202. for (j=0x80; j; j>>=1) {
  203.  
  204. bit = crc & crchighbit;
  205. crc<<= 1;
  206. if (c & j) bit^= crchighbit;
  207. if (bit) crc^= polynom;
  208. }
  209. }
  210.  
  211. if (refout) crc=reflect(crc, order);
  212. crc^= crcxor;
  213. crc&= crcmask;
  214.  
  215. return(crc);
  216. }
  217.  
  218.  
  219.  
  220. int main() {
  221.  
  222. // test program for checking four different CRC computing types that are:
  223. // crcbit(), crcbitfast(), crctable() and crctablefast(), see above.
  224. // parameters are at the top of this program.
  225. // Result will be printed on the console.
  226.  
  227. int i;
  228. unsigned long bit, crc;
  229.  
  230.  
  231. // at first, compute constant bit masks for whole CRC and CRC high bit
  232.  
  233. crcmask = ((((unsigned long)1<<(order-1))-1)<<1)|1;
  234. crchighbit = (unsigned long)1<<(order-1);
  235.  
  236.  
  237. // check parameters
  238.  
  239. if (order < 1 || order > 32) {
  240. printf("ERROR, invalid order, it must be between 1..32.\n");
  241. return(0);
  242. }
  243.  
  244. if (polynom != (polynom & crcmask)) {
  245. printf("ERROR, invalid polynom.\n");
  246. return(0);
  247. }
  248.  
  249. if (crcinit != (crcinit & crcmask)) {
  250. printf("ERROR, invalid crcinit.\n");
  251. return(0);
  252. }
  253.  
  254. if (crcxor != (crcxor & crcmask)) {
  255. printf("ERROR, invalid crcxor.\n");
  256. return(0);
  257. }
  258.  
  259.  
  260. // generate lookup table
  261.  
  262. generate_crc_table();
  263.  
  264.  
  265. // compute missing initial CRC value
  266.  
  267. if (!direct) {
  268.  
  269. crcinit_nondirect = crcinit;
  270. crc = crcinit;
  271. for (i=0; i<order; i++) {
  272.  
  273. bit = crc & crchighbit;
  274. crc<<= 1;
  275. if (bit) crc^= polynom;
  276. }
  277. crc&= crcmask;
  278. crcinit_direct = crc;
  279. }
  280.  
  281. else {
  282.  
  283. crcinit_direct = crcinit;
  284. crc = crcinit;
  285. for (i=0; i<order; i++) {
  286.  
  287. bit = crc & 1;
  288. if (bit) crc^= polynom;
  289. crc >>= 1;
  290. if (bit) crc|= crchighbit;
  291. }
  292. crcinit_nondirect = crc;
  293. }
  294.  
  295.  
  296. // call CRC algorithms using the CRC parameters above and print result to the console
  297.  
  298. printf("\n");
  299. printf("CRC tester v1.1 written on 13/01/2003 by Sven Reifegerste (zorc/reflex)\n");
  300. printf("-----------------------------------------------------------------------\n");
  301. printf("\n");
  302. printf("Parameters:\n");
  303. printf("\n");
  304. printf(" polynom : 0x%x\n", polynom);
  305. printf(" order : %d\n", order);
  306. printf(" crcinit : 0x%x direct, 0x%x nondirect\n", crcinit_direct, crcinit_nondirect);
  307. printf(" crcxor : 0x%x\n", crcxor);
  308. printf(" refin : %d\n", refin);
  309. printf(" refout : %d\n", refout);
  310. printf("\n");
  311. printf(" data string : '%s' (%d bytes)\n", string, strlen(string));
  312. printf("\n");
  313. printf("Results:\n");
  314. printf("\n");
  315.  
  316. printf(" crc bit by bit : 0x%x\n", crcbitbybit((unsigned char *)string, strlen(string)));
  317. printf(" crc bit by bit fast : 0x%x\n", crcbitbybitfast((unsigned char *)string, strlen(string)));
  318. if (!(order&7)) printf(" crc table : 0x%x\n", crctable((unsigned char *)string, strlen(string)));
  319. if (!(order&7)) printf(" crc table fast : 0x%x\n", crctablefast((unsigned char *)string, strlen(string)));
  320. for(i=0;i<256;i++){
  321. printf("table[%d] = %d (0x%X)\n", i, crctab[i], crctab[i]);
  322. }
  323. return(0);
  324. }
Success #stdin #stdout 0s 2052KB
stdin
Standard input is empty
stdout
CRC tester v1.1 written on 13/01/2003 by Sven Reifegerste (zorc/reflex)
-----------------------------------------------------------------------

Parameters:

 polynom             :  0x31
 order               :  8
 crcinit             :  0x0 direct, 0x0 nondirect
 crcxor              :  0x0
 refin               :  1
 refout              :  1

 data string         :  '123456789' (9 bytes)

Results:

 crc bit by bit      :  0xa1
 crc bit by bit fast :  0xa1
 crc table           :  0xa1
 crc table fast      :  0xa1
table[0] = 0 (0x0)
table[1] = 94 (0x5E)
table[2] = 188 (0xBC)
table[3] = 226 (0xE2)
table[4] = 97 (0x61)
table[5] = 63 (0x3F)
table[6] = 221 (0xDD)
table[7] = 131 (0x83)
table[8] = 194 (0xC2)
table[9] = 156 (0x9C)
table[10] = 126 (0x7E)
table[11] = 32 (0x20)
table[12] = 163 (0xA3)
table[13] = 253 (0xFD)
table[14] = 31 (0x1F)
table[15] = 65 (0x41)
table[16] = 157 (0x9D)
table[17] = 195 (0xC3)
table[18] = 33 (0x21)
table[19] = 127 (0x7F)
table[20] = 252 (0xFC)
table[21] = 162 (0xA2)
table[22] = 64 (0x40)
table[23] = 30 (0x1E)
table[24] = 95 (0x5F)
table[25] = 1 (0x1)
table[26] = 227 (0xE3)
table[27] = 189 (0xBD)
table[28] = 62 (0x3E)
table[29] = 96 (0x60)
table[30] = 130 (0x82)
table[31] = 220 (0xDC)
table[32] = 35 (0x23)
table[33] = 125 (0x7D)
table[34] = 159 (0x9F)
table[35] = 193 (0xC1)
table[36] = 66 (0x42)
table[37] = 28 (0x1C)
table[38] = 254 (0xFE)
table[39] = 160 (0xA0)
table[40] = 225 (0xE1)
table[41] = 191 (0xBF)
table[42] = 93 (0x5D)
table[43] = 3 (0x3)
table[44] = 128 (0x80)
table[45] = 222 (0xDE)
table[46] = 60 (0x3C)
table[47] = 98 (0x62)
table[48] = 190 (0xBE)
table[49] = 224 (0xE0)
table[50] = 2 (0x2)
table[51] = 92 (0x5C)
table[52] = 223 (0xDF)
table[53] = 129 (0x81)
table[54] = 99 (0x63)
table[55] = 61 (0x3D)
table[56] = 124 (0x7C)
table[57] = 34 (0x22)
table[58] = 192 (0xC0)
table[59] = 158 (0x9E)
table[60] = 29 (0x1D)
table[61] = 67 (0x43)
table[62] = 161 (0xA1)
table[63] = 255 (0xFF)
table[64] = 70 (0x46)
table[65] = 24 (0x18)
table[66] = 250 (0xFA)
table[67] = 164 (0xA4)
table[68] = 39 (0x27)
table[69] = 121 (0x79)
table[70] = 155 (0x9B)
table[71] = 197 (0xC5)
table[72] = 132 (0x84)
table[73] = 218 (0xDA)
table[74] = 56 (0x38)
table[75] = 102 (0x66)
table[76] = 229 (0xE5)
table[77] = 187 (0xBB)
table[78] = 89 (0x59)
table[79] = 7 (0x7)
table[80] = 219 (0xDB)
table[81] = 133 (0x85)
table[82] = 103 (0x67)
table[83] = 57 (0x39)
table[84] = 186 (0xBA)
table[85] = 228 (0xE4)
table[86] = 6 (0x6)
table[87] = 88 (0x58)
table[88] = 25 (0x19)
table[89] = 71 (0x47)
table[90] = 165 (0xA5)
table[91] = 251 (0xFB)
table[92] = 120 (0x78)
table[93] = 38 (0x26)
table[94] = 196 (0xC4)
table[95] = 154 (0x9A)
table[96] = 101 (0x65)
table[97] = 59 (0x3B)
table[98] = 217 (0xD9)
table[99] = 135 (0x87)
table[100] = 4 (0x4)
table[101] = 90 (0x5A)
table[102] = 184 (0xB8)
table[103] = 230 (0xE6)
table[104] = 167 (0xA7)
table[105] = 249 (0xF9)
table[106] = 27 (0x1B)
table[107] = 69 (0x45)
table[108] = 198 (0xC6)
table[109] = 152 (0x98)
table[110] = 122 (0x7A)
table[111] = 36 (0x24)
table[112] = 248 (0xF8)
table[113] = 166 (0xA6)
table[114] = 68 (0x44)
table[115] = 26 (0x1A)
table[116] = 153 (0x99)
table[117] = 199 (0xC7)
table[118] = 37 (0x25)
table[119] = 123 (0x7B)
table[120] = 58 (0x3A)
table[121] = 100 (0x64)
table[122] = 134 (0x86)
table[123] = 216 (0xD8)
table[124] = 91 (0x5B)
table[125] = 5 (0x5)
table[126] = 231 (0xE7)
table[127] = 185 (0xB9)
table[128] = 140 (0x8C)
table[129] = 210 (0xD2)
table[130] = 48 (0x30)
table[131] = 110 (0x6E)
table[132] = 237 (0xED)
table[133] = 179 (0xB3)
table[134] = 81 (0x51)
table[135] = 15 (0xF)
table[136] = 78 (0x4E)
table[137] = 16 (0x10)
table[138] = 242 (0xF2)
table[139] = 172 (0xAC)
table[140] = 47 (0x2F)
table[141] = 113 (0x71)
table[142] = 147 (0x93)
table[143] = 205 (0xCD)
table[144] = 17 (0x11)
table[145] = 79 (0x4F)
table[146] = 173 (0xAD)
table[147] = 243 (0xF3)
table[148] = 112 (0x70)
table[149] = 46 (0x2E)
table[150] = 204 (0xCC)
table[151] = 146 (0x92)
table[152] = 211 (0xD3)
table[153] = 141 (0x8D)
table[154] = 111 (0x6F)
table[155] = 49 (0x31)
table[156] = 178 (0xB2)
table[157] = 236 (0xEC)
table[158] = 14 (0xE)
table[159] = 80 (0x50)
table[160] = 175 (0xAF)
table[161] = 241 (0xF1)
table[162] = 19 (0x13)
table[163] = 77 (0x4D)
table[164] = 206 (0xCE)
table[165] = 144 (0x90)
table[166] = 114 (0x72)
table[167] = 44 (0x2C)
table[168] = 109 (0x6D)
table[169] = 51 (0x33)
table[170] = 209 (0xD1)
table[171] = 143 (0x8F)
table[172] = 12 (0xC)
table[173] = 82 (0x52)
table[174] = 176 (0xB0)
table[175] = 238 (0xEE)
table[176] = 50 (0x32)
table[177] = 108 (0x6C)
table[178] = 142 (0x8E)
table[179] = 208 (0xD0)
table[180] = 83 (0x53)
table[181] = 13 (0xD)
table[182] = 239 (0xEF)
table[183] = 177 (0xB1)
table[184] = 240 (0xF0)
table[185] = 174 (0xAE)
table[186] = 76 (0x4C)
table[187] = 18 (0x12)
table[188] = 145 (0x91)
table[189] = 207 (0xCF)
table[190] = 45 (0x2D)
table[191] = 115 (0x73)
table[192] = 202 (0xCA)
table[193] = 148 (0x94)
table[194] = 118 (0x76)
table[195] = 40 (0x28)
table[196] = 171 (0xAB)
table[197] = 245 (0xF5)
table[198] = 23 (0x17)
table[199] = 73 (0x49)
table[200] = 8 (0x8)
table[201] = 86 (0x56)
table[202] = 180 (0xB4)
table[203] = 234 (0xEA)
table[204] = 105 (0x69)
table[205] = 55 (0x37)
table[206] = 213 (0xD5)
table[207] = 139 (0x8B)
table[208] = 87 (0x57)
table[209] = 9 (0x9)
table[210] = 235 (0xEB)
table[211] = 181 (0xB5)
table[212] = 54 (0x36)
table[213] = 104 (0x68)
table[214] = 138 (0x8A)
table[215] = 212 (0xD4)
table[216] = 149 (0x95)
table[217] = 203 (0xCB)
table[218] = 41 (0x29)
table[219] = 119 (0x77)
table[220] = 244 (0xF4)
table[221] = 170 (0xAA)
table[222] = 72 (0x48)
table[223] = 22 (0x16)
table[224] = 233 (0xE9)
table[225] = 183 (0xB7)
table[226] = 85 (0x55)
table[227] = 11 (0xB)
table[228] = 136 (0x88)
table[229] = 214 (0xD6)
table[230] = 52 (0x34)
table[231] = 106 (0x6A)
table[232] = 43 (0x2B)
table[233] = 117 (0x75)
table[234] = 151 (0x97)
table[235] = 201 (0xC9)
table[236] = 74 (0x4A)
table[237] = 20 (0x14)
table[238] = 246 (0xF6)
table[239] = 168 (0xA8)
table[240] = 116 (0x74)
table[241] = 42 (0x2A)
table[242] = 200 (0xC8)
table[243] = 150 (0x96)
table[244] = 21 (0x15)
table[245] = 75 (0x4B)
table[246] = 169 (0xA9)
table[247] = 247 (0xF7)
table[248] = 182 (0xB6)
table[249] = 232 (0xE8)
table[250] = 10 (0xA)
table[251] = 84 (0x54)
table[252] = 215 (0xD7)
table[253] = 137 (0x89)
table[254] = 107 (0x6B)
table[255] = 53 (0x35)