blob: 3a5b1b374b21c47017d2de15f855bd231150ce6e [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 Tandaf177f922016-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
reverserbcf09f42015-04-09 18:28:19 +01007#if defined(CAPSTONE_HAS_OSXKERNEL)
vit969677231752018-06-15 00:12:26 +03008#include <Availability.h>
reverserbcf09f42015-04-09 18:28:19 +01009#include <libkern/libkern.h>
10#else
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +080011#include <stddef.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080012#include <stdio.h>
13#include <stdlib.h>
reverserbcf09f42015-04-09 18:28:19 +010014#endif
15
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080016#include <string.h>
pancake9c10ace2015-02-24 04:55:55 +010017#include <capstone/capstone.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080018
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080019#include "utils.h"
Nguyen Anh Quynhc7404072014-01-05 11:35:47 +080020#include "MCRegisterInfo.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080021
tandasat1dd89872016-04-23 15:51:24 -070022#if defined(_KERNEL_MODE)
23#include "windows\winkernel_mm.h"
24#endif
25
tandasat2729f3b2016-05-16 08:32:58 -070026// Issue #681: Windows kernel does not support formatting float point
27#if defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
tandasatf7fe6402016-05-16 20:32:36 -070028#if defined(CAPSTONE_HAS_ARM) || defined(CAPSTONE_HAS_ARM64) || defined(CAPSTONE_HAS_M68K)
tandasat2729f3b2016-05-16 08:32:58 -070029#define CAPSTONE_STR_INTERNAL(x) #x
30#define CAPSTONE_STR(x) CAPSTONE_STR_INTERNAL(x)
31#define CAPSTONE_MSVC_WRANING_PREFIX __FILE__ "("CAPSTONE_STR(__LINE__)") : warning message : "
32
33#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.")
34
35#undef CAPSTONE_MSVC_WRANING_PREFIX
36#undef CAPSTONE_STR
37#undef CAPSTONE_STR_INTERNAL
38#endif
39#endif // defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
40
tandasat1dd89872016-04-23 15:51:24 -070041#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(CAPSTONE_DIET) && !defined(_KERNEL_MODE)
Nguyen Anh Quynh472a4a42014-03-06 09:13:04 +080042#define INSN_CACHE_SIZE 32
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080043#else
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080044// reduce stack variable size for kernel/firmware
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080045#define INSN_CACHE_SIZE 8
46#endif
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +080047
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080048// default SKIPDATA mnemonic
vit96964a757e12017-05-24 08:07:11 +030049#ifndef CAPSTONE_DIET
Nguyen Anh Quynhc75a9092014-04-10 10:26:49 +080050#define SKIPDATA_MNEM ".byte"
vit96964a757e12017-05-24 08:07:11 +030051#else // No printing is available in diet mode
52#define SKIPDATA_MNEM NULL
53#endif
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080054
Travis Finkenauer853a2872018-06-21 02:52:35 -040055#include "arch/AArch64/AArch64Module.h"
56#include "arch/ARM/ARMModule.h"
Travis Finkenauer65da43d2018-06-24 09:05:04 -040057#include "arch/EVM/EVMModule.h"
58#include "arch/M680X/M680XModule.h"
59#include "arch/M68K/M68KModule.h"
Travis Finkenauer853a2872018-06-21 02:52:35 -040060#include "arch/Mips/MipsModule.h"
61#include "arch/PowerPC/PPCModule.h"
62#include "arch/Sparc/SparcModule.h"
63#include "arch/SystemZ/SystemZModule.h"
Travis Finkenauer65da43d2018-06-24 09:05:04 -040064#include "arch/TMS320C64x/TMS320C64xModule.h"
Travis Finkenauer853a2872018-06-21 02:52:35 -040065#include "arch/X86/X86Module.h"
66#include "arch/XCore/XCoreModule.h"
Sebastian Macke87221fa2018-12-02 21:39:41 +010067#include "arch/MOS65XX/MOS65XXModule.h"
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080068
Nguyen Anh Quynh0b874b22018-06-22 01:03:26 +080069// constructor initialization for all archs
70static cs_err (*cs_arch_init[MAX_ARCH])(cs_struct *) = {
danghvub33bd2c2014-01-09 12:22:56 +070071#ifdef CAPSTONE_HAS_ARM
Travis Finkenauer853a2872018-06-21 02:52:35 -040072 ARM_global_init,
73#else
74 NULL,
danghvub33bd2c2014-01-09 12:22:56 +070075#endif
76#ifdef CAPSTONE_HAS_ARM64
Travis Finkenauer853a2872018-06-21 02:52:35 -040077 AArch64_global_init,
78#else
79 NULL,
danghvub33bd2c2014-01-09 12:22:56 +070080#endif
81#ifdef CAPSTONE_HAS_MIPS
Travis Finkenauer853a2872018-06-21 02:52:35 -040082 Mips_global_init,
83#else
84 NULL,
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080085#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080086#ifdef CAPSTONE_HAS_X86
Travis Finkenauer853a2872018-06-21 02:52:35 -040087 X86_global_init,
88#else
89 NULL,
90#endif
91#ifdef CAPSTONE_HAS_POWERPC
92 PPC_global_init,
93#else
94 NULL,
95#endif
96#ifdef CAPSTONE_HAS_SPARC
97 Sparc_global_init,
98#else
99 NULL,
100#endif
101#ifdef CAPSTONE_HAS_SYSZ
102 SystemZ_global_init,
103#else
104 NULL,
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800105#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800106#ifdef CAPSTONE_HAS_XCORE
Travis Finkenauer853a2872018-06-21 02:52:35 -0400107 XCore_global_init,
108#else
109 NULL,
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800110#endif
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400111#ifdef CAPSTONE_HAS_M68K
112 M68K_global_init,
113#else
114 NULL,
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800115#endif
Fotis Loukos0850d552016-05-03 15:52:11 +0300116#ifdef CAPSTONE_HAS_TMS320C64X
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400117 TMS320C64x_global_init,
118#else
119 NULL,
120#endif
121#ifdef CAPSTONE_HAS_M680X
122 M680X_global_init,
123#else
124 NULL,
Fotis Loukos0850d552016-05-03 15:52:11 +0300125#endif
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +0800126#ifdef CAPSTONE_HAS_EVM
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400127 EVM_global_init,
128#else
129 NULL,
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +0800130#endif
Sebastian Macke87221fa2018-12-02 21:39:41 +0100131#ifdef CAPSTONE_HAS_MOS65XX
132 MOS65XX_global_init,
133#else
134 NULL,
135#endif
Travis Finkenauer853a2872018-06-21 02:52:35 -0400136};
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800137
Nguyen Anh Quynh0b874b22018-06-22 01:03:26 +0800138// support cs_option() for all archs
139static cs_err (*cs_arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = {
Travis Finkenauer853a2872018-06-21 02:52:35 -0400140#ifdef CAPSTONE_HAS_ARM
141 ARM_option,
142#else
143 NULL,
144#endif
145#ifdef CAPSTONE_HAS_ARM64
146 AArch64_option,
147#else
148 NULL,
149#endif
150#ifdef CAPSTONE_HAS_MIPS
151 Mips_option,
152#else
153 NULL,
154#endif
155#ifdef CAPSTONE_HAS_X86
156 X86_option,
157#else
158 NULL,
159#endif
160#ifdef CAPSTONE_HAS_POWERPC
161 PPC_option,
162#else
163 NULL,
164#endif
165#ifdef CAPSTONE_HAS_SPARC
166 Sparc_option,
167#else
168 NULL,
169#endif
170#ifdef CAPSTONE_HAS_SYSZ
171 SystemZ_option,
172#else
173 NULL,
174#endif
175#ifdef CAPSTONE_HAS_XCORE
176 XCore_option,
177#else
178 NULL,
179#endif
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400180#ifdef CAPSTONE_HAS_M68K
181 M68K_option,
182#else
183 NULL,
184#endif
185#ifdef CAPSTONE_HAS_TMS320C64X
186 TMS320C64x_option,
187#else
188 NULL,
189#endif
190#ifdef CAPSTONE_HAS_M680X
191 M680X_option,
192#else
193 NULL,
194#endif
195#ifdef CAPSTONE_HAS_EVM
196 EVM_option,
197#else
198 NULL,
199#endif
Sebastian Macke87221fa2018-12-02 21:39:41 +0100200#ifdef CAPSTONE_HAS_MOS65XX
201 MOS65XX_option,
202#else
203 NULL,
204#endif
205
Travis Finkenauer853a2872018-06-21 02:52:35 -0400206};
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800207
Nguyen Anh Quynh0b874b22018-06-22 01:03:26 +0800208// bitmask for finding disallowed modes for an arch:
209// to be called in cs_open()/cs_option()
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400210static cs_mode cs_arch_disallowed_mode_mask[MAX_ARCH] = {
Travis Finkenauer853a2872018-06-21 02:52:35 -0400211#ifdef CAPSTONE_HAS_ARM
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400212 ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_ARM | CS_MODE_V8 | CS_MODE_MCLASS
213 | CS_MODE_THUMB | CS_MODE_BIG_ENDIAN),
Travis Finkenauer853a2872018-06-21 02:52:35 -0400214#else
215 0,
216#endif
217#ifdef CAPSTONE_HAS_ARM64
218 ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_ARM | CS_MODE_BIG_ENDIAN),
219#else
220 0,
221#endif
222#ifdef CAPSTONE_HAS_MIPS
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400223 ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_32 | CS_MODE_64 | CS_MODE_MICRO
224 | CS_MODE_MIPS32R6 | CS_MODE_BIG_ENDIAN | CS_MODE_MIPS2 | CS_MODE_MIPS3),
Travis Finkenauer853a2872018-06-21 02:52:35 -0400225#else
226 0,
227#endif
228#ifdef CAPSTONE_HAS_X86
229 ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_32 | CS_MODE_64 | CS_MODE_16),
230#else
231 0,
232#endif
233#ifdef CAPSTONE_HAS_POWERPC
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400234 ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_32 | CS_MODE_64 | CS_MODE_BIG_ENDIAN
235 | CS_MODE_QPX),
Travis Finkenauer853a2872018-06-21 02:52:35 -0400236#else
237 0,
238#endif
239#ifdef CAPSTONE_HAS_SPARC
240 ~(CS_MODE_BIG_ENDIAN | CS_MODE_V9),
241#else
242 0,
243#endif
244#ifdef CAPSTONE_HAS_SYSZ
245 ~(CS_MODE_BIG_ENDIAN),
246#else
247 0,
248#endif
249#ifdef CAPSTONE_HAS_XCORE
250 ~(CS_MODE_BIG_ENDIAN),
251#else
252 0,
253#endif
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400254#ifdef CAPSTONE_HAS_M68K
255 ~(CS_MODE_BIG_ENDIAN | CS_MODE_M68K_000 | CS_MODE_M68K_010 | CS_MODE_M68K_020
256 | CS_MODE_M68K_030 | CS_MODE_M68K_040 | CS_MODE_M68K_060),
257#else
258 0,
259#endif
260#ifdef CAPSTONE_HAS_TMS320C64X
261 ~(CS_MODE_BIG_ENDIAN),
262#else
263 0,
264#endif
265#ifdef CAPSTONE_HAS_M680X
266 ~(CS_MODE_M680X_6301 | CS_MODE_M680X_6309 | CS_MODE_M680X_6800
267 | CS_MODE_M680X_6801 | CS_MODE_M680X_6805 | CS_MODE_M680X_6808
268 | CS_MODE_M680X_6809 | CS_MODE_M680X_6811 | CS_MODE_M680X_CPU12
269 | CS_MODE_M680X_HCS08),
270#else
271 0,
272#endif
273#ifdef CAPSTONE_HAS_EVM
274 0,
275#else
276 0,
277#endif
Sebastian Macke87221fa2018-12-02 21:39:41 +0100278#ifdef CAPSTONE_HAS_MOS65XX
279 ~(CS_MODE_BIG_ENDIAN),
280#else
281 0,
282#endif
Travis Finkenauer853a2872018-06-21 02:52:35 -0400283};
284
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400285// bitmask of enabled architectures
286static uint32_t all_arch = 0
Travis Finkenauer853a2872018-06-21 02:52:35 -0400287#ifdef CAPSTONE_HAS_ARM
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400288 | (1 << CS_ARCH_ARM)
Travis Finkenauer853a2872018-06-21 02:52:35 -0400289#endif
Travis Finkenauer853a2872018-06-21 02:52:35 -0400290#ifdef CAPSTONE_HAS_ARM64
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400291 | (1 << CS_ARCH_ARM64)
Travis Finkenauer853a2872018-06-21 02:52:35 -0400292#endif
Travis Finkenauer853a2872018-06-21 02:52:35 -0400293#ifdef CAPSTONE_HAS_MIPS
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400294 | (1 << CS_ARCH_MIPS)
Travis Finkenauer853a2872018-06-21 02:52:35 -0400295#endif
Travis Finkenauer853a2872018-06-21 02:52:35 -0400296#ifdef CAPSTONE_HAS_X86
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400297 | (1 << CS_ARCH_X86)
Travis Finkenauer853a2872018-06-21 02:52:35 -0400298#endif
Travis Finkenauer853a2872018-06-21 02:52:35 -0400299#ifdef CAPSTONE_HAS_POWERPC
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400300 | (1 << CS_ARCH_PPC)
Travis Finkenauer853a2872018-06-21 02:52:35 -0400301#endif
Travis Finkenauer853a2872018-06-21 02:52:35 -0400302#ifdef CAPSTONE_HAS_SPARC
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400303 | (1 << CS_ARCH_SPARC)
Travis Finkenauer853a2872018-06-21 02:52:35 -0400304#endif
Travis Finkenauer853a2872018-06-21 02:52:35 -0400305#ifdef CAPSTONE_HAS_SYSZ
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400306 | (1 << CS_ARCH_SYSZ)
Travis Finkenauer853a2872018-06-21 02:52:35 -0400307#endif
Travis Finkenauer853a2872018-06-21 02:52:35 -0400308#ifdef CAPSTONE_HAS_XCORE
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400309 | (1 << CS_ARCH_XCORE)
310#endif
311#ifdef CAPSTONE_HAS_M68K
312 | (1 << CS_ARCH_M68K)
313#endif
314#ifdef CAPSTONE_HAS_TMS320C64X
315 | (1 << CS_ARCH_TMS320C64X)
316#endif
317#ifdef CAPSTONE_HAS_M680X
318 | (1 << CS_ARCH_M680X)
319#endif
320#ifdef CAPSTONE_HAS_EVM
321 | (1 << CS_ARCH_EVM)
Travis Finkenauer853a2872018-06-21 02:52:35 -0400322#endif
Sebastian Macke87221fa2018-12-02 21:39:41 +0100323#ifdef CAPSTONE_HAS_MOS65XX
324 | (1 << CS_ARCH_MOS65XX)
325#endif
Travis Finkenauer853a2872018-06-21 02:52:35 -0400326;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +0800327
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800328
tandasat1dd89872016-04-23 15:51:24 -0700329#if defined(CAPSTONE_USE_SYS_DYN_MEM)
330#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
tandasatc45f1db2016-05-11 22:52:34 -0700331// default
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800332cs_malloc_t cs_mem_malloc = malloc;
333cs_calloc_t cs_mem_calloc = calloc;
334cs_realloc_t cs_mem_realloc = realloc;
335cs_free_t cs_mem_free = free;
Koutheir Attouchi201a0a12016-04-12 09:25:37 +0200336#if defined(_WIN32_WCE)
337cs_vsnprintf_t cs_vsnprintf = _vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800338#else
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800339cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700340#endif // defined(_WIN32_WCE)
341
tandasat1dd89872016-04-23 15:51:24 -0700342#elif defined(_KERNEL_MODE)
tandasatc45f1db2016-05-11 22:52:34 -0700343// Windows driver
tandasat1dd89872016-04-23 15:51:24 -0700344cs_malloc_t cs_mem_malloc = cs_winkernel_malloc;
345cs_calloc_t cs_mem_calloc = cs_winkernel_calloc;
346cs_realloc_t cs_mem_realloc = cs_winkernel_realloc;
347cs_free_t cs_mem_free = cs_winkernel_free;
348cs_vsnprintf_t cs_vsnprintf = cs_winkernel_vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800349#else
tandasatc45f1db2016-05-11 22:52:34 -0700350// OSX kernel
Pbb8741bd2015-11-10 23:02:26 +0100351extern void* kern_os_malloc(size_t size);
352extern void kern_os_free(void* addr);
353extern void* kern_os_realloc(void* addr, size_t nsize);
354
355static void* cs_kern_os_calloc(size_t num, size_t size)
356{
357 return kern_os_malloc(num * size); // malloc bzeroes the buffer
358}
359
360cs_malloc_t cs_mem_malloc = kern_os_malloc;
361cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
362cs_realloc_t cs_mem_realloc = kern_os_realloc;
363cs_free_t cs_mem_free = kern_os_free;
364cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasatc45f1db2016-05-11 22:52:34 -0700365#endif // !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
Pbb8741bd2015-11-10 23:02:26 +0100366#else
tandasatc45f1db2016-05-11 22:52:34 -0700367// User-defined
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800368cs_malloc_t cs_mem_malloc = NULL;
369cs_calloc_t cs_mem_calloc = NULL;
370cs_realloc_t cs_mem_realloc = NULL;
371cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800372cs_vsnprintf_t cs_vsnprintf = NULL;
tandasatc45f1db2016-05-11 22:52:34 -0700373
374#endif // defined(CAPSTONE_USE_SYS_DYN_MEM)
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800375
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800376CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700377unsigned int CAPSTONE_API cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800378{
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800379 if (major != NULL && minor != NULL) {
380 *major = CS_API_MAJOR;
381 *minor = CS_API_MINOR;
382 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800383
384 return (CS_API_MAJOR << 8) + CS_API_MINOR;
385}
386
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800387CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700388bool CAPSTONE_API cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800389{
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800390 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800391 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800392 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800393 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Fotis Loukos0850d552016-05-03 15:52:11 +0300394 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE) |
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +0200395 (1 << CS_ARCH_M68K) | (1 << CS_ARCH_TMS320C64X) |
Sebastian Macke87221fa2018-12-02 21:39:41 +0100396 (1 << CS_ARCH_M680X) | (1 << CS_ARCH_EVM) |
397 (1 << CS_ARCH_MOS65XX));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800398
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800399 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800400 return all_arch & (1 << query);
401
402 if (query == CS_SUPPORT_DIET) {
403#ifdef CAPSTONE_DIET
404 return true;
405#else
406 return false;
407#endif
408 }
409
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800410 if (query == CS_SUPPORT_X86_REDUCE) {
411#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800412 return true;
413#else
414 return false;
415#endif
416 }
417
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800418 // unsupported query
419 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800420}
421
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800422CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700423cs_err CAPSTONE_API cs_errno(csh handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800424{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800425 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800426 if (!handle)
427 return CS_ERR_CSH;
428
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100429 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800430
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800431 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800432}
433
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800434CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700435const char * CAPSTONE_API cs_strerror(cs_err code)
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800436{
437 switch(code) {
438 default:
439 return "Unknown error code";
440 case CS_ERR_OK:
441 return "OK (CS_ERR_OK)";
442 case CS_ERR_MEM:
443 return "Out of memory (CS_ERR_MEM)";
444 case CS_ERR_ARCH:
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800445 return "Invalid/unsupported architecture(CS_ERR_ARCH)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800446 case CS_ERR_HANDLE:
447 return "Invalid handle (CS_ERR_HANDLE)";
448 case CS_ERR_CSH:
449 return "Invalid csh (CS_ERR_CSH)";
450 case CS_ERR_MODE:
451 return "Invalid mode (CS_ERR_MODE)";
452 case CS_ERR_OPTION:
453 return "Invalid option (CS_ERR_OPTION)";
454 case CS_ERR_DETAIL:
455 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800456 case CS_ERR_MEMSETUP:
457 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800458 case CS_ERR_VERSION:
459 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800460 case CS_ERR_DIET:
461 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800462 case CS_ERR_SKIPDATA:
463 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh8aba4cd2015-08-09 10:52:18 -0700464 case CS_ERR_X86_ATT:
465 return "AT&T syntax is unavailable (CS_ERR_X86_ATT)";
466 case CS_ERR_X86_INTEL:
467 return "INTEL syntax is unavailable (CS_ERR_X86_INTEL)";
468 case CS_ERR_X86_MASM:
469 return "MASM syntax is unavailable (CS_ERR_X86_MASM)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800470 }
471}
472
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800473CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700474cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800475{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800476 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800477 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800478 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800479 // Error: before cs_open(), dynamic memory management must be initialized
480 // with cs_option(CS_OPT_MEM)
481 return CS_ERR_MEMSETUP;
482
Alberto Garcia Illera5f173b02018-03-19 15:23:09 +0100483 if (arch < CS_ARCH_MAX && cs_arch_init[arch]) {
Travis Finkenauer8998a3a2017-10-20 08:33:24 -0700484 // verify if requested mode is valid
Alberto Garcia Illera5f173b02018-03-19 15:23:09 +0100485 if (mode & cs_arch_disallowed_mode_mask[arch]) {
Travis Finkenauer8998a3a2017-10-20 08:33:24 -0700486 *handle = 0;
487 return CS_ERR_MODE;
488 }
489
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800490 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800491 if (!ud) {
492 // memory insufficient
493 return CS_ERR_MEM;
494 }
495
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800496 ud->errnum = CS_ERR_OK;
497 ud->arch = arch;
498 ud->mode = mode;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800499 // by default, do not break instruction into details
500 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800501
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800502 // default skipdata setup
503 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
504
Alberto Garcia Illera5f173b02018-03-19 15:23:09 +0100505 err = cs_arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800506 if (err) {
507 cs_mem_free(ud);
508 *handle = 0;
509 return err;
510 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800511
512 *handle = (uintptr_t)ud;
513
514 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800515 } else {
516 *handle = 0;
517 return CS_ERR_ARCH;
518 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800519}
520
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800521CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700522cs_err CAPSTONE_API cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800523{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800524 struct cs_struct *ud;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800525 struct insn_mnem *next, *tmp;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800526
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800527 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800528 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800529 return CS_ERR_CSH;
530
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100531 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800532
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800533 if (ud->printer_info)
534 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800535
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800536 // free the linked list of customized mnemonic
537 tmp = ud->mnem_list;
538 while(tmp) {
539 next = tmp->next;
540 cs_mem_free(tmp);
541 tmp = next;
542 }
543
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800544 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800545
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800546 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800547 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800548
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800549 // invalidate this handle by ZERO out its value.
550 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800551 *handle = 0;
552
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800553 return CS_ERR_OK;
554}
555
556// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800557static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800558 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800559{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800560#ifndef CAPSTONE_DIET
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700561 char *sp, *mnem;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800562#endif
tandasata6609fe2016-05-04 05:54:28 -0700563 uint16_t copy_size = MIN(sizeof(insn->bytes), insn->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800564
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800565 // fill the instruction bytes.
566 // we might skip some redundant bytes in front in the case of X86
567 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
568 insn->size = copy_size;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800569
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800570 // alias instruction might have ID saved in OpcodePub
571 if (MCInst_getOpcodePub(mci))
572 insn->id = MCInst_getOpcodePub(mci);
573
574 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800575 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800576 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800577
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800578#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800579 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800580 // find first space or tab
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800581 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800582 for (sp = buffer; *sp; sp++) {
583 if (*sp == ' '|| *sp == '\t')
584 break;
585 if (*sp == '|') // lock|rep prefix for x86
586 *sp = ' ';
587 // copy to @mnemonic
588 *mnem = *sp;
589 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800590 }
591
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800592 *mnem = '\0';
593
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800594 // we might have customized mnemonic
595 if (handle->mnem_list) {
596 struct insn_mnem *tmp = handle->mnem_list;
597 while(tmp) {
598 if (tmp->insn.id == insn->id) {
599 // found this instruction, so copy its mnemonic
600 (void)strncpy(insn->mnemonic, tmp->insn.mnemonic, sizeof(insn->mnemonic) - 1);
601 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
602 break;
603 }
604 tmp = tmp->next;
605 }
606 }
607
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800608 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800609 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800610 // find the next non-space char
611 sp++;
612 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
613 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800614 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
615 } else
616 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800617#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800618}
619
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800620// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800621// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800622static uint8_t skipdata_size(cs_struct *handle)
623{
624 switch(handle->arch) {
625 default:
626 // should never reach
tandasat59df5502016-05-14 16:04:28 -0700627 return (uint8_t)-1;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800628 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800629 // skip 2 bytes on Thumb mode.
630 if (handle->mode & CS_MODE_THUMB)
631 return 2;
632 // otherwise, skip 4 bytes
633 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800634 case CS_ARCH_ARM64:
635 case CS_ARCH_MIPS:
636 case CS_ARCH_PPC:
637 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800638 // skip 4 bytes
639 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800640 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800641 // SystemZ instruction's length can be 2, 4 or 6 bytes,
642 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800643 return 2;
644 case CS_ARCH_X86:
645 // X86 has no restriction on instruction alignment
646 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800647 case CS_ARCH_XCORE:
648 // XCore instruction's length can be 2 or 4 bytes,
649 // so we just skip 2 bytes
650 return 2;
Daniel Collin2ee675c2015-08-03 18:45:08 +0200651 case CS_ARCH_M68K:
652 // M68K has 2 bytes instruction alignment but contain multibyte instruction so we skip 2 bytes
653 return 2;
Fotis Loukos0850d552016-05-03 15:52:11 +0300654 case CS_ARCH_TMS320C64X:
655 // TMS320C64x alignment is 4.
656 return 4;
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +0200657 case CS_ARCH_M680X:
658 // M680X alignment is 1.
659 return 1;
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +0800660 case CS_ARCH_EVM:
661 // EVM alignment is 1.
662 return 1;
Sebastian Macke87221fa2018-12-02 21:39:41 +0100663 case CS_ARCH_MOS65XX:
664 // MOS65XX alignment is 1.
665 return 1;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800666 }
667}
668
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800669CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700670cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800671{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800672 struct cs_struct *handle;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800673 cs_opt_mnem *opt;
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800674
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800675 // cs_option() can be called with NULL handle just for CS_OPT_MEM
676 // This is supposed to be executed before all other APIs (even cs_open())
677 if (type == CS_OPT_MEM) {
678 cs_opt_mem *mem = (cs_opt_mem *)value;
679
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800680 cs_mem_malloc = mem->malloc;
681 cs_mem_calloc = mem->calloc;
682 cs_mem_realloc = mem->realloc;
683 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800684 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800685
686 return CS_ERR_OK;
687 }
688
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100689 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600690 if (!handle)
691 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800692
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800693 switch(type) {
694 default:
695 break;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800696
Nguyen Anh Quynh256090a2016-03-14 13:52:23 +0800697 case CS_OPT_UNSIGNED:
698 handle->imm_unsigned = (cs_opt_value)value;
699 return CS_ERR_OK;
700
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800701 case CS_OPT_DETAIL:
Félix Cloutierc141af92015-03-02 22:06:56 -0500702 handle->detail = (cs_opt_value)value;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800703 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800704
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800705 case CS_OPT_SKIPDATA:
706 handle->skipdata = (value == CS_OPT_ON);
707 if (handle->skipdata) {
708 if (handle->skipdata_size == 0) {
709 // set the default skipdata size
710 handle->skipdata_size = skipdata_size(handle);
711 }
712 }
713 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800714
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800715 case CS_OPT_SKIPDATA_SETUP:
716 if (value)
717 handle->skipdata_setup = *((cs_opt_skipdata *)value);
718 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800719
720 case CS_OPT_MNEMONIC:
721 opt = (cs_opt_mnem *)value;
722 if (opt->id) {
723 if (opt->mnemonic) {
724 struct insn_mnem *tmp;
725
726 // add new instruction, or replace existing instruction
727 // 1. find if we already had this insn in the linked list
728 tmp = handle->mnem_list;
729 while(tmp) {
730 if (tmp->insn.id == opt->id) {
731 // found this instruction, so replace its mnemonic
732 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
733 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
734 break;
735 }
736 tmp = tmp->next;
737 }
738
739 // 2. add this instruction if we have not had it yet
740 if (!tmp) {
741 tmp = cs_mem_malloc(sizeof(*tmp));
742 tmp->insn.id = opt->id;
743 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
744 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
745 // this new instruction is heading the list
746 tmp->next = handle->mnem_list;
747 handle->mnem_list = tmp;
748 }
749 return CS_ERR_OK;
750 } else {
751 struct insn_mnem *prev, *tmp;
752
753 // we want to delete an existing instruction
754 // iterate the list to find the instruction to remove it
755 tmp = handle->mnem_list;
756 prev = tmp;
757 while(tmp) {
758 if (tmp->insn.id == opt->id) {
759 // delete this instruction
760 if (tmp == prev) {
761 // head of the list
762 handle->mnem_list = tmp->next;
763 } else {
764 prev->next = tmp->next;
765 }
766 cs_mem_free(tmp);
767 break;
768 }
769 prev = tmp;
770 tmp = tmp->next;
771 }
772 }
773 }
774 return CS_ERR_OK;
Travis Finkenauer65da43d2018-06-24 09:05:04 -0400775
Travis Finkenauer8998a3a2017-10-20 08:33:24 -0700776 case CS_OPT_MODE:
777 // verify if requested mode is valid
Alberto Garcia Illera5f173b02018-03-19 15:23:09 +0100778 if (value & cs_arch_disallowed_mode_mask[handle->arch]) {
Travis Finkenauer8998a3a2017-10-20 08:33:24 -0700779 return CS_ERR_OPTION;
780 }
781 break;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800782 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800783
Alberto Garcia Illera5f173b02018-03-19 15:23:09 +0100784 return cs_arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800785}
786
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800787// generate @op_str for data instruction of SKIPDATA
vit96964a757e12017-05-24 08:07:11 +0300788#ifndef CAPSTONE_DIET
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800789static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
790{
791 char *p = opstr;
792 int len;
793 size_t i;
tandasat760940f2016-03-31 18:14:43 -0700794 size_t available = sizeof(((cs_insn*)NULL)->op_str);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800795
796 if (!size) {
797 opstr[0] = '\0';
798 return;
799 }
800
tandasat760940f2016-03-31 18:14:43 -0700801 len = cs_snprintf(p, available, "0x%02x", buffer[0]);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800802 p+= len;
tandasat760940f2016-03-31 18:14:43 -0700803 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800804
805 for(i = 1; i < size; i++) {
tandasat760940f2016-03-31 18:14:43 -0700806 len = cs_snprintf(p, available, ", 0x%02x", buffer[i]);
807 if (len < 0) {
808 break;
809 }
810 if ((size_t)len > available - 1) {
811 break;
812 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800813 p+= len;
tandasat760940f2016-03-31 18:14:43 -0700814 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800815 }
816}
vit96964a757e12017-05-24 08:07:11 +0300817#endif
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800818
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800819// dynamicly allocate memory to contain disasm insn
820// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800821CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700822size_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 +0800823{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800824 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800825 MCInst mci;
826 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800827 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800828 unsigned int f = 0; // index of the next instruction in the cache
829 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800830 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800831 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800832 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800833 void *tmp;
834 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800835 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800836 size_t size_org;
837 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800838 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500839 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800840
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800841 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800842 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800843 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800844 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800845 return 0;
846 }
847
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800848 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800849
Nguyen Anh Quynhc2cb0e72016-04-27 14:43:10 +0800850 // reset IT block of ARM structure
Nguyen Anh Quynh0c864402016-04-27 14:47:05 +0800851 if (handle->arch == CS_ARCH_ARM)
852 handle->ITBlock.size = 0;
Nguyen Anh Quynhc2cb0e72016-04-27 14:43:10 +0800853
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800854#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800855 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400856 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800857#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800858
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800859 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800860 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800861 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800862 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800863
864 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700865 total = cs_mem_malloc(total_size);
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800866 if (total == NULL) {
867 // insufficient memory
Edward Williamsonf1e49752014-12-14 20:45:19 -0500868 handle->errnum = CS_ERR_MEM;
869 return 0;
870 }
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800871
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700872 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800873
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800874 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800875 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800876 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800877
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700878 // relative branches need to know the address & size of current insn
879 mci.address = offset;
880
881 if (handle->detail) {
882 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700883 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700884 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700885 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700886 }
887
888 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700889 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700890 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800891#ifdef CAPSTONE_DIET
892 // zero out mnemonic & op_str
893 mci.flat_insn->mnemonic[0] = '\0';
894 mci.flat_insn->op_str[0] = '\0';
895#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700896
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800897 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800898 if (r) {
899 SStream ss;
900 SStream_Init(&ss);
901
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700902 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800903
904 // map internal instruction opcode to public insn ID
vit96964a757e12017-05-24 08:07:11 +0300905
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800906 handle->insn_id(handle, insn_cache, mci.Opcode);
907
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800908 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700909 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800910
Nguyen Anh Quynhdabc9f22016-07-15 20:37:19 +0800911 // adjust for pseudo opcode (X86)
912 if (handle->arch == CS_ARCH_X86)
913 insn_cache->id += mci.popcode_adjust;
914
danghvu0d1aad12014-10-01 23:12:18 -0500915 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800916 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800917 // encounter a broken instruction
918
919 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800920 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800921 cs_mem_free(insn_cache->detail);
922 }
923
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800924 // if there is no request to skip data, or remaining data is too small,
925 // then bail out
926 if (!handle->skipdata || handle->skipdata_size > size)
927 break;
928
929 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800930 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800931 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800932 if (skipdata_bytes > size)
933 // remaining data is not enough
934 break;
935
936 if (!skipdata_bytes)
937 // user requested not to skip data, so bail out
938 break;
939 } else
940 skipdata_bytes = handle->skipdata_size;
941
942 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700943 insn_cache->id = 0; // invalid ID for this "data" instruction
944 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800945 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700946 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
vit96964a757e12017-05-24 08:07:11 +0300947#ifdef CAPSTONE_DIET
vit9696a8c7e4a2017-05-25 11:01:48 +0300948 insn_cache->mnemonic[0] = '\0';
vit96964a757e12017-05-24 08:07:11 +0300949 insn_cache->op_str[0] = '\0';
950#else
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700951 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
952 sizeof(insn_cache->mnemonic) - 1);
953 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
vit96964a757e12017-05-24 08:07:11 +0300954#endif
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700955 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800956
danghvu0d1aad12014-10-01 23:12:18 -0500957 next_offset = skipdata_bytes;
958 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800959
danghvu0d1aad12014-10-01 23:12:18 -0500960 // one more instruction entering the cache
961 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800962
danghvu0d1aad12014-10-01 23:12:18 -0500963 // one more instruction disassembled
964 c++;
965 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800966 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500967 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800968
danghvu0d1aad12014-10-01 23:12:18 -0500969 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800970 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500971 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500972 total_size += (sizeof(cs_insn) * cache_size);
973 tmp = cs_mem_realloc(total, total_size);
974 if (tmp == NULL) { // insufficient memory
975 if (handle->detail) {
976 insn_cache = (cs_insn *)total;
977 for (i = 0; i < c; i++, insn_cache++)
978 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800979 }
980
danghvu0d1aad12014-10-01 23:12:18 -0500981 cs_mem_free(total);
982 *insn = NULL;
983 handle->errnum = CS_ERR_MEM;
984 return 0;
985 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800986
danghvu0d1aad12014-10-01 23:12:18 -0500987 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800988 // continue to fill in the cache after the last instruction
989 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800990
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800991 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500992 f = 0;
993 } else
994 insn_cache++;
995
996 buffer += next_offset;
997 size -= next_offset;
998 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800999 }
1000
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +08001001 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +08001002 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +08001003 cs_mem_free(total);
1004 total = NULL;
1005 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +08001006 // total did not fully use the last cache, so downsize it
Tyler J. Stachecki30d11672015-10-23 20:59:20 -04001007 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001008 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +08001009 // free all detail pointers
1010 if (handle->detail) {
1011 insn_cache = (cs_insn *)total;
1012 for (i = 0; i < c; i++, insn_cache++)
1013 cs_mem_free(insn_cache->detail);
1014 }
1015
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +08001016 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +08001017 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +08001018
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001019 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001020 return 0;
1021 }
1022
1023 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001024 }
1025
1026 *insn = total;
1027
1028 return c;
1029}
1030
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001031CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +08001032CAPSTONE_DEPRECATED
tandasat760940f2016-03-31 18:14:43 -07001033size_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 +08001034{
1035 return cs_disasm(ud, buffer, size, offset, count, insn);
1036}
1037
1038CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -07001039void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001040{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001041 size_t i;
1042
1043 // free all detail pointers
1044 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +08001045 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001046
1047 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +08001048 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001049}
1050
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001051CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -07001052cs_insn * CAPSTONE_API cs_malloc(csh ud)
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001053{
1054 cs_insn *insn;
1055 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1056
1057 insn = cs_mem_malloc(sizeof(cs_insn));
1058 if (!insn) {
1059 // insufficient memory
1060 handle->errnum = CS_ERR_MEM;
1061 return NULL;
1062 } else {
1063 if (handle->detail) {
1064 // allocate memory for @detail pointer
1065 insn->detail = cs_mem_malloc(sizeof(cs_detail));
1066 if (insn->detail == NULL) { // insufficient memory
1067 cs_mem_free(insn);
1068 handle->errnum = CS_ERR_MEM;
1069 return NULL;
1070 }
1071 } else
1072 insn->detail = NULL;
1073 }
1074
1075 return insn;
1076}
1077
hlide993f3622014-10-05 18:14:40 +02001078// iterator for instruction "single-stepping"
1079CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -07001080bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001081 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +02001082{
1083 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +02001084 uint16_t insn_size;
1085 MCInst mci;
1086 bool r;
1087
1088 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001089 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +08001090 return false;
hlide993f3622014-10-05 18:14:40 +02001091 }
1092
1093 handle->errnum = CS_ERR_OK;
1094
hlide993f3622014-10-05 18:14:40 +02001095 MCInst_Init(&mci);
1096 mci.csh = handle;
1097
1098 // relative branches need to know the address & size of current insn
1099 mci.address = *address;
1100
1101 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001102 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +02001103 mci.flat_insn->address = *address;
1104#ifdef CAPSTONE_DIET
1105 // zero out mnemonic & op_str
1106 mci.flat_insn->mnemonic[0] = '\0';
1107 mci.flat_insn->op_str[0] = '\0';
1108#endif
1109
1110 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001111 if (r) {
hlide993f3622014-10-05 18:14:40 +02001112 SStream ss;
1113 SStream_Init(&ss);
1114
1115 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +08001116
1117 // map internal instruction opcode to public insn ID
1118 handle->insn_id(handle, insn, mci.Opcode);
1119
hlide993f3622014-10-05 18:14:40 +02001120 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +08001121
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001122 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +08001123
Nguyen Anh Quynhdabc9f22016-07-15 20:37:19 +08001124 // adjust for pseudo opcode (X86)
1125 if (handle->arch == CS_ARCH_X86)
1126 insn->id += mci.popcode_adjust;
1127
hlide993f3622014-10-05 18:14:40 +02001128 *code += insn_size;
1129 *size -= insn_size;
1130 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001131 } else { // encounter a broken instruction
1132 size_t skipdata_bytes;
1133
1134 // if there is no request to skip data, or remaining data is too small,
1135 // then bail out
1136 if (!handle->skipdata || handle->skipdata_size > *size)
1137 return false;
1138
1139 if (handle->skipdata_setup.callback) {
1140 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
1141 0, handle->skipdata_setup.user_data);
1142 if (skipdata_bytes > *size)
1143 // remaining data is not enough
1144 return false;
1145
1146 if (!skipdata_bytes)
1147 // user requested not to skip data, so bail out
1148 return false;
1149 } else
1150 skipdata_bytes = handle->skipdata_size;
1151
1152 // we have to skip some amount of data, depending on arch & mode
1153 insn->id = 0; // invalid ID for this "data" instruction
1154 insn->address = *address;
1155 insn->size = (uint16_t)skipdata_bytes;
vit96964a757e12017-05-24 08:07:11 +03001156#ifdef CAPSTONE_DIET
1157 insn->mnemonic[0] = '\0';
1158 insn->op_str[0] = '\0';
1159#else
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001160 memcpy(insn->bytes, *code, skipdata_bytes);
1161 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
1162 sizeof(insn->mnemonic) - 1);
1163 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
vit96964a757e12017-05-24 08:07:11 +03001164#endif
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001165
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001166 *code += skipdata_bytes;
1167 *size -= skipdata_bytes;
1168 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +02001169 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +08001170
1171 return true;
hlide993f3622014-10-05 18:14:40 +02001172}
1173
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001174// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001175CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -07001176const char * CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001177{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +08001178 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001179
1180 if (!handle || handle->reg_name == NULL) {
1181 return NULL;
1182 }
1183
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +08001184 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001185}
1186
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001187CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -07001188const char * CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001189{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +08001190 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001191
1192 if (!handle || handle->insn_name == NULL) {
1193 return NULL;
1194 }
1195
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +08001196 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001197}
1198
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +08001199CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -07001200const char * CAPSTONE_API cs_group_name(csh ud, unsigned int group)
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +08001201{
1202 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1203
1204 if (!handle || handle->group_name == NULL) {
1205 return NULL;
1206 }
1207
1208 return handle->group_name(ud, group);
1209}
1210
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001211CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -07001212bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001213{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001214 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001215 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001216 return false;
1217
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001218 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001219
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001220 if (!handle->detail) {
1221 handle->errnum = CS_ERR_DETAIL;
1222 return false;
1223 }
1224
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001225 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001226 handle->errnum = CS_ERR_SKIPDATA;
1227 return false;
1228 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001229
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001230 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001231 handle->errnum = CS_ERR_DETAIL;
1232 return false;
1233 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001234
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001235 return arr_exist8(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001236}
1237
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001238CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -07001239bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001240{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001241 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001242 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001243 return false;
1244
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001245 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001246
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001247 if (!handle->detail) {
1248 handle->errnum = CS_ERR_DETAIL;
1249 return false;
1250 }
1251
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001252 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001253 handle->errnum = CS_ERR_SKIPDATA;
1254 return false;
1255 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001256
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001257 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001258 handle->errnum = CS_ERR_DETAIL;
1259 return false;
1260 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001261
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001262 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001263}
1264
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001265CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -07001266bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001267{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001268 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001269 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001270 return false;
1271
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001272 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +02001273
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001274 if (!handle->detail) {
1275 handle->errnum = CS_ERR_DETAIL;
1276 return false;
1277 }
1278
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001279 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001280 handle->errnum = CS_ERR_SKIPDATA;
1281 return false;
1282 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001283
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001284 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001285 handle->errnum = CS_ERR_DETAIL;
1286 return false;
1287 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001288
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001289 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001290}
1291
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001292CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -07001293int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001294{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001295 struct cs_struct *handle;
1296 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001297 if (!ud)
1298 return -1;
1299
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001300 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001301
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001302 if (!handle->detail) {
1303 handle->errnum = CS_ERR_DETAIL;
1304 return -1;
1305 }
1306
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001307 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001308 handle->errnum = CS_ERR_SKIPDATA;
1309 return -1;
1310 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001311
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001312 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001313 handle->errnum = CS_ERR_DETAIL;
1314 return -1;
1315 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001316
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001317 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001318
1319 switch (handle->arch) {
1320 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001321 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001322 return -1;
1323 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001324 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001325 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001326 count++;
1327 break;
1328 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001329 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001330 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001331 count++;
1332 break;
1333 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001334 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001335 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001336 count++;
1337 break;
1338 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001339 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001340 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001341 count++;
1342 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001343 case CS_ARCH_PPC:
1344 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001345 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001346 count++;
1347 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001348 case CS_ARCH_SPARC:
1349 for (i = 0; i < insn->detail->sparc.op_count; i++)
1350 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1351 count++;
1352 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001353 case CS_ARCH_SYSZ:
1354 for (i = 0; i < insn->detail->sysz.op_count; i++)
1355 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1356 count++;
1357 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001358 case CS_ARCH_XCORE:
1359 for (i = 0; i < insn->detail->xcore.op_count; i++)
1360 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1361 count++;
1362 break;
Fotis Loukos0e7a2e72016-05-03 16:02:40 +03001363 case CS_ARCH_M68K:
1364 for (i = 0; i < insn->detail->m68k.op_count; i++)
1365 if (insn->detail->m68k.operands[i].type == (m68k_op_type)op_type)
1366 count++;
1367 break;
Fotis Loukos0850d552016-05-03 15:52:11 +03001368 case CS_ARCH_TMS320C64X:
1369 for (i = 0; i < insn->detail->tms320c64x.op_count; i++)
1370 if (insn->detail->tms320c64x.operands[i].type == (tms320c64x_op_type)op_type)
1371 count++;
1372 break;
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +02001373 case CS_ARCH_M680X:
1374 for (i = 0; i < insn->detail->m680x.op_count; i++)
1375 if (insn->detail->m680x.operands[i].type == (m680x_op_type)op_type)
1376 count++;
1377 break;
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +08001378 case CS_ARCH_EVM:
1379#if 0
1380 for (i = 0; i < insn->detail->evm.op_count; i++)
1381 if (insn->detail->evm.operands[i].type == (evm_op_type)op_type)
1382 count++;
1383#endif
Sebastian Macke87221fa2018-12-02 21:39:41 +01001384 break;
david942j0b373902019-02-18 19:52:51 +08001385 case CS_ARCH_MOS65XX:
1386 for (i = 0; i < insn->detail->mos65xx.op_count; i++)
1387 if (insn->detail->mos65xx.operands[i].type == (mos65xx_op_type)op_type)
1388 count++;
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +08001389 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001390 }
1391
1392 return count;
1393}
1394
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001395CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -07001396int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001397 unsigned int post)
1398{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001399 struct cs_struct *handle;
1400 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001401 if (!ud)
1402 return -1;
1403
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001404 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001405
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001406 if (!handle->detail) {
1407 handle->errnum = CS_ERR_DETAIL;
1408 return -1;
1409 }
1410
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001411 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001412 handle->errnum = CS_ERR_SKIPDATA;
1413 return -1;
1414 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001415
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001416 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001417 handle->errnum = CS_ERR_DETAIL;
1418 return -1;
1419 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001420
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001421 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001422
1423 switch (handle->arch) {
1424 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001425 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001426 return -1;
1427 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001428 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001429 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001430 count++;
1431 if (count == post)
1432 return i;
1433 }
1434 break;
1435 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001436 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001437 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001438 count++;
1439 if (count == post)
1440 return i;
1441 }
1442 break;
1443 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001444 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001445 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001446 count++;
1447 if (count == post)
1448 return i;
1449 }
1450 break;
1451 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001452 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001453 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001454 count++;
1455 if (count == post)
1456 return i;
1457 }
1458 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001459 case CS_ARCH_PPC:
1460 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001461 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001462 count++;
1463 if (count == post)
1464 return i;
1465 }
1466 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001467 case CS_ARCH_SPARC:
1468 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1469 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1470 count++;
1471 if (count == post)
1472 return i;
1473 }
1474 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001475 case CS_ARCH_SYSZ:
1476 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1477 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1478 count++;
1479 if (count == post)
1480 return i;
1481 }
1482 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001483 case CS_ARCH_XCORE:
1484 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1485 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1486 count++;
1487 if (count == post)
1488 return i;
1489 }
1490 break;
Fotis Loukos0e7a2e72016-05-03 16:02:40 +03001491 case CS_ARCH_M68K:
1492 for (i = 0; i < insn->detail->m68k.op_count; i++) {
1493 if (insn->detail->m68k.operands[i].type == (m68k_op_type)op_type)
1494 count++;
1495 if (count == post)
1496 return i;
1497 }
1498 break;
Fotis Loukos0850d552016-05-03 15:52:11 +03001499 case CS_ARCH_TMS320C64X:
1500 for (i = 0; i < insn->detail->tms320c64x.op_count; i++) {
1501 if (insn->detail->tms320c64x.operands[i].type == (tms320c64x_op_type)op_type)
1502 count++;
1503 if (count == post)
1504 return i;
1505 }
1506 break;
Wolfgang Schwotzer22b4d0e2017-10-21 15:44:36 +02001507 case CS_ARCH_M680X:
1508 for (i = 0; i < insn->detail->m680x.op_count; i++) {
1509 if (insn->detail->m680x.operands[i].type == (m680x_op_type)op_type)
1510 count++;
1511 if (count == post)
1512 return i;
1513 }
1514 break;
Nguyen Anh Quynhed1246d2018-03-31 17:29:22 +08001515 case CS_ARCH_EVM:
1516#if 0
1517 for (i = 0; i < insn->detail->evm.op_count; i++) {
1518 if (insn->detail->evm.operands[i].type == (evm_op_type)op_type)
1519 count++;
1520 if (count == post)
1521 return i;
1522 }
1523#endif
1524 break;
Sebastian Macke87221fa2018-12-02 21:39:41 +01001525 case CS_ARCH_MOS65XX:
1526 for (i = 0; i < insn->detail->mos65xx.op_count; i++) {
1527 if (insn->detail->mos65xx.operands[i].type == (mos65xx_op_type)op_type)
1528 count++;
1529 if (count == post)
1530 return i;
1531 }
1532 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001533 }
1534
1535 return -1;
1536}
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001537
1538CAPSTONE_EXPORT
tandasat45e5eab2016-05-11 21:48:32 -07001539cs_err CAPSTONE_API cs_regs_access(csh ud, const cs_insn *insn,
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001540 cs_regs regs_read, uint8_t *regs_read_count,
1541 cs_regs regs_write, uint8_t *regs_write_count)
1542{
1543 struct cs_struct *handle;
1544
1545 if (!ud)
1546 return -1;
1547
1548 handle = (struct cs_struct *)(uintptr_t)ud;
1549
1550#ifdef CAPSTONE_DIET
1551 // This API does not work in DIET mode
1552 handle->errnum = CS_ERR_DIET;
1553 return CS_ERR_DIET;
1554#else
1555 if (!handle->detail) {
1556 handle->errnum = CS_ERR_DETAIL;
1557 return CS_ERR_DETAIL;
1558 }
1559
1560 if (!insn->id) {
1561 handle->errnum = CS_ERR_SKIPDATA;
1562 return CS_ERR_SKIPDATA;
1563 }
1564
1565 if (!insn->detail) {
1566 handle->errnum = CS_ERR_DETAIL;
1567 return CS_ERR_DETAIL;
1568 }
1569
1570 if (handle->reg_access) {
1571 handle->reg_access(insn, regs_read, regs_read_count, regs_write, regs_write_count);
1572 } else {
1573 // this arch is unsupported yet
1574 handle->errnum = CS_ERR_ARCH;
1575 return CS_ERR_ARCH;
1576 }
1577
1578 return CS_ERR_OK;
1579#endif
1580}