blob: d2f5fdcb200a079ad1ab2307baffbc1797485a8b [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
reverser160e1982015-04-09 18:28:19 +01004#if defined(CAPSTONE_HAS_OSXKERNEL)
5#include <libkern/libkern.h>
6#else
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +08007#include <stdlib.h>
reverser160e1982015-04-09 18:28:19 +01008#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08009#include <string.h>
10
11#include "utils.h"
12
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +080013// create a cache for fast id lookup
14static unsigned short *make_id2insn(insn_map *insns, unsigned int size)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080015{
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +080016 // NOTE: assume that the max id is always put at the end of insns array
17 unsigned short max_id = insns[size - 1].id;
Alex Ionescu46018db2014-01-22 09:45:00 -080018 unsigned short i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080019
Nguyen Anh Quynhaaddb252014-06-17 13:32:37 +080020 unsigned short *cache = (unsigned short *)cs_mem_malloc(sizeof(*cache) * (max_id + 1));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080021
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +080022 for (i = 1; i < size; i++)
23 cache[insns[i].id] = i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080024
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +080025 return cache;
26}
27
28// look for @id in @insns, given its size in @max. first time call will update @cache.
29// return 0 if not found
30unsigned short insn_find(insn_map *insns, unsigned int max, unsigned int id, unsigned short **cache)
31{
32 if (id > insns[max - 1].id)
33 return 0;
34
35 if (*cache == NULL)
36 *cache = make_id2insn(insns, max);
37
38 return (*cache)[id];
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080039}
40
pancakef0e4eed2013-12-11 22:14:42 +010041int name2id(name_map* map, int max, const char *name)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080042{
43 int i;
44
45 for (i = 0; i < max; i++) {
Nguyen Anh Quynh19146e92014-05-28 12:41:31 +080046 if (!strcmp(map[i].name, name)) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080047 return map[i].id;
48 }
49 }
50
51 // nothing match
52 return -1;
53}
54
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +080055// count number of positive members in a list.
56// NOTE: list must be guaranteed to end in 0
Nguyen Anh Quynhefffe782015-03-25 15:02:13 +080057unsigned int count_positive(uint16_t *list)
58{
59 unsigned int c;
60
61 for (c = 0; list[c] > 0; c++);
62
63 return c;
64}
65
66// count number of positive members in a list.
67// NOTE: list must be guaranteed to end in 0
68unsigned int count_positive8(unsigned char *list)
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +080069{
70 unsigned int c;
71
72 for (c = 0; list[c] > 0; c++);
73
74 return c;
75}
Nguyen Anh Quynha9ffb442014-01-15 18:27:01 +080076
77char *cs_strdup(const char *str)
78{
79 size_t len = strlen(str)+ 1;
80 void *new = cs_mem_malloc(len);
81
82 if (new == NULL)
83 return NULL;
84
85 return (char *)memmove(new, str, len);
86}
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +080087
88// we need this since Windows doesnt have snprintf()
89int cs_snprintf(char *buffer, size_t size, const char *fmt, ...)
90{
91 int ret;
92
93 va_list ap;
94 va_start(ap, fmt);
95 ret = cs_vsnprintf(buffer, size, fmt, ap);
96 va_end(ap);
97
98 return ret;
99}
Nguyen Anh Quynh58eb0732015-04-02 15:18:33 +0800100
101bool arr_exist8(unsigned char *arr, unsigned char max, unsigned int id)
102{
103 int i;
104
105 for (i = 0; i < max; i++) {
106 if (arr[i] == id)
107 return true;
108 }
109
110 return false;
111}
112
113bool arr_exist(uint16_t *arr, unsigned char max, unsigned int id)
114{
115 int i;
116
117 for (i = 0; i < max; i++) {
118 if (arr[i] == id)
119 return true;
120 }
121
122 return false;
123}
124