| 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 | |
| 39 | namespace epee |
| 40 | { |
| 41 | struct byte_slice_data; |
| 42 | class byte_stream; |
| 43 | |
| 44 | struct release_byte_slice |
| 45 | { |
| 46 | //! For use with `zmq_message_init_data`, use second arg for buffer pointer. |
| 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 | |
| 54 | //! Frees ref count + buffer allocated internally by `byte_buffer`. |
| 55 | struct release_byte_buffer |
| 56 | { |
| 57 | void operator()(std::uint8_t* buf) const noexcept; |
| 58 | }; |
| 59 | |
| 60 | /*! Inspired by slices in golang. Storage is thread-safe reference counted, |
| 61 | allowing for cheap copies or range selection on the bytes. The bytes |
| 62 | owned by this class are always immutable. |
| 63 | |
| 64 | The functions `operator=`, `take_slice` and `remove_prefix` may alter the |
| 65 | reference count for the backing store, which will invalidate pointers |
| 66 | previously returned if the reference count is zero. Be careful about |
| 67 | "caching" pointers in these circumstances. */ |
| 68 | class byte_slice |
| 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_; |
| 74 | span<const std::uint8_t> portion_; // within storage_ |
| 75 | |
| 76 | //! Internal use only; use to increase `storage` reference count. |
| 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; |
| 92 | using iterator = pointer; |
| 93 | using const_iterator = const_pointer; |
| 94 | |
| 95 | //! Construct empty slice. |
| 96 | byte_slice() noexcept |
| 97 | : storage_(nullptr), portion_() |
| 98 | {} |
| 99 | |
| 100 | //! Construct empty slice |
| 101 | byte_slice(std::nullptr_t) noexcept |
| 102 | : byte_slice() |
| 103 | {} |
| 104 | |
| 105 | //! Scatter-gather (copy) multiple `sources` into a single allocated slice. |
| 106 | explicit byte_slice(std::initializer_list<span<const std::uint8_t>> sources); |
| 107 | |
| 108 | //! Convert `buffer` into a slice using one allocation for shared count. |
| 109 | explicit byte_slice(std::vector<std::uint8_t>&& buffer); |
| 110 | |
| 111 | //! Convert `buffer` into a slice using one allocation for shared count. |
| 112 | explicit byte_slice(std::string&& buffer); |
| 113 | |
| 114 | //! Convert `stream` into a slice with zero allocations. |
| 115 | explicit byte_slice(byte_stream&& stream, bool shrink = true); |
| 116 | |
| 117 | byte_slice(byte_slice&& source) noexcept; |
| 118 | ~byte_slice() noexcept = default; |
| 119 | |
| 120 | //! \note May invalidate previously retrieved pointers. |
| 121 | byte_slice& operator=(byte_slice&&) noexcept; |
| 122 | |
| 123 | //! \return A shallow (cheap) copy of the data from `this` slice. |
| 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 | |
| 136 | /*! Drop bytes from the beginning of `this` slice. |
| 137 | |
| 138 | \note May invalidate previously retrieved pointers. |
| 139 | \post `this->size() = this->size() - std::min(this->size(), max_bytes)` |
| 140 | \post `if (this->size() <= max_bytes) this->data() = nullptr` |
| 141 | \return Number of bytes removed. */ |
| 142 | std::size_t remove_prefix(std::size_t max_bytes) noexcept; |
| 143 | |
| 144 | /*! "Take" bytes from the beginning of `this` slice. |
| 145 | |
| 146 | \note May invalidate previously retrieved pointers. |
| 147 | \post `this->size() = this->size() - std::min(this->size(), max_bytes)` |
| 148 | \post `if (this->size() <= max_bytes) this->data() = nullptr` |
| 149 | \return Slice containing the bytes removed from `this` slice. */ |
| 150 | byte_slice take_slice(std::size_t max_bytes) noexcept; |
| 151 | |
| 152 | /*! Return a shallow (cheap) copy of a slice from `begin` and `end` offsets. |
| 153 | |
| 154 | \throw std::out_of_range If `end < begin`. |
| 155 | \throw std::out_of_range If `size() < end`. |
| 156 | \return Slice starting at `data() + begin` of size `end - begin`. */ |
| 157 | byte_slice get_slice(std::size_t begin, std::size_t end) const; |
| 158 | |
| 159 | //! \post `empty()` \return Ownership of ref-counted buffer. |
| 160 | std::unique_ptr<byte_slice_data, release_byte_slice> take_buffer() noexcept; |
| 161 | }; |
| 162 | |
| 163 | //! Alias for a buffer that has space for a `byte_slice` ref count. |
| 164 | using byte_buffer = std::unique_ptr<std::uint8_t, release_byte_buffer>; |
| 165 | |
| 166 | /*! \return `buf` with a new size of exactly `length`. New bytes not |
| 167 | initialized. A `nullptr` is returned on allocation failure. */ |
| 168 | byte_buffer byte_buffer_resize(byte_buffer buf, std::size_t length) noexcept; |
| 169 | |
| 170 | /*! Increase `buf` of size `current` by `more` bytes. |
| 171 | |
| 172 | \throw std::range_error if `current + more` exceeds `size_t` bounds. |
| 173 | \return Buffer of `current + more` bytes. A `nullptr` is returned on |
| 174 | allocation failure. */ |
| 175 | byte_buffer byte_buffer_increase(byte_buffer buf, std::size_t current, std::size_t more); |
| 176 | } // epee |
| 177 |