IDA C++ SDK 9.2
Loading...
Searching...
No Matches
range.hpp
Go to the documentation of this file.
1/*
2 * Interactive disassembler (IDA).
3 * Copyright (c) 1990-2025 Hex-Rays
4 * ALL RIGHTS RESERVED.
5 *
6 */
7
8#ifndef _RANGE_HPP
9#define _RANGE_HPP
10
23
24#ifndef SWIG
25struct range_t;
27idaman size_t ida_export range_t_print(const range_t *, char *buf, size_t bufsize);
28#endif
29
30//--------------------------------------------------------------------------
34struct range_t
35{
36 friend size_t ida_export range_t_print(const range_t *cb, char *buf, size_t bufsize);
39 range_t(ea_t ea1=0, ea_t ea2=0) : start_ea(ea1), end_ea(ea2) {}
40
43 {
44 return start_ea > r.start_ea ? 1 : start_ea < r.start_ea ? -1 : end_ea > r.end_ea ? 1 : end_ea < r.end_ea ? -1 : 0;
45 }
46
48 bool contains(ea_t ea) const { return start_ea <= ea && end_ea > ea; }
49
51 bool contains(const range_t &r) const { return r.start_ea >= start_ea && r.end_ea <= end_ea; }
52
54 bool overlaps(const range_t &r) const { return r.start_ea < end_ea && start_ea < r.end_ea; }
55
57 void clear() { start_ea = end_ea = 0; }
58
60 bool empty() const { return start_ea >= end_ea; }
61
63 asize_t size() const { return end_ea - start_ea; }
64
66 void intersect(const range_t &r)
67 {
68 if ( start_ea < r.start_ea )
70 if ( end_ea > r.end_ea )
71 end_ea = r.end_ea;
72 if ( end_ea < start_ea )
74 }
75
77 void extend(ea_t ea)
78 {
79 if ( start_ea > ea )
80 start_ea = ea;
81 if ( end_ea < ea )
82 end_ea = ea;
83 }
84
88 size_t print(char *buf, size_t bufsize) const { return range_t_print(this, buf, bufsize); }
89};
93{
94};
95
96//--------------------------------------------------------------------------
97// Various kinds of ranges, see
98// \ref idb_event::changing_range_cmt
99// \ref idb_event::range_cmt_changed
107
108//--------------------------------------------------------------------------
110#ifndef SWIG
111#define RANGESET_HELPER_DEFINITIONS(decl) \
112decl bool ida_export rangeset_t_add(rangeset_t *, const range_t &range);\
113decl bool ida_export rangeset_t_sub(rangeset_t *, const range_t &range);\
114decl bool ida_export rangeset_t_add2(rangeset_t *, const rangeset_t &aset);\
115decl bool ida_export rangeset_t_sub2(rangeset_t *, const rangeset_t &aset);\
116decl bool ida_export rangeset_t_has_common(const rangeset_t *, const range_t &range, bool strict);\
117decl bool ida_export rangeset_t_has_common2(const rangeset_t *, const rangeset_t &aset);\
118decl bool ida_export rangeset_t_contains(const rangeset_t *, const rangeset_t &aset);\
119decl size_t ida_export rangeset_t_print(const rangeset_t *, char *buf, size_t bufsize);\
120decl bool ida_export rangeset_t_intersect(rangeset_t *, const rangeset_t &aset);\
121decl const range_t *ida_export rangeset_t_find_range(const rangeset_t *, ea_t ea);\
122decl ea_t ida_export rangeset_t_next_addr(const rangeset_t *, ea_t ea);\
123decl ea_t ida_export rangeset_t_prev_addr(const rangeset_t *, ea_t ea);\
124decl ea_t ida_export rangeset_t_next_range(const rangeset_t *, ea_t ea);\
125decl ea_t ida_export rangeset_t_prev_range(const rangeset_t *, ea_t ea);\
126decl rangevec_t::const_iterator ida_export rangeset_t_lower_bound(const rangeset_t *, ea_t ea);\
127decl rangevec_t::const_iterator ida_export rangeset_t_upper_bound(const rangeset_t *, ea_t ea);\
128decl void ida_export rangeset_t_swap(rangeset_t *, rangeset_t &r);
129#else
130#define RANGESET_HELPER_DEFINITIONS(decl)
131#endif // SWIG
132
133class rangeset_t;
134
136
137
138class rangeset_t
139{
140 rangevec_t bag;
141 mutable const range_t *cache;
142 int undo_code = -1;
143
145 bool verify(void) const;
146public:
147 DEFINE_MEMORY_ALLOCATION_FUNCS()
149 rangeset_t(void) : cache(nullptr) {}
151 rangeset_t(const range_t &range): cache(nullptr) { if ( !range.empty() ) bag.push_back(range); }
153 rangeset_t(const rangeset_t &ivs) : bag(ivs.bag), cache(nullptr) {}
154 rangeset_t &operator=(const rangeset_t &ivs) { bag = ivs.bag; cache = nullptr; return *this; }
156 void swap(rangeset_t &r) { rangeset_t_swap(this, r); }
157
163 bool add(const range_t &range) { return rangeset_t_add(this, range); }
164
166 bool add(ea_t start, ea_t _end) { return add(range_t(start, _end)); }
167
170 bool add(const rangeset_t &aset) { return rangeset_t_add2(this, aset); }
171
177 bool sub(const range_t &range) { return rangeset_t_sub(this, range); }
178
180 bool sub(ea_t ea) { return sub(range_t(ea, ea+1)); }
181
184 bool sub(const rangeset_t &aset) { return rangeset_t_sub2(this, aset); }
185
187 bool has_common(const range_t &range) const
188 { return rangeset_t_has_common(this, range, false); }
189
191 bool includes(const range_t &range) const
192 { return rangeset_t_has_common(this, range, true); }
193
195 size_t print(char *buf, size_t bufsize) const
196 { return rangeset_t_print(this, buf, bufsize); }
197
199 asize_t count(void) const;
200
202 const range_t &getrange(int idx) const { return bag[idx]; }
203
205 const range_t &lastrange(void) const { return bag.back(); }
206
208 size_t nranges(void) const { return bag.size(); }
209
211 bool empty(void) const { return bag.empty(); }
212
214 void clear(void) { bag.clear(); cache = nullptr; }
215
217 bool has_common(const rangeset_t &aset) const
218 { return rangeset_t_has_common2(this, aset); }
219
221 bool contains(ea_t ea) const { return !empty() && find_range(ea) != nullptr; }
222
224 bool contains(const rangeset_t &aset) const
225 { return rangeset_t_contains(this, aset); }
226
229 bool intersect(const rangeset_t &aset)
230 { return rangeset_t_intersect(this, aset); }
231
233 bool is_subset_of(const rangeset_t &aset) const { return aset.contains(*this); }
234
236 bool is_equal(const rangeset_t &aset) const { return bag == aset.bag; }
237
238 bool operator==(const rangeset_t &aset) const { return is_equal(aset); }
239 bool operator!=(const rangeset_t &aset) const { return !is_equal(aset); }
240
241 typedef rangevec_t::iterator iterator;
242 typedef rangevec_t::const_iterator const_iterator;
243 const_iterator begin(void) const { return bag.begin(); }
244 const_iterator end(void) const { return bag.end(); }
245 iterator begin(void) { return bag.begin(); }
246 iterator end(void) { return bag.end(); }
247
249 const_iterator lower_bound(ea_t ea) const { return rangeset_t_lower_bound(this, ea); }
250
252 const_iterator upper_bound(ea_t ea) const { return rangeset_t_upper_bound(this, ea); }
253
256 const range_t *find_range(ea_t ea) const
257 { return rangeset_t_find_range(this, ea); }
258
261 const range_t *cached_range(void) const { return cache; }
262
264 ea_t next_addr(ea_t ea) const { return rangeset_t_next_addr(this, ea); }
265
267 ea_t prev_addr(ea_t ea) const { return rangeset_t_prev_addr(this, ea); }
268
270 ea_t next_range(ea_t ea) const { return rangeset_t_next_range(this, ea); }
271
273 ea_t prev_range(ea_t ea) const { return rangeset_t_prev_range(this, ea); }
274
276 int move_chunk(ea_t from, ea_t to, asize_t size);
277
279 int check_move_args(ea_t from, ea_t to, asize_t size); // returns VAMOVE_...
280
282 const rangevec_t &as_rangevec() const
283 { return bag; }
284};
288
289//--------------------------------------------------------------------------
291{
294
295 range64_t(uint64 _start=0, uint64 _end=0) : start(_start), end(_end) {}
296 DECLARE_COMPARISONS(range64_t) { return start > r.start ? 1 : start < r.start ? -1 : end > r.end ? 1 : end < r.end ? -1 : 0; }
297 bool contains(uint64 x) const { return start <= x && end > x; }
298 bool contains(const range64_t &r) const { return r.start >= start && r.end <= end; }
299 bool overlaps(const range64_t &r) const { return r.start < end && start < r.end; }
300 void clear() { start = end = 0; }
301 bool empty() const { return start >= end; }
302 uint64 size() const { return end - start; }
303 void intersect(const range64_t &r)
304 {
305 if ( start < r.start )
306 start = r.start;
307 if ( end > r.end )
308 end = r.end;
309 if ( end < start )
310 end = start;
311 }
312 void extend(uint64 ea)
313 {
314 if ( start > ea )
315 start = ea;
316 if ( end < ea )
317 end = ea;
318 }
319};
321
322struct range64vec_t : public qvector<range64_t>
323{
324 const range64_t *find_range(uint64 off) const
325 {
326 for ( const range64_t &r : *this )
327 if ( r.contains(off) )
328 return &r;
329 return nullptr;
330 }
331};
332
333#endif // _RANGE_HPP
idaman ea_t ida_export next_addr(ea_t ea)
Get next address in the program (i.e.
idaman ea_t ida_export prev_addr(ea_t ea)
Get previous address in the program.
Reimplementation of vector class from STL.
Definition pro.h:2250
void clear(void)
Destroy all elements and free memory.
Definition pro.h:2439
const range_t * const_iterator
Definition pro.h:2607
bool empty(void) const
Does the qvector have 0 elements?
Definition pro.h:2424
iterator end(void)
Get an iterator that points to the end of the qvector (NOT the last element)
Definition pro.h:2610
const T & back(void) const
Get the last element in the qvector.
Definition pro.h:2431
qvector(void)
Definition pro.h:2328
range_t * iterator
Definition pro.h:2606
iterator begin(void)
Get an iterator that points to the first element in the qvector.
Definition pro.h:2609
void push_back(T &&x)
Append a new element to the end the qvector with a move semantics.
Definition pro.h:2361
size_t size(void) const
Get the number of elements in the qvector.
Definition pro.h:2423
idaman const char * end
Definition pro.h:1001
idaman int64 size_t count
Definition kernwin.hpp:1366
asize_t size
Definition kernwin.hpp:6339
unsigned __int64 uint64
Definition llong.hpp:13
uint64 asize_t
Definition pro.h:423
uint64 ea_t
Definition pro.h:421
idaman size_t bufsize
Definition pro.h:600
qvector< range_t > rangevec_base_t
Definition range.hpp:91
range_kind_t
Definition range.hpp:101
@ RANGE_KIND_FUNC
func_t
Definition range.hpp:103
@ RANGE_KIND_SEGMENT
segment_t
Definition range.hpp:104
@ RANGE_KIND_UNKNOWN
Definition range.hpp:102
@ RANGE_KIND_HIDDEN_RANGE
hidden_range_t
Definition range.hpp:105
RANGESET_HELPER_DEFINITIONS(idaman) class rangeset_t
An ordered set of non-overlapping address ranges.
Definition range.hpp:135
qvector< const rangeset_t * > rangeset_crefvec_t
Definition range.hpp:287
qvector< rangeset_t > array_of_rangesets
Array of rangeset_t objects.
Definition range.hpp:286
DECLARE_TYPE_AS_MOVABLE(range_t)
idaman size_t ida_export range_t_print(const range_t *, char *buf, size_t bufsize)
Helper function. Should not be called directly!
Definition range.hpp:291
uint64 end
Definition range.hpp:293
bool contains(uint64 x) const
Definition range.hpp:297
void clear()
Definition range.hpp:300
uint64 size() const
Definition range.hpp:302
bool overlaps(const range64_t &r) const
Definition range.hpp:299
bool empty() const
Definition range.hpp:301
range64_t(uint64 _start=0, uint64 _end=0)
Definition range.hpp:295
uint64 start
Definition range.hpp:292
void intersect(const range64_t &r)
Definition range.hpp:303
bool contains(const range64_t &r) const
Definition range.hpp:298
DECLARE_COMPARISONS(range64_t)
Definition range.hpp:296
void extend(uint64 ea)
Definition range.hpp:312
Definition range.hpp:323
const range64_t * find_range(uint64 off) const
Definition range.hpp:324
Base class for an range.
Definition range.hpp:35
bool contains(const range_t &r) const
Is every ea in 'r' also in this range_t?
Definition range.hpp:51
bool empty() const
Is the size of the range_t <= 0?
Definition range.hpp:60
void extend(ea_t ea)
Ensure that the range_t includes 'ea'.
Definition range.hpp:77
ea_t end_ea
end_ea excluded
Definition range.hpp:38
size_t print(char *buf, size_t bufsize) const
Print the range_t.
Definition range.hpp:88
bool contains(ea_t ea) const
Is 'ea' in the address range?
Definition range.hpp:48
ea_t start_ea
start_ea included
Definition range.hpp:37
asize_t size() const
Get end_ea - start_ea.
Definition range.hpp:63
range_t(ea_t ea1=0, ea_t ea2=0)
Definition range.hpp:39
void intersect(const range_t &r)
Assign the range_t to the intersection between the range_t and 'r'.
Definition range.hpp:66
bool overlaps(const range_t &r) const
Is there an ea in 'r' that is also in this range_t?
Definition range.hpp:54
friend size_t ida_export range_t_print(const range_t *cb, char *buf, size_t bufsize)
Helper function. Should not be called directly!
void clear()
Set start_ea, end_ea to 0.
Definition range.hpp:57
DECLARE_COMPARISONS(range_t)
Compare two range_t instances, based on the start_ea.
Definition range.hpp:42
Vector of range_t instances.
Definition range.hpp:93