blob: 60c5061a1c2d2eed8183ba7921ce2b90c4d0a84e [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
danghvu2f666882013-12-01 13:32:16 -060011import static capstone.X86_const.*;
12
danghvu6a6947f2013-11-26 22:28:41 -060013public class TestX86 {
14
15 static byte[] hexString2Byte(String s) {
16 // from http://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java
17 int len = s.length();
18 byte[] data = new byte[len / 2];
19 for (int i = 0; i < len; i += 2) {
20 data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
21 + Character.digit(s.charAt(i+1), 16));
22 }
23 return data;
24 }
25
danghvu6a6947f2013-11-26 22:28:41 -060026 static final String X86_CODE64 = "55488b05b8130000";
danghvu7b088042013-11-27 10:58:31 -060027 static final String X86_CODE16 = "8d4c320801d881c6341200000523010000368b849123010000418d8439896700008d8789670000b4c6";
28 static final String X86_CODE32 = "8d4c320801d881c6341200000523010000368b849123010000418d8439896700008d8789670000b4c6";
danghvu6a6947f2013-11-26 22:28:41 -060029
30 public static Capstone cs;
31
32 private static String hex(int i) {
33 return Integer.toString(i, 16);
34 }
35
36 private static String hex(long i) {
37 return Long.toString(i, 16);
38 }
39
40 private static String array2hex(byte[] arr) {
41 String ret = "";
42 for (int i=0 ;i<arr.length; i++)
43 ret += String.format("0x%02x ", arr[i]);
44 return ret;
45 }
46
47 public static void print_ins_detail(Capstone.cs_insn ins) {
48 System.out.printf("0x%x:\t%s\t%s\n", ins.address, ins.mnemonic, ins.operands);
49
50 X86.OpInfo op_info = (X86.OpInfo) ins.op_info;
51
52 System.out.printf("\tPrefix: %s\n", array2hex(op_info.prefix));
53
danghvu2f666882013-12-01 13:32:16 -060054 if (op_info.segment != X86_REG_INVALID)
danghvu5f495522013-11-27 02:13:32 -060055 System.out.println("\tSegment override: " + cs.reg_name(op_info.segment));
danghvu6a6947f2013-11-26 22:28:41 -060056
57
58 System.out.printf("\tOpcode: %s\n", array2hex(op_info.opcode));
59
60 // print operand's size, address size, displacement size & immediate size
61 System.out.printf("\top_size: %d, addr_size: %d, disp_size: %d, imm_size: %d\n"
62 , op_info.op_size, op_info.addr_size, op_info.disp_size, op_info.imm_size);
63
64 // print modRM byte
65 System.out.printf("\tmodrm: 0x%x\n", op_info.modrm);
66
67 // print displacement value
danghvu7b088042013-11-27 10:58:31 -060068 System.out.printf("\tdisp: 0x%x\n", op_info.disp);
danghvu6a6947f2013-11-26 22:28:41 -060069
70 // SIB is not available in 16-bit mode
danghvuf3ef6962013-11-27 21:41:17 -060071 if ( (cs.mode & Capstone.CS_MODE_16) == 0) {
danghvu35855b52013-11-26 22:42:30 -060072 // print SIB byte
danghvu5f495522013-11-27 02:13:32 -060073 System.out.printf("\tsib: 0x%x\n", op_info.sib);
danghvuf3ef6962013-11-27 21:41:17 -060074 if (op_info.sib != 0)
75 System.out.printf("\tsib_index: %s, sib_scale: %d, sib_base: %s\n",
76 cs.reg_name(op_info.sib_index), op_info.sib_scale, cs.reg_name(op_info.sib_base));
77 }
danghvu6a6947f2013-11-26 22:28:41 -060078
danghvu2f666882013-12-01 13:32:16 -060079 int count = ins.op_count(X86_OP_IMM);
danghvu6a6947f2013-11-26 22:28:41 -060080 if (count > 0) {
81 System.out.printf("\timm_count: %d\n", count);
82 for (int i=0; i<count; i++) {
danghvu2f666882013-12-01 13:32:16 -060083 int index = ins.op_index(X86_OP_IMM, i + 1);
danghvu7b088042013-11-27 10:58:31 -060084 System.out.printf("\t\timms[%d]: 0x%x\n", i+1, (op_info.op[index].value.imm));
danghvu6a6947f2013-11-26 22:28:41 -060085 }
86 }
87
88 if (op_info.op != null) {
89 System.out.printf("\top_count: %d\n", op_info.op.length);
danghvu7b088042013-11-27 10:58:31 -060090 for (int c=0; c<op_info.op.length; c++) {
91 X86.Operand i = (X86.Operand) op_info.op[c];
danghvu6a6947f2013-11-26 22:28:41 -060092 String imm = hex(i.value.imm);
danghvu2f666882013-12-01 13:32:16 -060093 if (i.type == X86_OP_REG)
danghvu35855b52013-11-26 22:42:30 -060094 System.out.printf("\t\toperands[%d].type: REG = %s\n", c, cs.reg_name(i.value.reg));
danghvu2f666882013-12-01 13:32:16 -060095 if (i.type == X86_OP_IMM)
danghvu7b088042013-11-27 10:58:31 -060096 System.out.printf("\t\toperands[%d].type: IMM = 0x%x\n", c, i.value.imm);
danghvu2f666882013-12-01 13:32:16 -060097 if (i.type == X86_OP_FP)
danghvu35855b52013-11-26 22:42:30 -060098 System.out.printf("\t\toperands[%d].type: FP = %f\n", c, i.value.fp);
danghvu2f666882013-12-01 13:32:16 -060099 if (i.type == X86_OP_MEM) {
danghvu6a6947f2013-11-26 22:28:41 -0600100 System.out.printf("\t\toperands[%d].type: MEM\n",c);
101 String base = cs.reg_name(i.value.mem.base);
102 String index = cs.reg_name(i.value.mem.index);
103 if (base != null)
104 System.out.printf("\t\t\toperands[%d].mem.base: REG = %s\n", c, base);
105 if (index != null)
106 System.out.printf("\t\t\toperands[%d].mem.index: REG = %s\n", c, index);
107 if (i.value.mem.scale != 1)
danghvu7b088042013-11-27 10:58:31 -0600108 System.out.printf("\t\t\toperands[%d].mem.scale: %d\n", c, i.value.mem.scale);
danghvu6a6947f2013-11-26 22:28:41 -0600109 if (i.value.mem.disp != 0)
danghvu7b088042013-11-27 10:58:31 -0600110 System.out.printf("\t\t\toperands[%d].mem.disp: 0x%x\n", c, i.value.mem.disp);
danghvu6a6947f2013-11-26 22:28:41 -0600111 }
112 }
113 }
114 }
115
116 public static void main(String argv[]) {
117
118 final Test.platform[] all_tests = {
119 new Test.platform(Capstone.CS_ARCH_X86, Capstone.CS_MODE_16, hexString2Byte(X86_CODE16), "X86 16bit (Intel syntax)"),
danghvu05006912013-12-05 19:33:38 -0600120 new Test.platform(Capstone.CS_ARCH_X86, Capstone.CS_MODE_32, Capstone.CS_OPT_SYNTAX_ATT, hexString2Byte(X86_CODE32), "X86 32 (AT&T syntax)"),
danghvu6a6947f2013-11-26 22:28:41 -0600121 new Test.platform(Capstone.CS_ARCH_X86, Capstone.CS_MODE_32, hexString2Byte(X86_CODE32), "X86 32 (Intel syntax)"),
122 new Test.platform(Capstone.CS_ARCH_X86, Capstone.CS_MODE_64, hexString2Byte(X86_CODE64), "X86 64 (Intel syntax)"),
123 };
124
125 for (int i=0; i<all_tests.length; i++) {
126 Test.platform test = all_tests[i];
danghvuf3ef6962013-11-27 21:41:17 -0600127 System.out.println(new String(new char[16]).replace("\0", "*"));
danghvu6a6947f2013-11-26 22:28:41 -0600128 System.out.println("Platform: " + test.comment);
danghvuf3ef6962013-11-27 21:41:17 -0600129 System.out.println("Code: " + Test.stringToHex(test.code));
danghvu6a6947f2013-11-26 22:28:41 -0600130 System.out.println("Disasm:");
131
132 cs = new Capstone(test.arch, test.mode);
danghvu05006912013-12-05 19:33:38 -0600133 if (test.syntax != 0) {
134 cs.setSyntax(test.syntax);
135 }
danghvu6a6947f2013-11-26 22:28:41 -0600136 Capstone.cs_insn[] all_ins = cs.disasm(test.code, 0x1000);
137
138 for (int j = 0; j < all_ins.length; j++) {
139 print_ins_detail(all_ins[j]);
140 System.out.println();
141 }
danghvuf3ef6962013-11-27 21:41:17 -0600142
143 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 -0600144 }
145 }
146
147}