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 <string>
20
21#include "LIEF/errors.hpp"
22#include "LIEF/span.hpp"
23#include "LIEF/BinaryStream/BinaryStream.hpp"
24
25namespace LIEF {
26class SpanStream : public BinaryStream {
27 public:
28 using BinaryStream::p;
29 using BinaryStream::end;
30 using BinaryStream::start;
31
32 static result<SpanStream> from_vector(const std::vector<uint8_t>& data) {
33 return SpanStream(data);
34 }
35
36 template<size_t N>
37 static result<SpanStream> from_array(const std::array<uint8_t, N>& data) {
38 return SpanStream(data.data(), N);
39 }
40
41 SpanStream(span<const uint8_t> data);
42 SpanStream(span<uint8_t> data);
43
44 SpanStream(const uint8_t* p, size_t size) :
45 data_{p, p + size}
46 {
47 stype_ = STREAM_TYPE::SPAN;
48 }
49
50 SpanStream(const std::vector<uint8_t>& data);
51 SpanStream() = delete;
52
53 SpanStream(const SpanStream&) = delete;
54 SpanStream& operator=(const SpanStream&) = delete;
55
56 SpanStream(SpanStream&& other);
57 SpanStream& operator=(SpanStream&& other);
58
59 uint64_t size() const override {
60 return data_.size();
61 }
62
63 const uint8_t* p() const override {
64 return data_.data() + this->pos();
65 }
66
67 const uint8_t* start() const override {
68 return data_.data();
69 }
70
71 const uint8_t* end() const override {
72 return data_.data() + size();
73 }
74
75 std::vector<uint8_t> content() const;
76
77 result<SpanStream> slice(size_t offset, size_t size) const;
78 result<SpanStream> slice(size_t offset) const;
79
80 static bool classof(const BinaryStream& stream);
81 ~SpanStream() override;
82
83 protected:
84 result<const void*> read_at(uint64_t offset, uint64_t size) const override;
85 span<const uint8_t> data_;
86};
87}
88
89#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:34
Definition SpanStream.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