LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
SpanStream.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 LIEF_SPAN_STREAM_H
17#define LIEF_SPAN_STREAM_H
18
19#include <cstdint>
20#include <vector>
21#include <array>
22#include <cstddef>
23
24#include "LIEF/errors.hpp"
25#include "LIEF/span.hpp"
26#include "LIEF/BinaryStream/BinaryStream.hpp"
27
28namespace LIEF {
29class SpanStream : public BinaryStream {
30 public:
31 using BinaryStream::p;
32 using BinaryStream::end;
33 using BinaryStream::start;
34
35 static result<SpanStream> from_vector(const std::vector<uint8_t>& data) {
36 return SpanStream(data);
37 }
38
39 template<size_t N>
40 static result<SpanStream> from_array(const std::array<uint8_t, N>& data) {
41 return SpanStream(data.data(), N);
42 }
43
44 SpanStream(span<const uint8_t> data) :
45 SpanStream(data.data(), data.size())
46 {}
47
48 SpanStream(span<uint8_t> data) :
49 SpanStream(data.data(), data.size())
50 {}
51
52 SpanStream(const uint8_t* p, size_t size) :
53 BinaryStream(BinaryStream::STREAM_TYPE::SPAN),
54 data_{p, p + size}
55 {}
56
57 SpanStream(const std::vector<uint8_t>& data) :
58 SpanStream(data.data(), data.size())
59 {}
60
61 SpanStream() = delete;
62
63 SpanStream(const SpanStream&) = delete;
64 SpanStream& operator=(const SpanStream&) = delete;
65
66 SpanStream(SpanStream&& other) noexcept = default;
67 SpanStream& operator=(SpanStream&& other) noexcept = default;
68
69 uint64_t size() const override {
70 return data_.size();
71 }
72
73 const uint8_t* p() const override {
74 return data_.data() + this->pos();
75 }
76
77 const uint8_t* start() const override {
78 return data_.data();
79 }
80
81 const uint8_t* end() const override {
82 return data_.data() + size();
83 }
84
85 std::vector<uint8_t> content() const {
86 return {data_.begin(), data_.end()};
87 }
88
89 result<SpanStream> slice(size_t offset, size_t size) const {
90 if (offset > data_.size() || (offset + size) > data_.size()) {
91 return make_error_code(lief_errors::read_out_of_bound);
92 }
93 return data_.subspan(offset, size);
94 }
95 result<SpanStream> slice(size_t offset) const {
96 if (offset > data_.size()) {
97 return make_error_code(lief_errors::read_out_of_bound);
98 }
99 return data_.subspan(offset, data_.size() - offset);
100 }
101
102 static bool classof(const BinaryStream& stream) {
103 return stream.type() == BinaryStream::STREAM_TYPE::SPAN;
104 }
105
106 ~SpanStream() override = default;
107
108 protected:
109 result<const void*> read_at(uint64_t offset, uint64_t size) const override {
110 const uint64_t stream_size = this->size();
111 if (offset > stream_size || (offset + size) > stream_size) {
112 return make_error_code(lief_errors::read_error);
113 }
114 return data_.data() + offset;
115 }
116 span<const uint8_t> data_;
117};
118}
119
120#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:34
Definition SpanStream.hpp:29
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