blob: 2e011ca0447f0319e3fd16612a1203eba7c28150 [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 Quynh56aa0ac2016-04-27 14:43:10 +0800565 // reset IT block of ARM structure
566 handle->ITBlock.size = 0;
567
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800568#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800569 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400570 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800571#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800572
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800573 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800574 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800575 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800576 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800577
578 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700579 total = cs_mem_malloc(total_size);
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800580 if (total == NULL) {
581 // insufficient memory
Edward Williamsonf1e49752014-12-14 20:45:19 -0500582 handle->errnum = CS_ERR_MEM;
583 return 0;
584 }
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800585
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700586 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800587
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800588 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800589 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800590 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800591
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700592 // relative branches need to know the address & size of current insn
593 mci.address = offset;
594
595 if (handle->detail) {
596 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700597 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700598 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700599 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700600 }
601
602 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700603 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700604 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800605#ifdef CAPSTONE_DIET
606 // zero out mnemonic & op_str
607 mci.flat_insn->mnemonic[0] = '\0';
608 mci.flat_insn->op_str[0] = '\0';
609#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700610
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800611 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800612 if (r) {
613 SStream ss;
614 SStream_Init(&ss);
615
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700616 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800617
618 // map internal instruction opcode to public insn ID
619 handle->insn_id(handle, insn_cache, mci.Opcode);
620
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800621 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800622
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700623 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800624
danghvu0d1aad12014-10-01 23:12:18 -0500625 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800626 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800627 // encounter a broken instruction
628
629 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800630 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800631 cs_mem_free(insn_cache->detail);
632 }
633
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800634 // if there is no request to skip data, or remaining data is too small,
635 // then bail out
636 if (!handle->skipdata || handle->skipdata_size > size)
637 break;
638
639 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800640 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800641 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800642 if (skipdata_bytes > size)
643 // remaining data is not enough
644 break;
645
646 if (!skipdata_bytes)
647 // user requested not to skip data, so bail out
648 break;
649 } else
650 skipdata_bytes = handle->skipdata_size;
651
652 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700653 insn_cache->id = 0; // invalid ID for this "data" instruction
654 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800655 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700656 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
657 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
658 sizeof(insn_cache->mnemonic) - 1);
659 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
660 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800661
danghvu0d1aad12014-10-01 23:12:18 -0500662 next_offset = skipdata_bytes;
663 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800664
danghvu0d1aad12014-10-01 23:12:18 -0500665 // one more instruction entering the cache
666 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800667
danghvu0d1aad12014-10-01 23:12:18 -0500668 // one more instruction disassembled
669 c++;
670 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800671 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500672 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800673
danghvu0d1aad12014-10-01 23:12:18 -0500674 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800675 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500676 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500677 total_size += (sizeof(cs_insn) * cache_size);
678 tmp = cs_mem_realloc(total, total_size);
679 if (tmp == NULL) { // insufficient memory
680 if (handle->detail) {
681 insn_cache = (cs_insn *)total;
682 for (i = 0; i < c; i++, insn_cache++)
683 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800684 }
685
danghvu0d1aad12014-10-01 23:12:18 -0500686 cs_mem_free(total);
687 *insn = NULL;
688 handle->errnum = CS_ERR_MEM;
689 return 0;
690 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800691
danghvu0d1aad12014-10-01 23:12:18 -0500692 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800693 // continue to fill in the cache after the last instruction
694 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800695
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800696 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500697 f = 0;
698 } else
699 insn_cache++;
700
701 buffer += next_offset;
702 size -= next_offset;
703 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800704 }
705
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800706 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800707 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800708 cs_mem_free(total);
709 total = NULL;
710 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800711 // total did not fully use the last cache, so downsize it
Tyler J. Stacheckid07a21b2015-10-23 20:59:20 -0400712 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800713 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800714 // free all detail pointers
715 if (handle->detail) {
716 insn_cache = (cs_insn *)total;
717 for (i = 0; i < c; i++, insn_cache++)
718 cs_mem_free(insn_cache->detail);
719 }
720
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800721 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800722 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800723
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800724 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800725 return 0;
726 }
727
728 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800729 }
730
731 *insn = total;
732
733 return c;
734}
735
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800736CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800737CAPSTONE_DEPRECATED
738size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
739{
740 return cs_disasm(ud, buffer, size, offset, count, insn);
741}
742
743CAPSTONE_EXPORT
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800744void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800745{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800746 size_t i;
747
748 // free all detail pointers
749 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800750 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800751
752 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800753 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800754}
755
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800756CAPSTONE_EXPORT
757cs_insn *cs_malloc(csh ud)
758{
759 cs_insn *insn;
760 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
761
762 insn = cs_mem_malloc(sizeof(cs_insn));
763 if (!insn) {
764 // insufficient memory
765 handle->errnum = CS_ERR_MEM;
766 return NULL;
767 } else {
768 if (handle->detail) {
769 // allocate memory for @detail pointer
770 insn->detail = cs_mem_malloc(sizeof(cs_detail));
771 if (insn->detail == NULL) { // insufficient memory
772 cs_mem_free(insn);
773 handle->errnum = CS_ERR_MEM;
774 return NULL;
775 }
776 } else
777 insn->detail = NULL;
778 }
779
780 return insn;
781}
782
hlide993f3622014-10-05 18:14:40 +0200783// iterator for instruction "single-stepping"
784CAPSTONE_EXPORT
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800785bool cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
786 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200787{
788 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200789 uint16_t insn_size;
790 MCInst mci;
791 bool r;
792
793 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800794 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800795 return false;
hlide993f3622014-10-05 18:14:40 +0200796 }
797
798 handle->errnum = CS_ERR_OK;
799
hlide993f3622014-10-05 18:14:40 +0200800 MCInst_Init(&mci);
801 mci.csh = handle;
802
803 // relative branches need to know the address & size of current insn
804 mci.address = *address;
805
806 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800807 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200808 mci.flat_insn->address = *address;
809#ifdef CAPSTONE_DIET
810 // zero out mnemonic & op_str
811 mci.flat_insn->mnemonic[0] = '\0';
812 mci.flat_insn->op_str[0] = '\0';
813#endif
814
815 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800816 if (r) {
hlide993f3622014-10-05 18:14:40 +0200817 SStream ss;
818 SStream_Init(&ss);
819
820 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800821
822 // map internal instruction opcode to public insn ID
823 handle->insn_id(handle, insn, mci.Opcode);
824
hlide993f3622014-10-05 18:14:40 +0200825 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800826
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800827 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800828
hlide993f3622014-10-05 18:14:40 +0200829 *code += insn_size;
830 *size -= insn_size;
831 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800832 } else { // encounter a broken instruction
833 size_t skipdata_bytes;
834
835 // if there is no request to skip data, or remaining data is too small,
836 // then bail out
837 if (!handle->skipdata || handle->skipdata_size > *size)
838 return false;
839
840 if (handle->skipdata_setup.callback) {
841 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
842 0, handle->skipdata_setup.user_data);
843 if (skipdata_bytes > *size)
844 // remaining data is not enough
845 return false;
846
847 if (!skipdata_bytes)
848 // user requested not to skip data, so bail out
849 return false;
850 } else
851 skipdata_bytes = handle->skipdata_size;
852
853 // we have to skip some amount of data, depending on arch & mode
854 insn->id = 0; // invalid ID for this "data" instruction
855 insn->address = *address;
856 insn->size = (uint16_t)skipdata_bytes;
857 memcpy(insn->bytes, *code, skipdata_bytes);
858 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
859 sizeof(insn->mnemonic) - 1);
860 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
861
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800862 *code += skipdata_bytes;
863 *size -= skipdata_bytes;
864 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200865 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800866
867 return true;
hlide993f3622014-10-05 18:14:40 +0200868}
869
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800870// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800871CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100872const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800873{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800874 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800875
876 if (!handle || handle->reg_name == NULL) {
877 return NULL;
878 }
879
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800880 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800881}
882
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800883CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100884const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800885{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800886 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800887
888 if (!handle || handle->insn_name == NULL) {
889 return NULL;
890 }
891
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800892 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800893}
894
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800895CAPSTONE_EXPORT
896const char *cs_group_name(csh ud, unsigned int group)
897{
898 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
899
900 if (!handle || handle->group_name == NULL) {
901 return NULL;
902 }
903
904 return handle->group_name(ud, group);
905}
906
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800907CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200908bool cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800909{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800910 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800911 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800912 return false;
913
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100914 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200915
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800916 if (!handle->detail) {
917 handle->errnum = CS_ERR_DETAIL;
918 return false;
919 }
920
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800921 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200922 handle->errnum = CS_ERR_SKIPDATA;
923 return false;
924 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200925
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800926 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200927 handle->errnum = CS_ERR_DETAIL;
928 return false;
929 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200930
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800931 return arr_exist8(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800932}
933
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800934CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200935bool cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800936{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800937 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800938 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800939 return false;
940
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100941 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200942
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800943 if (!handle->detail) {
944 handle->errnum = CS_ERR_DETAIL;
945 return false;
946 }
947
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800948 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200949 handle->errnum = CS_ERR_SKIPDATA;
950 return false;
951 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200952
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800953 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200954 handle->errnum = CS_ERR_DETAIL;
955 return false;
956 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200957
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800958 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800959}
960
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800961CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200962bool cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800963{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800964 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800965 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800966 return false;
967
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100968 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +0200969
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800970 if (!handle->detail) {
971 handle->errnum = CS_ERR_DETAIL;
972 return false;
973 }
974
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800975 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200976 handle->errnum = CS_ERR_SKIPDATA;
977 return false;
978 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200979
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800980 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200981 handle->errnum = CS_ERR_DETAIL;
982 return false;
983 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200984
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800985 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800986}
987
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800988CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200989int cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800990{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800991 struct cs_struct *handle;
992 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800993 if (!ud)
994 return -1;
995
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100996 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200997
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800998 if (!handle->detail) {
999 handle->errnum = CS_ERR_DETAIL;
1000 return -1;
1001 }
1002
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001003 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001004 handle->errnum = CS_ERR_SKIPDATA;
1005 return -1;
1006 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001007
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001008 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001009 handle->errnum = CS_ERR_DETAIL;
1010 return -1;
1011 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001012
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001013 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001014
1015 switch (handle->arch) {
1016 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001017 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001018 return -1;
1019 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001020 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001021 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001022 count++;
1023 break;
1024 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001025 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001026 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001027 count++;
1028 break;
1029 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001030 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001031 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001032 count++;
1033 break;
1034 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001035 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001036 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001037 count++;
1038 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001039 case CS_ARCH_PPC:
1040 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001041 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001042 count++;
1043 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001044 case CS_ARCH_SPARC:
1045 for (i = 0; i < insn->detail->sparc.op_count; i++)
1046 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1047 count++;
1048 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001049 case CS_ARCH_SYSZ:
1050 for (i = 0; i < insn->detail->sysz.op_count; i++)
1051 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1052 count++;
1053 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001054 case CS_ARCH_XCORE:
1055 for (i = 0; i < insn->detail->xcore.op_count; i++)
1056 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1057 count++;
1058 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001059 }
1060
1061 return count;
1062}
1063
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001064CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +02001065int cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001066 unsigned int post)
1067{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001068 struct cs_struct *handle;
1069 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001070 if (!ud)
1071 return -1;
1072
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001073 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001074
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001075 if (!handle->detail) {
1076 handle->errnum = CS_ERR_DETAIL;
1077 return -1;
1078 }
1079
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001080 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001081 handle->errnum = CS_ERR_SKIPDATA;
1082 return -1;
1083 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001084
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001085 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001086 handle->errnum = CS_ERR_DETAIL;
1087 return -1;
1088 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001089
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001090 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001091
1092 switch (handle->arch) {
1093 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001094 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001095 return -1;
1096 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001097 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001098 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001099 count++;
1100 if (count == post)
1101 return i;
1102 }
1103 break;
1104 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001105 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001106 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001107 count++;
1108 if (count == post)
1109 return i;
1110 }
1111 break;
1112 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001113 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001114 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001115 count++;
1116 if (count == post)
1117 return i;
1118 }
1119 break;
1120 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001121 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001122 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001123 count++;
1124 if (count == post)
1125 return i;
1126 }
1127 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001128 case CS_ARCH_PPC:
1129 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001130 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001131 count++;
1132 if (count == post)
1133 return i;
1134 }
1135 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001136 case CS_ARCH_SPARC:
1137 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1138 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1139 count++;
1140 if (count == post)
1141 return i;
1142 }
1143 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001144 case CS_ARCH_SYSZ:
1145 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1146 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1147 count++;
1148 if (count == post)
1149 return i;
1150 }
1151 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001152 case CS_ARCH_XCORE:
1153 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1154 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1155 count++;
1156 if (count == post)
1157 return i;
1158 }
1159 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001160 }
1161
1162 return -1;
1163}
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001164
1165CAPSTONE_EXPORT
1166cs_err cs_regs_access(csh ud, const cs_insn *insn,
1167 cs_regs regs_read, uint8_t *regs_read_count,
1168 cs_regs regs_write, uint8_t *regs_write_count)
1169{
1170 struct cs_struct *handle;
1171
1172 if (!ud)
1173 return -1;
1174
1175 handle = (struct cs_struct *)(uintptr_t)ud;
1176
1177#ifdef CAPSTONE_DIET
1178 // This API does not work in DIET mode
1179 handle->errnum = CS_ERR_DIET;
1180 return CS_ERR_DIET;
1181#else
1182 if (!handle->detail) {
1183 handle->errnum = CS_ERR_DETAIL;
1184 return CS_ERR_DETAIL;
1185 }
1186
1187 if (!insn->id) {
1188 handle->errnum = CS_ERR_SKIPDATA;
1189 return CS_ERR_SKIPDATA;
1190 }
1191
1192 if (!insn->detail) {
1193 handle->errnum = CS_ERR_DETAIL;
1194 return CS_ERR_DETAIL;
1195 }
1196
1197 if (handle->reg_access) {
1198 handle->reg_access(insn, regs_read, regs_read_count, regs_write, regs_write_count);
1199 } else {
1200 // this arch is unsupported yet
1201 handle->errnum = CS_ERR_ARCH;
1202 return CS_ERR_ARCH;
1203 }
1204
1205 return CS_ERR_OK;
1206#endif
1207}