-
Notifications
You must be signed in to change notification settings - Fork 47
/
crc.c
462 lines (427 loc) · 14.9 KB
/
crc.c
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* crc.c -- Generic CRC calculations
* Copyright (C) 2014, 2016, 2017, 2020, 2021 Mark Adler
* For conditions of distribution and use, see copyright notice in crcany.c.
*/
#include <stddef.h>
#include <assert.h>
#include "crc.h"
word_t crc_bitwise(model_t *model, word_t crc, void const *dat, size_t len) {
unsigned char const *buf = dat;
word_t poly = model->poly;
// If requested, return the initial CRC.
if (buf == NULL)
return model->init;
// Pre-process the CRC.
crc ^= model->xorout;
if (model->rev)
crc = reverse(crc, model->width);
// Process the input data a bit at a time.
if (model->ref) {
crc &= ONES(model->width);
while (len--) {
crc ^= *buf++;
for (int k = 0; k < 8; k++)
crc = crc & 1 ? (crc >> 1) ^ poly : crc >> 1;
}
}
else if (model->width <= 8) {
unsigned shift = 8 - model->width; // 0..7
poly <<= shift;
crc <<= shift;
while (len--) {
crc ^= *buf++;
for (int k = 0; k < 8; k++)
crc = crc & 0x80 ? (crc << 1) ^ poly : crc << 1;
}
crc >>= shift;
crc &= ONES(model->width);
}
else {
word_t mask = (word_t)1 << (model->width - 1);
unsigned shift = model->width - 8; // 1..WORDBITS-8
while (len--) {
crc ^= (word_t)(*buf++) << shift;
for (int k = 0; k < 8; k++)
crc = crc & mask ? (crc << 1) ^ poly : crc << 1;
}
crc &= ONES(model->width);
}
// Post-process and return the CRC.
if (model->rev)
crc = reverse(crc, model->width);
return crc ^ model->xorout;
}
void crc_table_bytewise(model_t *model) {
unsigned char k = 0;
do {
word_t crc = crc_bitwise(model, 0, &k, 1);
if (model->rev)
crc = reverse(crc, model->width);
if (model->width < 8 && !model->ref)
crc <<= 8 - model->width;
model->table_byte[k] = crc;
} while (++k);
}
word_t crc_bytewise(model_t *model, word_t crc, void const *dat, size_t len) {
unsigned char const *buf = dat;
// If requested, return the initial CRC.
if (buf == NULL)
return model->init;
// Pre-process the CRC.
if (model->rev)
crc = reverse(crc, model->width);
// Process the input data a byte at a time.
if (model->ref) {
crc &= ONES(model->width);
while (len--)
crc = (crc >> 8) ^ model->table_byte[(crc ^ *buf++) & 0xff];
}
else if (model->width <= 8) {
unsigned shift = 8 - model->width; // 0..7
crc <<= shift;
while (len--)
crc = model->table_byte[crc ^ *buf++];
crc >>= shift;
}
else {
unsigned shift = model->width - 8; // 1..WORDBITS-8
while (len--)
crc = (crc << 8) ^
model->table_byte[((crc >> shift) ^ *buf++) & 0xff];
crc &= ONES(model->width);
}
// Post-process and return the CRC
if (model->rev)
crc = reverse(crc, model->width);
return crc;
}
// Swap the low n bytes of x. Bytes above those are discarded.
static inline word_t swaplow(word_t x, unsigned n) {
if (n == 0)
return 0;
word_t y = x & 0xff;
while (--n) {
x >>= 8;
y <<= 8;
y |= x & 0xff;
}
return y;
}
// Swap the bytes in a word_t. swap() is used at most twice per crc_wordwise()
// call, and then only on little-endian machines if the CRC is not reflected,
// or on big-endian machines if the CRC is reflected.
static inline word_t swap(word_t x) {
return swaplow(x, WORDCHARS);
}
void crc_table_wordwise(model_t *model, unsigned little, unsigned word_bits) {
unsigned opp = little ^ model->ref;
unsigned top =
model->ref ? 0 :
word_bits - (model->width > 8 ? model->width : 8);
word_t xor = model->xorout;
if (model->width < 8 && !model->ref)
xor <<= 8 - model->width;
unsigned word_bytes = word_bits >> 3;
for (unsigned k = 0; k < 256; k++) {
word_t crc = model->table_byte[k];
model->table_word[0][k] = opp ? swaplow(crc << top, word_bytes) :
crc << top;
for (unsigned n = 1; n < (word_bits >> 3); n++) {
crc ^= xor;
if (model->ref)
crc = (crc >> 8) ^ model->table_byte[crc & 0xff];
else if (model->width <= 8)
crc = model->table_byte[crc];
else {
crc = (crc << 8) ^
model->table_byte[(crc >> (model->width - 8)) & 0xff];
crc &= ONES(model->width);
}
crc ^= xor;
model->table_word[n][k] = opp ? swaplow(crc << top, word_bytes) :
crc << top;
}
}
}
word_t crc_wordwise(model_t *model, word_t crc, void const *dat, size_t len) {
unsigned char const *buf = dat;
// If requested, return the initial CRC.
if (buf == NULL)
return model->init;
// Prepare common constants.
unsigned little = 1;
little = *((unsigned char *)(&little));
unsigned top = model->ref ? 0 :
WORDBITS - (model->width > 8 ? model->width : 8);
unsigned shift = model->width <= 8 ? 8 - model->width : model->width - 8;
// Pre-process the CRC.
if (model->rev)
crc = reverse(crc, model->width);
// Process the first few bytes up to a word_t boundary, if any.
if (model->ref) {
crc &= ONES(model->width);
while (len && ((ptrdiff_t)buf & (WORDCHARS - 1))) {
crc = (crc >> 8) ^ model->table_byte[(crc ^ *buf++) & 0xff];
len--;
}
}
else if (model->width <= 8) {
crc <<= shift;
while (len && ((ptrdiff_t)buf & (WORDCHARS - 1))) {
crc = model->table_byte[(crc ^ *buf++) & 0xff];
len--;
}
}
else
while (len && ((ptrdiff_t)buf & (WORDCHARS - 1))) {
crc = (crc << 8) ^
model->table_byte[((crc >> shift) ^ *buf++) & 0xff];
len--;
}
// Process as many word_t's as are available.
if (len >= WORDCHARS) {
crc <<= top;
if (little) {
if (!model->ref)
crc = swap(crc);
do {
crc ^= *(word_t const *)buf;
crc = model->table_word[WORDCHARS - 1][crc & 0xff]
^ model->table_word[WORDCHARS - 2][(crc >> 8)
#if WORDCHARS > 2
& 0xff]
^ model->table_word[WORDCHARS - 3][(crc >> 16) & 0xff]
^ model->table_word[WORDCHARS - 4][(crc >> 24)
#if WORDCHARS > 4
& 0xff]
^ model->table_word[WORDCHARS - 5][(crc >> 32) & 0xff]
^ model->table_word[WORDCHARS - 6][(crc >> 40) & 0xff]
^ model->table_word[WORDCHARS - 7][(crc >> 48) & 0xff]
^ model->table_word[WORDCHARS - 8][(crc >> 56)
#if WORDCHARS > 8
& 0xff]
^ model->table_word[WORDCHARS - 9][(crc >> 64) & 0xff]
^ model->table_word[WORDCHARS - 10][(crc >> 72) & 0xff]
^ model->table_word[WORDCHARS - 11][(crc >> 80) & 0xff]
^ model->table_word[WORDCHARS - 12][(crc >> 88) & 0xff]
^ model->table_word[WORDCHARS - 13][(crc >> 96) & 0xff]
^ model->table_word[WORDCHARS - 14][(crc >> 104) & 0xff]
^ model->table_word[WORDCHARS - 15][(crc >> 112) & 0xff]
^ model->table_word[WORDCHARS - 16][(crc >> 120)
#endif
#endif
#endif
];
buf += WORDCHARS;
len -= WORDCHARS;
} while (len >= WORDCHARS);
if (!model->ref)
crc = swap(crc);
}
else {
if (model->ref)
crc = swap(crc);
do {
crc ^= *(word_t const *)buf;
crc = model->table_word[0][crc & 0xff]
^ model->table_word[1][(crc >> 8)
#if WORDCHARS > 2
& 0xff]
^ model->table_word[2][(crc >> 16) & 0xff]
^ model->table_word[3][(crc >> 24)
#if WORDCHARS > 4
& 0xff]
^ model->table_word[4][(crc >> 32) & 0xff]
^ model->table_word[5][(crc >> 40) & 0xff]
^ model->table_word[6][(crc >> 48) & 0xff]
^ model->table_word[7][(crc >> 56)
#if WORDCHARS > 8
& 0xff]
^ model->table_word[8][(crc >> 64) & 0xff]
^ model->table_word[9][(crc >> 72) & 0xff]
^ model->table_word[10][(crc >> 80) & 0xff]
^ model->table_word[11][(crc >> 88) & 0xff]
^ model->table_word[12][(crc >> 96) & 0xff]
^ model->table_word[13][(crc >> 104) & 0xff]
^ model->table_word[14][(crc >> 112) & 0xff]
^ model->table_word[15][(crc >> 120)
#endif
#endif
#endif
];
buf += WORDCHARS;
len -= WORDCHARS;
} while (len >= WORDCHARS);
if (model->ref)
crc = swap(crc);
}
crc >>= top;
}
// Process any remaining bytes after the last word_t.
if (model->ref)
while (len--)
crc = (crc >> 8) ^ model->table_byte[(crc ^ *buf++) & 0xff];
else if (model->width <= 8) {
while (len--)
crc = model->table_byte[(crc ^ *buf++) & 0xff];
crc >>= shift;
}
else {
while (len--)
crc = (crc << 8) ^
model->table_byte[((crc >> shift) ^ *buf++) & 0xff];
crc &= ONES(model->width);
}
// Post-process and return the CRC.
if (model->rev)
crc = reverse(crc, model->width);
return crc;
}
// Return a(x) multiplied by b(x) modulo p(x), where p(x) is the CRC
// polynomial. For speed, this requires that a not be zero.
static word_t multmodp(model_t *model, word_t a, word_t b) {
word_t top = (word_t)1 << (model->width - 1);
word_t prod = 0;
if (model->ref) {
// Reflected polynomial.
for (;;) {
if (a & top) {
prod ^= b;
if ((a & (top - 1)) == 0)
break;
}
a <<= 1;
b = b & 1 ? (b >> 1) ^ model->poly : b >> 1;
}
}
else {
// Normal polynomial.
for (;;) {
if (a & 1) {
prod ^= b;
if (a == 1)
break;
}
a >>= 1;
b = b & top ? (b << 1) ^ model->poly : b << 1;
}
prod &= ((top << 1) - 1);
}
return prod;
}
// Build table_comb[] for model. Stop when a cycle is detected, or the table is
// full. On return, model->cycle is the number of entries in the table, which
// is the index at which to cycle. model->back is the index to go to when
// model->cycle is reached. If no cycle was detected, then model->back is -1.
void crc_table_combine(model_t *model) {
// Keep squaring x^1 modulo p(x), where p(x) is the CRC polynomial, to
// generate x^2^n modulo p(x).
word_t sq = model->ref ? (word_t)1 << (model->width - 2) : 2; // x^1
model->table_comb[0] = sq;
int n = 1;
while ((unsigned)n < sizeof(model->table_comb) / sizeof(word_t)) {
sq = multmodp(model, sq, sq); // x^2^n
// If this value has already appeared, then done.
for (int j = 0; j < n; j++)
if (model->table_comb[j] == sq) {
model->cycle = n;
model->back = j;
return;
}
// New value -- append to table.
model->table_comb[n++] = sq;
}
// No cycle was found, up to the size of the table.
model->cycle = n;
model->back = -1;
#ifdef FIND_CYCLE
# define GIVEUP 10000
// Just out of curiosity, see when x^2^n cycles for this CRC.
word_t comb[GIVEUP];
for (int k = 0; k < n; k++)
comb[k] = model->table_comb[k];
while (n < GIVEUP) {
sq = multmodp(model, sq, sq);
for (int j = 0; j < n; j++)
if (comb[j] == sq) {
fprintf(stderr, "%s cycled at %u to %u\n",
model->name, n, j);
return;
}
comb[n++] = sq;
}
fprintf(stderr, "%s never cycled?\n", model->name);
#endif
}
word_t crc_zeros(model_t *model, word_t crc, uintmax_t n) {
// Pre-process the CRC.
crc ^= model->xorout;
if (model->rev)
crc = reverse(crc, model->width);
// Apply n zero bits to crc.
if (n < 128) {
word_t poly = model->poly;
if (model->ref) {
crc &= ONES(model->width);
while (n--)
crc = crc & 1 ? (crc >> 1) ^ poly : crc >> 1;
}
else {
word_t mask = (word_t)1 << (model->width - 1);
while (n--)
crc = crc & mask ? (crc << 1) ^ poly : crc << 1;
crc &= ONES(model->width);
}
}
else {
crc &= ONES(model->width);
int k = 0;
for (;;) {
if (n & 1)
crc = multmodp(model, model->table_comb[k], crc);
n >>= 1;
if (n == 0)
break;
if (++k == model->cycle) {
assert(model->back != -1);
k = model->back;
}
}
}
// Post-process and return the CRC.
if (model->rev)
crc = reverse(crc, model->width);
return crc ^ model->xorout;
}
// Return x^(8n) modulo p(x), where p(x) is the CRC polynomial. model->cycle
// and model->table_comb[] must first be initialized by crc_table_combine().
static word_t x8nmodp(model_t *model, uintmax_t n) {
word_t xp = model->ref ? (word_t)1 << (model->width - 1) : 1; // x^0
int k = model->cycle > 3 ? 3 :
model->cycle == 3 ? model->back :
model->cycle - 1;
for (;;) {
if (n & 1)
xp = multmodp(model, model->table_comb[k], xp);
n >>= 1;
if (n == 0)
break;
if (++k == model->cycle) {
assert(model->back != -1);
k = model->back;
}
}
return xp;
}
word_t crc_combine(model_t *model, word_t crc1, word_t crc2,
uintmax_t len2) {
crc1 ^= model->init;
if (model->rev) {
crc1 = reverse(crc1, model->width);
crc2 = reverse(crc2, model->width);
}
word_t crc = multmodp(model, x8nmodp(model, len2), crc1) ^ crc2;
if (model->rev)
crc = reverse(crc, model->width);
return crc;
}