Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions bindings/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ TEST_ARM64 = $(TMPDIR)/test_arm64
TEST_MIPS = $(TMPDIR)/test_mips
TEST_X86 = $(TMPDIR)/test_x86

all: expected python java #oclma ruby
tests: expected python java #oclma ruby

expected:
$(MAKE) -C ../tests
Expand All @@ -20,12 +20,13 @@ expected:
../tests/test_x86 > $(TEST_X86)_e

python: FORCE
$(MAKE) -C python
python python/test.py > $(TEST)_o
python python/test_arm.py > $(TEST_ARM)_o
python python/test_arm64.py > $(TEST_ARM64)_o
python python/test_mips.py > $(TEST_MIPS)_o
python python/test_x86.py > $(TEST_X86)_o
$(MAKE) test
$(MAKE) test_diff

java: FORCE
$(MAKE) -C java
Expand All @@ -34,13 +35,18 @@ java: FORCE
cd java; ./run.sh arm64 > $(TEST_ARM64)_o
cd java; ./run.sh mips > $(TEST_MIPS)_o
cd java; ./run.sh x86 > $(TEST_X86)_o
$(MAKE) test
$(MAKE) test_diff

test: FORCE
test_diff: FORCE
$(DIFF) $(TEST)_e $(TEST)_o
$(DIFF) $(TEST_ARM)_e $(TEST_ARM)_o
$(DIFF) $(TEST_ARM64)_e $(TEST_ARM64)_o
$(DIFF) $(TEST_MIPS)_e $(TEST_MIPS)_o
$(DIFF) $(TEST_X86)_e $(TEST_X86)_o

clean:
rm -rf $(TMPDIR)
$(MAKE) clean -C java
$(MAKE) clean -C python

FORCE:
79 changes: 79 additions & 0 deletions bindings/const_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import sys, re

INCL_DIR = '../include'

include = [
('/arm.h', 'ARM_'),
('/arm64.h', 'ARM64_'),
('/x86.h', 'X86_'),
('/mips.h', 'MIPS_'),
]

template = {
'java': {
'header': "// AUTO-GENERATED FILE, DO NOT EDIT\npackage capstone;\n\npublic class %sconst {\n",
'footer': "}",
'line_format': '\tpublic static final int %s = %s;\n',
'out_file': './java/capstone/%sconst.java',
},
'python': {
'header': "# AUTO-GENERATED FILE, DO NOT EDIT [%s]\n",
'footer': "",
'line_format': '%s = %s\n',
'out_file': './python/capstone/%sconst.py',
}
}

def gen(templ):
global include, INCL_DIR
for target in include:
prefix = target[1];
outfile = open(templ['out_file'] %(prefix.capitalize()), 'w')
outfile.write(templ['header'] % (prefix.capitalize()))

lines = open(INCL_DIR + target[0]).readlines()

count = 0
for line in lines:
line = line.strip()
if line == '' or line.startswith('//'):
continue
if not line.startswith(prefix):
continue

tmp = line.strip().split(',')
for t in tmp:
t = t.strip()
if not t or t.startswith('//'): continue
f = re.split('\s+', t)

if f[0].startswith(prefix):
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():
try:
gen(template[sys.argv[1]])
except:
raise RuntimeError("Unsupported binding %s" % sys.argv[1])

if __name__ == "__main__":
if len(sys.argv) < 2:
print "Usage:", sys.argv[0], " <bindings: java|python>"
sys.exit(1)
main()
8 changes: 6 additions & 2 deletions bindings/java/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ else
endif
endif

CAPSTONE_JAVA = Capstone.java Arm.java Arm64.java Mips.java X86.java
CAPSTONE_JAVA = Capstone.java Arm_const.java Arm64_const.java Mips_const.java X86_const.java Arm.java Arm64.java Mips.java X86.java

all: capstone tests
all: gen_const capstone tests

