tft: all four checksums solved (three header CRCs, trailer for any length)

0x44 = CRC of the resource area, 0xC4 = CRC of header [0,0xC4), 0x18C = CRC of
[0xC8,0x18C); trailer = CRC of the whole file ^ (len & 0xFF) ^ 0x56. tft.seal()
recomputes all of them. Verified against 24 Editor-made files of 8 lengths.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
mangavalkandClaude Fable 5.2 committed 2026-11-16 16:54:59 +01:00
1 parent 6b2835334a
commit 7337e693a5
6 files changed
+124 -22

No files matched your search

+31
View File
@@ -23,6 +23,21 @@ def fake_tft(payload: bytes) -> bytes:
return body + struct.pack("<I", bitwise_word_crc(body, 0x12345678))
def sealed_tft(resources: bytes, pages: bytes) -> bytes:
"""A synthetic image with all four checksums computed by the independent bitwise CRC."""
head = bytearray(b"\xff" * tft.RES_START)
head[:0x200] = bytes(0x200)
struct.pack_into("<I", head, tft.OFF_RES_SIZE, len(resources))
for i, o in enumerate(range(tft.LAYOUT_START, tft.LAYOUT_END, 4)):
struct.pack_into("<I", head, o, (i * 7) ^ tft.LAYOUT_KEY)
struct.pack_into("<I", head, tft.OFF_RES_CRC, bitwise_word_crc(resources, 0xFFFFFFFF))
struct.pack_into("<I", head, tft.OFF_HEAD1_CRC, bitwise_word_crc(bytes(head[:0xC4]), 0xFFFFFFFF))
struct.pack_into("<I", head, tft.OFF_HEAD2_CRC, bitwise_word_crc(bytes(head[0xC8:0x18C]), 0xFFFFFFFF))
body = bytes(head) + resources + pages
n = len(body) + 4
return body + struct.pack("<I", bitwise_word_crc(body, 0xFFFFFFFF) ^ (n & 0xFF) ^ 0x56)
class Tft(unittest.TestCase):
def test_layout_table(self):
t = fake_tft(b"x" * 10)
@@ -35,6 +50,22 @@ class Tft(unittest.TestCase):
expect = fake_tft(b"page data: colour 00 f8")
self.assertEqual(tft.patch_trailer(old, new_body + b"\0\0\0\0"), expect)
def test_checksums_and_seal(self):
# rules measured on Editor output: 0x44 = CRC of the resource area, 0xC4 = CRC of
# [0, 0xC4), 0x18C = CRC of [0xC8, 0x18C), trailer = CRC of the file ^ (len & 0xFF) ^ 0x56
good = sealed_tft(b"firmware and pictures", b"page data, any length!")
self.assertTrue(tft.verify_header(good))
self.assertTrue(tft.verify_trailer(good))
broken = bytearray(good)
for o in (tft.OFF_RES_CRC, tft.OFF_HEAD1_CRC, tft.OFF_HEAD2_CRC, len(good) - 4):
broken[o] ^= 0x5A
self.assertFalse(tft.verify_header(bytes(broken)))
self.assertFalse(tft.verify_trailer(bytes(broken)))
self.assertEqual(tft.seal(bytes(broken)), good)
# a different length changes the trailer's XOR byte
longer = sealed_tft(b"firmware and pictures", b"page data, any length!!!")
self.assertEqual(tft.seal(longer[:-4] + b"\0\0\0\0"), longer)
def test_patch_trailer_rejects_length_change(self):
with self.assertRaises(ValueError):
tft.patch_trailer(b"\0" * 20, b"\0" * 24)