danghvu | 6a6947f | 2013-11-26 22:28:41 -0600 | [diff] [blame^] | 1 | // Capstone Java binding |
| 2 | // By Nguyen Anh Quynh & Dang Hoang Vu, 2013 |
| 3 | |
| 4 | import com.sun.jna.Native; |
| 5 | import com.sun.jna.Memory; |
| 6 | import com.sun.jna.Pointer; |
| 7 | |
| 8 | public class TestX86 { |
| 9 | |
| 10 | static byte[] hexString2Byte(String s) { |
| 11 | // from http://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java |
| 12 | int len = s.length(); |
| 13 | byte[] data = new byte[len / 2]; |
| 14 | for (int i = 0; i < len; i += 2) { |
| 15 | data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) |
| 16 | + Character.digit(s.charAt(i+1), 16)); |
| 17 | } |
| 18 | return data; |
| 19 | } |
| 20 | |
| 21 | static final String X86_CODE16 = "8d4c320801d881c6341200000523010000368b84912301000041a113486d3a"; |
| 22 | static final String X86_CODE32 = "8d4c320801d881c6341200000523010000368b84912301000041a113486d3a8d0534120000"; |
| 23 | static final String X86_CODE64 = "55488b05b8130000"; |
| 24 | |
| 25 | public static Capstone cs; |
| 26 | |
| 27 | private static String hex(int i) { |
| 28 | return Integer.toString(i, 16); |
| 29 | } |
| 30 | |
| 31 | private static String hex(long i) { |
| 32 | return Long.toString(i, 16); |
| 33 | } |
| 34 | |
| 35 | private static String array2hex(byte[] arr) { |
| 36 | String ret = ""; |
| 37 | for (int i=0 ;i<arr.length; i++) |
| 38 | ret += String.format("0x%02x ", arr[i]); |
| 39 | return ret; |
| 40 | } |
| 41 | |
| 42 | public static void print_ins_detail(Capstone.cs_insn ins) { |
| 43 | System.out.printf("0x%x:\t%s\t%s\n", ins.address, ins.mnemonic, ins.operands); |
| 44 | |
| 45 | X86.OpInfo op_info = (X86.OpInfo) ins.op_info; |
| 46 | |
| 47 | System.out.printf("\tPrefix: %s\n", array2hex(op_info.prefix)); |
| 48 | |
| 49 | if (op_info.segment != X86.X86_REG_INVALID) |
| 50 | System.out.println("\tSegment override:" + cs.reg_name(op_info.segment)); |
| 51 | |
| 52 | |
| 53 | System.out.printf("\tOpcode: %s\n", array2hex(op_info.opcode)); |
| 54 | |
| 55 | // print operand's size, address size, displacement size & immediate size |
| 56 | System.out.printf("\top_size: %d, addr_size: %d, disp_size: %d, imm_size: %d\n" |
| 57 | , op_info.op_size, op_info.addr_size, op_info.disp_size, op_info.imm_size); |
| 58 | |
| 59 | // print modRM byte |
| 60 | System.out.printf("\tmodrm: 0x%x\n", op_info.modrm); |
| 61 | |
| 62 | // print displacement value |
| 63 | System.out.printf("\tdisp: 0x%s\n", hex(op_info.disp)); |
| 64 | |
| 65 | // SIB is not available in 16-bit mode |
| 66 | if ( (cs.mode & Capstone.CS_MODE_16) == 0) |
| 67 | // print SIB byte |
| 68 | System.out.printf("\tsib: 0x%s\n", hex(op_info.sib)); |
| 69 | |
| 70 | int count = ins.op_count(X86.X86_OP_IMM); |
| 71 | if (count > 0) { |
| 72 | System.out.printf("\timm_count: %d\n", count); |
| 73 | for (int i=0; i<count; i++) { |
| 74 | int index = ins.op_index(X86.X86_OP_IMM, i + 1); |
| 75 | System.out.printf("\t\timms[%d] = 0x%x\n", i+1, (op_info.op[index].value.imm)); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if (op_info.op != null) { |
| 80 | System.out.printf("\top_count: %d\n", op_info.op.length); |
| 81 | for (int c=1; c<op_info.op.length+1; c++) { |
| 82 | X86.Operand i = (X86.Operand) op_info.op[c-1]; |
| 83 | String imm = hex(i.value.imm); |
| 84 | if (i.type == X86.X86_OP_REG) |
| 85 | System.out.printf("\t\toperands[%d].type: REG = %s\n", c, cs.reg_name(i.value.reg)); |
| 86 | if (i.type == X86.X86_OP_IMM) |
| 87 | System.out.printf("\t\toperands[%d].type: IMM = %s\n", c, imm); |
| 88 | if (i.type == X86.X86_OP_FP) |
| 89 | System.out.printf("\t\toperands[%d].type: FP = %f\n", c, i.value.fp); |
| 90 | if (i.type == X86.X86_OP_MEM) { |
| 91 | System.out.printf("\t\toperands[%d].type: MEM\n",c); |
| 92 | String base = cs.reg_name(i.value.mem.base); |
| 93 | String index = cs.reg_name(i.value.mem.index); |
| 94 | if (base != null) |
| 95 | System.out.printf("\t\t\toperands[%d].mem.base: REG = %s\n", c, base); |
| 96 | if (index != null) |
| 97 | System.out.printf("\t\t\toperands[%d].mem.index: REG = %s\n", c, index); |
| 98 | if (i.value.mem.scale != 1) |
| 99 | System.out.printf("\t\t\toperands[%d].mem.scale: 0x%s\n", c, hex(i.value.mem.scale)); |
| 100 | if (i.value.mem.disp != 0) |
| 101 | System.out.printf("\t\t\toperands[%d].mem.disp: 0x%s\n", c, hex(i.value.mem.disp)); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | public static void main(String argv[]) { |
| 108 | |
| 109 | final Test.platform[] all_tests = { |
| 110 | new Test.platform(Capstone.CS_ARCH_X86, Capstone.CS_MODE_16, hexString2Byte(X86_CODE16), "X86 16bit (Intel syntax)"), |
| 111 | new Test.platform(Capstone.CS_ARCH_X86, Capstone.CS_MODE_32 + Capstone.CS_MODE_SYNTAX_ATT, hexString2Byte(X86_CODE32), "X86 32bit (ATT syntax)"), |
| 112 | new Test.platform(Capstone.CS_ARCH_X86, Capstone.CS_MODE_32, hexString2Byte(X86_CODE32), "X86 32 (Intel syntax)"), |
| 113 | new Test.platform(Capstone.CS_ARCH_X86, Capstone.CS_MODE_64, hexString2Byte(X86_CODE64), "X86 64 (Intel syntax)"), |
| 114 | }; |
| 115 | |
| 116 | for (int i=0; i<all_tests.length; i++) { |
| 117 | Test.platform test = all_tests[i]; |
| 118 | System.out.println(new String(new char[30]).replace("\0", "*")); |
| 119 | System.out.println("Platform: " + test.comment); |
| 120 | System.out.println("Disasm:"); |
| 121 | |
| 122 | cs = new Capstone(test.arch, test.mode); |
| 123 | Capstone.cs_insn[] all_ins = cs.disasm(test.code, 0x1000); |
| 124 | |
| 125 | for (int j = 0; j < all_ins.length; j++) { |
| 126 | print_ins_detail(all_ins[j]); |
| 127 | System.out.println(); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | } |