Go to the documentation of this file.
16#ifndef LIEF_BINARY_STREAM_H
17#define LIEF_BINARY_STREAM_H
35 friend class ASN1Reader;
37 enum class STREAM_TYPE {
51 virtual uint64_t
size()
const = 0;
62 result<std::string>
read_string(
size_t maxsize = ~
static_cast<size_t>(0))
const;
63 result<std::string>
peek_string(
size_t maxsize = ~
static_cast<size_t>(0))
const;
64 result<std::string>
peek_string_at(
size_t offset,
size_t maxsize = ~
static_cast<size_t>(0))
const;
69 result<std::string>
read_mutf8(
size_t maxsize = ~
static_cast<size_t>(0))
const;
77 uint64_t offset, uint64_t size,
78 uint64_t virtual_address = 0)
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, virtual_address)) {
101 if (!peek_data(container, pos(), size)) {
112 const size_t size = count *
sizeof(T);
113 auto ret = peek_objects(container, count);
123 return peek_objects_at(pos(), container, count);
128 const auto current_p = pos();
131 const size_t size = count *
sizeof(T);
133 if (!can_read(offset, size)) {
138 container.resize(count);
140 if (!peek_in(container.data(), pos(), size)) {
169 operator bool()
const {
170 return pos_ < size();
174 const T* read_array(
size_t size)
const;
177 result<T> peek()
const;
180 result<T> peek(
size_t offset)
const;
183 const T* peek_array(
size_t size)
const;
186 const T* peek_array(
size_t offset,
size_t size)
const;
189 result<T> read()
const;
192 bool can_read()
const;
195 bool can_read(
size_t offset)
const;
197 bool can_read(int64_t offset, int64_t size)
const {
198 return offset < (int64_t)this->size() && (offset + size) < (int64_t)this->size();
201 size_t align(
size_t align_on)
const;
209 const auto* ptr =
reinterpret_cast<const uint8_t *const
>(&buffer);
210 return std::all_of(ptr, ptr +
sizeof(T),
211 [] (uint8_t x) {
return x == 0; });
218 virtual const uint8_t*
p()
const {
223 return const_cast<uint8_t*
>(
static_cast<const BinaryStream*
>(
this)->start());
226 virtual uint8_t*
p() {
227 return const_cast<uint8_t*
>(
static_cast<const BinaryStream*
>(
this)->p());
231 return const_cast<uint8_t*
>(
static_cast<const BinaryStream*
>(
this)->end());
234 virtual const uint8_t*
start()
const {
238 virtual const uint8_t*
end()
const {
243 BinaryStream() =
default;
244 virtual result<const void*> read_at(uint64_t offset, uint64_t size,
245 uint64_t virtual_address = 0)
const = 0;
246 virtual ok_error_t peek_in(
void* dst, uint64_t offset, uint64_t size,
247 uint64_t virtual_address = 0)
const {
248 if (
auto raw = read_at(offset, size, virtual_address)) {
249 if (dst ==
nullptr) {
253 const void* ptr = *raw;
255 if (ptr ==
nullptr) {
259 memcpy(dst, ptr, size);
264 mutable size_t pos_ = 0;
265 bool endian_swap_ =
false;
266 STREAM_TYPE stype_ = STREAM_TYPE::UNKNOWN;
290 stream_.setpos(pos_);
307 BinaryStream& stream_;
319 endian_swap_(stream.should_swap()),
326 endian_swap_(stream.should_swap()),
333 stream_.set_endian_swap(endian_swap_);
349 bool endian_swap_ =
false;
350 BinaryStream& stream_;
366 const auto current_p =
pos();
368 if (
auto res = peek_in(&ret,
pos(),
sizeof(T))) {
382 const size_t saved_offset = this->
pos();
385 this->
setpos(saved_offset);
396 return reinterpret_cast<const T*
>(raw.value());
401 const size_t saved_offset = this->
pos();
404 this->
setpos(saved_offset);
413 return pos_ <
size() && (pos_ +
sizeof(T)) <
size();
421 return offset <
size() && (offset +
sizeof(T)) <
size();
Definition ASN1Reader.hpp:32
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
size_t align(size_t align_on) const
void increment_pos(size_t value) const
Definition BinaryStream.hpp:153
result< std::string > read_mutf8(size_t maxsize=~static_cast< size_t >(0)) const
result< uint64_t > read_sleb128() const
result< T > peek() const
Definition BinaryStream.hpp:365
result< uint64_t > read_uleb128() const
result< int64_t > read_dwarf_encoded(uint8_t encoding) const
virtual uint8_t * p()
Definition BinaryStream.hpp:226
result< std::string > peek_string(size_t maxsize=~static_cast< size_t >(0)) const
virtual const uint8_t * p() const
Definition BinaryStream.hpp:218
const T * read_array(size_t size) const
Definition BinaryStream.hpp:426
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:410
ok_error_t read_objects(std::vector< T > &container, uint64_t count)
Definition BinaryStream.hpp:111
ok_error_t peek_objects_at(uint64_t offset, std::vector< T > &container, uint64_t count)
Definition BinaryStream.hpp:127
result< std::u16string > peek_u16string(size_t length) const
result< std::u16string > read_u16string() const
void set_endian_swap(bool swap)
Definition BinaryStream.hpp:203
result< std::u16string > peek_u16string() const
bool should_swap() const
Definition BinaryStream.hpp:214
result< std::string > peek_string_at(size_t offset, size_t maxsize=~static_cast< size_t >(0)) const
ok_error_t peek_objects(std::vector< T > &container, uint64_t count)
Definition BinaryStream.hpp:122
size_t pos() const
Definition BinaryStream.hpp:165
virtual uint8_t * start()
Definition BinaryStream.hpp:222
const T * peek_array(size_t size) const
Definition BinaryStream.hpp:391
void setpos(size_t pos) const
Definition BinaryStream.hpp:149
virtual const uint8_t * end() const
Definition BinaryStream.hpp:238
static bool is_all_zero(const T &buffer)
Definition BinaryStream.hpp:208
result< std::string > read_string(size_t maxsize=~static_cast< size_t >(0)) const
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:76
void decrement_pos(size_t value) const
Definition BinaryStream.hpp:157
result< T > read() const
Definition BinaryStream.hpp:355
BinaryStream(STREAM_TYPE type)
Definition BinaryStream.hpp:47
virtual const uint8_t * start() const
Definition BinaryStream.hpp:234
bool can_read(int64_t offset, int64_t size) const
Definition BinaryStream.hpp:197
virtual ~BinaryStream()=default
STREAM_TYPE type() const
Definition BinaryStream.hpp:53
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:230
Definition BinaryStream.hpp:269
const BinaryStream & operator*() const
Definition BinaryStream.hpp:301
ScopedStream(BinaryStream &stream, uint64_t pos)
Definition BinaryStream.hpp:277
ScopedStream(BinaryStream &stream)
Definition BinaryStream.hpp:284
ScopedStream(const ScopedStream &)=delete
ScopedStream & operator=(const ScopedStream &)=delete
ScopedStream & operator=(ScopedStream &&)=delete
~ScopedStream()
Definition BinaryStream.hpp:289
BinaryStream & operator*()
Definition BinaryStream.hpp:297
ScopedStream(ScopedStream &&)=delete
BinaryStream * operator->()
Definition BinaryStream.hpp:293
Definition BinaryStream.hpp:310
ToggleEndianness(BinaryStream &stream, bool value)
Definition BinaryStream.hpp:318
ToggleEndianness(ToggleEndianness &&)=delete
ToggleEndianness & operator=(const ToggleEndianness &)=delete
ToggleEndianness & operator=(ToggleEndianness &&)=delete
BinaryStream & operator*()
Definition BinaryStream.hpp:340
BinaryStream * operator->()
Definition BinaryStream.hpp:336
ToggleEndianness(BinaryStream &stream)
Definition BinaryStream.hpp:325
~ToggleEndianness()
Definition BinaryStream.hpp:332
const BinaryStream & operator*() const
Definition BinaryStream.hpp:344
ToggleEndianness(const ToggleEndianness &)=delete
@ read_error
Definition errors.hpp:24
tl::unexpected< lief_errors > make_error_code(lief_errors e)
Create an standard error code from lief_errors.
Definition errors.hpp:52
LIEF namespace.
Definition Abstract/Binary.hpp:36
result< ok_t > ok_error_t
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:109
ok_t ok()
Return success for function with return type ok_error_t.
Definition errors.hpp:93
void swap_endian(T *)
Definition endianness_support.hpp:116
tl::expected< T, lief_errors > result
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:75
#define LIEF_API
Definition visibility.h:41