blob: 5e424d7a35dfe6826f1ab525f104d460b6d96e4b [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 Quynh1acfd0b2014-01-06 10:56:59 +0800141 arch_destroy[ud->arch](ud);
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800142
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800143 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800144 my_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800145
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800146 return CS_ERR_OK;
147}
148
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800149#define MIN(x, y) ((x) < (y) ? (x) : (y))
150
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800151// fill insn with mnemonic & operands info
152static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800153 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800154{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800155 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800156 // avoiding copy insn->detail
157 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800158
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800159 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
160 // copy from @regs_read until @arm
161 memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read),
162 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
163 // then copy from @arm until end
164 memcpy((void *)(insn->detail) + offsetof(cs_detail, arm), (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm),
165 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800166 } else {
167 insn->address = mci->address;
168 insn->size = mci->insn_size;
169 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800170
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800171 // fill the instruction bytes
172 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
173
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800174 // map internal instruction opcode to public insn ID
175 if (handle->insn_id)
Nguyen Anh Quynh1acfd0b2014-01-06 10:56:59 +0800176 handle->insn_id(handle, insn, MCInst_getOpcode(mci));
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800177
178 // alias instruction might have ID saved in OpcodePub
179 if (MCInst_getOpcodePub(mci))
180 insn->id = MCInst_getOpcodePub(mci);
181
182 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800183 if (postprinter)
184 postprinter((csh)handle, insn, buffer);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800185
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800186 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800187 // find first space or tab
188 char *sp = buffer;
189 for (sp = buffer; *sp; sp++)
190 if (*sp == ' '||*sp == '\t')
191 break;
192 if (*sp) {
193 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800194 // find the next non-space char
195 sp++;
196 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
197 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800198 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
199 } else
200 insn->op_str[0] = '\0';
201
202 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
203 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
204}
205
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800206cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800207{
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800208 // cs_option() can be called with NULL handle just for CS_OPT_MEM
209 // This is supposed to be executed before all other APIs (even cs_open())
210 if (type == CS_OPT_MEM) {
211 cs_opt_mem *mem = (cs_opt_mem *)value;
212
213 my_malloc = mem->malloc;
214 my_calloc = mem->calloc;
215 my_realloc = mem->realloc;
216 my_free = mem->free;
217
218 return CS_ERR_OK;
219 }
220
danghvu2b192962013-12-19 22:40:28 -0600221 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
222 if (!handle)
223 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800224
danghvu0b6ea042013-12-19 23:07:26 -0600225 if (type == CS_OPT_DETAIL) {
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800226 handle->detail = value;
227 return CS_ERR_OK;
228 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800229
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800230 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800231}
232
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800233// dynamicly allocate memory to contain disasm insn
234// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800235size_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 +0800236{
237 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
238 MCInst mci;
239 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800240 size_t c = 0, f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800241 cs_insn insn_cache[64];
242 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800243 size_t total_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800244
245 if (!handle) {
246 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800247 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800248 return 0;
249 }
250
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800251 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800252
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800253 memset(insn_cache, 0, sizeof(insn_cache));
254
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800255 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600256 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800257 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800258
259 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
260 if (r) {
261 SStream ss;
262 SStream_Init(&ss);
263
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800264 // relative branches need to know the address & size of current insn
265 mci.insn_size = insn_size;
266 mci.address = offset;
267
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800268 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800269 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800270 mci.flat_insn.address = offset;
271 mci.flat_insn.size = insn_size;
272 // allocate memory for @detail pointer
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800273 insn_cache[f].detail = my_calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800274 }
275
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800276 handle->printer(&mci, &ss, handle->printer_info);
277
Joxean114df0e2013-12-04 07:11:32 +0100278 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800279
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800280 f++;
281
282 if (f == ARR_SIZE(insn_cache)) {
283 // resize total to contain newly disasm insns
284 total_size += sizeof(insn_cache);
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800285 void *tmp = my_realloc(total, total_size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800286 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800287 my_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800288 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800289 return 0;
290 }
291
292 total = tmp;
293 memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache));
294 // reset f back to 0
295 f = 0;
296 }
297
298 c++;
299 buffer += insn_size;
300 size -= insn_size;
301 offset += insn_size;
302
303 if (count > 0 && c == count)
304 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800305 } else {
306 // encounter a broken instruction
307 // XXX: TODO: JOXEAN continue here
308 break;
309 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800310 }
311
312 if (f) {
313 // resize total to contain newly disasm insns
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800314 void *tmp = my_realloc(total, total_size + f * sizeof(insn_cache[0]));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800315 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800316 my_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800317 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800318 return 0;
319 }
320
321 total = tmp;
322 memcpy(total + total_size, insn_cache, f * sizeof(insn_cache[0]));
323 }
324
325 *insn = total;
326
327 return c;
328}
329
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800330void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800331{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800332 size_t i;
333
334 // free all detail pointers
335 for (i = 0; i < count; i++)
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800336 my_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800337
338 // then free pointer to cs_insn array
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800339 my_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800340}
341
342// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100343const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800344{
345 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
346
347 if (!handle || handle->reg_name == NULL) {
348 return NULL;
349 }
350
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800351 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800352}
353
pancakef0e4eed2013-12-11 22:14:42 +0100354const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800355{
356 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
357
358 if (!handle || handle->insn_name == NULL) {
359 return NULL;
360 }
361
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800362 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800363}
364
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800365static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800366{
367 int i;
368
369 for (i = 0; i < max; i++) {
370 if (arr[i] == id)
371 return true;
372 }
373
374 return false;
375}
376
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800377bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800378{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800379 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800380 return false;
381
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800382 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
383
384 if (!handle->detail) {
385 handle->errnum = CS_ERR_DETAIL;
386 return false;
387 }
388
389 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800390}
391
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800392bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800393{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800394 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800395 return false;
396
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800397 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
398
399 if (!handle->detail) {
400 handle->errnum = CS_ERR_DETAIL;
401 return false;
402 }
403
404 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800405}
406
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800407bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_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;
413
414 if (!handle->detail) {
415 handle->errnum = CS_ERR_DETAIL;
416 return false;
417 }
418
419 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800420}
421
422int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
423{
424 if (!ud)
425 return -1;
426
427 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
428 unsigned int count = 0, i;
429
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800430 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800431
432 switch (handle->arch) {
433 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800434 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800435 return -1;
436 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800437 for (i = 0; i < insn->detail->arm.op_count; i++)
438 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800439 count++;
440 break;
441 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800442 for (i = 0; i < insn->detail->arm64.op_count; i++)
443 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800444 count++;
445 break;
446 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800447 for (i = 0; i < insn->detail->x86.op_count; i++)
448 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800449 count++;
450 break;
451 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800452 for (i = 0; i < insn->detail->mips.op_count; i++)
453 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800454 count++;
455 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800456 case CS_ARCH_PPC:
457 for (i = 0; i < insn->detail->ppc.op_count; i++)
458 if (insn->detail->ppc.operands[i].type == op_type)
459 count++;
460 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800461 }
462
463 return count;
464}
465
466int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
467 unsigned int post)
468{
469 if (!ud)
470 return -1;
471
472 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
473 unsigned int count = 0, i;
474
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800475 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800476
477 switch (handle->arch) {
478 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800479 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800480 return -1;
481 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800482 for (i = 0; i < insn->detail->arm.op_count; i++) {
483 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800484 count++;
485 if (count == post)
486 return i;
487 }
488 break;
489 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800490 for (i = 0; i < insn->detail->arm64.op_count; i++) {
491 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800492 count++;
493 if (count == post)
494 return i;
495 }
496 break;
497 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800498 for (i = 0; i < insn->detail->x86.op_count; i++) {
499 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800500 count++;
501 if (count == post)
502 return i;
503 }
504 break;
505 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800506 for (i = 0; i < insn->detail->mips.op_count; i++) {
507 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800508 count++;
509 if (count == post)
510 return i;
511 }
512 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800513 case CS_ARCH_PPC:
514 for (i = 0; i < insn->detail->ppc.op_count; i++) {
515 if (insn->detail->ppc.operands[i].type == op_type)
516 count++;
517 if (count == post)
518 return i;
519 }
520 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800521 }
522
523 return -1;
524}