blob: bcfcfd22528acfa2b4cdd99d94824453eb3dad17 [file] [log] [blame]
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001/* Capstone Disassembler Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */
3
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08004#include <stddef.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08005#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <capstone.h>
9
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080010#include "utils.h"
Nguyen Anh Quynhc7404072014-01-05 11:35:47 +080011#include "MCRegisterInfo.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080012
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080013cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080014cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
15void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080016
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080017// we need this trick to enable module constructors in static lib
danghvu701b8502014-01-09 11:06:44 +070018extern void enable_arm();
19extern void enable_arm64();
20extern void enable_mips();
21extern void enable_x86();
22extern void enable_powerpc();
23
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080024void enable_construct()
25{
danghvub33bd2c2014-01-09 12:22:56 +070026#ifdef CAPSTONE_HAS_ARM
27 enable_arm();
28#endif
29#ifdef CAPSTONE_HAS_ARM64
30 enable_arm64();
31#endif
32#ifdef CAPSTONE_HAS_MIPS
33 enable_mips();
34#endif
35#ifdef CAPSTONE_HAS_X86
36 enable_x86();
37#endif
38#ifdef CAPSTONE_HAS_POWERPC
39 enable_powerpc();
40#endif
41}
42
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080043unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080044
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080045#ifdef USE_SYS_DYN_MEM
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080046cs_malloc_t cs_mem_malloc = malloc;
47cs_calloc_t cs_mem_calloc = calloc;
48cs_realloc_t cs_mem_realloc = realloc;
49cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080050cs_vsnprintf_t cs_vsnprintf = vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080051#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080052cs_malloc_t cs_mem_malloc = NULL;
53cs_calloc_t cs_mem_calloc = NULL;
54cs_realloc_t cs_mem_realloc = NULL;
55cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080056cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080057#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080058
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080059unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080060{
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080061 if (major != NULL && minor != NULL) {
62 *major = CS_API_MAJOR;
63 *minor = CS_API_MINOR;
64 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080065
66 return (CS_API_MAJOR << 8) + CS_API_MINOR;
67}
68
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +080069bool cs_support(int arch)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080070{
71 if (arch == CS_ARCH_ALL)
72 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080073 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
74 (1 << CS_ARCH_PPC));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080075
76 return all_arch & (1 << arch);
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080077}
78
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080079cs_err cs_errno(csh handle)
80{
81 if (!handle)
82 return CS_ERR_CSH;
83
84 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
85
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +080086 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080087}
88
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +080089const char *cs_strerror(cs_err code)
90{
91 switch(code) {
92 default:
93 return "Unknown error code";
94 case CS_ERR_OK:
95 return "OK (CS_ERR_OK)";
96 case CS_ERR_MEM:
97 return "Out of memory (CS_ERR_MEM)";
98 case CS_ERR_ARCH:
99 return "Invalid architecture (CS_ERR_ARCH)";
100 case CS_ERR_HANDLE:
101 return "Invalid handle (CS_ERR_HANDLE)";
102 case CS_ERR_CSH:
103 return "Invalid csh (CS_ERR_CSH)";
104 case CS_ERR_MODE:
105 return "Invalid mode (CS_ERR_MODE)";
106 case CS_ERR_OPTION:
107 return "Invalid option (CS_ERR_OPTION)";
108 case CS_ERR_DETAIL:
109 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800110 case CS_ERR_MEMSETUP:
111 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800112 }
113}
114
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800115cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
116{
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800117 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800118 // Error: before cs_open(), dynamic memory management must be initialized
119 // with cs_option(CS_OPT_MEM)
120 return CS_ERR_MEMSETUP;
121
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800122 if (arch < CS_ARCH_MAX && arch_init[arch]) {
123 cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800124
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800125 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800126 if (!ud) {
127 // memory insufficient
128 return CS_ERR_MEM;
129 }
130
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800131 ud->errnum = CS_ERR_OK;
132 ud->arch = arch;
133 ud->mode = mode;
134 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800135 // by default, do not break instruction into details
136 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800137
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800138 arch_init[ud->arch](ud);
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800139
140 *handle = (uintptr_t)ud;
141
142 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800143 } else {
144 *handle = 0;
145 return CS_ERR_ARCH;
146 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800147}
148
149cs_err cs_close(csh handle)
150{
151 if (!handle)
152 return CS_ERR_CSH;
153
154 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
155
156 switch (ud->arch) {
157 case CS_ARCH_X86:
158 break;
159 case CS_ARCH_ARM:
160 case CS_ARCH_MIPS:
161 case CS_ARCH_ARM64:
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800162 case CS_ARCH_PPC:
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800163 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800164 break;
165 default: // unsupported architecture
166 return CS_ERR_HANDLE;
167 }
168
Nguyen Anh Quynh9fac5122014-01-07 10:56:04 +0800169 // arch_destroy[ud->arch](ud);
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800170
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800171 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800172 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800173 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800174
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800175 return CS_ERR_OK;
176}
177
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800178#define MIN(x, y) ((x) < (y) ? (x) : (y))
179
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800180// fill insn with mnemonic & operands info
181static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800182 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800183{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800184 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800185 // avoiding copy insn->detail
186 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800187
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800188 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
189 // copy from @regs_read until @arm
190 memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read),
191 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
192 // then copy from @arm until end
193 memcpy((void *)(insn->detail) + offsetof(cs_detail, arm), (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm),
194 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800195 } else {
196 insn->address = mci->address;
197 insn->size = mci->insn_size;
198 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800199
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800200 // fill the instruction bytes
201 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
202
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800203 // map internal instruction opcode to public insn ID
204 if (handle->insn_id)
Nguyen Anh Quynh1acfd0b2014-01-06 10:56:59 +0800205 handle->insn_id(handle, insn, MCInst_getOpcode(mci));
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800206
207 // alias instruction might have ID saved in OpcodePub
208 if (MCInst_getOpcodePub(mci))
209 insn->id = MCInst_getOpcodePub(mci);
210
211 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800212 if (postprinter)
213 postprinter((csh)handle, insn, buffer);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800214
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800215 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800216 // find first space or tab
217 char *sp = buffer;
218 for (sp = buffer; *sp; sp++)
219 if (*sp == ' '||*sp == '\t')
220 break;
221 if (*sp) {
222 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800223 // find the next non-space char
224 sp++;
225 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
226 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800227 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
228 } else
229 insn->op_str[0] = '\0';
230
231 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
232 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
233}
234
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800235cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800236{
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800237 // cs_option() can be called with NULL handle just for CS_OPT_MEM
238 // This is supposed to be executed before all other APIs (even cs_open())
239 if (type == CS_OPT_MEM) {
240 cs_opt_mem *mem = (cs_opt_mem *)value;
241
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800242 cs_mem_malloc = mem->malloc;
243 cs_mem_calloc = mem->calloc;
244 cs_mem_realloc = mem->realloc;
245 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800246 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800247
248 return CS_ERR_OK;
249 }
250
danghvu2b192962013-12-19 22:40:28 -0600251 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
252 if (!handle)
253 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800254
danghvu0b6ea042013-12-19 23:07:26 -0600255 if (type == CS_OPT_DETAIL) {
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800256 handle->detail = value;
257 return CS_ERR_OK;
258 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800259
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800260 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800261}
262
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800263// dynamicly allocate memory to contain disasm insn
264// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800265size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800266{
267 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
268 MCInst mci;
269 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800270 size_t c = 0, f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800271 cs_insn insn_cache[64];
272 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800273 size_t total_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800274
275 if (!handle) {
276 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800277 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800278 return 0;
279 }
280
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800281 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800282
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800283 memset(insn_cache, 0, sizeof(insn_cache));
284
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800285 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600286 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800287 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800288
289 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
290 if (r) {
291 SStream ss;
292 SStream_Init(&ss);
293
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800294 // relative branches need to know the address & size of current insn
295 mci.insn_size = insn_size;
296 mci.address = offset;
297
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800298 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800299 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800300 mci.flat_insn.address = offset;
301 mci.flat_insn.size = insn_size;
302 // allocate memory for @detail pointer
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800303 insn_cache[f].detail = cs_mem_calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800304 }
305
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800306 handle->printer(&mci, &ss, handle->printer_info);
307
Joxean114df0e2013-12-04 07:11:32 +0100308 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800309
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800310 f++;
311
312 if (f == ARR_SIZE(insn_cache)) {
313 // resize total to contain newly disasm insns
314 total_size += sizeof(insn_cache);
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800315 void *tmp = cs_mem_realloc(total, total_size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800316 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800317 cs_mem_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800318 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800319 return 0;
320 }
321
322 total = tmp;
323 memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache));
324 // reset f back to 0
325 f = 0;
326 }
327
328 c++;
329 buffer += insn_size;
330 size -= insn_size;
331 offset += insn_size;
332
333 if (count > 0 && c == count)
334 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800335 } else {
336 // encounter a broken instruction
337 // XXX: TODO: JOXEAN continue here
338 break;
339 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800340 }
341
342 if (f) {
343 // resize total to contain newly disasm insns
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800344 void *tmp = cs_mem_realloc(total, total_size + f * sizeof(insn_cache[0]));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800345 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800346 cs_mem_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800347 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800348 return 0;
349 }
350
351 total = tmp;
352 memcpy(total + total_size, insn_cache, f * sizeof(insn_cache[0]));
353 }
354
355 *insn = total;
356
357 return c;
358}
359
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800360void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800361{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800362 size_t i;
363
364 // free all detail pointers
365 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800366 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800367
368 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800369 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800370}
371
372// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100373const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800374{
375 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
376
377 if (!handle || handle->reg_name == NULL) {
378 return NULL;
379 }
380
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800381 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800382}
383
pancakef0e4eed2013-12-11 22:14:42 +0100384const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800385{
386 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
387
388 if (!handle || handle->insn_name == NULL) {
389 return NULL;
390 }
391
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800392 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800393}
394
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800395static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800396{
397 int i;
398
399 for (i = 0; i < max; i++) {
400 if (arr[i] == id)
401 return true;
402 }
403
404 return false;
405}
406
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800407bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800408{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800409 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800410 return false;
411
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800412 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800413 if (!handle->detail) {
414 handle->errnum = CS_ERR_DETAIL;
415 return false;
416 }
417
418 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800419}
420
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800421bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800422{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800423 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800424 return false;
425
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800426 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800427 if (!handle->detail) {
428 handle->errnum = CS_ERR_DETAIL;
429 return false;
430 }
431
432 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800433}
434
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800435bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800436{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800437 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800438 return false;
439
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800440 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800441 if (!handle->detail) {
442 handle->errnum = CS_ERR_DETAIL;
443 return false;
444 }
445
446 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800447}
448
449int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
450{
451 if (!ud)
452 return -1;
453
454 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800455 if (!handle->detail) {
456 handle->errnum = CS_ERR_DETAIL;
457 return -1;
458 }
459
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800460 unsigned int count = 0, i;
461
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800462 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800463
464 switch (handle->arch) {
465 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800466 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800467 return -1;
468 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800469 for (i = 0; i < insn->detail->arm.op_count; i++)
470 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800471 count++;
472 break;
473 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800474 for (i = 0; i < insn->detail->arm64.op_count; i++)
475 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800476 count++;
477 break;
478 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800479 for (i = 0; i < insn->detail->x86.op_count; i++)
480 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800481 count++;
482 break;
483 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800484 for (i = 0; i < insn->detail->mips.op_count; i++)
485 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800486 count++;
487 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800488 case CS_ARCH_PPC:
489 for (i = 0; i < insn->detail->ppc.op_count; i++)
490 if (insn->detail->ppc.operands[i].type == op_type)
491 count++;
492 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800493 }
494
495 return count;
496}
497
498int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
499 unsigned int post)
500{
501 if (!ud)
502 return -1;
503
504 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800505 if (!handle->detail) {
506 handle->errnum = CS_ERR_DETAIL;
507 return -1;
508 }
509
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800510 unsigned int count = 0, i;
511
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800512 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800513
514 switch (handle->arch) {
515 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800516 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800517 return -1;
518 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800519 for (i = 0; i < insn->detail->arm.op_count; i++) {
520 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800521 count++;
522 if (count == post)
523 return i;
524 }
525 break;
526 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800527 for (i = 0; i < insn->detail->arm64.op_count; i++) {
528 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800529 count++;
530 if (count == post)
531 return i;
532 }
533 break;
534 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800535 for (i = 0; i < insn->detail->x86.op_count; i++) {
536 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800537 count++;
538 if (count == post)
539 return i;
540 }
541 break;
542 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800543 for (i = 0; i < insn->detail->mips.op_count; i++) {
544 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800545 count++;
546 if (count == post)
547 return i;
548 }
549 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800550 case CS_ARCH_PPC:
551 for (i = 0; i < insn->detail->ppc.op_count; i++) {
552 if (insn->detail->ppc.operands[i].type == op_type)
553 count++;
554 if (count == post)
555 return i;
556 }
557 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800558 }
559
560 return -1;
561}