2020-06-14 14:08:32 +00:00
|
|
|
/* radare - LGPL - Copyright 2008-2020 pancake<nopcode.org> */
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-03-03 01:41:41 +00:00
|
|
|
#include <r_util.h>
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-06-04 21:47:35 +00:00
|
|
|
// TODO: use r_list instead of list.h
|
|
|
|
// TODO: redesign this api.. why? :)
|
2009-02-05 21:08:46 +00:00
|
|
|
// TODO: add tags to ranges
|
|
|
|
|
|
|
|
//void (*ranges_new_callback)(struct range_t *r) = NULL;
|
|
|
|
|
2020-06-14 14:08:32 +00:00
|
|
|
R_API RRange *r_range_new(void) {
|
2016-05-24 20:22:15 +00:00
|
|
|
RRange *r = R_NEW0 (RRange);
|
2010-05-20 15:40:58 +00:00
|
|
|
if (r) {
|
2010-06-04 21:47:35 +00:00
|
|
|
r->count = r->changed = 0;
|
|
|
|
r->ranges = r_list_new ();
|
2016-05-24 20:22:15 +00:00
|
|
|
if (!r->ranges) {
|
|
|
|
r_range_free (r);
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-06-04 21:47:35 +00:00
|
|
|
r->ranges->free = free;
|
2010-05-20 15:40:58 +00:00
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2010-03-03 01:41:41 +00:00
|
|
|
R_API RRange *r_range_free(RRange *r) {
|
2014-05-02 14:54:27 +00:00
|
|
|
r_list_purge (r->ranges);
|
2010-03-03 01:41:41 +00:00
|
|
|
free (r);
|
2009-02-05 21:08:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: optimize by just returning the pointers to the internal foo?
|
2010-03-03 01:41:41 +00:00
|
|
|
R_API int r_range_get_data(RRange *rgs, ut64 addr, ut8 *buf, int len) {
|
2010-03-30 15:37:15 +00:00
|
|
|
RRangeItem *r = r_range_item_get (rgs, addr);
|
2016-10-24 23:12:06 +00:00
|
|
|
if (!r) {
|
2009-02-05 21:08:46 +00:00
|
|
|
return 0;
|
2016-10-24 23:12:06 +00:00
|
|
|
}
|
|
|
|
if (r->datalen < len) {
|
2009-02-05 21:08:46 +00:00
|
|
|
len = r->datalen;
|
2016-10-24 23:12:06 +00:00
|
|
|
}
|
2010-03-30 15:37:15 +00:00
|
|
|
memcpy (buf, r->data, len);
|
2009-02-05 21:08:46 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2010-03-03 01:41:41 +00:00
|
|
|
R_API int r_range_set_data(RRange *rgs, ut64 addr, const ut8 *buf, int len) {
|
2010-03-30 15:37:15 +00:00
|
|
|
RRangeItem *r = r_range_item_get (rgs, addr);
|
2016-10-24 23:12:06 +00:00
|
|
|
if (!r) {
|
2009-02-05 21:08:46 +00:00
|
|
|
return 0;
|
2016-10-24 23:12:06 +00:00
|
|
|
}
|
2010-03-30 15:37:15 +00:00
|
|
|
r->data = (ut8*)malloc (len);
|
2016-10-24 23:12:06 +00:00
|
|
|
if (!r->data) {
|
|
|
|
return 0;
|
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
r->datalen = len;
|
2010-03-30 15:37:15 +00:00
|
|
|
memcpy (r->data, buf, len);
|
2009-02-05 21:08:46 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-03-30 15:37:15 +00:00
|
|
|
RRangeItem *r_range_item_get(RRange *rgs, ut64 addr) {
|
2010-06-04 21:47:35 +00:00
|
|
|
RRangeItem *r;
|
|
|
|
RListIter *iter;
|
|
|
|
r_list_foreach (rgs->ranges, iter, r) {
|
2018-09-13 08:17:26 +00:00
|
|
|
if (addr >= r->fr && addr < r->to) {
|
2009-02-05 21:08:46 +00:00
|
|
|
return r;
|
2018-09-13 08:17:26 +00:00
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns the sum of all the ranges contained */
|
2019-06-20 04:45:00 +00:00
|
|
|
// XXX: can be caught while adding/removing elements
|
2010-03-03 01:41:41 +00:00
|
|
|
R_API ut64 r_range_size(RRange *rgs) {
|
2009-07-08 11:49:55 +00:00
|
|
|
ut64 sum = 0;
|
2010-06-04 21:47:35 +00:00
|
|
|
RListIter *iter;
|
|
|
|
RRangeItem *r;
|
2016-10-24 23:12:06 +00:00
|
|
|
r_list_foreach (rgs->ranges, iter, r) {
|
2010-01-04 00:25:52 +00:00
|
|
|
sum += r->to - r->fr;
|
2016-10-24 23:12:06 +00:00
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2010-03-30 15:37:15 +00:00
|
|
|
R_API RRange *r_range_new_from_string(const char *string) {
|
|
|
|
RRange *rgs = r_range_new ();
|
|
|
|
r_range_add_from_string (rgs, string);
|
2009-02-05 21:08:46 +00:00
|
|
|
return rgs;
|
|
|
|
}
|
|
|
|
|
2010-03-03 01:41:41 +00:00
|
|
|
R_API int r_range_add_from_string(RRange *rgs, const char *string) {
|
2009-07-08 11:49:55 +00:00
|
|
|
ut64 addr, addr2;
|
2016-10-24 23:12:06 +00:00
|
|
|
int i, len = strlen (string) + 1;
|
2011-07-06 09:48:16 +00:00
|
|
|
char *str, *ostr = malloc (len);
|
2016-10-24 23:12:06 +00:00
|
|
|
if (!ostr) {
|
|
|
|
return 0;
|
|
|
|
}
|
2011-07-06 09:48:16 +00:00
|
|
|
char *p = str = ostr;
|
2009-02-05 21:08:46 +00:00
|
|
|
char *p2 = NULL;
|
|
|
|
|
2010-03-03 01:41:41 +00:00
|
|
|
memcpy (str, string, len);
|
2016-10-24 23:12:06 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
2010-03-30 15:37:15 +00:00
|
|
|
switch (str[i]) {
|
2009-02-05 21:08:46 +00:00
|
|
|
case '-':
|
2016-10-24 23:12:06 +00:00
|
|
|
str[i] = '\0';
|
2009-02-05 21:08:46 +00:00
|
|
|
p2 = p;
|
2016-10-24 23:12:06 +00:00
|
|
|
p = str + i + 1;
|
2009-02-05 21:08:46 +00:00
|
|
|
break;
|
|
|
|
case ',':
|
2016-10-24 23:12:06 +00:00
|
|
|
str[i] = '\0';
|
2009-02-05 21:08:46 +00:00
|
|
|
if (p2) {
|
2011-06-29 22:17:12 +00:00
|
|
|
addr = r_num_get (NULL, p);
|
|
|
|
addr2 = r_num_get (NULL, p2);
|
|
|
|
r_range_add(rgs, addr, addr2, 1);
|
2009-02-05 21:08:46 +00:00
|
|
|
p2 = NULL;
|
|
|
|
} else {
|
2011-06-29 22:17:12 +00:00
|
|
|
addr = r_num_get (NULL, p);
|
2016-10-24 23:12:06 +00:00
|
|
|
r_range_add (rgs, addr, addr + 1, 1);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2016-10-24 23:12:06 +00:00
|
|
|
p = str + i + 1;
|
2011-06-29 22:17:12 +00:00
|
|
|
str[i] = ',';
|
2009-02-05 21:08:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p2) {
|
2010-03-03 01:41:41 +00:00
|
|
|
addr = r_num_get (NULL, p);
|
|
|
|
addr2 = r_num_get (NULL, p2);
|
2011-06-29 22:17:12 +00:00
|
|
|
r_range_add (rgs, addr, addr2, 1);
|
2016-05-24 20:22:15 +00:00
|
|
|
} else
|
2009-02-05 21:08:46 +00:00
|
|
|
if (p) {
|
2010-03-03 01:41:41 +00:00
|
|
|
addr = r_num_get (NULL, p);
|
2016-10-24 23:12:06 +00:00
|
|
|
r_range_add (rgs, addr, addr + 1, 1);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2011-07-06 09:48:16 +00:00
|
|
|
free (ostr);
|
2012-09-26 08:01:43 +00:00
|
|
|
return rgs? rgs->changed: 0;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2010-01-04 00:25:52 +00:00
|
|
|
update to new one update fr update fr/to ignore
|
2009-02-05 21:08:46 +00:00
|
|
|
|
|
|
|
|______| |___| |_____| |____| |_______| range_t
|
2010-01-04 00:25:52 +00:00
|
|
|
+ |______| + |__| + |___| + |_________| + |__| fr/to
|
2009-02-05 21:08:46 +00:00
|
|
|
------------ ----------- ----------- ------------- -----------
|
|
|
|
= |_________| = |___||__| = |_______| = |_________| |_______| result
|
|
|
|
#endif
|
|
|
|
|
2010-03-03 01:41:41 +00:00
|
|
|
RRangeItem *r_range_add(RRange *rgs, ut64 fr, ut64 to, int rw) {
|
2010-06-04 21:47:35 +00:00
|
|
|
RListIter *iter;
|
|
|
|
RRangeItem *r, *ret = NULL;
|
2009-02-05 21:08:46 +00:00
|
|
|
int add = 1;
|
|
|
|
|
2010-03-03 01:41:41 +00:00
|
|
|
r_num_minmax_swap (&fr, &to);
|
2010-06-04 21:47:35 +00:00
|
|
|
r_list_foreach (rgs->ranges, iter, r) {
|
2016-10-24 23:12:06 +00:00
|
|
|
if (r->fr == fr && r->to == to) {
|
2009-02-05 21:08:46 +00:00
|
|
|
add = 0;
|
2016-10-24 23:12:06 +00:00
|
|
|
} else if (r->fr <= fr && r->fr <= to && r->to >= fr && r->to <= to) {
|
2009-02-05 21:08:46 +00:00
|
|
|
r->to = to;
|
|
|
|
ret = r;
|
|
|
|
add = 0;
|
2016-10-24 23:12:06 +00:00
|
|
|
} else if (r->fr >= fr && r->fr <= to && r->to >= fr && r->to >= to) {
|
2010-01-04 00:25:52 +00:00
|
|
|
r->fr = fr;
|
2009-02-05 21:08:46 +00:00
|
|
|
ret = r;
|
|
|
|
add = 0;
|
2016-10-24 23:12:06 +00:00
|
|
|
} else if (r->fr <= fr && r->fr <= to && r->to >= fr && r->to >= to) {
|
2009-02-05 21:08:46 +00:00
|
|
|
/* ignore */
|
|
|
|
add = 0;
|
2016-10-24 23:12:06 +00:00
|
|
|
} else if (r->fr >= fr && r->fr <= to && r->to >= fr && r->to <= to) {
|
2010-01-04 00:25:52 +00:00
|
|
|
r->fr = fr;
|
2009-02-05 21:08:46 +00:00
|
|
|
r->to = to;
|
|
|
|
ret = r;
|
|
|
|
add = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rw && add) {
|
2010-03-03 01:41:41 +00:00
|
|
|
ret = R_NEW (RRangeItem);
|
2010-01-04 00:25:52 +00:00
|
|
|
ret->fr = fr;
|
2009-02-05 21:08:46 +00:00
|
|
|
ret->to = to;
|
|
|
|
ret->datalen = 0;
|
|
|
|
ret->data = NULL;
|
2010-06-04 21:47:35 +00:00
|
|
|
r_list_append (rgs->ranges, ret);
|
2009-02-05 21:08:46 +00:00
|
|
|
rgs->changed = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2010-01-04 00:25:52 +00:00
|
|
|
update to ignore update fr delete split
|
2009-02-05 21:08:46 +00:00
|
|
|
|
|
|
|
|______| |___| |_____| |____| |________| range_t
|
2010-01-04 00:25:52 +00:00
|
|
|
- |______| - |__| - |___| - |_________| - |__| fr/to
|
2009-02-05 21:08:46 +00:00
|
|
|
------------ ----------- ----------- ------------- ------------
|
|
|
|
= |__| = = |___| = |__| |__| result
|
|
|
|
#endif
|
|
|
|
|
2010-03-03 01:41:41 +00:00
|
|
|
R_API int r_range_sub(RRange *rgs, ut64 fr, ut64 to) {
|
|
|
|
RRangeItem *r;
|
2010-06-04 21:47:35 +00:00
|
|
|
RListIter *iter;
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-03-03 01:41:41 +00:00
|
|
|
r_num_minmax_swap (&fr, &to);
|
2009-02-05 21:08:46 +00:00
|
|
|
|
|
|
|
__reloop:
|
2010-06-04 21:47:35 +00:00
|
|
|
r_list_foreach (rgs->ranges, iter, r) {
|
2009-02-05 21:08:46 +00:00
|
|
|
/* update to */
|
2016-10-24 23:12:06 +00:00
|
|
|
if (r->fr < fr && r->fr < to && r->to > fr && r->to < to) {
|
2010-01-04 00:25:52 +00:00
|
|
|
r->to = fr;
|
2016-10-24 23:12:06 +00:00
|
|
|
} else if (r->fr > fr && r->fr < to && r->to > fr && r->to > to) {
|
|
|
|
/* update fr */
|
2010-01-04 00:25:52 +00:00
|
|
|
r->fr = to;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
/* delete */
|
2016-10-24 23:12:06 +00:00
|
|
|
if (r->fr > fr && r->fr < to && r->to > fr && r->to < to) {
|
2009-02-05 21:08:46 +00:00
|
|
|
/* delete */
|
2010-06-04 21:47:35 +00:00
|
|
|
r_list_delete (rgs->ranges, iter);
|
2009-02-05 21:08:46 +00:00
|
|
|
rgs->changed = 1;
|
|
|
|
goto __reloop;
|
|
|
|
}
|
|
|
|
/* split */
|
2016-10-24 23:12:06 +00:00
|
|
|
if (r->fr < fr && r->fr < to && r->to > fr && r->to > to) {
|
2010-01-04 00:25:52 +00:00
|
|
|
r->to = fr;
|
2010-03-03 01:41:41 +00:00
|
|
|
r_range_add (rgs, to, r->to, 1);
|
2009-02-05 21:08:46 +00:00
|
|
|
//ranges_add(rang, to, r->to, 1);
|
|
|
|
goto __reloop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-06-04 21:47:35 +00:00
|
|
|
#if 0
|
2009-02-05 21:08:46 +00:00
|
|
|
/* TODO: should remove some of them right? */
|
2010-06-04 21:47:35 +00:00
|
|
|
R_API void r_range_merge(RRange *rgs, RRange *r) {
|
|
|
|
RListIter *iter;
|
|
|
|
RRangeItem *r;
|
|
|
|
r_list_foreach (rgs->ranges, iter, r)
|
2010-03-03 01:41:41 +00:00
|
|
|
r_range_add (rgs, r->fr, r->to, 0);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2010-06-04 21:47:35 +00:00
|
|
|
#endif
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2009-07-08 11:49:55 +00:00
|
|
|
//int ranges_is_used(ut64 addr)
|
2010-03-03 01:41:41 +00:00
|
|
|
R_API int r_range_contains(RRange *rgs, ut64 addr) {
|
2010-06-04 21:47:35 +00:00
|
|
|
RRangeItem *r;
|
|
|
|
RListIter *iter;
|
|
|
|
r_list_foreach (rgs->ranges, iter, r) {
|
2016-10-24 23:12:06 +00:00
|
|
|
if (addr >= r->fr && addr <= r->to) {
|
2016-07-12 20:15:19 +00:00
|
|
|
return true;
|
2016-10-24 23:12:06 +00:00
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2016-07-12 20:15:19 +00:00
|
|
|
return false;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 23:12:06 +00:00
|
|
|
static int cmp_ranges(void *a, void *b) {
|
|
|
|
RRangeItem *first = (RRangeItem *)a;
|
|
|
|
RRangeItem *second = (RRangeItem *)b;
|
2020-09-09 13:45:34 +00:00
|
|
|
return (first->fr > second->fr) - (first->fr < second->fr);
|
2016-10-24 23:12:06 +00:00
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2016-10-24 23:12:06 +00:00
|
|
|
R_API int r_range_sort(RRange *rgs) {
|
|
|
|
bool ch = rgs->ranges->sorted;
|
|
|
|
if (!rgs->changed) {
|
2016-07-12 20:15:19 +00:00
|
|
|
return false;
|
2016-10-24 23:12:06 +00:00
|
|
|
}
|
2016-07-12 20:15:19 +00:00
|
|
|
rgs->changed = false;
|
2016-10-24 23:12:06 +00:00
|
|
|
r_list_sort (rgs->ranges, (RListComparator)cmp_ranges);
|
|
|
|
if (ch != rgs->ranges->sorted) {
|
|
|
|
rgs->changed = true;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
return rgs->changed;
|
|
|
|
}
|
|
|
|
|
2010-03-03 01:41:41 +00:00
|
|
|
R_API void r_range_percent(RRange *rgs) {
|
2010-06-04 21:47:35 +00:00
|
|
|
RListIter *iter;
|
|
|
|
RRangeItem *r;
|
2009-02-05 21:08:46 +00:00
|
|
|
int w, i;
|
2009-07-08 11:49:55 +00:00
|
|
|
ut64 seek, step;
|
2010-01-04 00:25:52 +00:00
|
|
|
ut64 dif, fr = -1, to = -1;
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-06-04 21:47:35 +00:00
|
|
|
r_list_foreach (rgs->ranges, iter, r) {
|
2010-01-04 00:25:52 +00:00
|
|
|
if (fr == -1) {
|
2009-02-05 21:08:46 +00:00
|
|
|
/* init */
|
2010-01-04 00:25:52 +00:00
|
|
|
fr = r->fr;
|
2009-02-05 21:08:46 +00:00
|
|
|
to = r->to;
|
|
|
|
} else {
|
2016-10-24 23:12:06 +00:00
|
|
|
if (fr > r->fr) {
|
|
|
|
fr = r->fr;
|
|
|
|
}
|
|
|
|
if (to < r->to) {
|
|
|
|
to = r->to;
|
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
w = 65 ; // columns
|
2010-03-03 01:41:41 +00:00
|
|
|
if (fr != -1) {
|
2016-10-24 23:12:06 +00:00
|
|
|
dif = to - fr;
|
|
|
|
if (dif < w) {
|
|
|
|
step = 1; // XXX
|
|
|
|
} else {
|
|
|
|
step = dif / w;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
step = fr = to = 0;
|
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
seek = 0;
|
|
|
|
// XXX do not use printf here!
|
2010-04-14 11:02:23 +00:00
|
|
|
printf ("0x%08"PFMT64x" [", fr);
|
2016-10-24 23:12:06 +00:00
|
|
|
for (i = 0; i < w; i++) {
|
|
|
|
if (r_range_contains (rgs, seek)) {
|
2010-03-03 01:41:41 +00:00
|
|
|
printf ("#");
|
2016-10-24 23:12:06 +00:00
|
|
|
} else {
|
|
|
|
printf (".");
|
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
seek += step;
|
|
|
|
}
|
2010-04-14 11:02:23 +00:00
|
|
|
printf ("] 0x%08"PFMT64x"\n", to);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: total can be cached in rgs!!
|
2010-03-03 01:41:41 +00:00
|
|
|
int r_range_list(RRange *rgs, int rad) {
|
2009-07-08 11:49:55 +00:00
|
|
|
ut64 total = 0;
|
2010-06-04 21:47:35 +00:00
|
|
|
RRangeItem *r;
|
|
|
|
RListIter *iter;
|
2010-03-03 01:41:41 +00:00
|
|
|
r_range_sort (rgs);
|
2010-06-04 21:47:35 +00:00
|
|
|
r_list_foreach (rgs->ranges, iter, r) {
|
2016-10-24 23:12:06 +00:00
|
|
|
if (rad) {
|
|
|
|
printf ("ar+ 0x%08"PFMT64x" 0x%08"PFMT64x"\n", r->fr, r->to);
|
|
|
|
} else {
|
|
|
|
printf ("0x%08"PFMT64x" 0x%08"PFMT64x" ; %"PFMT64d"\n", r->fr, r->to, r->to - r->fr);
|
|
|
|
}
|
|
|
|
total += (r->to - r->fr);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2010-04-14 11:02:23 +00:00
|
|
|
eprintf ("Total bytes: %"PFMT64d"\n", total);
|
2009-02-05 21:08:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-03 01:41:41 +00:00
|
|
|
int r_range_get_n(RRange *rgs, int n, ut64 *fr, ut64 *to) {
|
2009-02-05 21:08:46 +00:00
|
|
|
int count = 0;
|
2010-06-04 21:47:35 +00:00
|
|
|
RRangeItem *r;
|
|
|
|
RListIter *iter;
|
2010-03-03 01:41:41 +00:00
|
|
|
r_range_sort (rgs);
|
2010-06-04 21:47:35 +00:00
|
|
|
r_list_foreach (rgs->ranges, iter, r) {
|
2010-03-03 01:41:41 +00:00
|
|
|
if (count == n) {
|
2010-01-04 00:25:52 +00:00
|
|
|
*fr = r->fr;
|
2009-02-05 21:08:46 +00:00
|
|
|
*to = r->to;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
.....|______________________|...
|
|
|
|
|_____| |____| |_______|
|
|
|
|
---------------------------------
|
2016-05-24 20:22:15 +00:00
|
|
|
|__| |__| |_|
|
2009-02-05 21:08:46 +00:00
|
|
|
#endif
|
2010-03-03 01:41:41 +00:00
|
|
|
RRange *r_range_inverse(RRange *rgs, ut64 fr, ut64 to, int flags) {
|
2009-07-08 11:49:55 +00:00
|
|
|
ut64 total = 0;
|
2010-06-04 21:47:35 +00:00
|
|
|
RListIter *iter;
|
2010-03-03 01:41:41 +00:00
|
|
|
RRangeItem *r = NULL;
|
|
|
|
RRange *newrgs = r_range_new();
|
2009-02-05 21:08:46 +00:00
|
|
|
|
|
|
|
r_range_sort(rgs);
|
|
|
|
|
2010-06-04 21:47:35 +00:00
|
|
|
r_list_foreach (rgs->ranges, iter, r) {
|
2010-01-04 00:25:52 +00:00
|
|
|
if (r->fr > fr && r->fr < to) {
|
2016-10-24 23:12:06 +00:00
|
|
|
r_range_add (newrgs, fr, r->fr, 1);
|
2010-04-14 11:02:23 +00:00
|
|
|
//eprintf("0x%08"PFMT64x" .. 0x%08"PFMT64x"\n", fr, r->fr);
|
2010-01-04 00:25:52 +00:00
|
|
|
total += (r->fr - fr);
|
|
|
|
fr = r->to;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
2010-01-04 00:25:52 +00:00
|
|
|
if (fr < to) {
|
2010-04-14 11:02:23 +00:00
|
|
|
//eprintf("0x%08"PFMT64x" .. 0x%08"PFMT64x"\n", fr, to);
|
2010-03-30 15:37:15 +00:00
|
|
|
r_range_add (newrgs, fr, to, 1);
|
2016-10-24 23:12:06 +00:00
|
|
|
total += (to - fr);
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2010-04-14 11:02:23 +00:00
|
|
|
// eprintf("Total bytes: %"PFMT64d"\n", total);
|
2009-02-05 21:08:46 +00:00
|
|
|
return newrgs;
|
|
|
|
}
|
2010-03-30 15:37:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
return true if overlap
|
2016-05-24 20:22:15 +00:00
|
|
|
in *d
|
2010-03-30 15:37:15 +00:00
|
|
|
*/
|
|
|
|
// TODO: make it a macro
|
|
|
|
// TODO: move to num.c ?
|
|
|
|
R_API int r_range_overlap(ut64 a0, ut64 a1, ut64 b0, ut64 b1, int *d) {
|
|
|
|
// TODO: ensure ranges minmax .. innecesary at runtime?
|
|
|
|
//r_num_minmax_swap (&a0, &a1);
|
|
|
|
//r_num_minmax_swap (&b0, &b1);
|
2016-10-24 23:12:06 +00:00
|
|
|
return *d = (b0 - a0), !(a1 < b0 || a0 > b1);
|
2010-03-30 15:37:15 +00:00
|
|
|
#if 0
|
|
|
|
// does not overlap
|
|
|
|
// a |__| |__|
|
|
|
|
// b |__| |__|
|
|
|
|
if (a1<b0 || a0>b1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// a |____| |_____| |____| |_____|
|
|
|
|
// b |____| |_| |____| |_______|
|
|
|
|
// b needs a needs a needs b needs
|
|
|
|
// delta required
|
|
|
|
return (b0-a0);
|
|
|
|
#endif
|
|
|
|
}
|