LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
VectorStream.hpp
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"
23#include "LIEF/BinaryStream/BinaryStream.hpp"
24
25namespace LIEF {
26class VectorStream : public BinaryStream {
27 public:
28 using BinaryStream::p;
29 using BinaryStream::end;
30 using BinaryStream::start;
31
32 static result<VectorStream> from_file(const std::string& file);
33 VectorStream(std::vector<uint8_t> data) :
34 BinaryStream(BinaryStream::STREAM_TYPE::VECTOR),
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;
43 VectorStream& operator=(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 return this->binary_.data() + this->binary_.size();
71 }
72
73 static bool classof(const BinaryStream& stream) {
74 return stream.type() == STREAM_TYPE::VECTOR;
75 }
76
77 protected:
78 result<const void*> read_at(uint64_t offset, uint64_t size) const override {
79 const uint64_t stream_size = this->size();
80 if (offset > stream_size || (offset + size) > stream_size) {
81 return make_error_code(lief_errors::read_error);
82 }
83 return binary_.data() + offset;
84 }
85 std::vector<uint8_t> binary_;
86 uint64_t size_ = 0; // Original size without alignment
87};
88}
89
90#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:34
Definition VectorStream.hpp:26
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:72