LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
MemoryStream.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_MEMORY_STREAM_H
17#define LIEF_MEMORY_STREAM_H
18
19#include <cstdint>
20
21#include "LIEF/errors.hpp"
22#include "LIEF/visibility.h"
24
25namespace LIEF {
26class Binary;
28 public:
29 using BinaryStream::p;
32
33 MemoryStream() = delete;
35 MemoryStream(uintptr_t base_address, uint64_t size) :
37 baseaddr_(base_address),
38 size_(size) {}
39
40 MemoryStream(const MemoryStream&) = delete;
42
43 MemoryStream(MemoryStream&&) noexcept = default;
44 MemoryStream& operator=(MemoryStream&&) noexcept = default;
45
46 uintptr_t base_address() const {
47 return baseaddr_;
48 }
49
50 const uint8_t* p() const override {
51 return start() + pos();
52 }
53
54 const uint8_t* start() const override {
55 return reinterpret_cast<const uint8_t*>(baseaddr_);
56 }
57
58 const uint8_t* end() const override {
59 return start() + size_;
60 }
61
63 return binary_;
64 }
65
66 uint64_t size() const override {
67 return size_;
68 }
69
70 bool bind_binary(Binary& bin) override {
71 binary_ = &bin;
72 return true;
73 }
74
75 ~MemoryStream() override = default;
76
77 static bool classof(const BinaryStream& stream) {
78 return stream.type() == BinaryStream::STREAM_TYPE::MEMORY;
79 }
80
81 protected:
82 result<const void*> read_at(uint64_t offset, uint64_t size,
83 uint64_t va) const override;
84 uintptr_t baseaddr_ = 0;
85 uint64_t size_ = 0;
86 Binary* binary_ = nullptr;
87};
88}
89
90#endif
STREAM_TYPE
Definition BinaryStream.hpp:39
@ MEMORY
Definition BinaryStream.hpp:42
virtual const uint8_t * p() const
Definition BinaryStream.hpp:289
size_t pos() const
Definition BinaryStream.hpp:199
virtual uint8_t * start()
Definition BinaryStream.hpp:293
BinaryStream(STREAM_TYPE type)
Definition BinaryStream.hpp:50
STREAM_TYPE type() const
Definition BinaryStream.hpp:55
virtual uint8_t * end()
Definition BinaryStream.hpp:301
Generic interface representing a binary executable.
Definition Abstract/Binary.hpp:60
const uint8_t * end() const override
Definition MemoryStream.hpp:58
bool bind_binary(Binary &bin) override
Definition MemoryStream.hpp:70
MemoryStream(uintptr_t base_address)
static bool classof(const BinaryStream &stream)
Definition MemoryStream.hpp:77
MemoryStream(const MemoryStream &)=delete
Binary * binary()
Definition MemoryStream.hpp:62
MemoryStream & operator=(const MemoryStream &)=delete
uint64_t size() const override
Definition MemoryStream.hpp:66
~MemoryStream() override=default
MemoryStream(MemoryStream &&) noexcept=default
const uint8_t * start() const override
Definition MemoryStream.hpp:54
uintptr_t base_address() const
Definition MemoryStream.hpp:46
const uint8_t * p() const override
Definition MemoryStream.hpp:50
MemoryStream(uintptr_t base_address, uint64_t size)
Definition MemoryStream.hpp:35
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:79
LIEF namespace.
Definition Abstract/Binary.hpp:41
#define LIEF_API
Definition visibility.h:45