-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrc_generic.c
295 lines (288 loc) · 9.73 KB
/
crc_generic.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
/**
* CRC GENERIC LIBRARY.
*
* Copyright (c) 2016 Zaltora (https://github.com/Zaltora)
*
* BSD Licensed as described in the file LICENSE
*/
#include "crc_generic.h"
#if (CRC_DEBUG == 1)
#define debug(fmt, ...) printf("%s: " fmt "\n", "CRC", ## __VA_ARGS__)
#else
#define debug(fmt, ...)
#endif
#define _CRC_SET_ALGO_SRC(type) \
static type _##type##_reflect(type crc, int bitnum)\
{\
type i, j=1, crcout=0;\
for (i=(type)1<<(bitnum-1); i; i>>=1)\
{\
if (crc & i) crcout|=j;\
j<<= 1;\
}\
return (crcout);\
}\
void type##_generate_table(config_##type* crcdata, type* tab, crc_16 len)\
{\
if (crcdata->order&7)\
return;\
crc_16 i,j;\
type bit, crc;\
for (i=0; i<len; i++)\
{\
crc=(type)i;\
if (crcdata->refin) crc=_##type##_reflect(crc, 8);\
crc<<= crcdata->order-8;\
for (j=0; j<8; j++){\
bit = crc & crcdata->private.crchighbit;\
crc<<= 1;\
if (bit) crc^= crcdata->polynom;\
}\
if (crcdata->refin) crc = _##type##_reflect(crc, crcdata->order);\
crc&= crcdata->private.crcmask;\
tab[i] = crc;\
}\
}\
static type type##_bit_by_bit_fast_upd(config_##type* crcdata, crc_8* p, crc_16 len)\
{\
type j, c, bit;\
type crc = crcdata->private.seed_memory ;\
while (len--)\
{\
c = (type)*p++;\
if (crcdata->refin)\
c = _##type##_reflect(c, 8);\
for (j=0x80; j; j>>=1)\
{\
bit = crc & crcdata->private.crchighbit;\
crc<<= 1;\
if (c & j) bit^= crcdata->private.crchighbit;\
if (bit) crc^= crcdata->polynom;\
}\
}\
crcdata->private.seed_memory = crc;\
if (crcdata->refout) crc=_##type##_reflect(crc, crcdata->order);\
crc^= crcdata->crcxor;\
crc&= crcdata->private.crcmask;\
return(crc);\
}\
static type type##_bit_by_bit_fast(config_##type* crcdata, crc_8* p, crc_16 len)\
{\
debug("ALGO: bit by bit fast.");\
crcdata->private.seed_memory = crcdata->private.crcinit_direct;\
return type##_bit_by_bit_fast_upd(crcdata, p, len);\
}\
static type type##_bit_by_bit_upd(config_##type* crcdata, crc_8* p, crc_16 len)\
{\
crc_8 i;\
type j, c, bit;\
type crc = crcdata->private.seed_memory;\
while (len--)\
{\
c = (type)*p++;\
if (crcdata->refin)\
c = _##type##_reflect(c, 8);\
for (j=0x80; j; j>>=1)\
{\
bit = crc & crcdata->private.crchighbit;\
crc<<= 1;\
if (c & j) crc|= 1;\
if (bit) crc^= crcdata->polynom;\
}\
}\
crcdata->private.seed_memory = crc;\
for (i=0; i<crcdata->order; i++)\
{\
bit = crc & crcdata->private.crchighbit;\
crc<<= 1;\
if (bit) crc^= crcdata->polynom;\
}\
if (crcdata->refout) crc=_##type##_reflect(crc, crcdata->order);\
crc^= crcdata->crcxor;\
crc&= crcdata->private.crcmask;\
return(crc);\
}\
static type type##_bit_by_bit(config_##type* crcdata, crc_8* p, crc_16 len)\
{\
debug("ALGO: bit by bit.");\
crcdata->private.seed_memory = crcdata->private.crcinit_nondirect;\
return type##_bit_by_bit_upd(crcdata, p, len);\
}\
static type type##_table_upd(config_##type* crcdata, crc_8* p, crc_16 len)\
{\
crc_8 size = ((crcdata->order-1)/8+1)*8;\
type crc = crcdata->private.seed_memory;\
if (!crcdata->refin)\
while (len--) crc = ((crc << 8) | *p++) ^ crcdata->private.tab[(crc >> (size-8)) & 0xff];\
else\
while (len--) crc = ((crc >> 8) | (*p++ << (size-8))) ^ crcdata->private.tab[crc & 0xff];\
crcdata->private.seed_memory= crc ;\
if (!crcdata->refin)\
while (++len < size/8) crc = (crc << 8) ^ crcdata->private.tab[(crc >> (size-8)) & 0xff];\
else\
while (++len < size/8) crc = (crc >> 8) ^ crcdata->private.tab[crc & 0xff];\
if (crcdata->refout^crcdata->refin) crc = _##type##_reflect(crc, crcdata->order);\
crc>>= size-crcdata->order;\
crc^= crcdata->crcxor;\
crc&= crcdata->private.crcmask;\
return(crc);\
}\
static type type##_table (config_##type* crcdata, crc_8* p, crc_16 len)\
{\
debug("ALGO: table.");\
crcdata->private.seed_memory = crcdata->private.crcinit_nondirect;\
if (crcdata->refin) crcdata->private.seed_memory = _##type##_reflect(crcdata->private.seed_memory, crcdata->order);\
return type##_table_upd(crcdata, p, len);\
}\
static type type##_table_fast_upd(config_##type* crcdata, crc_8* p, crc_16 len)\
{\
crc_8 size = ((crcdata->order-1)/8+1)*8;\
type crc = crcdata->private.seed_memory;\
if (!crcdata->refin)\
while (len--) { crc = (crc << 8) ^ crcdata->private.tab[((crc >> (size-8)) & 0xff) ^ *p++]; }\
else\
while (len--) { crc = (crc >> 8) ^ crcdata->private.tab[(crc & 0xff) ^ *p++]; }\
crcdata->private.seed_memory= crc ;\
if (crcdata->refout^crcdata->refin) crc = _##type##_reflect(crc, crcdata->order);\
crc>>= size-crcdata->order;\
crc^= crcdata->crcxor;\
crc&= crcdata->private.crcmask;\
return(crc);\
}\
static type type##_table_fast(config_##type* crcdata, crc_8* p, crc_16 len)\
{\
debug("ALGO: table fast.");\
crcdata->private.seed_memory = crcdata->private.crcinit_direct;\
if (crcdata->refin) crcdata->private.seed_memory = _##type##_reflect(crcdata->private.seed_memory, crcdata->order);\
return type##_table_fast_upd(crcdata, p, len);\
}\
void type##_generic_init(config_##type* crcdata, type polynom, type order, type crcinit,\
type crcxor, crc_8 direct, crc_8 refin, crc_8 refout)\
{\
crc_16 i;\
type bit, crc;\
crcdata->crcinit = crcinit;\
crcdata->crcxor = crcxor;\
crcdata->order = order;\
crcdata->direct = direct;\
crcdata->polynom = polynom;\
crcdata->refin = refin;\
crcdata->refout = refout;\
/* Use default algorithm crc */\
crcdata->private.crc_compute = (type(*)(config_##type*,crc_8*,crc_16))type##_bit_by_bit_fast;\
crcdata->private.crc_update = (type(*)(config_##type*,crc_8*,crc_16))type##_bit_by_bit_fast_upd;\
/* Compute mask data */\
crcdata->private.crcmask = ((((type)1<<(crcdata->order-1))-1)<<1)|1;\
crcdata->private.crchighbit = (type)1<<(crcdata->order-1);\
/* Do some check */\
if (crcdata->order < 1 || crcdata->order > sizeof(crcdata->private.crcmask)*8)\
{\
debug("ERROR, invalid order, it must be between 1..%u.",sizeof(type)*8);\
return;\
}\
if (crcdata->polynom != (crcdata->polynom & crcdata->private.crcmask))\
{\
debug("ERROR, invalid polynom.");\
return;\
}\
if (crcdata->crcinit != (crcdata->crcinit & crcdata->private.crcmask))\
{\
debug("ERROR, invalid crcinit.");\
return;\
}\
if (crcdata->crcxor != (crcdata->crcxor & crcdata->private.crcmask))\
{\
debug("ERROR, invalid crcxor.");\
return;\
}\
/* compute missing initial CRC value */\
if (!crcdata->direct)\
{\
crcdata->private.crcinit_nondirect = crcdata->crcinit;\
crc = crcdata->crcinit;\
for (i=0; i<crcdata->order; i++)\
{\
bit = crc & crcdata->private.crchighbit;\
crc<<= 1;\
if (bit) crc^= crcdata->polynom;\
}\
crc&= crcdata->private.crcmask;\
crcdata->private.crcinit_direct = crc;\
}\
else\
{\
crcdata->private.crcinit_direct = crcdata->crcinit;\
crc = crcdata->crcinit;\
for (i=0; i<crcdata->order; i++)\
{\
bit = crc & 1;\
if (bit) crc^= crcdata->polynom;\
crc >>= 1;\
if (bit) crc|= crcdata->private.crchighbit;\
}\
crcdata->private.crcinit_nondirect = crc;\
}\
}\
void type##_generic_select_algo(config_##type* crcdata, const type* tab, crc_16 len, crc_algo algo)\
{\
if (tab == NULL)\
{\
switch (algo)\
{\
case CRC_BIT_TO_BIT:\
crcdata->private.crc_compute = (type(*)(config_##type*,crc_8*,crc_16))type##_bit_by_bit;\
crcdata->private.crc_update = (type(*)(config_##type*,crc_8*,crc_16))type##_bit_by_bit_upd;\
break;\
case CRC_BIT_TO_BIT_FAST:\
crcdata->private.crc_compute = (type(*)(config_##type*,crc_8*,crc_16))type##_bit_by_bit_fast;\
crcdata->private.crc_update = (type(*)(config_##type*,crc_8*,crc_16))type##_bit_by_bit_fast_upd;\
break;\
default:\
debug("ERROR, invalid algo setting. Keep last selection");\
break;\
}\
}\
else\
{\
/*Save location pointer */\
if (len > 256) return;\
crcdata->private.tab = tab;\
switch (algo)\
{\
case CRC_TABLE:\
crcdata->private.crc_compute = (type(*)(config_##type*,crc_8*,crc_16))type##_table;\
crcdata->private.crc_update = (type(*)(config_##type*,crc_8*,crc_16))type##_table_upd;\
break;\
case CRC_TABLE_FAST:\
crcdata->private.crc_compute = (type(*)(config_##type*,crc_8*,crc_16))type##_table_fast;\
crcdata->private.crc_update = (type(*)(config_##type*,crc_8*,crc_16))type##_table_fast_upd;\
break;\
default:\
debug("ERROR, invalid algo setting. Keep last selection");\
break;\
}\
}\
}\
type type##_generic_compute(config_##type* crcdata, crc_8* p, crc_16 len)\
{\
return crcdata->private.crc_compute(crcdata, p,len);\
}\
type type##_generic_update(config_##type* crcdata, crc_8* p, crc_16 len)\
{\
return crcdata->private.crc_update(crcdata, p,len);\
}\
/*
* Create all functions for all size variable usage
*/
#if CRC_1BYTE_SUPPORT == 1
_CRC_SET_ALGO_SRC(crc_8)
#endif
#if CRC_2BYTE_SUPPORT == 1
_CRC_SET_ALGO_SRC(crc_16)
#endif
#if CRC_4BYTE_SUPPORT == 1
_CRC_SET_ALGO_SRC(crc_32)
#endif
#if CRC_8BYTE_SUPPORT == 1
_CRC_SET_ALGO_SRC(crc_64)
#endif