Cleanup flag zones code

This commit is contained in:
Anton Kochkov 2021-03-10 14:30:12 +08:00 committed by Anton Kochkov
parent 820615969a
commit b37d6fbe0e
4 changed files with 0 additions and 174 deletions

View file

@ -229,19 +229,11 @@ RZ_API RzFlag *rz_flag_new(void) {
}
f->base = 0;
f->cb_printf = (PrintfCallback)printf;
#if RZ_FLAG_ZONE_USE_SDB
f->zones = sdb_new0();
#else
f->zones = NULL;
#endif
f->tags = sdb_new0();
f->ht_name = ht_pp_new(NULL, ht_free_flag, NULL);
f->by_off = rz_skiplist_new(flag_skiplist_free, flag_skiplist_cmp);
#if RZ_FLAG_ZONE_USE_SDB
sdb_free(f->zones);
#else
rz_list_free(f->zones);
#endif
new_spaces(f);
return f;
}

View file

@ -4,10 +4,6 @@
#include <rz_util/rz_serialize.h>
#include <rz_flag.h>
#if RZ_FLAG_ZONE_USE_SDB
#error "RZ_FLAG_ZONE_USE_SDB not supported by rz_serialize"
#endif
/*
* SDB Format:
*

View file

@ -6,8 +6,6 @@
#define DB f->zones
#if !RZ_FLAG_ZONE_USE_SDB
static RzFlagZoneItem *rz_flag_zone_get(RzFlag *f, const char *name) {
RzListIter *iter;
RzFlagZoneItem *zi;
@ -18,7 +16,6 @@ static RzFlagZoneItem *rz_flag_zone_get(RzFlag *f, const char *name) {
}
return NULL;
}
#endif
static RzFlagZoneItem *rz_flag_zone_get_inrange(RzFlag *f, ut64 from, ut64 to) {
RzListIter *iter;
@ -33,27 +30,6 @@ static RzFlagZoneItem *rz_flag_zone_get_inrange(RzFlag *f, ut64 from, ut64 to) {
RZ_API bool rz_flag_zone_add(RzFlag *f, const char *name, ut64 addr) {
rz_return_val_if_fail(f && name && *name, false);
#if RZ_FLAG_ZONE_USE_SDB
RzFlagZoneItem zi = { 0, 0, (const char *)name };
if (!DB) {
return false;
}
const char *bound = sdb_const_get(DB, name, NULL);
if (bound) {
sdb_fmt_tobin(bound, "qq", &zi);
if (addr < zi.from) {
zi.from = addr;
}
if (addr > zi.to) {
zi.to = addr;
}
char *newBounds = sdb_fmt_tostr(&zi, "qq");
sdb_set(DB, name, newBounds, 0);
free(newBounds);
} else {
sdb_set(DB, name, sdb_fmt("%" PFMT64d ",%" PFMT64d, addr, addr), 0);
}
#else
RzFlagZoneItem *zi = rz_flag_zone_get(f, name);
if (zi) {
if (addr < zi->from) {
@ -71,24 +47,16 @@ RZ_API bool rz_flag_zone_add(RzFlag *f, const char *name, ut64 addr) {
zi->from = zi->to = addr;
rz_list_append(DB, zi);
}
#endif
return true;
}
RZ_API bool rz_flag_zone_reset(RzFlag *f) {
#if RZ_FLAG_ZONE_USE_SDB
return sdb_reset(DB);
#else
rz_list_free(f->zones);
f->zones = rz_list_newf(rz_flag_zone_item_free);
return true;
#endif
}
RZ_API bool rz_flag_zone_del(RzFlag *f, const char *name) {
#if RZ_FLAG_ZONE_USE_SDB
return sdb_unset(DB, name, 0);
#else
RzListIter *iter;
RzFlagZoneItem *zi;
rz_list_foreach (DB, iter, zi) {
@ -98,89 +66,8 @@ RZ_API bool rz_flag_zone_del(RzFlag *f, const char *name) {
}
}
return false;
#endif
}
#if RZ_FLAG_ZONE_USE_SDB
typedef struct rz_flag_zone_context_t {
RzFlag *f;
ut64 addr;
ut64 l, h; // lower, higher closest offsets
const char **prev;
const char **next;
} RzFlagZoneContext;
static bool cb(void *user, const char *name, const char *from_to) {
RzFlagZoneContext *zc = (RzFlagZoneContext *)user;
RzFlagZoneItem zi = { 0, 0, name };
sdb_fmt_tobin(from_to, "qq", &zi);
if (zi.from > zc->addr) {
if (zc->h == UT64_MAX) {
zc->h = zi.from;
*zc->next = name;
} else {
if (zi.from < zc->h) {
zc->h = zi.from;
*zc->next = name;
}
}
}
if (zi.from < zc->addr) {
if (zc->l == UT64_MAX) {
zc->l = zi.from;
*zc->prev = name;
} else {
if (zi.from >= zc->l) {
zc->l = zi.from;
*zc->prev = name;
}
}
}
if (zi.to <= zc->addr) {
if (zc->l == UT64_MAX) {
zc->l = zi.to;
*zc->prev = name;
} else {
if (zi.to >= zc->l) {
zc->l = zi.to;
*zc->prev = name;
}
}
}
if (zi.to > zc->addr) {
if (zc->h == UT64_MAX) {
zc->h = zi.to;
*zc->next = name;
} else {
if (zi.to < zc->h) {
zc->h = zi.to;
*zc->next = name;
}
}
}
return true;
}
RZ_API bool rz_flag_zone_around(RzFlag *f, ut64 addr, const char **prev, const char **next) {
RzFlagZoneContext ctx = { f, addr, 0, UT64_MAX, prev, next };
*prev = *next = NULL;
sdb_foreach(DB, cb, &ctx);
return true;
}
static bool cb_list(void *user, const char *name, const char *from_to) {
eprintf("%s%s %s\n", name, rz_str_pad(' ', 10 - strlen(name)), from_to);
return true;
}
RZ_API bool rz_flag_zone_list(RzFlag *f, int mode) {
sdb_foreach(DB, cb_list, NULL);
return true;
}
#else
RZ_API void rz_flag_zone_item_free(void *a) {
RzFlagZoneItem *zi = a;
free(zi->name);
@ -272,42 +159,3 @@ RZ_API bool rz_flag_zone_list(RzFlag *f, int mode) {
}
return true;
}
#endif
// #define __MAIN__ 1
#if __MAIN__
#define FZ(x) rz_flag_zone_##x
int main() {
const char *a, *b;
RzFlagZone *fz = rz_flag_zone_new();
FZ(add)
(fz, "main", 0x80000);
FZ(add)
(fz, "network", 0x85000);
FZ(add)
(fz, "libc", 0x90000);
FZ(add)
(fz, "network", 0x000);
FZ(around)
(fz, 0x83000, &a, &b);
printf("%s %s\n", a, b);
FZ(around)
(fz, 0x50000, &a, &b);
printf("%s %s\n", a, b);
FZ(around)
(fz, 0x500000, &a, &b);
printf("%s %s\n", a, b);
FZ(list)
(fz);
rz_flag_zone_free(fz);
return 0;
}
#endif

View file

@ -18,16 +18,10 @@ RZ_LIB_VERSION_HEADER(rz_flag);
/* zones.c */
#define RZ_FLAG_ZONE_USE_SDB 0
typedef struct rz_flag_zone_item_t {
ut64 from;
ut64 to;
#if RZ_FLAG_ZONE_USE_SDB
const char *name;
#else
char *name;
#endif
} RzFlagZoneItem;
/* flag.c */
@ -58,11 +52,7 @@ typedef struct rz_flag_t {
RzSkipList *by_off; /* flags sorted by offset, value=RzFlagsAtOffset */
HtPP *ht_name; /* hashmap key=item name, value=RzFlagItem * */
PrintfCallback cb_printf;
#if RZ_FLAG_ZONE_USE_SDB
Sdb *zones;
#else
RzList *zones;
#endif
} RzFlag;
/* compile time dependency */