• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Capstone Java binding
2 // By Nguyen Anh Quynh & Dang Hoang Vu,  2013
3 
4 package capstone;
5 
6 import com.sun.jna.Structure;
7 import com.sun.jna.Union;
8 
9 import java.util.List;
10 import java.util.Arrays;
11 
12 import static capstone.X86_const.*;
13 
14 public class X86 {
15 
16   public static class MemType extends Structure {
17     public int segment;
18     public int base;
19     public int index;
20     public int scale;
21     public long disp;
22 
23     @Override
getFieldOrder()24     public List getFieldOrder() {
25       return Arrays.asList("segment", "base", "index", "scale", "disp");
26     }
27   }
28 
29   public static class OpValue extends Union {
30     public int reg;
31     public long imm;
32     public double fp;
33     public MemType mem;
34 
35     @Override
getFieldOrder()36     public List getFieldOrder() {
37       return Arrays.asList("reg", "imm", "fp", "mem");
38     }
39   }
40 
41   public static class Operand extends Structure {
42     public int type;
43     public OpValue value;
44     public byte size;
45     public int avx_bcast;
46     public boolean avx_zero_opmask;
47 
read()48     public void read() {
49       super.read();
50       if (type == X86_OP_MEM)
51         value.setType(MemType.class);
52       if (type == X86_OP_FP)
53         value.setType(Double.TYPE);
54       if (type == X86_OP_IMM)
55         value.setType(Long.TYPE);
56       if (type == X86_OP_REG)
57         value.setType(Integer.TYPE);
58       if (type == X86_OP_INVALID)
59         return;
60       readField("value");
61     }
62 
63     @Override
getFieldOrder()64     public List getFieldOrder() {
65       return Arrays.asList("type", "value", "size", "avx_bcast", "avx_zero_opmask");
66     }
67   }
68 
69   public static class UnionOpInfo extends Capstone.UnionOpInfo {
70     public byte [] prefix;
71     public byte [] opcode;
72     public byte rex;
73     public byte addr_size;
74     public byte modrm;
75     public byte sib;
76     public int disp;
77     public int sib_index;
78     public byte sib_scale;
79     public int sib_base;
80     public int sse_cc;
81     public int avx_cc;
82     public byte avx_sae;
83     public int avx_rm;
84 
85     public byte op_count;
86 
87     public Operand [] op;
88 
UnionOpInfo()89     public UnionOpInfo() {
90       op = new Operand[8];
91       opcode = new byte[4];
92       prefix = new byte[4];
93     }
94 
95     @Override
getFieldOrder()96     public List getFieldOrder() {
97       return Arrays.asList("prefix", "opcode", "rex", "addr_size",
98           "modrm", "sib", "disp", "sib_index", "sib_scale", "sib_base", "sse_cc", "avx_cc", "avx_sae", "avx_rm", "op_count", "op");
99     }
100   }
101 
102   public static class OpInfo extends Capstone.OpInfo {
103     public byte [] prefix;
104     public byte [] opcode;
105     public byte opSize;
106     public byte rex;
107     public byte addrSize;
108     public byte dispSize;
109     public byte immSize;
110     public byte modrm;
111     public byte sib;
112     public int disp;
113     public int sibIndex;
114     public byte sibScale;
115     public int sibBase;
116     public int sseCC;
117     public int avxCC;
118     public boolean avxSae;
119     public int avxRm;
120 
121     public Operand[] op;
122 
OpInfo(UnionOpInfo e)123     public OpInfo(UnionOpInfo e) {
124       prefix = e.prefix;
125       opcode = e.opcode;
126       rex = e.rex;
127       addrSize = e.addr_size;
128       modrm = e.modrm;
129       sib = e.sib;
130       disp = e.disp;
131       sibIndex = e.sib_index;
132       sibScale = e.sib_scale;
133       sibBase = e.sib_base;
134       sseCC = e.sse_cc;
135       avxCC = e.avx_cc;
136       avxSae = e.avx_sae > 0;
137       avxRm = e.avx_rm;
138       op = new Operand[e.op_count];
139       for (int i=0; i<e.op_count; i++)
140         op[i] = e.op[i];
141     }
142   }
143 }
144