Fix java binding for cs2 branch
This commit is contained in:
parent
4ebdd216f1
commit
03be9f5820
|
@ -28,53 +28,49 @@ public class Capstone {
|
||||||
protected static abstract class OpInfo {}
|
protected static abstract class OpInfo {}
|
||||||
protected static abstract class UnionOpInfo extends Structure {}
|
protected static abstract class UnionOpInfo extends Structure {}
|
||||||
|
|
||||||
protected static int max(int a, int b, int c, int d) {
|
protected static class _cs_insn extends Structure implements Structure.ByReference {
|
||||||
if (a<b) a = b;
|
|
||||||
if (c<d) c = d;
|
|
||||||
if (a<c) a = c;
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static class _cs_insn extends Structure {
|
|
||||||
public int id;
|
public int id;
|
||||||
public long address;
|
public long address;
|
||||||
public short size;
|
public short size;
|
||||||
public byte[] bytes;
|
public byte[] bytes = new byte[16];
|
||||||
public byte[] mnemonic;
|
public byte[] mnemonic = new byte[32];
|
||||||
public byte[] operands;
|
public byte[] operands = new byte[96];
|
||||||
public byte[] regs_read;
|
public _cs_detail.ByReference cs_detail;
|
||||||
public byte regs_read_count;
|
|
||||||
public byte[] regs_write;
|
|
||||||
public byte regs_write_count;
|
|
||||||
public byte[] groups;
|
|
||||||
public byte groups_count;
|
|
||||||
|
|
||||||
public _cs_insn(Pointer p) {
|
public _cs_insn(Pointer p) {
|
||||||
bytes = new byte[16];
|
super(p);
|
||||||
mnemonic = new byte[32];
|
|
||||||
operands = new byte[96];
|
|
||||||
regs_read = new byte[12];
|
|
||||||
regs_write = new byte[20];
|
|
||||||
groups = new byte[8];
|
|
||||||
useMemory(p);
|
|
||||||
read();
|
read();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List getFieldOrder() {
|
public List getFieldOrder() {
|
||||||
return Arrays.asList("id", "address", "size", "bytes", "mnemonic", "operands",
|
return Arrays.asList("id", "address", "size", "bytes", "mnemonic", "operands", "cs_detail");
|
||||||
"regs_read", "regs_read_count",
|
}
|
||||||
"regs_write", "regs_write_count",
|
}
|
||||||
"groups", "groups_count");
|
|
||||||
|
protected static class _cs_detail extends Structure {
|
||||||
|
public static class ByReference extends _cs_detail implements Structure.ByReference {};
|
||||||
|
|
||||||
|
public byte[] regs_read = new byte[12];
|
||||||
|
public byte regs_read_count;
|
||||||
|
public byte[] regs_write = new byte[20];
|
||||||
|
public byte regs_write_count;
|
||||||
|
public byte[] groups = new byte[8];
|
||||||
|
public byte groups_count;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List getFieldOrder() {
|
||||||
|
return Arrays.asList("regs_read", "regs_read_count", "regs_write", "regs_write_count", "groups", "groups_count");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class CsInsn {
|
public static class CsInsn {
|
||||||
public OpInfo operands;
|
public OpInfo operands;
|
||||||
private Pointer ptr_origin;
|
|
||||||
private NativeLong csh;
|
private NativeLong csh;
|
||||||
private CS cs;
|
private CS cs;
|
||||||
|
private _cs_insn raw;
|
||||||
private static int _size = -1;
|
private static int _size = -1;
|
||||||
|
private int arch;
|
||||||
|
|
||||||
public int id;
|
public int id;
|
||||||
public long address;
|
public long address;
|
||||||
|
@ -85,53 +81,84 @@ public class Capstone {
|
||||||
public byte[] regsWrite;
|
public byte[] regsWrite;
|
||||||
public byte[] groups;
|
public byte[] groups;
|
||||||
|
|
||||||
public CsInsn (_cs_insn struct, Pointer _ptr_origin, NativeLong _csh, CS _cs, OpInfo _op_info) {
|
public CsInsn (_cs_insn insn, int _arch, NativeLong _csh, CS _cs) {
|
||||||
id = struct.id;
|
id = insn.id;
|
||||||
address = struct.address;
|
address = insn.address;
|
||||||
size = struct.size;
|
size = insn.size;
|
||||||
mnemonic = new String(struct.mnemonic).replace("\u0000","");
|
mnemonic = new String(insn.mnemonic).replace("\u0000","");
|
||||||
opStr = new String(struct.operands).replace("\u0000","");
|
opStr = new String(insn.operands).replace("\u0000","");
|
||||||
|
|
||||||
regsRead = new byte[struct.regs_read_count];
|
arch = _arch;
|
||||||
|
|
||||||
|
if (insn.cs_detail != null) {
|
||||||
|
regsRead = new byte[insn.cs_detail.regs_read_count];
|
||||||
for (int i=0; i<regsRead.length; i++)
|
for (int i=0; i<regsRead.length; i++)
|
||||||
regsRead[i] = struct.regs_read[i];
|
regsRead[i] = insn.cs_detail.regs_read[i];
|
||||||
regsWrite = new byte[struct.regs_write_count];
|
regsWrite = new byte[insn.cs_detail.regs_write_count];
|
||||||
for (int i=0; i<regsWrite.length; i++)
|
for (int i=0; i<regsWrite.length; i++)
|
||||||
regsWrite[i] = struct.regs_write[i];
|
regsWrite[i] = insn.cs_detail.regs_write[i];
|
||||||
groups = new byte[struct.groups_count];
|
groups = new byte[insn.cs_detail.groups_count];
|
||||||
for (int i=0; i<groups.length; i++)
|
for (int i=0; i<groups.length; i++)
|
||||||
groups[i] = struct.groups[i];
|
groups[i] = insn.cs_detail.groups[i];
|
||||||
operands = _op_info;
|
|
||||||
|
operands = getOptInfo(insn.cs_detail.getPointer());
|
||||||
|
}
|
||||||
|
|
||||||
ptr_origin = _ptr_origin;
|
|
||||||
csh = _csh;
|
csh = _csh;
|
||||||
cs = _cs;
|
cs = _cs;
|
||||||
|
raw = insn;
|
||||||
|
|
||||||
// cache the size so we do not need to recompute the offset everytime
|
// cache the size so we do not need to recompute the offset everytime
|
||||||
if (_size == -1)
|
if (_size == -1)
|
||||||
_size = struct.size() + Arm.UnionOpInfo.getSize();
|
_size = insn.size();
|
||||||
// Arm is the max, so we optimized it here, a more generic way is as follows:
|
|
||||||
// = max( Arm.UnionOpInfo.getSize(), Arm64.UnionOpInfo.getSize(), Mips.UnionOpInfo.getSize(), X86.UnionOpInfo.getSize() );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int size() {
|
protected int size() {
|
||||||
return _size;
|
return _size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private OpInfo getOptInfo(Pointer ptrDetail) {
|
||||||
|
OpInfo op_info = null;
|
||||||
|
UnionOpInfo _op_info = null;
|
||||||
|
Pointer p = ptrDetail.share(48);
|
||||||
|
|
||||||
|
switch (this.arch) {
|
||||||
|
case CS_ARCH_ARM:
|
||||||
|
_op_info = new Arm.UnionOpInfo(p);
|
||||||
|
op_info = new Arm.OpInfo((Arm.UnionOpInfo) _op_info);
|
||||||
|
break;
|
||||||
|
case CS_ARCH_ARM64:
|
||||||
|
_op_info = new Arm64.UnionOpInfo(p);
|
||||||
|
op_info = new Arm64.OpInfo((Arm64.UnionOpInfo) _op_info);
|
||||||
|
break;
|
||||||
|
case CS_ARCH_MIPS:
|
||||||
|
_op_info = new Mips.UnionOpInfo(p);
|
||||||
|
op_info = new Mips.OpInfo((Mips.UnionOpInfo) _op_info);
|
||||||
|
break;
|
||||||
|
case CS_ARCH_X86:
|
||||||
|
_op_info = new X86.UnionOpInfo(p);
|
||||||
|
op_info = new X86.OpInfo((X86.UnionOpInfo) _op_info);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
return op_info;
|
||||||
|
}
|
||||||
|
|
||||||
public int opCount(int type) {
|
public int opCount(int type) {
|
||||||
return cs.cs_op_count(csh, ptr_origin, type);
|
return cs.cs_op_count(csh, raw.getPointer(), type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int opIndex(int type, int index) {
|
public int opIndex(int type, int index) {
|
||||||
return cs.cs_op_index(csh, ptr_origin, type, index);
|
return cs.cs_op_index(csh, raw.getPointer(), type, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean regRead(int reg_id) {
|
public boolean regRead(int reg_id) {
|
||||||
return cs.cs_reg_read(csh, ptr_origin, reg_id) != 0;
|
return cs.cs_reg_read(csh, raw.getPointer(), reg_id) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean regWrite(int reg_id) {
|
public boolean regWrite(int reg_id) {
|
||||||
return cs.cs_reg_write(csh, ptr_origin, reg_id) != 0;
|
return cs.cs_reg_write(csh, raw.getPointer(), reg_id) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int errno() {
|
public int errno() {
|
||||||
|
@ -147,47 +174,17 @@ public class Capstone {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean group(int gid) {
|
public boolean group(int gid) {
|
||||||
return cs.cs_insn_group(csh, ptr_origin, gid) != 0;
|
return cs.cs_insn_group(csh, raw.getPointer(), gid) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private CsInsn fromPointer(Pointer pointer)
|
private CsInsn[] fromArrayRaw(_cs_insn[] arr_raw)
|
||||||
{
|
{
|
||||||
_cs_insn insn = new _cs_insn(pointer);
|
CsInsn[] arr = new CsInsn[arr_raw.length];
|
||||||
OpInfo op_info = null;
|
|
||||||
UnionOpInfo _op_info = null;
|
|
||||||
|
|
||||||
switch (this.arch) {
|
for (int i = 0; i < arr_raw.length; i++) {
|
||||||
case CS_ARCH_ARM:
|
arr[i] = new CsInsn(arr_raw[i], this.arch, ns.csh, cs);
|
||||||
_op_info = new Arm.UnionOpInfo(pointer.share(insn.size()));
|
|
||||||
op_info = new Arm.OpInfo((Arm.UnionOpInfo) _op_info);
|
|
||||||
break;
|
|
||||||
case CS_ARCH_ARM64:
|
|
||||||
_op_info = new Arm64.UnionOpInfo(pointer.share(insn.size()));
|
|
||||||
op_info = new Arm64.OpInfo((Arm64.UnionOpInfo) _op_info);
|
|
||||||
break;
|
|
||||||
case CS_ARCH_MIPS:
|
|
||||||
_op_info = new Mips.UnionOpInfo(pointer.share(insn.size()));
|
|
||||||
op_info = new Mips.OpInfo((Mips.UnionOpInfo) _op_info);
|
|
||||||
break;
|
|
||||||
case CS_ARCH_X86:
|
|
||||||
_op_info = new X86.UnionOpInfo(pointer.share(insn.size()));
|
|
||||||
op_info = new X86.OpInfo((X86.UnionOpInfo) _op_info);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
return new CsInsn(insn, pointer, ns.csh, cs, op_info);
|
|
||||||
}
|
|
||||||
|
|
||||||
private CsInsn[] fromArrayPointer(Pointer pointer, int numberResults)
|
|
||||||
{
|
|
||||||
CsInsn[] arr = new CsInsn[numberResults];
|
|
||||||
int offset = 0;
|
|
||||||
|
|
||||||
for (int i = 0; i < numberResults; i++) {
|
|
||||||
arr[i] = fromPointer(pointer.share(offset));
|
|
||||||
offset += arr[i].size();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return arr;
|
return arr;
|
||||||
|
@ -195,7 +192,7 @@ public class Capstone {
|
||||||
|
|
||||||
private interface CS extends Library {
|
private interface CS extends Library {
|
||||||
public int cs_open(int arch, int mode, NativeLongByReference handle);
|
public int cs_open(int arch, int mode, NativeLongByReference handle);
|
||||||
public NativeLong cs_disasm_dyn(NativeLong handle, byte[] code, NativeLong code_len,
|
public NativeLong cs_disasm_ex(NativeLong handle, byte[] code, NativeLong code_len,
|
||||||
long addr, NativeLong count, PointerByReference insn);
|
long addr, NativeLong count, PointerByReference insn);
|
||||||
public void cs_free(Pointer p);
|
public void cs_free(Pointer p);
|
||||||
public int cs_close(NativeLong handle);
|
public int cs_close(NativeLong handle);
|
||||||
|
@ -311,9 +308,12 @@ public class Capstone {
|
||||||
public CsInsn[] disasm(byte[] code, long address, long count) {
|
public CsInsn[] disasm(byte[] code, long address, long count) {
|
||||||
PointerByReference insnRef = new PointerByReference();
|
PointerByReference insnRef = new PointerByReference();
|
||||||
|
|
||||||
NativeLong c = cs.cs_disasm_dyn(ns.csh, code, new NativeLong(code.length), address, new NativeLong(count), insnRef);
|
NativeLong c = cs.cs_disasm_ex(ns.csh, code, new NativeLong(code.length), address, new NativeLong(count), insnRef);
|
||||||
|
|
||||||
CsInsn[] allInsn = fromArrayPointer(insnRef.getValue(), c.intValue());
|
Pointer p = insnRef.getValue();
|
||||||
|
_cs_insn byref = new _cs_insn(p);
|
||||||
|
|
||||||
|
CsInsn[] allInsn = fromArrayRaw((_cs_insn[]) byref.toArray(c.intValue()));
|
||||||
return allInsn;
|
return allInsn;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue