/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 | | |
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.00M | { |
18 | 5.00M | unsigned int c; |
19 | | |
20 | 6.62M | for (c = 0; list[c] > 0; c++); |
21 | | |
22 | 5.00M | return c; |
23 | 5.00M | } |
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.50M | { |
29 | 2.50M | unsigned int c; |
30 | | |
31 | 4.02M | for (c = 0; list[c] > 0; c++); |
32 | | |
33 | 2.50M | return c; |
34 | 2.50M | } |
35 | | |
36 | | char *cs_strdup(const char *str) |
37 | 185k | { |
38 | 185k | size_t len = strlen(str) + 1; |
39 | 185k | void *new = cs_mem_malloc(len); |
40 | | |
41 | 185k | if (new == NULL) |
42 | 0 | return NULL; |
43 | | |
44 | 185k | return (char *)memmove(new, str, len); |
45 | 185k | } |
46 | | |
47 | | // we need this since Windows doesn't have snprintf() |
48 | | int cs_snprintf(char *buffer, size_t size, const char *fmt, ...) |
49 | 114k | { |
50 | 114k | int ret; |
51 | | |
52 | 114k | va_list ap; |
53 | 114k | va_start(ap, fmt); |
54 | 114k | ret = cs_vsnprintf(buffer, size, fmt, ap); |
55 | 114k | va_end(ap); |
56 | | |
57 | 114k | return ret; |
58 | 114k | } |
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 | 781k | { |
74 | 781k | int i; |
75 | | |
76 | 815k | for (i = 0; i < max; i++) { |
77 | 90.6k | if (arr[i] == id) |
78 | 56.3k | return true; |
79 | 90.6k | } |
80 | | |
81 | 725k | return false; |
82 | 781k | } |
83 | | |
84 | | /// Reads 4 bytes in the endian order specified in MI->cs->mode. |
85 | | uint32_t readBytes32(MCInst *MI, const uint8_t *Bytes) |
86 | 191k | { |
87 | 191k | assert(MI && Bytes); |
88 | 191k | uint32_t Insn; |
89 | 191k | if (MODE_IS_BIG_ENDIAN(MI->csh->mode)) |
90 | 191k | Insn = (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | |
91 | 191k | ((uint32_t)Bytes[0] << 24); |
92 | 0 | else |
93 | 0 | Insn = ((uint32_t)Bytes[3] << 24) | (Bytes[2] << 16) | |
94 | 0 | (Bytes[1] << 8) | (Bytes[0] << 0); |
95 | 191k | return Insn; |
96 | 191k | } |
97 | | |
98 | | /// Reads 2 bytes in the endian order specified in MI->cs->mode. |
99 | | uint16_t readBytes16(MCInst *MI, const uint8_t *Bytes) |
100 | 0 | { |
101 | 0 | assert(MI && Bytes); |
102 | 0 | uint16_t Insn; |
103 | 0 | if (MODE_IS_BIG_ENDIAN(MI->csh->mode)) |
104 | 0 | Insn = (Bytes[0] << 8) | Bytes[1]; |
105 | 0 | else |
106 | 0 | Insn = (Bytes[1] << 8) | Bytes[0]; |
107 | |
|
108 | 0 | return Insn; |
109 | 0 | } |