-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdisassemble.py
More file actions
64 lines (44 loc) · 1.38 KB
/
disassemble.py
File metadata and controls
64 lines (44 loc) · 1.38 KB
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
import extractor
import lua51
import sutil
def match_bytes(bytecode: bytes, test: str) -> bool:
ok: bool = True
if len(bytecode) >= len(test):
for x in range(len(test)):
if bytecode[x] != ord(test[x]):
ok = False
break
else:
ok = False
return ok
def get_proto(bytecode: bytes) -> extractor.LFuncRead:
if not match_bytes(bytecode, "\x1BLua"):
raise RuntimeError("File is not Lua bytecode")
if match_bytes(bytecode, "\x1BLua\x51"):
result = lua51.L51FuncRead(bytecode[5:])
else:
raise NotImplementedError("Lua version not supported")
return result
def disassemble_bytecode(bytecode: bytes, args) -> str:
pp: extractor.ProtoPrint = extractor.ProtoPrint()
lf: extractor.LFuncRead = get_proto(bytecode)
pp.flags.has_comments = args.comments
pp.flags.has_lineinfo = not args.nolines
pp.flags.inl_consts = args.inlconsts
pp.flags.jmp_optimize = args.smartjumps
lf.read_function()
return pp.get_assembly(lf)
def process_query(output, args):
timer: sutil.LightTimer = sutil.LightTimer()
for name in args.files:
with open(name, 'rb') as file:
code: bytes = file.read()
timer.restart()
dis: str = disassemble_bytecode(code, args)
if args.verbose:
off: str = timer.pretty()
pad: str = len(file.name) * ' '
sz: str = sutil.sizeof_fmt(len(code), 'B')
dbgp = f'{file.name}: size {sz}\n{pad}: time {off}'
print(dbgp)
output.write(dis)