blob: c3becd8cf82133a2a67bf72520f88a798ce5af4e [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
tandasat45e5eab2016-05-11 21:48:32 -070020#if defined(_KERNEL_MODE)
21#include "windows\winkernel_mm.h"
22#endif
23
tandasatf7fe6402016-05-16 20:32:36 -070024// Issue #681: Windows kernel does not support formatting float point
25#if defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
26#if defined(CAPSTONE_HAS_ARM) || defined(CAPSTONE_HAS_ARM64) || defined(CAPSTONE_HAS_M68K)
27#define CAPSTONE_STR_INTERNAL(x) #x
28#define CAPSTONE_STR(x) CAPSTONE_STR_INTERNAL(x)
29#define CAPSTONE_MSVC_WRANING_PREFIX __FILE__ "("CAPSTONE_STR(__LINE__)") : warning message : "
30
31#pragma message(CAPSTONE_MSVC_WRANING_PREFIX "Windows driver does not support full features for selected architecture(s). Define CAPSTONE_DIET to compile Capstone with only supported features. See issue #681 for details.")
32
33#undef CAPSTONE_MSVC_WRANING_PREFIX
34#undef CAPSTONE_STR
35#undef CAPSTONE_STR_INTERNAL
36#endif
37#endif // defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
38
tandasat45e5eab2016-05-11 21:48:32 -070039#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(CAPSTONE_DIET) && !defined(_KERNEL_MODE)
Nguyen Anh Quynh472a4a42014-03-06 09:13:04 +080040#define INSN_CACHE_SIZE 32
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080041#else
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080042// reduce stack variable size for kernel/firmware
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080043#define INSN_CACHE_SIZE 8
44#endif
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +080045
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080046// default SKIPDATA mnemonic
Nguyen Anh Quynhc75a9092014-04-10 10:26:49 +080047#define SKIPDATA_MNEM ".byte"
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080048
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080049cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080050cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
51void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080052
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080053extern void ARM_enable(void);
54extern void AArch64_enable(void);
Daniel Collin2ee675c2015-08-03 18:45:08 +020055extern void M68K_enable(void);
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080056extern void Mips_enable(void);
57extern void X86_enable(void);
58extern void PPC_enable(void);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080059extern void Sparc_enable(void);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080060extern void SystemZ_enable(void);
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080061extern void XCore_enable(void);
danghvu701b8502014-01-09 11:06:44 +070062
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080063static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080064{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080065 static bool initialized = false;
66
67 if (initialized)
68 return;
69
danghvub33bd2c2014-01-09 12:22:56 +070070#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080071 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070072#endif
73#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080074 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070075#endif
Daniel Collin2ee675c2015-08-03 18:45:08 +020076#ifdef CAPSTONE_HAS_M68K
77 M68K_enable();
78#endif
danghvub33bd2c2014-01-09 12:22:56 +070079#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080080 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070081#endif
danghvub33bd2c2014-01-09 12:22:56 +070082#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080083 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070084#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080085#ifdef CAPSTONE_HAS_SPARC
86 Sparc_enable();
87#endif
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080088#ifdef CAPSTONE_HAS_SYSZ
89 SystemZ_enable();
90#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080091#ifdef CAPSTONE_HAS_X86
92 X86_enable();
93#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080094#ifdef CAPSTONE_HAS_XCORE
95 XCore_enable();
96#endif
97
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080098
99 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +0700100}
101
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800102unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +0800103
tandasat45e5eab2016-05-11 21:48:32 -0700104#if defined(CAPSTONE_USE_SYS_DYN_MEM)
105#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
tandasatc45f1db2016-05-11 22:52:34 -0700106// default
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800107cs_malloc_t cs_mem_malloc = malloc;
108cs_calloc_t cs_mem_calloc = calloc;
109cs_realloc_t cs_mem_realloc = realloc;
110cs_free_t cs_mem_free = free;
Koutheir Attouchi201a0a12016-04-12 09:25:37 +0200111#if defined(_WIN32_WCE)
112cs_vsnprintf_t cs_vsnprintf = _vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800113#else
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800114cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700115#endif // defined(_WIN32_WCE)
116
tandasat45e5eab2016-05-11 21:48:32 -0700117#elif defined(_KERNEL_MODE)
tandasatc45f1db2016-05-11 22:52:34 -0700118// Windows driver
tandasat45e5eab2016-05-11 21:48:32 -0700119cs_malloc_t cs_mem_malloc = cs_winkernel_malloc;
120cs_calloc_t cs_mem_calloc = cs_winkernel_calloc;
121cs_realloc_t cs_mem_realloc = cs_winkernel_realloc;
122cs_free_t cs_mem_free = cs_winkernel_free;
123cs_vsnprintf_t cs_vsnprintf = cs_winkernel_vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700124
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800125#else
tandasatc45f1db2016-05-11 22:52:34 -0700126// OSX kernel
Pb86ad3d62015-11-05 18:08:25 +0100127extern void* kern_os_malloc(size_t size);
128extern void kern_os_free(void* addr);
129extern void* kern_os_realloc(void* addr, size_t nsize);
130
Nguyen Anh Quynh197e4572015-11-10 22:56:53 +0800131static void* cs_kern_os_calloc(size_t num, size_t size)
132{
Pb86ad3d62015-11-05 18:08:25 +0100133 return kern_os_malloc(num * size); // malloc bzeroes the buffer
134}
135
136cs_malloc_t cs_mem_malloc = kern_os_malloc;
Pb2769c772015-11-06 14:44:55 +0100137cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
Pb86ad3d62015-11-05 18:08:25 +0100138cs_realloc_t cs_mem_realloc = kern_os_realloc;
139cs_free_t cs_mem_free = kern_os_free;
140cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700141
142#endif // !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
Pb86ad3d62015-11-05 18:08:25 +0100143#else
tandasatc45f1db2016-05-11 22:52:34 -0700144// User-defined
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800145cs_malloc_t cs_mem_malloc = NULL;
146cs_calloc_t cs_mem_calloc = NULL;
147cs_realloc_t cs_mem_realloc = NULL;
148cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800149cs_vsnprintf_t cs_vsnprintf = NULL;
tandasatc45f1db2016-05-11 22:52:34 -0700150
151#endif // defined(CAPSTONE_USE_SYS_DYN_MEM)
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800152
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800153CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700154unsigned int CAPSTONE_API cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800155{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800156 archs_enable();
157
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800158 if (major != NULL && minor != NULL) {
159 *major = CS_API_MAJOR;
160 *minor = CS_API_MINOR;
161 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800162
163 return (CS_API_MAJOR << 8) + CS_API_MINOR;
164}
165
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800166CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700167bool CAPSTONE_API cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800168{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800169 archs_enable();
170
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800171 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800172 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800173 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800174 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800175 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800176
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800177 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800178 return all_arch & (1 << query);
179
180 if (query == CS_SUPPORT_DIET) {
181#ifdef CAPSTONE_DIET
182 return true;
183#else
184 return false;
185#endif
186 }
187
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800188 if (query == CS_SUPPORT_X86_REDUCE) {
189#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800190 return true;
191#else
192 return false;
193#endif
194 }
195
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800196 // unsupported query
197 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800198}
199
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800200CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700201cs_err CAPSTONE_API cs_errno(csh handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800202{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800203 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800204 if (!handle)
205 return CS_ERR_CSH;
206
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100207 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800208
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800209 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800210}
211
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800212CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700213const char * CAPSTONE_API cs_strerror(cs_err code)
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800214{
215 switch(code) {
216 default:
217 return "Unknown error code";
218 case CS_ERR_OK:
219 return "OK (CS_ERR_OK)";
220 case CS_ERR_MEM:
221 return "Out of memory (CS_ERR_MEM)";
222 case CS_ERR_ARCH:
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800223 return "Invalid/unsupported architecture(CS_ERR_ARCH)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800224 case CS_ERR_HANDLE:
225 return "Invalid handle (CS_ERR_HANDLE)";
226 case CS_ERR_CSH:
227 return "Invalid csh (CS_ERR_CSH)";
228 case CS_ERR_MODE:
229 return "Invalid mode (CS_ERR_MODE)";
230 case CS_ERR_OPTION:
231 return "Invalid option (CS_ERR_OPTION)";
232 case CS_ERR_DETAIL:
233 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800234 case CS_ERR_MEMSETUP:
235 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800236 case CS_ERR_VERSION:
237 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800238 case CS_ERR_DIET:
239 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800240 case CS_ERR_SKIPDATA:
241 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh8aba4cd2015-08-09 10:52:18 -0700242 case CS_ERR_X86_ATT:
243 return "AT&T syntax is unavailable (CS_ERR_X86_ATT)";
244 case CS_ERR_X86_INTEL:
245 return "INTEL syntax is unavailable (CS_ERR_X86_INTEL)";
246 case CS_ERR_X86_MASM:
247 return "MASM syntax is unavailable (CS_ERR_X86_MASM)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800248 }
249}
250
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800251CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700252cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800253{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800254 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800255 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800256 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800257 // Error: before cs_open(), dynamic memory management must be initialized
258 // with cs_option(CS_OPT_MEM)
259 return CS_ERR_MEMSETUP;
260
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800261 archs_enable();
262
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800263 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800264 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800265 if (!ud) {
266 // memory insufficient
267 return CS_ERR_MEM;
268 }
269
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800270 ud->errnum = CS_ERR_OK;
271 ud->arch = arch;
272 ud->mode = mode;
tandasat45e5eab2016-05-11 21:48:32 -0700273 ud->big_endian = (mode & CS_MODE_BIG_ENDIAN) != 0;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800274 // by default, do not break instruction into details
275 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800276
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800277 // default skipdata setup
278 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
279
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100280 err = arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800281 if (err) {
282 cs_mem_free(ud);
283 *handle = 0;
284 return err;
285 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800286
287 *handle = (uintptr_t)ud;
288
289 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800290 } else {
291 *handle = 0;
292 return CS_ERR_ARCH;
293 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800294}
295
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800296CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700297cs_err CAPSTONE_API cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800298{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800299 struct cs_struct *ud;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800300 struct insn_mnem *next, *tmp;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800301
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800302 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800303 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800304 return CS_ERR_CSH;
305
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100306 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800307
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800308 if (ud->printer_info)
309 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800310
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800311 // free the linked list of customized mnemonic
312 tmp = ud->mnem_list;
313 while(tmp) {
314 next = tmp->next;
315 cs_mem_free(tmp);
316 tmp = next;
317 }
318
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800319 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800320
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800321 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800322 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800323
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800324 // invalidate this handle by ZERO out its value.
325 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800326 *handle = 0;
327
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800328 return CS_ERR_OK;
329}
330
331// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800332static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800333 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800334{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800335#ifndef CAPSTONE_DIET
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700336 char *sp, *mnem;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800337#endif
tandasat45e5eab2016-05-11 21:48:32 -0700338 uint16_t copy_size = MIN(sizeof(insn->bytes), insn->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800339
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800340 // fill the instruction bytes.
341 // we might skip some redundant bytes in front in the case of X86
342 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
343 insn->size = copy_size;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800344
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800345 // alias instruction might have ID saved in OpcodePub
346 if (MCInst_getOpcodePub(mci))
347 insn->id = MCInst_getOpcodePub(mci);
348
349 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800350 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800351 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800352
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800353#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800354 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800355 // find first space or tab
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100356 sp = buffer;
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800357 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800358 for (sp = buffer; *sp; sp++) {
359 if (*sp == ' '|| *sp == '\t')
360 break;
361 if (*sp == '|') // lock|rep prefix for x86
362 *sp = ' ';
363 // copy to @mnemonic
364 *mnem = *sp;
365 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800366 }
367
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800368 *mnem = '\0';
369
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800370 // we might have customized mnemonic
371 if (handle->mnem_list) {
372 struct insn_mnem *tmp = handle->mnem_list;
373 while(tmp) {
374 if (tmp->insn.id == insn->id) {
375 // found this instruction, so copy its mnemonic
376 (void)strncpy(insn->mnemonic, tmp->insn.mnemonic, sizeof(insn->mnemonic) - 1);
377 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
378 break;
379 }
380 tmp = tmp->next;
381 }
382 }
383
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800384 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800385 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800386 // find the next non-space char
387 sp++;
388 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
389 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800390 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
391 } else
392 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800393#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800394}
395
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800396// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800397// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800398static uint8_t skipdata_size(cs_struct *handle)
399{
400 switch(handle->arch) {
401 default:
402 // should never reach
tandasat45e5eab2016-05-11 21:48:32 -0700403 return (uint8_t)-1;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800404 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800405 // skip 2 bytes on Thumb mode.
406 if (handle->mode & CS_MODE_THUMB)
407 return 2;
408 // otherwise, skip 4 bytes
409 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800410 case CS_ARCH_ARM64:
411 case CS_ARCH_MIPS:
412 case CS_ARCH_PPC:
413 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800414 // skip 4 bytes
415 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800416 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800417 // SystemZ instruction's length can be 2, 4 or 6 bytes,
418 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800419 return 2;
420 case CS_ARCH_X86:
421 // X86 has no restriction on instruction alignment
422 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800423 case CS_ARCH_XCORE:
424 // XCore instruction's length can be 2 or 4 bytes,
425 // so we just skip 2 bytes
426 return 2;
Daniel Collin2ee675c2015-08-03 18:45:08 +0200427 case CS_ARCH_M68K:
428 // M68K has 2 bytes instruction alignment but contain multibyte instruction so we skip 2 bytes
429 return 2;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800430 }
431}
432
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800433CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700434cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800435{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800436 struct cs_struct *handle;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800437 cs_opt_mnem *opt;
438
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800439 archs_enable();
440
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800441 // cs_option() can be called with NULL handle just for CS_OPT_MEM
442 // This is supposed to be executed before all other APIs (even cs_open())
443 if (type == CS_OPT_MEM) {
444 cs_opt_mem *mem = (cs_opt_mem *)value;
445
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800446 cs_mem_malloc = mem->malloc;
447 cs_mem_calloc = mem->calloc;
448 cs_mem_realloc = mem->realloc;
449 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800450 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800451
452 return CS_ERR_OK;
453 }
454
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100455 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600456 if (!handle)
457 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800458
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800459 switch(type) {
460 default:
461 break;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800462
Nguyen Anh Quynh256090a2016-03-14 13:52:23 +0800463 case CS_OPT_UNSIGNED:
464 handle->imm_unsigned = (cs_opt_value)value;
465 return CS_ERR_OK;
466
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800467 case CS_OPT_DETAIL:
Félix Cloutier3973d8b2015-03-02 22:06:56 -0500468 handle->detail = (cs_opt_value)value;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800469 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800470
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800471 case CS_OPT_SKIPDATA:
472 handle->skipdata = (value == CS_OPT_ON);
473 if (handle->skipdata) {
474 if (handle->skipdata_size == 0) {
475 // set the default skipdata size
476 handle->skipdata_size = skipdata_size(handle);
477 }
478 }
479 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800480
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800481 case CS_OPT_SKIPDATA_SETUP:
482 if (value)
483 handle->skipdata_setup = *((cs_opt_skipdata *)value);
484 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800485
486 case CS_OPT_MNEMONIC:
487 opt = (cs_opt_mnem *)value;
488 if (opt->id) {
489 if (opt->mnemonic) {
490 struct insn_mnem *tmp;
491
492 // add new instruction, or replace existing instruction
493 // 1. find if we already had this insn in the linked list
494 tmp = handle->mnem_list;
495 while(tmp) {
496 if (tmp->insn.id == opt->id) {
497 // found this instruction, so replace its mnemonic
498 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
499 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
500 break;
501 }
502 tmp = tmp->next;
503 }
504
505 // 2. add this instruction if we have not had it yet
506 if (!tmp) {
507 tmp = cs_mem_malloc(sizeof(*tmp));
508 tmp->insn.id = opt->id;
509 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
510 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
511 // this new instruction is heading the list
512 tmp->next = handle->mnem_list;
513 handle->mnem_list = tmp;
514 }
515 return CS_ERR_OK;
516 } else {
517 struct insn_mnem *prev, *tmp;
518
519 // we want to delete an existing instruction
520 // iterate the list to find the instruction to remove it
521 tmp = handle->mnem_list;
522 prev = tmp;
523 while(tmp) {
524 if (tmp->insn.id == opt->id) {
525 // delete this instruction
526 if (tmp == prev) {
527 // head of the list
528 handle->mnem_list = tmp->next;
529 } else {
530 prev->next = tmp->next;
531 }
532 cs_mem_free(tmp);
533 break;
534 }
535 prev = tmp;
536 tmp = tmp->next;
537 }
538 }
539 }
540 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800541 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800542
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800543 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800544}
545
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800546// generate @op_str for data instruction of SKIPDATA
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800547static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
548{
549 char *p = opstr;
550 int len;
551 size_t i;
tandasat45e5eab2016-05-11 21:48:32 -0700552 size_t available = sizeof(((cs_insn*)NULL)->op_str);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800553
554 if (!size) {
555 opstr[0] = '\0';
556 return;
557 }
558
tandasat45e5eab2016-05-11 21:48:32 -0700559 len = cs_snprintf(p, available, "0x%02x", buffer[0]);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800560 p+= len;
tandasat45e5eab2016-05-11 21:48:32 -0700561 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800562
563 for(i = 1; i < size; i++) {
tandasat45e5eab2016-05-11 21:48:32 -0700564 len = cs_snprintf(p, available, ", 0x%02x", buffer[i]);
565 if (len < 0) {
566 break;
567 }
568 if ((size_t)len > available - 1) {
569 break;
570 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800571 p+= len;
tandasat45e5eab2016-05-11 21:48:32 -0700572 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800573 }
574}
575
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800576// dynamicly allocate memory to contain disasm insn
577// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800578CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700579size_t CAPSTONE_API cs_disasm(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800580{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800581 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800582 MCInst mci;
583 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800584 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800585 unsigned int f = 0; // index of the next instruction in the cache
586 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800587 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800588 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800589 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800590 void *tmp;
591 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800592 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800593 size_t size_org;
594 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800595 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500596 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800597
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800598 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800599 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800600 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800601 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800602 return 0;
603 }
604
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800605 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800606
Nguyen Anh Quynh56aa0ac2016-04-27 14:43:10 +0800607 // reset IT block of ARM structure
Nguyen Anh Quynh9d07c1c2016-04-27 14:47:05 +0800608 if (handle->arch == CS_ARCH_ARM)
609 handle->ITBlock.size = 0;
Nguyen Anh Quynh56aa0ac2016-04-27 14:43:10 +0800610
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800611#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800612 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400613 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800614#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800615
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800616 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800617 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800618 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800619 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800620
621 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700622 total = cs_mem_malloc(total_size);
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800623 if (total == NULL) {
624 // insufficient memory
Edward Williamsonf1e49752014-12-14 20:45:19 -0500625 handle->errnum = CS_ERR_MEM;
626 return 0;
627 }
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800628
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700629 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800630
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800631 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800632 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800633 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800634
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700635 // relative branches need to know the address & size of current insn
636 mci.address = offset;
637
638 if (handle->detail) {
639 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700640 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700641 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700642 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700643 }
644
645 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700646 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700647 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800648#ifdef CAPSTONE_DIET
649 // zero out mnemonic & op_str
650 mci.flat_insn->mnemonic[0] = '\0';
651 mci.flat_insn->op_str[0] = '\0';
652#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700653
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800654 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800655 if (r) {
656 SStream ss;
657 SStream_Init(&ss);
658
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700659 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800660
661 // map internal instruction opcode to public insn ID
662 handle->insn_id(handle, insn_cache, mci.Opcode);
663
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800664 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700665 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800666
Nguyen Anh Quynhdabc9f22016-07-15 20:37:19 +0800667 // adjust for pseudo opcode (X86)
668 if (handle->arch == CS_ARCH_X86)
669 insn_cache->id += mci.popcode_adjust;
670
danghvu0d1aad12014-10-01 23:12:18 -0500671 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800672 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800673 // encounter a broken instruction
674
675 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800676 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800677 cs_mem_free(insn_cache->detail);
678 }
679
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800680 // if there is no request to skip data, or remaining data is too small,
681 // then bail out
682 if (!handle->skipdata || handle->skipdata_size > size)
683 break;
684
685 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800686 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800687 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800688 if (skipdata_bytes > size)
689 // remaining data is not enough
690 break;
691
692 if (!skipdata_bytes)
693 // user requested not to skip data, so bail out
694 break;
695 } else
696 skipdata_bytes = handle->skipdata_size;
697
698 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700699 insn_cache->id = 0; // invalid ID for this "data" instruction
700 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800701 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700702 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
703 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
704 sizeof(insn_cache->mnemonic) - 1);
705 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
706 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800707
danghvu0d1aad12014-10-01 23:12:18 -0500708 next_offset = skipdata_bytes;
709 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800710
danghvu0d1aad12014-10-01 23:12:18 -0500711 // one more instruction entering the cache
712 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800713
danghvu0d1aad12014-10-01 23:12:18 -0500714 // one more instruction disassembled
715 c++;
716 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800717 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500718 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800719
danghvu0d1aad12014-10-01 23:12:18 -0500720 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800721 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500722 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500723 total_size += (sizeof(cs_insn) * cache_size);
724 tmp = cs_mem_realloc(total, total_size);
725 if (tmp == NULL) { // insufficient memory
726 if (handle->detail) {
727 insn_cache = (cs_insn *)total;
728 for (i = 0; i < c; i++, insn_cache++)
729 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800730 }
731
danghvu0d1aad12014-10-01 23:12:18 -0500732 cs_mem_free(total);
733 *insn = NULL;
734 handle->errnum = CS_ERR_MEM;
735 return 0;
736 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800737
danghvu0d1aad12014-10-01 23:12:18 -0500738 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800739 // continue to fill in the cache after the last instruction
740 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800741
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800742 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500743 f = 0;
744 } else
745 insn_cache++;
746
747 buffer += next_offset;
748 size -= next_offset;
749 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800750 }
751
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800752 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800753 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800754 cs_mem_free(total);
755 total = NULL;
756 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800757 // total did not fully use the last cache, so downsize it
Tyler J. Stacheckid07a21b2015-10-23 20:59:20 -0400758 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800759 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800760 // free all detail pointers
761 if (handle->detail) {
762 insn_cache = (cs_insn *)total;
763 for (i = 0; i < c; i++, insn_cache++)
764 cs_mem_free(insn_cache->detail);
765 }
766
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800767 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800768 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800769
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800770 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800771 return 0;
772 }
773
774 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800775 }
776
777 *insn = total;
778
779 return c;
780}
781
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800782CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800783CAPSTONE_DEPRECATED
tandasat45e5eab2016-05-11 21:48:32 -0700784size_t CAPSTONE_API cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800785{
786 return cs_disasm(ud, buffer, size, offset, count, insn);
787}
788
789CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700790void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800791{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800792 size_t i;
793
794 // free all detail pointers
795 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800796 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800797
798 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800799 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800800}
801
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800802CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700803cs_insn * CAPSTONE_API cs_malloc(csh ud)
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800804{
805 cs_insn *insn;
806 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
807
808 insn = cs_mem_malloc(sizeof(cs_insn));
809 if (!insn) {
810 // insufficient memory
811 handle->errnum = CS_ERR_MEM;
812 return NULL;
813 } else {
814 if (handle->detail) {
815 // allocate memory for @detail pointer
816 insn->detail = cs_mem_malloc(sizeof(cs_detail));
817 if (insn->detail == NULL) { // insufficient memory
818 cs_mem_free(insn);
819 handle->errnum = CS_ERR_MEM;
820 return NULL;
821 }
822 } else
823 insn->detail = NULL;
824 }
825
826 return insn;
827}
828
hlide993f3622014-10-05 18:14:40 +0200829// iterator for instruction "single-stepping"
830CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700831bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800832 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200833{
834 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200835 uint16_t insn_size;
836 MCInst mci;
837 bool r;
838
839 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800840 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800841 return false;
hlide993f3622014-10-05 18:14:40 +0200842 }
843
844 handle->errnum = CS_ERR_OK;
845
hlide993f3622014-10-05 18:14:40 +0200846 MCInst_Init(&mci);
847 mci.csh = handle;
848
849 // relative branches need to know the address & size of current insn
850 mci.address = *address;
851
852 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800853 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200854 mci.flat_insn->address = *address;
855#ifdef CAPSTONE_DIET
856 // zero out mnemonic & op_str
857 mci.flat_insn->mnemonic[0] = '\0';
858 mci.flat_insn->op_str[0] = '\0';
859#endif
860
861 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800862 if (r) {
hlide993f3622014-10-05 18:14:40 +0200863 SStream ss;
864 SStream_Init(&ss);
865
866 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800867
868 // map internal instruction opcode to public insn ID
869 handle->insn_id(handle, insn, mci.Opcode);
870
hlide993f3622014-10-05 18:14:40 +0200871 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800872
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800873 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800874
Nguyen Anh Quynhdabc9f22016-07-15 20:37:19 +0800875 // adjust for pseudo opcode (X86)
876 if (handle->arch == CS_ARCH_X86)
877 insn->id += mci.popcode_adjust;
878
hlide993f3622014-10-05 18:14:40 +0200879 *code += insn_size;
880 *size -= insn_size;
881 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800882 } else { // encounter a broken instruction
883 size_t skipdata_bytes;
884
885 // if there is no request to skip data, or remaining data is too small,
886 // then bail out
887 if (!handle->skipdata || handle->skipdata_size > *size)
888 return false;
889
890 if (handle->skipdata_setup.callback) {
891 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
892 0, handle->skipdata_setup.user_data);
893 if (skipdata_bytes > *size)
894 // remaining data is not enough
895 return false;
896
897 if (!skipdata_bytes)
898 // user requested not to skip data, so bail out
899 return false;
900 } else
901 skipdata_bytes = handle->skipdata_size;
902
903 // we have to skip some amount of data, depending on arch & mode
904 insn->id = 0; // invalid ID for this "data" instruction
905 insn->address = *address;
906 insn->size = (uint16_t)skipdata_bytes;
907 memcpy(insn->bytes, *code, skipdata_bytes);
908 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
909 sizeof(insn->mnemonic) - 1);
910 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
911
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800912 *code += skipdata_bytes;
913 *size -= skipdata_bytes;
914 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200915 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800916
917 return true;
hlide993f3622014-10-05 18:14:40 +0200918}
919
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800920// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800921CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700922const char * CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800923{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800924 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800925
926 if (!handle || handle->reg_name == NULL) {
927 return NULL;
928 }
929
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800930 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800931}
932
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800933CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700934const char * CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800935{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800936 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800937
938 if (!handle || handle->insn_name == NULL) {
939 return NULL;
940 }
941
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800942 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800943}
944
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800945CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700946const char * CAPSTONE_API cs_group_name(csh ud, unsigned int group)
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800947{
948 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
949
950 if (!handle || handle->group_name == NULL) {
951 return NULL;
952 }
953
954 return handle->group_name(ud, group);
955}
956
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800957CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700958bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800959{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800960 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800961 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800962 return false;
963
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100964 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200965
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800966 if (!handle->detail) {
967 handle->errnum = CS_ERR_DETAIL;
968 return false;
969 }
970
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800971 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200972 handle->errnum = CS_ERR_SKIPDATA;
973 return false;
974 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200975
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800976 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200977 handle->errnum = CS_ERR_DETAIL;
978 return false;
979 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200980
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800981 return arr_exist8(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800982}
983
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800984CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700985bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800986{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800987 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800988 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800989 return false;
990
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100991 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200992
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800993 if (!handle->detail) {
994 handle->errnum = CS_ERR_DETAIL;
995 return false;
996 }
997
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800998 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200999 handle->errnum = CS_ERR_SKIPDATA;
1000 return false;
1001 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001002
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001003 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001004 handle->errnum = CS_ERR_DETAIL;
1005 return false;
1006 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001007
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001008 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001009}
1010
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001011CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001012bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001013{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001014 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001015 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001016 return false;
1017
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001018 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +02001019
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001020 if (!handle->detail) {
1021 handle->errnum = CS_ERR_DETAIL;
1022 return false;
1023 }
1024
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001025 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001026 handle->errnum = CS_ERR_SKIPDATA;
1027 return false;
1028 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001029
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001030 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001031 handle->errnum = CS_ERR_DETAIL;
1032 return false;
1033 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001034
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001035 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001036}
1037
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001038CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001039int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001040{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001041 struct cs_struct *handle;
1042 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001043 if (!ud)
1044 return -1;
1045
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001046 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001047
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001048 if (!handle->detail) {
1049 handle->errnum = CS_ERR_DETAIL;
1050 return -1;
1051 }
1052
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001053 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001054 handle->errnum = CS_ERR_SKIPDATA;
1055 return -1;
1056 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001057
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001058 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001059 handle->errnum = CS_ERR_DETAIL;
1060 return -1;
1061 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001062
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001063 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001064
1065 switch (handle->arch) {
1066 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001067 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001068 return -1;
1069 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001070 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001071 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001072 count++;
1073 break;
1074 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001075 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001076 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001077 count++;
1078 break;
1079 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001080 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001081 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001082 count++;
1083 break;
1084 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001085 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001086 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001087 count++;
1088 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001089 case CS_ARCH_PPC:
1090 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001091 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001092 count++;
1093 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001094 case CS_ARCH_SPARC:
1095 for (i = 0; i < insn->detail->sparc.op_count; i++)
1096 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1097 count++;
1098 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001099 case CS_ARCH_SYSZ:
1100 for (i = 0; i < insn->detail->sysz.op_count; i++)
1101 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1102 count++;
1103 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001104 case CS_ARCH_XCORE:
1105 for (i = 0; i < insn->detail->xcore.op_count; i++)
1106 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1107 count++;
1108 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001109 }
1110
1111 return count;
1112}
1113
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001114CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001115int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001116 unsigned int post)
1117{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001118 struct cs_struct *handle;
1119 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001120 if (!ud)
1121 return -1;
1122
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001123 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001124
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001125 if (!handle->detail) {
1126 handle->errnum = CS_ERR_DETAIL;
1127 return -1;
1128 }
1129
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001130 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001131 handle->errnum = CS_ERR_SKIPDATA;
1132 return -1;
1133 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001134
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001135 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001136 handle->errnum = CS_ERR_DETAIL;
1137 return -1;
1138 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001139
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001140 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001141
1142 switch (handle->arch) {
1143 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001144 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001145 return -1;
1146 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001147 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001148 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001149 count++;
1150 if (count == post)
1151 return i;
1152 }
1153 break;
1154 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001155 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001156 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001157 count++;
1158 if (count == post)
1159 return i;
1160 }
1161 break;
1162 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001163 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001164 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001165 count++;
1166 if (count == post)
1167 return i;
1168 }
1169 break;
1170 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001171 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001172 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001173 count++;
1174 if (count == post)
1175 return i;
1176 }
1177 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001178 case CS_ARCH_PPC:
1179 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001180 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001181 count++;
1182 if (count == post)
1183 return i;
1184 }
1185 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001186 case CS_ARCH_SPARC:
1187 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1188 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1189 count++;
1190 if (count == post)
1191 return i;
1192 }
1193 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001194 case CS_ARCH_SYSZ:
1195 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1196 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1197 count++;
1198 if (count == post)
1199 return i;
1200 }
1201 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001202 case CS_ARCH_XCORE:
1203 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1204 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1205 count++;
1206 if (count == post)
1207 return i;
1208 }
1209 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001210 }
1211
1212 return -1;
1213}
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001214
1215CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001216cs_err CAPSTONE_API cs_regs_access(csh ud, const cs_insn *insn,
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001217 cs_regs regs_read, uint8_t *regs_read_count,
1218 cs_regs regs_write, uint8_t *regs_write_count)
1219{
1220 struct cs_struct *handle;
1221
1222 if (!ud)
1223 return -1;
1224
1225 handle = (struct cs_struct *)(uintptr_t)ud;
1226
1227#ifdef CAPSTONE_DIET
1228 // This API does not work in DIET mode
1229 handle->errnum = CS_ERR_DIET;
1230 return CS_ERR_DIET;
1231#else
1232 if (!handle->detail) {
1233 handle->errnum = CS_ERR_DETAIL;
1234 return CS_ERR_DETAIL;
1235 }
1236
1237 if (!insn->id) {
1238 handle->errnum = CS_ERR_SKIPDATA;
1239 return CS_ERR_SKIPDATA;
1240 }
1241
1242 if (!insn->detail) {
1243 handle->errnum = CS_ERR_DETAIL;
1244 return CS_ERR_DETAIL;
1245 }
1246
1247 if (handle->reg_access) {
1248 handle->reg_access(insn, regs_read, regs_read_count, regs_write, regs_write_count);
1249 } else {
1250 // this arch is unsupported yet
1251 handle->errnum = CS_ERR_ARCH;
1252 return CS_ERR_ARCH;
1253 }
1254
1255 return CS_ERR_OK;
1256#endif
1257}