1# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
2
3import ctypes
4from . import copy_ctypes_list
5from .x86_const import *
6
7# define the API
8class X86OpMem(ctypes.Structure):
9    _fields_ = (
10        ('segment', ctypes.c_uint),
11        ('base', ctypes.c_uint),
12        ('index', ctypes.c_uint),
13        ('scale', ctypes.c_int),
14        ('disp', ctypes.c_int64),
15    )
16
17class X86OpValue(ctypes.Union):
18    _fields_ = (
19        ('reg', ctypes.c_uint),
20        ('imm', ctypes.c_int64),
21        ('mem', X86OpMem),
22    )
23
24class X86Op(ctypes.Structure):
25    _fields_ = (
26        ('type', ctypes.c_uint),
27        ('value', X86OpValue),
28        ('size', ctypes.c_uint8),
29        ('access', ctypes.c_uint8),
30        ('avx_bcast', ctypes.c_uint),
31        ('avx_zero_opmask', ctypes.c_bool),
32    )
33
34    @property
35    def imm(self):
36        return self.value.imm
37
38    @property
39    def reg(self):
40        return self.value.reg
41
42    @property
43    def mem(self):
44        return self.value.mem
45
46
47class CsX86Encoding(ctypes.Structure):
48    _fields_ = (
49        ('modrm_offset', ctypes.c_uint8),
50        ('disp_offset', ctypes.c_uint8),
51        ('disp_size', ctypes.c_uint8),
52        ('imm_offset', ctypes.c_uint8),
53        ('imm_size', ctypes.c_uint8),
54    )
55
56class CsX86(ctypes.Structure):
57    _fields_ = (
58        ('prefix', ctypes.c_uint8 * 4),
59        ('opcode', ctypes.c_uint8 * 4),
60        ('rex', ctypes.c_uint8),
61        ('addr_size', ctypes.c_uint8),
62        ('modrm', ctypes.c_uint8),
63        ('sib', ctypes.c_uint8),
64        ('disp', ctypes.c_int64),
65        ('sib_index', ctypes.c_uint),
66        ('sib_scale', ctypes.c_int8),
67        ('sib_base', ctypes.c_uint),
68        ('xop_cc', ctypes.c_uint),
69        ('sse_cc', ctypes.c_uint),
70        ('avx_cc', ctypes.c_uint),
71        ('avx_sae', ctypes.c_bool),
72        ('avx_rm', ctypes.c_uint),
73        ('eflags', ctypes.c_uint64),
74        ('op_count', ctypes.c_uint8),
75        ('operands', X86Op * 8),
76        ('encoding', CsX86Encoding),
77    )
78
79def get_arch_info(a):
80    return (a.prefix[:], a.opcode[:], a.rex, a.addr_size, \
81            a.modrm, a.sib, a.disp, a.sib_index, a.sib_scale, \
82            a.sib_base, a.xop_cc, a.sse_cc, a.avx_cc, a.avx_sae, a.avx_rm, a.eflags, \
83            a.encoding.modrm_offset, a.encoding.disp_offset, a.encoding.disp_size, a.encoding.imm_offset, a.encoding.imm_size, \
84            copy_ctypes_list(a.operands[:a.op_count]))
85
86