blob: 36d578555b41a3c7355bfef3869903bd0d3d0fc3 [file] [log] [blame]
Nguyen Anh Quynh6023ef72014-04-29 11:21:04 +08001/* Capstone Disassembly Engine */
Nguyen Anh Quynhbfcaba52015-03-04 17:45:23 +08002/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */
Axel 0vercl0k Souchet605faf12014-05-09 20:40:00 +01003#if defined (WIN32) || defined (WIN64) || defined (_WIN32) || defined (_WIN64)
4#pragma warning(disable:4996)
5#endif
reverser160e1982015-04-09 18:28:19 +01006#if defined(CAPSTONE_HAS_OSXKERNEL)
7#include <libkern/libkern.h>
8#else
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08009#include <stddef.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080010#include <stdio.h>
11#include <stdlib.h>
reverser160e1982015-04-09 18:28:19 +010012#endif
13
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080014#include <string.h>
pancake9c10ace2015-02-24 04:55:55 +010015#include <capstone/capstone.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080016
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080017#include "utils.h"
Nguyen Anh Quynhc7404072014-01-05 11:35:47 +080018#include "MCRegisterInfo.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080019
Pba21cc8c2015-11-10 22:43:51 +010020#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(CAPSTONE_DIET)
Nguyen Anh Quynh472a4a42014-03-06 09:13:04 +080021#define INSN_CACHE_SIZE 32
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080022#else
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080023// reduce stack variable size for kernel/firmware
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080024#define INSN_CACHE_SIZE 8
25#endif
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +080026
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080027// default SKIPDATA mnemonic
Nguyen Anh Quynhc75a9092014-04-10 10:26:49 +080028#define SKIPDATA_MNEM ".byte"
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080029
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080030cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080031cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
32void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080033
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080034extern void ARM_enable(void);
35extern void AArch64_enable(void);
Daniel Collin2ee675c2015-08-03 18:45:08 +020036extern void M68K_enable(void);
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080037extern void Mips_enable(void);
38extern void X86_enable(void);
39extern void PPC_enable(void);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080040extern void Sparc_enable(void);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080041extern void SystemZ_enable(void);
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080042extern void XCore_enable(void);
danghvu701b8502014-01-09 11:06:44 +070043
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080044static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080045{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080046 static bool initialized = false;
47
48 if (initialized)
49 return;
50
danghvub33bd2c2014-01-09 12:22:56 +070051#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080052 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070053#endif
54#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080055 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070056#endif
Daniel Collin2ee675c2015-08-03 18:45:08 +020057#ifdef CAPSTONE_HAS_M68K
58 M68K_enable();
59#endif
danghvub33bd2c2014-01-09 12:22:56 +070060#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080061 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070062#endif
danghvub33bd2c2014-01-09 12:22:56 +070063#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080064 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070065#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080066#ifdef CAPSTONE_HAS_SPARC
67 Sparc_enable();
68#endif
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080069#ifdef CAPSTONE_HAS_SYSZ
70 SystemZ_enable();
71#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080072#ifdef CAPSTONE_HAS_X86
73 X86_enable();
74#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080075#ifdef CAPSTONE_HAS_XCORE
76 XCore_enable();
77#endif
78
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080079
80 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +070081}
82
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080083unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080084
Axel 0vercl0k Souchet84fecf22014-05-10 09:49:29 +010085#ifdef CAPSTONE_USE_SYS_DYN_MEM
Pb86ad3d62015-11-05 18:08:25 +010086#ifndef CAPSTONE_HAS_OSXKERNEL
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080087cs_malloc_t cs_mem_malloc = malloc;
88cs_calloc_t cs_mem_calloc = calloc;
89cs_realloc_t cs_mem_realloc = realloc;
90cs_free_t cs_mem_free = free;
Koutheir Attouchi201a0a12016-04-12 09:25:37 +020091#if defined(_WIN32_WCE)
92cs_vsnprintf_t cs_vsnprintf = _vsnprintf;
93#else // !_WIN32_WCE
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080094cs_vsnprintf_t cs_vsnprintf = vsnprintf;
Koutheir Attouchi201a0a12016-04-12 09:25:37 +020095#endif // _WIN32_WCE
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080096#else
Pb86ad3d62015-11-05 18:08:25 +010097extern void* kern_os_malloc(size_t size);
98extern void kern_os_free(void* addr);
99extern void* kern_os_realloc(void* addr, size_t nsize);
100
Nguyen Anh Quynh197e4572015-11-10 22:56:53 +0800101static void* cs_kern_os_calloc(size_t num, size_t size)
102{
Pb86ad3d62015-11-05 18:08:25 +0100103 return kern_os_malloc(num * size); // malloc bzeroes the buffer
104}
105
106cs_malloc_t cs_mem_malloc = kern_os_malloc;
Pb2769c772015-11-06 14:44:55 +0100107cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
Pb86ad3d62015-11-05 18:08:25 +0100108cs_realloc_t cs_mem_realloc = kern_os_realloc;
109cs_free_t cs_mem_free = kern_os_free;
110cs_vsnprintf_t cs_vsnprintf = vsnprintf;
111#endif
112#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800113cs_malloc_t cs_mem_malloc = NULL;
114cs_calloc_t cs_mem_calloc = NULL;
115cs_realloc_t cs_mem_realloc = NULL;
116cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800117cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800118#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800119
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800120CAPSTONE_EXPORT
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +0800121unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800122{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800123 archs_enable();
124
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800125 if (major != NULL && minor != NULL) {
126 *major = CS_API_MAJOR;
127 *minor = CS_API_MINOR;
128 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800129
130 return (CS_API_MAJOR << 8) + CS_API_MINOR;
131}
132
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800133CAPSTONE_EXPORT
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800134bool cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800135{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800136 archs_enable();
137
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800138 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800139 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800140 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800141 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800142 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800143
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800144 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800145 return all_arch & (1 << query);
146
147 if (query == CS_SUPPORT_DIET) {
148#ifdef CAPSTONE_DIET
149 return true;
150#else
151 return false;
152#endif
153 }
154
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800155 if (query == CS_SUPPORT_X86_REDUCE) {
156#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800157 return true;
158#else
159 return false;
160#endif
161 }
162
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800163 // unsupported query
164 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800165}
166
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800167CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800168cs_err cs_errno(csh handle)
169{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800170 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800171 if (!handle)
172 return CS_ERR_CSH;
173
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100174 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800175
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800176 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800177}
178
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800179CAPSTONE_EXPORT
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800180const char *cs_strerror(cs_err code)
181{
182 switch(code) {
183 default:
184 return "Unknown error code";
185 case CS_ERR_OK:
186 return "OK (CS_ERR_OK)";
187 case CS_ERR_MEM:
188 return "Out of memory (CS_ERR_MEM)";
189 case CS_ERR_ARCH:
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800190 return "Invalid/unsupported architecture(CS_ERR_ARCH)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800191 case CS_ERR_HANDLE:
192 return "Invalid handle (CS_ERR_HANDLE)";
193 case CS_ERR_CSH:
194 return "Invalid csh (CS_ERR_CSH)";
195 case CS_ERR_MODE:
196 return "Invalid mode (CS_ERR_MODE)";
197 case CS_ERR_OPTION:
198 return "Invalid option (CS_ERR_OPTION)";
199 case CS_ERR_DETAIL:
200 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800201 case CS_ERR_MEMSETUP:
202 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800203 case CS_ERR_VERSION:
204 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800205 case CS_ERR_DIET:
206 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800207 case CS_ERR_SKIPDATA:
208 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh8aba4cd2015-08-09 10:52:18 -0700209 case CS_ERR_X86_ATT:
210 return "AT&T syntax is unavailable (CS_ERR_X86_ATT)";
211 case CS_ERR_X86_INTEL:
212 return "INTEL syntax is unavailable (CS_ERR_X86_INTEL)";
213 case CS_ERR_X86_MASM:
214 return "MASM syntax is unavailable (CS_ERR_X86_MASM)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800215 }
216}
217
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800218CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800219cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
220{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800221 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800222 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800223 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800224 // Error: before cs_open(), dynamic memory management must be initialized
225 // with cs_option(CS_OPT_MEM)
226 return CS_ERR_MEMSETUP;
227
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800228 archs_enable();
229
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800230 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800231 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800232 if (!ud) {
233 // memory insufficient
234 return CS_ERR_MEM;
235 }
236
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800237 ud->errnum = CS_ERR_OK;
238 ud->arch = arch;
239 ud->mode = mode;
240 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800241 // by default, do not break instruction into details
242 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800243
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800244 // default skipdata setup
245 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
246
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100247 err = arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800248 if (err) {
249 cs_mem_free(ud);
250 *handle = 0;
251 return err;
252 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800253
254 *handle = (uintptr_t)ud;
255
256 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800257 } else {
258 *handle = 0;
259 return CS_ERR_ARCH;
260 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800261}
262
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800263CAPSTONE_EXPORT
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800264cs_err cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800265{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800266 struct cs_struct *ud;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800267 struct insn_mnem *next, *tmp;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800268
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800269 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800270 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800271 return CS_ERR_CSH;
272
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100273 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800274
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800275 if (ud->printer_info)
276 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800277
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800278 // free the linked list of customized mnemonic
279 tmp = ud->mnem_list;
280 while(tmp) {
281 next = tmp->next;
282 cs_mem_free(tmp);
283 tmp = next;
284 }
285
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800286 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800287
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800288 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800289 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800290
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800291 // invalidate this handle by ZERO out its value.
292 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800293 *handle = 0;
294
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800295 return CS_ERR_OK;
296}
297
298// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800299static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800300 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800301{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800302#ifndef CAPSTONE_DIET
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700303 char *sp, *mnem;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800304#endif
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800305 unsigned int copy_size = MIN(sizeof(insn->bytes), insn->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800306
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800307 // fill the instruction bytes.
308 // we might skip some redundant bytes in front in the case of X86
309 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
310 insn->size = copy_size;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800311
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800312 // alias instruction might have ID saved in OpcodePub
313 if (MCInst_getOpcodePub(mci))
314 insn->id = MCInst_getOpcodePub(mci);
315
316 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800317 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800318 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800319
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800320#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800321 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800322 // find first space or tab
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100323 sp = buffer;
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800324 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800325 for (sp = buffer; *sp; sp++) {
326 if (*sp == ' '|| *sp == '\t')
327 break;
328 if (*sp == '|') // lock|rep prefix for x86
329 *sp = ' ';
330 // copy to @mnemonic
331 *mnem = *sp;
332 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800333 }
334
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800335 *mnem = '\0';
336
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800337 // we might have customized mnemonic
338 if (handle->mnem_list) {
339 struct insn_mnem *tmp = handle->mnem_list;
340 while(tmp) {
341 if (tmp->insn.id == insn->id) {
342 // found this instruction, so copy its mnemonic
343 (void)strncpy(insn->mnemonic, tmp->insn.mnemonic, sizeof(insn->mnemonic) - 1);
344 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
345 break;
346 }
347 tmp = tmp->next;
348 }
349 }
350
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800351 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800352 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800353 // find the next non-space char
354 sp++;
355 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
356 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800357 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
358 } else
359 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800360#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800361}
362
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800363// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800364// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800365static uint8_t skipdata_size(cs_struct *handle)
366{
367 switch(handle->arch) {
368 default:
369 // should never reach
370 return -1;
371 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800372 // skip 2 bytes on Thumb mode.
373 if (handle->mode & CS_MODE_THUMB)
374 return 2;
375 // otherwise, skip 4 bytes
376 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800377 case CS_ARCH_ARM64:
378 case CS_ARCH_MIPS:
379 case CS_ARCH_PPC:
380 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800381 // skip 4 bytes
382 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800383 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800384 // SystemZ instruction's length can be 2, 4 or 6 bytes,
385 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800386 return 2;
387 case CS_ARCH_X86:
388 // X86 has no restriction on instruction alignment
389 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800390 case CS_ARCH_XCORE:
391 // XCore instruction's length can be 2 or 4 bytes,
392 // so we just skip 2 bytes
393 return 2;
Daniel Collin2ee675c2015-08-03 18:45:08 +0200394 case CS_ARCH_M68K:
395 // M68K has 2 bytes instruction alignment but contain multibyte instruction so we skip 2 bytes
396 return 2;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800397 }
398}
399
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800400CAPSTONE_EXPORT
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800401cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800402{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800403 struct cs_struct *handle;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800404 cs_opt_mnem *opt;
405
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800406 archs_enable();
407
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800408 // cs_option() can be called with NULL handle just for CS_OPT_MEM
409 // This is supposed to be executed before all other APIs (even cs_open())
410 if (type == CS_OPT_MEM) {
411 cs_opt_mem *mem = (cs_opt_mem *)value;
412
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800413 cs_mem_malloc = mem->malloc;
414 cs_mem_calloc = mem->calloc;
415 cs_mem_realloc = mem->realloc;
416 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800417 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800418
419 return CS_ERR_OK;
420 }
421
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100422 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600423 if (!handle)
424 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800425
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800426 switch(type) {
427 default:
428 break;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800429
Nguyen Anh Quynh256090a2016-03-14 13:52:23 +0800430 case CS_OPT_UNSIGNED:
431 handle->imm_unsigned = (cs_opt_value)value;
432 return CS_ERR_OK;
433
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800434 case CS_OPT_DETAIL:
Félix Cloutier3973d8b2015-03-02 22:06:56 -0500435 handle->detail = (cs_opt_value)value;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800436 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800437
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800438 case CS_OPT_SKIPDATA:
439 handle->skipdata = (value == CS_OPT_ON);
440 if (handle->skipdata) {
441 if (handle->skipdata_size == 0) {
442 // set the default skipdata size
443 handle->skipdata_size = skipdata_size(handle);
444 }
445 }
446 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800447
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800448 case CS_OPT_SKIPDATA_SETUP:
449 if (value)
450 handle->skipdata_setup = *((cs_opt_skipdata *)value);
451 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800452
453 case CS_OPT_MNEMONIC:
454 opt = (cs_opt_mnem *)value;
455 if (opt->id) {
456 if (opt->mnemonic) {
457 struct insn_mnem *tmp;
458
459 // add new instruction, or replace existing instruction
460 // 1. find if we already had this insn in the linked list
461 tmp = handle->mnem_list;
462 while(tmp) {
463 if (tmp->insn.id == opt->id) {
464 // found this instruction, so replace its mnemonic
465 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
466 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
467 break;
468 }
469 tmp = tmp->next;
470 }
471
472 // 2. add this instruction if we have not had it yet
473 if (!tmp) {
474 tmp = cs_mem_malloc(sizeof(*tmp));
475 tmp->insn.id = opt->id;
476 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
477 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
478 // this new instruction is heading the list
479 tmp->next = handle->mnem_list;
480 handle->mnem_list = tmp;
481 }
482 return CS_ERR_OK;
483 } else {
484 struct insn_mnem *prev, *tmp;
485
486 // we want to delete an existing instruction
487 // iterate the list to find the instruction to remove it
488 tmp = handle->mnem_list;
489 prev = tmp;
490 while(tmp) {
491 if (tmp->insn.id == opt->id) {
492 // delete this instruction
493 if (tmp == prev) {
494 // head of the list
495 handle->mnem_list = tmp->next;
496 } else {
497 prev->next = tmp->next;
498 }
499 cs_mem_free(tmp);
500 break;
501 }
502 prev = tmp;
503 tmp = tmp->next;
504 }
505 }
506 }
507 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800508 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800509
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800510 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800511}
512
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800513// generate @op_str for data instruction of SKIPDATA
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800514static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
515{
516 char *p = opstr;
517 int len;
518 size_t i;
519
520 if (!size) {
521 opstr[0] = '\0';
522 return;
523 }
524
525 len = sprintf(p, "0x%02x", buffer[0]);
526 p+= len;
527
528 for(i = 1; i < size; i++) {
529 len = sprintf(p, ", 0x%02x", buffer[i]);
530 p+= len;
531 }
532}
533
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800534// dynamicly allocate memory to contain disasm insn
535// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800536CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800537size_t 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 +0800538{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800539 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800540 MCInst mci;
541 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800542 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800543 unsigned int f = 0; // index of the next instruction in the cache
544 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800545 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800546 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800547 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800548 void *tmp;
549 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800550 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800551 size_t size_org;
552 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800553 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500554 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800555
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800556 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800557 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800558 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800559 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800560 return 0;
561 }
562
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800563 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800564
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800565#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800566 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400567 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800568#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800569
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800570 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800571 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800572 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800573 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800574
575 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700576 total = cs_mem_malloc(total_size);
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800577 if (total == NULL) {
578 // insufficient memory
Edward Williamsonf1e49752014-12-14 20:45:19 -0500579 handle->errnum = CS_ERR_MEM;
580 return 0;
581 }
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800582
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700583 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800584
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800585 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800586 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800587 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800588
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700589 // relative branches need to know the address & size of current insn
590 mci.address = offset;
591
592 if (handle->detail) {
593 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700594 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700595 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700596 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700597 }
598
599 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700600 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700601 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800602#ifdef CAPSTONE_DIET
603 // zero out mnemonic & op_str
604 mci.flat_insn->mnemonic[0] = '\0';
605 mci.flat_insn->op_str[0] = '\0';
606#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700607
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800608 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800609 if (r) {
610 SStream ss;
611 SStream_Init(&ss);
612
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700613 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800614
615 // map internal instruction opcode to public insn ID
616 handle->insn_id(handle, insn_cache, mci.Opcode);
617
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800618 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800619
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700620 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800621
danghvu0d1aad12014-10-01 23:12:18 -0500622 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800623 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800624 // encounter a broken instruction
625
626 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800627 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800628 cs_mem_free(insn_cache->detail);
629 }
630
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800631 // if there is no request to skip data, or remaining data is too small,
632 // then bail out
633 if (!handle->skipdata || handle->skipdata_size > size)
634 break;
635
636 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800637 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800638 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800639 if (skipdata_bytes > size)
640 // remaining data is not enough
641 break;
642
643 if (!skipdata_bytes)
644 // user requested not to skip data, so bail out
645 break;
646 } else
647 skipdata_bytes = handle->skipdata_size;
648
649 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700650 insn_cache->id = 0; // invalid ID for this "data" instruction
651 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800652 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700653 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
654 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
655 sizeof(insn_cache->mnemonic) - 1);
656 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
657 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800658
danghvu0d1aad12014-10-01 23:12:18 -0500659 next_offset = skipdata_bytes;
660 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800661
danghvu0d1aad12014-10-01 23:12:18 -0500662 // one more instruction entering the cache
663 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800664
danghvu0d1aad12014-10-01 23:12:18 -0500665 // one more instruction disassembled
666 c++;
667 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800668 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500669 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800670
danghvu0d1aad12014-10-01 23:12:18 -0500671 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800672 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500673 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500674 total_size += (sizeof(cs_insn) * cache_size);
675 tmp = cs_mem_realloc(total, total_size);
676 if (tmp == NULL) { // insufficient memory
677 if (handle->detail) {
678 insn_cache = (cs_insn *)total;
679 for (i = 0; i < c; i++, insn_cache++)
680 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800681 }
682
danghvu0d1aad12014-10-01 23:12:18 -0500683 cs_mem_free(total);
684 *insn = NULL;
685 handle->errnum = CS_ERR_MEM;
686 return 0;
687 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800688
danghvu0d1aad12014-10-01 23:12:18 -0500689 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800690 // continue to fill in the cache after the last instruction
691 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800692
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800693 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500694 f = 0;
695 } else
696 insn_cache++;
697
698 buffer += next_offset;
699 size -= next_offset;
700 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800701 }
702
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800703 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800704 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800705 cs_mem_free(total);
706 total = NULL;
707 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800708 // total did not fully use the last cache, so downsize it
Tyler J. Stacheckid07a21b2015-10-23 20:59:20 -0400709 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800710 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800711 // free all detail pointers
712 if (handle->detail) {
713 insn_cache = (cs_insn *)total;
714 for (i = 0; i < c; i++, insn_cache++)
715 cs_mem_free(insn_cache->detail);
716 }
717
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800718 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800719 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800720
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800721 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800722 return 0;
723 }
724
725 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800726 }
727
728 *insn = total;
729
730 return c;
731}
732
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800733CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800734CAPSTONE_DEPRECATED
735size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
736{
737 return cs_disasm(ud, buffer, size, offset, count, insn);
738}
739
740CAPSTONE_EXPORT
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800741void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800742{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800743 size_t i;
744
745 // free all detail pointers
746 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800747 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800748
749 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800750 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800751}
752
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800753CAPSTONE_EXPORT
754cs_insn *cs_malloc(csh ud)
755{
756 cs_insn *insn;
757 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
758
759 insn = cs_mem_malloc(sizeof(cs_insn));
760 if (!insn) {
761 // insufficient memory
762 handle->errnum = CS_ERR_MEM;
763 return NULL;
764 } else {
765 if (handle->detail) {
766 // allocate memory for @detail pointer
767 insn->detail = cs_mem_malloc(sizeof(cs_detail));
768 if (insn->detail == NULL) { // insufficient memory
769 cs_mem_free(insn);
770 handle->errnum = CS_ERR_MEM;
771 return NULL;
772 }
773 } else
774 insn->detail = NULL;
775 }
776
777 return insn;
778}
779
hlide993f3622014-10-05 18:14:40 +0200780// iterator for instruction "single-stepping"
781CAPSTONE_EXPORT
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800782bool cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
783 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200784{
785 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200786 uint16_t insn_size;
787 MCInst mci;
788 bool r;
789
790 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800791 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800792 return false;
hlide993f3622014-10-05 18:14:40 +0200793 }
794
795 handle->errnum = CS_ERR_OK;
796
hlide993f3622014-10-05 18:14:40 +0200797 MCInst_Init(&mci);
798 mci.csh = handle;
799
800 // relative branches need to know the address & size of current insn
801 mci.address = *address;
802
803 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800804 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200805 mci.flat_insn->address = *address;
806#ifdef CAPSTONE_DIET
807 // zero out mnemonic & op_str
808 mci.flat_insn->mnemonic[0] = '\0';
809 mci.flat_insn->op_str[0] = '\0';
810#endif
811
812 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800813 if (r) {
hlide993f3622014-10-05 18:14:40 +0200814 SStream ss;
815 SStream_Init(&ss);
816
817 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800818
819 // map internal instruction opcode to public insn ID
820 handle->insn_id(handle, insn, mci.Opcode);
821
hlide993f3622014-10-05 18:14:40 +0200822 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800823
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800824 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800825
hlide993f3622014-10-05 18:14:40 +0200826 *code += insn_size;
827 *size -= insn_size;
828 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800829 } else { // encounter a broken instruction
830 size_t skipdata_bytes;
831
832 // if there is no request to skip data, or remaining data is too small,
833 // then bail out
834 if (!handle->skipdata || handle->skipdata_size > *size)
835 return false;
836
837 if (handle->skipdata_setup.callback) {
838 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
839 0, handle->skipdata_setup.user_data);
840 if (skipdata_bytes > *size)
841 // remaining data is not enough
842 return false;
843
844 if (!skipdata_bytes)
845 // user requested not to skip data, so bail out
846 return false;
847 } else
848 skipdata_bytes = handle->skipdata_size;
849
850 // we have to skip some amount of data, depending on arch & mode
851 insn->id = 0; // invalid ID for this "data" instruction
852 insn->address = *address;
853 insn->size = (uint16_t)skipdata_bytes;
854 memcpy(insn->bytes, *code, skipdata_bytes);
855 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
856 sizeof(insn->mnemonic) - 1);
857 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
858
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800859 *code += skipdata_bytes;
860 *size -= skipdata_bytes;
861 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200862 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800863
864 return true;
hlide993f3622014-10-05 18:14:40 +0200865}
866
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800867// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800868CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100869const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800870{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800871 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800872
873 if (!handle || handle->reg_name == NULL) {
874 return NULL;
875 }
876
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800877 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800878}
879
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800880CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100881const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800882{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800883 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800884
885 if (!handle || handle->insn_name == NULL) {
886 return NULL;
887 }
888
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800889 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800890}
891
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800892CAPSTONE_EXPORT
893const char *cs_group_name(csh ud, unsigned int group)
894{
895 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
896
897 if (!handle || handle->group_name == NULL) {
898 return NULL;
899 }
900
901 return handle->group_name(ud, group);
902}
903
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800904CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200905bool cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800906{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800907 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800908 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800909 return false;
910
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100911 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200912
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800913 if (!handle->detail) {
914 handle->errnum = CS_ERR_DETAIL;
915 return false;
916 }
917
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800918 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200919 handle->errnum = CS_ERR_SKIPDATA;
920 return false;
921 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200922
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800923 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200924 handle->errnum = CS_ERR_DETAIL;
925 return false;
926 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200927
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800928 return arr_exist8(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800929}
930
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800931CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200932bool cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800933{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800934 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800935 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800936 return false;
937
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100938 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200939
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800940 if (!handle->detail) {
941 handle->errnum = CS_ERR_DETAIL;
942 return false;
943 }
944
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800945 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200946 handle->errnum = CS_ERR_SKIPDATA;
947 return false;
948 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200949
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800950 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200951 handle->errnum = CS_ERR_DETAIL;
952 return false;
953 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200954
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800955 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800956}
957
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800958CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200959bool cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800960{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800961 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800962 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800963 return false;
964
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100965 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +0200966
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800967 if (!handle->detail) {
968 handle->errnum = CS_ERR_DETAIL;
969 return false;
970 }
971
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800972 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200973 handle->errnum = CS_ERR_SKIPDATA;
974 return false;
975 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200976
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800977 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200978 handle->errnum = CS_ERR_DETAIL;
979 return false;
980 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200981
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800982 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800983}
984
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800985CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200986int cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800987{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800988 struct cs_struct *handle;
989 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800990 if (!ud)
991 return -1;
992
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100993 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200994
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800995 if (!handle->detail) {
996 handle->errnum = CS_ERR_DETAIL;
997 return -1;
998 }
999
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001000 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001001 handle->errnum = CS_ERR_SKIPDATA;
1002 return -1;
1003 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001004
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001005 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001006 handle->errnum = CS_ERR_DETAIL;
1007 return -1;
1008 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001009
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001010 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001011
1012 switch (handle->arch) {
1013 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001014 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001015 return -1;
1016 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001017 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001018 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001019 count++;
1020 break;
1021 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001022 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001023 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001024 count++;
1025 break;
1026 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001027 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001028 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001029 count++;
1030 break;
1031 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001032 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001033 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001034 count++;
1035 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001036 case CS_ARCH_PPC:
1037 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001038 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001039 count++;
1040 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001041 case CS_ARCH_SPARC:
1042 for (i = 0; i < insn->detail->sparc.op_count; i++)
1043 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1044 count++;
1045 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001046 case CS_ARCH_SYSZ:
1047 for (i = 0; i < insn->detail->sysz.op_count; i++)
1048 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1049 count++;
1050 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001051 case CS_ARCH_XCORE:
1052 for (i = 0; i < insn->detail->xcore.op_count; i++)
1053 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1054 count++;
1055 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001056 }
1057
1058 return count;
1059}
1060
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001061CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +02001062int cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001063 unsigned int post)
1064{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001065 struct cs_struct *handle;
1066 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001067 if (!ud)
1068 return -1;
1069
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001070 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001071
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001072 if (!handle->detail) {
1073 handle->errnum = CS_ERR_DETAIL;
1074 return -1;
1075 }
1076
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001077 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001078 handle->errnum = CS_ERR_SKIPDATA;
1079 return -1;
1080 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001081
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001082 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001083 handle->errnum = CS_ERR_DETAIL;
1084 return -1;
1085 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001086
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001087 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001088
1089 switch (handle->arch) {
1090 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001091 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001092 return -1;
1093 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001094 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001095 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001096 count++;
1097 if (count == post)
1098 return i;
1099 }
1100 break;
1101 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001102 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001103 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001104 count++;
1105 if (count == post)
1106 return i;
1107 }
1108 break;
1109 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001110 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001111 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001112 count++;
1113 if (count == post)
1114 return i;
1115 }
1116 break;
1117 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001118 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001119 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001120 count++;
1121 if (count == post)
1122 return i;
1123 }
1124 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001125 case CS_ARCH_PPC:
1126 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001127 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001128 count++;
1129 if (count == post)
1130 return i;
1131 }
1132 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001133 case CS_ARCH_SPARC:
1134 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1135 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1136 count++;
1137 if (count == post)
1138 return i;
1139 }
1140 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001141 case CS_ARCH_SYSZ:
1142 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1143 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1144 count++;
1145 if (count == post)
1146 return i;
1147 }
1148 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001149 case CS_ARCH_XCORE:
1150 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1151 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1152 count++;
1153 if (count == post)
1154 return i;
1155 }
1156 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001157 }
1158
1159 return -1;
1160}
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001161
1162CAPSTONE_EXPORT
1163cs_err cs_regs_access(csh ud, const cs_insn *insn,
1164 cs_regs regs_read, uint8_t *regs_read_count,
1165 cs_regs regs_write, uint8_t *regs_write_count)
1166{
1167 struct cs_struct *handle;
1168
1169 if (!ud)
1170 return -1;
1171
1172 handle = (struct cs_struct *)(uintptr_t)ud;
1173
1174#ifdef CAPSTONE_DIET
1175 // This API does not work in DIET mode
1176 handle->errnum = CS_ERR_DIET;
1177 return CS_ERR_DIET;
1178#else
1179 if (!handle->detail) {
1180 handle->errnum = CS_ERR_DETAIL;
1181 return CS_ERR_DETAIL;
1182 }
1183
1184 if (!insn->id) {
1185 handle->errnum = CS_ERR_SKIPDATA;
1186 return CS_ERR_SKIPDATA;
1187 }
1188
1189 if (!insn->detail) {
1190 handle->errnum = CS_ERR_DETAIL;
1191 return CS_ERR_DETAIL;
1192 }
1193
1194 if (handle->reg_access) {
1195 handle->reg_access(insn, regs_read, regs_read_count, regs_write, regs_write_count);
1196 } else {
1197 // this arch is unsupported yet
1198 handle->errnum = CS_ERR_ARCH;
1199 return CS_ERR_ARCH;
1200 }
1201
1202 return CS_ERR_OK;
1203#endif
1204}