Debian Codegraph @

monero/contrib/epee/include/storages/portable_storage_from_bin.h

raw symbols SCIP
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"
32 #include "misc_log_ex.h"
33 #include "portable_storage_base.h"
34 #include "portable_storage_bin_utils.h"
35
36 #undef MONERO_DEFAULT_LOG_CATEGORY
37 #define MONERO_DEFAULT_LOG_CATEGORY "serialization"
38
39 #ifdef EPEE_PORTABLE_STORAGE_RECURSION_LIMIT
40 #define EPEE_PORTABLE_STORAGE_RECURSION_LIMIT_INTERNAL EPEE_PORTABLE_STORAGE_RECURSION_LIMIT
41 #else
42 #define EPEE_PORTABLE_STORAGE_RECURSION_LIMIT_INTERNAL 100
43 #endif
44
45 namespace epee
46 {
47 namespace serialization
48 {
49 template<typename T>
50 struct ps_min_bytes {
51 static constexpr const size_t strict = 4096; // actual low bound
52 };
53 template<> struct ps_min_bytes<uint64_t> { static constexpr const size_t strict = 8; };
54 template<> struct ps_min_bytes<int64_t> { static constexpr const size_t strict = 8; };
55 template<> struct ps_min_bytes<uint32_t> { static constexpr const size_t strict = 4; };
56 template<> struct ps_min_bytes<int32_t> { static constexpr const size_t strict = 4; };
57 template<> struct ps_min_bytes<uint16_t> { static constexpr const size_t strict = 2; };
58 template<> struct ps_min_bytes<int16_t> { static constexpr const size_t strict = 2; };
59 template<> struct ps_min_bytes<uint8_t> { static constexpr const size_t strict = 1; };
60 template<> struct ps_min_bytes<int8_t> { static constexpr const size_t strict = 1; };
61 template<> struct ps_min_bytes<double> { static constexpr const size_t strict = 8; };
62 template<> struct ps_min_bytes<bool> { static constexpr const size_t strict = 1; };
63 template<> struct ps_min_bytes<std::string> { static constexpr const size_t strict = 2; };
64 template<> struct ps_min_bytes<section> { static constexpr const size_t strict = 1; };
65 template<> struct ps_min_bytes<array_entry> { static constexpr const size_t strict = 1; };
66
67 struct throwable_buffer_reader
68 {
69 throwable_buffer_reader(const void* ptr, size_t sz);
70 void read(void* target, size_t count);
71 void read_sec_name(std::string& sce_name);
72 template<class t_pod_type>
73 void read(t_pod_type& pod_val);
74 template<class t_type>
75 t_type read();
76 template<class type_name>
77 storage_entry read_ae();
78 storage_entry load_storage_array_entry(uint8_t type);
79 size_t read_varint();
80 template<class t_type>
81 storage_entry read_se();
82 storage_entry load_storage_entry();
83 void read(section& sec);
84 void read(std::string& str);
85 void read(array_entry &ae);
86 template<class t_type>
87 size_t min_bytes() const;
88 void set_limits(size_t objects, size_t fields, size_t strings);
89 private:
90 struct recursuion_limitation_guard
91 {
92 size_t& m_counter_ref;
93 recursuion_limitation_guard(size_t& counter):m_counter_ref(counter)
94 {
95 ++m_counter_ref;
96 CHECK_AND_ASSERT_THROW_MES(m_counter_ref < EPEE_PORTABLE_STORAGE_RECURSION_LIMIT_INTERNAL, "Wrong blob data in portable storage: recursion limitation (" << EPEE_PORTABLE_STORAGE_RECURSION_LIMIT_INTERNAL << ") exceeded");
97 }
98 ~recursuion_limitation_guard() noexcept(false)
99 {
100 CHECK_AND_ASSERT_THROW_MES(m_counter_ref != 0, "Internal error: m_counter_ref == 0 while ~recursuion_limitation_guard()");
101 --m_counter_ref;
102 }
103 };
104 #define RECURSION_LIMITATION() recursuion_limitation_guard rl(m_recursion_count)
105
106 const uint8_t* m_ptr;
107 size_t m_count;
108 size_t m_recursion_count;
109 size_t m_objects;
110 size_t m_fields;
111 size_t m_strings;
112
113 size_t max_objects;
114 size_t max_fields;
115 size_t max_strings;
116 };
117
118 inline throwable_buffer_reader::throwable_buffer_reader(const void* ptr, size_t sz)
119 {
120 if(!ptr)
121 throw std::runtime_error("throwable_buffer_reader: ptr==nullptr");
122 if(!sz)
123 throw std::runtime_error("throwable_buffer_reader: sz==0");
124 m_ptr = (uint8_t*)ptr;
125 m_count = sz;
126 m_recursion_count = 0;
127 m_objects = 0;
128 m_fields = 0;
129 m_strings = 0;
130 max_objects = std::numeric_limits<size_t>::max();
131 max_fields = std::numeric_limits<size_t>::max();
132 max_strings = std::numeric_limits<size_t>::max();
133 }
134 inline
135 void throwable_buffer_reader::read(void* target, size_t count)
136 {
137 RECURSION_LIMITATION();
138 CHECK_AND_ASSERT_THROW_MES(m_count >= count, " attempt to read " << count << " bytes from buffer with " << m_count << " bytes remained");
139 memcpy(target, m_ptr, count);
140 m_ptr += count;
141 m_count -= count;
142 }
143 inline
144 void throwable_buffer_reader::read_sec_name(std::string& sce_name)
145 {
146 RECURSION_LIMITATION();
147 uint8_t name_len = 0;
148 read(name_len);
149 CHECK_AND_ASSERT_THROW_MES(name_len > 0, "Section name is missing");
150 sce_name.resize(name_len);
151 read((void*)sce_name.data(), name_len);
152 }
153
154 template<class t_pod_type>
155 void throwable_buffer_reader::read(t_pod_type& pod_val)
156 {
157 RECURSION_LIMITATION();
158 static_assert(std::is_pod<t_pod_type>::value, "POD type expected");
159 read(&pod_val, sizeof(pod_val));
160 pod_val = CONVERT_POD(pod_val);
161 }
162
163 template<>
164 void throwable_buffer_reader::read<bool>(bool& pod_val)
165 {
166 RECURSION_LIMITATION();
167 static_assert(std::is_pod<bool>::value, "POD type expected");
168 static_assert(sizeof(bool) == sizeof(uint8_t), "We really shouldn't use bool directly in serialization code. Replace it with uint8_t if this assert triggers!");
169 uint8_t t;
170 read(&t, sizeof(t));
171 CHECK_AND_ASSERT_THROW_MES(t <= 1, "Invalid bool value " << t);
172 pod_val = (t != 0);
173 }
174
175 template<class t_type>
176 t_type throwable_buffer_reader::read()
177 {
178 RECURSION_LIMITATION();
179 t_type v;
180 read(v);
181 return v;
182 }
183
184
185 template<class type_name>
186 storage_entry throwable_buffer_reader::read_ae()
187 {
188 RECURSION_LIMITATION();
189 //for pod types
190 array_entry_t<type_name> sa;
191 size_t size = read_varint();
192 CHECK_AND_ASSERT_THROW_MES(size <= m_count / ps_min_bytes<type_name>::strict, "Size sanity check failed");
193 if (std::is_same<type_name, section>())
194 {
195 CHECK_AND_ASSERT_THROW_MES(size <= max_objects - m_objects, "Too many objects");
196 m_objects += size;
197 }
198 else if (std::is_same<type_name, std::string>())
199 {
200 CHECK_AND_ASSERT_THROW_MES(size <= max_strings - m_strings, "Too many strings");
201 m_strings += size;
202 }
203
204 sa.reserve(size);
205 //TODO: add some optimization here later
206 while(size--)
207 sa.m_array.push_back(read<type_name>());
208 return storage_entry(array_entry(std::move(sa)));
209 }
210
211 inline
212 storage_entry throwable_buffer_reader::load_storage_array_entry(uint8_t type)
213 {
214 RECURSION_LIMITATION();
215 type &= ~SERIALIZE_FLAG_ARRAY;
216 switch(type)
217 {
218 case SERIALIZE_TYPE_INT64: return read_ae<int64_t>();
219 case SERIALIZE_TYPE_INT32: return read_ae<int32_t>();
220 case SERIALIZE_TYPE_INT16: return read_ae<int16_t>();
221 case SERIALIZE_TYPE_INT8: return read_ae<int8_t>();
222 case SERIALIZE_TYPE_UINT64: return read_ae<uint64_t>();
223 case SERIALIZE_TYPE_UINT32: return read_ae<uint32_t>();
224 case SERIALIZE_TYPE_UINT16: return read_ae<uint16_t>();
225 case SERIALIZE_TYPE_UINT8: return read_ae<uint8_t>();
226 case SERIALIZE_TYPE_DOUBLE: return read_ae<double>();
227 case SERIALIZE_TYPE_BOOL: return read_ae<bool>();
228 case SERIALIZE_TYPE_STRING: return read_ae<std::string>();
229 case SERIALIZE_TYPE_OBJECT: return read_ae<section>();
230 case SERIALIZE_TYPE_ARRAY: return read_ae<array_entry>();
231 default:
232 CHECK_AND_ASSERT_THROW_MES(false, "unknown entry_type code = " << type);
233 }
234 return {};
235 }
236
237 inline
238 size_t throwable_buffer_reader::read_varint()
239 {
240 RECURSION_LIMITATION();
241 CHECK_AND_ASSERT_THROW_MES(m_count >= 1, "empty buff, expected place for varint");
242 size_t v = 0;
243 uint8_t size_mask = (*(uint8_t*)m_ptr) &PORTABLE_RAW_SIZE_MARK_MASK;
244 switch (size_mask)
245 {
246 case PORTABLE_RAW_SIZE_MARK_BYTE: v = read<uint8_t>();break;
247 case PORTABLE_RAW_SIZE_MARK_WORD: v = read<uint16_t>();break;
248 case PORTABLE_RAW_SIZE_MARK_DWORD: v = read<uint32_t>();break;
249 case PORTABLE_RAW_SIZE_MARK_INT64: v = read<uint64_t>();break;
250 default:
251 CHECK_AND_ASSERT_THROW_MES(false, "unknown varint size_mask = " << size_mask);
252 }
253 v >>= 2;
254 return v;
255 }
256
257 template<class t_type>
258 storage_entry throwable_buffer_reader::read_se()
259 {
260 RECURSION_LIMITATION();
261 t_type v;
262 read(v);
263 return storage_entry(v);
264 }
265
266 template<>
267 inline storage_entry throwable_buffer_reader::read_se<std::string>()
268 {
269 RECURSION_LIMITATION();
270 CHECK_AND_ASSERT_THROW_MES(m_strings + 1 <= max_strings, "Too many strings");
271 m_strings += 1;
272 return storage_entry(read<std::string>());
273 }
274
275
276 template<>
277 inline storage_entry throwable_buffer_reader::read_se<section>()
278 {
279 RECURSION_LIMITATION();
280 CHECK_AND_ASSERT_THROW_MES(m_objects < max_objects, "Too many objects");
281 ++m_objects;
282 section s;//use extra variable due to vs bug, line "storage_entry se(section()); " can't be compiled in visual studio
283 storage_entry se(std::move(s));
284 section& section_entry = boost::get<section>(se);
285 read(section_entry);
286 return se;
287 }
288
289 template<>
290 inline storage_entry throwable_buffer_reader::read_se<array_entry>()
291 {
292 RECURSION_LIMITATION();
293 uint8_t ent_type = 0;
294 read(ent_type);
295 CHECK_AND_ASSERT_THROW_MES(ent_type&SERIALIZE_FLAG_ARRAY, "wrong type sequenses");
296 return load_storage_array_entry(ent_type);
297 }
298
299 inline
300 storage_entry throwable_buffer_reader::load_storage_entry()
301 {
302 RECURSION_LIMITATION();
303 uint8_t ent_type = 0;
304 read(ent_type);
305 if(ent_type&SERIALIZE_FLAG_ARRAY)
306 return load_storage_array_entry(ent_type);
307
308 switch(ent_type)
309 {
310 case SERIALIZE_TYPE_INT64: return read_se<int64_t>();
311 case SERIALIZE_TYPE_INT32: return read_se<int32_t>();
312 case SERIALIZE_TYPE_INT16: return read_se<int16_t>();
313 case SERIALIZE_TYPE_INT8: return read_se<int8_t>();
314 case SERIALIZE_TYPE_UINT64: return read_se<uint64_t>();
315 case SERIALIZE_TYPE_UINT32: return read_se<uint32_t>();
316 case SERIALIZE_TYPE_UINT16: return read_se<uint16_t>();
317 case SERIALIZE_TYPE_UINT8: return read_se<uint8_t>();
318 case SERIALIZE_TYPE_DOUBLE: return read_se<double>();
319 case SERIALIZE_TYPE_BOOL: return read_se<bool>();
320 case SERIALIZE_TYPE_STRING: return read_se<std::string>();
321 case SERIALIZE_TYPE_OBJECT: return read_se<section>();
322 case SERIALIZE_TYPE_ARRAY: return read_se<array_entry>();
323 default:
324 CHECK_AND_ASSERT_THROW_MES(false, "unknown entry_type code = " << ent_type);
325 }
326 return {};
327 }
328 inline
329 void throwable_buffer_reader::read(section& sec)
330 {
331 RECURSION_LIMITATION();
332 sec.m_entries.clear();
333 size_t count = read_varint();
334 CHECK_AND_ASSERT_THROW_MES(count <= max_fields - m_fields, "Too many object fields");
335 m_fields += count;
336 while(count--)
337 {
338 //read section name string
339 std::string sec_name;
340 read_sec_name(sec_name);
341 const auto insert_loc = sec.m_entries.lower_bound(sec_name);
342 CHECK_AND_ASSERT_THROW_MES(insert_loc == sec.m_entries.end() || insert_loc->first != sec_name, "duplicate key: " << sec_name);
343 sec.m_entries.emplace_hint(insert_loc, std::move(sec_name), load_storage_entry());
344 }
345 }
346 inline
347 void throwable_buffer_reader::read(std::string& str)
348 {
349 RECURSION_LIMITATION();
350 size_t len = read_varint();
351 CHECK_AND_ASSERT_THROW_MES(len < MAX_STRING_LEN_POSSIBLE, "to big string len value in storage: " << len);
352 CHECK_AND_ASSERT_THROW_MES(m_count >= len, "string len count value " << len << " goes out of remain storage len " << m_count);
353 //do this manually to avoid double memory write in huge strings (first time at resize, second at read)
354 str.assign((const char*)m_ptr, len);
355 m_ptr+=len;
356 m_count -= len;
357 }
358 inline
359 void throwable_buffer_reader::read(array_entry &ae)
360 {
361 RECURSION_LIMITATION();
362 CHECK_AND_ASSERT_THROW_MES(false, "Reading array entry is not supported");
363 }
364 inline
365 void throwable_buffer_reader::set_limits(size_t objects, size_t fields, size_t strings)
366 {
367 max_objects = objects;
368 max_fields = fields;
369 max_strings = strings;
370 }
371 }
372 }