| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#pragma once |
| 30 |
|
| 31 |
#include "misc_language.h" |
| 32 |
#include "portable_storage_base.h" |
| 33 |
#include "portable_storage_bin_utils.h" |
| 34 |
#include "misc_log_ex.h" |
| 35 |
|
| 36 |
namespace 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 |
{ |
| 54 |
|
| 55 |
if(val <= 63) |
| 56 |
{ |
| 57 |
return pack_varint_t<uint8_t>(strm, PORTABLE_RAW_SIZE_MARK_BYTE, val); |
| 58 |
} |
| 59 |
else if(val <= 16383) |
| 60 |
{ |
| 61 |
return pack_varint_t<uint16_t>(strm, PORTABLE_RAW_SIZE_MARK_WORD, val); |
| 62 |
}else if(val <= 1073741823) |
| 63 |
{ |
| 64 |
return pack_varint_t<uint32_t>(strm, PORTABLE_RAW_SIZE_MARK_DWORD, val); |
| 65 |
}else |
| 66 |
{ |
| 67 |
|
| 68 |
CHECK_AND_ASSERT_THROW_MES(!(val >> 31 >> 31), "failed to pack varint - too big amount = " << val); |
| 69 |
return pack_varint_t<uint64_t>(strm, PORTABLE_RAW_SIZE_MARK_INT64, 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){} |
| 102 |
bool operator()(const array_entry_t<uint64_t>& v){ return pack_pod_array_type(SERIALIZE_TYPE_UINT64, v);} |
| 103 |
bool operator()(const array_entry_t<uint32_t>& v){ return pack_pod_array_type(SERIALIZE_TYPE_UINT32, v);} |
| 104 |
bool operator()(const array_entry_t<uint16_t>& v){ return pack_pod_array_type(SERIALIZE_TYPE_UINT16, v);} |
| 105 |
bool operator()(const array_entry_t<uint8_t>& v) { return pack_pod_array_type(SERIALIZE_TYPE_UINT8, v);} |
| 106 |
bool operator()(const array_entry_t<int64_t>& v) { return pack_pod_array_type(SERIALIZE_TYPE_INT64, v);} |
| 107 |
bool operator()(const array_entry_t<int32_t>& v) { return pack_pod_array_type(SERIALIZE_TYPE_INT32, v);} |
| 108 |
bool operator()(const array_entry_t<int16_t>& v) { return pack_pod_array_type(SERIALIZE_TYPE_INT16, v);} |
| 109 |
bool operator()(const array_entry_t<int8_t>& v) { return pack_pod_array_type(SERIALIZE_TYPE_INT8, v);} |
| 110 |
bool operator()(const array_entry_t<double>& v) { return pack_pod_array_type(SERIALIZE_TYPE_DOUBLE, v);} |
| 111 |
bool operator()(const array_entry_t<bool>& v) { return pack_pod_array_type(SERIALIZE_TYPE_BOOL, v);} |
| 112 |
bool operator()(const array_entry_t<std::string>& arr_str) |
| 113 |
{ |
| 114 |
uint8_t type = SERIALIZE_TYPE_STRING|SERIALIZE_FLAG_ARRAY; |
| 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 |
{ |
| 123 |
uint8_t type = SERIALIZE_TYPE_OBJECT|SERIALIZE_FLAG_ARRAY; |
| 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) |
| 127 |
pack_entry_to_buff(m_strm, s); |
| 128 |
return true; |
| 129 |
} |
| 130 |
bool operator()(const array_entry_t<array_entry>& arra_ar) |
| 131 |
{ |
| 132 |
uint8_t type = SERIALIZE_TYPE_ARRAY|SERIALIZE_FLAG_ARRAY; |
| 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) |
| 136 |
pack_entry_to_buff(m_strm, s); |
| 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 |
|
| 155 |
bool operator()(const uint64_t& v){ return pack_pod_type(SERIALIZE_TYPE_UINT64, v);} |
| 156 |
bool operator()(const uint32_t& v){ return pack_pod_type(SERIALIZE_TYPE_UINT32, v);} |
| 157 |
bool operator()(const uint16_t& v){ return pack_pod_type(SERIALIZE_TYPE_UINT16, v);} |
| 158 |
bool operator()(const uint8_t& v) { return pack_pod_type(SERIALIZE_TYPE_UINT8, v);} |
| 159 |
bool operator()(const int64_t& v) { return pack_pod_type(SERIALIZE_TYPE_INT64, v);} |
| 160 |
bool operator()(const int32_t& v) { return pack_pod_type(SERIALIZE_TYPE_INT32, v);} |
| 161 |
bool operator()(const int16_t& v) { return pack_pod_type(SERIALIZE_TYPE_INT16, v);} |
| 162 |
bool operator()(const int8_t& v) { return pack_pod_type(SERIALIZE_TYPE_INT8, v);} |
| 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 |
{ |
| 167 |
uint8_t type = SERIALIZE_TYPE_STRING; |
| 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 |
{ |
| 174 |
uint8_t type = SERIALIZE_TYPE_OBJECT; |
| 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 |
|
| 182 |
|
| 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 |
{ |
| 190 |
array_entry_store_visitor<t_stream> aesv(strm); |
| 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 |
{ |
| 197 |
storage_entry_store_visitor<t_stream> sv(strm); |
| 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 |
} |