LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
BinaryStream.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 * Copyright 2017 - 2026 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_BINARY_STREAM_H
17#define LIEF_BINARY_STREAM_H
18
19#include <algorithm>
20#include <cstdint>
21#include <cstring>
22#include <string>
23#include <utility>
24#include <vector>
25
27#include "LIEF/errors.hpp"
28#include "LIEF/visibility.h"
29
30namespace LIEF {
31class ASN1Reader;
32class Binary;
33
36 public:
37 friend class ASN1Reader;
38
39 enum class STREAM_TYPE {
40 UNKNOWN = 0,
41 VECTOR,
42 MEMORY,
43 SPAN,
44 FILE,
45 DUMP,
46
47 ELF_DATA_HANDLER,
48 };
49
52 virtual ~BinaryStream() = default;
53 virtual uint64_t size() const = 0;
54
55 STREAM_TYPE type() const {
56 return stype_;
57 }
58
59 bool is_memory_stream() const {
60 return type() == STREAM_TYPE::MEMORY;
61 }
62
63 bool is_dump_stream() const {
64 return type() == STREAM_TYPE::DUMP;
65 }
66
67 bool is_memory_view() const {
68 return is_memory_stream() || is_dump_stream();
69 }
70
72 static_cast<void>(read_uleb128());
73 return *this;
74 }
75
77 static_cast<void>(read_sleb128());
78 return *this;
79 }
80
81 result<uint64_t> read_uleb128(size_t* size = nullptr) const;
82 result<uint64_t> read_sleb128(size_t* size = nullptr) const;
83
84 result<int64_t> read_dwarf_encoded(uint8_t encoding) const;
85
86 result<std::string> read_string(size_t maxsize = ~size_t{0}) const;
87 result<std::string> peek_string(size_t maxsize = ~size_t{0}) const;
89 size_t maxsize = ~size_t{0}) const;
90
93
94 result<std::string> read_mutf8(size_t maxsize = ~size_t{0}) const;
95
98 result<std::u16string> peek_u16string_at(size_t offset, size_t length) const;
99
100
101 virtual ok_error_t peek_data(std::vector<uint8_t>& container, uint64_t offset,
102 uint64_t size, uint64_t virtual_address = 0) {
103 if (size == 0) {
104 return ok();
105 }
106 // Even though offset + size < ... => offset < ...
107 // the addition could overflow so it's worth checking both
108 const bool read_ok = offset <= this->size() &&
109 (offset + size) <= this->size()
110 /* Check for an overflow */
111 && (int64_t(offset) >= 0 && int64_t(size) >= 0) &&
112 (int64_t(offset + size) >= 0);
113 if (!read_ok) {
115 }
116
117 if (container.size() < size) {
118 std::vector<uint8_t> buffer(size);
119 const ok_error_t res = peek_in(buffer.data(), offset, size, virtual_address);
120 container = std::move(buffer);
121 return res;
122 }
123
124 return peek_in(container.data(), offset, size, virtual_address);
125 }
126
127 virtual ok_error_t read_data(std::vector<uint8_t>& container, uint64_t size) {
128 if (!peek_data(container, pos(), size)) {
130 }
131
133 return ok();
134 }
135
136 ok_error_t read_data(std::vector<uint8_t>& container) {
137 const size_t size = this->size() - this->pos();
138 return read_data(container, size);
139 }
140
141 template<class T>
142 ok_error_t read_objects(std::vector<T>& container, uint64_t count) {
143 if (count == 0) {
144 return ok();
145 }
146 const size_t size = count * sizeof(T);
147 auto ret = peek_objects(container, count);
148 if (!ret) {
150 }
152 return ok();
153 }
154
155 template<class T>
156 ok_error_t peek_objects(std::vector<T>& container, uint64_t count) {
157 return peek_objects_at(pos(), container, count);
158 }
159
160 template<class T>
161 ok_error_t peek_objects_at(uint64_t offset, std::vector<T>& container,
162 uint64_t count) {
163 if (count == 0) {
164 return ok();
165 }
166 const auto current_p = pos();
167 setpos(offset);
168
169 const size_t size = count * sizeof(T);
170
171 if (!can_read(offset, size)) {
172 setpos(current_p);
174 }
175
177
178 if (container.size() < count) {
179 std::vector<T> buffer(count);
180 res = peek_in(buffer.data(), pos(), size);
181 container = std::move(buffer);
182 } else {
183 res = peek_in(container.data(), pos(), size);
184 }
185
186 setpos(current_p);
187 return res;
188 }
189
190 void setpos(size_t pos) const {
191 pos_ = pos;
192 }
193
194 const BinaryStream& increment_pos(size_t value) const LIEF_LIFETIMEBOUND {
195 pos_ += value;
196 return *this;
197 }
198
199 void decrement_pos(size_t value) const {
200 if (pos_ > value) {
201 pos_ -= value;
202 } else {
203 pos_ = 0;
204 }
205 }
206
207 size_t pos() const {
208 return pos_;
209 }
210
211 bool is_valid() const {
212 return pos_ < size();
213 }
214
215 operator bool() const {
216 return is_valid();
217 }
218
219 template<class T>
220 const T* read_array(size_t size) const;
221
222 template<class T, size_t N>
223 ok_error_t peek_array(std::array<T, N>& dst) const {
224 if /*constexpr*/ (N == 0) {
225 return ok();
226 }
227 // Even though offset + size < ... => offset < ...
228 // the addition could overflow so it's worth checking both
229 const bool read_ok = pos_ <= size() &&
230 (pos_ + N) <= size()
231 /* Check for an overflow */
232 && (int64_t(pos_) >= 0 && int64_t(N) >= 0) &&
233 (int64_t(pos_ + N) >= 0);
234
235 if (!read_ok) {
237 }
238 if (peek_in(dst.data(), pos_, N)) {
239 return ok();
240 }
242 }
243
244 template<class T, size_t N>
245 ok_error_t read_array(std::array<T, N>& dst) const {
246 if (!peek_array<T, N>(dst)) {
248 }
249
250 increment_pos(N);
251 return ok();
252 }
253
254 template<class T>
255 result<T> peek() const;
256
257 template<class T>
258 result<T> peek(size_t offset) const;
259
260 template<class T>
261 const T* peek_array(size_t size) const;
262
263 template<class T>
264 const T* peek_array(size_t offset, size_t size) const;
265
266 template<class T>
267 result<T> read() const;
268
269 template<class T>
271 static_cast<void>(read<T>());
272 return *this;
273 }
274
275 template<typename T>
276 bool can_read() const;
277
278 template<typename T>
279 bool can_read(size_t offset) const;
280
281 bool can_read(int64_t offset, int64_t size) const {
282 return offset < (int64_t)this->size() &&
283 size < ((int64_t)this->size() - offset);
284 }
285
286 size_t align(size_t align_on) const;
287
288 void set_endian_swap(bool swap) {
289 endian_swap_ = swap;
290 }
291
292 template<class T>
293 static bool is_all_zero(const T& buffer) {
294 const auto* ptr = reinterpret_cast<const uint8_t* const>(&buffer);
295 return std::all_of(ptr, ptr + sizeof(T), [](uint8_t x) { return x == 0; });
296 }
297
298 bool should_swap() const {
299 return endian_swap_;
300 }
301
302 virtual const uint8_t* p() const {
303 return nullptr;
304 }
305
306 virtual uint8_t* start() {
307 return const_cast<uint8_t*>(static_cast<const BinaryStream*>(this)->start());
308 }
309
310 virtual uint8_t* p() {
311 return const_cast<uint8_t*>(static_cast<const BinaryStream*>(this)->p());
312 }
313
314 virtual uint8_t* end() {
315 return const_cast<uint8_t*>(static_cast<const BinaryStream*>(this)->end());
316 }
317
318 virtual const uint8_t* start() const {
319 return nullptr;
320 }
321
322 virtual const uint8_t* end() const {
323 return nullptr;
324 }
325
326 virtual result<const void*> read_at(uint64_t offset, uint64_t size,
327 uint64_t virtual_address = 0) const = 0;
328 virtual ok_error_t peek_in(void* dst, uint64_t offset, uint64_t size,
329 uint64_t virtual_address = 0) const {
330 if (dst == nullptr) {
332 }
333 if (auto raw = read_at(offset, size, virtual_address)) {
334 const void* ptr = *raw;
335
336 if (ptr == nullptr) {
338 }
339
340 memcpy(dst, ptr, size);
341 return ok();
342 }
344 }
345
346 template<class T>
347 const T* cast() const {
348 static_assert(std::is_base_of_v<BinaryStream, T>,
349 "Require BinaryStream inheritance");
350 if (T::classof(*this)) {
351 return static_cast<const T*>(this);
352 }
353 return nullptr;
354 }
355
356 template<class T>
357 T* cast() {
358 return const_cast<T*>(static_cast<const BinaryStream*>(this)->cast<T>());
359 }
360
361 virtual bool bind_binary(Binary& /*bin*/) {
362 return false;
363 }
364
365 protected:
366 BinaryStream() = default;
367
368 mutable size_t pos_ = 0;
369 bool endian_swap_ = false;
370 STREAM_TYPE stype_ = STREAM_TYPE::UNKNOWN;
371};
372
374 public:
375 ScopedStream(const ScopedStream&) = delete;
377
380
381 explicit ScopedStream(BinaryStream& stream, uint64_t pos) :
382 pos_{stream.pos()},
383 stream_{stream} {
384 stream_.setpos(pos);
385 }
386
387 explicit ScopedStream(BinaryStream& stream) :
388 pos_{stream.pos()},
389 stream_{stream} {}
390
392 stream_.setpos(pos_);
393 }
394
396 return &stream_;
397 }
398
400 return stream_;
401 }
402
403 const BinaryStream& operator*() const {
404 return stream_;
405 }
406
407 private:
408 uint64_t pos_ = 0;
409 BinaryStream& stream_;
410};
411
413 public:
416
419
420 explicit ToggleEndianness(BinaryStream& stream, bool value) :
421 endian_swap_(stream.should_swap()),
422 stream_{stream} {
423 stream.set_endian_swap(value);
424 }
425
426 explicit ToggleEndianness(BinaryStream& stream) :
427 endian_swap_(stream.should_swap()),
428 stream_{stream} {
429 stream.set_endian_swap(!stream_.should_swap());
430 }
431
433 stream_.set_endian_swap(endian_swap_);
434 }
435
437 return &stream_;
438 }
439
441 return stream_;
442 }
443
444 const BinaryStream& operator*() const {
445 return stream_;
446 }
447
448 private:
449 bool endian_swap_ = false;
450 BinaryStream& stream_;
451};
452
453
454template<class T>
456 result<T> tmp = this->peek<T>();
457 if (!tmp) {
458 return tmp;
459 }
460 this->increment_pos(sizeof(T));
461 return tmp;
462}
463
464template<class T>
466 const auto current_p = pos();
467 T ret{};
468 if (auto res = peek_in(&ret, pos(), sizeof(T))) {
469 setpos(current_p);
470 if (endian_swap_) {
471 swap_endian(&ret);
472 }
473 return ret;
474 }
475
476 setpos(current_p);
478}
479
480template<class T>
481result<T> BinaryStream::peek(size_t offset) const {
482 const size_t saved_offset = this->pos();
483 this->setpos(offset);
484 result<T> r = this->peek<T>();
485 this->setpos(saved_offset);
486 return r;
487}
488
489
490template<class T>
491const T* BinaryStream::peek_array(size_t size) const {
492 result<const void*> raw = this->read_at(this->pos(), sizeof(T) * size);
493 if (!raw) {
494 return nullptr;
495 }
496 return reinterpret_cast<const T*>(raw.value());
497}
498
499template<class T>
500const T* BinaryStream::peek_array(size_t offset, size_t size) const {
501 const size_t saved_offset = this->pos();
502 this->setpos(offset);
503 const T* r = this->peek_array<T>(size);
504 this->setpos(saved_offset);
505 return r;
506}
507
508
509template<typename T>
511 // Even though pos_ + sizeof(T) < ... => pos_ < ...
512 // the addition could overflow so it's worth checking both
513 return pos_ < size() && (pos_ + sizeof(T)) < size();
514}
515
516
517template<typename T>
518bool BinaryStream::can_read(size_t offset) const {
519 // Even though offset + sizeof(T) < ... => offset < ...
520 // the addition could overflow so it's worth checking both
521 return offset < size() && (offset + sizeof(T)) < size();
522}
523
524
525template<class T>
526const T* BinaryStream::read_array(size_t size) const {
527 const T* tmp = this->peek_array<T>(size);
528 this->increment_pos(sizeof(T) * size);
529 return tmp;
530}
531
532}
533#endif
Definition ASN1Reader.hpp:32
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:35
size_t align(size_t align_on) const
ok_error_t peek_array(std::array< T, N > &dst) const
Definition BinaryStream.hpp:223
STREAM_TYPE
Definition BinaryStream.hpp:39
@ MEMORY
Definition BinaryStream.hpp:42
@ DUMP
Definition BinaryStream.hpp:45
const BinaryStream & skip_uleb128() const
Definition BinaryStream.hpp:71
result< T > peek() const
Definition BinaryStream.hpp:465
result< int64_t > read_dwarf_encoded(uint8_t encoding) const
virtual uint8_t * p()
Definition BinaryStream.hpp:310
virtual bool bind_binary(Binary &)
Definition BinaryStream.hpp:361
virtual const uint8_t * p() const
Definition BinaryStream.hpp:302
result< std::string > read_string(size_t maxsize=~size_t{0}) const
const T * read_array(size_t size) const
Definition BinaryStream.hpp:526
virtual uint64_t size() const =0
friend class ASN1Reader
Definition BinaryStream.hpp:37
result< std::u16string > peek_u16string_at(size_t offset, size_t length) const
bool can_read() const
Definition BinaryStream.hpp:510
ok_error_t read_objects(std::vector< T > &container, uint64_t count)
Definition BinaryStream.hpp:142
bool is_dump_stream() const
Definition BinaryStream.hpp:63
ok_error_t peek_objects_at(uint64_t offset, std::vector< T > &container, uint64_t count)
Definition BinaryStream.hpp:161
result< std::u16string > peek_u16string(size_t length) const
const T * cast() const
Definition BinaryStream.hpp:347
result< std::string > peek_string_at(size_t offset, size_t maxsize=~size_t{0}) const
result< std::u16string > read_u16string() const
bool is_memory_view() const
Definition BinaryStream.hpp:67
void set_endian_swap(bool swap)
Definition BinaryStream.hpp:288
bool is_memory_stream() const
Definition BinaryStream.hpp:59
const BinaryStream & increment_pos(size_t value) const
Definition BinaryStream.hpp:194
result< std::u16string > peek_u16string() const
bool should_swap() const
Definition BinaryStream.hpp:298
ok_error_t peek_objects(std::vector< T > &container, uint64_t count)
Definition BinaryStream.hpp:156
size_t pos() const
Definition BinaryStream.hpp:207
virtual uint8_t * start()
Definition BinaryStream.hpp:306
result< uint64_t > read_uleb128(size_t *size=nullptr) const
virtual result< const void * > read_at(uint64_t offset, uint64_t size, uint64_t virtual_address=0) const =0
void setpos(size_t pos) const
Definition BinaryStream.hpp:190
bool is_valid() const
Definition BinaryStream.hpp:211
result< std::string > peek_string(size_t maxsize=~size_t{0}) const
virtual const uint8_t * end() const
Definition BinaryStream.hpp:322
static bool is_all_zero(const T &buffer)
Definition BinaryStream.hpp:293
T * cast()
Definition BinaryStream.hpp:357
virtual ok_error_t peek_in(void *dst, uint64_t offset, uint64_t size, uint64_t virtual_address=0) const
Definition BinaryStream.hpp:328
const BinaryStream & skip_sleb128() const
Definition BinaryStream.hpp:76
result< std::u16string > read_u16string(size_t length) const
virtual ok_error_t peek_data(std::vector< uint8_t > &container, uint64_t offset, uint64_t size, uint64_t virtual_address=0)
Definition BinaryStream.hpp:101
void decrement_pos(size_t value) const
Definition BinaryStream.hpp:199
result< T > read() const
Definition BinaryStream.hpp:455
BinaryStream(STREAM_TYPE type)
Definition BinaryStream.hpp:50
const BinaryStream & skip() const
Definition BinaryStream.hpp:270
virtual const uint8_t * start() const
Definition BinaryStream.hpp:318
result< uint64_t > read_sleb128(size_t *size=nullptr) const
ok_error_t read_array(std::array< T, N > &dst) const
Definition BinaryStream.hpp:245
bool can_read(int64_t offset, int64_t size) const
Definition BinaryStream.hpp:281
result< std::string > read_mutf8(size_t maxsize=~size_t{0}) const
ok_error_t read_data(std::vector< uint8_t > &container)
Definition BinaryStream.hpp:136
virtual ~BinaryStream()=default
STREAM_TYPE type() const
Definition BinaryStream.hpp:55
virtual ok_error_t read_data(std::vector< uint8_t > &container, uint64_t size)
Definition BinaryStream.hpp:127
virtual uint8_t * end()
Definition BinaryStream.hpp:314
Generic interface representing a binary executable.
Definition Abstract/Binary.hpp:60
const BinaryStream & operator*() const
Definition BinaryStream.hpp:403
ScopedStream(BinaryStream &stream, uint64_t pos)
Definition BinaryStream.hpp:381
ScopedStream(BinaryStream &stream)
Definition BinaryStream.hpp:387
ScopedStream(const ScopedStream &)=delete
ScopedStream & operator=(const ScopedStream &)=delete
ScopedStream & operator=(ScopedStream &&)=delete
~ScopedStream()
Definition BinaryStream.hpp:391
BinaryStream & operator*()
Definition BinaryStream.hpp:399
ScopedStream(ScopedStream &&)=delete
BinaryStream * operator->()
Definition BinaryStream.hpp:395
ToggleEndianness(BinaryStream &stream, bool value)
Definition BinaryStream.hpp:420
ToggleEndianness(ToggleEndianness &&)=delete
ToggleEndianness & operator=(const ToggleEndianness &)=delete
ToggleEndianness & operator=(ToggleEndianness &&)=delete
BinaryStream & operator*()
Definition BinaryStream.hpp:440
BinaryStream * operator->()
Definition BinaryStream.hpp:436
ToggleEndianness(BinaryStream &stream)
Definition BinaryStream.hpp:426
~ToggleEndianness()
Definition BinaryStream.hpp:432
const BinaryStream & operator*() const
Definition BinaryStream.hpp:444
ToggleEndianness(const ToggleEndianness &)=delete
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:119
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:79
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
@ read_error
Definition errors.hpp:25
@ inconsistent
Definition errors.hpp:43
tl::unexpected< lief_errors > make_error_code(lief_errors e)
Create a standard error code from lief_errors.
Definition errors.hpp:55
LIEF namespace.
Definition Abstract/Binary.hpp:41
ok_t ok()
Return success for function with return type ok_error_t.
Definition errors.hpp:103
void swap_endian(T *)
Definition endianness_support.hpp:117
#define LIEF_API
Definition visibility.h:45