Electroneum
binary_archive.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2019, 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/type_traits/make_unsigned.hpp>
40 
41 #include "common/varint.h"
42 #include "warnings.h"
43 
44 /* I have no clue what these lines means */
47 
48 //TODO: fix size_t warning in x32 platform
49 
50 
58 template <class Stream, bool IsSaving>
60 {
61  typedef Stream stream_type;
63  typedef boost::mpl::bool_<IsSaving> is_saving;
64 
65  typedef uint8_t variant_tag_type;
66 
67  explicit binary_archive_base(stream_type &s) : stream_(s) { }
68 
69  /* definition of standard API functions */
70  void tag(const char *) { }
71  void begin_object() { }
72  void end_object() { }
73  void begin_variant() { }
74  void end_variant() { }
75  /* I just want to leave a comment saying how this line really shows
76  flaws in the ownership model of many OOP languages, that is all. */
77  stream_type &stream() { return stream_; }
78 
79 protected:
81 };
82 
83 /* \struct binary_archive
84  *
85  * \brief the actually binary archive type
86  *
87  * \detailed The boolean template argument /a W is the is_saving
88  * parameter for binary_archive_base.
89  *
90  * The is_saving parameter says whether the archive is being read from
91  * (false) or written to (true)
92  */
93 template <bool W>
95 
96 
97 template <>
98 struct binary_archive<false> : public binary_archive_base<std::istream, false>
99 {
100 
102  stream_type::pos_type pos = stream_.tellg();
103  stream_.seekg(0, std::ios_base::end);
104  eof_pos_ = stream_.tellg();
105  stream_.seekg(pos);
106  }
107 
108  template <class T>
109  void serialize_int(T &v)
110  {
111  serialize_uint(*(typename boost::make_unsigned<T>::type *)&v);
112  }
113 
118  template <class T>
119  void serialize_uint(T &v, size_t width = sizeof(T))
120  {
121  T ret = 0;
122  unsigned shift = 0;
123  for (size_t i = 0; i < width; i++) {
124  //std::cerr << "tell: " << stream_.tellg() << " value: " << ret << std::endl;
125  char c;
126  stream_.get(c);
127  T b = (unsigned char)c;
128  ret += (b << shift); // can this be changed to OR, i think it can.
129  shift += 8;
130  }
131  v = ret;
132  }
133 
134  void serialize_blob(void *buf, size_t len, const char *delimiter="")
135  {
136  stream_.read((char *)buf, len);
137  }
138 
139  template <class T>
141  {
142  serialize_uvarint(*(typename boost::make_unsigned<T>::type *)(&v));
143  }
144 
145  template <class T>
147  {
148  typedef std::istreambuf_iterator<char> it;
149  if (tools::read_varint(it(stream_), it(), v) < 0)
150  stream_.setstate(std::ios_base::failbit);
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
#define s(x, c)
Definition: aesb.c:47
const uint32_t T[512]
Definition: groestl_tables.h:37
PUSH_WARNINGS
Definition: hash-ops.h:54
int b
Definition: base.py:1
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
#define true
Definition: stdbool.h:37
#define false
Definition: stdbool.h:38
void begin_array(size_t &s)
Definition: binary_archive.h:153
void end_string(const char *delimiter)
Definition: binary_archive.h:163
std::streamoff eof_pos_
Definition: binary_archive.h:177
void delimit_array()
Definition: binary_archive.h:159
void serialize_varint(T &v)
Definition: binary_archive.h:140
size_t remaining_bytes()
Definition: binary_archive.h:169
void serialize_blob(void *buf, size_t len, const char *delimiter="")
Definition: binary_archive.h:134
void serialize_uint(T &v, size_t width=sizeof(T))
serializes an unsigned integer
Definition: binary_archive.h:119
binary_archive(stream_type &s)
Definition: binary_archive.h:101
void read_variant_tag(variant_tag_type &t)
Definition: binary_archive.h:165
void serialize_uvarint(T &v)
Definition: binary_archive.h:146
void serialize_int(T &v)
Definition: binary_archive.h:109
void end_array()
Definition: binary_archive.h:160
void begin_string(const char *delimiter)
Definition: binary_archive.h:162
void begin_array()
Definition: binary_archive.h:158
void end_string(const char *delimiter="\"")
Definition: binary_archive.h:225
void serialize_blob(void *buf, size_t len, const char *delimiter="")
Definition: binary_archive.h:199
void serialize_uvarint(T &v)
Definition: binary_archive.h:211
void serialize_int(T v)
Definition: binary_archive.h:186
binary_archive(stream_type &s)
Definition: binary_archive.h:183
void begin_string(const char *delimiter="\"")
Definition: binary_archive.h:224
void write_variant_tag(variant_tag_type t)
Definition: binary_archive.h:227
void delimit_array()
Definition: binary_archive.h:221
void end_array()
Definition: binary_archive.h:222
void serialize_varint(T &v)
Definition: binary_archive.h:205
void begin_array(size_t s)
Definition: binary_archive.h:216
void serialize_uint(T v)
Definition: binary_archive.h:191
void begin_array()
Definition: binary_archive.h:220
base for the binary archive type
Definition: binary_archive.h:60
void begin_object()
Definition: binary_archive.h:71
void end_object()
Definition: binary_archive.h:72
stream_type & stream()
Definition: binary_archive.h:77
boost::mpl::bool_< IsSaving > is_saving
Definition: binary_archive.h:63
Stream stream_type
Definition: binary_archive.h:61
void tag(const char *)
Definition: binary_archive.h:70
binary_archive_base< Stream, IsSaving > base_type
Definition: binary_archive.h:62
uint8_t variant_tag_type
Definition: binary_archive.h:65
void end_variant()
Definition: binary_archive.h:74
binary_archive_base(stream_type &s)
Definition: binary_archive.h:67
stream_type & stream_
Definition: binary_archive.h:80
void begin_variant()
Definition: binary_archive.h:73
Definition: binary_archive.h:94
DISABLE_VS_WARNINGS(4244 4345 4503) using namespace crypto
provides the implementation of varint's