blob: f91472bc33ffd660e592396567f3e0a518f00b24 [file] [log] [blame]
danghvu6a6947f2013-11-26 22:28:41 -06001// Capstone Java binding
2// By Nguyen Anh Quynh & Dang Hoang Vu, 2013
3
4import com.sun.jna.Native;
5import com.sun.jna.Memory;
6import com.sun.jna.Pointer;
7
danghvuf86a7d52013-11-27 15:09:07 -06008import capstone.Capstone;
9import capstone.X86;
10
danghvu6a6947f2013-11-26 22:28:41 -060011public class TestX86 {
12
13 static byte[] hexString2Byte(String s) {
14 // from http://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java
15 int len = s.length();
16 byte[] data = new byte[len / 2];
17 for (int i = 0; i < len; i += 2) {
18 data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
19 + Character.digit(s.charAt(i+1), 16));
20 }
21 return data;
22 }
23
danghvu6a6947f2013-11-26 22:28:41 -060024 static final String X86_CODE64 = "55488b05b8130000";
danghvu7b088042013-11-27 10:58:31 -060025 static final String X86_CODE16 = "8d4c320801d881c6341200000523010000368b849123010000418d8439896700008d8789670000b4c6";
26 static final String X86_CODE32 = "8d4c320801d881c6341200000523010000368b849123010000418d8439896700008d8789670000b4c6";
danghvu6a6947f2013-11-26 22:28:41 -060027
28 public static Capstone cs;
29
30 private static String hex(int i) {
31 return Integer.toString(i, 16);
32 }
33
34 private static String hex(long i) {
35 return Long.toString(i, 16);
36 }
37
38 private static String array2hex(byte[] arr) {
39 String ret = "";
40 for (int i=0 ;i<arr.length; i++)
41 ret += String.format("0x%02x ", arr[i]);
42 return ret;
43 }
44
45 public static void print_ins_detail(Capstone.cs_insn ins) {
46 System.out.printf("0x%x:\t%s\t%s\n", ins.address, ins.mnemonic, ins.operands);
47
48 X86.OpInfo op_info = (X86.OpInfo) ins.op_info;
49
50 System.out.printf("\tPrefix: %s\n", array2hex(op_info.prefix));
51
52 if (op_info.segment != X86.X86_REG_INVALID)
danghvu5f495522013-11-27 02:13:32 -060053 System.out.println("\tSegment override: " + cs.reg_name(op_info.segment));
danghvu6a6947f2013-11-26 22:28:41 -060054
55
56 System.out.printf("\tOpcode: %s\n", array2hex(op_info.opcode));
57
58 // print operand's size, address size, displacement size & immediate size
59 System.out.printf("\top_size: %d, addr_size: %d, disp_size: %d, imm_size: %d\n"
60 , op_info.op_size, op_info.addr_size, op_info.disp_size, op_info.imm_size);
61
62 // print modRM byte
63 System.out.printf("\tmodrm: 0x%x\n", op_info.modrm);
64
65 // print displacement value
danghvu7b088042013-11-27 10:58:31 -060066 System.out.printf("\tdisp: 0x%x\n", op_info.disp);
danghvu6a6947f2013-11-26 22:28:41 -060067
68 // SIB is not available in 16-bit mode
danghvuf3ef6962013-11-27 21:41:17 -060069 if ( (cs.mode & Capstone.CS_MODE_16) == 0) {
danghvu35855b52013-11-26 22:42:30 -060070 // print SIB byte
danghvu5f495522013-11-27 02:13:32 -060071 System.out.printf("\tsib: 0x%x\n", op_info.sib);
danghvuf3ef6962013-11-27 21:41:17 -060072 if (op_info.sib != 0)
73 System.out.printf("\tsib_index: %s, sib_scale: %d, sib_base: %s\n",
74 cs.reg_name(op_info.sib_index), op_info.sib_scale, cs.reg_name(op_info.sib_base));
75 }
danghvu6a6947f2013-11-26 22:28:41 -060076
77 int count = ins.op_count(X86.X86_OP_IMM);
78 if (count > 0) {
79 System.out.printf("\timm_count: %d\n", count);
80 for (int i=0; i<count; i++) {
81 int index = ins.op_index(X86.X86_OP_IMM, i + 1);
danghvu7b088042013-11-27 10:58:31 -060082 System.out.printf("\t\timms[%d]: 0x%x\n", i+1, (op_info.op[index].value.imm));
danghvu6a6947f2013-11-26 22:28:41 -060083 }
84 }
85
86 if (op_info.op != null) {
87 System.out.printf("\top_count: %d\n", op_info.op.length);
danghvu7b088042013-11-27 10:58:31 -060088 for (int c=0; c<op_info.op.length; c++) {
89 X86.Operand i = (X86.Operand) op_info.op[c];
danghvu6a6947f2013-11-26 22:28:41 -060090 String imm = hex(i.value.imm);
91 if (i.type == X86.X86_OP_REG)
danghvu35855b52013-11-26 22:42:30 -060092 System.out.printf("\t\toperands[%d].type: REG = %s\n", c, cs.reg_name(i.value.reg));
danghvu6a6947f2013-11-26 22:28:41 -060093 if (i.type == X86.X86_OP_IMM)
danghvu7b088042013-11-27 10:58:31 -060094 System.out.printf("\t\toperands[%d].type: IMM = 0x%x\n", c, i.value.imm);
danghvu6a6947f2013-11-26 22:28:41 -060095 if (i.type == X86.X86_OP_FP)
danghvu35855b52013-11-26 22:42:30 -060096 System.out.printf("\t\toperands[%d].type: FP = %f\n", c, i.value.fp);
danghvu6a6947f2013-11-26 22:28:41 -060097 if (i.type == X86.X86_OP_MEM) {
98 System.out.printf("\t\toperands[%d].type: MEM\n",c);
99 String base = cs.reg_name(i.value.mem.base);
100 String index = cs.reg_name(i.value.mem.index);
101 if (base != null)
102 System.out.printf("\t\t\toperands[%d].mem.base: REG = %s\n", c, base);
103 if (index != null)
104 System.out.printf("\t\t\toperands[%d].mem.index: REG = %s\n", c, index);
105 if (i.value.mem.scale != 1)
danghvu7b088042013-11-27 10:58:31 -0600106 System.out.printf("\t\t\toperands[%d].mem.scale: %d\n", c, i.value.mem.scale);
danghvu6a6947f2013-11-26 22:28:41 -0600107 if (i.value.mem.disp != 0)
danghvu7b088042013-11-27 10:58:31 -0600108 System.out.printf("\t\t\toperands[%d].mem.disp: 0x%x\n", c, i.value.mem.disp);
danghvu6a6947f2013-11-26 22:28:41 -0600109 }
110 }
111 }
112 }
113
114 public static void main(String argv[]) {
115
116 final Test.platform[] all_tests = {
117 new Test.platform(Capstone.CS_ARCH_X86, Capstone.CS_MODE_16, hexString2Byte(X86_CODE16), "X86 16bit (Intel syntax)"),
danghvuf3ef6962013-11-27 21:41:17 -0600118 new Test.platform(Capstone.CS_ARCH_X86, Capstone.CS_MODE_32 + Capstone.CS_MODE_SYNTAX_ATT, hexString2Byte(X86_CODE32), "X86 32 (AT&T syntax)"),
danghvu6a6947f2013-11-26 22:28:41 -0600119 new Test.platform(Capstone.CS_ARCH_X86, Capstone.CS_MODE_32, hexString2Byte(X86_CODE32), "X86 32 (Intel syntax)"),
120 new Test.platform(Capstone.CS_ARCH_X86, Capstone.CS_MODE_64, hexString2Byte(X86_CODE64), "X86 64 (Intel syntax)"),
121 };
122
123 for (int i=0; i<all_tests.length; i++) {
124 Test.platform test = all_tests[i];
danghvuf3ef6962013-11-27 21:41:17 -0600125 System.out.println(new String(new char[16]).replace("\0", "*"));
danghvu6a6947f2013-11-26 22:28:41 -0600126 System.out.println("Platform: " + test.comment);
danghvuf3ef6962013-11-27 21:41:17 -0600127 System.out.println("Code: " + Test.stringToHex(test.code));
danghvu6a6947f2013-11-26 22:28:41 -0600128 System.out.println("Disasm:");
129
130 cs = new Capstone(test.arch, test.mode);
131 Capstone.cs_insn[] all_ins = cs.disasm(test.code, 0x1000);
132
133 for (int j = 0; j < all_ins.length; j++) {
134 print_ins_detail(all_ins[j]);
135 System.out.println();
136 }
danghvuf3ef6962013-11-27 21:41:17 -0600137
138 System.out.printf("0x%x:\n\n", all_ins[all_ins.length-1].address + all_ins[all_ins.length-1].size);
danghvu6a6947f2013-11-26 22:28:41 -0600139 }
140 }
141
142}