/rust/registry/src/index.crates.io-6f17d22bba15001f/capstone-sys-0.13.0/capstone/utils.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Capstone Disassembly Engine */ |
2 | | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */ |
3 | | |
4 | | #if defined(CAPSTONE_HAS_OSXKERNEL) |
5 | | #include <Availability.h> |
6 | | #include <libkern/libkern.h> |
7 | | #else |
8 | | #include <stdlib.h> |
9 | | #endif |
10 | | #include <string.h> |
11 | | |
12 | | #include "utils.h" |
13 | | |
14 | | // create a cache for fast id lookup |
15 | | static unsigned short *make_id2insn(const insn_map *insns, unsigned int size) |
16 | 0 | { |
17 | | // NOTE: assume that the max id is always put at the end of insns array |
18 | 0 | unsigned short max_id = insns[size - 1].id; |
19 | 0 | unsigned short i; |
20 | |
|
21 | 0 | unsigned short *cache = (unsigned short *)cs_mem_calloc(max_id + 1, sizeof(*cache)); |
22 | |
|
23 | 0 | for (i = 1; i < size; i++) |
24 | 0 | cache[insns[i].id] = i; |
25 | |
|
26 | 0 | return cache; |
27 | 0 | } |
28 | | |
29 | | // look for @id in @insns, given its size in @max. first time call will update @cache. |
30 | | // return 0 if not found |
31 | | unsigned short insn_find(const insn_map *insns, unsigned int max, unsigned int id, unsigned short **cache) |
32 | 0 | { |
33 | 0 | if (id > insns[max - 1].id) |
34 | 0 | return 0; |
35 | | |
36 | 0 | if (*cache == NULL) |
37 | 0 | *cache = make_id2insn(insns, max); |
38 | |
|
39 | 0 | return (*cache)[id]; |
40 | 0 | } |
41 | | |
42 | | int name2id(const name_map* map, int max, const char *name) |
43 | 0 | { |
44 | 0 | int i; |
45 | |
|
46 | 0 | for (i = 0; i < max; i++) { |
47 | 0 | if (!strcmp(map[i].name, name)) { |
48 | 0 | return map[i].id; |
49 | 0 | } |
50 | 0 | } |
51 | | |
52 | | // nothing match |
53 | 0 | return -1; |
54 | 0 | } |
55 | | |
56 | | const char *id2name(const name_map* map, int max, const unsigned int id) |
57 | 0 | { |
58 | 0 | int i; |
59 | |
|
60 | 0 | for (i = 0; i < max; i++) { |
61 | 0 | if (map[i].id == id) { |
62 | 0 | return map[i].name; |
63 | 0 | } |
64 | 0 | } |
65 | | |
66 | | // nothing match |
67 | 0 | return NULL; |
68 | 0 | } |
69 | | |
70 | | // count number of positive members in a list. |
71 | | // NOTE: list must be guaranteed to end in 0 |
72 | | unsigned int count_positive(const uint16_t *list) |
73 | 0 | { |
74 | 0 | unsigned int c; |
75 | |
|
76 | 0 | for (c = 0; list[c] > 0; c++); |
77 | |
|
78 | 0 | return c; |
79 | 0 | } |
80 | | |
81 | | // count number of positive members in a list. |
82 | | // NOTE: list must be guaranteed to end in 0 |
83 | | unsigned int count_positive8(const unsigned char *list) |
84 | 0 | { |
85 | 0 | unsigned int c; |
86 | |
|
87 | 0 | for (c = 0; list[c] > 0; c++); |
88 | |
|
89 | 0 | return c; |
90 | 0 | } |
91 | | |
92 | | char *cs_strdup(const char *str) |
93 | 0 | { |
94 | 0 | size_t len = strlen(str)+ 1; |
95 | 0 | void *new = cs_mem_malloc(len); |
96 | |
|
97 | 0 | if (new == NULL) |
98 | 0 | return NULL; |
99 | | |
100 | 0 | return (char *)memmove(new, str, len); |
101 | 0 | } |
102 | | |
103 | | // we need this since Windows doesn't have snprintf() |
104 | | int cs_snprintf(char *buffer, size_t size, const char *fmt, ...) |
105 | 0 | { |
106 | 0 | int ret; |
107 | |
|
108 | 0 | va_list ap; |
109 | 0 | va_start(ap, fmt); |
110 | 0 | ret = cs_vsnprintf(buffer, size, fmt, ap); |
111 | 0 | va_end(ap); |
112 | |
|
113 | 0 | return ret; |
114 | 0 | } |
115 | | |
116 | | bool arr_exist8(unsigned char *arr, unsigned char max, unsigned int id) |
117 | 0 | { |
118 | 0 | int i; |
119 | |
|
120 | 0 | for (i = 0; i < max; i++) { |
121 | 0 | if (arr[i] == id) |
122 | 0 | return true; |
123 | 0 | } |
124 | | |
125 | 0 | return false; |
126 | 0 | } |
127 | | |
128 | | bool arr_exist(uint16_t *arr, unsigned char max, unsigned int id) |
129 | 0 | { |
130 | 0 | int i; |
131 | |
|
132 | 0 | for (i = 0; i < max; i++) { |
133 | 0 | if (arr[i] == id) |
134 | 0 | return true; |
135 | 0 | } |
136 | | |
137 | 0 | return false; |
138 | 0 | } |
139 | | |
140 | | // binary search for encoding in IndexType array |
141 | | // return -1 if not found, or index if found |
142 | | unsigned int binsearch_IndexTypeEncoding(const struct IndexType *index, size_t size, uint16_t encoding) |
143 | 0 | { |
144 | | // binary searching since the index is sorted in encoding order |
145 | 0 | size_t left, right, m; |
146 | |
|
147 | 0 | right = size - 1; |
148 | |
|
149 | 0 | if (encoding < index[0].encoding || encoding > index[right].encoding) |
150 | | // not found |
151 | 0 | return -1; |
152 | | |
153 | 0 | left = 0; |
154 | |
|
155 | 0 | while(left <= right) { |
156 | 0 | m = (left + right) / 2; |
157 | 0 | if (encoding == index[m].encoding) { |
158 | 0 | return m; |
159 | 0 | } |
160 | | |
161 | 0 | if (encoding < index[m].encoding) |
162 | 0 | right = m - 1; |
163 | 0 | else |
164 | 0 | left = m + 1; |
165 | 0 | } |
166 | | |
167 | | // not found |
168 | 0 | return -1; |
169 | 0 | } |