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 */ |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 3 | |
Nguyen Anh Quynh | b265406 | 2014-01-03 17:08:58 +0800 | [diff] [blame] | 4 | #include <stdlib.h> |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 5 | #include <string.h> |
| 6 | |
| 7 | #include "utils.h" |
| 8 | |
Nguyen Anh Quynh | b265406 | 2014-01-03 17:08:58 +0800 | [diff] [blame] | 9 | // create a cache for fast id lookup |
| 10 | static unsigned short *make_id2insn(insn_map *insns, unsigned int size) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 11 | { |
Nguyen Anh Quynh | b265406 | 2014-01-03 17:08:58 +0800 | [diff] [blame] | 12 | // NOTE: assume that the max id is always put at the end of insns array |
| 13 | unsigned short max_id = insns[size - 1].id; |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 14 | unsigned short i; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 15 | |
Nguyen Anh Quynh | aaddb25 | 2014-06-17 13:32:37 +0800 | [diff] [blame] | 16 | 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] | 17 | |
Nguyen Anh Quynh | b265406 | 2014-01-03 17:08:58 +0800 | [diff] [blame] | 18 | for (i = 1; i < size; i++) |
| 19 | cache[insns[i].id] = i; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 20 | |
Nguyen Anh Quynh | b265406 | 2014-01-03 17:08:58 +0800 | [diff] [blame] | 21 | return cache; |
| 22 | } |
| 23 | |
| 24 | // look for @id in @insns, given its size in @max. first time call will update @cache. |
| 25 | // return 0 if not found |
| 26 | unsigned short insn_find(insn_map *insns, unsigned int max, unsigned int id, unsigned short **cache) |
| 27 | { |
| 28 | if (id > insns[max - 1].id) |
| 29 | return 0; |
| 30 | |
| 31 | if (*cache == NULL) |
| 32 | *cache = make_id2insn(insns, max); |
| 33 | |
| 34 | return (*cache)[id]; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 35 | } |
| 36 | |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 37 | int name2id(name_map* map, int max, const char *name) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 38 | { |
| 39 | int i; |
| 40 | |
| 41 | for (i = 0; i < max; i++) { |
Nguyen Anh Quynh | 19146e9 | 2014-05-28 12:41:31 +0800 | [diff] [blame] | 42 | if (!strcmp(map[i].name, name)) { |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 43 | return map[i].id; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // nothing match |
| 48 | return -1; |
| 49 | } |
| 50 | |
Nguyen Anh Quynh | f35e2ad | 2013-12-03 11:10:26 +0800 | [diff] [blame] | 51 | // count number of positive members in a list. |
| 52 | // NOTE: list must be guaranteed to end in 0 |
Nguyen Anh Quynh | 18103e4 | 2013-12-20 17:35:15 +0800 | [diff] [blame] | 53 | unsigned int count_positive(unsigned char *list) |
Nguyen Anh Quynh | f35e2ad | 2013-12-03 11:10:26 +0800 | [diff] [blame] | 54 | { |
| 55 | unsigned int c; |
| 56 | |
| 57 | for (c = 0; list[c] > 0; c++); |
| 58 | |
| 59 | return c; |
| 60 | } |
Nguyen Anh Quynh | a9ffb44 | 2014-01-15 18:27:01 +0800 | [diff] [blame] | 61 | |
| 62 | char *cs_strdup(const char *str) |
| 63 | { |
| 64 | size_t len = strlen(str)+ 1; |
| 65 | void *new = cs_mem_malloc(len); |
| 66 | |
| 67 | if (new == NULL) |
| 68 | return NULL; |
| 69 | |
| 70 | return (char *)memmove(new, str, len); |
| 71 | } |
Nguyen Anh Quynh | 4b6b15f | 2014-08-26 15:57:04 +0800 | [diff] [blame] | 72 | |
| 73 | // we need this since Windows doesnt have snprintf() |
| 74 | int cs_snprintf(char *buffer, size_t size, const char *fmt, ...) |
| 75 | { |
| 76 | int ret; |
| 77 | |
| 78 | va_list ap; |
| 79 | va_start(ap, fmt); |
| 80 | ret = cs_vsnprintf(buffer, size, fmt, ap); |
| 81 | va_end(ap); |
| 82 | |
| 83 | return ret; |
| 84 | } |