Skip to content

Commit

Permalink
Java: Support cs_strerror() and cs_regs_access()
Browse files Browse the repository at this point in the history
  • Loading branch information
akirschbaum committed Nov 27, 2016
1 parent b3a71cd commit 7a8c7af
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ This file details the changelog of Capstone.
- Fix a bug where Arm.OpInfo.memBarrier and Arm.OpInfo.op is wrongly
calculated

[ Bindings ]

- Java; add Capstone.strerror() and CsInsn.regsAccess().

---------------------------------
Version 3.0.2: March 11th, 2015

Expand Down
40 changes: 40 additions & 0 deletions bindings/java/capstone/Capstone.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
package capstone;

import com.sun.jna.Library;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.ptr.ByteByReference;
import com.sun.jna.ptr.NativeLongByReference;
import com.sun.jna.Structure;
import com.sun.jna.Union;
Expand Down Expand Up @@ -238,6 +240,38 @@ public boolean group(int gid) {
return cs.cs_insn_group(csh, raw.getPointer(), gid) != 0;
}

public CsRegsAccess regsAccess() {
Memory regsReadMemory = new Memory(64*2);
ByteByReference regsReadCountRef = new ByteByReference();
Memory regsWriteMemory = new Memory(64*2);
ByteByReference regsWriteCountRef = new ByteByReference();

int c = cs.cs_regs_access(csh, raw.getPointer(), regsReadMemory, regsReadCountRef, regsWriteMemory, regsWriteCountRef);
if (c != CS_ERR_OK) {
return null;
}

byte regsReadCount = regsReadCountRef.getValue();
byte regsWriteCount = regsWriteCountRef.getValue();

short[] regsRead = new short[regsReadCount];
regsReadMemory.read(0, regsRead, 0, regsReadCount);

short[] regsWrite = new short[regsWriteCount];
regsWriteMemory.read(0, regsWrite, 0, regsWriteCount);

return new CsRegsAccess(regsRead, regsWrite);
}
}

public static class CsRegsAccess {
public short[] regsRead;
public short[] regsWrite;

public CsRegsAccess(short[] regsRead, short[] regsWrite) {
this.regsRead = regsRead;
this.regsWrite = regsWrite;
}
}

private CsInsn[] fromArrayRaw(_cs_insn[] arr_raw) {
Expand Down Expand Up @@ -270,6 +304,8 @@ public NativeLong cs_disasm(NativeLong handle, byte[] code, NativeLong code_len,
public int cs_errno(NativeLong csh);
public int cs_version(IntByReference major, IntByReference minor);
public boolean cs_support(int query);
public String cs_strerror(int code);
public int cs_regs_access(NativeLong handle, Pointer insn, Pointer regs_read, ByteByReference regs_read_count, Pointer regs_write, ByteByReference regs_write_count);
}

// Capstone API version
Expand Down Expand Up @@ -478,4 +514,8 @@ public CsInsn[] disasm(byte[] code, long address, long count) {

return allInsn;
}

public String strerror(int code) {
return cs.cs_strerror(code);
}
}

0 comments on commit 7a8c7af

Please sign in to comment.