blob: ebd810958d604c6c6641ba2f18d65b44242a93eb [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
Pba21cc8c2015-11-10 22:43:51 +010020#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(CAPSTONE_DIET)
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
Nguyen Anh Quynh197e4572015-11-10 22:56:53 +080097static void* cs_kern_os_calloc(size_t num, size_t size)
98{
Pb86ad3d62015-11-05 18:08:25 +010099 return kern_os_malloc(num * size); // malloc bzeroes the buffer
100}
101
102cs_malloc_t cs_mem_malloc = kern_os_malloc;
Pb2769c772015-11-06 14:44:55 +0100103cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
Pb86ad3d62015-11-05 18:08:25 +0100104cs_realloc_t cs_mem_realloc = kern_os_realloc;
105cs_free_t cs_mem_free = kern_os_free;
106cs_vsnprintf_t cs_vsnprintf = vsnprintf;
107#endif
108#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800109cs_malloc_t cs_mem_malloc = NULL;
110cs_calloc_t cs_mem_calloc = NULL;
111cs_realloc_t cs_mem_realloc = NULL;
112cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800113cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800114#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800115
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800116CAPSTONE_EXPORT
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +0800117unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800118{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800119 archs_enable();
120
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800121 if (major != NULL && minor != NULL) {
122 *major = CS_API_MAJOR;
123 *minor = CS_API_MINOR;
124 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800125
126 return (CS_API_MAJOR << 8) + CS_API_MINOR;
127}
128
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800129CAPSTONE_EXPORT
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800130bool cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800131{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800132 archs_enable();
133
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800134 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800135 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800136 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800137 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800138 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800139
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800140 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800141 return all_arch & (1 << query);
142
143 if (query == CS_SUPPORT_DIET) {
144#ifdef CAPSTONE_DIET
145 return true;
146#else
147 return false;
148#endif
149 }
150
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800151 if (query == CS_SUPPORT_X86_REDUCE) {
152#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800153 return true;
154#else
155 return false;
156#endif
157 }
158
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800159 // unsupported query
160 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800161}
162
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800163CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800164cs_err cs_errno(csh handle)
165{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800166 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800167 if (!handle)
168 return CS_ERR_CSH;
169
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100170 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800171
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800172 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800173}
174
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800175CAPSTONE_EXPORT
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800176const char *cs_strerror(cs_err code)
177{
178 switch(code) {
179 default:
180 return "Unknown error code";
181 case CS_ERR_OK:
182 return "OK (CS_ERR_OK)";
183 case CS_ERR_MEM:
184 return "Out of memory (CS_ERR_MEM)";
185 case CS_ERR_ARCH:
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800186 return "Invalid/unsupported architecture(CS_ERR_ARCH)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800187 case CS_ERR_HANDLE:
188 return "Invalid handle (CS_ERR_HANDLE)";
189 case CS_ERR_CSH:
190 return "Invalid csh (CS_ERR_CSH)";
191 case CS_ERR_MODE:
192 return "Invalid mode (CS_ERR_MODE)";
193 case CS_ERR_OPTION:
194 return "Invalid option (CS_ERR_OPTION)";
195 case CS_ERR_DETAIL:
196 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800197 case CS_ERR_MEMSETUP:
198 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800199 case CS_ERR_VERSION:
200 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800201 case CS_ERR_DIET:
202 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800203 case CS_ERR_SKIPDATA:
204 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh8aba4cd2015-08-09 10:52:18 -0700205 case CS_ERR_X86_ATT:
206 return "AT&T syntax is unavailable (CS_ERR_X86_ATT)";
207 case CS_ERR_X86_INTEL:
208 return "INTEL syntax is unavailable (CS_ERR_X86_INTEL)";
209 case CS_ERR_X86_MASM:
210 return "MASM syntax is unavailable (CS_ERR_X86_MASM)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800211 }
212}
213
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800214CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800215cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
216{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800217 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800218 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800219 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800220 // Error: before cs_open(), dynamic memory management must be initialized
221 // with cs_option(CS_OPT_MEM)
222 return CS_ERR_MEMSETUP;
223
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800224 archs_enable();
225
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800226 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800227 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800228 if (!ud) {
229 // memory insufficient
230 return CS_ERR_MEM;
231 }
232
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800233 ud->errnum = CS_ERR_OK;
234 ud->arch = arch;
235 ud->mode = mode;
236 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800237 // by default, do not break instruction into details
238 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800239
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800240 // default skipdata setup
241 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
242
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100243 err = arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800244 if (err) {
245 cs_mem_free(ud);
246 *handle = 0;
247 return err;
248 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800249
250 *handle = (uintptr_t)ud;
251
252 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800253 } else {
254 *handle = 0;
255 return CS_ERR_ARCH;
256 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800257}
258
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800259CAPSTONE_EXPORT
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800260cs_err cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800261{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800262 struct cs_struct *ud;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800263 struct insn_mnem *next, *tmp;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800264
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800265 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800266 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800267 return CS_ERR_CSH;
268
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100269 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800270
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800271 if (ud->printer_info)
272 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800273
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800274 // free the linked list of customized mnemonic
275 tmp = ud->mnem_list;
276 while(tmp) {
277 next = tmp->next;
278 cs_mem_free(tmp);
279 tmp = next;
280 }
281
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800282 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800283
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800284 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800285 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800286
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800287 // invalidate this handle by ZERO out its value.
288 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800289 *handle = 0;
290
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800291 return CS_ERR_OK;
292}
293
294// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800295static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800296 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800297{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800298#ifndef CAPSTONE_DIET
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700299 char *sp, *mnem;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800300#endif
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800301 unsigned int copy_size = MIN(sizeof(insn->bytes), insn->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800302
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800303 // fill the instruction bytes.
304 // we might skip some redundant bytes in front in the case of X86
305 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
306 insn->size = copy_size;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800307
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800308 // alias instruction might have ID saved in OpcodePub
309 if (MCInst_getOpcodePub(mci))
310 insn->id = MCInst_getOpcodePub(mci);
311
312 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800313 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800314 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800315
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800316#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800317 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800318 // find first space or tab
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100319 sp = buffer;
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800320 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800321 for (sp = buffer; *sp; sp++) {
322 if (*sp == ' '|| *sp == '\t')
323 break;
324 if (*sp == '|') // lock|rep prefix for x86
325 *sp = ' ';
326 // copy to @mnemonic
327 *mnem = *sp;
328 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800329 }
330
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800331 *mnem = '\0';
332
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800333 // we might have customized mnemonic
334 if (handle->mnem_list) {
335 struct insn_mnem *tmp = handle->mnem_list;
336 while(tmp) {
337 if (tmp->insn.id == insn->id) {
338 // found this instruction, so copy its mnemonic
339 (void)strncpy(insn->mnemonic, tmp->insn.mnemonic, sizeof(insn->mnemonic) - 1);
340 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
341 break;
342 }
343 tmp = tmp->next;
344 }
345 }
346
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800347 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800348 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800349 // find the next non-space char
350 sp++;
351 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
352 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800353 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
354 } else
355 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800356#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800357}
358
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800359// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800360// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800361static uint8_t skipdata_size(cs_struct *handle)
362{
363 switch(handle->arch) {
364 default:
365 // should never reach
366 return -1;
367 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800368 // skip 2 bytes on Thumb mode.
369 if (handle->mode & CS_MODE_THUMB)
370 return 2;
371 // otherwise, skip 4 bytes
372 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800373 case CS_ARCH_ARM64:
374 case CS_ARCH_MIPS:
375 case CS_ARCH_PPC:
376 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800377 // skip 4 bytes
378 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800379 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800380 // SystemZ instruction's length can be 2, 4 or 6 bytes,
381 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800382 return 2;
383 case CS_ARCH_X86:
384 // X86 has no restriction on instruction alignment
385 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800386 case CS_ARCH_XCORE:
387 // XCore instruction's length can be 2 or 4 bytes,
388 // so we just skip 2 bytes
389 return 2;
Daniel Collin2ee675c2015-08-03 18:45:08 +0200390 case CS_ARCH_M68K:
391 // M68K has 2 bytes instruction alignment but contain multibyte instruction so we skip 2 bytes
392 return 2;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800393 }
394}
395
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800396CAPSTONE_EXPORT
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800397cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800398{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800399 struct cs_struct *handle;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800400 cs_opt_mnem *opt;
401
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800402 archs_enable();
403
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800404 // cs_option() can be called with NULL handle just for CS_OPT_MEM
405 // This is supposed to be executed before all other APIs (even cs_open())
406 if (type == CS_OPT_MEM) {
407 cs_opt_mem *mem = (cs_opt_mem *)value;
408
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800409 cs_mem_malloc = mem->malloc;
410 cs_mem_calloc = mem->calloc;
411 cs_mem_realloc = mem->realloc;
412 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800413 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800414
415 return CS_ERR_OK;
416 }
417
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100418 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600419 if (!handle)
420 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800421
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800422 switch(type) {
423 default:
424 break;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800425
Nguyen Anh Quynh256090a2016-03-14 13:52:23 +0800426 case CS_OPT_UNSIGNED:
427 handle->imm_unsigned = (cs_opt_value)value;
428 return CS_ERR_OK;
429
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800430 case CS_OPT_DETAIL:
Félix Cloutier3973d8b2015-03-02 22:06:56 -0500431 handle->detail = (cs_opt_value)value;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800432 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800433
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800434 case CS_OPT_SKIPDATA:
435 handle->skipdata = (value == CS_OPT_ON);
436 if (handle->skipdata) {
437 if (handle->skipdata_size == 0) {
438 // set the default skipdata size
439 handle->skipdata_size = skipdata_size(handle);
440 }
441 }
442 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800443
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800444 case CS_OPT_SKIPDATA_SETUP:
445 if (value)
446 handle->skipdata_setup = *((cs_opt_skipdata *)value);
447 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800448
449 case CS_OPT_MNEMONIC:
450 opt = (cs_opt_mnem *)value;
451 if (opt->id) {
452 if (opt->mnemonic) {
453 struct insn_mnem *tmp;
454
455 // add new instruction, or replace existing instruction
456 // 1. find if we already had this insn in the linked list
457 tmp = handle->mnem_list;
458 while(tmp) {
459 if (tmp->insn.id == opt->id) {
460 // found this instruction, so replace its mnemonic
461 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
462 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
463 break;
464 }
465 tmp = tmp->next;
466 }
467
468 // 2. add this instruction if we have not had it yet
469 if (!tmp) {
470 tmp = cs_mem_malloc(sizeof(*tmp));
471 tmp->insn.id = opt->id;
472 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
473 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
474 // this new instruction is heading the list
475 tmp->next = handle->mnem_list;
476 handle->mnem_list = tmp;
477 }
478 return CS_ERR_OK;
479 } else {
480 struct insn_mnem *prev, *tmp;
481
482 // we want to delete an existing instruction
483 // iterate the list to find the instruction to remove it
484 tmp = handle->mnem_list;
485 prev = tmp;
486 while(tmp) {
487 if (tmp->insn.id == opt->id) {
488 // delete this instruction
489 if (tmp == prev) {
490 // head of the list
491 handle->mnem_list = tmp->next;
492 } else {
493 prev->next = tmp->next;
494 }
495 cs_mem_free(tmp);
496 break;
497 }
498 prev = tmp;
499 tmp = tmp->next;
500 }
501 }
502 }
503 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800504 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800505
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800506 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800507}
508
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800509// generate @op_str for data instruction of SKIPDATA
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800510static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
511{
512 char *p = opstr;
513 int len;
514 size_t i;
515
516 if (!size) {
517 opstr[0] = '\0';
518 return;
519 }
520
521 len = sprintf(p, "0x%02x", buffer[0]);
522 p+= len;
523
524 for(i = 1; i < size; i++) {
525 len = sprintf(p, ", 0x%02x", buffer[i]);
526 p+= len;
527 }
528}
529
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800530// dynamicly allocate memory to contain disasm insn
531// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800532CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800533size_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 +0800534{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800535 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800536 MCInst mci;
537 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800538 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800539 unsigned int f = 0; // index of the next instruction in the cache
540 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800541 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800542 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800543 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800544 void *tmp;
545 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800546 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800547 size_t size_org;
548 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800549 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500550 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800551
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800552 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800553 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800554 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800555 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800556 return 0;
557 }
558
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800559 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800560
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800561#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800562 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400563 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800564#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800565
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800566 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800567 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800568 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800569 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800570
571 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700572 total = cs_mem_malloc(total_size);
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800573 if (total == NULL) {
574 // insufficient memory
Edward Williamsonf1e49752014-12-14 20:45:19 -0500575 handle->errnum = CS_ERR_MEM;
576 return 0;
577 }
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800578
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700579 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800580
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800581 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800582 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800583 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800584
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700585 // relative branches need to know the address & size of current insn
586 mci.address = offset;
587
588 if (handle->detail) {
589 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700590 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700591 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700592 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700593 }
594
595 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700596 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700597 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800598#ifdef CAPSTONE_DIET
599 // zero out mnemonic & op_str
600 mci.flat_insn->mnemonic[0] = '\0';
601 mci.flat_insn->op_str[0] = '\0';
602#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700603
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800604 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800605 if (r) {
606 SStream ss;
607 SStream_Init(&ss);
608
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700609 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800610
611 // map internal instruction opcode to public insn ID
612 handle->insn_id(handle, insn_cache, mci.Opcode);
613
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800614 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800615
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700616 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800617
danghvu0d1aad12014-10-01 23:12:18 -0500618 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800619 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800620 // encounter a broken instruction
621
622 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800623 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800624 cs_mem_free(insn_cache->detail);
625 }
626
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800627 // if there is no request to skip data, or remaining data is too small,
628 // then bail out
629 if (!handle->skipdata || handle->skipdata_size > size)
630 break;
631
632 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800633 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800634 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800635 if (skipdata_bytes > size)
636 // remaining data is not enough
637 break;
638
639 if (!skipdata_bytes)
640 // user requested not to skip data, so bail out
641 break;
642 } else
643 skipdata_bytes = handle->skipdata_size;
644
645 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700646 insn_cache->id = 0; // invalid ID for this "data" instruction
647 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800648 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700649 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
650 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
651 sizeof(insn_cache->mnemonic) - 1);
652 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
653 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800654
danghvu0d1aad12014-10-01 23:12:18 -0500655 next_offset = skipdata_bytes;
656 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800657
danghvu0d1aad12014-10-01 23:12:18 -0500658 // one more instruction entering the cache
659 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800660
danghvu0d1aad12014-10-01 23:12:18 -0500661 // one more instruction disassembled
662 c++;
663 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800664 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500665 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800666
danghvu0d1aad12014-10-01 23:12:18 -0500667 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800668 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500669 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500670 total_size += (sizeof(cs_insn) * cache_size);
671 tmp = cs_mem_realloc(total, total_size);
672 if (tmp == NULL) { // insufficient memory
673 if (handle->detail) {
674 insn_cache = (cs_insn *)total;
675 for (i = 0; i < c; i++, insn_cache++)
676 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800677 }
678
danghvu0d1aad12014-10-01 23:12:18 -0500679 cs_mem_free(total);
680 *insn = NULL;
681 handle->errnum = CS_ERR_MEM;
682 return 0;
683 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800684
danghvu0d1aad12014-10-01 23:12:18 -0500685 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800686 // continue to fill in the cache after the last instruction
687 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800688
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800689 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500690 f = 0;
691 } else
692 insn_cache++;
693
694 buffer += next_offset;
695 size -= next_offset;
696 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800697 }
698
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800699 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800700 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800701 cs_mem_free(total);
702 total = NULL;
703 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800704 // total did not fully use the last cache, so downsize it
Tyler J. Stacheckid07a21b2015-10-23 20:59:20 -0400705 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800706 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800707 // free all detail pointers
708 if (handle->detail) {
709 insn_cache = (cs_insn *)total;
710 for (i = 0; i < c; i++, insn_cache++)
711 cs_mem_free(insn_cache->detail);
712 }
713
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800714 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800715 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800716
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800717 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800718 return 0;
719 }
720
721 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800722 }
723
724 *insn = total;
725
726 return c;
727}
728
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800729CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800730CAPSTONE_DEPRECATED
731size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
732{
733 return cs_disasm(ud, buffer, size, offset, count, insn);
734}
735
736CAPSTONE_EXPORT
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800737void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800738{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800739 size_t i;
740
741 // free all detail pointers
742 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800743 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800744
745 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800746 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800747}
748
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800749CAPSTONE_EXPORT
750cs_insn *cs_malloc(csh ud)
751{
752 cs_insn *insn;
753 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
754
755 insn = cs_mem_malloc(sizeof(cs_insn));
756 if (!insn) {
757 // insufficient memory
758 handle->errnum = CS_ERR_MEM;
759 return NULL;
760 } else {
761 if (handle->detail) {
762 // allocate memory for @detail pointer
763 insn->detail = cs_mem_malloc(sizeof(cs_detail));
764 if (insn->detail == NULL) { // insufficient memory
765 cs_mem_free(insn);
766 handle->errnum = CS_ERR_MEM;
767 return NULL;
768 }
769 } else
770 insn->detail = NULL;
771 }
772
773 return insn;
774}
775
hlide993f3622014-10-05 18:14:40 +0200776// iterator for instruction "single-stepping"
777CAPSTONE_EXPORT
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800778bool cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
779 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200780{
781 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200782 uint16_t insn_size;
783 MCInst mci;
784 bool r;
785
786 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800787 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800788 return false;
hlide993f3622014-10-05 18:14:40 +0200789 }
790
791 handle->errnum = CS_ERR_OK;
792
hlide993f3622014-10-05 18:14:40 +0200793 MCInst_Init(&mci);
794 mci.csh = handle;
795
796 // relative branches need to know the address & size of current insn
797 mci.address = *address;
798
799 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800800 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200801 mci.flat_insn->address = *address;
802#ifdef CAPSTONE_DIET
803 // zero out mnemonic & op_str
804 mci.flat_insn->mnemonic[0] = '\0';
805 mci.flat_insn->op_str[0] = '\0';
806#endif
807
808 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800809 if (r) {
hlide993f3622014-10-05 18:14:40 +0200810 SStream ss;
811 SStream_Init(&ss);
812
813 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800814
815 // map internal instruction opcode to public insn ID
816 handle->insn_id(handle, insn, mci.Opcode);
817
hlide993f3622014-10-05 18:14:40 +0200818 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800819
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800820 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800821
hlide993f3622014-10-05 18:14:40 +0200822 *code += insn_size;
823 *size -= insn_size;
824 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800825 } else { // encounter a broken instruction
826 size_t skipdata_bytes;
827
828 // if there is no request to skip data, or remaining data is too small,
829 // then bail out
830 if (!handle->skipdata || handle->skipdata_size > *size)
831 return false;
832
833 if (handle->skipdata_setup.callback) {
834 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
835 0, handle->skipdata_setup.user_data);
836 if (skipdata_bytes > *size)
837 // remaining data is not enough
838 return false;
839
840 if (!skipdata_bytes)
841 // user requested not to skip data, so bail out
842 return false;
843 } else
844 skipdata_bytes = handle->skipdata_size;
845
846 // we have to skip some amount of data, depending on arch & mode
847 insn->id = 0; // invalid ID for this "data" instruction
848 insn->address = *address;
849 insn->size = (uint16_t)skipdata_bytes;
850 memcpy(insn->bytes, *code, skipdata_bytes);
851 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
852 sizeof(insn->mnemonic) - 1);
853 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
854
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800855 *code += skipdata_bytes;
856 *size -= skipdata_bytes;
857 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200858 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800859
860 return true;
hlide993f3622014-10-05 18:14:40 +0200861}
862
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800863// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800864CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100865const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800866{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800867 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800868
869 if (!handle || handle->reg_name == NULL) {
870 return NULL;
871 }
872
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800873 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800874}
875
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800876CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100877const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800878{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800879 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800880
881 if (!handle || handle->insn_name == NULL) {
882 return NULL;
883 }
884
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800885 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800886}
887
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800888CAPSTONE_EXPORT
889const char *cs_group_name(csh ud, unsigned int group)
890{
891 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
892
893 if (!handle || handle->group_name == NULL) {
894 return NULL;
895 }
896
897 return handle->group_name(ud, group);
898}
899
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800900CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200901bool cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800902{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800903 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800904 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800905 return false;
906
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100907 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200908
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800909 if (!handle->detail) {
910 handle->errnum = CS_ERR_DETAIL;
911 return false;
912 }
913
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800914 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200915 handle->errnum = CS_ERR_SKIPDATA;
916 return false;
917 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200918
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800919 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200920 handle->errnum = CS_ERR_DETAIL;
921 return false;
922 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200923
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800924 return arr_exist8(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800925}
926
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800927CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200928bool cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800929{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800930 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800931 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800932 return false;
933
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100934 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200935
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800936 if (!handle->detail) {
937 handle->errnum = CS_ERR_DETAIL;
938 return false;
939 }
940
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800941 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200942 handle->errnum = CS_ERR_SKIPDATA;
943 return false;
944 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200945
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800946 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200947 handle->errnum = CS_ERR_DETAIL;
948 return false;
949 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200950
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800951 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800952}
953
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800954CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200955bool cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800956{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800957 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800958 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800959 return false;
960
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100961 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +0200962
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800963 if (!handle->detail) {
964 handle->errnum = CS_ERR_DETAIL;
965 return false;
966 }
967
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800968 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200969 handle->errnum = CS_ERR_SKIPDATA;
970 return false;
971 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200972
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800973 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200974 handle->errnum = CS_ERR_DETAIL;
975 return false;
976 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200977
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800978 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800979}
980
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800981CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200982int cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800983{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800984 struct cs_struct *handle;
985 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800986 if (!ud)
987 return -1;
988
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100989 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200990
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800991 if (!handle->detail) {
992 handle->errnum = CS_ERR_DETAIL;
993 return -1;
994 }
995
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800996 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200997 handle->errnum = CS_ERR_SKIPDATA;
998 return -1;
999 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001000
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001001 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001002 handle->errnum = CS_ERR_DETAIL;
1003 return -1;
1004 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001005
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001006 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001007
1008 switch (handle->arch) {
1009 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001010 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001011 return -1;
1012 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001013 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001014 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001015 count++;
1016 break;
1017 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001018 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001019 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001020 count++;
1021 break;
1022 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001023 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001024 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001025 count++;
1026 break;
1027 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001028 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001029 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001030 count++;
1031 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001032 case CS_ARCH_PPC:
1033 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001034 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001035 count++;
1036 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001037 case CS_ARCH_SPARC:
1038 for (i = 0; i < insn->detail->sparc.op_count; i++)
1039 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1040 count++;
1041 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001042 case CS_ARCH_SYSZ:
1043 for (i = 0; i < insn->detail->sysz.op_count; i++)
1044 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1045 count++;
1046 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001047 case CS_ARCH_XCORE:
1048 for (i = 0; i < insn->detail->xcore.op_count; i++)
1049 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1050 count++;
1051 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001052 }
1053
1054 return count;
1055}
1056
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001057CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +02001058int cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001059 unsigned int post)
1060{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001061 struct cs_struct *handle;
1062 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001063 if (!ud)
1064 return -1;
1065
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001066 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001067
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001068 if (!handle->detail) {
1069 handle->errnum = CS_ERR_DETAIL;
1070 return -1;
1071 }
1072
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001073 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001074 handle->errnum = CS_ERR_SKIPDATA;
1075 return -1;
1076 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001077
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001078 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001079 handle->errnum = CS_ERR_DETAIL;
1080 return -1;
1081 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001082
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001083 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001084
1085 switch (handle->arch) {
1086 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001087 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001088 return -1;
1089 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001090 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001091 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001092 count++;
1093 if (count == post)
1094 return i;
1095 }
1096 break;
1097 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001098 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001099 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001100 count++;
1101 if (count == post)
1102 return i;
1103 }
1104 break;
1105 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001106 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001107 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001108 count++;
1109 if (count == post)
1110 return i;
1111 }
1112 break;
1113 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001114 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001115 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001116 count++;
1117 if (count == post)
1118 return i;
1119 }
1120 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001121 case CS_ARCH_PPC:
1122 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001123 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001124 count++;
1125 if (count == post)
1126 return i;
1127 }
1128 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001129 case CS_ARCH_SPARC:
1130 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1131 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1132 count++;
1133 if (count == post)
1134 return i;
1135 }
1136 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001137 case CS_ARCH_SYSZ:
1138 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1139 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1140 count++;
1141 if (count == post)
1142 return i;
1143 }
1144 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001145 case CS_ARCH_XCORE:
1146 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1147 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1148 count++;
1149 if (count == post)
1150 return i;
1151 }
1152 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001153 }
1154
1155 return -1;
1156}
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001157
1158CAPSTONE_EXPORT
1159cs_err cs_regs_access(csh ud, const cs_insn *insn,
1160 cs_regs regs_read, uint8_t *regs_read_count,
1161 cs_regs regs_write, uint8_t *regs_write_count)
1162{
1163 struct cs_struct *handle;
1164
1165 if (!ud)
1166 return -1;
1167
1168 handle = (struct cs_struct *)(uintptr_t)ud;
1169
1170#ifdef CAPSTONE_DIET
1171 // This API does not work in DIET mode
1172 handle->errnum = CS_ERR_DIET;
1173 return CS_ERR_DIET;
1174#else
1175 if (!handle->detail) {
1176 handle->errnum = CS_ERR_DETAIL;
1177 return CS_ERR_DETAIL;
1178 }
1179
1180 if (!insn->id) {
1181 handle->errnum = CS_ERR_SKIPDATA;
1182 return CS_ERR_SKIPDATA;
1183 }
1184
1185 if (!insn->detail) {
1186 handle->errnum = CS_ERR_DETAIL;
1187 return CS_ERR_DETAIL;
1188 }
1189
1190 if (handle->reg_access) {
1191 handle->reg_access(insn, regs_read, regs_read_count, regs_write, regs_write_count);
1192 } else {
1193 // this arch is unsupported yet
1194 handle->errnum = CS_ERR_ARCH;
1195 return CS_ERR_ARCH;
1196 }
1197
1198 return CS_ERR_OK;
1199#endif
1200}