fcs.c.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. <?xml version='1.0' encoding='iso-8859-1'?>
  2. <!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  3. <html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
  4. <head>
  5. <title>
  6. fcs.c
  7. </title>
  8. <meta http-equiv='content-type' content='text/html;iso-8859-1'/>
  9. <meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
  10. <meta name='author' content='cmaier@cmassoc.net'/>
  11. <meta name='robots' content='noindex,nofollow'/>
  12. <link href='toolkit.css' rel='stylesheet' type='text/css'/>
  13. </head>
  14. <body>
  15. <div class='headerlink'>
  16. [<a href='Failure.c.html' title=' Failure.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='fdchecksum32.c.html' title=' fdchecksum32.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * fcs.c - ethernet frame check sequence functions
  24. *
  25. * see http://www.csm.ornl.gov/~dunigan/crc.html for an explanation
  26. * of the CRC-32 algorithm reportedly used by PKZip, Ethernet, FDDI
  27. * and other popular protocols for error detection;
  28. *
  29. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  30. * Copyright (c) 2001-2006 by Charles Maier Associates;
  31. * Licensed under the Internet Software Consortium License;
  32. *
  33. *--------------------------------------------------------------------*/
  34. #ifndef FCS_SOURCE
  35. #define FCS_SOURCE
  36. /*====================================================================*
  37. * system header files;
  38. *--------------------------------------------------------------------*/
  39. #include &lt;stdint.h&gt;
  40. /*====================================================================*
  41. * variables;
  42. *--------------------------------------------------------------------*/
  43. static uint32_t CRCTable [256] =
  44. {
  45. 0x00000000,
  46. 0x77073096,
  47. 0xee0e612c,
  48. 0x990951ba,
  49. 0x076dc419,
  50. 0x706af48f,
  51. 0xe963a535,
  52. 0x9e6495a3,
  53. 0x0edb8832,
  54. 0x79dcb8a4,
  55. 0xe0d5e91e,
  56. 0x97d2d988,
  57. 0x09b64c2b,
  58. 0x7eb17cbd,
  59. 0xe7b82d07,
  60. 0x90bf1d91,
  61. 0x1db71064,
  62. 0x6ab020f2,
  63. 0xf3b97148,
  64. 0x84be41de,
  65. 0x1adad47d,
  66. 0x6ddde4eb,
  67. 0xf4d4b551,
  68. 0x83d385c7,
  69. 0x136c9856,
  70. 0x646ba8c0,
  71. 0xfd62f97a,
  72. 0x8a65c9ec,
  73. 0x14015c4f,
  74. 0x63066cd9,
  75. 0xfa0f3d63,
  76. 0x8d080df5,
  77. 0x3b6e20c8,
  78. 0x4c69105e,
  79. 0xd56041e4,
  80. 0xa2677172,
  81. 0x3c03e4d1,
  82. 0x4b04d447,
  83. 0xd20d85fd,
  84. 0xa50ab56b,
  85. 0x35b5a8fa,
  86. 0x42b2986c,
  87. 0xdbbbc9d6,
  88. 0xacbcf940,
  89. 0x32d86ce3,
  90. 0x45df5c75,
  91. 0xdcd60dcf,
  92. 0xabd13d59,
  93. 0x26d930ac,
  94. 0x51de003a,
  95. 0xc8d75180,
  96. 0xbfd06116,
  97. 0x21b4f4b5,
  98. 0x56b3c423,
  99. 0xcfba9599,
  100. 0xb8bda50f,
  101. 0x2802b89e,
  102. 0x5f058808,
  103. 0xc60cd9b2,
  104. 0xb10be924,
  105. 0x2f6f7c87,
  106. 0x58684c11,
  107. 0xc1611dab,
  108. 0xb6662d3d,
  109. 0x76dc4190,
  110. 0x01db7106,
  111. 0x98d220bc,
  112. 0xefd5102a,
  113. 0x71b18589,
  114. 0x06b6b51f,
  115. 0x9fbfe4a5,
  116. 0xe8b8d433,
  117. 0x7807c9a2,
  118. 0x0f00f934,
  119. 0x9609a88e,
  120. 0xe10e9818,
  121. 0x7f6a0dbb,
  122. 0x086d3d2d,
  123. 0x91646c97,
  124. 0xe6635c01,
  125. 0x6b6b51f4,
  126. 0x1c6c6162,
  127. 0x856530d8,
  128. 0xf262004e,
  129. 0x6c0695ed,
  130. 0x1b01a57b,
  131. 0x8208f4c1,
  132. 0xf50fc457,
  133. 0x65b0d9c6,
  134. 0x12b7e950,
  135. 0x8bbeb8ea,
  136. 0xfcb9887c,
  137. 0x62dd1ddf,
  138. 0x15da2d49,
  139. 0x8cd37cf3,
  140. 0xfbd44c65,
  141. 0x4db26158,
  142. 0x3ab551ce,
  143. 0xa3bc0074,
  144. 0xd4bb30e2,
  145. 0x4adfa541,
  146. 0x3dd895d7,
  147. 0xa4d1c46d,
  148. 0xd3d6f4fb,
  149. 0x4369e96a,
  150. 0x346ed9fc,
  151. 0xad678846,
  152. 0xda60b8d0,
  153. 0x44042d73,
  154. 0x33031de5,
  155. 0xaa0a4c5f,
  156. 0xdd0d7cc9,
  157. 0x5005713c,
  158. 0x270241aa,
  159. 0xbe0b1010,
  160. 0xc90c2086,
  161. 0x5768b525,
  162. 0x206f85b3,
  163. 0xb966d409,
  164. 0xce61e49f,
  165. 0x5edef90e,
  166. 0x29d9c998,
  167. 0xb0d09822,
  168. 0xc7d7a8b4,
  169. 0x59b33d17,
  170. 0x2eb40d81,
  171. 0xb7bd5c3b,
  172. 0xc0ba6cad,
  173. 0xedb88320,
  174. 0x9abfb3b6,
  175. 0x03b6e20c,
  176. 0x74b1d29a,
  177. 0xead54739,
  178. 0x9dd277af,
  179. 0x04db2615,
  180. 0x73dc1683,
  181. 0xe3630b12,
  182. 0x94643b84,
  183. 0x0d6d6a3e,
  184. 0x7a6a5aa8,
  185. 0xe40ecf0b,
  186. 0x9309ff9d,
  187. 0x0a00ae27,
  188. 0x7d079eb1,
  189. 0xf00f9344,
  190. 0x8708a3d2,
  191. 0x1e01f268,
  192. 0x6906c2fe,
  193. 0xf762575d,
  194. 0x806567cb,
  195. 0x196c3671,
  196. 0x6e6b06e7,
  197. 0xfed41b76,
  198. 0x89d32be0,
  199. 0x10da7a5a,
  200. 0x67dd4acc,
  201. 0xf9b9df6f,
  202. 0x8ebeeff9,
  203. 0x17b7be43,
  204. 0x60b08ed5,
  205. 0xd6d6a3e8,
  206. 0xa1d1937e,
  207. 0x38d8c2c4,
  208. 0x4fdff252,
  209. 0xd1bb67f1,
  210. 0xa6bc5767,
  211. 0x3fb506dd,
  212. 0x48b2364b,
  213. 0xd80d2bda,
  214. 0xaf0a1b4c,
  215. 0x36034af6,
  216. 0x41047a60,
  217. 0xdf60efc3,
  218. 0xa867df55,
  219. 0x316e8eef,
  220. 0x4669be79,
  221. 0xcb61b38c,
  222. 0xbc66831a,
  223. 0x256fd2a0,
  224. 0x5268e236,
  225. 0xcc0c7795,
  226. 0xbb0b4703,
  227. 0x220216b9,
  228. 0x5505262f,
  229. 0xc5ba3bbe,
  230. 0xb2bd0b28,
  231. 0x2bb45a92,
  232. 0x5cb36a04,
  233. 0xc2d7ffa7,
  234. 0xb5d0cf31,
  235. 0x2cd99e8b,
  236. 0x5bdeae1d,
  237. 0x9b64c2b0,
  238. 0xec63f226,
  239. 0x756aa39c,
  240. 0x026d930a,
  241. 0x9c0906a9,
  242. 0xeb0e363f,
  243. 0x72076785,
  244. 0x05005713,
  245. 0x95bf4a82,
  246. 0xe2b87a14,
  247. 0x7bb12bae,
  248. 0x0cb61b38,
  249. 0x92d28e9b,
  250. 0xe5d5be0d,
  251. 0x7cdcefb7,
  252. 0x0bdbdf21,
  253. 0x86d3d2d4,
  254. 0xf1d4e242,
  255. 0x68ddb3f8,
  256. 0x1fda836e,
  257. 0x81be16cd,
  258. 0xf6b9265b,
  259. 0x6fb077e1,
  260. 0x18b74777,
  261. 0x88085ae6,
  262. 0xff0f6a70,
  263. 0x66063bca,
  264. 0x11010b5c,
  265. 0x8f659eff,
  266. 0xf862ae69,
  267. 0x616bffd3,
  268. 0x166ccf45,
  269. 0xa00ae278,
  270. 0xd70dd2ee,
  271. 0x4e048354,
  272. 0x3903b3c2,
  273. 0xa7672661,
  274. 0xd06016f7,
  275. 0x4969474d,
  276. 0x3e6e77db,
  277. 0xaed16a4a,
  278. 0xd9d65adc,
  279. 0x40df0b66,
  280. 0x37d83bf0,
  281. 0xa9bcae53,
  282. 0xdebb9ec5,
  283. 0x47b2cf7f,
  284. 0x30b5ffe9,
  285. 0xbdbdf21c,
  286. 0xcabac28a,
  287. 0x53b39330,
  288. 0x24b4a3a6,
  289. 0xbad03605,
  290. 0xcdd70693,
  291. 0x54de5729,
  292. 0x23d967bf,
  293. 0xb3667a2e,
  294. 0xc4614ab8,
  295. 0x5d681b02,
  296. 0x2a6f2b94,
  297. 0xb40bbe37,
  298. 0xc30c8ea1,
  299. 0x5a05df1b,
  300. 0x2d02ef8d
  301. };
  302. /*====================================================================*
  303. *
  304. * uint32_t ReflectBits (uint32_t value, uint32_t bits);
  305. *
  306. * return the bitwise mirror image of an integer value;
  307. *
  308. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  309. * Copyright (c) 2001-2006 by Charles Maier Associates;
  310. * Licensed under the Internet Software Consortium License;
  311. *
  312. *--------------------------------------------------------------------*/
  313. #if 0
  314. static uint32_t ReflectBits (uint32_t value, uint32_t bits)
  315. {
  316. uint32_t image = 0;
  317. uint32_t bit;
  318. for (bit = 0; bit &lt; bits; bit++)
  319. {
  320. if (value &amp; 1)
  321. {
  322. image |= 1 &lt;&lt; (bits - bit - 1);
  323. }
  324. value &gt;&gt;= 1;
  325. }
  326. return (image);
  327. }
  328. #endif
  329. /*====================================================================*
  330. *
  331. * void InitCRCTable (void);
  332. *
  333. * write frame control sequence table with values; this function is
  334. * only needed to populate an empty CRCTable;
  335. *
  336. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  337. * Copyright (c) 2001-2006 by Charles Maier Associates;
  338. * Licensed under the Internet Software Consortium License;
  339. *
  340. *--------------------------------------------------------------------*/
  341. #if 0
  342. static void InitCRCTable (uint32_t CRCTable [])
  343. {
  344. extern uint32_t CRCTable [];
  345. uint32_t word;
  346. uint32_t bit;
  347. for (word = 0; word &lt; 256; word++)
  348. {
  349. uint32_t crc = ReflectBits (word, 8) &lt;&lt; 24;
  350. for (bit = 0; bit &lt; 8; bit++)
  351. {
  352. crc = (crc &lt;&lt; 1) ^ ((crc &amp; 0x80000000)? 0x04c11db7: 0);
  353. }
  354. CRCTable [word] = ReflectBits (crc, 32);
  355. }
  356. return;
  357. }
  358. #endif
  359. /*====================================================================*
  360. *
  361. * uint32_t ComputeCRC (uint8_t buffer [], uint32_t length);
  362. *
  363. *
  364. *
  365. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  366. * Copyright (c) 2001-2006 by Charles Maier Associates;
  367. * Licensed under the Internet Software Consortium License;
  368. *
  369. *--------------------------------------------------------------------*/
  370. uint32_t ComputeCRC (uint8_t buffer [], uint32_t length)
  371. {
  372. extern uint32_t CRCTable [];
  373. uint32_t crc = ~0;
  374. while (length--)
  375. {
  376. crc = (crc &gt;&gt; 8) ^ CRCTable [(crc &amp; 0xff) ^ *buffer++];
  377. }
  378. return (~crc);
  379. }
  380. /*====================================================================*
  381. *
  382. * int VerifyCRC (uint8_t buffer [], uint32_t length, uint32_t crc);
  383. *
  384. *
  385. *
  386. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  387. * Copyright (c) 2001-2006 by Charles Maier Associates;
  388. * Licensed under the Internet Software Consortium License;
  389. *
  390. *--------------------------------------------------------------------*/
  391. int VerifyCRC (uint8_t buffer [], uint32_t length, uint32_t crc)
  392. {
  393. return (crc = ComputeCRC (buffer, length));
  394. }
  395. /*====================================================================*
  396. *
  397. * int main (int argc, const char * argv [])
  398. *
  399. *
  400. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  401. * Copyright (c) 2001-2006 by Charles Maier Associates;
  402. * Licensed under the Internet Software Consortium License;
  403. *
  404. *--------------------------------------------------------------------*/
  405. #if 0
  406. #include &lt;stdio.h&gt;
  407. static uint8_t frame [192]=
  408. {
  409. 0x00,
  410. 0x00,
  411. 0x00,
  412. 0x00,
  413. 0x00,
  414. 0x01,
  415. 0x00,
  416. 0x00,
  417. 0x00,
  418. 0x00,
  419. 0x00,
  420. 0x02,
  421. 0x08,
  422. 0x00,
  423. 0x45,
  424. 0x60,
  425. 0x00,
  426. 0xAE,
  427. 0x00,
  428. 0x00,
  429. 0x40,
  430. 0x00,
  431. 0x40,
  432. 0x11,
  433. 0xDF,
  434. 0xB9,
  435. 0xAC,
  436. 0x11,
  437. 0x01,
  438. 0x02,
  439. 0xAC,
  440. 0x11,
  441. 0x01,
  442. 0x01,
  443. 0x00,
  444. 0x00,
  445. 0x00,
  446. 0x00,
  447. 0x00,
  448. 0x9A,
  449. 0xA4,
  450. 0x94,
  451. 0x00,
  452. 0x00,
  453. 0x00,
  454. 0x00,
  455. 0x00,
  456. 0x00,
  457. 0x00,
  458. 0x00,
  459. 0x00,
  460. 0x00,
  461. 0x00,
  462. 0x00,
  463. 0x00,
  464. 0x00,
  465. 0x00,
  466. 0x00,
  467. 0x00,
  468. 0x00,
  469. 0x00,
  470. 0x00,
  471. 0x00,
  472. 0x00,
  473. 0x00,
  474. 0x00,
  475. 0x00,
  476. 0x00,
  477. 0x00,
  478. 0x00,
  479. 0x00,
  480. 0x00,
  481. 0x00,
  482. 0x00,
  483. 0x00,
  484. 0x00,
  485. 0x00,
  486. 0x00,
  487. 0x00,
  488. 0x00,
  489. 0x00,
  490. 0x00,
  491. 0x00,
  492. 0x00,
  493. 0x00,
  494. 0x00,
  495. 0x00,
  496. 0x00,
  497. 0x00,
  498. 0x00,
  499. 0x00,
  500. 0x00,
  501. 0x00,
  502. 0x00,
  503. 0x00,
  504. 0x00,
  505. 0x00,
  506. 0x00,
  507. 0x00,
  508. 0x00,
  509. 0x00,
  510. 0x00,
  511. 0x00,
  512. 0x00,
  513. 0x00,
  514. 0x00,
  515. 0x00,
  516. 0x00,
  517. 0x00,
  518. 0x00,
  519. 0x00,
  520. 0x00,
  521. 0x00,
  522. 0x00,
  523. 0x00,
  524. 0x00,
  525. 0x00,
  526. 0x00,
  527. 0x00,
  528. 0x00,
  529. 0x00,
  530. 0x00,
  531. 0x00,
  532. 0x00,
  533. 0x00,
  534. 0x00,
  535. 0x00,
  536. 0x00,
  537. 0x00,
  538. 0x00,
  539. 0x00,
  540. 0x00,
  541. 0x00,
  542. 0x00,
  543. 0x00,
  544. 0x00,
  545. 0x00,
  546. 0x00,
  547. 0x00,
  548. 0x00,
  549. 0x00,
  550. 0x00,
  551. 0x00,
  552. 0x00,
  553. 0x00,
  554. 0x00,
  555. 0x00,
  556. 0x00,
  557. 0x00,
  558. 0x00,
  559. 0x00,
  560. 0x00,
  561. 0x00,
  562. 0x00,
  563. 0x00,
  564. 0x00,
  565. 0x00,
  566. 0x00,
  567. 0x00,
  568. 0x00,
  569. 0x00,
  570. 0x00,
  571. 0x00,
  572. 0x00,
  573. 0x00,
  574. 0x00,
  575. 0x00,
  576. 0x00,
  577. 0x00,
  578. 0x00,
  579. 0x00,
  580. 0x00,
  581. 0x00,
  582. 0x00,
  583. 0x00,
  584. 0x00,
  585. 0x00,
  586. 0x00,
  587. 0x00,
  588. 0x00,
  589. 0x00,
  590. 0x00,
  591. 0x00,
  592. 0x00,
  593. 0x00,
  594. 0x00,
  595. 0x00,
  596. 0x00,
  597. 0x00,
  598. 0x00,
  599. 0x00,
  600. 0x00
  601. };
  602. int main (int argc, const char * argv [])
  603. {
  604. uint32_t size = sizeof (frame);
  605. uint32_t word;
  606. uint8_t blah [] =
  607. {
  608. 0x00,
  609. 0xB0,
  610. 0x52,
  611. 0x11,
  612. 0x22,
  613. 0x33,
  614. 0x00,
  615. 0x0A,
  616. 0x5E,
  617. 0x5A,
  618. 0x27,
  619. 0x38,
  620. 0x88,
  621. 0xE1,
  622. 0x00,
  623. 0x28,
  624. 0xA0,
  625. 0x00,
  626. 0xB0,
  627. 0x52,
  628. 0x02
  629. };
  630. while (size &gt; 160)
  631. {
  632. word = ComputeCRC (frame, size);
  633. printf (&quot;%d %08X\n&quot;, size, word);
  634. size -= sizeof (word);
  635. }
  636. size = 21;
  637. word = ComputeCRC (blah, size);
  638. printf (&quot;%d %08X\n&quot;, size, word);
  639. return (0);
  640. }
  641. #endif
  642. /*====================================================================*
  643. *
  644. *--------------------------------------------------------------------*/
  645. #endif
  646. </pre>
  647. <div class='footerlink'>
  648. [<a href='Failure.c.html' title=' Failure.c '>PREV</a>]
  649. [<a href='toolkit.html' title=' Index '>HOME</a>]
  650. [<a href='fdchecksum32.c.html' title=' fdchecksum32.c '>NEXT</a>]
  651. </div>
  652. </body>
  653. </html>