/src/capstonenext/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 | | #include <ctype.h> |
12 | | |
13 | | #include "utils.h" |
14 | | |
15 | | // count number of positive members in a list. |
16 | | // NOTE: list must be guaranteed to end in 0 |
17 | | unsigned int count_positive(const uint16_t *list) |
18 | 7.70M | { |
19 | 7.70M | unsigned int c; |
20 | | |
21 | 10.3M | for (c = 0; list[c] > 0; c++); |
22 | | |
23 | 7.70M | return c; |
24 | 7.70M | } |
25 | | |
26 | | // count number of positive members in a list. |
27 | | // NOTE: list must be guaranteed to end in 0 |
28 | | unsigned int count_positive8(const unsigned char *list) |
29 | 3.85M | { |
30 | 3.85M | unsigned int c; |
31 | | |
32 | 6.82M | for (c = 0; list[c] > 0; c++); |
33 | | |
34 | 3.85M | return c; |
35 | 3.85M | } |
36 | | |
37 | | char *cs_strdup(const char *str) |
38 | 162k | { |
39 | 162k | size_t len = strlen(str) + 1; |
40 | 162k | void *new = cs_mem_malloc(len); |
41 | | |
42 | 162k | if (new == NULL) |
43 | 0 | return NULL; |
44 | | |
45 | 162k | return (char *)memmove(new, str, len); |
46 | 162k | } |
47 | | |
48 | | // we need this since Windows doesn't have snprintf() |
49 | | int cs_snprintf(char *buffer, size_t size, const char *fmt, ...) |
50 | 108k | { |
51 | 108k | int ret; |
52 | | |
53 | 108k | va_list ap; |
54 | 108k | va_start(ap, fmt); |
55 | 108k | ret = cs_vsnprintf(buffer, size, fmt, ap); |
56 | 108k | va_end(ap); |
57 | | |
58 | 108k | return ret; |
59 | 108k | } |
60 | | |
61 | | bool arr_exist8(unsigned char *arr, unsigned char max, unsigned int id) |
62 | 0 | { |
63 | 0 | int i; |
64 | |
|
65 | 0 | for (i = 0; i < max; i++) { |
66 | 0 | if (arr[i] == id) |
67 | 0 | return true; |
68 | 0 | } |
69 | | |
70 | 0 | return false; |
71 | 0 | } |
72 | | |
73 | | bool arr_exist(uint16_t *arr, unsigned char max, unsigned int id) |
74 | 1.35M | { |
75 | 1.35M | int i; |
76 | | |
77 | 1.44M | for (i = 0; i < max; i++) { |
78 | 218k | if (arr[i] == id) |
79 | 130k | return true; |
80 | 218k | } |
81 | | |
82 | 1.22M | return false; |
83 | 1.35M | } |
84 | | |
85 | | /// Reads 4 bytes in the endian order specified in MI->cs->mode. |
86 | | uint32_t readBytes32(MCInst *MI, const uint8_t *Bytes) |
87 | 367k | { |
88 | 367k | assert(MI && Bytes); |
89 | 367k | uint32_t Insn; |
90 | 367k | if (MODE_IS_BIG_ENDIAN(MI->csh->mode)) |
91 | 159k | Insn = (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | |
92 | 159k | ((uint32_t)Bytes[0] << 24); |
93 | 208k | else |
94 | 208k | Insn = ((uint32_t)Bytes[3] << 24) | (Bytes[2] << 16) | |
95 | 208k | (Bytes[1] << 8) | (Bytes[0] << 0); |
96 | 367k | return Insn; |
97 | 367k | } |
98 | | |
99 | | /// Reads 2 bytes in the endian order specified in MI->cs->mode. |
100 | | uint16_t readBytes16(MCInst *MI, const uint8_t *Bytes) |
101 | 0 | { |
102 | 0 | assert(MI && Bytes); |
103 | 0 | uint16_t Insn; |
104 | 0 | if (MODE_IS_BIG_ENDIAN(MI->csh->mode)) |
105 | 0 | Insn = (Bytes[0] << 8) | Bytes[1]; |
106 | 0 | else |
107 | 0 | Insn = (Bytes[1] << 8) | Bytes[0]; |
108 | |
|
109 | 0 | return Insn; |
110 | 0 | } |
111 | | |
112 | | /// @brief Appends the string @p src to the string @p str. @p src is put to lower case. |
113 | | /// @param str The string to append to. |
114 | | /// @param str_size The lengt of @p str |
115 | | /// @param src The string to append. |
116 | 2.35k | void append_to_str_lower(char *str, size_t str_size, const char *src) { |
117 | 2.35k | char *dest = strchr(str, '\0'); |
118 | 2.35k | if (dest - str >= str_size) { |
119 | 0 | assert("str_size does not match actual string length." && 0); |
120 | 0 | return; |
121 | 0 | } |
122 | | |
123 | 2.35k | int i = dest - str; |
124 | 13.9k | for (int j = 0; (i < str_size) && (j < strlen(src)); ++i, ++j) { |
125 | 11.6k | str[i] = tolower(src[j]); |
126 | 11.6k | } |
127 | 2.35k | str[i] = '\0'; |
128 | 2.35k | } |