Debian Codegraph @

contrib/epee/include/byte_stream.h

raw symbols SCIP
1 // Copyright (c) 2020-2022, The Monero Project
2
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #pragma once
31
32 #include <cassert>
33 #include <cstdint>
34 #include <cstring>
35
36 #include "byte_slice.h"
37 #include "span.h"
38
39 namespace epee
40 {
41 /*! \brief A partial drop-in replacement for `std::ostream`.
42
43 Only a few base `std::ostream` functions are implemented - enough for
44 rapidjson output currently.
45
46 Advantages over `std::stringstream` or `rapidjson::StringBuffer`:
47 - The internal buffer can be taken without a copy.
48 - The internal buffer can be given to `byte_slice` with zero
49 allocations for reference count.
50 - The internal buffer can be given to `zmq_msg_data_init` without a
51 copy or extra allocation.
52 an additional advantage over `std::stringstream`:
53 - Construction is significantly faster - the global `std::locale`
54 does not have to be acquired (global thread synchronization), and
55 an extra allocation for `std::stringbuf` is not needed (which is an
56 addition to the buffer inside of that object). */
57 class byte_stream
58 {
59 byte_buffer buffer_; //! Beginning of buffer
60 std::uint8_t* next_write_; //! Current write position
61 const std::uint8_t* end_; //! End of buffer
62
63 //! \post `requested <= available()`
64 void overflow(const std::size_t requested);
65
66 //! Ensures that at least `requested` bytes are available.
67 void check(const std::size_t requested)
68 {
69 const std::size_t remaining = available();
70 if (remaining < requested)
71 overflow(requested);
72 }
73
74 public:
75 using char_type = std::uint8_t;
76 using Ch = char_type;
77 using value_type = char_type;
78
79 //! Increase internal buffer by at least `byte_stream_increase` bytes.
80 byte_stream() noexcept
81 : buffer_(nullptr),
82 next_write_(nullptr),
83 end_(nullptr)
84 {}
85
86 byte_stream(byte_stream&& rhs) noexcept;
87 ~byte_stream() noexcept = default;
88 byte_stream& operator=(byte_stream&& rhs) noexcept;
89
90 std::uint8_t* data() noexcept { return buffer_.get(); }
91 const std::uint8_t* data() const noexcept { return buffer_.get(); }
92 std::uint8_t* tellp() const noexcept { return next_write_; }
93 std::size_t available() const noexcept { return end_ - next_write_; }
94 std::size_t size() const noexcept { return next_write_ - buffer_.get(); }
95 std::size_t capacity() const noexcept { return end_ - buffer_.get(); }
96
97 //! Compatibility with rapidjson.
98 void Flush() const noexcept
99 {}
100
101 /*! Reserve at least `more` bytes.
102 \post `size() + more <= available()`.
103 \throw std::range_error if exceeding max `size_t` value.
104 \throw std::bad_alloc if allocation fails. */
105 void reserve(const std::size_t more)
106 {
107 check(more);
108 }
109
110 //! Reset write position, but do not release internal memory. \post `size() == 0`.
111 void clear() noexcept { next_write_ = buffer_.get(); }
112
113 /*! Copy `length` bytes starting at `ptr` to end of stream.
114 \throw std::range_error If exceeding max size_t value.
115 \throw std::bad_alloc If allocation fails. */
116 void write(const std::uint8_t* ptr, const std::size_t length)
117 {
118 check(length);
119 std::memcpy(tellp(), ptr, length);
120 next_write_ += length;
121 }
122
123 /*! Copy `length` bytes starting at `ptr` to end of stream.
124 \throw std::range_error if exceeding max `size_t` value.
125 \throw std::bad_alloc if allocation fails. */
126 void write(const char* ptr, const std::size_t length)
127 {
128 write(reinterpret_cast<const std::uint8_t*>(ptr), length);
129 }
130
131 /*! Copy `source` to end of stream.
132 \throw std::range_error if exceeding max `size_t` value.
133 \throw std::bad_alloc if allocation fails. */
134 void write(const epee::span<const std::uint8_t> source)
135 {
136 write(source.data(), source.size());
137 }
138
139 /*! Copy `source` to end of stream.
140 \throw std::range_error if exceeding max `size_t` value.
141 \throw std::bad_alloc if allocation fails. */
142 void write(const epee::span<const char> source)
143 {
144 write(source.data(), source.size());
145 }
146
147 /*! Copy `ch` to end of stream.
148 \throw std::range_error if exceeding max `size_t` value.
149 \throw std::bad_alloc if allocation fails. */
150 void put(const std::uint8_t ch)
151 {
152 check(1);
153 put_unsafe(ch);
154 }
155
156 /*! Copy `ch` to end of stream. Provides rapidjson compatability.
157 \throw std::range_error if exceeding max `size_t` value.
158 \throw std::bad_alloc if allocation fails. */
159 void Put(const std::uint8_t ch)
160 {
161 put(ch);
162 }
163
164 /*! Writes `ch` to end of stream without runtime capacity checks. Must use
165 `reserve` before calling this function. Primarily for use with
166 rapidjson, which writes characters at a time but reserves memory in
167 blocks. Most applications want to use `put` or `write`. */
168 void put_unsafe(const std::uint8_t ch) noexcept
169 {
170 assert(1 <= available());
171 *(tellp()) = ch;
172 ++next_write_;
173 }
174
175 /*! Write `ch` to end of stream `count` times.
176 \throw std::range_error if exceeding max `size_t` value.
177 \throw std::bad_alloc if allocation fails. */
178 void put_n(const std::uint8_t ch, const std::size_t count)
179 {
180 check(count);
181 std::memset(tellp(), ch, count);
182 next_write_ += count;
183 }
184
185 /*! Copy `ch` to end of stream.
186 \throw std::range_error if exceeding max `size_t` value.
187 \throw std::bad_alloc if allocation fails. */
188 void push_back(const std::uint8_t ch)
189 {
190 put(ch);
191 }
192
193 //! \return The internal buffer. \post `size() == capacity() == 0`.
194 byte_buffer take_buffer() noexcept;
195 };
196
197 //! Compatability/optimization for rapidjson.
198
199 inline void PutReserve(byte_stream& dest, const std::size_t length)
200 {
201 dest.reserve(length);
202 }
203
204 //! Compatability/optimization for rapidjson.
205
206 inline void PutUnsafe(byte_stream& dest, const std::uint8_t ch)
207 {
208 dest.put_unsafe(ch);
209 }
210
211 //! Compability/optimization for rapidjson.
212 inline void PutN(byte_stream& dest, const std::uint8_t ch, const std::size_t count)
213 {
214 dest.put_n(ch, count);
215 }
216 } // epee
217