-
Notifications
You must be signed in to change notification settings - Fork 2
/
crc.py
executable file
·240 lines (199 loc) · 8.12 KB
/
crc.py
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
#!/usr/bin/env python3
# parameterized CRC implementation
# Copyright 2016 Eric Smith <[email protected]>
# supports operation on arbitrary data word widths
# supports table-driven operation with selectable table size(s)
# for common 8-bit use, after instantiation, call make_table(8)
# Algorithms defined per section 14 of "A Painless Guide to CRC
# Error Detection Algorithms" by Ross N. Williams:
# http://www.ross.net/crc/download/crc_v3.txt
# This program is free software: you can redistribute it and/or
# modify it under the terms of version 3 of the GNU General Public
# License as published by the Free Software Foundation.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see
# <http://www.gnu.org/licenses/>.
from collections import namedtuple
import struct
class CRC:
CRCParam = namedtuple('CRCParam',
['name',
'order',
'poly',
'init',
'xorot',
'refin', # reflect input data
'refot']) # reflect output word
crc16_ccitt_param = CRCParam(name = 'CRC-16-CCITT',
order = 16,
poly = 0x1021,
init = 0xffff,
xorot = 0x0000,
refin = False,
refot = False)
crc32_param = CRCParam(name = 'CRC-32',
order = 32,
poly = 0x04c11db7,
init = 0xffffffff,
xorot = 0xffffffff,
refin = True,
refot = True)
crc32_bzip2_param = CRCParam(name = 'CRC-32/BZIP2',
order = 32,
poly = 0x04c11db7,
init = 0xffffffff,
xorot = 0xffffffff,
refin = False,
refot = False)
# Catagnoli polynomial
crc32c_param = CRCParam(name = 'CRC-32C',
order = 32,
poly = 0x1edc6f41,
init = 0xffffffff,
xorot = 0xffffffff,
refin = True,
refot = True)
def __init__(self,
param):
self.tables = { }
self.cache = { }
self.param = param
self.reg = param.init
self.widmask = (1 << self.param.order) - 1
self.topbit = 1 << (self.param.order - 1)
def reset(self):
self.reg = self.param.init
def reflect(self, data, bit_count):
d1 = data
d2 = 0
for b in range(bit_count):
d2 <<= 1
if d1 & 1:
d2 |= 1
d1 >>= 1
#print("%02x %02x" % (data, d2))
return d2
# this one works only for bit_count <= self.param.order
def comp1(self, data, bit_count = 8):
if self.param.refin:
data = self.reflect(data, bit_count)
self.reg ^= data << (self.param.order - bit_count)
for b in range(bit_count):
if self.reg & self.topbit:
self.reg = (self.reg << 1) ^ self.param.poly
else:
self.reg <<= 1
self.reg &= self.widmask
# this one doesn't restrict bit_count
def comp2(self, data, bit_count = 8):
if self.param.refin:
r = range(bit_count)
else:
r = range(bit_count - 1, -1, -1)
for b in r:
self.reg ^= ((data >> b) & 1) << (self.param.order - 1)
if self.reg & self.topbit:
self.reg = (self.reg << 1) ^ self.param.poly
else:
self.reg <<= 1
self.reg &= self.widmask
def find_table(self, bit_count):
self.cache[bit_count] = 0 # assume no suitable table
for i in range(bit_count, 1, -1):
if i in self.tables:
self.cache[bit_count] = i
return
def comp_int(self, data, bit_count = 8):
if self.param.refin:
data = self.reflect(data, bit_count)
while bit_count > 0:
if bit_count not in self.cache:
self.find_table(bit_count)
table_size = self.cache[bit_count]
if table_size:
b = data >> (bit_count - table_size) & ((1 << table_size) - 1)
self.reg = self.tables[table_size][(self.reg >> (self.param.order - table_size)) ^ b] ^ (self.reg << table_size)
self.reg &= self.widmask
bit_count -= table_size
else:
b = (data >> bit_count - 1) & 1
self.reg ^= b << (self.param.order - 1)
if self.reg & self.topbit:
self.reg = (self.reg << 1) ^ self.param.poly
else:
self.reg <<= 1
self.reg &= self.widmask
bit_count -= 1
def comp(self, data, bit_count = 8):
try:
for b in data:
self.comp_int(b, bit_count)
except TypeError:
self.comp_int(data, bit_count)
def make_table_entry(self, d, bit_count):
v = 0
for b in range(bit_count - 1, -1, -1):
b = (d >> bit_count - 1) & 1
v ^= b << (self.param.order - 1)
if v & self.topbit:
v = (v << 1) ^ self.param.poly
else:
v <<= 1
v &= self.widmask
bit_count -= 1
return v
def make_table(self, bit_count = 8):
if bit_count in self.tables:
return
assert bit_count > 1
self.cache = { }
self.tables[bit_count] = [self.make_table_entry(i, bit_count) for i in range(1 << bit_count)]
#for i in range(len(self.tables[bit_count])):
# print("%02x: %08x" % (i, self.tables[bit_count][i]))
def get(self):
if self.param.refot:
return self.reflect(self.reg ^ self.param.xorot, self.param.order)
else:
return self.reg ^ self.param.xorot
def crc(self, data):
self.reset()
self.comp(data)
return self.get()
if __name__ == '__main__':
pass_count = 0
fail_count = 0
def test(param, data, expected_value, use_table = True):
global pass_count, fail_count
crc = CRC(param)
if use_table:
crc.make_table(5)
crc.make_table(3)
for b in data:
crc.comp(b)
v = crc.get()
if v == expected_value:
print('%s OK' % param.name)
pass_count += 1
else:
print('%s crc result %08x, expected %08x' % (param.name, v, expected_value))
fail_count += 1
def swap32(i):
return struct.unpack("<I", struct.pack(">I", i))[0]
# http://reveng.sourceforge.net/crc-catalogue/17plus.htm
# http://stackoverflow.com/questions/1918090/crc-test-vectors-for-crc16-ccitt
vector = [ord(c) for c in '123456789']
test(CRC.crc16_ccitt_param, vector, 0x29b1)
test(CRC.crc32_param, vector, 0xcbf43926)
test(CRC.crc32_bzip2_param, vector, 0xfc891918)
test(CRC.crc32c_param, vector, 0xe3069283)
# Test vectors for CRC-32C from RFC3270:
# https://tools.ietf.org/html/rfc3720#appendix-B.4
test(CRC.crc32c_param, [0x00] * 32, swap32(0xaa36918a))
test(CRC.crc32c_param, [0xff] * 32, swap32(0x43aba862))
test(CRC.crc32c_param, range(32), swap32(0x4e79dd46))
test(CRC.crc32c_param, range(31, -1, -1), swap32(0x5cdb3f11))
print("%d passed, %d failed" % (pass_count, fail_count))