Monero
Loading...
Searching...
No Matches
byte_slice.h
Go to the documentation of this file.
1// Copyright (c) 2019-2022, The Monero Project
2//
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without modification, are
6// permitted provided that the following conditions are met:
7//
8// 1. Redistributions of source code must retain the above copyright notice, this list of
9// conditions and the following disclaimer.
10//
11// 2. Redistributions in binary form must reproduce the above copyright notice, this list
12// of conditions and the following disclaimer in the documentation and/or other
13// materials provided with the distribution.
14//
15// 3. Neither the name of the copyright holder nor the names of its contributors may be
16// used to endorse or promote products derived from this software without specific
17// prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29#pragma once
30
31#include <cstddef>
32#include <cstdint>
33#include <memory>
34#include <string>
35#include <vector>
36
37#include "span.h"
38
39namespace epee
40{
41 struct byte_slice_data;
42 class byte_stream;
43
45 {
47 static void call(void*, void* ptr) noexcept;
48 void operator()(byte_slice_data* ptr) const noexcept
49 {
50 call(nullptr, ptr);
51 }
52 };
53
56 {
57 void operator()(std::uint8_t* buf) const noexcept;
58 };
59
69 {
70 /* A custom reference count is used instead of shared_ptr because it allows
71 for an allocation optimization for the span constructor. This also
72 reduces the size of this class by one pointer. */
73 std::unique_ptr<byte_slice_data, release_byte_slice> storage_;
75
77 byte_slice(byte_slice_data* storage, span<const std::uint8_t> portion) noexcept;
78
79 struct adapt_buffer{};
80
81 template<typename T>
82 explicit byte_slice(const adapt_buffer, T&& buffer);
83
84 public:
85 using value_type = std::uint8_t;
86 using size_type = std::size_t;
87 using difference_type = std::ptrdiff_t;
88 using pointer = const std::uint8_t*;
89 using const_pointer = const std::uint8_t*;
90 using reference = std::uint8_t;
91 using const_reference = std::uint8_t;
94
96 byte_slice() noexcept
97 : storage_(nullptr), portion_()
98 {}
99
101 byte_slice(std::nullptr_t) noexcept
102 : byte_slice()
103 {}
104
106 explicit byte_slice(std::initializer_list<span<const std::uint8_t>> sources);
107
109 explicit byte_slice(std::vector<std::uint8_t>&& buffer);
110
112 explicit byte_slice(std::string&& buffer);
113
115 explicit byte_slice(byte_stream&& stream, bool shrink = true);
116
117 byte_slice(byte_slice&& source) noexcept;
118 ~byte_slice() noexcept = default;
119
121 byte_slice& operator=(byte_slice&&) noexcept;
122
124 byte_slice clone() const noexcept { return {storage_.get(), portion_}; }
125
126 iterator begin() const noexcept { return portion_.begin(); }
127 const_iterator cbegin() const noexcept { return portion_.begin(); }
128
129 iterator end() const noexcept { return portion_.end(); }
130 const_iterator cend() const noexcept { return portion_.end(); }
131
132 bool empty() const noexcept { return storage_ == nullptr; }
133 const std::uint8_t* data() const noexcept { return portion_.data(); }
134 std::size_t size() const noexcept { return portion_.size(); }
135
142 std::size_t remove_prefix(std::size_t max_bytes) noexcept;
143
150 byte_slice take_slice(std::size_t max_bytes) noexcept;
151
157 byte_slice get_slice(std::size_t begin, std::size_t end) const;
158
160 std::unique_ptr<byte_slice_data, release_byte_slice> take_buffer() noexcept;
161 };
162
165
168 byte_buffer byte_buffer_resize(byte_buffer buf, std::size_t length) noexcept;
169
175 byte_buffer byte_buffer_increase(byte_buffer buf, std::size_t current, std::size_t more);
176} // epee
177
Definition byte_slice.h:69
std::uint8_t value_type
Definition byte_slice.h:85
byte_slice(std::nullptr_t) noexcept
Construct empty slice.
Definition byte_slice.h:101
std::uint8_t reference
Definition byte_slice.h:90
byte_slice(byte_slice_data *storage, span< const std::uint8_t > portion) noexcept
Internal use only; use to increase storage reference count.
Definition byte_slice.cpp:132
std::unique_ptr< byte_slice_data, release_byte_slice > storage_
Definition byte_slice.h:73
std::size_t size_type
Definition byte_slice.h:86
const std::uint8_t * data() const noexcept
Definition byte_slice.h:133
std::ptrdiff_t difference_type
Definition byte_slice.h:87
byte_slice clone() const noexcept
Definition byte_slice.h:124
~byte_slice() noexcept=default
std::unique_ptr< byte_slice_data, release_byte_slice > take_buffer() noexcept
Definition byte_slice.cpp:260
pointer iterator
Definition byte_slice.h:92
iterator end() const noexcept
Definition byte_slice.h:129
std::size_t size() const noexcept
Definition byte_slice.h:134
byte_slice take_slice(std::size_t max_bytes) noexcept
Definition byte_slice.cpp:233
std::size_t remove_prefix(std::size_t max_bytes) noexcept
Definition byte_slice.cpp:225
const std::uint8_t * const_pointer
Definition byte_slice.h:89
const_iterator cbegin() const noexcept
Definition byte_slice.h:127
span< const std::uint8_t > portion_
Definition byte_slice.h:74
iterator begin() const noexcept
Definition byte_slice.h:126
byte_slice() noexcept
Construct empty slice.
Definition byte_slice.h:96
std::uint8_t const_reference
Definition byte_slice.h:91
byte_slice get_slice(std::size_t begin, std::size_t end) const
Definition byte_slice.cpp:250
const_pointer const_iterator
Definition byte_slice.h:93
const_iterator cend() const noexcept
Definition byte_slice.h:130
const std::uint8_t * pointer
Definition byte_slice.h:88
bool empty() const noexcept
Definition byte_slice.h:132
A partial drop-in replacement for std::ostream.
Definition byte_stream.h:58
Non-owning sequence of data. Does not deep copy.
Definition span.h:55
#define const
Definition ipfrdr.c:80
TODO: (mj-xmr) This will be reduced in an another PR.
Definition byte_slice.h:40
std::unique_ptr< std::uint8_t, release_byte_buffer > byte_buffer
Alias for a buffer that has space for a byte_slice ref count.
Definition byte_slice.h:164
byte_buffer byte_buffer_increase(byte_buffer buf, std::size_t current, std::size_t more)
Definition byte_slice.cpp:285
byte_buffer byte_buffer_resize(byte_buffer buf, std::size_t length) noexcept
Definition byte_slice.cpp:267
Definition enums.h:68
const CharType(& source)[N]
Definition pointer.h:1147
const char * buf
Definition slow_memmem.cpp:73
unsigned char uint8_t
Definition stdint.h:124
Definition byte_slice.h:79
Definition byte_slice.cpp:47
Frees ref count + buffer allocated internally by byte_buffer.
Definition byte_slice.h:56
void operator()(std::uint8_t *buf) const noexcept
Definition byte_slice.cpp:126
Definition byte_slice.h:45
void operator()(byte_slice_data *ptr) const noexcept
Definition byte_slice.h:48
static void call(void *, void *ptr) noexcept
For use with zmq_message_init_data, use second arg for buffer pointer.
Definition byte_slice.cpp:58
#define T(x)