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;
77 
79  byte_stream() noexcept
80  : buffer_(nullptr),
81  next_write_(nullptr),
82  end_(nullptr)
83  {}
84 
85  byte_stream(byte_stream&& rhs) noexcept;
86  ~byte_stream() noexcept = default;
87  byte_stream& operator=(byte_stream&& rhs) noexcept;
88 
89  const std::uint8_t* data() const noexcept { return buffer_.get(); }
90  std::uint8_t* tellp() const noexcept { return next_write_; }
91  std::size_t available() const noexcept { return end_ - next_write_; }
92  std::size_t size() const noexcept { return next_write_ - buffer_.get(); }
93  std::size_t capacity() const noexcept { return end_ - buffer_.get(); }
94 
96  void Flush() const noexcept
97  {}
98 
103  void reserve(const std::size_t more)
104  {
105  check(more);
106  }
107 
109  void clear() noexcept { next_write_ = buffer_.get(); }
110 
114  void write(const std::uint8_t* ptr, const std::size_t length)
115  {
116  check(length);
117  std::memcpy(tellp(), ptr, length);
118  next_write_ += length;
119  }
120 
124  void write(const char* ptr, const std::size_t length)
125  {
126  write(reinterpret_cast<const std::uint8_t*>(ptr), length);
127  }
128 
133  {
134  write(source.data(), source.size());
135  }
136 
141  {
142  write(source.data(), source.size());
143  }
144 
148  void put(const std::uint8_t ch)
149  {
150  check(1);
151  put_unsafe(ch);
152  }
153 
157  void Put(const std::uint8_t ch)
158  {
159  put(ch);
160  }
161 
166  void put_unsafe(const std::uint8_t ch) noexcept
167  {
168  assert(1 <= available());
169  *(tellp()) = ch;
170  ++next_write_;
171  }
172 
176  void put_n(const std::uint8_t ch, const std::size_t count)
177  {
178  check(count);
179  std::memset(tellp(), ch, count);
180  next_write_ += count;
181  }
182 
186  void push_back(const std::uint8_t ch)
187  {
188  put(ch);
189  }
190 
192  byte_buffer take_buffer() noexcept;
193  };
194 
196 
197  inline void PutReserve(byte_stream& dest, const std::size_t length)
198  {
199  dest.reserve(length);
200  }
201 
203 
204  inline void PutUnsafe(byte_stream& dest, const std::uint8_t ch)
205  {
206  dest.put_unsafe(ch);
207  }
208 
210  inline void PutN(byte_stream& dest, const std::uint8_t ch, const std::size_t count)
211  {
212  dest.put_n(ch, count);
213  }
214 } // epee
215 
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:197
int * count
Definition: gmock_stress_test.cc:176
void write(const epee::span< const char > source)
Definition: byte_stream.h:140
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:204
~byte_stream() noexcept=default
void put_unsafe(const std::uint8_t ch) noexcept
Definition: byte_stream.h:166
void put_n(const std::uint8_t ch, const std::size_t count)
Definition: byte_stream.h:176
CXA_THROW_INFO_T void(* dest)(void *))
Definition: stack_trace.cpp:90
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:210
unsigned char uint8_t
Definition: stdint.h:124
void Flush() const noexcept
Compatibility with rapidjson.
Definition: byte_stream.h:96
void put(const std::uint8_t ch)
Definition: byte_stream.h:148
const std::uint8_t * data() const noexcept
Definition: byte_stream.h:89
default
Definition: pymoduletest.py:17
const std::uint8_t * end_
Current write position.
Definition: byte_stream.h:61
std::size_t available() const noexcept
Definition: byte_stream.h:91
std::size_t capacity() const noexcept
Definition: byte_stream.h:93
void clear() noexcept
Reset write position, but do not release internal memory.
Definition: byte_stream.h:109
void Put(const std::uint8_t ch)
Definition: byte_stream.h:157
void write(const char *ptr, const std::size_t length)
Definition: byte_stream.h:124
std::size_t size() const noexcept
Definition: byte_stream.h:92
std::uint8_t * tellp() const noexcept
Definition: byte_stream.h:90
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:114
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:79
void reserve(const std::size_t more)
Definition: byte_stream.h:103
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:186
void write(const epee::span< const std::uint8_t > source)
Definition: byte_stream.h:132
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