Created
May 8, 2025 11:16
-
-
Save developerfromjokela/c0a4f9e422e21ce92611d0e25347ebe7 to your computer and use it in GitHub Desktop.
Calculate NK update file SUM, Xanavi/Clarion 72xx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
def compute_checksum_and_verify(data: bytes) -> bool: | |
data_array = bytearray(data) | |
checksum_offset = 0x30 | |
# Read and temporarily clear the stored checksum for region 0 | |
original_checksum = int.from_bytes(data_array[checksum_offset:checksum_offset+2], byteorder='little') | |
data_array[checksum_offset:checksum_offset+2] = [0, 0] | |
# Calculate checksum by summing all bytes | |
calculated_checksum = sum(data_array) & 0xFFFF # Ensure 16-bit checksum | |
data_array[checksum_offset:checksum_offset+2] = original_checksum.to_bytes(2, byteorder='little') | |
# Print calculated checksum | |
print(f"File SUM is 0x{original_checksum:04X}") | |
print(f"Calculated SUM is 0x{calculated_checksum:04X}") | |
return calculated_checksum == original_checksum | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Compute checksums in NK Update files.") | |
parser.add_argument("input_file", help="Path to the input NK file") | |
args = parser.parse_args() | |
file_path = args.input_file | |
try: | |
with open(file_path, 'rb') as file: | |
data = file.read() | |
is_valid = compute_checksum_and_verify(data) | |
print(f"Checksum {'valid' if is_valid else 'invalid'}") | |
except FileNotFoundError: | |
print(f"Error: File '{file_path}' not found") | |
except Exception as e: | |
print(f"Error reading file: {e}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment