-
Notifications
You must be signed in to change notification settings - Fork 0
/
crc_calc.py
executable file
·301 lines (233 loc) · 10.4 KB
/
crc_calc.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
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
#!/usr/bin/python
"""Generic CRC calculate implementation.
Copyright (c) 2023-present SKB([email protected])
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import array
class CRC_CALC(object):
"""Generic CRC model implemented with lookup tables.
The model parameter can are the constructor parameters.
Args:
width (int): Number of bits of the polynomial
polynomial (int): CRC polynomial
initial_value (int): Initial value of the checksum
final_xor_value (int): Value that will be XOR-ed with final checksum
input_reflected (bool): True, if each input byte should be reflected
result_reflected (bool): True, if the result should be reflected before the final XOR is applied
"""
def __init__(self, width, polynomial, initial_value, final_xor_value, input_reflected, result_reflected):
assert 8 <= width <= 64
assert width % 8 == 0
self._width = width
self._polynomial = polynomial
self._initial_value = initial_value
self._final_xor_value = final_xor_value
self._input_reflected = input_reflected
self._result_reflected = result_reflected
self.__accumulate = initial_value
# Initialize casting mask to keep the correct width for dynamic Python
# integers
self._cast_mask = int('1' * self._width, base=2)
# Mask that can be applied to get the Most Significant Bit (MSB) if the
# number with given width
self._msb_mask = 0x01 << (self._width - 1)
# The lookup tables get initialized lazzily. This ensures that only
# tables are calculated that are actually needed.
self.__table = None
self.__reflected_table = None
def __reflect(self, num, width):
"""Reverts bit order of the given number
Args:
num (int): Number that should be reflected
width (int): Size of the number in bits
"""
reflected = 0
for i in range(width):
if (num >> i) & 1 != 0:
reflected |= 1 << (width - 1 - i)
return reflected
def __make_table(self, width):
"""Create static sized CRC lookup table and initialize it with ``0``.
For 8, 16, 32, and 64 bit width :class:`array.array` instances are used. For
all other widths it falls back to a generic Python :class:`list`.
Args:
width (int): Size of elements in bits
"""
initializer = (0 for _ in range(256))
if width <= 8:
return array.array('B', initializer)
elif width <= 16:
return array.array('H', initializer)
elif width <= 32:
return array.array('L', initializer)
elif width <= 64:
return array.array('Q', initializer)
else:
# Fallback to a generic list
return list(initializer)
def __calculate_crc_table(self):
table = self.__make_table(self._width)
for divident in range(256):
cur_byte = (divident << (self._width - 8)) & self._cast_mask
for bit in range(8):
if (cur_byte & self._msb_mask) != 0:
cur_byte <<= 1
cur_byte ^= self._polynomial
else:
cur_byte <<= 1
table[divident] = cur_byte & self._cast_mask
return table
def __calculate_crc_table_reflected(self):
table = self.__make_table(self._width)
for divident in range(256):
reflected_divident = self.__reflect(divident, 8)
cur_byte = (reflected_divident << (
self._width - 8)) & self._cast_mask
for bit in range(8):
if (cur_byte & self._msb_mask) != 0:
cur_byte <<= 1
cur_byte ^= self._polynomial
else:
cur_byte <<= 1
cur_byte = self.__reflect(cur_byte, self._width)
table[divident] = (cur_byte & self._cast_mask)
return table
def __repr__(self):
if self._input_reflected and self._result_reflected:
if self.__reflected_table is None:
self.__reflected_table = self.__calculate_crc_table_reflected()
table = self.__reflected_table
else:
if self.__table is None:
self.__table = self.__calculate_crc_table()
table = self.__table
table = [table[i:i+8] for i in range(0, len(table), 8)]
table_str = [', '.join(
["0x{:0{}X}".format(i, self._width // 4) for i in t]
) for t in table]
return ',\n'.join(table_str) + ','
def __fast_reflected(self, value):
"""If the input data and the result checksum are both reflected in the
current model, an optimized algorithm can be used that reflects the
looup table rather then the input data. This saves the reflection
operation of the input data.
"""
if not self._input_reflected or not self._result_reflected:
raise ValueError("Input and result must be reflected")
# Lazy initialization of the lookup table
if self.__reflected_table is None:
self.__reflected_table = self.__calculate_crc_table_reflected()
crc = self._initial_value
for cur_byte in value:
# The LSB of the XOR-red remainder and the next byte is the index
# into the lookup table
index = (crc & 0xff) ^ cur_byte
# Shift out the index
crc = (crc >> 8) & self._cast_mask
# XOR-ing remainder from the loopup table
crc = crc ^ self.__reflected_table[index]
# Final XBOR
return crc ^ self._final_xor_value
def __call__(self, value):
"""Compute the CRC checksum with respect to the model parameters by using
a looup table algorithm.
Args:
value (bytes): Input bytes that should be checked
Returns:
int - CRC checksum
"""
# Use the reflection optimization if applicable
if self._input_reflected and self._result_reflected:
return self.__fast_reflected(value)
# Lazy initialization of the lookup table
if self.__table is None:
self.__table = self.__calculate_crc_table()
crc = self._initial_value
for cur_byte in value:
if self._input_reflected:
cur_byte = self.__reflect(cur_byte, 8)
# Update the MSB of the CRC value with the next input byte
crc = (crc ^ (cur_byte << (self._width - 8))) & self._cast_mask
# This MSB byte value is the index into the lookup table
index = (crc >> (self._width - 8)) & 0xff
# Shift out the index
crc = (crc << 8) & self._cast_mask
# XOR-ing crc from the lookup table using the calculated index
crc = crc ^ self.__table[index]
if self._result_reflected:
crc = self.__reflect(crc, self._width)
# Final XBOR
return crc ^ self._final_xor_value
def __fast_reflected_acc(self, value):
if not self._input_reflected or not self._result_reflected:
raise ValueError("Input and result must be reflected")
# Lazy initialization of the lookup table
if self.__reflected_table is None:
self.__reflected_table = self.__calculate_crc_table_reflected()
for cur_byte in value:
# The LSB of the XOR-red remainder and the next byte is the index
# into the lookup table
index = (self.__accumulate & 0xff) ^ cur_byte
# Shift out the index
self.__accumulate = (self.__accumulate >> 8) & self._cast_mask
# XOR-ing remainder from the loopup table
self.__accumulate = self.__accumulate ^ self.__reflected_table[index]
# Final XBOR
return self.__accumulate ^ self._final_xor_value
def accumulate(self, value):
# Use the reflection optimization if applicable
if self._input_reflected and self._result_reflected:
return self.__fast_reflected_acc(value)
# Lazy initialization of the lookup table
if self.__table is None:
self.__table = self.__calculate_crc_table()
for cur_byte in value:
if self._input_reflected:
cur_byte = self.__reflect(cur_byte, 8)
# Update the MSB of the CRC value with the next input byte
self.__accumulate = (self.__accumulate ^ (
cur_byte << (self._width - 8))) & self._cast_mask
# This MSB byte value is the index into the lookup table
index = (self.__accumulate >> (self._width - 8)) & 0xff
# Shift out the index
self.__accumulate = (self.__accumulate << 8) & self._cast_mask
# XOR-ing crc from the lookup table using the calculated index
self.__accumulate = self.__accumulate ^ self.__table[index]
if self._result_reflected:
self.__accumulate = self.__reflect(self.__accumulate, self._width)
# Final XBOR
return self.__accumulate ^ self._final_xor_value
def reset(self):
self.__accumulate = self._initial_value
def get(self):
crc = self.__accumulate
# self.reset()
return crc ^ self._final_xor_value
if __name__ == '__main__':
# 自定义 CRC 参数模型
crc32 = CRC_CALC(32, 0x04c11db7, 0xffffffff, 0xffffffff, True, True)
data1 = b'hello '
data2 = b'world'
data3 = b'!!!'
# 单次校验
val_a1 = crc32(data1)
val_a2 = crc32(data2)
val_a3 = crc32(data3)
crc1 = crc32(data1 + data2 + data3)
# 分次校验
crc32.reset()
val_b1 = crc32.accumulate(data1)
val_b2 = crc32.accumulate(data2)
val_b3 = crc32.accumulate(data3)
crc2 = crc32.get()
assert crc1 == crc2
print(hex(val_a1), hex(val_a2), hex(val_a3), hex(crc1))
print(hex(val_b1), hex(val_b2), hex(val_b3), hex(crc2))