blob: 194b56b44216b18c4ef0e3e55b37e1c31670a9ea [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 Quynh59492c22014-01-05 23:41:31 +080050#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080051cs_malloc_t cs_mem_malloc = NULL;
52cs_calloc_t cs_mem_calloc = NULL;
53cs_realloc_t cs_mem_realloc = NULL;
54cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080055#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080056
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080057unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080058{
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080059 if (major != NULL && minor != NULL) {
60 *major = CS_API_MAJOR;
61 *minor = CS_API_MINOR;
62 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080063
64 return (CS_API_MAJOR << 8) + CS_API_MINOR;
65}
66
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +080067bool cs_support(int arch)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080068{
69 if (arch == CS_ARCH_ALL)
70 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080071 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
72 (1 << CS_ARCH_PPC));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080073
74 return all_arch & (1 << arch);
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080075}
76
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080077cs_err cs_errno(csh handle)
78{
79 if (!handle)
80 return CS_ERR_CSH;
81
82 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
83
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +080084 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080085}
86
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +080087const char *cs_strerror(cs_err code)
88{
89 switch(code) {
90 default:
91 return "Unknown error code";
92 case CS_ERR_OK:
93 return "OK (CS_ERR_OK)";
94 case CS_ERR_MEM:
95 return "Out of memory (CS_ERR_MEM)";
96 case CS_ERR_ARCH:
97 return "Invalid architecture (CS_ERR_ARCH)";
98 case CS_ERR_HANDLE:
99 return "Invalid handle (CS_ERR_HANDLE)";
100 case CS_ERR_CSH:
101 return "Invalid csh (CS_ERR_CSH)";
102 case CS_ERR_MODE:
103 return "Invalid mode (CS_ERR_MODE)";
104 case CS_ERR_OPTION:
105 return "Invalid option (CS_ERR_OPTION)";
106 case CS_ERR_DETAIL:
107 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800108 case CS_ERR_MEMSETUP:
109 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800110 }
111}
112
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800113cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
114{
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800115 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800116 // Error: before cs_open(), dynamic memory management must be initialized
117 // with cs_option(CS_OPT_MEM)
118 return CS_ERR_MEMSETUP;
119
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800120 if (arch < CS_ARCH_MAX && arch_init[arch]) {
121 cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800122
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800123 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800124 if (!ud) {
125 // memory insufficient
126 return CS_ERR_MEM;
127 }
128
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800129 ud->errnum = CS_ERR_OK;
130 ud->arch = arch;
131 ud->mode = mode;
132 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800133 // by default, do not break instruction into details
134 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800135
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800136 arch_init[ud->arch](ud);
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800137
138 *handle = (uintptr_t)ud;
139
140 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800141 } else {
142 *handle = 0;
143 return CS_ERR_ARCH;
144 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800145}
146
147cs_err cs_close(csh handle)
148{
149 if (!handle)
150 return CS_ERR_CSH;
151
152 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
153
154 switch (ud->arch) {
155 case CS_ARCH_X86:
156 break;
157 case CS_ARCH_ARM:
158 case CS_ARCH_MIPS:
159 case CS_ARCH_ARM64:
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800160 case CS_ARCH_PPC:
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800161 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800162 break;
163 default: // unsupported architecture
164 return CS_ERR_HANDLE;
165 }
166
Nguyen Anh Quynh9fac5122014-01-07 10:56:04 +0800167 // arch_destroy[ud->arch](ud);
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800168
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800169 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800170 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800171 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800172
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800173 return CS_ERR_OK;
174}
175
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800176#define MIN(x, y) ((x) < (y) ? (x) : (y))
177
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800178// fill insn with mnemonic & operands info
179static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800180 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800181{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800182 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800183 // avoiding copy insn->detail
184 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800185
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800186 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
187 // copy from @regs_read until @arm
188 memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read),
189 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
190 // then copy from @arm until end
191 memcpy((void *)(insn->detail) + offsetof(cs_detail, arm), (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm),
192 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800193 } else {
194 insn->address = mci->address;
195 insn->size = mci->insn_size;
196 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800197
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800198 // fill the instruction bytes
199 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
200
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800201 // map internal instruction opcode to public insn ID
202 if (handle->insn_id)
Nguyen Anh Quynh1acfd0b2014-01-06 10:56:59 +0800203 handle->insn_id(handle, insn, MCInst_getOpcode(mci));
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800204
205 // alias instruction might have ID saved in OpcodePub
206 if (MCInst_getOpcodePub(mci))
207 insn->id = MCInst_getOpcodePub(mci);
208
209 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800210 if (postprinter)
211 postprinter((csh)handle, insn, buffer);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800212
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800213 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800214 // find first space or tab
215 char *sp = buffer;
216 for (sp = buffer; *sp; sp++)
217 if (*sp == ' '||*sp == '\t')
218 break;
219 if (*sp) {
220 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800221 // find the next non-space char
222 sp++;
223 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
224 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800225 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
226 } else
227 insn->op_str[0] = '\0';
228
229 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
230 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
231}
232
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800233cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800234{
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800235 // cs_option() can be called with NULL handle just for CS_OPT_MEM
236 // This is supposed to be executed before all other APIs (even cs_open())
237 if (type == CS_OPT_MEM) {
238 cs_opt_mem *mem = (cs_opt_mem *)value;
239
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800240 cs_mem_malloc = mem->malloc;
241 cs_mem_calloc = mem->calloc;
242 cs_mem_realloc = mem->realloc;
243 cs_mem_free = mem->free;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800244
245 return CS_ERR_OK;
246 }
247
danghvu2b192962013-12-19 22:40:28 -0600248 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
249 if (!handle)
250 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800251
danghvu0b6ea042013-12-19 23:07:26 -0600252 if (type == CS_OPT_DETAIL) {
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800253 handle->detail = value;
254 return CS_ERR_OK;
255 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800256
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800257 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800258}
259
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800260// dynamicly allocate memory to contain disasm insn
261// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800262size_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 +0800263{
264 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
265 MCInst mci;
266 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800267 size_t c = 0, f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800268 cs_insn insn_cache[64];
269 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800270 size_t total_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800271
272 if (!handle) {
273 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800274 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800275 return 0;
276 }
277
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800278 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800279
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800280 memset(insn_cache, 0, sizeof(insn_cache));
281
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800282 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600283 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800284 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800285
286 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
287 if (r) {
288 SStream ss;
289 SStream_Init(&ss);
290
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800291 // relative branches need to know the address & size of current insn
292 mci.insn_size = insn_size;
293 mci.address = offset;
294
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800295 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800296 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800297 mci.flat_insn.address = offset;
298 mci.flat_insn.size = insn_size;
299 // allocate memory for @detail pointer
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800300 insn_cache[f].detail = cs_mem_calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800301 }
302
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800303 handle->printer(&mci, &ss, handle->printer_info);
304
Joxean114df0e2013-12-04 07:11:32 +0100305 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800306
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800307 f++;
308
309 if (f == ARR_SIZE(insn_cache)) {
310 // resize total to contain newly disasm insns
311 total_size += sizeof(insn_cache);
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800312 void *tmp = cs_mem_realloc(total, total_size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800313 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800314 cs_mem_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800315 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800316 return 0;
317 }
318
319 total = tmp;
320 memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache));
321 // reset f back to 0
322 f = 0;
323 }
324
325 c++;
326 buffer += insn_size;
327 size -= insn_size;
328 offset += insn_size;
329
330 if (count > 0 && c == count)
331 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800332 } else {
333 // encounter a broken instruction
334 // XXX: TODO: JOXEAN continue here
335 break;
336 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800337 }
338
339 if (f) {
340 // resize total to contain newly disasm insns
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800341 void *tmp = cs_mem_realloc(total, total_size + f * sizeof(insn_cache[0]));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800342 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800343 cs_mem_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800344 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800345 return 0;
346 }
347
348 total = tmp;
349 memcpy(total + total_size, insn_cache, f * sizeof(insn_cache[0]));
350 }
351
352 *insn = total;
353
354 return c;
355}
356
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800357void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800358{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800359 size_t i;
360
361 // free all detail pointers
362 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800363 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800364
365 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800366 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800367}
368
369// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100370const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800371{
372 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
373
374 if (!handle || handle->reg_name == NULL) {
375 return NULL;
376 }
377
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800378 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800379}
380
pancakef0e4eed2013-12-11 22:14:42 +0100381const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800382{
383 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
384
385 if (!handle || handle->insn_name == NULL) {
386 return NULL;
387 }
388
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800389 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800390}
391
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800392static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800393{
394 int i;
395
396 for (i = 0; i < max; i++) {
397 if (arr[i] == id)
398 return true;
399 }
400
401 return false;
402}
403
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800404bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800405{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800406 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800407 return false;
408
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800409 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
410
411 if (!handle->detail) {
412 handle->errnum = CS_ERR_DETAIL;
413 return false;
414 }
415
416 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800417}
418
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800419bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800420{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800421 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800422 return false;
423
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800424 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
425
426 if (!handle->detail) {
427 handle->errnum = CS_ERR_DETAIL;
428 return false;
429 }
430
431 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800432}
433
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800434bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800435{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800436 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800437 return false;
438
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800439 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
440
441 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;
455 unsigned int count = 0, i;
456
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800457 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800458
459 switch (handle->arch) {
460 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800461 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800462 return -1;
463 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800464 for (i = 0; i < insn->detail->arm.op_count; i++)
465 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800466 count++;
467 break;
468 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800469 for (i = 0; i < insn->detail->arm64.op_count; i++)
470 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800471 count++;
472 break;
473 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800474 for (i = 0; i < insn->detail->x86.op_count; i++)
475 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800476 count++;
477 break;
478 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800479 for (i = 0; i < insn->detail->mips.op_count; i++)
480 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800481 count++;
482 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800483 case CS_ARCH_PPC:
484 for (i = 0; i < insn->detail->ppc.op_count; i++)
485 if (insn->detail->ppc.operands[i].type == op_type)
486 count++;
487 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800488 }
489
490 return count;
491}
492
493int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
494 unsigned int post)
495{
496 if (!ud)
497 return -1;
498
499 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
500 unsigned int count = 0, i;
501
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800502 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800503
504 switch (handle->arch) {
505 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800506 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800507 return -1;
508 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800509 for (i = 0; i < insn->detail->arm.op_count; i++) {
510 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800511 count++;
512 if (count == post)
513 return i;
514 }
515 break;
516 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800517 for (i = 0; i < insn->detail->arm64.op_count; i++) {
518 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800519 count++;
520 if (count == post)
521 return i;
522 }
523 break;
524 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800525 for (i = 0; i < insn->detail->x86.op_count; i++) {
526 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800527 count++;
528 if (count == post)
529 return i;
530 }
531 break;
532 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800533 for (i = 0; i < insn->detail->mips.op_count; i++) {
534 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800535 count++;
536 if (count == post)
537 return i;
538 }
539 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800540 case CS_ARCH_PPC:
541 for (i = 0; i < insn->detail->ppc.op_count; i++) {
542 if (insn->detail->ppc.operands[i].type == op_type)
543 count++;
544 if (count == post)
545 return i;
546 }
547 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800548 }
549
550 return -1;
551}