utils.c
Go to the documentation of this file.
1 /* Capstone Disassembly Engine */
2 /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */
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 {
17  // NOTE: assume that the max id is always put at the end of insns array
18  unsigned short max_id = insns[size - 1].id;
19  unsigned short i;
20 
21  unsigned short *cache = (unsigned short *)cs_mem_calloc(max_id + 1, sizeof(*cache));
22 
23  for (i = 1; i < size; i++)
24  cache[insns[i].id] = i;
25 
26  return cache;
27 }
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 {
33  if (id > insns[max - 1].id)
34  return 0;
35 
36  if (*cache == NULL)
37  *cache = make_id2insn(insns, max);
38 
39  return (*cache)[id];
40 }
41 
42 int name2id(const name_map* map, int max, const char *name)
43 {
44  int i;
45 
46  for (i = 0; i < max; i++) {
47  if (!strcmp(map[i].name, name)) {
48  return map[i].id;
49  }
50  }
51 
52  // nothing match
53  return -1;
54 }
55 
56 const char *id2name(const name_map* map, int max, const unsigned int id)
57 {
58  int i;
59 
60  for (i = 0; i < max; i++) {
61  if (map[i].id == id) {
62  return map[i].name;
63  }
64  }
65 
66  // nothing match
67  return NULL;
68 }
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 {
74  unsigned int c;
75 
76  for (c = 0; list[c] > 0; c++);
77 
78  return c;
79 }
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 {
85  unsigned int c;
86 
87  for (c = 0; list[c] > 0; c++);
88 
89  return c;
90 }
91 
92 char *cs_strdup(const char *str)
93 {
94  size_t len = strlen(str)+ 1;
95  void *new = cs_mem_malloc(len);
96 
97  if (new == NULL)
98  return NULL;
99 
100  return (char *)memmove(new, str, len);
101 }
102 
103 // we need this since Windows doesn't have snprintf()
104 int cs_snprintf(char *buffer, size_t size, const char *fmt, ...)
105 {
106  int ret;
107 
108  va_list ap;
109  va_start(ap, fmt);
110  ret = cs_vsnprintf(buffer, size, fmt, ap);
111  va_end(ap);
112 
113  return ret;
114 }
115 
116 bool arr_exist8(unsigned char *arr, unsigned char max, unsigned int id)
117 {
118  int i;
119 
120  for (i = 0; i < max; i++) {
121  if (arr[i] == id)
122  return true;
123  }
124 
125  return false;
126 }
127 
128 bool arr_exist(uint16_t *arr, unsigned char max, unsigned int id)
129 {
130  int i;
131 
132  for (i = 0; i < max; i++) {
133  if (arr[i] == id)
134  return true;
135  }
136 
137  return false;
138 }
139 
xds_interop_client.str
str
Definition: xds_interop_client.py:487
insn_find
unsigned short insn_find(const insn_map *insns, unsigned int max, unsigned int id, unsigned short **cache)
Definition: utils.c:31
cs_mem_malloc
cs_malloc_t cs_mem_malloc
Definition: cs.c:368
arr_exist
bool arr_exist(uint16_t *arr, unsigned char max, unsigned int id)
Definition: utils.c:128
uint16_t
unsigned short uint16_t
Definition: stdint-msvc2008.h:79
string.h
name2id
int name2id(const name_map *map, int max, const char *name)
Definition: utils.c:42
setup.name
name
Definition: setup.py:542
count_positive
unsigned int count_positive(const uint16_t *list)
Definition: utils.c:72
map
zval * map
Definition: php/ext/google/protobuf/encode_decode.c:480
id2name
const char * id2name(const name_map *map, int max, const unsigned int id)
Definition: utils.c:56
insn_map
Definition: utils.h:19
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
utils.h
cs_snprintf
int cs_snprintf(char *buffer, size_t size, const char *fmt,...)
Definition: utils.c:104
arr_exist8
bool arr_exist8(unsigned char *arr, unsigned char max, unsigned int id)
Definition: utils.c:116
cs_vsnprintf
cs_vsnprintf_t cs_vsnprintf
Definition: cs.c:372
buffer
char buffer[1024]
Definition: libuv/docs/code/idle-compute/main.c:8
count_positive8
unsigned int count_positive8(const unsigned char *list)
Definition: utils.c:83
testing::internal::fmt
GTEST_API_ const char * fmt
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1808
insn_map::id
unsigned short id
Definition: utils.h:20
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
make_id2insn
static unsigned short * make_id2insn(const insn_map *insns, unsigned int size)
Definition: utils.c:15
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
cs_mem_calloc
cs_calloc_t cs_mem_calloc
Definition: cs.c:369
name_map
Definition: utils.h:36
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
id
uint32_t id
Definition: flow_control_fuzzer.cc:70
cs_strdup
char * cs_strdup(const char *str)
Definition: utils.c:92


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:48