Monero
Loading...
Searching...
No Matches
portable_storage_to_bin.h
Go to the documentation of this file.
1// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above copyright
9// notice, this list of conditions and the following disclaimer in the
10// documentation and/or other materials provided with the distribution.
11// * Neither the name of the Andrey N. Sabelnikov nor the
12// names of its contributors may be used to endorse or promote products
13// derived from this software without specific prior written permission.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25//
26
27
28
29#pragma once
30
31#include "misc_language.h"
34#include "misc_log_ex.h"
35
36namespace epee
37{
38 namespace serialization
39 {
40
41 template<class pack_value, class t_stream>
42 size_t pack_varint_t(t_stream& strm, uint8_t type_or, size_t& pv)
43 {
44 pack_value v = pv << 2;
45 v |= type_or;
46 v = CONVERT_POD(v);
47 strm.write((const char*)&v, sizeof(pack_value));
48 return sizeof(pack_value);
49 }
50
51 template<class t_stream>
52 size_t pack_varint(t_stream& strm, size_t val)
53 { //the first two bits always reserved for size information
54
55 if(val <= 63)
56 {//mean enough one byte
58 }
59 else if(val <= 16383)
60 {//mean need word
62 }else if(val <= 1073741823)
63 {//mean need dword
65 }else
66 {
67 // Same as checking val <= 4611686018427387903 except that it's portable for 32-bit size_t
68 CHECK_AND_ASSERT_THROW_MES(!(val >> 31 >> 31), "failed to pack varint - too big amount = " << val);
70 }
71 }
72
73 template<class t_stream>
74 bool put_string(t_stream& strm, const std::string& v)
75 {
76 pack_varint(strm, v.size());
77 if(v.size())
78 strm.write((const char*)v.data(), v.size());
79 return true;
80 }
81
82 template<class t_stream>
83 struct array_entry_store_visitor: public boost::static_visitor<bool>
84 {
85 t_stream& m_strm;
86
87 template<class t_pod_type>
88 bool pack_pod_array_type(uint8_t contained_type, const array_entry_t<t_pod_type>& arr_pod)
89 {
90 uint8_t type = contained_type|SERIALIZE_FLAG_ARRAY;
91 m_strm.write((const char*)&type, 1);
92 pack_varint(m_strm, arr_pod.m_array.size());
93 for(t_pod_type x: arr_pod.m_array)
94 {
95 x = CONVERT_POD(x);
96 m_strm.write((const char*)&x, sizeof(t_pod_type));
97 }
98 return true;
99 }
100
101 array_entry_store_visitor(t_stream& strm):m_strm(strm){}
113 {
115 m_strm.write((const char*)&type, 1);
116 pack_varint(m_strm, arr_str.m_array.size());
117 for(const std::string& s: arr_str.m_array)
119 return true;
120 }
121 bool operator()(const array_entry_t<section>& arr_sec)
122 {
124 m_strm.write((const char*)&type, 1);
125 pack_varint(m_strm, arr_sec.m_array.size());
126 for(const section& s: arr_sec.m_array)
128 return true;
129 }
131 {
133 m_strm.write((const char*)&type, 1);
134 pack_varint(m_strm, arra_ar.m_array.size());
135 for(const array_entry& s: arra_ar.m_array)
137 return true;
138 }
139 };
140
141 template<class t_stream>
142 struct storage_entry_store_visitor: public boost::static_visitor<bool>
143 {
144 t_stream& m_strm;
145 storage_entry_store_visitor(t_stream& strm):m_strm(strm){}
146 template<class pod_type>
147 bool pack_pod_type(uint8_t type, const pod_type& v)
148 {
149 m_strm.write((const char*)&type, 1);
150 pod_type v0 = CONVERT_POD(v);
151 m_strm.write((const char*)&v0, sizeof(pod_type));
152 return true;
153 }
154 //section, array_entry
163 bool operator()(const double& v) { return pack_pod_type(SERIALIZE_TYPE_DOUBLE, v);}
164 bool operator()(const bool& v) { return pack_pod_type(SERIALIZE_TYPE_BOOL, v);}
165 bool operator()(const std::string& v)
166 {
168 m_strm.write((const char*)&type, 1);
169 put_string(m_strm, v);
170 return true;
171 }
172 bool operator()(const section& v)
173 {
175 m_strm.write((const char*)&type, 1);
176 return pack_entry_to_buff(m_strm, v);
177 }
178
179 bool operator()(const array_entry& v)
180 {
181 //uint8_t type = SERIALIZE_TYPE_ARRAY;
182 //m_strm.write((const char*)&type, 1);
183 return pack_entry_to_buff(m_strm, v);
184 }
185 };
186
187 template<class t_stream>
188 bool pack_entry_to_buff(t_stream& strm, const array_entry& ae)
189 {
191 return boost::apply_visitor(aesv, ae);
192 }
193
194 template<class t_stream>
195 bool pack_entry_to_buff(t_stream& strm, const storage_entry& se)
196 {
198 return boost::apply_visitor(sv, se);
199 }
200
201 template<class t_stream>
202 bool pack_entry_to_buff(t_stream& strm, const section& sec)
203 {
204 typedef std::map<std::string, storage_entry>::value_type section_pair;
205 pack_varint(strm, sec.m_entries.size());
206 for(const section_pair& se: sec.m_entries)
207 {
208 CHECK_AND_ASSERT_THROW_MES(se.first.size() < std::numeric_limits<uint8_t>::max(), "storage_entry_name is too long: " << se.first.size() << ", val: " << se.first);
209 CHECK_AND_ASSERT_THROW_MES(!se.first.empty(), "storage_entry_name is empty");
210 uint8_t len = static_cast<uint8_t>(se.first.size());
211 strm.write((const char*)&len, sizeof(len));
212 strm.write(se.first.data(), size_t(len));
213 pack_entry_to_buff(strm, se.second);
214 }
215 return true;
216 }
217 }
218}
#define s(x, c)
Definition aesb.c:47
#define v0(p)
Definition aesb.c:116
Definition keyvalue_serialization_overloads.h:49
bool put_string(t_stream &strm, const std::string &v)
Definition portable_storage_to_bin.h:74
boost::variant< uint64_t, uint32_t, uint16_t, uint8_t, int64_t, int32_t, int16_t, int8_t, double, bool, std::string, section, array_entry > storage_entry
Definition portable_storage_base.h:161
bool pack_entry_to_buff(t_stream &strm, const array_entry &ae)
Definition portable_storage_to_bin.h:188
size_t pack_varint(t_stream &strm, size_t val)
Definition portable_storage_to_bin.h:52
size_t pack_varint_t(t_stream &strm, uint8_t type_or, size_t &pv)
Definition portable_storage_to_bin.h:42
boost::make_recursive_variant< array_entry_t< section >, array_entry_t< uint64_t >, array_entry_t< uint32_t >, array_entry_t< uint16_t >, array_entry_t< uint8_t >, array_entry_t< int64_t >, array_entry_t< int32_t >, array_entry_t< int16_t >, array_entry_t< int8_t >, array_entry_t< double >, array_entry_t< bool >, array_entry_t< std::string >, array_entry_t< section >, array_entry_t< boost::recursive_variant_ > >::type array_entry
Definition portable_storage_base.h:159
TODO: (mj-xmr) This will be reduced in an another PR.
Definition byte_slice.h:40
#define PORTABLE_RAW_SIZE_MARK_WORD
Definition portable_storage_base.h:43
#define SERIALIZE_FLAG_ARRAY
Definition portable_storage_base.h:66
#define PORTABLE_RAW_SIZE_MARK_DWORD
Definition portable_storage_base.h:44
#define SERIALIZE_TYPE_UINT32
Definition portable_storage_base.h:57
#define SERIALIZE_TYPE_INT32
Definition portable_storage_base.h:53
#define SERIALIZE_TYPE_UINT8
Definition portable_storage_base.h:59
#define SERIALIZE_TYPE_UINT16
Definition portable_storage_base.h:58
#define PORTABLE_RAW_SIZE_MARK_BYTE
Definition portable_storage_base.h:42
#define PORTABLE_RAW_SIZE_MARK_INT64
Definition portable_storage_base.h:45
#define SERIALIZE_TYPE_INT64
Definition portable_storage_base.h:52
#define SERIALIZE_TYPE_UINT64
Definition portable_storage_base.h:56
#define SERIALIZE_TYPE_INT8
Definition portable_storage_base.h:55
#define SERIALIZE_TYPE_DOUBLE
Definition portable_storage_base.h:60
#define SERIALIZE_TYPE_STRING
Definition portable_storage_base.h:61
#define SERIALIZE_TYPE_INT16
Definition portable_storage_base.h:54
#define SERIALIZE_TYPE_OBJECT
Definition portable_storage_base.h:63
#define SERIALIZE_TYPE_ARRAY
Definition portable_storage_base.h:64
#define SERIALIZE_TYPE_BOOL
Definition portable_storage_base.h:62
#define CONVERT_POD(x)
Definition portable_storage_bin_utils.h:43
signed short int16_t
Definition stdint.h:122
unsigned short uint16_t
Definition stdint.h:125
signed __int64 int64_t
Definition stdint.h:135
unsigned int uint32_t
Definition stdint.h:126
signed int int32_t
Definition stdint.h:123
unsigned char uint8_t
Definition stdint.h:124
unsigned __int64 uint64_t
Definition stdint.h:136
signed char int8_t
Definition stdint.h:121
Definition portable_storage_to_bin.h:84
bool operator()(const array_entry_t< int64_t > &v)
Definition portable_storage_to_bin.h:106
bool operator()(const array_entry_t< int16_t > &v)
Definition portable_storage_to_bin.h:108
array_entry_store_visitor(t_stream &strm)
Definition portable_storage_to_bin.h:101
bool operator()(const array_entry_t< uint64_t > &v)
Definition portable_storage_to_bin.h:102
bool operator()(const array_entry_t< std::string > &arr_str)
Definition portable_storage_to_bin.h:112
bool operator()(const array_entry_t< section > &arr_sec)
Definition portable_storage_to_bin.h:121
bool operator()(const array_entry_t< uint16_t > &v)
Definition portable_storage_to_bin.h:104
bool operator()(const array_entry_t< array_entry > &arra_ar)
Definition portable_storage_to_bin.h:130
bool operator()(const array_entry_t< bool > &v)
Definition portable_storage_to_bin.h:111
bool operator()(const array_entry_t< int32_t > &v)
Definition portable_storage_to_bin.h:107
t_stream & m_strm
Definition portable_storage_to_bin.h:85
bool operator()(const array_entry_t< uint32_t > &v)
Definition portable_storage_to_bin.h:103
bool pack_pod_array_type(uint8_t contained_type, const array_entry_t< t_pod_type > &arr_pod)
Definition portable_storage_to_bin.h:88
bool operator()(const array_entry_t< double > &v)
Definition portable_storage_to_bin.h:110
bool operator()(const array_entry_t< int8_t > &v)
Definition portable_storage_to_bin.h:109
bool operator()(const array_entry_t< uint8_t > &v)
Definition portable_storage_to_bin.h:105
Definition portable_storage_base.h:83
entry_container< t_entry_type >::type m_array
Definition portable_storage_base.h:139
Definition portable_storage_base.h:169
std::map< std::string, storage_entry > m_entries
Definition portable_storage_base.h:170
Definition portable_storage_to_bin.h:143
bool operator()(const int64_t &v)
Definition portable_storage_to_bin.h:159
t_stream & m_strm
Definition portable_storage_to_bin.h:144
storage_entry_store_visitor(t_stream &strm)
Definition portable_storage_to_bin.h:145
bool operator()(const section &v)
Definition portable_storage_to_bin.h:172
bool operator()(const uint8_t &v)
Definition portable_storage_to_bin.h:158
bool pack_pod_type(uint8_t type, const pod_type &v)
Definition portable_storage_to_bin.h:147
bool operator()(const uint16_t &v)
Definition portable_storage_to_bin.h:157
bool operator()(const int32_t &v)
Definition portable_storage_to_bin.h:160
bool operator()(const int16_t &v)
Definition portable_storage_to_bin.h:161
bool operator()(const double &v)
Definition portable_storage_to_bin.h:163
bool operator()(const uint64_t &v)
Definition portable_storage_to_bin.h:155
bool operator()(const int8_t &v)
Definition portable_storage_to_bin.h:162
bool operator()(const uint32_t &v)
Definition portable_storage_to_bin.h:156
bool operator()(const array_entry &v)
Definition portable_storage_to_bin.h:179
bool operator()(const std::string &v)
Definition portable_storage_to_bin.h:165
bool operator()(const bool &v)
Definition portable_storage_to_bin.h:164