blob: 36627298f347199d28238b75bb2f3b34d09d516d [file] [log] [blame]
Nguyen Anh Quynh6023ef72014-04-29 11:21:04 +08001/* Capstone Disassembly Engine */
Nguyen Anh Quynhbfcaba52015-03-04 17:45:23 +08002/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */
Axel 0vercl0k Souchet605faf12014-05-09 20:40:00 +01003#if defined (WIN32) || defined (WIN64) || defined (_WIN32) || defined (_WIN64)
4#pragma warning(disable:4996)
5#endif
reverser160e1982015-04-09 18:28:19 +01006#if defined(CAPSTONE_HAS_OSXKERNEL)
7#include <libkern/libkern.h>
8#else
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08009#include <stddef.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080010#include <stdio.h>
11#include <stdlib.h>
reverser160e1982015-04-09 18:28:19 +010012#endif
13
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080014#include <string.h>
pancake9c10ace2015-02-24 04:55:55 +010015#include <capstone/capstone.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080016
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080017#include "utils.h"
Nguyen Anh Quynhc7404072014-01-05 11:35:47 +080018#include "MCRegisterInfo.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080019
Axel 0vercl0k Souchet84fecf22014-05-10 09:49:29 +010020#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh472a4a42014-03-06 09:13:04 +080021#define INSN_CACHE_SIZE 32
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080022#else
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080023// reduce stack variable size for kernel/firmware
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080024#define INSN_CACHE_SIZE 8
25#endif
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +080026
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080027// default SKIPDATA mnemonic
Nguyen Anh Quynhc75a9092014-04-10 10:26:49 +080028#define SKIPDATA_MNEM ".byte"
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080029
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080030cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080031cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
32void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080033
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080034extern void ARM_enable(void);
35extern void AArch64_enable(void);
Daniel Collin2ee675c2015-08-03 18:45:08 +020036extern void M68K_enable(void);
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080037extern void Mips_enable(void);
38extern void X86_enable(void);
39extern void PPC_enable(void);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080040extern void Sparc_enable(void);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080041extern void SystemZ_enable(void);
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080042extern void XCore_enable(void);
danghvu701b8502014-01-09 11:06:44 +070043
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080044static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080045{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080046 static bool initialized = false;
47
48 if (initialized)
49 return;
50
danghvub33bd2c2014-01-09 12:22:56 +070051#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080052 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070053#endif
54#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080055 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070056#endif
Daniel Collin2ee675c2015-08-03 18:45:08 +020057#ifdef CAPSTONE_HAS_M68K
58 M68K_enable();
59#endif
danghvub33bd2c2014-01-09 12:22:56 +070060#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080061 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070062#endif
danghvub33bd2c2014-01-09 12:22:56 +070063#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080064 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070065#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080066#ifdef CAPSTONE_HAS_SPARC
67 Sparc_enable();
68#endif
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080069#ifdef CAPSTONE_HAS_SYSZ
70 SystemZ_enable();
71#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080072#ifdef CAPSTONE_HAS_X86
73 X86_enable();
74#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080075#ifdef CAPSTONE_HAS_XCORE
76 XCore_enable();
77#endif
78
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080079
80 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +070081}
82
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080083unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080084
Axel 0vercl0k Souchet84fecf22014-05-10 09:49:29 +010085#ifdef CAPSTONE_USE_SYS_DYN_MEM
Pb86ad3d62015-11-05 18:08:25 +010086#ifndef CAPSTONE_HAS_OSXKERNEL
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080087cs_malloc_t cs_mem_malloc = malloc;
88cs_calloc_t cs_mem_calloc = calloc;
89cs_realloc_t cs_mem_realloc = realloc;
90cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080091cs_vsnprintf_t cs_vsnprintf = vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080092#else
Pb86ad3d62015-11-05 18:08:25 +010093extern void* kern_os_malloc(size_t size);
94extern void kern_os_free(void* addr);
95extern void* kern_os_realloc(void* addr, size_t nsize);
96
Pb2769c772015-11-06 14:44:55 +010097static void* cs_kern_os_calloc(size_t num, size_t size) {
Pb86ad3d62015-11-05 18:08:25 +010098 return kern_os_malloc(num * size); // malloc bzeroes the buffer
99}
100
101cs_malloc_t cs_mem_malloc = kern_os_malloc;
Pb2769c772015-11-06 14:44:55 +0100102cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
Pb86ad3d62015-11-05 18:08:25 +0100103cs_realloc_t cs_mem_realloc = kern_os_realloc;
104cs_free_t cs_mem_free = kern_os_free;
105cs_vsnprintf_t cs_vsnprintf = vsnprintf;
106#endif
107#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800108cs_malloc_t cs_mem_malloc = NULL;
109cs_calloc_t cs_mem_calloc = NULL;
110cs_realloc_t cs_mem_realloc = NULL;
111cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800112cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800113#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800114
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800115CAPSTONE_EXPORT
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +0800116unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800117{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800118 archs_enable();
119
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800120 if (major != NULL && minor != NULL) {
121 *major = CS_API_MAJOR;
122 *minor = CS_API_MINOR;
123 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800124
125 return (CS_API_MAJOR << 8) + CS_API_MINOR;
126}
127
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800128CAPSTONE_EXPORT
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800129bool cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800130{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800131 archs_enable();
132
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800133 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800134 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800135 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800136 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800137 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800138
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800139 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800140 return all_arch & (1 << query);
141
142 if (query == CS_SUPPORT_DIET) {
143#ifdef CAPSTONE_DIET
144 return true;
145#else
146 return false;
147#endif
148 }
149
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800150 if (query == CS_SUPPORT_X86_REDUCE) {
151#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800152 return true;
153#else
154 return false;
155#endif
156 }
157
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800158 // unsupported query
159 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800160}
161
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800162CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800163cs_err cs_errno(csh handle)
164{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800165 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800166 if (!handle)
167 return CS_ERR_CSH;
168
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100169 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800170
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800171 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800172}
173
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800174CAPSTONE_EXPORT
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800175const char *cs_strerror(cs_err code)
176{
177 switch(code) {
178 default:
179 return "Unknown error code";
180 case CS_ERR_OK:
181 return "OK (CS_ERR_OK)";
182 case CS_ERR_MEM:
183 return "Out of memory (CS_ERR_MEM)";
184 case CS_ERR_ARCH:
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800185 return "Invalid/unsupported architecture(CS_ERR_ARCH)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800186 case CS_ERR_HANDLE:
187 return "Invalid handle (CS_ERR_HANDLE)";
188 case CS_ERR_CSH:
189 return "Invalid csh (CS_ERR_CSH)";
190 case CS_ERR_MODE:
191 return "Invalid mode (CS_ERR_MODE)";
192 case CS_ERR_OPTION:
193 return "Invalid option (CS_ERR_OPTION)";
194 case CS_ERR_DETAIL:
195 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800196 case CS_ERR_MEMSETUP:
197 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800198 case CS_ERR_VERSION:
199 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800200 case CS_ERR_DIET:
201 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800202 case CS_ERR_SKIPDATA:
203 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh8aba4cd2015-08-09 10:52:18 -0700204 case CS_ERR_X86_ATT:
205 return "AT&T syntax is unavailable (CS_ERR_X86_ATT)";
206 case CS_ERR_X86_INTEL:
207 return "INTEL syntax is unavailable (CS_ERR_X86_INTEL)";
208 case CS_ERR_X86_MASM:
209 return "MASM syntax is unavailable (CS_ERR_X86_MASM)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800210 }
211}
212
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800213CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800214cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
215{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800216 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800217 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800218 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800219 // Error: before cs_open(), dynamic memory management must be initialized
220 // with cs_option(CS_OPT_MEM)
221 return CS_ERR_MEMSETUP;
222
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800223 archs_enable();
224
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800225 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800226 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800227 if (!ud) {
228 // memory insufficient
229 return CS_ERR_MEM;
230 }
231
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800232 ud->errnum = CS_ERR_OK;
233 ud->arch = arch;
234 ud->mode = mode;
235 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800236 // by default, do not break instruction into details
237 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800238
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800239 // default skipdata setup
240 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
241
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100242 err = arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800243 if (err) {
244 cs_mem_free(ud);
245 *handle = 0;
246 return err;
247 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800248
249 *handle = (uintptr_t)ud;
250
251 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800252 } else {
253 *handle = 0;
254 return CS_ERR_ARCH;
255 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800256}
257
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800258CAPSTONE_EXPORT
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800259cs_err cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800260{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800261 struct cs_struct *ud;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800262 struct insn_mnem *next, *tmp;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800263
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800264 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800265 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800266 return CS_ERR_CSH;
267
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100268 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800269
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800270 if (ud->printer_info)
271 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800272
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800273 // free the linked list of customized mnemonic
274 tmp = ud->mnem_list;
275 while(tmp) {
276 next = tmp->next;
277 cs_mem_free(tmp);
278 tmp = next;
279 }
280
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800281 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800282
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800283 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800284 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800285
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800286 // invalidate this handle by ZERO out its value.
287 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800288 *handle = 0;
289
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800290 return CS_ERR_OK;
291}
292
293// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800294static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800295 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800296{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800297#ifndef CAPSTONE_DIET
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700298 char *sp, *mnem;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800299#endif
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800300 unsigned int copy_size = MIN(sizeof(insn->bytes), insn->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800301
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800302 // fill the instruction bytes.
303 // we might skip some redundant bytes in front in the case of X86
304 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
305 insn->size = copy_size;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800306
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800307 // alias instruction might have ID saved in OpcodePub
308 if (MCInst_getOpcodePub(mci))
309 insn->id = MCInst_getOpcodePub(mci);
310
311 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800312 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800313 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800314
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800315#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800316 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800317 // find first space or tab
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100318 sp = buffer;
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800319 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800320 for (sp = buffer; *sp; sp++) {
321 if (*sp == ' '|| *sp == '\t')
322 break;
323 if (*sp == '|') // lock|rep prefix for x86
324 *sp = ' ';
325 // copy to @mnemonic
326 *mnem = *sp;
327 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800328 }
329
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800330 *mnem = '\0';
331
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800332 // we might have customized mnemonic
333 if (handle->mnem_list) {
334 struct insn_mnem *tmp = handle->mnem_list;
335 while(tmp) {
336 if (tmp->insn.id == insn->id) {
337 // found this instruction, so copy its mnemonic
338 (void)strncpy(insn->mnemonic, tmp->insn.mnemonic, sizeof(insn->mnemonic) - 1);
339 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
340 break;
341 }
342 tmp = tmp->next;
343 }
344 }
345
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800346 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800347 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800348 // find the next non-space char
349 sp++;
350 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
351 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800352 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
353 } else
354 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800355#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800356}
357
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800358// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800359// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800360static uint8_t skipdata_size(cs_struct *handle)
361{
362 switch(handle->arch) {
363 default:
364 // should never reach
365 return -1;
366 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800367 // skip 2 bytes on Thumb mode.
368 if (handle->mode & CS_MODE_THUMB)
369 return 2;
370 // otherwise, skip 4 bytes
371 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800372 case CS_ARCH_ARM64:
373 case CS_ARCH_MIPS:
374 case CS_ARCH_PPC:
375 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800376 // skip 4 bytes
377 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800378 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800379 // SystemZ instruction's length can be 2, 4 or 6 bytes,
380 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800381 return 2;
382 case CS_ARCH_X86:
383 // X86 has no restriction on instruction alignment
384 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800385 case CS_ARCH_XCORE:
386 // XCore instruction's length can be 2 or 4 bytes,
387 // so we just skip 2 bytes
388 return 2;
Daniel Collin2ee675c2015-08-03 18:45:08 +0200389 case CS_ARCH_M68K:
390 // M68K has 2 bytes instruction alignment but contain multibyte instruction so we skip 2 bytes
391 return 2;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800392 }
393}
394
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800395CAPSTONE_EXPORT
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800396cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800397{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800398 struct cs_struct *handle;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800399 cs_opt_mnem *opt;
400
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800401 archs_enable();
402
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800403 // cs_option() can be called with NULL handle just for CS_OPT_MEM
404 // This is supposed to be executed before all other APIs (even cs_open())
405 if (type == CS_OPT_MEM) {
406 cs_opt_mem *mem = (cs_opt_mem *)value;
407
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800408 cs_mem_malloc = mem->malloc;
409 cs_mem_calloc = mem->calloc;
410 cs_mem_realloc = mem->realloc;
411 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800412 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800413
414 return CS_ERR_OK;
415 }
416
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100417 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600418 if (!handle)
419 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800420
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800421 switch(type) {
422 default:
423 break;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800424
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800425 case CS_OPT_DETAIL:
Félix Cloutier3973d8b2015-03-02 22:06:56 -0500426 handle->detail = (cs_opt_value)value;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800427 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800428
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800429 case CS_OPT_SKIPDATA:
430 handle->skipdata = (value == CS_OPT_ON);
431 if (handle->skipdata) {
432 if (handle->skipdata_size == 0) {
433 // set the default skipdata size
434 handle->skipdata_size = skipdata_size(handle);
435 }
436 }
437 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800438
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800439 case CS_OPT_SKIPDATA_SETUP:
440 if (value)
441 handle->skipdata_setup = *((cs_opt_skipdata *)value);
442 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800443
444 case CS_OPT_MNEMONIC:
445 opt = (cs_opt_mnem *)value;
446 if (opt->id) {
447 if (opt->mnemonic) {
448 struct insn_mnem *tmp;
449
450 // add new instruction, or replace existing instruction
451 // 1. find if we already had this insn in the linked list
452 tmp = handle->mnem_list;
453 while(tmp) {
454 if (tmp->insn.id == opt->id) {
455 // found this instruction, so replace its mnemonic
456 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
457 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
458 break;
459 }
460 tmp = tmp->next;
461 }
462
463 // 2. add this instruction if we have not had it yet
464 if (!tmp) {
465 tmp = cs_mem_malloc(sizeof(*tmp));
466 tmp->insn.id = opt->id;
467 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
468 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
469 // this new instruction is heading the list
470 tmp->next = handle->mnem_list;
471 handle->mnem_list = tmp;
472 }
473 return CS_ERR_OK;
474 } else {
475 struct insn_mnem *prev, *tmp;
476
477 // we want to delete an existing instruction
478 // iterate the list to find the instruction to remove it
479 tmp = handle->mnem_list;
480 prev = tmp;
481 while(tmp) {
482 if (tmp->insn.id == opt->id) {
483 // delete this instruction
484 if (tmp == prev) {
485 // head of the list
486 handle->mnem_list = tmp->next;
487 } else {
488 prev->next = tmp->next;
489 }
490 cs_mem_free(tmp);
491 break;
492 }
493 prev = tmp;
494 tmp = tmp->next;
495 }
496 }
497 }
498 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800499 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800500
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800501 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800502}
503
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800504// generate @op_str for data instruction of SKIPDATA
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800505static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
506{
507 char *p = opstr;
508 int len;
509 size_t i;
510
511 if (!size) {
512 opstr[0] = '\0';
513 return;
514 }
515
516 len = sprintf(p, "0x%02x", buffer[0]);
517 p+= len;
518
519 for(i = 1; i < size; i++) {
520 len = sprintf(p, ", 0x%02x", buffer[i]);
521 p+= len;
522 }
523}
524
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800525// dynamicly allocate memory to contain disasm insn
526// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800527CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800528size_t 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 +0800529{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800530 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800531 MCInst mci;
532 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800533 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800534 unsigned int f = 0; // index of the next instruction in the cache
535 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800536 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800537 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800538 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800539 void *tmp;
540 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800541 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800542 size_t size_org;
543 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800544 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500545 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800546
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800547 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800548 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800549 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800550 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800551 return 0;
552 }
553
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800554 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800555
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800556#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800557 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400558 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800559#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800560
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800561 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800562 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800563 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800564 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800565
566 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700567 total = cs_mem_malloc(total_size);
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800568 if (total == NULL) {
569 // insufficient memory
Edward Williamsonf1e49752014-12-14 20:45:19 -0500570 handle->errnum = CS_ERR_MEM;
571 return 0;
572 }
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800573
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700574 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800575
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800576 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800577 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800578 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800579
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700580 // relative branches need to know the address & size of current insn
581 mci.address = offset;
582
583 if (handle->detail) {
584 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700585 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700586 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700587 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700588 }
589
590 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700591 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700592 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800593#ifdef CAPSTONE_DIET
594 // zero out mnemonic & op_str
595 mci.flat_insn->mnemonic[0] = '\0';
596 mci.flat_insn->op_str[0] = '\0';
597#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700598
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800599 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800600 if (r) {
601 SStream ss;
602 SStream_Init(&ss);
603
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700604 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800605
606 // map internal instruction opcode to public insn ID
607 handle->insn_id(handle, insn_cache, mci.Opcode);
608
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800609 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800610
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700611 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800612
danghvu0d1aad12014-10-01 23:12:18 -0500613 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800614 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800615 // encounter a broken instruction
616
617 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800618 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800619 cs_mem_free(insn_cache->detail);
620 }
621
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800622 // if there is no request to skip data, or remaining data is too small,
623 // then bail out
624 if (!handle->skipdata || handle->skipdata_size > size)
625 break;
626
627 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800628 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800629 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800630 if (skipdata_bytes > size)
631 // remaining data is not enough
632 break;
633
634 if (!skipdata_bytes)
635 // user requested not to skip data, so bail out
636 break;
637 } else
638 skipdata_bytes = handle->skipdata_size;
639
640 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700641 insn_cache->id = 0; // invalid ID for this "data" instruction
642 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800643 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700644 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
645 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
646 sizeof(insn_cache->mnemonic) - 1);
647 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
648 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800649
danghvu0d1aad12014-10-01 23:12:18 -0500650 next_offset = skipdata_bytes;
651 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800652
danghvu0d1aad12014-10-01 23:12:18 -0500653 // one more instruction entering the cache
654 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800655
danghvu0d1aad12014-10-01 23:12:18 -0500656 // one more instruction disassembled
657 c++;
658 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800659 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500660 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800661
danghvu0d1aad12014-10-01 23:12:18 -0500662 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800663 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500664 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500665 total_size += (sizeof(cs_insn) * cache_size);
666 tmp = cs_mem_realloc(total, total_size);
667 if (tmp == NULL) { // insufficient memory
668 if (handle->detail) {
669 insn_cache = (cs_insn *)total;
670 for (i = 0; i < c; i++, insn_cache++)
671 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800672 }
673
danghvu0d1aad12014-10-01 23:12:18 -0500674 cs_mem_free(total);
675 *insn = NULL;
676 handle->errnum = CS_ERR_MEM;
677 return 0;
678 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800679
danghvu0d1aad12014-10-01 23:12:18 -0500680 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800681 // continue to fill in the cache after the last instruction
682 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800683
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800684 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500685 f = 0;
686 } else
687 insn_cache++;
688
689 buffer += next_offset;
690 size -= next_offset;
691 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800692 }
693
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800694 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800695 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800696 cs_mem_free(total);
697 total = NULL;
698 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800699 // total did not fully use the last cache, so downsize it
Tyler J. Stacheckid07a21b2015-10-23 20:59:20 -0400700 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800701 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800702 // free all detail pointers
703 if (handle->detail) {
704 insn_cache = (cs_insn *)total;
705 for (i = 0; i < c; i++, insn_cache++)
706 cs_mem_free(insn_cache->detail);
707 }
708
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800709 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800710 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800711
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800712 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800713 return 0;
714 }
715
716 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800717 }
718
719 *insn = total;
720
721 return c;
722}
723
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800724CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800725CAPSTONE_DEPRECATED
726size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
727{
728 return cs_disasm(ud, buffer, size, offset, count, insn);
729}
730
731CAPSTONE_EXPORT
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800732void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800733{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800734 size_t i;
735
736 // free all detail pointers
737 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800738 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800739
740 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800741 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800742}
743
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800744CAPSTONE_EXPORT
745cs_insn *cs_malloc(csh ud)
746{
747 cs_insn *insn;
748 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
749
750 insn = cs_mem_malloc(sizeof(cs_insn));
751 if (!insn) {
752 // insufficient memory
753 handle->errnum = CS_ERR_MEM;
754 return NULL;
755 } else {
756 if (handle->detail) {
757 // allocate memory for @detail pointer
758 insn->detail = cs_mem_malloc(sizeof(cs_detail));
759 if (insn->detail == NULL) { // insufficient memory
760 cs_mem_free(insn);
761 handle->errnum = CS_ERR_MEM;
762 return NULL;
763 }
764 } else
765 insn->detail = NULL;
766 }
767
768 return insn;
769}
770
hlide993f3622014-10-05 18:14:40 +0200771// iterator for instruction "single-stepping"
772CAPSTONE_EXPORT
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800773bool cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
774 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200775{
776 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200777 uint16_t insn_size;
778 MCInst mci;
779 bool r;
780
781 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800782 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800783 return false;
hlide993f3622014-10-05 18:14:40 +0200784 }
785
786 handle->errnum = CS_ERR_OK;
787
hlide993f3622014-10-05 18:14:40 +0200788 MCInst_Init(&mci);
789 mci.csh = handle;
790
791 // relative branches need to know the address & size of current insn
792 mci.address = *address;
793
794 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800795 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200796 mci.flat_insn->address = *address;
797#ifdef CAPSTONE_DIET
798 // zero out mnemonic & op_str
799 mci.flat_insn->mnemonic[0] = '\0';
800 mci.flat_insn->op_str[0] = '\0';
801#endif
802
803 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800804 if (r) {
hlide993f3622014-10-05 18:14:40 +0200805 SStream ss;
806 SStream_Init(&ss);
807
808 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800809
810 // map internal instruction opcode to public insn ID
811 handle->insn_id(handle, insn, mci.Opcode);
812
hlide993f3622014-10-05 18:14:40 +0200813 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800814
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800815 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800816
hlide993f3622014-10-05 18:14:40 +0200817 *code += insn_size;
818 *size -= insn_size;
819 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800820 } else { // encounter a broken instruction
821 size_t skipdata_bytes;
822
823 // if there is no request to skip data, or remaining data is too small,
824 // then bail out
825 if (!handle->skipdata || handle->skipdata_size > *size)
826 return false;
827
828 if (handle->skipdata_setup.callback) {
829 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
830 0, handle->skipdata_setup.user_data);
831 if (skipdata_bytes > *size)
832 // remaining data is not enough
833 return false;
834
835 if (!skipdata_bytes)
836 // user requested not to skip data, so bail out
837 return false;
838 } else
839 skipdata_bytes = handle->skipdata_size;
840
841 // we have to skip some amount of data, depending on arch & mode
842 insn->id = 0; // invalid ID for this "data" instruction
843 insn->address = *address;
844 insn->size = (uint16_t)skipdata_bytes;
845 memcpy(insn->bytes, *code, skipdata_bytes);
846 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
847 sizeof(insn->mnemonic) - 1);
848 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
849
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800850 *code += skipdata_bytes;
851 *size -= skipdata_bytes;
852 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200853 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800854
855 return true;
hlide993f3622014-10-05 18:14:40 +0200856}
857
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800858// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800859CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100860const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800861{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800862 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800863
864 if (!handle || handle->reg_name == NULL) {
865 return NULL;
866 }
867
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800868 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800869}
870
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800871CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100872const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800873{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800874 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800875
876 if (!handle || handle->insn_name == NULL) {
877 return NULL;
878 }
879
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800880 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800881}
882
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800883CAPSTONE_EXPORT
884const char *cs_group_name(csh ud, unsigned int group)
885{
886 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
887
888 if (!handle || handle->group_name == NULL) {
889 return NULL;
890 }
891
892 return handle->group_name(ud, group);
893}
894
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800895CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200896bool cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800897{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800898 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800899 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800900 return false;
901
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100902 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200903
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800904 if (!handle->detail) {
905 handle->errnum = CS_ERR_DETAIL;
906 return false;
907 }
908
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800909 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200910 handle->errnum = CS_ERR_SKIPDATA;
911 return false;
912 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200913
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800914 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200915 handle->errnum = CS_ERR_DETAIL;
916 return false;
917 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200918
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800919 return arr_exist8(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800920}
921
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800922CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200923bool cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800924{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800925 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800926 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800927 return false;
928
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100929 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200930
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800931 if (!handle->detail) {
932 handle->errnum = CS_ERR_DETAIL;
933 return false;
934 }
935
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800936 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200937 handle->errnum = CS_ERR_SKIPDATA;
938 return false;
939 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200940
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800941 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200942 handle->errnum = CS_ERR_DETAIL;
943 return false;
944 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200945
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800946 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800947}
948
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800949CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200950bool cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800951{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800952 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800953 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800954 return false;
955
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100956 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +0200957
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800958 if (!handle->detail) {
959 handle->errnum = CS_ERR_DETAIL;
960 return false;
961 }
962
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800963 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200964 handle->errnum = CS_ERR_SKIPDATA;
965 return false;
966 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200967
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800968 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200969 handle->errnum = CS_ERR_DETAIL;
970 return false;
971 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200972
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800973 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800974}
975
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800976CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200977int cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800978{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800979 struct cs_struct *handle;
980 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800981 if (!ud)
982 return -1;
983
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100984 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200985
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800986 if (!handle->detail) {
987 handle->errnum = CS_ERR_DETAIL;
988 return -1;
989 }
990
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800991 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200992 handle->errnum = CS_ERR_SKIPDATA;
993 return -1;
994 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200995
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800996 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200997 handle->errnum = CS_ERR_DETAIL;
998 return -1;
999 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001000
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001001 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001002
1003 switch (handle->arch) {
1004 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001005 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001006 return -1;
1007 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001008 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001009 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001010 count++;
1011 break;
1012 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001013 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001014 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001015 count++;
1016 break;
1017 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001018 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001019 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001020 count++;
1021 break;
1022 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001023 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001024 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001025 count++;
1026 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001027 case CS_ARCH_PPC:
1028 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001029 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001030 count++;
1031 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001032 case CS_ARCH_SPARC:
1033 for (i = 0; i < insn->detail->sparc.op_count; i++)
1034 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1035 count++;
1036 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001037 case CS_ARCH_SYSZ:
1038 for (i = 0; i < insn->detail->sysz.op_count; i++)
1039 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1040 count++;
1041 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001042 case CS_ARCH_XCORE:
1043 for (i = 0; i < insn->detail->xcore.op_count; i++)
1044 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1045 count++;
1046 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001047 }
1048
1049 return count;
1050}
1051
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001052CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +02001053int cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001054 unsigned int post)
1055{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001056 struct cs_struct *handle;
1057 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001058 if (!ud)
1059 return -1;
1060
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001061 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001062
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001063 if (!handle->detail) {
1064 handle->errnum = CS_ERR_DETAIL;
1065 return -1;
1066 }
1067
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001068 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001069 handle->errnum = CS_ERR_SKIPDATA;
1070 return -1;
1071 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001072
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001073 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001074 handle->errnum = CS_ERR_DETAIL;
1075 return -1;
1076 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001077
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001078 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001079
1080 switch (handle->arch) {
1081 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001082 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001083 return -1;
1084 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001085 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001086 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001087 count++;
1088 if (count == post)
1089 return i;
1090 }
1091 break;
1092 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001093 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001094 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001095 count++;
1096 if (count == post)
1097 return i;
1098 }
1099 break;
1100 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001101 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001102 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001103 count++;
1104 if (count == post)
1105 return i;
1106 }
1107 break;
1108 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001109 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001110 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001111 count++;
1112 if (count == post)
1113 return i;
1114 }
1115 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001116 case CS_ARCH_PPC:
1117 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001118 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001119 count++;
1120 if (count == post)
1121 return i;
1122 }
1123 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001124 case CS_ARCH_SPARC:
1125 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1126 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1127 count++;
1128 if (count == post)
1129 return i;
1130 }
1131 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001132 case CS_ARCH_SYSZ:
1133 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1134 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1135 count++;
1136 if (count == post)
1137 return i;
1138 }
1139 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001140 case CS_ARCH_XCORE:
1141 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1142 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1143 count++;
1144 if (count == post)
1145 return i;
1146 }
1147 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001148 }
1149
1150 return -1;
1151}
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001152
1153CAPSTONE_EXPORT
1154cs_err cs_regs_access(csh ud, const cs_insn *insn,
1155 cs_regs regs_read, uint8_t *regs_read_count,
1156 cs_regs regs_write, uint8_t *regs_write_count)
1157{
1158 struct cs_struct *handle;
1159
1160 if (!ud)
1161 return -1;
1162
1163 handle = (struct cs_struct *)(uintptr_t)ud;
1164
1165#ifdef CAPSTONE_DIET
1166 // This API does not work in DIET mode
1167 handle->errnum = CS_ERR_DIET;
1168 return CS_ERR_DIET;
1169#else
1170 if (!handle->detail) {
1171 handle->errnum = CS_ERR_DETAIL;
1172 return CS_ERR_DETAIL;
1173 }
1174
1175 if (!insn->id) {
1176 handle->errnum = CS_ERR_SKIPDATA;
1177 return CS_ERR_SKIPDATA;
1178 }
1179
1180 if (!insn->detail) {
1181 handle->errnum = CS_ERR_DETAIL;
1182 return CS_ERR_DETAIL;
1183 }
1184
1185 if (handle->reg_access) {
1186 handle->reg_access(insn, regs_read, regs_read_count, regs_write, regs_write_count);
1187 } else {
1188 // this arch is unsupported yet
1189 handle->errnum = CS_ERR_ARCH;
1190 return CS_ERR_ARCH;
1191 }
1192
1193 return CS_ERR_OK;
1194#endif
1195}