-
Notifications
You must be signed in to change notification settings - Fork 0
/
rom-diff.py
153 lines (128 loc) · 3.86 KB
/
rom-diff.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import argparse
import binascii
import collections
import enum
import math
import sys
RomConfig = collections.namedtuple("RomConfig", ["header_length", "ram_offset"])
mario_config = RomConfig(header_length=0x40, ram_offset=0x245000)
class DiffFormatter:
def __init__(self, max_run, max_gap, formatter, out):
self.run = max_run
self.gap = max_gap
self.formatter = formatter
self.out = out
self.buffer = []
self.match = []
self.start = 0
def add_diff(self, address, byte):
if not self.buffer:
self.start = address
self.buffer = [byte]
self.match = []
else:
if self.match:
self.buffer += self.match
self.match = []
self.buffer.append(byte)
if len(self.buffer) == self.run:
self.flush()
def add_same(self, byte):
if self.buffer:
if len(self.match) < self.gap:
self.match.append(byte)
else:
self.flush()
def flush(self):
if self.buffer:
self.out.write(self.formatter(self.start, bytes(self.buffer)))
self.buffer = []
self.match = []
def gameshark_format(out):
return DiffFormatter(
max_run=2,
max_gap=0,
formatter=lambda addr, data: "A{}{:06X} {:04X}\n".format(
len(data) - 1, addr, int.from_bytes(data, byteorder="big")
),
out=out,
)
def stroop_format(out):
return DiffFormatter(
max_run=math.inf,
max_gap=16,
formatter=lambda addr, data: "80{:06X}: {}\n".format(
addr, str(binascii.hexlify(data), "ascii")
),
out=out,
)
class ByteSwapper:
def __init__(self, f):
self.file = f
self.buffer = b""
def read(self, n):
assert n == 1
if self.buffer:
x = self.buffer
self.buffer = b""
return x
x = self.file.read(2)
if len(x) < 2:
return b"" # silently ignore odd length for now
self.buffer = x[:1]
return x[1:]
def seek(self, d, w):
self.file.seek(d, w)
if self.file.tell() % 2 == 0:
self.buffer = b""
else:
self.file.seek(-1, 1)
self.read(1)
def ordered_file(f):
magic = f.read(4)
f.seek(0, 0)
if len(magic) < 4:
raise ValueError("File way too short")
if magic[0] == 0x80:
return f
if magic[0] == 0x37:
return ByteSwapper(f)
def compute_diff(base, hack, diff, rom_config):
base = ordered_file(base)
hack = ordered_file(hack)
base.seek(rom_config.header_length, 0)
hack.seek(rom_config.header_length, 0)
address = rom_config.header_length + rom_config.ram_offset
while True:
b, h = base.read(1), hack.read(1)
if not b or not h:
if b or h:
print("File length mismatch, ending early", file=sys.stderr)
break
if b != h:
diff.add_diff(address, h[0])
else:
diff.add_same(h[0])
address += 1
diff.flush()
if __name__ == "__main__":
ap = argparse.ArgumentParser()
ap.add_argument(
"--base", type=str, required=True, help="Base file to compare against"
)
ap.add_argument(
"--hack", type=str, required=True, help="Hack file to create a patch for"
)
ap.add_argument(
"--stroop",
dest="fmt",
action="store_const",
default=gameshark_format,
const=stroop_format,
help="Generate a diff in STROOP's .hck format",
)
ap.add_argument("--header", type=int, help="Length of header to ignore (in bytes)")
args = ap.parse_args()
base = open(args.base, "rb")
hack = open(args.hack, "rb")
compute_diff(base, hack, args.fmt(sys.stdout), mario_config)