-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path__init__.py
More file actions
360 lines (310 loc) · 9.86 KB
/
Copy path__init__.py
File metadata and controls
360 lines (310 loc) · 9.86 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
__author__ = 'Diogo Alves'
class crc:
order = ""
polynom = ""
init = ""
xor = ""
reflect0 = False
reflect1 = False
direct = False
data = ""
result = ""
def __init__(self):
pass
def setCRC8(self):
"""
Sets the CRC Calculation tothe CRC8. The values are set as follow:
Width = 8 bits
Truncated Polynomial = 0x01
Initial Value = 0x0000
Data is reflected
Output is reflected
No XOR is performed on the output CRC
"""
self.reflect1 = True
self.direct = True
self.reflect0 = True
self.init = "0"
self.xor = "0"
self.order = "8"
self.polynom = "1"
def setCRCccitt(self):
"""
Sets the CRC Calculation tothe CRC16-CCITT. The values are set as follow:
Width = 16 bits
Truncated Polynomial = 0x1021
Initial Value = 0xFFFF
Data is not reflected
Output is not reflected
No XOR is performed on the output CRC
"""
self.order = "16"
self.polynom = "1021"
self.init = "ffff"
self.xor = ""
self.reflect0 = False
self.reflect1 = False
self.direct = True
def setCRC16(self):
"""
Sets the CRC Calculation to the CRC16. The values are set as follow:
Width = 16 bits
Truncated Polynomial = 0x8005
Initial Value = 0x0000
Data is reflected
Output is reflected
No XOR is performed on the output CRC
"""
self.order = "16"
self.polynom = "8005"
self.init = "0"
self.xor = "0"
self.reflect0 = True
self.reflect1 = True
self.direct = True
def setCRC32(self):
"""
Sets the CRC Calculation to the CRC32. The values are set as follow:
Width = 32 bits
Truncated Polynomial = 0x4c11db7
Initial Value = 0xffffffff
Data is reflected
Output is reflected
XOR is performed on the output CRC
"""
self.order = "32"
self.polynom = "4c11db7"
self.init = "ffffffff"
self.xor = "ffffffff"
self.reflect0 = True
self.reflect1 = True
self.direct = True
def compute(self):
"""
Computes the CRC with the selected values and store the result at self.result
"""
i = 0
j = 0
k = 0
bit = False
datalen = 0
lenght = 0
flag = False
counter = 0
c = 0
crc = ["", "", "", "", "", "", "", "", ""]
mask = ["", "", "", "", "", "", "", ""]
init = ["", "", "", "", "", "", "", ""]
hexnum = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"]
data = ""
order = ""
polynom = ["", "", "", "", "", "", "", ""]
xor = ["", "", "", "", "", "", "", ""]
# Check if parameters are present
if self.order == "" or self.polynom == "" or self.init == "" or self.xor == "":
raise Exception("Invalid Parameters")
# Convert CRC Order
order = int(self.order, 10)
if order < 1 or order > 64:
raise Exception("CRC order must be between 1 and 64")
#Convert CRC Polynom
polynom = self.convertentry(self.polynom, order)
if polynom[0] < 0:
raise Exception("Invalid CRC polynom")
if not(polynom[7] & 1):
raise Exception("CRC polynom LSB must be set")
init = self.convertentry(self.init, order)
if init[0] < 0:
raise Exception("Invalid initial value")
# Convert CRC XOR value
xor = self.convertentry(self.xor, order)
if xor[0] < 0:
raise Exception("Invalid XOR value")
# Generate bit mask
counter = order
for i in range(7, -1, -1):
if counter >= 8:
mask[i] = 255
else:
mask[i] = (1 << counter) - 1
counter -= 8
if counter < 0:
counter = 0
crc = init
if self.direct: # Non Direct -> Direct
crc.append(0)
for i in range(0, order):
bit = crc[7-((order-1) >> 3)] & (1 << ((order-1) & 7))
for k in range(0, 8):
crc[k] = ((crc[k] << 1) | (crc[k+1] >> 7)) & mask[k]
if bit:
crc[k] ^= polynom[k]
data = self.data
datalen = len(data)
lenght = 0 # number of data bytes
crc.append(0)
for i in range(0, datalen):
c = ord(data[i])
if data[i] == '%':
if i > datalen-3:
raise Exception("Invalid data Sequence")
try:
ch = int(data[++i], 16)
except ValueError:
raise Exception("Invalid data Sequence")
c = (c & 15) | ((ch & 15) << 4)
# perform revin
if self.reflect0:
c = self.reflectByte(c)
# rotate one data byte including crcmask
for j in range(0,8):
bit = 0
if crc[7-((order-1) >> 3)] & (1 << ((order-1) & 7)):
bit = 1
if c & 0x80:
bit ^= 1
c <<= 1
for k in range(0,8): # Rotate all (max 8) crc bytes
crc[k] = ((crc[k] << 1) | (crc[k+1] >> 7)) & mask[k]
if bit:
crc[k] ^= polynom[k]
lenght += 1
# perform revout
if self.reflect1:
crc = self.reflect(crc, order, 0)
# perform xor value
for i in range(0, 8):
crc[i] ^= xor[i]
# write results
self.result = ""
flag = 0
for i in range(0,8):
actchar = crc[i] >> 4
if flag or actchar:
self.result += hexnum[actchar]
flag=1
actchar = crc[i] & 15
if flag or actchar or i == 7:
self.result += hexnum[actchar]
flag = 1
def revpoly(self):
"""
Reverses the polynom
"""
# reverses poly
polynom = ["", "", "", "", "", "", "", "", ""]
order = 0
actchar = ""
flag = False
hexnum = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"]
self.result = ""
# convert crc order
try:
order = int(self.order, 10)
except ValueError:
raise Exception("CRC order must be between 1 and 64")
# convert crc polynom
polynom = self.convertentry(self.polynom, order)
if polynom[0] < 0:
raise Exception("Invalid CRC polynom")
# check if polynom is valid
if not (polynom[7] & 1):
raise Exception("CRC polynom LSB must be set")
# compute reversed polynom
polynom = self.reflect(polynom, order, 1)
# write result
self.polynom = ""
flag = 0
for i in range(0, 8):
actchar = polynom[i] >> 4
if flag or actchar:
self.polynom += hexnum[actchar]
flag = 1
actchar = polynom[i] & 15
if flag or actchar or i == 7:
self.polynom += hexnum[actchar]
flag = 1
def reflectByte(self, inbyte):
"""
Reflects a byte
:param inbyte: input byte
:return: reflected input byte
"""
outbyte = 0
i = 0x01
j = 0x80
while j != 0:
if inbyte & i:
outbyte |= j
i <<= 1
j>>=1
return outbyte
def reflect(self, crc, bitnum, startLSB):
"""
Reflect a number of bits starting a the lowest bit defined by startLSB
:param crc: the current crc hash
:param bitnum: the number of bits to reflect
:param startLSB: the index of the the LSB
:return: returns a crc with the reflected bits
"""
# reflect bitnum bits starting at lowest bit = startLSB
i = 0
j = 0
k = 0
iw = 0
jw = 0
bit = 0
while k+startLSB < bitnum-1-k:
iw = 7-((k+startLSB) >> 3)
jw = 1 << ((k+startLSB) & 7)
i = 7-((bitnum-1-k) >> 3)
j = 1 << ((bitnum-1-k) & 7)
bit = crc[iw] & jw
if crc[i] & j:
crc[iw] |= jw
else:
crc[iw] &= (0xff-jw)
if bit:
crc[i] |= j
else:
crc[i] &= (0xff-j)
k += 1
return crc
def convertentry(self, input, order):
"""
Converts from a ASCII value to another base value
:param input: string input value
:param order: base order
:return:
"""
# convert from ascii to hexadecimal value
lenght = 0
actchar = 0
polynom = [0, 0, 0, 0, 0, 0, 0, 0, 0]
brk = [-1, 0, 0, 0, 0, 0, 0, 0]
#convert crc value into byte sequence
input = list(input)
lenght = len(input)
for i in range(0, lenght):
try:
actchar = int(input[i], 16)
except ValueError:
return brk
actchar &=15
for j in range(0, 8):
polynom[j] = ((polynom[j] << 4) | (polynom[j+1] >> 4 )) & 255
polynom[7] = ((polynom[7] << 4) | actchar) & 255
# compute and check crc order
count = 64
for i in range(0, 8):
j = 0x80
while j > 0:
if polynom[i] & j:
break
count -= 1
j >>= 1
if polynom[i] & j:
break
if count > order:
return brk
return polynom