From 66e6af89ce413ed7bb9f427d64250c6cc4bc3120 Mon Sep 17 00:00:00 2001 From: kenshi84 Date: Fri, 16 Dec 2016 18:10:03 +0900 Subject: [PATCH 1/4] added experimental boost::archive::portable_binary_{i|o}archive --- external/CMakeLists.txt | 1 + external/boost/CMakeLists.txt | 35 +++ .../boost/archive/portable_binary_archive.hpp | 50 +++++ .../archive/portable_binary_iarchive.hpp | 208 ++++++++++++++++++ .../archive/portable_binary_oarchive.hpp | 194 ++++++++++++++++ .../boost/src/portable_binary_iarchive.cpp | 135 ++++++++++++ .../boost/src/portable_binary_oarchive.cpp | 102 +++++++++ .../cryptonote_boost_serialization.h | 8 +- src/simplewallet/simplewallet.cpp | 4 +- src/wallet/CMakeLists.txt | 1 + src/wallet/wallet2.cpp | 34 ++- src/wallet/wallet2.h | 7 +- 12 files changed, 764 insertions(+), 15 deletions(-) create mode 100644 external/boost/CMakeLists.txt create mode 100644 external/boost/archive/portable_binary_archive.hpp create mode 100644 external/boost/archive/portable_binary_iarchive.hpp create mode 100644 external/boost/archive/portable_binary_oarchive.hpp create mode 100644 external/boost/src/portable_binary_iarchive.cpp create mode 100644 external/boost/src/portable_binary_oarchive.cpp diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt index 91611b117..c3eba8385 100644 --- a/external/CMakeLists.txt +++ b/external/CMakeLists.txt @@ -98,3 +98,4 @@ else() endif() add_subdirectory(db_drivers) +add_subdirectory(boost) diff --git a/external/boost/CMakeLists.txt b/external/boost/CMakeLists.txt new file mode 100644 index 000000000..03bdbe868 --- /dev/null +++ b/external/boost/CMakeLists.txt @@ -0,0 +1,35 @@ +# Copyright (c) 2014-2016, The Monero Project +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, are +# permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other +# materials provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be +# used to endorse or promote products derived from this software without specific +# prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +cmake_minimum_required(VERSION 2.8.7) + +project(boost_extra) + +add_library(boost_extra + src/portable_binary_iarchive.cpp + src/portable_binary_oarchive.cpp) diff --git a/external/boost/archive/portable_binary_archive.hpp b/external/boost/archive/portable_binary_archive.hpp new file mode 100644 index 000000000..f3668b9ae --- /dev/null +++ b/external/boost/archive/portable_binary_archive.hpp @@ -0,0 +1,50 @@ +#ifndef PORTABLE_BINARY_ARCHIVE_HPP +#define PORTABLE_BINARY_ARCHIVE_HPP + +// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . +// Use, modification and distribution is subject to the Boost Software +// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// MS compatible compilers support #pragma once +#if defined(_MSC_VER) +# pragma once +#endif + +#include +#include +#include + +#include +#if CHAR_BIT != 8 +#error This code assumes an eight-bit byte. +#endif + +#include +#include + +namespace boost { namespace archive { + +enum portable_binary_archive_flags { + endian_big = 0x4000, + endian_little = 0x8000 +}; + +//#if ( endian_big <= boost::archive::flags_last ) +//#error archive flags conflict +//#endif + +inline void +reverse_bytes(char size, char *address){ + char * first = address; + char * last = first + size - 1; + for(;first < last;++first, --last){ + char x = *last; + *last = *first; + *first = x; + } +} + +} } + +#endif // PORTABLE_BINARY_ARCHIVE_HPP diff --git a/external/boost/archive/portable_binary_iarchive.hpp b/external/boost/archive/portable_binary_iarchive.hpp new file mode 100644 index 000000000..30804d956 --- /dev/null +++ b/external/boost/archive/portable_binary_iarchive.hpp @@ -0,0 +1,208 @@ +#ifndef PORTABLE_BINARY_IARCHIVE_HPP +#define PORTABLE_BINARY_IARCHIVE_HPP + +// MS compatible compilers support #pragma once +#if defined(_MSC_VER) +# pragma once +#endif + +#if defined(_MSC_VER) +#pragma warning( push ) +#pragma warning( disable : 4244 ) +#endif + +/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 +// portable_binary_iarchive.hpp + +// (C) Copyright 2002-7 Robert Ramey - http://www.rrsd.com . +// Use, modification and distribution is subject to the Boost Software +// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org for updates, documentation, and revision history. + +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace boost { namespace archive { + +/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 +// exception to be thrown if integer read from archive doesn't fit +// variable being loaded +class portable_binary_iarchive_exception : + public boost::archive::archive_exception +{ +public: + enum exception_code { + incompatible_integer_size + } m_exception_code ; + portable_binary_iarchive_exception(exception_code c = incompatible_integer_size ) : + boost::archive::archive_exception(boost::archive::archive_exception::other_exception), + m_exception_code(c) + {} + virtual const char *what( ) const throw( ) + { + const char *msg = "programmer error"; + switch(m_exception_code){ + case incompatible_integer_size: + msg = "integer cannot be represented"; + break; + default: + msg = boost::archive::archive_exception::what(); + assert(false); + break; + } + return msg; + } +}; + +/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 +// "Portable" input binary archive. It addresses integer size and endienness so +// that binary archives can be passed across systems. Note:floating point types +// not addressed here +class portable_binary_iarchive : + public boost::archive::basic_binary_iprimitive< + portable_binary_iarchive, + std::istream::char_type, + std::istream::traits_type + >, + public boost::archive::detail::common_iarchive< + portable_binary_iarchive + > + { + typedef boost::archive::basic_binary_iprimitive< + portable_binary_iarchive, + std::istream::char_type, + std::istream::traits_type + > primitive_base_t; + typedef boost::archive::detail::common_iarchive< + portable_binary_iarchive + > archive_base_t; +#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS +public: +#else + friend archive_base_t; + friend primitive_base_t; // since with override load below + friend class boost::archive::detail::interface_iarchive< + portable_binary_iarchive + >; + friend class boost::archive::load_access; +protected: +#endif + unsigned int m_flags; + void load_impl(boost::intmax_t & l, char maxsize); + + // default fall through for any types not specified here + template + void load(T & t){ + boost::intmax_t l; + load_impl(l, sizeof(T)); + // use cast to avoid compile time warning + //t = static_cast< T >(l); + t = T(l); + } + void load(boost::serialization::item_version_type & t){ + boost::intmax_t l; + load_impl(l, sizeof(boost::serialization::item_version_type)); + // use cast to avoid compile time warning + t = boost::serialization::item_version_type(l); + } + void load(boost::archive::version_type & t){ + boost::intmax_t l; + load_impl(l, sizeof(boost::archive::version_type)); + // use cast to avoid compile time warning + t = boost::archive::version_type(l); + } + void load(boost::archive::class_id_type & t){ + boost::intmax_t l; + load_impl(l, sizeof(boost::archive::class_id_type)); + // use cast to avoid compile time warning + t = boost::archive::class_id_type(static_cast(l)); + } + void load(std::string & t){ + this->primitive_base_t::load(t); + } + #ifndef BOOST_NO_STD_WSTRING + void load(std::wstring & t){ + this->primitive_base_t::load(t); + } + #endif + void load(float & t){ + this->primitive_base_t::load(t); + // floats not supported + //BOOST_STATIC_ASSERT(false); + } + void load(double & t){ + this->primitive_base_t::load(t); + // doubles not supported + //BOOST_STATIC_ASSERT(false); + } + void load(char & t){ + this->primitive_base_t::load(t); + } + void load(unsigned char & t){ + this->primitive_base_t::load(t); + } + typedef boost::archive::detail::common_iarchive + detail_common_iarchive; + template + void load_override(T & t){ + this->detail_common_iarchive::load_override(t); + } + void load_override(boost::archive::class_name_type & t); + // binary files don't include the optional information + void load_override(boost::archive::class_id_optional_type &){} + + void init(unsigned int flags); +public: + portable_binary_iarchive(std::istream & is, unsigned flags = 0) : + primitive_base_t( + * is.rdbuf(), + 0 != (flags & boost::archive::no_codecvt) + ), + archive_base_t(flags), + m_flags(0) + { + init(flags); + } + + portable_binary_iarchive( + std::basic_streambuf< + std::istream::char_type, + std::istream::traits_type + > & bsb, + unsigned int flags + ) : + primitive_base_t( + bsb, + 0 != (flags & boost::archive::no_codecvt) + ), + archive_base_t(flags), + m_flags(0) + { + init(flags); + } +}; + +} } + +// required by export in boost version > 1.34 +#ifdef BOOST_SERIALIZATION_REGISTER_ARCHIVE + BOOST_SERIALIZATION_REGISTER_ARCHIVE(portable_binary_iarchive) +#endif + +// required by export in boost <= 1.34 +#define BOOST_ARCHIVE_CUSTOM_IARCHIVE_TYPES portable_binary_iarchive + +#if defined(_MSC_VER) +#pragma warning( pop ) +#endif + +#endif // PORTABLE_BINARY_IARCHIVE_HPP diff --git a/external/boost/archive/portable_binary_oarchive.hpp b/external/boost/archive/portable_binary_oarchive.hpp new file mode 100644 index 000000000..89ac3d9fc --- /dev/null +++ b/external/boost/archive/portable_binary_oarchive.hpp @@ -0,0 +1,194 @@ +#ifndef PORTABLE_BINARY_OARCHIVE_HPP +#define PORTABLE_BINARY_OARCHIVE_HPP + +// MS compatible compilers support #pragma once +#if defined(_MSC_VER) +# pragma once +#endif + +#if defined(_MSC_VER) +#pragma warning( push ) +#pragma warning( disable : 4244 ) +#endif + +/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 +// portable_binary_oarchive.hpp + +// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . +// Use, modification and distribution is subject to the Boost Software +// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org for updates, documentation, and revision history. + +#include +#include +#include +#include +#include +#include + +#include + +namespace boost { namespace archive { + +/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 +// exception to be thrown if integer read from archive doesn't fit +// variable being loaded +class portable_binary_oarchive_exception : + public boost::archive::archive_exception +{ +public: + typedef enum { + invalid_flags + } exception_code; + portable_binary_oarchive_exception(exception_code c = invalid_flags ) + {} + virtual const char *what( ) const throw( ) + { + const char *msg = "programmer error"; + switch(code){ + case invalid_flags: + msg = "cannot be both big and little endian"; + default: + boost::archive::archive_exception::what(); + } + return msg; + } +}; + +/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 +// "Portable" output binary archive. This is a variation of the native binary +// archive. it addresses integer size and endienness so that binary archives can +// be passed across systems. Note:floating point types not addressed here + +class portable_binary_oarchive : + public boost::archive::basic_binary_oprimitive< + portable_binary_oarchive, + std::ostream::char_type, + std::ostream::traits_type + >, + public boost::archive::detail::common_oarchive< + portable_binary_oarchive + > +{ + typedef boost::archive::basic_binary_oprimitive< + portable_binary_oarchive, + std::ostream::char_type, + std::ostream::traits_type + > primitive_base_t; + typedef boost::archive::detail::common_oarchive< + portable_binary_oarchive + > archive_base_t; +#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS +public: +#else + friend archive_base_t; + friend primitive_base_t; // since with override save below + friend class boost::archive::detail::interface_oarchive< + portable_binary_oarchive + >; + friend class boost::archive::save_access; +protected: +#endif + unsigned int m_flags; + void save_impl(const boost::intmax_t l, const char maxsize); + // add base class to the places considered when matching + // save function to a specific set of arguments. Note, this didn't + // work on my MSVC 7.0 system so we use the sure-fire method below + // using archive_base_t::save; + + // default fall through for any types not specified here + template + void save(const T & t){ + save_impl(t, sizeof(T)); + } + void save(const std::string & t){ + this->primitive_base_t::save(t); + } + #ifndef BOOST_NO_STD_WSTRING + void save(const std::wstring & t){ + this->primitive_base_t::save(t); + } + #endif + void save(const float & t){ + this->primitive_base_t::save(t); + // floats not supported + //BOOST_STATIC_ASSERT(false); + } + void save(const double & t){ + this->primitive_base_t::save(t); + // doubles not supported + //BOOST_STATIC_ASSERT(false); + } + void save(const char & t){ + this->primitive_base_t::save(t); + } + void save(const unsigned char & t){ + this->primitive_base_t::save(t); + } + + // default processing - kick back to base class. Note the + // extra stuff to get it passed borland compilers + typedef boost::archive::detail::common_oarchive + detail_common_oarchive; + template + void save_override(T & t){ + this->detail_common_oarchive::save_override(t); + } + // explicitly convert to char * to avoid compile ambiguities + void save_override(const boost::archive::class_name_type & t){ + const std::string s(t); + * this << s; + } + // binary files don't include the optional information + void save_override( + const boost::archive::class_id_optional_type & /* t */ + ){} + + void init(unsigned int flags); +public: + portable_binary_oarchive(std::ostream & os, unsigned flags = 0) : + primitive_base_t( + * os.rdbuf(), + 0 != (flags & boost::archive::no_codecvt) + ), + archive_base_t(flags), + m_flags(flags & (endian_big | endian_little)) + { + init(flags); + } + + portable_binary_oarchive( + std::basic_streambuf< + std::ostream::char_type, + std::ostream::traits_type + > & bsb, + unsigned int flags + ) : + primitive_base_t( + bsb, + 0 != (flags & boost::archive::no_codecvt) + ), + archive_base_t(flags), + m_flags(0) + { + init(flags); + } +}; + +} } + +// required by export in boost version > 1.34 +#ifdef BOOST_SERIALIZATION_REGISTER_ARCHIVE + BOOST_SERIALIZATION_REGISTER_ARCHIVE(portable_binary_oarchive) +#endif + +// required by export in boost <= 1.34 +#define BOOST_ARCHIVE_CUSTOM_OARCHIVE_TYPES portable_binary_oarchive + +#if defined(_MSC_VER) +#pragma warning( pop ) +#endif + +#endif // PORTABLE_BINARY_OARCHIVE_HPP diff --git a/external/boost/src/portable_binary_iarchive.cpp b/external/boost/src/portable_binary_iarchive.cpp new file mode 100644 index 000000000..4fdcf882a --- /dev/null +++ b/external/boost/src/portable_binary_iarchive.cpp @@ -0,0 +1,135 @@ +/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 +// portable_binary_iarchive.cpp + +// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . +// Use, modification and distribution is subject to the Boost Software +// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org for updates, documentation, and revision history. + +#include +#include + +#include +#include +#include + +#include + +namespace boost { namespace archive { + +void +portable_binary_iarchive::load_impl(boost::intmax_t & l, char maxsize){ + char size; + l = 0; + this->primitive_base_t::load(size); + + if(0 == size){ + return; + } + + bool negative = (size < 0); + if(negative) + size = -size; + + if(size > maxsize) + boost::serialization::throw_exception( + portable_binary_iarchive_exception() + ); + + char * cptr = reinterpret_cast(& l); + #ifdef BOOST_BIG_ENDIAN + cptr += (sizeof(boost::intmax_t) - size); + #endif + this->primitive_base_t::load_binary(cptr, size); + + #ifdef BOOST_BIG_ENDIAN + if(m_flags & endian_little) + #else + if(m_flags & endian_big) + #endif + reverse_bytes(size, cptr); + + if(negative) + l = -l; +} + +void +portable_binary_iarchive::load_override( + boost::archive::class_name_type & t +){ + std::string cn; + cn.reserve(BOOST_SERIALIZATION_MAX_KEY_SIZE); + load_override(cn); + if(cn.size() > (BOOST_SERIALIZATION_MAX_KEY_SIZE - 1)) + boost::serialization::throw_exception( + boost::archive::archive_exception( + boost::archive::archive_exception::invalid_class_name) + ); + std::memcpy(t, cn.data(), cn.size()); + // borland tweak + t.t[cn.size()] = '\0'; +} + +void +portable_binary_iarchive::init(unsigned int flags){ + if(0 == (flags & boost::archive::no_header)){ + // read signature in an archive version independent manner + std::string file_signature; + * this >> file_signature; + if(file_signature != boost::archive::BOOST_ARCHIVE_SIGNATURE()) + boost::serialization::throw_exception( + boost::archive::archive_exception( + boost::archive::archive_exception::invalid_signature + ) + ); + // make sure the version of the reading archive library can + // support the format of the archive being read + boost::archive::library_version_type input_library_version; + * this >> input_library_version; + + // extra little .t is to get around borland quirk + if(boost::archive::BOOST_ARCHIVE_VERSION() < input_library_version) + boost::serialization::throw_exception( + boost::archive::archive_exception( + boost::archive::archive_exception::unsupported_version + ) + ); + + #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205)) + this->set_library_version(input_library_version); + //#else + //#if ! BOOST_WORKAROUND(BOOST_MSVC, <= 1200) + //detail:: + //#endif + boost::archive::detail::basic_iarchive::set_library_version( + input_library_version + ); + #endif + } + unsigned char x; + load(x); + m_flags = x << CHAR_BIT; +} + +} } + +#include +#include + +namespace boost { +namespace archive { + +namespace detail { + template class archive_serializer_map; +} + +template class basic_binary_iprimitive< + portable_binary_iarchive, + std::istream::char_type, + std::istream::traits_type +> ; + +} // namespace archive +} // namespace boost diff --git a/external/boost/src/portable_binary_oarchive.cpp b/external/boost/src/portable_binary_oarchive.cpp new file mode 100644 index 000000000..ee6215bd3 --- /dev/null +++ b/external/boost/src/portable_binary_oarchive.cpp @@ -0,0 +1,102 @@ +/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 +// portable_binary_oarchive.cpp + +// (C) Copyright 2002-7 Robert Ramey - http://www.rrsd.com . +// Use, modification and distribution is subject to the Boost Software +// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org for updates, documentation, and revision history. + +#include +#include +#include + +namespace boost { namespace archive { + +void +portable_binary_oarchive::save_impl( + const boost::intmax_t l, + const char maxsize +){ + char size = 0; + + if(l == 0){ + this->primitive_base_t::save(size); + return; + } + + boost::intmax_t ll; + bool negative = (l < 0); + if(negative) + ll = -l; + else + ll = l; + + do{ + ll >>= CHAR_BIT; + ++size; + }while(ll != 0); + + this->primitive_base_t::save( + static_cast(negative ? -size : size) + ); + + if(negative) + ll = -l; + else + ll = l; + char * cptr = reinterpret_cast(& ll); + #ifdef BOOST_BIG_ENDIAN + cptr += (sizeof(boost::intmax_t) - size); + if(m_flags & endian_little) + reverse_bytes(size, cptr); + #else + if(m_flags & endian_big) + reverse_bytes(size, cptr); + #endif + this->primitive_base_t::save_binary(cptr, size); +} + +void +portable_binary_oarchive::init(unsigned int flags) { + if(m_flags == (endian_big | endian_little)){ + boost::serialization::throw_exception( + portable_binary_oarchive_exception() + ); + } + if(0 == (flags & boost::archive::no_header)){ + // write signature in an archive version independent manner + const std::string file_signature( + boost::archive::BOOST_ARCHIVE_SIGNATURE() + ); + * this << file_signature; + // write library version + const boost::archive::library_version_type v( + boost::archive::BOOST_ARCHIVE_VERSION() + ); + * this << v; + } + save(static_cast(m_flags >> CHAR_BIT)); +} + +} } + +#include +#include + +namespace boost { +namespace archive { + +namespace detail { + template class archive_serializer_map; +} + +template class basic_binary_oprimitive< + portable_binary_oarchive, + std::ostream::char_type, + std::ostream::traits_type +> ; + +} // namespace archive +} // namespace boost diff --git a/src/cryptonote_core/cryptonote_boost_serialization.h b/src/cryptonote_core/cryptonote_boost_serialization.h index 663ef5070..9a81ac307 100644 --- a/src/cryptonote_core/cryptonote_boost_serialization.h +++ b/src/cryptonote_core/cryptonote_boost_serialization.h @@ -39,6 +39,8 @@ #include #include #include +#include +#include #include "cryptonote_basic.h" #include "common/unordered_containers_boost_serialization.h" #include "crypto/crypto.h" @@ -230,7 +232,8 @@ namespace boost // a & x.senderPk; // not serialized, as we do not use it in monero currently } - inline void serializeOutPk(boost::archive::binary_iarchive &a, rct::ctkeyV &outPk_, const boost::serialization::version_type ver) + template + inline typename std::enable_if::type serializeOutPk(Archive &a, rct::ctkeyV &outPk_, const boost::serialization::version_type ver) { rct::keyV outPk; a & outPk; @@ -242,7 +245,8 @@ namespace boost } } - inline void serializeOutPk(boost::archive::binary_oarchive &a, rct::ctkeyV &outPk_, const boost::serialization::version_type ver) + template + inline typename std::enable_if::type serializeOutPk(Archive &a, rct::ctkeyV &outPk_, const boost::serialization::version_type ver) { rct::keyV outPk(outPk_.size()); for (size_t n = 0; n < outPk_.size(); ++n) diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index b46447975..222998dfd 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -3882,7 +3882,7 @@ bool simple_wallet::export_outputs(const std::vector &args) std::vector outs = m_wallet->export_outputs(); std::stringstream oss; - boost::archive::binary_oarchive ar(oss); + boost::archive::portable_binary_oarchive ar(oss); ar << outs; std::string magic(OUTPUT_EXPORT_FILE_MAGIC, strlen(OUTPUT_EXPORT_FILE_MAGIC)); @@ -3962,7 +3962,7 @@ bool simple_wallet::import_outputs(const std::vector &args) std::string body(data, headerlen); std::stringstream iss; iss << body; - boost::archive::binary_iarchive ar(iss); + boost::archive::portable_binary_iarchive ar(iss); std::vector outputs; ar >> outputs; diff --git a/src/wallet/CMakeLists.txt b/src/wallet/CMakeLists.txt index 056a1ca10..e34b7b579 100644 --- a/src/wallet/CMakeLists.txt +++ b/src/wallet/CMakeLists.txt @@ -73,6 +73,7 @@ target_link_libraries(wallet cryptonote_core mnemonics p2p + boost_extra ${Boost_CHRONO_LIBRARY} ${Boost_SERIALIZATION_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index db4fae557..f22410563 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -30,8 +30,6 @@ #include #include -#include -#include #include #include #include @@ -2313,16 +2311,38 @@ void wallet2::load(const std::string& wallet_, const std::string& password) std::stringstream iss; iss << cache_data; - boost::archive::binary_iarchive ar(iss); - ar >> *this; + try { + boost::archive::portable_binary_iarchive ar(iss); + ar >> *this; + } + catch (...) + { + LOG_PRINT_L0("Failed to open portable binary, trying unportable"); + boost::filesystem::copy_file(m_wallet_file, m_wallet_file + ".old", boost::filesystem::copy_option::overwrite_if_exists); + iss.str(""); + iss << cache_data; + boost::archive::binary_iarchive ar(iss); + ar >> *this; + } } catch (...) { LOG_PRINT_L1("Failed to load encrypted cache, trying unencrypted"); std::stringstream iss; iss << buf; - boost::archive::binary_iarchive ar(iss); - ar >> *this; + try { + boost::archive::portable_binary_iarchive ar(iss); + ar >> *this; + } + catch (...) + { + LOG_PRINT_L0("Failed to open portable binary, trying unportable"); + boost::filesystem::copy_file(m_wallet_file, m_wallet_file + ".old", boost::filesystem::copy_option::overwrite_if_exists); + iss.str(""); + iss << buf; + boost::archive::binary_iarchive ar(iss); + ar >> *this; + } } THROW_WALLET_EXCEPTION_IF( m_account_public_address.m_spend_public_key != m_account.get_keys().m_account_address.m_spend_public_key || @@ -2396,7 +2416,7 @@ void wallet2::store_to(const std::string &path, const std::string &password) } // preparing wallet data std::stringstream oss; - boost::archive::binary_oarchive ar(oss); + boost::archive::portable_binary_oarchive ar(oss); ar << *this; wallet2::cache_file_data cache_file_data = boost::value_initialized(); diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index c3381730b..9f4180c0d 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -31,7 +31,6 @@ #pragma once #include -#include #include #include @@ -676,11 +675,11 @@ namespace boost namespace serialization { template - inline void initialize_transfer_details(Archive &a, tools::wallet2::transfer_details &x, const boost::serialization::version_type ver) + inline typename std::enable_if::type initialize_transfer_details(Archive &a, tools::wallet2::transfer_details &x, const boost::serialization::version_type ver) { } - template<> - inline void initialize_transfer_details(boost::archive::binary_iarchive &a, tools::wallet2::transfer_details &x, const boost::serialization::version_type ver) + template + inline typename std::enable_if::type initialize_transfer_details(Archive &a, tools::wallet2::transfer_details &x, const boost::serialization::version_type ver) { if (ver < 1) { From d1d6e27ab661f71d90fb6530db84d5a2b92550a8 Mon Sep 17 00:00:00 2001 From: kenshi84 Date: Tue, 20 Dec 2016 11:51:22 +0900 Subject: [PATCH 2/4] moved boost cpp into hpp since they're supposed to be header only --- external/CMakeLists.txt | 1 - external/boost/CMakeLists.txt | 35 ----- .../boost/archive/portable_binary_archive.hpp | 2 + .../archive/portable_binary_iarchive.hpp | 132 +++++++++++++++++ .../archive/portable_binary_oarchive.hpp | 100 +++++++++++++ .../boost/src/portable_binary_iarchive.cpp | 135 ------------------ .../boost/src/portable_binary_oarchive.cpp | 102 ------------- src/wallet/CMakeLists.txt | 1 - 8 files changed, 234 insertions(+), 274 deletions(-) delete mode 100644 external/boost/CMakeLists.txt delete mode 100644 external/boost/src/portable_binary_iarchive.cpp delete mode 100644 external/boost/src/portable_binary_oarchive.cpp diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt index c3eba8385..91611b117 100644 --- a/external/CMakeLists.txt +++ b/external/CMakeLists.txt @@ -98,4 +98,3 @@ else() endif() add_subdirectory(db_drivers) -add_subdirectory(boost) diff --git a/external/boost/CMakeLists.txt b/external/boost/CMakeLists.txt deleted file mode 100644 index 03bdbe868..000000000 --- a/external/boost/CMakeLists.txt +++ /dev/null @@ -1,35 +0,0 @@ -# Copyright (c) 2014-2016, The Monero Project -# -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without modification, are -# permitted provided that the following conditions are met: -# -# 1. Redistributions of source code must retain the above copyright notice, this list of -# conditions and the following disclaimer. -# -# 2. Redistributions in binary form must reproduce the above copyright notice, this list -# of conditions and the following disclaimer in the documentation and/or other -# materials provided with the distribution. -# -# 3. Neither the name of the copyright holder nor the names of its contributors may be -# used to endorse or promote products derived from this software without specific -# prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY -# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL -# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, -# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF -# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -cmake_minimum_required(VERSION 2.8.7) - -project(boost_extra) - -add_library(boost_extra - src/portable_binary_iarchive.cpp - src/portable_binary_oarchive.cpp) diff --git a/external/boost/archive/portable_binary_archive.hpp b/external/boost/archive/portable_binary_archive.hpp index f3668b9ae..560ef121b 100644 --- a/external/boost/archive/portable_binary_archive.hpp +++ b/external/boost/archive/portable_binary_archive.hpp @@ -23,6 +23,8 @@ #include #include +#include + namespace boost { namespace archive { enum portable_binary_archive_flags { diff --git a/external/boost/archive/portable_binary_iarchive.hpp b/external/boost/archive/portable_binary_iarchive.hpp index 30804d956..c33085b05 100644 --- a/external/boost/archive/portable_binary_iarchive.hpp +++ b/external/boost/archive/portable_binary_iarchive.hpp @@ -30,6 +30,7 @@ #include #include +#include namespace boost { namespace archive { @@ -201,6 +202,137 @@ public: // required by export in boost <= 1.34 #define BOOST_ARCHIVE_CUSTOM_IARCHIVE_TYPES portable_binary_iarchive +/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 +// portable_binary_iarchive.cpp + +// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . +// Use, modification and distribution is subject to the Boost Software +// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org for updates, documentation, and revision history. + +#include +#include + +#include +#include +#include + +namespace boost { namespace archive { + +inline void +portable_binary_iarchive::load_impl(boost::intmax_t & l, char maxsize){ + char size; + l = 0; + this->primitive_base_t::load(size); + + if(0 == size){ + return; + } + + bool negative = (size < 0); + if(negative) + size = -size; + + if(size > maxsize) + boost::serialization::throw_exception( + portable_binary_iarchive_exception() + ); + + char * cptr = reinterpret_cast(& l); + #ifdef BOOST_BIG_ENDIAN + cptr += (sizeof(boost::intmax_t) - size); + #endif + this->primitive_base_t::load_binary(cptr, size); + + #ifdef BOOST_BIG_ENDIAN + if(m_flags & endian_little) + #else + if(m_flags & endian_big) + #endif + reverse_bytes(size, cptr); + + if(negative) + l = -l; +} + +inline void +portable_binary_iarchive::load_override( + boost::archive::class_name_type & t +){ + std::string cn; + cn.reserve(BOOST_SERIALIZATION_MAX_KEY_SIZE); + load_override(cn); + if(cn.size() > (BOOST_SERIALIZATION_MAX_KEY_SIZE - 1)) + boost::serialization::throw_exception( + boost::archive::archive_exception( + boost::archive::archive_exception::invalid_class_name) + ); + std::memcpy(t, cn.data(), cn.size()); + // borland tweak + t.t[cn.size()] = '\0'; +} + +inline void +portable_binary_iarchive::init(unsigned int flags){ + if(0 == (flags & boost::archive::no_header)){ + // read signature in an archive version independent manner + std::string file_signature; + * this >> file_signature; + if(file_signature != boost::archive::BOOST_ARCHIVE_SIGNATURE()) + boost::serialization::throw_exception( + boost::archive::archive_exception( + boost::archive::archive_exception::invalid_signature + ) + ); + // make sure the version of the reading archive library can + // support the format of the archive being read + boost::archive::library_version_type input_library_version; + * this >> input_library_version; + + // extra little .t is to get around borland quirk + if(boost::archive::BOOST_ARCHIVE_VERSION() < input_library_version) + boost::serialization::throw_exception( + boost::archive::archive_exception( + boost::archive::archive_exception::unsupported_version + ) + ); + + #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205)) + this->set_library_version(input_library_version); + //#else + //#if ! BOOST_WORKAROUND(BOOST_MSVC, <= 1200) + //detail:: + //#endif + boost::archive::detail::basic_iarchive::set_library_version( + input_library_version + ); + #endif + } + unsigned char x; + load(x); + m_flags = x << CHAR_BIT; +} + +} } + +namespace boost { +namespace archive { + +namespace detail { + template class archive_serializer_map; +} + +template class basic_binary_iprimitive< + portable_binary_iarchive, + std::istream::char_type, + std::istream::traits_type +> ; + +} // namespace archive +} // namespace boost + #if defined(_MSC_VER) #pragma warning( pop ) #endif diff --git a/external/boost/archive/portable_binary_oarchive.hpp b/external/boost/archive/portable_binary_oarchive.hpp index 89ac3d9fc..19027f65a 100644 --- a/external/boost/archive/portable_binary_oarchive.hpp +++ b/external/boost/archive/portable_binary_oarchive.hpp @@ -29,6 +29,7 @@ #include #include +#include namespace boost { namespace archive { @@ -187,6 +188,105 @@ public: // required by export in boost <= 1.34 #define BOOST_ARCHIVE_CUSTOM_OARCHIVE_TYPES portable_binary_oarchive +/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 +// portable_binary_oarchive.cpp + +// (C) Copyright 2002-7 Robert Ramey - http://www.rrsd.com . +// Use, modification and distribution is subject to the Boost Software +// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org for updates, documentation, and revision history. + +#include +#include + +namespace boost { namespace archive { + +inline void +portable_binary_oarchive::save_impl( + const boost::intmax_t l, + const char maxsize +){ + char size = 0; + + if(l == 0){ + this->primitive_base_t::save(size); + return; + } + + boost::intmax_t ll; + bool negative = (l < 0); + if(negative) + ll = -l; + else + ll = l; + + do{ + ll >>= CHAR_BIT; + ++size; + }while(ll != 0); + + this->primitive_base_t::save( + static_cast(negative ? -size : size) + ); + + if(negative) + ll = -l; + else + ll = l; + char * cptr = reinterpret_cast(& ll); + #ifdef BOOST_BIG_ENDIAN + cptr += (sizeof(boost::intmax_t) - size); + if(m_flags & endian_little) + reverse_bytes(size, cptr); + #else + if(m_flags & endian_big) + reverse_bytes(size, cptr); + #endif + this->primitive_base_t::save_binary(cptr, size); +} + +inline void +portable_binary_oarchive::init(unsigned int flags) { + if(m_flags == (endian_big | endian_little)){ + boost::serialization::throw_exception( + portable_binary_oarchive_exception() + ); + } + if(0 == (flags & boost::archive::no_header)){ + // write signature in an archive version independent manner + const std::string file_signature( + boost::archive::BOOST_ARCHIVE_SIGNATURE() + ); + * this << file_signature; + // write library version + const boost::archive::library_version_type v( + boost::archive::BOOST_ARCHIVE_VERSION() + ); + * this << v; + } + save(static_cast(m_flags >> CHAR_BIT)); +} + +} } + +namespace boost { +namespace archive { + +namespace detail { + template class archive_serializer_map; +} + +template class basic_binary_oprimitive< + portable_binary_oarchive, + std::ostream::char_type, + std::ostream::traits_type +> ; + +} // namespace archive +} // namespace boost + #if defined(_MSC_VER) #pragma warning( pop ) #endif diff --git a/external/boost/src/portable_binary_iarchive.cpp b/external/boost/src/portable_binary_iarchive.cpp deleted file mode 100644 index 4fdcf882a..000000000 --- a/external/boost/src/portable_binary_iarchive.cpp +++ /dev/null @@ -1,135 +0,0 @@ -/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 -// portable_binary_iarchive.cpp - -// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . -// Use, modification and distribution is subject to the Boost Software -// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) - -// See http://www.boost.org for updates, documentation, and revision history. - -#include -#include - -#include -#include -#include - -#include - -namespace boost { namespace archive { - -void -portable_binary_iarchive::load_impl(boost::intmax_t & l, char maxsize){ - char size; - l = 0; - this->primitive_base_t::load(size); - - if(0 == size){ - return; - } - - bool negative = (size < 0); - if(negative) - size = -size; - - if(size > maxsize) - boost::serialization::throw_exception( - portable_binary_iarchive_exception() - ); - - char * cptr = reinterpret_cast(& l); - #ifdef BOOST_BIG_ENDIAN - cptr += (sizeof(boost::intmax_t) - size); - #endif - this->primitive_base_t::load_binary(cptr, size); - - #ifdef BOOST_BIG_ENDIAN - if(m_flags & endian_little) - #else - if(m_flags & endian_big) - #endif - reverse_bytes(size, cptr); - - if(negative) - l = -l; -} - -void -portable_binary_iarchive::load_override( - boost::archive::class_name_type & t -){ - std::string cn; - cn.reserve(BOOST_SERIALIZATION_MAX_KEY_SIZE); - load_override(cn); - if(cn.size() > (BOOST_SERIALIZATION_MAX_KEY_SIZE - 1)) - boost::serialization::throw_exception( - boost::archive::archive_exception( - boost::archive::archive_exception::invalid_class_name) - ); - std::memcpy(t, cn.data(), cn.size()); - // borland tweak - t.t[cn.size()] = '\0'; -} - -void -portable_binary_iarchive::init(unsigned int flags){ - if(0 == (flags & boost::archive::no_header)){ - // read signature in an archive version independent manner - std::string file_signature; - * this >> file_signature; - if(file_signature != boost::archive::BOOST_ARCHIVE_SIGNATURE()) - boost::serialization::throw_exception( - boost::archive::archive_exception( - boost::archive::archive_exception::invalid_signature - ) - ); - // make sure the version of the reading archive library can - // support the format of the archive being read - boost::archive::library_version_type input_library_version; - * this >> input_library_version; - - // extra little .t is to get around borland quirk - if(boost::archive::BOOST_ARCHIVE_VERSION() < input_library_version) - boost::serialization::throw_exception( - boost::archive::archive_exception( - boost::archive::archive_exception::unsupported_version - ) - ); - - #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205)) - this->set_library_version(input_library_version); - //#else - //#if ! BOOST_WORKAROUND(BOOST_MSVC, <= 1200) - //detail:: - //#endif - boost::archive::detail::basic_iarchive::set_library_version( - input_library_version - ); - #endif - } - unsigned char x; - load(x); - m_flags = x << CHAR_BIT; -} - -} } - -#include -#include - -namespace boost { -namespace archive { - -namespace detail { - template class archive_serializer_map; -} - -template class basic_binary_iprimitive< - portable_binary_iarchive, - std::istream::char_type, - std::istream::traits_type -> ; - -} // namespace archive -} // namespace boost diff --git a/external/boost/src/portable_binary_oarchive.cpp b/external/boost/src/portable_binary_oarchive.cpp deleted file mode 100644 index ee6215bd3..000000000 --- a/external/boost/src/portable_binary_oarchive.cpp +++ /dev/null @@ -1,102 +0,0 @@ -/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 -// portable_binary_oarchive.cpp - -// (C) Copyright 2002-7 Robert Ramey - http://www.rrsd.com . -// Use, modification and distribution is subject to the Boost Software -// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) - -// See http://www.boost.org for updates, documentation, and revision history. - -#include -#include -#include - -namespace boost { namespace archive { - -void -portable_binary_oarchive::save_impl( - const boost::intmax_t l, - const char maxsize -){ - char size = 0; - - if(l == 0){ - this->primitive_base_t::save(size); - return; - } - - boost::intmax_t ll; - bool negative = (l < 0); - if(negative) - ll = -l; - else - ll = l; - - do{ - ll >>= CHAR_BIT; - ++size; - }while(ll != 0); - - this->primitive_base_t::save( - static_cast(negative ? -size : size) - ); - - if(negative) - ll = -l; - else - ll = l; - char * cptr = reinterpret_cast(& ll); - #ifdef BOOST_BIG_ENDIAN - cptr += (sizeof(boost::intmax_t) - size); - if(m_flags & endian_little) - reverse_bytes(size, cptr); - #else - if(m_flags & endian_big) - reverse_bytes(size, cptr); - #endif - this->primitive_base_t::save_binary(cptr, size); -} - -void -portable_binary_oarchive::init(unsigned int flags) { - if(m_flags == (endian_big | endian_little)){ - boost::serialization::throw_exception( - portable_binary_oarchive_exception() - ); - } - if(0 == (flags & boost::archive::no_header)){ - // write signature in an archive version independent manner - const std::string file_signature( - boost::archive::BOOST_ARCHIVE_SIGNATURE() - ); - * this << file_signature; - // write library version - const boost::archive::library_version_type v( - boost::archive::BOOST_ARCHIVE_VERSION() - ); - * this << v; - } - save(static_cast(m_flags >> CHAR_BIT)); -} - -} } - -#include -#include - -namespace boost { -namespace archive { - -namespace detail { - template class archive_serializer_map; -} - -template class basic_binary_oprimitive< - portable_binary_oarchive, - std::ostream::char_type, - std::ostream::traits_type -> ; - -} // namespace archive -} // namespace boost diff --git a/src/wallet/CMakeLists.txt b/src/wallet/CMakeLists.txt index e34b7b579..056a1ca10 100644 --- a/src/wallet/CMakeLists.txt +++ b/src/wallet/CMakeLists.txt @@ -73,7 +73,6 @@ target_link_libraries(wallet cryptonote_core mnemonics p2p - boost_extra ${Boost_CHRONO_LIBRARY} ${Boost_SERIALIZATION_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} From 2ac80075444ebac2feef59f98c912342a708606d Mon Sep 17 00:00:00 2001 From: kenshi84 Date: Tue, 20 Dec 2016 13:04:19 +0900 Subject: [PATCH 3/4] also use portable serializer for boost_serialization_helper.h and net_node.inl, completely adandon boost/archive/binary_oarchive.hpp --- src/common/boost_serialization_helper.h | 25 ++++++++++++--- src/cryptonote_core/account.cpp | 2 -- src/cryptonote_core/blockchain.cpp | 2 -- .../cryptonote_boost_serialization.h | 1 - src/p2p/net_node.inl | 32 +++++++++++++++---- src/p2p/net_peerlist.h | 3 +- src/wallet/wallet2.cpp | 4 +-- 7 files changed, 50 insertions(+), 19 deletions(-) diff --git a/src/common/boost_serialization_helper.h b/src/common/boost_serialization_helper.h index c640a1705..fe01532d8 100644 --- a/src/common/boost_serialization_helper.h +++ b/src/common/boost_serialization_helper.h @@ -30,8 +30,9 @@ #pragma once -#include #include +#include +#include namespace tools @@ -76,7 +77,7 @@ namespace tools return false; #endif - boost::archive::binary_oarchive a(data_file); + boost::archive::portable_binary_oarchive a(data_file); a << obj; if (data_file.fail()) return false; @@ -101,9 +102,23 @@ namespace tools data_file.open( file_path, std::ios_base::binary | std::ios_base::in); if(data_file.fail()) return false; - boost::archive::binary_iarchive a(data_file); - - a >> obj; + try + { + // first try reading in portable mode + boost::archive::portable_binary_iarchive a(data_file); + a >> obj; + } + catch(...) + { + // if failed, try reading in unportable mode + boost::filesystem::copy_file(file_path, file_path + ".unportable", boost::filesystem::copy_option::overwrite_if_exists); + data_file.close(); + data_file.open( file_path, std::ios_base::binary | std::ios_base::in); + if(data_file.fail()) + return false; + boost::archive::binary_iarchive a(data_file); + a >> obj; + } return !data_file.fail(); CATCH_ENTRY_L0("unserialize_obj_from_file", false); } diff --git a/src/cryptonote_core/account.cpp b/src/cryptonote_core/account.cpp index c3f2b4446..89ad4184c 100644 --- a/src/cryptonote_core/account.cpp +++ b/src/cryptonote_core/account.cpp @@ -28,8 +28,6 @@ // // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers -#include -#include #include #include "include_base_utils.h" diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index 70b2ccc79..ba369afbd 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -30,8 +30,6 @@ #include #include -#include -#include #include #include "include_base_utils.h" diff --git a/src/cryptonote_core/cryptonote_boost_serialization.h b/src/cryptonote_core/cryptonote_boost_serialization.h index 9a81ac307..7423b222a 100644 --- a/src/cryptonote_core/cryptonote_boost_serialization.h +++ b/src/cryptonote_core/cryptonote_boost_serialization.h @@ -38,7 +38,6 @@ #include #include #include -#include #include #include #include "cryptonote_basic.h" diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index 442c42517..f32e7a435 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -137,14 +137,34 @@ namespace nodetool { try { - boost::archive::binary_iarchive a(p2p_data); + // first try reading in portable mode + boost::archive::portable_binary_iarchive a(p2p_data); a >> *this; } - catch (const std::exception &e) + catch (...) { - LOG_ERROR("Failed to load p2p config file, falling back to default config"); - m_peerlist = peerlist_manager(); // it was probably half clobbered by the failed load - make_default_config(); + // if failed, try reading in unportable mode + boost::filesystem::copy_file(state_file_path, state_file_path + ".unportable", boost::filesystem::copy_option::overwrite_if_exists); + p2p_data.close(); + p2p_data.open( state_file_path , std::ios_base::binary | std::ios_base::in); + if(!p2p_data.fail()) + { + try + { + boost::archive::binary_iarchive a(p2p_data); + a >> *this; + } + catch (const std::exception &e) + { + LOG_ERROR("Failed to load p2p config file, falling back to default config"); + m_peerlist = peerlist_manager(); // it was probably half clobbered by the failed load + make_default_config(); + } + } + else + { + make_default_config(); + } } }else { @@ -645,7 +665,7 @@ namespace nodetool return false; }; - boost::archive::binary_oarchive a(p2p_data); + boost::archive::portable_binary_oarchive a(p2p_data); a << *this; return true; CATCH_ENTRY_L0("blockchain_storage::save", false); diff --git a/src/p2p/net_peerlist.h b/src/p2p/net_peerlist.h index c19ecf65f..fa69abd6e 100644 --- a/src/p2p/net_peerlist.h +++ b/src/p2p/net_peerlist.h @@ -36,8 +36,9 @@ #include //#include //#include -#include #include +#include +#include #include #include diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index f22410563..e58c31156 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -2318,7 +2318,7 @@ void wallet2::load(const std::string& wallet_, const std::string& password) catch (...) { LOG_PRINT_L0("Failed to open portable binary, trying unportable"); - boost::filesystem::copy_file(m_wallet_file, m_wallet_file + ".old", boost::filesystem::copy_option::overwrite_if_exists); + boost::filesystem::copy_file(m_wallet_file, m_wallet_file + ".unportable", boost::filesystem::copy_option::overwrite_if_exists); iss.str(""); iss << cache_data; boost::archive::binary_iarchive ar(iss); @@ -2337,7 +2337,7 @@ void wallet2::load(const std::string& wallet_, const std::string& password) catch (...) { LOG_PRINT_L0("Failed to open portable binary, trying unportable"); - boost::filesystem::copy_file(m_wallet_file, m_wallet_file + ".old", boost::filesystem::copy_option::overwrite_if_exists); + boost::filesystem::copy_file(m_wallet_file, m_wallet_file + ".unportable", boost::filesystem::copy_option::overwrite_if_exists); iss.str(""); iss << buf; boost::archive::binary_iarchive ar(iss); From 07b9138cad7d5e8aa046f2e555563760c11a9e37 Mon Sep 17 00:00:00 2001 From: kenshi84 Date: Tue, 20 Dec 2016 13:26:39 +0900 Subject: [PATCH 4/4] support importing unportable outputs --- src/simplewallet/simplewallet.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 222998dfd..251f9c231 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -3962,10 +3962,19 @@ bool simple_wallet::import_outputs(const std::vector &args) std::string body(data, headerlen); std::stringstream iss; iss << body; - boost::archive::portable_binary_iarchive ar(iss); std::vector outputs; - ar >> outputs; - + try + { + boost::archive::portable_binary_iarchive ar(iss); + ar >> outputs; + } + catch (...) + { + iss.str(""); + iss << body; + boost::archive::binary_iarchive ar(iss); + ar >> outputs; + } size_t n_outputs = m_wallet->import_outputs(outputs); success_msg_writer() << boost::lexical_cast(n_outputs) << " outputs imported"; }