-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCRC.py
63 lines (51 loc) · 1.7 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
class CRC:
static_divisor = '1011101000010111'
# Metoda pełniąca rolę logicznego XOR
@staticmethod
def xor(arg1, arg2):
# tablica na rezultat
result = []
for i in range(1, len(arg2)):
if arg1[i] == arg2[i]:
result.append('0')
else:
result.append('1')
# Zwraca nic dołączając tablice result
return ''.join(result)
@staticmethod
def mod2division(divident, divisor):
# Liczba bitów, które podlegają XOR (długość dzielnika)
pick = len(divisor)
tmp = divident[0:pick]
while pick < len(divident):
if tmp[0] == '1':
tmp = CRC.xor(divisor, tmp) + divident[pick]
else:
tmp = CRC.xor('0'*pick, tmp) + divident[pick]
pick += 1
if tmp[0] == '1':
tmp = CRC.xor(divisor, tmp)
else:
tmp = CRC.xor('0'*pick, tmp)
checkword = tmp
return checkword
@staticmethod
def encode_data(data):
l_key = len(CRC.static_divisor )
appended_data = data + '0'*(l_key-1)
remainder = CRC.mod2division(appended_data, CRC.static_divisor )
coded_word = data + remainder
return coded_word
@staticmethod
def decode_data(data):
l_key = len(CRC.static_divisor )
appended_data = data + '0'*(l_key-1)
remainder = CRC.mod2division(appended_data, CRC.static_divisor )
return remainder
@staticmethod
def check_CRC(binary):
result = CRC.decode_data(binary)
for i in range(len(result)):
if result[i] == '1':
return False
return True