blob: 47fa14b4c199ddf997bbae23d05916bb2262e361 [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
10#include "cs_priv.h"
11
12#include "MCRegisterInfo.h"
13
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080014#include "utils.h"
15
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080016cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +080017cs_err (*arch_option[MAX_ARCH]) (cs_struct*, cs_opt_type, size_t value);
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080018
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080019unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080020
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080021
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080022unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080023{
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080024 if (major != NULL && minor != NULL) {
25 *major = CS_API_MAJOR;
26 *minor = CS_API_MINOR;
27 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080028
29 return (CS_API_MAJOR << 8) + CS_API_MINOR;
30}
31
32bool cs_support(cs_arch arch)
33{
34 if (arch == CS_ARCH_ALL)
35 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080036 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
37 (1 << CS_ARCH_PPC));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080038
39 return all_arch & (1 << arch);
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080040}
41
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080042cs_err cs_errno(csh handle)
43{
44 if (!handle)
45 return CS_ERR_CSH;
46
47 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
48
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +080049 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080050}
51
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080052cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
53{
danghvu2b192962013-12-19 22:40:28 -060054 cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080055
danghvu2b192962013-12-19 22:40:28 -060056 ud = calloc(1, sizeof(*ud));
57 if (!ud) {
58 // memory insufficient
59 return CS_ERR_MEM;
60 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080061
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +080062 if (arch < CS_ARCH_MAX && arch_init[ud->arch]) {
63 ud->errnum = CS_ERR_OK;
64 ud->arch = arch;
65 ud->mode = mode;
66 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
67 ud->reg_name = NULL;
68 ud->detail = CS_OPT_ON; // by default break instruction into details
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080069
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +080070 arch_init[ud->arch](ud);
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +080071 } else {
72 *handle = 0;
73 return CS_ERR_ARCH;
74 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080075
danghvu2b192962013-12-19 22:40:28 -060076 *handle = (uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080077
danghvu2b192962013-12-19 22:40:28 -060078 return CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080079}
80
81cs_err cs_close(csh handle)
82{
83 if (!handle)
84 return CS_ERR_CSH;
85
86 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
87
88 switch (ud->arch) {
89 case CS_ARCH_X86:
90 break;
91 case CS_ARCH_ARM:
92 case CS_ARCH_MIPS:
93 case CS_ARCH_ARM64:
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080094 case CS_ARCH_PPC:
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080095 free(ud->printer_info);
96 break;
97 default: // unsupported architecture
98 return CS_ERR_HANDLE;
99 }
100
101 memset(ud, 0, sizeof(*ud));
102 free(ud);
103
104 return CS_ERR_OK;
105}
106
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800107#define MIN(x, y) ((x) < (y) ? (x) : (y))
108
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800109// fill insn with mnemonic & operands info
110static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800111 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800112{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800113 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800114 // avoiding copy insn->detail
115 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800116
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800117 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
118 // copy from @regs_read until @arm
119 memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read),
120 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
121 // then copy from @arm until end
122 memcpy((void *)(insn->detail) + offsetof(cs_detail, arm), (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm),
123 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800124 } else {
125 insn->address = mci->address;
126 insn->size = mci->insn_size;
127 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800128
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800129 // fill the instruction bytes
130 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
131
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800132 // map internal instruction opcode to public insn ID
133 if (handle->insn_id)
134 handle->insn_id(insn, MCInst_getOpcode(mci), handle->detail);
135
136 // alias instruction might have ID saved in OpcodePub
137 if (MCInst_getOpcodePub(mci))
138 insn->id = MCInst_getOpcodePub(mci);
139
140 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800141 if (postprinter)
142 postprinter((csh)handle, insn, buffer);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800143
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800144 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800145 // find first space or tab
146 char *sp = buffer;
147 for (sp = buffer; *sp; sp++)
148 if (*sp == ' '||*sp == '\t')
149 break;
150 if (*sp) {
151 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800152 // find the next non-space char
153 sp++;
154 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
155 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800156 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
157 } else
158 insn->op_str[0] = '\0';
159
160 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
161 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
162}
163
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800164cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800165{
danghvu2b192962013-12-19 22:40:28 -0600166 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
167 if (!handle)
168 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800169
danghvu0b6ea042013-12-19 23:07:26 -0600170 if (type == CS_OPT_DETAIL) {
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800171 handle->detail = value;
172 return CS_ERR_OK;
173 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800174
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800175 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800176}
177
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800178// dynamicly allocate memory to contain disasm insn
179// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800180size_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 +0800181{
182 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
183 MCInst mci;
184 uint16_t insn_size;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800185 size_t c = 0, f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800186 cs_insn insn_cache[64];
187 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800188 size_t total_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800189
190 if (!handle) {
191 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800192 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800193 return 0;
194 }
195
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800196 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800197
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800198 memset(insn_cache, 0, sizeof(insn_cache));
199
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800200 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600201 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800202 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800203
204 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
205 if (r) {
206 SStream ss;
207 SStream_Init(&ss);
208
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800209 // relative branches need to know the address & size of current insn
210 mci.insn_size = insn_size;
211 mci.address = offset;
212
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800213 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800214 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800215 mci.flat_insn.address = offset;
216 mci.flat_insn.size = insn_size;
217 // allocate memory for @detail pointer
218 insn_cache[f].detail = calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800219 }
220
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800221 handle->printer(&mci, &ss, handle->printer_info);
222
Joxean114df0e2013-12-04 07:11:32 +0100223 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800224
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800225 f++;
226
227 if (f == ARR_SIZE(insn_cache)) {
228 // resize total to contain newly disasm insns
229 total_size += sizeof(insn_cache);
230 void *tmp = realloc(total, total_size);
231 if (tmp == NULL) { // insufficient memory
232 free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800233 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800234 return 0;
235 }
236
237 total = tmp;
238 memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache));
239 // reset f back to 0
240 f = 0;
241 }
242
243 c++;
244 buffer += insn_size;
245 size -= insn_size;
246 offset += insn_size;
247
248 if (count > 0 && c == count)
249 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800250 } else {
251 // encounter a broken instruction
252 // XXX: TODO: JOXEAN continue here
253 break;
254 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800255 }
256
257 if (f) {
258 // resize total to contain newly disasm insns
259 void *tmp = realloc(total, total_size + f * sizeof(insn_cache[0]));
260 if (tmp == NULL) { // insufficient memory
261 free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800262 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800263 return 0;
264 }
265
266 total = tmp;
267 memcpy(total + total_size, insn_cache, f * sizeof(insn_cache[0]));
268 }
269
270 *insn = total;
271
272 return c;
273}
274
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800275void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800276{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800277 size_t i;
278
279 // free all detail pointers
280 for (i = 0; i < count; i++)
281 free(insn[i].detail);
282
283 // then free pointer to cs_insn array
284 free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800285}
286
287// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100288const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800289{
290 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
291
292 if (!handle || handle->reg_name == NULL) {
293 return NULL;
294 }
295
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800296 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800297}
298
pancakef0e4eed2013-12-11 22:14:42 +0100299const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800300{
301 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
302
303 if (!handle || handle->insn_name == NULL) {
304 return NULL;
305 }
306
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800307 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800308}
309
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800310static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800311{
312 int i;
313
314 for (i = 0; i < max; i++) {
315 if (arr[i] == id)
316 return true;
317 }
318
319 return false;
320}
321
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800322bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800323{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800324 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800325 return false;
326
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800327 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
328
329 if (!handle->detail) {
330 handle->errnum = CS_ERR_DETAIL;
331 return false;
332 }
333
334 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800335}
336
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800337bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800338{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800339 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800340 return false;
341
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800342 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
343
344 if (!handle->detail) {
345 handle->errnum = CS_ERR_DETAIL;
346 return false;
347 }
348
349 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800350}
351
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800352bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800353{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800354 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800355 return false;
356
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800357 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
358
359 if (!handle->detail) {
360 handle->errnum = CS_ERR_DETAIL;
361 return false;
362 }
363
364 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800365}
366
367int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
368{
369 if (!ud)
370 return -1;
371
372 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
373 unsigned int count = 0, i;
374
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800375 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800376
377 switch (handle->arch) {
378 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800379 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800380 return -1;
381 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800382 for (i = 0; i < insn->detail->arm.op_count; i++)
383 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800384 count++;
385 break;
386 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800387 for (i = 0; i < insn->detail->arm64.op_count; i++)
388 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800389 count++;
390 break;
391 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800392 for (i = 0; i < insn->detail->x86.op_count; i++)
393 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800394 count++;
395 break;
396 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800397 for (i = 0; i < insn->detail->mips.op_count; i++)
398 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800399 count++;
400 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800401 case CS_ARCH_PPC:
402 for (i = 0; i < insn->detail->ppc.op_count; i++)
403 if (insn->detail->ppc.operands[i].type == op_type)
404 count++;
405 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800406 }
407
408 return count;
409}
410
411int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
412 unsigned int post)
413{
414 if (!ud)
415 return -1;
416
417 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
418 unsigned int count = 0, i;
419
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800420 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800421
422 switch (handle->arch) {
423 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800424 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800425 return -1;
426 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800427 for (i = 0; i < insn->detail->arm.op_count; i++) {
428 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800429 count++;
430 if (count == post)
431 return i;
432 }
433 break;
434 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800435 for (i = 0; i < insn->detail->arm64.op_count; i++) {
436 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800437 count++;
438 if (count == post)
439 return i;
440 }
441 break;
442 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800443 for (i = 0; i < insn->detail->x86.op_count; i++) {
444 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800445 count++;
446 if (count == post)
447 return i;
448 }
449 break;
450 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800451 for (i = 0; i < insn->detail->mips.op_count; i++) {
452 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800453 count++;
454 if (count == post)
455 return i;
456 }
457 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800458 case CS_ARCH_PPC:
459 for (i = 0; i < insn->detail->ppc.op_count; i++) {
460 if (insn->detail->ppc.operands[i].type == op_type)
461 count++;
462 if (count == post)
463 return i;
464 }
465 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800466 }
467
468 return -1;
469}