LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
SpanStream.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef LIEF_SPAN_STREAM_H
16#define LIEF_SPAN_STREAM_H
17
18#include <array>
19#include <cstddef>
20#include <cstdint>
21#include <memory>
22#include <vector>
23
25#include "LIEF/errors.hpp"
26#include "LIEF/span.hpp"
27#include "LIEF/visibility.h"
28
29namespace LIEF {
30class VectorStream;
32 public:
34 using BinaryStream::p;
36
37 static result<SpanStream> from_vector(const std::vector<uint8_t>& data) {
38 return SpanStream(data);
39 }
40
41 template<size_t N>
42 static result<SpanStream> from_array(const std::array<uint8_t, N>& data) {
43 return SpanStream(data.data(), N);
44 }
45
47 SpanStream(data.data(), data.size()) {}
48
50 SpanStream(data.data(), data.size()) {}
51
52 SpanStream(const uint8_t* p, size_t size) :
54 data_{p, p + size} {}
55
56 SpanStream(const std::vector<uint8_t>& data) :
57 SpanStream(data.data(), data.size()) {}
58
59 std::unique_ptr<SpanStream> clone() const {
60 return std::make_unique<SpanStream>(*this);
61 }
62
63 SpanStream() = delete;
64
65 SpanStream(const SpanStream& other) = default;
66 SpanStream& operator=(const SpanStream& other) = default;
67
68 SpanStream(SpanStream&& other) noexcept = default;
69 SpanStream& operator=(SpanStream&& other) noexcept = default;
70
71 uint64_t size() const override {
72 return data_.size();
73 }
74
75 const uint8_t* p() const override {
76 return data_.data() + this->pos();
77 }
78
79 const uint8_t* start() const override {
80 return data_.data();
81 }
82
83 const uint8_t* end() const override {
84 return data_.data() + size();
85 }
86
87 std::vector<uint8_t> content() const {
88 return {data_.begin(), data_.end()};
89 }
90
91 result<SpanStream> slice(size_t offset, size_t size) const {
92 if (offset > data_.size() || size > data_.size() - offset) {
94 }
95 return data_.subspan(offset, size);
96 }
97
98 result<SpanStream> slice(size_t offset) const {
99 if (offset > data_.size()) {
101 }
102 return data_.subspan(offset, data_.size() - offset);
103 }
104
105 std::unique_ptr<VectorStream> to_vector() const;
106
107 static bool classof(const BinaryStream& stream) {
108 return stream.type() == BinaryStream::STREAM_TYPE::SPAN;
109 }
110
111 ~SpanStream() override = default;
112
113 protected:
114 result<const void*> read_at(uint64_t offset, uint64_t size,
115 uint64_t /*va*/) const override {
116 const uint64_t stream_size = this->size();
117 if (offset > stream_size || size > stream_size - offset) {
119 }
120 return data_.data() + offset;
121 }
123};
124}
125
126#endif
STREAM_TYPE
Definition BinaryStream.hpp:38
@ SPAN
Definition BinaryStream.hpp:42
virtual const uint8_t * p() const
Definition BinaryStream.hpp:302
size_t pos() const
Definition BinaryStream.hpp:206
virtual uint8_t * start()
Definition BinaryStream.hpp:306
BinaryStream(STREAM_TYPE type)
Definition BinaryStream.hpp:49
STREAM_TYPE type() const
Definition BinaryStream.hpp:54
virtual uint8_t * end()
Definition BinaryStream.hpp:314
~SpanStream() override=default
std::unique_ptr< VectorStream > to_vector() const
SpanStream & operator=(SpanStream &&other) noexcept=default
SpanStream & operator=(const SpanStream &other)=default
SpanStream()=delete
SpanStream(const SpanStream &other)=default
const uint8_t * p() const override
Definition SpanStream.hpp:75
SpanStream(const uint8_t *p, size_t size)
Definition SpanStream.hpp:52
static bool classof(const BinaryStream &stream)
Definition SpanStream.hpp:107
std::unique_ptr< SpanStream > clone() const
Definition SpanStream.hpp:59
SpanStream(span< uint8_t > data)
Definition SpanStream.hpp:49
result< SpanStream > slice(size_t offset, size_t size) const
Definition SpanStream.hpp:91
static result< SpanStream > from_vector(const std::vector< uint8_t > &data)
Definition SpanStream.hpp:37
static result< SpanStream > from_array(const std::array< uint8_t, N > &data)
Definition SpanStream.hpp:42
const uint8_t * start() const override
Definition SpanStream.hpp:79
SpanStream(const std::vector< uint8_t > &data)
Definition SpanStream.hpp:56
SpanStream(SpanStream &&other) noexcept=default
const uint8_t * end() const override
Definition SpanStream.hpp:83
result< SpanStream > slice(size_t offset) const
Definition SpanStream.hpp:98
uint64_t size() const override
Definition SpanStream.hpp:71
std::vector< uint8_t > content() const
Definition SpanStream.hpp:87
SpanStream(span< const uint8_t > data)
Definition SpanStream.hpp:46
Definition VectorStream.hpp:28
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:78
@ read_out_of_bound
Definition errors.hpp:32
@ read_error
Definition errors.hpp:24
tl::unexpected< lief_errors > make_error_code(lief_errors e)
Create a standard error code from lief_errors.
Definition errors.hpp:54
LIEF namespace.
Definition Abstract/Binary.hpp:41
tcb::span< ElementType, Extent > span
Definition span.hpp:22
#define LIEF_API
Definition visibility.h:44