Monero
binary_archive.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2018, 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:
80  stream_type &stream_;
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::streampos 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  tools::read_varint(it(stream_), it(), v); // XXX handle failure
150  }
151 
152  void begin_array(size_t &s)
153  {
154  serialize_varint(s);
155  }
156 
157  void begin_array() { }
158  void delimit_array() { }
159  void end_array() { }
160 
161  void begin_string(const char *delimiter /*="\""*/) { }
162  void end_string(const char *delimiter /*="\""*/) { }
163 
165  serialize_int(t);
166  }
167 
168  size_t remaining_bytes() {
169  if (!stream_.good())
170  return 0;
171  //std::cerr << "tell: " << stream_.tellg() << std::endl;
172  assert(stream_.tellg() <= eof_pos_);
173  return eof_pos_ - stream_.tellg();
174  }
175 protected:
176  std::streamoff eof_pos_;
177 };
178 
179 template <>
180 struct binary_archive<true> : public binary_archive_base<std::ostream, true>
181 {
183 
184  template <class T>
185  void serialize_int(T v)
186  {
187  serialize_uint(static_cast<typename boost::make_unsigned<T>::type>(v));
188  }
189  template <class T>
191  {
192  for (size_t i = 0; i < sizeof(T); i++) {
193  stream_.put((char)(v & 0xff));
194  if (1 < sizeof(T)) v >>= 8;
195  }
196  }
197 
198  void serialize_blob(void *buf, size_t len, const char *delimiter="")
199  {
200  stream_.write((char *)buf, len);
201  }
202 
203  template <class T>
205  {
206  serialize_uvarint(*(typename boost::make_unsigned<T>::type *)(&v));
207  }
208 
209  template <class T>
211  {
212  typedef std::ostreambuf_iterator<char> it;
213  tools::write_varint(it(stream_), v);
214  }
215  void begin_array(size_t s)
216  {
217  serialize_varint(s);
218  }
219  void begin_array() { }
220  void delimit_array() { }
221  void end_array() { }
222 
223  void begin_string(const char *delimiter="\"") { }
224  void end_string(const char *delimiter="\"") { }
225 
227  serialize_int(t);
228  }
229 };
230 
231 POP_WARNINGS
void write_variant_tag(variant_tag_type t)
Definition: binary_archive.h:226
void end_array()
Definition: binary_archive.h:159
void serialize_varint(T &v)
Definition: binary_archive.h:140
void serialize_uvarint(T &v)
Definition: binary_archive.h:146
binary_archive(stream_type &s)
Definition: binary_archive.h:101
const uint32_t T[512]
Definition: groestl_tables.h:33
PUSH_WARNINGS
Definition: hash-ops.h:53
uint8_t variant_tag_type
Definition: binary_archive.h:65
void serialize_blob(void *buf, size_t len, const char *delimiter="")
Definition: binary_archive.h:198
void read_variant_tag(variant_tag_type &t)
Definition: binary_archive.h:164
stream_type & stream()
Definition: binary_archive.h:77
binary_archive_base< Stream, IsSaving > base_type
Definition: binary_archive.h:62
void serialize_int(T v)
Definition: binary_archive.h:185
binary_archive(stream_type &s)
Definition: binary_archive.h:182
boost::mpl::bool_< IsSaving > is_saving
Definition: binary_archive.h:63
void tag(const char *)
Definition: binary_archive.h:70
void serialize_blob(void *buf, size_t len, const char *delimiter="")
Definition: binary_archive.h:134
void begin_array(size_t &s)
Definition: binary_archive.h:152
void serialize_uint(T v)
Definition: binary_archive.h:190
provides the implementation of varint&#39;s
stream_type & stream_
Definition: binary_archive.h:80
void begin_array()
Definition: binary_archive.h:157
void begin_string(const char *delimiter="\)
Definition: binary_archive.h:223
void begin_variant()
Definition: binary_archive.h:73
void end_string(const char *delimiter)
Definition: binary_archive.h:162
void begin_object()
Definition: binary_archive.h:71
void serialize_uvarint(T &v)
Definition: binary_archive.h:210
void serialize_varint(T &v)
Definition: binary_archive.h:204
void end_object()
Definition: binary_archive.h:72
std::streamoff eof_pos_
Definition: binary_archive.h:176
void end_variant()
Definition: binary_archive.h:74
int b
Definition: base.py:1
#define false
Definition: stdbool.h:37
void begin_array(size_t s)
Definition: binary_archive.h:215
base for the binary archive type
Definition: binary_archive.h:59
void end_array()
Definition: binary_archive.h:221
void end_string(const char *delimiter="\)
Definition: binary_archive.h:224
binary_archive_base(stream_type &s)
Definition: binary_archive.h:67
DISABLE_VS_WARNINGS(4244 4345 4503) using namespace crypto
size_t remaining_bytes()
Definition: binary_archive.h:168
void delimit_array()
Definition: binary_archive.h:220
void delimit_array()
Definition: binary_archive.h:158
void serialize_uint(T &v, size_t width=sizeof(T))
serializes an unsigned integer
Definition: binary_archive.h:119
void begin_array()
Definition: binary_archive.h:219
Stream stream_type
Definition: binary_archive.h:61
Definition: binary_archive.h:94
#define true
Definition: stdbool.h:36
#define s(x, c)
Definition: aesb.c:46
void serialize_int(T &v)
Definition: binary_archive.h:109
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
void begin_string(const char *delimiter)
Definition: binary_archive.h:161