Debian Codegraph @

src/serialization/binary_archive.h

raw symbols SCIP
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
31 /*! \file binary_archive.h
32 *
33 * Portable (low-endian) binary archive */
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 */
47 PUSH_WARNINGS
48 DISABLE_VS_WARNINGS(4244)
49
50 //TODO: fix size_t warning in x32 platform
51
52 /*! \struct binary_archive_base
53 *
54 * \brief base for the binary archive type
55 *
56 * \detailed It isn't used outside of this file, which its only
57 * purpse is to define the functions used for the binary_archive. Its
58 * a header, basically. I think it was declared simply to save typing...
59 */
60 template <bool IsSaving>
61 struct binary_archive_base
62 {
63 typedef binary_archive_base<IsSaving> base_type;
64 typedef boost::mpl::bool_<IsSaving> is_saving;
65
66 typedef uint8_t variant_tag_type;
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>
89 struct binary_archive;
90
91
92 template <>
93 struct binary_archive<false> : public binary_archive_base<false>
94 {
95 explicit binary_archive(epee::span<const std::uint8_t> s)
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
102 //! If implementing as `std::istream`, reset stream error state after `peek()` call.
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
112 /*! \fn serialize_uint
113 *
114 * \brief serializes an unsigned integer
115 */
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>
138 void serialize_varint(T &v)
139 {
140 serialize_uvarint(*(typename boost::make_unsigned<T>::type *)(&v));
141 }
142
143 template <class T>
144 void serialize_uvarint(T &v)
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
165 void read_variant_tag(variant_tag_type &t) {
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:
173 epee::span<const std::uint8_t> bytes_;
174 std::uint8_t const* const begin_;
175 bool good_;
176 bool varint_bug_backward_compatibility_;
177 };
178
179 template <>
180 struct binary_archive<true> : public binary_archive_base<true>
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>
196 void serialize_uint(T v)
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>
210 void serialize_varint(T &v)
211 {
212 serialize_uvarint(*(typename boost::make_unsigned<T>::type *)(&v));
213 }
214
215 template <class T>
216 void serialize_uvarint(T &v)
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
232 void write_variant_tag(variant_tag_type t) {
233 serialize_int(t);
234 }
235
236 bool varint_bug_backward_compatibility_enabled() const { return false; }
237 protected:
238 stream_type& stream_;
239 };
240
241 POP_WARNINGS