Electroneum
binary_archive.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2018, 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  tools::read_varint(it(stream_), it(), v); // XXX handle failure
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() {
170  if (!stream_.good())
171  return 0;
172  //std::cerr << "tell: " << stream_.tellg() << std::endl;
173  assert(stream_.tellg() <= eof_pos_);
174  return eof_pos_ - stream_.tellg();
175  }
176 protected:
177  std::streamoff eof_pos_;
178 };
179 
180 template <>
181 struct binary_archive<true> : public binary_archive_base<std::ostream, true>
182 {
184 
185  template <class T>
186  void serialize_int(T v)
187  {
188  serialize_uint(static_cast<typename boost::make_unsigned<T>::type>(v));
189  }
190  template <class T>
192  {
193  for (size_t i = 0; i < sizeof(T); i++) {
194  stream_.put((char)(v & 0xff));
195  if (1 < sizeof(T)) v >>= 8;
196  }
197  }
198 
199  void serialize_blob(void *buf, size_t len, const char *delimiter="")
200  {
201  stream_.write((char *)buf, len);
202  }
203 
204  template <class T>
206  {
207  serialize_uvarint(*(typename boost::make_unsigned<T>::type *)(&v));
208  }
209 
210  template <class T>
212  {
213  typedef std::ostreambuf_iterator<char> it;
214  tools::write_varint(it(stream_), v);
215  }
216  void begin_array(size_t s)
217  {
218  serialize_varint(s);
219  }
220  void begin_array() { }
221  void delimit_array() { }
222  void end_array() { }
223 
224  void begin_string(const char *delimiter="\"") { }
225  void end_string(const char *delimiter="\"") { }
226 
228  serialize_int(t);
229  }
230 };
231 
232 POP_WARNINGS
void write_variant_tag(variant_tag_type t)
Definition: binary_archive.h:227
void end_array()
Definition: binary_archive.h:160
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:199
void read_variant_tag(variant_tag_type &t)
Definition: binary_archive.h:165
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:186
binary_archive(stream_type &s)
Definition: binary_archive.h:183
void end_string(const char *delimiter="\"")
Definition: binary_archive.h:225
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:153
void serialize_uint(T v)
Definition: binary_archive.h:191
provides the implementation of varint's
void begin_string(const char *delimiter="\"")
Definition: binary_archive.h:224
stream_type & stream_
Definition: binary_archive.h:81
void begin_array()
Definition: binary_archive.h:158
void begin_variant()
Definition: binary_archive.h:74
void end_string(const char *delimiter)
Definition: binary_archive.h:163
void begin_object()
Definition: binary_archive.h:72
void serialize_uvarint(T &v)
Definition: binary_archive.h:211
void serialize_varint(T &v)
Definition: binary_archive.h:205
void end_object()
Definition: binary_archive.h:73
std::streamoff eof_pos_
Definition: binary_archive.h:177
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
#define false
Definition: stdbool.h:38
void begin_array(size_t s)
Definition: binary_archive.h:216
base for the binary archive type
Definition: binary_archive.h:60
void end_array()
Definition: binary_archive.h:222
binary_archive_base(stream_type &s)
Definition: binary_archive.h:68
size_t remaining_bytes()
Definition: binary_archive.h:169
void delimit_array()
Definition: binary_archive.h:221
void delimit_array()
Definition: binary_archive.h:159
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:220
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:162