-
Notifications
You must be signed in to change notification settings - Fork 2
/
assfix.py
51 lines (42 loc) · 1.24 KB
/
assfix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/python3
import struct, sys, os
import argparse
from pathlib import Path
parser = argparse.ArgumentParser(description='Pad an A3X .ap3 ROM file and fix its checksum.')
parser.add_argument('inFile', help='source .ap3 file')
parser.add_argument('outFile', nargs='?', help='target .ap3 file')
parser.add_argument('-p', '--pad', help='only pad, skip the checksum', action='store_true')
parser.add_argument('-v', '--verbose', help='use verbose output', action='store_true')
args = parser.parse_args()
if not args.outFile:
args.outFile = args.inFile
fileSize = Path(args.inFile).stat().st_size
def roundup(v):
v -= 1;
v |= v >> 1
v |= v >> 2
v |= v >> 4
v |= v >> 8
v |= v >> 16
v += 1
return v
padded = roundup(fileSize)
with open(args.inFile, "rb") as inp:
wft = inp.read()
sum = 0
for i in range(0, 0x20):
sum += wft[i]
for i in range(0x20, fileSize):
sum += wft[i]
if args.verbose:
if not args.pad:
print(f'Checksum is {sum:08X}.')
print(f'Padding from {fileSize:08X} ({fileSize // 1024} KiB) up to {padded:08X} ({padded // 1024} KiB).')
with open(args.outFile, "wb") as outp:
outp.write(wft)
if not args.pad:
outp.seek(0x20)
outp.write(struct.pack('>L', sum))
if padded > fileSize:
outp.seek(padded - 1)
outp.write(b'\x00')