Monero
binary_archive.h
Go to the documentation of this file.
1 // Copyright (c) 2014-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 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30 
34 #pragma once
35 
36 #include <cassert>
37 #include <iostream>
38 #include <iterator>
39 #include <boost/endian/conversion.hpp>
40 #include <boost/type_traits/make_unsigned.hpp>
41 
42 #include "common/varint.h"
43 #include "span.h"
44 #include "warnings.h"
45 
46 /* I have no clue what these lines means */
49 
50 //TODO: fix size_t warning in x32 platform
51 
52 
60 template <bool IsSaving>
62 {
64  typedef boost::mpl::bool_<IsSaving> is_saving;
65 
67 
68  explicit binary_archive_base() { }
69 
70  /* definition of standard API functions */
71  void tag(const char *) { }
72  void begin_object() { }
73  void end_object() { }
74  void begin_variant() { }
75  void end_variant() { }
76 };
77 
78 /* \struct binary_archive
79  *
80  * \brief the actually binary archive type
81  *
82  * \detailed The boolean template argument /a W is the is_saving
83  * parameter for binary_archive_base.
84  *
85  * The is_saving parameter says whether the archive is being read from
86  * (false) or written to (true)
87  */
88 template <bool W>
90 
91 
92 template <>
93 struct binary_archive<false> : public binary_archive_base<false>
94 {
96  : base_type(), bytes_(s), begin_(s.begin()), good_(true), varint_bug_backward_compatibility_(false)
97  {}
98 
99  bool good() const noexcept { return good_; }
100  void set_fail() noexcept { good_ = false; }
101 
103  bool eof() const noexcept { return bytes_.empty(); }
104  std::size_t getpos() const noexcept { return bytes_.begin() - begin_; }
105 
106  template <class T>
107  void serialize_int(T &v)
108  {
109  serialize_uint(*(typename boost::make_unsigned<T>::type *)&v);
110  }
111 
116  template <class T>
117  void serialize_uint(T &v)
118  {
119  const std::size_t actual = bytes_.remove_prefix(sizeof(T));
120  good_ &= (actual == sizeof(T));
121  if (actual == sizeof(T))
122  {
123  std::memcpy(std::addressof(v), bytes_.data() - sizeof(T), sizeof(T));
124  boost::endian::little_to_native_inplace(v); // epee isn't templated
125  }
126  else
127  v = 0; // ensures initialization
128  }
129 
130  void serialize_blob(void *buf, size_t len, const char *delimiter="")
131  {
132  const std::size_t actual = bytes_.remove_prefix(len);
133  good_ &= (len == actual);
134  std::memcpy(buf, bytes_.data() - actual, actual);
135  }
136 
137  template <class T>
139  {
140  serialize_uvarint(*(typename boost::make_unsigned<T>::type *)(&v));
141  }
142 
143  template <class T>
145  {
146  auto current = bytes_.cbegin();
147  auto end = bytes_.cend();
148  good_ &= (0 <= tools::read_varint(current, end, v));
149  current = std::min(current, bytes_.cend());
150  bytes_ = {current, std::size_t(bytes_.cend() - current)};
151  }
152 
153  void begin_array(size_t &s)
154  {
155  serialize_varint(s);
156  }
157 
158  void begin_array() { }
159  void delimit_array() { }
160  void end_array() { }
161 
162  void begin_string(const char *delimiter /*="\""*/) { }
163  void end_string(const char *delimiter /*="\""*/) { }
164 
166  serialize_int(t);
167  }
168 
169  size_t remaining_bytes() const noexcept { return good() ? bytes_.size() : 0; }
170  void enable_varint_bug_backward_compatibility() { varint_bug_backward_compatibility_ = true; }
171  bool varint_bug_backward_compatibility_enabled() const { return varint_bug_backward_compatibility_; }
172 protected:
174  std::uint8_t const* const begin_;
175  bool good_;
177 };
178 
179 template <>
181 {
182  typedef std::ostream stream_type;
183  explicit binary_archive(stream_type &s) : base_type(), stream_(s) { }
184 
185  bool good() const { return stream_.good(); }
186  void set_fail() { stream_.setstate(std::ios::failbit); }
187 
188  std::streampos getpos() const { return stream_.tellp(); }
189 
190  template <class T>
191  void serialize_int(T v)
192  {
193  serialize_uint(static_cast<typename boost::make_unsigned<T>::type>(v));
194  }
195  template <class T>
197  {
198  for (size_t i = 0; i < sizeof(T); i++) {
199  stream_.put((char)(v & 0xff));
200  if (1 < sizeof(T)) v >>= 8;
201  }
202  }
203 
204  void serialize_blob(void *buf, size_t len, const char *delimiter="")
205  {
206  stream_.write((char *)buf, len);
207  }
208 
209  template <class T>
211  {
212  serialize_uvarint(*(typename boost::make_unsigned<T>::type *)(&v));
213  }
214 
215  template <class T>
217  {
218  typedef std::ostreambuf_iterator<char> it;
219  tools::write_varint(it(stream_), v);
220  }
221  void begin_array(size_t s)
222  {
223  serialize_varint(s);
224  }
225  void begin_array() { }
226  void delimit_array() { }
227  void end_array() { }
228 
229  void begin_string(const char *delimiter="\"") { }
230  void end_string(const char *delimiter="\"") { }
231 
233  serialize_int(t);
234  }
235 
236  bool varint_bug_backward_compatibility_enabled() const { return false; }
237 protected:
239 };
240 
uint8_t variant_tag_type
Definition: binary_archive.h:66
bool eof() const noexcept
If implementing as std::istream, reset stream error state after peek() call.
Definition: binary_archive.h:103
void write_variant_tag(variant_tag_type t)
Definition: binary_archive.h:232
void end_array()
Definition: binary_archive.h:160
void serialize_varint(T &v)
Definition: binary_archive.h:138
void serialize_uvarint(T &v)
Definition: binary_archive.h:144
const uint32_t T[512]
Definition: groestl_tables.h:36
PUSH_WARNINGS
Definition: hash-ops.h:53
bool good_
Definition: binary_archive.h:175
void serialize_blob(void *buf, size_t len, const char *delimiter="")
Definition: binary_archive.h:204
void read_variant_tag(variant_tag_type &t)
Definition: binary_archive.h:165
int i
Definition: pymoduletest.py:23
void serialize_int(T v)
Definition: binary_archive.h:191
void begin_object()
Definition: binary_archive.h:72
binary_archive(stream_type &s)
Definition: binary_archive.h:183
std::ostream stream_type
Definition: binary_archive.h:182
t
Definition: console.py:33
void serialize_blob(void *buf, size_t len, const char *delimiter="")
Definition: binary_archive.h:130
void begin_array(size_t &s)
Definition: binary_archive.h:153
void serialize_uint(T v)
Definition: binary_archive.h:196
provides the implementation of varint&#39;s
void tag(const char *)
Definition: binary_archive.h:71
int type
Definition: superscalar.cpp:50
const char * s
Definition: minissdp.c:596
binary_archive(epee::span< const std::uint8_t > s)
Definition: binary_archive.h:95
void set_fail() noexcept
Definition: binary_archive.h:100
unsigned char uint8_t
Definition: stdint.h:124
void begin_array()
Definition: binary_archive.h:158
void end_object()
Definition: binary_archive.h:73
void begin_string(const char *delimiter="\)
Definition: binary_archive.h:229
void end_string(const char *delimiter)
Definition: binary_archive.h:163
void serialize_uvarint(T &v)
Definition: binary_archive.h:216
void end_variant()
Definition: binary_archive.h:75
void serialize_varint(T &v)
Definition: binary_archive.h:210
#define POP_WARNINGS
Definition: warnings.h:17
bool varint_bug_backward_compatibility_enabled() const
Definition: binary_archive.h:236
epee::span< const std::uint8_t > bytes_
Definition: binary_archive.h:173
void set_fail()
Definition: binary_archive.h:186
boost::mpl::bool_< IsSaving > is_saving
Definition: binary_archive.h:64
binary_archive_base()
Definition: binary_archive.h:68
std::size_t getpos() const noexcept
Definition: binary_archive.h:104
#define min(a, b)
Definition: oaes_lib.c:78
bool varint_bug_backward_compatibility_
Definition: binary_archive.h:176
#define false
Definition: stdbool.h:37
void begin_array(size_t s)
Definition: binary_archive.h:221
const char * buf
Definition: slow_memmem.cpp:73
base for the binary archive type
Definition: binary_archive.h:61
stream_type & stream_
Definition: binary_archive.h:238
DISABLE_VS_WARNINGS(4244 4345 4503) using namespace crypto
void end_array()
Definition: binary_archive.h:227
void * memcpy(void *a, const void *b, size_t c)
Definition: glibc_compat.cpp:16
void serialize_uint(T &v)
serializes an unsigned integer
Definition: binary_archive.h:117
void end_string(const char *delimiter="\)
Definition: binary_archive.h:230
size_t remaining_bytes() const noexcept
Definition: binary_archive.h:169
void begin_variant()
Definition: binary_archive.h:74
void delimit_array()
Definition: binary_archive.h:226
void delimit_array()
Definition: binary_archive.h:159
bool good() const
Definition: binary_archive.h:185
void begin_array()
Definition: binary_archive.h:225
bool varint_bug_backward_compatibility_enabled() const
Definition: binary_archive.h:171
std::uint8_t const *const begin_
Definition: binary_archive.h:174
Definition: binary_archive.h:89
#define const
Definition: ipfrdr.c:80
std::streampos getpos() const
Definition: binary_archive.h:188
#define true
Definition: stdbool.h:36
bool good() const noexcept
Definition: binary_archive.h:99
void serialize_int(T &v)
Definition: binary_archive.h:107
std::enable_if< std::is_integral< T >::value &&std::is_unsigned< T >::value, void >::type write_varint(OutputIt &&dest, T i)
writes a varint to a stream.
Definition: varint.h:69
binary_archive_base< IsSaving > base_type
Definition: binary_archive.h:63
void begin_string(const char *delimiter)
Definition: binary_archive.h:162
void enable_varint_bug_backward_compatibility()
Definition: binary_archive.h:170