1 /* Capstone Disassembly Engine */
2 /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */
3 
4 #ifndef CS_UTILS_H
5 #define CS_UTILS_H
6 
7 #if defined(CAPSTONE_HAS_OSXKERNEL)
8 #include <libkern/libkern.h>
9 #else
10 #include <stddef.h>
11 #include "include/capstone/capstone.h"
12 #endif
13 #include "cs_priv.h"
14 
15 // threshold number, so above this number will be printed in hexa mode
16 #define HEX_THRESHOLD 9
17 
18 // map instruction to its characteristics
19 typedef struct insn_map {
20 	unsigned short id;
21 	unsigned short mapid;
22 #ifndef CAPSTONE_DIET
23 	uint16_t regs_use[12]; // list of implicit registers used by this instruction
24 	uint16_t regs_mod[20]; // list of implicit registers modified by this instruction
25 	unsigned char groups[8]; // list of group this instruction belong to
26 	bool branch;	// branch instruction?
27 	bool indirect_branch;	// indirect branch instruction?
28 #endif
29 } insn_map;
30 
31 // look for @id in @m, given its size in @max. first time call will update @cache.
32 // return 0 if not found
33 unsigned short insn_find(const insn_map *m, unsigned int max, unsigned int id, unsigned short **cache);
34 
35 // map id to string
36 typedef struct name_map {
37 	unsigned int id;
38 	const char *name;
39 } name_map;
40 
41 // map a name to its ID
42 // return 0 if not found
43 int name2id(const name_map* map, int max, const char *name);
44 
45 // map ID to a name
46 // return NULL if not found
47 const char *id2name(const name_map* map, int max, const unsigned int id);
48 
49 // count number of positive members in a list.
50 // NOTE: list must be guaranteed to end in 0
51 unsigned int count_positive(const uint16_t *list);
52 unsigned int count_positive8(const unsigned char *list);
53 
54 #define ARR_SIZE(a) (sizeof(a)/sizeof(a[0]))
55 #define MATRIX_SIZE(a) (sizeof(a[0])/sizeof(a[0][0]))
56 
57 char *cs_strdup(const char *str);
58 
59 #define MIN(x, y) ((x) < (y) ? (x) : (y))
60 
61 // we need this since Windows doesn't have snprintf()
62 int cs_snprintf(char *buffer, size_t size, const char *fmt, ...);
63 
64 #define CS_AC_IGNORE (1 << 7)
65 
66 // check if an id is existent in an array
67 bool arr_exist8(unsigned char *arr, unsigned char max, unsigned int id);
68 
69 bool arr_exist(uint16_t *arr, unsigned char max, unsigned int id);
70 
71 struct IndexType {
72 	uint16_t encoding;
73 	unsigned index;
74 };
75 
76 // binary search for encoding in IndexType array
77 // return -1 if not found, or index if found
78 unsigned int binsearch_IndexTypeEncoding(const struct IndexType *index, size_t size, uint16_t encoding);
79 
80 #endif
81 
82