Monero
Loading...
Searching...
No Matches
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
60template <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 */
88template <bool W>
90
91
92template <>
94{
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>
108 {
109 serialize_uint(*(typename boost::make_unsigned<T>::type *)&v);
110 }
111
116 template <class T>
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 {
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
168
169 size_t remaining_bytes() const noexcept { return good() ? bytes_.size() : 0; }
172protected:
174 std::uint8_t const* const begin_;
175 bool good_;
177};
178
179template <>
181{
182 typedef std::ostream stream_type;
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>
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;
220 }
221 void begin_array(size_t s)
222 {
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
235
236 bool varint_bug_backward_compatibility_enabled() const { return false; }
237protected:
239};
240
#define s(x, c)
Definition aesb.c:47
Non-owning sequence of data. Does not deep copy.
Definition span.h:55
#define true
#define false
#define const
Definition ipfrdr.c:80
std::enable_if< std::is_integral< T >::value &&std::is_unsigned< T >::value &&0<=bits &&bits<=std::numeric_limits< T >::digits, int >::type read_varint(InputIt &&first, InputIt &&last, T &write)
reads in the varint that is pointed to by InputIt into write
Definition varint.h:94
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
const char * buf
Definition slow_memmem.cpp:73
unsigned char uint8_t
Definition stdint.h:124
bool good() const noexcept
Definition binary_archive.h:99
void begin_array(size_t &s)
Definition binary_archive.h:153
void end_string(const char *delimiter)
Definition binary_archive.h:163
void enable_varint_bug_backward_compatibility()
Definition binary_archive.h:170
bool eof() const noexcept
If implementing as std::istream, reset stream error state after peek() call.
Definition binary_archive.h:103
binary_archive(epee::span< const std::uint8_t > s)
Definition binary_archive.h:95
epee::span< const std::uint8_t > bytes_
Definition binary_archive.h:173
void delimit_array()
Definition binary_archive.h:159
void serialize_varint(T &v)
Definition binary_archive.h:138
void serialize_blob(void *buf, size_t len, const char *delimiter="")
Definition binary_archive.h:130
size_t remaining_bytes() const noexcept
Definition binary_archive.h:169
void read_variant_tag(variant_tag_type &t)
Definition binary_archive.h:165
void serialize_uvarint(T &v)
Definition binary_archive.h:144
bool good_
Definition binary_archive.h:175
void serialize_int(T &v)
Definition binary_archive.h:107
void end_array()
Definition binary_archive.h:160
bool varint_bug_backward_compatibility_
Definition binary_archive.h:176
void begin_string(const char *delimiter)
Definition binary_archive.h:162
void begin_array()
Definition binary_archive.h:158
std::size_t getpos() const noexcept
Definition binary_archive.h:104
void serialize_uint(T &v)
serializes an unsigned integer
Definition binary_archive.h:117
std::uint8_t const *const begin_
Definition binary_archive.h:174
bool varint_bug_backward_compatibility_enabled() const
Definition binary_archive.h:171
void set_fail() noexcept
Definition binary_archive.h:100
std::streampos getpos() const
Definition binary_archive.h:188
bool varint_bug_backward_compatibility_enabled() const
Definition binary_archive.h:236
void end_string(const char *delimiter="\"")
Definition binary_archive.h:230
void serialize_blob(void *buf, size_t len, const char *delimiter="")
Definition binary_archive.h:204
void serialize_uvarint(T &v)
Definition binary_archive.h:216
stream_type & stream_
Definition binary_archive.h:238
void serialize_int(T v)
Definition binary_archive.h:191
binary_archive(stream_type &s)
Definition binary_archive.h:183
void begin_string(const char *delimiter="\"")
Definition binary_archive.h:229
void write_variant_tag(variant_tag_type t)
Definition binary_archive.h:232
void delimit_array()
Definition binary_archive.h:226
void end_array()
Definition binary_archive.h:227
std::ostream stream_type
Definition binary_archive.h:182
bool good() const
Definition binary_archive.h:185
void serialize_varint(T &v)
Definition binary_archive.h:210
void begin_array(size_t s)
Definition binary_archive.h:221
void serialize_uint(T v)
Definition binary_archive.h:196
void begin_array()
Definition binary_archive.h:225
void set_fail()
Definition binary_archive.h:186
uint8_t variant_tag_type
Definition binary_archive.h:66
void begin_object()
Definition binary_archive.h:72
boost::mpl::bool_< IsSaving > is_saving
Definition binary_archive.h:64
binary_archive_base< IsSaving > base_type
Definition binary_archive.h:63
binary_archive_base()
Definition binary_archive.h:68
void end_object()
Definition binary_archive.h:73
void end_variant()
Definition binary_archive.h:75
void begin_variant()
Definition binary_archive.h:74
void tag(const char *)
Definition binary_archive.h:71
Definition binary_archive.h:89
provides the implementation of varint's
#define DISABLE_VS_WARNINGS(w)
Definition warnings.h:18
#define POP_WARNINGS
Definition warnings.h:17
#define PUSH_WARNINGS
Definition warnings.h:16
#define T(x)