blob: 9d7db5071cb63b09e64e1840c1778264938a2988 [file] [log] [blame]
Nguyen Anh Quynh6023ef72014-04-29 11:21:04 +08001/* Capstone Disassembly Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
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
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08006#include <stddef.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08007#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <capstone.h>
11
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080012#include "utils.h"
Nguyen Anh Quynhc7404072014-01-05 11:35:47 +080013#include "MCRegisterInfo.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080014
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080015#ifdef USE_SYS_DYN_MEM
Nguyen Anh Quynh472a4a42014-03-06 09:13:04 +080016#define INSN_CACHE_SIZE 32
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080017#else
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080018// reduce stack variable size for kernel/firmware
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080019#define INSN_CACHE_SIZE 8
20#endif
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +080021
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080022// default SKIPDATA mnemonic
Nguyen Anh Quynhc75a9092014-04-10 10:26:49 +080023#define SKIPDATA_MNEM ".byte"
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080024
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080025cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080026cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
27void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080028
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080029extern void ARM_enable(void);
30extern void AArch64_enable(void);
31extern void Mips_enable(void);
32extern void X86_enable(void);
33extern void PPC_enable(void);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080034extern void Sparc_enable(void);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080035extern void SystemZ_enable(void);
danghvu701b8502014-01-09 11:06:44 +070036
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080037static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080038{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080039 static bool initialized = false;
40
41 if (initialized)
42 return;
43
danghvub33bd2c2014-01-09 12:22:56 +070044#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080045 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070046#endif
47#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080048 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070049#endif
50#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080051 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070052#endif
danghvub33bd2c2014-01-09 12:22:56 +070053#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080054 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070055#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080056#ifdef CAPSTONE_HAS_SPARC
57 Sparc_enable();
58#endif
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080059#ifdef CAPSTONE_HAS_SYSZ
60 SystemZ_enable();
61#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080062#ifdef CAPSTONE_HAS_X86
63 X86_enable();
64#endif
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080065
66 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +070067}
68
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080069unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080070
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080071#ifdef USE_SYS_DYN_MEM
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080072cs_malloc_t cs_mem_malloc = malloc;
73cs_calloc_t cs_mem_calloc = calloc;
74cs_realloc_t cs_mem_realloc = realloc;
75cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080076cs_vsnprintf_t cs_vsnprintf = vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080077#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080078cs_malloc_t cs_mem_malloc = NULL;
79cs_calloc_t cs_mem_calloc = NULL;
80cs_realloc_t cs_mem_realloc = NULL;
81cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080082cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080083#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080084
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080085unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080086{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +080087 archs_enable();
88
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080089 if (major != NULL && minor != NULL) {
90 *major = CS_API_MAJOR;
91 *minor = CS_API_MINOR;
92 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080093
94 return (CS_API_MAJOR << 8) + CS_API_MINOR;
95}
96
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +080097bool cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080098{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +080099 archs_enable();
100
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800101 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800102 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800103 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800104 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
105 (1 << CS_ARCH_SYSZ));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800106
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800107 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800108 return all_arch & (1 << query);
109
110 if (query == CS_SUPPORT_DIET) {
111#ifdef CAPSTONE_DIET
112 return true;
113#else
114 return false;
115#endif
116 }
117
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800118 if (query == CS_SUPPORT_X86_REDUCE) {
119#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800120 return true;
121#else
122 return false;
123#endif
124 }
125
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800126 // unsupported query
127 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800128}
129
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800130cs_err cs_errno(csh handle)
131{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800132 struct cs_struct *ud = NULL;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800133 if (!handle)
134 return CS_ERR_CSH;
135
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100136 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800137
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800138 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800139}
140
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800141const char *cs_strerror(cs_err code)
142{
143 switch(code) {
144 default:
145 return "Unknown error code";
146 case CS_ERR_OK:
147 return "OK (CS_ERR_OK)";
148 case CS_ERR_MEM:
149 return "Out of memory (CS_ERR_MEM)";
150 case CS_ERR_ARCH:
151 return "Invalid architecture (CS_ERR_ARCH)";
152 case CS_ERR_HANDLE:
153 return "Invalid handle (CS_ERR_HANDLE)";
154 case CS_ERR_CSH:
155 return "Invalid csh (CS_ERR_CSH)";
156 case CS_ERR_MODE:
157 return "Invalid mode (CS_ERR_MODE)";
158 case CS_ERR_OPTION:
159 return "Invalid option (CS_ERR_OPTION)";
160 case CS_ERR_DETAIL:
161 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800162 case CS_ERR_MEMSETUP:
163 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800164 case CS_ERR_VERSION:
165 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800166 case CS_ERR_DIET:
167 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800168 case CS_ERR_SKIPDATA:
169 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800170 }
171}
172
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800173cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
174{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800175 cs_err err;
176 struct cs_struct *ud = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800177 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800178 // Error: before cs_open(), dynamic memory management must be initialized
179 // with cs_option(CS_OPT_MEM)
180 return CS_ERR_MEMSETUP;
181
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800182 archs_enable();
183
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800184 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800185 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800186 if (!ud) {
187 // memory insufficient
188 return CS_ERR_MEM;
189 }
190
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800191 ud->errnum = CS_ERR_OK;
192 ud->arch = arch;
193 ud->mode = mode;
194 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800195 // by default, do not break instruction into details
196 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800197
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800198 // default skipdata setup
199 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
200
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100201 err = arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800202 if (err) {
203 cs_mem_free(ud);
204 *handle = 0;
205 return err;
206 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800207
208 *handle = (uintptr_t)ud;
209
210 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800211 } else {
212 *handle = 0;
213 return CS_ERR_ARCH;
214 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800215}
216
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800217cs_err cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800218{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800219 struct cs_struct *ud = NULL;
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800220 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800221 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800222 return CS_ERR_CSH;
223
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100224 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800225
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800226 if (ud->printer_info)
227 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800228
Nguyen Anh Quynh9fac5122014-01-07 10:56:04 +0800229 // arch_destroy[ud->arch](ud);
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800230
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800231 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800232 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800233 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800234
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800235 // invalidate this handle by ZERO out its value.
236 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800237 *handle = 0;
238
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800239 return CS_ERR_OK;
240}
241
242// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800243static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800244 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800245{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800246 char *sp = NULL;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800247 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800248 // avoiding copy insn->detail
249 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800250
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800251 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
252 // copy from @regs_read until @arm
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100253 memcpy(insn->detail, (void *)((uintptr_t)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read)),
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800254 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
255 // then copy from @arm until end
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800256 memcpy((void *)((uintptr_t)(insn->detail) + offsetof(cs_detail, arm)),
257 (void *)((uintptr_t)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm)),
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800258 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800259 } else {
260 insn->address = mci->address;
Alex Ionescu46018db2014-01-22 09:45:00 -0800261 insn->size = (uint16_t)mci->insn_size;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800262 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800263
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800264 // fill the instruction bytes
265 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
266
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800267 // map internal instruction opcode to public insn ID
268 if (handle->insn_id)
Nguyen Anh Quynh1acfd0b2014-01-06 10:56:59 +0800269 handle->insn_id(handle, insn, MCInst_getOpcode(mci));
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800270
271 // alias instruction might have ID saved in OpcodePub
272 if (MCInst_getOpcodePub(mci))
273 insn->id = MCInst_getOpcodePub(mci);
274
275 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800276 if (postprinter)
277 postprinter((csh)handle, insn, buffer);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800278
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800279#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800280 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800281 // find first space or tab
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100282 sp = buffer;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800283 for (sp = buffer; *sp; sp++) {
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800284 if (*sp == ' '||*sp == '\t')
285 break;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800286 if (*sp == '|')
287 *sp = ' ';
288 }
289
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800290 if (*sp) {
291 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800292 // find the next non-space char
293 sp++;
294 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
295 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800296 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
297 } else
298 insn->op_str[0] = '\0';
299
Nguyen Anh Quynh1dbc9592014-05-07 14:14:07 +0800300 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800301 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800302#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800303}
304
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800305// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800306// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800307static uint8_t skipdata_size(cs_struct *handle)
308{
309 switch(handle->arch) {
310 default:
311 // should never reach
312 return -1;
313 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800314 // skip 2 bytes on Thumb mode.
315 if (handle->mode & CS_MODE_THUMB)
316 return 2;
317 // otherwise, skip 4 bytes
318 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800319 case CS_ARCH_ARM64:
320 case CS_ARCH_MIPS:
321 case CS_ARCH_PPC:
322 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800323 // skip 4 bytes
324 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800325 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800326 // SystemZ instruction's length can be 2, 4 or 6 bytes,
327 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800328 return 2;
329 case CS_ARCH_X86:
330 // X86 has no restriction on instruction alignment
331 return 1;
332 }
333}
334
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800335cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800336{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800337 struct cs_struct *handle = NULL;
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800338 archs_enable();
339
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800340 // cs_option() can be called with NULL handle just for CS_OPT_MEM
341 // This is supposed to be executed before all other APIs (even cs_open())
342 if (type == CS_OPT_MEM) {
343 cs_opt_mem *mem = (cs_opt_mem *)value;
344
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800345 cs_mem_malloc = mem->malloc;
346 cs_mem_calloc = mem->calloc;
347 cs_mem_realloc = mem->realloc;
348 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800349 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800350
351 return CS_ERR_OK;
352 }
353
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100354 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600355 if (!handle)
356 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800357
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800358 switch(type) {
359 default:
360 break;
361 case CS_OPT_DETAIL:
362 handle->detail = value;
363 return CS_ERR_OK;
364 case CS_OPT_SKIPDATA:
365 handle->skipdata = (value == CS_OPT_ON);
366 if (handle->skipdata) {
367 if (handle->skipdata_size == 0) {
368 // set the default skipdata size
369 handle->skipdata_size = skipdata_size(handle);
370 }
371 }
372 return CS_ERR_OK;
373 case CS_OPT_SKIPDATA_SETUP:
374 if (value)
375 handle->skipdata_setup = *((cs_opt_skipdata *)value);
376 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800377 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800378
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800379 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800380}
381
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800382// generate @op_str for data instruction of SKIPDATA
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800383static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
384{
385 char *p = opstr;
386 int len;
387 size_t i;
388
389 if (!size) {
390 opstr[0] = '\0';
391 return;
392 }
393
394 len = sprintf(p, "0x%02x", buffer[0]);
395 p+= len;
396
397 for(i = 1; i < size; i++) {
398 len = sprintf(p, ", 0x%02x", buffer[i]);
399 p+= len;
400 }
401}
402
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800403// dynamicly allocate memory to contain disasm insn
404// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800405size_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 +0800406{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800407 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800408 MCInst mci;
409 uint16_t insn_size;
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800410 size_t c = 0;
411 unsigned int f = 0;
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800412 cs_insn insn_cache[INSN_CACHE_SIZE];
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800413 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800414 size_t total_size = 0;
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800415 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800416 void *tmp;
417 size_t skipdata_bytes;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800418 uint64_t offset_org;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800419
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800420 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800421 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800422 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800423 return 0;
424 }
425
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800426 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800427
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800428 memset(insn_cache, 0, sizeof(insn_cache));
429
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800430 // save the original offset for SKIPDATA
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800431 offset_org = offset;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800432
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800433 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600434 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800435 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800436
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800437 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800438 if (r) {
439 SStream ss;
440 SStream_Init(&ss);
441
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800442 // relative branches need to know the address & size of current insn
443 mci.insn_size = insn_size;
444 mci.address = offset;
445
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800446 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800447 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800448 mci.flat_insn.address = offset;
449 mci.flat_insn.size = insn_size;
450 // allocate memory for @detail pointer
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800451 insn_cache[f].detail = cs_mem_calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800452 }
453
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800454 handle->printer(&mci, &ss, handle->printer_info);
455
Joxean114df0e2013-12-04 07:11:32 +0100456 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800457
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800458 f++;
459 if (f == ARR_SIZE(insn_cache)) {
460 // resize total to contain newly disasm insns
461 total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
462 tmp = cs_mem_realloc(total, total_size);
463 if (tmp == NULL) { // insufficient memory
464 cs_mem_free(total);
465 handle->errnum = CS_ERR_MEM;
466 return 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800467 }
468
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800469 total = tmp;
470 memcpy((void*)((uintptr_t)total + total_size - sizeof(insn_cache)), insn_cache, sizeof(insn_cache));
471
472 // reset f back to 0
473 f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800474 }
475
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800476 c++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800477 buffer += insn_size;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800478
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800479 size -= insn_size;
480 offset += insn_size;
481
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800482 if (count > 0 && c == count)
483 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800484 } else {
485 // encounter a broken instruction
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800486 // if there is no request to skip data, or remaining data is too small,
487 // then bail out
488 if (!handle->skipdata || handle->skipdata_size > size)
489 break;
490
491 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh42288ac2014-04-14 14:53:13 +0800492 skipdata_bytes = handle->skipdata_setup.callback(buffer, offset - offset_org,
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800493 handle->skipdata_setup.user_data);
494 if (skipdata_bytes > size)
495 // remaining data is not enough
496 break;
497
498 if (!skipdata_bytes)
499 // user requested not to skip data, so bail out
500 break;
501 } else
502 skipdata_bytes = handle->skipdata_size;
503
504 // we have to skip some amount of data, depending on arch & mode
505 insn_cache[f].id = 0; // invalid ID for this "data" instruction
506 insn_cache[f].address = offset;
507 insn_cache[f].size = skipdata_bytes;
508 memcpy(insn_cache[f].bytes, buffer, skipdata_bytes);
509 strncpy(insn_cache[f].mnemonic, handle->skipdata_setup.mnemonic,
510 sizeof(insn_cache[f].mnemonic) - 1);
511 skipdata_opstr(insn_cache[f].op_str, buffer, skipdata_bytes);
512 insn_cache[f].detail = NULL;
513
514 f++;
515 if (f == ARR_SIZE(insn_cache)) {
516 // resize total to contain newly disasm insns
517
518 total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
519 tmp = cs_mem_realloc(total, total_size);
520 if (tmp == NULL) { // insufficient memory
521 cs_mem_free(total);
522 handle->errnum = CS_ERR_MEM;
523 return 0;
524 }
525
526 total = tmp;
527 memcpy((void*)((uintptr_t)total + total_size - sizeof(insn_cache)), insn_cache, sizeof(insn_cache));
528
529 // reset f back to 0
530 f = 0;
531 }
532
533 buffer += skipdata_bytes;
534 size -= skipdata_bytes;
535 offset += skipdata_bytes;
536 c++;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800537 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800538 }
539
540 if (f) {
541 // resize total to contain newly disasm insns
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800542 void *tmp = cs_mem_realloc(total, total_size + f * sizeof(insn_cache[0]));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800543 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800544 cs_mem_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800545 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800546 return 0;
547 }
548
549 total = tmp;
Alex Ionescu46018db2014-01-22 09:45:00 -0800550 memcpy((void*)((uintptr_t)total + total_size), insn_cache, f * sizeof(insn_cache[0]));
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800551
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800552 }
553
554 *insn = total;
555
556 return c;
557}
558
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800559void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800560{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800561 size_t i;
562
563 // free all detail pointers
564 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800565 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800566
567 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800568 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800569}
570
571// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100572const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800573{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800574 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800575
576 if (!handle || handle->reg_name == NULL) {
577 return NULL;
578 }
579
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800580 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800581}
582
pancakef0e4eed2013-12-11 22:14:42 +0100583const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800584{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800585 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800586
587 if (!handle || handle->insn_name == NULL) {
588 return NULL;
589 }
590
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800591 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800592}
593
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800594static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800595{
596 int i;
597
598 for (i = 0; i < max; i++) {
599 if (arr[i] == id)
600 return true;
601 }
602
603 return false;
604}
605
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800606bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800607{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800608 struct cs_struct *handle = NULL;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800609 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800610 return false;
611
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100612 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200613
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800614 if (!handle->detail) {
615 handle->errnum = CS_ERR_DETAIL;
616 return false;
617 }
618
Giovanni Condello95657e02014-05-07 17:31:27 +0200619 if(!insn->id) {
620 handle->errnum = CS_ERR_SKIPDATA;
621 return false;
622 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200623
Giovanni Condello95657e02014-05-07 17:31:27 +0200624 if(!insn->detail) {
625 handle->errnum = CS_ERR_DETAIL;
626 return false;
627 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200628
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800629 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800630}
631
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800632bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800633{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800634 struct cs_struct *handle = NULL;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800635 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800636 return false;
637
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100638 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200639
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800640 if (!handle->detail) {
641 handle->errnum = CS_ERR_DETAIL;
642 return false;
643 }
644
Giovanni Condello95657e02014-05-07 17:31:27 +0200645 if(!insn->id) {
646 handle->errnum = CS_ERR_SKIPDATA;
647 return false;
648 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200649
Giovanni Condello95657e02014-05-07 17:31:27 +0200650 if(!insn->detail) {
651 handle->errnum = CS_ERR_DETAIL;
652 return false;
653 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200654
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800655 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800656}
657
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800658bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800659{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800660 struct cs_struct *handle = NULL;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800661 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800662 return false;
663
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100664 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +0200665
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800666 if (!handle->detail) {
667 handle->errnum = CS_ERR_DETAIL;
668 return false;
669 }
670
Giovanni Condello95657e02014-05-07 17:31:27 +0200671 if(!insn->id) {
672 handle->errnum = CS_ERR_SKIPDATA;
673 return false;
674 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200675
Giovanni Condello95657e02014-05-07 17:31:27 +0200676 if(!insn->detail) {
677 handle->errnum = CS_ERR_DETAIL;
678 return false;
679 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200680
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800681 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800682}
683
684int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
685{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800686 struct cs_struct *handle = NULL;
687 unsigned int count = 0, i = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800688 if (!ud)
689 return -1;
690
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100691 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200692
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800693 if (!handle->detail) {
694 handle->errnum = CS_ERR_DETAIL;
695 return -1;
696 }
697
Giovanni Condello95657e02014-05-07 17:31:27 +0200698 if(!insn->id) {
699 handle->errnum = CS_ERR_SKIPDATA;
700 return -1;
701 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200702
Giovanni Condello95657e02014-05-07 17:31:27 +0200703 if(!insn->detail) {
704 handle->errnum = CS_ERR_DETAIL;
705 return -1;
706 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200707
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800708 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800709
710 switch (handle->arch) {
711 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800712 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800713 return -1;
714 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800715 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800716 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800717 count++;
718 break;
719 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800720 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800721 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800722 count++;
723 break;
724 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800725 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800726 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800727 count++;
728 break;
729 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800730 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800731 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800732 count++;
733 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800734 case CS_ARCH_PPC:
735 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800736 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800737 count++;
738 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800739 case CS_ARCH_SPARC:
740 for (i = 0; i < insn->detail->sparc.op_count; i++)
741 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
742 count++;
743 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800744 case CS_ARCH_SYSZ:
745 for (i = 0; i < insn->detail->sysz.op_count; i++)
746 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
747 count++;
748 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800749 }
750
751 return count;
752}
753
754int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
755 unsigned int post)
756{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800757 struct cs_struct *handle = NULL;
758 unsigned int count = 0, i = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800759 if (!ud)
760 return -1;
761
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100762 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200763
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800764 if (!handle->detail) {
765 handle->errnum = CS_ERR_DETAIL;
766 return -1;
767 }
768
Giovanni Condello95657e02014-05-07 17:31:27 +0200769 if(!insn->id) {
770 handle->errnum = CS_ERR_SKIPDATA;
771 return -1;
772 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200773
Giovanni Condello95657e02014-05-07 17:31:27 +0200774 if(!insn->detail) {
775 handle->errnum = CS_ERR_DETAIL;
776 return -1;
777 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200778
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800779 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800780
781 switch (handle->arch) {
782 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800783 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800784 return -1;
785 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800786 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800787 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800788 count++;
789 if (count == post)
790 return i;
791 }
792 break;
793 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800794 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800795 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800796 count++;
797 if (count == post)
798 return i;
799 }
800 break;
801 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800802 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800803 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800804 count++;
805 if (count == post)
806 return i;
807 }
808 break;
809 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800810 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800811 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800812 count++;
813 if (count == post)
814 return i;
815 }
816 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800817 case CS_ARCH_PPC:
818 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800819 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800820 count++;
821 if (count == post)
822 return i;
823 }
824 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800825 case CS_ARCH_SPARC:
826 for (i = 0; i < insn->detail->sparc.op_count; i++) {
827 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
828 count++;
829 if (count == post)
830 return i;
831 }
832 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800833 case CS_ARCH_SYSZ:
834 for (i = 0; i < insn->detail->sysz.op_count; i++) {
835 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
836 count++;
837 if (count == post)
838 return i;
839 }
840 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800841 }
842
843 return -1;
844}