-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrc.py
126 lines (107 loc) · 3.74 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
import struct
class Crc(object):
"""
A base class for CRC routines.
"""
# pylint: disable=too-many-instance-attributes
def __init__(self, width, poly, reflect_in, xor_in, reflect_out, xor_out, table_idx_width=None, slice_by=1):
"""The Crc constructor.
The parameters are as follows:
width
poly
reflect_in
xor_in
reflect_out
xor_out
"""
# pylint: disable=too-many-arguments
self.width = width
self.poly = poly
self.reflect_in = reflect_in
self.xor_in = xor_in
self.reflect_out = reflect_out
self.xor_out = xor_out
self.tbl_idx_width = table_idx_width
self.slice_by = slice_by
self.msb_mask = 0x1 << (self.width - 1)
self.mask = ((self.msb_mask - 1) << 1) | 1
if self.tbl_idx_width != None:
self.tbl_width = 1 << self.tbl_idx_width
else:
self.tbl_idx_width = 8
self.tbl_width = 1 << self.tbl_idx_width
self.direct_init = self.xor_in
self.nondirect_init = self.__get_nondirect_init(self.xor_in)
if self.width < 8:
self.crc_shift = 8 - self.width
else:
self.crc_shift = 0
def __get_nondirect_init(self, init):
"""
return the non-direct init if the direct algorithm has been selected.
"""
crc = init
for dummy_i in range(self.width):
bit = crc & 0x01
if bit:
crc ^= self.poly
crc >>= 1
if bit:
crc |= self.msb_mask
return crc & self.mask
def reflect(self, data, width):
"""
reflect a data word, i.e. reverts the bit order.
"""
# pylint: disable=no-self-use
res = data & 0x01
for dummy_i in range(width - 1):
data >>= 1
res = (res << 1) | (data & 0x01)
return res
def bit_by_bit(self, in_data):
"""
Classic simple and slow CRC implementation. This function iterates bit
by bit over the augmented input message and returns the calculated CRC
value at the end.
"""
reg = self.nondirect_init
for octet in in_data:
octet = struct.unpack("B", octet)[0]
if self.reflect_in:
octet = self.reflect(octet, 8)
for i in range(8):
topbit = reg & self.msb_mask
reg = ((reg << 1) & self.mask) | ((octet >> (7 - i)) & 0x01)
if topbit:
reg ^= self.poly
for i in range(self.width):
topbit = reg & self.msb_mask
reg = ((reg << 1) & self.mask)
if topbit:
reg ^= self.poly
if self.reflect_out:
reg = self.reflect(reg, self.width)
return (reg ^ self.xor_out) & self.mask
def bit_by_bit_fast(self, in_data):
"""
This is a slightly modified version of the bit-by-bit algorithm: it
does not need to loop over the augmented bits, i.e. the Width 0-bits
wich are appended to the input message in the bit-by-bit algorithm.
"""
reg = self.direct_init
for octet in in_data:
octet = struct.unpack("B", octet)[0]
if self.reflect_in:
octet = self.reflect(octet, 8)
for i in range(8):
topbit = reg & self.msb_mask
if octet & (0x80 >> i):
topbit ^= self.msb_mask
reg <<= 1
if topbit:
reg ^= self.poly
reg &= self.mask
if self.reflect_out:
reg = self.reflect(reg, self.width)
return reg ^ self.xor_out