-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathqvmdis
executable file
·107 lines (90 loc) · 3.19 KB
/
qvmdis
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
#!/usr/bin/env python
####
# Copyright (C) 2012, 2020 Angelo Cano
#
# This file is part of Qvmdis.
#
# Qvmdis is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Qvmdis is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Qvmdis. If not, see <https://www.gnu.org/licenses/>.
####
import os.path, sys, traceback, Qvm
def usage ():
scriptName = os.path.basename(sys.argv[0])
sys.stderr.write("Usage: %s [--func-hash, -q] <qvm file> [cgame|game|ui]\n" % scriptName)
sys.stderr.write(" optionally specify cgame, game, or ui qvm to match syscalls and function hashes\n")
sys.stderr.write(" --func-hash : only print function hash values\n")
sys.stderr.write(" -q : suppress warnings\n")
sys.stderr.write(" -dr : replace code disassembly with decompiled output\n")
sys.stderr.write("\n")
sys.stderr.write(" ex: %s cgame.qvm cgame > cgame.dis\n" % scriptName)
sys.exit(1)
def output (msg):
sys.stdout.write(msg)
def main ():
onlyPrintFunctionHashes = False
qvmFile = None
qvmType = None
parsingOptions = True
for arg in sys.argv[1:]:
if arg == "--func-hash" and parsingOptions:
onlyPrintFunctionHashes = True
elif arg == "-q" and parsingOptions:
Qvm.SuppressWarnings = True
elif arg == "-dr" and parsingOptions:
Qvm.ReplaceDecompiled = True
elif arg == "--" and parsingOptions:
parsingOptions = False
elif qvmFile == None:
qvmFile = arg
elif qvmType == None:
qvmType = arg
if qvmFile == None:
usage()
if qvmType != None:
if not qvmType in ("cgame", "game", "ui"):
output("invalid qvm type '%s'\n" % qvmType)
usage()
q = Qvm.Qvm(qvmFile, qvmType)
if onlyPrintFunctionHashes:
q.print_function_hashes()
return
output(";; header\n")
q.print_header()
output("\n")
output(";; function hashes\n")
q.print_function_hashes()
output("\n")
output(";; code segment\n")
q.print_code_disassembly()
output("\n")
output(";; data segment\n")
q.print_data_disassembly()
output("\n")
output(";; lit segment\n")
q.print_lit_disassembly()
if q.magic != Qvm.QVM_MAGIC_VER1:
output("\n;; jump table\n")
q.print_jump_table()
if __name__ == "__main__":
try:
main()
except SystemExit as se:
# don't print traceback for exit()
raise se
except Exception as ex:
# send to both stdout and stderr since output is usually redirected to file
traceback.print_exc(file=sys.stdout)
sys.stderr.write("\n")
traceback.print_exc(file=sys.stderr)
# feed back up to exit() with correct exit value
raise ex