capstone: capstone_class
jar cf capstone.jar capstone/*.class
Expand All @@ -25,8 +25,12 @@ capstone_class: jna
tests: jna
javac -classpath "$(JNA):capstone.jar" Test.java TestArm.java TestArm64.java TestMips.java TestX86.java

gen_const:
cd ../; python const_generator.py java

jna:
@if [ ! $(JNA) ]; then echo "*** Unable to find JNA ***"; exit 1; fi

clean:
rm -rf capstone/*_const.java capstone/*.class
rm -rf *.class *.log *.jar
18 changes: 10 additions & 8 deletions bindings/java/TestArm.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import capstone.Capstone;
import capstone.Arm;

import static capstone.Arm_const.*;

public class TestArm {

static byte[] hexString2Byte(String s) {
Expand Down Expand Up @@ -46,17 +48,17 @@ public static void print_ins_detail(Capstone.cs_insn ins) {
for (int c=0; c<op_info.op.length; c++) {
Arm.Operand i = (Arm.Operand) op_info.op[c];
String imm = hex(i.value.imm);
if (i.type == Arm.ARM_OP_REG)
if (i.type == ARM_OP_REG)
System.out.printf("\t\toperands[%d].type: REG = %s\n", c, cs.reg_name(i.value.reg));
if (i.type == Arm.ARM_OP_IMM)
if (i.type == ARM_OP_IMM)
System.out.printf("\t\toperands[%d].type: IMM = 0x%x\n", c, i.value.imm);
if (i.type == Arm.ARM_OP_PIMM)
if (i.type == ARM_OP_PIMM)
System.out.printf("\t\toperands[%d].type: P-IMM = %d\n", c, i.value.imm);
if (i.type == Arm.ARM_OP_CIMM)
if (i.type == ARM_OP_CIMM)
System.out.printf("\t\toperands[%d].type: C-IMM = %d\n", c, i.value.imm);
if (i.type == Arm.ARM_OP_FP)
if (i.type == ARM_OP_FP)
System.out.printf("\t\toperands[%d].type: FP = %f\n", c, i.value.fp);
if (i.type == Arm.ARM_OP_MEM) {
if (i.type == ARM_OP_MEM) {
System.out.printf("\t\toperands[%d].type: MEM\n",c);
String base = cs.reg_name(i.value.mem.base);
String index = cs.reg_name(i.value.mem.index);
Expand All @@ -69,7 +71,7 @@ public static void print_ins_detail(Capstone.cs_insn ins) {
if (i.value.mem.disp != 0)
System.out.printf("\t\t\toperands[%d].mem.disp: 0x%x\n", c, (i.value.mem.disp));
}
if (i.shift.type != Arm.ARM_SFT_INVALID && i.shift.value > 0)
if (i.shift.type != ARM_SFT_INVALID && i.shift.value > 0)
System.out.printf("\t\t\tShift: type = %d, value = %d\n", i.shift.type, i.shift.value);
}
if (op_info.writeback)
Expand All @@ -78,7 +80,7 @@ public static void print_ins_detail(Capstone.cs_insn ins) {
if (op_info.update_flags)
System.out.println("\tUpdate-flags: True");

if (op_info.cc != Arm.ARM_CC_AL && op_info.cc != Arm.ARM_CC_INVALID)
if (op_info.cc != ARM_CC_AL && op_info.cc != ARM_CC_INVALID)
System.out.printf("\tCode condition: %d\n", op_info.cc);
}
}
Expand Down
18 changes: 10 additions & 8 deletions bindings/java/TestArm64.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import capstone.Capstone;
import capstone.Arm64;

import static capstone.Arm64_const.*;

public class TestArm64 {

static byte[] hexString2Byte(String s) {
Expand Down Expand Up @@ -43,15 +45,15 @@ public static void print_ins_detail(Capstone.cs_insn ins) {
for (int c=0; c<op_info.op.length; c++) {
Arm64.Operand i = (Arm64.Operand) op_info.op[c];
String imm = hex(i.value.imm);
if (i.type == Arm64.ARM64_OP_REG)
if (i.type == ARM64_OP_REG)
System.out.printf("\t\toperands[%d].type: REG = %s\n", c, cs.reg_name(i.value.reg));
if (i.type == Arm64.ARM64_OP_IMM)
if (i.type == ARM64_OP_IMM)
System.out.printf("\t\toperands[%d].type: IMM = 0x%x\n", c, i.value.imm);
if (i.type == Arm64.ARM64_OP_CIMM)
if (i.type == ARM64_OP_CIMM)
System.out.printf("\t\toperands[%d].type: C-IMM = %d\n", c, i.value.imm);
if (i.type == Arm64.ARM64_OP_FP)
if (i.type == ARM64_OP_FP)
System.out.printf("\t\toperands[%d].type: FP = %f\n", c, i.value.fp);
if (i.type == Arm64.ARM64_OP_MEM) {
if (i.type == ARM64_OP_MEM) {
System.out.printf("\t\toperands[%d].type: MEM\n",c);
String base = cs.reg_name(i.value.mem.base);
String index = cs.reg_name(i.value.mem.index);
Expand All @@ -62,9 +64,9 @@ public static void print_ins_detail(Capstone.cs_insn ins) {
if (i.value.mem.disp != 0)
System.out.printf("\t\t\toperands[%d].mem.disp: 0x%x\n", c, i.value.mem.disp);
}
if (i.shift.type != Arm64.ARM64_SFT_INVALID && i.shift.value > 0)
if (i.shift.type != ARM64_SFT_INVALID && i.shift.value > 0)
System.out.printf("\t\t\tShift: type = %d, value = %d\n", i.shift.type, i.shift.value);
if (i.ext != Arm64.ARM64_EXT_INVALID)
if (i.ext != ARM64_EXT_INVALID)
System.out.printf("\t\t\tExt: %d\n", i.ext);
}
}
Expand All @@ -75,7 +77,7 @@ public static void print_ins_detail(Capstone.cs_insn ins) {
if (op_info.update_flags)
System.out.println("\tUpdate-flags: True");

if (op_info.cc != Arm64.ARM64_CC_AL && op_info.cc != Arm64.ARM64_CC_INVALID)
if (op_info.cc != ARM64_CC_AL && op_info.cc != ARM64_CC_INVALID)
System.out.printf("\tCode condition: %d\n", op_info.cc);

}
Expand Down
8 changes: 5 additions & 3 deletions bindings/java/TestMips.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import capstone.Capstone;
import capstone.Mips;

import static capstone.Mips_const.*;

public class TestMips {

static byte[] hexString2Byte(String s) {
Expand Down Expand Up @@ -44,11 +46,11 @@ public static void print_ins_detail(Capstone.cs_insn ins) {
for (int c=0; c<op_info.op.length; c++) {
Mips.Operand i = (Mips.Operand) op_info.op[c];
String imm = hex(i.value.imm);
if (i.type == Mips.MIPS_OP_REG)
if (i.type == MIPS_OP_REG)
System.out.printf("\t\toperands[%d].type: REG = %s\n", c, cs.reg_name(i.value.reg));
if (i.type == Mips.MIPS_OP_IMM)
if (i.type == MIPS_OP_IMM)
System.out.printf("\t\toperands[%d].type: IMM = 0x%x\n", c, i.value.imm);
if (i.type == Mips.MIPS_OP_MEM) {
if (i.type == MIPS_OP_MEM) {
System.out.printf("\t\toperands[%d].type: MEM\n",c);
String base = cs.reg_name(i.value.mem.base);
if (base != null)
Expand Down
16 changes: 9 additions & 7 deletions bindings/java/TestX86.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import capstone.Capstone;
import capstone.X86;

import static capstone.X86_const.*;

public class TestX86 {

static byte[] hexString2Byte(String s) {
Expand Down Expand Up @@ -49,7 +51,7 @@ public static void print_ins_detail(Capstone.cs_insn ins) {

System.out.printf("\tPrefix: %s\n", array2hex(op_info.prefix));

if (op_info.segment != X86.X86_REG_INVALID)
if (op_info.segment != X86_REG_INVALID)
System.out.println("\tSegment override: " + cs.reg_name(op_info.segment));


Expand All @@ -74,11 +76,11 @@ public static void print_ins_detail(Capstone.cs_insn ins) {
cs.reg_name(op_info.sib_index), op_info.sib_scale, cs.reg_name(op_info.sib_base));
}

int count = ins.op_count(X86.X86_OP_IMM);
int count = ins.op_count(X86_OP_IMM);
if (count > 0) {
System.out.printf("\timm_count: %d\n", count);
for (int i=0; i<count; i++) {
int index = ins.op_index(X86.X86_OP_IMM, i + 1);
int index = ins.op_index(X86_OP_IMM, i + 1);
System.out.printf("\t\timms[%d]: 0x%x\n", i+1, (op_info.op[index].value.imm));
}
}
Expand All @@ -88,13 +90,13 @@ public static void print_ins_detail(Capstone.cs_insn ins) {
for (int c=0; c<op_info.op.length; c++) {
X86.Operand i = (X86.Operand) op_info.op[c];
String imm = hex(i.value.imm);
if (i.type == X86.X86_OP_REG)
if (i.type == X86_OP_REG)
System.out.printf("\t\toperands[%d].type: REG = %s\n", c, cs.reg_name(i.value.reg));
if (i.type == X86.X86_OP_IMM)
if (i.type == X86_OP_IMM)
System.out.printf("\t\toperands[%d].type: IMM = 0x%x\n", c, i.value.imm);
if (i.type == X86.X86_OP_FP)
if (i.type == X86_OP_FP)
System.out.printf("\t\toperands[%d].type: FP = %f\n", c, i.value.fp);
if (i.type == X86.X86_OP_MEM) {
if (i.type == X86_OP_MEM) {
System.out.printf("\t\toperands[%d].type: MEM\n",c);
String base = cs.reg_name(i.value.mem.base);
String index = cs.reg_name(i.value.mem.index);
Expand Down
1 change: 1 addition & 0 deletions bindings/java/capstone/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*_const.java
Loading