From a0f41aa397d950c329eb80c11f2c30faf8788924 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Wed, 17 Jul 2013 02:01:35 -0400 Subject: [PATCH] various updates and enhancements to crypto and network code --- include/fc/crypto/bigint.hpp | 2 ++ include/fc/io/raw.hpp | 32 +++++++++++++++++++++++++++ include/fc/network/ip.hpp | 43 +++++++++++++++++++++++++++++++----- src/crypto/bigint.cpp | 10 +++++++++ src/network/ip.cpp | 14 ++++++++++++ src/variant.cpp | 17 ++++++++++---- 6 files changed, 108 insertions(+), 10 deletions(-) diff --git a/include/fc/crypto/bigint.hpp b/include/fc/crypto/bigint.hpp index 91639d5..36ec1a3 100644 --- a/include/fc/crypto/bigint.hpp +++ b/include/fc/crypto/bigint.hpp @@ -41,6 +41,8 @@ namespace fc { bigint operator / ( const bigint& a )const; bigint operator % ( const bigint& a )const; bigint operator /= ( const bigint& a ); + bigint operator *= ( const bigint& a ); + bigint& operator <<= ( uint32_t i ); bigint operator - ( const bigint& a )const; diff --git a/include/fc/io/raw.hpp b/include/fc/io/raw.hpp index 5163036..2f6375c 100644 --- a/include/fc/io/raw.hpp +++ b/include/fc/io/raw.hpp @@ -13,6 +13,9 @@ #include #include #include +#include + +#define MAX_ARRAY_ALLOC_SIZE (1024*1024*10) namespace fc { namespace raw { @@ -21,6 +24,10 @@ namespace fc { inline void pack( Stream& s, const std::set& value ); template inline void unpack( Stream& s, std::set& value ); + template + inline void pack( Stream& s, const std::unordered_set& value ); + template + inline void unpack( Stream& s, std::unordered_set& value ); template inline void pack( Stream& s, const variant_object& v ); @@ -121,6 +128,7 @@ namespace fc { } template inline void unpack( Stream& s, std::vector& value ) { unsigned_int size; unpack( s, size ); + FC_ASSERT( size.value < MAX_ARRAY_ALLOC_SIZE ); value.resize(size.value); if( value.size() ) s.read( value.data(), value.size() ); @@ -218,6 +226,29 @@ namespace fc { } // namesapce detail + template + inline void pack( Stream& s, const std::unordered_set& value ) { + pack( s, unsigned_int(value.size()) ); + auto itr = value.begin(); + auto end = value.end(); + while( itr != end ) { + fc::raw::pack( s, *itr ); + ++itr; + } + } + template + inline void unpack( Stream& s, std::unordered_set& value ) { + unsigned_int size; unpack( s, size ); + value.clear(); + FC_ASSERT( size.value*sizeof(T) < MAX_ARRAY_ALLOC_SIZE ); + value.reserve(size.value); + for( uint32_t i = 0; i < size.value; ++i ) + { + T tmp; + fc::raw::unpack( s, tmp ); + value.insert( std::move(tmp) ); + } + } template inline void pack( Stream& s, const std::vector& value ) { @@ -233,6 +264,7 @@ namespace fc { template inline void unpack( Stream& s, std::vector& value ) { unsigned_int size; unpack( s, size ); + FC_ASSERT( size.value*sizeof(T) < MAX_ARRAY_ALLOC_SIZE ); value.resize(size.value); auto itr = value.begin(); auto end = value.end(); diff --git a/include/fc/network/ip.hpp b/include/fc/network/ip.hpp index 555d468..56ee2e6 100644 --- a/include/fc/network/ip.hpp +++ b/include/fc/network/ip.hpp @@ -1,6 +1,7 @@ #pragma once #include #include +#include namespace fc { @@ -15,6 +16,7 @@ namespace fc { operator uint32_t()const; friend bool operator==( const address& a, const address& b ); + friend bool operator!=( const address& a, const address& b ); private: uint32_t _ip; }; @@ -34,12 +36,8 @@ namespace fc { const address& get_address()const; friend bool operator==( const endpoint& a, const endpoint& b ); - friend bool operator< ( const endpoint& a, const endpoint& b ) - { - return uint32_t(a.get_address()) < uint32_t(b.get_address()) || - (uint32_t(a.get_address()) == uint32_t(b.get_address()) && - uint32_t(a.port()) < uint32_t(b.port())); - } + friend bool operator!=( const endpoint& a, const endpoint& b ); + friend bool operator< ( const endpoint& a, const endpoint& b ); private: /** @@ -59,6 +57,39 @@ namespace fc { void to_variant( const ip::address& var, variant& vo ); void from_variant( const variant& var, ip::address& vo ); + + namespace raw + { + template + inline void pack( Stream& s, const ip::address& v ) + { + fc::raw::pack( s, uint32_t(v) ); + } + template + inline void unpack( Stream& s, ip::address& v ) + { + uint32_t _ip; + fc::raw::unpack( s, _ip ); + v = ip::address(_ip); + } + + template + inline void pack( Stream& s, const ip::endpoint& v ) + { + fc::raw::pack( s, v.get_address() ); + fc::raw::pack( s, v.port() ); + } + template + inline void unpack( Stream& s, ip::endpoint& v ) + { + ip::address a; + uint16_t p; + fc::raw::unpack( s, a ); + fc::raw::unpack( s, p ); + v = ip::endpoint(a,p); + } + + } } namespace std { diff --git a/src/crypto/bigint.cpp b/src/crypto/bigint.cpp index 50355d1..ec89528 100644 --- a/src/crypto/bigint.cpp +++ b/src/crypto/bigint.cpp @@ -118,7 +118,17 @@ namespace fc { BN_CTX_free(ctx); return tmp; } + bigint bigint::operator *= ( const bigint& a ) { + auto tmp = *this * a; + *this = std::move(tmp); + return *this; + } + bigint& bigint::operator <<= ( uint32_t i ) + { + BN_lshift( n, n, i ); + return *this; + } bigint bigint::operator - ( const bigint& a )const { bigint tmp; diff --git a/src/network/ip.cpp b/src/network/ip.cpp index 26c628a..117394b 100644 --- a/src/network/ip.cpp +++ b/src/network/ip.cpp @@ -16,6 +16,9 @@ namespace fc { namespace ip { bool operator==( const address& a, const address& b ) { return uint32_t(a) == uint32_t(b); } + bool operator!=( const address& a, const address& b ) { + return uint32_t(a) != uint32_t(b); + } address& address::operator=( const fc::string& s ) { _ip = boost::asio::ip::address_v4::from_string(s.c_str()).to_ulong(); @@ -38,6 +41,17 @@ namespace fc { namespace ip { bool operator==( const endpoint& a, const endpoint& b ) { return a._port == b._port && a._ip == b._ip; } + bool operator!=( const endpoint& a, const endpoint& b ) { + return a._port != b._port || a._ip != b._ip; + } + + bool operator< ( const endpoint& a, const endpoint& b ) + { + return uint32_t(a.get_address()) < uint32_t(b.get_address()) || + (uint32_t(a.get_address()) == uint32_t(b.get_address()) && + uint32_t(a.port()) < uint32_t(b.port())); + } + uint16_t endpoint::port()const { return _port; } const address& endpoint::get_address()const { return _ip; } diff --git a/src/variant.cpp b/src/variant.cpp index 8adab43..3aabb00 100644 --- a/src/variant.cpp +++ b/src/variant.cpp @@ -5,7 +5,8 @@ #include #include #include -#include +//#include +#include #include namespace fc @@ -17,13 +18,21 @@ namespace fc void to_variant( const std::vector& var, variant& vo ) { if( var.size() ) - vo = variant(base64_encode((unsigned char*)var.data(),var.size())); + //vo = variant(base64_encode((unsigned char*)var.data(),var.size())); + vo = variant(to_hex(var.data(),var.size())); else vo = ""; } void from_variant( const variant& var, std::vector& vo ) { - std::string b64 = base64_decode( var.as_string() ); - vo = std::vector( b64.c_str(), b64.c_str() + b64.size() ); + auto str = var.as_string(); + vo.resize( str.size() / 2 ); + if( vo.size() ) + { + size_t r = from_hex( str, vo.data(), vo.size() ); + FC_ASSERT( r = vo.size() ); + } +// std::string b64 = base64_decode( var.as_string() ); +// vo = std::vector( b64.c_str(), b64.c_str() + b64.size() ); } /** * The TypeID is stored in the 'last byte' of the variant.