crc.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**********************************************************************
  2. *
  3. * Filename: crc.c
  4. *
  5. * Description: Slow and fast implementations of the CRC standards.
  6. *
  7. * Notes: The parameters for each supported CRC standard are
  8. * defined in the header file crc.h. The implementations
  9. * here should stand up to further additions to that list.
  10. *
  11. *
  12. * Copyright (c) 2000 by Michael Barr. This software is placed into
  13. * the public domain and may be used for any purpose. However, this
  14. * notice must not be changed or removed and no warranty is either
  15. * expressed or implied by its publication or distribution.
  16. **********************************************************************/
  17. #include "crc.h"
  18. /*
  19. * Derive parameters from the standard-specific parameters in crc.h.
  20. */
  21. #define WIDTH (8 * sizeof(crc))
  22. #define TOPBIT (1 << (WIDTH - 1))
  23. #if (REFLECT_DATA == CRC_TRUE)
  24. #undef REFLECT_DATA
  25. #define REFLECT_DATA(X) ((unsigned char) reflect((X), 8))
  26. #else
  27. #undef REFLECT_DATA
  28. #define REFLECT_DATA(X) (X)
  29. #endif
  30. #if (REFLECT_REMAINDER == CRC_TRUE)
  31. #undef REFLECT_REMAINDER
  32. #define REFLECT_REMAINDER(X) ((crc) reflect((X), WIDTH))
  33. #else
  34. #undef REFLECT_REMAINDER
  35. #define REFLECT_REMAINDER(X) (X)
  36. #endif
  37. /*********************************************************************
  38. *
  39. * Function: reflect()
  40. *
  41. * Description: Reorder the bits of a binary sequence, by reflecting
  42. * them about the middle position.
  43. *
  44. * Notes: No checking is done that nBits <= 32.
  45. *
  46. * Returns: The reflection of the original data.
  47. *
  48. *********************************************************************/
  49. unsigned long
  50. reflect(unsigned long data, unsigned char nBits)
  51. {
  52. unsigned long reflection = 0x00000000;
  53. unsigned char bit;
  54. /*
  55. * Reflect the data about the center bit.
  56. */
  57. for (bit = 0; bit < nBits; ++bit)
  58. {
  59. /*
  60. * If the LSB bit is set, set the reflection of it.
  61. */
  62. if (data & 0x01)
  63. {
  64. reflection |= (1 << ((nBits - 1) - bit));
  65. }
  66. data = (data >> 1);
  67. }
  68. return (reflection);
  69. } /* reflect() */
  70. crc crcTable[256];
  71. /*********************************************************************
  72. *
  73. * Function: crcInit()
  74. *
  75. * Description: Populate the partial CRC lookup table.
  76. *
  77. * Notes: This function must be rerun any time the CRC standard
  78. * is changed. If desired, it can be run "offline" and
  79. * the table results stored in an embedded system's ROM.
  80. *
  81. * Returns: None defined.
  82. *
  83. *********************************************************************/
  84. void
  85. crcInit(void)
  86. {
  87. crc remainder;
  88. int dividend;
  89. unsigned char bit;
  90. /*
  91. * Compute the remainder of each possible dividend.
  92. */
  93. for (dividend = 0; dividend < 256; ++dividend)
  94. {
  95. /*
  96. * Start with the dividend followed by zeros.
  97. */
  98. remainder = dividend << (WIDTH - 8);
  99. /*
  100. * Perform modulo-2 division, a bit at a time.
  101. */
  102. for (bit = 8; bit > 0; --bit)
  103. {
  104. /*
  105. * Try to divide the current data bit.
  106. */
  107. if (remainder & TOPBIT)
  108. {
  109. remainder = (remainder << 1) ^ POLYNOMIAL;
  110. }
  111. else
  112. {
  113. remainder = (remainder << 1);
  114. }
  115. }
  116. /*
  117. * Store the result into the table.
  118. */
  119. crcTable[dividend] = remainder;
  120. }
  121. } /* crcInit() */