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