forked from capstone-engine/capstone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
81 lines (63 loc) · 1.29 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* Capstone Disassembler Engine */
/* By Nguyen Anh Quynh <[email protected]>, 2013> */
#include <string.h>
#include "utils.h"
// return the position of a string in a list of strings
// or -1 if given string is not in the list
int str_in_list(char **list, char *s)
{
char **l;
int c = 0;
for(l = list; *l; c++, l++) {
if (!strcasecmp(*l, s))
return c;
}
return -1;
}
// binary searching
int insn_find(insn_map *m, unsigned int max, unsigned int id)
{
unsigned int i, begin, end;
begin = 0;
end = max;
while(begin <= end) {
i = (begin + end) / 2;
if (id == m[i].id)
return i;
else if (id < m[i].id)
end = i - 1;
else
begin = i + 1;
}
// found nothing
return -1;
}
int name2id(name_map* map, int max, const char *name)
{
int i;
for (i = 0; i < max; i++) {
if (!strcasecmp(map[i].name, name)) {
return map[i].id;
}
}
// nothing match
return -1;
}
unsigned int insn_reverse_id(insn_map *insns, unsigned int max, unsigned int id)
{
unsigned int i;
for (i = 0; i < max; i++) {
if (id == insns[i].mapid)
return insns[i].id;
}
// found nothing
return 0;
}
// count number of positive members in a list.
// NOTE: list must be guaranteed to end in 0
unsigned int count_positive(unsigned int *list)
{
unsigned int c;
for (c = 0; list[c] > 0; c++);
return c;
}