danicoin/src/serialization/BinaryInputStreamSerializer.cpp

155 lines
4.2 KiB
C++
Raw Normal View History

2015-05-27 12:08:46 +00:00
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
2014-09-15 17:33:47 +00:00
//
// This file is part of Bytecoin.
//
// Bytecoin is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Bytecoin is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.
#include "BinaryInputStreamSerializer.h"
#include "SerializationOverloads.h"
#include <algorithm>
#include <cassert>
#include <stdexcept>
namespace {
template<typename T, int bits = std::numeric_limits<T>::digits>
typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value, size_t>::type
readVarint(std::istream& s, T &i) {
size_t read = 0;
i = 0;
for (int shift = 0;; shift += 7) {
if (s.eof()) {
return read;
}
uint8_t byte = s.get();
if (!s) {
throw std::runtime_error("Stream read error");
}
++read;
if (shift + 7 >= bits && byte >= 1 << (bits - shift)) {
throw std::runtime_error("Varint overflow");
}
if (byte == 0 && shift != 0) {
throw std::runtime_error("Non-canonical varint representation");
}
i |= static_cast<T>(byte & 0x7f) << shift;
if ((byte & 0x80) == 0) {
break;
}
}
return read;
2014-09-15 17:33:47 +00:00
}
template<typename StorageType, typename T>
void readVarintAs(std::istream& s, T &i) {
StorageType v;
readVarint(s, v);
i = static_cast<T>(v);
2014-09-15 17:33:47 +00:00
}
}
2015-05-27 12:08:46 +00:00
namespace CryptoNote {
2014-09-15 17:33:47 +00:00
ISerializer::SerializerType BinaryInputStreamSerializer::type() const {
return ISerializer::INPUT;
}
2015-07-15 12:23:00 +00:00
bool BinaryInputStreamSerializer::beginObject(Common::StringView name) {
return true;
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
void BinaryInputStreamSerializer::endObject() {
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool BinaryInputStreamSerializer::beginArray(std::size_t& size, Common::StringView name) {
readVarintAs<uint64_t>(stream, size);
2015-07-15 12:23:00 +00:00
return true;
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
void BinaryInputStreamSerializer::endArray() {
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool BinaryInputStreamSerializer::operator()(uint8_t& value, Common::StringView name) {
readVarint(stream, value);
2015-07-15 12:23:00 +00:00
return true;
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool BinaryInputStreamSerializer::operator()(uint32_t& value, Common::StringView name) {
readVarint(stream, value);
2015-07-15 12:23:00 +00:00
return true;
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool BinaryInputStreamSerializer::operator()(int32_t& value, Common::StringView name) {
readVarintAs<uint32_t>(stream, value);
2015-07-15 12:23:00 +00:00
return true;
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool BinaryInputStreamSerializer::operator()(int64_t& value, Common::StringView name) {
readVarintAs<uint64_t>(stream, value);
2015-07-15 12:23:00 +00:00
return true;
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool BinaryInputStreamSerializer::operator()(uint64_t& value, Common::StringView name) {
readVarint(stream, value);
2015-07-15 12:23:00 +00:00
return true;
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool BinaryInputStreamSerializer::operator()(bool& value, Common::StringView name) {
2015-05-27 12:08:46 +00:00
value = stream.get() != 0;
2015-07-15 12:23:00 +00:00
return true;
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool BinaryInputStreamSerializer::operator()(std::string& value, Common::StringView name) {
2014-09-15 17:33:47 +00:00
uint64_t size;
readVarint(stream, size);
if (size > 0) {
std::vector<char> temp;
temp.resize(size);
checkedRead(&temp[0], size);
value.reserve(size);
value.assign(&temp[0], size);
} else {
value.clear();
}
2014-09-15 17:33:47 +00:00
2015-07-15 12:23:00 +00:00
return true;
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool BinaryInputStreamSerializer::binary(void* value, std::size_t size, Common::StringView name) {
checkedRead(static_cast<char*>(value), size);
return true;
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool BinaryInputStreamSerializer::binary(std::string& value, Common::StringView name) {
return (*this)(value, name);
2014-09-15 17:33:47 +00:00
}
2015-07-15 12:23:00 +00:00
bool BinaryInputStreamSerializer::operator()(double& value, Common::StringView name) {
2014-09-15 17:33:47 +00:00
assert(false); //the method is not supported for this type of serialization
throw std::runtime_error("double serialization is not supported in BinaryInputStreamSerializer");
2015-07-15 12:23:00 +00:00
return false;
2014-09-15 17:33:47 +00:00
}
void BinaryInputStreamSerializer::checkedRead(char* buf, size_t size) {
2015-07-15 12:23:00 +00:00
if (stream.read(buf, size).gcount() != size) {
throw std::runtime_error("Stream read error");
}
}
2014-09-15 17:33:47 +00:00
}