LIEF: Library to Instrument Executable Formats Version 0.15.1
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 VECTOR_BINARY_STREAM_H
17#define VECTOR_BINARY_STREAM_H
18
19#include <vector>
20#include <string>
21
22#include "LIEF/errors.hpp"
24
25namespace LIEF {
26class VectorStream : public BinaryStream {
27 public:
28 using BinaryStream::p;
31
32 static result<VectorStream> from_file(const std::string& file);
33 VectorStream(std::vector<uint8_t> data) :
35 binary_(std::move(data)),
36 size_(binary_.size())
37 {}
38
39 VectorStream() = delete;
40
41 // VectorStream should not be copyable for performances reasons
42 VectorStream(const VectorStream&) = delete;
44
45 VectorStream(VectorStream&& other) noexcept = default;
46 VectorStream& operator=(VectorStream&& other) noexcept = default;
47
48 uint64_t size() const override {
49 return size_;
50 }
51
52 const std::vector<uint8_t>& content() const {
53 return binary_;
54 }
55
56 std::vector<uint8_t>&& move_content() {
57 size_ = 0;
58 return std::move(binary_);
59 }
60
61 const uint8_t* p() const override {
62 return this->binary_.data() + this->pos();
63 }
64
65 const uint8_t* start() const override {
66 return this->binary_.data();
67 }
68
69 const uint8_t* end() const override {
70
71 return this->binary_.data() + this->binary_.size();
72 }
73
74 static bool classof(const BinaryStream& stream) {
75 return stream.type() == STREAM_TYPE::VECTOR;
76 }
77
78 protected:
79 result<const void*> read_at(uint64_t offset, uint64_t size) const override {
80 const uint64_t stream_size = this->size();
81 if (offset > stream_size || (offset + size) > stream_size) {
83 }
84 return binary_.data() + offset;
85 }
86 std::vector<uint8_t> binary_;
87 uint64_t size_ = 0; // Original size without alignment
88};
89}
90
91#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:34
STREAM_TYPE
Definition BinaryStream.hpp:38
virtual const uint8_t * p() const
Definition BinaryStream.hpp:199
size_t pos() const
Definition BinaryStream.hpp:125
virtual uint8_t * start()
Definition BinaryStream.hpp:203
STREAM_TYPE type() const
Definition BinaryStream.hpp:54
virtual uint8_t * end()
Definition BinaryStream.hpp:211
Definition VectorStream.hpp:26
VectorStream(VectorStream &&other) noexcept=default
std::vector< uint8_t > && move_content()
Definition VectorStream.hpp:56
static bool classof(const BinaryStream &stream)
Definition VectorStream.hpp:74
static result< VectorStream > from_file(const std::string &file)
VectorStream(const VectorStream &)=delete
const uint8_t * p() const override
Definition VectorStream.hpp:61
VectorStream & operator=(const VectorStream &)=delete
uint64_t size() const override
Definition VectorStream.hpp:48
const std::vector< uint8_t > & content() const
Definition VectorStream.hpp:52
VectorStream & operator=(VectorStream &&other) noexcept=default
const uint8_t * start() const override
Definition VectorStream.hpp:65
const uint8_t * end() const override
Definition VectorStream.hpp:69
VectorStream(std::vector< uint8_t > data)
Definition VectorStream.hpp:33
tl::unexpected< lief_errors > make_error_code(lief_errors e)
Create an standard error code from lief_errors.
Definition errors.hpp:51
LIEF namespace.
Definition Abstract/Binary.hpp:32
tl::expected< T, lief_errors > result
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:74