LIEF: Library to Instrument Executable Formats Version 0.15.1
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
24#include "LIEF/errors.hpp"
25#include "LIEF/span.hpp"
27
28namespace LIEF {
29class SpanStream : public BinaryStream {
30 public:
31 using BinaryStream::p;
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
45 SpanStream(data.data(), data.size())
46 {}
47
49 SpanStream(data.data(), data.size())
50 {}
51
52 SpanStream(const uint8_t* p, size_t size) :
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()) {
92 }
93 return data_.subspan(offset, size);
94 }
95 result<SpanStream> slice(size_t offset) const {
96 if (offset > data_.size()) {
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) {
113 }
114 return data_.data() + offset;
115 }
117};
118}
119
120#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 SpanStream.hpp:29
~SpanStream() override=default
SpanStream & operator=(SpanStream &&other) noexcept=default
SpanStream()=delete
const uint8_t * p() const override
Definition SpanStream.hpp:73
SpanStream(const uint8_t *p, size_t size)
Definition SpanStream.hpp:52
static bool classof(const BinaryStream &stream)
Definition SpanStream.hpp:102
SpanStream(const SpanStream &)=delete
SpanStream(span< uint8_t > data)
Definition SpanStream.hpp:48
result< SpanStream > slice(size_t offset, size_t size) const
Definition SpanStream.hpp:89
static result< SpanStream > from_vector(const std::vector< uint8_t > &data)
Definition SpanStream.hpp:35
static result< SpanStream > from_array(const std::array< uint8_t, N > &data)
Definition SpanStream.hpp:40
const uint8_t * start() const override
Definition SpanStream.hpp:77
SpanStream(const std::vector< uint8_t > &data)
Definition SpanStream.hpp:57
SpanStream(SpanStream &&other) noexcept=default
const uint8_t * end() const override
Definition SpanStream.hpp:81
result< SpanStream > slice(size_t offset) const
Definition SpanStream.hpp:95
SpanStream & operator=(const SpanStream &)=delete
uint64_t size() const override
Definition SpanStream.hpp:69
std::vector< uint8_t > content() const
Definition SpanStream.hpp:85
SpanStream(span< const uint8_t > data)
Definition SpanStream.hpp:44
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
tcb::span< ElementType, Extent > span
Definition span.hpp:22
tl::expected< T, lief_errors > result
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:74