| 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 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#pragma once |
| 35 |
|
| 36 |
#include <cassert> |
| 37 |
#include <iostream> |
| 38 |
#include <iterator> |
| 39 |
#include <boost/endian/conversion.hpp> |
| 40 |
#include <boost/type_traits/make_unsigned.hpp> |
| 41 |
|
| 42 |
#include "common/varint.h" |
| 43 |
#include "span.h" |
| 44 |
#include "warnings.h" |
| 45 |
|
| 46 |
|
| 47 |
PUSH_WARNINGS |
| 48 |
DISABLE_VS_WARNINGS(4244) |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
template <bool IsSaving> |
| 61 |
struct binary_archive_base |
| 62 |
{ |
| 63 |
typedef binary_archive_base<IsSaving> base_type; |
| 64 |
typedef boost::mpl::bool_<IsSaving> is_saving; |
| 65 |
|
| 66 |
typedef uint8_t variant_tag_type; |
| 67 |
|
| 68 |
explicit binary_archive_base() { } |
| 69 |
|
| 70 |
|
| 71 |
void tag(const char *) { } |
| 72 |
void begin_object() { } |
| 73 |
void end_object() { } |
| 74 |
void begin_variant() { } |
| 75 |
void end_variant() { } |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
template <bool W> |
| 89 |
struct binary_archive; |
| 90 |
|
| 91 |
|
| 92 |
template <> |
| 93 |
struct binary_archive<false> : public binary_archive_base<false> |
| 94 |
{ |
| 95 |
explicit binary_archive(epee::span<const std::uint8_t> s) |
| 96 |
: base_type(), bytes_(s), begin_(s.begin()), good_(true), varint_bug_backward_compatibility_(false) |
| 97 |
{} |
| 98 |
|
| 99 |
bool good() const noexcept { return good_; } |
| 100 |
void set_fail() noexcept { good_ = false; } |
| 101 |
|
| 102 |
|
| 103 |
bool eof() const noexcept { return bytes_.empty(); } |
| 104 |
std::size_t getpos() const noexcept { return bytes_.begin() - begin_; } |
| 105 |
|
| 106 |
template <class T> |
| 107 |
void serialize_int(T &v) |
| 108 |
{ |
| 109 |
serialize_uint(*(typename boost::make_unsigned<T>::type *)&v); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
template <class T> |
| 117 |
void serialize_uint(T &v) |
| 118 |
{ |
| 119 |
const std::size_t actual = bytes_.remove_prefix(sizeof(T)); |
| 120 |
good_ &= (actual == sizeof(T)); |
| 121 |
if (actual == sizeof(T)) |
| 122 |
{ |
| 123 |
std::memcpy(std::addressof(v), bytes_.data() - sizeof(T), sizeof(T)); |
| 124 |
boost::endian::little_to_native_inplace(v); |
| 125 |
} |
| 126 |
else |
| 127 |
v = 0; |
| 128 |
} |
| 129 |
|
| 130 |
void serialize_blob(void *buf, size_t len, const char *delimiter="") |
| 131 |
{ |
| 132 |
const std::size_t actual = bytes_.remove_prefix(len); |
| 133 |
good_ &= (len == actual); |
| 134 |
std::memcpy(buf, bytes_.data() - actual, actual); |
| 135 |
} |
| 136 |
|
| 137 |
template <class T> |
| 138 |
void serialize_varint(T &v) |
| 139 |
{ |
| 140 |
serialize_uvarint(*(typename boost::make_unsigned<T>::type *)(&v)); |
| 141 |
} |
| 142 |
|
| 143 |
template <class T> |
| 144 |
void serialize_uvarint(T &v) |
| 145 |
{ |
| 146 |
auto current = bytes_.cbegin(); |
| 147 |
auto end = bytes_.cend(); |
| 148 |
good_ &= (0 <= tools::read_varint(current, end, v)); |
| 149 |
current = std::min(current, bytes_.cend()); |
| 150 |
bytes_ = {current, std::size_t(bytes_.cend() - current)}; |
| 151 |
} |
| 152 |
|
| 153 |
void begin_array(size_t &s) |
| 154 |
{ |
| 155 |
serialize_varint(s); |
| 156 |
} |
| 157 |
|
| 158 |
void begin_array() { } |
| 159 |
void delimit_array() { } |
| 160 |
void end_array() { } |
| 161 |
|
| 162 |
void begin_string(const char *delimiter ) { } |
| 163 |
void end_string(const char *delimiter ) { } |
| 164 |
|
| 165 |
void read_variant_tag(variant_tag_type &t) { |
| 166 |
serialize_int(t); |
| 167 |
} |
| 168 |
|
| 169 |
size_t remaining_bytes() const noexcept { return good() ? bytes_.size() : 0; } |
| 170 |
void enable_varint_bug_backward_compatibility() { varint_bug_backward_compatibility_ = true; } |
| 171 |
bool varint_bug_backward_compatibility_enabled() const { return varint_bug_backward_compatibility_; } |
| 172 |
protected: |
| 173 |
epee::span<const std::uint8_t> bytes_; |
| 174 |
std::uint8_t const* const begin_; |
| 175 |
bool good_; |
| 176 |
bool varint_bug_backward_compatibility_; |
| 177 |
}; |
| 178 |
|
| 179 |
template <> |
| 180 |
struct binary_archive<true> : public binary_archive_base<true> |
| 181 |
{ |
| 182 |
typedef std::ostream stream_type; |
| 183 |
explicit binary_archive(stream_type &s) : base_type(), stream_(s) { } |
| 184 |
|
| 185 |
bool good() const { return stream_.good(); } |
| 186 |
void set_fail() { stream_.setstate(std::ios::failbit); } |
| 187 |
|
| 188 |
std::streampos getpos() const { return stream_.tellp(); } |
| 189 |
|
| 190 |
template <class T> |
| 191 |
void serialize_int(T v) |
| 192 |
{ |
| 193 |
serialize_uint(static_cast<typename boost::make_unsigned<T>::type>(v)); |
| 194 |
} |
| 195 |
template <class T> |
| 196 |
void serialize_uint(T v) |
| 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 |
template <class T> |
| 210 |
void serialize_varint(T &v) |
| 211 |
{ |
| 212 |
serialize_uvarint(*(typename boost::make_unsigned<T>::type *)(&v)); |
| 213 |
} |
| 214 |
|
| 215 |
template <class T> |
| 216 |
void serialize_uvarint(T &v) |
| 217 |
{ |
| 218 |
typedef std::ostreambuf_iterator<char> it; |
| 219 |
tools::write_varint(it(stream_), v); |
| 220 |
} |
| 221 |
void begin_array(size_t s) |
| 222 |
{ |
| 223 |
serialize_varint(s); |
| 224 |
} |
| 225 |
void begin_array() { } |
| 226 |
void delimit_array() { } |
| 227 |
void end_array() { } |
| 228 |
|
| 229 |
void begin_string(const char *delimiter="\"") { } |
| 230 |
void end_string(const char *delimiter="\"") { } |
| 231 |
|
| 232 |
void write_variant_tag(variant_tag_type t) { |
| 233 |
serialize_int(t); |
| 234 |
} |
| 235 |
|
| 236 |
bool varint_bug_backward_compatibility_enabled() const { return false; } |
| 237 |
protected: |
| 238 |
stream_type& stream_; |
| 239 |
}; |
| 240 |
|
| 241 |
POP_WARNINGS |