|
| 1 | +// Copyright (c) 2012-2014, The CryptoNote developers, The Bytecoin developers |
| 2 | +// |
| 3 | +// This file is part of Bytecoin. |
| 4 | +// |
| 5 | +// Bytecoin is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU Lesser General Public License as published by |
| 7 | +// the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// Bytecoin is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU Lesser General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU Lesser General Public License |
| 16 | +// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +#include "BinaryInputStreamSerializer.h" |
| 19 | +#include "SerializationOverloads.h" |
| 20 | + |
| 21 | +#include <algorithm> |
| 22 | +#include <cassert> |
| 23 | +#include <stdexcept> |
| 24 | + |
| 25 | +namespace { |
| 26 | + |
| 27 | +void deserialize(std::istream& stream, uint8_t& v) { |
| 28 | + char c; |
| 29 | + stream.get(c); |
| 30 | + v = static_cast<uint8_t>(c); |
| 31 | +} |
| 32 | + |
| 33 | +void deserialize(std::istream& stream, int8_t& v) { |
| 34 | + uint8_t val; |
| 35 | + deserialize(stream, val); |
| 36 | + v = val; |
| 37 | +} |
| 38 | + |
| 39 | +void deserialize(std::istream& stream, bool& v) { |
| 40 | + uint8_t val; |
| 41 | + deserialize(stream, val); |
| 42 | + |
| 43 | + v = val; |
| 44 | +} |
| 45 | + |
| 46 | +void deserialize(std::istream& stream, uint32_t& v) { |
| 47 | + char c; |
| 48 | + |
| 49 | + stream.get(c); |
| 50 | + v = static_cast<uint8_t>(c); |
| 51 | + |
| 52 | + stream.get(c); |
| 53 | + v += static_cast<uint8_t>(c) << 8; |
| 54 | + |
| 55 | + stream.get(c); |
| 56 | + v += static_cast<uint8_t>(c) << 16; |
| 57 | + |
| 58 | + stream.get(c); |
| 59 | + v += static_cast<uint8_t>(c) << 24; |
| 60 | +} |
| 61 | + |
| 62 | +void deserialize(std::istream& stream, int32_t& v) { |
| 63 | + uint32_t val; |
| 64 | + deserialize(stream, val); |
| 65 | + v = val; |
| 66 | +} |
| 67 | + |
| 68 | +void deserialize(std::istream& stream, uint64_t& v) { |
| 69 | + char c; |
| 70 | + uint64_t uc; |
| 71 | + |
| 72 | + stream.get(c); |
| 73 | + uc = static_cast<unsigned char>(c); |
| 74 | + v = uc; |
| 75 | + |
| 76 | + stream.get(c); |
| 77 | + uc = static_cast<unsigned char>(c); |
| 78 | + v += (uc << 8); |
| 79 | + |
| 80 | + stream.get(c); |
| 81 | + uc = static_cast<unsigned char>(c); |
| 82 | + v += (uc << 16); |
| 83 | + |
| 84 | + stream.get(c); |
| 85 | + uc = static_cast<unsigned char>(c); |
| 86 | + v += (uc << 24); |
| 87 | + |
| 88 | + stream.get(c); |
| 89 | + uc = static_cast<unsigned char>(c); |
| 90 | + v += (uc << 32); |
| 91 | + |
| 92 | + stream.get(c); |
| 93 | + uc = static_cast<unsigned char>(c); |
| 94 | + v += (uc << 40); |
| 95 | + |
| 96 | + stream.get(c); |
| 97 | + uc = static_cast<unsigned char>(c); |
| 98 | + v += (uc << 48); |
| 99 | + |
| 100 | + stream.get(c); |
| 101 | + uc = static_cast<unsigned char>(c); |
| 102 | + v += (uc << 56); |
| 103 | +} |
| 104 | + |
| 105 | +void deserialize(std::istream& stream, int64_t& v) { |
| 106 | + uint64_t val; |
| 107 | + deserialize(stream, val); |
| 108 | + v = val; |
| 109 | +} |
| 110 | + |
| 111 | +void deserialize(std::istream& stream, char* buf, size_t len) { |
| 112 | + const size_t chunk = 1000; |
| 113 | + |
| 114 | +// stream.read(buf, len); |
| 115 | + |
| 116 | +// looks redundant, but i had a bug with it |
| 117 | + while (len && stream) { |
| 118 | + size_t toRead = std::min(len, chunk); |
| 119 | + stream.read(buf, toRead); |
| 120 | + len -= toRead; |
| 121 | + buf += toRead; |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +} |
| 126 | + |
| 127 | +namespace cryptonote { |
| 128 | + |
| 129 | +ISerializer::SerializerType BinaryInputStreamSerializer::type() const { |
| 130 | + return ISerializer::INPUT; |
| 131 | +} |
| 132 | + |
| 133 | +ISerializer& BinaryInputStreamSerializer::beginObject(const std::string& name) { |
| 134 | + return *this; |
| 135 | +} |
| 136 | + |
| 137 | +ISerializer& BinaryInputStreamSerializer::endObject() { |
| 138 | + return *this; |
| 139 | +} |
| 140 | + |
| 141 | +ISerializer& BinaryInputStreamSerializer::beginArray(std::size_t& size, const std::string& name) { |
| 142 | + uint64_t val; |
| 143 | + serializeVarint(val, name, *this); |
| 144 | + size = val; |
| 145 | + |
| 146 | + return *this; |
| 147 | +} |
| 148 | + |
| 149 | +ISerializer& BinaryInputStreamSerializer::endArray() { |
| 150 | + return *this; |
| 151 | +} |
| 152 | + |
| 153 | +ISerializer& BinaryInputStreamSerializer::operator()(uint8_t& value, const std::string& name) { |
| 154 | + deserialize(stream, value); |
| 155 | + |
| 156 | + return *this; |
| 157 | +} |
| 158 | + |
| 159 | +ISerializer& BinaryInputStreamSerializer::operator()(uint32_t& value, const std::string& name) { |
| 160 | + deserialize(stream, value); |
| 161 | + |
| 162 | + return *this; |
| 163 | +} |
| 164 | + |
| 165 | +ISerializer& BinaryInputStreamSerializer::operator()(int32_t& value, const std::string& name) { |
| 166 | + uint32_t v; |
| 167 | + operator()(v, name); |
| 168 | + value = v; |
| 169 | + |
| 170 | + return *this; |
| 171 | +} |
| 172 | + |
| 173 | +ISerializer& BinaryInputStreamSerializer::operator()(int64_t& value, const std::string& name) { |
| 174 | + deserialize(stream, value); |
| 175 | + |
| 176 | + return *this; |
| 177 | +} |
| 178 | + |
| 179 | +ISerializer& BinaryInputStreamSerializer::operator()(uint64_t& value, const std::string& name) { |
| 180 | + deserialize(stream, value); |
| 181 | + |
| 182 | + return *this; |
| 183 | +} |
| 184 | + |
| 185 | +ISerializer& BinaryInputStreamSerializer::operator()(bool& value, const std::string& name) { |
| 186 | + deserialize(stream, value); |
| 187 | + |
| 188 | + return *this; |
| 189 | +} |
| 190 | + |
| 191 | +ISerializer& BinaryInputStreamSerializer::operator()(std::string& value, const std::string& name) { |
| 192 | + uint64_t size; |
| 193 | + serializeVarint(size, name, *this); |
| 194 | + |
| 195 | + std::vector<char> temp; |
| 196 | + temp.resize(size); |
| 197 | + |
| 198 | + deserialize(stream, &temp[0], size); |
| 199 | + |
| 200 | + value.reserve(size); |
| 201 | + value.assign(&temp[0], size); |
| 202 | + |
| 203 | + return *this; |
| 204 | +} |
| 205 | + |
| 206 | +ISerializer& BinaryInputStreamSerializer::operator()(char* value, std::size_t size, const std::string& name) { |
| 207 | + stream.read(value, size); |
| 208 | + |
| 209 | + return *this; |
| 210 | +} |
| 211 | + |
| 212 | +ISerializer& BinaryInputStreamSerializer::tag(const std::string& name) { |
| 213 | + return *this; |
| 214 | +} |
| 215 | + |
| 216 | +ISerializer& BinaryInputStreamSerializer::untagged(uint8_t& value) { |
| 217 | + char v; |
| 218 | + stream.get(v); |
| 219 | + value = v; |
| 220 | + |
| 221 | + return *this; |
| 222 | +} |
| 223 | + |
| 224 | +ISerializer& BinaryInputStreamSerializer::endTag() { |
| 225 | + return *this; |
| 226 | +} |
| 227 | + |
| 228 | +bool BinaryInputStreamSerializer::hasObject(const std::string& name) { |
| 229 | + assert(false); //the method is not supported for this type of serialization |
| 230 | + throw std::runtime_error("hasObject method is not supported in BinaryInputStreamSerializer"); |
| 231 | + |
| 232 | + return false; |
| 233 | +} |
| 234 | + |
| 235 | +ISerializer& BinaryInputStreamSerializer::operator()(double& value, const std::string& name) { |
| 236 | + assert(false); //the method is not supported for this type of serialization |
| 237 | + throw std::runtime_error("double serialization is not supported in BinaryInputStreamSerializer"); |
| 238 | + |
| 239 | + return *this; |
| 240 | +} |
| 241 | + |
| 242 | +} |
0 commit comments