Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions bindings/java/capstone/Arm.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static class MemType extends Structure {
public int base;
public int index;
public int scale;
public long disp;
public int disp;

@Override
public List getFieldOrder() {
Expand All @@ -67,7 +67,7 @@ public List getFieldOrder() {

public static class OpValue extends Union {
public int reg;
public long imm;
public int imm;
public double fp;
public MemType mem;

Expand Down Expand Up @@ -99,7 +99,7 @@ public void read() {
if (type == ARM_OP_FP)
value.setType(Double.TYPE);
if (type == ARM_OP_PIMM || type == ARM_OP_IMM || type == ARM_OP_CIMM)
value.setType(Long.TYPE);
value.setType(Integer.TYPE);
if (type == ARM_OP_REG)
value.setType(Integer.TYPE);
if (type == ARM_OP_INVALID)
Expand All @@ -122,12 +122,12 @@ public static class UnionOpInfo extends Capstone.UnionOpInfo {

public Operand [] op;

public UnionOpInfo(){
op = new Operand[32];
public UnionOpInfo(){
op = new Operand[20];
}

public UnionOpInfo(Pointer p){
op = new Operand[32];
op = new Operand[20];
useMemory(p);
read();
}
Expand All @@ -142,6 +142,7 @@ public void read() {
readField("_update_flags");
readField("_writeback");
readField("op_count");
if (op_count == 0) return;
op = new Operand[op_count];
readField("op");
}
Expand Down
13 changes: 6 additions & 7 deletions bindings/java/capstone/Arm64.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class Arm64 {
public static class MemType extends Structure {
public int base;
public int index;
public long disp;
public int disp;

@Override
public List getFieldOrder() {
Expand All @@ -72,7 +72,7 @@ public List getFieldOrder() {

public static class OpValue extends Union {
public int reg;
public long imm;
public int imm;
public double fp;
public MemType mem;

Expand Down Expand Up @@ -104,9 +104,7 @@ public void read() {
value.setType(MemType.class);
if (type == ARM64_OP_FP)
value.setType(Double.TYPE);
if (type == ARM64_OP_IMM || type == ARM64_OP_CIMM)
value.setType(Long.TYPE);
if (type == ARM64_OP_REG)
if (type == ARM64_OP_IMM || type == ARM64_OP_CIMM || type == ARM64_OP_REG)
value.setType(Integer.TYPE);
if (type == ARM64_OP_INVALID)
return;
Expand All @@ -130,11 +128,11 @@ public static class UnionOpInfo extends Capstone.UnionOpInfo {
public Operand [] op;

public UnionOpInfo() {
op = new Operand[32];
op = new Operand[8];
}

public UnionOpInfo(Pointer p) {
op = new Operand[32];
op = new Operand[8];
useMemory(p);
read();
}
Expand All @@ -148,6 +146,7 @@ public void read() {
readField("_update_flags");
readField("_writeback");
readField("op_count");
if (op_count == 0) return;
op = new Operand[op_count];
readField("op");
}
Expand Down
73 changes: 41 additions & 32 deletions bindings/java/capstone/Capstone.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.ptr.NativeLongByReference;
import com.sun.jna.Structure;
import com.sun.jna.Union;
import com.sun.jna.ptr.LongByReference;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.PointerByReference;
import com.sun.jna.ptr.IntByReference;
Expand All @@ -26,12 +27,15 @@ protected static abstract class OpInfo {}
protected static abstract class UnionOpInfo extends Structure {}

protected static int max(int a, int b, int c, int d) {
return Math.max(Math.max(Math.max(a,b),c),d);
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 long address;
public NativeLong address;
public short size;
public byte[] mnemonic = new byte[32];
public byte[] operands = new byte[96];
Expand All @@ -40,13 +44,13 @@ protected static class _cs_insn extends Structure {
public int[] groups = new int[8];

public _cs_insn(Pointer p) {
mnemonic = new byte[32];
operands = new byte[96];
regs_read = new int[32];
regs_write = new int[32];
groups = new int[8];
useMemory(p);
read();
mnemonic = new byte[32];
operands = new byte[96];
regs_read = new int[32];
regs_write = new int[32];
groups = new int[8];
useMemory(p);
read();
}

@Override
Expand All @@ -58,7 +62,7 @@ public List getFieldOrder() {
public static class cs_insn {
public OpInfo op_info;
public Pointer ptr_origin;
public long csh;
public NativeLong csh;

public int id;
public long address;
Expand All @@ -70,11 +74,11 @@ public static class cs_insn {
public int[] groups;

private CS cs;
private int _size;
private static int _size = -1;

public cs_insn (_cs_insn struct, Pointer _ptr_origin, long _csh, CS _cs, OpInfo _op_info) {
public cs_insn (_cs_insn struct, Pointer _ptr_origin, NativeLong _csh, CS _cs, OpInfo _op_info) {
id = struct.id;
address = struct.address;
address = struct.address.longValue();
size = struct.size;
mnemonic = new String(struct.mnemonic).replace("\u0000","");
operands = new String(struct.operands).replace("\u0000","");
Expand All @@ -86,7 +90,12 @@ public cs_insn (_cs_insn struct, Pointer _ptr_origin, long _csh, CS _cs, OpInfo
op_info = _op_info;
csh = _csh;
cs = _cs;
_size = struct.size() + max( Arm.UnionOpInfo.getSize(), Arm64.UnionOpInfo.getSize(), Mips.UnionOpInfo.getSize(), X86.UnionOpInfo.getSize() );

// cache the size so we do not need to recompute the offset everytime
if (_size == -1)
_size = struct.size() + Arm.UnionOpInfo.getSize();
// 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() {
Expand Down Expand Up @@ -169,21 +178,21 @@ private cs_insn[] fromArrayPointer(Pointer pointer, int numberResults)
}

private interface CS extends Library {
public int cs_open(int arch, int mode, LongByReference handle);
public long cs_disasm_dyn(long handle, byte[] code, long code_len,
long addr, long count, PointerByReference insn);
public int cs_open(int arch, int mode, NativeLongByReference handle);
public NativeLong cs_disasm_dyn(NativeLong handle, byte[] code, NativeLong code_len,
NativeLong addr, NativeLong count, PointerByReference insn);
public void cs_free(Pointer p);
public int cs_close(long handle);
public String cs_reg_name(long csh, int id);
public int cs_op_count(long csh, Pointer insn, int type);
public int cs_op_index(long csh, Pointer insn, int type, int index);

public String cs_insn_name(long csh, int id);
public byte cs_insn_group(long csh, Pointer insn, int id);
public byte cs_reg_read(long csh, Pointer insn, int id);
public byte cs_reg_write(long csh, Pointer insn, int id);
public int cs_close(NativeLong handle);
public String cs_reg_name(NativeLong csh, int id);
public int cs_op_count(NativeLong csh, Pointer insn, int type);
public int cs_op_index(NativeLong csh, Pointer insn, int type, int index);

public String cs_insn_name(NativeLong csh, int id);
public byte cs_insn_group(NativeLong csh, Pointer insn, int id);
public byte cs_reg_read(NativeLong csh, Pointer insn, int id);
public byte cs_reg_write(NativeLong csh, Pointer insn, int id);
public void cs_version(IntByReference major, IntByReference minor);
public int cs_errno(long csh);
public int cs_errno(NativeLong csh);
}

public static final int CS_ARCH_ARM = 0;
Expand All @@ -210,7 +219,7 @@ public long cs_disasm_dyn(long handle, byte[] code, long code_len,
public static final int CS_ERR_MODE = 5; // Invalid/unsupported mode


private long csh;
private NativeLong csh;
private PointerByReference insnRef;
private CS cs;

Expand All @@ -219,7 +228,7 @@ public Capstone(int arch, int mode)
this.arch = arch;
this.mode = mode;
cs = (CS)Native.loadLibrary("capstone", CS.class);
LongByReference handleref = new LongByReference();
NativeLongByReference handleref = new NativeLongByReference();
if (cs.cs_open(arch, mode, handleref) != CS_ERR_OK) {
throw new RuntimeException("ERROR: Wrong arch or mode");
}
Expand All @@ -241,10 +250,10 @@ public cs_insn[] disasm(byte[] code, long address) {
public cs_insn[] disasm(byte[] code, long address, long count) {
insnRef = new PointerByReference();

long c = cs.cs_disasm_dyn(csh, code, code.length, address, count, insnRef);
NativeLong c = cs.cs_disasm_dyn(csh, code, new NativeLong(code.length), new NativeLong(address), new NativeLong(count), insnRef);

Pointer p = insnRef.getValue();
cs_insn[] all_insn = fromArrayPointer(p, (int)c);
cs_insn[] all_insn = fromArrayPointer(p, c.intValue());
return all_insn;
}
}
Expand Down
3 changes: 2 additions & 1 deletion bindings/java/capstone/Mips.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public List getFieldOrder() {
}

public static class UnionOpInfo extends Capstone.UnionOpInfo {
public short op_count;
public byte op_count;
public Operand [] op;

public UnionOpInfo() {
Expand All @@ -82,6 +82,7 @@ public static int getSize() {

public void read() {
readField("op_count");
if (op_count ==0) return;
op = new Operand[op_count];
readField("op");
}
Expand Down
2 changes: 1 addition & 1 deletion bindings/java/capstone/X86.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static class UnionOpInfo extends Capstone.UnionOpInfo {
public byte sib_scale;
public int sib_base;

public int op_count;
public char op_count;

public Operand [] op;

Expand Down
11 changes: 9 additions & 2 deletions bindings/python/test_arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ def to_x(s):
while x[0] == '0': x = x[1:]
return x

def to_x_32(s):
from struct import pack
if not s: return '0'
x = pack(">i", s).encode('hex')
while x[0] == '0': x = x[1:]
return x

### Test class cs
def test_class():
def print_insn_detail(insn):
Expand All @@ -40,7 +47,7 @@ def print_insn_detail(insn):
if i.type == ARM_OP_REG:
print("\t\toperands[%u].type: REG = %s" %(c, insn.reg_name(i.value.reg)))
if i.type == ARM_OP_IMM:
print("\t\toperands[%u].type: IMM = 0x%s" %(c, to_x(i.value.imm)))
print("\t\toperands[%u].type: IMM = 0x%s" %(c, to_x_32(i.value.imm)))
if i.type == ARM_OP_PIMM:
print("\t\toperands[%u].type: P-IMM = %u" %(c, i.value.imm))
if i.type == ARM_OP_CIMM:
Expand All @@ -60,7 +67,7 @@ def print_insn_detail(insn):
%(c, i.value.mem.scale))
if i.value.mem.disp != 0:
print("\t\t\toperands[%u].mem.disp: 0x%s" \
%(c, to_x(i.value.mem.disp)))
%(c, to_x_32(i.value.mem.disp)))

if i.shift.type != ARM_SFT_INVALID and i.shift.value:
print("\t\t\tShift: type = %u, value = %u\n" \
Expand Down