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 | | // count number of positive members in a list. |
15 | | // NOTE: list must be guaranteed to end in 0 |
16 | | unsigned int count_positive(const uint16_t *list) |
17 | 5.03M | { |
18 | 5.03M | unsigned int c; |
19 | | |
20 | 6.96M | for (c = 0; list[c] > 0; c++); |
21 | | |
22 | 5.03M | return c; |
23 | 5.03M | } |
24 | | |
25 | | // count number of positive members in a list. |
26 | | // NOTE: list must be guaranteed to end in 0 |
27 | | unsigned int count_positive8(const unsigned char *list) |
28 | 2.51M | { |
29 | 2.51M | unsigned int c; |
30 | | |
31 | 4.40M | for (c = 0; list[c] > 0; c++); |
32 | | |
33 | 2.51M | return c; |
34 | 2.51M | } |
35 | | |
36 | | char *cs_strdup(const char *str) |
37 | 114k | { |
38 | 114k | size_t len = strlen(str) + 1; |
39 | 114k | void *new = cs_mem_malloc(len); |
40 | | |
41 | 114k | if (new == NULL) |
42 | 0 | return NULL; |
43 | | |
44 | 114k | return (char *)memmove(new, str, len); |
45 | 114k | } |
46 | | |
47 | | // we need this since Windows doesn't have snprintf() |
48 | | int cs_snprintf(char *buffer, size_t size, const char *fmt, ...) |
49 | 94.1k | { |
50 | 94.1k | int ret; |
51 | | |
52 | 94.1k | va_list ap; |
53 | 94.1k | va_start(ap, fmt); |
54 | 94.1k | ret = cs_vsnprintf(buffer, size, fmt, ap); |
55 | 94.1k | va_end(ap); |
56 | | |
57 | 94.1k | return ret; |
58 | 94.1k | } |
59 | | |
60 | | bool arr_exist8(unsigned char *arr, unsigned char max, unsigned int id) |
61 | 0 | { |
62 | 0 | int i; |
63 | |
|
64 | 0 | for (i = 0; i < max; i++) { |
65 | 0 | if (arr[i] == id) |
66 | 0 | return true; |
67 | 0 | } |
68 | | |
69 | 0 | return false; |
70 | 0 | } |
71 | | |
72 | | bool arr_exist(uint16_t *arr, unsigned char max, unsigned int id) |
73 | 917k | { |
74 | 917k | int i; |
75 | | |
76 | 982k | for (i = 0; i < max; i++) { |
77 | 149k | if (arr[i] == id) |
78 | 84.6k | return true; |
79 | 149k | } |
80 | | |
81 | 832k | return false; |
82 | 917k | } |
83 | | |
84 | | // binary search for encoding in IndexType array |
85 | | // return -1 if not found, or index if found |
86 | | unsigned int binsearch_IndexTypeEncoding(const struct IndexType *index, size_t size, uint16_t encoding) |
87 | 22.8k | { |
88 | | // binary searching since the index is sorted in encoding order |
89 | 22.8k | size_t left, right, m; |
90 | | |
91 | 22.8k | right = size - 1; |
92 | | |
93 | 22.8k | if (encoding < index[0].encoding || encoding > index[right].encoding) |
94 | | // not found |
95 | 5.07k | return -1; |
96 | | |
97 | 17.7k | left = 0; |
98 | | |
99 | 83.0k | while(left <= right) { |
100 | 77.2k | m = (left + right) / 2; |
101 | 77.2k | if (encoding == index[m].encoding) { |
102 | 11.9k | return m; |
103 | 11.9k | } |
104 | | |
105 | 65.2k | if (encoding < index[m].encoding) |
106 | 31.3k | right = m - 1; |
107 | 33.9k | else |
108 | 33.9k | left = m + 1; |
109 | 65.2k | } |
110 | | |
111 | | // not found |
112 | 5.78k | return -1; |
113 | 17.7k | } |
114 | | |
115 | | /// Reads 4 bytes in the endian order specified in MI->cs->mode. |
116 | | uint32_t readBytes32(MCInst *MI, const uint8_t *Bytes) |
117 | 564k | { |
118 | 564k | assert(MI && Bytes); |
119 | 564k | uint32_t Insn; |
120 | 564k | if (MODE_IS_BIG_ENDIAN(MI->csh->mode)) |
121 | 273k | Insn = (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | |
122 | 273k | ((uint32_t)Bytes[0] << 24); |
123 | 290k | else |
124 | 290k | Insn = ((uint32_t)Bytes[3] << 24) | (Bytes[2] << 16) | |
125 | 290k | (Bytes[1] << 8) | (Bytes[0] << 0); |
126 | 564k | return Insn; |
127 | 564k | } |
128 | | |
129 | | /// Reads 2 bytes in the endian order specified in MI->cs->mode. |
130 | | uint16_t readBytes16(MCInst *MI, const uint8_t *Bytes) |
131 | 964k | { |
132 | 964k | assert(MI && Bytes); |
133 | 964k | uint16_t Insn; |
134 | 964k | if (MODE_IS_BIG_ENDIAN(MI->csh->mode)) |
135 | 34.0k | Insn = (Bytes[0] << 8) | Bytes[1]; |
136 | 930k | else |
137 | 930k | Insn = (Bytes[1] << 8) | Bytes[0]; |
138 | | |
139 | 964k | return Insn; |
140 | 964k | } |