DALGLI0/crc16.py

85 lines
2.6 KiB
Python
Raw Normal View History

2018-06-27 19:38:32 +02:00
from poly import Polynomial
2018-06-20 17:28:24 +02:00
import sys
import ast
2018-06-27 19:38:32 +02:00
Polynomial.n = 2
2018-06-20 17:28:24 +02:00
def normalize_byte(data):
while len(data) != 8:
if len(data) < 8:
data = str(0) + data
else:
2018-06-25 17:21:17 +02:00
data = data[1:]
2018-06-20 17:28:24 +02:00
return data
def encode(in_data):
data = in_data
data = list(data)
data_binary = [bin(ord(char)).replace('b', '') for char in data]
data_binary = [normalize_byte(byte) for byte in data_binary]
data_binary = [int(bit) for bit in list(''.join(data_binary))]
data_binary.reverse()
M = [0] * 16 + data_binary
L = [0] * (len(data_binary)) + [1] * 16
G = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1]
2018-06-25 19:54:51 +02:00
result_poly = Polynomial.divide(Polynomial.add(Polynomial(M), Polynomial(L)), Polynomial(G))
result = result_poly.coefficients + [0] * (16 - len(result_poly.coefficients))
2018-06-20 17:28:24 +02:00
2018-06-25 19:54:51 +02:00
result.reverse()
2018-06-20 17:28:24 +02:00
data_binary.reverse()
2018-06-25 19:54:51 +02:00
data_binary = data_binary + result
2018-06-20 17:28:24 +02:00
data_binary = [data_binary[i:i+8] for i in range(0, len(data_binary), 8)]
2018-06-25 19:54:51 +02:00
data_binary = [int(''.join(map(str, byte)), 2) for byte in data_binary]
2018-06-20 17:28:24 +02:00
2018-06-25 19:54:51 +02:00
fcs = [hex(byte) for byte in data_binary]
fcs = fcs[-2:]
2018-06-20 17:28:24 +02:00
2018-06-25 19:54:51 +02:00
data = [chr(byte) for byte in data_binary]
data = ''.join(data[0:-2])
2018-06-20 17:28:24 +02:00
2018-06-25 19:54:51 +02:00
return data, fcs
def check_fcs(in_data, fcs):
2018-06-20 17:28:24 +02:00
data = in_data
data = list(data)
data_binary = [bin(ord(char)).replace('b', '') for char in data]
data_binary = [normalize_byte(byte) for byte in data_binary]
data_binary = [int(bit) for bit in list(''.join(data_binary))]
2018-06-25 19:54:51 +02:00
fcs_binary = [bin(int(in_hex, 16)).replace('b', '') for in_hex in fcs]
fcs_binary = [normalize_byte(byte) for byte in fcs_binary]
fcs_binary = [int(bit) for bit in list(''.join(fcs_binary))]
data_binary += fcs_binary
2018-06-20 17:28:24 +02:00
data_binary.reverse()
C = [0] * 16 + data_binary
L = [0] * len(data_binary) + [1] * 16
G = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1]
S = Polynomial.divide(Polynomial.add(Polynomial(C), Polynomial(L)), Polynomial(G))
if S.coefficients == []:
return True
return False
def main():
try:
if sys.argv[1] == '-e' or sys.argv[1] == '-encode':
2018-06-25 19:54:51 +02:00
arg, fcs = encode(sys.argv[2])
print(arg, fcs)
print(check_fcs(arg, fcs)) # powinno zawsze zwracać true
2018-06-20 17:28:24 +02:00
elif sys.argv[1] == '-c' or sys.argv[1] == '-check':
2018-06-25 19:54:51 +02:00
arg = sys.argv[2]
fcs = ast.literal_eval(sys.argv[3])
print(check_fcs(arg, fcs))
2018-06-20 17:28:24 +02:00
else:
raise IndexError
except IndexError:
2018-06-25 19:54:51 +02:00
print("To encode: python3 CRC16.py -e [argument]\nTo check result: python3 CRC16.py -c [argument] [fcs]")
2018-06-20 17:28:24 +02:00
if __name__ == "__main__":
2018-06-25 17:21:17 +02:00
main()