LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
VectorStream.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_VECTOR_STREAM_H
16#define LIEF_VECTOR_STREAM_H
17
18#include <string_view>
19#include <memory>
20#include <vector>
21
23#include "LIEF/errors.hpp"
24#include "LIEF/visibility.h"
25
26namespace LIEF {
27class SpanStream;
29 public:
31 using BinaryStream::p;
33
34 static result<VectorStream> from_file(std::string_view file);
35 VectorStream(std::vector<uint8_t> data) :
37 binary_(std::move(data)),
38 size_(binary_.size()) {}
39
40 VectorStream() = delete;
41
42 // VectorStream should not be copyable for performances reasons
43 VectorStream(const VectorStream&) = delete;
45
46 VectorStream(VectorStream&& other) noexcept = default;
47 VectorStream& operator=(VectorStream&& other) noexcept = default;
48
49 uint64_t size() const override {
50 return size_;
51 }
52
53 const std::vector<uint8_t>& content() const {
54 return binary_;
55 }
56
57 std::vector<uint8_t>&& move_content() {
58 size_ = 0;
59 return std::move(binary_);
60 }
61
62 const uint8_t* p() const override {
63 return this->binary_.data() + this->pos();
64 }
65
66 const uint8_t* start() const override {
67 return this->binary_.data();
68 }
69
70 const uint8_t* end() const override {
71 return this->binary_.data() + this->binary_.size();
72 }
73
74
75 std::unique_ptr<SpanStream> slice(uint32_t offset, size_t size) const;
76 std::unique_ptr<SpanStream> slice(uint32_t offset) const;
77
78 static bool classof(const BinaryStream& stream) {
79 return stream.type() == STREAM_TYPE::VECTOR;
80 }
81
82 protected:
83 result<const void*> read_at(uint64_t offset, uint64_t size,
84 uint64_t /*va*/) const override {
85 const uint64_t stream_size = this->size();
86 if (offset > stream_size || size > stream_size - offset) {
88 }
89 return binary_.data() + offset;
90 }
91 std::vector<uint8_t> binary_;
92 uint64_t size_ = 0; // Original size without alignment
93};
94}
95
96#endif
STREAM_TYPE
Definition BinaryStream.hpp:38
@ VECTOR
Definition BinaryStream.hpp:40
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
Definition SpanStream.hpp:31
VectorStream(VectorStream &&other) noexcept=default
std::vector< uint8_t > && move_content()
Definition VectorStream.hpp:57
static bool classof(const BinaryStream &stream)
Definition VectorStream.hpp:78
VectorStream(const VectorStream &)=delete
const uint8_t * p() const override
Definition VectorStream.hpp:62
VectorStream & operator=(const VectorStream &)=delete
static result< VectorStream > from_file(std::string_view file)
uint64_t size() const override
Definition VectorStream.hpp:49
const std::vector< uint8_t > & content() const
Definition VectorStream.hpp:53
std::unique_ptr< SpanStream > slice(uint32_t offset) const
std::unique_ptr< SpanStream > slice(uint32_t offset, size_t size) const
VectorStream & operator=(VectorStream &&other) noexcept=default
const uint8_t * start() const override
Definition VectorStream.hpp:66
const uint8_t * end() const override
Definition VectorStream.hpp:70
VectorStream(std::vector< uint8_t > data)
Definition VectorStream.hpp:35
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:78
@ read_error
Definition errors.hpp:24
tl::unexpected< lief_errors > make_error_code(lief_errors e)
Create a standard error code from lief_errors.
Definition errors.hpp:54
LIEF namespace.
Definition Abstract/Binary.hpp:41
#define LIEF_API
Definition visibility.h:44