blob: d17cb550f503c1736b22bded8d90582192c633b1 [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 Quynhff7bba32014-11-03 16:32:06 +0800665
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700666 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800667
danghvu0d1aad12014-10-01 23:12:18 -0500668 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800669 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800670 // encounter a broken instruction
671
672 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800673 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800674 cs_mem_free(insn_cache->detail);
675 }
676
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800677 // if there is no request to skip data, or remaining data is too small,
678 // then bail out
679 if (!handle->skipdata || handle->skipdata_size > size)
680 break;
681
682 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800683 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800684 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800685 if (skipdata_bytes > size)
686 // remaining data is not enough
687 break;
688
689 if (!skipdata_bytes)
690 // user requested not to skip data, so bail out
691 break;
692 } else
693 skipdata_bytes = handle->skipdata_size;
694
695 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700696 insn_cache->id = 0; // invalid ID for this "data" instruction
697 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800698 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700699 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
700 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
701 sizeof(insn_cache->mnemonic) - 1);
702 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
703 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800704
danghvu0d1aad12014-10-01 23:12:18 -0500705 next_offset = skipdata_bytes;
706 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800707
danghvu0d1aad12014-10-01 23:12:18 -0500708 // one more instruction entering the cache
709 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800710
danghvu0d1aad12014-10-01 23:12:18 -0500711 // one more instruction disassembled
712 c++;
713 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800714 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500715 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800716
danghvu0d1aad12014-10-01 23:12:18 -0500717 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800718 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500719 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500720 total_size += (sizeof(cs_insn) * cache_size);
721 tmp = cs_mem_realloc(total, total_size);
722 if (tmp == NULL) { // insufficient memory
723 if (handle->detail) {
724 insn_cache = (cs_insn *)total;
725 for (i = 0; i < c; i++, insn_cache++)
726 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800727 }
728
danghvu0d1aad12014-10-01 23:12:18 -0500729 cs_mem_free(total);
730 *insn = NULL;
731 handle->errnum = CS_ERR_MEM;
732 return 0;
733 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800734
danghvu0d1aad12014-10-01 23:12:18 -0500735 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800736 // continue to fill in the cache after the last instruction
737 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800738
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800739 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500740 f = 0;
741 } else
742 insn_cache++;
743
744 buffer += next_offset;
745 size -= next_offset;
746 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800747 }
748
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800749 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800750 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800751 cs_mem_free(total);
752 total = NULL;
753 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800754 // total did not fully use the last cache, so downsize it
Tyler J. Stacheckid07a21b2015-10-23 20:59:20 -0400755 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800756 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800757 // free all detail pointers
758 if (handle->detail) {
759 insn_cache = (cs_insn *)total;
760 for (i = 0; i < c; i++, insn_cache++)
761 cs_mem_free(insn_cache->detail);
762 }
763
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800764 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800765 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800766
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800767 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800768 return 0;
769 }
770
771 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800772 }
773
774 *insn = total;
775
776 return c;
777}
778
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800779CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800780CAPSTONE_DEPRECATED
tandasat45e5eab2016-05-11 21:48:32 -0700781size_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 +0800782{
783 return cs_disasm(ud, buffer, size, offset, count, insn);
784}
785
786CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700787void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800788{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800789 size_t i;
790
791 // free all detail pointers
792 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800793 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800794
795 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800796 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800797}
798
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800799CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700800cs_insn * CAPSTONE_API cs_malloc(csh ud)
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800801{
802 cs_insn *insn;
803 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
804
805 insn = cs_mem_malloc(sizeof(cs_insn));
806 if (!insn) {
807 // insufficient memory
808 handle->errnum = CS_ERR_MEM;
809 return NULL;
810 } else {
811 if (handle->detail) {
812 // allocate memory for @detail pointer
813 insn->detail = cs_mem_malloc(sizeof(cs_detail));
814 if (insn->detail == NULL) { // insufficient memory
815 cs_mem_free(insn);
816 handle->errnum = CS_ERR_MEM;
817 return NULL;
818 }
819 } else
820 insn->detail = NULL;
821 }
822
823 return insn;
824}
825
hlide993f3622014-10-05 18:14:40 +0200826// iterator for instruction "single-stepping"
827CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700828bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800829 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200830{
831 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200832 uint16_t insn_size;
833 MCInst mci;
834 bool r;
835
836 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800837 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800838 return false;
hlide993f3622014-10-05 18:14:40 +0200839 }
840
841 handle->errnum = CS_ERR_OK;
842
hlide993f3622014-10-05 18:14:40 +0200843 MCInst_Init(&mci);
844 mci.csh = handle;
845
846 // relative branches need to know the address & size of current insn
847 mci.address = *address;
848
849 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800850 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200851 mci.flat_insn->address = *address;
852#ifdef CAPSTONE_DIET
853 // zero out mnemonic & op_str
854 mci.flat_insn->mnemonic[0] = '\0';
855 mci.flat_insn->op_str[0] = '\0';
856#endif
857
858 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800859 if (r) {
hlide993f3622014-10-05 18:14:40 +0200860 SStream ss;
861 SStream_Init(&ss);
862
863 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800864
865 // map internal instruction opcode to public insn ID
866 handle->insn_id(handle, insn, mci.Opcode);
867
hlide993f3622014-10-05 18:14:40 +0200868 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800869
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800870 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800871
hlide993f3622014-10-05 18:14:40 +0200872 *code += insn_size;
873 *size -= insn_size;
874 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800875 } else { // encounter a broken instruction
876 size_t skipdata_bytes;
877
878 // if there is no request to skip data, or remaining data is too small,
879 // then bail out
880 if (!handle->skipdata || handle->skipdata_size > *size)
881 return false;
882
883 if (handle->skipdata_setup.callback) {
884 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
885 0, handle->skipdata_setup.user_data);
886 if (skipdata_bytes > *size)
887 // remaining data is not enough
888 return false;
889
890 if (!skipdata_bytes)
891 // user requested not to skip data, so bail out
892 return false;
893 } else
894 skipdata_bytes = handle->skipdata_size;
895
896 // we have to skip some amount of data, depending on arch & mode
897 insn->id = 0; // invalid ID for this "data" instruction
898 insn->address = *address;
899 insn->size = (uint16_t)skipdata_bytes;
900 memcpy(insn->bytes, *code, skipdata_bytes);
901 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
902 sizeof(insn->mnemonic) - 1);
903 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
904
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800905 *code += skipdata_bytes;
906 *size -= skipdata_bytes;
907 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200908 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800909
910 return true;
hlide993f3622014-10-05 18:14:40 +0200911}
912
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800913// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800914CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700915const char * CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800916{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800917 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800918
919 if (!handle || handle->reg_name == NULL) {
920 return NULL;
921 }
922
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800923 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800924}
925
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800926CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700927const char * CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800928{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800929 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800930
931 if (!handle || handle->insn_name == NULL) {
932 return NULL;
933 }
934
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800935 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800936}
937
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800938CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700939const char * CAPSTONE_API cs_group_name(csh ud, unsigned int group)
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800940{
941 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
942
943 if (!handle || handle->group_name == NULL) {
944 return NULL;
945 }
946
947 return handle->group_name(ud, group);
948}
949
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800950CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700951bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800952{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800953 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800954 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800955 return false;
956
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100957 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200958
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800959 if (!handle->detail) {
960 handle->errnum = CS_ERR_DETAIL;
961 return false;
962 }
963
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800964 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200965 handle->errnum = CS_ERR_SKIPDATA;
966 return false;
967 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200968
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800969 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200970 handle->errnum = CS_ERR_DETAIL;
971 return false;
972 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200973
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800974 return arr_exist8(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800975}
976
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800977CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700978bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800979{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800980 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800981 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800982 return false;
983
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100984 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200985
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800986 if (!handle->detail) {
987 handle->errnum = CS_ERR_DETAIL;
988 return false;
989 }
990
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800991 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200992 handle->errnum = CS_ERR_SKIPDATA;
993 return false;
994 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200995
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800996 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200997 handle->errnum = CS_ERR_DETAIL;
998 return false;
999 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001000
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001001 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001002}
1003
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001004CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001005bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001006{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001007 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001008 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001009 return false;
1010
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001011 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +02001012
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001013 if (!handle->detail) {
1014 handle->errnum = CS_ERR_DETAIL;
1015 return false;
1016 }
1017
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001018 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001019 handle->errnum = CS_ERR_SKIPDATA;
1020 return false;
1021 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001022
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001023 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001024 handle->errnum = CS_ERR_DETAIL;
1025 return false;
1026 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001027
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001028 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001029}
1030
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001031CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001032int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001033{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001034 struct cs_struct *handle;
1035 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001036 if (!ud)
1037 return -1;
1038
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001039 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001040
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001041 if (!handle->detail) {
1042 handle->errnum = CS_ERR_DETAIL;
1043 return -1;
1044 }
1045
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001046 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001047 handle->errnum = CS_ERR_SKIPDATA;
1048 return -1;
1049 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001050
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001051 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001052 handle->errnum = CS_ERR_DETAIL;
1053 return -1;
1054 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001055
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001056 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001057
1058 switch (handle->arch) {
1059 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001060 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001061 return -1;
1062 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001063 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001064 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001065 count++;
1066 break;
1067 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001068 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001069 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001070 count++;
1071 break;
1072 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001073 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001074 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001075 count++;
1076 break;
1077 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001078 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001079 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001080 count++;
1081 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001082 case CS_ARCH_PPC:
1083 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001084 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001085 count++;
1086 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001087 case CS_ARCH_SPARC:
1088 for (i = 0; i < insn->detail->sparc.op_count; i++)
1089 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1090 count++;
1091 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001092 case CS_ARCH_SYSZ:
1093 for (i = 0; i < insn->detail->sysz.op_count; i++)
1094 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1095 count++;
1096 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001097 case CS_ARCH_XCORE:
1098 for (i = 0; i < insn->detail->xcore.op_count; i++)
1099 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1100 count++;
1101 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001102 }
1103
1104 return count;
1105}
1106
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001107CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001108int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001109 unsigned int post)
1110{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001111 struct cs_struct *handle;
1112 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001113 if (!ud)
1114 return -1;
1115
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001116 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001117
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001118 if (!handle->detail) {
1119 handle->errnum = CS_ERR_DETAIL;
1120 return -1;
1121 }
1122
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001123 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001124 handle->errnum = CS_ERR_SKIPDATA;
1125 return -1;
1126 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001127
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001128 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001129 handle->errnum = CS_ERR_DETAIL;
1130 return -1;
1131 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001132
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001133 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001134
1135 switch (handle->arch) {
1136 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001137 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001138 return -1;
1139 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001140 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001141 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001142 count++;
1143 if (count == post)
1144 return i;
1145 }
1146 break;
1147 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001148 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001149 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001150 count++;
1151 if (count == post)
1152 return i;
1153 }
1154 break;
1155 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001156 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001157 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001158 count++;
1159 if (count == post)
1160 return i;
1161 }
1162 break;
1163 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001164 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001165 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001166 count++;
1167 if (count == post)
1168 return i;
1169 }
1170 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001171 case CS_ARCH_PPC:
1172 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001173 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001174 count++;
1175 if (count == post)
1176 return i;
1177 }
1178 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001179 case CS_ARCH_SPARC:
1180 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1181 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1182 count++;
1183 if (count == post)
1184 return i;
1185 }
1186 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001187 case CS_ARCH_SYSZ:
1188 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1189 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1190 count++;
1191 if (count == post)
1192 return i;
1193 }
1194 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001195 case CS_ARCH_XCORE:
1196 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1197 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1198 count++;
1199 if (count == post)
1200 return i;
1201 }
1202 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001203 }
1204
1205 return -1;
1206}
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001207
1208CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001209cs_err CAPSTONE_API cs_regs_access(csh ud, const cs_insn *insn,
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001210 cs_regs regs_read, uint8_t *regs_read_count,
1211 cs_regs regs_write, uint8_t *regs_write_count)
1212{
1213 struct cs_struct *handle;
1214
1215 if (!ud)
1216 return -1;
1217
1218 handle = (struct cs_struct *)(uintptr_t)ud;
1219
1220#ifdef CAPSTONE_DIET
1221 // This API does not work in DIET mode
1222 handle->errnum = CS_ERR_DIET;
1223 return CS_ERR_DIET;
1224#else
1225 if (!handle->detail) {
1226 handle->errnum = CS_ERR_DETAIL;
1227 return CS_ERR_DETAIL;
1228 }
1229
1230 if (!insn->id) {
1231 handle->errnum = CS_ERR_SKIPDATA;
1232 return CS_ERR_SKIPDATA;
1233 }
1234
1235 if (!insn->detail) {
1236 handle->errnum = CS_ERR_DETAIL;
1237 return CS_ERR_DETAIL;
1238 }
1239
1240 if (handle->reg_access) {
1241 handle->reg_access(insn, regs_read, regs_read_count, regs_write, regs_write_count);
1242 } else {
1243 // this arch is unsupported yet
1244 handle->errnum = CS_ERR_ARCH;
1245 return CS_ERR_ARCH;
1246 }
1247
1248 return CS_ERR_OK;
1249#endif
1250}