Rizin
unix-like reverse engineering framework and cli tools
rz_range.h File Reference
#include <rz_types.h>
#include <rz_list.h>

Go to the source code of this file.

Classes

struct  rz_range_item_t
 
struct  rz_range_t
 

Typedefs

typedef struct rz_range_item_t RRangeItem
 
typedef struct rz_range_t RRange
 

Functions

RZ_API RRangerz_range_new (void)
 
RZ_API RRangerz_range_new_from_string (const char *string)
 
RZ_API RRangerz_range_free (RRange *r)
 
RZ_API RRangeItemrz_range_item_get (RRange *r, ut64 addr)
 
RZ_API ut64 rz_range_size (RRange *r)
 
RZ_API int rz_range_add_from_string (RRange *rgs, const char *string)
 
RZ_API RRangeItemrz_range_add (RRange *rgs, ut64 from, ut64 to, int rw)
 
RZ_API int rz_range_sub (RRange *rgs, ut64 from, ut64 to)
 
RZ_API int rz_range_contains (RRange *rgs, ut64 addr)
 
RZ_API int rz_range_sort (RRange *rgs)
 
RZ_API void rz_range_percent (RRange *rgs)
 
RZ_API int rz_range_list (RRange *rgs, int rad)
 
RZ_API int rz_range_get_n (RRange *rgs, int n, ut64 *from, ut64 *to)
 
RZ_API RRangerz_range_inverse (RRange *rgs, ut64 from, ut64 to, int flags)
 
RZ_API int rz_range_overlap (ut64 a0, ut64 a1, ut64 b0, ut64 b1, int *d)
 

Typedef Documentation

◆ RRange

typedef struct rz_range_t RRange

◆ RRangeItem

typedef struct rz_range_item_t RRangeItem

Function Documentation

◆ rz_range_add()

RZ_API RRangeItem* rz_range_add ( RRange rgs,
ut64  from,
ut64  to,
int  rw 
)

Definition at line 143 of file range.c.

143  {
144  RzListIter *iter;
145  RRangeItem *r, *ret = NULL;
146  int add = 1;
147 
148  rz_num_minmax_swap(&fr, &to);
149  rz_list_foreach (rgs->ranges, iter, r) {
150  if (r->fr == fr && r->to == to) {
151  add = 0;
152  } else if (r->fr <= fr && r->fr <= to && r->to >= fr && r->to <= to) {
153  r->to = to;
154  ret = r;
155  add = 0;
156  } else if (r->fr >= fr && r->fr <= to && r->to >= fr && r->to >= to) {
157  r->fr = fr;
158  ret = r;
159  add = 0;
160  } else if (r->fr <= fr && r->fr <= to && r->to >= fr && r->to >= to) {
161  /* ignore */
162  add = 0;
163  } else if (r->fr >= fr && r->fr <= to && r->to >= fr && r->to <= to) {
164  r->fr = fr;
165  r->to = to;
166  ret = r;
167  add = 0;
168  }
169  }
170 
171  if (rw && add) {
172  ret = RZ_NEW(RRangeItem);
173  ret->fr = fr;
174  ret->to = to;
175  ret->datalen = 0;
176  ret->data = NULL;
177  rz_list_append(rgs->ranges, ret);
178  rgs->changed = 1;
179  }
180 
181  return ret;
182 }
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
RZ_API void rz_num_minmax_swap(ut64 *a, ut64 *b)
Definition: unum.c:59
#define RZ_NEW(x)
Definition: rz_types.h:285
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr socklen_t static fromlen const void const struct sockaddr to
Definition: sfsocketcall.h:125
RzList * ranges
Definition: rz_range.h:23
int changed
Definition: rz_range.h:22
static int add(char *argv[])
Definition: ziptool.c:84

References add(), rz_range_t::changed, rz_range_item_t::data, rz_range_item_t::datalen, rz_range_item_t::fr, NULL, r, rz_range_t::ranges, rz_list_append(), RZ_NEW, rz_num_minmax_swap(), rz_range_item_t::to, and to.

Referenced by rz_range_add_from_string(), rz_range_inverse(), and rz_range_sub().

◆ rz_range_add_from_string()

RZ_API int rz_range_add_from_string ( RRange rgs,
const char *  string 
)

Definition at line 88 of file range.c.

