blob: 8ee19fec98245a7e6fc156b6b0739431e9344264 [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 Quynh39a42ed2013-12-22 10:40:58 +080017unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080018
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080019#ifdef USE_SYS_DYN_MEM
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +080020malloc_t my_malloc = malloc;
21calloc_t my_calloc = calloc;
22realloc_t my_realloc = realloc;
23free_t my_free = free;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080024#else
25malloc_t my_malloc = NULL;
26calloc_t my_calloc = NULL;
27realloc_t my_realloc = NULL;
28free_t my_free = NULL;
29#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080030
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080031unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080032{
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080033 if (major != NULL && minor != NULL) {
34 *major = CS_API_MAJOR;
35 *minor = CS_API_MINOR;
36 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080037
38 return (CS_API_MAJOR << 8) + CS_API_MINOR;
39}
40
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +080041bool cs_support(int arch)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080042{
43 if (arch == CS_ARCH_ALL)
44 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080045 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
46 (1 << CS_ARCH_PPC));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080047
48 return all_arch & (1 << arch);
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080049}
50
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080051cs_err cs_errno(csh handle)
52{
53 if (!handle)
54 return CS_ERR_CSH;
55
56 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
57
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +080058 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080059}
60
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +080061const char *cs_strerror(cs_err code)
62{
63 switch(code) {
64 default:
65 return "Unknown error code";
66 case CS_ERR_OK:
67 return "OK (CS_ERR_OK)";
68 case CS_ERR_MEM:
69 return "Out of memory (CS_ERR_MEM)";
70 case CS_ERR_ARCH:
71 return "Invalid architecture (CS_ERR_ARCH)";
72 case CS_ERR_HANDLE:
73 return "Invalid handle (CS_ERR_HANDLE)";
74 case CS_ERR_CSH:
75 return "Invalid csh (CS_ERR_CSH)";
76 case CS_ERR_MODE:
77 return "Invalid mode (CS_ERR_MODE)";
78 case CS_ERR_OPTION:
79 return "Invalid option (CS_ERR_OPTION)";
80 case CS_ERR_DETAIL:
81 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +080082 case CS_ERR_MEMSETUP:
83 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +080084 }
85}
86
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080087cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
88{
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +080089 if (!my_malloc || !my_calloc || !my_realloc || !my_free)
90 // Error: before cs_open(), dynamic memory management must be initialized
91 // with cs_option(CS_OPT_MEM)
92 return CS_ERR_MEMSETUP;
93
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +080094 if (arch < CS_ARCH_MAX && arch_init[arch]) {
95 cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080096
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +080097 ud = my_calloc(1, sizeof(*ud));
98 if (!ud) {
99 // memory insufficient
100 return CS_ERR_MEM;
101 }
102
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800103 ud->errnum = CS_ERR_OK;
104 ud->arch = arch;
105 ud->mode = mode;
106 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
107 ud->reg_name = NULL;
108 ud->detail = CS_OPT_ON; // by default break instruction into details
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800109
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800110 arch_init[ud->arch](ud);
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800111
112 *handle = (uintptr_t)ud;
113
114 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800115 } else {
116 *handle = 0;
117 return CS_ERR_ARCH;
118 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800119}
120
121cs_err cs_close(csh handle)
122{
123 if (!handle)
124 return CS_ERR_CSH;
125
126 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
127
128 switch (ud->arch) {
129 case CS_ARCH_X86:
130 break;
131 case CS_ARCH_ARM:
132 case CS_ARCH_MIPS:
133 case CS_ARCH_ARM64:
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800134 case CS_ARCH_PPC:
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800135 my_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800136 break;
137 default: // unsupported architecture
138 return CS_ERR_HANDLE;
139 }
140
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800141 if (arch_destroy[ud->arch])
142 arch_destroy[ud->arch](ud);
143
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800144 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800145 my_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800146
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800147 return CS_ERR_OK;
148}
149
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800150#define MIN(x, y) ((x) < (y) ? (x) : (y))
151
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800152// fill insn with mnemonic & operands info
153static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800154 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800155{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800156 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800157 // avoiding copy insn->detail
158 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800159
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800160 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
161 // copy from @regs_read until @arm
162 memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read),
163 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
164 // then copy from @arm until end
165 memcpy((void *)(insn->detail) + offsetof(cs_detail, arm), (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm),
166 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800167 } else {
168 insn->address = mci->address;
169 insn->size = mci->insn_size;
170 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800171
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800172 // fill the instruction bytes
173 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
174
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800175 // map internal instruction opcode to public insn ID
176 if (handle->insn_id)
177 handle->insn_id(insn, MCInst_getOpcode(mci), handle->detail);
178
179 // alias instruction might have ID saved in OpcodePub
180 if (MCInst_getOpcodePub(mci))
181 insn->id = MCInst_getOpcodePub(mci);
182
183 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800184 if (postprinter)
185 postprinter((csh)handle, insn, buffer);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800186
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800187 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800188 // find first space or tab
189 char *sp = buffer;
190 for (sp = buffer; *sp; sp++)
191 if (*sp == ' '||*sp == '\t')
192 break;
193 if (*sp) {
194 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800195 // find the next non-space char
196 sp++;
197 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
198 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800199 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
200 } else
201 insn->op_str[0] = '\0';
202
203 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
204 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
205}
206
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800207cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800208{
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800209 // cs_option() can be called with NULL handle just for CS_OPT_MEM
210 // This is supposed to be executed before all other APIs (even cs_open())
211 if (type == CS_OPT_MEM) {
212 cs_opt_mem *mem = (cs_opt_mem *)value;
213
214 my_malloc = mem->malloc;
215 my_calloc = mem->calloc;
216 my_realloc = mem->realloc;
217 my_free = mem->free;
218
219 return CS_ERR_OK;
220 }
221
danghvu2b192962013-12-19 22:40:28 -0600222 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
223 if (!handle)
224 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800225
danghvu0b6ea042013-12-19 23:07:26 -0600226 if (type == CS_OPT_DETAIL) {
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800227 handle->detail = value;
228 return CS_ERR_OK;
229 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800230
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800231 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800232}
233
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800234// dynamicly allocate memory to contain disasm insn
235// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800236size_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 +0800237{
238 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
239 MCInst mci;
240 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800241 size_t c = 0, f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800242 cs_insn insn_cache[64];
243 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800244 size_t total_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800245
246 if (!handle) {
247 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800248 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800249 return 0;
250 }
251
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800252 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800253
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800254 memset(insn_cache, 0, sizeof(insn_cache));
255
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800256 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600257 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800258 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800259
260 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
261 if (r) {
262 SStream ss;
263 SStream_Init(&ss);
264
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800265 // relative branches need to know the address & size of current insn
266 mci.insn_size = insn_size;
267 mci.address = offset;
268
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800269 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800270 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800271 mci.flat_insn.address = offset;
272 mci.flat_insn.size = insn_size;
273 // allocate memory for @detail pointer
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800274 insn_cache[f].detail = my_calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800275 }
276
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800277 handle->printer(&mci, &ss, handle->printer_info);
278
Joxean114df0e2013-12-04 07:11:32 +0100279 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800280
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800281 f++;
282
283 if (f == ARR_SIZE(insn_cache)) {
284 // resize total to contain newly disasm insns
285 total_size += sizeof(insn_cache);
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800286 void *tmp = my_realloc(total, total_size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800287 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800288 my_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800289 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800290 return 0;
291 }
292
293 total = tmp;
294 memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache));
295 // reset f back to 0
296 f = 0;
297 }
298
299 c++;
300 buffer += insn_size;
301 size -= insn_size;
302 offset += insn_size;
303
304 if (count > 0 && c == count)
305 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800306 } else {
307 // encounter a broken instruction
308 // XXX: TODO: JOXEAN continue here
309 break;
310 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800311 }
312
313 if (f) {
314 // resize total to contain newly disasm insns
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800315 void *tmp = my_realloc(total, total_size + f * sizeof(insn_cache[0]));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800316 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800317 my_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, insn_cache, f * sizeof(insn_cache[0]));
324 }
325
326 *insn = total;
327
328 return c;
329}
330
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800331void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800332{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800333 size_t i;
334
335 // free all detail pointers
336 for (i = 0; i < count; i++)
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800337 my_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800338
339 // then free pointer to cs_insn array
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800340 my_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800341}
342
343// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100344const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800345{
346 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
347
348 if (!handle || handle->reg_name == NULL) {
349 return NULL;
350 }
351
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800352 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800353}
354
pancakef0e4eed2013-12-11 22:14:42 +0100355const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800356{
357 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
358
359 if (!handle || handle->insn_name == NULL) {
360 return NULL;
361 }
362
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800363 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800364}
365
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800366static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800367{
368 int i;
369
370 for (i = 0; i < max; i++) {
371 if (arr[i] == id)
372 return true;
373 }
374
375 return false;
376}
377
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800378bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800379{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800380 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800381 return false;
382
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800383 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
384
385 if (!handle->detail) {
386 handle->errnum = CS_ERR_DETAIL;
387 return false;
388 }
389
390 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800391}
392
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800393bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800394{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800395 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800396 return false;
397
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800398 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
399
400 if (!handle->detail) {
401 handle->errnum = CS_ERR_DETAIL;
402 return false;
403 }
404
405 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800406}
407
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800408bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800409{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800410 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800411 return false;
412
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800413 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
414
415 if (!handle->detail) {
416 handle->errnum = CS_ERR_DETAIL;
417 return false;
418 }
419
420 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800421}
422
423int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
424{
425 if (!ud)
426 return -1;
427
428 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
429 unsigned int count = 0, i;
430
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800431 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800432
433 switch (handle->arch) {
434 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800435 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800436 return -1;
437 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800438 for (i = 0; i < insn->detail->arm.op_count; i++)
439 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800440 count++;
441 break;
442 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800443 for (i = 0; i < insn->detail->arm64.op_count; i++)
444 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800445 count++;
446 break;
447 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800448 for (i = 0; i < insn->detail->x86.op_count; i++)
449 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800450 count++;
451 break;
452 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800453 for (i = 0; i < insn->detail->mips.op_count; i++)
454 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800455 count++;
456 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800457 case CS_ARCH_PPC:
458 for (i = 0; i < insn->detail->ppc.op_count; i++)
459 if (insn->detail->ppc.operands[i].type == op_type)
460 count++;
461 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800462 }
463
464 return count;
465}
466
467int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
468 unsigned int post)
469{
470 if (!ud)
471 return -1;
472
473 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
474 unsigned int count = 0, i;
475
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800476 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800477
478 switch (handle->arch) {
479 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800480 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800481 return -1;
482 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800483 for (i = 0; i < insn->detail->arm.op_count; i++) {
484 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800485 count++;
486 if (count == post)
487 return i;
488 }
489 break;
490 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800491 for (i = 0; i < insn->detail->arm64.op_count; i++) {
492 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800493 count++;
494 if (count == post)
495 return i;
496 }
497 break;
498 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800499 for (i = 0; i < insn->detail->x86.op_count; i++) {
500 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800501 count++;
502 if (count == post)
503 return i;
504 }
505 break;
506 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800507 for (i = 0; i < insn->detail->mips.op_count; i++) {
508 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800509 count++;
510 if (count == post)
511 return i;
512 }
513 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800514 case CS_ARCH_PPC:
515 for (i = 0; i < insn->detail->ppc.op_count; i++) {
516 if (insn->detail->ppc.operands[i].type == op_type)
517 count++;
518 if (count == post)
519 return i;
520 }
521 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800522 }
523
524 return -1;
525}