2013-12-02 09:44:48 +00:00
|
|
|
# Capstone Disassembler Engine
|
|
|
|
# By Dang Hoang Vu, 2013
|
|
|
|
|
2013-12-01 19:24:11 +00:00
|
|
|
import sys, re
|
|
|
|
|
2013-12-02 10:37:46 +00:00
|
|
|
INCL_DIR = '../include/'
|
2013-12-01 19:24:11 +00:00
|
|
|
|
2013-12-02 10:37:46 +00:00
|
|
|
include = [ 'arm.h', 'arm64.h', 'mips.h', 'x86.h' ]
|
2013-12-01 19:24:11 +00:00
|
|
|
|
|
|
|
template = {
|
|
|
|
'java': {
|
2013-12-02 10:37:46 +00:00
|
|
|
'header': "// For Capstone Engine. AUTO-GENERATED FILE, DO NOT EDIT\npackage capstone;\n\npublic class %s_const {\n",
|
2013-12-01 19:24:11 +00:00
|
|
|
'footer': "}",
|
|
|
|
'line_format': '\tpublic static final int %s = %s;\n',
|
2013-12-02 10:37:46 +00:00
|
|
|
'out_file': './java/capstone/%s_const.java',
|
|
|
|
# prefixes for constant filenames of all archs - case sensitive
|
|
|
|
'arm.h': 'Arm',
|
|
|
|
'arm64.h': 'Arm64',
|
|
|
|
'mips.h': 'Mips',
|
|
|
|
'x86.h': 'X86',
|
2013-12-01 19:46:49 +00:00
|
|
|
},
|
|
|
|
'python': {
|
2013-12-02 10:37:46 +00:00
|
|
|
'header': "# For Capstone Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.py]\n",
|
2013-12-01 19:46:49 +00:00
|
|
|
'footer': "",
|
|
|
|
'line_format': '%s = %s\n',
|
2013-12-02 10:37:46 +00:00
|
|
|
'out_file': './python/capstone/%s_const.py',
|
|
|
|
# prefixes for constant filenames of all archs - case sensitive
|
|
|
|
'arm.h': 'arm',
|
|
|
|
'arm64.h': 'arm64',
|
|
|
|
'mips.h': 'mips',
|
|
|
|
'x86.h': 'x86',
|
2013-12-01 19:24:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def gen(templ):
|
|
|
|
global include, INCL_DIR
|
|
|
|
for target in include:
|
2013-12-02 10:37:46 +00:00
|
|
|
prefix = templ[target]
|
|
|
|
outfile = open(templ['out_file'] %(prefix), 'w')
|
|
|
|
outfile.write(templ['header'] % (prefix))
|
2013-12-01 19:24:11 +00:00
|
|
|
|
2013-12-02 10:37:46 +00:00
|
|
|
lines = open(INCL_DIR + target).readlines()
|
2013-12-01 19:24:11 +00:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
for line in lines:
|
|
|
|
line = line.strip()
|
|
|
|
if line == '' or line.startswith('//'):
|
|
|
|
continue
|
2013-12-02 10:37:46 +00:00
|
|
|
if not line.startswith(prefix.upper()):
|
2013-12-01 19:24:11 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
tmp = line.strip().split(',')
|
|
|
|
for t in tmp:
|
|
|
|
t = t.strip()
|
|
|
|
if not t or t.startswith('//'): continue
|
|
|
|
f = re.split('\s+', t)
|
|
|
|
|
2013-12-02 10:37:46 +00:00
|
|
|
if f[0].startswith(prefix.upper()):
|
2013-12-01 19:24:11 +00:00
|
|
|
if len(f) > 1 and f[1] not in '//=':
|
|
|
|
print "Error: Unable to convert %s" % f
|
|
|
|
continue
|
|
|
|
elif len(f) > 1 and f[1] == '=':
|
|
|
|
rhs = f[2]
|
|
|
|
else:
|
|
|
|
rhs = str(count)
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
if rhs == '0':
|
|
|
|
outfile.write("\n")
|
|
|
|
count = 1
|
|
|
|
|
|
|
|
outfile.write(templ['line_format'] %(f[0].strip(), rhs))
|
|
|
|
|
|
|
|
outfile.write(templ['footer'])
|
|
|
|
outfile.close()
|
|
|
|
|
|
|
|
def main():
|
2013-12-01 19:46:49 +00:00
|
|
|
try:
|
|
|
|
gen(template[sys.argv[1]])
|
|
|
|
except:
|
|
|
|
raise RuntimeError("Unsupported binding %s" % sys.argv[1])
|
2013-12-01 19:24:11 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if len(sys.argv) < 2:
|
|
|
|
print "Usage:", sys.argv[0], " <bindings: java|python>"
|
|
|
|
sys.exit(1)
|
|
|
|
main()
|