-
Notifications
You must be signed in to change notification settings - Fork 17
/
patchluastr.py
347 lines (316 loc) · 13 KB
/
patchluastr.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# Copyright (c) 2018-2020 Adrian Studer
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from sys import exit
from sys import argv
import argparse
def decode_int(data, endian):
num = 0
if endian == 0:
for b in data:
num = (num << 8) | b
else:
for b in reversed(data):
num = (num << 8) | b
return num
def encode_int(num, size, endian):
out = bytearray(size)
for pos in range(size):
out[pos] = num & 0xff
num = num >> 8
if endian == 0:
out = reversed(out)
return out
class LuaFile():
def __init__(self, file):
self.file = file
self.header = bytearray()
def load(self):
# read and verify Lua signature: 4 bytes (0x1b, 0x4c, 0x75, 0x61) (ESC Lua)
print("Reading", self.file.name)
self.sig = self.file.read(4)
if self.sig != b'\x1bLua':
print("This is not a Lua file.")
exit()
self.header.extend(self.sig)
# read and verify version: 2 bytes (0x52, 0x00)
self.ver = self.file.read(2)
print("Lua %d.%d detected" % (self.ver[0] >> 4, self.ver[0] & 0x0f))
if self.ver[1] != 0:
print("Error: Lua file not compatible with PUC-Rio")
exit()
self.header.extend(self.ver)
# read Lua system parameters: 6 bytes (endianess, int size, size_t size,
# instruction size, num size, float)
lua_sys_par = self.file.read(6)
self.endian = lua_sys_par[0]
self.int_size = lua_sys_par[1]
self.sizet_size = lua_sys_par[2]
self.instr_size = lua_sys_par[3]
self.num_size = lua_sys_par[4]
print("Lua settings: endian %d, int %d, size_t %d, instruction %d, number %d" %
(self.endian, self.int_size, self.sizet_size, self.instr_size, self.num_size))
if self.endian != 1:
print("Error: Lua file is big endian. Sorry, not supported yet :-(")
exit()
self.header.extend(lua_sys_par)
# read rest of file
self.body = self.file.read()
print("%d bytes read from file" % (len(self.header) + len(self.body)))
def replace(self, find, replace):
# search and replace strings
pos_start = 0
replace_count = 0
new_body = bytearray()
while True:
pos = self.body.find(find.encode('utf-8'), pos_start)
if pos == -1:
break
print("Found '%s' at %d" % (find, pos_start + pos))
# find start of Lua string (has to be fancier to work for big endian)
str_start = pos
while self.body[str_start] != 0:
str_start -= 1
# find end of Lua string
str_end = pos
while self.body[str_end] != 0:
str_end += 1
str_original = self.body[str_start + 1:str_end].decode("utf-8")
# verify Lua string
if self.body[str_start - self.sizet_size] != 4:
print("Error: Failed to parse Lua string structure")
exit()
str_len = decode_int(self.body[str_start - self.sizet_size + 1:str_start + 1], self.endian)
if str_len != len(str_original) + 1:
print("Error: Failed to parse Lua string structure")
exit()
# copy data before Lua string
new_body.extend(self.body[pos_start:str_start - self.sizet_size])
# write new Lua string
str_new = str_original.replace(find, replace).encode("utf-8")
new_body.extend([4])
new_body.extend(encode_int(len(str_new) + 1, self.sizet_size, self.endian))
new_body.extend(str_new)
new_body.extend([0])
# continue search
replace_count += 1
pos_start = str_end + 1
if replace_count == 0:
print("'%s' not found, nothing to patch" % (find))
else:
print("Updated %d strings" % (replace_count))
# copy remaining data
new_body.extend(self.body[pos_start:])
# replace body with processed version
self.body = new_body;
def write_to(self, file):
# write Lua header and body to a new file
file.write(self.header)
file.write(self.body)
print("%d bytes written to %s" % (len(self.header)+len(self.body), file.name))
class Section:
AUTO_SECTION_NAME = "!AUTO!"
def __init__(self):
self.name = self.AUTO_SECTION_NAME
self.infile = ""
self.outfile = ""
self.patches = []
def is_auto_section(self):
if self.name != self.AUTO_SECTION_NAME:
return False
else:
return True
class Patch:
def __init__(self):
self.find = ""
self.replace = ""
class PatchFile:
def __init__(self, file):
errors = 0
self.sections = []
currsection = None
currpatch = None
for lnr, line in enumerate(file):
line = line.lstrip()
lnr += 1 # count lines starting at 1, not 0
if len(line) == 0:
# ignore empty lines
pass
elif line.startswith("#"):
# ignore comment lines
pass
elif line.startswith("[") and line.rstrip().endswith("]"):
# new section
if not currsection is None and currsection.is_auto_section():
print("Error: Can't mix simplified with sectioned patch file in line", lnr)
print(line.rstrip())
errors += 1
# create section despite error to complete parsing file
currsection = self.new_section(line.rstrip()[1:-1])
currpatch = None
elif line.startswith("< "):
# find string
if len(line[2:-1]) == 0:
print("Error: Missing search string in line", lnr)
print(line.rstrip())
errors += 1
else:
if currsection is None:
# auto create section for simplified patches
currsection = self.new_section()
currpatch = Patch()
currsection.patches.append(currpatch)
currpatch.find = line[2:-1]
elif line.startswith(">"):
# replace string
if currpatch is None:
print("Error: Missing search string before line", lnr)
print(line.rstrip())
errors += 1
elif len(line.rstrip()) > 2 and not line.startswith("> "):
print("Error: Missing space after > in line", lnr)
print(line.rstrip())
errors += 1
else:
if len(currpatch.replace) != 0:
currpatch.replace += "\n"
currpatch.replace += line[2:-1]
else:
try:
name, value = line.split("=", 1)
name = name.rstrip()
value = value.lstrip().rstrip()
if currsection is None or currsection.is_auto_section():
print("Error: No current section in line", lnr)
print(line.rstrip())
errors += 1
elif name == "in":
# input file
if currsection.infile == "":
currsection.infile = value
else:
print("Error: More than 1 input file per section in line", lnr)
print(line.rstrip())
errors += 1
elif name == "out":
# input file
if currsection.outfile == "":
currsection.outfile = value
else:
print("Error: More than 1 output file per section in line", lnr)
print(line.rstrip())
errors += 1
else:
# unknown option
print("Error: Unknown option in line", lnr)
print(line.rstrip())
errors += 1
except ValueError:
print("Error: Failed to parse line",lnr)
print(line.rstrip())
errors += 1
# validate in/out files
for s in self.sections:
if s.is_auto_section():
# no files in simplified patch
pass
else:
if len(s.patches) == 0:
print("Error: No patch instructions in section", s.name)
errors += 1
if s.infile == "":
print("Error: Missing input file for section", s.name)
errors += 1
elif s.outfile == "":
s.outfile = s.infile + ".patched"
if errors > 0:
print("Aborting due to errors in patch script. No files were patched.")
exit()
def new_section(self, name = None):
section = Section()
if not name is None:
section.name = name
self.sections.append(section)
return section
if __name__ == "__main__":
parser = argparse.ArgumentParser(
epilog="Without patch file, all positional arguments are mandatory.")
parser.add_argument("input", type=argparse.FileType("rb", 0), nargs="?",
help="input file")
parser.add_argument("find", type=str, nargs="?",
help="string to find")
parser.add_argument("replace", type=str, nargs="?",
help="string to use as replacement")
parser.add_argument("-o", "--output", type=argparse.FileType("wb", 0),
help="output file")
parser.add_argument("-p", "--patch", type=argparse.FileType("r"),
help="file with patch instructions")
args = parser.parse_args()
# check if any arguments were passed
if not len(argv) > 1:
parser.print_usage()
exit()
# using input, find, replace arguments
if args.patch is None:
if args.input is None or args.find is None or args.replace is None:
print("Either a patch file, or an input file plus find and replace strings are required")
exit()
file = LuaFile(args.input)
file.load()
file.replace(args.find, args.replace)
# write new file
if args.output is None:
args.output = open(args.input.name + ".patched", "wb")
file.write_to(args.output)
# clean up
args.output.close()
args.input.close()
else:
# using patch file
patch = PatchFile(args.patch)
if len(patch.sections) == 0:
print("Error: Patch file is empty")
exit()
if patch.sections[0].is_auto_section():
# simplified patch file
if args.input is None:
print("Error: Missing input file for patching")
exit()
file = LuaFile(args.input)
file.load()
for p in patch.sections[0].patches:
file.replace(p.find, p.replace)
if args.output is None:
args.output = open(args.input.name + ".patched", "wb")
file.write_to(args.output)
else:
# sectioned patch file
for s in patch.sections:
print("Processing section [{}]".format(s.name))
with open(s.infile, "rb", 0) as infile:
with open(s.outfile, "wb", 0) as outfile:
file = LuaFile(infile)
file.load()
for p in s.patches:
file.replace(p.find, p.replace)
file.write_to(outfile)
# clean up
args.patch.close()
# done
exit()