blob: 7ca042504e2aad518ffe040dc71b5b34180879b5 [file] [log] [blame]
Nguyen Anh Quynh6023ef72014-04-29 11:21:04 +08001/* Capstone Disassembly Engine */
Nguyen Anh Quynhbfcaba52015-03-04 17:45:23 +08002/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08003
reverserbcf09f42015-04-09 18:28:19 +01004#if defined(CAPSTONE_HAS_OSXKERNEL)
vit969677231752018-06-15 00:12:26 +03005#include <Availability.h>
reverserbcf09f42015-04-09 18:28:19 +01006#include <libkern/libkern.h>
7#else
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +08008#include <stdlib.h>
reverserbcf09f42015-04-09 18:28:19 +01009#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080010#include <string.h>
11
12#include "utils.h"
13
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +080014// create a cache for fast id lookup
Richard Henderson22ead3e2017-10-21 17:45:40 -070015static unsigned short *make_id2insn(const insn_map *insns, unsigned int size)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080016{
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +080017 // NOTE: assume that the max id is always put at the end of insns array
18 unsigned short max_id = insns[size - 1].id;
Alex Ionescu46018db2014-01-22 09:45:00 -080019 unsigned short i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080020
obs1dium33f39e12017-10-13 03:04:16 +020021 unsigned short *cache = (unsigned short *)cs_mem_calloc(max_id + 1, sizeof(*cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080022
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +080023 for (i = 1; i < size; i++)
24 cache[insns[i].id] = i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080025
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +080026 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
Richard Henderson22ead3e2017-10-21 17:45:40 -070031unsigned short insn_find(const insn_map *insns, unsigned int max, unsigned int id, unsigned short **cache)
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +080032{
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];
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080040}
41
Richard Henderson22ead3e2017-10-21 17:45:40 -070042int name2id(const name_map* map, int max, const char *name)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080043{
44 int i;
45
46 for (i = 0; i < max; i++) {
Nguyen Anh Quynh19146e92014-05-28 12:41:31 +080047 if (!strcmp(map[i].name, name)) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080048 return map[i].id;
49 }
50 }
51
52 // nothing match
53 return -1;
54}
55
Nguyen Anh Quynhafffa5d2018-07-20 12:36:50 +080056const char *id2name(const name_map* map, int max, const unsigned int id)
Nguyen Anh Quynh1182d252015-04-27 12:13:34 +080057{
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
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +080070// count number of positive members in a list.
71// NOTE: list must be guaranteed to end in 0
Nguyen Anh Quynhafffa5d2018-07-20 12:36:50 +080072unsigned int count_positive(const uint16_t *list)
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +080073{
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
Nguyen Anh Quynhafffa5d2018-07-20 12:36:50 +080083unsigned int count_positive8(const unsigned char *list)
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +080084{
85 unsigned int c;
86
87 for (c = 0; list[c] > 0; c++);
88
89 return c;
90}
Nguyen Anh Quynha9ffb442014-01-15 18:27:01 +080091
92char *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}
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800102
Bruce Mitchenere8b234d2018-09-17 19:54:00 +0700103// we need this since Windows doesn't have snprintf()
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800104int 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}
Nguyen Anh Quynh58eb0732015-04-02 15:18:33 +0800115
116bool 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
128bool 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