blob: 68f60d9d7e2f47c6e473b84ecfceb9e1f8e0576b [file] [log] [blame]
Nguyen Anh Quynh6023ef72014-04-29 11:21:04 +08001/* Capstone Disassembly Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
Axel 0vercl0k Souchet605faf12014-05-09 20:40:00 +01003#if defined (WIN32) || defined (WIN64) || defined (_WIN32) || defined (_WIN64)
Satoshi Tandaf177f922016-09-27 08:08:58 -07004#pragma warning(disable:4996) // disable MSVC's warning on strcpy()
5#pragma warning(disable:28719) // disable MSVC's warning on strcpy()
Axel 0vercl0k Souchet605faf12014-05-09 20:40:00 +01006#endif
reverserbcf09f42015-04-09 18:28:19 +01007#if defined(CAPSTONE_HAS_OSXKERNEL)
8#include <libkern/libkern.h>
9#else
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +080010#include <stddef.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080011#include <stdio.h>
12#include <stdlib.h>
reverserbcf09f42015-04-09 18:28:19 +010013#endif
14
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080015#include <string.h>
16#include <capstone.h>
17
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080018#include "utils.h"
Nguyen Anh Quynhc7404072014-01-05 11:35:47 +080019#include "MCRegisterInfo.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080020
tandasat1dd89872016-04-23 15:51:24 -070021#if defined(_KERNEL_MODE)
22#include "windows\winkernel_mm.h"
23#endif
24
tandasat2729f3b2016-05-16 08:32:58 -070025// Issue #681: Windows kernel does not support formatting float point
26#if defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
27#if defined(CAPSTONE_HAS_ARM) || defined(CAPSTONE_HAS_ARM64)
28#define CAPSTONE_STR_INTERNAL(x) #x
29#define CAPSTONE_STR(x) CAPSTONE_STR_INTERNAL(x)
30#define CAPSTONE_MSVC_WRANING_PREFIX __FILE__ "("CAPSTONE_STR(__LINE__)") : warning message : "
31
32#pragma message(CAPSTONE_MSVC_WRANING_PREFIX "Windows driver does not support full features for selected architecture(s). Define CAPSTONE_DIET to compile Capstone with only supported features. See issue #681 for details.")
33
34#undef CAPSTONE_MSVC_WRANING_PREFIX
35#undef CAPSTONE_STR
36#undef CAPSTONE_STR_INTERNAL
37#endif
38#endif // defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
39
tandasat1dd89872016-04-23 15:51:24 -070040#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(CAPSTONE_DIET) && !defined(_KERNEL_MODE)
Nguyen Anh Quynh472a4a42014-03-06 09:13:04 +080041#define INSN_CACHE_SIZE 32
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080042#else
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080043// reduce stack variable size for kernel/firmware
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080044#define INSN_CACHE_SIZE 8
45#endif
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +080046
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080047// default SKIPDATA mnemonic
vit96964a757e12017-05-24 08:07:11 +030048#ifndef CAPSTONE_DIET
Nguyen Anh Quynhc75a9092014-04-10 10:26:49 +080049#define SKIPDATA_MNEM ".byte"
vit96964a757e12017-05-24 08:07:11 +030050#else // No printing is available in diet mode
51#define SKIPDATA_MNEM NULL
52#endif
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080053
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080054cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080055cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
56void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080057
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080058extern void ARM_enable(void);
59extern void AArch64_enable(void);
60extern void Mips_enable(void);
61extern void X86_enable(void);
62extern void PPC_enable(void);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080063extern void Sparc_enable(void);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080064extern void SystemZ_enable(void);
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080065extern void XCore_enable(void);
danghvu701b8502014-01-09 11:06:44 +070066
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080067static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080068{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080069 static bool initialized = false;
70
71 if (initialized)
72 return;
73
danghvub33bd2c2014-01-09 12:22:56 +070074#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080075 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070076#endif
77#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080078 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070079#endif
80#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080081 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070082#endif
danghvub33bd2c2014-01-09 12:22:56 +070083#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080084 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070085#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080086#ifdef CAPSTONE_HAS_SPARC
87 Sparc_enable();
88#endif
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080089#ifdef CAPSTONE_HAS_SYSZ
90 SystemZ_enable();
91#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080092#ifdef CAPSTONE_HAS_X86
93 X86_enable();
94#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080095#ifdef CAPSTONE_HAS_XCORE
96 XCore_enable();
97#endif
98
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080099
100 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +0700101}
102
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800103unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +0800104
tandasat1dd89872016-04-23 15:51:24 -0700105#if defined(CAPSTONE_USE_SYS_DYN_MEM)
106#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800107cs_malloc_t cs_mem_malloc = malloc;
108cs_calloc_t cs_mem_calloc = calloc;
109cs_realloc_t cs_mem_realloc = realloc;
110cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800111cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasat1dd89872016-04-23 15:51:24 -0700112#elif defined(_KERNEL_MODE)
113cs_malloc_t cs_mem_malloc = cs_winkernel_malloc;
114cs_calloc_t cs_mem_calloc = cs_winkernel_calloc;
115cs_realloc_t cs_mem_realloc = cs_winkernel_realloc;
116cs_free_t cs_mem_free = cs_winkernel_free;
117cs_vsnprintf_t cs_vsnprintf = cs_winkernel_vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800118#else
Pbb8741bd2015-11-10 23:02:26 +0100119extern void* kern_os_malloc(size_t size);
120extern void kern_os_free(void* addr);
121extern void* kern_os_realloc(void* addr, size_t nsize);
122
123static void* cs_kern_os_calloc(size_t num, size_t size)
124{
125 return kern_os_malloc(num * size); // malloc bzeroes the buffer
126}
127
128cs_malloc_t cs_mem_malloc = kern_os_malloc;
129cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
130cs_realloc_t cs_mem_realloc = kern_os_realloc;
131cs_free_t cs_mem_free = kern_os_free;
132cs_vsnprintf_t cs_vsnprintf = vsnprintf;
133#endif
134#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800135cs_malloc_t cs_mem_malloc = NULL;
136cs_calloc_t cs_mem_calloc = NULL;
137cs_realloc_t cs_mem_realloc = NULL;
138cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800139cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800140#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800141
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800142CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700143unsigned int CAPSTONE_API cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800144{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800145 archs_enable();
146
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800147 if (major != NULL && minor != NULL) {
148 *major = CS_API_MAJOR;
149 *minor = CS_API_MINOR;
150 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800151
152 return (CS_API_MAJOR << 8) + CS_API_MINOR;
153}
154
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800155CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700156bool CAPSTONE_API cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800157{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800158 archs_enable();
159
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800160 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800161 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800162 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800163 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800164 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800165
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800166 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800167 return all_arch & (1 << query);
168
169 if (query == CS_SUPPORT_DIET) {
170#ifdef CAPSTONE_DIET
171 return true;
172#else
173 return false;
174#endif
175 }
176
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800177 if (query == CS_SUPPORT_X86_REDUCE) {
178#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800179 return true;
180#else
181 return false;
182#endif
183 }
184
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800185 // unsupported query
186 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800187}
188
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800189CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700190cs_err CAPSTONE_API cs_errno(csh handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800191{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800192 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800193 if (!handle)
194 return CS_ERR_CSH;
195
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100196 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800197
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800198 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800199}
200
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800201CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700202const char * CAPSTONE_API cs_strerror(cs_err code)
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800203{
204 switch(code) {
205 default:
206 return "Unknown error code";
207 case CS_ERR_OK:
208 return "OK (CS_ERR_OK)";
209 case CS_ERR_MEM:
210 return "Out of memory (CS_ERR_MEM)";
211 case CS_ERR_ARCH:
212 return "Invalid architecture (CS_ERR_ARCH)";
213 case CS_ERR_HANDLE:
214 return "Invalid handle (CS_ERR_HANDLE)";
215 case CS_ERR_CSH:
216 return "Invalid csh (CS_ERR_CSH)";
217 case CS_ERR_MODE:
218 return "Invalid mode (CS_ERR_MODE)";
219 case CS_ERR_OPTION:
220 return "Invalid option (CS_ERR_OPTION)";
221 case CS_ERR_DETAIL:
222 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800223 case CS_ERR_MEMSETUP:
224 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800225 case CS_ERR_VERSION:
226 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800227 case CS_ERR_DIET:
228 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800229 case CS_ERR_SKIPDATA:
230 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800231 }
232}
233
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800234CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700235cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800236{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800237 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800238 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800239 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800240 // Error: before cs_open(), dynamic memory management must be initialized
241 // with cs_option(CS_OPT_MEM)
242 return CS_ERR_MEMSETUP;
243
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800244 archs_enable();
245
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800246 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800247 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800248 if (!ud) {
249 // memory insufficient
250 return CS_ERR_MEM;
251 }
252
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800253 ud->errnum = CS_ERR_OK;
254 ud->arch = arch;
255 ud->mode = mode;
tandasatb3d38cf2016-05-06 17:03:57 -0700256 ud->big_endian = (mode & CS_MODE_BIG_ENDIAN) != 0;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800257 // by default, do not break instruction into details
258 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800259
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800260 // default skipdata setup
261 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
262
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100263 err = arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800264 if (err) {
265 cs_mem_free(ud);
266 *handle = 0;
267 return err;
268 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800269
270 *handle = (uintptr_t)ud;
271
272 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800273 } else {
274 *handle = 0;
275 return CS_ERR_ARCH;
276 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800277}
278
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800279CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700280cs_err CAPSTONE_API cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800281{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800282 struct cs_struct *ud;
283
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800284 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800285 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800286 return CS_ERR_CSH;
287
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100288 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800289
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800290 if (ud->printer_info)
291 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800292
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800293 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800294
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800295 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800296 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800297
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800298 // invalidate this handle by ZERO out its value.
299 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800300 *handle = 0;
301
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800302 return CS_ERR_OK;
303}
304
305// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800306static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800307 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800308{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800309#ifndef CAPSTONE_DIET
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700310 char *sp, *mnem;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800311#endif
tandasata6609fe2016-05-04 05:54:28 -0700312 uint16_t copy_size = MIN(sizeof(insn->bytes), insn->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800313
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800314 // fill the instruction bytes.
315 // we might skip some redundant bytes in front in the case of X86
316 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
317 insn->size = copy_size;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800318
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800319 // alias instruction might have ID saved in OpcodePub
320 if (MCInst_getOpcodePub(mci))
321 insn->id = MCInst_getOpcodePub(mci);
322
323 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800324 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800325 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800326
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800327#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800328 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800329 // find first space or tab
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800330 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800331 for (sp = buffer; *sp; sp++) {
332 if (*sp == ' '|| *sp == '\t')
333 break;
334 if (*sp == '|') // lock|rep prefix for x86
335 *sp = ' ';
336 // copy to @mnemonic
337 *mnem = *sp;
338 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800339 }
340
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800341 *mnem = '\0';
342
343 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800344 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800345 // find the next non-space char
346 sp++;
347 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
348 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800349 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
350 } else
351 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800352#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800353}
354
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800355// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800356// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800357static uint8_t skipdata_size(cs_struct *handle)
358{
359 switch(handle->arch) {
360 default:
361 // should never reach
tandasat59df5502016-05-14 16:04:28 -0700362 return (uint8_t)-1;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800363 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800364 // skip 2 bytes on Thumb mode.
365 if (handle->mode & CS_MODE_THUMB)
366 return 2;
367 // otherwise, skip 4 bytes
368 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800369 case CS_ARCH_ARM64:
370 case CS_ARCH_MIPS:
371 case CS_ARCH_PPC:
372 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800373 // skip 4 bytes
374 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800375 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800376 // SystemZ instruction's length can be 2, 4 or 6 bytes,
377 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800378 return 2;
379 case CS_ARCH_X86:
380 // X86 has no restriction on instruction alignment
381 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800382 case CS_ARCH_XCORE:
383 // XCore instruction's length can be 2 or 4 bytes,
384 // so we just skip 2 bytes
385 return 2;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800386 }
387}
388
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800389CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700390cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800391{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800392 struct cs_struct *handle;
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800393 archs_enable();
394
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800395 // cs_option() can be called with NULL handle just for CS_OPT_MEM
396 // This is supposed to be executed before all other APIs (even cs_open())
397 if (type == CS_OPT_MEM) {
398 cs_opt_mem *mem = (cs_opt_mem *)value;
399
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800400 cs_mem_malloc = mem->malloc;
401 cs_mem_calloc = mem->calloc;
402 cs_mem_realloc = mem->realloc;
403 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800404 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800405
406 return CS_ERR_OK;
407 }
408
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100409 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600410 if (!handle)
411 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800412
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800413 switch(type) {
414 default:
415 break;
416 case CS_OPT_DETAIL:
Félix Cloutierc141af92015-03-02 22:06:56 -0500417 handle->detail = (cs_opt_value)value;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800418 return CS_ERR_OK;
419 case CS_OPT_SKIPDATA:
420 handle->skipdata = (value == CS_OPT_ON);
421 if (handle->skipdata) {
422 if (handle->skipdata_size == 0) {
423 // set the default skipdata size
424 handle->skipdata_size = skipdata_size(handle);
425 }
426 }
427 return CS_ERR_OK;
428 case CS_OPT_SKIPDATA_SETUP:
429 if (value)
430 handle->skipdata_setup = *((cs_opt_skipdata *)value);
431 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800432 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800433
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800434 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800435}
436
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800437// generate @op_str for data instruction of SKIPDATA
vit96964a757e12017-05-24 08:07:11 +0300438#ifndef CAPSTONE_DIET
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800439static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
440{
441 char *p = opstr;
442 int len;
443 size_t i;
tandasat760940f2016-03-31 18:14:43 -0700444 size_t available = sizeof(((cs_insn*)NULL)->op_str);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800445
446 if (!size) {
447 opstr[0] = '\0';
448 return;
449 }
450
tandasat760940f2016-03-31 18:14:43 -0700451 len = cs_snprintf(p, available, "0x%02x", buffer[0]);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800452 p+= len;
tandasat760940f2016-03-31 18:14:43 -0700453 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800454
455 for(i = 1; i < size; i++) {
tandasat760940f2016-03-31 18:14:43 -0700456 len = cs_snprintf(p, available, ", 0x%02x", buffer[i]);
457 if (len < 0) {
458 break;
459 }
460 if ((size_t)len > available - 1) {
461 break;
462 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800463 p+= len;
tandasat760940f2016-03-31 18:14:43 -0700464 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800465 }
466}
vit96964a757e12017-05-24 08:07:11 +0300467#endif
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800468
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800469// dynamicly allocate memory to contain disasm insn
470// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800471CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700472size_t CAPSTONE_API cs_disasm(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 +0800473{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800474 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800475 MCInst mci;
476 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800477 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800478 unsigned int f = 0; // index of the next instruction in the cache
479 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800480 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800481 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800482 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800483 void *tmp;
484 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800485 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800486 size_t size_org;
487 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800488 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500489 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800490
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800491 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800492 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800493 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800494 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800495 return 0;
496 }
497
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800498 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800499
Nguyen Anh Quynhc2cb0e72016-04-27 14:43:10 +0800500 // reset IT block of ARM structure
Nguyen Anh Quynh0c864402016-04-27 14:47:05 +0800501 if (handle->arch == CS_ARCH_ARM)
502 handle->ITBlock.size = 0;
Nguyen Anh Quynhc2cb0e72016-04-27 14:43:10 +0800503
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800504#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800505 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400506 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800507#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800508
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800509 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800510 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800511 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800512 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800513
514 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700515 total = cs_mem_malloc(total_size);
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800516 if (total == NULL) {
517 // insufficient memory
Edward Williamsonf1e49752014-12-14 20:45:19 -0500518 handle->errnum = CS_ERR_MEM;
519 return 0;
520 }
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800521
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700522 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800523
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800524 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800525 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800526 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800527
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700528 // relative branches need to know the address & size of current insn
529 mci.address = offset;
530
531 if (handle->detail) {
532 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700533 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700534 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700535 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700536 }
537
538 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700539 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700540 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800541#ifdef CAPSTONE_DIET
542 // zero out mnemonic & op_str
543 mci.flat_insn->mnemonic[0] = '\0';
544 mci.flat_insn->op_str[0] = '\0';
545#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700546
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800547 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800548 if (r) {
549 SStream ss;
550 SStream_Init(&ss);
551
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700552 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800553
554 // map internal instruction opcode to public insn ID
vit96964a757e12017-05-24 08:07:11 +0300555
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800556 handle->insn_id(handle, insn_cache, mci.Opcode);
557
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800558 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800559
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700560 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800561
danghvu0d1aad12014-10-01 23:12:18 -0500562 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800563 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800564 // encounter a broken instruction
565
566 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800567 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800568 cs_mem_free(insn_cache->detail);
569 }
570
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800571 // if there is no request to skip data, or remaining data is too small,
572 // then bail out
573 if (!handle->skipdata || handle->skipdata_size > size)
574 break;
575
576 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800577 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800578 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800579 if (skipdata_bytes > size)
580 // remaining data is not enough
581 break;
582
583 if (!skipdata_bytes)
584 // user requested not to skip data, so bail out
585 break;
586 } else
587 skipdata_bytes = handle->skipdata_size;
588
589 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700590 insn_cache->id = 0; // invalid ID for this "data" instruction
591 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800592 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700593 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
vit96964a757e12017-05-24 08:07:11 +0300594#ifdef CAPSTONE_DIET
595 insn_cache->mnemonic[0] = '\0';
596 insn_cache->op_str[0] = '\0';
597#else
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700598 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
vit96964a757e12017-05-24 08:07:11 +0300599 sizeof(insn_cache->mnemonic) - 1);
600 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
601#endif
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700602 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800603
danghvu0d1aad12014-10-01 23:12:18 -0500604 next_offset = skipdata_bytes;
605 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800606
danghvu0d1aad12014-10-01 23:12:18 -0500607 // one more instruction entering the cache
608 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800609
danghvu0d1aad12014-10-01 23:12:18 -0500610 // one more instruction disassembled
611 c++;
612 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800613 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500614 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800615
danghvu0d1aad12014-10-01 23:12:18 -0500616 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800617 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500618 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500619 total_size += (sizeof(cs_insn) * cache_size);
620 tmp = cs_mem_realloc(total, total_size);
621 if (tmp == NULL) { // insufficient memory
622 if (handle->detail) {
623 insn_cache = (cs_insn *)total;
624 for (i = 0; i < c; i++, insn_cache++)
625 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800626 }
627
danghvu0d1aad12014-10-01 23:12:18 -0500628 cs_mem_free(total);
629 *insn = NULL;
630 handle->errnum = CS_ERR_MEM;
631 return 0;
632 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800633
danghvu0d1aad12014-10-01 23:12:18 -0500634 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800635 // continue to fill in the cache after the last instruction
636 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800637
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800638 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500639 f = 0;
640 } else
641 insn_cache++;
642
643 buffer += next_offset;
644 size -= next_offset;
645 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800646 }
647
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800648 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800649 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800650 cs_mem_free(total);
651 total = NULL;
652 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800653 // total did not fully use the last cache, so downsize it
Tyler J. Stachecki30d11672015-10-23 20:59:20 -0400654 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800655 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800656 // free all detail pointers
657 if (handle->detail) {
658 insn_cache = (cs_insn *)total;
659 for (i = 0; i < c; i++, insn_cache++)
660 cs_mem_free(insn_cache->detail);
661 }
662
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800663 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800664 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800665
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800666 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800667 return 0;
668 }
669
670 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800671 }
672
673 *insn = total;
674
675 return c;
676}
677
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800678CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800679CAPSTONE_DEPRECATED
tandasat760940f2016-03-31 18:14:43 -0700680size_t CAPSTONE_API cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800681{
682 return cs_disasm(ud, buffer, size, offset, count, insn);
683}
684
685CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700686void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800687{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800688 size_t i;
689
690 // free all detail pointers
691 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800692 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800693
694 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800695 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800696}
697
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800698CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700699cs_insn * CAPSTONE_API cs_malloc(csh ud)
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800700{
701 cs_insn *insn;
702 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
703
704 insn = cs_mem_malloc(sizeof(cs_insn));
705 if (!insn) {
706 // insufficient memory
707 handle->errnum = CS_ERR_MEM;
708 return NULL;
709 } else {
710 if (handle->detail) {
711 // allocate memory for @detail pointer
712 insn->detail = cs_mem_malloc(sizeof(cs_detail));
713 if (insn->detail == NULL) { // insufficient memory
714 cs_mem_free(insn);
715 handle->errnum = CS_ERR_MEM;
716 return NULL;
717 }
718 } else
719 insn->detail = NULL;
720 }
721
722 return insn;
723}
724
hlide993f3622014-10-05 18:14:40 +0200725// iterator for instruction "single-stepping"
726CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700727bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800728 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200729{
730 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200731 uint16_t insn_size;
732 MCInst mci;
733 bool r;
734
735 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800736 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800737 return false;
hlide993f3622014-10-05 18:14:40 +0200738 }
739
740 handle->errnum = CS_ERR_OK;
741
hlide993f3622014-10-05 18:14:40 +0200742 MCInst_Init(&mci);
743 mci.csh = handle;
744
745 // relative branches need to know the address & size of current insn
746 mci.address = *address;
747
748 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800749 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200750 mci.flat_insn->address = *address;
751#ifdef CAPSTONE_DIET
752 // zero out mnemonic & op_str
753 mci.flat_insn->mnemonic[0] = '\0';
754 mci.flat_insn->op_str[0] = '\0';
755#endif
756
757 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800758 if (r) {
hlide993f3622014-10-05 18:14:40 +0200759 SStream ss;
760 SStream_Init(&ss);
761
762 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800763
764 // map internal instruction opcode to public insn ID
765 handle->insn_id(handle, insn, mci.Opcode);
766
hlide993f3622014-10-05 18:14:40 +0200767 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800768
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800769 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800770
hlide993f3622014-10-05 18:14:40 +0200771 *code += insn_size;
772 *size -= insn_size;
773 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800774 } else { // encounter a broken instruction
775 size_t skipdata_bytes;
776
777 // if there is no request to skip data, or remaining data is too small,
778 // then bail out
779 if (!handle->skipdata || handle->skipdata_size > *size)
780 return false;
781
782 if (handle->skipdata_setup.callback) {
783 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
784 0, handle->skipdata_setup.user_data);
785 if (skipdata_bytes > *size)
786 // remaining data is not enough
787 return false;
788
789 if (!skipdata_bytes)
790 // user requested not to skip data, so bail out
791 return false;
792 } else
793 skipdata_bytes = handle->skipdata_size;
794
795 // we have to skip some amount of data, depending on arch & mode
796 insn->id = 0; // invalid ID for this "data" instruction
797 insn->address = *address;
798 insn->size = (uint16_t)skipdata_bytes;
799 memcpy(insn->bytes, *code, skipdata_bytes);
vit96964a757e12017-05-24 08:07:11 +0300800#ifdef CAPSTONE_DIET
801 insn->mnemonic[0] = '\0';
802 insn->op_str[0] = '\0';
803#else
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800804 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
805 sizeof(insn->mnemonic) - 1);
806 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
vit96964a757e12017-05-24 08:07:11 +0300807#endif
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800808
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800809 *code += skipdata_bytes;
810 *size -= skipdata_bytes;
811 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200812 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800813
814 return true;
hlide993f3622014-10-05 18:14:40 +0200815}
816
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800817// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800818CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700819const char * CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800820{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800821 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800822
823 if (!handle || handle->reg_name == NULL) {
824 return NULL;
825 }
826
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800827 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800828}
829
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800830CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700831const char * CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800832{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800833 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800834
835 if (!handle || handle->insn_name == NULL) {
836 return NULL;
837 }
838
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800839 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800840}
841
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800842CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700843const char * CAPSTONE_API cs_group_name(csh ud, unsigned int group)
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800844{
845 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
846
847 if (!handle || handle->group_name == NULL) {
848 return NULL;
849 }
850
851 return handle->group_name(ud, group);
852}
853
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800854static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800855{
856 int i;
857
858 for (i = 0; i < max; i++) {
859 if (arr[i] == id)
860 return true;
861 }
862
863 return false;
864}
865
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800866CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700867bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800868{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800869 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800870 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800871 return false;
872
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100873 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200874
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800875 if (!handle->detail) {
876 handle->errnum = CS_ERR_DETAIL;
877 return false;
878 }
879
Giovanni Condello95657e02014-05-07 17:31:27 +0200880 if(!insn->id) {
881 handle->errnum = CS_ERR_SKIPDATA;
882 return false;
883 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200884
Giovanni Condello95657e02014-05-07 17:31:27 +0200885 if(!insn->detail) {
886 handle->errnum = CS_ERR_DETAIL;
887 return false;
888 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200889
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800890 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800891}
892
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800893CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700894bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800895{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800896 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800897 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800898 return false;
899
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100900 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200901
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800902 if (!handle->detail) {
903 handle->errnum = CS_ERR_DETAIL;
904 return false;
905 }
906
Giovanni Condello95657e02014-05-07 17:31:27 +0200907 if(!insn->id) {
908 handle->errnum = CS_ERR_SKIPDATA;
909 return false;
910 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200911
Giovanni Condello95657e02014-05-07 17:31:27 +0200912 if(!insn->detail) {
913 handle->errnum = CS_ERR_DETAIL;
914 return false;
915 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200916
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800917 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800918}
919
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800920CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700921bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800922{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800923 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800924 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800925 return false;
926
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100927 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +0200928
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800929 if (!handle->detail) {
930 handle->errnum = CS_ERR_DETAIL;
931 return false;
932 }
933
Giovanni Condello95657e02014-05-07 17:31:27 +0200934 if(!insn->id) {
935 handle->errnum = CS_ERR_SKIPDATA;
936 return false;
937 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200938
Giovanni Condello95657e02014-05-07 17:31:27 +0200939 if(!insn->detail) {
940 handle->errnum = CS_ERR_DETAIL;
941 return false;
942 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200943
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800944 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800945}
946
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800947CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700948int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800949{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800950 struct cs_struct *handle;
951 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800952 if (!ud)
953 return -1;
954
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100955 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200956
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800957 if (!handle->detail) {
958 handle->errnum = CS_ERR_DETAIL;
959 return -1;
960 }
961
Giovanni Condello95657e02014-05-07 17:31:27 +0200962 if(!insn->id) {
963 handle->errnum = CS_ERR_SKIPDATA;
964 return -1;
965 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200966
Giovanni Condello95657e02014-05-07 17:31:27 +0200967 if(!insn->detail) {
968 handle->errnum = CS_ERR_DETAIL;
969 return -1;
970 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200971
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800972 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800973
974 switch (handle->arch) {
975 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800976 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800977 return -1;
978 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800979 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800980 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800981 count++;
982 break;
983 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800984 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800985 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800986 count++;
987 break;
988 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800989 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800990 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800991 count++;
992 break;
993 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800994 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800995 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800996 count++;
997 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800998 case CS_ARCH_PPC:
999 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001000 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001001 count++;
1002 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001003 case CS_ARCH_SPARC:
1004 for (i = 0; i < insn->detail->sparc.op_count; i++)
1005 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1006 count++;
1007 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001008 case CS_ARCH_SYSZ:
1009 for (i = 0; i < insn->detail->sysz.op_count; i++)
1010 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1011 count++;
1012 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001013 case CS_ARCH_XCORE:
1014 for (i = 0; i < insn->detail->xcore.op_count; i++)
1015 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1016 count++;
1017 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001018 }
1019
1020 return count;
1021}
1022
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001023CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -07001024int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001025 unsigned int post)
1026{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001027 struct cs_struct *handle;
1028 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001029 if (!ud)
1030 return -1;
1031
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001032 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001033
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001034 if (!handle->detail) {
1035 handle->errnum = CS_ERR_DETAIL;
1036 return -1;
1037 }
1038
Giovanni Condello95657e02014-05-07 17:31:27 +02001039 if(!insn->id) {
1040 handle->errnum = CS_ERR_SKIPDATA;
1041 return -1;
1042 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001043
Giovanni Condello95657e02014-05-07 17:31:27 +02001044 if(!insn->detail) {
1045 handle->errnum = CS_ERR_DETAIL;
1046 return -1;
1047 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001048
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001049 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001050
1051 switch (handle->arch) {
1052 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001053 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001054 return -1;
1055 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001056 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001057 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001058 count++;
1059 if (count == post)
1060 return i;
1061 }
1062 break;
1063 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001064 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001065 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001066 count++;
1067 if (count == post)
1068 return i;
1069 }
1070 break;
1071 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001072 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001073 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001074 count++;
1075 if (count == post)
1076 return i;
1077 }
1078 break;
1079 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001080 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001081 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001082 count++;
1083 if (count == post)
1084 return i;
1085 }
1086 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001087 case CS_ARCH_PPC:
1088 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001089 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001090 count++;
1091 if (count == post)
1092 return i;
1093 }
1094 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001095 case CS_ARCH_SPARC:
1096 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1097 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1098 count++;
1099 if (count == post)
1100 return i;
1101 }
1102 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001103 case CS_ARCH_SYSZ:
1104 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1105 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1106 count++;
1107 if (count == post)
1108 return i;
1109 }
1110 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001111 case CS_ARCH_XCORE:
1112 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1113 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1114 count++;
1115 if (count == post)
1116 return i;
1117 }
1118 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001119 }
1120
1121 return -1;
1122}