crcmod.py
16.2 KB
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
#-----------------------------------------------------------------------------
# Copyright (c) 2010 Raymond L. Buvel
# Copyright (c) 2010 Craig McQueen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#-----------------------------------------------------------------------------
'''crcmod is a Python module for gererating objects that compute the Cyclic
Redundancy Check. Any 8, 16, 24, 32, or 64 bit polynomial can be used.
The following are the public components of this module.
Crc -- a class that creates instances providing the same interface as the
algorithms in the hashlib module in the Python standard library. These
instances also provide a method for generating a C/C++ function to compute
the CRC.
mkCrcFun -- create a Python function to compute the CRC using the specified
polynomial and initial value. This provides a much simpler interface if
all you need is a function for CRC calculation.
'''
__all__ = '''mkCrcFun Crc
'''.split()
# Select the appropriate set of low-level CRC functions for this installation.
# If the extension module was not built, drop back to the Python implementation
# even though it is significantly slower.
try:
import crcmod._crcfunext as _crcfun
_usingExtension = True
except ImportError:
import crcmod._crcfunpy as _crcfun
_usingExtension = False
import sys, struct
#-----------------------------------------------------------------------------
class Crc:
'''Compute a Cyclic Redundancy Check (CRC) using the specified polynomial.
Instances of this class have the same interface as the algorithms in the
hashlib module in the Python standard library. See the documentation of
this module for examples of how to use a Crc instance.
The string representation of a Crc instance identifies the polynomial,
initial value, XOR out value, and the current CRC value. The print
statement can be used to output this information.
If you need to generate a C/C++ function for use in another application,
use the generateCode method. If you need to generate code for another
language, subclass Crc and override the generateCode method.
The following are the parameters supplied to the constructor.
poly -- The generator polynomial to use in calculating the CRC. The value
is specified as a Python integer. The bits in this integer are the
coefficients of the polynomial. The only polynomials allowed are those
that generate 8, 16, 24, 32, or 64 bit CRCs.
initCrc -- Initial value used to start the CRC calculation. This initial
value should be the initial shift register value XORed with the final XOR
value. That is equivalent to the CRC result the algorithm should return for
a zero-length string. Defaults to all bits set because that starting value
will take leading zero bytes into account. Starting with zero will ignore
all leading zero bytes.
rev -- A flag that selects a bit reversed algorithm when True. Defaults to
True because the bit reversed algorithms are more efficient.
xorOut -- Final value to XOR with the calculated CRC value. Used by some
CRC algorithms. Defaults to zero.
'''
def __init__(self, poly, initCrc=~0, rev=True, xorOut=0, initialize=True):
if not initialize:
# Don't want to perform the initialization when using new or copy
# to create a new instance.
return
(sizeBits, initCrc, xorOut) = _verifyParams(poly, initCrc, xorOut)
self.digest_size = sizeBits//8
self.initCrc = initCrc
self.xorOut = xorOut
self.poly = poly
self.reverse = rev
(crcfun, table) = _mkCrcFun(poly, sizeBits, initCrc, rev, xorOut)
self._crc = crcfun
self.table = table
self.crcValue = self.initCrc
def __str__(self):
lst = []
lst.append('poly = 0x%X' % self.poly)
lst.append('reverse = %s' % self.reverse)
fmt = '0x%%0%dX' % (self.digest_size*2)
lst.append('initCrc = %s' % (fmt % self.initCrc))
lst.append('xorOut = %s' % (fmt % self.xorOut))
lst.append('crcValue = %s' % (fmt % self.crcValue))
return '\n'.join(lst)
def new(self, arg=None):
'''Create a new instance of the Crc class initialized to the same
values as the original instance. The current CRC is set to the initial
value. If a string is provided in the optional arg parameter, it is
passed to the update method.
'''
n = Crc(poly=None, initialize=False)
n._crc = self._crc
n.digest_size = self.digest_size
n.initCrc = self.initCrc
n.xorOut = self.xorOut
n.table = self.table
n.crcValue = self.initCrc
n.reverse = self.reverse
n.poly = self.poly
if arg is not None:
n.update(arg)
return n
def copy(self):
'''Create a new instance of the Crc class initialized to the same
values as the original instance. The current CRC is set to the current
value. This allows multiple CRC calculations using a common initial
string.
'''
c = self.new()
c.crcValue = self.crcValue
return c
def update(self, data):
'''Update the current CRC value using the string specified as the data
parameter.
'''
self.crcValue = self._crc(data, self.crcValue)
def digest(self):
'''Return the current CRC value as a string of bytes. The length of
this string is specified in the digest_size attribute.
'''
n = self.digest_size
crc = self.crcValue
lst = []
while n > 0:
lst.append(crc & 0xFF)
crc = crc >> 8
n -= 1
lst.reverse()
return bytes(lst)
def hexdigest(self):
'''Return the current CRC value as a string of hex digits. The length
of this string is twice the digest_size attribute.
'''
n = self.digest_size
crc = self.crcValue
lst = []
while n > 0:
lst.append('%02X' % (crc & 0xFF))
crc = crc >> 8
n -= 1
lst.reverse()
return ''.join(lst)
def generateCode(self, functionName, out, dataType=None, crcType=None):
'''Generate a C/C++ function.
functionName -- String specifying the name of the function.
out -- An open file-like object with a write method. This specifies
where the generated code is written.
dataType -- An optional parameter specifying the data type of the input
data to the function. Defaults to UINT8.
crcType -- An optional parameter specifying the data type of the CRC
value. Defaults to one of UINT8, UINT16, UINT32, or UINT64 depending
on the size of the CRC value.
'''
if dataType is None:
dataType = 'UINT8'
if crcType is None:
size = 8*self.digest_size
if size == 24:
size = 32
crcType = 'UINT%d' % size
if self.digest_size == 1:
# Both 8-bit CRC algorithms are the same
crcAlgor = 'table[*data ^ (%s)crc]'
elif self.reverse:
# The bit reverse algorithms are all the same except for the data
# type of the crc variable which is specified elsewhere.
crcAlgor = 'table[*data ^ (%s)crc] ^ (crc >> 8)'
else:
# The forward CRC algorithms larger than 8 bits have an extra shift
# operation to get the high byte.
shift = 8*(self.digest_size - 1)
crcAlgor = 'table[*data ^ (%%s)(crc >> %d)] ^ (crc << 8)' % shift
fmt = '0x%%0%dX' % (2*self.digest_size)
if self.digest_size <= 4:
fmt = fmt + 'U,'
else:
# Need the long long type identifier to keep gcc from complaining.
fmt = fmt + 'ULL,'
# Select the number of entries per row in the output code.
n = {1:8, 2:8, 3:4, 4:4, 8:2}[self.digest_size]
lst = []
for i, val in enumerate(self.table):
if (i % n) == 0:
lst.append('\n ')
lst.append(fmt % val)
poly = 'polynomial: 0x%X' % self.poly
if self.reverse:
poly = poly + ', bit reverse algorithm'
if self.xorOut:
# Need to remove the comma from the format.
preCondition = '\n crc = crc ^ %s;' % (fmt[:-1] % self.xorOut)
postCondition = preCondition
else:
preCondition = ''
postCondition = ''
if self.digest_size == 3:
# The 24-bit CRC needs to be conditioned so that only 24-bits are
# used from the 32-bit variable.
if self.reverse:
preCondition += '\n crc = crc & 0xFFFFFFU;'
else:
postCondition += '\n crc = crc & 0xFFFFFFU;'
parms = {
'dataType' : dataType,
'crcType' : crcType,
'name' : functionName,
'crcAlgor' : crcAlgor % dataType,
'crcTable' : ''.join(lst),
'poly' : poly,
'preCondition' : preCondition,
'postCondition' : postCondition,
}
out.write(_codeTemplate % parms)
#-----------------------------------------------------------------------------
def mkCrcFun(poly, initCrc=~0, rev=True, xorOut=0):
'''Return a function that computes the CRC using the specified polynomial.
poly -- integer representation of the generator polynomial
initCrc -- default initial CRC value
rev -- when true, indicates that the data is processed bit reversed.
xorOut -- the final XOR value
The returned function has the following user interface
def crcfun(data, crc=initCrc):
'''
# First we must verify the params
(sizeBits, initCrc, xorOut) = _verifyParams(poly, initCrc, xorOut)
# Make the function (and table), return the function
return _mkCrcFun(poly, sizeBits, initCrc, rev, xorOut)[0]
#-----------------------------------------------------------------------------
# Naming convention:
# All function names ending with r are bit reverse variants of the ones
# without the r.
#-----------------------------------------------------------------------------
# Check the polynomial to make sure that it is acceptable and return the number
# of bits in the CRC.
def _verifyPoly(poly):
msg = 'The degree of the polynomial must be 8, 16, 24, 32 or 64'
for n in (8,16,24,32,64):
low = 1<<n
high = low*2
if low <= poly < high:
return n
raise ValueError(msg)
#-----------------------------------------------------------------------------
# Bit reverse the input value.
def _bitrev(x, n):
y = 0
for i in range(n):
y = (y << 1) | (x & 1)
x = x >> 1
return y
#-----------------------------------------------------------------------------
# The following functions compute the CRC for a single byte. These are used
# to build up the tables needed in the CRC algorithm. Assumes the high order
# bit of the polynomial has been stripped off.
def _bytecrc(crc, poly, n):
mask = 1<<(n-1)
for i in range(8):
if crc & mask:
crc = (crc << 1) ^ poly
else:
crc = crc << 1
mask = (1<<n) - 1
crc = crc & mask
return crc
def _bytecrc_r(crc, poly, n):
for i in range(8):
if crc & 1:
crc = (crc >> 1) ^ poly
else:
crc = crc >> 1
mask = (1<<n) - 1
crc = crc & mask
return crc
#-----------------------------------------------------------------------------
# The following functions compute the table needed to compute the CRC. The
# table is returned as a list. Note that the array module does not support
# 64-bit integers on a 32-bit architecture as of Python 2.3.
#
# These routines assume that the polynomial and the number of bits in the CRC
# have been checked for validity by the caller.
def _mkTable(poly, n):
mask = (1<<n) - 1
poly = poly & mask
table = [_bytecrc(i<<(n-8),poly,n) for i in range(256)]
return table
def _mkTable_r(poly, n):
mask = (1<<n) - 1
poly = _bitrev(poly & mask, n)
table = [_bytecrc_r(i,poly,n) for i in range(256)]
return table
#-----------------------------------------------------------------------------
# Map the CRC size onto the functions that handle these sizes.
_sizeMap = {
8 : [_crcfun._crc8, _crcfun._crc8r],
16 : [_crcfun._crc16, _crcfun._crc16r],
24 : [_crcfun._crc24, _crcfun._crc24r],
32 : [_crcfun._crc32, _crcfun._crc32r],
64 : [_crcfun._crc64, _crcfun._crc64r],
}
#-----------------------------------------------------------------------------
# Build a mapping of size to struct module type code. This table is
# constructed dynamically so that it has the best chance of picking the best
# code to use for the platform we are running on. This should properly adapt
# to 32 and 64 bit machines.
_sizeToTypeCode = {}
for typeCode in 'B H I L Q'.split():
size = {1:8, 2:16, 4:32, 8:64}.get(struct.calcsize(typeCode),None)
if size is not None and size not in _sizeToTypeCode:
_sizeToTypeCode[size] = '256%s' % typeCode
_sizeToTypeCode[24] = _sizeToTypeCode[32]
del typeCode, size
#-----------------------------------------------------------------------------
# The following function validates the parameters of the CRC, namely,
# poly, and initial/final XOR values.
# It returns the size of the CRC (in bits), and "sanitized" initial/final XOR values.
def _verifyParams(poly, initCrc, xorOut):
sizeBits = _verifyPoly(poly)
mask = (1<<sizeBits) - 1
# Adjust the initial CRC to the correct data type (unsigned value).
initCrc = initCrc & mask
# Similar for XOR-out value.
xorOut = xorOut & mask
return (sizeBits, initCrc, xorOut)
#-----------------------------------------------------------------------------
# The following function returns a Python function to compute the CRC.
#
# It must be passed parameters that are already verified & sanitized by
# _verifyParams().
#
# The returned function calls a low level function that is written in C if the
# extension module could be loaded. Otherwise, a Python implementation is
# used.
#
# In addition to this function, a list containing the CRC table is returned.
def _mkCrcFun(poly, sizeBits, initCrc, rev, xorOut):
if rev:
tableList = _mkTable_r(poly, sizeBits)
_fun = _sizeMap[sizeBits][1]
else:
tableList = _mkTable(poly, sizeBits)
_fun = _sizeMap[sizeBits][0]
_table = tableList
if _usingExtension:
_table = struct.pack(_sizeToTypeCode[sizeBits], *tableList)
if xorOut == 0:
def crcfun(data, crc=initCrc, table=_table, fun=_fun):
return fun(data, crc, table)
else:
def crcfun(data, crc=initCrc, table=_table, fun=_fun):
return xorOut ^ fun(data, xorOut ^ crc, table)
return crcfun, tableList
#-----------------------------------------------------------------------------
_codeTemplate = '''// Automatically generated CRC function
// %(poly)s
%(crcType)s
%(name)s(%(dataType)s *data, int len, %(crcType)s crc)
{
static const %(crcType)s table[256] = {%(crcTable)s
};
%(preCondition)s
while (len > 0)
{
crc = %(crcAlgor)s;
data++;
len--;
}%(postCondition)s
return crc;
}
'''