blob: e6e44345450104eefd496f8bad548b8a7920937a [file] [log] [blame]
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001/* Capstone Disassembler Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */
3
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08004#include <stddef.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08005#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <capstone.h>
9
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080010#include "utils.h"
Nguyen Anh Quynhc7404072014-01-05 11:35:47 +080011#include "MCRegisterInfo.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080012
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +080013#define INSN_CACHE_SIZE 64
14
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080015cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080016cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
17void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080018
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080019extern void ARM_enable(void);
20extern void AArch64_enable(void);
21extern void Mips_enable(void);
22extern void X86_enable(void);
23extern void PPC_enable(void);
danghvu701b8502014-01-09 11:06:44 +070024
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080025static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080026{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080027 static bool initialized = false;
28
29 if (initialized)
30 return;
31
danghvub33bd2c2014-01-09 12:22:56 +070032#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080033 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070034#endif
35#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080036 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070037#endif
38#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080039 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070040#endif
41#ifdef CAPSTONE_HAS_X86
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080042 X86_enable();
danghvub33bd2c2014-01-09 12:22:56 +070043#endif
44#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080045 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070046#endif
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080047
48 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +070049}
50
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080051unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080052
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080053#ifdef USE_SYS_DYN_MEM
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080054cs_malloc_t cs_mem_malloc = malloc;
55cs_calloc_t cs_mem_calloc = calloc;
56cs_realloc_t cs_mem_realloc = realloc;
57cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080058cs_vsnprintf_t cs_vsnprintf = vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080059#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080060cs_malloc_t cs_mem_malloc = NULL;
61cs_calloc_t cs_mem_calloc = NULL;
62cs_realloc_t cs_mem_realloc = NULL;
63cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080064cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080065#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080066
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080067unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080068{
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080069 if (major != NULL && minor != NULL) {
70 *major = CS_API_MAJOR;
71 *minor = CS_API_MINOR;
72 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080073
74 return (CS_API_MAJOR << 8) + CS_API_MINOR;
75}
76
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +080077bool cs_support(int arch)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080078{
79 if (arch == CS_ARCH_ALL)
80 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080081 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
82 (1 << CS_ARCH_PPC));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080083
84 return all_arch & (1 << arch);
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080085}
86
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080087cs_err cs_errno(csh handle)
88{
89 if (!handle)
90 return CS_ERR_CSH;
91
92 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
93
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +080094 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080095}
96
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +080097const char *cs_strerror(cs_err code)
98{
99 switch(code) {
100 default:
101 return "Unknown error code";
102 case CS_ERR_OK:
103 return "OK (CS_ERR_OK)";
104 case CS_ERR_MEM:
105 return "Out of memory (CS_ERR_MEM)";
106 case CS_ERR_ARCH:
107 return "Invalid architecture (CS_ERR_ARCH)";
108 case CS_ERR_HANDLE:
109 return "Invalid handle (CS_ERR_HANDLE)";
110 case CS_ERR_CSH:
111 return "Invalid csh (CS_ERR_CSH)";
112 case CS_ERR_MODE:
113 return "Invalid mode (CS_ERR_MODE)";
114 case CS_ERR_OPTION:
115 return "Invalid option (CS_ERR_OPTION)";
116 case CS_ERR_DETAIL:
117 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800118 case CS_ERR_MEMSETUP:
119 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800120 case CS_ERR_VERSION:
121 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800122 }
123}
124
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800125cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
126{
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800127 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800128 // Error: before cs_open(), dynamic memory management must be initialized
129 // with cs_option(CS_OPT_MEM)
130 return CS_ERR_MEMSETUP;
131
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800132 archs_enable();
133
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800134 if (arch < CS_ARCH_MAX && arch_init[arch]) {
135 cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800136
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800137 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800138 if (!ud) {
139 // memory insufficient
140 return CS_ERR_MEM;
141 }
142
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800143 ud->errnum = CS_ERR_OK;
144 ud->arch = arch;
145 ud->mode = mode;
146 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800147 // by default, do not break instruction into details
148 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800149
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800150 cs_err err = arch_init[ud->arch](ud);
151 if (err) {
152 cs_mem_free(ud);
153 *handle = 0;
154 return err;
155 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800156
157 *handle = (uintptr_t)ud;
158
159 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800160 } else {
161 *handle = 0;
162 return CS_ERR_ARCH;
163 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800164}
165
166cs_err cs_close(csh handle)
167{
168 if (!handle)
169 return CS_ERR_CSH;
170
171 cs_struct *ud = (cs_struct *)(uintptr_t)handle;
172
173 switch (ud->arch) {
174 case CS_ARCH_X86:
175 break;
176 case CS_ARCH_ARM:
177 case CS_ARCH_MIPS:
178 case CS_ARCH_ARM64:
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800179 case CS_ARCH_PPC:
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800180 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800181 break;
182 default: // unsupported architecture
183 return CS_ERR_HANDLE;
184 }
185
Nguyen Anh Quynh9fac5122014-01-07 10:56:04 +0800186 // arch_destroy[ud->arch](ud);
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800187
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800188 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800189 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800190 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800191
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800192 return CS_ERR_OK;
193}
194
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800195#define MIN(x, y) ((x) < (y) ? (x) : (y))
196
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800197// fill insn with mnemonic & operands info
198static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800199 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800200{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800201 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800202 // avoiding copy insn->detail
203 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800204
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800205 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
206 // copy from @regs_read until @arm
207 memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read),
208 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
209 // then copy from @arm until end
210 memcpy((void *)(insn->detail) + offsetof(cs_detail, arm), (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm),
211 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800212 } else {
213 insn->address = mci->address;
214 insn->size = mci->insn_size;
215 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800216
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800217 // fill the instruction bytes
218 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
219
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800220 // map internal instruction opcode to public insn ID
221 if (handle->insn_id)
Nguyen Anh Quynh1acfd0b2014-01-06 10:56:59 +0800222 handle->insn_id(handle, insn, MCInst_getOpcode(mci));
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800223
224 // alias instruction might have ID saved in OpcodePub
225 if (MCInst_getOpcodePub(mci))
226 insn->id = MCInst_getOpcodePub(mci);
227
228 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800229 if (postprinter)
230 postprinter((csh)handle, insn, buffer);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800231
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800232 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800233 // find first space or tab
234 char *sp = buffer;
235 for (sp = buffer; *sp; sp++)
236 if (*sp == ' '||*sp == '\t')
237 break;
238 if (*sp) {
239 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800240 // find the next non-space char
241 sp++;
242 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
243 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800244 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
245 } else
246 insn->op_str[0] = '\0';
247
248 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
249 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
250}
251
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800252cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800253{
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800254 // cs_option() can be called with NULL handle just for CS_OPT_MEM
255 // This is supposed to be executed before all other APIs (even cs_open())
256 if (type == CS_OPT_MEM) {
257 cs_opt_mem *mem = (cs_opt_mem *)value;
258
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800259 cs_mem_malloc = mem->malloc;
260 cs_mem_calloc = mem->calloc;
261 cs_mem_realloc = mem->realloc;
262 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800263 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800264
265 return CS_ERR_OK;
266 }
267
danghvu2b192962013-12-19 22:40:28 -0600268 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
269 if (!handle)
270 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800271
danghvu0b6ea042013-12-19 23:07:26 -0600272 if (type == CS_OPT_DETAIL) {
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800273 handle->detail = value;
274 return CS_ERR_OK;
275 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800276
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800277 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800278}
279
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800280// get previous instruction, which can be in the cache, or in total buffer
281static cs_insn *get_prev_insn(cs_insn *cache, unsigned int f, void *total, size_t total_size)
282{
283 if (f == 0) {
284 if (total == NULL)
285 return NULL;
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800286 // get the trailing insn from total buffer, which is at
287 // the end of the latest cache trunk
288 return (cs_insn *)(total + total_size - (sizeof(cs_insn) * INSN_CACHE_SIZE));
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800289 } else
290 return &cache[f - 1];
291}
292
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800293// dynamicly allocate memory to contain disasm insn
294// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800295size_t cs_disasm_ex(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 +0800296{
297 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
298 MCInst mci;
299 uint16_t insn_size;
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800300 size_t c = 0;
301 unsigned int f = 0;
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800302 cs_insn insn_cache[INSN_CACHE_SIZE];
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800303 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800304 size_t total_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800305
306 if (!handle) {
307 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800308 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800309 return 0;
310 }
311
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800312 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800313
Nguyen Anh Quynh11b05192014-01-22 11:04:25 +0800314 // reset previous prefix for X86
315 handle->prev_prefix = 0;
316
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800317 memset(insn_cache, 0, sizeof(insn_cache));
318
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800319 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600320 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800321 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800322
323 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
324 if (r) {
325 SStream ss;
326 SStream_Init(&ss);
327
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800328 // relative branches need to know the address & size of current insn
329 mci.insn_size = insn_size;
330 mci.address = offset;
331
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800332 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800333 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800334 mci.flat_insn.address = offset;
335 mci.flat_insn.size = insn_size;
336 // allocate memory for @detail pointer
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800337 insn_cache[f].detail = cs_mem_calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800338 }
339
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800340 handle->printer(&mci, &ss, handle->printer_info);
341
Joxean114df0e2013-12-04 07:11:32 +0100342 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800343
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800344 if (!handle->check_combine || !handle->check_combine(handle, &insn_cache[f])) {
345 f++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800346
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800347 if (f == ARR_SIZE(insn_cache)) {
348 // resize total to contain newly disasm insns
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800349 total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800350 void *tmp = cs_mem_realloc(total, total_size);
351 if (tmp == NULL) { // insufficient memory
352 cs_mem_free(total);
353 handle->errnum = CS_ERR_MEM;
354 return 0;
355 }
356
357 total = tmp;
358 memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache));
359 // reset f back to 0
360 f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800361 }
362
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800363 c++;
364 } else {
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800365 // combine this instruction with previous prefix "instruction"
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800366 cs_insn *prev = get_prev_insn(insn_cache, f, total, total_size);
367 handle->combine(handle, &insn_cache[f], prev);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800368 }
369
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800370 buffer += insn_size;
371 size -= insn_size;
372 offset += insn_size;
373
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800374 if (count > 0) {
375 // x86 hacky
376 if (!handle->prev_prefix) {
377 if (c == count)
378 break;
379 } else {
380 // only combine 1 prefix with regular instruction
Nguyen Anh Quynh8ce50e42014-01-27 18:15:28 +0800381 if (c == count + 1) {
382 // the last insn is redundant
383 c--;
384 f--;
385 // free allocated detail pointer of the last redundant instruction
386 if (handle->detail)
387 cs_mem_free(insn_cache[f].detail);
388
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800389 break;
Nguyen Anh Quynh8ce50e42014-01-27 18:15:28 +0800390 }
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800391 }
392 }
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800393 } else {
394 // encounter a broken instruction
395 // XXX: TODO: JOXEAN continue here
396 break;
397 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800398 }
399
400 if (f) {
401 // resize total to contain newly disasm insns
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800402 void *tmp = cs_mem_realloc(total, total_size + f * sizeof(insn_cache[0]));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800403 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800404 cs_mem_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800405 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800406 return 0;
407 }
408
409 total = tmp;
410 memcpy(total + total_size, insn_cache, f * sizeof(insn_cache[0]));
411 }
412
413 *insn = total;
414
415 return c;
416}
417
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800418void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800419{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800420 size_t i;
421
422 // free all detail pointers
423 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800424 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800425
426 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800427 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800428}
429
430// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100431const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800432{
433 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
434
435 if (!handle || handle->reg_name == NULL) {
436 return NULL;
437 }
438
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800439 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800440}
441
pancakef0e4eed2013-12-11 22:14:42 +0100442const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800443{
444 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
445
446 if (!handle || handle->insn_name == NULL) {
447 return NULL;
448 }
449
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800450 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800451}
452
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800453static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800454{
455 int i;
456
457 for (i = 0; i < max; i++) {
458 if (arr[i] == id)
459 return true;
460 }
461
462 return false;
463}
464
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800465bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800466{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800467 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800468 return false;
469
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800470 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800471 if (!handle->detail) {
472 handle->errnum = CS_ERR_DETAIL;
473 return false;
474 }
475
476 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800477}
478
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800479bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800480{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800481 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800482 return false;
483
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800484 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800485 if (!handle->detail) {
486 handle->errnum = CS_ERR_DETAIL;
487 return false;
488 }
489
490 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800491}
492
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800493bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800494{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800495 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800496 return false;
497
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800498 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800499 if (!handle->detail) {
500 handle->errnum = CS_ERR_DETAIL;
501 return false;
502 }
503
504 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800505}
506
507int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
508{
509 if (!ud)
510 return -1;
511
512 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800513 if (!handle->detail) {
514 handle->errnum = CS_ERR_DETAIL;
515 return -1;
516 }
517
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800518 unsigned int count = 0, i;
519
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800520 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800521
522 switch (handle->arch) {
523 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800524 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800525 return -1;
526 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800527 for (i = 0; i < insn->detail->arm.op_count; i++)
528 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800529 count++;
530 break;
531 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800532 for (i = 0; i < insn->detail->arm64.op_count; i++)
533 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800534 count++;
535 break;
536 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800537 for (i = 0; i < insn->detail->x86.op_count; i++)
538 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800539 count++;
540 break;
541 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800542 for (i = 0; i < insn->detail->mips.op_count; i++)
543 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800544 count++;
545 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800546 case CS_ARCH_PPC:
547 for (i = 0; i < insn->detail->ppc.op_count; i++)
548 if (insn->detail->ppc.operands[i].type == op_type)
549 count++;
550 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800551 }
552
553 return count;
554}
555
556int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
557 unsigned int post)
558{
559 if (!ud)
560 return -1;
561
562 cs_struct *handle = (cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800563 if (!handle->detail) {
564 handle->errnum = CS_ERR_DETAIL;
565 return -1;
566 }
567
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800568 unsigned int count = 0, i;
569
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800570 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800571
572 switch (handle->arch) {
573 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800574 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800575 return -1;
576 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800577 for (i = 0; i < insn->detail->arm.op_count; i++) {
578 if (insn->detail->arm.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800579 count++;
580 if (count == post)
581 return i;
582 }
583 break;
584 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800585 for (i = 0; i < insn->detail->arm64.op_count; i++) {
586 if (insn->detail->arm64.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800587 count++;
588 if (count == post)
589 return i;
590 }
591 break;
592 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800593 for (i = 0; i < insn->detail->x86.op_count; i++) {
594 if (insn->detail->x86.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800595 count++;
596 if (count == post)
597 return i;
598 }
599 break;
600 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800601 for (i = 0; i < insn->detail->mips.op_count; i++) {
602 if (insn->detail->mips.operands[i].type == op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800603 count++;
604 if (count == post)
605 return i;
606 }
607 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800608 case CS_ARCH_PPC:
609 for (i = 0; i < insn->detail->ppc.op_count; i++) {
610 if (insn->detail->ppc.operands[i].type == op_type)
611 count++;
612 if (count == post)
613 return i;
614 }
615 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800616 }
617
618 return -1;
619}