Electroneum
Loading...
Searching...
No Matches
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
58template <class Stream, bool IsSaving>
60{
63 typedef boost::mpl::bool_<IsSaving> is_saving;
64
66
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
79protected:
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 */
93template <bool W>
95
96
97template <>
98struct 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>
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 void serialize_string(std::string str, const char *delimiter="")
140 {
141 stream_.read((char *)str.c_str(), str.size());
142 }
143
144 template <class T>
146 {
147 serialize_uvarint(*(typename boost::make_unsigned<T>::type *)(&v));
148 }
149
150 template <class T>
152 {
153 typedef std::istreambuf_iterator<char> it;
154 if (tools::read_varint(it(stream_), it(), v) < 0)
155 stream_.setstate(std::ios_base::failbit);
156 }
157
158 void begin_array(size_t &s)
159 {
161 }
162
163 void begin_array() { }
164 void delimit_array() { }
165 void end_array() { }
166
167 void begin_string(const char *delimiter /*="\""*/) { }
168 void end_string(const char *delimiter /*="\""*/) { }
169
173
175 if (!stream_.good())
176 return 0;
177 //std::cerr << "tell: " << stream_.tellg() << std::endl;
178 assert(stream_.tellg() <= eof_pos_);
179 return eof_pos_ - stream_.tellg();
180 }
181protected:
182 std::streamoff eof_pos_;
183};
184
185template <>
186struct binary_archive<true> : public binary_archive_base<std::ostream, true>
187{
189
190 template <class T>
192 {
193 serialize_uint(static_cast<typename boost::make_unsigned<T>::type>(v));
194 }
195 template <class T>
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 void serialize_string(std::string str, const char *delimiter="")
210 {
211 stream_.write((char *)str.c_str(), str.size());
212 }
213
214 template <class T>
216 {
217 serialize_uvarint(*(typename boost::make_unsigned<T>::type *)(&v));
218 }
219
220 template <class T>
222 {
223 typedef std::ostreambuf_iterator<char> it;
225 }
226 void begin_array(size_t s)
227 {
229 }
230 void begin_array() { }
231 void delimit_array() { }
232 void end_array() { }
233
234 void begin_string(const char *delimiter="\"") { }
235 void end_string(const char *delimiter="\"") { }
236
240};
241
Concept for reading and writing characters.
std::enable_if< std::is_integral< T >::value &&std::is_unsigned< T >::value &&0<=bits &&bits<=std::numeric_limits< T >::digits, int >::type read_varint(InputIt &&first, InputIt &&last, T &write)
reads in the varint that is pointed to by InputIt into write
Definition varint.h:95
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
const char * buf
#define true
#define false
unsigned char uint8_t
Definition stdint.h:124
void begin_array(size_t &s)
void end_string(const char *delimiter)
void serialize_blob(void *buf, size_t len, const char *delimiter="")
binary_archive(stream_type &s)
void read_variant_tag(variant_tag_type &t)
void serialize_string(std::string str, const char *delimiter="")
void begin_string(const char *delimiter)
void serialize_uint(T &v, size_t width=sizeof(T))
serializes an unsigned integer
void end_string(const char *delimiter="\"")
void serialize_blob(void *buf, size_t len, const char *delimiter="")
void serialize_string(std::string str, const char *delimiter="")
binary_archive(stream_type &s)
void begin_string(const char *delimiter="\"")
void write_variant_tag(variant_tag_type t)
void begin_array(size_t s)
stream_type & stream()
boost::mpl::bool_< IsSaving > is_saving
void tag(const char *)
binary_archive_base< Stream, IsSaving > base_type
binary_archive_base(stream_type &s)
stream_type & stream_
provides the implementation of varint's
#define DISABLE_VS_WARNINGS(w)
Definition warnings.h:18
#define POP_WARNINGS
Definition warnings.h:17
#define PUSH_WARNINGS
Definition warnings.h:16
#define T(x)