2014-04-29 03:21:04 +00:00
|
|
|
/* Capstone Disassembly Engine */
|
|
|
|
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
|
2014-05-09 19:40:00 +00:00
|
|
|
#if defined (WIN32) || defined (WIN64) || defined (_WIN32) || defined (_WIN64)
|
|
|
|
#pragma warning(disable:4996)
|
|
|
|
#endif
|
2013-12-24 08:49:36 +00:00
|
|
|
#include <stddef.h>
|
2013-11-27 04:11:31 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <capstone.h>
|
|
|
|
|
|
|
|
#include "utils.h"
|
2014-01-05 03:35:47 +00:00
|
|
|
#include "MCRegisterInfo.h"
|
2013-11-27 04:11:31 +00:00
|
|
|
|
2014-05-10 08:49:29 +00:00
|
|
|
#ifdef CAPSTONE_USE_SYS_DYN_MEM
|
2014-03-06 01:13:04 +00:00
|
|
|
#define INSN_CACHE_SIZE 32
|
2014-03-05 19:36:03 +00:00
|
|
|
#else
|
2014-03-07 00:40:35 +00:00
|
|
|
// reduce stack variable size for kernel/firmware
|
2014-03-05 19:36:03 +00:00
|
|
|
#define INSN_CACHE_SIZE 8
|
|
|
|
#endif
|
2014-01-22 01:46:42 +00:00
|
|
|
|
2014-04-09 15:49:30 +00:00
|
|
|
// default SKIPDATA mnemonic
|
2014-04-10 02:26:49 +00:00
|
|
|
#define SKIPDATA_MNEM ".byte"
|
2014-04-09 15:49:30 +00:00
|
|
|
|
2013-12-29 16:15:25 +00:00
|
|
|
cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
|
2014-01-03 13:51:59 +00:00
|
|
|
cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
|
|
|
|
void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
|
2013-12-21 04:16:47 +00:00
|
|
|
|
2014-01-19 04:03:22 +00:00
|
|
|
extern void ARM_enable(void);
|
|
|
|
extern void AArch64_enable(void);
|
|
|
|
extern void Mips_enable(void);
|
|
|
|
extern void X86_enable(void);
|
|
|
|
extern void PPC_enable(void);
|
2014-03-10 03:58:57 +00:00
|
|
|
extern void Sparc_enable(void);
|
2014-03-23 00:35:45 +00:00
|
|
|
extern void SystemZ_enable(void);
|
2014-01-19 04:03:22 +00:00
|
|
|
|
|
|
|
static void archs_enable(void)
|
2014-01-09 06:33:56 +00:00
|
|
|
{
|
2014-01-19 04:03:22 +00:00
|
|
|
static bool initialized = false;
|
|
|
|
|
|
|
|
if (initialized)
|
|
|
|
return;
|
|
|
|
|
2014-01-09 05:22:56 +00:00
|
|
|
#ifdef CAPSTONE_HAS_ARM
|
2014-01-19 04:03:22 +00:00
|
|
|
ARM_enable();
|
2014-01-09 05:22:56 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CAPSTONE_HAS_ARM64
|
2014-01-19 04:03:22 +00:00
|
|
|
AArch64_enable();
|
2014-01-09 05:22:56 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CAPSTONE_HAS_MIPS
|
2014-01-19 04:03:22 +00:00
|
|
|
Mips_enable();
|
2014-01-09 05:22:56 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CAPSTONE_HAS_POWERPC
|
2014-01-19 04:03:22 +00:00
|
|
|
PPC_enable();
|
2014-01-09 05:22:56 +00:00
|
|
|
#endif
|
2014-03-10 03:58:57 +00:00
|
|
|
#ifdef CAPSTONE_HAS_SPARC
|
|
|
|
Sparc_enable();
|
|
|
|
#endif
|
2014-03-23 00:35:45 +00:00
|
|
|
#ifdef CAPSTONE_HAS_SYSZ
|
|
|
|
SystemZ_enable();
|
|
|
|
#endif
|
2014-03-10 03:58:57 +00:00
|
|
|
#ifdef CAPSTONE_HAS_X86
|
|
|
|
X86_enable();
|
|
|
|
#endif
|
2014-01-19 04:03:22 +00:00
|
|
|
|
|
|
|
initialized = true;
|
2014-01-09 05:22:56 +00:00
|
|
|
}
|
|
|
|
|
2013-12-22 02:40:58 +00:00
|
|
|
unsigned int all_arch = 0;
|
2013-12-21 04:16:47 +00:00
|
|
|
|
2014-05-10 08:49:29 +00:00
|
|
|
#ifdef CAPSTONE_USE_SYS_DYN_MEM
|
2014-01-11 04:55:31 +00:00
|
|
|
cs_malloc_t cs_mem_malloc = malloc;
|
|
|
|
cs_calloc_t cs_mem_calloc = calloc;
|
|
|
|
cs_realloc_t cs_mem_realloc = realloc;
|
|
|
|
cs_free_t cs_mem_free = free;
|
2014-01-15 12:44:03 +00:00
|
|
|
cs_vsnprintf_t cs_vsnprintf = vsnprintf;
|
2014-01-05 15:41:31 +00:00
|
|
|
#else
|
2014-01-11 04:55:31 +00:00
|
|
|
cs_malloc_t cs_mem_malloc = NULL;
|
|
|
|
cs_calloc_t cs_mem_calloc = NULL;
|
|
|
|
cs_realloc_t cs_mem_realloc = NULL;
|
|
|
|
cs_free_t cs_mem_free = NULL;
|
2014-01-15 12:44:03 +00:00
|
|
|
cs_vsnprintf_t cs_vsnprintf = NULL;
|
2014-01-05 15:41:31 +00:00
|
|
|
#endif
|
2013-12-22 07:20:07 +00:00
|
|
|
|
2013-12-28 05:59:09 +00:00
|
|
|
unsigned int cs_version(int *major, int *minor)
|
2013-12-10 05:31:20 +00:00
|
|
|
{
|
2014-03-04 16:21:53 +00:00
|
|
|
archs_enable();
|
|
|
|
|
2013-12-22 06:16:28 +00:00
|
|
|
if (major != NULL && minor != NULL) {
|
|
|
|
*major = CS_API_MAJOR;
|
|
|
|
*minor = CS_API_MINOR;
|
|
|
|
}
|
2013-12-22 02:40:58 +00:00
|
|
|
|
|
|
|
return (CS_API_MAJOR << 8) + CS_API_MINOR;
|
|
|
|
}
|
|
|
|
|
2014-02-22 15:41:16 +00:00
|
|
|
bool cs_support(int query)
|
2013-12-22 02:40:58 +00:00
|
|
|
{
|
2014-03-04 16:21:53 +00:00
|
|
|
archs_enable();
|
|
|
|
|
2014-02-22 15:41:16 +00:00
|
|
|
if (query == CS_ARCH_ALL)
|
2013-12-22 02:40:58 +00:00
|
|
|
return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
|
2013-12-29 16:15:25 +00:00
|
|
|
(1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
|
2014-03-23 00:35:45 +00:00
|
|
|
(1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
|
|
|
|
(1 << CS_ARCH_SYSZ));
|
2013-12-22 02:40:58 +00:00
|
|
|
|
2014-02-27 05:54:28 +00:00
|
|
|
if ((unsigned int)query < CS_ARCH_MAX)
|
2014-02-22 15:41:16 +00:00
|
|
|
return all_arch & (1 << query);
|
|
|
|
|
|
|
|
if (query == CS_SUPPORT_DIET) {
|
|
|
|
#ifdef CAPSTONE_DIET
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-03-27 02:54:44 +00:00
|
|
|
if (query == CS_SUPPORT_X86_REDUCE) {
|
|
|
|
#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
|
2014-03-25 15:20:41 +00:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-02-22 15:41:16 +00:00
|
|
|
// unsupported query
|
|
|
|
return false;
|
2013-12-10 05:31:20 +00:00
|
|
|
}
|
|
|
|
|
2013-11-27 04:11:31 +00:00
|
|
|
cs_err cs_errno(csh handle)
|
|
|
|
{
|
2014-05-08 23:33:35 +00:00
|
|
|
struct cs_struct *ud = NULL;
|
2013-11-27 04:11:31 +00:00
|
|
|
if (!handle)
|
|
|
|
return CS_ERR_CSH;
|
|
|
|
|
2014-05-08 22:44:49 +00:00
|
|
|
ud = (struct cs_struct *)(uintptr_t)handle;
|
2013-11-27 04:11:31 +00:00
|
|
|
|
2013-11-27 07:24:47 +00:00
|
|
|
return ud->errnum;
|
2013-11-27 04:11:31 +00:00
|
|
|
}
|
|
|
|
|
2014-01-03 14:49:07 +00:00
|
|
|
const char *cs_strerror(cs_err code)
|
|
|
|
{
|
|
|
|
switch(code) {
|
|
|
|
default:
|
|
|
|
return "Unknown error code";
|
|
|
|
case CS_ERR_OK:
|
|
|
|
return "OK (CS_ERR_OK)";
|
|
|
|
case CS_ERR_MEM:
|
|
|
|
return "Out of memory (CS_ERR_MEM)";
|
|
|
|
case CS_ERR_ARCH:
|
|
|
|
return "Invalid architecture (CS_ERR_ARCH)";
|
|
|
|
case CS_ERR_HANDLE:
|
|
|
|
return "Invalid handle (CS_ERR_HANDLE)";
|
|
|
|
case CS_ERR_CSH:
|
|
|
|
return "Invalid csh (CS_ERR_CSH)";
|
|
|
|
case CS_ERR_MODE:
|
|
|
|
return "Invalid mode (CS_ERR_MODE)";
|
|
|
|
case CS_ERR_OPTION:
|
|
|
|
return "Invalid option (CS_ERR_OPTION)";
|
|
|
|
case CS_ERR_DETAIL:
|
|
|
|
return "Details are unavailable (CS_ERR_DETAIL)";
|
2014-01-06 01:06:30 +00:00
|
|
|
case CS_ERR_MEMSETUP:
|
|
|
|
return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
|
2014-02-08 16:19:15 +00:00
|
|
|
case CS_ERR_VERSION:
|
|
|
|
return "Different API version between core & binding (CS_ERR_VERSION)";
|
2014-02-24 08:47:36 +00:00
|
|
|
case CS_ERR_DIET:
|
|
|
|
return "Information irrelevant in diet engine (CS_ERR_DIET)";
|
2014-04-10 09:20:01 +00:00
|
|
|
case CS_ERR_SKIPDATA:
|
|
|
|
return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
|
2014-01-03 14:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-27 04:11:31 +00:00
|
|
|
cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
|
|
|
|
{
|
2014-05-08 23:33:35 +00:00
|
|
|
cs_err err;
|
|
|
|
struct cs_struct *ud = NULL;
|
2014-01-15 12:44:03 +00:00
|
|
|
if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
|
2014-01-06 01:06:30 +00:00
|
|
|
// Error: before cs_open(), dynamic memory management must be initialized
|
|
|
|
// with cs_option(CS_OPT_MEM)
|
|
|
|
return CS_ERR_MEMSETUP;
|
|
|
|
|
2014-01-19 04:03:22 +00:00
|
|
|
archs_enable();
|
|
|
|
|
2014-01-06 01:52:57 +00:00
|
|
|
if (arch < CS_ARCH_MAX && arch_init[arch]) {
|
2014-01-11 04:55:31 +00:00
|
|
|
ud = cs_mem_calloc(1, sizeof(*ud));
|
2014-01-06 01:52:57 +00:00
|
|
|
if (!ud) {
|
|
|
|
// memory insufficient
|
|
|
|
return CS_ERR_MEM;
|
|
|
|
}
|
2013-11-27 04:11:31 +00:00
|
|
|
|
2013-12-22 05:41:38 +00:00
|
|
|
ud->errnum = CS_ERR_OK;
|
|
|
|
ud->arch = arch;
|
|
|
|
ud->mode = mode;
|
|
|
|
ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
|
2014-01-07 15:36:26 +00:00
|
|
|
// by default, do not break instruction into details
|
|
|
|
ud->detail = CS_OPT_OFF;
|
2013-12-22 05:41:38 +00:00
|
|
|
|
2014-04-09 15:49:30 +00:00
|
|
|
// default skipdata setup
|
|
|
|
ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
|
|
|
|
|
2014-05-08 22:44:49 +00:00
|
|
|
err = arch_init[ud->arch](ud);
|
2014-01-21 07:26:02 +00:00
|
|
|
if (err) {
|
|
|
|
cs_mem_free(ud);
|
|
|
|
*handle = 0;
|
|
|
|
return err;
|
|
|
|
}
|
2014-01-06 01:52:57 +00:00
|
|
|
|
|
|
|
*handle = (uintptr_t)ud;
|
|
|
|
|
|
|
|
return CS_ERR_OK;
|
2013-12-22 05:41:38 +00:00
|
|
|
} else {
|
|
|
|
*handle = 0;
|
|
|
|
return CS_ERR_ARCH;
|
|
|
|
}
|
2013-11-27 04:11:31 +00:00
|
|
|
}
|
|
|
|
|
2014-02-27 14:20:39 +00:00
|
|
|
cs_err cs_close(csh *handle)
|
2013-11-27 04:11:31 +00:00
|
|
|
{
|
2014-05-08 23:33:35 +00:00
|
|
|
struct cs_struct *ud = NULL;
|
2014-02-27 15:42:49 +00:00
|
|
|
if (*handle == 0)
|
2014-02-27 14:20:39 +00:00
|
|
|
// invalid handle
|
2013-11-27 04:11:31 +00:00
|
|
|
return CS_ERR_CSH;
|
|
|
|
|
2014-05-08 22:44:49 +00:00
|
|
|
ud = (struct cs_struct *)(*handle);
|
2013-11-27 04:11:31 +00:00
|
|
|
|
2014-02-27 15:59:08 +00:00
|
|
|
if (ud->printer_info)
|
|
|
|
cs_mem_free(ud->printer_info);
|
2013-11-27 04:11:31 +00:00
|
|
|
|
2014-01-07 02:56:04 +00:00
|
|
|
// arch_destroy[ud->arch](ud);
|
2014-01-03 09:08:58 +00:00
|
|
|
|
2014-01-11 04:55:31 +00:00
|
|
|
cs_mem_free(ud->insn_cache);
|
2014-01-03 13:51:59 +00:00
|
|
|
memset(ud, 0, sizeof(*ud));
|
2014-01-11 04:55:31 +00:00
|
|
|
cs_mem_free(ud);
|
2014-01-03 13:51:59 +00:00
|
|
|
|
2014-02-28 01:38:11 +00:00
|
|
|
// invalidate this handle by ZERO out its value.
|
|
|
|
// this is to make sure it is unusable after cs_close()
|
2014-02-27 14:20:39 +00:00
|
|
|
*handle = 0;
|
|
|
|
|
2013-11-27 04:11:31 +00:00
|
|
|
return CS_ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill insn with mnemonic & operands info
|
2014-01-23 15:42:40 +00:00
|
|
|
static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
|
2013-12-24 08:49:36 +00:00
|
|
|
PostPrinter_t postprinter, const uint8_t *code)
|
2013-11-27 04:11:31 +00:00
|
|
|
{
|
2014-05-08 23:33:35 +00:00
|
|
|
char *sp = NULL;
|
2013-12-13 16:23:41 +00:00
|
|
|
if (handle->detail) {
|
2013-12-24 08:49:36 +00:00
|
|
|
// avoiding copy insn->detail
|
|
|
|
memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
|
|
|
|
|
|
|
|
// NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
|
|
|
|
// copy from @regs_read until @arm
|
2014-05-08 22:44:49 +00:00
|
|
|
memcpy(insn->detail, (void *)((uintptr_t)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read)),
|
2013-12-24 08:49:36 +00:00
|
|
|
offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
|
|
|
|
// then copy from @arm until end
|
2014-01-23 15:42:40 +00:00
|
|
|
memcpy((void *)((uintptr_t)(insn->detail) + offsetof(cs_detail, arm)),
|
|
|
|
(void *)((uintptr_t)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm)),
|
2013-12-24 08:49:36 +00:00
|
|
|
sizeof(cs_detail) - offsetof(cs_detail, arm));
|
2013-12-13 16:23:41 +00:00
|
|
|
} else {
|
|
|
|
insn->address = mci->address;
|
2014-01-22 17:45:00 +00:00
|
|
|
insn->size = (uint16_t)mci->insn_size;
|
2013-12-13 16:23:41 +00:00
|
|
|
}
|
2013-11-27 04:11:31 +00:00
|
|
|
|
2013-12-24 08:49:36 +00:00
|
|
|
// fill the instruction bytes
|
|
|
|
memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
|
|
|
|
|
2013-12-14 02:45:09 +00:00
|
|
|
// map internal instruction opcode to public insn ID
|
|
|
|
if (handle->insn_id)
|
2014-01-06 02:56:59 +00:00
|
|
|
handle->insn_id(handle, insn, MCInst_getOpcode(mci));
|
2013-12-14 02:45:09 +00:00
|
|
|
|
|
|
|
// alias instruction might have ID saved in OpcodePub
|
|
|
|
if (MCInst_getOpcodePub(mci))
|
|
|
|
insn->id = MCInst_getOpcodePub(mci);
|
|
|
|
|
|
|
|
// post printer handles some corner cases (hacky)
|
2013-12-24 08:49:36 +00:00
|
|
|
if (postprinter)
|
|
|
|
postprinter((csh)handle, insn, buffer);
|
2013-12-14 02:45:09 +00:00
|
|
|
|
2014-02-22 15:26:27 +00:00
|
|
|
#ifndef CAPSTONE_DIET
|
2013-11-27 04:11:31 +00:00
|
|
|
// fill in mnemonic & operands
|
2013-12-12 06:00:12 +00:00
|
|
|
// find first space or tab
|
2014-05-08 22:44:49 +00:00
|
|
|
sp = buffer;
|
2014-05-07 00:25:24 +00:00
|
|
|
for (sp = buffer; *sp; sp++) {
|
2013-12-12 06:00:12 +00:00
|
|
|
if (*sp == ' '||*sp == '\t')
|
|
|
|
break;
|
2014-05-07 00:25:24 +00:00
|
|
|
if (*sp == '|')
|
|
|
|
*sp = ' ';
|
|
|
|
}
|
|
|
|
|
2013-12-12 06:00:12 +00:00
|
|
|
if (*sp) {
|
|
|
|
*sp = '\0';
|
2013-12-12 06:43:39 +00:00
|
|
|
// find the next non-space char
|
|
|
|
sp++;
|
|
|
|
for (; ((*sp == ' ') || (*sp == '\t')); sp++);
|
|
|
|
strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
|
2013-11-27 04:11:31 +00:00
|
|
|
insn->op_str[sizeof(insn->op_str) - 1] = '\0';
|
|
|
|
} else
|
|
|
|
insn->op_str[0] = '\0';
|
|
|
|
|
2014-05-07 06:14:07 +00:00
|
|
|
strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
|
2013-11-27 04:11:31 +00:00
|
|
|
insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
|
2014-02-22 15:26:27 +00:00
|
|
|
#endif
|
2013-11-27 04:11:31 +00:00
|
|
|
}
|
|
|
|
|
2014-04-09 15:49:30 +00:00
|
|
|
// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
|
2014-04-10 03:53:46 +00:00
|
|
|
// this very much depends on instruction alignment requirement of each arch.
|
2014-04-09 15:49:30 +00:00
|
|
|
static uint8_t skipdata_size(cs_struct *handle)
|
|
|
|
{
|
|
|
|
switch(handle->arch) {
|
|
|
|
default:
|
|
|
|
// should never reach
|
|
|
|
return -1;
|
|
|
|
case CS_ARCH_ARM:
|
2014-04-10 03:53:46 +00:00
|
|
|
// skip 2 bytes on Thumb mode.
|
|
|
|
if (handle->mode & CS_MODE_THUMB)
|
|
|
|
return 2;
|
|
|
|
// otherwise, skip 4 bytes
|
|
|
|
return 4;
|
2014-04-09 15:49:30 +00:00
|
|
|
case CS_ARCH_ARM64:
|
|
|
|
case CS_ARCH_MIPS:
|
|
|
|
case CS_ARCH_PPC:
|
|
|
|
case CS_ARCH_SPARC:
|
2014-04-10 03:53:46 +00:00
|
|
|
// skip 4 bytes
|
|
|
|
return 4;
|
2014-04-09 15:49:30 +00:00
|
|
|
case CS_ARCH_SYSZ:
|
2014-04-10 03:53:46 +00:00
|
|
|
// SystemZ instruction's length can be 2, 4 or 6 bytes,
|
|
|
|
// so we just skip 2 bytes
|
2014-04-09 15:49:30 +00:00
|
|
|
return 2;
|
|
|
|
case CS_ARCH_X86:
|
|
|
|
// X86 has no restriction on instruction alignment
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-04 01:44:07 +00:00
|
|
|
cs_err cs_option(csh ud, cs_opt_type type, size_t value)
|
2013-12-03 13:00:09 +00:00
|
|
|
{
|
2014-05-08 23:33:35 +00:00
|
|
|
struct cs_struct *handle = NULL;
|
2014-03-04 16:21:53 +00:00
|
|
|
archs_enable();
|
|
|
|
|
2014-01-05 03:19:04 +00:00
|
|
|
// cs_option() can be called with NULL handle just for CS_OPT_MEM
|
|
|
|
// This is supposed to be executed before all other APIs (even cs_open())
|
|
|
|
if (type == CS_OPT_MEM) {
|
|
|
|
cs_opt_mem *mem = (cs_opt_mem *)value;
|
|
|
|
|
2014-01-11 04:55:31 +00:00
|
|
|
cs_mem_malloc = mem->malloc;
|
|
|
|
cs_mem_calloc = mem->calloc;
|
|
|
|
cs_mem_realloc = mem->realloc;
|
|
|
|
cs_mem_free = mem->free;
|
2014-01-15 12:44:03 +00:00
|
|
|
cs_vsnprintf = mem->vsnprintf;
|
2014-01-05 03:19:04 +00:00
|
|
|
|
|
|
|
return CS_ERR_OK;
|
|
|
|
}
|
|
|
|
|
2014-05-08 22:44:49 +00:00
|
|
|
handle = (struct cs_struct *)(uintptr_t)ud;
|
2013-12-20 04:40:28 +00:00
|
|
|
if (!handle)
|
|
|
|
return CS_ERR_CSH;
|
|
|
|
|
2014-04-09 15:49:30 +00:00
|
|
|
switch(type) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case CS_OPT_DETAIL:
|
|
|
|
handle->detail = value;
|
|
|
|
return CS_ERR_OK;
|
|
|
|
case CS_OPT_SKIPDATA:
|
|
|
|
handle->skipdata = (value == CS_OPT_ON);
|
|
|
|
if (handle->skipdata) {
|
|
|
|
if (handle->skipdata_size == 0) {
|
|
|
|
// set the default skipdata size
|
|
|
|
handle->skipdata_size = skipdata_size(handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CS_ERR_OK;
|
|
|
|
case CS_OPT_SKIPDATA_SETUP:
|
|
|
|
if (value)
|
|
|
|
handle->skipdata_setup = *((cs_opt_skipdata *)value);
|
|
|
|
return CS_ERR_OK;
|
2013-12-21 01:59:31 +00:00
|
|
|
}
|
2013-12-20 04:40:28 +00:00
|
|
|
|
2013-12-22 03:10:56 +00:00
|
|
|
return arch_option[handle->arch](handle, type, value);
|
2013-12-03 13:00:09 +00:00
|
|
|
}
|
|
|
|
|
2014-04-10 06:36:08 +00:00
|
|
|
// generate @op_str for data instruction of SKIPDATA
|
2014-04-09 15:49:30 +00:00
|
|
|
static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
|
|
|
|
{
|
|
|
|
char *p = opstr;
|
|
|
|
int len;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!size) {
|
|
|
|
opstr[0] = '\0';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = sprintf(p, "0x%02x", buffer[0]);
|
|
|
|
p+= len;
|
|
|
|
|
|
|
|
for(i = 1; i < size; i++) {
|
|
|
|
len = sprintf(p, ", 0x%02x", buffer[i]);
|
|
|
|
p+= len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-27 04:11:31 +00:00
|
|
|
// dynamicly allocate memory to contain disasm insn
|
|
|
|
// NOTE: caller must free() the allocated memory itself to avoid memory leaking
|
2013-12-25 05:26:22 +00:00
|
|
|
size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
|
2013-11-27 04:11:31 +00:00
|
|
|
{
|
2014-01-23 15:42:40 +00:00
|
|
|
struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
|
2013-11-27 04:11:31 +00:00
|
|
|
MCInst mci;
|
|
|
|
uint16_t insn_size;
|
2014-01-21 03:49:25 +00:00
|
|
|
size_t c = 0;
|
|
|
|
unsigned int f = 0;
|
2014-01-22 01:46:42 +00:00
|
|
|
cs_insn insn_cache[INSN_CACHE_SIZE];
|
2013-11-27 04:11:31 +00:00
|
|
|
void *total = NULL;
|
2013-11-29 09:40:07 +00:00
|
|
|
size_t total_size = 0;
|
2014-03-06 14:40:08 +00:00
|
|
|
bool r;
|
2014-04-09 15:49:30 +00:00
|
|
|
void *tmp;
|
|
|
|
size_t skipdata_bytes;
|
2014-04-10 06:36:08 +00:00
|
|
|
uint64_t offset_org;
|
2013-11-27 04:11:31 +00:00
|
|
|
|
2014-02-27 03:10:41 +00:00
|
|
|
if (!handle) {
|
2013-11-27 04:11:31 +00:00
|
|
|
// FIXME: how to handle this case:
|
2013-11-27 07:24:47 +00:00
|
|
|
// handle->errnum = CS_ERR_HANDLE;
|
2013-11-27 04:11:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-27 07:24:47 +00:00
|
|
|
handle->errnum = CS_ERR_OK;
|
2013-11-27 04:11:31 +00:00
|
|
|
|
2013-12-14 02:45:09 +00:00
|
|
|
memset(insn_cache, 0, sizeof(insn_cache));
|
|
|
|
|
2014-04-28 03:19:44 +00:00
|
|
|
// save the original offset for SKIPDATA
|
2014-04-10 06:36:08 +00:00
|
|
|
offset_org = offset;
|
2014-04-28 03:19:44 +00:00
|
|
|
|
2013-11-27 04:11:31 +00:00
|
|
|
while (size > 0) {
|
2013-12-20 04:40:28 +00:00
|
|
|
MCInst_Init(&mci);
|
2013-12-29 16:15:25 +00:00
|
|
|
mci.csh = handle;
|
2013-11-27 04:11:31 +00:00
|
|
|
|
2014-05-07 00:25:24 +00:00
|
|
|
r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
|
2013-11-27 04:11:31 +00:00
|
|
|
if (r) {
|
|
|
|
SStream ss;
|
|
|
|
SStream_Init(&ss);
|
|
|
|
|
2013-12-13 16:23:41 +00:00
|
|
|
// relative branches need to know the address & size of current insn
|
|
|
|
mci.insn_size = insn_size;
|
|
|
|
mci.address = offset;
|
|
|
|
|
2013-12-14 02:45:09 +00:00
|
|
|
if (handle->detail) {
|
|
|
|
// save all the information for non-detailed mode
|
2013-12-24 08:49:36 +00:00
|
|
|
mci.flat_insn.address = offset;
|
|
|
|
mci.flat_insn.size = insn_size;
|
|
|
|
// allocate memory for @detail pointer
|
2014-01-11 04:55:31 +00:00
|
|
|
insn_cache[f].detail = cs_mem_calloc(1, sizeof(cs_detail));
|
2013-12-13 16:23:41 +00:00
|
|
|
}
|
|
|
|
|
2013-11-27 04:11:31 +00:00
|
|
|
handle->printer(&mci, &ss, handle->printer_info);
|
|
|
|
|
2013-12-04 06:11:32 +00:00
|
|
|
fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
|
2013-12-13 16:23:41 +00:00
|
|
|
|
2014-05-07 00:25:24 +00:00
|
|
|
f++;
|
|
|
|
if (f == ARR_SIZE(insn_cache)) {
|
|
|
|
// resize total to contain newly disasm insns
|
|
|
|
total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
|
|
|
|
tmp = cs_mem_realloc(total, total_size);
|
|
|
|
if (tmp == NULL) { // insufficient memory
|
|
|
|
cs_mem_free(total);
|
|
|
|
handle->errnum = CS_ERR_MEM;
|
|
|
|
return 0;
|
2013-11-27 04:11:31 +00:00
|
|
|
}
|
|
|
|
|
2014-05-07 00:25:24 +00:00
|
|
|
total = tmp;
|
|
|
|
memcpy((void*)((uintptr_t)total + total_size - sizeof(insn_cache)), insn_cache, sizeof(insn_cache));
|
|
|
|
|
|
|
|
// reset f back to 0
|
|
|
|
f = 0;
|
2013-11-27 04:11:31 +00:00
|
|
|
}
|
|
|
|
|
2014-05-07 00:25:24 +00:00
|
|
|
c++;
|
2013-11-27 04:11:31 +00:00
|
|
|
buffer += insn_size;
|
2014-04-28 03:19:44 +00:00
|
|
|
|
2013-11-27 04:11:31 +00:00
|
|
|
size -= insn_size;
|
|
|
|
offset += insn_size;
|
|
|
|
|
2014-05-07 00:25:24 +00:00
|
|
|
if (count > 0 && c == count)
|
|
|
|
break;
|
2013-12-04 14:57:04 +00:00
|
|
|
} else {
|
|
|
|
// encounter a broken instruction
|
2014-04-09 15:49:30 +00:00
|
|
|
// if there is no request to skip data, or remaining data is too small,
|
|
|
|
// then bail out
|
|
|
|
if (!handle->skipdata || handle->skipdata_size > size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (handle->skipdata_setup.callback) {
|
2014-04-14 06:53:13 +00:00
|
|
|
skipdata_bytes = handle->skipdata_setup.callback(buffer, offset - offset_org,
|
2014-04-09 15:49:30 +00:00
|
|
|
handle->skipdata_setup.user_data);
|
|
|
|
if (skipdata_bytes > size)
|
|
|
|
// remaining data is not enough
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!skipdata_bytes)
|
|
|
|
// user requested not to skip data, so bail out
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
skipdata_bytes = handle->skipdata_size;
|
|
|
|
|
|
|
|
// we have to skip some amount of data, depending on arch & mode
|
|
|
|
insn_cache[f].id = 0; // invalid ID for this "data" instruction
|
|
|
|
insn_cache[f].address = offset;
|
|
|
|
insn_cache[f].size = skipdata_bytes;
|
|
|
|
memcpy(insn_cache[f].bytes, buffer, skipdata_bytes);
|
|
|
|
strncpy(insn_cache[f].mnemonic, handle->skipdata_setup.mnemonic,
|
|
|
|
sizeof(insn_cache[f].mnemonic) - 1);
|
|
|
|
skipdata_opstr(insn_cache[f].op_str, buffer, skipdata_bytes);
|
|
|
|
insn_cache[f].detail = NULL;
|
|
|
|
|
|
|
|
f++;
|
|
|
|
if (f == ARR_SIZE(insn_cache)) {
|
|
|
|
// resize total to contain newly disasm insns
|
|
|
|
|
|
|
|
total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
|
|
|
|
tmp = cs_mem_realloc(total, total_size);
|
|
|
|
if (tmp == NULL) { // insufficient memory
|
|
|
|
cs_mem_free(total);
|
|
|
|
handle->errnum = CS_ERR_MEM;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
total = tmp;
|
|
|
|
memcpy((void*)((uintptr_t)total + total_size - sizeof(insn_cache)), insn_cache, sizeof(insn_cache));
|
|
|
|
|
|
|
|
// reset f back to 0
|
|
|
|
f = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer += skipdata_bytes;
|
|
|
|
size -= skipdata_bytes;
|
|
|
|
offset += skipdata_bytes;
|
|
|
|
c++;
|
2013-12-04 14:57:04 +00:00
|
|
|
}
|
2013-11-27 04:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (f) {
|
|
|
|
// resize total to contain newly disasm insns
|
2014-01-11 04:55:31 +00:00
|
|
|
void *tmp = cs_mem_realloc(total, total_size + f * sizeof(insn_cache[0]));
|
2013-11-27 04:11:31 +00:00
|
|
|
if (tmp == NULL) { // insufficient memory
|
2014-01-11 04:55:31 +00:00
|
|
|
cs_mem_free(total);
|
2013-11-27 07:24:47 +00:00
|
|
|
handle->errnum = CS_ERR_MEM;
|
2013-11-27 04:11:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
total = tmp;
|
2014-01-22 17:45:00 +00:00
|
|
|
memcpy((void*)((uintptr_t)total + total_size), insn_cache, f * sizeof(insn_cache[0]));
|
2014-01-23 15:42:40 +00:00
|
|
|
|
2013-11-27 04:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*insn = total;
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2013-12-24 08:49:36 +00:00
|
|
|
void cs_free(cs_insn *insn, size_t count)
|
2013-11-27 04:11:31 +00:00
|
|
|
{
|
2013-12-24 08:49:36 +00:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
// free all detail pointers
|
|
|
|
for (i = 0; i < count; i++)
|
2014-01-11 04:55:31 +00:00
|
|
|
cs_mem_free(insn[i].detail);
|
2013-12-24 08:49:36 +00:00
|
|
|
|
|
|
|
// then free pointer to cs_insn array
|
2014-01-11 04:55:31 +00:00
|
|
|
cs_mem_free(insn);
|
2013-11-27 04:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// return friendly name of regiser in a string
|
2013-12-11 21:14:42 +00:00
|
|
|
const char *cs_reg_name(csh ud, unsigned int reg)
|
2013-11-27 04:11:31 +00:00
|
|
|
{
|
2014-01-23 15:42:40 +00:00
|
|
|
struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
|
2013-11-27 04:11:31 +00:00
|
|
|
|
|
|
|
if (!handle || handle->reg_name == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-12-09 02:26:18 +00:00
|
|
|
return handle->reg_name(ud, reg);
|
2013-11-27 04:11:31 +00:00
|
|
|
}
|
|
|
|
|
2013-12-11 21:14:42 +00:00
|
|
|
const char *cs_insn_name(csh ud, unsigned int insn)
|
2013-11-27 04:11:31 +00:00
|
|
|
{
|
2014-01-23 15:42:40 +00:00
|
|
|
struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
|
2013-11-27 04:11:31 +00:00
|
|
|
|
|
|
|
if (!handle || handle->insn_name == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-12-09 02:26:18 +00:00
|
|
|
return handle->insn_name(ud, insn);
|
2013-11-27 04:11:31 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 14:02:20 +00:00
|
|
|
static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
|
2013-11-27 04:11:31 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < max; i++) {
|
|
|
|
if (arr[i] == id)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-24 08:49:36 +00:00
|
|
|
bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
|
2013-11-27 04:11:31 +00:00
|
|
|
{
|
2014-05-08 23:33:35 +00:00
|
|
|
struct cs_struct *handle = NULL;
|
2013-12-24 08:49:36 +00:00
|
|
|
if (!ud)
|
|
|
|
return false;
|
|
|
|
|
2014-05-08 22:44:49 +00:00
|
|
|
handle = (struct cs_struct *)(uintptr_t)ud;
|
2014-05-07 15:25:38 +00:00
|
|
|
|
2013-12-24 08:49:36 +00:00
|
|
|
if (!handle->detail) {
|
|
|
|
handle->errnum = CS_ERR_DETAIL;
|
2013-11-27 04:11:31 +00:00
|
|
|
return false;
|
2013-12-24 08:49:36 +00:00
|
|
|
}
|
2013-11-27 04:11:31 +00:00
|
|
|
|
2014-05-07 15:31:27 +00:00
|
|
|
if(!insn->id) {
|
|
|
|
handle->errnum = CS_ERR_SKIPDATA;
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-07 15:25:38 +00:00
|
|
|
|
2014-05-07 15:31:27 +00:00
|
|
|
if(!insn->detail) {
|
|
|
|
handle->errnum = CS_ERR_DETAIL;
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-07 15:25:38 +00:00
|
|
|
|
2013-12-24 08:49:36 +00:00
|
|
|
return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
|
2013-11-27 04:11:31 +00:00
|
|
|
}
|
|
|
|
|
2013-12-24 08:49:36 +00:00
|
|
|
bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
|
2013-11-27 04:11:31 +00:00
|
|
|
{
|
2014-05-08 23:33:35 +00:00
|
|
|
struct cs_struct *handle = NULL;
|
2013-12-24 08:49:36 +00:00
|
|
|
if (!ud)
|
2013-11-27 04:11:31 +00:00
|
|
|
return false;
|
|
|
|
|
2014-05-08 22:44:49 +00:00
|
|
|
handle = (struct cs_struct *)(uintptr_t)ud;
|
2014-05-07 15:25:38 +00:00
|
|
|
|
2013-12-24 08:49:36 +00:00
|
|
|
if (!handle->detail) {
|
|
|
|
handle->errnum = CS_ERR_DETAIL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-07 15:31:27 +00:00
|
|
|
if(!insn->id) {
|
|
|
|
handle->errnum = CS_ERR_SKIPDATA;
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-07 15:25:38 +00:00
|
|
|
|
2014-05-07 15:31:27 +00:00
|
|
|
if(!insn->detail) {
|
|
|
|
handle->errnum = CS_ERR_DETAIL;
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-07 15:25:38 +00:00
|
|
|
|
2013-12-24 08:49:36 +00:00
|
|
|
return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
|
2013-11-27 04:11:31 +00:00
|
|
|
}
|
|
|
|
|
2013-12-24 08:49:36 +00:00
|
|
|
bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
|
2013-11-27 04:11:31 +00:00
|
|
|
{
|
2014-05-08 23:33:35 +00:00
|
|
|
struct cs_struct *handle = NULL;
|
2013-12-24 08:49:36 +00:00
|
|
|
if (!ud)
|
|
|
|
return false;
|
|
|
|
|
2014-05-08 22:44:49 +00:00
|
|
|
handle = (struct cs_struct *)(uintptr_t)ud;
|
2014-05-07 15:31:27 +00:00
|
|
|
|
2013-12-24 08:49:36 +00:00
|
|
|
if (!handle->detail) {
|
|
|
|
handle->errnum = CS_ERR_DETAIL;
|
2013-11-27 04:11:31 +00:00
|
|
|
return false;
|
2013-12-24 08:49:36 +00:00
|
|
|
}
|
2013-11-27 04:11:31 +00:00
|
|
|
|
2014-05-07 15:31:27 +00:00
|
|
|
if(!insn->id) {
|
|
|
|
handle->errnum = CS_ERR_SKIPDATA;
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-07 15:25:38 +00:00
|
|
|
|
2014-05-07 15:31:27 +00:00
|
|
|
if(!insn->detail) {
|
|
|
|
handle->errnum = CS_ERR_DETAIL;
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-07 15:25:38 +00:00
|
|
|
|
2013-12-24 08:49:36 +00:00
|
|
|
return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
|
2013-11-27 04:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
|
|
|
|
{
|
2014-05-08 23:33:35 +00:00
|
|
|
struct cs_struct *handle = NULL;
|
|
|
|
unsigned int count = 0, i = 0;
|
2013-11-27 04:11:31 +00:00
|
|
|
if (!ud)
|
|
|
|
return -1;
|
|
|
|
|
2014-05-08 22:44:49 +00:00
|
|
|
handle = (struct cs_struct *)(uintptr_t)ud;
|
2014-05-07 15:25:38 +00:00
|
|
|
|
2014-01-15 13:27:23 +00:00
|
|
|
if (!handle->detail) {
|
|
|
|
handle->errnum = CS_ERR_DETAIL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-05-07 15:31:27 +00:00
|
|
|
if(!insn->id) {
|
|
|
|
handle->errnum = CS_ERR_SKIPDATA;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-05-07 15:25:38 +00:00
|
|
|
|
2014-05-07 15:31:27 +00:00
|
|
|
if(!insn->detail) {
|
|
|
|
handle->errnum = CS_ERR_DETAIL;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-05-07 15:25:38 +00:00
|
|
|
|
2013-11-27 07:24:47 +00:00
|
|
|
handle->errnum = CS_ERR_OK;
|
2013-11-27 04:11:31 +00:00
|
|
|
|
|
|
|
switch (handle->arch) {
|
|
|
|
default:
|
2013-11-27 07:24:47 +00:00
|
|
|
handle->errnum = CS_ERR_HANDLE;
|
2013-11-27 04:11:31 +00:00
|
|
|
return -1;
|
|
|
|
case CS_ARCH_ARM:
|
2013-12-24 08:49:36 +00:00
|
|
|
for (i = 0; i < insn->detail->arm.op_count; i++)
|
2014-01-22 17:45:00 +00:00
|
|
|
if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
|
2013-11-27 04:11:31 +00:00
|
|
|
count++;
|
|
|
|
break;
|
|
|
|
case CS_ARCH_ARM64:
|
2013-12-24 08:49:36 +00:00
|
|
|
for (i = 0; i < insn->detail->arm64.op_count; i++)
|
2014-01-22 17:45:00 +00:00
|
|
|
if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
|
2013-11-27 04:11:31 +00:00
|
|
|
count++;
|
|
|
|
break;
|
|
|
|
case CS_ARCH_X86:
|
2013-12-24 08:49:36 +00:00
|
|
|
for (i = 0; i < insn->detail->x86.op_count; i++)
|
2014-01-22 17:45:00 +00:00
|
|
|
if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
|
2013-11-27 04:11:31 +00:00
|
|
|
count++;
|
|
|
|
break;
|
|
|
|
case CS_ARCH_MIPS:
|
2013-12-24 08:49:36 +00:00
|
|
|
for (i = 0; i < insn->detail->mips.op_count; i++)
|
2014-01-22 17:45:00 +00:00
|
|
|
if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
|
2013-11-27 04:11:31 +00:00
|
|
|
count++;
|
|
|
|
break;
|
2013-12-29 16:15:25 +00:00
|
|
|
case CS_ARCH_PPC:
|
|
|
|
for (i = 0; i < insn->detail->ppc.op_count; i++)
|
2014-01-22 17:45:00 +00:00
|
|
|
if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
|
2013-12-29 16:15:25 +00:00
|
|
|
count++;
|
|
|
|
break;
|
2014-03-10 03:58:57 +00:00
|
|
|
case CS_ARCH_SPARC:
|
|
|
|
for (i = 0; i < insn->detail->sparc.op_count; i++)
|
|
|
|
if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
|
|
|
|
count++;
|
|
|
|
break;
|
2014-03-23 00:35:45 +00:00
|
|
|
case CS_ARCH_SYSZ:
|
|
|
|
for (i = 0; i < insn->detail->sysz.op_count; i++)
|
|
|
|
if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
|
|
|
|
count++;
|
|
|
|
break;
|
2013-11-27 04:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
|
|
|
|
unsigned int post)
|
|
|
|
{
|
2014-05-08 23:33:35 +00:00
|
|
|
struct cs_struct *handle = NULL;
|
|
|
|
unsigned int count = 0, i = 0;
|
2013-11-27 04:11:31 +00:00
|
|
|
if (!ud)
|
|
|
|
return -1;
|
|
|
|
|
2014-05-08 22:44:49 +00:00
|
|
|
handle = (struct cs_struct *)(uintptr_t)ud;
|
2014-05-07 15:25:38 +00:00
|
|
|
|
2014-01-15 13:27:23 +00:00
|
|
|
if (!handle->detail) {
|
|
|
|
handle->errnum = CS_ERR_DETAIL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-05-07 15:31:27 +00:00
|
|
|
if(!insn->id) {
|
|
|
|
handle->errnum = CS_ERR_SKIPDATA;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-05-07 15:25:38 +00:00
|
|
|
|
2014-05-07 15:31:27 +00:00
|
|
|
if(!insn->detail) {
|
|
|
|
handle->errnum = CS_ERR_DETAIL;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-05-07 15:25:38 +00:00
|
|
|
|
2013-11-27 07:24:47 +00:00
|
|
|
handle->errnum = CS_ERR_OK;
|
2013-11-27 04:11:31 +00:00
|
|
|
|
|
|
|
switch (handle->arch) {
|
|
|
|
default:
|
2013-11-27 07:24:47 +00:00
|
|
|
handle->errnum = CS_ERR_HANDLE;
|
2013-11-27 04:11:31 +00:00
|
|
|
return -1;
|
|
|
|
case CS_ARCH_ARM:
|
2013-12-24 08:49:36 +00:00
|
|
|
for (i = 0; i < insn->detail->arm.op_count; i++) {
|
2014-01-22 17:45:00 +00:00
|
|
|
if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
|
2013-11-27 04:11:31 +00:00
|
|
|
count++;
|
|
|
|
if (count == post)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CS_ARCH_ARM64:
|
2013-12-24 08:49:36 +00:00
|
|
|
for (i = 0; i < insn->detail->arm64.op_count; i++) {
|
2014-01-22 17:45:00 +00:00
|
|
|
if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
|
2013-11-27 04:11:31 +00:00
|
|
|
count++;
|
|
|
|
if (count == post)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CS_ARCH_X86:
|
2013-12-24 08:49:36 +00:00
|
|
|
for (i = 0; i < insn->detail->x86.op_count; i++) {
|
2014-01-22 17:45:00 +00:00
|
|
|
if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
|
2013-11-27 04:11:31 +00:00
|
|
|
count++;
|
|
|
|
if (count == post)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CS_ARCH_MIPS:
|
2013-12-24 08:49:36 +00:00
|
|
|
for (i = 0; i < insn->detail->mips.op_count; i++) {
|
2014-01-22 17:45:00 +00:00
|
|
|
if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
|
2013-11-27 04:11:31 +00:00
|
|
|
count++;
|
|
|
|
if (count == post)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
break;
|
2013-12-29 16:15:25 +00:00
|
|
|
case CS_ARCH_PPC:
|
|
|
|
for (i = 0; i < insn->detail->ppc.op_count; i++) {
|
2014-01-22 17:45:00 +00:00
|
|
|
if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
|
2013-12-29 16:15:25 +00:00
|
|
|
count++;
|
|
|
|
if (count == post)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
break;
|
2014-03-10 03:58:57 +00:00
|
|
|
case CS_ARCH_SPARC:
|
|
|
|
for (i = 0; i < insn->detail->sparc.op_count; i++) {
|
|
|
|
if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
|
|
|
|
count++;
|
|
|
|
if (count == post)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
break;
|
2014-03-23 00:35:45 +00:00
|
|
|
case CS_ARCH_SYSZ:
|
|
|
|
for (i = 0; i < insn->detail->sysz.op_count; i++) {
|
|
|
|
if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
|
|
|
|
count++;
|
|
|
|
if (count == post)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
break;
|
2013-11-27 04:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|