Electroneum
binary_archive.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2019, The Electroneum Project
2 // Copyrights(c) 2014-2017, The Monero Project
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 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31 
35 #pragma once
36 
37 #include <cassert>
38 #include <iostream>
39 #include <iterator>
40 #include <boost/type_traits/make_unsigned.hpp>
41 
42 #include "common/varint.h"
43 #include "warnings.h"
44 
45 /* I have no clue what these lines means */
48 
49 //TODO: fix size_t warning in x32 platform
50 
51 
59 template <class Stream, bool IsSaving>
61 {
62  typedef Stream stream_type;
64  typedef boost::mpl::bool_<IsSaving> is_saving;
65 
66  typedef uint8_t variant_tag_type;
67 
68  explicit binary_archive_base(stream_type &s) : stream_(s) { }
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  /* I just want to leave a comment saying how this line really shows
77  flaws in the ownership model of many OOP languages, that is all. */
78  stream_type &stream() { return stream_; }
79 
80 protected:
81  stream_type &stream_;
82 };
83 
84 /* \struct binary_archive
85  *
86  * \brief the actualy binary archive type
87  *
88  * \detailed The boolean template argument /a W is the is_saving
89  * parameter for binary_archive_base.
90  *
91  * The is_saving parameter says whether the archive is being read from
92  * (false) or written to (true)
93  */
94 template <bool W>
96 
97 
98 template <>
99 struct binary_archive<false> : public binary_archive_base<std::istream, false>
100 {
101 
103  stream_type::streampos pos = stream_.tellg();
104  stream_.seekg(0, std::ios_base::end);
105  eof_pos_ = stream_.tellg();
106  stream_.seekg(pos);
107  }
108 
109  template <class T>
110  void serialize_int(T &v)
111  {
112  serialize_uint(*(typename boost::make_unsigned<T>::type *)&v);
113  }
114 
119  template <class T>
120  void serialize_uint(T &v, size_t width = sizeof(T))
121  {
122  T ret = 0;
123  unsigned shift = 0;
124  for (size_t i = 0; i < width; i++) {
125  //std::cerr << "tell: " << stream_.tellg() << " value: " << ret << std::endl;
126  char c;
127  stream_.get(c);
128  T b = (unsigned char)c;
129  ret += (b << shift); // can this be changed to OR, i think it can.
130  shift += 8;
131  }
132  v = ret;
133  }
134 
135  void serialize_blob(void *buf, size_t len, const char *delimiter="")
136  {
137  stream_.read((char *)buf, len);
138  }
139 
140  template <class T>
142  {
143  serialize_uvarint(*(typename boost::make_unsigned<T>::type *)(&v));
144  }
145 
146  template <class T>
148  {
149  typedef std::istreambuf_iterator<char> it;
150  if (tools::read_varint(it(stream_), it(), v) < 0)
151  stream_.setstate(std::ios_base::failbit);
152  }
153 
154  void begin_array(size_t &s)
155  {
156  serialize_varint(s);
157  }
158 
159  void begin_array() { }
160  void delimit_array() { }
161  void end_array() { }
162 
163  void begin_string(const char *delimiter /*="\""*/) { }
164  void end_string(const char *delimiter /*="\""*/) { }
165 
167  serialize_int(t);
168  }
169 
170  size_t remaining_bytes() {
171  if (!stream_.good())
172  return 0;
173  //std::cerr << "tell: " << stream_.tellg() << std::endl;
174  assert(stream_.tellg() <= eof_pos_);
175  return eof_pos_ - stream_.tellg();
176  }
177 protected:
178  std::streamoff eof_pos_;
179 };
180 
181 template <>
182 struct binary_archive<true> : public binary_archive_base<std::ostream, true>
183 {
185 
186  template <class T>
187  void serialize_int(T v)
188  {
189  serialize_uint(static_cast<typename boost::make_unsigned<T>::type>(v));
190  }
191  template <class T>
193  {
194  for (size_t i = 0; i < sizeof(T); i++) {
195  stream_.put((char)(v & 0xff));
196  if (1 < sizeof(T)) v >>= 8;
197  }
198  }
199 
200  void serialize_blob(void *buf, size_t len, const char *delimiter="")
201  {
202  stream_.write((char *)buf, len);
203  }
204 
205  template <class T>
207  {
208  serialize_uvarint(*(typename boost::make_unsigned<T>::type *)(&v));
209  }
210 
211  template <class T>
213  {
214  typedef std::ostreambuf_iterator<char> it;
215  tools::write_varint(it(stream_), v);
216  }
217  void begin_array(size_t s)
218  {
219  serialize_varint(s);
220  }
221  void begin_array() { }
222  void delimit_array() { }
223  void end_array() { }
224 
225  void begin_string(const char *delimiter="\"") { }
226  void end_string(const char *delimiter="\"") { }
227 
229  serialize_int(t);
230  }
231 };
232 
233 POP_WARNINGS
void write_variant_tag(variant_tag_type t)
Definition: binary_archive.h:228
void end_array()
Definition: binary_archive.h:161
void serialize_varint(T &v)
Definition: binary_archive.h:141
void serialize_uvarint(T &v)
Definition: binary_archive.h:147
binary_archive(stream_type &s)
Definition: binary_archive.h:102
const uint32_t T[512]
Definition: groestl_tables.h:34
PUSH_WARNINGS
Definition: hash-ops.h:54
uint8_t variant_tag_type
Definition: binary_archive.h:66
void serialize_blob(void *buf, size_t len, const char *delimiter="")
Definition: binary_archive.h:200
void read_variant_tag(variant_tag_type &t)
Definition: binary_archive.h:166
stream_type & stream()
Definition: binary_archive.h:78
binary_archive_base< Stream, IsSaving > base_type
Definition: binary_archive.h:63
void serialize_int(T v)
Definition: binary_archive.h:187
binary_archive(stream_type &s)
Definition: binary_archive.h:184
boost::mpl::bool_< IsSaving > is_saving
Definition: binary_archive.h:64
void tag(const char *)
Definition: binary_archive.h:71
void serialize_blob(void *buf, size_t len, const char *delimiter="")
Definition: binary_archive.h:135
void begin_array(size_t &s)
Definition: binary_archive.h:154
void serialize_uint(T v)
Definition: binary_archive.h:192
provides the implementation of varint&#39;s
stream_type & stream_
Definition: binary_archive.h:81
void begin_array()
Definition: binary_archive.h:159
void begin_string(const char *delimiter="\)
Definition: binary_archive.h:225
void begin_variant()
Definition: binary_archive.h:74
void end_string(const char *delimiter)
Definition: binary_archive.h:164
void begin_object()
Definition: binary_archive.h:72
void serialize_uvarint(T &v)
Definition: binary_archive.h:212
void serialize_varint(T &v)
Definition: binary_archive.h:206
void end_object()
Definition: binary_archive.h:73
std::streamoff eof_pos_
Definition: binary_archive.h:178
void end_variant()
Definition: binary_archive.h:75
DISABLE_VS_WARNINGS(4244 4345 4503) namespace cryptonote
Definition: tx_pool.cpp:53
int b
Definition: base.py:1
type
Definition: json.h:74
#define false
Definition: stdbool.h:38
void begin_array(size_t s)
Definition: binary_archive.h:217
base for the binary archive type
Definition: binary_archive.h:60
void end_array()
Definition: binary_archive.h:223
void end_string(const char *delimiter="\)
Definition: binary_archive.h:226
binary_archive_base(stream_type &s)
Definition: binary_archive.h:68
size_t remaining_bytes()
Definition: binary_archive.h:170
void delimit_array()
Definition: binary_archive.h:222
void delimit_array()
Definition: binary_archive.h:160
void serialize_uint(T &v, size_t width=sizeof(T))
serializes an unsigned integer
Definition: binary_archive.h:120
void begin_array()
Definition: binary_archive.h:221
Stream stream_type
Definition: binary_archive.h:62
Definition: binary_archive.h:95
#define true
Definition: stdbool.h:37
#define s(x, c)
Definition: aesb.c:46
void serialize_int(T &v)
Definition: binary_archive.h:110
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:70
void begin_string(const char *delimiter)
Definition: binary_archive.h:163