fcs.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /*====================================================================*
  2. *
  3. * fcs.c - ethernet frame check sequence functions
  4. *
  5. * see http://www.csm.ornl.gov/~dunigan/crc.html for an explanation
  6. * of the CRC-32 algorithm reportedly used by PKZip, Ethernet, FDDI
  7. * and other popular protocols for error detection;
  8. *
  9. * Motley Tools by Charles Maier;
  10. * Copyright (c) 2001-2006 by Charles Maier Associates;
  11. * Licensed under the Internet Software Consortium License;
  12. *
  13. *--------------------------------------------------------------------*/
  14. #ifndef FCS_SOURCE
  15. #define FCS_SOURCE
  16. /*====================================================================*
  17. * system header files;
  18. *--------------------------------------------------------------------*/
  19. #include <stdint.h>
  20. /*====================================================================*
  21. * variables;
  22. *--------------------------------------------------------------------*/
  23. static uint32_t CRCTable [256] =
  24. {
  25. 0x00000000,
  26. 0x77073096,
  27. 0xee0e612c,
  28. 0x990951ba,
  29. 0x076dc419,
  30. 0x706af48f,
  31. 0xe963a535,
  32. 0x9e6495a3,
  33. 0x0edb8832,
  34. 0x79dcb8a4,
  35. 0xe0d5e91e,
  36. 0x97d2d988,
  37. 0x09b64c2b,
  38. 0x7eb17cbd,
  39. 0xe7b82d07,
  40. 0x90bf1d91,
  41. 0x1db71064,
  42. 0x6ab020f2,
  43. 0xf3b97148,
  44. 0x84be41de,
  45. 0x1adad47d,
  46. 0x6ddde4eb,
  47. 0xf4d4b551,
  48. 0x83d385c7,
  49. 0x136c9856,
  50. 0x646ba8c0,
  51. 0xfd62f97a,
  52. 0x8a65c9ec,
  53. 0x14015c4f,
  54. 0x63066cd9,
  55. 0xfa0f3d63,
  56. 0x8d080df5,
  57. 0x3b6e20c8,
  58. 0x4c69105e,
  59. 0xd56041e4,
  60. 0xa2677172,
  61. 0x3c03e4d1,
  62. 0x4b04d447,
  63. 0xd20d85fd,
  64. 0xa50ab56b,
  65. 0x35b5a8fa,
  66. 0x42b2986c,
  67. 0xdbbbc9d6,
  68. 0xacbcf940,
  69. 0x32d86ce3,
  70. 0x45df5c75,
  71. 0xdcd60dcf,
  72. 0xabd13d59,
  73. 0x26d930ac,
  74. 0x51de003a,
  75. 0xc8d75180,
  76. 0xbfd06116,
  77. 0x21b4f4b5,
  78. 0x56b3c423,
  79. 0xcfba9599,
  80. 0xb8bda50f,
  81. 0x2802b89e,
  82. 0x5f058808,
  83. 0xc60cd9b2,
  84. 0xb10be924,
  85. 0x2f6f7c87,
  86. 0x58684c11,
  87. 0xc1611dab,
  88. 0xb6662d3d,
  89. 0x76dc4190,
  90. 0x01db7106,
  91. 0x98d220bc,
  92. 0xefd5102a,
  93. 0x71b18589,
  94. 0x06b6b51f,
  95. 0x9fbfe4a5,
  96. 0xe8b8d433,
  97. 0x7807c9a2,
  98. 0x0f00f934,
  99. 0x9609a88e,
  100. 0xe10e9818,
  101. 0x7f6a0dbb,
  102. 0x086d3d2d,
  103. 0x91646c97,
  104. 0xe6635c01,
  105. 0x6b6b51f4,
  106. 0x1c6c6162,
  107. 0x856530d8,
  108. 0xf262004e,
  109. 0x6c0695ed,
  110. 0x1b01a57b,
  111. 0x8208f4c1,
  112. 0xf50fc457,
  113. 0x65b0d9c6,
  114. 0x12b7e950,
  115. 0x8bbeb8ea,
  116. 0xfcb9887c,
  117. 0x62dd1ddf,
  118. 0x15da2d49,
  119. 0x8cd37cf3,
  120. 0xfbd44c65,
  121. 0x4db26158,
  122. 0x3ab551ce,
  123. 0xa3bc0074,
  124. 0xd4bb30e2,
  125. 0x4adfa541,
  126. 0x3dd895d7,
  127. 0xa4d1c46d,
  128. 0xd3d6f4fb,
  129. 0x4369e96a,
  130. 0x346ed9fc,
  131. 0xad678846,
  132. 0xda60b8d0,
  133. 0x44042d73,
  134. 0x33031de5,
  135. 0xaa0a4c5f,
  136. 0xdd0d7cc9,
  137. 0x5005713c,
  138. 0x270241aa,
  139. 0xbe0b1010,
  140. 0xc90c2086,
  141. 0x5768b525,
  142. 0x206f85b3,
  143. 0xb966d409,
  144. 0xce61e49f,
  145. 0x5edef90e,
  146. 0x29d9c998,
  147. 0xb0d09822,
  148. 0xc7d7a8b4,
  149. 0x59b33d17,
  150. 0x2eb40d81,
  151. 0xb7bd5c3b,
  152. 0xc0ba6cad,
  153. 0xedb88320,
  154. 0x9abfb3b6,
  155. 0x03b6e20c,
  156. 0x74b1d29a,
  157. 0xead54739,
  158. 0x9dd277af,
  159. 0x04db2615,
  160. 0x73dc1683,
  161. 0xe3630b12,
  162. 0x94643b84,
  163. 0x0d6d6a3e,
  164. 0x7a6a5aa8,
  165. 0xe40ecf0b,
  166. 0x9309ff9d,
  167. 0x0a00ae27,
  168. 0x7d079eb1,
  169. 0xf00f9344,
  170. 0x8708a3d2,
  171. 0x1e01f268,
  172. 0x6906c2fe,
  173. 0xf762575d,
  174. 0x806567cb,
  175. 0x196c3671,
  176. 0x6e6b06e7,
  177. 0xfed41b76,
  178. 0x89d32be0,
  179. 0x10da7a5a,
  180. 0x67dd4acc,
  181. 0xf9b9df6f,
  182. 0x8ebeeff9,
  183. 0x17b7be43,
  184. 0x60b08ed5,
  185. 0xd6d6a3e8,
  186. 0xa1d1937e,
  187. 0x38d8c2c4,
  188. 0x4fdff252,
  189. 0xd1bb67f1,
  190. 0xa6bc5767,
  191. 0x3fb506dd,
  192. 0x48b2364b,
  193. 0xd80d2bda,
  194. 0xaf0a1b4c,
  195. 0x36034af6,
  196. 0x41047a60,
  197. 0xdf60efc3,
  198. 0xa867df55,
  199. 0x316e8eef,
  200. 0x4669be79,
  201. 0xcb61b38c,
  202. 0xbc66831a,
  203. 0x256fd2a0,
  204. 0x5268e236,
  205. 0xcc0c7795,
  206. 0xbb0b4703,
  207. 0x220216b9,
  208. 0x5505262f,
  209. 0xc5ba3bbe,
  210. 0xb2bd0b28,
  211. 0x2bb45a92,
  212. 0x5cb36a04,
  213. 0xc2d7ffa7,
  214. 0xb5d0cf31,
  215. 0x2cd99e8b,
  216. 0x5bdeae1d,
  217. 0x9b64c2b0,
  218. 0xec63f226,
  219. 0x756aa39c,
  220. 0x026d930a,
  221. 0x9c0906a9,
  222. 0xeb0e363f,
  223. 0x72076785,
  224. 0x05005713,
  225. 0x95bf4a82,
  226. 0xe2b87a14,
  227. 0x7bb12bae,
  228. 0x0cb61b38,
  229. 0x92d28e9b,
  230. 0xe5d5be0d,
  231. 0x7cdcefb7,
  232. 0x0bdbdf21,
  233. 0x86d3d2d4,
  234. 0xf1d4e242,
  235. 0x68ddb3f8,
  236. 0x1fda836e,
  237. 0x81be16cd,
  238. 0xf6b9265b,
  239. 0x6fb077e1,
  240. 0x18b74777,
  241. 0x88085ae6,
  242. 0xff0f6a70,
  243. 0x66063bca,
  244. 0x11010b5c,
  245. 0x8f659eff,
  246. 0xf862ae69,
  247. 0x616bffd3,
  248. 0x166ccf45,
  249. 0xa00ae278,
  250. 0xd70dd2ee,
  251. 0x4e048354,
  252. 0x3903b3c2,
  253. 0xa7672661,
  254. 0xd06016f7,
  255. 0x4969474d,
  256. 0x3e6e77db,
  257. 0xaed16a4a,
  258. 0xd9d65adc,
  259. 0x40df0b66,
  260. 0x37d83bf0,
  261. 0xa9bcae53,
  262. 0xdebb9ec5,
  263. 0x47b2cf7f,
  264. 0x30b5ffe9,
  265. 0xbdbdf21c,
  266. 0xcabac28a,
  267. 0x53b39330,
  268. 0x24b4a3a6,
  269. 0xbad03605,
  270. 0xcdd70693,
  271. 0x54de5729,
  272. 0x23d967bf,
  273. 0xb3667a2e,
  274. 0xc4614ab8,
  275. 0x5d681b02,
  276. 0x2a6f2b94,
  277. 0xb40bbe37,
  278. 0xc30c8ea1,
  279. 0x5a05df1b,
  280. 0x2d02ef8d
  281. };
  282. /*====================================================================*
  283. *
  284. * uint32_t ReflectBits (uint32_t value, uint32_t bits);
  285. *
  286. * return the bitwise mirror image of an integer value;
  287. *
  288. * Motley Tools by Charles Maier;
  289. * Copyright (c) 2001-2006 by Charles Maier Associates;
  290. * Licensed under the Internet Software Consortium License;
  291. *
  292. *--------------------------------------------------------------------*/
  293. #if 0
  294. static uint32_t ReflectBits (uint32_t value, uint32_t bits)
  295. {
  296. uint32_t image = 0;
  297. uint32_t bit;
  298. for (bit = 0; bit < bits; bit++)
  299. {
  300. if (value & 1)
  301. {
  302. image |= 1 << (bits - bit - 1);
  303. }
  304. value >>= 1;
  305. }
  306. return (image);
  307. }
  308. #endif
  309. /*====================================================================*
  310. *
  311. * void InitCRCTable (void);
  312. *
  313. * write frame control sequence table with values; this function is
  314. * only needed to populate an empty CRCTable;
  315. *
  316. * Motley Tools by Charles Maier;
  317. * Copyright (c) 2001-2006 by Charles Maier Associates;
  318. * Licensed under the Internet Software Consortium License;
  319. *
  320. *--------------------------------------------------------------------*/
  321. #if 0
  322. static void InitCRCTable (uint32_t CRCTable [])
  323. {
  324. extern uint32_t CRCTable [];
  325. uint32_t word;
  326. uint32_t bit;
  327. for (word = 0; word < 256; word++)
  328. {
  329. uint32_t crc = ReflectBits (word, 8) << 24;
  330. for (bit = 0; bit < 8; bit++)
  331. {
  332. crc = (crc << 1) ^ ((crc & 0x80000000)? 0x04c11db7: 0);
  333. }
  334. CRCTable [word] = ReflectBits (crc, 32);
  335. }
  336. return;
  337. }
  338. #endif
  339. /*====================================================================*
  340. *
  341. * uint32_t ComputeCRC (uint8_t buffer [], uint32_t length);
  342. *
  343. *
  344. *
  345. * Motley Tools by Charles Maier;
  346. * Copyright (c) 2001-2006 by Charles Maier Associates;
  347. * Licensed under the Internet Software Consortium License;
  348. *
  349. *--------------------------------------------------------------------*/
  350. uint32_t ComputeCRC (uint8_t buffer [], uint32_t length)
  351. {
  352. extern uint32_t CRCTable [];
  353. uint32_t crc = ~0;
  354. while (length--)
  355. {
  356. crc = (crc >> 8) ^ CRCTable [(crc & 0xff) ^ *buffer++];
  357. }
  358. return (~crc);
  359. }
  360. /*====================================================================*
  361. *
  362. * int VerifyCRC (uint8_t buffer [], uint32_t length, uint32_t crc);
  363. *
  364. *
  365. *
  366. * Motley Tools by Charles Maier;
  367. * Copyright (c) 2001-2006 by Charles Maier Associates;
  368. * Licensed under the Internet Software Consortium License;
  369. *
  370. *--------------------------------------------------------------------*/
  371. int VerifyCRC (uint8_t buffer [], uint32_t length, uint32_t crc)
  372. {
  373. return (crc = ComputeCRC (buffer, length));
  374. }
  375. /*====================================================================*
  376. *
  377. * int main (int argc, const char * argv [])
  378. *
  379. *
  380. * Motley Tools by Charles Maier;
  381. * Copyright (c) 2001-2006 by Charles Maier Associates;
  382. * Licensed under the Internet Software Consortium License;
  383. *
  384. *--------------------------------------------------------------------*/
  385. #if 0
  386. #include <stdio.h>
  387. static uint8_t frame [192]=
  388. {
  389. 0x00,
  390. 0x00,
  391. 0x00,
  392. 0x00,
  393. 0x00,
  394. 0x01,
  395. 0x00,
  396. 0x00,
  397. 0x00,
  398. 0x00,
  399. 0x00,
  400. 0x02,
  401. 0x08,
  402. 0x00,
  403. 0x45,
  404. 0x60,
  405. 0x00,
  406. 0xAE,
  407. 0x00,
  408. 0x00,
  409. 0x40,
  410. 0x00,
  411. 0x40,
  412. 0x11,
  413. 0xDF,
  414. 0xB9,
  415. 0xAC,
  416. 0x11,
  417. 0x01,
  418. 0x02,
  419. 0xAC,
  420. 0x11,
  421. 0x01,
  422. 0x01,
  423. 0x00,
  424. 0x00,
  425. 0x00,
  426. 0x00,
  427. 0x00,
  428. 0x9A,
  429. 0xA4,
  430. 0x94,
  431. 0x00,
  432. 0x00,
  433. 0x00,
  434. 0x00,
  435. 0x00,
  436. 0x00,
  437. 0x00,
  438. 0x00,
  439. 0x00,
  440. 0x00,
  441. 0x00,
  442. 0x00,
  443. 0x00,
  444. 0x00,
  445. 0x00,
  446. 0x00,
  447. 0x00,
  448. 0x00,
  449. 0x00,
  450. 0x00,
  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. };
  582. int main (int argc, const char * argv [])
  583. {
  584. uint32_t size = sizeof (frame);
  585. uint32_t word;
  586. uint8_t blah [] =
  587. {
  588. 0x00,
  589. 0xB0,
  590. 0x52,
  591. 0x11,
  592. 0x22,
  593. 0x33,
  594. 0x00,
  595. 0x0A,
  596. 0x5E,
  597. 0x5A,
  598. 0x27,
  599. 0x38,
  600. 0x88,
  601. 0xE1,
  602. 0x00,
  603. 0x28,
  604. 0xA0,
  605. 0x00,
  606. 0xB0,
  607. 0x52,
  608. 0x02
  609. };
  610. while (size > 160)
  611. {
  612. word = ComputeCRC (frame, size);
  613. printf ("%d %08X\n", size, word);
  614. size -= sizeof (word);
  615. }
  616. size = 21;
  617. word = ComputeCRC (blah, size);
  618. printf ("%d %08X\n", size, word);
  619. return (0);
  620. }
  621. #endif
  622. /*====================================================================*
  623. *
  624. *--------------------------------------------------------------------*/
  625. #endif