LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
MemoryStream.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef LIEF_MEMORY_STREAM_H
16#define LIEF_MEMORY_STREAM_H
17
18#include <cstdint>
19
21#include "LIEF/errors.hpp"
22#include "LIEF/visibility.h"
23
24namespace LIEF {
25class Binary;
27 public:
29 using BinaryStream::p;
31
32 MemoryStream() = delete;
34 MemoryStream(uintptr_t base_address, uint64_t size) :
36 baseaddr_(base_address),
37 size_(size) {}
38
39 MemoryStream(const MemoryStream&) = delete;
41
42 MemoryStream(MemoryStream&&) noexcept = default;
43 MemoryStream& operator=(MemoryStream&&) noexcept = default;
44
45 uintptr_t base_address() const {
46 return baseaddr_;
47 }
48
49 const uint8_t* p() const override {
50 return start() + pos();
51 }
52
53 const uint8_t* start() const override {
54 return reinterpret_cast<const uint8_t*>(baseaddr_);
55 }
56
57 const uint8_t* end() const override {
58 return start() + size_;
59 }
60
62 return binary_;
63 }
64
65 uint64_t size() const override {
66 return size_;
67 }
68
69 bool bind_binary(Binary& bin) override {
70 binary_ = &bin;
71 return true;
72 }
73
74 ~MemoryStream() override = default;
75
76 static bool classof(const BinaryStream& stream) {
77 return stream.type() == BinaryStream::STREAM_TYPE::MEMORY;
78 }
79
80 protected:
81 result<const void*> read_at(uint64_t offset, uint64_t size,
82 uint64_t va) const override;
83 uintptr_t baseaddr_ = 0;
84 uint64_t size_ = 0;
85 Binary* binary_ = nullptr;
86};
87}
88
89#endif
STREAM_TYPE
Definition BinaryStream.hpp:38
@ MEMORY
Definition BinaryStream.hpp:41
virtual const uint8_t * p() const
Definition BinaryStream.hpp:302
size_t pos() const
Definition BinaryStream.hpp:206
virtual uint8_t * start()
Definition BinaryStream.hpp:306
BinaryStream(STREAM_TYPE type)
Definition BinaryStream.hpp:49
STREAM_TYPE type() const
Definition BinaryStream.hpp:54
virtual uint8_t * end()
Definition BinaryStream.hpp:314
Generic interface representing a binary executable.
Definition Abstract/Binary.hpp:60
const uint8_t * end() const override
Definition MemoryStream.hpp:57
bool bind_binary(Binary &bin) override
Definition MemoryStream.hpp:69
MemoryStream(uintptr_t base_address)
static bool classof(const BinaryStream &stream)
Definition MemoryStream.hpp:76
MemoryStream(const MemoryStream &)=delete
Binary * binary()
Definition MemoryStream.hpp:61
MemoryStream & operator=(const MemoryStream &)=delete
uint64_t size() const override
Definition MemoryStream.hpp:65
~MemoryStream() override=default
MemoryStream(MemoryStream &&) noexcept=default
const uint8_t * start() const override
Definition MemoryStream.hpp:53
uintptr_t base_address() const
Definition MemoryStream.hpp:45
const uint8_t * p() const override
Definition MemoryStream.hpp:49
MemoryStream(uintptr_t base_address, uint64_t size)
Definition MemoryStream.hpp:34
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:78
LIEF namespace.
Definition Abstract/Binary.hpp:41
#define LIEF_API
Definition visibility.h:44