forked from rizinorg/rizin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zones.c
143 lines (133 loc) · 2.84 KB
/
zones.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// SPDX-FileCopyrightText: 2016-2020 pancake <pancake@nopcode.org>
// SPDX-License-Identifier: LGPL-3.0-only
#include <rz_flag.h>
#include <rz_util.h>
static RzFlagZoneItem *rz_flag_zone_get(RzFlag *f, const char *name) {
RzListIter *iter;
RzFlagZoneItem *zi;
rz_list_foreach (f->zones, iter, zi) {
if (!strcmp(name, zi->name)) {
return zi;
}
}
return NULL;
}
static RzFlagZoneItem *rz_flag_zone_get_inrange(RzFlag *f, ut64 from, ut64 to) {
RzListIter *iter;
RzFlagZoneItem *zi;
rz_list_foreach (f->zones, iter, zi) {
if (RZ_BETWEEN(from, zi->from, to)) {
return zi;
}
}
return NULL;
}
RZ_API bool rz_flag_zone_add(RzFlag *f, const char *name, ut64 addr) {
rz_return_val_if_fail(f && name && *name, false);
RzFlagZoneItem *zi = rz_flag_zone_get(f, name);
if (zi) {
if (addr < zi->from) {
zi->from = addr;
}
if (addr > zi->to) {
zi->to = addr;
}
} else {
if (!f->zones) {
rz_flag_zone_reset(f);
}
zi = RZ_NEW0(RzFlagZoneItem);
zi->name = strdup(name);
zi->from = zi->to = addr;
rz_list_append(f->zones, zi);
}
return true;
}
RZ_API bool rz_flag_zone_reset(RzFlag *f) {
rz_list_free(f->zones);
f->zones = rz_list_newf(rz_flag_zone_item_free);
return true;
}
RZ_API bool rz_flag_zone_del(RzFlag *f, const char *name) {
RzListIter *iter;
RzFlagZoneItem *zi;
rz_list_foreach (f->zones, iter, zi) {
if (!strcmp(name, zi->name)) {
rz_list_delete(f->zones, iter);
return true;
}
}
return false;
}
RZ_API void rz_flag_zone_item_free(void *a) {
RzFlagZoneItem *zi = a;
free(zi->name);
free(zi);
}
RZ_API bool rz_flag_zone_around(RzFlag *f, ut64 addr, const char **prev, const char **next) {
RzListIter *iter;
RzFlagZoneItem *zi;
*prev = *next = NULL;
ut64 h = UT64_MAX, l = 0LL;
rz_list_foreach (f->zones, iter, zi) {
if (zi->from > addr) {
if (h == UT64_MAX) {
h = zi->from;
*next = zi->name;
} else {
if (zi->from < h) {
h = zi->from;
*next = zi->name;
}
}
}
if (zi->from < addr) {
if (l == UT64_MAX) {
l = zi->from;
*prev = zi->name;
} else {
if (zi->from >= l) {
l = zi->from;
*prev = zi->name;
}
}
}
if (zi->to <= addr) {
if (l == UT64_MAX) {
l = zi->to;
*prev = zi->name;
} else {
if (zi->to >= l) {
l = zi->to;
*prev = zi->name;
}
}
}
if (zi->to > addr) {
if (h == UT64_MAX) {
h = zi->to;
*next = zi->name;
} else {
if (zi->to < h) {
h = zi->to;
*next = zi->name;
}
}
}
}
return true;
}
RZ_API RzList /*<char *>*/ *rz_flag_zone_barlist(RzFlag *f, ut64 from, ut64 bsize, int rows) {
RzList *list = rz_list_newf(NULL);
int i;
for (i = 0; i < rows; i++) {
RzFlagZoneItem *zi = rz_flag_zone_get_inrange(f, from, from + bsize);
if (zi) {
rz_list_append(list, zi->name);
} else {
rz_list_append(list, "");
}
from += bsize;
}
return list;
}