blob: 96dd91d3ed6a6aa1abb5a518b1e12f3ae9f37ca0 [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 Quynhb2870e42014-02-22 23:41:16 +080077bool cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080078{
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +080079 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080080 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
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +080084 if (query < CS_ARCH_MAX)
85 return all_arch & (1 << query);
86
87 if (query == CS_SUPPORT_DIET) {
88#ifdef CAPSTONE_DIET
89 return true;
90#else
91 return false;
92#endif
93 }
94
95 // unsupported query
96 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080097}
98
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080099cs_err cs_errno(csh handle)
100{
101 if (!handle)
102 return CS_ERR_CSH;
103
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800104 struct cs_struct *ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800105
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800106 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800107}
108
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800109const char *cs_strerror(cs_err code)
110{
111 switch(code) {
112 default:
113 return "Unknown error code";
114 case CS_ERR_OK:
115 return "OK (CS_ERR_OK)";
116 case CS_ERR_MEM:
117 return "Out of memory (CS_ERR_MEM)";
118 case CS_ERR_ARCH:
119 return "Invalid architecture (CS_ERR_ARCH)";
120 case CS_ERR_HANDLE:
121 return "Invalid handle (CS_ERR_HANDLE)";
122 case CS_ERR_CSH:
123 return "Invalid csh (CS_ERR_CSH)";
124 case CS_ERR_MODE:
125 return "Invalid mode (CS_ERR_MODE)";
126 case CS_ERR_OPTION:
127 return "Invalid option (CS_ERR_OPTION)";
128 case CS_ERR_DETAIL:
129 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800130 case CS_ERR_MEMSETUP:
131 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800132 case CS_ERR_VERSION:
133 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800134 }
135}
136
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800137cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
138{
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800139 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800140 // Error: before cs_open(), dynamic memory management must be initialized
141 // with cs_option(CS_OPT_MEM)
142 return CS_ERR_MEMSETUP;
143
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800144 archs_enable();
145
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800146 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800147 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800148
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800149 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800150 if (!ud) {
151 // memory insufficient
152 return CS_ERR_MEM;
153 }
154
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800155 ud->errnum = CS_ERR_OK;
156 ud->arch = arch;
157 ud->mode = mode;
158 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800159 // by default, do not break instruction into details
160 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800161
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800162 cs_err err = arch_init[ud->arch](ud);
163 if (err) {
164 cs_mem_free(ud);
165 *handle = 0;
166 return err;
167 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800168
169 *handle = (uintptr_t)ud;
170
171 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800172 } else {
173 *handle = 0;
174 return CS_ERR_ARCH;
175 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800176}
177
178cs_err cs_close(csh handle)
179{
180 if (!handle)
181 return CS_ERR_CSH;
182
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800183 struct cs_struct *ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800184
185 switch (ud->arch) {
186 case CS_ARCH_X86:
187 break;
188 case CS_ARCH_ARM:
189 case CS_ARCH_MIPS:
190 case CS_ARCH_ARM64:
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800191 case CS_ARCH_PPC:
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800192 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800193 break;
194 default: // unsupported architecture
195 return CS_ERR_HANDLE;
196 }
197
Nguyen Anh Quynh9fac5122014-01-07 10:56:04 +0800198 // arch_destroy[ud->arch](ud);
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800199
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800200 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800201 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800202 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800203
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800204 return CS_ERR_OK;
205}
206
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800207#define MIN(x, y) ((x) < (y) ? (x) : (y))
208
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800209// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800210static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800211 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800212{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800213 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800214 // avoiding copy insn->detail
215 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800216
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800217 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
218 // copy from @regs_read until @arm
219 memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read),
220 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
221 // then copy from @arm until end
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800222 memcpy((void *)((uintptr_t)(insn->detail) + offsetof(cs_detail, arm)),
223 (void *)((uintptr_t)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm)),
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800224 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800225 } else {
226 insn->address = mci->address;
Alex Ionescu46018db2014-01-22 09:45:00 -0800227 insn->size = (uint16_t)mci->insn_size;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800228 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800229
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800230 // fill the instruction bytes
231 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
232
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800233 // map internal instruction opcode to public insn ID
234 if (handle->insn_id)
Nguyen Anh Quynh1acfd0b2014-01-06 10:56:59 +0800235 handle->insn_id(handle, insn, MCInst_getOpcode(mci));
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800236
237 // alias instruction might have ID saved in OpcodePub
238 if (MCInst_getOpcodePub(mci))
239 insn->id = MCInst_getOpcodePub(mci);
240
241 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800242 if (postprinter)
243 postprinter((csh)handle, insn, buffer);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800244
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800245#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800246 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800247 // find first space or tab
248 char *sp = buffer;
249 for (sp = buffer; *sp; sp++)
250 if (*sp == ' '||*sp == '\t')
251 break;
252 if (*sp) {
253 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800254 // find the next non-space char
255 sp++;
256 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
257 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800258 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
259 } else
260 insn->op_str[0] = '\0';
261
262 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
263 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800264#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800265}
266
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800267cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800268{
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800269 // cs_option() can be called with NULL handle just for CS_OPT_MEM
270 // This is supposed to be executed before all other APIs (even cs_open())
271 if (type == CS_OPT_MEM) {
272 cs_opt_mem *mem = (cs_opt_mem *)value;
273
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800274 cs_mem_malloc = mem->malloc;
275 cs_mem_calloc = mem->calloc;
276 cs_mem_realloc = mem->realloc;
277 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800278 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800279
280 return CS_ERR_OK;
281 }
282
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800283 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600284 if (!handle)
285 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800286
danghvu0b6ea042013-12-19 23:07:26 -0600287 if (type == CS_OPT_DETAIL) {
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800288 handle->detail = value;
289 return CS_ERR_OK;
290 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800291
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800292 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800293}
294
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800295// get previous instruction, which can be in the cache, or in total buffer
296static cs_insn *get_prev_insn(cs_insn *cache, unsigned int f, void *total, size_t total_size)
297{
298 if (f == 0) {
299 if (total == NULL)
300 return NULL;
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800301 // get the trailing insn from total buffer, which is at
302 // the end of the latest cache trunk
Alex Ionescu46018db2014-01-22 09:45:00 -0800303 return (cs_insn *)((void*)((uintptr_t)total + total_size - sizeof(cs_insn)));
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800304 } else
305 return &cache[f - 1];
306}
307
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800308// dynamicly allocate memory to contain disasm insn
309// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800310size_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 +0800311{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800312 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800313 MCInst mci;
314 uint16_t insn_size;
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800315 size_t c = 0;
316 unsigned int f = 0;
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800317 cs_insn insn_cache[INSN_CACHE_SIZE];
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800318 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800319 size_t total_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800320
321 if (!handle) {
322 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800323 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800324 return 0;
325 }
326
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800327 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800328
Nguyen Anh Quynh11b05192014-01-22 11:04:25 +0800329 // reset previous prefix for X86
330 handle->prev_prefix = 0;
331
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800332 memset(insn_cache, 0, sizeof(insn_cache));
333
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800334 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600335 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800336 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800337
338 bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
339 if (r) {
340 SStream ss;
341 SStream_Init(&ss);
342
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800343 // relative branches need to know the address & size of current insn
344 mci.insn_size = insn_size;
345 mci.address = offset;
346
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800347 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800348 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800349 mci.flat_insn.address = offset;
350 mci.flat_insn.size = insn_size;
351 // allocate memory for @detail pointer
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800352 insn_cache[f].detail = cs_mem_calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800353 }
354
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800355 handle->printer(&mci, &ss, handle->printer_info);
356
Joxean114df0e2013-12-04 07:11:32 +0100357 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800358
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800359 if (!handle->check_combine || !handle->check_combine(handle, &insn_cache[f])) {
360 f++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800361
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800362 if (f == ARR_SIZE(insn_cache)) {
363 // resize total to contain newly disasm insns
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800364 total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800365 void *tmp = cs_mem_realloc(total, total_size);
366 if (tmp == NULL) { // insufficient memory
367 cs_mem_free(total);
368 handle->errnum = CS_ERR_MEM;
369 return 0;
370 }
371
372 total = tmp;
Alex Ionescu46018db2014-01-22 09:45:00 -0800373 memcpy((void*)((uintptr_t)total + total_size - sizeof(insn_cache)), insn_cache, sizeof(insn_cache));
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800374
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800375 // reset f back to 0
376 f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800377 }
378
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800379 c++;
380 } else {
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800381 // combine this instruction with previous prefix "instruction"
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800382 cs_insn *prev = get_prev_insn(insn_cache, f, total, total_size);
383 handle->combine(handle, &insn_cache[f], prev);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800384 }
385
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800386 buffer += insn_size;
387 size -= insn_size;
388 offset += insn_size;
389
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800390 if (count > 0) {
391 // x86 hacky
392 if (!handle->prev_prefix) {
393 if (c == count)
394 break;
395 } else {
396 // only combine 1 prefix with regular instruction
Nguyen Anh Quynh8ce50e42014-01-27 18:15:28 +0800397 if (c == count + 1) {
398 // the last insn is redundant
399 c--;
400 f--;
401 // free allocated detail pointer of the last redundant instruction
402 if (handle->detail)
403 cs_mem_free(insn_cache[f].detail);
404
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800405 break;
Nguyen Anh Quynh8ce50e42014-01-27 18:15:28 +0800406 }
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800407 }
408 }
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800409 } else {
410 // encounter a broken instruction
411 // XXX: TODO: JOXEAN continue here
412 break;
413 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800414 }
415
416 if (f) {
417 // resize total to contain newly disasm insns
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800418 void *tmp = cs_mem_realloc(total, total_size + f * sizeof(insn_cache[0]));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800419 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800420 cs_mem_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800421 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800422 return 0;
423 }
424
425 total = tmp;
Alex Ionescu46018db2014-01-22 09:45:00 -0800426 memcpy((void*)((uintptr_t)total + total_size), insn_cache, f * sizeof(insn_cache[0]));
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800427
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800428 }
429
430 *insn = total;
431
432 return c;
433}
434
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800435void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800436{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800437 size_t i;
438
439 // free all detail pointers
440 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800441 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800442
443 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800444 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800445}
446
447// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100448const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800449{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800450 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800451
452 if (!handle || handle->reg_name == NULL) {
453 return NULL;
454 }
455
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800456 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800457}
458
pancakef0e4eed2013-12-11 22:14:42 +0100459const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800460{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800461 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800462
463 if (!handle || handle->insn_name == NULL) {
464 return NULL;
465 }
466
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800467 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800468}
469
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800470static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800471{
472 int i;
473
474 for (i = 0; i < max; i++) {
475 if (arr[i] == id)
476 return true;
477 }
478
479 return false;
480}
481
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800482bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800483{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800484 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800485 return false;
486
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800487 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800488 if (!handle->detail) {
489 handle->errnum = CS_ERR_DETAIL;
490 return false;
491 }
492
493 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800494}
495
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800496bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800497{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800498 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800499 return false;
500
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800501 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800502 if (!handle->detail) {
503 handle->errnum = CS_ERR_DETAIL;
504 return false;
505 }
506
507 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800508}
509
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800510bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800511{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800512 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800513 return false;
514
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800515 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800516 if (!handle->detail) {
517 handle->errnum = CS_ERR_DETAIL;
518 return false;
519 }
520
521 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800522}
523
524int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
525{
526 if (!ud)
527 return -1;
528
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800529 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800530 if (!handle->detail) {
531 handle->errnum = CS_ERR_DETAIL;
532 return -1;
533 }
534
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800535 unsigned int count = 0, i;
536
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800537 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800538
539 switch (handle->arch) {
540 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800541 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800542 return -1;
543 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800544 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800545 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800546 count++;
547 break;
548 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800549 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800550 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800551 count++;
552 break;
553 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800554 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800555 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800556 count++;
557 break;
558 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800559 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800560 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800561 count++;
562 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800563 case CS_ARCH_PPC:
564 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800565 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800566 count++;
567 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800568 }
569
570 return count;
571}
572
573int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
574 unsigned int post)
575{
576 if (!ud)
577 return -1;
578
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800579 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800580 if (!handle->detail) {
581 handle->errnum = CS_ERR_DETAIL;
582 return -1;
583 }
584
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800585 unsigned int count = 0, i;
586
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800587 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800588
589 switch (handle->arch) {
590 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800591 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800592 return -1;
593 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800594 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800595 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800596 count++;
597 if (count == post)
598 return i;
599 }
600 break;
601 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800602 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800603 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800604 count++;
605 if (count == post)
606 return i;
607 }
608 break;
609 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800610 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800611 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800612 count++;
613 if (count == post)
614 return i;
615 }
616 break;
617 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800618 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800619 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800620 count++;
621 if (count == post)
622 return i;
623 }
624 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800625 case CS_ARCH_PPC:
626 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800627 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800628 count++;
629 if (count == post)
630 return i;
631 }
632 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800633 }
634
635 return -1;
636}