blob: 35264f47e66a425d3b24a992f8f5c95ea08f4d96 [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
Axel 0vercl0k Souchet84fecf22014-05-10 09:49:29 +010015#ifdef CAPSTONE_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
Axel 0vercl0k Souchet84fecf22014-05-10 09:49:29 +010071#ifdef CAPSTONE_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 Quynhbb0744d2014-05-12 13:41:49 +0800132 struct cs_struct *ud;
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;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800176 struct cs_struct *ud;
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 Quynhbb0744d2014-05-12 13:41:49 +0800219 struct cs_struct *ud;
220
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800221 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800222 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800223 return CS_ERR_CSH;
224
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100225 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800226
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800227 if (ud->printer_info)
228 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800229
Nguyen Anh Quynh9fac5122014-01-07 10:56:04 +0800230 // arch_destroy[ud->arch](ud);
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800231
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800232 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800233 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800234 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800235
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800236 // invalidate this handle by ZERO out its value.
237 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800238 *handle = 0;
239
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800240 return CS_ERR_OK;
241}
242
243// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800244static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800245 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800246{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800247 char *sp;
248
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800249 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800250 // avoiding copy insn->detail
251 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800252
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800253 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
254 // copy from @regs_read until @arm
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100255 memcpy(insn->detail, (void *)((uintptr_t)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read)),
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800256 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
257 // then copy from @arm until end
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800258 memcpy((void *)((uintptr_t)(insn->detail) + offsetof(cs_detail, arm)),
259 (void *)((uintptr_t)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm)),
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800260 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800261 } else {
262 insn->address = mci->address;
Alex Ionescu46018db2014-01-22 09:45:00 -0800263 insn->size = (uint16_t)mci->insn_size;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800264 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800265
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800266 // fill the instruction bytes
267 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
268
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800269 // map internal instruction opcode to public insn ID
270 if (handle->insn_id)
Nguyen Anh Quynh1acfd0b2014-01-06 10:56:59 +0800271 handle->insn_id(handle, insn, MCInst_getOpcode(mci));
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800272
273 // alias instruction might have ID saved in OpcodePub
274 if (MCInst_getOpcodePub(mci))
275 insn->id = MCInst_getOpcodePub(mci);
276
277 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800278 if (postprinter)
279 postprinter((csh)handle, insn, buffer);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800280
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800281#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800282 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800283 // find first space or tab
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100284 sp = buffer;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800285 for (sp = buffer; *sp; sp++) {
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800286 if (*sp == ' '||*sp == '\t')
287 break;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800288 if (*sp == '|')
289 *sp = ' ';
290 }
291
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800292 if (*sp) {
293 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800294 // find the next non-space char
295 sp++;
296 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
297 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800298 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
299 } else
300 insn->op_str[0] = '\0';
301
Nguyen Anh Quynh1dbc9592014-05-07 14:14:07 +0800302 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800303 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800304#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800305}
306
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800307// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800308// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800309static uint8_t skipdata_size(cs_struct *handle)
310{
311 switch(handle->arch) {
312 default:
313 // should never reach
314 return -1;
315 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800316 // skip 2 bytes on Thumb mode.
317 if (handle->mode & CS_MODE_THUMB)
318 return 2;
319 // otherwise, skip 4 bytes
320 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800321 case CS_ARCH_ARM64:
322 case CS_ARCH_MIPS:
323 case CS_ARCH_PPC:
324 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800325 // skip 4 bytes
326 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800327 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800328 // SystemZ instruction's length can be 2, 4 or 6 bytes,
329 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800330 return 2;
331 case CS_ARCH_X86:
332 // X86 has no restriction on instruction alignment
333 return 1;
334 }
335}
336
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800337cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800338{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800339 struct cs_struct *handle;
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800340 archs_enable();
341
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800342 // cs_option() can be called with NULL handle just for CS_OPT_MEM
343 // This is supposed to be executed before all other APIs (even cs_open())
344 if (type == CS_OPT_MEM) {
345 cs_opt_mem *mem = (cs_opt_mem *)value;
346
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800347 cs_mem_malloc = mem->malloc;
348 cs_mem_calloc = mem->calloc;
349 cs_mem_realloc = mem->realloc;
350 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800351 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800352
353 return CS_ERR_OK;
354 }
355
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100356 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600357 if (!handle)
358 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800359
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800360 switch(type) {
361 default:
362 break;
363 case CS_OPT_DETAIL:
364 handle->detail = value;
365 return CS_ERR_OK;
366 case CS_OPT_SKIPDATA:
367 handle->skipdata = (value == CS_OPT_ON);
368 if (handle->skipdata) {
369 if (handle->skipdata_size == 0) {
370 // set the default skipdata size
371 handle->skipdata_size = skipdata_size(handle);
372 }
373 }
374 return CS_ERR_OK;
375 case CS_OPT_SKIPDATA_SETUP:
376 if (value)
377 handle->skipdata_setup = *((cs_opt_skipdata *)value);
378 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800379 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800380
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800381 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800382}
383
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800384// generate @op_str for data instruction of SKIPDATA
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800385static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
386{
387 char *p = opstr;
388 int len;
389 size_t i;
390
391 if (!size) {
392 opstr[0] = '\0';
393 return;
394 }
395
396 len = sprintf(p, "0x%02x", buffer[0]);
397 p+= len;
398
399 for(i = 1; i < size; i++) {
400 len = sprintf(p, ", 0x%02x", buffer[i]);
401 p+= len;
402 }
403}
404
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800405// dynamicly allocate memory to contain disasm insn
406// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800407size_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 +0800408{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800409 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800410 MCInst mci;
411 uint16_t insn_size;
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800412 size_t c = 0;
413 unsigned int f = 0;
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800414 cs_insn insn_cache[INSN_CACHE_SIZE];
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800415 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800416 size_t total_size = 0;
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800417 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800418 void *tmp;
419 size_t skipdata_bytes;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800420 uint64_t offset_org;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800421
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800422 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800423 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800424 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800425 return 0;
426 }
427
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800428 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800429
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800430 memset(insn_cache, 0, sizeof(insn_cache));
431
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800432 // save the original offset for SKIPDATA
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800433 offset_org = offset;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800434
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800435 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600436 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800437 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800438
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800439 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800440 if (r) {
441 SStream ss;
442 SStream_Init(&ss);
443
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800444 // relative branches need to know the address & size of current insn
445 mci.insn_size = insn_size;
446 mci.address = offset;
447
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800448 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800449 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800450 mci.flat_insn.address = offset;
451 mci.flat_insn.size = insn_size;
452 // allocate memory for @detail pointer
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800453 insn_cache[f].detail = cs_mem_calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800454 }
455
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800456 handle->printer(&mci, &ss, handle->printer_info);
457
Joxean114df0e2013-12-04 07:11:32 +0100458 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800459
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800460 f++;
461 if (f == ARR_SIZE(insn_cache)) {
462 // resize total to contain newly disasm insns
463 total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
464 tmp = cs_mem_realloc(total, total_size);
465 if (tmp == NULL) { // insufficient memory
466 cs_mem_free(total);
467 handle->errnum = CS_ERR_MEM;
468 return 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800469 }
470
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800471 total = tmp;
472 memcpy((void*)((uintptr_t)total + total_size - sizeof(insn_cache)), insn_cache, sizeof(insn_cache));
473
474 // reset f back to 0
475 f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800476 }
477
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800478 c++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800479 buffer += insn_size;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800480
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800481 size -= insn_size;
482 offset += insn_size;
483
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800484 if (count > 0 && c == count)
485 break;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800486 } else {
487 // encounter a broken instruction
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800488 // if there is no request to skip data, or remaining data is too small,
489 // then bail out
490 if (!handle->skipdata || handle->skipdata_size > size)
491 break;
492
493 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh42288ac2014-04-14 14:53:13 +0800494 skipdata_bytes = handle->skipdata_setup.callback(buffer, offset - offset_org,
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800495 handle->skipdata_setup.user_data);
496 if (skipdata_bytes > size)
497 // remaining data is not enough
498 break;
499
500 if (!skipdata_bytes)
501 // user requested not to skip data, so bail out
502 break;
503 } else
504 skipdata_bytes = handle->skipdata_size;
505
506 // we have to skip some amount of data, depending on arch & mode
507 insn_cache[f].id = 0; // invalid ID for this "data" instruction
508 insn_cache[f].address = offset;
509 insn_cache[f].size = skipdata_bytes;
510 memcpy(insn_cache[f].bytes, buffer, skipdata_bytes);
511 strncpy(insn_cache[f].mnemonic, handle->skipdata_setup.mnemonic,
512 sizeof(insn_cache[f].mnemonic) - 1);
513 skipdata_opstr(insn_cache[f].op_str, buffer, skipdata_bytes);
514 insn_cache[f].detail = NULL;
515
516 f++;
517 if (f == ARR_SIZE(insn_cache)) {
518 // resize total to contain newly disasm insns
519
520 total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
521 tmp = cs_mem_realloc(total, total_size);
522 if (tmp == NULL) { // insufficient memory
523 cs_mem_free(total);
524 handle->errnum = CS_ERR_MEM;
525 return 0;
526 }
527
528 total = tmp;
529 memcpy((void*)((uintptr_t)total + total_size - sizeof(insn_cache)), insn_cache, sizeof(insn_cache));
530
531 // reset f back to 0
532 f = 0;
533 }
534
535 buffer += skipdata_bytes;
536 size -= skipdata_bytes;
537 offset += skipdata_bytes;
538 c++;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800539 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800540 }
541
542 if (f) {
543 // resize total to contain newly disasm insns
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800544 void *tmp = cs_mem_realloc(total, total_size + f * sizeof(insn_cache[0]));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800545 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800546 cs_mem_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800547 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800548 return 0;
549 }
550
551 total = tmp;
Alex Ionescu46018db2014-01-22 09:45:00 -0800552 memcpy((void*)((uintptr_t)total + total_size), insn_cache, f * sizeof(insn_cache[0]));
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800553
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800554 }
555
556 *insn = total;
557
558 return c;
559}
560
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800561void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800562{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800563 size_t i;
564
565 // free all detail pointers
566 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800567 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800568
569 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800570 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800571}
572
573// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100574const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800575{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800576 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800577
578 if (!handle || handle->reg_name == NULL) {
579 return NULL;
580 }
581
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800582 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800583}
584
pancakef0e4eed2013-12-11 22:14:42 +0100585const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800586{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800587 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800588
589 if (!handle || handle->insn_name == NULL) {
590 return NULL;
591 }
592
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800593 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800594}
595
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800596static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800597{
598 int i;
599
600 for (i = 0; i < max; i++) {
601 if (arr[i] == id)
602 return true;
603 }
604
605 return false;
606}
607
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800608bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800609{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800610 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800611 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800612 return false;
613
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100614 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200615
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800616 if (!handle->detail) {
617 handle->errnum = CS_ERR_DETAIL;
618 return false;
619 }
620
Giovanni Condello95657e02014-05-07 17:31:27 +0200621 if(!insn->id) {
622 handle->errnum = CS_ERR_SKIPDATA;
623 return false;
624 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200625
Giovanni Condello95657e02014-05-07 17:31:27 +0200626 if(!insn->detail) {
627 handle->errnum = CS_ERR_DETAIL;
628 return false;
629 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200630
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800631 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800632}
633
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800634bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800635{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800636 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800637 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800638 return false;
639
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100640 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200641
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800642 if (!handle->detail) {
643 handle->errnum = CS_ERR_DETAIL;
644 return false;
645 }
646
Giovanni Condello95657e02014-05-07 17:31:27 +0200647 if(!insn->id) {
648 handle->errnum = CS_ERR_SKIPDATA;
649 return false;
650 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200651
Giovanni Condello95657e02014-05-07 17:31:27 +0200652 if(!insn->detail) {
653 handle->errnum = CS_ERR_DETAIL;
654 return false;
655 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200656
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800657 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800658}
659
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800660bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800661{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800662 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800663 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800664 return false;
665
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100666 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +0200667
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800668 if (!handle->detail) {
669 handle->errnum = CS_ERR_DETAIL;
670 return false;
671 }
672
Giovanni Condello95657e02014-05-07 17:31:27 +0200673 if(!insn->id) {
674 handle->errnum = CS_ERR_SKIPDATA;
675 return false;
676 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200677
Giovanni Condello95657e02014-05-07 17:31:27 +0200678 if(!insn->detail) {
679 handle->errnum = CS_ERR_DETAIL;
680 return false;
681 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200682
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800683 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800684}
685
686int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
687{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800688 struct cs_struct *handle;
689 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800690 if (!ud)
691 return -1;
692
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100693 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200694
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800695 if (!handle->detail) {
696 handle->errnum = CS_ERR_DETAIL;
697 return -1;
698 }
699
Giovanni Condello95657e02014-05-07 17:31:27 +0200700 if(!insn->id) {
701 handle->errnum = CS_ERR_SKIPDATA;
702 return -1;
703 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200704
Giovanni Condello95657e02014-05-07 17:31:27 +0200705 if(!insn->detail) {
706 handle->errnum = CS_ERR_DETAIL;
707 return -1;
708 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200709
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800710 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800711
712 switch (handle->arch) {
713 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800714 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800715 return -1;
716 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800717 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800718 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800719 count++;
720 break;
721 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800722 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800723 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800724 count++;
725 break;
726 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800727 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800728 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800729 count++;
730 break;
731 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800732 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800733 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800734 count++;
735 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800736 case CS_ARCH_PPC:
737 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800738 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800739 count++;
740 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800741 case CS_ARCH_SPARC:
742 for (i = 0; i < insn->detail->sparc.op_count; i++)
743 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
744 count++;
745 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800746 case CS_ARCH_SYSZ:
747 for (i = 0; i < insn->detail->sysz.op_count; i++)
748 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
749 count++;
750 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800751 }
752
753 return count;
754}
755
756int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
757 unsigned int post)
758{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800759 struct cs_struct *handle;
760 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800761 if (!ud)
762 return -1;
763
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100764 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200765
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800766 if (!handle->detail) {
767 handle->errnum = CS_ERR_DETAIL;
768 return -1;
769 }
770
Giovanni Condello95657e02014-05-07 17:31:27 +0200771 if(!insn->id) {
772 handle->errnum = CS_ERR_SKIPDATA;
773 return -1;
774 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200775
Giovanni Condello95657e02014-05-07 17:31:27 +0200776 if(!insn->detail) {
777 handle->errnum = CS_ERR_DETAIL;
778 return -1;
779 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200780
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800781 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800782
783 switch (handle->arch) {
784 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800785 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800786 return -1;
787 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800788 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800789 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800790 count++;
791 if (count == post)
792 return i;
793 }
794 break;
795 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800796 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800797 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800798 count++;
799 if (count == post)
800 return i;
801 }
802 break;
803 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800804 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800805 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800806 count++;
807 if (count == post)
808 return i;
809 }
810 break;
811 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800812 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800813 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800814 count++;
815 if (count == post)
816 return i;
817 }
818 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800819 case CS_ARCH_PPC:
820 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800821 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800822 count++;
823 if (count == post)
824 return i;
825 }
826 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800827 case CS_ARCH_SPARC:
828 for (i = 0; i < insn->detail->sparc.op_count; i++) {
829 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
830 count++;
831 if (count == post)
832 return i;
833 }
834 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800835 case CS_ARCH_SYSZ:
836 for (i = 0; i < insn->detail->sysz.op_count; i++) {
837 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
838 count++;
839 if (count == post)
840 return i;
841 }
842 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800843 }
844
845 return -1;
846}