summaryrefslogtreecommitdiff
path: root/hardware/fw/crctest.py
diff options
context:
space:
mode:
authorjaseg <git-bigdata-wsl-arch@jaseg.de>2021-04-09 18:38:02 +0200
committerjaseg <git-bigdata-wsl-arch@jaseg.de>2021-04-09 18:38:57 +0200
commit50998fcfb916ae251309bd4b464f2c122e8cb30d (patch)
tree4ecf7a7443b75ab51c4dc0c0fc9289342dc7d6a0 /hardware/fw/crctest.py
parent312fee491cfab436d52db4b6265107e20f3e1293 (diff)
downloadmaster-thesis-50998fcfb916ae251309bd4b464f2c122e8cb30d.tar.gz
master-thesis-50998fcfb916ae251309bd4b464f2c122e8cb30d.tar.bz2
master-thesis-50998fcfb916ae251309bd4b464f2c122e8cb30d.zip
Repo re-org
Diffstat (limited to 'hardware/fw/crctest.py')
-rw-r--r--hardware/fw/crctest.py79
1 files changed, 79 insertions, 0 deletions
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))