Merge pull request #40 from danghvu/master

Naming convention and size of read/write_reg + groups
_v3_old
Nguyen Anh Quynh 11 years ago
commit bc0a110e99
  1. 4
      bindings/java/Test.java
  2. 30
      bindings/java/TestArm.java
  3. 30
      bindings/java/TestArm64.java
  4. 20
      bindings/java/TestMips.java
  5. 48
      bindings/java/TestX86.java
  6. 4
      bindings/java/capstone/Arm.java
  7. 4
      bindings/java/capstone/Arm64.java
  8. 68
      bindings/java/capstone/Capstone.java
  9. 28
      bindings/java/capstone/X86.java

@ -119,11 +119,11 @@ public class Test {
if (platforms[j].syntax != 0)
cs.setSyntax(platforms[j].syntax);
Capstone.cs_insn[] all_insn = cs.disasm(platforms[j].code, 0x1000);
Capstone.CsInsn[] all_insn = cs.disasm(platforms[j].code, 0x1000);
for (int i = 0; i < all_insn.length; i++) {
System.out.println(String.format("0x%x: \t%s\t%s", all_insn[i].address,
all_insn[i].mnemonic, all_insn[i].operands));
all_insn[i].mnemonic, all_insn[i].opStr));
}
System.out.printf("0x%x:\n\n", all_insn[all_insn.length-1].address + all_insn[all_insn.length-1].size);
}

