-
Notifications
You must be signed in to change notification settings - Fork 0
/
crc.h
290 lines (259 loc) · 7.17 KB
/
crc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#pragma once
//
// Author: Colin Mahoney (cmahoney@readysoft.es)
//
// C++ implementation of CRC error checking
//
// Based on Ross Williams' Rocksoft^tm Model CRC Algorithm,
// available as part of the document "A Painless Guide to
// CRC Error Detection Algorithms", also by Ross Williams.
//
// Ref 1: C-Users Journal, June 1999
// Ref 2: www.ross.net/crc/.
//
#include <sstream>
static const int CRCMaxBits = 32;
template<int Width, unsigned long Poly, unsigned long Init,
unsigned long XOrOut, bool Ref, bool Direct = true>
class CRCGenerator
{
public:
class CRCTable
{
unsigned long Table[256];
// Calculate the table entry at 'index'
//
unsigned long CalcTableEntry(int index)
{
unsigned long inbyte = (unsigned long)index;
if (Ref)
{
inbyte = Reflect(inbyte, 8);
}
unsigned long topbit = Bitmask(Width - 1);
unsigned long reg = inbyte << (Width - 8);
for (int i = 0; i < 8; i++)
{
if (reg & topbit)
{
reg = (reg << 1) ^ Poly;
}
else
{
reg <<= 1;
}
}
if (Ref)
{
reg = Reflect(reg, Width);
}
return reg & WidthMask(Width);
}
public:
CRCTable()
{
for (int i = 0; i < 256; i++)
{
Table[i] = CalcTableEntry(i);
}
}
unsigned long operator[](int i) const { return Table[i]; }
};
private:
static const CRCTable Table;
// Register holds the current value of the CRC calculation
unsigned long Register;
// Return an unsigned long with i'th bit set to one
static unsigned long Bitmask(int i) { return 1UL << i; }
// Reflect the bottom b bits of val
static unsigned long Reflect(unsigned long val, int b)
{
unsigned long t = val;
for (int i = 0; i < b; i++)
{
if (t & 1L)
{
val |= Bitmask((b - 1) - i);
}
else
{
val &= ~Bitmask((b - 1) - i);
}
t >>= 1;
}
return val;
}
// Return an unsigned long with value ( 2^width )-1
static unsigned long WidthMask(int width)
{
return (((1UL << (width - 1)) - 1UL) << 1) | 1UL;
}
public:
CRCGenerator() :
Register(Init) {}
unsigned long GetCRC() const
{
return (XOrOut ^ Register) & WidthMask(Width);
}
unsigned long GetNormalCRC() const
{
unsigned long normCRC;
if (!Ref)
{
normCRC = GetCRC();
normCRC <<= (CRCMaxBits - Width);
}
else
{
normCRC = 0;
unsigned long crc = GetCRC();
for (int i = 8; i <= CRCMaxBits; i += 8)
{
normCRC |= (crc & 0xFF) << (CRCMaxBits - i);
crc >>= 8;
}
}
return normCRC;
}
bool GetDirect() const { return Direct; }
bool GetReflect() const { return Ref; }
int GetWidth() const { return Width; }
void LoadRegister(unsigned long val)
{
if (Ref)
{
unsigned long v = 0;
int i;
for (i = 0; i < Width - 8; i += 8)
{
v <<= 8;
v |= (val & 0xFF);
val >>= 8;
}
int extraBits = Width & 0x07;
v <<= extraBits;
v |= (val & 0xFF) >> (8 - extraBits);
val = v;
}
Register ^= val;
}
void Process(unsigned char ch)
{
if (!Ref)
{
if (Direct)
{
Register = Table[((Register >> (Width - 8)) ^ ch) & 0xFFL]
^ (Register << 8);
}
else
{
Register = Table[(Register >> (Width - 8)) & 0xFFL] ^
(Register << 8);
Register ^= ch;
}
}
else
{
if (Direct)
{
Register = Table[(Register ^ ch) & 0xFFL] ^
(Register >> 8);
}
else
{
Register = Table[Register & 0xFFL] ^ (Register >> 8);
Register ^= ch << (Width - 8);
}
}
}
void Process(unsigned char* block, int block_length)
{
for (int i = 0; i < block_length; i++)
{
Process(*block++);
}
}
// Un-comment the following version if no member templates
/// void Process( unsigned char* first, unsigned char* last )
/// {
/// while( first != last )
/// {
/// Process( *first++ );
/// }
/// }
// Comment out the following version if no member templates
template<class InIter>
void Process(InIter first, InIter last)
{
while (first != last)
{
Process(*first);
++first;
}
}
// Process 'count' bits from 'bits'. if 'Ref' is false reads
// starting from MSB of 'bits'. If 'Ref' is true starts from
// LSB
void ProcessBits(unsigned char bits, int count)
{
if (!Ref)
{
if (Direct)
{
Register = Table[((Register >> (Width - count)) ^
(bits >> (8 - count))) & (0xFF >> (8 - count))] ^
(Register << count);
}
else
{
Register = Table[(Register >> (Width - count)) &
(0xFF >> (8 - count))] ^ (Register << count);
Register ^= bits >> (8 - count);
}
}
else
{
if (Direct)
{
Register = Table[((Register ^ bits) & (0xFF >> (8 - count)))
<< (8 - count)] ^ (Register >> count);
}
else
{
Register = Table[(Register & (0xFF >> (8 - count))) <<
(8 - count)] ^ (Register >> count);
Register ^= bits << (Width - count);
}
}
}
void Reset()
{
Register = Init;
}
void Write(std::ostream& os) const
{
unsigned long ncrc = GetNormalCRC();
for (int i = 0; i < Width; i += 8)
{
unsigned char byte =
(unsigned char)(ncrc >> (CRCMaxBits - i - 8));
os << byte;
}
}
};
template<int Width, unsigned long Poly, unsigned long Init,
unsigned long XOrOut, bool Ref, bool Direct>
const typename CRCGenerator<Width, Poly, Init, XOrOut, Ref,
Direct>::CRCTable
CRCGenerator<Width, Poly, Init, XOrOut, Ref, Direct>::Table;
template<int Width, unsigned long Poly, unsigned long Init,
unsigned long XOrOut, bool Ref, bool Direct>
std::ostream& operator<<(std::ostream& os,
CRCGenerator<Width, Poly, Init, XOrOut, Ref, Direct>& crc)
{
crc.Write(os);
return os;
}
typedef CRCGenerator<16, 0x8005, 0, 0, true> CRC16;
typedef CRCGenerator<32, 0x80000005, -1, 0, true> CRC32;