From 30b5e3c1c1fd9de17ed57f8b5a51699be1e1e784 Mon Sep 17 00:00:00 2001 From: "B. Stack" Date: Wed, 20 Mar 2024 09:42:35 -0400 Subject: simplify checksum, rearrange position bytes * simplify checksum work by packing its value as little-endian unsigned. * rearranged bytes to be in order --- srb_lib.py | 70 ++++++++++++++++++++++++-------------------------------------- 1 file changed, 27 insertions(+), 43 deletions(-) (limited to 'srb_lib.py') diff --git a/srb_lib.py b/srb_lib.py index 3daae7b..874c287 100644 --- a/srb_lib.py +++ b/srb_lib.py @@ -25,24 +25,36 @@ # Dependencies: import sys, struct -srb_lib_version = "20240320a" +srb_lib_version = "20240320b" # Table of byte positions of values in the savegame file, minus the first four bytes which are the checksum. Due to zero-indexing of python lists, but for ease of usage, we will always put a zero as the value of index 0. That is, profile 1 will use index 1 of the list. # "ZL',checksum)) - #f.write(struct.pack('>I',int(checksum))) + f.write(struct.pack(' # crc width=32 poly=0x4c11db7 init=0x0 xorout=0x235b4b9c refin=false refout=false out_endian=little - csum = hex(crc_poly(data, 32, 0x4C11DB7, crc=0x0, ref_in=False, ref_out=False, xor_out=0x235b4b9c)) - # comes in as ceb434d4, but needs to be d434b4ce, and bytearray is convenient way to swap it like that - # trim off the '0x' from the string - print(f"debug: Got csum={csum}") - # pad any odd-digit-count hex value with a leading 0 for bytearray.fromhex() which is not very smart. - csum = ("0" if len(str(csum)) % 2 else "") + str(csum)[2:] - csum = bytearray.fromhex(csum) - csum.reverse() - # and back to bytes because that is how we will want to use it. - return bytes(csum) + csum = crc_poly(data, 32, 0x4C11DB7, crc=0x0, ref_in=False, ref_out=False, xor_out=0x235b4b9c) + # Note that this value will be stored little-endian in the file. That detail does not matter here. + return csum def correct_file(filename, debuglevel = 0): """ Given a savegame file, calculate the correct checksum and store that sum instead of whatever is there. """ @@ -839,10 +823,10 @@ def correct_file(filename, debuglevel = 0): ran = False if csum != cs: if debuglev(5,debuglevel): - ferror(f"Stored checksum is {cs} and will update it to {csum}") + ferror(f"Stored checksum is 0x{cs:04x} and will update it to 0x{csum:04x}") write_file(filename,csum,data) ran = True else: if debuglev(2,debuglevel): - ferror(f"Stored checksum is still correct, {csum}. Skipping {filename}") + ferror(f"Stored checksum is still correct, 0x{csum:04x}. Skipping {filename}") return ran -- cgit