Electroneum
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 "pragma_comp_defs.h"
32 #include "misc_language.h"
33 #include "portable_storage_base.h"
34 
35 namespace epee
36 {
37  namespace serialization
38  {
39 
40  template<class pack_value, class t_stream>
41  size_t pack_varint_t(t_stream& strm, uint8_t type_or, size_t& pv)
42  {
43  pack_value v = (*((pack_value*)&pv)) << 2;
44  v |= type_or;
45  strm.write((const char*)&v, sizeof(pack_value));
46  return sizeof(pack_value);
47  }
48 
49  PRAGMA_WARNING_PUSH
50  PRAGMA_GCC("GCC diagnostic ignored \"-Wstrict-aliasing\"")
51 #ifdef __clang__
52  PRAGMA_GCC("GCC diagnostic ignored \"-Wtautological-constant-out-of-range-compare\"")
53 #endif
54  template<class t_stream>
55  size_t pack_varint(t_stream& strm, size_t val)
56  { //the first two bits always reserved for size information
57 
58  if(val <= 63)
59  {//mean enough one byte
60  return pack_varint_t<uint8_t>(strm, PORTABLE_RAW_SIZE_MARK_BYTE, val);
61  }
62  else if(val <= 16383)
63  {//mean need word
64  return pack_varint_t<uint16_t>(strm, PORTABLE_RAW_SIZE_MARK_WORD, val);
65  }else if(val <= 1073741823)
66  {//mean need dword
67  return pack_varint_t<uint32_t>(strm, PORTABLE_RAW_SIZE_MARK_DWORD, val);
68  }else
69  {
70  CHECK_AND_ASSERT_THROW_MES(val <= 4611686018427387903, "failed to pack varint - too big amount = " << val);
71  return pack_varint_t<uint64_t>(strm, PORTABLE_RAW_SIZE_MARK_INT64, val);
72  }
73  }
74  PRAGMA_WARNING_POP
75 
76  template<class t_stream>
77  bool put_string(t_stream& strm, const std::string& v)
78  {
79  pack_varint(strm, v.size());
80  if(v.size())
81  strm.write((const char*)v.data(), v.size());
82  return true;
83  }
84 
85  template<class t_stream>
86  struct array_entry_store_visitor: public boost::static_visitor<bool>
87  {
88  t_stream& m_strm;
89 
90  template<class t_pod_type>
91  bool pack_pod_array_type(uint8_t contained_type, const array_entry_t<t_pod_type>& arr_pod)
92  {
93  uint8_t type = contained_type|SERIALIZE_FLAG_ARRAY;
94  m_strm.write((const char*)&type, 1);
95  pack_varint(m_strm, arr_pod.m_array.size());
96  for(const t_pod_type& x: arr_pod.m_array)
97  m_strm.write((const char*)&x, sizeof(t_pod_type));
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)
118  put_string(m_strm, s);
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  m_strm.write((const char*)&v, sizeof(pod_type));
151  return true;
152  }
153  //section, array_entry
157  bool operator()(const uint8_t& v) { return pack_pod_type(SERIALIZE_TYPE_UINT8, v);}
158  bool operator()(const int64_t& v) { return pack_pod_type(SERIALIZE_TYPE_INT64, v);}
159  bool operator()(const int32_t& v) { return pack_pod_type(SERIALIZE_TYPE_INT32, v);}
160  bool operator()(const int16_t& v) { return pack_pod_type(SERIALIZE_TYPE_INT16, v);}
161  bool operator()(const int8_t& v) { return pack_pod_type(SERIALIZE_TYPE_INT8, v);}
162  bool operator()(const double& v) { return pack_pod_type(SERIALIZE_TYPE_DUOBLE, v);}
163  bool operator()(const bool& v) { return pack_pod_type(SERIALIZE_TYPE_BOOL, v);}
164  bool operator()(const std::string& v)
165  {
167  m_strm.write((const char*)&type, 1);
168  put_string(m_strm, v);
169  return true;
170  }
171  bool operator()(const section& v)
172  {
174  m_strm.write((const char*)&type, 1);
175  return pack_entry_to_buff(m_strm, v);
176  }
177 
178  bool operator()(const array_entry& v)
179  {
180  //uint8_t type = SERIALIZE_TYPE_ARRAY;
181  //m_strm.write((const char*)&type, 1);
182  return pack_entry_to_buff(m_strm, v);
183  }
184  };
185 
186  template<class t_stream>
187  bool pack_entry_to_buff(t_stream& strm, const array_entry& ae)
188  {
190  return boost::apply_visitor(aesv, ae);
191  }
192 
193  template<class t_stream>
194  bool pack_entry_to_buff(t_stream& strm, const storage_entry& se)
195  {
197  return boost::apply_visitor(sv, se);
198  }
199 
200  template<class t_stream>
201  bool pack_entry_to_buff(t_stream& strm, const section& sec)
202  {
203  typedef std::map<std::string, storage_entry>::value_type section_pair;
204  pack_varint(strm, sec.m_entries.size());
205  for(const section_pair& se: sec.m_entries)
206  {
207  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);
208  uint8_t len = static_cast<uint8_t>(se.first.size());
209  strm.write((const char*)&len, sizeof(len));
210  strm.write(se.first.data(), size_t(len));
211  pack_entry_to_buff(strm, se.second);
212  }
213  return true;
214  }
215  }
216 }
#define CHECK_AND_ASSERT_THROW_MES(expr, message)
Definition: misc_log_ex.h:173
storage_entry_store_visitor(t_stream &strm)
bool operator()(const int64_t &v)
bool operator()(const array_entry_t< int8_t > &v)
#define PORTABLE_RAW_SIZE_MARK_BYTE
PRAGMA_WARNING_PUSH size_t pack_varint(t_stream &strm, size_t val)
::std::string string
Definition: gtest-port.h:1097
bool operator()(const array_entry_t< section > &arr_sec)
#define PORTABLE_RAW_SIZE_MARK_WORD
#define SERIALIZE_TYPE_INT32
#define SERIALIZE_TYPE_INT16
bool operator()(const double &v)
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
unsigned short uint16_t
Definition: stdint.h:125
bool operator()(const array_entry_t< uint64_t > &v)
bool operator()(const int32_t &v)
unsigned char uint8_t
Definition: stdint.h:124
#define SERIALIZE_TYPE_STRING
#define SERIALIZE_TYPE_INT64
bool operator()(const array_entry_t< array_entry > &arra_ar)
#define SERIALIZE_TYPE_OBJECT
#define SERIALIZE_TYPE_UINT32
#define SERIALIZE_TYPE_UINT64
#define SERIALIZE_TYPE_BOOL
bool operator()(const array_entry_t< double > &v)
bool operator()(const int16_t &v)
bool operator()(const array_entry_t< int64_t > &v)
bool operator()(const array_entry_t< std::string > &arr_str)
unsigned int uint32_t
Definition: stdint.h:126
#define SERIALIZE_TYPE_UINT16
t_stream & m_strm
signed short int16_t
Definition: stdint.h:122
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
bool operator()(const uint8_t &v)
bool operator()(const section &v)
#define SERIALIZE_FLAG_ARRAY
PRAGMA_WARNING_POP bool put_string(t_stream &strm, const std::string &v)
bool pack_pod_type(uint8_t type, const pod_type &v)
bool pack_entry_to_buff(t_stream &strm, const array_entry &ae)
bool operator()(const bool &v)
bool operator()(const std::string &v)
unsigned __int64 uint64_t
Definition: stdint.h:136
#define SERIALIZE_TYPE_ARRAY
signed char int8_t
Definition: stdint.h:121
bool operator()(const array_entry_t< int16_t > &v)
#define SERIALIZE_TYPE_INT8
bool operator()(const array_entry_t< bool > &v)
entry_container< t_entry_type >::type m_array
#define SERIALIZE_TYPE_UINT8
bool pack_pod_array_type(uint8_t contained_type, const array_entry_t< t_pod_type > &arr_pod)
bool operator()(const array_entry_t< uint8_t > &v)
signed __int64 int64_t
Definition: stdint.h:135
#define PORTABLE_RAW_SIZE_MARK_DWORD
size_t pack_varint_t(t_stream &strm, uint8_t type_or, size_t &pv)
t_stream & m_strm
#define PORTABLE_RAW_SIZE_MARK_INT64
array_entry_store_visitor(t_stream &strm)
#define SERIALIZE_TYPE_DUOBLE
signed int int32_t
Definition: stdint.h:123
bool operator()(const array_entry &v)
bool operator()(const array_entry_t< int32_t > &v)
bool operator()(const uint32_t &v)
std::map< std::string, storage_entry > m_entries
bool operator()(const int8_t &v)
bool operator()(const array_entry_t< uint32_t > &v)
bool operator()(const uint64_t &v)
bool operator()(const uint16_t &v)
bool operator()(const array_entry_t< uint16_t > &v)