From 296ae46ed8f8f6e5f986f978febad302e3df231a Mon Sep 17 00:00:00 2001 From: Antonio Juarez Date: Mon, 3 Mar 2014 22:07:58 +0000 Subject: moved all stuff to github --- src/serialization/binary_archive.h | 169 +++++++++++++++++++++++++++++++++++++ src/serialization/binary_utils.h | 28 ++++++ src/serialization/crypto.h | 62 ++++++++++++++ src/serialization/debug_archive.h | 28 ++++++ src/serialization/json_archive.h | 145 +++++++++++++++++++++++++++++++ src/serialization/json_utils.h | 19 +++++ src/serialization/serialization.h | 146 ++++++++++++++++++++++++++++++++ src/serialization/string.h | 35 ++++++++ src/serialization/variant.h | 110 ++++++++++++++++++++++++ src/serialization/vector.h | 74 ++++++++++++++++ 10 files changed, 816 insertions(+) create mode 100644 src/serialization/binary_archive.h create mode 100644 src/serialization/binary_utils.h create mode 100644 src/serialization/crypto.h create mode 100644 src/serialization/debug_archive.h create mode 100644 src/serialization/json_archive.h create mode 100644 src/serialization/json_utils.h create mode 100644 src/serialization/serialization.h create mode 100644 src/serialization/string.h create mode 100644 src/serialization/variant.h create mode 100644 src/serialization/vector.h (limited to 'src/serialization') diff --git a/src/serialization/binary_archive.h b/src/serialization/binary_archive.h new file mode 100644 index 000000000..f28e45c0c --- /dev/null +++ b/src/serialization/binary_archive.h @@ -0,0 +1,169 @@ +// Copyright (c) 2012-2013 The Cryptonote developers +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +/* binary_archive.h + * + * Portable (low-endian) binary archive */ +#pragma once + +#include +#include +#include +#include + +#include "common/varint.h" +#include "warnings.h" + +PUSH_WARNINGS +DISABLE_VS_WARNINGS(4244) + +//TODO: fix size_t warning in x32 platform + +template +struct binary_archive_base +{ + typedef Stream stream_type; + typedef binary_archive_base base_type; + typedef boost::mpl::bool_ is_saving; + + typedef uint8_t variant_tag_type; + + explicit binary_archive_base(stream_type &s) : stream_(s) { } + + void tag(const char *) { } + void begin_object() { } + void end_object() { } + void begin_variant() { } + void end_variant() { } + stream_type &stream() { return stream_; } +protected: + stream_type &stream_; +}; + +template +struct binary_archive; + +template <> +struct binary_archive : public binary_archive_base +{ + explicit binary_archive(stream_type &s) : base_type(s) { + stream_type::streampos pos = stream_.tellg(); + stream_.seekg(0, std::ios_base::end); + eof_pos_ = stream_.tellg(); + stream_.seekg(pos); + } + + template + void serialize_int(T &v) + { + serialize_uint(*(typename boost::make_unsigned::type *)&v); + } + + template + void serialize_uint(T &v, size_t width = sizeof(T)) + { + T ret = 0; + unsigned shift = 0; + for (size_t i = 0; i < width; i++) { + //std::cerr << "tell: " << stream_.tellg() << " value: " << ret << std::endl; + char c; + stream_.get(c); + T b = (unsigned char)c; + ret += (b << shift); + shift += 8; + } + v = ret; + } + void serialize_blob(void *buf, size_t len, const char *delimiter="") { stream_.read((char *)buf, len); } + + template + void serialize_varint(T &v) + { + serialize_uvarint(*(typename boost::make_unsigned::type *)(&v)); + } + + template + void serialize_uvarint(T &v) + { + typedef std::istreambuf_iterator it; + tools::read_varint(it(stream_), it(), v); // XXX handle failure + } + void begin_array(size_t &s) + { + serialize_varint(s); + } + void begin_array() { } + + void delimit_array() { } + void end_array() { } + + void begin_string(const char *delimiter="\"") { } + void end_string(const char *delimiter="\"") { } + + void read_variant_tag(variant_tag_type &t) { + serialize_int(t); + } + + size_t remaining_bytes() { + if (!stream_.good()) + return 0; + //std::cerr << "tell: " << stream_.tellg() << std::endl; + assert(stream_.tellg() <= eof_pos_); + return eof_pos_ - stream_.tellg(); + } +protected: + std::streamoff eof_pos_; +}; + +template <> +struct binary_archive : public binary_archive_base +{ + explicit binary_archive(stream_type &s) : base_type(s) { } + + template + void serialize_int(T v) + { + serialize_uint(static_cast::type>(v)); + } + template + void serialize_uint(T v) + { + for (size_t i = 0; i < sizeof(T); i++) { + stream_.put((char)(v & 0xff)); + if (1 < sizeof(T)) { + v >>= 8; + } + } + } + void serialize_blob(void *buf, size_t len, const char *delimiter="") { stream_.write((char *)buf, len); } + + template + void serialize_varint(T &v) + { + serialize_uvarint(*(typename boost::make_unsigned::type *)(&v)); + } + + template + void serialize_uvarint(T &v) + { + typedef std::ostreambuf_iterator it; + tools::write_varint(it(stream_), v); + } + void begin_array(size_t s) + { + serialize_varint(s); + } + void begin_array() { } + void delimit_array() { } + void end_array() { } + + void begin_string(const char *delimiter="\"") { } + void end_string(const char *delimiter="\"") { } + + void write_variant_tag(variant_tag_type t) { + serialize_int(t); + } +}; + +POP_WARNINGS diff --git a/src/serialization/binary_utils.h b/src/serialization/binary_utils.h new file mode 100644 index 000000000..d06e8a22a --- /dev/null +++ b/src/serialization/binary_utils.h @@ -0,0 +1,28 @@ +// Copyright (c) 2012-2013 The Cryptonote developers +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include +#include "binary_archive.h" + +namespace serialization { + +template +bool parse_binary(const std::string &blob, T &v) +{ + std::istringstream istr(blob); + binary_archive iar(istr); + return ::serialization::serialize(iar, v); +} + +template +bool dump_binary(T& v, std::string& blob) +{ + std::stringstream ostr; + binary_archive oar(ostr); + bool success = ::serialization::serialize(oar, v); + blob = ostr.str(); + return success && ostr.good(); +}; + +} // namespace serialization diff --git a/src/serialization/crypto.h b/src/serialization/crypto.h new file mode 100644 index 000000000..89d3c9885 --- /dev/null +++ b/src/serialization/crypto.h @@ -0,0 +1,62 @@ +// Copyright (c) 2012-2013 The Cryptonote developers +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include "serialization.h" +#include "debug_archive.h" +#include "crypto/chacha8.h" +#include "crypto/crypto.h" +#include "crypto/hash.h" + +// read +template