Nguyen Anh Quynh | 6023ef7 | 2014-04-29 11:21:04 +0800 | [diff] [blame] | 1 | /* Capstone Disassembly Engine */ |
Nguyen Anh Quynh | bfcaba5 | 2015-03-04 17:45:23 +0800 | [diff] [blame] | 2 | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */ |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 3 | |
reverser | 160e198 | 2015-04-09 18:28:19 +0100 | [diff] [blame] | 4 | #if defined(CAPSTONE_HAS_OSXKERNEL) |
| 5 | #include <libkern/libkern.h> |
| 6 | #else |
Nguyen Anh Quynh | b265406 | 2014-01-03 17:08:58 +0800 | [diff] [blame] | 7 | #include <stdlib.h> |
reverser | 160e198 | 2015-04-09 18:28:19 +0100 | [diff] [blame] | 8 | #endif |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 9 | #include <string.h> |
| 10 | |
| 11 | #include "utils.h" |
| 12 | |
Nguyen Anh Quynh | b265406 | 2014-01-03 17:08:58 +0800 | [diff] [blame] | 13 | // create a cache for fast id lookup |
| 14 | static unsigned short *make_id2insn(insn_map *insns, unsigned int size) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 15 | { |
Nguyen Anh Quynh | b265406 | 2014-01-03 17:08:58 +0800 | [diff] [blame] | 16 | // NOTE: assume that the max id is always put at the end of insns array |
| 17 | unsigned short max_id = insns[size - 1].id; |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 18 | unsigned short i; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 19 | |
Nguyen Anh Quynh | aaddb25 | 2014-06-17 13:32:37 +0800 | [diff] [blame] | 20 | unsigned short *cache = (unsigned short *)cs_mem_malloc(sizeof(*cache) * (max_id + 1)); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 21 | |
Nguyen Anh Quynh | b265406 | 2014-01-03 17:08:58 +0800 | [diff] [blame] | 22 | for (i = 1; i < size; i++) |
| 23 | cache[insns[i].id] = i; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 24 | |
Nguyen Anh Quynh | b265406 | 2014-01-03 17:08:58 +0800 | [diff] [blame] | 25 | return cache; |
| 26 | } |
| 27 | |
| 28 | // look for @id in @insns, given its size in @max. first time call will update @cache. |
| 29 | // return 0 if not found |
| 30 | unsigned short insn_find(insn_map *insns, unsigned int max, unsigned int id, unsigned short **cache) |
| 31 | { |
| 32 | if (id > insns[max - 1].id) |
| 33 | return 0; |
| 34 | |
| 35 | if (*cache == NULL) |
| 36 | *cache = make_id2insn(insns, max); |
| 37 | |
| 38 | return (*cache)[id]; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 39 | } |
| 40 | |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 41 | int name2id(name_map* map, int max, const char *name) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 42 | { |
| 43 | int i; |
| 44 | |
| 45 | for (i = 0; i < max; i++) { |
Nguyen Anh Quynh | 19146e9 | 2014-05-28 12:41:31 +0800 | [diff] [blame] | 46 | if (!strcmp(map[i].name, name)) { |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 47 | return map[i].id; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // nothing match |
| 52 | return -1; |
| 53 | } |
| 54 | |
Nguyen Anh Quynh | 1182d25 | 2015-04-27 12:13:34 +0800 | [diff] [blame] | 55 | char *id2name(name_map* map, int max, const unsigned int id) |
| 56 | { |
| 57 | int i; |
| 58 | |
| 59 | for (i = 0; i < max; i++) { |
| 60 | if (map[i].id == id) { |
| 61 | return map[i].name; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // nothing match |
| 66 | return NULL; |
| 67 | } |
| 68 | |
Nguyen Anh Quynh | f35e2ad | 2013-12-03 11:10:26 +0800 | [diff] [blame] | 69 | // count number of positive members in a list. |
| 70 | // NOTE: list must be guaranteed to end in 0 |
Nguyen Anh Quynh | efffe78 | 2015-03-25 15:02:13 +0800 | [diff] [blame] | 71 | unsigned int count_positive(uint16_t *list) |
| 72 | { |
| 73 | unsigned int c; |
| 74 | |
| 75 | for (c = 0; list[c] > 0; c++); |
| 76 | |
| 77 | return c; |
| 78 | } |
| 79 | |
| 80 | // count number of positive members in a list. |
| 81 | // NOTE: list must be guaranteed to end in 0 |
| 82 | unsigned int count_positive8(unsigned char *list) |
Nguyen Anh Quynh | f35e2ad | 2013-12-03 11:10:26 +0800 | [diff] [blame] | 83 | { |
| 84 | unsigned int c; |
| 85 | |
| 86 | for (c = 0; list[c] > 0; c++); |
| 87 | |
| 88 | return c; |
| 89 | } |
Nguyen Anh Quynh | a9ffb44 | 2014-01-15 18:27:01 +0800 | [diff] [blame] | 90 | |
| 91 | char *cs_strdup(const char *str) |
| 92 | { |
| 93 | size_t len = strlen(str)+ 1; |
| 94 | void *new = cs_mem_malloc(len); |
| 95 | |
| 96 | if (new == NULL) |
| 97 | return NULL; |
| 98 | |
| 99 | return (char *)memmove(new, str, len); |
| 100 | } |
Nguyen Anh Quynh | 4b6b15f | 2014-08-26 15:57:04 +0800 | [diff] [blame] | 101 | |
| 102 | // we need this since Windows doesnt have snprintf() |
| 103 | int cs_snprintf(char *buffer, size_t size, const char *fmt, ...) |
| 104 | { |
| 105 | int ret; |
| 106 | |
| 107 | va_list ap; |
| 108 | va_start(ap, fmt); |
| 109 | ret = cs_vsnprintf(buffer, size, fmt, ap); |
| 110 | va_end(ap); |
| 111 | |
| 112 | return ret; |
| 113 | } |
Nguyen Anh Quynh | 58eb073 | 2015-04-02 15:18:33 +0800 | [diff] [blame] | 114 | |
| 115 | bool arr_exist8(unsigned char *arr, unsigned char max, unsigned int id) |
| 116 | { |
| 117 | int i; |
| 118 | |
| 119 | for (i = 0; i < max; i++) { |
| 120 | if (arr[i] == id) |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | bool arr_exist(uint16_t *arr, unsigned char max, unsigned int id) |
| 128 | { |
| 129 | int i; |
| 130 | |
| 131 | for (i = 0; i < max; i++) { |
| 132 | if (arr[i] == id) |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | return false; |
| 137 | } |
| 138 | |