From a91822616e712f94dba66da2b596a42e9fb68a07 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Sun, 18 Mar 2018 10:49:44 +0100 Subject: [PATCH] Added max_depth to remaining variant conversions --- include/fc/container/flat.hpp | 33 ++++++++------ include/fc/crypto/bigint.hpp | 4 +- include/fc/crypto/pke.hpp | 8 ++-- include/fc/fixed_string.hpp | 24 +--------- include/fc/interprocess/container.hpp | 66 +++++++++++---------------- include/fc/io/enum_type.hpp | 8 ++-- include/fc/io/varint.hpp | 8 ++-- include/fc/network/ip.hpp | 8 ++-- include/fc/network/url.hpp | 4 +- include/fc/real128.hpp | 4 +- include/fc/reflect/variant.hpp | 22 ++++----- include/fc/rpc/cli.hpp | 1 + include/fc/static_variant.hpp | 21 +++++---- include/fc/time.hpp | 4 +- include/fc/uint128.hpp | 4 +- include/fc/variant.hpp | 13 ++++-- include/fc/variant_object.hpp | 5 +- src/crypto/bigint.cpp | 12 +++-- src/crypto/pke.cpp | 12 ++--- src/io/varint.cpp | 8 ++-- src/network/ip.cpp | 8 ++-- src/network/url.cpp | 5 +- src/real128.cpp | 4 +- src/uint128.cpp | 30 ++++-------- src/variant.cpp | 7 +-- src/variant_object.cpp | 16 +++++-- 26 files changed, 158 insertions(+), 181 deletions(-) diff --git a/include/fc/container/flat.hpp b/include/fc/container/flat.hpp index b323c25..2eca459 100644 --- a/include/fc/container/flat.hpp +++ b/include/fc/container/flat.hpp @@ -99,41 +99,48 @@ namespace fc { template - void to_variant( const flat_set& var, variant& vo ) + void to_variant( const flat_set& var, variant& vo, uint32_t _max_depth ) { + FC_ASSERT( _max_depth > 0 ); + --_max_depth; std::vector vars(var.size()); size_t i = 0; - for( auto itr = var.begin(); itr != var.end(); ++itr, ++i ) - vars[i] = variant(*itr); + for( const auto& item : var ) + vars[i++] = variant( item, _max_depth ); vo = vars; } template - void from_variant( const variant& var, flat_set& vo ) + void from_variant( const variant& var, flat_set& vo, uint32_t _max_depth ) { + FC_ASSERT( _max_depth > 0 ); + --_max_depth; const variants& vars = var.get_array(); vo.clear(); vo.reserve( vars.size() ); - for( auto itr = vars.begin(); itr != vars.end(); ++itr ) - vo.insert( itr->as() ); + for( const auto& item : vars ) + vo.insert( item.as(_max_depth) ); } template - void to_variant( const flat_map& var, variant& vo ) + void to_variant( const flat_map& var, variant& vo, uint32_t _max_depth ) { + FC_ASSERT( _max_depth > 0 ); + --_max_depth; std::vector< variant > vars(var.size()); size_t i = 0; - for( auto itr = var.begin(); itr != var.end(); ++itr, ++i ) - vars[i] = fc::variant(*itr); + for( const auto& item : var ) + vars[i++] = variant( item, _max_depth ); vo = vars; } template - void from_variant( const variant& var, flat_map& vo ) + void from_variant( const variant& var, flat_map& vo, uint32_t _max_depth ) { + FC_ASSERT( _max_depth > 0 ); + --_max_depth; const variants& vars = var.get_array(); vo.clear(); - for( auto itr = vars.begin(); itr != vars.end(); ++itr ) - vo.insert( itr->as< std::pair >() ); - + for( const auto& item : vars ) + vo.insert( item.as>(_max_depth) ); } } diff --git a/include/fc/crypto/bigint.hpp b/include/fc/crypto/bigint.hpp index b91631a..58703a1 100644 --- a/include/fc/crypto/bigint.hpp +++ b/include/fc/crypto/bigint.hpp @@ -69,8 +69,8 @@ namespace fc { class variant; /** encodes the big int as base64 string, or a number */ - void to_variant( const bigint& bi, variant& v ); + void to_variant( const bigint& bi, variant& v, uint32_t max_depth = 1 ); /** decodes the big int as base64 string, or a number */ - void from_variant( const variant& v, bigint& bi ); + void from_variant( const variant& v, bigint& bi, uint32_t max_depth = 1 ); } // namespace fc diff --git a/include/fc/crypto/pke.hpp b/include/fc/crypto/pke.hpp index 551ef99..db33b46 100644 --- a/include/fc/crypto/pke.hpp +++ b/include/fc/crypto/pke.hpp @@ -108,10 +108,10 @@ namespace fc { } } class variant; - void to_variant( const public_key& bi, variant& v ); - void from_variant( const variant& v, public_key& bi ); - void to_variant( const private_key& bi, variant& v ); - void from_variant( const variant& v, private_key& bi ); + void to_variant( const public_key& bi, variant& v, uint32_t max_depth = 1 ); + void from_variant( const variant& v, public_key& bi, uint32_t max_depth = 1 ); + void to_variant( const private_key& bi, variant& v, uint32_t max_depth = 1 ); + void from_variant( const variant& v, private_key& bi, uint32_t max_depth = 1 ); } // fc diff --git a/include/fc/fixed_string.hpp b/include/fc/fixed_string.hpp index ce00390..a91b651 100644 --- a/include/fc/fixed_string.hpp +++ b/include/fc/fixed_string.hpp @@ -121,43 +121,23 @@ namespace fc { left -= 1024; } s.read( buf, left ); - - /* - s.seekp( s.tellp() + (size.value - sizeof(Storage)) ); - char tmp; - size.value -= sizeof(storage); - while( size.value ){ s.read( &tmp, 1 ); --size.value; } - */ - // s.skip( size.value - sizeof(Storage) ); } else { s.read( (char*)&u.data, size.value ); } } } - - /* - template - inline void pack( Stream& s, const boost::multiprecision::number& d, uint32_t _max_depth=FC_PACK_MAX_DEPTH ) { - s.write( (const char*)&d, sizeof(d) ); - } - - template - inline void unpack( Stream& s, boost::multiprecision::number& u, uint32_t _max_depth=FC_PACK_MAX_DEPTH ) { - s.read( (const char*)&u, sizeof(u) ); - } - */ } } #include namespace fc { template - void to_variant( const fixed_string& s, variant& v ) { + void to_variant( const fixed_string& s, variant& v, uint32_t max_depth = 1 ) { v = std::string(s); } template - void from_variant( const variant& v, fixed_string& s ) { + void from_variant( const variant& v, fixed_string& s, uint32_t max_depth = 1 ) { s = v.as_string(); } } diff --git a/include/fc/interprocess/container.hpp b/include/fc/interprocess/container.hpp index f61a68c..7ede74b 100644 --- a/include/fc/interprocess/container.hpp +++ b/include/fc/interprocess/container.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #include namespace fc { @@ -16,87 +17,76 @@ namespace fc { namespace bip = boost::interprocess; template - void to_variant( const bip::deque< T... >& t, fc::variant& v ) { + void to_variant( const bip::deque< T... >& t, fc::variant& v, uint32_t max_depth ) { + FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" ); + --max_depth; std::vector vars(t.size()); for( size_t i = 0; i < t.size(); ++i ) { - vars[i] = t[i]; + to_variant( t[i], vars[i], max_depth ); } v = std::move(vars); } template - void from_variant( const fc::variant& v, bip::deque< T, A... >& d ) { + void from_variant( const fc::variant& v, bip::deque< T, A... >& d, uint32_t max_depth ) { + FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" ); + --max_depth; const variants& vars = v.get_array(); d.clear(); d.resize( vars.size() ); for( uint32_t i = 0; i < vars.size(); ++i ) { - from_variant( vars[i], d[i] ); + from_variant( vars[i], d[i], max_depth ); } } - //bip::map == boost::map template - void to_variant( const bip::map< K, V, T... >& var, fc::variant& vo ) { + void to_variant( const bip::map< K, V, T... >& var, fc::variant& vo, uint32_t max_depth ) { + FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" ); + --max_depth; std::vector< variant > vars(var.size()); size_t i = 0; for( auto itr = var.begin(); itr != var.end(); ++itr, ++i ) - vars[i] = fc::variant(*itr); + vars[i] = fc::variant( *itr, max_depth ); vo = vars; } -/* - template - void from_variant( const variant& var, bip::map& vo ) - { - const variants& vars = var.get_array(); - vo.clear(); - for( auto itr = vars.begin(); itr != vars.end(); ++itr ) - vo.insert( itr->as< std::pair >() ); Not safe for interprocess. Needs allocator - } -*/ template - void to_variant( const bip::vector< T... >& t, fc::variant& v ) { + void to_variant( const bip::vector< T... >& t, fc::variant& v, uint32_t max_depth ) { + FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" ); + --max_depth; std::vector vars(t.size()); for( size_t i = 0; i < t.size(); ++i ) { - vars[i] = t[i]; + to_variant( t[i], vars[i], max_depth ); } v = std::move(vars); } template - void from_variant( const fc::variant& v, bip::vector< T, A... >& d ) { + void from_variant( const fc::variant& v, bip::vector< T, A... >& d, uint32_t max_depth ) { + FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" ); + --max_depth; const variants& vars = v.get_array(); d.clear(); d.resize( vars.size() ); for( uint32_t i = 0; i < vars.size(); ++i ) { - from_variant( vars[i], d[i] ); + from_variant( vars[i], d[i], max_depth ); } } template - void to_variant( const bip::set< T... >& t, fc::variant& v ) { + void to_variant( const bip::set< T... >& t, fc::variant& v, uint32_t max_depth ) { + FC_ASSERT( max_depth > 0, "Recursion depth exceeded!" ); + --max_depth; std::vector vars; vars.reserve(t.size()); for( const auto& item : t ) { - vars.emplace_back( item ); + vars.emplace_back( variant( item, max_depth ) ); } v = std::move(vars); } -/* - template - void from_variant( const fc::variant& v, bip::set< T, A... >& d ) { - const variants& vars = v.get_array(); - d.clear(); - d.reserve( vars.size() ); - for( uint32_t i = 0; i < vars.size(); ++i ) { - from_variant( vars[i], d[i] ); Not safe for interprocess. Needs allocator - } - } -*/ - template - void to_variant( const bip::vector& t, fc::variant& v ) + void to_variant( const bip::vector& t, fc::variant& v, uint32_t max_depth = 1 ) { if( t.size() ) v = variant(fc::to_hex(t.data(), t.size())); @@ -105,7 +95,7 @@ namespace fc { } template - void from_variant( const fc::variant& v, bip::vector& d ) + void from_variant( const fc::variant& v, bip::vector& d, uint32_t max_depth = 1 ) { auto str = v.as_string(); d.resize( str.size() / 2 ); @@ -114,8 +104,6 @@ namespace fc { size_t r = fc::from_hex( str, d.data(), d.size() ); FC_ASSERT( r == d.size() ); } - // std::string b64 = base64_decode( var.as_string() ); - // vo = std::vector( b64.c_str(), b64.c_str() + b64.size() ); } namespace raw { diff --git a/include/fc/io/enum_type.hpp b/include/fc/io/enum_type.hpp index 46da877..0d35c4b 100644 --- a/include/fc/io/enum_type.hpp +++ b/include/fc/io/enum_type.hpp @@ -48,14 +48,14 @@ namespace fc template - void to_variant( const enum_type& var, variant& vo ) + void to_variant( const enum_type& var, variant& vo, uint32_t max_depth = 1 ) { - vo = (EnumType)var.value; + to_variant( var.value, vo, max_depth ); } template - void from_variant( const variant& var, enum_type& vo ) + void from_variant( const variant& var, enum_type& vo, uint32_t max_depth ) { - vo.value = var.as(); + vo.value = var.as(1); } diff --git a/include/fc/io/varint.hpp b/include/fc/io/varint.hpp index b4988c5..00a8023 100644 --- a/include/fc/io/varint.hpp +++ b/include/fc/io/varint.hpp @@ -70,10 +70,10 @@ struct signed_int { class variant; -void to_variant( const signed_int& var, variant& vo ); -void from_variant( const variant& var, signed_int& vo ); -void to_variant( const unsigned_int& var, variant& vo ); -void from_variant( const variant& var, unsigned_int& vo ); +void to_variant( const signed_int& var, variant& vo, uint32_t max_depth = 1 ); +void from_variant( const variant& var, signed_int& vo, uint32_t max_depth = 1 ); +void to_variant( const unsigned_int& var, variant& vo, uint32_t max_depth = 1 ); +void from_variant( const variant& var, unsigned_int& vo, uint32_t max_depth = 1 ); } // namespace fc diff --git a/include/fc/network/ip.hpp b/include/fc/network/ip.hpp index 04c5030..7a55616 100644 --- a/include/fc/network/ip.hpp +++ b/include/fc/network/ip.hpp @@ -73,11 +73,11 @@ namespace fc { } class variant; - void to_variant( const ip::endpoint& var, variant& vo ); - void from_variant( const variant& var, ip::endpoint& vo ); + void to_variant( const ip::endpoint& var, variant& vo, uint32_t _max_depth = 2 ); + void from_variant( const variant& var, ip::endpoint& vo, uint32_t _max_depth = 2 ); - void to_variant( const ip::address& var, variant& vo ); - void from_variant( const variant& var, ip::address& vo ); + void to_variant( const ip::address& var, variant& vo, uint32_t _max_depth = 1 ); + void from_variant( const variant& var, ip::address& vo, uint32_t _max_depth = 1 ); namespace raw diff --git a/include/fc/network/url.hpp b/include/fc/network/url.hpp index 6f8c745..314aa29 100644 --- a/include/fc/network/url.hpp +++ b/include/fc/network/url.hpp @@ -55,8 +55,8 @@ namespace fc { std::shared_ptr my; }; - void to_variant( const url& u, fc::variant& v ); - void from_variant( const fc::variant& v, url& u ); + void to_variant( const url& u, fc::variant& v, uint32_t max_depth = 1 ); + void from_variant( const fc::variant& v, url& u, uint32_t max_depth = 1 ); /** * Used to create / manipulate a URL diff --git a/include/fc/real128.hpp b/include/fc/real128.hpp index 142f5ec..e445ab6 100644 --- a/include/fc/real128.hpp +++ b/include/fc/real128.hpp @@ -36,8 +36,8 @@ namespace fc { uint128 fixed; }; - void to_variant( const real128& var, variant& vo ); - void from_variant( const variant& var, real128& vo ); + void to_variant( const real128& var, variant& vo, uint32_t max_depth = 1 ); + void from_variant( const variant& var, real128& vo, uint32_t max_depth = 1 ); namespace raw { diff --git a/include/fc/reflect/variant.hpp b/include/fc/reflect/variant.hpp index 907b20e..1a91854 100644 --- a/include/fc/reflect/variant.hpp +++ b/include/fc/reflect/variant.hpp @@ -63,22 +63,22 @@ namespace fc const uint32_t _max_depth; }; - template - struct if_enum - { - template - static inline void to_variant( const T& v, fc::variant& vo, uint32_t max_depth ) - { + template + struct if_enum + { + template + static inline void to_variant( const T& v, fc::variant& vo, uint32_t max_depth ) + { mutable_variant_object mvo; fc::reflector::visit( to_variant_visitor( mvo, v, max_depth ) ); vo = fc::move(mvo); - } - template - static inline void from_variant( const fc::variant& v, T& o, uint32_t max_depth ) - { + } + template + static inline void from_variant( const fc::variant& v, T& o, uint32_t max_depth ) + { const variant_object& vo = v.get_object(); fc::reflector::visit( from_variant_visitor( vo, o, max_depth ) ); - } + } }; template<> diff --git a/include/fc/rpc/cli.hpp b/include/fc/rpc/cli.hpp index bb4975f..6670a14 100644 --- a/include/fc/rpc/cli.hpp +++ b/include/fc/rpc/cli.hpp @@ -16,6 +16,7 @@ namespace fc { namespace rpc { class cli : public api_connection { public: + cli( uint32_t max_depth ) : api_connection(max_depth) {} ~cli(); virtual variant send_call( api_id_type api_id, string method_name, variants args = variants() ); diff --git a/include/fc/static_variant.hpp b/include/fc/static_variant.hpp index d38408d..c9d5115 100644 --- a/include/fc/static_variant.hpp +++ b/include/fc/static_variant.hpp @@ -346,42 +346,45 @@ struct visitor { struct from_static_variant { variant& var; - from_static_variant( variant& dv ):var(dv){} + const uint32_t _max_depth; + from_static_variant( variant& dv, uint32_t max_depth ):var(dv),_max_depth(max_depth){} typedef void result_type; template void operator()( const T& v )const { - to_variant( v, var ); + to_variant( v, var, _max_depth ); } }; struct to_static_variant { const variant& var; - to_static_variant( const variant& dv ):var(dv){} + const uint32_t _max_depth; + to_static_variant( const variant& dv, uint32_t max_depth ):var(dv),_max_depth(max_depth){} typedef void result_type; template void operator()( T& v )const { - from_variant( var, v ); + from_variant( var, v, _max_depth ); } }; - template void to_variant( const fc::static_variant& s, fc::variant& v ) + template void to_variant( const fc::static_variant& s, fc::variant& v, uint32_t max_depth ) { - variant tmp; + FC_ASSERT( max_depth > 0 ); variants vars(2); vars[0] = s.which(); - s.visit( from_static_variant(vars[1]) ); + s.visit( from_static_variant(vars[1], max_depth - 1) ); v = std::move(vars); } - template void from_variant( const fc::variant& v, fc::static_variant& s ) + template void from_variant( const fc::variant& v, fc::static_variant& s, uint32_t max_depth ) { + FC_ASSERT( max_depth > 0 ); auto ar = v.get_array(); if( ar.size() < 2 ) return; s.set_which( ar[0].as_uint64() ); - s.visit( to_static_variant(ar[1]) ); + s.visit( to_static_variant(ar[1], max_depth - 1) ); } template struct get_typename { static const char* name() { return typeid(static_variant).name(); } }; diff --git a/include/fc/time.hpp b/include/fc/time.hpp index 11aa96d..c78c0f3 100644 --- a/include/fc/time.hpp +++ b/include/fc/time.hpp @@ -38,8 +38,8 @@ namespace fc { inline microseconds days(int64_t d) { return hours(24*d); } class variant; - void to_variant( const fc::microseconds&, fc::variant& ); - void from_variant( const fc::variant& , fc::microseconds& ); + void to_variant( const fc::microseconds&, fc::variant&, uint32_t max_depth = 1 ); + void from_variant( const fc::variant&, fc::microseconds&, uint32_t max_depth = 1 ); class time_point { public: diff --git a/include/fc/uint128.hpp b/include/fc/uint128.hpp index e2a72cc..9c49bd6 100644 --- a/include/fc/uint128.hpp +++ b/include/fc/uint128.hpp @@ -121,8 +121,8 @@ namespace fc class variant; - void to_variant( const uint128& var, variant& vo ); - void from_variant( const variant& var, uint128& vo ); + void to_variant( const uint128& var, variant& vo, uint32_t max_depth = 1 ); + void from_variant( const variant& var, uint128& vo, uint32_t max_depth = 1 ); namespace raw { diff --git a/include/fc/variant.hpp b/include/fc/variant.hpp index 7dc453a..e9a55c1 100644 --- a/include/fc/variant.hpp +++ b/include/fc/variant.hpp @@ -38,8 +38,8 @@ namespace fc * for your Serializable_type * * @code - * void to_variant( const Serializable_type& e, variant& v ); - * void from_variant( const variant& e, Serializable_type& ll ); + * void to_variant( const Serializable_type& e, variant& v, uint32_t max_depth ); + * void from_variant( const variant& e, Serializable_type& ll, uint32_t max_depth ); * @endcode */ @@ -89,6 +89,9 @@ namespace fc /** @ingroup Serializable */ void from_variant( const variant& var, int32_t& vo, uint32_t max_depth = 1 ); + void to_variant( const uint64_t& var, variant& vo, uint32_t max_depth = 1 ); + void to_variant( const int64_t& var, variant& vo, uint32_t max_depth = 1 ); + void to_variant( const variant_object& var, variant& vo, uint32_t max_depth ); void from_variant( const variant& var, variant_object& vo, uint32_t max_depth ); void to_variant( const mutable_variant_object& var, variant& vo, uint32_t max_depth ); @@ -146,7 +149,7 @@ namespace fc void from_variant( const variant& input_variant, microseconds& output_microseconds, uint32_t max_depth ); #ifdef __APPLE__ - void to_variant( size_t s, variant& v ); + void to_variant( size_t s, variant& v, uint32_t max_depth = 1 ); #elif !defined(_MSC_VER) void to_variant( long long int s, variant& v, uint32_t max_depth = 1 ); void to_variant( unsigned long long int s, variant& v, uint32_t max_depth = 1 ); @@ -301,7 +304,7 @@ namespace fc * following method to implement conversion from variant to T. * * - * void from_variant( const Variant& var, T& val ) + * void from_variant( const Variant& var, T& val, uint32_t max_depth ) * * * The above form is not always convienant, so the this templated @@ -558,7 +561,7 @@ namespace fc to_variant( val, *this, max_depth ); } #ifdef __APPLE__ - inline void to_variant( size_t s, variant& v ) { v = variant(uint64_t(s)); } + inline void to_variant( size_t s, variant& v, uint32_t max_depth = 1 ) { v = variant(uint64_t(s)); } #endif template void to_variant( const std::shared_ptr& var, variant& vo, uint32_t max_depth ) diff --git a/include/fc/variant_object.hpp b/include/fc/variant_object.hpp index dc72b9e..aa1d5b9 100644 --- a/include/fc/variant_object.hpp +++ b/include/fc/variant_object.hpp @@ -90,9 +90,9 @@ namespace fc friend class mutable_variant_object; }; /** @ingroup Serializable */ - void to_variant( const variant_object& var, variant& vo ); + void to_variant( const variant_object& var, variant& vo, uint32_t max_depth = 1 ); /** @ingroup Serializable */ - void from_variant( const variant& var, variant_object& vo ); + void from_variant( const variant& var, variant_object& vo, uint32_t max_depth = 1 ); /** @@ -229,6 +229,7 @@ namespace fc set( std::move(key), variant( fc::forward(var), _max_depth ) ); return *this; } + limited_mutable_variant_object& operator()( const variant_object& vo ); private: const uint32_t _max_depth; diff --git a/src/crypto/bigint.cpp b/src/crypto/bigint.cpp index 5bc96d4..478f949 100644 --- a/src/crypto/bigint.cpp +++ b/src/crypto/bigint.cpp @@ -128,14 +128,14 @@ namespace fc { } bigint bigint::operator / ( const bigint& a ) const { BN_CTX* ctx = BN_CTX_new(); - bigint tmp;//(*this); + bigint tmp; BN_div( tmp.n, NULL, n, a.n, ctx ); BN_CTX_free(ctx); return tmp; } bigint bigint::operator % ( const bigint& a ) const { BN_CTX* ctx = BN_CTX_new(); - bigint tmp;//(*this); + bigint tmp; BN_mod( tmp.n, n, a.n, ctx ); BN_CTX_free(ctx); return tmp; @@ -143,7 +143,7 @@ namespace fc { bigint bigint::operator /= ( const bigint& a ) { BN_CTX* ctx = BN_CTX_new(); - bigint tmp;//*this); + bigint tmp; BN_div( tmp.n, NULL, n, a.n, ctx ); fc_swap( tmp.n, n ); BN_CTX_free(ctx); @@ -188,6 +188,8 @@ namespace fc { bigint& bigint::operator = ( bigint&& a ) { + if( &a == this ) + return *this; fc_swap( a.n, n ); return *this; } @@ -208,14 +210,14 @@ namespace fc { } /** encodes the big int as base64 string, or a number */ - void to_variant( const bigint& bi, variant& v ) + void to_variant( const bigint& bi, variant& v, uint32_t max_depth ) { std::vector ve = bi; v = fc::variant(base64_encode((unsigned char*)ve.data(),ve.size())); } /** decodes the big int as base64 string, or a number */ - void from_variant( const variant& v, bigint& bi ) + void from_variant( const variant& v, bigint& bi, uint32_t max_depth ) { if( v.is_numeric() ) bi = bigint( static_cast(v.as_uint64()) ); else diff --git a/src/crypto/pke.cpp b/src/crypto/pke.cpp index a425621..3ec0199 100644 --- a/src/crypto/pke.cpp +++ b/src/crypto/pke.cpp @@ -338,28 +338,28 @@ namespace fc { } /** encodes the big int as base64 string, or a number */ - void to_variant( const public_key& bi, variant& v ) + void to_variant( const public_key& bi, variant& v, uint32_t max_depth ) { v = bi.serialize(); } /** decodes the big int as base64 string, or a number */ - void from_variant( const variant& v, public_key& bi ) + void from_variant( const variant& v, public_key& bi, uint32_t max_depth ) { - bi = public_key( v.as >() ); + bi = public_key( v.as >(max_depth) ); } /** encodes the big int as base64 string, or a number */ - void to_variant( const private_key& bi, variant& v ) + void to_variant( const private_key& bi, variant& v, uint32_t max_depth ) { v = bi.serialize(); } /** decodes the big int as base64 string, or a number */ - void from_variant( const variant& v, private_key& bi ) + void from_variant( const variant& v, private_key& bi, uint32_t max_depth ) { - bi = private_key( v.as >() ); + bi = private_key( v.as >(max_depth) ); } } // fc diff --git a/src/io/varint.cpp b/src/io/varint.cpp index a3eaac2..6d5df6c 100644 --- a/src/io/varint.cpp +++ b/src/io/varint.cpp @@ -3,8 +3,8 @@ namespace fc { -void to_variant( const signed_int& var, variant& vo ) { vo = var.value; } -void from_variant( const variant& var, signed_int& vo ) { vo.value = static_cast(var.as_int64()); } -void to_variant( const unsigned_int& var, variant& vo ) { vo = var.value; } -void from_variant( const variant& var, unsigned_int& vo ) { vo.value = static_cast(var.as_uint64()); } +void to_variant( const signed_int& var, variant& vo, uint32_t max_depth ) { vo = var.value; } +void from_variant( const variant& var, signed_int& vo, uint32_t max_depth ) { vo.value = static_cast(var.as_int64()); } +void to_variant( const unsigned_int& var, variant& vo, uint32_t max_depth ) { vo = var.value; } +void from_variant( const variant& var, unsigned_int& vo, uint32_t max_depth ) { vo.value = static_cast(var.as_uint64()); } } diff --git a/src/network/ip.cpp b/src/network/ip.cpp index 1961659..570a621 100644 --- a/src/network/ip.cpp +++ b/src/network/ip.cpp @@ -137,20 +137,20 @@ namespace fc { namespace ip { } // namespace ip - void to_variant( const ip::endpoint& var, variant& vo ) + void to_variant( const ip::endpoint& var, variant& vo, uint32_t max_depth ) { vo = fc::string(var); } - void from_variant( const variant& var, ip::endpoint& vo ) + void from_variant( const variant& var, ip::endpoint& vo, uint32_t max_depth ) { vo = ip::endpoint::from_string(var.as_string()); } - void to_variant( const ip::address& var, variant& vo ) + void to_variant( const ip::address& var, variant& vo, uint32_t max_depth ) { vo = fc::string(var); } - void from_variant( const variant& var, ip::address& vo ) + void from_variant( const variant& var, ip::address& vo, uint32_t max_depth ) { vo = ip::address(var.as_string()); } diff --git a/src/network/url.cpp b/src/network/url.cpp index 635dd2d..73d0536 100644 --- a/src/network/url.cpp +++ b/src/network/url.cpp @@ -76,11 +76,11 @@ namespace fc }; } - void to_variant( const url& u, fc::variant& v ) + void to_variant( const url& u, fc::variant& v, uint32_t max_depth ) { v = fc::string(u); } - void from_variant( const fc::variant& v, url& u ) + void from_variant( const fc::variant& v, url& u, uint32_t max_depth ) { u = url( v.as_string() ); } @@ -99,7 +99,6 @@ namespace fc if( my->_host.valid() ) ss<<*my->_host; if( my->_port.valid() ) ss<<":"<<*my->_port; if( my->_path.valid() ) ss<_path->generic_string(); - // if( my->_args ) ss<<"?"<<*my->_args; return ss.str(); } diff --git a/src/real128.cpp b/src/real128.cpp index c83336e..f941359 100644 --- a/src/real128.cpp +++ b/src/real128.cpp @@ -118,11 +118,11 @@ namespace fc return result; } - void to_variant( const real128& var, variant& vo ) + void to_variant( const real128& var, variant& vo, uint32_t max_depth ) { vo = std::string(var); } - void from_variant( const variant& var, real128& vo ) + void from_variant( const variant& var, real128& vo, uint32_t max_depth ) { vo = real128(var.as_string()); } diff --git a/src/uint128.cpp b/src/uint128.cpp index 66128ce..5b12a4d 100644 --- a/src/uint128.cpp +++ b/src/uint128.cpp @@ -13,7 +13,7 @@ namespace fc template static void divide(const T &numerator, const T &denominator, T "ient, T &remainder) { - static const int bits = sizeof(T) * 8;//CHAR_BIT; + static const int bits = sizeof(T) * 8; if(denominator == 0) { throw std::domain_error("divide by zero"); @@ -228,22 +228,6 @@ namespace fc self /= other; hi = static_cast(self >> 64); lo = static_cast((self << 64 ) >> 64); - - /* - uint128 remainder; - divide(*this, b, *this, remainder ); //, *this); - if( tmp.hi != hi || tmp.lo != lo ) { - std::cerr << tmp.hi << " " << hi <<"\n"; - std::cerr << tmp.lo << " " << lo << "\n"; - exit(1); - } - */ - - /* - const auto& b128 = std::reinterpret_cast(b); - auto& this128 = std::reinterpret_cast(*this); - this128 /= b128; - */ return *this; } @@ -344,8 +328,6 @@ namespace fc result_hi = uint128( y[3], y[2] ); result_lo = uint128( y[1], y[0] ); - - return; } static uint8_t _popcount_64( uint64_t x ) @@ -374,8 +356,14 @@ namespace fc return _popcount_64( lo ) + _popcount_64( hi ); } - void to_variant( const uint128& var, variant& vo ) { vo = std::string(var); } - void from_variant( const variant& var, uint128& vo ){ vo = uint128(var.as_string()); } + void to_variant( const uint128& var, variant& vo, uint32_t max_depth ) + { + vo = std::string(var); + } + void from_variant( const variant& var, uint128& vo, uint32_t max_depth ) + { + vo = uint128(var.as_string()); + } } // namespace fc diff --git a/src/variant.cpp b/src/variant.cpp index c8e0493..ef28bc5 100644 --- a/src/variant.cpp +++ b/src/variant.cpp @@ -579,11 +579,6 @@ void from_variant( const variant& var, variants& vo, uint32_t max_depth ) vo = var.get_array(); } -//void from_variant( const variant& var, variant_object& vo ) -//{ -// vo = var.get_object(); -//} - void from_variant( const variant& var, variant& vo, uint32_t max_depth ) { vo = var; } void to_variant( const uint8_t& var, variant& vo, uint32_t max_depth ) { vo = uint64_t(var); } @@ -614,11 +609,13 @@ void from_variant( const variant& var,int32_t& vo, uint32_t max_depth ) vo = static_cast(var.as_int64()); } +void to_variant( const int64_t& var, variant& vo, uint32_t max_depth ) { vo = var; } void from_variant( const variant& var, int64_t& vo, uint32_t max_depth ) { vo = var.as_int64(); } +void to_variant( const uint64_t& var, variant& vo, uint32_t max_depth ) { vo = var; } void from_variant( const variant& var, uint64_t& vo, uint32_t max_depth ) { vo = var.as_uint64(); diff --git a/src/variant_object.cpp b/src/variant_object.cpp index 95b3118..55c3181 100644 --- a/src/variant_object.cpp +++ b/src/variant_object.cpp @@ -103,7 +103,6 @@ namespace fc variant_object::variant_object( string key, variant val ) : _key_value(std::make_shared>()) { - //_key_value->push_back(entry(fc::move(key), fc::move(val))); _key_value->emplace_back(entry(fc::move(key), fc::move(val))); } @@ -367,14 +366,23 @@ namespace fc } limited_mutable_variant_object::limited_mutable_variant_object( uint32_t m ) - : mutable_variant_object(), _max_depth(m) {} + : mutable_variant_object(), _max_depth(m - 1) + { + FC_ASSERT( m > 0, "Recursion depth exceeded!" ); + } - void to_variant( const mutable_variant_object& var, variant& vo ) + limited_mutable_variant_object& limited_mutable_variant_object::operator()( const variant_object& vo ) + { + mutable_variant_object::operator()( vo ); + return *this; + } + + void to_variant( const mutable_variant_object& var, variant& vo, uint32_t max_depth ) { vo = variant(var); } - void from_variant( const variant& var, mutable_variant_object& vo ) + void from_variant( const variant& var, mutable_variant_object& vo, uint32_t max_depth ) { vo = var.get_object(); }