16#ifndef LIEF_BINARY_STREAM_H
17#define LIEF_BINARY_STREAM_H
52 virtual uint64_t
size()
const = 0;
78 uint64_t offset, uint64_t
size)
86 const bool read_ok = offset <= this->
size() && (offset +
size) <= this->
size()
88 && (
static_cast<int64_t
>(offset) >= 0 &&
static_cast<int64_t
>(
size) >= 0)
89 && (
static_cast<int64_t
>(offset +
size) >= 0);
93 container.resize(
size);
94 if (peek_in(container.data(), offset,
size)) {
129 operator bool()
const {
130 return pos_ <
size();
157 size_t align(
size_t align_on)
const;
190 const auto* ptr =
reinterpret_cast<const uint8_t *const
>(&buffer);
191 return std::all_of(ptr, ptr +
sizeof(T),
192 [] (uint8_t x) {
return x == 0; });
199 virtual const uint8_t*
p()
const {
207 virtual uint8_t*
p() {
208 return const_cast<uint8_t*
>(
static_cast<const BinaryStream*
>(
this)->
p());
212 return const_cast<uint8_t*
>(
static_cast<const BinaryStream*
>(
this)->
end());
215 virtual const uint8_t*
start()
const {
219 virtual const uint8_t*
end()
const {
226 virtual ok_error_t peek_in(
void* dst, uint64_t offset, uint64_t
size)
const {
227 if (
auto raw = read_at(offset,
size)) {
228 if (dst ==
nullptr) {
232 const void* ptr = *raw;
234 if (ptr ==
nullptr) {
238 memcpy(dst, ptr,
size);
243 mutable size_t pos_ = 0;
244 bool endian_swap_ =
false;
302 const auto current_p =
pos();
304 if (
auto res = peek_in(&ret,
pos(),
sizeof(T))) {
315 const size_t saved_offset = this->
pos();
318 this->
setpos(saved_offset);
329 return reinterpret_cast<const T*
>(raw.value());
334 const size_t saved_offset = this->
pos();
337 this->
setpos(saved_offset);
346 return pos_ <
size() && (pos_ +
sizeof(T)) <
size();
354 return offset <
size() && (offset +
sizeof(T)) <
size();
377typename std::enable_if<std::is_integral<T>::value,
result<T>>::type
380 if (
auto res = peek_in(&ret,
pos(),
sizeof(T))) {
387typename std::enable_if<!std::is_integral<T>::value,
result<T>>::type
390 if (
auto res = peek_in(&ret,
pos(),
sizeof(T))) {
402 const size_t saved_offset = this->
pos();
405 this->
setpos(saved_offset);
418 std::unique_ptr<T[]> uptr(
new T[
size]);
420 for (
size_t i = 0; i <
size; i++) {
422 if (this->endian_swap_) {
438 std::unique_ptr<T[]> uptr(
new T[
size]);
440 for (
size_t i = 0; i <
size; i++) {
442 if (this->endian_swap_) {
Definition ASN1Reader.hpp:32
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:34
size_t align(size_t align_on) const
void increment_pos(size_t value) const
Definition BinaryStream.hpp:113
result< std::string > read_mutf8(size_t maxsize=~static_cast< size_t >(0)) const
STREAM_TYPE
Definition BinaryStream.hpp:38
result< uint64_t > read_sleb128() const
virtual ok_error_t peek_data(std::vector< uint8_t > &container, uint64_t offset, uint64_t size)
Definition BinaryStream.hpp:77
result< T > peek() const
Definition BinaryStream.hpp:301
result< uint64_t > read_uleb128() const
result< int64_t > read_dwarf_encoded(uint8_t encoding) const
static T swap_endian(T u)
virtual uint8_t * p()
Definition BinaryStream.hpp:207
result< std::string > peek_string(size_t maxsize=~static_cast< size_t >(0)) const
virtual const uint8_t * p() const
Definition BinaryStream.hpp:199
const T * read_array(size_t size) const
Definition BinaryStream.hpp:359
virtual uint64_t size() const =0
result< std::u16string > peek_u16string_at(size_t offset, size_t length) const
bool can_read() const
Definition BinaryStream.hpp:343
result< std::u16string > peek_u16string(size_t length) const
std::unique_ptr< T[]> peek_conv_array(size_t offset, size_t size) const
Definition BinaryStream.hpp:431
result< std::u16string > read_u16string() const
void set_endian_swap(bool swap)
Definition BinaryStream.hpp:184
std::enable_if< std::is_integral< T >::value, result< T > >::type peek_conv() const
Definition BinaryStream.hpp:378
result< std::u16string > peek_u16string() const
bool should_swap() const
Definition BinaryStream.hpp:195
result< std::string > peek_string_at(size_t offset, size_t maxsize=~static_cast< size_t >(0)) const
std::unique_ptr< T[]> read_conv_array(size_t size) const
Definition BinaryStream.hpp:411
size_t pos() const
Definition BinaryStream.hpp:125
virtual uint8_t * start()
Definition BinaryStream.hpp:203
result< T > read_conv() const
Definition BinaryStream.hpp:367
const T * peek_array(size_t size) const
Definition BinaryStream.hpp:324
void setpos(size_t pos) const
Definition BinaryStream.hpp:109
virtual const uint8_t * end() const
Definition BinaryStream.hpp:219
static bool is_all_zero(const T &buffer)
Definition BinaryStream.hpp:189
result< std::string > read_string(size_t maxsize=~static_cast< size_t >(0)) const
result< std::u16string > read_u16string(size_t length) const
void decrement_pos(size_t value) const
Definition BinaryStream.hpp:117
result< T > read() const
Definition BinaryStream.hpp:291
BinaryStream(STREAM_TYPE type)
Definition BinaryStream.hpp:48
virtual const uint8_t * start() const
Definition BinaryStream.hpp:215
virtual ~BinaryStream()=default
STREAM_TYPE type() const
Definition BinaryStream.hpp:54
virtual ok_error_t read_data(std::vector< uint8_t > &container, uint64_t size)
Definition BinaryStream.hpp:100
virtual uint8_t * end()
Definition BinaryStream.hpp:211
Definition BinaryStream.hpp:248
const BinaryStream & operator*() const
Definition BinaryStream.hpp:280
ScopedStream(BinaryStream &stream, uint64_t pos)
Definition BinaryStream.hpp:256
ScopedStream(BinaryStream &stream)
Definition BinaryStream.hpp:263
ScopedStream(const ScopedStream &)=delete
ScopedStream & operator=(const ScopedStream &)=delete
ScopedStream & operator=(ScopedStream &&)=delete
~ScopedStream()
Definition BinaryStream.hpp:268
BinaryStream & operator*()
Definition BinaryStream.hpp:276
ScopedStream(ScopedStream &&)=delete
BinaryStream * operator->()
Definition BinaryStream.hpp:272
tl::unexpected< lief_errors > make_error_code(lief_errors e)
Create an standard error code from lief_errors.
Definition errors.hpp:51
LIEF namespace.
Definition Abstract/Binary.hpp:32
result< ok_t > ok_error_t
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:108
ok_t ok()
Return success for function with return type ok_error_t.
Definition errors.hpp:92
tl::expected< T, lief_errors > result
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:74