16#ifndef LIEF_BINARY_STREAM_H
17#define LIEF_BINARY_STREAM_H
27#include "LIEF/BinaryStream/Convert.hpp"
28#include "LIEF/errors.hpp"
37 enum class STREAM_TYPE {
51 virtual uint64_t size()
const = 0;
53 STREAM_TYPE type()
const {
64 result<std::string> peek_string_at(
size_t offset,
size_t maxsize = ~
static_cast<size_t>(0))
const;
76 virtual ok_error_t peek_data(std::vector<uint8_t>& container,
77 uint64_t offset, uint64_t size)
85 const bool read_ok = offset <= this->size() && (offset + size) <= this->size()
87 && (
static_cast<int64_t
>(offset) >= 0 &&
static_cast<int64_t
>(size) >= 0)
88 && (
static_cast<int64_t
>(offset + size) >= 0);
90 return make_error_code(lief_errors::read_error);
92 container.resize(size);
93 if (peek_in(container.data(), offset, size)) {
96 return make_error_code(lief_errors::read_error);
99 virtual ok_error_t read_data(std::vector<uint8_t>& container, uint64_t size) {
100 if (!peek_data(container, pos(), size)) {
101 return make_error_code(lief_errors::read_error);
108 void setpos(
size_t pos)
const {
112 void increment_pos(
size_t value)
const {
116 void decrement_pos(
size_t value)
const {
128 operator bool()
const {
129 return pos_ < size();
133 const T* read_array(
size_t size)
const;
142 const T* peek_array(
size_t size)
const;
145 const T* peek_array(
size_t offset,
size_t size)
const;
151 bool can_read()
const;
154 bool can_read(
size_t offset)
const;
156 size_t align(
size_t align_on)
const;
160 typename std::enable_if<std::is_integral<T>::value,
result<T>>::type
164 typename std::enable_if<!std::is_integral<T>::value,
result<T>>::type
168 result<T> peek_conv(
size_t offset)
const;
175 std::unique_ptr<T[]> read_conv_array(
size_t size)
const;
178 std::unique_ptr<T[]> peek_conv_array(
size_t offset,
size_t size)
const;
181 static T swap_endian(T u);
183 void set_endian_swap(
bool swap) {
188 static bool is_all_zero(
const T& buffer) {
189 const auto* ptr =
reinterpret_cast<const uint8_t *const
>(&buffer);
190 return std::all_of(ptr, ptr +
sizeof(T),
191 [] (uint8_t x) {
return x == 0; });
194 bool should_swap()
const {
198 virtual const uint8_t* p()
const {
202 virtual uint8_t* start() {
203 return const_cast<uint8_t*
>(
static_cast<const BinaryStream*
>(
this)->start());
206 virtual uint8_t* p() {
207 return const_cast<uint8_t*
>(
static_cast<const BinaryStream*
>(
this)->p());
210 virtual uint8_t* end() {
211 return const_cast<uint8_t*
>(
static_cast<const BinaryStream*
>(
this)->end());
214 virtual const uint8_t* start()
const {
218 virtual const uint8_t* end()
const {
225 virtual ok_error_t peek_in(
void* dst, uint64_t offset, uint64_t size)
const {
226 if (
auto raw = read_at(offset, size)) {
227 if (dst ==
nullptr) {
228 return make_error_code(lief_errors::read_error);
231 const void* ptr = *raw;
233 if (ptr ==
nullptr) {
234 return make_error_code(lief_errors::read_error);
237 memcpy(dst, ptr, size);
240 return make_error_code(lief_errors::read_error);
242 mutable size_t pos_ = 0;
243 bool endian_swap_ =
false;
244 STREAM_TYPE stype_ = STREAM_TYPE::UNKNOWN;
268 stream_.setpos(pos_);
295 this->increment_pos(
sizeof(T));
301 const auto current_p = pos();
303 if (
auto res = peek_in(&ret, pos(),
sizeof(T))) {
309 return make_error_code(lief_errors::read_error);
313result<T> BinaryStream::peek(
size_t offset)
const {
314 const size_t saved_offset = this->pos();
315 this->setpos(offset);
317 this->setpos(saved_offset);
323const T* BinaryStream::peek_array(
size_t size)
const {
328 return reinterpret_cast<const T*
>(raw.value());
332const T* BinaryStream::peek_array(
size_t offset,
size_t size)
const {
333 const size_t saved_offset = this->pos();
334 this->setpos(offset);
335 const T* r = this->peek_array<T>(size);
336 this->setpos(saved_offset);
342bool BinaryStream::can_read()
const {
345 return pos_ < size() && (pos_ +
sizeof(T)) < size();
350bool BinaryStream::can_read(
size_t offset)
const {
353 return offset < size() && (offset +
sizeof(T)) < size();
358const T* BinaryStream::read_array(
size_t size)
const {
359 const T* tmp = this->peek_array<T>(size);
360 this->increment_pos(
sizeof(T) * size);
366result<T> BinaryStream::read_conv()
const {
371 this->increment_pos(
sizeof(T));
376typename std::enable_if<std::is_integral<T>::value,
result<T>>::type
377BinaryStream::peek_conv()
const {
379 if (
auto res = peek_in(&ret, pos(),
sizeof(T))) {
380 return endian_swap_ ? swap_endian<T>(ret) : ret;
382 return make_error_code(lief_errors::read_error);
386typename std::enable_if<!std::is_integral<T>::value,
result<T>>::type
387BinaryStream::peek_conv()
const {
389 if (
auto res = peek_in(&ret, pos(),
sizeof(T))) {
391 LIEF::Convert::swap_endian<T>(&ret);
395 return make_error_code(lief_errors::read_error);
400result<T> BinaryStream::peek_conv(
size_t offset)
const {
401 const size_t saved_offset = this->pos();
402 this->setpos(offset);
404 this->setpos(saved_offset);
410std::unique_ptr<T[]> BinaryStream::read_conv_array(
size_t size)
const {
411 const T *t = this->read_array<T>(size);
417 std::unique_ptr<T[]> uptr(
new T[size]);
419 for (
size_t i = 0; i < size; i++) {
421 if (this->endian_swap_) {
422 LIEF::Convert::swap_endian<T>(& uptr[i]);
430std::unique_ptr<T[]> BinaryStream::peek_conv_array(
size_t offset,
size_t size)
const {
431 const T *t = this->peek_array<T>(offset, size);
437 std::unique_ptr<T[]> uptr(
new T[size]);
439 for (
size_t i = 0; i < size; i++) {
441 if (this->endian_swap_) {
442 LIEF::Convert::swap_endian<T>(& uptr[i]);
Definition ASN1Reader.hpp:32
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:34
Definition BinaryStream.hpp:247
LIEF namespace.
Definition Abstract/Binary.hpp:31
result< ok_t > ok_error_t
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:107
ok_t ok()
Return success for function with return type ok_error_t.
Definition errors.hpp:91
tl::expected< T, lief_errors > result
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:73