LIEF: Library to Instrument Executable Formats Version 0.16.0
Loading...
Searching...
No Matches
MemoryStream.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2024 R. Thomas
2 * Copyright 2017 - 2024 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;
30 using BinaryStream::end;
31 using BinaryStream::start;
32
33 MemoryStream() = delete;
34 MemoryStream(uintptr_t base_address);
35 MemoryStream(uintptr_t base_address, uint64_t size) :
36 BinaryStream(BinaryStream::STREAM_TYPE::MEMORY),
37 baseaddr_(base_address),
38 size_(size)
39 {}
40
41 MemoryStream(const MemoryStream&) = delete;
42 MemoryStream& operator=(const MemoryStream&) = delete;
43
44 MemoryStream(MemoryStream&&) noexcept = default;
45 MemoryStream& operator=(MemoryStream&&) noexcept = default;
46
47 uintptr_t base_address() const {
48 return this->baseaddr_;
49 }
50
51 const uint8_t* p() const override {
52 return start() + pos();
53 }
54
55 const uint8_t* start() const override {
56 return reinterpret_cast<const uint8_t*>(baseaddr_);
57 }
58
59 const uint8_t* end() const override {
60 return start() + size_;
61 }
62
63 void binary(Binary& bin) {
64 this->binary_ = &bin;
65 }
66
67 Binary* binary() {
68 return this->binary_;
69 }
70
71 uint64_t size() const override {
72 return size_;
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, uint64_t va) const override;
83 uintptr_t baseaddr_ = 0;
84 uint64_t size_ = 0;
85 Binary* binary_ = nullptr;
86};
87}
88
89#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
STREAM_TYPE type() const
Definition BinaryStream.hpp:53
Abstract binary that exposes an uniform API for the different executable file formats.
Definition Abstract/Binary.hpp:49
Definition MemoryStream.hpp:27
const uint8_t * end() const override
Definition MemoryStream.hpp:59
MemoryStream(uintptr_t base_address)
static bool classof(const BinaryStream &stream)
Definition MemoryStream.hpp:77
MemoryStream(const MemoryStream &)=delete
Binary * binary()
Definition MemoryStream.hpp:67
MemoryStream & operator=(const MemoryStream &)=delete
void binary(Binary &bin)
Definition MemoryStream.hpp:63
uint64_t size() const override
Definition MemoryStream.hpp:71
~MemoryStream() override=default
MemoryStream(MemoryStream &&) noexcept=default
const uint8_t * start() const override
Definition MemoryStream.hpp:55
const uint8_t * p() const override
Definition MemoryStream.hpp:51
MemoryStream(uintptr_t base_address, uint64_t size)
Definition MemoryStream.hpp:35
LIEF namespace.
Definition Abstract/Binary.hpp:36
#define LIEF_API
Definition visibility.h:41