blob: 19c65c7159e6c7a171fa4f32ad9cc354e28464c3 [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
Nguyen Anh Quynhc75a9092014-04-10 10:26:49 +080048#define SKIPDATA_MNEM ".byte"
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080049
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080050cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080051cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
52void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080053
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080054extern void ARM_enable(void);
55extern void AArch64_enable(void);
Daniel Collin2ee675c2015-08-03 18:45:08 +020056extern void M68K_enable(void);
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080057extern void Mips_enable(void);
58extern void X86_enable(void);
59extern void PPC_enable(void);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080060extern void Sparc_enable(void);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080061extern void SystemZ_enable(void);
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080062extern void XCore_enable(void);
Fotis Loukos0850d552016-05-03 15:52:11 +030063extern void TMS320C64x_enable(void);
danghvu701b8502014-01-09 11:06:44 +070064
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080065static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080066{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080067 static bool initialized = false;
68
69 if (initialized)
70 return;
71
danghvub33bd2c2014-01-09 12:22:56 +070072#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080073 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070074#endif
75#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080076 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070077#endif
Daniel Collin2ee675c2015-08-03 18:45:08 +020078#ifdef CAPSTONE_HAS_M68K
79 M68K_enable();
80#endif
danghvub33bd2c2014-01-09 12:22:56 +070081#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080082 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070083#endif
danghvub33bd2c2014-01-09 12:22:56 +070084#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080085 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070086#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080087#ifdef CAPSTONE_HAS_SPARC
88 Sparc_enable();
89#endif
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080090#ifdef CAPSTONE_HAS_SYSZ
91 SystemZ_enable();
92#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080093#ifdef CAPSTONE_HAS_X86
94 X86_enable();
95#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080096#ifdef CAPSTONE_HAS_XCORE
97 XCore_enable();
98#endif
Fotis Loukos0850d552016-05-03 15:52:11 +030099#ifdef CAPSTONE_HAS_TMS320C64X
100 TMS320C64x_enable();
101#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800102
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800103
104 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +0700105}
106
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800107unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +0800108
tandasat45e5eab2016-05-11 21:48:32 -0700109#if defined(CAPSTONE_USE_SYS_DYN_MEM)
110#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
tandasatc45f1db2016-05-11 22:52:34 -0700111// default
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800112cs_malloc_t cs_mem_malloc = malloc;
113cs_calloc_t cs_mem_calloc = calloc;
114cs_realloc_t cs_mem_realloc = realloc;
115cs_free_t cs_mem_free = free;
Koutheir Attouchi201a0a12016-04-12 09:25:37 +0200116#if defined(_WIN32_WCE)
117cs_vsnprintf_t cs_vsnprintf = _vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800118#else
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800119cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700120#endif // defined(_WIN32_WCE)
121
tandasat45e5eab2016-05-11 21:48:32 -0700122#elif defined(_KERNEL_MODE)
tandasatc45f1db2016-05-11 22:52:34 -0700123// Windows driver
tandasat45e5eab2016-05-11 21:48:32 -0700124cs_malloc_t cs_mem_malloc = cs_winkernel_malloc;
125cs_calloc_t cs_mem_calloc = cs_winkernel_calloc;
126cs_realloc_t cs_mem_realloc = cs_winkernel_realloc;
127cs_free_t cs_mem_free = cs_winkernel_free;
128cs_vsnprintf_t cs_vsnprintf = cs_winkernel_vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700129
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800130#else
tandasatc45f1db2016-05-11 22:52:34 -0700131// OSX kernel
Pb86ad3d62015-11-05 18:08:25 +0100132extern void* kern_os_malloc(size_t size);
133extern void kern_os_free(void* addr);
134extern void* kern_os_realloc(void* addr, size_t nsize);
135
Nguyen Anh Quynh197e4572015-11-10 22:56:53 +0800136static void* cs_kern_os_calloc(size_t num, size_t size)
137{
Pb86ad3d62015-11-05 18:08:25 +0100138 return kern_os_malloc(num * size); // malloc bzeroes the buffer
139}
140
141cs_malloc_t cs_mem_malloc = kern_os_malloc;
Pb2769c772015-11-06 14:44:55 +0100142cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
Pb86ad3d62015-11-05 18:08:25 +0100143cs_realloc_t cs_mem_realloc = kern_os_realloc;
144cs_free_t cs_mem_free = kern_os_free;
145cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700146
147#endif // !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
Pb86ad3d62015-11-05 18:08:25 +0100148#else
tandasatc45f1db2016-05-11 22:52:34 -0700149// User-defined
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800150cs_malloc_t cs_mem_malloc = NULL;
151cs_calloc_t cs_mem_calloc = NULL;
152cs_realloc_t cs_mem_realloc = NULL;
153cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800154cs_vsnprintf_t cs_vsnprintf = NULL;
tandasatc45f1db2016-05-11 22:52:34 -0700155
156#endif // defined(CAPSTONE_USE_SYS_DYN_MEM)
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800157
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800158CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700159unsigned int CAPSTONE_API cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800160{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800161 archs_enable();
162
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800163 if (major != NULL && minor != NULL) {
164 *major = CS_API_MAJOR;
165 *minor = CS_API_MINOR;
166 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800167
168 return (CS_API_MAJOR << 8) + CS_API_MINOR;
169}
170
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800171CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700172bool CAPSTONE_API cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800173{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800174 archs_enable();
175
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800176 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800177 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800178 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800179 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Fotis Loukos0850d552016-05-03 15:52:11 +0300180 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE) |
Fotis Loukos0e7a2e72016-05-03 16:02:40 +0300181 (1 << CS_ARCH_M68K) | (1 << CS_ARCH_TMS320C64X));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800182
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800183 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800184 return all_arch & (1 << query);
185
186 if (query == CS_SUPPORT_DIET) {
187#ifdef CAPSTONE_DIET
188 return true;
189#else
190 return false;
191#endif
192 }
193
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800194 if (query == CS_SUPPORT_X86_REDUCE) {
195#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800196 return true;
197#else
198 return false;
199#endif
200 }
201
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800202 // unsupported query
203 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800204}
205
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800206CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700207cs_err CAPSTONE_API cs_errno(csh handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800208{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800209 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800210 if (!handle)
211 return CS_ERR_CSH;
212
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100213 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800214
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800215 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800216}
217
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800218CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700219const char * CAPSTONE_API cs_strerror(cs_err code)
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800220{
221 switch(code) {
222 default:
223 return "Unknown error code";
224 case CS_ERR_OK:
225 return "OK (CS_ERR_OK)";
226 case CS_ERR_MEM:
227 return "Out of memory (CS_ERR_MEM)";
228 case CS_ERR_ARCH:
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800229 return "Invalid/unsupported architecture(CS_ERR_ARCH)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800230 case CS_ERR_HANDLE:
231 return "Invalid handle (CS_ERR_HANDLE)";
232 case CS_ERR_CSH:
233 return "Invalid csh (CS_ERR_CSH)";
234 case CS_ERR_MODE:
235 return "Invalid mode (CS_ERR_MODE)";
236 case CS_ERR_OPTION:
237 return "Invalid option (CS_ERR_OPTION)";
238 case CS_ERR_DETAIL:
239 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800240 case CS_ERR_MEMSETUP:
241 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800242 case CS_ERR_VERSION:
243 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800244 case CS_ERR_DIET:
245 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800246 case CS_ERR_SKIPDATA:
247 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh8aba4cd2015-08-09 10:52:18 -0700248 case CS_ERR_X86_ATT:
249 return "AT&T syntax is unavailable (CS_ERR_X86_ATT)";
250 case CS_ERR_X86_INTEL:
251 return "INTEL syntax is unavailable (CS_ERR_X86_INTEL)";
252 case CS_ERR_X86_MASM:
253 return "MASM syntax is unavailable (CS_ERR_X86_MASM)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800254 }
255}
256
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800257CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700258cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800259{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800260 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800261 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800262 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800263 // Error: before cs_open(), dynamic memory management must be initialized
264 // with cs_option(CS_OPT_MEM)
265 return CS_ERR_MEMSETUP;
266
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800267 archs_enable();
268
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800269 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800270 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800271 if (!ud) {
272 // memory insufficient
273 return CS_ERR_MEM;
274 }
275
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800276 ud->errnum = CS_ERR_OK;
277 ud->arch = arch;
278 ud->mode = mode;
tandasat45e5eab2016-05-11 21:48:32 -0700279 ud->big_endian = (mode & CS_MODE_BIG_ENDIAN) != 0;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800280 // by default, do not break instruction into details
281 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800282
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800283 // default skipdata setup
284 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
285
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100286 err = arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800287 if (err) {
288 cs_mem_free(ud);
289 *handle = 0;
290 return err;
291 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800292
293 *handle = (uintptr_t)ud;
294
295 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800296 } else {
297 *handle = 0;
298 return CS_ERR_ARCH;
299 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800300}
301
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800302CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700303cs_err CAPSTONE_API cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800304{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800305 struct cs_struct *ud;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800306 struct insn_mnem *next, *tmp;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800307
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800308 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800309 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800310 return CS_ERR_CSH;
311
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100312 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800313
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800314 if (ud->printer_info)
315 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800316
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800317 // free the linked list of customized mnemonic
318 tmp = ud->mnem_list;
319 while(tmp) {
320 next = tmp->next;
321 cs_mem_free(tmp);
322 tmp = next;
323 }
324
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800325 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800326
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800327 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800328 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800329
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800330 // invalidate this handle by ZERO out its value.
331 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800332 *handle = 0;
333
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800334 return CS_ERR_OK;
335}
336
337// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800338static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800339 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800340{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800341#ifndef CAPSTONE_DIET
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700342 char *sp, *mnem;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800343#endif
tandasat45e5eab2016-05-11 21:48:32 -0700344 uint16_t copy_size = MIN(sizeof(insn->bytes), insn->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800345
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800346 // fill the instruction bytes.
347 // we might skip some redundant bytes in front in the case of X86
348 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
349 insn->size = copy_size;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800350
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800351 // alias instruction might have ID saved in OpcodePub
352 if (MCInst_getOpcodePub(mci))
353 insn->id = MCInst_getOpcodePub(mci);
354
355 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800356 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800357 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800358
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800359#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800360 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800361 // find first space or tab
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800362 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800363 for (sp = buffer; *sp; sp++) {
364 if (*sp == ' '|| *sp == '\t')
365 break;
366 if (*sp == '|') // lock|rep prefix for x86
367 *sp = ' ';
368 // copy to @mnemonic
369 *mnem = *sp;
370 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800371 }
372
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800373 *mnem = '\0';
374
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800375 // we might have customized mnemonic
376 if (handle->mnem_list) {
377 struct insn_mnem *tmp = handle->mnem_list;
378 while(tmp) {
379 if (tmp->insn.id == insn->id) {
380 // found this instruction, so copy its mnemonic
381 (void)strncpy(insn->mnemonic, tmp->insn.mnemonic, sizeof(insn->mnemonic) - 1);
382 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
383 break;
384 }
385 tmp = tmp->next;
386 }
387 }
388
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800389 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800390 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800391 // find the next non-space char
392 sp++;
393 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
394 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800395 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
396 } else
397 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800398#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800399}
400
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800401// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800402// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800403static uint8_t skipdata_size(cs_struct *handle)
404{
405 switch(handle->arch) {
406 default:
407 // should never reach
tandasat45e5eab2016-05-11 21:48:32 -0700408 return (uint8_t)-1;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800409 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800410 // skip 2 bytes on Thumb mode.
411 if (handle->mode & CS_MODE_THUMB)
412 return 2;
413 // otherwise, skip 4 bytes
414 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800415 case CS_ARCH_ARM64:
416 case CS_ARCH_MIPS:
417 case CS_ARCH_PPC:
418 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800419 // skip 4 bytes
420 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800421 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800422 // SystemZ instruction's length can be 2, 4 or 6 bytes,
423 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800424 return 2;
425 case CS_ARCH_X86:
426 // X86 has no restriction on instruction alignment
427 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800428 case CS_ARCH_XCORE:
429 // XCore instruction's length can be 2 or 4 bytes,
430 // so we just skip 2 bytes
431 return 2;
Daniel Collin2ee675c2015-08-03 18:45:08 +0200432 case CS_ARCH_M68K:
433 // M68K has 2 bytes instruction alignment but contain multibyte instruction so we skip 2 bytes
434 return 2;
Fotis Loukos0850d552016-05-03 15:52:11 +0300435 case CS_ARCH_TMS320C64X:
436 // TMS320C64x alignment is 4.
437 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800438 }
439}
440
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800441CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700442cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800443{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800444 struct cs_struct *handle;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800445 cs_opt_mnem *opt;
446
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800447 archs_enable();
448
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800449 // cs_option() can be called with NULL handle just for CS_OPT_MEM
450 // This is supposed to be executed before all other APIs (even cs_open())
451 if (type == CS_OPT_MEM) {
452 cs_opt_mem *mem = (cs_opt_mem *)value;
453
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800454 cs_mem_malloc = mem->malloc;
455 cs_mem_calloc = mem->calloc;
456 cs_mem_realloc = mem->realloc;
457 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800458 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800459
460 return CS_ERR_OK;
461 }
462
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100463 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600464 if (!handle)
465 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800466
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800467 switch(type) {
468 default:
469 break;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800470
Nguyen Anh Quynh256090a2016-03-14 13:52:23 +0800471 case CS_OPT_UNSIGNED:
472 handle->imm_unsigned = (cs_opt_value)value;
473 return CS_ERR_OK;
474
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800475 case CS_OPT_DETAIL:
Félix Cloutier3973d8b2015-03-02 22:06:56 -0500476 handle->detail = (cs_opt_value)value;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800477 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800478
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800479 case CS_OPT_SKIPDATA:
480 handle->skipdata = (value == CS_OPT_ON);
481 if (handle->skipdata) {
482 if (handle->skipdata_size == 0) {
483 // set the default skipdata size
484 handle->skipdata_size = skipdata_size(handle);
485 }
486 }
487 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800488
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800489 case CS_OPT_SKIPDATA_SETUP:
490 if (value)
491 handle->skipdata_setup = *((cs_opt_skipdata *)value);
492 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800493
494 case CS_OPT_MNEMONIC:
495 opt = (cs_opt_mnem *)value;
496 if (opt->id) {
497 if (opt->mnemonic) {
498 struct insn_mnem *tmp;
499
500 // add new instruction, or replace existing instruction
501 // 1. find if we already had this insn in the linked list
502 tmp = handle->mnem_list;
503 while(tmp) {
504 if (tmp->insn.id == opt->id) {
505 // found this instruction, so replace its mnemonic
506 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
507 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
508 break;
509 }
510 tmp = tmp->next;
511 }
512
513 // 2. add this instruction if we have not had it yet
514 if (!tmp) {
515 tmp = cs_mem_malloc(sizeof(*tmp));
516 tmp->insn.id = opt->id;
517 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
518 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
519 // this new instruction is heading the list
520 tmp->next = handle->mnem_list;
521 handle->mnem_list = tmp;
522 }
523 return CS_ERR_OK;
524 } else {
525 struct insn_mnem *prev, *tmp;
526
527 // we want to delete an existing instruction
528 // iterate the list to find the instruction to remove it
529 tmp = handle->mnem_list;
530 prev = tmp;
531 while(tmp) {
532 if (tmp->insn.id == opt->id) {
533 // delete this instruction
534 if (tmp == prev) {
535 // head of the list
536 handle->mnem_list = tmp->next;
537 } else {
538 prev->next = tmp->next;
539 }
540 cs_mem_free(tmp);
541 break;
542 }
543 prev = tmp;
544 tmp = tmp->next;
545 }
546 }
547 }
548 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800549 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800550
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800551 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800552}
553
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800554// generate @op_str for data instruction of SKIPDATA
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800555static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
556{
557 char *p = opstr;
558 int len;
559 size_t i;
tandasat45e5eab2016-05-11 21:48:32 -0700560 size_t available = sizeof(((cs_insn*)NULL)->op_str);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800561
562 if (!size) {
563 opstr[0] = '\0';
564 return;
565 }
566
tandasat45e5eab2016-05-11 21:48:32 -0700567 len = cs_snprintf(p, available, "0x%02x", buffer[0]);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800568 p+= len;
tandasat45e5eab2016-05-11 21:48:32 -0700569 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800570
571 for(i = 1; i < size; i++) {
tandasat45e5eab2016-05-11 21:48:32 -0700572 len = cs_snprintf(p, available, ", 0x%02x", buffer[i]);
573 if (len < 0) {
574 break;
575 }
576 if ((size_t)len > available - 1) {
577 break;
578 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800579 p+= len;
tandasat45e5eab2016-05-11 21:48:32 -0700580 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800581 }
582}
583
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800584// dynamicly allocate memory to contain disasm insn
585// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800586CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700587size_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 +0800588{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800589 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800590 MCInst mci;
591 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800592 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800593 unsigned int f = 0; // index of the next instruction in the cache
594 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800595 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800596 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800597 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800598 void *tmp;
599 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800600 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800601 size_t size_org;
602 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800603 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500604 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800605
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800606 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800607 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800608 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800609 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800610 return 0;
611 }
612
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800613 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800614
Nguyen Anh Quynh56aa0ac2016-04-27 14:43:10 +0800615 // reset IT block of ARM structure
Nguyen Anh Quynh9d07c1c2016-04-27 14:47:05 +0800616 if (handle->arch == CS_ARCH_ARM)
617 handle->ITBlock.size = 0;
Nguyen Anh Quynh56aa0ac2016-04-27 14:43:10 +0800618
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800619#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800620 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400621 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800622#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800623
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800624 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800625 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800626 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800627 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800628
629 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700630 total = cs_mem_malloc(total_size);
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800631 if (total == NULL) {
632 // insufficient memory
Edward Williamsonf1e49752014-12-14 20:45:19 -0500633 handle->errnum = CS_ERR_MEM;
634 return 0;
635 }
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800636
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700637 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800638
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800639 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800640 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800641 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800642
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700643 // relative branches need to know the address & size of current insn
644 mci.address = offset;
645
646 if (handle->detail) {
647 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700648 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700649 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700650 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700651 }
652
653 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700654 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700655 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800656#ifdef CAPSTONE_DIET
657 // zero out mnemonic & op_str
658 mci.flat_insn->mnemonic[0] = '\0';
659 mci.flat_insn->op_str[0] = '\0';
660#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700661
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800662 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800663 if (r) {
664 SStream ss;
665 SStream_Init(&ss);
666
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700667 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800668
669 // map internal instruction opcode to public insn ID
670 handle->insn_id(handle, insn_cache, mci.Opcode);
671
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800672 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700673 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800674
Nguyen Anh Quynhdabc9f22016-07-15 20:37:19 +0800675 // adjust for pseudo opcode (X86)
676 if (handle->arch == CS_ARCH_X86)
677 insn_cache->id += mci.popcode_adjust;
678
danghvu0d1aad12014-10-01 23:12:18 -0500679 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800680 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800681 // encounter a broken instruction
682
683 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800684 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800685 cs_mem_free(insn_cache->detail);
686 }
687
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800688 // if there is no request to skip data, or remaining data is too small,
689 // then bail out
690 if (!handle->skipdata || handle->skipdata_size > size)
691 break;
692
693 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800694 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800695 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800696 if (skipdata_bytes > size)
697 // remaining data is not enough
698 break;
699
700 if (!skipdata_bytes)
701 // user requested not to skip data, so bail out
702 break;
703 } else
704 skipdata_bytes = handle->skipdata_size;
705
706 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700707 insn_cache->id = 0; // invalid ID for this "data" instruction
708 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800709 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700710 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
711 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
712 sizeof(insn_cache->mnemonic) - 1);
713 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
714 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800715
danghvu0d1aad12014-10-01 23:12:18 -0500716 next_offset = skipdata_bytes;
717 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800718
danghvu0d1aad12014-10-01 23:12:18 -0500719 // one more instruction entering the cache
720 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800721
danghvu0d1aad12014-10-01 23:12:18 -0500722 // one more instruction disassembled
723 c++;
724 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800725 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500726 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800727
danghvu0d1aad12014-10-01 23:12:18 -0500728 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800729 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500730 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500731 total_size += (sizeof(cs_insn) * cache_size);
732 tmp = cs_mem_realloc(total, total_size);
733 if (tmp == NULL) { // insufficient memory
734 if (handle->detail) {
735 insn_cache = (cs_insn *)total;
736 for (i = 0; i < c; i++, insn_cache++)
737 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800738 }
739
danghvu0d1aad12014-10-01 23:12:18 -0500740 cs_mem_free(total);
741 *insn = NULL;
742 handle->errnum = CS_ERR_MEM;
743 return 0;
744 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800745
danghvu0d1aad12014-10-01 23:12:18 -0500746 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800747 // continue to fill in the cache after the last instruction
748 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800749
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800750 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500751 f = 0;
752 } else
753 insn_cache++;
754
755 buffer += next_offset;
756 size -= next_offset;
757 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800758 }
759
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800760 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800761 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800762 cs_mem_free(total);
763 total = NULL;
764 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800765 // total did not fully use the last cache, so downsize it
Tyler J. Stacheckid07a21b2015-10-23 20:59:20 -0400766 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800767 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800768 // free all detail pointers
769 if (handle->detail) {
770 insn_cache = (cs_insn *)total;
771 for (i = 0; i < c; i++, insn_cache++)
772 cs_mem_free(insn_cache->detail);
773 }
774
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800775 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800776 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800777
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800778 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800779 return 0;
780 }
781
782 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800783 }
784
785 *insn = total;
786
787 return c;
788}
789
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800790CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800791CAPSTONE_DEPRECATED
tandasat45e5eab2016-05-11 21:48:32 -0700792size_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 +0800793{
794 return cs_disasm(ud, buffer, size, offset, count, insn);
795}
796
797CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700798void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800799{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800800 size_t i;
801
802 // free all detail pointers
803 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800804 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800805
806 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800807 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800808}
809
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800810CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700811cs_insn * CAPSTONE_API cs_malloc(csh ud)
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800812{
813 cs_insn *insn;
814 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
815
816 insn = cs_mem_malloc(sizeof(cs_insn));
817 if (!insn) {
818 // insufficient memory
819 handle->errnum = CS_ERR_MEM;
820 return NULL;
821 } else {
822 if (handle->detail) {
823 // allocate memory for @detail pointer
824 insn->detail = cs_mem_malloc(sizeof(cs_detail));
825 if (insn->detail == NULL) { // insufficient memory
826 cs_mem_free(insn);
827 handle->errnum = CS_ERR_MEM;
828 return NULL;
829 }
830 } else
831 insn->detail = NULL;
832 }
833
834 return insn;
835}
836
hlide993f3622014-10-05 18:14:40 +0200837// iterator for instruction "single-stepping"
838CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700839bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800840 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200841{
842 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200843 uint16_t insn_size;
844 MCInst mci;
845 bool r;
846
847 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800848 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800849 return false;
hlide993f3622014-10-05 18:14:40 +0200850 }
851
852 handle->errnum = CS_ERR_OK;
853
hlide993f3622014-10-05 18:14:40 +0200854 MCInst_Init(&mci);
855 mci.csh = handle;
856
857 // relative branches need to know the address & size of current insn
858 mci.address = *address;
859
860 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800861 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200862 mci.flat_insn->address = *address;
863#ifdef CAPSTONE_DIET
864 // zero out mnemonic & op_str
865 mci.flat_insn->mnemonic[0] = '\0';
866 mci.flat_insn->op_str[0] = '\0';
867#endif
868
869 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800870 if (r) {
hlide993f3622014-10-05 18:14:40 +0200871 SStream ss;
872 SStream_Init(&ss);
873
874 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800875
876 // map internal instruction opcode to public insn ID
877 handle->insn_id(handle, insn, mci.Opcode);
878
hlide993f3622014-10-05 18:14:40 +0200879 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800880
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800881 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800882
Nguyen Anh Quynhdabc9f22016-07-15 20:37:19 +0800883 // adjust for pseudo opcode (X86)
884 if (handle->arch == CS_ARCH_X86)
885 insn->id += mci.popcode_adjust;
886
hlide993f3622014-10-05 18:14:40 +0200887 *code += insn_size;
888 *size -= insn_size;
889 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800890 } else { // encounter a broken instruction
891 size_t skipdata_bytes;
892
893 // if there is no request to skip data, or remaining data is too small,
894 // then bail out
895 if (!handle->skipdata || handle->skipdata_size > *size)
896 return false;
897
898 if (handle->skipdata_setup.callback) {
899 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
900 0, handle->skipdata_setup.user_data);
901 if (skipdata_bytes > *size)
902 // remaining data is not enough
903 return false;
904
905 if (!skipdata_bytes)
906 // user requested not to skip data, so bail out
907 return false;
908 } else
909 skipdata_bytes = handle->skipdata_size;
910
911 // we have to skip some amount of data, depending on arch & mode
912 insn->id = 0; // invalid ID for this "data" instruction
913 insn->address = *address;
914 insn->size = (uint16_t)skipdata_bytes;
915 memcpy(insn->bytes, *code, skipdata_bytes);
916 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
917 sizeof(insn->mnemonic) - 1);
918 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
919
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800920 *code += skipdata_bytes;
921 *size -= skipdata_bytes;
922 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200923 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800924
925 return true;
hlide993f3622014-10-05 18:14:40 +0200926}
927
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800928// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800929CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700930const char * CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800931{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800932 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800933
934 if (!handle || handle->reg_name == NULL) {
935 return NULL;
936 }
937
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800938 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800939}
940
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800941CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700942const char * CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800943{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800944 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800945
946 if (!handle || handle->insn_name == NULL) {
947 return NULL;
948 }
949
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800950 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800951}
952
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800953CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700954const char * CAPSTONE_API cs_group_name(csh ud, unsigned int group)
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800955{
956 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
957
958 if (!handle || handle->group_name == NULL) {
959 return NULL;
960 }
961
962 return handle->group_name(ud, group);
963}
964
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800965CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700966bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800967{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800968 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800969 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800970 return false;
971
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100972 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200973
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800974 if (!handle->detail) {
975 handle->errnum = CS_ERR_DETAIL;
976 return false;
977 }
978
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800979 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200980 handle->errnum = CS_ERR_SKIPDATA;
981 return false;
982 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200983
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800984 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200985 handle->errnum = CS_ERR_DETAIL;
986 return false;
987 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200988
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800989 return arr_exist8(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800990}
991
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800992CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700993bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800994{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800995 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800996 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800997 return false;
998
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100999 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001000
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001001 if (!handle->detail) {
1002 handle->errnum = CS_ERR_DETAIL;
1003 return false;
1004 }
1005
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001006 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001007 handle->errnum = CS_ERR_SKIPDATA;
1008 return false;
1009 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001010
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001011 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001012 handle->errnum = CS_ERR_DETAIL;
1013 return false;
1014 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001015
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001016 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001017}
1018
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001019CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001020bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001021{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001022 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001023 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001024 return false;
1025
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001026 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +02001027
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001028 if (!handle->detail) {
1029 handle->errnum = CS_ERR_DETAIL;
1030 return false;
1031 }
1032
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001033 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001034 handle->errnum = CS_ERR_SKIPDATA;
1035 return false;
1036 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001037
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001038 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001039 handle->errnum = CS_ERR_DETAIL;
1040 return false;
1041 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001042
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001043 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001044}
1045
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001046CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001047int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001048{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001049 struct cs_struct *handle;
1050 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001051 if (!ud)
1052 return -1;
1053
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001054 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001055
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001056 if (!handle->detail) {
1057 handle->errnum = CS_ERR_DETAIL;
1058 return -1;
1059 }
1060
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001061 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001062 handle->errnum = CS_ERR_SKIPDATA;
1063 return -1;
1064 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001065
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001066 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001067 handle->errnum = CS_ERR_DETAIL;
1068 return -1;
1069 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001070
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001071 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001072
1073 switch (handle->arch) {
1074 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001075 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001076 return -1;
1077 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001078 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001079 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001080 count++;
1081 break;
1082 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001083 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001084 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001085 count++;
1086 break;
1087 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001088 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001089 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001090 count++;
1091 break;
1092 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001093 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001094 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001095 count++;
1096 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001097 case CS_ARCH_PPC:
1098 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001099 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001100 count++;
1101 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001102 case CS_ARCH_SPARC:
1103 for (i = 0; i < insn->detail->sparc.op_count; i++)
1104 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1105 count++;
1106 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001107 case CS_ARCH_SYSZ:
1108 for (i = 0; i < insn->detail->sysz.op_count; i++)
1109 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1110 count++;
1111 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001112 case CS_ARCH_XCORE:
1113 for (i = 0; i < insn->detail->xcore.op_count; i++)
1114 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1115 count++;
1116 break;
Fotis Loukos0e7a2e72016-05-03 16:02:40 +03001117 case CS_ARCH_M68K:
1118 for (i = 0; i < insn->detail->m68k.op_count; i++)
1119 if (insn->detail->m68k.operands[i].type == (m68k_op_type)op_type)
1120 count++;
1121 break;
Fotis Loukos0850d552016-05-03 15:52:11 +03001122 case CS_ARCH_TMS320C64X:
1123 for (i = 0; i < insn->detail->tms320c64x.op_count; i++)
1124 if (insn->detail->tms320c64x.operands[i].type == (tms320c64x_op_type)op_type)
1125 count++;
1126 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001127 }
1128
1129 return count;
1130}
1131
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001132CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001133int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001134 unsigned int post)
1135{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001136 struct cs_struct *handle;
1137 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001138 if (!ud)
1139 return -1;
1140
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001141 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001142
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001143 if (!handle->detail) {
1144 handle->errnum = CS_ERR_DETAIL;
1145 return -1;
1146 }
1147
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001148 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001149 handle->errnum = CS_ERR_SKIPDATA;
1150 return -1;
1151 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001152
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001153 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001154 handle->errnum = CS_ERR_DETAIL;
1155 return -1;
1156 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001157
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001158 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001159
1160 switch (handle->arch) {
1161 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001162 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001163 return -1;
1164 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001165 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001166 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001167 count++;
1168 if (count == post)
1169 return i;
1170 }
1171 break;
1172 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001173 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001174 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001175 count++;
1176 if (count == post)
1177 return i;
1178 }
1179 break;
1180 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001181 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001182 if (insn->detail->x86.operands[i].type == (x86_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_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001189 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001190 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001191 count++;
1192 if (count == post)
1193 return i;
1194 }
1195 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001196 case CS_ARCH_PPC:
1197 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001198 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001199 count++;
1200 if (count == post)
1201 return i;
1202 }
1203 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001204 case CS_ARCH_SPARC:
1205 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1206 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1207 count++;
1208 if (count == post)
1209 return i;
1210 }
1211 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001212 case CS_ARCH_SYSZ:
1213 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1214 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1215 count++;
1216 if (count == post)
1217 return i;
1218 }
1219 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001220 case CS_ARCH_XCORE:
1221 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1222 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1223 count++;
1224 if (count == post)
1225 return i;
1226 }
1227 break;
Fotis Loukos0e7a2e72016-05-03 16:02:40 +03001228 case CS_ARCH_M68K:
1229 for (i = 0; i < insn->detail->m68k.op_count; i++) {
1230 if (insn->detail->m68k.operands[i].type == (m68k_op_type)op_type)
1231 count++;
1232 if (count == post)
1233 return i;
1234 }
1235 break;
Fotis Loukos0850d552016-05-03 15:52:11 +03001236 case CS_ARCH_TMS320C64X:
1237 for (i = 0; i < insn->detail->tms320c64x.op_count; i++) {
1238 if (insn->detail->tms320c64x.operands[i].type == (tms320c64x_op_type)op_type)
1239 count++;
1240 if (count == post)
1241 return i;
1242 }
1243 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001244 }
1245
1246 return -1;
1247}
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001248
1249CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001250cs_err CAPSTONE_API cs_regs_access(csh ud, const cs_insn *insn,
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001251 cs_regs regs_read, uint8_t *regs_read_count,
1252 cs_regs regs_write, uint8_t *regs_write_count)
1253{
1254 struct cs_struct *handle;
1255
1256 if (!ud)
1257 return -1;
1258
1259 handle = (struct cs_struct *)(uintptr_t)ud;
1260
1261#ifdef CAPSTONE_DIET
1262 // This API does not work in DIET mode
1263 handle->errnum = CS_ERR_DIET;
1264 return CS_ERR_DIET;
1265#else
1266 if (!handle->detail) {
1267 handle->errnum = CS_ERR_DETAIL;
1268 return CS_ERR_DETAIL;
1269 }
1270
1271 if (!insn->id) {
1272 handle->errnum = CS_ERR_SKIPDATA;
1273 return CS_ERR_SKIPDATA;
1274 }
1275
1276 if (!insn->detail) {
1277 handle->errnum = CS_ERR_DETAIL;
1278 return CS_ERR_DETAIL;
1279 }
1280
1281 if (handle->reg_access) {
1282 handle->reg_access(insn, regs_read, regs_read_count, regs_write, regs_write_count);
1283 } else {
1284 // this arch is unsupported yet
1285 handle->errnum = CS_ERR_ARCH;
1286 return CS_ERR_ARCH;
1287 }
1288
1289 return CS_ERR_OK;
1290#endif
1291}