From 50998fcfb916ae251309bd4b464f2c122e8cb30d Mon Sep 17 00:00:00 2001 From: jaseg Date: Fri, 9 Apr 2021 18:38:02 +0200 Subject: Repo re-org --- hardware/fw/crctest.py | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 hardware/fw/crctest.py (limited to 'hardware/fw/crctest.py') diff --git a/hardware/fw/crctest.py b/hardware/fw/crctest.py new file mode 100644 index 0000000..5c97be9 --- /dev/null +++ b/hardware/fw/crctest.py @@ -0,0 +1,79 @@ +custom_crc_table = {} + +def generate_crc32_table(_poly): + + global custom_crc_table + + for i in range(256): + c = i << 24 + + for j in range(8): + c = (c << 1) ^ _poly if (c & 0x80000000) else c << 1 + + custom_crc_table[i] = c & 0xffffffff + + +def crc32_stm(bytes_arr): + + length = len(bytes_arr) + crc = 0xffffffff + + k = 0 + while length >= 4: + + v = ((bytes_arr[k] << 24) & 0xFF000000) | ((bytes_arr[k+1] << 16) & 0xFF0000) | \ + ((bytes_arr[k+2] << 8) & 0xFF00) | (bytes_arr[k+3] & 0xFF) + + crc = ((crc << 8) & 0xffffffff) ^ custom_crc_table[0xFF & ((crc >> 24) ^ v)] + crc = ((crc << 8) & 0xffffffff) ^ custom_crc_table[0xFF & ((crc >> 24) ^ (v >> 8))] + crc = ((crc << 8) & 0xffffffff) ^ custom_crc_table[0xFF & ((crc >> 24) ^ (v >> 16))] + crc = ((crc << 8) & 0xffffffff) ^ custom_crc_table[0xFF & ((crc >> 24) ^ (v >> 24))] + + k += 4 + length -= 4 + + if length > 0: + v = 0 + + for i in range(length): + v |= (bytes_arr[k+i] << 24-i*8) + + if length == 1: + v &= 0xFF000000 + + elif length == 2: + v &= 0xFFFF0000 + + elif length == 3: + v &= 0xFFFFFF00 + + crc = (( crc << 8 ) & 0xffffffff) ^ custom_crc_table[0xFF & ( (crc >> 24) ^ (v ) )]; + crc = (( crc << 8 ) & 0xffffffff) ^ custom_crc_table[0xFF & ( (crc >> 24) ^ (v >> 8) )]; + crc = (( crc << 8 ) & 0xffffffff) ^ custom_crc_table[0xFF & ( (crc >> 24) ^ (v >> 16) )]; + crc = (( crc << 8 ) & 0xffffffff) ^ custom_crc_table[0xFF & ( (crc >> 24) ^ (v >> 24) )]; + + return crc + +poly = 0x04C11DB7 +buf = bytes(reversed([1, 2, 3, 4])) + +generate_crc32_table(poly) +print(hex(crc32_stm(bytearray(buf)))) + +from crccheck import crc +import struct + +def rev_bits_in_word(w): + return sum( ((w>>i)&1) << (31-i) for i in range(32) ) + +import zlib +def crc32stm(inbytes): + crc32 = crc.Crc32.calc(inbytes)^0xffffffff + #crc32 = zlib.crc32(inbytes)^0xffffffff + crc32 = rev_bits_in_word(crc32) + return crc32 + +#data = [0x80,0x40,0xc0,0x20] +data = [0x00, 0, 0, 0x80, 0, 0, 0, 0x80] +print(hex(crc32stm(bytes(data)))) +print(hex(zlib.crc32(bytes([0, 0, 0, 1]))^0xffffffff)) -- cgit