Nguyen Anh Quynh | 6023ef7 | 2014-04-29 11:21:04 +0800 | [diff] [blame] | 1 | /* Capstone Disassembly Engine */ |
| 2 | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */ |
Axel 0vercl0k Souchet | 605faf1 | 2014-05-09 20:40:00 +0100 | [diff] [blame] | 3 | #if defined (WIN32) || defined (WIN64) || defined (_WIN32) || defined (_WIN64) |
| 4 | #pragma warning(disable:4996) |
| 5 | #endif |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 6 | #include <stddef.h> |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | #include <capstone.h> |
| 11 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 12 | #include "utils.h" |
Nguyen Anh Quynh | c740407 | 2014-01-05 11:35:47 +0800 | [diff] [blame] | 13 | #include "MCRegisterInfo.h" |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 14 | |
Axel 0vercl0k Souchet | 84fecf2 | 2014-05-10 09:49:29 +0100 | [diff] [blame] | 15 | #ifdef CAPSTONE_USE_SYS_DYN_MEM |
Nguyen Anh Quynh | 472a4a4 | 2014-03-06 09:13:04 +0800 | [diff] [blame] | 16 | #define INSN_CACHE_SIZE 32 |
Nguyen Anh Quynh | a836b75 | 2014-03-06 03:36:03 +0800 | [diff] [blame] | 17 | #else |
Nguyen Anh Quynh | 5ee2b45 | 2014-03-07 08:40:35 +0800 | [diff] [blame] | 18 | // reduce stack variable size for kernel/firmware |
Nguyen Anh Quynh | a836b75 | 2014-03-06 03:36:03 +0800 | [diff] [blame] | 19 | #define INSN_CACHE_SIZE 8 |
| 20 | #endif |
Nguyen Anh Quynh | c34959b | 2014-01-22 09:46:42 +0800 | [diff] [blame] | 21 | |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 22 | // default SKIPDATA mnemonic |
Nguyen Anh Quynh | c75a909 | 2014-04-10 10:26:49 +0800 | [diff] [blame] | 23 | #define SKIPDATA_MNEM ".byte" |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 24 | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 25 | cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL }; |
Nguyen Anh Quynh | ee143c8 | 2014-01-03 21:51:59 +0800 | [diff] [blame] | 26 | cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL }; |
| 27 | void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL }; |
Nguyen Anh Quynh | f185180 | 2013-12-21 12:16:47 +0800 | [diff] [blame] | 28 | |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 29 | extern void ARM_enable(void); |
| 30 | extern void AArch64_enable(void); |
| 31 | extern void Mips_enable(void); |
| 32 | extern void X86_enable(void); |
| 33 | extern void PPC_enable(void); |
Nguyen Anh Quynh | 05e2713 | 2014-03-10 11:58:57 +0800 | [diff] [blame] | 34 | extern void Sparc_enable(void); |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 35 | extern void SystemZ_enable(void); |
danghvu | 701b850 | 2014-01-09 11:06:44 +0700 | [diff] [blame] | 36 | |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 37 | static void archs_enable(void) |
Nguyen Anh Quynh | 625b5bc | 2014-01-09 14:33:56 +0800 | [diff] [blame] | 38 | { |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 39 | static bool initialized = false; |
| 40 | |
| 41 | if (initialized) |
| 42 | return; |
| 43 | |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 44 | #ifdef CAPSTONE_HAS_ARM |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 45 | ARM_enable(); |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 46 | #endif |
| 47 | #ifdef CAPSTONE_HAS_ARM64 |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 48 | AArch64_enable(); |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 49 | #endif |
| 50 | #ifdef CAPSTONE_HAS_MIPS |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 51 | Mips_enable(); |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 52 | #endif |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 53 | #ifdef CAPSTONE_HAS_POWERPC |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 54 | PPC_enable(); |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 55 | #endif |
Nguyen Anh Quynh | 05e2713 | 2014-03-10 11:58:57 +0800 | [diff] [blame] | 56 | #ifdef CAPSTONE_HAS_SPARC |
| 57 | Sparc_enable(); |
| 58 | #endif |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 59 | #ifdef CAPSTONE_HAS_SYSZ |
| 60 | SystemZ_enable(); |
| 61 | #endif |
Nguyen Anh Quynh | 05e2713 | 2014-03-10 11:58:57 +0800 | [diff] [blame] | 62 | #ifdef CAPSTONE_HAS_X86 |
| 63 | X86_enable(); |
| 64 | #endif |
Nguyen Anh Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 65 | |
| 66 | initialized = true; |
danghvu | b33bd2c | 2014-01-09 12:22:56 +0700 | [diff] [blame] | 67 | } |
| 68 | |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 69 | unsigned int all_arch = 0; |
Nguyen Anh Quynh | f185180 | 2013-12-21 12:16:47 +0800 | [diff] [blame] | 70 | |
Axel 0vercl0k Souchet | 84fecf2 | 2014-05-10 09:49:29 +0100 | [diff] [blame] | 71 | #ifdef CAPSTONE_USE_SYS_DYN_MEM |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 72 | cs_malloc_t cs_mem_malloc = malloc; |
| 73 | cs_calloc_t cs_mem_calloc = calloc; |
| 74 | cs_realloc_t cs_mem_realloc = realloc; |
| 75 | cs_free_t cs_mem_free = free; |
Nguyen Anh Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 76 | cs_vsnprintf_t cs_vsnprintf = vsnprintf; |
Nguyen Anh Quynh | 59492c2 | 2014-01-05 23:41:31 +0800 | [diff] [blame] | 77 | #else |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 78 | cs_malloc_t cs_mem_malloc = NULL; |
| 79 | cs_calloc_t cs_mem_calloc = NULL; |
| 80 | cs_realloc_t cs_mem_realloc = NULL; |
| 81 | cs_free_t cs_mem_free = NULL; |
Nguyen Anh Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 82 | cs_vsnprintf_t cs_vsnprintf = NULL; |
Nguyen Anh Quynh | 59492c2 | 2014-01-05 23:41:31 +0800 | [diff] [blame] | 83 | #endif |
Nguyen Anh Quynh | b880678 | 2013-12-22 15:20:07 +0800 | [diff] [blame] | 84 | |
Nguyen Anh Quynh | b90cb99 | 2013-12-28 13:59:09 +0800 | [diff] [blame] | 85 | unsigned int cs_version(int *major, int *minor) |
Nguyen Anh Quynh | 36df4bb | 2013-12-10 13:31:20 +0800 | [diff] [blame] | 86 | { |
Nguyen Anh Quynh | c46c35d | 2014-03-05 00:21:53 +0800 | [diff] [blame] | 87 | archs_enable(); |
| 88 | |
Nguyen Anh Quynh | 0877747 | 2013-12-22 14:16:28 +0800 | [diff] [blame] | 89 | if (major != NULL && minor != NULL) { |
| 90 | *major = CS_API_MAJOR; |
| 91 | *minor = CS_API_MINOR; |
| 92 | } |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 93 | |
| 94 | return (CS_API_MAJOR << 8) + CS_API_MINOR; |
| 95 | } |
| 96 | |
Nguyen Anh Quynh | b2870e4 | 2014-02-22 23:41:16 +0800 | [diff] [blame] | 97 | bool cs_support(int query) |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 98 | { |
Nguyen Anh Quynh | c46c35d | 2014-03-05 00:21:53 +0800 | [diff] [blame] | 99 | archs_enable(); |
| 100 | |
Nguyen Anh Quynh | b2870e4 | 2014-02-22 23:41:16 +0800 | [diff] [blame] | 101 | if (query == CS_ARCH_ALL) |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 102 | return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) | |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 103 | (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) | |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 104 | (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) | |
| 105 | (1 << CS_ARCH_SYSZ)); |
Nguyen Anh Quynh | 39a42ed | 2013-12-22 10:40:58 +0800 | [diff] [blame] | 106 | |
Nguyen Anh Quynh | 48d5832 | 2014-02-27 13:54:28 +0800 | [diff] [blame] | 107 | if ((unsigned int)query < CS_ARCH_MAX) |
Nguyen Anh Quynh | b2870e4 | 2014-02-22 23:41:16 +0800 | [diff] [blame] | 108 | 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 Quynh | 59b5489 | 2014-03-27 10:54:44 +0800 | [diff] [blame] | 118 | if (query == CS_SUPPORT_X86_REDUCE) { |
| 119 | #if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE) |
Nguyen Anh Quynh | 9518148 | 2014-03-25 23:20:41 +0800 | [diff] [blame] | 120 | return true; |
| 121 | #else |
| 122 | return false; |
| 123 | #endif |
| 124 | } |
| 125 | |
Nguyen Anh Quynh | b2870e4 | 2014-02-22 23:41:16 +0800 | [diff] [blame] | 126 | // unsupported query |
| 127 | return false; |
Nguyen Anh Quynh | 36df4bb | 2013-12-10 13:31:20 +0800 | [diff] [blame] | 128 | } |
| 129 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 130 | cs_err cs_errno(csh handle) |
| 131 | { |
Nguyen Anh Quynh | 42706a3 | 2014-05-09 07:33:35 +0800 | [diff] [blame] | 132 | struct cs_struct *ud = NULL; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 133 | if (!handle) |
| 134 | return CS_ERR_CSH; |
| 135 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 136 | ud = (struct cs_struct *)(uintptr_t)handle; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 137 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 138 | return ud->errnum; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 139 | } |
| 140 | |
Nguyen Anh Quynh | 34f9638 | 2014-01-03 22:49:07 +0800 | [diff] [blame] | 141 | const 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 Quynh | c52352d | 2014-01-06 09:06:30 +0800 | [diff] [blame] | 162 | case CS_ERR_MEMSETUP: |
| 163 | return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)"; |
Nguyen Anh Quynh | 4f22028 | 2014-02-09 00:19:15 +0800 | [diff] [blame] | 164 | case CS_ERR_VERSION: |
| 165 | return "Different API version between core & binding (CS_ERR_VERSION)"; |
Nguyen Anh Quynh | f7cdbdf | 2014-02-24 16:47:36 +0800 | [diff] [blame] | 166 | case CS_ERR_DIET: |
| 167 | return "Information irrelevant in diet engine (CS_ERR_DIET)"; |
Nguyen Anh Quynh | 11ec881 | 2014-04-10 17:20:01 +0800 | [diff] [blame] | 168 | case CS_ERR_SKIPDATA: |
| 169 | return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)"; |
Nguyen Anh Quynh | 34f9638 | 2014-01-03 22:49:07 +0800 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 173 | cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle) |
| 174 | { |
Nguyen Anh Quynh | 42706a3 | 2014-05-09 07:33:35 +0800 | [diff] [blame] | 175 | cs_err err; |
| 176 | struct cs_struct *ud = NULL; |
Nguyen Anh Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 177 | if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf) |
Nguyen Anh Quynh | c52352d | 2014-01-06 09:06:30 +0800 | [diff] [blame] | 178 | // 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 Quynh | c272e9d | 2014-01-19 12:03:22 +0800 | [diff] [blame] | 182 | archs_enable(); |
| 183 | |
Nguyen Anh Quynh | 8f7ab49 | 2014-01-06 09:52:57 +0800 | [diff] [blame] | 184 | if (arch < CS_ARCH_MAX && arch_init[arch]) { |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 185 | ud = cs_mem_calloc(1, sizeof(*ud)); |
Nguyen Anh Quynh | 8f7ab49 | 2014-01-06 09:52:57 +0800 | [diff] [blame] | 186 | if (!ud) { |
| 187 | // memory insufficient |
| 188 | return CS_ERR_MEM; |
| 189 | } |
| 190 | |
Nguyen Anh Quynh | 9a197b3 | 2013-12-22 13:41:38 +0800 | [diff] [blame] | 191 | ud->errnum = CS_ERR_OK; |
| 192 | ud->arch = arch; |
| 193 | ud->mode = mode; |
| 194 | ud->big_endian = mode & CS_MODE_BIG_ENDIAN; |
Nguyen Anh Quynh | 39b812d | 2014-01-07 23:36:26 +0800 | [diff] [blame] | 195 | // by default, do not break instruction into details |
| 196 | ud->detail = CS_OPT_OFF; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 197 | |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 198 | // default skipdata setup |
| 199 | ud->skipdata_setup.mnemonic = SKIPDATA_MNEM; |
| 200 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 201 | err = arch_init[ud->arch](ud); |
Nguyen Anh Quynh | 06b3c05 | 2014-01-21 15:26:02 +0800 | [diff] [blame] | 202 | if (err) { |
| 203 | cs_mem_free(ud); |
| 204 | *handle = 0; |
| 205 | return err; |
| 206 | } |
Nguyen Anh Quynh | 8f7ab49 | 2014-01-06 09:52:57 +0800 | [diff] [blame] | 207 | |
| 208 | *handle = (uintptr_t)ud; |
| 209 | |
| 210 | return CS_ERR_OK; |
Nguyen Anh Quynh | 9a197b3 | 2013-12-22 13:41:38 +0800 | [diff] [blame] | 211 | } else { |
| 212 | *handle = 0; |
| 213 | return CS_ERR_ARCH; |
| 214 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 215 | } |
| 216 | |
Nguyen Anh Quynh | 226d7dc | 2014-02-27 22:20:39 +0800 | [diff] [blame] | 217 | cs_err cs_close(csh *handle) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 218 | { |
Nguyen Anh Quynh | 42706a3 | 2014-05-09 07:33:35 +0800 | [diff] [blame] | 219 | struct cs_struct *ud = NULL; |
Nguyen Anh Quynh | ef3d04d | 2014-02-27 23:42:49 +0800 | [diff] [blame] | 220 | if (*handle == 0) |
Nguyen Anh Quynh | 226d7dc | 2014-02-27 22:20:39 +0800 | [diff] [blame] | 221 | // invalid handle |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 222 | return CS_ERR_CSH; |
| 223 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 224 | ud = (struct cs_struct *)(*handle); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 225 | |
Nguyen Anh Quynh | fbe10a5 | 2014-02-27 23:59:08 +0800 | [diff] [blame] | 226 | if (ud->printer_info) |
| 227 | cs_mem_free(ud->printer_info); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 228 | |
Nguyen Anh Quynh | 9fac512 | 2014-01-07 10:56:04 +0800 | [diff] [blame] | 229 | // arch_destroy[ud->arch](ud); |
Nguyen Anh Quynh | b265406 | 2014-01-03 17:08:58 +0800 | [diff] [blame] | 230 | |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 231 | cs_mem_free(ud->insn_cache); |
Nguyen Anh Quynh | ee143c8 | 2014-01-03 21:51:59 +0800 | [diff] [blame] | 232 | memset(ud, 0, sizeof(*ud)); |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 233 | cs_mem_free(ud); |
Nguyen Anh Quynh | ee143c8 | 2014-01-03 21:51:59 +0800 | [diff] [blame] | 234 | |
Nguyen Anh Quynh | c8e0785 | 2014-02-28 09:38:11 +0800 | [diff] [blame] | 235 | // invalidate this handle by ZERO out its value. |
| 236 | // this is to make sure it is unusable after cs_close() |
Nguyen Anh Quynh | 226d7dc | 2014-02-27 22:20:39 +0800 | [diff] [blame] | 237 | *handle = 0; |
| 238 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 239 | return CS_ERR_OK; |
| 240 | } |
| 241 | |
| 242 | // fill insn with mnemonic & operands info |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 243 | static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci, |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 244 | PostPrinter_t postprinter, const uint8_t *code) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 245 | { |
Nguyen Anh Quynh | 42706a3 | 2014-05-09 07:33:35 +0800 | [diff] [blame] | 246 | char *sp = NULL; |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 247 | if (handle->detail) { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 248 | // avoiding copy insn->detail |
| 249 | memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail)); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 250 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 251 | // 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 Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 253 | memcpy(insn->detail, (void *)((uintptr_t)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read)), |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 254 | offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read)); |
| 255 | // then copy from @arm until end |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 256 | memcpy((void *)((uintptr_t)(insn->detail) + offsetof(cs_detail, arm)), |
| 257 | (void *)((uintptr_t)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm)), |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 258 | sizeof(cs_detail) - offsetof(cs_detail, arm)); |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 259 | } else { |
| 260 | insn->address = mci->address; |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 261 | insn->size = (uint16_t)mci->insn_size; |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 262 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 263 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 264 | // fill the instruction bytes |
| 265 | memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size)); |
| 266 | |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 267 | // map internal instruction opcode to public insn ID |
| 268 | if (handle->insn_id) |
Nguyen Anh Quynh | 1acfd0b | 2014-01-06 10:56:59 +0800 | [diff] [blame] | 269 | handle->insn_id(handle, insn, MCInst_getOpcode(mci)); |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 270 | |
| 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 Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 276 | if (postprinter) |
| 277 | postprinter((csh)handle, insn, buffer); |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 278 | |
Nguyen Anh Quynh | fc83a43 | 2014-02-22 23:26:27 +0800 | [diff] [blame] | 279 | #ifndef CAPSTONE_DIET |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 280 | // fill in mnemonic & operands |
Nguyen Anh Quynh | defb9bc | 2013-12-12 14:00:12 +0800 | [diff] [blame] | 281 | // find first space or tab |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 282 | sp = buffer; |
Nguyen Anh Quynh | a5ffdc3 | 2014-05-07 08:25:24 +0800 | [diff] [blame] | 283 | for (sp = buffer; *sp; sp++) { |
Nguyen Anh Quynh | defb9bc | 2013-12-12 14:00:12 +0800 | [diff] [blame] | 284 | if (*sp == ' '||*sp == '\t') |
| 285 | break; |
Nguyen Anh Quynh | a5ffdc3 | 2014-05-07 08:25:24 +0800 | [diff] [blame] | 286 | if (*sp == '|') |
| 287 | *sp = ' '; |
| 288 | } |
| 289 | |
Nguyen Anh Quynh | defb9bc | 2013-12-12 14:00:12 +0800 | [diff] [blame] | 290 | if (*sp) { |
| 291 | *sp = '\0'; |
Nguyen Anh Quynh | 86dc393 | 2013-12-12 14:43:39 +0800 | [diff] [blame] | 292 | // 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 Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 296 | insn->op_str[sizeof(insn->op_str) - 1] = '\0'; |
| 297 | } else |
| 298 | insn->op_str[0] = '\0'; |
| 299 | |
Nguyen Anh Quynh | 1dbc959 | 2014-05-07 14:14:07 +0800 | [diff] [blame] | 300 | strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 301 | insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0'; |
Nguyen Anh Quynh | fc83a43 | 2014-02-22 23:26:27 +0800 | [diff] [blame] | 302 | #endif |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 303 | } |
| 304 | |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 305 | // how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)? |
Nguyen Anh Quynh | a89383e | 2014-04-10 11:53:46 +0800 | [diff] [blame] | 306 | // this very much depends on instruction alignment requirement of each arch. |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 307 | static 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 Quynh | a89383e | 2014-04-10 11:53:46 +0800 | [diff] [blame] | 314 | // 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 Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 319 | case CS_ARCH_ARM64: |
| 320 | case CS_ARCH_MIPS: |
| 321 | case CS_ARCH_PPC: |
| 322 | case CS_ARCH_SPARC: |
Nguyen Anh Quynh | a89383e | 2014-04-10 11:53:46 +0800 | [diff] [blame] | 323 | // skip 4 bytes |
| 324 | return 4; |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 325 | case CS_ARCH_SYSZ: |
Nguyen Anh Quynh | a89383e | 2014-04-10 11:53:46 +0800 | [diff] [blame] | 326 | // SystemZ instruction's length can be 2, 4 or 6 bytes, |
| 327 | // so we just skip 2 bytes |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 328 | return 2; |
| 329 | case CS_ARCH_X86: |
| 330 | // X86 has no restriction on instruction alignment |
| 331 | return 1; |
| 332 | } |
| 333 | } |
| 334 | |
Nguyen Anh Quynh | da8adad | 2013-12-04 09:44:07 +0800 | [diff] [blame] | 335 | cs_err cs_option(csh ud, cs_opt_type type, size_t value) |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 336 | { |
Nguyen Anh Quynh | 42706a3 | 2014-05-09 07:33:35 +0800 | [diff] [blame] | 337 | struct cs_struct *handle = NULL; |
Nguyen Anh Quynh | c46c35d | 2014-03-05 00:21:53 +0800 | [diff] [blame] | 338 | archs_enable(); |
| 339 | |
Nguyen Anh Quynh | 24bf0d9 | 2014-01-05 11:19:04 +0800 | [diff] [blame] | 340 | // 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 Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 345 | 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 Quynh | edeeb04 | 2014-01-15 20:44:03 +0800 | [diff] [blame] | 349 | cs_vsnprintf = mem->vsnprintf; |
Nguyen Anh Quynh | 24bf0d9 | 2014-01-05 11:19:04 +0800 | [diff] [blame] | 350 | |
| 351 | return CS_ERR_OK; |
| 352 | } |
| 353 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 354 | handle = (struct cs_struct *)(uintptr_t)ud; |
danghvu | 2b19296 | 2013-12-19 22:40:28 -0600 | [diff] [blame] | 355 | if (!handle) |
| 356 | return CS_ERR_CSH; |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 357 | |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 358 | 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 Quynh | 7d02c92 | 2013-12-21 09:59:31 +0800 | [diff] [blame] | 377 | } |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 378 | |
Nguyen Anh Quynh | d345839 | 2013-12-22 11:10:56 +0800 | [diff] [blame] | 379 | return arch_option[handle->arch](handle, type, value); |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 380 | } |
| 381 | |
Nguyen Anh Quynh | 07ffd64 | 2014-04-10 14:36:08 +0800 | [diff] [blame] | 382 | // generate @op_str for data instruction of SKIPDATA |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 383 | static 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 Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 403 | // dynamicly allocate memory to contain disasm insn |
| 404 | // NOTE: caller must free() the allocated memory itself to avoid memory leaking |
Nguyen Anh Quynh | 04c19be | 2013-12-25 13:26:22 +0800 | [diff] [blame] | 405 | size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 406 | { |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 407 | struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 408 | MCInst mci; |
| 409 | uint16_t insn_size; |
Nguyen Anh Quynh | 7772d85 | 2014-01-21 11:49:25 +0800 | [diff] [blame] | 410 | size_t c = 0; |
| 411 | unsigned int f = 0; |
Nguyen Anh Quynh | c34959b | 2014-01-22 09:46:42 +0800 | [diff] [blame] | 412 | cs_insn insn_cache[INSN_CACHE_SIZE]; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 413 | void *total = NULL; |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 414 | size_t total_size = 0; |
Nguyen Anh Quynh | 034a748 | 2014-03-06 22:40:08 +0800 | [diff] [blame] | 415 | bool r; |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 416 | void *tmp; |
| 417 | size_t skipdata_bytes; |
Nguyen Anh Quynh | 07ffd64 | 2014-04-10 14:36:08 +0800 | [diff] [blame] | 418 | uint64_t offset_org; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 419 | |
Nguyen Anh Quynh | 5848aaa | 2014-02-27 11:10:41 +0800 | [diff] [blame] | 420 | if (!handle) { |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 421 | // FIXME: how to handle this case: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 422 | // handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 423 | return 0; |
| 424 | } |
| 425 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 426 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 427 | |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 428 | memset(insn_cache, 0, sizeof(insn_cache)); |
| 429 | |
Nguyen Anh Quynh | 2cff6f6 | 2014-04-28 11:19:44 +0800 | [diff] [blame] | 430 | // save the original offset for SKIPDATA |
Nguyen Anh Quynh | 07ffd64 | 2014-04-10 14:36:08 +0800 | [diff] [blame] | 431 | offset_org = offset; |
Nguyen Anh Quynh | 2cff6f6 | 2014-04-28 11:19:44 +0800 | [diff] [blame] | 432 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 433 | while (size > 0) { |
danghvu | 2b19296 | 2013-12-19 22:40:28 -0600 | [diff] [blame] | 434 | MCInst_Init(&mci); |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 435 | mci.csh = handle; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 436 | |
Nguyen Anh Quynh | a5ffdc3 | 2014-05-07 08:25:24 +0800 | [diff] [blame] | 437 | r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 438 | if (r) { |
| 439 | SStream ss; |
| 440 | SStream_Init(&ss); |
| 441 | |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 442 | // relative branches need to know the address & size of current insn |
| 443 | mci.insn_size = insn_size; |
| 444 | mci.address = offset; |
| 445 | |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 446 | if (handle->detail) { |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame] | 447 | // save all the information for non-detailed mode |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 448 | mci.flat_insn.address = offset; |
| 449 | mci.flat_insn.size = insn_size; |
| 450 | // allocate memory for @detail pointer |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 451 | insn_cache[f].detail = cs_mem_calloc(1, sizeof(cs_detail)); |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 452 | } |
| 453 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 454 | handle->printer(&mci, &ss, handle->printer_info); |
| 455 | |
Joxean | 114df0e | 2013-12-04 07:11:32 +0100 | [diff] [blame] | 456 | fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer); |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 457 | |
Nguyen Anh Quynh | a5ffdc3 | 2014-05-07 08:25:24 +0800 | [diff] [blame] | 458 | 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 Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 467 | } |
| 468 | |
Nguyen Anh Quynh | a5ffdc3 | 2014-05-07 08:25:24 +0800 | [diff] [blame] | 469 | 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 Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 474 | } |
| 475 | |
Nguyen Anh Quynh | a5ffdc3 | 2014-05-07 08:25:24 +0800 | [diff] [blame] | 476 | c++; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 477 | buffer += insn_size; |
Nguyen Anh Quynh | 2cff6f6 | 2014-04-28 11:19:44 +0800 | [diff] [blame] | 478 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 479 | size -= insn_size; |
| 480 | offset += insn_size; |
| 481 | |
Nguyen Anh Quynh | a5ffdc3 | 2014-05-07 08:25:24 +0800 | [diff] [blame] | 482 | if (count > 0 && c == count) |
| 483 | break; |
Nguyen Anh Quynh | 8f13f3c | 2013-12-04 22:57:04 +0800 | [diff] [blame] | 484 | } else { |
| 485 | // encounter a broken instruction |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 486 | // 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 Quynh | 42288ac | 2014-04-14 14:53:13 +0800 | [diff] [blame] | 492 | skipdata_bytes = handle->skipdata_setup.callback(buffer, offset - offset_org, |
Nguyen Anh Quynh | d3ffe37 | 2014-04-09 23:49:30 +0800 | [diff] [blame] | 493 | 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 Quynh | 8f13f3c | 2013-12-04 22:57:04 +0800 | [diff] [blame] | 537 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | if (f) { |
| 541 | // resize total to contain newly disasm insns |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 542 | void *tmp = cs_mem_realloc(total, total_size + f * sizeof(insn_cache[0])); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 543 | if (tmp == NULL) { // insufficient memory |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 544 | cs_mem_free(total); |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 545 | handle->errnum = CS_ERR_MEM; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | total = tmp; |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 550 | memcpy((void*)((uintptr_t)total + total_size), insn_cache, f * sizeof(insn_cache[0])); |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 551 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | *insn = total; |
| 555 | |
| 556 | return c; |
| 557 | } |
| 558 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 559 | void cs_free(cs_insn *insn, size_t count) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 560 | { |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 561 | size_t i; |
| 562 | |
| 563 | // free all detail pointers |
| 564 | for (i = 0; i < count; i++) |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 565 | cs_mem_free(insn[i].detail); |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 566 | |
| 567 | // then free pointer to cs_insn array |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 568 | cs_mem_free(insn); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | // return friendly name of regiser in a string |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 572 | const char *cs_reg_name(csh ud, unsigned int reg) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 573 | { |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 574 | struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 575 | |
| 576 | if (!handle || handle->reg_name == NULL) { |
| 577 | return NULL; |
| 578 | } |
| 579 | |
Nguyen Anh Quynh | a253c7a | 2013-12-09 10:26:18 +0800 | [diff] [blame] | 580 | return handle->reg_name(ud, reg); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 581 | } |
| 582 | |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 583 | const char *cs_insn_name(csh ud, unsigned int insn) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 584 | { |
Nguyen Anh Quynh | a82a089 | 2014-01-23 23:42:40 +0800 | [diff] [blame] | 585 | struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 586 | |
| 587 | if (!handle || handle->insn_name == NULL) { |
| 588 | return NULL; |
| 589 | } |
| 590 | |
Nguyen Anh Quynh | a253c7a | 2013-12-09 10:26:18 +0800 | [diff] [blame] | 591 | return handle->insn_name(ud, insn); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 592 | } |
| 593 | |
Nguyen Anh Quynh | 7008356 | 2013-12-20 22:02:20 +0800 | [diff] [blame] | 594 | static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 595 | { |
| 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 Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 606 | bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 607 | { |
Nguyen Anh Quynh | 42706a3 | 2014-05-09 07:33:35 +0800 | [diff] [blame] | 608 | struct cs_struct *handle = NULL; |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 609 | if (!ud) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 610 | return false; |
| 611 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 612 | handle = (struct cs_struct *)(uintptr_t)ud; |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 613 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 614 | if (!handle->detail) { |
| 615 | handle->errnum = CS_ERR_DETAIL; |
| 616 | return false; |
| 617 | } |
| 618 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 619 | if(!insn->id) { |
| 620 | handle->errnum = CS_ERR_SKIPDATA; |
| 621 | return false; |
| 622 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 623 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 624 | if(!insn->detail) { |
| 625 | handle->errnum = CS_ERR_DETAIL; |
| 626 | return false; |
| 627 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 628 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 629 | return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 630 | } |
| 631 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 632 | bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 633 | { |
Nguyen Anh Quynh | 42706a3 | 2014-05-09 07:33:35 +0800 | [diff] [blame] | 634 | struct cs_struct *handle = NULL; |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 635 | if (!ud) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 636 | return false; |
| 637 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 638 | handle = (struct cs_struct *)(uintptr_t)ud; |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 639 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 640 | if (!handle->detail) { |
| 641 | handle->errnum = CS_ERR_DETAIL; |
| 642 | return false; |
| 643 | } |
| 644 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 645 | if(!insn->id) { |
| 646 | handle->errnum = CS_ERR_SKIPDATA; |
| 647 | return false; |
| 648 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 649 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 650 | if(!insn->detail) { |
| 651 | handle->errnum = CS_ERR_DETAIL; |
| 652 | return false; |
| 653 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 654 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 655 | return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 656 | } |
| 657 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 658 | bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 659 | { |
Nguyen Anh Quynh | 42706a3 | 2014-05-09 07:33:35 +0800 | [diff] [blame] | 660 | struct cs_struct *handle = NULL; |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 661 | if (!ud) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 662 | return false; |
| 663 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 664 | handle = (struct cs_struct *)(uintptr_t)ud; |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 665 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 666 | if (!handle->detail) { |
| 667 | handle->errnum = CS_ERR_DETAIL; |
| 668 | return false; |
| 669 | } |
| 670 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 671 | if(!insn->id) { |
| 672 | handle->errnum = CS_ERR_SKIPDATA; |
| 673 | return false; |
| 674 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 675 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 676 | if(!insn->detail) { |
| 677 | handle->errnum = CS_ERR_DETAIL; |
| 678 | return false; |
| 679 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 680 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 681 | return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type) |
| 685 | { |
Nguyen Anh Quynh | 42706a3 | 2014-05-09 07:33:35 +0800 | [diff] [blame] | 686 | struct cs_struct *handle = NULL; |
| 687 | unsigned int count = 0, i = 0; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 688 | if (!ud) |
| 689 | return -1; |
| 690 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 691 | handle = (struct cs_struct *)(uintptr_t)ud; |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 692 | |
Nguyen Anh Quynh | 24e1227 | 2014-01-15 21:27:23 +0800 | [diff] [blame] | 693 | if (!handle->detail) { |
| 694 | handle->errnum = CS_ERR_DETAIL; |
| 695 | return -1; |
| 696 | } |
| 697 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 698 | if(!insn->id) { |
| 699 | handle->errnum = CS_ERR_SKIPDATA; |
| 700 | return -1; |
| 701 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 702 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 703 | if(!insn->detail) { |
| 704 | handle->errnum = CS_ERR_DETAIL; |
| 705 | return -1; |
| 706 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 707 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 708 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 709 | |
| 710 | switch (handle->arch) { |
| 711 | default: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 712 | handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 713 | return -1; |
| 714 | case CS_ARCH_ARM: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 715 | for (i = 0; i < insn->detail->arm.op_count; i++) |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 716 | if (insn->detail->arm.operands[i].type == (arm_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 717 | count++; |
| 718 | break; |
| 719 | case CS_ARCH_ARM64: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 720 | for (i = 0; i < insn->detail->arm64.op_count; i++) |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 721 | if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 722 | count++; |
| 723 | break; |
| 724 | case CS_ARCH_X86: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 725 | for (i = 0; i < insn->detail->x86.op_count; i++) |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 726 | if (insn->detail->x86.operands[i].type == (x86_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 727 | count++; |
| 728 | break; |
| 729 | case CS_ARCH_MIPS: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 730 | for (i = 0; i < insn->detail->mips.op_count; i++) |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 731 | if (insn->detail->mips.operands[i].type == (mips_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 732 | count++; |
| 733 | break; |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 734 | case CS_ARCH_PPC: |
| 735 | for (i = 0; i < insn->detail->ppc.op_count; i++) |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 736 | if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type) |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 737 | count++; |
| 738 | break; |
Nguyen Anh Quynh | 05e2713 | 2014-03-10 11:58:57 +0800 | [diff] [blame] | 739 | 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 Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 744 | 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 Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | return count; |
| 752 | } |
| 753 | |
| 754 | int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type, |
| 755 | unsigned int post) |
| 756 | { |
Nguyen Anh Quynh | 42706a3 | 2014-05-09 07:33:35 +0800 | [diff] [blame] | 757 | struct cs_struct *handle = NULL; |
| 758 | unsigned int count = 0, i = 0; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 759 | if (!ud) |
| 760 | return -1; |
| 761 | |
Axel 0vercl0k Souchet | 779d4c7 | 2014-05-08 23:44:49 +0100 | [diff] [blame] | 762 | handle = (struct cs_struct *)(uintptr_t)ud; |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 763 | |
Nguyen Anh Quynh | 24e1227 | 2014-01-15 21:27:23 +0800 | [diff] [blame] | 764 | if (!handle->detail) { |
| 765 | handle->errnum = CS_ERR_DETAIL; |
| 766 | return -1; |
| 767 | } |
| 768 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 769 | if(!insn->id) { |
| 770 | handle->errnum = CS_ERR_SKIPDATA; |
| 771 | return -1; |
| 772 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 773 | |
Giovanni Condello | 95657e0 | 2014-05-07 17:31:27 +0200 | [diff] [blame] | 774 | if(!insn->detail) { |
| 775 | handle->errnum = CS_ERR_DETAIL; |
| 776 | return -1; |
| 777 | } |
Giovanni Condello | a715df1 | 2014-05-07 17:25:38 +0200 | [diff] [blame] | 778 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 779 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 780 | |
| 781 | switch (handle->arch) { |
| 782 | default: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 783 | handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 784 | return -1; |
| 785 | case CS_ARCH_ARM: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 786 | for (i = 0; i < insn->detail->arm.op_count; i++) { |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 787 | if (insn->detail->arm.operands[i].type == (arm_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 788 | count++; |
| 789 | if (count == post) |
| 790 | return i; |
| 791 | } |
| 792 | break; |
| 793 | case CS_ARCH_ARM64: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 794 | for (i = 0; i < insn->detail->arm64.op_count; i++) { |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 795 | if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 796 | count++; |
| 797 | if (count == post) |
| 798 | return i; |
| 799 | } |
| 800 | break; |
| 801 | case CS_ARCH_X86: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 802 | for (i = 0; i < insn->detail->x86.op_count; i++) { |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 803 | if (insn->detail->x86.operands[i].type == (x86_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 804 | count++; |
| 805 | if (count == post) |
| 806 | return i; |
| 807 | } |
| 808 | break; |
| 809 | case CS_ARCH_MIPS: |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 810 | for (i = 0; i < insn->detail->mips.op_count; i++) { |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 811 | if (insn->detail->mips.operands[i].type == (mips_op_type)op_type) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 812 | count++; |
| 813 | if (count == post) |
| 814 | return i; |
| 815 | } |
| 816 | break; |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 817 | case CS_ARCH_PPC: |
| 818 | for (i = 0; i < insn->detail->ppc.op_count; i++) { |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 819 | if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type) |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 820 | count++; |
| 821 | if (count == post) |
| 822 | return i; |
| 823 | } |
| 824 | break; |
Nguyen Anh Quynh | 05e2713 | 2014-03-10 11:58:57 +0800 | [diff] [blame] | 825 | 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 Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 833 | 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 Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | return -1; |
| 844 | } |