Rizin
unix-like reverse engineering framework and cli tools
rz_itv.h
Go to the documentation of this file.
1 #ifndef RZ_INTERVAL_H
2 #define RZ_INTERVAL_H
3 
4 #include <rz_types.h>
5 
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9 
10 // An interval in 64-bit address space which is aware of address space wraparound
11 // Precondition: 0 <= size < 2**64 and addr + size <= 2**64
12 // range is [], [10, 5) => 10 <= x < (10 + 5)
13 typedef struct rz_interval_t {
14  // public:
18 
20 
21 static inline RzInterval *rz_itv_new(ut64 addr, ut64 size) {
23  if (itv) {
24  itv->addr = addr;
25  itv->size = size;
26  }
27  return itv;
28 }
29 
30 static inline void rz_itv_free(RzInterval *itv) {
31  free(itv);
32 }
33 
34 static inline ut64 rz_itv_begin(RzInterval itv) {
35  return itv.addr;
36 }
37 
38 static inline ut64 rz_itv_size(RzInterval itv) {
39  return itv.size;
40 }
41 
42 static inline ut64 rz_itv_end(RzInterval itv) {
43  return itv.addr + itv.size;
44 }
45 
46 // Returns true if itv equals itv2
47 static inline bool rz_itv_eq(RzInterval itv, RzInterval itv2) {
48  return itv.addr == itv2.addr && itv.size == itv2.size;
49 }
50 
51 // Returns true if itv contained addr
52 static inline bool rz_itv_contain(RzInterval itv, ut64 addr) {
53  const ut64 end = itv.addr + itv.size;
54  return itv.addr <= addr && (!end || addr < end);
55 }
56 
57 // Returns true if x is a subset of itv
58 static inline bool rz_itv_include(RzInterval itv, RzInterval x) {
59  const ut64 end = itv.addr + itv.size;
60  return itv.addr <= x.addr && (!end || (x.addr + x.size && x.addr + x.size <= end));
61 }
62 
63 // Returns true if itv and x overlap (implying they are non-empty)
64 static inline bool rz_itv_overlap(RzInterval itv, RzInterval x) {
65  const ut64 end = itv.addr + itv.size, end1 = x.addr + x.size;
66  return (!end1 || itv.addr < end1) && (!end || x.addr < end);
67 }
68 
69 static inline bool rz_itv_overlap2(RzInterval itv, ut64 addr, ut64 size) {
70  RzInterval rai = { addr, size };
71  return rz_itv_overlap(itv, rai);
72 }
73 
74 // Precondition: itv and x overlap
75 // Returns the intersection of itv and x
77  const ut64 addr = RZ_MAX(itv.addr, x.addr);
78  const ut64 end = RZ_MIN(itv.addr + itv.size - 1, x.addr + x.size - 1) + 1;
79  RzInterval rai = { addr, end - addr };
80  return rai;
81 }
82 
83 #ifdef __cplusplus
84 }
85 #endif
86 
87 #endif // RZ_INTERVAL_H
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void uLong size
Definition: ioapi.h:138
int x
Definition: mipsasm.c:20
static bool rz_itv_include(RzInterval itv, RzInterval x)
Definition: rz_itv.h:58
static ut64 rz_itv_begin(RzInterval itv)
Definition: rz_itv.h:34
static void rz_itv_free(RzInterval *itv)
Definition: rz_itv.h:30
static bool rz_itv_contain(RzInterval itv, ut64 addr)
Definition: rz_itv.h:52
static RzInterval * rz_itv_new(ut64 addr, ut64 size)
Definition: rz_itv.h:21
struct rz_interval_t RzInterval
static ut64 rz_itv_end(RzInterval itv)
Definition: rz_itv.h:42
static bool rz_itv_eq(RzInterval itv, RzInterval itv2)
Definition: rz_itv.h:47
RzInterval rz_itv_t
Definition: rz_itv.h:19
static bool rz_itv_overlap2(RzInterval itv, ut64 addr, ut64 size)
Definition: rz_itv.h:69
static bool rz_itv_overlap(RzInterval itv, RzInterval x)
Definition: rz_itv.h:64
static RzInterval rz_itv_intersect(RzInterval itv, RzInterval x)
Definition: rz_itv.h:76
static ut64 rz_itv_size(RzInterval itv)
Definition: rz_itv.h:38
#define RZ_NEW(x)
Definition: rz_types.h:285
#define RZ_MIN(x, y)
#define RZ_MAX(x, y)
ut64 addr
Definition: rz_itv.h:15
ut64 size
Definition: rz_itv.h:16
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58