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