LIEF: Library to Instrument Executable Formats Version 0.16.0
Loading...
Searching...
No Matches
VectorStream.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_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;
32 using BinaryStream::end;
33 using BinaryStream::start;
34
35 static result<VectorStream> from_file(const std::string& file);
36 VectorStream(std::vector<uint8_t> data) :
37 BinaryStream(BinaryStream::STREAM_TYPE::VECTOR),
38 binary_(std::move(data)),
39 size_(binary_.size())
40 {}
41
42 VectorStream() = delete;
43
44 // VectorStream should not be copyable for performances reasons
45 VectorStream(const VectorStream&) = delete;
46 VectorStream& operator=(const VectorStream&) = delete;
47
48 VectorStream(VectorStream&& other) noexcept = default;
49 VectorStream& operator=(VectorStream&& other) noexcept = default;
50
51 uint64_t size() const override {
52 return size_;
53 }
54
55 const std::vector<uint8_t>& content() const {
56 return binary_;
57 }
58
59 std::vector<uint8_t>&& move_content() {
60 size_ = 0;
61 return std::move(binary_);
62 }
63
64 const uint8_t* p() const override {
65 return this->binary_.data() + this->pos();
66 }
67
68 const uint8_t* start() const override {
69 return this->binary_.data();
70 }
71
72 const uint8_t* end() const override {
73 return this->binary_.data() + this->binary_.size();
74 }
75
76
77 std::unique_ptr<SpanStream> slice(uint32_t offset, size_t size) const;
78 std::unique_ptr<SpanStream> slice(uint32_t offset) const;
79
80 static bool classof(const BinaryStream& stream) {
81 return stream.type() == STREAM_TYPE::VECTOR;
82 }
83
84 protected:
85 result<const void*> read_at(uint64_t offset, uint64_t size, 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
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
Definition SpanStream.hpp:32
Definition VectorStream.hpp:29
VectorStream(VectorStream &&other) noexcept=default
std::vector< uint8_t > && move_content()
Definition VectorStream.hpp:59
static bool classof(const BinaryStream &stream)
Definition VectorStream.hpp:80
static result< VectorStream > from_file(const std::string &file)
VectorStream(const VectorStream &)=delete
const uint8_t * p() const override
Definition VectorStream.hpp:64
VectorStream & operator=(const VectorStream &)=delete
uint64_t size() const override
Definition VectorStream.hpp:51
const std::vector< uint8_t > & content() const
Definition VectorStream.hpp:55
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:68
const uint8_t * end() const override
Definition VectorStream.hpp:72
VectorStream(std::vector< uint8_t > data)
Definition VectorStream.hpp:36
@ read_error
Definition errors.hpp:24
tl::unexpected< lief_errors > make_error_code(lief_errors e)
Create an standard error code from lief_errors.
Definition errors.hpp:52
LIEF namespace.
Definition Abstract/Binary.hpp:36
#define LIEF_API
Definition visibility.h:41