Monero
buffer.h
Go to the documentation of this file.
1 // Copyright (c) 2018-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 <vector>
33 #include "misc_log_ex.h"
34 #include "span.h"
35 
36 #undef MONERO_DEFAULT_LOG_CATEGORY
37 #define MONERO_DEFAULT_LOG_CATEGORY "net.buffer"
38 
39 //#define NET_BUFFER_LOG(x) MDEBUG(x)
40 #define NET_BUFFER_LOG(x) ((void)0)
41 
42 namespace epee
43 {
44 namespace net_utils
45 {
46 class buffer
47 {
48 public:
49  buffer(size_t reserve = 0): offset(0) { storage.reserve(reserve); }
50 
51  void append(const void *data, size_t sz);
52  void erase(size_t sz) { NET_BUFFER_LOG("erasing " << sz << "/" << size()); CHECK_AND_ASSERT_THROW_MES(offset + sz <= storage.size(), "erase: sz too large"); offset += sz; if (offset == storage.size()) { storage.resize(0); offset = 0; } }
53  epee::span<const uint8_t> span(size_t sz) const { CHECK_AND_ASSERT_THROW_MES(sz <= size(), "span is too large"); return epee::span<const uint8_t>(storage.data() + offset, sz); }
54  // carve must keep the data in scope till next call, other API calls (such as append, erase) can invalidate the carved buffer
55  epee::span<const uint8_t> carve(size_t sz) { CHECK_AND_ASSERT_THROW_MES(sz <= size(), "span is too large"); offset += sz; return epee::span<const uint8_t>(storage.data() + offset - sz, sz); }
56  size_t size() const { return storage.size() - offset; }
57 
58 private:
59  std::vector<uint8_t> storage;
60  size_t offset;
61 };
62 }
63 }
void append(const void *data, size_t sz)
Definition: buffer.cpp:42
void reserve(const T &, std::size_t)
Definition: json_object.h:388
std::string data
Definition: base58.cpp:37
void erase(size_t sz)
Definition: buffer.h:52
Non-owning sequence of data. Does not deep copy.
Definition: span.h:54
#define NET_BUFFER_LOG(x)
Definition: buffer.h:40
size_t size() const
Definition: buffer.h:56
epee::span< const uint8_t > carve(size_t sz)
Definition: buffer.h:55
std::vector< uint8_t > storage
Definition: buffer.h:59
Definition: buffer.h:46
buffer(size_t reserve=0)
Definition: buffer.h:49
TODO: (mj-xmr) This will be reduced in an another PR.
Definition: byte_slice.h:39
size_t offset
Definition: buffer.h:60
epee::span< const uint8_t > span(size_t sz) const
Definition: buffer.h:53