crc.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**********************************************************************
  2. *
  3. * Filename: crc.h
  4. *
  5. * Description: A header file describing the various CRC standards.
  6. *
  7. * Notes:
  8. *
  9. *
  10. * Copyright (c) 2000 by Michael Barr. This software is placed into
  11. * the public domain and may be used for any purpose. However, this
  12. * notice must not be changed or removed and no warranty is either
  13. * expressed or implied by its publication or distribution.
  14. **********************************************************************/
  15. #ifndef _crc_h
  16. #define _crc_h
  17. #if !defined(CRC_FALSE)
  18. #define CRC_FALSE 0
  19. #endif
  20. #if !defined(CRC_TRUE)
  21. #define CRC_TRUE !CRC_FALSE
  22. #endif
  23. /*
  24. * Select the CRC standard from the list that follows.
  25. */
  26. #define CRC32
  27. #if defined(CRC_CCITT)
  28. typedef unsigned short crc;
  29. #define CRC_NAME "CRC-CCITT"
  30. #define POLYNOMIAL 0x1021
  31. #define INITIAL_REMAINDER 0xFFFF
  32. #define FINAL_XOR_VALUE 0x0000
  33. #define REFLECT_DATA CRC_FALSE
  34. #define REFLECT_REMAINDER CRC_FALSE
  35. #define CHECK_VALUE 0x29B1
  36. #elif defined(CRC16)
  37. typedef unsigned short crc;
  38. #define CRC_NAME "CRC-16"
  39. #define POLYNOMIAL 0x8005
  40. #define INITIAL_REMAINDER 0x0000
  41. #define FINAL_XOR_VALUE 0x0000
  42. #define REFLECT_DATA CRC_TRUE
  43. #define REFLECT_REMAINDER CRC_TRUE
  44. #define CHECK_VALUE 0xBB3D
  45. #elif defined(CRC32)
  46. // xxxnsubtil: unsigned long was a 64-bit type for us! switched to nvbio's uint32 instead
  47. // xxxjpantaleoni: removed dependeny on nvbio and switched to unsigned int
  48. typedef unsigned int crc;
  49. #define CRC_NAME "CRC-32"
  50. #define POLYNOMIAL 0x04C11DB7
  51. #define INITIAL_REMAINDER 0xFFFFFFFF
  52. #define FINAL_XOR_VALUE 0xFFFFFFFF
  53. #define REFLECT_DATA CRC_TRUE
  54. #define REFLECT_REMAINDER CRC_TRUE
  55. #define CHECK_VALUE 0xCBF43926
  56. #else
  57. #error "One of CRC_CCITT, CRC16, or CRC32 must be #define'd."
  58. #endif
  59. /*
  60. * Derive parameters from the standard-specific parameters in crc.h.
  61. */
  62. #define WIDTH (8 * sizeof(crc))
  63. #define TOPBIT (1 << (WIDTH - 1))
  64. #if (REFLECT_DATA == CRC_TRUE)
  65. #undef REFLECT_DATA
  66. #define REFLECT_DATA(X) ((unsigned char) reflect((X), 8))
  67. #else
  68. #undef REFLECT_DATA
  69. #define REFLECT_DATA(X) (X)
  70. #endif
  71. #if (REFLECT_REMAINDER == CRC_TRUE)
  72. #undef REFLECT_REMAINDER
  73. #define REFLECT_REMAINDER(X) ((crc) reflect((X), WIDTH))
  74. #else
  75. #undef REFLECT_REMAINDER
  76. #define REFLECT_REMAINDER(X) (X)
  77. #endif
  78. unsigned long reflect(unsigned long data, unsigned char nBits);
  79. extern crc crcTable[256];
  80. void crcInit(void);
  81. /*********************************************************************
  82. *
  83. * Function: crcCalc()
  84. *
  85. * Description: Compute the CRC of a given message.
  86. *
  87. * Notes: crcInit() must be called first.
  88. *
  89. * Returns: The CRC of the message.
  90. *
  91. *********************************************************************/
  92. template <typename CharIterator>
  93. crc crcCalc(const CharIterator message, unsigned int nBytes)
  94. {
  95. crc remainder = INITIAL_REMAINDER;
  96. unsigned char data;
  97. unsigned int byte;
  98. /*
  99. * Divide the message by the polynomial, a byte at a time.
  100. */
  101. for (byte = 0; byte < nBytes; ++byte)
  102. {
  103. data = REFLECT_DATA(message[byte]) ^ (remainder >> (WIDTH - 8));
  104. remainder = crcTable[data] ^ (remainder << 8);
  105. }
  106. /*
  107. * The final remainder is the CRC.
  108. */
  109. return (REFLECT_REMAINDER(remainder) ^ FINAL_XOR_VALUE);
  110. }
  111. #endif /* _crc_h */