blob: 82f9efec8c8ab03bbaefbede60548e1c92e11d00 [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)
Satoshi Tanda2df9a8e2016-09-27 08:08:58 -07004#pragma warning(disable:4996) // disable MSVC's warning on strcpy()
5#pragma warning(disable:28719) // disable MSVC's warning on strcpy()
Axel 0vercl0k Souchet605faf12014-05-09 20:40:00 +01006#endif
reverser160e1982015-04-09 18:28:19 +01007#if defined(CAPSTONE_HAS_OSXKERNEL)
8#include <libkern/libkern.h>
9#else
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +080010#include <stddef.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080011#include <stdio.h>
12#include <stdlib.h>
reverser160e1982015-04-09 18:28:19 +010013#endif
14
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080015#include <string.h>
pancake9c10ace2015-02-24 04:55:55 +010016#include <capstone/capstone.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080017
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080018#include "utils.h"
Nguyen Anh Quynhc7404072014-01-05 11:35:47 +080019#include "MCRegisterInfo.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080020
tandasat45e5eab2016-05-11 21:48:32 -070021#if defined(_KERNEL_MODE)
22#include "windows\winkernel_mm.h"
23#endif
24
tandasatf7fe6402016-05-16 20:32:36 -070025// Issue #681: Windows kernel does not support formatting float point
26#if defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
27#if defined(CAPSTONE_HAS_ARM) || defined(CAPSTONE_HAS_ARM64) || defined(CAPSTONE_HAS_M68K)
28#define CAPSTONE_STR_INTERNAL(x) #x
29#define CAPSTONE_STR(x) CAPSTONE_STR_INTERNAL(x)
30#define CAPSTONE_MSVC_WRANING_PREFIX __FILE__ "("CAPSTONE_STR(__LINE__)") : warning message : "
31
32#pragma message(CAPSTONE_MSVC_WRANING_PREFIX "Windows driver does not support full features for selected architecture(s). Define CAPSTONE_DIET to compile Capstone with only supported features. See issue #681 for details.")
33
34#undef CAPSTONE_MSVC_WRANING_PREFIX
35#undef CAPSTONE_STR
36#undef CAPSTONE_STR_INTERNAL
37#endif
38#endif // defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
39
tandasat45e5eab2016-05-11 21:48:32 -070040#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(CAPSTONE_DIET) && !defined(_KERNEL_MODE)
Nguyen Anh Quynh472a4a42014-03-06 09:13:04 +080041#define INSN_CACHE_SIZE 32
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080042#else
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080043// reduce stack variable size for kernel/firmware
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080044#define INSN_CACHE_SIZE 8
45#endif
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +080046
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080047// default SKIPDATA mnemonic
vit9696a8c7e4a2017-05-25 11:01:48 +030048#ifndef CAPSTONE_DIET
Nguyen Anh Quynhc75a9092014-04-10 10:26:49 +080049#define SKIPDATA_MNEM ".byte"
vit9696a8c7e4a2017-05-25 11:01:48 +030050#else // No printing is available in diet mode
51#define SKIPDATA_MNEM NULL
52#endif
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080053
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080054cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080055cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
56void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080057
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080058extern void ARM_enable(void);
59extern void AArch64_enable(void);
Daniel Collin2ee675c2015-08-03 18:45:08 +020060extern void M68K_enable(void);
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080061extern void Mips_enable(void);
62extern void X86_enable(void);
63extern void PPC_enable(void);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080064extern void Sparc_enable(void);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080065extern void SystemZ_enable(void);
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080066extern void XCore_enable(void);
Fotis Loukos0850d552016-05-03 15:52:11 +030067extern void TMS320C64x_enable(void);
danghvu701b8502014-01-09 11:06:44 +070068
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080069static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080070{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080071 static bool initialized = false;
72
73 if (initialized)
74 return;
75
danghvub33bd2c2014-01-09 12:22:56 +070076#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080077 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070078#endif
79#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080080 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070081#endif
Daniel Collin2ee675c2015-08-03 18:45:08 +020082#ifdef CAPSTONE_HAS_M68K
83 M68K_enable();
84#endif
danghvub33bd2c2014-01-09 12:22:56 +070085#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080086 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070087#endif
danghvub33bd2c2014-01-09 12:22:56 +070088#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080089 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070090#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080091#ifdef CAPSTONE_HAS_SPARC
92 Sparc_enable();
93#endif
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080094#ifdef CAPSTONE_HAS_SYSZ
95 SystemZ_enable();
96#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080097#ifdef CAPSTONE_HAS_X86
98 X86_enable();
99#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800100#ifdef CAPSTONE_HAS_XCORE
101 XCore_enable();
102#endif
Fotis Loukos0850d552016-05-03 15:52:11 +0300103#ifdef CAPSTONE_HAS_TMS320C64X
104 TMS320C64x_enable();
105#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800106
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800107
108 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +0700109}
110
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800111unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +0800112
tandasat45e5eab2016-05-11 21:48:32 -0700113#if defined(CAPSTONE_USE_SYS_DYN_MEM)
114#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
tandasatc45f1db2016-05-11 22:52:34 -0700115// default
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800116cs_malloc_t cs_mem_malloc = malloc;
117cs_calloc_t cs_mem_calloc = calloc;
118cs_realloc_t cs_mem_realloc = realloc;
119cs_free_t cs_mem_free = free;
Koutheir Attouchi201a0a12016-04-12 09:25:37 +0200120#if defined(_WIN32_WCE)
121cs_vsnprintf_t cs_vsnprintf = _vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800122#else
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800123cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700124#endif // defined(_WIN32_WCE)
125
tandasat45e5eab2016-05-11 21:48:32 -0700126#elif defined(_KERNEL_MODE)
tandasatc45f1db2016-05-11 22:52:34 -0700127// Windows driver
tandasat45e5eab2016-05-11 21:48:32 -0700128cs_malloc_t cs_mem_malloc = cs_winkernel_malloc;
129cs_calloc_t cs_mem_calloc = cs_winkernel_calloc;
130cs_realloc_t cs_mem_realloc = cs_winkernel_realloc;
131cs_free_t cs_mem_free = cs_winkernel_free;
132cs_vsnprintf_t cs_vsnprintf = cs_winkernel_vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700133
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800134#else
tandasatc45f1db2016-05-11 22:52:34 -0700135// OSX kernel
Pb86ad3d62015-11-05 18:08:25 +0100136extern void* kern_os_malloc(size_t size);
137extern void kern_os_free(void* addr);
138extern void* kern_os_realloc(void* addr, size_t nsize);
139
Nguyen Anh Quynh197e4572015-11-10 22:56:53 +0800140static void* cs_kern_os_calloc(size_t num, size_t size)
141{
Pb86ad3d62015-11-05 18:08:25 +0100142 return kern_os_malloc(num * size); // malloc bzeroes the buffer
143}
144
145cs_malloc_t cs_mem_malloc = kern_os_malloc;
Pb2769c772015-11-06 14:44:55 +0100146cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
Pb86ad3d62015-11-05 18:08:25 +0100147cs_realloc_t cs_mem_realloc = kern_os_realloc;
148cs_free_t cs_mem_free = kern_os_free;
149cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700150
151#endif // !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
Pb86ad3d62015-11-05 18:08:25 +0100152#else
tandasatc45f1db2016-05-11 22:52:34 -0700153// User-defined
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800154cs_malloc_t cs_mem_malloc = NULL;
155cs_calloc_t cs_mem_calloc = NULL;
156cs_realloc_t cs_mem_realloc = NULL;
157cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800158cs_vsnprintf_t cs_vsnprintf = NULL;
tandasatc45f1db2016-05-11 22:52:34 -0700159
160#endif // defined(CAPSTONE_USE_SYS_DYN_MEM)
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800161
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800162CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700163unsigned int CAPSTONE_API cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800164{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800165 archs_enable();
166
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800167 if (major != NULL && minor != NULL) {
168 *major = CS_API_MAJOR;
169 *minor = CS_API_MINOR;
170 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800171
172 return (CS_API_MAJOR << 8) + CS_API_MINOR;
173}
174
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800175CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700176bool CAPSTONE_API cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800177{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800178 archs_enable();
179
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800180 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800181 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800182 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800183 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Fotis Loukos0850d552016-05-03 15:52:11 +0300184 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE) |
Fotis Loukos0e7a2e72016-05-03 16:02:40 +0300185 (1 << CS_ARCH_M68K) | (1 << CS_ARCH_TMS320C64X));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800186
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800187 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800188 return all_arch & (1 << query);
189
190 if (query == CS_SUPPORT_DIET) {
191#ifdef CAPSTONE_DIET
192 return true;
193#else
194 return false;
195#endif
196 }
197
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800198 if (query == CS_SUPPORT_X86_REDUCE) {
199#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800200 return true;
201#else
202 return false;
203#endif
204 }
205
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800206 // unsupported query
207 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800208}
209
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800210CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700211cs_err CAPSTONE_API cs_errno(csh handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800212{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800213 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800214 if (!handle)
215 return CS_ERR_CSH;
216
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100217 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800218
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800219 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800220}
221
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800222CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700223const char * CAPSTONE_API cs_strerror(cs_err code)
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800224{
225 switch(code) {
226 default:
227 return "Unknown error code";
228 case CS_ERR_OK:
229 return "OK (CS_ERR_OK)";
230 case CS_ERR_MEM:
231 return "Out of memory (CS_ERR_MEM)";
232 case CS_ERR_ARCH:
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800233 return "Invalid/unsupported architecture(CS_ERR_ARCH)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800234 case CS_ERR_HANDLE:
235 return "Invalid handle (CS_ERR_HANDLE)";
236 case CS_ERR_CSH:
237 return "Invalid csh (CS_ERR_CSH)";
238 case CS_ERR_MODE:
239 return "Invalid mode (CS_ERR_MODE)";
240 case CS_ERR_OPTION:
241 return "Invalid option (CS_ERR_OPTION)";
242 case CS_ERR_DETAIL:
243 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800244 case CS_ERR_MEMSETUP:
245 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800246 case CS_ERR_VERSION:
247 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800248 case CS_ERR_DIET:
249 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800250 case CS_ERR_SKIPDATA:
251 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh8aba4cd2015-08-09 10:52:18 -0700252 case CS_ERR_X86_ATT:
253 return "AT&T syntax is unavailable (CS_ERR_X86_ATT)";
254 case CS_ERR_X86_INTEL:
255 return "INTEL syntax is unavailable (CS_ERR_X86_INTEL)";
256 case CS_ERR_X86_MASM:
257 return "MASM syntax is unavailable (CS_ERR_X86_MASM)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800258 }
259}
260
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800261CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700262cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800263{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800264 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800265 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800266 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800267 // Error: before cs_open(), dynamic memory management must be initialized
268 // with cs_option(CS_OPT_MEM)
269 return CS_ERR_MEMSETUP;
270
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800271 archs_enable();
272
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800273 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800274 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800275 if (!ud) {
276 // memory insufficient
277 return CS_ERR_MEM;
278 }
279
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800280 ud->errnum = CS_ERR_OK;
281 ud->arch = arch;
282 ud->mode = mode;
tandasat45e5eab2016-05-11 21:48:32 -0700283 ud->big_endian = (mode & CS_MODE_BIG_ENDIAN) != 0;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800284 // by default, do not break instruction into details
285 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800286
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800287 // default skipdata setup
288 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
289
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100290 err = arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800291 if (err) {
292 cs_mem_free(ud);
293 *handle = 0;
294 return err;
295 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800296
297 *handle = (uintptr_t)ud;
298
299 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800300 } else {
301 *handle = 0;
302 return CS_ERR_ARCH;
303 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800304}
305
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800306CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700307cs_err CAPSTONE_API cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800308{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800309 struct cs_struct *ud;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800310 struct insn_mnem *next, *tmp;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800311
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800312 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800313 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800314 return CS_ERR_CSH;
315
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100316 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800317
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800318 if (ud->printer_info)
319 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800320
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800321 // free the linked list of customized mnemonic
322 tmp = ud->mnem_list;
323 while(tmp) {
324 next = tmp->next;
325 cs_mem_free(tmp);
326 tmp = next;
327 }
328
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800329 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800330
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800331 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800332 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800333
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800334 // invalidate this handle by ZERO out its value.
335 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800336 *handle = 0;
337
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800338 return CS_ERR_OK;
339}
340
341// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800342static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800343 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800344{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800345#ifndef CAPSTONE_DIET
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700346 char *sp, *mnem;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800347#endif
tandasat45e5eab2016-05-11 21:48:32 -0700348 uint16_t copy_size = MIN(sizeof(insn->bytes), insn->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800349
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800350 // fill the instruction bytes.
351 // we might skip some redundant bytes in front in the case of X86
352 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
353 insn->size = copy_size;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800354
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800355 // alias instruction might have ID saved in OpcodePub
356 if (MCInst_getOpcodePub(mci))
357 insn->id = MCInst_getOpcodePub(mci);
358
359 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800360 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800361 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800362
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800363#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800364 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800365 // find first space or tab
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800366 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800367 for (sp = buffer; *sp; sp++) {
368 if (*sp == ' '|| *sp == '\t')
369 break;
370 if (*sp == '|') // lock|rep prefix for x86
371 *sp = ' ';
372 // copy to @mnemonic
373 *mnem = *sp;
374 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800375 }
376
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800377 *mnem = '\0';
378
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800379 // we might have customized mnemonic
380 if (handle->mnem_list) {
381 struct insn_mnem *tmp = handle->mnem_list;
382 while(tmp) {
383 if (tmp->insn.id == insn->id) {
384 // found this instruction, so copy its mnemonic
385 (void)strncpy(insn->mnemonic, tmp->insn.mnemonic, sizeof(insn->mnemonic) - 1);
386 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
387 break;
388 }
389 tmp = tmp->next;
390 }
391 }
392
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800393 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800394 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800395 // find the next non-space char
396 sp++;
397 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
398 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800399 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
400 } else
401 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800402#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800403}
404
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800405// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800406// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800407static uint8_t skipdata_size(cs_struct *handle)
408{
409 switch(handle->arch) {
410 default:
411 // should never reach
tandasat45e5eab2016-05-11 21:48:32 -0700412 return (uint8_t)-1;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800413 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800414 // skip 2 bytes on Thumb mode.
415 if (handle->mode & CS_MODE_THUMB)
416 return 2;
417 // otherwise, skip 4 bytes
418 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800419 case CS_ARCH_ARM64:
420 case CS_ARCH_MIPS:
421 case CS_ARCH_PPC:
422 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800423 // skip 4 bytes
424 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800425 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800426 // SystemZ instruction's length can be 2, 4 or 6 bytes,
427 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800428 return 2;
429 case CS_ARCH_X86:
430 // X86 has no restriction on instruction alignment
431 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800432 case CS_ARCH_XCORE:
433 // XCore instruction's length can be 2 or 4 bytes,
434 // so we just skip 2 bytes
435 return 2;
Daniel Collin2ee675c2015-08-03 18:45:08 +0200436 case CS_ARCH_M68K:
437 // M68K has 2 bytes instruction alignment but contain multibyte instruction so we skip 2 bytes
438 return 2;
Fotis Loukos0850d552016-05-03 15:52:11 +0300439 case CS_ARCH_TMS320C64X:
440 // TMS320C64x alignment is 4.
441 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800442 }
443}
444
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800445CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700446cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800447{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800448 struct cs_struct *handle;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800449 cs_opt_mnem *opt;
450
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800451 archs_enable();
452
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800453 // cs_option() can be called with NULL handle just for CS_OPT_MEM
454 // This is supposed to be executed before all other APIs (even cs_open())
455 if (type == CS_OPT_MEM) {
456 cs_opt_mem *mem = (cs_opt_mem *)value;
457
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800458 cs_mem_malloc = mem->malloc;
459 cs_mem_calloc = mem->calloc;
460 cs_mem_realloc = mem->realloc;
461 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800462 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800463
464 return CS_ERR_OK;
465 }
466
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100467 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600468 if (!handle)
469 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800470
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800471 switch(type) {
472 default:
473 break;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800474
Nguyen Anh Quynh256090a2016-03-14 13:52:23 +0800475 case CS_OPT_UNSIGNED:
476 handle->imm_unsigned = (cs_opt_value)value;
477 return CS_ERR_OK;
478
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800479 case CS_OPT_DETAIL:
Félix Cloutier3973d8b2015-03-02 22:06:56 -0500480 handle->detail = (cs_opt_value)value;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800481 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800482
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800483 case CS_OPT_SKIPDATA:
484 handle->skipdata = (value == CS_OPT_ON);
485 if (handle->skipdata) {
486 if (handle->skipdata_size == 0) {
487 // set the default skipdata size
488 handle->skipdata_size = skipdata_size(handle);
489 }
490 }
491 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800492
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800493 case CS_OPT_SKIPDATA_SETUP:
494 if (value)
495 handle->skipdata_setup = *((cs_opt_skipdata *)value);
496 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800497
498 case CS_OPT_MNEMONIC:
499 opt = (cs_opt_mnem *)value;
500 if (opt->id) {
501 if (opt->mnemonic) {
502 struct insn_mnem *tmp;
503
504 // add new instruction, or replace existing instruction
505 // 1. find if we already had this insn in the linked list
506 tmp = handle->mnem_list;
507 while(tmp) {
508 if (tmp->insn.id == opt->id) {
509 // found this instruction, so replace its mnemonic
510 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
511 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
512 break;
513 }
514 tmp = tmp->next;
515 }
516
517 // 2. add this instruction if we have not had it yet
518 if (!tmp) {
519 tmp = cs_mem_malloc(sizeof(*tmp));
520 tmp->insn.id = opt->id;
521 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
522 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
523 // this new instruction is heading the list
524 tmp->next = handle->mnem_list;
525 handle->mnem_list = tmp;
526 }
527 return CS_ERR_OK;
528 } else {
529 struct insn_mnem *prev, *tmp;
530
531 // we want to delete an existing instruction
532 // iterate the list to find the instruction to remove it
533 tmp = handle->mnem_list;
534 prev = tmp;
535 while(tmp) {
536 if (tmp->insn.id == opt->id) {
537 // delete this instruction
538 if (tmp == prev) {
539 // head of the list
540 handle->mnem_list = tmp->next;
541 } else {
542 prev->next = tmp->next;
543 }
544 cs_mem_free(tmp);
545 break;
546 }
547 prev = tmp;
548 tmp = tmp->next;
549 }
550 }
551 }
552 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800553 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800554
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800555 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800556}
557
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800558// generate @op_str for data instruction of SKIPDATA
vit9696a8c7e4a2017-05-25 11:01:48 +0300559#ifndef CAPSTONE_DIET
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800560static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
561{
562 char *p = opstr;
563 int len;
564 size_t i;
tandasat45e5eab2016-05-11 21:48:32 -0700565 size_t available = sizeof(((cs_insn*)NULL)->op_str);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800566
567 if (!size) {
568 opstr[0] = '\0';
569 return;
570 }
571
tandasat45e5eab2016-05-11 21:48:32 -0700572 len = cs_snprintf(p, available, "0x%02x", buffer[0]);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800573 p+= len;
tandasat45e5eab2016-05-11 21:48:32 -0700574 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800575
576 for(i = 1; i < size; i++) {
tandasat45e5eab2016-05-11 21:48:32 -0700577 len = cs_snprintf(p, available, ", 0x%02x", buffer[i]);
578 if (len < 0) {
579 break;
580 }
581 if ((size_t)len > available - 1) {
582 break;
583 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800584 p+= len;
tandasat45e5eab2016-05-11 21:48:32 -0700585 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800586 }
587}
vit9696a8c7e4a2017-05-25 11:01:48 +0300588#endif
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800589
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800590// dynamicly allocate memory to contain disasm insn
591// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800592CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700593size_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 +0800594{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800595 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800596 MCInst mci;
597 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800598 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800599 unsigned int f = 0; // index of the next instruction in the cache
600 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800601 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800602 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800603 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800604 void *tmp;
605 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800606 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800607 size_t size_org;
608 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800609 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500610 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800611
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800612 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800613 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800614 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800615 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800616 return 0;
617 }
618
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800619 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800620
Nguyen Anh Quynh56aa0ac2016-04-27 14:43:10 +0800621 // reset IT block of ARM structure
Nguyen Anh Quynh9d07c1c2016-04-27 14:47:05 +0800622 if (handle->arch == CS_ARCH_ARM)
623 handle->ITBlock.size = 0;
Nguyen Anh Quynh56aa0ac2016-04-27 14:43:10 +0800624
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800625#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800626 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400627 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800628#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800629
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800630 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800631 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800632 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800633 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800634
635 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700636 total = cs_mem_malloc(total_size);
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800637 if (total == NULL) {
638 // insufficient memory
Edward Williamsonf1e49752014-12-14 20:45:19 -0500639 handle->errnum = CS_ERR_MEM;
640 return 0;
641 }
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800642
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700643 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800644
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800645 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800646 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800647 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800648
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700649 // relative branches need to know the address & size of current insn
650 mci.address = offset;
651
652 if (handle->detail) {
653 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700654 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700655 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700656 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700657 }
658
659 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700660 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700661 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800662#ifdef CAPSTONE_DIET
663 // zero out mnemonic & op_str
664 mci.flat_insn->mnemonic[0] = '\0';
665 mci.flat_insn->op_str[0] = '\0';
666#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700667
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800668 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800669 if (r) {
670 SStream ss;
671 SStream_Init(&ss);
672
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700673 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800674
675 // map internal instruction opcode to public insn ID
676 handle->insn_id(handle, insn_cache, mci.Opcode);
677
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800678 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700679 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800680
Nguyen Anh Quynhdabc9f22016-07-15 20:37:19 +0800681 // adjust for pseudo opcode (X86)
682 if (handle->arch == CS_ARCH_X86)
683 insn_cache->id += mci.popcode_adjust;
684
danghvu0d1aad12014-10-01 23:12:18 -0500685 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800686 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800687 // encounter a broken instruction
688
689 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800690 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800691 cs_mem_free(insn_cache->detail);
692 }
693
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800694 // if there is no request to skip data, or remaining data is too small,
695 // then bail out
696 if (!handle->skipdata || handle->skipdata_size > size)
697 break;
698
699 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800700 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800701 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800702 if (skipdata_bytes > size)
703 // remaining data is not enough
704 break;
705
706 if (!skipdata_bytes)
707 // user requested not to skip data, so bail out
708 break;
709 } else
710 skipdata_bytes = handle->skipdata_size;
711
712 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700713 insn_cache->id = 0; // invalid ID for this "data" instruction
714 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800715 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700716 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
vit9696a8c7e4a2017-05-25 11:01:48 +0300717#ifdef CAPSTONE_DIET
718 insn_cache->mnemonic[0] = '\0';
719 insn_cache->op_str[0] = '\0';
720#else
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700721 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
722 sizeof(insn_cache->mnemonic) - 1);
723 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
vit9696a8c7e4a2017-05-25 11:01:48 +0300724#endif
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700725 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800726
danghvu0d1aad12014-10-01 23:12:18 -0500727 next_offset = skipdata_bytes;
728 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800729
danghvu0d1aad12014-10-01 23:12:18 -0500730 // one more instruction entering the cache
731 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800732
danghvu0d1aad12014-10-01 23:12:18 -0500733 // one more instruction disassembled
734 c++;
735 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800736 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500737 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800738
danghvu0d1aad12014-10-01 23:12:18 -0500739 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800740 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500741 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500742 total_size += (sizeof(cs_insn) * cache_size);
743 tmp = cs_mem_realloc(total, total_size);
744 if (tmp == NULL) { // insufficient memory
745 if (handle->detail) {
746 insn_cache = (cs_insn *)total;
747 for (i = 0; i < c; i++, insn_cache++)
748 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800749 }
750
danghvu0d1aad12014-10-01 23:12:18 -0500751 cs_mem_free(total);
752 *insn = NULL;
753 handle->errnum = CS_ERR_MEM;
754 return 0;
755 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800756
danghvu0d1aad12014-10-01 23:12:18 -0500757 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800758 // continue to fill in the cache after the last instruction
759 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800760
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800761 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500762 f = 0;
763 } else
764 insn_cache++;
765
766 buffer += next_offset;
767 size -= next_offset;
768 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800769 }
770
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800771 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800772 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800773 cs_mem_free(total);
774 total = NULL;
775 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800776 // total did not fully use the last cache, so downsize it
Tyler J. Stacheckid07a21b2015-10-23 20:59:20 -0400777 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800778 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800779 // free all detail pointers
780 if (handle->detail) {
781 insn_cache = (cs_insn *)total;
782 for (i = 0; i < c; i++, insn_cache++)
783 cs_mem_free(insn_cache->detail);
784 }
785
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800786 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800787 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800788
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800789 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800790 return 0;
791 }
792
793 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800794 }
795
796 *insn = total;
797
798 return c;
799}
800
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800801CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800802CAPSTONE_DEPRECATED
tandasat45e5eab2016-05-11 21:48:32 -0700803size_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 +0800804{
805 return cs_disasm(ud, buffer, size, offset, count, insn);
806}
807
808CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700809void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800810{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800811 size_t i;
812
813 // free all detail pointers
814 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800815 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800816
817 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800818 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800819}
820
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800821CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700822cs_insn * CAPSTONE_API cs_malloc(csh ud)
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800823{
824 cs_insn *insn;
825 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
826
827 insn = cs_mem_malloc(sizeof(cs_insn));
828 if (!insn) {
829 // insufficient memory
830 handle->errnum = CS_ERR_MEM;
831 return NULL;
832 } else {
833 if (handle->detail) {
834 // allocate memory for @detail pointer
835 insn->detail = cs_mem_malloc(sizeof(cs_detail));
836 if (insn->detail == NULL) { // insufficient memory
837 cs_mem_free(insn);
838 handle->errnum = CS_ERR_MEM;
839 return NULL;
840 }
841 } else
842 insn->detail = NULL;
843 }
844
845 return insn;
846}
847
hlide993f3622014-10-05 18:14:40 +0200848// iterator for instruction "single-stepping"
849CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700850bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800851 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200852{
853 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200854 uint16_t insn_size;
855 MCInst mci;
856 bool r;
857
858 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800859 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800860 return false;
hlide993f3622014-10-05 18:14:40 +0200861 }
862
863 handle->errnum = CS_ERR_OK;
864
hlide993f3622014-10-05 18:14:40 +0200865 MCInst_Init(&mci);
866 mci.csh = handle;
867
868 // relative branches need to know the address & size of current insn
869 mci.address = *address;
870
871 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800872 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200873 mci.flat_insn->address = *address;
874#ifdef CAPSTONE_DIET
875 // zero out mnemonic & op_str
876 mci.flat_insn->mnemonic[0] = '\0';
877 mci.flat_insn->op_str[0] = '\0';
878#endif
879
880 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800881 if (r) {
hlide993f3622014-10-05 18:14:40 +0200882 SStream ss;
883 SStream_Init(&ss);
884
885 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800886
887 // map internal instruction opcode to public insn ID
888 handle->insn_id(handle, insn, mci.Opcode);
889
hlide993f3622014-10-05 18:14:40 +0200890 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800891
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800892 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800893
Nguyen Anh Quynhdabc9f22016-07-15 20:37:19 +0800894 // adjust for pseudo opcode (X86)
895 if (handle->arch == CS_ARCH_X86)
896 insn->id += mci.popcode_adjust;
897
hlide993f3622014-10-05 18:14:40 +0200898 *code += insn_size;
899 *size -= insn_size;
900 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800901 } else { // encounter a broken instruction
902 size_t skipdata_bytes;
903
904 // if there is no request to skip data, or remaining data is too small,
905 // then bail out
906 if (!handle->skipdata || handle->skipdata_size > *size)
907 return false;
908
909 if (handle->skipdata_setup.callback) {
910 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
911 0, handle->skipdata_setup.user_data);
912 if (skipdata_bytes > *size)
913 // remaining data is not enough
914 return false;
915
916 if (!skipdata_bytes)
917 // user requested not to skip data, so bail out
918 return false;
919 } else
920 skipdata_bytes = handle->skipdata_size;
921
922 // we have to skip some amount of data, depending on arch & mode
923 insn->id = 0; // invalid ID for this "data" instruction
924 insn->address = *address;
925 insn->size = (uint16_t)skipdata_bytes;
vit9696a8c7e4a2017-05-25 11:01:48 +0300926#ifdef CAPSTONE_DIET
927 insn->mnemonic[0] = '\0';
928 insn->op_str[0] = '\0';
929#else
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800930 memcpy(insn->bytes, *code, skipdata_bytes);
931 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
932 sizeof(insn->mnemonic) - 1);
933 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
vit9696a8c7e4a2017-05-25 11:01:48 +0300934#endif
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800935
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800936 *code += skipdata_bytes;
937 *size -= skipdata_bytes;
938 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200939 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800940
941 return true;
hlide993f3622014-10-05 18:14:40 +0200942}
943
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800944// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800945CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700946const char * CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800947{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800948 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800949
950 if (!handle || handle->reg_name == NULL) {
951 return NULL;
952 }
953
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800954 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800955}
956
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800957CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700958const char * CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800959{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800960 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800961
962 if (!handle || handle->insn_name == NULL) {
963 return NULL;
964 }
965
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800966 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800967}
968
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800969CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700970const char * CAPSTONE_API cs_group_name(csh ud, unsigned int group)
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800971{
972 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
973
974 if (!handle || handle->group_name == NULL) {
975 return NULL;
976 }
977
978 return handle->group_name(ud, group);
979}
980
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800981CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700982bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800983{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800984 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800985 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800986 return false;
987
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100988 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200989
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800990 if (!handle->detail) {
991 handle->errnum = CS_ERR_DETAIL;
992 return false;
993 }
994
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800995 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200996 handle->errnum = CS_ERR_SKIPDATA;
997 return false;
998 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200999
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001000 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001001 handle->errnum = CS_ERR_DETAIL;
1002 return false;
1003 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001004
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001005 return arr_exist8(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001006}
1007
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001008CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001009bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001010{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001011 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001012 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001013 return false;
1014
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001015 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001016
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001017 if (!handle->detail) {
1018 handle->errnum = CS_ERR_DETAIL;
1019 return false;
1020 }
1021
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001022 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001023 handle->errnum = CS_ERR_SKIPDATA;
1024 return false;
1025 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001026
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001027 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001028 handle->errnum = CS_ERR_DETAIL;
1029 return false;
1030 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001031
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001032 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001033}
1034
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001035CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001036bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001037{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001038 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001039 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001040 return false;
1041
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001042 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +02001043
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001044 if (!handle->detail) {
1045 handle->errnum = CS_ERR_DETAIL;
1046 return false;
1047 }
1048
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001049 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001050 handle->errnum = CS_ERR_SKIPDATA;
1051 return false;
1052 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001053
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001054 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001055 handle->errnum = CS_ERR_DETAIL;
1056 return false;
1057 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001058
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001059 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001060}
1061
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001062CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001063int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001064{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001065 struct cs_struct *handle;
1066 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001067 if (!ud)
1068 return -1;
1069
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001070 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001071
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001072 if (!handle->detail) {
1073 handle->errnum = CS_ERR_DETAIL;
1074 return -1;
1075 }
1076
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001077 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001078 handle->errnum = CS_ERR_SKIPDATA;
1079 return -1;
1080 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001081
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001082 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001083 handle->errnum = CS_ERR_DETAIL;
1084 return -1;
1085 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001086
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001087 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001088
1089 switch (handle->arch) {
1090 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001091 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001092 return -1;
1093 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001094 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001095 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001096 count++;
1097 break;
1098 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001099 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001100 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001101 count++;
1102 break;
1103 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001104 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001105 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001106 count++;
1107 break;
1108 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001109 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001110 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001111 count++;
1112 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001113 case CS_ARCH_PPC:
1114 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001115 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001116 count++;
1117 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001118 case CS_ARCH_SPARC:
1119 for (i = 0; i < insn->detail->sparc.op_count; i++)
1120 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1121 count++;
1122 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001123 case CS_ARCH_SYSZ:
1124 for (i = 0; i < insn->detail->sysz.op_count; i++)
1125 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1126 count++;
1127 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001128 case CS_ARCH_XCORE:
1129 for (i = 0; i < insn->detail->xcore.op_count; i++)
1130 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1131 count++;
1132 break;
Fotis Loukos0e7a2e72016-05-03 16:02:40 +03001133 case CS_ARCH_M68K:
1134 for (i = 0; i < insn->detail->m68k.op_count; i++)
1135 if (insn->detail->m68k.operands[i].type == (m68k_op_type)op_type)
1136 count++;
1137 break;
Fotis Loukos0850d552016-05-03 15:52:11 +03001138 case CS_ARCH_TMS320C64X:
1139 for (i = 0; i < insn->detail->tms320c64x.op_count; i++)
1140 if (insn->detail->tms320c64x.operands[i].type == (tms320c64x_op_type)op_type)
1141 count++;
1142 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001143 }
1144
1145 return count;
1146}
1147
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001148CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001149int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001150 unsigned int post)
1151{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001152 struct cs_struct *handle;
1153 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001154 if (!ud)
1155 return -1;
1156
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001157 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001158
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001159 if (!handle->detail) {
1160 handle->errnum = CS_ERR_DETAIL;
1161 return -1;
1162 }
1163
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001164 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001165 handle->errnum = CS_ERR_SKIPDATA;
1166 return -1;
1167 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001168
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001169 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001170 handle->errnum = CS_ERR_DETAIL;
1171 return -1;
1172 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001173
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001174 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001175
1176 switch (handle->arch) {
1177 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001178 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001179 return -1;
1180 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001181 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001182 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001183 count++;
1184 if (count == post)
1185 return i;
1186 }
1187 break;
1188 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001189 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001190 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001191 count++;
1192 if (count == post)
1193 return i;
1194 }
1195 break;
1196 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001197 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001198 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001199 count++;
1200 if (count == post)
1201 return i;
1202 }
1203 break;
1204 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001205 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001206 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001207 count++;
1208 if (count == post)
1209 return i;
1210 }
1211 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001212 case CS_ARCH_PPC:
1213 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001214 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001215 count++;
1216 if (count == post)
1217 return i;
1218 }
1219 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001220 case CS_ARCH_SPARC:
1221 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1222 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1223 count++;
1224 if (count == post)
1225 return i;
1226 }
1227 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001228 case CS_ARCH_SYSZ:
1229 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1230 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1231 count++;
1232 if (count == post)
1233 return i;
1234 }
1235 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001236 case CS_ARCH_XCORE:
1237 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1238 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1239 count++;
1240 if (count == post)
1241 return i;
1242 }
1243 break;
Fotis Loukos0e7a2e72016-05-03 16:02:40 +03001244 case CS_ARCH_M68K:
1245 for (i = 0; i < insn->detail->m68k.op_count; i++) {
1246 if (insn->detail->m68k.operands[i].type == (m68k_op_type)op_type)
1247 count++;
1248 if (count == post)
1249 return i;
1250 }
1251 break;
Fotis Loukos0850d552016-05-03 15:52:11 +03001252 case CS_ARCH_TMS320C64X:
1253 for (i = 0; i < insn->detail->tms320c64x.op_count; i++) {
1254 if (insn->detail->tms320c64x.operands[i].type == (tms320c64x_op_type)op_type)
1255 count++;
1256 if (count == post)
1257 return i;
1258 }
1259 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001260 }
1261
1262 return -1;
1263}
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001264
1265CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001266cs_err CAPSTONE_API cs_regs_access(csh ud, const cs_insn *insn,
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001267 cs_regs regs_read, uint8_t *regs_read_count,
1268 cs_regs regs_write, uint8_t *regs_write_count)
1269{
1270 struct cs_struct *handle;
1271
1272 if (!ud)
1273 return -1;
1274
1275 handle = (struct cs_struct *)(uintptr_t)ud;
1276
1277#ifdef CAPSTONE_DIET
1278 // This API does not work in DIET mode
1279 handle->errnum = CS_ERR_DIET;
1280 return CS_ERR_DIET;
1281#else
1282 if (!handle->detail) {
1283 handle->errnum = CS_ERR_DETAIL;
1284 return CS_ERR_DETAIL;
1285 }
1286
1287 if (!insn->id) {
1288 handle->errnum = CS_ERR_SKIPDATA;
1289 return CS_ERR_SKIPDATA;
1290 }
1291
1292 if (!insn->detail) {
1293 handle->errnum = CS_ERR_DETAIL;
1294 return CS_ERR_DETAIL;
1295 }
1296
1297 if (handle->reg_access) {
1298 handle->reg_access(insn, regs_read, regs_read_count, regs_write, regs_write_count);
1299 } else {
1300 // this arch is unsupported yet
1301 handle->errnum = CS_ERR_ARCH;
1302 return CS_ERR_ARCH;
1303 }
1304
1305 return CS_ERR_OK;
1306#endif
1307}