Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 1 | /* Capstone Disassembler Engine */ |
| 2 | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */ |
| 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 | bed9091 | 2013-12-13 18:28:38 +0800 | [diff] [blame] | 9 | // return the position of a string in a list of strings |
| 10 | // or -1 if given string is not in the list |
| 11 | int str_in_list(char **list, char *s) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 12 | { |
| 13 | char **l; |
| 14 | |
Nguyen Anh Quynh | bed9091 | 2013-12-13 18:28:38 +0800 | [diff] [blame] | 15 | int c = 0; |
| 16 | for(l = list; *l; c++, l++) { |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 17 | if (!strcasecmp(*l, s)) |
Nguyen Anh Quynh | bed9091 | 2013-12-13 18:28:38 +0800 | [diff] [blame] | 18 | return c; |
| 19 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 20 | |
Nguyen Anh Quynh | bed9091 | 2013-12-13 18:28:38 +0800 | [diff] [blame] | 21 | return -1; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 22 | } |
| 23 | |
Nguyen Anh Quynh | b265406 | 2014-01-03 17:08:58 +0800 | [diff] [blame] | 24 | // create a cache for fast id lookup |
| 25 | static unsigned short *make_id2insn(insn_map *insns, unsigned int size) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 26 | { |
Nguyen Anh Quynh | b265406 | 2014-01-03 17:08:58 +0800 | [diff] [blame] | 27 | // NOTE: assume that the max id is always put at the end of insns array |
| 28 | unsigned short max_id = insns[size - 1].id; |
Alex Ionescu | 46018db | 2014-01-22 09:45:00 -0800 | [diff] [blame] | 29 | unsigned short i; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 30 | |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 31 | unsigned short *cache = (unsigned short *)cs_mem_calloc(sizeof(*cache), max_id + 1); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 32 | |
Nguyen Anh Quynh | b265406 | 2014-01-03 17:08:58 +0800 | [diff] [blame] | 33 | for (i = 1; i < size; i++) |
| 34 | cache[insns[i].id] = i; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 35 | |
Nguyen Anh Quynh | b265406 | 2014-01-03 17:08:58 +0800 | [diff] [blame] | 36 | return cache; |
| 37 | } |
| 38 | |
| 39 | // look for @id in @insns, given its size in @max. first time call will update @cache. |
| 40 | // return 0 if not found |
| 41 | unsigned short insn_find(insn_map *insns, unsigned int max, unsigned int id, unsigned short **cache) |
| 42 | { |
| 43 | if (id > insns[max - 1].id) |
| 44 | return 0; |
| 45 | |
| 46 | if (*cache == NULL) |
| 47 | *cache = make_id2insn(insns, max); |
| 48 | |
| 49 | return (*cache)[id]; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 50 | } |
| 51 | |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 52 | int name2id(name_map* map, int max, const char *name) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 53 | { |
| 54 | int i; |
| 55 | |
| 56 | for (i = 0; i < max; i++) { |
| 57 | if (!strcasecmp(map[i].name, name)) { |
| 58 | return map[i].id; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // nothing match |
| 63 | return -1; |
| 64 | } |
| 65 | |
Nguyen Anh Quynh | f35e2ad | 2013-12-03 11:10:26 +0800 | [diff] [blame] | 66 | // count number of positive members in a list. |
| 67 | // NOTE: list must be guaranteed to end in 0 |
Nguyen Anh Quynh | 18103e4 | 2013-12-20 17:35:15 +0800 | [diff] [blame] | 68 | unsigned int count_positive(unsigned char *list) |
Nguyen Anh Quynh | f35e2ad | 2013-12-03 11:10:26 +0800 | [diff] [blame] | 69 | { |
| 70 | unsigned int c; |
| 71 | |
| 72 | for (c = 0; list[c] > 0; c++); |
| 73 | |
| 74 | return c; |
| 75 | } |
Nguyen Anh Quynh | a9ffb44 | 2014-01-15 18:27:01 +0800 | [diff] [blame] | 76 | |
| 77 | char *cs_strdup(const char *str) |
| 78 | { |
| 79 | size_t len = strlen(str)+ 1; |
| 80 | void *new = cs_mem_malloc(len); |
| 81 | |
| 82 | if (new == NULL) |
| 83 | return NULL; |
| 84 | |
| 85 | return (char *)memmove(new, str, len); |
| 86 | } |