Monero
byte_stream.h
Go to the documentation of this file.
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 {
58  {
61  const std::uint8_t* end_;
62 
64  void overflow(const std::size_t requested);
65 
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:
76  using Ch = char_type;
78 
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 
98  void Flush() const noexcept
99  {}
100 
105  void reserve(const std::size_t more)
106  {
107  check(more);
108  }
109 
111  void clear() noexcept { next_write_ = buffer_.get(); }
112 
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 
126  void write(const char* ptr, const std::size_t length)
127  {
128  write(reinterpret_cast<const std::uint8_t*>(ptr), length);
129  }
130 
135  {
136  write(source.data(), source.size());
137  }
138 
143  {
144  write(source.data(), source.size());
145  }
146 
150  void put(const std::uint8_t ch)
151  {
152  check(1);
153  put_unsafe(ch);
154  }
155 
159  void Put(const std::uint8_t ch)
160  {
161  put(ch);
162  }
163 
168  void put_unsafe(const std::uint8_t ch) noexcept
169  {
170  assert(1 <= available());
171  *(tellp()) = ch;
172  ++next_write_;
173  }
174 
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 
188  void push_back(const std::uint8_t ch)
189  {
190  put(ch);
191  }
192 
194  byte_buffer take_buffer() noexcept;
195  };
196 
198 
199  inline void PutReserve(byte_stream& dest, const std::size_t length)
200  {
201  dest.reserve(length);
202  }
203 
205 
206  inline void PutUnsafe(byte_stream& dest, const std::uint8_t ch)
207  {
208  dest.put_unsafe(ch);
209  }
210 
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 
const CharType(& source)[N]
Definition: pointer.h:1147
void PutReserve(byte_stream &dest, const std::size_t length)
Compatability/optimization for rapidjson.
Definition: byte_stream.h:199
int * count
Definition: gmock_stress_test.cc:176
void write(const epee::span< const char > source)
Definition: byte_stream.h:142
byte_buffer buffer_
Definition: byte_stream.h:59
void PutUnsafe(byte_stream &dest, const std::uint8_t ch)
Compatability/optimization for rapidjson.
Definition: byte_stream.h:206
~byte_stream() noexcept=default
void put_unsafe(const std::uint8_t ch) noexcept
Definition: byte_stream.h:168
void put_n(const std::uint8_t ch, const std::size_t count)
Definition: byte_stream.h:178
CXA_THROW_INFO_T void(* dest)(void *))
Definition: stack_trace.cpp:91
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
A partial drop-in replacement for std::ostream.
Definition: byte_stream.h:57
Definition: enums.h:67
void PutN(byte_stream &dest, const std::uint8_t ch, const std::size_t count)
Compability/optimization for rapidjson.
Definition: byte_stream.h:212
unsigned char uint8_t
Definition: stdint.h:124
void Flush() const noexcept
Compatibility with rapidjson.
Definition: byte_stream.h:98
void put(const std::uint8_t ch)
Definition: byte_stream.h:150
const std::uint8_t * data() const noexcept
Definition: byte_stream.h:91
default
Definition: pymoduletest.py:17
const std::uint8_t * end_
Current write position.
Definition: byte_stream.h:61
std::uint8_t * data() noexcept
Definition: byte_stream.h:90
std::size_t available() const noexcept
Definition: byte_stream.h:93
std::size_t capacity() const noexcept
Definition: byte_stream.h:95
void clear() noexcept
Reset write position, but do not release internal memory.
Definition: byte_stream.h:111
void Put(const std::uint8_t ch)
Definition: byte_stream.h:159
char_type value_type
Definition: byte_stream.h:77
void write(const char *ptr, const std::size_t length)
Definition: byte_stream.h:126
std::size_t size() const noexcept
Definition: byte_stream.h:94
std::uint8_t * tellp() const noexcept
Definition: byte_stream.h:92
char_type Ch
Definition: byte_stream.h:76
void check(const std::size_t requested)
Ensures that at least requested bytes are available.
Definition: byte_stream.h:67
void write(const std::uint8_t *ptr, const std::size_t length)
Definition: byte_stream.h:116
void overflow(const std::size_t requested)
End of buffer.
Definition: byte_stream.cpp:45
TODO: (mj-xmr) This will be reduced in an another PR.
Definition: byte_slice.h:39
byte_stream() noexcept
Increase internal buffer by at least byte_stream_increase bytes.
Definition: byte_stream.h:80
void reserve(const std::size_t more)
Definition: byte_stream.h:105
void * memcpy(void *a, const void *b, size_t c)
Definition: glibc_compat.cpp:16
byte_buffer take_buffer() noexcept
Definition: byte_stream.cpp:90
void push_back(const std::uint8_t ch)
Definition: byte_stream.h:188
void write(const epee::span< const std::uint8_t > source)
Definition: byte_stream.h:134
std::uint8_t * next_write_
Beginning of buffer.
Definition: byte_stream.h:60
#define const
Definition: ipfrdr.c:80
std::uint8_t char_type
Definition: byte_stream.h:75
#define inline
Definition: inline_c.h:34