| 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 |  | 
|  | 4 | #include <string.h> | 
|  | 5 |  | 
|  | 6 | #include "utils.h" | 
|  | 7 |  | 
|  | 8 | // check to see if a string exists in a list of string ... | 
|  | 9 | bool str_in_list(char **list, char *s) | 
|  | 10 | { | 
|  | 11 | char **l; | 
|  | 12 |  | 
|  | 13 | for(l = list; *l; l++) | 
|  | 14 | if (!strcasecmp(*l, s)) | 
|  | 15 | return true; | 
|  | 16 |  | 
|  | 17 | return false; | 
|  | 18 | } | 
|  | 19 |  | 
|  | 20 | // binary searching | 
|  | 21 | int insn_find(insn_map *m, unsigned int max, unsigned int id) | 
|  | 22 | { | 
|  | 23 | unsigned int i, begin, end; | 
|  | 24 |  | 
|  | 25 | begin = 0; | 
|  | 26 | end = max; | 
|  | 27 |  | 
|  | 28 | while(begin <= end) { | 
|  | 29 | i = (begin + end) / 2; | 
|  | 30 | if (id == m[i].id) | 
|  | 31 | return i; | 
|  | 32 | else if (id < m[i].id) | 
|  | 33 | end = i - 1; | 
|  | 34 | else | 
|  | 35 | begin = i + 1; | 
|  | 36 | } | 
|  | 37 |  | 
|  | 38 | // found nothing | 
|  | 39 | return -1; | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | int name2id(name_map* map, int max, char *name) | 
|  | 43 | { | 
|  | 44 | int i; | 
|  | 45 |  | 
|  | 46 | for (i = 0; i < max; i++) { | 
|  | 47 | if (!strcasecmp(map[i].name, name)) { | 
|  | 48 | return map[i].id; | 
|  | 49 | } | 
|  | 50 | } | 
|  | 51 |  | 
|  | 52 | // nothing match | 
|  | 53 | return -1; | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | unsigned int insn_reverse_id(insn_map *insns, unsigned int max, unsigned int id) | 
|  | 57 | { | 
|  | 58 | unsigned int i; | 
|  | 59 |  | 
|  | 60 | for (i = 0; i < max; i++) { | 
|  | 61 | if (id == insns[i].mapid) | 
|  | 62 | return insns[i].id; | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | // found nothing | 
|  | 66 | return 0; | 
|  | 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 | 
|  | 71 | unsigned int count_positive(unsigned int *list) | 
|  | 72 | { | 
|  | 73 | unsigned int c; | 
|  | 74 |  | 
|  | 75 | for (c = 0; list[c] > 0; c++); | 
|  | 76 |  | 
|  | 77 | return c; | 
|  | 78 | } |