blob: 449b1da54f021a09db26ecb8a8d12a68367462fc [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);
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +020060extern void M680X_enable(void);
Daniel Collin2ee675c2015-08-03 18:45:08 +020061extern void M68K_enable(void);
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080062extern void Mips_enable(void);
63extern void X86_enable(void);
64extern void PPC_enable(void);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080065extern void Sparc_enable(void);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080066extern void SystemZ_enable(void);
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080067extern void XCore_enable(void);
Fotis Loukos0850d552016-05-03 15:52:11 +030068extern void TMS320C64x_enable(void);
danghvu701b8502014-01-09 11:06:44 +070069
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080070static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080071{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080072 static bool initialized = false;
73
74 if (initialized)
75 return;
76
danghvub33bd2c2014-01-09 12:22:56 +070077#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080078 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070079#endif
80#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080081 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070082#endif
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +020083#ifdef CAPSTONE_HAS_M680X
84 M680X_enable();
85#endif
Daniel Collin2ee675c2015-08-03 18:45:08 +020086#ifdef CAPSTONE_HAS_M68K
87 M68K_enable();
88#endif
danghvub33bd2c2014-01-09 12:22:56 +070089#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080090 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070091#endif
danghvub33bd2c2014-01-09 12:22:56 +070092#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080093 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070094#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080095#ifdef CAPSTONE_HAS_SPARC
96 Sparc_enable();
97#endif
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080098#ifdef CAPSTONE_HAS_SYSZ
99 SystemZ_enable();
100#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800101#ifdef CAPSTONE_HAS_X86
102 X86_enable();
103#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800104#ifdef CAPSTONE_HAS_XCORE
105 XCore_enable();
106#endif
Fotis Loukos0850d552016-05-03 15:52:11 +0300107#ifdef CAPSTONE_HAS_TMS320C64X
108 TMS320C64x_enable();
109#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800110
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800111
112 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +0700113}
114
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800115unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +0800116
tandasat45e5eab2016-05-11 21:48:32 -0700117#if defined(CAPSTONE_USE_SYS_DYN_MEM)
118#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
tandasatc45f1db2016-05-11 22:52:34 -0700119// default
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800120cs_malloc_t cs_mem_malloc = malloc;
121cs_calloc_t cs_mem_calloc = calloc;
122cs_realloc_t cs_mem_realloc = realloc;
123cs_free_t cs_mem_free = free;
Koutheir Attouchi201a0a12016-04-12 09:25:37 +0200124#if defined(_WIN32_WCE)
125cs_vsnprintf_t cs_vsnprintf = _vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800126#else
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800127cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700128#endif // defined(_WIN32_WCE)
129
tandasat45e5eab2016-05-11 21:48:32 -0700130#elif defined(_KERNEL_MODE)
tandasatc45f1db2016-05-11 22:52:34 -0700131// Windows driver
tandasat45e5eab2016-05-11 21:48:32 -0700132cs_malloc_t cs_mem_malloc = cs_winkernel_malloc;
133cs_calloc_t cs_mem_calloc = cs_winkernel_calloc;
134cs_realloc_t cs_mem_realloc = cs_winkernel_realloc;
135cs_free_t cs_mem_free = cs_winkernel_free;
136cs_vsnprintf_t cs_vsnprintf = cs_winkernel_vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700137
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800138#else
tandasatc45f1db2016-05-11 22:52:34 -0700139// OSX kernel
Pb86ad3d62015-11-05 18:08:25 +0100140extern void* kern_os_malloc(size_t size);
141extern void kern_os_free(void* addr);
142extern void* kern_os_realloc(void* addr, size_t nsize);
143
Nguyen Anh Quynh197e4572015-11-10 22:56:53 +0800144static void* cs_kern_os_calloc(size_t num, size_t size)
145{
Pb86ad3d62015-11-05 18:08:25 +0100146 return kern_os_malloc(num * size); // malloc bzeroes the buffer
147}
148
149cs_malloc_t cs_mem_malloc = kern_os_malloc;
Pb2769c772015-11-06 14:44:55 +0100150cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
Pb86ad3d62015-11-05 18:08:25 +0100151cs_realloc_t cs_mem_realloc = kern_os_realloc;
152cs_free_t cs_mem_free = kern_os_free;
153cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700154
155#endif // !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
Pb86ad3d62015-11-05 18:08:25 +0100156#else
tandasatc45f1db2016-05-11 22:52:34 -0700157// User-defined
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800158cs_malloc_t cs_mem_malloc = NULL;
159cs_calloc_t cs_mem_calloc = NULL;
160cs_realloc_t cs_mem_realloc = NULL;
161cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800162cs_vsnprintf_t cs_vsnprintf = NULL;
tandasatc45f1db2016-05-11 22:52:34 -0700163
164#endif // defined(CAPSTONE_USE_SYS_DYN_MEM)
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800165
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800166CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700167unsigned int CAPSTONE_API cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800168{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800169 archs_enable();
170
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800171 if (major != NULL && minor != NULL) {
172 *major = CS_API_MAJOR;
173 *minor = CS_API_MINOR;
174 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800175
176 return (CS_API_MAJOR << 8) + CS_API_MINOR;
177}
178
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800179CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700180bool CAPSTONE_API cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800181{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800182 archs_enable();
183
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800184 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800185 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800186 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800187 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Fotis Loukos0850d552016-05-03 15:52:11 +0300188 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE) |
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +0200189 (1 << CS_ARCH_M68K) | (1 << CS_ARCH_TMS320C64X) |
190 (1 << CS_ARCH_M680X));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800191
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800192 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800193 return all_arch & (1 << query);
194
195 if (query == CS_SUPPORT_DIET) {
196#ifdef CAPSTONE_DIET
197 return true;
198#else
199 return false;
200#endif
201 }
202
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800203 if (query == CS_SUPPORT_X86_REDUCE) {
204#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800205 return true;
206#else
207 return false;
208#endif
209 }
210
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800211 // unsupported query
212 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800213}
214
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800215CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700216cs_err CAPSTONE_API cs_errno(csh handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800217{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800218 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800219 if (!handle)
220 return CS_ERR_CSH;
221
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100222 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800223
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800224 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800225}
226
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800227CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700228const char * CAPSTONE_API cs_strerror(cs_err code)
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800229{
230 switch(code) {
231 default:
232 return "Unknown error code";
233 case CS_ERR_OK:
234 return "OK (CS_ERR_OK)";
235 case CS_ERR_MEM:
236 return "Out of memory (CS_ERR_MEM)";
237 case CS_ERR_ARCH:
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800238 return "Invalid/unsupported architecture(CS_ERR_ARCH)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800239 case CS_ERR_HANDLE:
240 return "Invalid handle (CS_ERR_HANDLE)";
241 case CS_ERR_CSH:
242 return "Invalid csh (CS_ERR_CSH)";
243 case CS_ERR_MODE:
244 return "Invalid mode (CS_ERR_MODE)";
245 case CS_ERR_OPTION:
246 return "Invalid option (CS_ERR_OPTION)";
247 case CS_ERR_DETAIL:
248 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800249 case CS_ERR_MEMSETUP:
250 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800251 case CS_ERR_VERSION:
252 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800253 case CS_ERR_DIET:
254 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800255 case CS_ERR_SKIPDATA:
256 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh8aba4cd2015-08-09 10:52:18 -0700257 case CS_ERR_X86_ATT:
258 return "AT&T syntax is unavailable (CS_ERR_X86_ATT)";
259 case CS_ERR_X86_INTEL:
260 return "INTEL syntax is unavailable (CS_ERR_X86_INTEL)";
261 case CS_ERR_X86_MASM:
262 return "MASM syntax is unavailable (CS_ERR_X86_MASM)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800263 }
264}
265
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800266CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700267cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800268{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800269 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800270 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800271 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800272 // Error: before cs_open(), dynamic memory management must be initialized
273 // with cs_option(CS_OPT_MEM)
274 return CS_ERR_MEMSETUP;
275
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800276 archs_enable();
277
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800278 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800279 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800280 if (!ud) {
281 // memory insufficient
282 return CS_ERR_MEM;
283 }
284
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800285 ud->errnum = CS_ERR_OK;
286 ud->arch = arch;
287 ud->mode = mode;
tandasat45e5eab2016-05-11 21:48:32 -0700288 ud->big_endian = (mode & CS_MODE_BIG_ENDIAN) != 0;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800289 // by default, do not break instruction into details
290 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800291
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800292 // default skipdata setup
293 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
294
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100295 err = arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800296 if (err) {
297 cs_mem_free(ud);
298 *handle = 0;
299 return err;
300 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800301
302 *handle = (uintptr_t)ud;
303
304 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800305 } else {
306 *handle = 0;
307 return CS_ERR_ARCH;
308 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800309}
310
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800311CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700312cs_err CAPSTONE_API cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800313{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800314 struct cs_struct *ud;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800315 struct insn_mnem *next, *tmp;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800316
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800317 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800318 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800319 return CS_ERR_CSH;
320
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100321 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800322
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800323 if (ud->printer_info)
324 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800325
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800326 // free the linked list of customized mnemonic
327 tmp = ud->mnem_list;
328 while(tmp) {
329 next = tmp->next;
330 cs_mem_free(tmp);
331 tmp = next;
332 }
333
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800334 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800335
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800336 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800337 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800338
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800339 // invalidate this handle by ZERO out its value.
340 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800341 *handle = 0;
342
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800343 return CS_ERR_OK;
344}
345
346// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800347static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800348 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800349{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800350#ifndef CAPSTONE_DIET
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700351 char *sp, *mnem;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800352#endif
tandasat45e5eab2016-05-11 21:48:32 -0700353 uint16_t copy_size = MIN(sizeof(insn->bytes), insn->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800354
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800355 // fill the instruction bytes.
356 // we might skip some redundant bytes in front in the case of X86
357 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
358 insn->size = copy_size;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800359
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800360 // alias instruction might have ID saved in OpcodePub
361 if (MCInst_getOpcodePub(mci))
362 insn->id = MCInst_getOpcodePub(mci);
363
364 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800365 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800366 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800367
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800368#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800369 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800370 // find first space or tab
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800371 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800372 for (sp = buffer; *sp; sp++) {
373 if (*sp == ' '|| *sp == '\t')
374 break;
375 if (*sp == '|') // lock|rep prefix for x86
376 *sp = ' ';
377 // copy to @mnemonic
378 *mnem = *sp;
379 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800380 }
381
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800382 *mnem = '\0';
383
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800384 // we might have customized mnemonic
385 if (handle->mnem_list) {
386 struct insn_mnem *tmp = handle->mnem_list;
387 while(tmp) {
388 if (tmp->insn.id == insn->id) {
389 // found this instruction, so copy its mnemonic
390 (void)strncpy(insn->mnemonic, tmp->insn.mnemonic, sizeof(insn->mnemonic) - 1);
391 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
392 break;
393 }
394 tmp = tmp->next;
395 }
396 }
397
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800398 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800399 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800400 // find the next non-space char
401 sp++;
402 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
403 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800404 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
405 } else
406 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800407#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800408}
409
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800410// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800411// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800412static uint8_t skipdata_size(cs_struct *handle)
413{
414 switch(handle->arch) {
415 default:
416 // should never reach
tandasat45e5eab2016-05-11 21:48:32 -0700417 return (uint8_t)-1;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800418 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800419 // skip 2 bytes on Thumb mode.
420 if (handle->mode & CS_MODE_THUMB)
421 return 2;
422 // otherwise, skip 4 bytes
423 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800424 case CS_ARCH_ARM64:
425 case CS_ARCH_MIPS:
426 case CS_ARCH_PPC:
427 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800428 // skip 4 bytes
429 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800430 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800431 // SystemZ instruction's length can be 2, 4 or 6 bytes,
432 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800433 return 2;
434 case CS_ARCH_X86:
435 // X86 has no restriction on instruction alignment
436 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800437 case CS_ARCH_XCORE:
438 // XCore instruction's length can be 2 or 4 bytes,
439 // so we just skip 2 bytes
440 return 2;
Daniel Collin2ee675c2015-08-03 18:45:08 +0200441 case CS_ARCH_M68K:
442 // M68K has 2 bytes instruction alignment but contain multibyte instruction so we skip 2 bytes
443 return 2;
Fotis Loukos0850d552016-05-03 15:52:11 +0300444 case CS_ARCH_TMS320C64X:
445 // TMS320C64x alignment is 4.
446 return 4;
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +0200447 case CS_ARCH_M680X:
448 // M680X alignment is 1.
449 return 1;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800450 }
451}
452
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800453CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700454cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800455{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800456 struct cs_struct *handle;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800457 cs_opt_mnem *opt;
458
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800459 archs_enable();
460
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800461 // cs_option() can be called with NULL handle just for CS_OPT_MEM
462 // This is supposed to be executed before all other APIs (even cs_open())
463 if (type == CS_OPT_MEM) {
464 cs_opt_mem *mem = (cs_opt_mem *)value;
465
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800466 cs_mem_malloc = mem->malloc;
467 cs_mem_calloc = mem->calloc;
468 cs_mem_realloc = mem->realloc;
469 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800470 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800471
472 return CS_ERR_OK;
473 }
474
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100475 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600476 if (!handle)
477 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800478
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800479 switch(type) {
480 default:
481 break;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800482
Nguyen Anh Quynh256090a2016-03-14 13:52:23 +0800483 case CS_OPT_UNSIGNED:
484 handle->imm_unsigned = (cs_opt_value)value;
485 return CS_ERR_OK;
486
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800487 case CS_OPT_DETAIL:
Félix Cloutier3973d8b2015-03-02 22:06:56 -0500488 handle->detail = (cs_opt_value)value;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800489 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800490
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800491 case CS_OPT_SKIPDATA:
492 handle->skipdata = (value == CS_OPT_ON);
493 if (handle->skipdata) {
494 if (handle->skipdata_size == 0) {
495 // set the default skipdata size
496 handle->skipdata_size = skipdata_size(handle);
497 }
498 }
499 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800500
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800501 case CS_OPT_SKIPDATA_SETUP:
502 if (value)
503 handle->skipdata_setup = *((cs_opt_skipdata *)value);
504 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800505
506 case CS_OPT_MNEMONIC:
507 opt = (cs_opt_mnem *)value;
508 if (opt->id) {
509 if (opt->mnemonic) {
510 struct insn_mnem *tmp;
511
512 // add new instruction, or replace existing instruction
513 // 1. find if we already had this insn in the linked list
514 tmp = handle->mnem_list;
515 while(tmp) {
516 if (tmp->insn.id == opt->id) {
517 // found this instruction, so replace its mnemonic
518 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
519 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
520 break;
521 }
522 tmp = tmp->next;
523 }
524
525 // 2. add this instruction if we have not had it yet
526 if (!tmp) {
527 tmp = cs_mem_malloc(sizeof(*tmp));
528 tmp->insn.id = opt->id;
529 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
530 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
531 // this new instruction is heading the list
532 tmp->next = handle->mnem_list;
533 handle->mnem_list = tmp;
534 }
535 return CS_ERR_OK;
536 } else {
537 struct insn_mnem *prev, *tmp;
538
539 // we want to delete an existing instruction
540 // iterate the list to find the instruction to remove it
541 tmp = handle->mnem_list;
542 prev = tmp;
543 while(tmp) {
544 if (tmp->insn.id == opt->id) {
545 // delete this instruction
546 if (tmp == prev) {
547 // head of the list
548 handle->mnem_list = tmp->next;
549 } else {
550 prev->next = tmp->next;
551 }
552 cs_mem_free(tmp);
553 break;
554 }
555 prev = tmp;
556 tmp = tmp->next;
557 }
558 }
559 }
560 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800561 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800562
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800563 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800564}
565
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800566// generate @op_str for data instruction of SKIPDATA
vit9696a8c7e4a2017-05-25 11:01:48 +0300567#ifndef CAPSTONE_DIET
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800568static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
569{
570 char *p = opstr;
571 int len;
572 size_t i;
tandasat45e5eab2016-05-11 21:48:32 -0700573 size_t available = sizeof(((cs_insn*)NULL)->op_str);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800574
575 if (!size) {
576 opstr[0] = '\0';
577 return;
578 }
579
tandasat45e5eab2016-05-11 21:48:32 -0700580 len = cs_snprintf(p, available, "0x%02x", buffer[0]);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800581 p+= len;
tandasat45e5eab2016-05-11 21:48:32 -0700582 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800583
584 for(i = 1; i < size; i++) {
tandasat45e5eab2016-05-11 21:48:32 -0700585 len = cs_snprintf(p, available, ", 0x%02x", buffer[i]);
586 if (len < 0) {
587 break;
588 }
589 if ((size_t)len > available - 1) {
590 break;
591 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800592 p+= len;
tandasat45e5eab2016-05-11 21:48:32 -0700593 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800594 }
595}
vit9696a8c7e4a2017-05-25 11:01:48 +0300596#endif
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800597
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800598// dynamicly allocate memory to contain disasm insn
599// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800600CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700601size_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 +0800602{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800603 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800604 MCInst mci;
605 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800606 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800607 unsigned int f = 0; // index of the next instruction in the cache
608 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800609 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800610 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800611 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800612 void *tmp;
613 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800614 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800615 size_t size_org;
616 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800617 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500618 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800619
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800620 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800621 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800622 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800623 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800624 return 0;
625 }
626
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800627 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800628
Nguyen Anh Quynh56aa0ac2016-04-27 14:43:10 +0800629 // reset IT block of ARM structure
Nguyen Anh Quynh9d07c1c2016-04-27 14:47:05 +0800630 if (handle->arch == CS_ARCH_ARM)
631 handle->ITBlock.size = 0;
Nguyen Anh Quynh56aa0ac2016-04-27 14:43:10 +0800632
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800633#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800634 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400635 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800636#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800637
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800638 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800639 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800640 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800641 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800642
643 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700644 total = cs_mem_malloc(total_size);
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800645 if (total == NULL) {
646 // insufficient memory
Edward Williamsonf1e49752014-12-14 20:45:19 -0500647 handle->errnum = CS_ERR_MEM;
648 return 0;
649 }
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800650
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700651 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800652
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800653 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800654 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800655 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800656
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700657 // relative branches need to know the address & size of current insn
658 mci.address = offset;
659
660 if (handle->detail) {
661 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700662 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700663 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700664 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700665 }
666
667 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700668 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700669 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800670#ifdef CAPSTONE_DIET
671 // zero out mnemonic & op_str
672 mci.flat_insn->mnemonic[0] = '\0';
673 mci.flat_insn->op_str[0] = '\0';
674#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700675
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800676 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800677 if (r) {
678 SStream ss;
679 SStream_Init(&ss);
680
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700681 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800682
683 // map internal instruction opcode to public insn ID
684 handle->insn_id(handle, insn_cache, mci.Opcode);
685
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800686 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700687 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800688
Nguyen Anh Quynhdabc9f22016-07-15 20:37:19 +0800689 // adjust for pseudo opcode (X86)
690 if (handle->arch == CS_ARCH_X86)
691 insn_cache->id += mci.popcode_adjust;
692
danghvu0d1aad12014-10-01 23:12:18 -0500693 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800694 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800695 // encounter a broken instruction
696
697 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800698 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800699 cs_mem_free(insn_cache->detail);
700 }
701
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800702 // if there is no request to skip data, or remaining data is too small,
703 // then bail out
704 if (!handle->skipdata || handle->skipdata_size > size)
705 break;
706
707 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800708 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800709 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800710 if (skipdata_bytes > size)
711 // remaining data is not enough
712 break;
713
714 if (!skipdata_bytes)
715 // user requested not to skip data, so bail out
716 break;
717 } else
718 skipdata_bytes = handle->skipdata_size;
719
720 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700721 insn_cache->id = 0; // invalid ID for this "data" instruction
722 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800723 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700724 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
vit9696a8c7e4a2017-05-25 11:01:48 +0300725#ifdef CAPSTONE_DIET
726 insn_cache->mnemonic[0] = '\0';
727 insn_cache->op_str[0] = '\0';
728#else
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700729 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
730 sizeof(insn_cache->mnemonic) - 1);
731 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
vit9696a8c7e4a2017-05-25 11:01:48 +0300732#endif
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700733 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800734
danghvu0d1aad12014-10-01 23:12:18 -0500735 next_offset = skipdata_bytes;
736 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800737
danghvu0d1aad12014-10-01 23:12:18 -0500738 // one more instruction entering the cache
739 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800740
danghvu0d1aad12014-10-01 23:12:18 -0500741 // one more instruction disassembled
742 c++;
743 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800744 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500745 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800746
danghvu0d1aad12014-10-01 23:12:18 -0500747 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800748 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500749 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500750 total_size += (sizeof(cs_insn) * cache_size);
751 tmp = cs_mem_realloc(total, total_size);
752 if (tmp == NULL) { // insufficient memory
753 if (handle->detail) {
754 insn_cache = (cs_insn *)total;
755 for (i = 0; i < c; i++, insn_cache++)
756 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800757 }
758
danghvu0d1aad12014-10-01 23:12:18 -0500759 cs_mem_free(total);
760 *insn = NULL;
761 handle->errnum = CS_ERR_MEM;
762 return 0;
763 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800764
danghvu0d1aad12014-10-01 23:12:18 -0500765 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800766 // continue to fill in the cache after the last instruction
767 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800768
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800769 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500770 f = 0;
771 } else
772 insn_cache++;
773
774 buffer += next_offset;
775 size -= next_offset;
776 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800777 }
778
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800779 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800780 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800781 cs_mem_free(total);
782 total = NULL;
783 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800784 // total did not fully use the last cache, so downsize it
Tyler J. Stacheckid07a21b2015-10-23 20:59:20 -0400785 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800786 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800787 // free all detail pointers
788 if (handle->detail) {
789 insn_cache = (cs_insn *)total;
790 for (i = 0; i < c; i++, insn_cache++)
791 cs_mem_free(insn_cache->detail);
792 }
793
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800794 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800795 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800796
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800797 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800798 return 0;
799 }
800
801 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800802 }
803
804 *insn = total;
805
806 return c;
807}
808
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800809CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800810CAPSTONE_DEPRECATED
tandasat45e5eab2016-05-11 21:48:32 -0700811size_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 +0800812{
813 return cs_disasm(ud, buffer, size, offset, count, insn);
814}
815
816CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700817void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800818{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800819 size_t i;
820
821 // free all detail pointers
822 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800823 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800824
825 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800826 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800827}
828
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800829CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700830cs_insn * CAPSTONE_API cs_malloc(csh ud)
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800831{
832 cs_insn *insn;
833 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
834
835 insn = cs_mem_malloc(sizeof(cs_insn));
836 if (!insn) {
837 // insufficient memory
838 handle->errnum = CS_ERR_MEM;
839 return NULL;
840 } else {
841 if (handle->detail) {
842 // allocate memory for @detail pointer
843 insn->detail = cs_mem_malloc(sizeof(cs_detail));
844 if (insn->detail == NULL) { // insufficient memory
845 cs_mem_free(insn);
846 handle->errnum = CS_ERR_MEM;
847 return NULL;
848 }
849 } else
850 insn->detail = NULL;
851 }
852
853 return insn;
854}
855
hlide993f3622014-10-05 18:14:40 +0200856// iterator for instruction "single-stepping"
857CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700858bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800859 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200860{
861 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200862 uint16_t insn_size;
863 MCInst mci;
864 bool r;
865
866 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800867 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800868 return false;
hlide993f3622014-10-05 18:14:40 +0200869 }
870
871 handle->errnum = CS_ERR_OK;
872
hlide993f3622014-10-05 18:14:40 +0200873 MCInst_Init(&mci);
874 mci.csh = handle;
875
876 // relative branches need to know the address & size of current insn
877 mci.address = *address;
878
879 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800880 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200881 mci.flat_insn->address = *address;
882#ifdef CAPSTONE_DIET
883 // zero out mnemonic & op_str
884 mci.flat_insn->mnemonic[0] = '\0';
885 mci.flat_insn->op_str[0] = '\0';
886#endif
887
888 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800889 if (r) {
hlide993f3622014-10-05 18:14:40 +0200890 SStream ss;
891 SStream_Init(&ss);
892
893 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800894
895 // map internal instruction opcode to public insn ID
896 handle->insn_id(handle, insn, mci.Opcode);
897
hlide993f3622014-10-05 18:14:40 +0200898 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800899
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800900 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800901
Nguyen Anh Quynhdabc9f22016-07-15 20:37:19 +0800902 // adjust for pseudo opcode (X86)
903 if (handle->arch == CS_ARCH_X86)
904 insn->id += mci.popcode_adjust;
905
hlide993f3622014-10-05 18:14:40 +0200906 *code += insn_size;
907 *size -= insn_size;
908 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800909 } else { // encounter a broken instruction
910 size_t skipdata_bytes;
911
912 // if there is no request to skip data, or remaining data is too small,
913 // then bail out
914 if (!handle->skipdata || handle->skipdata_size > *size)
915 return false;
916
917 if (handle->skipdata_setup.callback) {
918 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
919 0, handle->skipdata_setup.user_data);
920 if (skipdata_bytes > *size)
921 // remaining data is not enough
922 return false;
923
924 if (!skipdata_bytes)
925 // user requested not to skip data, so bail out
926 return false;
927 } else
928 skipdata_bytes = handle->skipdata_size;
929
930 // we have to skip some amount of data, depending on arch & mode
931 insn->id = 0; // invalid ID for this "data" instruction
932 insn->address = *address;
933 insn->size = (uint16_t)skipdata_bytes;
vit9696a8c7e4a2017-05-25 11:01:48 +0300934#ifdef CAPSTONE_DIET
935 insn->mnemonic[0] = '\0';
936 insn->op_str[0] = '\0';
937#else
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800938 memcpy(insn->bytes, *code, skipdata_bytes);
939 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
940 sizeof(insn->mnemonic) - 1);
941 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
vit9696a8c7e4a2017-05-25 11:01:48 +0300942#endif
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800943
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800944 *code += skipdata_bytes;
945 *size -= skipdata_bytes;
946 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200947 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800948
949 return true;
hlide993f3622014-10-05 18:14:40 +0200950}
951
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800952// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800953CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700954const char * CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800955{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800956 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800957
958 if (!handle || handle->reg_name == NULL) {
959 return NULL;
960 }
961
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800962 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800963}
964
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800965CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700966const char * CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800967{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800968 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800969
970 if (!handle || handle->insn_name == NULL) {
971 return NULL;
972 }
973
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800974 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800975}
976
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800977CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700978const char * CAPSTONE_API cs_group_name(csh ud, unsigned int group)
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800979{
980 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
981
982 if (!handle || handle->group_name == NULL) {
983 return NULL;
984 }
985
986 return handle->group_name(ud, group);
987}
988
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800989CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -0700990bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800991{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800992 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800993 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800994 return false;
995
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100996 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200997
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800998 if (!handle->detail) {
999 handle->errnum = CS_ERR_DETAIL;
1000 return false;
1001 }
1002
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001003 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001004 handle->errnum = CS_ERR_SKIPDATA;
1005 return false;
1006 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001007
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001008 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001009 handle->errnum = CS_ERR_DETAIL;
1010 return false;
1011 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001012
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001013 return arr_exist8(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001014}
1015
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001016CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001017bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001018{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001019 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001020 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001021 return false;
1022
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001023 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001024
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001025 if (!handle->detail) {
1026 handle->errnum = CS_ERR_DETAIL;
1027 return false;
1028 }
1029
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001030 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001031 handle->errnum = CS_ERR_SKIPDATA;
1032 return false;
1033 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001034
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001035 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001036 handle->errnum = CS_ERR_DETAIL;
1037 return false;
1038 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001039
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001040 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001041}
1042
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001043CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001044bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001045{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001046 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001047 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001048 return false;
1049
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001050 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +02001051
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001052 if (!handle->detail) {
1053 handle->errnum = CS_ERR_DETAIL;
1054 return false;
1055 }
1056
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001057 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001058 handle->errnum = CS_ERR_SKIPDATA;
1059 return false;
1060 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001061
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001062 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001063 handle->errnum = CS_ERR_DETAIL;
1064 return false;
1065 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001066
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001067 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001068}
1069
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001070CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001071int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001072{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001073 struct cs_struct *handle;
1074 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001075 if (!ud)
1076 return -1;
1077
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001078 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001079
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001080 if (!handle->detail) {
1081 handle->errnum = CS_ERR_DETAIL;
1082 return -1;
1083 }
1084
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001085 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001086 handle->errnum = CS_ERR_SKIPDATA;
1087 return -1;
1088 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001089
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001090 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001091 handle->errnum = CS_ERR_DETAIL;
1092 return -1;
1093 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001094
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001095 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001096
1097 switch (handle->arch) {
1098 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001099 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001100 return -1;
1101 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001102 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001103 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001104 count++;
1105 break;
1106 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001107 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001108 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001109 count++;
1110 break;
1111 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001112 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001113 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001114 count++;
1115 break;
1116 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001117 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001118 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001119 count++;
1120 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001121 case CS_ARCH_PPC:
1122 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001123 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001124 count++;
1125 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001126 case CS_ARCH_SPARC:
1127 for (i = 0; i < insn->detail->sparc.op_count; i++)
1128 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1129 count++;
1130 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001131 case CS_ARCH_SYSZ:
1132 for (i = 0; i < insn->detail->sysz.op_count; i++)
1133 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1134 count++;
1135 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001136 case CS_ARCH_XCORE:
1137 for (i = 0; i < insn->detail->xcore.op_count; i++)
1138 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1139 count++;
1140 break;
Fotis Loukos0e7a2e72016-05-03 16:02:40 +03001141 case CS_ARCH_M68K:
1142 for (i = 0; i < insn->detail->m68k.op_count; i++)
1143 if (insn->detail->m68k.operands[i].type == (m68k_op_type)op_type)
1144 count++;
1145 break;
Fotis Loukos0850d552016-05-03 15:52:11 +03001146 case CS_ARCH_TMS320C64X:
1147 for (i = 0; i < insn->detail->tms320c64x.op_count; i++)
1148 if (insn->detail->tms320c64x.operands[i].type == (tms320c64x_op_type)op_type)
1149 count++;
1150 break;
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +02001151 case CS_ARCH_M680X:
1152 for (i = 0; i < insn->detail->m680x.op_count; i++)
1153 if (insn->detail->m680x.operands[i].type == (m680x_op_type)op_type)
1154 count++;
1155 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001156 }
1157
1158 return count;
1159}
1160
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001161CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001162int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001163 unsigned int post)
1164{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001165 struct cs_struct *handle;
1166 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001167 if (!ud)
1168 return -1;
1169
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001170 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001171
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001172 if (!handle->detail) {
1173 handle->errnum = CS_ERR_DETAIL;
1174 return -1;
1175 }
1176
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001177 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001178 handle->errnum = CS_ERR_SKIPDATA;
1179 return -1;
1180 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001181
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001182 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001183 handle->errnum = CS_ERR_DETAIL;
1184 return -1;
1185 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001186
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001187 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001188
1189 switch (handle->arch) {
1190 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001191 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001192 return -1;
1193 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001194 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001195 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001196 count++;
1197 if (count == post)
1198 return i;
1199 }
1200 break;
1201 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001202 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001203 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001204 count++;
1205 if (count == post)
1206 return i;
1207 }
1208 break;
1209 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001210 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001211 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001212 count++;
1213 if (count == post)
1214 return i;
1215 }
1216 break;
1217 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001218 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001219 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001220 count++;
1221 if (count == post)
1222 return i;
1223 }
1224 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001225 case CS_ARCH_PPC:
1226 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001227 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001228 count++;
1229 if (count == post)
1230 return i;
1231 }
1232 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001233 case CS_ARCH_SPARC:
1234 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1235 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1236 count++;
1237 if (count == post)
1238 return i;
1239 }
1240 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001241 case CS_ARCH_SYSZ:
1242 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1243 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1244 count++;
1245 if (count == post)
1246 return i;
1247 }
1248 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001249 case CS_ARCH_XCORE:
1250 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1251 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1252 count++;
1253 if (count == post)
1254 return i;
1255 }
1256 break;
Fotis Loukos0e7a2e72016-05-03 16:02:40 +03001257 case CS_ARCH_M68K:
1258 for (i = 0; i < insn->detail->m68k.op_count; i++) {
1259 if (insn->detail->m68k.operands[i].type == (m68k_op_type)op_type)
1260 count++;
1261 if (count == post)
1262 return i;
1263 }
1264 break;
Fotis Loukos0850d552016-05-03 15:52:11 +03001265 case CS_ARCH_TMS320C64X:
1266 for (i = 0; i < insn->detail->tms320c64x.op_count; i++) {
1267 if (insn->detail->tms320c64x.operands[i].type == (tms320c64x_op_type)op_type)
1268 count++;
1269 if (count == post)
1270 return i;
1271 }
1272 break;
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +02001273 case CS_ARCH_M680X:
1274 for (i = 0; i < insn->detail->m680x.op_count; i++) {
1275 if (insn->detail->m680x.operands[i].type == (m680x_op_type)op_type)
1276 count++;
1277 if (count == post)
1278 return i;
1279 }
1280 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001281 }
1282
1283 return -1;
1284}
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001285
1286CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001287cs_err CAPSTONE_API cs_regs_access(csh ud, const cs_insn *insn,
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001288 cs_regs regs_read, uint8_t *regs_read_count,
1289 cs_regs regs_write, uint8_t *regs_write_count)
1290{
1291 struct cs_struct *handle;
1292
1293 if (!ud)
1294 return -1;
1295
1296 handle = (struct cs_struct *)(uintptr_t)ud;
1297
1298#ifdef CAPSTONE_DIET
1299 // This API does not work in DIET mode
1300 handle->errnum = CS_ERR_DIET;
1301 return CS_ERR_DIET;
1302#else
1303 if (!handle->detail) {
1304 handle->errnum = CS_ERR_DETAIL;
1305 return CS_ERR_DETAIL;
1306 }
1307
1308 if (!insn->id) {
1309 handle->errnum = CS_ERR_SKIPDATA;
1310 return CS_ERR_SKIPDATA;
1311 }
1312
1313 if (!insn->detail) {
1314 handle->errnum = CS_ERR_DETAIL;
1315 return CS_ERR_DETAIL;
1316 }
1317
1318 if (handle->reg_access) {
1319 handle->reg_access(insn, regs_read, regs_read_count, regs_write, regs_write_count);
1320 } else {
1321 // this arch is unsupported yet
1322 handle->errnum = CS_ERR_ARCH;
1323 return CS_ERR_ARCH;
1324 }
1325
1326 return CS_ERR_OK;
1327#endif
1328}