blob: 9d745cfe8d32a83c0bbaeab7ebc812ecd5292e15 [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
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080086cs_malloc_t cs_mem_malloc = malloc;
87cs_calloc_t cs_mem_calloc = calloc;
88cs_realloc_t cs_mem_realloc = realloc;
89cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080090cs_vsnprintf_t cs_vsnprintf = vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080091#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080092cs_malloc_t cs_mem_malloc = NULL;
93cs_calloc_t cs_mem_calloc = NULL;
94cs_realloc_t cs_mem_realloc = NULL;
95cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080096cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080097#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080098
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +080099CAPSTONE_EXPORT
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +0800100unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800101{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800102 archs_enable();
103
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800104 if (major != NULL && minor != NULL) {
105 *major = CS_API_MAJOR;
106 *minor = CS_API_MINOR;
107 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800108
109 return (CS_API_MAJOR << 8) + CS_API_MINOR;
110}
111
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800112CAPSTONE_EXPORT
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800113bool cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800114{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800115 archs_enable();
116
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800117 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800118 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800119 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800120 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800121 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800122
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800123 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800124 return all_arch & (1 << query);
125
126 if (query == CS_SUPPORT_DIET) {
127#ifdef CAPSTONE_DIET
128 return true;
129#else
130 return false;
131#endif
132 }
133
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800134 if (query == CS_SUPPORT_X86_REDUCE) {
135#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800136 return true;
137#else
138 return false;
139#endif
140 }
141
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800142 // unsupported query
143 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800144}
145
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800146CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800147cs_err cs_errno(csh handle)
148{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800149 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800150 if (!handle)
151 return CS_ERR_CSH;
152
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100153 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800154
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800155 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800156}
157
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800158CAPSTONE_EXPORT
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800159const char *cs_strerror(cs_err code)
160{
161 switch(code) {
162 default:
163 return "Unknown error code";
164 case CS_ERR_OK:
165 return "OK (CS_ERR_OK)";
166 case CS_ERR_MEM:
167 return "Out of memory (CS_ERR_MEM)";
168 case CS_ERR_ARCH:
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800169 return "Invalid/unsupported architecture(CS_ERR_ARCH)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800170 case CS_ERR_HANDLE:
171 return "Invalid handle (CS_ERR_HANDLE)";
172 case CS_ERR_CSH:
173 return "Invalid csh (CS_ERR_CSH)";
174 case CS_ERR_MODE:
175 return "Invalid mode (CS_ERR_MODE)";
176 case CS_ERR_OPTION:
177 return "Invalid option (CS_ERR_OPTION)";
178 case CS_ERR_DETAIL:
179 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800180 case CS_ERR_MEMSETUP:
181 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800182 case CS_ERR_VERSION:
183 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800184 case CS_ERR_DIET:
185 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800186 case CS_ERR_SKIPDATA:
187 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh8aba4cd2015-08-09 10:52:18 -0700188 case CS_ERR_X86_ATT:
189 return "AT&T syntax is unavailable (CS_ERR_X86_ATT)";
190 case CS_ERR_X86_INTEL:
191 return "INTEL syntax is unavailable (CS_ERR_X86_INTEL)";
192 case CS_ERR_X86_MASM:
193 return "MASM syntax is unavailable (CS_ERR_X86_MASM)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800194 }
195}
196
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800197CAPSTONE_EXPORT
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800198cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
199{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800200 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800201 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800202 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800203 // Error: before cs_open(), dynamic memory management must be initialized
204 // with cs_option(CS_OPT_MEM)
205 return CS_ERR_MEMSETUP;
206
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800207 archs_enable();
208
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800209 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800210 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800211 if (!ud) {
212 // memory insufficient
213 return CS_ERR_MEM;
214 }
215
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800216 ud->errnum = CS_ERR_OK;
217 ud->arch = arch;
218 ud->mode = mode;
219 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800220 // by default, do not break instruction into details
221 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800222
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800223 // default skipdata setup
224 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
225
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100226 err = arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800227 if (err) {
228 cs_mem_free(ud);
229 *handle = 0;
230 return err;
231 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800232
233 *handle = (uintptr_t)ud;
234
235 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800236 } else {
237 *handle = 0;
238 return CS_ERR_ARCH;
239 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800240}
241
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800242CAPSTONE_EXPORT
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800243cs_err cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800244{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800245 struct cs_struct *ud;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800246 struct insn_mnem *next, *tmp;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800247
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800248 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800249 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800250 return CS_ERR_CSH;
251
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100252 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800253
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800254 if (ud->printer_info)
255 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800256
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800257 // free the linked list of customized mnemonic
258 tmp = ud->mnem_list;
259 while(tmp) {
260 next = tmp->next;
261 cs_mem_free(tmp);
262 tmp = next;
263 }
264
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800265 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800266
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800267 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800268 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800269
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800270 // invalidate this handle by ZERO out its value.
271 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800272 *handle = 0;
273
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800274 return CS_ERR_OK;
275}
276
277// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800278static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800279 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800280{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800281#ifndef CAPSTONE_DIET
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700282 char *sp, *mnem;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800283#endif
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800284 unsigned int copy_size = MIN(sizeof(insn->bytes), insn->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800285
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800286 // fill the instruction bytes.
287 // we might skip some redundant bytes in front in the case of X86
288 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
289 insn->size = copy_size;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800290
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800291 // alias instruction might have ID saved in OpcodePub
292 if (MCInst_getOpcodePub(mci))
293 insn->id = MCInst_getOpcodePub(mci);
294
295 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800296 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800297 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800298
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800299#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800300 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800301 // find first space or tab
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100302 sp = buffer;
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800303 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800304 for (sp = buffer; *sp; sp++) {
305 if (*sp == ' '|| *sp == '\t')
306 break;
307 if (*sp == '|') // lock|rep prefix for x86
308 *sp = ' ';
309 // copy to @mnemonic
310 *mnem = *sp;
311 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800312 }
313
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800314 *mnem = '\0';
315
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800316 // we might have customized mnemonic
317 if (handle->mnem_list) {
318 struct insn_mnem *tmp = handle->mnem_list;
319 while(tmp) {
320 if (tmp->insn.id == insn->id) {
321 // found this instruction, so copy its mnemonic
322 (void)strncpy(insn->mnemonic, tmp->insn.mnemonic, sizeof(insn->mnemonic) - 1);
323 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
324 break;
325 }
326 tmp = tmp->next;
327 }
328 }
329
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800330 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800331 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800332 // find the next non-space char
333 sp++;
334 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
335 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800336 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
337 } else
338 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800339#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800340}
341
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800342// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800343// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800344static uint8_t skipdata_size(cs_struct *handle)
345{
346 switch(handle->arch) {
347 default:
348 // should never reach
349 return -1;
350 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800351 // skip 2 bytes on Thumb mode.
352 if (handle->mode & CS_MODE_THUMB)
353 return 2;
354 // otherwise, skip 4 bytes
355 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800356 case CS_ARCH_ARM64:
357 case CS_ARCH_MIPS:
358 case CS_ARCH_PPC:
359 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800360 // skip 4 bytes
361 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800362 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800363 // SystemZ instruction's length can be 2, 4 or 6 bytes,
364 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800365 return 2;
366 case CS_ARCH_X86:
367 // X86 has no restriction on instruction alignment
368 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800369 case CS_ARCH_XCORE:
370 // XCore instruction's length can be 2 or 4 bytes,
371 // so we just skip 2 bytes
372 return 2;
Daniel Collin2ee675c2015-08-03 18:45:08 +0200373 case CS_ARCH_M68K:
374 // M68K has 2 bytes instruction alignment but contain multibyte instruction so we skip 2 bytes
375 return 2;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800376 }
377}
378
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800379CAPSTONE_EXPORT
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800380cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800381{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800382 struct cs_struct *handle;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800383 cs_opt_mnem *opt;
384
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800385 archs_enable();
386
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800387 // cs_option() can be called with NULL handle just for CS_OPT_MEM
388 // This is supposed to be executed before all other APIs (even cs_open())
389 if (type == CS_OPT_MEM) {
390 cs_opt_mem *mem = (cs_opt_mem *)value;
391
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800392 cs_mem_malloc = mem->malloc;
393 cs_mem_calloc = mem->calloc;
394 cs_mem_realloc = mem->realloc;
395 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800396 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800397
398 return CS_ERR_OK;
399 }
400
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100401 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600402 if (!handle)
403 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800404
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800405 switch(type) {
406 default:
407 break;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800408
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800409 case CS_OPT_DETAIL:
Félix Cloutier3973d8b2015-03-02 22:06:56 -0500410 handle->detail = (cs_opt_value)value;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800411 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800412
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800413 case CS_OPT_SKIPDATA:
414 handle->skipdata = (value == CS_OPT_ON);
415 if (handle->skipdata) {
416 if (handle->skipdata_size == 0) {
417 // set the default skipdata size
418 handle->skipdata_size = skipdata_size(handle);
419 }
420 }
421 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800422
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800423 case CS_OPT_SKIPDATA_SETUP:
424 if (value)
425 handle->skipdata_setup = *((cs_opt_skipdata *)value);
426 return CS_ERR_OK;
Nguyen Anh Quynh0b965452015-04-26 22:54:41 +0800427
428 case CS_OPT_MNEMONIC:
429 opt = (cs_opt_mnem *)value;
430 if (opt->id) {
431 if (opt->mnemonic) {
432 struct insn_mnem *tmp;
433
434 // add new instruction, or replace existing instruction
435 // 1. find if we already had this insn in the linked list
436 tmp = handle->mnem_list;
437 while(tmp) {
438 if (tmp->insn.id == opt->id) {
439 // found this instruction, so replace its mnemonic
440 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
441 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
442 break;
443 }
444 tmp = tmp->next;
445 }
446
447 // 2. add this instruction if we have not had it yet
448 if (!tmp) {
449 tmp = cs_mem_malloc(sizeof(*tmp));
450 tmp->insn.id = opt->id;
451 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
452 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
453 // this new instruction is heading the list
454 tmp->next = handle->mnem_list;
455 handle->mnem_list = tmp;
456 }
457 return CS_ERR_OK;
458 } else {
459 struct insn_mnem *prev, *tmp;
460
461 // we want to delete an existing instruction
462 // iterate the list to find the instruction to remove it
463 tmp = handle->mnem_list;
464 prev = tmp;
465 while(tmp) {
466 if (tmp->insn.id == opt->id) {
467 // delete this instruction
468 if (tmp == prev) {
469 // head of the list
470 handle->mnem_list = tmp->next;
471 } else {
472 prev->next = tmp->next;
473 }
474 cs_mem_free(tmp);
475 break;
476 }
477 prev = tmp;
478 tmp = tmp->next;
479 }
480 }
481 }
482 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800483 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800484
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800485 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800486}
487
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800488// generate @op_str for data instruction of SKIPDATA
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800489static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
490{
491 char *p = opstr;
492 int len;
493 size_t i;
494
495 if (!size) {
496 opstr[0] = '\0';
497 return;
498 }
499
500 len = sprintf(p, "0x%02x", buffer[0]);
501 p+= len;
502
503 for(i = 1; i < size; i++) {
504 len = sprintf(p, ", 0x%02x", buffer[i]);
505 p+= len;
506 }
507}
508
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800509// dynamicly allocate memory to contain disasm insn
510// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800511CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800512size_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 +0800513{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800514 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800515 MCInst mci;
516 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800517 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800518 unsigned int f = 0; // index of the next instruction in the cache
519 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800520 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800521 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800522 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800523 void *tmp;
524 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800525 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800526 size_t size_org;
527 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800528 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500529 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800530
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800531 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800532 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800533 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800534 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800535 return 0;
536 }
537
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800538 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800539
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800540#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800541 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400542 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800543#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800544
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800545 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800546 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800547 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800548 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800549
550 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700551 total = cs_mem_malloc(total_size);
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800552 if (total == NULL) {
553 // insufficient memory
Edward Williamsonf1e49752014-12-14 20:45:19 -0500554 handle->errnum = CS_ERR_MEM;
555 return 0;
556 }
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800557
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700558 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800559
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800560 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800561 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800562 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800563
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700564 // relative branches need to know the address & size of current insn
565 mci.address = offset;
566
567 if (handle->detail) {
568 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700569 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700570 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700571 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700572 }
573
574 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700575 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700576 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800577#ifdef CAPSTONE_DIET
578 // zero out mnemonic & op_str
579 mci.flat_insn->mnemonic[0] = '\0';
580 mci.flat_insn->op_str[0] = '\0';
581#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700582
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800583 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800584 if (r) {
585 SStream ss;
586 SStream_Init(&ss);
587
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700588 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800589
590 // map internal instruction opcode to public insn ID
591 handle->insn_id(handle, insn_cache, mci.Opcode);
592
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800593 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800594
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700595 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800596
danghvu0d1aad12014-10-01 23:12:18 -0500597 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800598 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800599 // encounter a broken instruction
600
601 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800602 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800603 cs_mem_free(insn_cache->detail);
604 }
605
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800606 // if there is no request to skip data, or remaining data is too small,
607 // then bail out
608 if (!handle->skipdata || handle->skipdata_size > size)
609 break;
610
611 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800612 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800613 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800614 if (skipdata_bytes > size)
615 // remaining data is not enough
616 break;
617
618 if (!skipdata_bytes)
619 // user requested not to skip data, so bail out
620 break;
621 } else
622 skipdata_bytes = handle->skipdata_size;
623
624 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700625 insn_cache->id = 0; // invalid ID for this "data" instruction
626 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800627 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700628 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
629 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
630 sizeof(insn_cache->mnemonic) - 1);
631 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
632 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800633
danghvu0d1aad12014-10-01 23:12:18 -0500634 next_offset = skipdata_bytes;
635 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800636
danghvu0d1aad12014-10-01 23:12:18 -0500637 // one more instruction entering the cache
638 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800639
danghvu0d1aad12014-10-01 23:12:18 -0500640 // one more instruction disassembled
641 c++;
642 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800643 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500644 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800645
danghvu0d1aad12014-10-01 23:12:18 -0500646 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800647 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500648 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500649 total_size += (sizeof(cs_insn) * cache_size);
650 tmp = cs_mem_realloc(total, total_size);
651 if (tmp == NULL) { // insufficient memory
652 if (handle->detail) {
653 insn_cache = (cs_insn *)total;
654 for (i = 0; i < c; i++, insn_cache++)
655 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800656 }
657
danghvu0d1aad12014-10-01 23:12:18 -0500658 cs_mem_free(total);
659 *insn = NULL;
660 handle->errnum = CS_ERR_MEM;
661 return 0;
662 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800663
danghvu0d1aad12014-10-01 23:12:18 -0500664 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800665 // continue to fill in the cache after the last instruction
666 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800667
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800668 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500669 f = 0;
670 } else
671 insn_cache++;
672
673 buffer += next_offset;
674 size -= next_offset;
675 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800676 }
677
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800678 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800679 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800680 cs_mem_free(total);
681 total = NULL;
682 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800683 // total did not fully use the last cache, so downsize it
Tyler J. Stacheckid07a21b2015-10-23 20:59:20 -0400684 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800685 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800686 // free all detail pointers
687 if (handle->detail) {
688 insn_cache = (cs_insn *)total;
689 for (i = 0; i < c; i++, insn_cache++)
690 cs_mem_free(insn_cache->detail);
691 }
692
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800693 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800694 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800695
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800696 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800697 return 0;
698 }
699
700 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800701 }
702
703 *insn = total;
704
705 return c;
706}
707
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800708CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800709CAPSTONE_DEPRECATED
710size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
711{
712 return cs_disasm(ud, buffer, size, offset, count, insn);
713}
714
715CAPSTONE_EXPORT
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800716void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800717{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800718 size_t i;
719
720 // free all detail pointers
721 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800722 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800723
724 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800725 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800726}
727
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800728CAPSTONE_EXPORT
729cs_insn *cs_malloc(csh ud)
730{
731 cs_insn *insn;
732 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
733
734 insn = cs_mem_malloc(sizeof(cs_insn));
735 if (!insn) {
736 // insufficient memory
737 handle->errnum = CS_ERR_MEM;
738 return NULL;
739 } else {
740 if (handle->detail) {
741 // allocate memory for @detail pointer
742 insn->detail = cs_mem_malloc(sizeof(cs_detail));
743 if (insn->detail == NULL) { // insufficient memory
744 cs_mem_free(insn);
745 handle->errnum = CS_ERR_MEM;
746 return NULL;
747 }
748 } else
749 insn->detail = NULL;
750 }
751
752 return insn;
753}
754
hlide993f3622014-10-05 18:14:40 +0200755// iterator for instruction "single-stepping"
756CAPSTONE_EXPORT
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800757bool cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
758 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200759{
760 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200761 uint16_t insn_size;
762 MCInst mci;
763 bool r;
764
765 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800766 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800767 return false;
hlide993f3622014-10-05 18:14:40 +0200768 }
769
770 handle->errnum = CS_ERR_OK;
771
hlide993f3622014-10-05 18:14:40 +0200772 MCInst_Init(&mci);
773 mci.csh = handle;
774
775 // relative branches need to know the address & size of current insn
776 mci.address = *address;
777
778 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800779 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200780 mci.flat_insn->address = *address;
781#ifdef CAPSTONE_DIET
782 // zero out mnemonic & op_str
783 mci.flat_insn->mnemonic[0] = '\0';
784 mci.flat_insn->op_str[0] = '\0';
785#endif
786
787 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800788 if (r) {
hlide993f3622014-10-05 18:14:40 +0200789 SStream ss;
790 SStream_Init(&ss);
791
792 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800793
794 // map internal instruction opcode to public insn ID
795 handle->insn_id(handle, insn, mci.Opcode);
796
hlide993f3622014-10-05 18:14:40 +0200797 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800798
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800799 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800800
hlide993f3622014-10-05 18:14:40 +0200801 *code += insn_size;
802 *size -= insn_size;
803 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800804 } else { // encounter a broken instruction
805 size_t skipdata_bytes;
806
807 // if there is no request to skip data, or remaining data is too small,
808 // then bail out
809 if (!handle->skipdata || handle->skipdata_size > *size)
810 return false;
811
812 if (handle->skipdata_setup.callback) {
813 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
814 0, handle->skipdata_setup.user_data);
815 if (skipdata_bytes > *size)
816 // remaining data is not enough
817 return false;
818
819 if (!skipdata_bytes)
820 // user requested not to skip data, so bail out
821 return false;
822 } else
823 skipdata_bytes = handle->skipdata_size;
824
825 // we have to skip some amount of data, depending on arch & mode
826 insn->id = 0; // invalid ID for this "data" instruction
827 insn->address = *address;
828 insn->size = (uint16_t)skipdata_bytes;
829 memcpy(insn->bytes, *code, skipdata_bytes);
830 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
831 sizeof(insn->mnemonic) - 1);
832 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
833
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800834 *code += skipdata_bytes;
835 *size -= skipdata_bytes;
836 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200837 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800838
839 return true;
hlide993f3622014-10-05 18:14:40 +0200840}
841
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800842// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800843CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100844const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800845{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800846 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800847
848 if (!handle || handle->reg_name == NULL) {
849 return NULL;
850 }
851
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800852 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800853}
854
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800855CAPSTONE_EXPORT
pancakef0e4eed2013-12-11 22:14:42 +0100856const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800857{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800858 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800859
860 if (!handle || handle->insn_name == NULL) {
861 return NULL;
862 }
863
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800864 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800865}
866
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800867CAPSTONE_EXPORT
868const char *cs_group_name(csh ud, unsigned int group)
869{
870 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
871
872 if (!handle || handle->group_name == NULL) {
873 return NULL;
874 }
875
876 return handle->group_name(ud, group);
877}
878
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800879CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200880bool cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800881{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800882 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800883 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800884 return false;
885
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100886 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200887
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800888 if (!handle->detail) {
889 handle->errnum = CS_ERR_DETAIL;
890 return false;
891 }
892
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800893 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200894 handle->errnum = CS_ERR_SKIPDATA;
895 return false;
896 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200897
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800898 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200899 handle->errnum = CS_ERR_DETAIL;
900 return false;
901 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200902
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800903 return arr_exist8(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800904}
905
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800906CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200907bool cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800908{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800909 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800910 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800911 return false;
912
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100913 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200914
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800915 if (!handle->detail) {
916 handle->errnum = CS_ERR_DETAIL;
917 return false;
918 }
919
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800920 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200921 handle->errnum = CS_ERR_SKIPDATA;
922 return false;
923 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200924
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800925 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200926 handle->errnum = CS_ERR_DETAIL;
927 return false;
928 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200929
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800930 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800931}
932
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800933CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200934bool cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800935{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800936 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800937 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800938 return false;
939
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100940 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +0200941
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800942 if (!handle->detail) {
943 handle->errnum = CS_ERR_DETAIL;
944 return false;
945 }
946
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800947 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200948 handle->errnum = CS_ERR_SKIPDATA;
949 return false;
950 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200951
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800952 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200953 handle->errnum = CS_ERR_DETAIL;
954 return false;
955 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200956
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800957 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800958}
959
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800960CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +0200961int cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800962{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800963 struct cs_struct *handle;
964 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800965 if (!ud)
966 return -1;
967
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100968 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200969
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800970 if (!handle->detail) {
971 handle->errnum = CS_ERR_DETAIL;
972 return -1;
973 }
974
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800975 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200976 handle->errnum = CS_ERR_SKIPDATA;
977 return -1;
978 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200979
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +0800980 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +0200981 handle->errnum = CS_ERR_DETAIL;
982 return -1;
983 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200984
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800985 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800986
987 switch (handle->arch) {
988 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800989 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800990 return -1;
991 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800992 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800993 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800994 count++;
995 break;
996 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800997 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800998 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800999 count++;
1000 break;
1001 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001002 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001003 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001004 count++;
1005 break;
1006 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001007 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001008 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001009 count++;
1010 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001011 case CS_ARCH_PPC:
1012 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -08001013 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001014 count++;
1015 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001016 case CS_ARCH_SPARC:
1017 for (i = 0; i < insn->detail->sparc.op_count; i++)
1018 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1019 count++;
1020 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001021 case CS_ARCH_SYSZ:
1022 for (i = 0; i < insn->detail->sysz.op_count; i++)
1023 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1024 count++;
1025 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001026 case CS_ARCH_XCORE:
1027 for (i = 0; i < insn->detail->xcore.op_count; i++)
1028 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1029 count++;
1030 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001031 }
1032
1033 return count;
1034}
1035
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001036CAPSTONE_EXPORT
obs876b6b62014-08-21 00:57:04 +02001037int cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001038 unsigned int post)
1039{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001040 struct cs_struct *handle;
1041 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001042 if (!ud)
1043 return -1;
1044
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001045 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001046
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001047 if (!handle->detail) {
1048 handle->errnum = CS_ERR_DETAIL;
1049 return -1;
1050 }
1051
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001052 if (!insn->id) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001053 handle->errnum = CS_ERR_SKIPDATA;
1054 return -1;
1055 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001056
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001057 if (!insn->detail) {
Giovanni Condello95657e02014-05-07 17:31:27 +02001058 handle->errnum = CS_ERR_DETAIL;
1059 return -1;
1060 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001061
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001062 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001063
1064 switch (handle->arch) {
1065 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001066 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001067 return -1;
1068 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001069 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001070 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001071 count++;
1072 if (count == post)
1073 return i;
1074 }
1075 break;
1076 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001077 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001078 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001079 count++;
1080 if (count == post)
1081 return i;
1082 }
1083 break;
1084 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001085 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001086 if (insn->detail->x86.operands[i].type == (x86_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_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001093 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001094 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001095 count++;
1096 if (count == post)
1097 return i;
1098 }
1099 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001100 case CS_ARCH_PPC:
1101 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001102 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001103 count++;
1104 if (count == post)
1105 return i;
1106 }
1107 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001108 case CS_ARCH_SPARC:
1109 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1110 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1111 count++;
1112 if (count == post)
1113 return i;
1114 }
1115 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001116 case CS_ARCH_SYSZ:
1117 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1118 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1119 count++;
1120 if (count == post)
1121 return i;
1122 }
1123 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001124 case CS_ARCH_XCORE:
1125 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1126 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1127 count++;
1128 if (count == post)
1129 return i;
1130 }
1131 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001132 }
1133
1134 return -1;
1135}
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +08001136
1137CAPSTONE_EXPORT
1138cs_err cs_regs_access(csh ud, const cs_insn *insn,
1139 cs_regs regs_read, uint8_t *regs_read_count,
1140 cs_regs regs_write, uint8_t *regs_write_count)
1141{
1142 struct cs_struct *handle;
1143
1144 if (!ud)
1145 return -1;
1146
1147 handle = (struct cs_struct *)(uintptr_t)ud;
1148
1149#ifdef CAPSTONE_DIET
1150 // This API does not work in DIET mode
1151 handle->errnum = CS_ERR_DIET;
1152 return CS_ERR_DIET;
1153#else
1154 if (!handle->detail) {
1155 handle->errnum = CS_ERR_DETAIL;
1156 return CS_ERR_DETAIL;
1157 }
1158
1159 if (!insn->id) {
1160 handle->errnum = CS_ERR_SKIPDATA;
1161 return CS_ERR_SKIPDATA;
1162 }
1163
1164 if (!insn->detail) {
1165 handle->errnum = CS_ERR_DETAIL;
1166 return CS_ERR_DETAIL;
1167 }
1168
1169 if (handle->reg_access) {
1170 handle->reg_access(insn, regs_read, regs_read_count, regs_write, regs_write_count);
1171 } else {
1172 // this arch is unsupported yet
1173 handle->errnum = CS_ERR_ARCH;
1174 return CS_ERR_ARCH;
1175 }
1176
1177 return CS_ERR_OK;
1178#endif
1179}