NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
crc.cpp
Go to the documentation of this file.
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 
18 #include "crc.h"
19 
20 
21 /*
22  * Derive parameters from the standard-specific parameters in crc.h.
23  */
24 #define WIDTH (8 * sizeof(crc))
25 #define TOPBIT (1 << (WIDTH - 1))
26 
27 #if (REFLECT_DATA == CRC_TRUE)
28 #undef REFLECT_DATA
29 #define REFLECT_DATA(X) ((unsigned char) reflect((X), 8))
30 #else
31 #undef REFLECT_DATA
32 #define REFLECT_DATA(X) (X)
33 #endif
34 
35 #if (REFLECT_REMAINDER == CRC_TRUE)
36 #undef REFLECT_REMAINDER
37 #define REFLECT_REMAINDER(X) ((crc) reflect((X), WIDTH))
38 #else
39 #undef REFLECT_REMAINDER
40 #define REFLECT_REMAINDER(X) (X)
41 #endif
42 
43 
44 /*********************************************************************
45  *
46  * Function: reflect()
47  *
48  * Description: Reorder the bits of a binary sequence, by reflecting
49  * them about the middle position.
50  *
51  * Notes: No checking is done that nBits <= 32.
52  *
53  * Returns: The reflection of the original data.
54  *
55  *********************************************************************/
56 unsigned long
57 reflect(unsigned long data, unsigned char nBits)
58 {
59  unsigned long reflection = 0x00000000;
60  unsigned char bit;
61 
62  /*
63  * Reflect the data about the center bit.
64  */
65  for (bit = 0; bit < nBits; ++bit)
66  {
67  /*
68  * If the LSB bit is set, set the reflection of it.
69  */
70  if (data & 0x01)
71  {
72  reflection |= (1 << ((nBits - 1) - bit));
73  }
74 
75  data = (data >> 1);
76  }
77 
78  return (reflection);
79 
80 } /* reflect() */
81 
83 
84 
85 /*********************************************************************
86  *
87  * Function: crcInit()
88  *
89  * Description: Populate the partial CRC lookup table.
90  *
91  * Notes: This function must be rerun any time the CRC standard
92  * is changed. If desired, it can be run "offline" and
93  * the table results stored in an embedded system's ROM.
94  *
95  * Returns: None defined.
96  *
97  *********************************************************************/
98 void
99 crcInit(void)
100 {
101  crc remainder;
102  int dividend;
103  unsigned char bit;
104 
105 
106  /*
107  * Compute the remainder of each possible dividend.
108  */
109  for (dividend = 0; dividend < 256; ++dividend)
110  {
111  /*
112  * Start with the dividend followed by zeros.
113  */
114  remainder = dividend << (WIDTH - 8);
115 
116  /*
117  * Perform modulo-2 division, a bit at a time.
118  */
119  for (bit = 8; bit > 0; --bit)
120  {
121  /*
122  * Try to divide the current data bit.
123  */
124  if (remainder & TOPBIT)
125  {
126  remainder = (remainder << 1) ^ POLYNOMIAL;
127  }
128  else
129  {
130  remainder = (remainder << 1);
131  }
132  }
133 
134  /*
135  * Store the result into the table.
136  */
137  crcTable[dividend] = remainder;
138  }
139 
140 } /* crcInit() */