blob: 76cab6a925ba9e7a7741821093cefa1b90924abe [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 Quynh24bf0d92014-01-05 11:19:04 +080019malloc_t my_malloc = malloc;
20calloc_t my_calloc = calloc;
21realloc_t my_realloc = realloc;
22free_t my_free = free;
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080023
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080024unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080025{
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080026 if (major != NULL && minor != NULL) {
27 *major = CS_API_MAJOR;
28 *minor = CS_API_MINOR;
29 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080030
31 return (CS_API_MAJOR << 8) + CS_API_MINOR;
32}
33
34bool cs_support(cs_arch arch)
35{
36 if (arch == CS_ARCH_ALL)
37 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080038 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
39 (1 << CS_ARCH_PPC));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080040
41 return all_arch & (1 << arch);
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080042}
43
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080044cs_err cs_errno(csh handle)
45{
46 if (!handle)
47 return CS_ERR_CSH;
48
49 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
50
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +080051 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080052}
53
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +080054const char *cs_strerror(cs_err code)
55{
56 switch(code) {
57 default:
58 return "Unknown error code";
59 case CS_ERR_OK:
60 return "OK (CS_ERR_OK)";
61 case CS_ERR_MEM:
62 return "Out of memory (CS_ERR_MEM)";
63 case CS_ERR_ARCH:
64 return "Invalid architecture (CS_ERR_ARCH)";
65 case CS_ERR_HANDLE:
66 return "Invalid handle (CS_ERR_HANDLE)";
67 case CS_ERR_CSH:
68 return "Invalid csh (CS_ERR_CSH)";
69 case CS_ERR_MODE:
70 return "Invalid mode (CS_ERR_MODE)";
71 case CS_ERR_OPTION:
72 return "Invalid option (CS_ERR_OPTION)";
73 case CS_ERR_DETAIL:
74 return "Details are unavailable (CS_ERR_DETAIL)";
75 }
76}
77
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080078cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
79{
danghvu2b192962013-12-19 22:40:28 -060080 cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080081
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +080082 ud = my_calloc(1, sizeof(*ud));
danghvu2b192962013-12-19 22:40:28 -060083 if (!ud) {
84 // memory insufficient
85 return CS_ERR_MEM;
86 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080087
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +080088 if (arch < CS_ARCH_MAX && arch_init[ud->arch]) {
89 ud->errnum = CS_ERR_OK;
90 ud->arch = arch;
91 ud->mode = mode;
92 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
93 ud->reg_name = NULL;
94 ud->detail = CS_OPT_ON; // by default break instruction into details
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080095
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +080096 arch_init[ud->arch](ud);
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +080097 } else {
98 *handle = 0;
99 return CS_ERR_ARCH;
100 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800101
danghvu2b192962013-12-19 22:40:28 -0600102 *handle = (uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800103
danghvu2b192962013-12-19 22:40:28 -0600104 return CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800105}
106
107cs_err cs_close(csh handle)
108{
109 if (!handle)
110 return CS_ERR_CSH;
111
112 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
113
114 switch (ud->arch) {
115 case CS_ARCH_X86:
116 break;
117 case CS_ARCH_ARM:
118 case CS_ARCH_MIPS:
119 case CS_ARCH_ARM64:
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800120 case CS_ARCH_PPC:
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800121 my_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800122 break;
123 default: // unsupported architecture
124 return CS_ERR_HANDLE;
125 }
126
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800127 if (arch_destroy[ud->arch])
128 arch_destroy[ud->arch](ud);
129
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800130 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800131 my_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800132
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800133 return CS_ERR_OK;
134}
135
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800136#define MIN(x, y) ((x) < (y) ? (x) : (y))
137
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800138// fill insn with mnemonic & operands info
139static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800140 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800141{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800142 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800143 // avoiding copy insn->detail
144 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800145
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800146 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
147 // copy from @regs_read until @arm
148 memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read),
149 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
150 // then copy from @arm until end
151 memcpy((void *)(insn->detail) + offsetof(cs_detail, arm), (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm),
152 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800153 } else {
154 insn->address = mci->address;
155 insn->size = mci->insn_size;
156 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800157
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800158 // fill the instruction bytes
159 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
160
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800161 // map internal instruction opcode to public insn ID
162 if (handle->insn_id)
163 handle->insn_id(insn, MCInst_getOpcode(mci), handle->detail);
164
165 // alias instruction might have ID saved in OpcodePub
166 if (MCInst_getOpcodePub(mci))
167 insn->id = MCInst_getOpcodePub(mci);
168
169 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800170 if (postprinter)
171 postprinter((csh)handle, insn, buffer);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800172
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800173 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800174 // find first space or tab
175 char *sp = buffer;
176 for (sp = buffer; *sp; sp++)
177 if (*sp == ' '||*sp == '\t')
178 break;
179 if (*sp) {
180 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800181 // find the next non-space char
182 sp++;
183 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
184 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800185 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
186 } else
187 insn->op_str[0] = '\0';
188
189 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
190 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
191}
192
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800193cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800194{
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800195 // cs_option() can be called with NULL handle just for CS_OPT_MEM
196 // This is supposed to be executed before all other APIs (even cs_open())
197 if (type == CS_OPT_MEM) {
198 cs_opt_mem *mem = (cs_opt_mem *)value;
199
200 my_malloc = mem->malloc;
201 my_calloc = mem->calloc;
202 my_realloc = mem->realloc;
203 my_free = mem->free;
204
205 return CS_ERR_OK;
206 }
207
danghvu2b192962013-12-19 22:40:28 -0600208 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
209 if (!handle)
210 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800211
danghvu0b6ea042013-12-19 23:07:26 -0600212 if (type == CS_OPT_DETAIL) {
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800213 handle->detail = value;
214 return CS_ERR_OK;
215 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800216
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800217 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800218}
219
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800220// dynamicly allocate memory to contain disasm insn
221// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800222size_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 +0800223{
224 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
225 MCInst mci;
226 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800227 size_t c = 0, f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800228 cs_insn insn_cache[64];
229 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800230 size_t total_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800231
232 if (!handle) {
233 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800234 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800235 return 0;
236 }
237
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800238 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800239
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800240 memset(insn_cache, 0, sizeof(insn_cache));
241
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800242 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600243 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800244 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800245
246 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
247 if (r) {
248 SStream ss;
249 SStream_Init(&ss);
250
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800251 // relative branches need to know the address & size of current insn
252 mci.insn_size = insn_size;
253 mci.address = offset;
254
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800255 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800256 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800257 mci.flat_insn.address = offset;
258 mci.flat_insn.size = insn_size;
259 // allocate memory for @detail pointer
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800260 insn_cache[f].detail = my_calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800261 }
262
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800263 handle->printer(&mci, &ss, handle->printer_info);
264
Joxean114df0e2013-12-04 07:11:32 +0100265 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800266
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800267 f++;
268
269 if (f == ARR_SIZE(insn_cache)) {
270 // resize total to contain newly disasm insns
271 total_size += sizeof(insn_cache);
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800272 void *tmp = my_realloc(total, total_size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800273 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800274 my_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800275 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800276 return 0;
277 }
278
279 total = tmp;
280 memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache));
281 // reset f back to 0
282 f = 0;
283 }
284
285 c++;
286 buffer += insn_size;
287 size -= insn_size;
288 offset += insn_size;
289
290 if (count > 0 && c == count)
291 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800292 } else {
293 // encounter a broken instruction
294 // XXX: TODO: JOXEAN continue here
295 break;
296 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800297 }
298
299 if (f) {
300 // resize total to contain newly disasm insns
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800301 void *tmp = my_realloc(total, total_size + f * sizeof(insn_cache[0]));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800302 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800303 my_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800304 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800305 return 0;
306 }
307
308 total = tmp;
309 memcpy(total + total_size, insn_cache, f * sizeof(insn_cache[0]));
310 }
311
312 *insn = total;
313
314 return c;
315}
316
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800317void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800318{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800319 size_t i;
320
321 // free all detail pointers
322 for (i = 0; i < count; i++)
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800323 my_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800324
325 // then free pointer to cs_insn array
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800326 my_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800327}
328
329// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100330const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800331{
332 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
333
334 if (!handle || handle->reg_name == NULL) {
335 return NULL;
336 }
337
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800338 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800339}
340
pancakef0e4eed2013-12-11 22:14:42 +0100341const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800342{
343 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
344
345 if (!handle || handle->insn_name == NULL) {
346 return NULL;
347 }
348
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800349 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800350}
351
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800352static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800353{
354 int i;
355
356 for (i = 0; i < max; i++) {
357 if (arr[i] == id)
358 return true;
359 }
360
361 return false;
362}
363
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800364bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800365{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800366 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800367 return false;
368
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800369 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
370
371 if (!handle->detail) {
372 handle->errnum = CS_ERR_DETAIL;
373 return false;
374 }
375
376 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800377}
378
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800379bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800380{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800381 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800382 return false;
383
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800384 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
385
386 if (!handle->detail) {
387 handle->errnum = CS_ERR_DETAIL;
388 return false;
389 }
390
391 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800392}
393
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800394bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800395{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800396 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800397 return false;
398
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800399 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
400
401 if (!handle->detail) {
402 handle->errnum = CS_ERR_DETAIL;
403 return false;
404 }
405
406 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800407}
408
409int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
410{
411 if (!ud)
412 return -1;
413
414 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
415 unsigned int count = 0, i;
416
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800417 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800418
419 switch (handle->arch) {
420 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800421 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800422 return -1;
423 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800424 for (i = 0; i < insn->detail->arm.op_count; i++)
425 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800426 count++;
427 break;
428 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800429 for (i = 0; i < insn->detail->arm64.op_count; i++)
430 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800431 count++;
432 break;
433 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800434 for (i = 0; i < insn->detail->x86.op_count; i++)
435 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800436 count++;
437 break;
438 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800439 for (i = 0; i < insn->detail->mips.op_count; i++)
440 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800441 count++;
442 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800443 case CS_ARCH_PPC:
444 for (i = 0; i < insn->detail->ppc.op_count; i++)
445 if (insn->detail->ppc.operands[i].type == op_type)
446 count++;
447 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800448 }
449
450 return count;
451}
452
453int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
454 unsigned int post)
455{
456 if (!ud)
457 return -1;
458
459 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
460 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 if (count == post)
473 return i;
474 }
475 break;
476 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800477 for (i = 0; i < insn->detail->arm64.op_count; i++) {
478 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800479 count++;
480 if (count == post)
481 return i;
482 }
483 break;
484 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800485 for (i = 0; i < insn->detail->x86.op_count; i++) {
486 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800487 count++;
488 if (count == post)
489 return i;
490 }
491 break;
492 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800493 for (i = 0; i < insn->detail->mips.op_count; i++) {
494 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800495 count++;
496 if (count == post)
497 return i;
498 }
499 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800500 case CS_ARCH_PPC:
501 for (i = 0; i < insn->detail->ppc.op_count; i++) {
502 if (insn->detail->ppc.operands[i].type == op_type)
503 count++;
504 if (count == post)
505 return i;
506 }
507 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800508 }
509
510 return -1;
511}