@ -34,18 +34,18 @@ public class TestArm {
return Long.toString(i, 16);
}
public static void print_ins_detail(Capstone.cs_insn ins) {
System.out.printf("0x%x:\t%s\t%s\n", ins.address, ins.mnemonic, ins.operands);
public static void print_ins_detail(Capstone.CsInsn ins) {
System.out.printf("0x%x:\t%s\t%s\n", ins.address, ins.mnemonic, ins.opStr);
Arm.OpInfo op_info = (Arm.OpInfo) ins.op_info;
Arm.OpInfo operands = (Arm.OpInfo) ins.operands;
if (op_info.op.length != 0) {
System.out.printf("\top_count: %d\n", op_info.op.length);
for (int c=0; c<op_info.op.length; c++) {
Arm.Operand i = (Arm.Operand) op_info.op[c];
if (operands.op.length != 0) {
System.out.printf("\top_count: %d\n", operands.op.length);
for (int c=0; c<operands.op.length; c++) {
Arm.Operand i = (Arm.Operand) operands.op[c];
String imm = hex(i.value.imm);
if (i.type == ARM_OP_REG)
System.out.printf("\t\toperands[%d].type: REG = %s\n", c, cs.reg_name(i.value.reg));
System.out.printf("\t\toperands[%d].type: REG = %s\n", c, ins.regName(i.value.reg));
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_OP_PIMM)
@ -56,8 +56,8 @@ public class TestArm {
System.out.printf("\t\toperands[%d].type: FP = %f\n", c, i.value.fp);
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);
String base = ins.regName(i.value.mem.base);
String index = ins.regName(i.value.mem.index);
if (base != null)
System.out.printf("\t\t\toperands[%d].mem.base: REG = %s\n", c, base);
if (index != null)
@ -70,14 +70,14 @@ public class TestArm {
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)
if (operands.writeback)
System.out.println("\tWrite-back: True");
if (op_info.update_flags)
if (operands.updateFlags)
System.out.println("\tUpdate-flags: True");
if (op_info.cc != ARM_CC_AL && op_info.cc != ARM_CC_INVALID)
System.out.printf("\tCode condition: %d\n", op_info.cc);
if (operands.cc != ARM_CC_AL && operands.cc != ARM_CC_INVALID)
System.out.printf("\tCode condition: %d\n", operands.cc);
}
}
@ -98,7 +98,7 @@ public class TestArm {
System.out.println("Disasm:");
cs = new Capstone(test.arch, test.mode);
Capstone.cs_insn[] all_ins = cs.disasm(test.code, 0x1000);
Capstone.CsInsn[] all_ins = cs.disasm(test.code, 0x1000);
for (int j = 0; j < all_ins.length; j++) {
print_ins_detail(all_ins[j]);

@ -31,18 +31,18 @@ public class TestArm64 {
return Long.toString(i, 16);
}
public static void print_ins_detail(Capstone.cs_insn ins) {
System.out.printf("0x%x:\t%s\t%s\n", ins.address, ins.mnemonic, ins.operands);
public static void print_ins_detail(Capstone.CsInsn ins) {
System.out.printf("0x%x:\t%s\t%s\n", ins.address, ins.mnemonic, ins.opStr);
Arm64.OpInfo op_info = (Arm64.OpInfo) ins.op_info;
Arm64.OpInfo operands = (Arm64.OpInfo) ins.operands;
if (op_info.op.length != 0) {
System.out.printf("\top_count: %d\n", op_info.op.length);
for (int c=0; c<op_info.op.length; c++) {
Arm64.Operand i = (Arm64.Operand) op_info.op[c];
if (operands.op.length != 0) {
System.out.printf("\top_count: %d\n", operands.op.length);
for (int c=0; c<operands.op.length; c++) {
Arm64.Operand i = (Arm64.Operand) operands.op[c];
String imm = hex(i.value.imm);
if (i.type == ARM64_OP_REG)
System.out.printf("\t\toperands[%d].type: REG = %s\n", c, cs.reg_name(i.value.reg));
System.out.printf("\t\toperands[%d].type: REG = %s\n", c, ins.regName(i.value.reg));
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_OP_CIMM)
@ -51,8 +51,8 @@ public class TestArm64 {
System.out.printf("\t\toperands[%d].type: FP = %f\n", c, i.value.fp);
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);
String base = ins.regName(i.value.mem.base);
String index = ins.regName(i.value.mem.index);
if (base != null)
System.out.printf("\t\t\toperands[%d].mem.base: REG = %s\n", c, base);
if (index != null)
@ -67,14 +67,14 @@ public class TestArm64 {
}
}
if (op_info.writeback)
if (operands.writeback)
System.out.println("\tWrite-back: True");
if (op_info.update_flags)
if (operands.updateFlags)
System.out.println("\tUpdate-flags: True");
if (op_info.cc != ARM64_CC_AL && op_info.cc != ARM64_CC_INVALID)
System.out.printf("\tCode condition: %d\n", op_info.cc);
if (operands.cc != ARM64_CC_AL && operands.cc != ARM64_CC_INVALID)
System.out.printf("\tCode condition: %d\n", operands.cc);
}
@ -92,7 +92,7 @@ public class TestArm64 {
System.out.println("Disasm:");
cs = new Capstone(test.arch, test.mode);
Capstone.cs_insn[] all_ins = cs.disasm(test.code, 0x2c);
Capstone.CsInsn[] all_ins = cs.disasm(test.code, 0x2c);
for (int j = 0; j < all_ins.length; j++) {
print_ins_detail(all_ins[j]);

@ -32,23 +32,23 @@ public class TestMips {
return Long.toString(i, 16);
}
public static void print_ins_detail(Capstone.cs_insn ins) {
System.out.printf("0x%x:\t%s\t%s\n", ins.address, ins.mnemonic, ins.operands);
public static void print_ins_detail(Capstone.CsInsn ins) {
System.out.printf("0x%x:\t%s\t%s\n", ins.address, ins.mnemonic, ins.opStr);
Mips.OpInfo op_info = (Mips.OpInfo) ins.op_info;
Mips.OpInfo operands = (Mips.OpInfo) ins.operands;
if (op_info.op.length != 0) {
System.out.printf("\top_count: %d\n", op_info.op.length);
for (int c=0; c<op_info.op.length; c++) {
Mips.Operand i = (Mips.Operand) op_info.op[c];
if (operands.op.length != 0) {
System.out.printf("\top_count: %d\n", operands.op.length);
for (int c=0; c<operands.op.length; c++) {
Mips.Operand i = (Mips.Operand) operands.op[c];
String imm = hex(i.value.imm);
if (i.type == MIPS_OP_REG)
System.out.printf("\t\toperands[%d].type: REG = %s\n", c, cs.reg_name(i.value.reg));
System.out.printf("\t\toperands[%d].type: REG = %s\n", c, ins.regName(i.value.reg));
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_OP_MEM) {
System.out.printf("\t\toperands[%d].type: MEM\n",c);
String base = cs.reg_name(i.value.mem.base);
String base = ins.regName(i.value.mem.base);
if (base != null)
System.out.printf("\t\t\toperands[%d].mem.base: REG = %s\n", c, base);
if (i.value.mem.disp != 0)
@ -73,7 +73,7 @@ public class TestMips {
System.out.println("Disasm:");
cs = new Capstone(test.arch, test.mode);
Capstone.cs_insn[] all_ins = cs.disasm(test.code, 0x1000);
Capstone.CsInsn[] all_ins = cs.disasm(test.code, 0x1000);
for (int j = 0; j < all_ins.length; j++) {
print_ins_detail(all_ins[j]);

@ -40,62 +40,62 @@ public class TestX86 {
return ret;
}
public static void print_ins_detail(Capstone.cs_insn ins) {
System.out.printf("0x%x:\t%s\t%s\n", ins.address, ins.mnemonic, ins.operands);
public static void print_ins_detail(Capstone.CsInsn ins) {
System.out.printf("0x%x:\t%s\t%s\n", ins.address, ins.mnemonic, ins.opStr);
X86.OpInfo op_info = (X86.OpInfo) ins.op_info;
X86.OpInfo operands = (X86.OpInfo) ins.operands;
System.out.printf("\tPrefix: %s\n", array2hex(op_info.prefix));
System.out.printf("\tPrefix: %s\n", array2hex(operands.prefix));
if (op_info.segment != X86_REG_INVALID)
System.out.println("\tSegment override: " + cs.reg_name(op_info.segment));
if (operands.segment != X86_REG_INVALID)
System.out.println("\tSegment override: " + ins.regName(operands.segment));
System.out.printf("\tOpcode: %s\n", array2hex(op_info.opcode));
System.out.printf("\tOpcode: %s\n", array2hex(operands.opcode));
// print operand's size, address size, displacement size & immediate size
System.out.printf("\top_size: %d, addr_size: %d, disp_size: %d, imm_size: %d\n"
, op_info.op_size, op_info.addr_size, op_info.disp_size, op_info.imm_size);
, operands.opSize, operands.addrSize, operands.dispSize, operands.immSize);
// print modRM byte
System.out.printf("\tmodrm: 0x%x\n", op_info.modrm);
System.out.printf("\tmodrm: 0x%x\n", operands.modrm);
// print displacement value
System.out.printf("\tdisp: 0x%x\n", op_info.disp);
System.out.printf("\tdisp: 0x%x\n", operands.disp);
// SIB is not available in 16-bit mode
if ( (cs.mode & Capstone.CS_MODE_16) == 0) {
// print SIB byte
System.out.printf("\tsib: 0x%x\n", op_info.sib);
if (op_info.sib != 0)
System.out.printf("\tsib: 0x%x\n", operands.sib);
if (operands.sib != 0)
System.out.printf("\tsib_index: %s, sib_scale: %d, sib_base: %s\n",
cs.reg_name(op_info.sib_index), op_info.sib_scale, cs.reg_name(op_info.sib_base));
ins.regName(operands.sibIndex), operands.sibScale, ins.regName(operands.sibBase));
}
int count = ins.op_count(X86_OP_IMM);
int count = ins.opCount(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_OP_IMM, i + 1);
System.out.printf("\t\timms[%d]: 0x%x\n", i+1, (op_info.op[index].value.imm));
int index = ins.opIndex(X86_OP_IMM, i + 1);
System.out.printf("\t\timms[%d]: 0x%x\n", i+1, (operands.op[index].value.imm));
}
}
if (op_info.op.length != 0) {
System.out.printf("\top_count: %d\n", op_info.op.length);
for (int c=0; c<op_info.op.length; c++) {
X86.Operand i = (X86.Operand) op_info.op[c];
if (operands.op.length != 0) {
System.out.printf("\top_count: %d\n", operands.op.length);
for (int c=0; c<operands.op.length; c++) {
X86.Operand i = (X86.Operand) operands.op[c];
String imm = hex(i.value.imm);
if (i.type == X86_OP_REG)
System.out.printf("\t\toperands[%d].type: REG = %s\n", c, cs.reg_name(i.value.reg));
System.out.printf("\t\toperands[%d].type: REG = %s\n", c, ins.regName(i.value.reg));
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_OP_FP)
System.out.printf("\t\toperands[%d].type: FP = %f\n", c, i.value.fp);
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);
String base = ins.regName(i.value.mem.base);
String index = ins.regName(i.value.mem.index);
if (base != null)
System.out.printf("\t\t\toperands[%d].mem.base: REG = %s\n", c, base);
if (index != null)
@ -129,7 +129,7 @@ public class TestX86 {
if (test.syntax != 0) {
cs.setSyntax(test.syntax);
}
Capstone.cs_insn[] all_ins = cs.disasm(test.code, 0x1000);
Capstone.CsInsn[] all_ins = cs.disasm(test.code, 0x1000);
for (int j = 0; j < all_ins.length; j++) {
print_ins_detail(all_ins[j]);

@ -117,13 +117,13 @@ public class Arm {
public static class OpInfo extends Capstone.OpInfo {
public int cc;
public boolean update_flags;
public boolean updateFlags;
public boolean writeback;
public Operand [] op = null;
public OpInfo(UnionOpInfo op_info) {
cc = op_info.cc;
update_flags = (op_info._update_flags > 0);
updateFlags = (op_info._update_flags > 0);
writeback = (op_info._writeback > 0);
op = op_info.op;
}

@ -115,13 +115,13 @@ public class Arm64 {
public static class OpInfo extends Capstone.OpInfo {
public int cc;
public boolean update_flags;
public boolean updateFlags;
public boolean writeback;
public Operand [] op = null;
public OpInfo(UnionOpInfo op_info) {
cc = op_info.cc;
update_flags = (op_info._update_flags > 0);
updateFlags = (op_info._update_flags > 0);
writeback = (op_info._writeback > 0);
op = op_info.op;
}

@ -69,35 +69,41 @@ public class Capstone {
}
}
public static class cs_insn {
public OpInfo op_info;
public Pointer ptr_origin;
public NativeLong csh;
public static class CsInsn {
public OpInfo operands;
private Pointer ptr_origin;
private NativeLong csh;
private CS cs;
private static int _size = -1;
public int id;
public long address;
public short size;
public String mnemonic;
public String operands;
public int[] regs_read;
public int[] regs_write;
public String opStr;
public int[] regsRead;
public int[] regsWrite;
public int[] groups;
private CS cs;
private static int _size = -1;
public cs_insn (_cs_insn struct, Pointer _ptr_origin, NativeLong _csh, CS _cs, OpInfo _op_info) {
public CsInsn (_cs_insn struct, Pointer _ptr_origin, NativeLong _csh, CS _cs, OpInfo _op_info) {
id = struct.id;
address = struct.address;
size = struct.size;
mnemonic = new String(struct.mnemonic).replace("\u0000","");
operands = new String(struct.operands).replace("\u0000","");
regs_read = struct.regs_read;
regs_write = struct.regs_write;
groups = struct.groups;
opStr = new String(struct.operands).replace("\u0000","");
regsRead = new int[struct.regs_read_count];
for (int i=0; i<regsRead.length; i++)
regsRead[i] = struct.regs_read[i];
regsWrite = new int[struct.regs_write_count];
for (int i=0; i<regsWrite.length; i++)
regsWrite[i] = struct.regs_write[i];
groups = new int[struct.groups_count];
for (int i=0; i<groups.length; i++)
groups[i] = struct.groups[i];
operands = _op_info;
ptr_origin = _ptr_origin;
op_info = _op_info;
csh = _csh;
cs = _cs;
@ -112,19 +118,19 @@ public class Capstone {
return _size;
}
public int op_count(int type) {
public int opCount(int type) {
return cs.cs_op_count(csh, ptr_origin, type);
}
public int op_index(int type, int index) {
public int opIndex(int type, int index) {
return cs.cs_op_index(csh, ptr_origin, type, index);
}
public boolean reg_read(int reg_id) {
public boolean regRead(int reg_id) {
return cs.cs_reg_read(csh, ptr_origin, reg_id) != 0;
}
public boolean reg_write(int reg_id) {
public boolean regWrite(int reg_id) {
return cs.cs_reg_write(csh, ptr_origin, reg_id) != 0;
}
@ -132,11 +138,11 @@ public class Capstone {
return cs.cs_errno(csh);
}
public String reg_name(int reg_id) {
public String regName(int reg_id) {
return cs.cs_reg_name(csh, reg_id);
}
public String insn_name() {
public String insnName() {
return cs.cs_insn_name(csh, id);
}
@ -146,7 +152,7 @@ public class Capstone {
}
private cs_insn fromPointer(Pointer pointer)
private CsInsn fromPointer(Pointer pointer)
{
_cs_insn insn = new _cs_insn(pointer);
OpInfo op_info = null;
@ -171,12 +177,12 @@ public class Capstone {
break;
default:
}
return new cs_insn(insn, pointer, ns.csh, cs, op_info);
return new CsInsn(insn, pointer, ns.csh, cs, op_info);
}
private cs_insn[] fromArrayPointer(Pointer pointer, int numberResults)
private CsInsn[] fromArrayPointer(Pointer pointer, int numberResults)
{
cs_insn[] arr = new cs_insn[numberResults];
CsInsn[] arr = new CsInsn[numberResults];
int offset = 0;
for (int i = 0; i < numberResults; i++) {
@ -281,7 +287,7 @@ public class Capstone {
}
}
public String reg_name(int reg) {
public String getRegName(int reg) {
return cs.cs_reg_name(ns.csh, reg);
}
@ -289,17 +295,17 @@ public class Capstone {
cs.cs_close(ns.csh);
}
public cs_insn[] disasm(byte[] code, long address) {
public CsInsn[] disasm(byte[] code, long address) {
return disasm(code, address, 0);
}
public cs_insn[] disasm(byte[] code, long address, long count) {
public CsInsn[] disasm(byte[] code, long address, long count) {
PointerByReference insnRef = new PointerByReference();
NativeLong c = cs.cs_disasm_dyn(ns.csh, code, new NativeLong(code.length), address, new NativeLong(count), insnRef);
cs_insn[] all_insn = fromArrayPointer(insnRef.getValue(), c.intValue());
return all_insn;
CsInsn[] allInsn = fromArrayPointer(insnRef.getValue(), c.intValue());
return allInsn;
}
}

@ -112,16 +112,16 @@ public class X86 {
public byte [] prefix;
public int segment;
public byte [] opcode;
public byte op_size;
public byte addr_size;
public byte disp_size;
public byte imm_size;
public byte opSize;
public byte addrSize;
public byte dispSize;
public byte immSize;
public byte modrm;
public byte sib;
public int disp;
public int sib_index;
public byte sib_scale;
public int sib_base;
public int sibIndex;
public byte sibScale;
public int sibBase;
public Operand[] op;
@ -129,16 +129,16 @@ public class X86 {
prefix = e.prefix;
segment = e.segment;
opcode = e.opcode;
op_size = e.op_size;
addr_size = e.addr_size;
disp_size = e.disp_size;
imm_size = e.imm_size;
opSize = e.op_size;
addrSize = e.addr_size;
dispSize = e.disp_size;
immSize = e.imm_size;
modrm = e.modrm;
sib = e.sib;
disp = e.disp;
sib_index = e.sib_index;
sib_scale = e.sib_scale;
sib_base = e.sib_base;
sibIndex = e.sib_index;
sibScale = e.sib_scale;
sibBase = e.sib_base;
op = new Operand[e.op_count];
for (int i=0; i<e.op_count; i++)
op[i] = e.op[i];

Loading…
Cancel
Save