blob: e41d21180fb63fc88ed7c41ae02baed568fd69dc [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
danghvu701b8502014-01-09 11:06:44 +070017extern void enable_arm();
18extern void enable_arm64();
19extern void enable_mips();
20extern void enable_x86();
21extern void enable_powerpc();
22
danghvub33bd2c2014-01-09 12:22:56 +070023void enable_construct() {
24#ifdef CAPSTONE_HAS_ARM
25 enable_arm();
26#endif
27#ifdef CAPSTONE_HAS_ARM64
28 enable_arm64();
29#endif
30#ifdef CAPSTONE_HAS_MIPS
31 enable_mips();
32#endif
33#ifdef CAPSTONE_HAS_X86
34 enable_x86();
35#endif
36#ifdef CAPSTONE_HAS_POWERPC
37 enable_powerpc();
38#endif
39}
40
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080041unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080042
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080043#ifdef USE_SYS_DYN_MEM
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +080044malloc_t my_malloc = malloc;
45calloc_t my_calloc = calloc;
46realloc_t my_realloc = realloc;
47free_t my_free = free;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080048#else
49malloc_t my_malloc = NULL;
50calloc_t my_calloc = NULL;
51realloc_t my_realloc = NULL;
52free_t my_free = NULL;
53#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080054
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080055unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080056{
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080057 if (major != NULL && minor != NULL) {
58 *major = CS_API_MAJOR;
59 *minor = CS_API_MINOR;
60 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080061
62 return (CS_API_MAJOR << 8) + CS_API_MINOR;
63}
64
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +080065bool cs_support(int arch)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080066{
67 if (arch == CS_ARCH_ALL)
68 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080069 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
70 (1 << CS_ARCH_PPC));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080071
72 return all_arch & (1 << arch);
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080073}
74
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080075cs_err cs_errno(csh handle)
76{
77 if (!handle)
78 return CS_ERR_CSH;
79
80 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
81
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +080082 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080083}
84
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +080085const char *cs_strerror(cs_err code)
86{
87 switch(code) {
88 default:
89 return "Unknown error code";
90 case CS_ERR_OK:
91 return "OK (CS_ERR_OK)";
92 case CS_ERR_MEM:
93 return "Out of memory (CS_ERR_MEM)";
94 case CS_ERR_ARCH:
95 return "Invalid architecture (CS_ERR_ARCH)";
96 case CS_ERR_HANDLE:
97 return "Invalid handle (CS_ERR_HANDLE)";
98 case CS_ERR_CSH:
99 return "Invalid csh (CS_ERR_CSH)";
100 case CS_ERR_MODE:
101 return "Invalid mode (CS_ERR_MODE)";
102 case CS_ERR_OPTION:
103 return "Invalid option (CS_ERR_OPTION)";
104 case CS_ERR_DETAIL:
105 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800106 case CS_ERR_MEMSETUP:
107 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800108 }
109}
110
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800111cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
112{
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800113 if (!my_malloc || !my_calloc || !my_realloc || !my_free)
114 // Error: before cs_open(), dynamic memory management must be initialized
115 // with cs_option(CS_OPT_MEM)
116 return CS_ERR_MEMSETUP;
117
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800118 if (arch < CS_ARCH_MAX && arch_init[arch]) {
119 cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800120
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800121 ud = my_calloc(1, sizeof(*ud));
122 if (!ud) {
123 // memory insufficient
124 return CS_ERR_MEM;
125 }
126
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800127 ud->errnum = CS_ERR_OK;
128 ud->arch = arch;
129 ud->mode = mode;
130 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
131 ud->reg_name = NULL;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800132 // by default, do not break instruction into details
133 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800134
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800135 arch_init[ud->arch](ud);
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800136
137 *handle = (uintptr_t)ud;
138
139 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800140 } else {
141 *handle = 0;
142 return CS_ERR_ARCH;
143 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800144}
145
146cs_err cs_close(csh handle)
147{
148 if (!handle)
149 return CS_ERR_CSH;
150
151 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
152
153 switch (ud->arch) {
154 case CS_ARCH_X86:
155 break;
156 case CS_ARCH_ARM:
157 case CS_ARCH_MIPS:
158 case CS_ARCH_ARM64:
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800159 case CS_ARCH_PPC:
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800160 my_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800161 break;
162 default: // unsupported architecture
163 return CS_ERR_HANDLE;
164 }
165
Nguyen Anh Quynh9fac5122014-01-07 10:56:04 +0800166 // arch_destroy[ud->arch](ud);
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800167
Nguyen Anh Quynh9fac5122014-01-07 10:56:04 +0800168 my_free(ud->insn_cache);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800169 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800170 my_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800171
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800172 return CS_ERR_OK;
173}
174
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800175#define MIN(x, y) ((x) < (y) ? (x) : (y))
176
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800177// fill insn with mnemonic & operands info
178static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800179 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800180{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800181 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800182 // avoiding copy insn->detail
183 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800184
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800185 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
186 // copy from @regs_read until @arm
187 memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read),
188 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
189 // then copy from @arm until end
190 memcpy((void *)(insn->detail) + offsetof(cs_detail, arm), (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm),
191 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800192 } else {
193 insn->address = mci->address;
194 insn->size = mci->insn_size;
195 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800196
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800197 // fill the instruction bytes
198 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
199
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800200 // map internal instruction opcode to public insn ID
201 if (handle->insn_id)
Nguyen Anh Quynh1acfd0b2014-01-06 10:56:59 +0800202 handle->insn_id(handle, insn, MCInst_getOpcode(mci));
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800203
204 // alias instruction might have ID saved in OpcodePub
205 if (MCInst_getOpcodePub(mci))
206 insn->id = MCInst_getOpcodePub(mci);
207
208 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800209 if (postprinter)
210 postprinter((csh)handle, insn, buffer);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800211
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800212 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800213 // find first space or tab
214 char *sp = buffer;
215 for (sp = buffer; *sp; sp++)
216 if (*sp == ' '||*sp == '\t')
217 break;
218 if (*sp) {
219 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800220 // find the next non-space char
221 sp++;
222 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
223 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800224 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
225 } else
226 insn->op_str[0] = '\0';
227
228 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
229 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
230}
231
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800232cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800233{
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800234 // cs_option() can be called with NULL handle just for CS_OPT_MEM
235 // This is supposed to be executed before all other APIs (even cs_open())
236 if (type == CS_OPT_MEM) {
237 cs_opt_mem *mem = (cs_opt_mem *)value;
238
239 my_malloc = mem->malloc;
240 my_calloc = mem->calloc;
241 my_realloc = mem->realloc;
242 my_free = mem->free;
243
244 return CS_ERR_OK;
245 }
246
danghvu2b192962013-12-19 22:40:28 -0600247 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
248 if (!handle)
249 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800250
danghvu0b6ea042013-12-19 23:07:26 -0600251 if (type == CS_OPT_DETAIL) {
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800252 handle->detail = value;
253 return CS_ERR_OK;
254 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800255
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800256 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800257}
258
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800259// dynamicly allocate memory to contain disasm insn
260// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800261size_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 +0800262{
263 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
264 MCInst mci;
265 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800266 size_t c = 0, f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800267 cs_insn insn_cache[64];
268 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800269 size_t total_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800270
271 if (!handle) {
272 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800273 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800274 return 0;
275 }
276
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800277 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800278
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800279 memset(insn_cache, 0, sizeof(insn_cache));
280
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800281 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600282 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800283 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800284
285 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
286 if (r) {
287 SStream ss;
288 SStream_Init(&ss);
289
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800290 // relative branches need to know the address & size of current insn
291 mci.insn_size = insn_size;
292 mci.address = offset;
293
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800294 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800295 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800296 mci.flat_insn.address = offset;
297 mci.flat_insn.size = insn_size;
298 // allocate memory for @detail pointer
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800299 insn_cache[f].detail = my_calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800300 }
301
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800302 handle->printer(&mci, &ss, handle->printer_info);
303
Joxean114df0e2013-12-04 07:11:32 +0100304 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800305
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800306 f++;
307
308 if (f == ARR_SIZE(insn_cache)) {
309 // resize total to contain newly disasm insns
310 total_size += sizeof(insn_cache);
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800311 void *tmp = my_realloc(total, total_size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800312 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800313 my_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800314 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800315 return 0;
316 }
317
318 total = tmp;
319 memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache));
320 // reset f back to 0
321 f = 0;
322 }
323
324 c++;
325 buffer += insn_size;
326 size -= insn_size;
327 offset += insn_size;
328
329 if (count > 0 && c == count)
330 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800331 } else {
332 // encounter a broken instruction
333 // XXX: TODO: JOXEAN continue here
334 break;
335 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800336 }
337
338 if (f) {
339 // resize total to contain newly disasm insns
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800340 void *tmp = my_realloc(total, total_size + f * sizeof(insn_cache[0]));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800341 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800342 my_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800343 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800344 return 0;
345 }
346
347 total = tmp;
348 memcpy(total + total_size, insn_cache, f * sizeof(insn_cache[0]));
349 }
350
351 *insn = total;
352
353 return c;
354}
355
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800356void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800357{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800358 size_t i;
359
360 // free all detail pointers
361 for (i = 0; i < count; i++)
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800362 my_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800363
364 // then free pointer to cs_insn array
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800365 my_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800366}
367
368// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100369const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800370{
371 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
372
373 if (!handle || handle->reg_name == NULL) {
374 return NULL;
375 }
376
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800377 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800378}
379
pancakef0e4eed2013-12-11 22:14:42 +0100380const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800381{
382 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
383
384 if (!handle || handle->insn_name == NULL) {
385 return NULL;
386 }
387
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800388 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800389}
390
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800391static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800392{
393 int i;
394
395 for (i = 0; i < max; i++) {
396 if (arr[i] == id)
397 return true;
398 }
399
400 return false;
401}
402
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800403bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800404{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800405 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800406 return false;
407
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800408 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
409
410 if (!handle->detail) {
411 handle->errnum = CS_ERR_DETAIL;
412 return false;
413 }
414
415 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800416}
417
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800418bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800419{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800420 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800421 return false;
422
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800423 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
424
425 if (!handle->detail) {
426 handle->errnum = CS_ERR_DETAIL;
427 return false;
428 }
429
430 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800431}
432
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800433bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800434{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800435 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800436 return false;
437
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800438 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
439
440 if (!handle->detail) {
441 handle->errnum = CS_ERR_DETAIL;
442 return false;
443 }
444
445 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800446}
447
448int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
449{
450 if (!ud)
451 return -1;
452
453 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
454 unsigned int count = 0, i;
455
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800456 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800457
458 switch (handle->arch) {
459 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800460 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800461 return -1;
462 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800463 for (i = 0; i < insn->detail->arm.op_count; i++)
464 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800465 count++;
466 break;
467 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800468 for (i = 0; i < insn->detail->arm64.op_count; i++)
469 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800470 count++;
471 break;
472 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800473 for (i = 0; i < insn->detail->x86.op_count; i++)
474 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800475 count++;
476 break;
477 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800478 for (i = 0; i < insn->detail->mips.op_count; i++)
479 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800480 count++;
481 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800482 case CS_ARCH_PPC:
483 for (i = 0; i < insn->detail->ppc.op_count; i++)
484 if (insn->detail->ppc.operands[i].type == op_type)
485 count++;
486 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800487 }
488
489 return count;
490}
491
492int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
493 unsigned int post)
494{
495 if (!ud)
496 return -1;
497
498 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
499 unsigned int count = 0, i;
500
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800501 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800502
503 switch (handle->arch) {
504 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800505 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800506 return -1;
507 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800508 for (i = 0; i < insn->detail->arm.op_count; i++) {
509 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800510 count++;
511 if (count == post)
512 return i;
513 }
514 break;
515 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800516 for (i = 0; i < insn->detail->arm64.op_count; i++) {
517 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800518 count++;
519 if (count == post)
520 return i;
521 }
522 break;
523 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800524 for (i = 0; i < insn->detail->x86.op_count; i++) {
525 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800526 count++;
527 if (count == post)
528 return i;
529 }
530 break;
531 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800532 for (i = 0; i < insn->detail->mips.op_count; i++) {
533 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800534 count++;
535 if (count == post)
536 return i;
537 }
538 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800539 case CS_ARCH_PPC:
540 for (i = 0; i < insn->detail->ppc.op_count; i++) {
541 if (insn->detail->ppc.operands[i].type == op_type)
542 count++;
543 if (count == post)
544 return i;
545 }
546 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800547 }
548
549 return -1;
550}