-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbytecode_reader.py
62 lines (50 loc) · 1.8 KB
/
bytecode_reader.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
import dis
import marshal
import types
ifPtr = open("input.pyc", 'rb')
header = ifPtr.read(8)
codeObject = marshal.load(ifPtr)
ifPtr.close()
def parse_code_object(code_object):
co_argcount = code_object.co_argcount
co_nlocals = code_object.co_nlocals
co_stacksize = code_object.co_stacksize
co_flags = code_object.co_flags
insBytes = bytearray(code_object.co_code)
i = 0
while i < len(insBytes):
opcode = insBytes[i]
if opcode not in dis.opmap.values():
print(str(i) + ":", "NOT EXISTING INSTRUCTION")
i += 1
elif opcode < dis.HAVE_ARGUMENT:
print(str(i) + " : ", dis.opname[opcode], opcode, 0, 1)
i += 1
elif opcode >= dis.HAVE_ARGUMENT:
if len(insBytes) > i + 2:
arg = (insBytes[i + 2] << 8) | insBytes[i + 1]
else:
arg = -1
print(str(i) + " : ", dis.opname[opcode], opcode, arg, 3)
i += 3
co_names = code_object.co_names
co_varnames = code_object.co_varnames
co_filename = code_object.co_filename
co_name = code_object.co_name
co_firstlineno = code_object.co_firstlineno
co_lnotab = code_object.co_lnotab
mod_const = []
for const in code_object.co_consts:
if isinstance(const, types.CodeType):
mod_const.append(parse_code_object(const))
pass
else:
mod_const.append(const)
co_constants = tuple(mod_const)
return types.CodeType(co_argcount, co_nlocals, co_stacksize, co_flags,
str(insBytes), co_constants, co_names, co_varnames,
co_filename, co_name, co_firstlineno, co_lnotab)
ofPtr = open('output.pyc', 'wb')
ofPtr.write(header)
marshal.dump(parse_code_object(codeObject), ofPtr)
ofPtr.close()