88  {
89  ut64 addr, addr2;
90  int i, len = strlen(string) + 1;
91  char *str, *ostr = malloc(len);
92  if (!ostr) {
93  return 0;
94  }
95  char *p = str = ostr;
96  char *p2 = NULL;
97 
98  memcpy(str, string, len);
99  for (i = 0; i < len; i++) {
100  switch (str[i]) {
101  case '-':
102  str[i] = '\0';
103  p2 = p;
104  p = str + i + 1;
105  break;
106  case ',':
107  str[i] = '\0';
108  if (p2) {
109  addr = rz_num_get(NULL, p);
110  addr2 = rz_num_get(NULL, p2);
111  rz_range_add(rgs, addr, addr2, 1);
112  p2 = NULL;
113  } else {
114  addr = rz_num_get(NULL, p);
115  rz_range_add(rgs, addr, addr + 1, 1);
116  }
117  p = str + i + 1;
118  str[i] = ',';
119  break;
120  }
121  }
122  if (p2) {
123  addr = rz_num_get(NULL, p);
124  addr2 = rz_num_get(NULL, p2);
125  rz_range_add(rgs, addr, addr2, 1);
126  } else if (p) {
127  addr = rz_num_get(NULL, p);
128  rz_range_add(rgs, addr, addr + 1, 1);
129  }
130  free(ostr);
131  return rgs ? rgs->changed : 0;
132 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void * p
Definition: libc.cpp:67
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * malloc(size_t size)
Definition: malloc.c:123
RZ_API RRangeItem * rz_range_add(RRange *rgs, ut64 fr, ut64 to, int rw)
Definition: range.c:143
RZ_API ut64 rz_num_get(RzNum *num, const char *str)
Definition: unum.c:172
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58

References addr, rz_range_t::changed, free(), i, len, malloc(), memcpy(), NULL, p, rz_num_get(), rz_range_add(), cmd_descs_generate::str, and ut64().

Referenced by rz_range_new_from_string().

◆ rz_range_contains()

RZ_API int rz_range_contains ( RRange rgs,
ut64  addr 
)

Definition at line 237 of file range.c.

237  {
238  RRangeItem *r;
239  RzListIter *iter;
240  rz_list_foreach (rgs->ranges, iter, r) {
241  if (addr >= r->fr && addr <= r->to) {
242  return true;
243  }
244  }
245  return false;
246 }

References addr, r, rz_range_t::ranges, and to.

Referenced by rz_range_percent().

◆ rz_range_free()

RZ_API RRange* rz_range_free ( RRange r)

Definition at line 26 of file range.c.

26  {
27  rz_list_purge(r->ranges);
28  free(r);
29  return NULL;
30 }
RZ_API void rz_list_purge(RZ_NONNULL RzList *list)
Empties the list without freeing the list pointer.
Definition: list.c:120

References free(), NULL, r, and rz_list_purge().

Referenced by rz_range_new().

◆ rz_range_get_n()

RZ_API int rz_range_get_n ( RRange rgs,
int  n,
ut64 from,
ut64 to 
)

Definition at line 331 of file range.c.

331  {
332  int count = 0;
333  RRangeItem *r;
334  RzListIter *iter;
335  rz_range_sort(rgs);
336  rz_list_foreach (rgs->ranges, iter, r) {
337  if (count == n) {
338  *fr = r->fr;
339  *to = r->to;
340  return 1;
341  }
342  count++;
343  }
344  return 0;
345 }
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
int n
Definition: mipsasm.c:19
RZ_API int rz_range_sort(RRange *rgs)
Definition: range.c:254

References count, n, r, rz_range_t::ranges, rz_range_sort(), and to.

◆ rz_range_inverse()

RZ_API RRange* rz_range_inverse ( RRange rgs,
ut64  from,
ut64  to,
int  flags 
)

Definition at line 353 of file range.c.

353  {
354  RzListIter *iter;
355  RRangeItem *r = NULL;
356  RRange *newrgs = rz_range_new();
357 
358  rz_range_sort(rgs);
359 
360  rz_list_foreach (rgs->ranges, iter, r) {
361  if (r->fr > fr && r->fr < to) {
362  rz_range_add(newrgs, fr, r->fr, 1);
363  fr = r->to;
364  }
365  }
366  if (fr < to) {
367  rz_range_add(newrgs, fr, to, 1);
368  }
369  return newrgs;
370 }
RZ_API RRange * rz_range_new(void)
Definition: range.c:12

References NULL, r, rz_range_t::ranges, rz_range_add(), rz_range_new(), rz_range_sort(), and to.

◆ rz_range_item_get()

RZ_API RRangeItem* rz_range_item_get ( RRange r,
ut64  addr 
)

Definition at line 59 of file range.c.

59  {
60  RRangeItem *r;
62  rz_list_foreach (rgs->ranges, iter, r) {
63  if (addr >= r->fr && addr < r->to) {
64  return r;
65  }
66  }
67  return NULL;
68 }

References addr, NULL, r, rz_range_t::ranges, and to.

Referenced by rz_range_get_data(), and rz_range_set_data().

◆ rz_range_list()

RZ_API int rz_range_list ( RRange rgs,
int  rad 
)

Definition at line 314 of file range.c.

314  {
315  ut64 total = 0;
316  RRangeItem *r;
317  RzListIter *iter;
318  rz_range_sort(rgs);
319  rz_list_foreach (rgs->ranges, iter, r) {
320  if (rad) {
321  printf("ar+ 0x%08" PFMT64x " 0x%08" PFMT64x "\n", r->fr, r->to);
322  } else {
323  printf("0x%08" PFMT64x " 0x%08" PFMT64x " ; %" PFMT64d "\n", r->fr, r->to, r->to - r->fr);
324  }
325  total += (r->to - r->fr);
326  }
327  eprintf("Total bytes: %" PFMT64d "\n", total);
328  return 0;
329 }
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
#define eprintf(x, y...)
Definition: rlcc.c:7
#define PFMT64d
Definition: rz_types.h:394
#define PFMT64x
Definition: rz_types.h:393

References eprintf, PFMT64d, PFMT64x, printf(), r, rz_range_t::ranges, rz_range_sort(), and ut64().

◆ rz_range_new()

RZ_API RRange* rz_range_new ( void  )

Definition at line 12 of file range.c.

12  {
13  RRange *r = RZ_NEW0(RRange);
14  if (r) {
15  r->count = r->changed = 0;
16  r->ranges = rz_list_new();
17  if (!r->ranges) {
19  return NULL;
20  }
21  r->ranges->free = free;
22  }
23  return r;
24 }
RZ_API RZ_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
RZ_API RRange * rz_range_free(RRange *r)
Definition: range.c:26
#define RZ_NEW0(x)
Definition: rz_types.h:284

References free(), NULL, r, rz_list_new(), RZ_NEW0, and rz_range_free().

Referenced by rz_range_inverse(), and rz_range_new_from_string().

◆ rz_range_new_from_string()

RZ_API RRange* rz_range_new_from_string ( const char *  string)

Definition at line 82 of file range.c.

82  {
83  RRange *rgs = rz_range_new();
84  rz_range_add_from_string(rgs, string);
85  return rgs;
86 }
RZ_API int rz_range_add_from_string(RRange *rgs, const char *string)
Definition: range.c:88

References rz_range_add_from_string(), and rz_range_new().

◆ rz_range_overlap()

RZ_API int rz_range_overlap ( ut64  a0,
ut64  a1,
ut64  b0,
ut64  b1,
int d 
)

Definition at line 378 of file range.c.

378  {
379  // TODO: ensure ranges minmax .. innecesary at runtime?
380  // rz_num_minmax_swap (&a0, &a1);
381  // rz_num_minmax_swap (&b0, &b1);
382  return *d = (b0 - a0), !(a1 < b0 || a0 > b1);
383 #if 0
384  // does not overlap
385  // a |__| |__|
386  // b |__| |__|
387  if (a1<b0 || a0>b1)
388  return 0;
389 
390  // a |____| |_____| |____| |_____|
391  // b |____| |_| |____| |_______|
392  // b needs a needs a needs b needs
393  // delta required
394  return (b0-a0);
395 #endif
396 }
a0
Definition: insn-good.s.cs:704
#define d(i)
Definition: sha256.c:44

References a0, b1, and d.

◆ rz_range_percent()

RZ_API void rz_range_percent ( RRange rgs)

Definition at line 267 of file range.c.

267  {
268  RzListIter *iter;
269  RRangeItem *r;
270  int w, i;
271  ut64 seek, step;
272  ut64 dif, fr = -1, to = -1;
273 
274  rz_list_foreach (rgs->ranges, iter, r) {
275  if (fr == -1) {
276  /* init */
277  fr = r->fr;
278  to = r->to;
279  } else {
280  if (fr > r->fr) {
281  fr = r->fr;
282  }
283  if (to < r->to) {
284  to = r->to;
285  }
286  }
287  }
288  w = 65; // columns
289  if (fr != -1) {
290  dif = to - fr;
291  if (dif < w) {
292  step = 1; // XXX
293  } else {
294  step = dif / w;
295  }
296  } else {
297  step = fr = to = 0;
298  }
299  seek = 0;
300  // XXX do not use printf here!
301  printf("0x%08" PFMT64x " [", fr);
302  for (i = 0; i < w; i++) {
303  if (rz_range_contains(rgs, seek)) {
304  printf("#");
305  } else {
306  printf(".");
307  }
308  seek += step;
309  }
310  printf("] 0x%08" PFMT64x "\n", to);
311 }
#define w
Definition: crypto_rc6.c:13
static states step(struct re_guts *, sopno, sopno, states, int, states)
Definition: engine.c:888
RZ_API int rz_range_contains(RRange *rgs, ut64 addr)
Definition: range.c:237
static int seek(char *argv[])

References i, PFMT64x, printf(), r, rz_range_t::ranges, rz_range_contains(), seek(), step(), to, ut64(), and w.

◆ rz_range_size()

RZ_API ut64 rz_range_size ( RRange r)

Definition at line 72 of file range.c.

72  {
73  ut64 sum = 0;
75  RRangeItem *r;
76  rz_list_foreach (rgs->ranges, iter, r) {
77  sum += r->to - r->fr;
78  }
79  return sum;
80 }

References r, rz_range_t::ranges, and ut64().

◆ rz_range_sort()

RZ_API int rz_range_sort ( RRange rgs)

Definition at line 254 of file range.c.

254  {
255  bool ch = rgs->ranges->sorted;
256  if (!rgs->changed) {
257  return false;
258  }
259  rgs->changed = false;
261  if (ch != rgs->ranges->sorted) {
262  rgs->changed = true;
263  }
264  return rgs->changed;
265 }
RZ_API void rz_list_sort(RZ_NONNULL RzList *list, RZ_NONNULL RzListComparator cmp)
Sorts via merge sort or via insertion sort a list.
Definition: list.c:743
static int cmp_ranges(void *a, void *b)
Definition: range.c:248
int(* RzListComparator)(const void *value, const void *list_data)
Definition: rz_list.h:33
bool sorted
Definition: rz_list.h:23

References rz_range_t::changed, cmp_ranges(), rz_range_t::ranges, rz_list_sort(), and rz_list_t::sorted.

Referenced by rz_range_get_n(), rz_range_inverse(), and rz_range_list().

◆ rz_range_sub()

RZ_API int rz_range_sub ( RRange rgs,
ut64  from,
ut64  to 
)

Definition at line 193 of file range.c.

193  {
194  RRangeItem *r;
195  RzListIter *iter;
196 
197  rz_num_minmax_swap(&fr, &to);
198 
199 __reloop:
200  rz_list_foreach (rgs->ranges, iter, r) {
201  /* update to */
202  if (r->fr < fr && r->fr < to && r->to > fr && r->to < to) {
203  r->to = fr;
204  } else if (r->fr > fr && r->fr < to && r->to > fr && r->to > to) {
205  /* update fr */
206  r->fr = to;
207  }
208  /* delete */
209  if (r->fr > fr && r->fr < to && r->to > fr && r->to < to) {
210  /* delete */
211  rz_list_delete(rgs->ranges, iter);
212  rgs->changed = 1;
213  goto __reloop;
214  }
215  /* split */
216  if (r->fr < fr && r->fr < to && r->to > fr && r->to > to) {
217  r->to = fr;
218  rz_range_add(rgs, to, r->to, 1);
219  // ranges_add(rang, to, r->to, 1);
220  goto __reloop;
221  }
222  }
223  return 0;
224 }
RZ_API void rz_list_delete(RZ_NONNULL RzList *list, RZ_NONNULL RzListIter *iter)
Removes an entry in the list by using the RzListIter pointer.
Definition: list.c:162

References rz_range_t::changed, r, rz_range_t::ranges, rz_list_delete(), rz_num_minmax_swap(), rz_range_add(), and to.