NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
crc.h
Go to the documentation of this file.
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 
16 #ifndef _crc_h
17 #define _crc_h
18 
19 #if !defined(CRC_FALSE)
20 #define CRC_FALSE 0
21 #endif
22 #if !defined(CRC_TRUE)
23 #define CRC_TRUE !CRC_FALSE
24 #endif
25 
26 /*
27  * Select the CRC standard from the list that follows.
28  */
29 #define CRC32
30 
31 
32 #if defined(CRC_CCITT)
33 
34 typedef unsigned short crc;
35 
36 #define CRC_NAME "CRC-CCITT"
37 #define POLYNOMIAL 0x1021
38 #define INITIAL_REMAINDER 0xFFFF
39 #define FINAL_XOR_VALUE 0x0000
40 #define REFLECT_DATA CRC_FALSE
41 #define REFLECT_REMAINDER CRC_FALSE
42 #define CHECK_VALUE 0x29B1
43 
44 #elif defined(CRC16)
45 
46 typedef unsigned short crc;
47 
48 #define CRC_NAME "CRC-16"
49 #define POLYNOMIAL 0x8005
50 #define INITIAL_REMAINDER 0x0000
51 #define FINAL_XOR_VALUE 0x0000
52 #define REFLECT_DATA CRC_TRUE
53 #define REFLECT_REMAINDER CRC_TRUE
54 #define CHECK_VALUE 0xBB3D
55 
56 #elif defined(CRC32)
57 
58 // xxxnsubtil: unsigned long was a 64-bit type for us! switched to nvbio's uint32 instead
59 // xxxjpantaleoni: removed dependeny on nvbio and switched to unsigned int
60 typedef unsigned int crc;
61 
62 #define CRC_NAME "CRC-32"
63 #define POLYNOMIAL 0x04C11DB7
64 #define INITIAL_REMAINDER 0xFFFFFFFF
65 #define FINAL_XOR_VALUE 0xFFFFFFFF
66 #define REFLECT_DATA CRC_TRUE
67 #define REFLECT_REMAINDER CRC_TRUE
68 #define CHECK_VALUE 0xCBF43926
69 
70 #else
71 
72 #error "One of CRC_CCITT, CRC16, or CRC32 must be #define'd."
73 
74 #endif
75 
76 /*
77  * Derive parameters from the standard-specific parameters in crc.h.
78  */
79 #define WIDTH (8 * sizeof(crc))
80 #define TOPBIT (1 << (WIDTH - 1))
81 
82 #if (REFLECT_DATA == CRC_TRUE)
83 #undef REFLECT_DATA
84 #define REFLECT_DATA(X) ((unsigned char) reflect((X), 8))
85 #else
86 #undef REFLECT_DATA
87 #define REFLECT_DATA(X) (X)
88 #endif
89 
90 #if (REFLECT_REMAINDER == CRC_TRUE)
91 #undef REFLECT_REMAINDER
92 #define REFLECT_REMAINDER(X) ((crc) reflect((X), WIDTH))
93 #else
94 #undef REFLECT_REMAINDER
95 #define REFLECT_REMAINDER(X) (X)
96 #endif
97 
98 unsigned long reflect(unsigned long data, unsigned char nBits);
99 
100 extern crc crcTable[256];
101 
102 void crcInit(void);
103 
104 /*********************************************************************
105  *
106  * Function: crcCalc()
107  *
108  * Description: Compute the CRC of a given message.
109  *
110  * Notes: crcInit() must be called first.
111  *
112  * Returns: The CRC of the message.
113  *
114  *********************************************************************/
115 template <typename CharIterator>
116 crc crcCalc(const CharIterator message, unsigned int nBytes)
117 {
118  crc remainder = INITIAL_REMAINDER;
119  unsigned char data;
120  unsigned int byte;
121 
122  /*
123  * Divide the message by the polynomial, a byte at a time.
124  */
125  for (byte = 0; byte < nBytes; ++byte)
126  {
127  data = REFLECT_DATA(message[byte]) ^ (remainder >> (WIDTH - 8));
128  remainder = crcTable[data] ^ (remainder << 8);
129  }
130 
131  /*
132  * The final remainder is the CRC.
133  */
134  return (REFLECT_REMAINDER(remainder) ^ FINAL_XOR_VALUE);
135 }
136 
137 #endif /* _crc_h */