#!/usr/bin/env python3 """Brute-force checksum hypotheses for cmtd+0x08 against all known images. Tests CRC32/zlib/adler/fletcher/sum/xor variants over multiple spans. A hypothesis must match ALL builds to be reported as candidate. Stdlib only. """ import binascii, os, struct, sys, zlib BASE = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) IMAGES = [ 'Lofi-12XT_v1.1.156/Lofi-12XT_v1.1.156/Lofi-12 XT.bin', 'Lofi-12XT_v1.2.179/Lofi-12XT_v1.2.179/Lofi-12 XT.bin', 'Lofi-12XT_v1.5.205/Lofi-12XT_v1.5.205/Lofi-12 XT.bin', ] def fletcher16(data): s1 = s2 = 0 for b in data: s1 = (s1 + b) % 255 s2 = (s2 + s1) % 255 return (s2 << 8) | s1 def fletcher32(data): s1 = s2 = 0 for i in range(0, len(data), 2): w = data[i] | (data[i+1] << 8 if i+1 < len(data) else 0) s1 = (s1 + w) % 0xFFFF s2 = (s2 + s1) % 0xFFFF return (s2 << 16) | s1 def sum32(data): return sum(data) & 0xFFFFFFFF def xor32(data): r = 0 for i in range(0, len(data) - 3, 4): (w,) = struct.unpack('I', struct.pack('