From 579914c84d7bf7dd5a57ad890b4690c033fac697 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Thu, 4 Apr 2019 13:46:38 +0200 Subject: [PATCH 01/11] Removed unused stuff --- CMakeLists.txt | 3 - fc.natvis | 8 +- include/fc/actor.hpp | 65 --- include/fc/aligned.hpp | 14 - include/fc/any.hpp | 7 - include/fc/api.hpp | 6 +- include/fc/array.hpp | 2 +- include/fc/asio.hpp | 4 +- include/fc/compress/zlib.hpp | 4 +- include/fc/container/deque_fwd.hpp | 14 - include/fc/crypto/base36.hpp | 9 +- include/fc/crypto/base58.hpp | 2 +- include/fc/crypto/bigint.hpp | 6 +- include/fc/crypto/elliptic.hpp | 14 +- include/fc/crypto/hex.hpp | 8 +- include/fc/crypto/sha1.hpp | 10 +- include/fc/crypto/sha224.hpp | 1 - include/fc/crypto/sha256.hpp | 1 - include/fc/crypto/sha512.hpp | 1 - include/fc/exception/exception.hpp | 4 +- include/fc/filesystem.hpp | 33 -- include/fc/fixed_string.hpp | 143 ------ include/fc/fwd.hpp | 3 +- include/fc/fwd_impl.hpp | 15 +- include/fc/interprocess/container.hpp | 108 ----- include/fc/interprocess/file_mapping.hpp | 5 +- include/fc/interprocess/file_mutex.hpp | 44 -- include/fc/interprocess/iprocess.hpp | 67 --- include/fc/interprocess/mmap_struct.hpp | 59 --- include/fc/interprocess/process.hpp | 37 -- include/fc/io/console.hpp | 8 - include/fc/io/iobuffer.hpp | 81 ---- include/fc/io/iostream.hpp | 8 +- include/fc/io/json.hpp | 2 +- include/fc/io/json_relaxed.hpp | 21 +- include/fc/io/raw.hpp | 12 +- include/fc/io/raw_fwd.hpp | 9 +- include/fc/io/raw_unpack_file.hpp | 24 - include/fc/io/raw_variant.hpp | 14 +- include/fc/io/sstream.hpp | 8 +- include/fc/log/appender.hpp | 10 +- include/fc/log/console_appender.hpp | 2 +- include/fc/log/file_appender.hpp | 2 +- include/fc/log/log_message.hpp | 21 +- include/fc/log/logger.hpp | 7 +- include/fc/log/logger_config.hpp | 4 +- include/fc/make_fused.hpp | 26 -- include/fc/network/http/connection.hpp | 25 +- include/fc/network/http/server.hpp | 2 +- include/fc/network/http/websocket.hpp | 8 +- include/fc/network/ip.hpp | 7 +- include/fc/network/resolve.hpp | 1 - include/fc/network/tcp_socket.hpp | 1 - include/fc/network/udp_socket.hpp | 1 - include/fc/network/url.hpp | 3 +- include/fc/noncopyable.hpp | 14 - include/fc/optional.hpp | 12 +- include/fc/reflect/reflect.hpp | 7 +- include/fc/reflect/typename.hpp | 5 +- include/fc/reflect/variant.hpp | 2 +- include/fc/rpc/api_connection.hpp | 4 +- include/fc/rpc/binary_api_connection.hpp | 532 ----------------------- include/fc/rpc/bstate.hpp | 4 +- include/fc/rpc/cli.hpp | 2 +- include/fc/rpc/json_connection.hpp | 74 ++-- include/fc/rpc/state.hpp | 6 +- include/fc/rpc/variant_connection.hpp | 140 ------ include/fc/rpc/variant_stream.hpp | 36 -- include/fc/scoped_exit.hpp | 32 -- include/fc/shared_ptr.hpp | 6 +- include/fc/signal.hpp | 130 ------ include/fc/string.hpp | 154 +------ include/fc/thread/future.hpp | 17 +- include/fc/thread/task.hpp | 5 +- include/fc/thread/thread.hpp | 6 +- include/fc/thread/wait_condition.hpp | 76 ---- include/fc/time.hpp | 26 +- include/fc/tuple.hpp | 136 ------ include/fc/unique_ptr.hpp | 60 --- include/fc/utility.hpp | 24 - include/fc/variant.hpp | 12 +- include/fc/variant_object.hpp | 1 - include/fc/vector.hpp | 2 - include/fc/vector_fwd.hpp | 11 - include/fc/wait_any.hpp | 13 - src/compress/zlib.cpp | 4 +- src/crypto/base36.cpp | 13 +- src/crypto/base58.cpp | 1 - src/crypto/bigint.cpp | 7 +- src/crypto/elliptic_common.cpp | 14 +- src/crypto/hex.cpp | 6 +- src/crypto/pke.cpp | 18 +- src/crypto/ripemd160.cpp | 2 +- src/crypto/sha1.cpp | 2 +- src/crypto/sha224.cpp | 2 +- src/crypto/sha256.cpp | 2 +- src/crypto/sha512.cpp | 2 +- src/exception.cpp | 14 +- src/filesystem.cpp | 110 +---- src/interprocess/file_mutex.cpp | 100 ----- src/interprocess/mmap_struct.cpp | 44 -- src/io/buffered_iostream.cpp | 16 +- src/io/console.cpp | 46 -- src/io/datastream.cpp | 2 +- src/io/iostream.cpp | 20 +- src/io/json.cpp | 22 +- src/io/sstream.cpp | 18 +- src/log/appender.cpp | 6 +- src/log/console_appender.cpp | 3 +- src/log/file_appender.cpp | 2 +- src/log/log_message.cpp | 4 +- src/log/logger.cpp | 12 +- src/network/http/http_connection.cpp | 30 +- src/network/http/http_server.cpp | 10 +- src/network/ip.cpp | 12 +- src/network/resolve.cpp | 2 +- src/network/url.cpp | 35 +- src/rpc/bstate.cpp | 6 +- src/rpc/cli.cpp | 2 +- src/rpc/json_connection.cpp | 54 +-- src/rpc/state.cpp | 6 +- src/string.cpp | 81 +--- src/thread/thread.cpp | 9 +- src/thread/thread_d.hpp | 7 +- src/time.cpp | 19 +- src/variant.cpp | 14 +- src/variant_object.cpp | 32 +- tests/CMakeLists.txt | 3 - tests/bip_lock.cpp | 44 -- tests/crypto/base_n_tests.cpp | 16 +- tests/hmac_test.cpp | 64 +-- tests/io/stream_tests.cpp | 8 +- tests/logging_tests.cpp | 2 +- 133 files changed, 494 insertions(+), 3007 deletions(-) delete mode 100644 include/fc/actor.hpp delete mode 100644 include/fc/aligned.hpp delete mode 100644 include/fc/any.hpp delete mode 100644 include/fc/container/deque_fwd.hpp delete mode 100644 include/fc/fixed_string.hpp delete mode 100644 include/fc/interprocess/container.hpp delete mode 100644 include/fc/interprocess/file_mutex.hpp delete mode 100644 include/fc/interprocess/iprocess.hpp delete mode 100644 include/fc/interprocess/mmap_struct.hpp delete mode 100644 include/fc/interprocess/process.hpp delete mode 100644 include/fc/io/console.hpp delete mode 100644 include/fc/io/iobuffer.hpp delete mode 100644 include/fc/io/raw_unpack_file.hpp delete mode 100644 include/fc/make_fused.hpp delete mode 100644 include/fc/noncopyable.hpp delete mode 100644 include/fc/rpc/binary_api_connection.hpp delete mode 100644 include/fc/rpc/variant_connection.hpp delete mode 100644 include/fc/rpc/variant_stream.hpp delete mode 100644 include/fc/scoped_exit.hpp delete mode 100644 include/fc/signal.hpp delete mode 100644 include/fc/thread/wait_condition.hpp delete mode 100644 include/fc/tuple.hpp delete mode 100644 include/fc/unique_ptr.hpp delete mode 100644 include/fc/vector.hpp delete mode 100644 include/fc/vector_fwd.hpp delete mode 100644 include/fc/wait_any.hpp delete mode 100644 src/interprocess/file_mutex.cpp delete mode 100644 src/interprocess/mmap_struct.cpp delete mode 100644 src/io/console.cpp delete mode 100644 tests/bip_lock.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ebf8d16..fbf1ca0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -233,12 +233,9 @@ set( fc_sources src/io/sstream.cpp src/io/json.cpp src/io/varint.cpp - src/io/console.cpp src/filesystem.cpp src/interprocess/signals.cpp src/interprocess/file_mapping.cpp - src/interprocess/mmap_struct.cpp - src/interprocess/file_mutex.cpp src/rpc/cli.cpp src/rpc/http_api.cpp src/rpc/json_connection.cpp diff --git a/fc.natvis b/fc.natvis index 7188bfa..81d74d6 100644 --- a/fc.natvis +++ b/fc.natvis @@ -31,12 +31,6 @@ - - {my} - - my - - invalid @@ -54,4 +48,4 @@ - \ No newline at end of file + diff --git a/include/fc/actor.hpp b/include/fc/actor.hpp deleted file mode 100644 index 920d742..0000000 --- a/include/fc/actor.hpp +++ /dev/null @@ -1,65 +0,0 @@ -#pragma once -#include -#include - -namespace fc { - - namespace detail { - struct actor_member { - #if 1 // BOOST_NO_VARIADIC_TEMPLATES - #define RPC_MEMBER_FUNCTOR(z,n,IS_CONST) \ - template \ - static std::function( BOOST_PP_ENUM_PARAMS(n,A) ) > \ - functor( P p, R (C::*mem_func)(BOOST_PP_ENUM_PARAMS(n,A)) IS_CONST, fc::thread* c = 0) { \ - return [=](BOOST_PP_ENUM_BINARY_PARAMS(n,A,a))->fc::future{ \ - return c->async( [=](){ return (p->*mem_func)(BOOST_PP_ENUM_PARAMS(n,a)); } ); }; \ - } - BOOST_PP_REPEAT( 8, RPC_MEMBER_FUNCTOR, const ) - BOOST_PP_REPEAT( 8, RPC_MEMBER_FUNCTOR, BOOST_PP_EMPTY() ) - #undef RPC_MEMBER_FUNCTOR - - #else // g++ has a bug that prevents lambdas and varidic templates from working together (G++ Bug 41933) - - template - static std::function(Args...)> functor( P&& p, R (C::*mem_func)(Args...), fc::thread* c ) { - return [=](Args... args)->fc::future{ c->async( [=]()->R { return p->*mem_func( fc::forward(args)... ); } ) }; - } - template - static std::function(Args...)> functor( P&& p, R (C::*mem_func)(Args...)const, fc::thread* c ){ - return [=](Args... args)->fc::future{ c->async( [=]()->R { return p->*mem_func( fc::forward(args)... ); } ) }; - } - #endif - }; - - template - struct actor_vtable_visitor { - template - actor_vtable_visitor( fc::thread* t, U&& u ):_thread(t),_this( fc::forward(u) ){} - - template - void operator()( const char* name, Function& memb, MemberPtr m )const { - memb = actor_member::functor( _this, m, _thread ); - } - fc::thread* _thread; - ThisPtr _this; - }; - } - - /** - * Posts all method calls to another thread and - * returns a future. - */ - template - class actor : public api { - public: - actor(){} - - template - actor( InterfaceType* p, fc::thread* t = &fc::thread::current() ) - { - this->_vtable.reset(new detail::vtable() ); - this->_vtable->template visit( detail::actor_vtable_visitor(t, p) ); - } - }; - -} // namespace fc diff --git a/include/fc/aligned.hpp b/include/fc/aligned.hpp deleted file mode 100644 index b107ed1..0000000 --- a/include/fc/aligned.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once -namespace fc { - - template - struct aligned { - union { - T _align; - char _data[S]; - } _store; - operator char*() { return _store._data; } - operator const char*()const { return _store._data; } - }; - -} diff --git a/include/fc/any.hpp b/include/fc/any.hpp deleted file mode 100644 index 022d29e..0000000 --- a/include/fc/any.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once -#include - -namespace fc { - // TODO: define this without using boost - typedef boost::any any; -} diff --git a/include/fc/api.hpp b/include/fc/api.hpp index 06414b1..3f68750 100644 --- a/include/fc/api.hpp +++ b/include/fc/api.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include #include +#include #include // ms visual c++ (as of 2013) doesn't accept the standard syntax for calling a @@ -75,7 +75,7 @@ namespace fc { api( const T& p ) :_vtable( std::make_shared() ) { - _data = std::make_shared(p); + _data = std::make_shared(p); T& ptr = boost::any_cast(*_data); auto& pointed_at = *ptr; typedef typename std::remove_reference::type source_vtable_type; @@ -95,7 +95,7 @@ namespace fc { protected: std::shared_ptr _vtable; - std::shared_ptr _data; + std::shared_ptr _data; }; } // namespace fc diff --git a/include/fc/array.hpp b/include/fc/array.hpp index 541cd69..687b175 100644 --- a/include/fc/array.hpp +++ b/include/fc/array.hpp @@ -112,7 +112,7 @@ namespace fc { std::vector ve = v.as< std::vector >( 1 ); if( ve.size() ) { - memcpy(&bi, ve.data(), fc::min(ve.size(),sizeof(bi)) ); + memcpy(&bi, ve.data(), std::min(ve.size(),sizeof(bi)) ); } else memset( &bi, char(0), sizeof(bi) ); diff --git a/include/fc/asio.hpp b/include/fc/asio.hpp index 3d11a38..06ba2f8 100644 --- a/include/fc/asio.hpp +++ b/include/fc/asio.hpp @@ -282,7 +282,7 @@ namespace asio { { public: istream( std::shared_ptr str ) - :_stream( fc::move(str) ){} + :_stream( std::move(str) ){} virtual size_t readsome( char* buf, size_t len ) { @@ -302,7 +302,7 @@ namespace asio { { public: ostream( std::shared_ptr str ) - :_stream( fc::move(str) ){} + :_stream( std::move(str) ){} virtual size_t writesome( const char* buf, size_t len ) { diff --git a/include/fc/compress/zlib.hpp b/include/fc/compress/zlib.hpp index 10a3a0e..cd08ccf 100644 --- a/include/fc/compress/zlib.hpp +++ b/include/fc/compress/zlib.hpp @@ -1,10 +1,10 @@ #pragma once -#include +#include namespace fc { - string zlib_compress(const string& in); +std::string zlib_compress(const std::string& in); } // namespace fc diff --git a/include/fc/container/deque_fwd.hpp b/include/fc/container/deque_fwd.hpp deleted file mode 100644 index 71df18d..0000000 --- a/include/fc/container/deque_fwd.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include -#include - -namespace fc { - - namespace raw { - template - void pack( Stream& s, const std::deque& value, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); - template - void unpack( Stream& s, std::deque& value, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); - } -} // namespace fc diff --git a/include/fc/crypto/base36.hpp b/include/fc/crypto/base36.hpp index aeb9c35..1cd158e 100644 --- a/include/fc/crypto/base36.hpp +++ b/include/fc/crypto/base36.hpp @@ -1,10 +1,9 @@ #pragma once -#include -#include +#include namespace fc { - std::vector from_base36( const fc::string& b36 ); - fc::string to_base36( const std::vector& vec ); - fc::string to_base36( const char* data, size_t len ); + std::vector from_base36( const std::string& b36 ); + std::string to_base36( const std::vector& vec ); + std::string to_base36( const char* data, size_t len ); } diff --git a/include/fc/crypto/base58.hpp b/include/fc/crypto/base58.hpp index 2f4fb38..17d52ea 100644 --- a/include/fc/crypto/base58.hpp +++ b/include/fc/crypto/base58.hpp @@ -1,5 +1,5 @@ #pragma once -#include +#include #include namespace fc { diff --git a/include/fc/crypto/bigint.hpp b/include/fc/crypto/bigint.hpp index 58703a1..1fdee13 100644 --- a/include/fc/crypto/bigint.hpp +++ b/include/fc/crypto/bigint.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include -#include +#include +#include struct bignum_st; typedef bignum_st BIGNUM; @@ -55,7 +55,7 @@ namespace fc { bigint operator--(int); bigint& operator--(); - operator fc::string()const; + operator std::string()const; // returns bignum as bigendian bytes operator std::vector()const; diff --git a/include/fc/crypto/elliptic.hpp b/include/fc/crypto/elliptic.hpp index 3bc16b7..7ca2ec6 100644 --- a/include/fc/crypto/elliptic.hpp +++ b/include/fc/crypto/elliptic.hpp @@ -153,9 +153,9 @@ namespace fc { extended_key_data serialize_extended() const; static extended_public_key deserialize( const extended_key_data& data ); - fc::string str() const; - fc::string to_base58() const { return str(); } - static extended_public_key from_base58( const fc::string& base58 ); + std::string str() const; + std::string to_base58() const { return str(); } + static extended_public_key from_base58( const std::string& base58 ); private: sha256 c; @@ -177,10 +177,10 @@ namespace fc { extended_key_data serialize_extended() const; static extended_private_key deserialize( const extended_key_data& data ); - fc::string str() const; - fc::string to_base58() const { return str(); } - static extended_private_key from_base58( const fc::string& base58 ); - static extended_private_key generate_master( const fc::string& seed ); + std::string str() const; + std::string to_base58() const { return str(); } + static extended_private_key from_base58( const std::string& base58 ); + static extended_private_key generate_master( const std::string& seed ); static extended_private_key generate_master( const char* seed, uint32_t seed_len ); private: diff --git a/include/fc/crypto/hex.hpp b/include/fc/crypto/hex.hpp index 201f167..fd60d84 100644 --- a/include/fc/crypto/hex.hpp +++ b/include/fc/crypto/hex.hpp @@ -1,15 +1,15 @@ #pragma once -#include -#include +#include +#include #include namespace fc { uint8_t from_hex( char c ); - fc::string to_hex( const char* d, uint32_t s ); + std::string to_hex( const char* d, uint32_t s ); std::string to_hex( const std::vector& data ); /** * @return the number of bytes decoded */ - size_t from_hex( const fc::string& hex_str, char* out_data, size_t out_data_len ); + size_t from_hex( const std::string& hex_str, char* out_data, size_t out_data_len ); } diff --git a/include/fc/crypto/sha1.hpp b/include/fc/crypto/sha1.hpp index 01c6ca8..46b76e4 100644 --- a/include/fc/crypto/sha1.hpp +++ b/include/fc/crypto/sha1.hpp @@ -1,8 +1,8 @@ #pragma once #include -#include #include +#include namespace fc{ @@ -10,16 +10,16 @@ class sha1 { public: sha1(); - explicit sha1( const string& hex_str ); + explicit sha1( const std::string& hex_str ); - string str()const; - operator string()const; + std::string str()const; + operator std::string()const; char* data()const; size_t data_size()const { return 20; } static sha1 hash( const char* d, uint32_t dlen ); - static sha1 hash( const string& ); + static sha1 hash( const std::string& ); template static sha1 hash( const T& t ) diff --git a/include/fc/crypto/sha224.hpp b/include/fc/crypto/sha224.hpp index 20d484f..97a36b8 100644 --- a/include/fc/crypto/sha224.hpp +++ b/include/fc/crypto/sha224.hpp @@ -2,7 +2,6 @@ #include #include #include -#include namespace fc { diff --git a/include/fc/crypto/sha256.hpp b/include/fc/crypto/sha256.hpp index 33a2bfc..bdc6737 100644 --- a/include/fc/crypto/sha256.hpp +++ b/include/fc/crypto/sha256.hpp @@ -1,6 +1,5 @@ #pragma once #include -#include #include #include diff --git a/include/fc/crypto/sha512.hpp b/include/fc/crypto/sha512.hpp index a6c4a36..9751a56 100644 --- a/include/fc/crypto/sha512.hpp +++ b/include/fc/crypto/sha512.hpp @@ -1,6 +1,5 @@ #pragma once #include -#include namespace fc { diff --git a/include/fc/exception/exception.hpp b/include/fc/exception/exception.hpp index ec100f4..b9b6d8e 100644 --- a/include/fc/exception/exception.hpp +++ b/include/fc/exception/exception.hpp @@ -240,9 +240,9 @@ namespace fc TYPE( const std::string& what_value, const fc::log_messages& m ) \ :BASE( m, CODE, BOOST_PP_STRINGIZE(TYPE), what_value ){} \ TYPE( fc::log_message&& m ) \ - :BASE( fc::move(m), CODE, BOOST_PP_STRINGIZE(TYPE), WHAT ){}\ + :BASE( std::move(m), CODE, BOOST_PP_STRINGIZE(TYPE), WHAT ){}\ TYPE( fc::log_messages msgs ) \ - :BASE( fc::move( msgs ), CODE, BOOST_PP_STRINGIZE(TYPE), WHAT ) {} \ + :BASE( std::move( msgs ), CODE, BOOST_PP_STRINGIZE(TYPE), WHAT ) {} \ TYPE( const TYPE& c ) \ :BASE(c){} \ TYPE( const BASE& c ) \ diff --git a/include/fc/filesystem.hpp b/include/fc/filesystem.hpp index 38fb74a..bd1aa5d 100644 --- a/include/fc/filesystem.hpp +++ b/include/fc/filesystem.hpp @@ -2,7 +2,6 @@ #include #include -#include #include #include #include @@ -228,37 +227,5 @@ namespace fc { temp_directory(const fc::path& tempFolder = fc::temp_directory_path()); }; - -#if !defined(__APPLE__) - // this code is known to work on linux and windows. It may work correctly on mac, - // or it may need slight tweaks or extra includes. It's disabled now to avoid giving - // a false sense of security. -# define FC_HAS_SIMPLE_FILE_LOCK -#endif -#ifdef FC_HAS_SIMPLE_FILE_LOCK - /** simple class which only allows one process to open any given file. - * approximate usage: - * int main() { - * fc::simple_file_lock instance_lock("~/.my_app/.lock"); - * if (!instance_lock.try_lock()) { - * elog("my_app is already running"); - * return 1; - * } - * // do stuff here, file will be unlocked when instance_lock goes out of scope - * } - */ - class simple_lock_file - { - public: - simple_lock_file(const path& lock_file_path); - ~simple_lock_file(); - bool try_lock(); - void unlock(); - private: - class impl; - std::unique_ptr my; - }; -#endif // FC_HAS_SIMPLE_FILE_LOCK - } diff --git a/include/fc/fixed_string.hpp b/include/fc/fixed_string.hpp deleted file mode 100644 index a91b651..0000000 --- a/include/fc/fixed_string.hpp +++ /dev/null @@ -1,143 +0,0 @@ -#pragma once -#include - - -namespace fc { - - - /** - * This class is designed to offer in-place memory allocation of a string up to Length equal to - * sizeof(Storage). - * - * The string will serialize the same way as std::string for variant and raw formats - * The string will sort according to the comparison operators defined for Storage, this enables effecient - * sorting. - */ - template > - class fixed_string { - public: - fixed_string(){} - fixed_string( const fixed_string& c ):data(c.data){} - - fixed_string( const std::string& str ) { - if( str.size() <= sizeof(data) ) - memcpy( (char*)&data, str.c_str(), str.size() ); - else { - memcpy( (char*)&data, str.c_str(), sizeof(data) ); - } - } - fixed_string( const char* str ) { - auto l = strlen(str); - if( l <= sizeof(data) ) - memcpy( (char*)&data, str, l ); - else { - memcpy( (char*)&data, str, sizeof(data) ); - } - } - - operator std::string()const { - const char* self = (const char*)&data; - return std::string( self, self + size() ); - } - - uint32_t size()const { - if( *(((const char*)&data)+sizeof(data) - 1) ) - return sizeof(data); - return strnlen( (const char*)&data, sizeof(data) ); - } - uint32_t length()const { return size(); } - - fixed_string& operator=( const fixed_string& str ) { - data = str.data; - return *this; - } - fixed_string& operator=( const char* str ) { - return *this = fixed_string(str); - } - - fixed_string& operator=( const std::string& str ) { - if( str.size() <= sizeof(data) ) { - data = Storage(); - memcpy( (char*)&data, str.c_str(), str.size() ); - } - else { - memcpy( (char*)&data, str.c_str(), sizeof(data) ); - } - return *this; - } - - friend std::string operator + ( const fixed_string& a, const std::string& b ) { - return std::string(a) + b; - } - friend std::string operator + ( const std::string& a, const fixed_string& b ) { - return a + std::string(b); - } - - friend bool operator < ( const fixed_string& a, const fixed_string& b ) { - return a.data < b.data; - } - friend bool operator <= ( const fixed_string& a, const fixed_string& b ) { - return a.data <= b.data; - } - friend bool operator > ( const fixed_string& a, const fixed_string& b ) { - return a.data > b.data; - } - friend bool operator >= ( const fixed_string& a, const fixed_string& b ) { - return a.data >= b.data; - } - friend bool operator == ( const fixed_string& a, const fixed_string& b ) { - return a.data == b.data; - } - friend bool operator != ( const fixed_string& a, const fixed_string& b ) { - return a.data != b.data; - } - //private: - Storage data; - }; - - namespace raw - { - template - inline void pack( Stream& s, const fc::fixed_string& u, uint32_t _max_depth=FC_PACK_MAX_DEPTH ) { - FC_ASSERT( _max_depth > 0 ); - unsigned_int size = u.size(); - pack( s, size, _max_depth - 1 ); - s.write( (const char*)&u.data, size ); - } - - template - inline void unpack( Stream& s, fc::fixed_string& u, uint32_t _max_depth=FC_PACK_MAX_DEPTH ) { - FC_ASSERT( _max_depth > 0 ); - unsigned_int size; - fc::raw::unpack( s, size, _max_depth - 1 ); - if( size.value > 0 ) { - if( size.value > sizeof(Storage) ) { - s.read( (char*)&u.data, sizeof(Storage) ); - char buf[1024]; - size_t left = size.value - sizeof(Storage); - while( left >= 1024 ) - { - s.read( buf, 1024 ); - left -= 1024; - } - s.read( buf, left ); - } else { - s.read( (char*)&u.data, size.value ); - } - } - } - } -} - -#include -namespace fc { - template - 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, uint32_t max_depth = 1 ) { - s = v.as_string(); - } -} diff --git a/include/fc/fwd.hpp b/include/fc/fwd.hpp index ccf0828..b1a4006 100644 --- a/include/fc/fwd.hpp +++ b/include/fc/fwd.hpp @@ -1,5 +1,4 @@ #pragma once -#include namespace fc { @@ -37,7 +36,7 @@ class fwd { ~fwd(); private: - aligned _store; + alignas(Align) char _store[S]; }; diff --git a/include/fc/fwd_impl.hpp b/include/fc/fwd_impl.hpp index 639085a..1c8155d 100644 --- a/include/fc/fwd_impl.hpp +++ b/include/fc/fwd_impl.hpp @@ -3,31 +3,32 @@ #include #include #include +#include namespace fc { namespace detail { template struct add { - typedef decltype( *((A*)0) + *((typename fc::remove_reference::type*)0) ) type; + typedef decltype( *((A*)0) + *((typename std::remove_reference::type*)0) ) type; }; template struct add_eq { - typedef decltype( *((A*)0) += *((typename fc::remove_reference::type*)0) ) type; + typedef decltype( *((A*)0) += *((typename std::remove_reference::type*)0) ) type; }; template struct sub { - typedef decltype( *((A*)0) - *((typename fc::remove_reference::type*)0) ) type; + typedef decltype( *((A*)0) - *((typename std::remove_reference::type*)0) ) type; }; template struct sub_eq { - typedef decltype( *((A*)0) -= *((typename fc::remove_reference::type*)0) ) type; + typedef decltype( *((A*)0) -= *((typename std::remove_reference::type*)0) ) type; }; template struct insert_op { - typedef decltype( *((A*)0) << *((typename fc::remove_reference::type*)0) ) type; + typedef decltype( *((A*)0) << *((typename std::remove_reference::type*)0) ) type; }; template struct extract_op { @@ -91,7 +92,7 @@ namespace fc { template fwd::fwd( fwd&& f ){ check_size(); - new (this) T( fc::move(*f) ); + new (this) T( std::move(*f) ); } @@ -124,7 +125,7 @@ namespace fc { template T& fwd::operator = ( fwd&& u ) { - return **this = fc::move(*u); + return **this = std::move(*u); } template T& fwd::operator = ( const fwd& u ) { diff --git a/include/fc/interprocess/container.hpp b/include/fc/interprocess/container.hpp deleted file mode 100644 index a1a4658..0000000 --- a/include/fc/interprocess/container.hpp +++ /dev/null @@ -1,108 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace fc { - - namespace bip = boost::interprocess; - - template - 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 ) { - 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, 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], max_depth ); - } - } - - template - 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, max_depth ); - vo = vars; - } - - template - 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 ) { - 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, 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], max_depth ); - } - } - - template - 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( variant( item, max_depth ) ); - } - v = std::move(vars); - } - - template - 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())); - else - v = ""; - } - - template - 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 ); - if( d.size() ) - { - size_t r = fc::from_hex( str, d.data(), d.size() ); - FC_ASSERT( r == d.size() ); - } - } -} diff --git a/include/fc/interprocess/file_mapping.hpp b/include/fc/interprocess/file_mapping.hpp index e6b6817..369e6fa 100644 --- a/include/fc/interprocess/file_mapping.hpp +++ b/include/fc/interprocess/file_mapping.hpp @@ -1,6 +1,7 @@ #pragma once #include -#include +#include +#include namespace boost { namespace interprocess { @@ -24,7 +25,7 @@ namespace fc { #ifdef _WIN64 fc::fwd my; #else - fc::fwd my; + fc::fwd my; #endif }; diff --git a/include/fc/interprocess/file_mutex.hpp b/include/fc/interprocess/file_mutex.hpp deleted file mode 100644 index 6041824..0000000 --- a/include/fc/interprocess/file_mutex.hpp +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once -#include -#include - -#include - -namespace fc { - class microseconds; - class time_point; - class path; - struct context; - - namespace detail { class file_mutex_impl; } - - /** - * The purpose of this class is to support synchronization of - * processes, threads, and coop-threads. - * - * Before grabbing the lock for a thread or coop, a file_mutex will first - * grab a process-level lock. After grabbing the process level lock, it will - * synchronize in the same way as a local process lock. - */ - class file_mutex { - public: - file_mutex( const fc::path& filename ); - ~file_mutex(); - - bool try_lock(); - bool try_lock_for( const microseconds& rel_time ); - bool try_lock_until( const time_point& abs_time ); - void lock(); - void unlock(); - - void lock_shared(); - void unlock_shared(); - bool try_lock_shared(); - - int readers()const; - - private: - std::unique_ptr my; - }; - -} // namespace fc diff --git a/include/fc/interprocess/iprocess.hpp b/include/fc/interprocess/iprocess.hpp deleted file mode 100644 index c7f8c4e..0000000 --- a/include/fc/interprocess/iprocess.hpp +++ /dev/null @@ -1,67 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include - -namespace fc -{ - /** - * @brief abstract interface for interacting with external processes - * - * At the very least we have ssh::process and direct child processes, and - * there may be other processes that need to implement this protocol. - */ - class iprocess - { - public: - enum exec_opts { - open_none = 0, - open_stdin = 0x01, - open_stdout = 0x02, - open_stderr = 0x04, - open_all = open_stdin|open_stdout|open_stderr, - suppress_console = 0x08 - }; - - virtual ~iprocess(){} - - /** - * - * @return *this - */ - virtual iprocess& exec( const path& exe, std::vector args, - const path& work_dir = path(), int opts = open_all ) = 0; - - /** - * @return blocks until the process exits - */ - virtual int result(const microseconds& timeout = microseconds::maximum()) = 0; - - - /** - * Forcefully kills the process. - */ - virtual void kill() = 0; - - /** - * @brief returns a stream that writes to the process' stdin - */ - virtual buffered_ostream_ptr in_stream() = 0; - - /** - * @brief returns a stream that reads from the process' stdout - */ - virtual buffered_istream_ptr out_stream() = 0; - /** - * @brief returns a stream that reads from the process' stderr - */ - virtual buffered_istream_ptr err_stream() = 0; - - }; - - typedef std::shared_ptr iprocess_ptr; - - -} // namespace fc diff --git a/include/fc/interprocess/mmap_struct.hpp b/include/fc/interprocess/mmap_struct.hpp deleted file mode 100644 index 0913deb..0000000 --- a/include/fc/interprocess/mmap_struct.hpp +++ /dev/null @@ -1,59 +0,0 @@ -#pragma once -#include -#include - -namespace fc -{ - class path; - namespace detail - { - /** - * Base class used to hide common implementation details. - */ - class mmap_struct_base - { - public: - size_t size()const; - void flush(); - - protected: - void open( const fc::path& file, size_t s, bool create ); - std::unique_ptr _file_mapping; - std::unique_ptr _mapped_region; - }; - }; - - /** - * @class mmap_struct - * @brief A struct that has been mapped from a file. - * - * @note T must be POD - */ - template - class mmap_struct : public detail::mmap_struct_base - { - public: - mmap_struct():_mapped_struct(nullptr){} - /** - * Create the file if it does not exist or is of the wrong size if create is true, then maps - * the file to memory. - * - * @throw an exception if the file does not exist or is the wrong size and create is false - */ - void open( const fc::path& file, bool create = false ) - { - detail::mmap_struct_base::open( file, sizeof(T), create ); - _mapped_struct = (T*)_mapped_region->get_address(); - } - - T* operator->() { return _mapped_struct; } - const T* operator->()const { return _mapped_struct; } - - T& operator*() { return *_mapped_struct; } - const T& operator*()const { return *_mapped_struct; } - - private: - T* _mapped_struct; - }; - -} diff --git a/include/fc/interprocess/process.hpp b/include/fc/interprocess/process.hpp deleted file mode 100644 index 777c154..0000000 --- a/include/fc/interprocess/process.hpp +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once -#include - -namespace fc { - - fc::path find_executable_in_path( const fc::string name ); - - /** - * @brief start and manage an local process - * @note this class implements reference semantics. - */ - class process : public iprocess - { - public: - process(); - ~process(); - - virtual iprocess& exec( const fc::path& exe, - std::vector args, - const fc::path& work_dir = fc::path(), - int opts = open_all ); - - - virtual int result(const microseconds& timeout = microseconds::maximum()); - virtual void kill(); - virtual fc::buffered_ostream_ptr in_stream(); - virtual fc::buffered_istream_ptr out_stream(); - virtual fc::buffered_istream_ptr err_stream(); - - class impl; - private: - std::unique_ptr my; - }; - - typedef std::shared_ptr process_ptr; - -} // namespace fc diff --git a/include/fc/io/console.hpp b/include/fc/io/console.hpp deleted file mode 100644 index 11a9d47..0000000 --- a/include/fc/io/console.hpp +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once -namespace fc -{ - /** enables / disables echoing of console input, useful for - * entering passwords on the console. - */ - void set_console_echo( bool enable_echo ); -} // namespace fc diff --git a/include/fc/io/iobuffer.hpp b/include/fc/io/iobuffer.hpp deleted file mode 100644 index 02f919b..0000000 --- a/include/fc/io/iobuffer.hpp +++ /dev/null @@ -1,81 +0,0 @@ -#pragma once -#include -#include - -namespace fc -{ - /** - * Records the size, but discards the data. - */ - class size_stream : public virtual fc::ostream - { - public: - size_stream( size_t s = 0):_size(s){} - - size_t size()const { return _size; } - size_t seek( size_t pos ) { return _size = pos; } - - virtual size_t writesome( const char* /*ignored buf*/, size_t len ) - { - _size += len; - return len; - } - - virtual void close(){} - virtual void flush(){} - - private: - size_t _size; - }; - - - class iobuffer : public virtual fc::iostream - { - public: - iobuffer( size_t s ) - :_data(s){} - - size_t size()const { return _data.size(); } - size_t pos()const { return _pos; } - size_t seek( size_t pos ) - { - return _pos = std::min(_data.size(),pos); - } - - virtual size_t readsome( char* buf, size_t len ) - { - auto avail = std::min( _data.size()-_pos, len ); - if( avail == 0 ) throw fc::eof_exception(); - memcpy( buf, _data.data()+_pos, avail ); - _pos += avail; - return avail; - } - /** - * This method may block until at least 1 character is - * available. - */ - char peek()const - { - if( _pos == _data.size() ) throw fc::eof_exception(); - return _data[_pos]; - } - - virtual size_t writesome( const char* buf, size_t len ) - { - auto avail = std::max( _data.size(), _pos + len ); - _data.resize(avail); - memcpy( _data.data()+_pos, buf, len ); - _pos += avail; - return avail; - } - char* data() { return _data.data(); } - - virtual void close(){} - virtual void flush(){} - - private: - std::vector _data; - size_t _pos; - }; - -} diff --git a/include/fc/io/iostream.hpp b/include/fc/io/iostream.hpp index 1ec632f..9fa592b 100644 --- a/include/fc/io/iostream.hpp +++ b/include/fc/io/iostream.hpp @@ -1,6 +1,4 @@ #pragma once -#include -#include #include namespace fc { @@ -61,7 +59,7 @@ namespace fc { class iostream : public virtual ostream, public virtual istream {}; - fc::istream& getline( fc::istream&, fc::string&, char delim = '\n' ); + fc::istream& getline( fc::istream&, std::string&, char delim = '\n' ); template ostream& operator<<( ostream& o, char (&array)[N] ) @@ -72,7 +70,7 @@ namespace fc { ostream& operator<<( ostream& o, char ); ostream& operator<<( ostream& o, const char* v ); ostream& operator<<( ostream& o, const std::string& v ); - ostream& operator<<( ostream& o, const fc::string& v ); + ostream& operator<<( ostream& o, const std::string& v ); ostream& operator<<( ostream& o, const double& v ); ostream& operator<<( ostream& o, const float& v ); ostream& operator<<( ostream& o, const int64_t& v ); @@ -88,7 +86,7 @@ namespace fc { #endif istream& operator>>( istream& o, std::string& v ); - istream& operator>>( istream& o, fc::string& v ); + istream& operator>>( istream& o, std::string& v ); istream& operator>>( istream& o, char& v ); istream& operator>>( istream& o, double& v ); istream& operator>>( istream& o, float& v ); diff --git a/include/fc/io/json.hpp b/include/fc/io/json.hpp index f07124b..0f97c40 100644 --- a/include/fc/io/json.hpp +++ b/include/fc/io/json.hpp @@ -33,7 +33,7 @@ namespace fc legacy_generator = 1 }; - static ostream& to_stream( ostream& out, const fc::string& ); + static ostream& to_stream( ostream& out, const std::string& ); static ostream& to_stream( ostream& out, const variant& v, output_formatting format = stringify_large_ints_and_doubles, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH ); static ostream& to_stream( ostream& out, const variants& v, output_formatting format = stringify_large_ints_and_doubles, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH ); static ostream& to_stream( ostream& out, const variant_object& v, output_formatting format = stringify_large_ints_and_doubles, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH ); diff --git a/include/fc/io/json_relaxed.hpp b/include/fc/io/json_relaxed.hpp index 9a25666..00893a3 100644 --- a/include/fc/io/json_relaxed.hpp +++ b/include/fc/io/json_relaxed.hpp @@ -10,7 +10,6 @@ #include #include #include -#include //#include #include #include @@ -24,7 +23,7 @@ namespace fc { namespace json_relaxed variant variant_from_stream( T& in, uint32_t max_depth ); template - fc::string tokenFromStream( T& in ) + std::string tokenFromStream( T& in ) { fc::stringstream token; try @@ -81,7 +80,7 @@ namespace fc { namespace json_relaxed } template - fc::string quoteStringFromStream( T& in ) + std::string quoteStringFromStream( T& in ) { fc::stringstream token; try @@ -107,11 +106,11 @@ namespace fc { namespace json_relaxed try { if( in.peek() != q ) - return fc::string(); + return std::string(); } catch( const fc::eof_exception& e ) { - return fc::string(); + return std::string(); } // triple quote processing @@ -186,7 +185,7 @@ namespace fc { namespace json_relaxed } template - fc::string stringFromStream( T& in ) + std::string stringFromStream( T& in ) { try { @@ -294,7 +293,7 @@ namespace fc { namespace json_relaxed }; template - fc::variant parseInt( const fc::string& token, size_t start ) + fc::variant parseInt( const std::string& token, size_t start ) { static const CharValueTable ctbl; static const uint64_t INT64_MAX_PLUS_ONE = static_cast(INT64_MAX) + 1; @@ -336,7 +335,7 @@ namespace fc { namespace json_relaxed } template - fc::variant maybeParseInt( const fc::string& token, size_t start ) + fc::variant maybeParseInt( const std::string& token, size_t start ) { try { @@ -352,7 +351,7 @@ namespace fc { namespace json_relaxed } template - fc::variant parseNumberOrStr( const fc::string& token ) + fc::variant parseNumberOrStr( const std::string& token ) { try { //ilog( (token) ); size_t i = 0, n = token.length(); @@ -588,7 +587,7 @@ namespace fc { namespace json_relaxed template variant numberFromStream( T& in ) { try { - fc::string token = tokenFromStream(in); + std::string token = tokenFromStream(in); variant result = json_relaxed::parseNumberOrStr( token ); if( strict && !(result.is_int64() || result.is_uint64() || result.is_double()) ) FC_THROW_EXCEPTION( parse_error_exception, "expected: number" ); @@ -598,7 +597,7 @@ namespace fc { namespace json_relaxed template variant wordFromStream( T& in ) { - fc::string token = tokenFromStream(in); + std::string token = tokenFromStream(in); FC_ASSERT( token.length() > 0 ); diff --git a/include/fc/io/raw.hpp b/include/fc/io/raw.hpp index 24aff5e..052110c 100644 --- a/include/fc/io/raw.hpp +++ b/include/fc/io/raw.hpp @@ -50,7 +50,7 @@ namespace fc { fc::raw::unpack( s, what, _max_depth ); fc::raw::unpack( s, msgs, _max_depth ); - e = fc::exception( fc::move(msgs), code, name, what ); + e = fc::exception( std::move(msgs), code, name, what ); } template @@ -206,7 +206,7 @@ namespace fc { template inline void pack( Stream& s, const char* v, uint32_t _max_depth ) { FC_ASSERT( _max_depth > 0 ); - fc::raw::pack( s, fc::string(v), _max_depth - 1 ); + fc::raw::pack( s, std::string(v), _max_depth - 1 ); } template @@ -270,18 +270,18 @@ namespace fc { } // fc::string - template inline void pack( Stream& s, const fc::string& v, uint32_t _max_depth ) { + template inline void pack( Stream& s, const std::string& v, uint32_t _max_depth ) { FC_ASSERT( _max_depth > 0 ); fc::raw::pack( s, unsigned_int(v.size()), _max_depth - 1 ); if( v.size() ) s.write( v.c_str(), v.size() ); } - template inline void unpack( Stream& s, fc::string& v, uint32_t _max_depth ) { + template inline void unpack( Stream& s, std::string& v, uint32_t _max_depth ) { FC_ASSERT( _max_depth > 0 ); std::vector tmp; fc::raw::unpack( s, tmp, _max_depth - 1 ); if( tmp.size() ) - v = fc::string( tmp.data(), tmp.data()+tmp.size() ); - else v = fc::string(); + v = std::string( tmp.data(), tmp.data()+tmp.size() ); + else v = std::string(); } // bool diff --git a/include/fc/io/raw_fwd.hpp b/include/fc/io/raw_fwd.hpp index df80768..17f2395 100644 --- a/include/fc/io/raw_fwd.hpp +++ b/include/fc/io/raw_fwd.hpp @@ -1,7 +1,6 @@ #pragma once #include #include -#include #include #include #include @@ -31,15 +30,11 @@ namespace fc { namespace ip { class endpoint; } namespace ecc { class public_key; class private_key; } - template class fixed_string; namespace raw { template inline size_t pack_size( const T& v ); - template inline void pack( Stream& s, const fc::fixed_string& u, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); - template inline void unpack( Stream& s, fc::fixed_string& u, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); - template inline void pack( Stream& s, const fc::enum_type& tp, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); template @@ -58,8 +53,8 @@ namespace fc { //template inline void pack( Stream& s, const flat_set& value, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); //template inline void unpack( Stream& s, flat_set& value, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); - //template inline void pack( Stream& s, const std::deque& value, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); - //template inline void unpack( Stream& s, std::deque& value, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); + template inline void pack( Stream& s, const std::deque& value, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); + template inline void unpack( Stream& s, std::deque& value, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); template inline void pack( Stream& s, const std::unordered_map& value, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); template inline void unpack( Stream& s, std::unordered_map& value, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); diff --git a/include/fc/io/raw_unpack_file.hpp b/include/fc/io/raw_unpack_file.hpp deleted file mode 100644 index 580a64c..0000000 --- a/include/fc/io/raw_unpack_file.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once -#include -#include -#include -#include - -namespace fc -{ - namespace raw - { - template - void unpack_file( const fc::path& filename, T& obj ) - { - try { - fc::file_mapping fmap( filename.generic_string().c_str(), fc::read_only); - fc::mapped_region mapr( fmap, fc::read_only, 0, fc::file_size(filename) ); - auto cs = (const char*)mapr.get_address(); - - fc::datastream ds( cs, mapr.get_size() ); - fc::raw::unpack(ds,obj); - } FC_RETHROW_EXCEPTIONS( info, "unpacking file ${file}", ("file",filename) ); - } - } -} diff --git a/include/fc/io/raw_variant.hpp b/include/fc/io/raw_variant.hpp index 4bf9fd8..f4a7e02 100644 --- a/include/fc/io/raw_variant.hpp +++ b/include/fc/io/raw_variant.hpp @@ -100,23 +100,23 @@ namespace fc { namespace raw { } case variant::string_type: { - fc::string val; + std::string val; raw::unpack( s, val, _max_depth ); - v = fc::move(val); + v = std::move(val); return; } case variant::array_type: { variants val; raw::unpack( s, val, _max_depth ); - v = fc::move(val); + v = std::move(val); return; } case variant::object_type: { variant_object val; raw::unpack( s, val, _max_depth ); - v = fc::move(val); + v = std::move(val); return; } default: @@ -148,13 +148,13 @@ namespace fc { namespace raw { mvo.reserve( std::min( vs.value, static_cast(FC_MAX_PREALLOC_SIZE) ) ); for( uint32_t i = 0; i < vs.value; ++i ) { - fc::string key; + std::string key; fc::variant value; fc::raw::unpack( s, key, _max_depth ); fc::raw::unpack( s, value, _max_depth ); - mvo.set( fc::move(key), fc::move(value) ); + mvo.set( std::move(key), std::move(value) ); } - v = fc::move(mvo); + v = std::move(mvo); } } } // fc::raw diff --git a/include/fc/io/sstream.hpp b/include/fc/io/sstream.hpp index 285c481..f5a2a89 100644 --- a/include/fc/io/sstream.hpp +++ b/include/fc/io/sstream.hpp @@ -7,12 +7,12 @@ namespace fc { class stringstream : virtual public iostream { public: stringstream(); - stringstream( fc::string& s); - stringstream( const fc::string& s); + stringstream( std::string& s); + stringstream( const std::string& s); ~stringstream(); - fc::string str(); - void str(const fc::string& s); + std::string str(); + void str(const std::string& s); void clear(); diff --git a/include/fc/log/appender.hpp b/include/fc/log/appender.hpp index f1f5aa8..8f57f4c 100644 --- a/include/fc/log/appender.hpp +++ b/include/fc/log/appender.hpp @@ -1,6 +1,6 @@ #pragma once #include -#include +#include namespace fc { class appender; @@ -30,13 +30,13 @@ namespace fc { typedef fc::shared_ptr ptr; template - static bool register_appender(const fc::string& type) { + static bool register_appender(const std::string& type) { return register_appender( type, new detail::appender_factory_impl() ); } - static appender::ptr create( const fc::string& name, const fc::string& type, const variant& args ); - static appender::ptr get( const fc::string& name ); - static bool register_appender( const fc::string& type, const appender_factory::ptr& f ); + static appender::ptr create( const std::string& name, const std::string& type, const variant& args ); + static appender::ptr get( const std::string& name ); + static bool register_appender( const std::string& type, const appender_factory::ptr& f ); virtual void log( const log_message& m ) = 0; }; diff --git a/include/fc/log/console_appender.hpp b/include/fc/log/console_appender.hpp index 4048da6..75ecbbd 100644 --- a/include/fc/log/console_appender.hpp +++ b/include/fc/log/console_appender.hpp @@ -40,7 +40,7 @@ namespace fc :format( "${timestamp} ${thread_name} ${context} ${file}:${line} ${method} ${level}] ${message}" ), stream(console_appender::stream::std_error),max_object_depth(FC_MAX_LOG_OBJECT_DEPTH),flush(true){} - fc::string format; + std::string format; console_appender::stream::type stream; std::vector level_colors; uint32_t max_object_depth; diff --git a/include/fc/log/file_appender.hpp b/include/fc/log/file_appender.hpp index 6f1fa6f..bd692ef 100644 --- a/include/fc/log/file_appender.hpp +++ b/include/fc/log/file_appender.hpp @@ -12,7 +12,7 @@ class file_appender : public appender { struct config { config( const fc::path& p = "log.txt" ); - fc::string format; + std::string format; fc::path filename; bool flush = true; bool rotate = false; diff --git a/include/fc/log/log_message.hpp b/include/fc/log/log_message.hpp index e9e1a68..7c529c0 100644 --- a/include/fc/log/log_message.hpp +++ b/include/fc/log/log_message.hpp @@ -8,6 +8,7 @@ #include #include #include +#include namespace fc { @@ -65,19 +66,19 @@ namespace fc explicit log_context( const variant& v, uint32_t max_depth ); variant to_variant( uint32_t max_depth )const; - string get_file()const; + std::string get_file()const; uint64_t get_line_number()const; - string get_method()const; - string get_thread_name()const; - string get_task_name()const; - string get_host_name()const; + std::string get_method()const; + std::string get_thread_name()const; + std::string get_task_name()const; + std::string get_host_name()const; time_point get_timestamp()const; log_level get_log_level()const; - string get_context()const; + std::string get_context()const; - void append_context( const fc::string& c ); + void append_context( const std::string& c ); - string to_string()const; + std::string to_string()const; private: std::shared_ptr my; }; @@ -116,10 +117,10 @@ namespace fc log_message( const variant& v, uint32_t max_depth ); variant to_variant(uint32_t max_depth)const; - string get_message()const; + std::string get_message()const; log_context get_context()const; - string get_format()const; + std::string get_format()const; variant_object get_data()const; private: diff --git a/include/fc/log/logger.hpp b/include/fc/log/logger.hpp index 16275dc..6e1a52b 100644 --- a/include/fc/log/logger.hpp +++ b/include/fc/log/logger.hpp @@ -1,6 +1,5 @@ #pragma once #include -#include #include #include #include @@ -23,7 +22,7 @@ namespace fc class logger { public: - static logger get( const fc::string& name = "default"); + static logger get( const std::string& name = "default"); logger(); logger( const string& name, const logger& parent = nullptr ); @@ -41,8 +40,8 @@ namespace fc logger& set_parent( const logger& l ); logger get_parent()const; - void set_name( const fc::string& n ); - const fc::string& name()const; + void set_name( const std::string& n ); + const std::string& name()const; void add_appender( const fc::shared_ptr& a ); std::vector > get_appenders()const; diff --git a/include/fc/log/logger_config.hpp b/include/fc/log/logger_config.hpp index f2fc1e2..8f73055 100644 --- a/include/fc/log/logger_config.hpp +++ b/include/fc/log/logger_config.hpp @@ -9,7 +9,7 @@ namespace fc { variant args = variant()) : name(name), type(type), - args(fc::move(args)), + args(std::move(args)), enabled(true) {} string name; @@ -19,7 +19,7 @@ namespace fc { }; struct logger_config { - logger_config(const fc::string& name = ""):name(name),enabled(true),additivity(false){} + logger_config(const std::string& name = ""):name(name),enabled(true),additivity(false){} string name; ostring parent; /// if not set, then parents level is used. diff --git a/include/fc/make_fused.hpp b/include/fc/make_fused.hpp deleted file mode 100644 index acbf901..0000000 --- a/include/fc/make_fused.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once -#include -#include - -namespace fc { - template - std::function > make_fused( const std::function& f ) { - return [=]( fc::tuple<> ){ return f(); }; - } - template - std::function) > make_fused( const std::function& f ) { - return [f]( fc::tuple t){ return f(t.a); }; - } - template - std::function) > make_fused( const std::function& f ) { - return [f]( fc::tuple t){ return f(t.a,t.b); }; - } - template - std::function) > make_fused( const std::function& f ) { - return [f]( fc::tuple t){ return f(t.a,t.b,t.c); }; - } - template - std::function) > make_fused( const std::function& f ) { - return [f]( fc::tuple t){ return f(t.a,t.b,t.c,t.d); }; - } -} diff --git a/include/fc/network/http/connection.hpp b/include/fc/network/http/connection.hpp index 786f24d..8a34076 100644 --- a/include/fc/network/http/connection.hpp +++ b/include/fc/network/http/connection.hpp @@ -1,7 +1,6 @@ #pragma once -#include -#include #include +#include namespace fc { namespace ip { class endpoint; } @@ -11,11 +10,11 @@ namespace fc { struct header { - header( fc::string k, fc::string v ) - :key(fc::move(k)),val(fc::move(v)){} + header( std::string k, std::string v ) + :key(std::move(k)),val(std::move(v)){} header(){} - fc::string key; - fc::string val; + std::string key; + std::string val; }; typedef std::vector
headers; @@ -39,16 +38,16 @@ namespace fc { struct request { - fc::string get_header( const fc::string& key )const; - fc::string remote_endpoint; - fc::string method; - fc::string domain; - fc::string path; + std::string get_header( const std::string& key )const; + std::string remote_endpoint; + std::string method; + std::string domain; + std::string path; std::vector
headers; std::vector body; }; - std::vector
parse_urlencoded_params( const fc::string& f ); + std::vector
parse_urlencoded_params( const std::string& f ); /** * Connections have reference semantics, all copies refer to the same @@ -61,7 +60,7 @@ namespace fc { ~connection(); // used for clients void connect_to( const fc::ip::endpoint& ep ); - http::reply request( const fc::string& method, const fc::string& url, const fc::string& body = std::string(), const headers& = headers()); + http::reply request( const std::string& method, const std::string& url, const std::string& body = std::string(), const headers& = headers()); // used for servers fc::tcp_socket& get_socket()const; diff --git a/include/fc/network/http/server.hpp b/include/fc/network/http/server.hpp index b5eda8b..a52ebcc 100644 --- a/include/fc/network/http/server.hpp +++ b/include/fc/network/http/server.hpp @@ -35,7 +35,7 @@ namespace fc { namespace http { response& operator=(const response& ); response& operator=( response&& ); - void add_header( const fc::string& key, const fc::string& val )const; + void add_header( const std::string& key, const std::string& val )const; void set_status( const http::reply::status_code& s )const; void set_length( uint64_t s )const; diff --git a/include/fc/network/http/websocket.hpp b/include/fc/network/http/websocket.hpp index f56b4be..8837057 100644 --- a/include/fc/network/http/websocket.hpp +++ b/include/fc/network/http/websocket.hpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include #include @@ -26,14 +26,14 @@ namespace fc { namespace http { void on_message_handler( const std::function& h ) { _on_message = h; } void on_http_handler( const std::function& h ) { _on_http = h; } - void set_session_data( fc::any d ){ _session_data = std::move(d); } - fc::any& get_session_data() { return _session_data; } + void set_session_data( boost::any d ){ _session_data = std::move(d); } + boost::any& get_session_data() { return _session_data; } virtual std::string get_request_header(const std::string& key) = 0; fc::signal closed; private: - fc::any _session_data; + boost::any _session_data; std::function _on_message; std::function _on_http; }; diff --git a/include/fc/network/ip.hpp b/include/fc/network/ip.hpp index 7a55616..e183220 100644 --- a/include/fc/network/ip.hpp +++ b/include/fc/network/ip.hpp @@ -1,5 +1,4 @@ #pragma once -#include #include #include #include @@ -11,10 +10,10 @@ namespace fc { class address { public: address( uint32_t _ip = 0 ); - address( const fc::string& s ); + address( const std::string& s ); - address& operator=( const fc::string& s ); - operator fc::string()const; + address& operator=( const std::string& s ); + operator std::string()const; operator uint32_t()const; friend bool operator==( const address& a, const address& b ); diff --git a/include/fc/network/resolve.hpp b/include/fc/network/resolve.hpp index f6bf6ec..0d0c7c7 100644 --- a/include/fc/network/resolve.hpp +++ b/include/fc/network/resolve.hpp @@ -1,5 +1,4 @@ #pragma once -#include #include namespace fc diff --git a/include/fc/network/tcp_socket.hpp b/include/fc/network/tcp_socket.hpp index 4c2e7d6..59fcb73 100644 --- a/include/fc/network/tcp_socket.hpp +++ b/include/fc/network/tcp_socket.hpp @@ -1,5 +1,4 @@ #pragma once -#include #include #include #include diff --git a/include/fc/network/udp_socket.hpp b/include/fc/network/udp_socket.hpp index 65d1107..f150178 100644 --- a/include/fc/network/udp_socket.hpp +++ b/include/fc/network/udp_socket.hpp @@ -1,5 +1,4 @@ #pragma once -#include #include #include diff --git a/include/fc/network/url.hpp b/include/fc/network/url.hpp index 314aa29..7ccde78 100644 --- a/include/fc/network/url.hpp +++ b/include/fc/network/url.hpp @@ -1,5 +1,4 @@ #pragma once -#include #include #include #include @@ -8,7 +7,7 @@ namespace fc { - typedef fc::optional ostring; + typedef fc::optional ostring; typedef fc::optional opath; typedef fc::optional ovariant_object; diff --git a/include/fc/noncopyable.hpp b/include/fc/noncopyable.hpp deleted file mode 100644 index 87fad6b..0000000 --- a/include/fc/noncopyable.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -namespace fc -{ - class noncopyable - { - public: - noncopyable(){} - private: - noncopyable( const noncopyable& ) = delete; - noncopyable& operator=( const noncopyable& ) = delete; - }; -} - diff --git a/include/fc/optional.hpp b/include/fc/optional.hpp index bb760a5..df6e1f2 100644 --- a/include/fc/optional.hpp +++ b/include/fc/optional.hpp @@ -43,7 +43,7 @@ namespace fc { optional( optional&& o ) :_valid(false) { - if( o._valid ) new (ptr()) T( fc::move(*o) ); + if( o._valid ) new (ptr()) T( std::move(*o) ); _valid = o._valid; o.reset(); } @@ -71,7 +71,7 @@ namespace fc { optional( optional&& o ) :_valid(false) { - if( o._valid ) new (ptr()) T( fc::move(*o) ); + if( o._valid ) new (ptr()) T( std::move(*o) ); _valid = o._valid; o.reset(); } @@ -156,10 +156,10 @@ namespace fc { { if( _valid && o._valid ) { - ref() = fc::move(*o); + ref() = std::move(*o); o.reset(); } else if ( !_valid && o._valid ) { - *this = fc::move(*o); + *this = std::move(*o); } else if (_valid) { reset(); } @@ -173,10 +173,10 @@ namespace fc { { if( _valid && o._valid ) { - ref() = fc::move(*o); + ref() = std::move(*o); o.reset(); } else if ( !_valid && o._valid ) { - *this = fc::move(*o); + *this = std::move(*o); } else if (_valid) { reset(); } diff --git a/include/fc/reflect/reflect.hpp b/include/fc/reflect/reflect.hpp index 5a97a8e..a5122d9 100644 --- a/include/fc/reflect/reflect.hpp +++ b/include/fc/reflect/reflect.hpp @@ -6,6 +6,7 @@ * */ +#include #include #include #include @@ -126,7 +127,7 @@ void fc::reflector::visit( const Visitor& v ) { \ #define FC_REFLECT_ENUM_TO_STRING( r, enum_type, elem ) \ case enum_type::elem: return BOOST_PP_STRINGIZE(elem); #define FC_REFLECT_ENUM_TO_FC_STRING( r, enum_type, elem ) \ - case enum_type::elem: return fc::string(BOOST_PP_STRINGIZE(elem)); + case enum_type::elem: return std::string(BOOST_PP_STRINGIZE(elem)); #define FC_REFLECT_ENUM_FROM_STRING( r, enum_type, elem ) \ if( strcmp( s, BOOST_PP_STRINGIZE(elem) ) == 0 ) return enum_type::elem; @@ -149,13 +150,13 @@ template<> struct reflector { \ static const char* to_string(int64_t i) { \ return to_string(ENUM(i)); \ } \ - static fc::string to_fc_string(ENUM elem) { \ + static std::string to_fc_string(ENUM elem) { \ switch( elem ) { \ BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_ENUM_TO_FC_STRING, ENUM, FIELDS ) \ } \ return fc::to_string(int64_t(elem)); \ } \ - static fc::string to_fc_string(int64_t i) { \ + static std::string to_fc_string(int64_t i) { \ return to_fc_string(ENUM(i)); \ } \ static ENUM from_int(int64_t i) { \ diff --git a/include/fc/reflect/typename.hpp b/include/fc/reflect/typename.hpp index e7758f0..e61f79b 100644 --- a/include/fc/reflect/typename.hpp +++ b/include/fc/reflect/typename.hpp @@ -3,13 +3,12 @@ #include #include #include +#include #include -#include #include #include -#include namespace fc { class value; @@ -30,8 +29,8 @@ namespace fc { template<> struct get_typename { static const char* name() { return "bool"; } }; template<> struct get_typename { static const char* name() { return "char"; } }; template<> struct get_typename { static const char* name() { return "char"; } }; - template<> struct get_typename { static const char* name() { return "string"; } }; template<> struct get_typename { static const char* name() { return "value"; } }; + template<> struct get_typename { static const char* name() { return "string"; } }; template<> struct get_typename { static const char* name() { return "fc::exception"; } }; template<> struct get_typename> { static const char* name() { return "std::vector"; } }; template struct get_typename> diff --git a/include/fc/reflect/variant.hpp b/include/fc/reflect/variant.hpp index 60dcea8..b47de30 100644 --- a/include/fc/reflect/variant.hpp +++ b/include/fc/reflect/variant.hpp @@ -71,7 +71,7 @@ namespace fc { mutable_variant_object mvo; fc::reflector::visit( to_variant_visitor( mvo, v, max_depth ) ); - vo = fc::move(mvo); + vo = std::move(mvo); } template static inline void from_variant( const fc::variant& v, T& o, uint32_t max_depth ) diff --git a/include/fc/rpc/api_connection.hpp b/include/fc/rpc/api_connection.hpp index 1345c45..7280871 100644 --- a/include/fc/rpc/api_connection.hpp +++ b/include/fc/rpc/api_connection.hpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include #include #include @@ -202,7 +202,7 @@ namespace fc { std::weak_ptr _api_connection; - fc::any _api; + boost::any _api; std::map< std::string, uint32_t > _by_name; std::vector< std::function > _methods; }; // class generic_api diff --git a/include/fc/rpc/binary_api_connection.hpp b/include/fc/rpc/binary_api_connection.hpp deleted file mode 100644 index 9c9ce2f..0000000 --- a/include/fc/rpc/binary_api_connection.hpp +++ /dev/null @@ -1,532 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -//#include - -namespace fc { - class binary_api_connection; - - namespace detail { - template - class callback_functor - { - public: - typedef typename std::function::result_type result_type; - - callback_functor( std::weak_ptr< fc::binary_api_connection > con, uint64_t id ) - :_callback_id(id),_binary_api_connection(con){} - - template - result_type operator()( Args... args )const; - - private: - uint64_t _callback_id; - std::weak_ptr< fc::binary_api_connection > _binary_api_connection; - }; - - template - std::function bind_first_arg( const std::function& f, Arg0 a0 ) - { - return [=]( Args... args ) { return f( a0, args... ); }; - } - template - R call_generic( const std::function& f, variants::const_iterator a0, variants::const_iterator e ) - { - return f(); - } - - template - R call_generic( const std::function& f, variants::const_iterator a0, variants::const_iterator e ) - { - FC_ASSERT( a0 != e ); - return call_generic( bind_first_arg( f, a0->as< typename std::decay::type >() ), a0+1, e ); - } - - template - std::function to_generic( const std::function& f ) - { - return [=]( const variants& args ) { - return variant( call_generic( f, args.begin(), args.end() ) ); - }; - } - - template - std::function to_generic( const std::function& f ) - { - return [=]( const variants& args ) { - call_generic( f, args.begin(), args.end() ); - return variant(); - }; - } - - /** - * If api is returned from a remote method, the API is eagerly bound to api of - * the correct type in api_visitor::from_variant(). This binding [1] needs a reference - * to the binary_api_connection, which is made available to from_variant() as a parameter. - * - * However, in the case of a remote method which returns api_base which can subsequently - * be cast by the caller with as, we need to keep track of the connection because - * the binding is done later (when the client code actually calls as). - * - * [1] The binding actually happens in get_remote_api(). - */ - class any_api : public api_base - { - public: - any_api( api_id_type api_id, const std::shared_ptr& con ) - : _api_id(api_id), _binary_api_connection(con) {} - - virtual uint64_t get_handle()const override - { return _api_id; } - - virtual api_id_type register_api( binary_api_connection& conn )const override - { FC_ASSERT( false ); return api_id_type(); } - - api_id_type _api_id; - std::weak_ptr _binary_api_connection; - }; - - } // namespace detail - - class generic_api - { - public: - template - generic_api( const Api& a, const std::shared_ptr& c ); - - generic_api( const generic_api& cpy ) = delete; - - vector call( const string& name, const vector& args ) - { - auto itr = _by_name.find(name); - FC_ASSERT( itr != _by_name.end(), "no method with name '${name}'", ("name",name)("api",_by_name) ); - return call( itr->second, args ); - } - - vector call( uint32_t method_id, const vector& args ) - { - FC_ASSERT( method_id < _methods.size() ); - return _methods[method_id](args); - } - - std::weak_ptr< fc::binary_api_connection > get_connection() - { - return _binary_api_connection; - } - - std::vector get_method_names()const - { - std::vector result; - result.reserve( _by_name.size() ); - for( auto& m : _by_name ) result.push_back(m.first); - return result; - } - - private: - friend struct api_visitor; - - template - std::function bind_first_arg( const std::function& f, Arg0 a0 )const - { - return [=]( Args... args ) { return f( a0, args... ); }; - } - - template - R call_generic( const std::function& f, datastream& ds )const - { - return f(); - } - - template - R call_generic( const std::function,Args...)>& f, datastream& ds ) - { - uint64_t callback_id = 0; - fc::raw::unpack( ds, callback_id ); - detail::callback_functor arg0( get_connection(), callback_id ); - return call_generic( this->bind_first_arg,Args...>( f, std::function(arg0) ), ds ); - } - template - R call_generic( const std::function&,Args...)>& f, fc::datastream& ds ) - { - uint64_t callback_id = 0; - fc::raw::unpack( ds, callback_id ); - detail::callback_functor arg0( get_connection(), callback_id ); - return call_generic( this->bind_first_arg&,Args...>( f, arg0 ), ds ); - } - - template - R call_generic( const std::function& f, fc::datastream& ds ) - { - std::decay::type a0; - fc::raw::unpack( ds, a0 ); - return call_generic( this->bind_first_arg( f, a0 ), ds ); - } - - struct api_visitor - { - api_visitor( generic_api& a, const std::weak_ptr& s ):api(a),_api_con(s){ } - - template - std::function to_generic( const std::function(Args...)>& f )const; - - template - std::function to_generic( const std::function>(Args...)>& f )const; - - template - std::function to_generic( const std::function& f )const; - - template - std::function to_generic( const std::function& f )const; - - template - std::function to_generic( const std::function& f )const; - - template - void operator()( const char* name, std::function& memb )const { - api._methods.emplace_back( to_generic( memb ) ); - api._by_name[name] = api._methods.size() - 1; - } - - generic_api& api; - const std::weak_ptr& _api_con; - }; - - - std::weak_ptr _binary_api_connection; - fc::any _api; - std::map< std::string, uint32_t > _by_name; - std::vector< std::function(const vector&)> > _methods; - }; // class generic_api - - - - class binary_api_connection : public std::enable_shared_from_this - { - public: - typedef std::vector params_type; - typedef std::vector result_type; - - binary_api_connection(){} - virtual ~binary_api_connection(){}; - - - template - api get_remote_api( api_id_type api_id = 0 ) - { - api result; - result->visit( api_visitor( api_id, this->shared_from_this() ) ); - return result; - } - - /** makes calls to the remote server */ - virtual result_type send_call( api_id_type api_id, string method_name, params_type args = params_type() ) = 0; - virtual result_type send_callback( uint64_t callback_id, params_type args = params_type() ) = 0; - virtual void send_notice( uint64_t callback_id, params_type args = params_type() ) = 0; - - result_type receive_call( api_id_type api_id, const string& method_name, const params_type& args = params_type() )const - { - FC_ASSERT( _local_apis.size() > api_id ); - return _local_apis[api_id]->call( method_name, args ); - } - result_type receive_callback( uint64_t callback_id, const params_type& args = params_type() )const - { - FC_ASSERT( _local_callbacks.size() > callback_id ); - return _local_callbacks[callback_id]( args ); - } - void receive_notice( uint64_t callback_id, const params_type& args = params_type() )const - { - FC_ASSERT( _local_callbacks.size() > callback_id ); - _local_callbacks[callback_id]( args ); - } - - template - api_id_type register_api( const Interface& a ) - { - auto handle = a.get_handle(); - auto itr = _handle_to_id.find(handle); - if( itr != _handle_to_id.end() ) return itr->second; - - _local_apis.push_back( std::unique_ptr( new generic_api(a, shared_from_this() ) ) ); - _handle_to_id[handle] = _local_apis.size() - 1; - return _local_apis.size() - 1; - } - - template - uint64_t register_callback( const std::function& cb ) - { - _local_callbacks.push_back( detail::to_generic( cb ) ); - return _local_callbacks.size() - 1; - } - - std::vector get_method_names( api_id_type local_api_id = 0 )const { return _local_apis[local_api_id]->get_method_names(); } - - fc::signal closed; - private: - std::vector< std::unique_ptr > _local_apis; - std::map< uint64_t, api_id_type > _handle_to_id; - std::vector< std::function > _local_callbacks; - - - struct api_visitor - { - uint32_t _api_id; - std::shared_ptr _connection; - - api_visitor( uint32_t api_id, std::shared_ptr con ) - :_api_id(api_id),_connection(std::move(con)) - { - } - - api_visitor() = delete; - - template - static Result from_vector( const vector& v, Result*, const std::shared_ptr& ) - { - return fc::raw::unpack( v ); - } - - template - static fc::api from_vector( const vector& v, - fc::api* /*used for template deduction*/, - const std::shared_ptr& con - ) - { - return con->get_remote_api( fc::raw::unpack( v ) ); - } - - static fc::api_ptr from_vector( - const vector& v, - fc::api_ptr* /* used for template deduction */, - const std::shared_ptr& con - ) - { - return fc::api_ptr( new detail::any_api( fc::raw::unpack(v), con ) ); - } - - template - static result_type convert_callbacks( const std::shared_ptr&, const T& v ) - { - return fc::raw::pack(v); - } - - template - static result_type convert_callbacks( const std::shared_ptr& con, const std::function& v ) - { - return con->register_callback( v ); - } - - template - void operator()( const char* name, std::function& memb )const - { - auto con = _connection; - auto api_id = _api_id; - memb = [con,api_id,name]( Args... args ) { - auto var_result = con->send_call( api_id, name, { convert_callbacks(con,args)...} ); - return from_vector( var_result, (Result*)nullptr, con ); - }; - } - template - void operator()( const char* name, std::function& memb )const - { - auto con = _connection; - auto api_id = _api_id; - memb = [con,api_id,name]( Args... args ) { - con->send_call( api_id, name, { convert_callbacks(con,args)...} ); - }; - } - }; - }; - - class local_binary_api_connection : public binary_api_connection - { - public: - /** makes calls to the remote server */ - virtual result_type send_call( api_id_type api_id, string method_name, params_type args = params_type() ) override - { - FC_ASSERT( _remote_connection ); - return _remote_connection->receive_call( api_id, method_name, std::move(args) ); - } - virtual result_type send_callback( uint64_t callback_id, params_type args = params_type() ) override - { - FC_ASSERT( _remote_connection ); - return _remote_connection->receive_callback( callback_id, args ); - } - virtual void send_notice( uint64_t callback_id, params_type args = params_type() ) override - { - FC_ASSERT( _remote_connection ); - _remote_connection->receive_notice( callback_id, args ); - } - - - void set_remote_connection( const std::shared_ptr& rc ) - { - FC_ASSERT( !_remote_connection ); - FC_ASSERT( rc != this->shared_from_this() ); - _remote_connection = rc; - } - const std::shared_ptr& remote_connection()const { return _remote_connection; } - - std::shared_ptr _remote_connection; - }; - - template - generic_api::generic_api( const Api& a, const std::shared_ptr& c ) - :_binary_api_connection(c),_api(a) - { - boost::any_cast(a)->visit( api_visitor( *this, c ) ); - } - - template - std::function generic_api::api_visitor::to_generic( - const std::function(Args...)>& f )const - { - auto api_con = _api_con; - auto gapi = &api; - return [=]( const params_type& args ) { - auto con = api_con.lock(); - FC_ASSERT( con, "not connected" ); - - fc::raw::datastream ds( args.data(), args.size() ); - auto api_result = gapi->call_generic( f, args ); - return con->register_api( api_result ); - }; - } - template - std::function generic_api::api_visitor::to_generic( - const std::function>(Args...)>& f )const - { - auto api_con = _api_con; - auto gapi = &api; - return [=]( const params_type& args )-> fc::variant { - auto con = api_con.lock(); - FC_ASSERT( con, "not connected" ); - - fc::raw::datastream ds( args.data(), args.size() ); - auto api_result = gapi->call_generic( f, ds ); - if( api_result ) - return con->register_api( *api_result ); - return result_type(); - }; - } - - template - std::function generic_api::api_visitor::to_generic( - const std::function& f )const - { - auto api_con = _api_con; - auto gapi = &api; - return [=]( const variants& args ) -> fc::variant { - auto con = api_con.lock(); - FC_ASSERT( con, "not connected" ); - - fc::raw::datastream ds( args.data(), args.size() ); - auto api_result = gapi->call_generic( f, ds ); - if( !api_result ) - return result_type(); - return api_result->register_api( *con ); - }; - } - - template - std::function generic_api::api_visitor::to_generic( const std::function& f )const - { - generic_api* gapi = &api; - return [f,gapi]( const params_type& args ) { - fc::raw::datastream ds( args.data(), args.size() ); - return fc::raw::pack(gapi->call_generic( f, ds )); - }; - } - - template - std::function generic_api::api_visitor::to_generic( const std::function& f )const - { - generic_api* gapi = &api; - return [f,gapi]( const params_type& args ) { - fc::raw::datastream ds( args.data(), args.size() ); - gapi->call_generic( f, ds ); - return result_type(); - }; - } - - /** - * It is slightly unclean tight coupling to have this method in the api class. - * It breaks encapsulation by requiring an api class method to have a pointer - * to an binary_api_connection. The reason this is necessary is we have a goal of being - * able to call register_api() on an api through its base class api_base. But - * register_api() must know the template parameters! - * - * The only reasonable way to achieve the goal is to implement register_api() - * as a method in api (which obviously knows the template parameter T), - * then make the implementation accessible through the base class (by making - * it a pure virtual method in the base class which is overridden by the subclass's - * implementation). - */ - template< typename Interface, typename Transform > - api_id_type api< Interface, Transform >::register_api( binary_api_connection& conn )const - { - return conn.register_api( *this ); - } - - template< typename T > - api api_base::as() - { - // TODO: this method should probably be const (if it is not too hard) - api* maybe_requested_type = dynamic_cast< api* >(this); - if( maybe_requested_type != nullptr ) - return *maybe_requested_type; - - detail::any_api* maybe_any = dynamic_cast< detail::any_api* >(this); - FC_ASSERT( maybe_any != nullptr ); - std::shared_ptr< binary_api_connection > api_conn = maybe_any->_binary_api_connection.lock(); - FC_ASSERT( api_conn ); - return api_conn->get_remote_api( maybe_any->_api_id ); - } - - namespace detail { - template - template - typename callback_functor::result_type callback_functor::operator()( Args... args )const - { - std::shared_ptr< fc::binary_api_connection > locked = _binary_api_connection.lock(); - // TODO: make new exception type for this instead of recycling eof_exception - if( !locked ) - throw fc::eof_exception(); - - /// TODO------------->>> pack args... - locked->send_callback( _callback_id, fc::raw::pack( args... ) ).template as< result_type >(); - } - - - template - class callback_functor - { - public: - typedef void result_type; - - callback_functor( std::weak_ptr< fc::binary_api_connection > con, uint64_t id ) - :_callback_id(id),_binary_api_connection(con){} - - void operator()( Args... args )const - { - std::shared_ptr< fc::binary_api_connection > locked = _binary_api_connection.lock(); - // TODO: make new exception type for this instead of recycling eof_exception - if( !locked ) - throw fc::eof_exception(); - locked->send_notice( _callback_id, fc::variants{ args... } ); - } - - private: - uint64_t _callback_id; - std::weak_ptr< fc::binary_api_connection > _binary_api_connection; - }; - } // namespace detail - -} // fc diff --git a/include/fc/rpc/bstate.hpp b/include/fc/rpc/bstate.hpp index b7c74b7..8b398c4 100644 --- a/include/fc/rpc/bstate.hpp +++ b/include/fc/rpc/bstate.hpp @@ -32,8 +32,8 @@ namespace fc { namespace rpc { typedef std::function method; ~bstate(); - void add_method( const fc::string& name, method m ); - void remove_method( const fc::string& name ); + void add_method( const std::string& name, method m ); + void remove_method( const std::string& name ); result_type local_call( const string& method_name, const params_type& args ); void handle_reply( const bresponse& response ); diff --git a/include/fc/rpc/cli.hpp b/include/fc/rpc/cli.hpp index 00714a8..1acfb42 100644 --- a/include/fc/rpc/cli.hpp +++ b/include/fc/rpc/cli.hpp @@ -28,7 +28,7 @@ namespace fc { namespace rpc { void wait(); void format_result( const string& method, std::function formatter); - virtual void getline( const fc::string& prompt, fc::string& line ); + virtual void getline( const std::string& prompt, std::string& line ); void set_prompt( const string& prompt ); diff --git a/include/fc/rpc/json_connection.hpp b/include/fc/rpc/json_connection.hpp index af76e89..0208173 100644 --- a/include/fc/rpc/json_connection.hpp +++ b/include/fc/rpc/json_connection.hpp @@ -44,57 +44,57 @@ namespace fc { namespace rpc { * to call them. */ ///@{ - void add_method( const fc::string& name, method ); - void add_named_param_method( const fc::string& name, named_param_method ); - void remove_method( const fc::string& name ); + void add_method( const std::string& name, method ); + void add_named_param_method( const std::string& name, named_param_method ); + void remove_method( const std::string& name ); //@} /** * @name client interface */ ///@{ - void notice( const fc::string& method ); - void notice( const fc::string& method, const variants& args ); - void notice( const fc::string& method, const variant_object& named_args ); + void notice( const std::string& method ); + void notice( const std::string& method, const variants& args ); + void notice( const std::string& method, const variant_object& named_args ); /// args will be handled as named params - future async_call( const fc::string& method, + future async_call( const std::string& method, const variant_object& args ); - future async_call( const fc::string& method, mutable_variant_object args ); + future async_call( const std::string& method, mutable_variant_object args ); /// Sending in an array of variants will be handled as positional arguments - future async_call( const fc::string& method, + future async_call( const std::string& method, const variants& args ); - future async_call( const fc::string& method ); + future async_call( const std::string& method ); - future async_call( const fc::string& method, + future async_call( const std::string& method, const variant& a1 ); - future async_call( const fc::string& method, + future async_call( const std::string& method, const variant& a1, const variant& a2 ); - future async_call( const fc::string& method, + future async_call( const std::string& method, const variant& a1, const variant& a2, const variant& a3 ); - future async_call( const fc::string& method, + future async_call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, const variant& a4 ); - future async_call( const fc::string& method, + future async_call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, const variant& a4, const variant& a5 ); - future async_call( const fc::string& method, + future async_call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, @@ -102,7 +102,7 @@ namespace fc { namespace rpc { const variant& a5, const variant& a6 ); - future async_call( const fc::string& method, + future async_call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, @@ -112,7 +112,7 @@ namespace fc { namespace rpc { const variant& a7 ); - future async_call( const fc::string& method, + future async_call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, @@ -122,7 +122,7 @@ namespace fc { namespace rpc { const variant& a7, const variant& a8 ); - future async_call( const fc::string& method, + future async_call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, @@ -133,7 +133,7 @@ namespace fc { namespace rpc { const variant& a8, const variant& a9 ); - future async_call( const fc::string& method, + future async_call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, @@ -147,7 +147,7 @@ namespace fc { namespace rpc { ); template - Result call( const fc::string& method, + Result call( const std::string& method, const variants& args, microseconds timeout = microseconds::maximum()) { @@ -155,7 +155,7 @@ namespace fc { namespace rpc { } template - Result call( const fc::string& method, + Result call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, @@ -165,7 +165,7 @@ namespace fc { namespace rpc { } template - Result call( const fc::string& method, + Result call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, @@ -176,7 +176,7 @@ namespace fc { namespace rpc { } template - Result call( const fc::string& method, + Result call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, @@ -188,7 +188,7 @@ namespace fc { namespace rpc { } template - Result call( const fc::string& method, + Result call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, @@ -200,7 +200,7 @@ namespace fc { namespace rpc { return async_call( method, a1, a2, a3, a4, a5, a6).wait(timeout).as(); } template - Result call( const fc::string& method, + Result call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, @@ -214,7 +214,7 @@ namespace fc { namespace rpc { } template - Result call( const fc::string& method, + Result call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, @@ -229,7 +229,7 @@ namespace fc { namespace rpc { } template - Result call( const fc::string& method, + Result call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, @@ -245,7 +245,7 @@ namespace fc { namespace rpc { } template - Result call( const fc::string& method, + Result call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, @@ -262,7 +262,7 @@ namespace fc { namespace rpc { } template - Result call( const fc::string& method, + Result call( const std::string& method, const variant& a1, const variant& a2, microseconds timeout = microseconds::maximum()) @@ -271,7 +271,7 @@ namespace fc { namespace rpc { } template - Result call( const fc::string& method, + Result call( const std::string& method, const variant& a1, microseconds timeout = microseconds::maximum()) { @@ -279,29 +279,29 @@ namespace fc { namespace rpc { } template - Result call( const fc::string& method, + Result call( const std::string& method, variant_object a1, microseconds timeout = microseconds::maximum()) { - return async_call( method, fc::move(a1) ).wait(timeout).as(); + return async_call( method, std::move(a1) ).wait(timeout).as(); } template - Result call( const fc::string& method, + Result call( const std::string& method, mutable_variant_object a1, microseconds timeout = microseconds::maximum()) { - return async_call( method, variant_object( fc::move(a1) ) ).wait(timeout).as(); + return async_call( method, variant_object( std::move(a1) ) ).wait(timeout).as(); } template - Result call( const fc::string& method, microseconds timeout = microseconds::maximum() ) + Result call( const std::string& method, microseconds timeout = microseconds::maximum() ) { return async_call( method ).wait(timeout).as(); } /// Sending in a variant_object will be issued as named parameters - variant call( const fc::string& method, const variant_object& named_args ); + variant call( const std::string& method, const variant_object& named_args ); ///@} protected: diff --git a/include/fc/rpc/state.hpp b/include/fc/rpc/state.hpp index e031530..5d6f618 100644 --- a/include/fc/rpc/state.hpp +++ b/include/fc/rpc/state.hpp @@ -26,7 +26,7 @@ namespace fc { namespace rpc { response( int64_t i, fc::variant r, string j ):id(i),jsonrpc(j),result(r){} response( int64_t i, error_object r, string j ):id(i),jsonrpc(j),error(r){} int64_t id = 0; - optional jsonrpc; + optional jsonrpc; optional result; optional error; }; @@ -37,8 +37,8 @@ namespace fc { namespace rpc { typedef std::function method; ~state(); - void add_method( const fc::string& name, method m ); - void remove_method( const fc::string& name ); + void add_method( const std::string& name, method m ); + void remove_method( const std::string& name ); variant local_call( const string& method_name, const variants& args ); void handle_reply( const response& response ); diff --git a/include/fc/rpc/variant_connection.hpp b/include/fc/rpc/variant_connection.hpp deleted file mode 100644 index 93cb29b..0000000 --- a/include/fc/rpc/variant_connection.hpp +++ /dev/null @@ -1,140 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include - -namespace fc { namespace rpc { - - namespace detail { class variant_connection_impl; } - - /** - * @brief Implements JSON-RPC 2.0 over a set of io streams - * - * Each JSON RPC message is expected to be on its own line, violators - * will be prosecuted to the fullest extent of the law. - */ - class variant_connection - { - public: - typedef std::function method; - typedef std::function named_param_method; - - variant_connection( fc::variant_stream::ptr in, fc::variant_stream::ptr out ); - ~variant_connection(); - - /** - * Starts processing messages from input - */ - future exec(); - - logger get_logger()const; - void set_logger( const logger& l ); - - /** - * @name server interface - * - * Adding methods to the interface allows the remote side - * to call them. - */ - ///@{ - void add_method( const fc::string& name, method ); - void add_named_param_method( const fc::string& name, named_param_method ); - void remove_method( const fc::string& name ); - //@} - - /** - * @name client interface - */ - ///@{ - void notice( const fc::string& method ); - void notice( const fc::string& method, const variants& args ); - void notice( const fc::string& method, const variant_object& named_args ); - - /// args will be handled as named params - future async_call( const fc::string& method, - const variant_object& args ); - - future async_call( const fc::string& method, mutable_variant_object args ); - - /// Sending in an array of variants will be handled as positional arguments - future async_call( const fc::string& method, - const variants& args ); - - future async_call( const fc::string& method ); - - future async_call( const fc::string& method, - const variant& a1 ); - - future async_call( const fc::string& method, - const variant& a1, - const variant& a2 ); - - future async_call( const fc::string& method, - const variant& a1, - const variant& a2, - const variant& a3 ); - - template - Result call( const fc::string& method, - const variant& a1, - const variant& a2, - const variant& a3, - microseconds timeout = microseconds::maximum()) - { - return async_call( method, a1, a2, a3 ).wait(timeout).as(); - } - - template - Result call( const fc::string& method, - const variant& a1, - const variant& a2, - microseconds timeout = microseconds::maximum()) - { - return async_call( method, a1, a2 ).wait(timeout).as(); - } - - template - Result call( const fc::string& method, - const variant& a1, - microseconds timeout = microseconds::maximum()) - { - return async_call( method, a1 ).wait(timeout).as(); - } - - template - Result call( const fc::string& method, - variant_object a1, - microseconds timeout = microseconds::maximum()) - { - return async_call( method, fc::move(a1) ).wait(timeout).as(); - } - template - Result call( const fc::string& method, - mutable_variant_object a1, - microseconds timeout = microseconds::maximum()) - { - return async_call( method, variant_object( fc::move(a1) ) ).wait(timeout).as(); - } - - - template - Result call( const fc::string& method, microseconds timeout = microseconds::maximum() ) - { - return async_call( method ).wait(timeout).as(); - } - - /// Sending in a variant_object will be issued as named parameters - variant call( const fc::string& method, const variant_object& named_args ); - ///@} - - private: - std::unique_ptr my; - }; - typedef std::shared_ptr variant_connection_ptr; - -}} // fc::rpc - - - diff --git a/include/fc/rpc/variant_stream.hpp b/include/fc/rpc/variant_stream.hpp deleted file mode 100644 index 585b776..0000000 --- a/include/fc/rpc/variant_stream.hpp +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -namespace fc -{ - - /** - * Thread-safe, circular buffer for passing variants - * between threads. - */ - class variant_stream - { - public: - variant_stream( size_t s ); - ~variant_stream(); - - /** producer api */ - int64_t free(); // number of spaces available - int64_t claim( int64_t num ); - int64_t publish( int64_t pos ); - int64_t wait_free(); // wait for free space - - // producer/consumer api - variant& get( int64_t pos ); - - /** consumer api */ - int64_t begin(); // returns the first index ready to be read - int64_t end(); // returns the first index that cannot be read - int64_t wait(); // wait for variants to be posted - - private: - std::vector _variants; - uint64_t _read_pos; - uint64_t _write_pos; - }; - -} diff --git a/include/fc/scoped_exit.hpp b/include/fc/scoped_exit.hpp deleted file mode 100644 index 98ec16d..0000000 --- a/include/fc/scoped_exit.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once - -namespace fc { - - template - class scoped_exit { - public: - template - scoped_exit( C&& c ):callback( std::forward(c) ){} - scoped_exit( scoped_exit&& mv ):callback( std::move( mv.callback ) ){} - - ~scoped_exit() { - try { callback(); } catch( ... ) {} - } - - scoped_exit& operator = ( scoped_exit&& mv ) { - callback = std::move(mv); - return *this; - } - private: - scoped_exit( const scoped_exit& ); - scoped_exit& operator=( const scoped_exit& ); - - Callback callback; - }; - - template - scoped_exit make_scoped_exit( Callback&& c ) { - return scoped_exit( std::forward(c) ); - } - -} diff --git a/include/fc/shared_ptr.hpp b/include/fc/shared_ptr.hpp index 60a2cd3..cd6a94c 100644 --- a/include/fc/shared_ptr.hpp +++ b/include/fc/shared_ptr.hpp @@ -1,5 +1,7 @@ #pragma once -#include + +#include +#include namespace fc { @@ -62,7 +64,7 @@ namespace fc { return *this; } shared_ptr& operator=(shared_ptr&& p ) { - fc_swap(_ptr,p._ptr); + std::swap(_ptr,p._ptr); return *this; } T& operator* ()const { return *_ptr; } diff --git a/include/fc/signal.hpp b/include/fc/signal.hpp deleted file mode 100644 index 73c2b7f..0000000 --- a/include/fc/signal.hpp +++ /dev/null @@ -1,130 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include -#include -#ifdef emit -#undef emit -#endif - -namespace fc { - - template - class signal { - private: - typedef std::function func_type; - typedef std::vector> list_type; - public: - typedef void* connection_id_type; - - template - connection_id_type connect( Functor&& f ) { - fc::unique_lock lock(_mutex); - //auto c = new std::function( fc::forward(f) ); - _handlers.push_back( std::make_shared(f) ); - return reinterpret_cast(_handlers.back().get()); - } -#ifdef WIN32 - template - void emit( Arg&& arg ) { - list_type handlers = getHandlers(); - for( size_t i = 0; i < handlers.size(); ++i ) { - (*handlers[i])( fc::forward(arg) ); - } - } - void operator()() { - list_type handlers = getHandlers(); - for( size_t i = 0; i < handlers.size(); ++i ) { - (*handlers[i])(); - } - } - template - void operator()( Arg&& arg ) { - list_type handlers = getHandlers(); - for( size_t i = 0; i < handlers.size(); ++i ) { - (*handlers[i])( fc::forward(arg) ); - } - } - template - void emit( Arg&& arg, Arg2&& arg2 ) { - list_type handlers = getHandlers(); - for( size_t i = 0; i < handlers.size(); ++i ) { - (*handlers[i])( fc::forward(arg), fc::forward(arg2) ); - } - } - template - void operator()( Arg&& arg, Arg2&& arg2 ) { - list_type handlers = getHandlers(); - for( size_t i = 0; i < handlers.size(); ++i ) { - (*handlers[i])( fc::forward(arg), fc::forward(arg2) ); - } - } - template - void emit( Arg&& arg, Arg2&& arg2, Arg3&& arg3 ) { - list_type handlers = getHandlers(); - for( size_t i = 0; i < handlers.size(); ++i ) { - (*handlers[i])( fc::forward(arg), fc::forward(arg2), fc::forward(arg3) ); - } - } - template - void operator()( Arg&& arg, Arg2&& arg2, Arg3&& arg3 ) { - list_type handlers = getHandlers(); - for( size_t i = 0; i < handlers.size(); ++i ) { - (*handlers[i])( fc::forward(arg), fc::forward(arg2), fc::forward(arg3) ); - } - } -#else - template - void emit( Args&&... args ) { - list_type handlers = getHandlers(); - for( size_t i = 0; i < handlers.size(); ++i ) { - (*handlers[i])( fc::forward(args)... ); - } - } - template - void operator()( Args&&... args ) { - list_type handlers = getHandlers(); - for( size_t i = 0; i < handlers.size(); ++i ) { - (*handlers[i])( fc::forward(args)... ); - } - } -#endif - - void disconnect( connection_id_type cid ) { - fc::unique_lock lock(_mutex); - auto itr = _handlers.begin(); - while( itr != _handlers.end() ) { - if( reinterpret_cast(itr->get()) == cid ) { - _handlers.erase(itr); - } - ++itr; - } - } - signal() - { - _handlers.reserve(4); - } - //~signal() - //{ - // for( auto itr = _handlers.begin(); itr != _handlers.end(); ++itr ) - // { - // delete *itr; - // } - // _handlers.clear(); - //} - - private: - fc::mutex _mutex; - list_type _handlers; - - list_type getHandlers() - { - fc::unique_lock lock(_mutex); - list_type handlers(_handlers); - return handlers; - } - }; - -} diff --git a/include/fc/string.hpp b/include/fc/string.hpp index 13e10da..7fbe2e5 100644 --- a/include/fc/string.hpp +++ b/include/fc/string.hpp @@ -1,154 +1,32 @@ #pragma once -#include #include #include - -#ifndef USE_FC_STRING #include + namespace fc { - typedef std::string string; + using std::string; - int64_t to_int64( const fc::string& ); - uint64_t to_uint64( const fc::string& ); - double to_double( const fc::string& ); - fc::string to_string( double ); - fc::string to_string( uint64_t ); - fc::string to_string( int64_t ); - fc::string to_string( uint16_t ); + int64_t to_int64( const std::string& ); + uint64_t to_uint64( const std::string& ); + double to_double( const std::string& ); + std::string to_string( double ); + std::string to_string( uint64_t ); + std::string to_string( int64_t ); + std::string to_string( uint16_t ); std::string to_pretty_string( int64_t ); - inline fc::string to_string( int32_t v ) { return to_string( int64_t(v) ); } - inline fc::string to_string( uint32_t v ){ return to_string( uint64_t(v) ); } + inline std::string to_string( int32_t v ) { return to_string( int64_t(v) ); } + inline std::string to_string( uint32_t v ){ return to_string( uint64_t(v) ); } #ifdef __APPLE__ - inline fc::string to_string( size_t s) { return to_string(uint64_t(s)); } + inline std::string to_string( size_t s) { return to_string(uint64_t(s)); } #endif - typedef fc::optional ostring; + typedef fc::optional ostring; class variant_object; - fc::string format_string( const fc::string&, const variant_object&, uint32_t max_object_depth = 200 ); - fc::string trim( const fc::string& ); - fc::string to_lower( const fc::string& ); + std::string format_string( const std::string&, const variant_object&, uint32_t max_object_depth = 200 ); + std::string trim( const std::string& ); + std::string to_lower( const std::string& ); string trim_and_normalize_spaces( const string& s ); uint64_t parse_size( const string& s ); } - -#else - -/** - * There is debate about whether doing this is 'standard conforming', but - * it works everywhere and enables the purpose of this library which is to - * accelerate compiles while maintaining compatability. - */ -namespace std { - template - struct char_traits; - - template - class allocator; - - template - class basic_string; - - typedef basic_string, allocator > string; -} - - -namespace fc { - /** - * @brief wrapper on std::string - * - * Including results in 4000 lines of code - * that must be included to build your header. This - * class hides all of those details while maintaining - * compatability with std::string. Using fc::string - * instead of std::string can accelerate compile times - * 10x. - * - * The implementation of this class is std::string, this header simply - * accelerates compile times. fc::string is automatically convertable to / from - * std::string. - */ - class string { - public: - typedef char* iterator; - typedef const char* const_iterator; - enum { npos = size_t(-1) }; - // static const size_t npos;// = -1; - - string(); - string( const std::string& s ); - string( std::string&& s ); - string( const string& c ); - string( string&& c ); - string( const char* c ); - string( const char* c, int s ); - string( const_iterator b, const_iterator e ); - ~string(); - - operator std::string&(); - operator const std::string&()const; - - iterator begin(); - iterator end(); - - const_iterator begin()const; - const_iterator end()const; - - char& operator[](size_t idx); - const char& operator[](size_t idx)const; - - string& operator =( const string& c ); - string& operator =( string&& c ); - - void reserve( size_t ); - size_t size()const; - size_t find( char c, size_t pos = 0 )const; - size_t find(const fc::string& str, size_t pos = 0) const; - size_t find(const char* s, size_t pos = 0) const; - size_t rfind( char c, size_t pos = npos )const; - size_t rfind( const char* c, size_t pos = npos )const; - size_t rfind( const fc::string& c, size_t pos = npos )const; - size_t find_first_of (const fc::string& str, size_t pos = 0) const; - size_t find_first_of (const char* s, size_t pos = 0) const; - string& replace(size_t pos, size_t len, const fc::string& str); - string& replace(size_t pos, size_t len, const char* s); - - void resize( size_t s ); - void clear(); - - const char* c_str()const; - char* data(); - - bool operator == ( const char* s )const; - bool operator == ( const string& s )const; - bool operator != ( const string& s )const; - - friend bool operator < ( const string& a, const string& b ); - - string& operator+=( const string& s ); - string& operator+=( char c ); - - friend string operator + ( const string&, const string& ); - friend string operator + ( const string&, char c ); - - fc::string substr( size_t start, size_t len = fc::string::npos )const; - - private: - fc::fwd my; - }; - - int64_t to_int64( const fc::string& ); - uint64_t to_uint64( const fc::string& ); - double to_double( const fc::string& ); - fc::string to_string( double ); - fc::string to_string( uint64_t ); - fc::string to_string( int64_t ); - - typedef fc::optional ostring; - class variant_object; - fc::string format_string( const fc::string&, const variant_object& ); - -} // namespace fc - -#endif diff --git a/include/fc/thread/future.hpp b/include/fc/thread/future.hpp index 0d0e2e7..f0365ea 100644 --- a/include/fc/thread/future.hpp +++ b/include/fc/thread/future.hpp @@ -1,5 +1,4 @@ #pragma once -#include #include #include #include @@ -35,7 +34,7 @@ namespace fc { template class completion_handler_impl : public completion_handler { public: - completion_handler_impl( Functor&& f ):_func(fc::move(f)){} + completion_handler_impl( Functor&& f ):_func(std::move(f)){} completion_handler_impl( const Functor& f ):_func(f){} virtual void on_complete( const void* v, const fc::exception_ptr& e ) { @@ -47,7 +46,7 @@ namespace fc { template class completion_handler_impl : public completion_handler { public: - completion_handler_impl( Functor&& f ):_func(fc::move(f)){} + completion_handler_impl( Functor&& f ):_func(std::move(f)){} completion_handler_impl( const Functor& f ):_func(f){} virtual void on_complete( const void* v, const fc::exception_ptr& e ) { _func(e); @@ -110,7 +109,7 @@ namespace fc { typedef fc::shared_ptr< promise > ptr; promise( const char* desc FC_TASK_NAME_DEFAULT_ARG):promise_base(desc){} promise( const T& val ){ set_value(val); } - promise( T&& val ){ set_value(fc::move(val) ); } + promise( T&& val ){ set_value(std::move(val) ); } const T& wait(const microseconds& timeout = microseconds::maximum() ){ this->_wait( timeout ); @@ -127,7 +126,7 @@ namespace fc { } void set_value( T&& v ) { - result = fc::move(v); + result = std::move(v); _set_value(&*result); } @@ -186,12 +185,12 @@ namespace fc { class future { public: future( const fc::shared_ptr>& p ):m_prom(p){} - future( fc::shared_ptr>&& p ):m_prom(fc::move(p)){} + future( fc::shared_ptr>&& p ):m_prom(std::move(p)){} future(const future& f ) : m_prom(f.m_prom){} future(){} future& operator=(future&& f ) { - fc_swap(m_prom,f.m_prom); + std::swap(m_prom,f.m_prom); return *this; } @@ -259,12 +258,12 @@ namespace fc { class future { public: future( const fc::shared_ptr>& p ):m_prom(p){} - future( fc::shared_ptr>&& p ):m_prom(fc::move(p)){} + future( fc::shared_ptr>&& p ):m_prom(std::move(p)){} future(const future& f ) : m_prom(f.m_prom){} future(){} future& operator=(future&& f ) { - fc_swap(m_prom,f.m_prom); + std::swap(m_prom,f.m_prom); return *this; } diff --git a/include/fc/thread/task.hpp b/include/fc/thread/task.hpp index 6c79d71..987810b 100644 --- a/include/fc/thread/task.hpp +++ b/include/fc/thread/task.hpp @@ -1,7 +1,6 @@ #pragma once #include #include -#include #include namespace fc { @@ -103,7 +102,7 @@ namespace fc { } virtual void cancel(const char* reason FC_CANCELATION_REASON_DEFAULT_ARG) override { task_base::cancel(reason); } - aligned _functor; + alignas(double) char _functor[FunctorSize]; private: ~task(){} }; @@ -123,7 +122,7 @@ namespace fc { } virtual void cancel(const char* reason FC_CANCELATION_REASON_DEFAULT_ARG) override { task_base::cancel(reason); } - aligned _functor; + alignas(double) char _functor[FunctorSize]; private: ~task(){} }; diff --git a/include/fc/thread/thread.hpp b/include/fc/thread/thread.hpp index 282fc30..7a886fa 100644 --- a/include/fc/thread/thread.hpp +++ b/include/fc/thread/thread.hpp @@ -3,8 +3,6 @@ #define FC_CONTEXT_STACK_SIZE (2048*1024) #include -#include -#include namespace fc { class time_point; @@ -75,7 +73,7 @@ namespace fc { * @note debug info is more useful if you provide a description for your * async tasks and promises. */ - void debug( const fc::string& d ); + void debug( const std::string& d ); /** @@ -146,7 +144,7 @@ namespace fc { std::vector proms(2); proms[0] = fc::static_pointer_cast(f1.m_prom); proms[1] = fc::static_pointer_cast(f2.m_prom); - return wait_any_until(fc::move(proms), fc::time_point::now()+timeout_us ); + return wait_any_until(std::move(proms), fc::time_point::now()+timeout_us ); } private: thread( class thread_d* ); // parameter is ignored, will create a new thread_d diff --git a/include/fc/thread/wait_condition.hpp b/include/fc/thread/wait_condition.hpp deleted file mode 100644 index 5006f87..0000000 --- a/include/fc/thread/wait_condition.hpp +++ /dev/null @@ -1,76 +0,0 @@ -#pragma once -#include -#include -#include -#include - -namespace fc -{ - /** - * A thread-safe, fiber-aware condition variable that - * can be used to signal/wait on a certain condition between - * threads. - */ - template - class wait_condition - { - public: - wait_condition(const char* name) : _name(name) {} - - void wait( const microseconds& timeout = microseconds::maximum() ) - { - typename fc::promise::ptr p = new fc::promise(_name); - { synchronized( _prom_lock ) - _promises.push_back( p ); - } - p->wait(timeout); - } - - template - T wait( LockType& l, const microseconds& timeout = microseconds::maximum() ) - { - typename fc::promise::ptr p( new fc::promise(_name)); - { synchronized( _prom_lock ) - _promises.push_back( p ); - } - l.unlock(); - struct relocker { - LockType& _lock; - relocker(LockType& l) : _lock(l) {} - ~relocker() { _lock.lock(); } - } lock_on_exit(l); - return p->wait(timeout); - } - - void notify_one( const T& t=T()) - { - typename fc::promise::ptr prom; - { synchronized( _prom_lock ) - if( _promises.size() ) - { - prom = _promises.front(); - _promises.pop_front(); - } - } - - if( prom && prom->retain_count() > 1 ) - prom->set_value(t); - } - void notify_all(const T& t=T()) - { - std::deque::ptr> all; - { synchronized( _prom_lock ) - std::swap(all, _promises); - } - for( auto itr = all.begin(); itr != all.end(); ++itr ) - { - if( (*itr)->retain_count() > 1 ) - (*itr)->set_value(t); - } - } - private: - fc::spin_yield_lock _prom_lock; - std::deque::ptr> _promises; - const char *const _name; - }; -} diff --git a/include/fc/time.hpp b/include/fc/time.hpp index c78c0f3..7eba301 100644 --- a/include/fc/time.hpp +++ b/include/fc/time.hpp @@ -1,6 +1,6 @@ #pragma once #include -#include +#include #include #ifdef _MSC_VER @@ -48,8 +48,8 @@ namespace fc { static time_point maximum() { return time_point( microseconds::maximum() ); } static time_point min() { return time_point(); } - operator fc::string()const; - static time_point from_iso_string( const fc::string& s ); + operator std::string()const; + static time_point from_iso_string( const std::string& s ); const microseconds& time_since_epoch()const { return elapsed; } uint32_t sec_since_epoch()const { return elapsed.count() / 1000000; } @@ -112,11 +112,11 @@ namespace fc { friend microseconds operator - ( const time_point_sec& t, const time_point_sec& m ) { return time_point(t) - time_point(m); } friend microseconds operator - ( const time_point& t, const time_point_sec& m ) { return time_point(t) - time_point(m); } - fc::string to_non_delimited_iso_string()const; - fc::string to_iso_string()const; + std::string to_non_delimited_iso_string()const; + std::string to_iso_string()const; - operator fc::string()const; - static time_point_sec from_iso_string( const fc::string& s ); + operator std::string()const; + static time_point_sec from_iso_string( const std::string& s ); private: uint32_t utc_seconds; @@ -127,12 +127,12 @@ namespace fc { /** return a human-readable approximate time, relative to now() * e.g., "4 hours ago", "2 months ago", etc. */ - string get_approximate_relative_time_string(const time_point_sec& event_time, - const time_point_sec& relative_to_time = fc::time_point::now(), - const std::string& ago = " ago"); - string get_approximate_relative_time_string(const time_point& event_time, - const time_point& relative_to_time = fc::time_point::now(), - const std::string& ago = " ago"); + std::string get_approximate_relative_time_string(const time_point_sec& event_time, + const time_point_sec& relative_to_time = fc::time_point::now(), + const std::string& ago = " ago"); + std::string get_approximate_relative_time_string(const time_point& event_time, + const time_point& relative_to_time = fc::time_point::now(), + const std::string& ago = " ago"); } #include diff --git a/include/fc/tuple.hpp b/include/fc/tuple.hpp deleted file mode 100644 index 9fe9638..0000000 --- a/include/fc/tuple.hpp +++ /dev/null @@ -1,136 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include -#include -#include -#include - -namespace fc { - - /** - * Provides a fast-compiling tuple that doesn't use fancy meta-programming - * techniques. It is limited to 4 parameters which is sufficient for most - * methods argument lists which is the primary use case for this tuple. Methods - * that require more than 4 parameters are probably better served by defining - * a struct. - * - * The members of the tuple are easily visited with a simple visitor functor - * of the form: - * @code - * struct visitor { - * template - * void operator()( MemberType& m ); - * - * template - * void operator()( const MemberType& m ); - * }; - * @endcode - template - struct tuple { - tuple(){} - enum size_enum { size = 4 }; - - template - tuple( AA&& aa, BB&& bb, CC&& cc, DD&& dd ) - :a( fc::forward(aa) ), - b( fc::forward(bb) ), - c( fc::forward(cc) ), - d( fc::forward
(dd) ) - {} - - template - void visit( V&& v ) { v(a); v(b); v(c); v(d); } - template - void visit( V&& v )const { v(a); v(b); v(c); v(d); } - - A a; - B b; - C c; - D d; - }; - */ - template struct tuple{}; - - template<> - struct tuple<> { - enum size_enum { size = 0 }; - template - void visit( V&& v)const{}; - }; - template - auto call_fused( Functor f, const tuple<>& t ) -> decltype( f( ) ) { - return f(); - } - - inline tuple<> make_tuple(){ return tuple<>(); } - - template - struct is_tuple { - typedef fc::false_type type; - }; - - #define RREF_PARAMS(z,n,data) BOOST_PP_CAT(AA,n)&& BOOST_PP_CAT(p,n) - #define ILIST_PARAMS(z,n,data) BOOST_PP_CAT(a,n)( fc::forward( BOOST_PP_CAT(p,n) ) ) - #define ILIST_PARAMS_COPY(z,n,data) BOOST_PP_CAT(a,n)( t.BOOST_PP_CAT(a,n) ) - #define VISIT_PARAMS(z,n,data) v(BOOST_PP_CAT(a,n)); - #define LIST_MEMBERS_ON(z,n,data) data.BOOST_PP_CAT(a,n) - #define DEDUCE_MEMBERS(z,n,data) typename fc::deduce::type - #define FORWARD_PARAMS(z,n,data) fc::forward(BOOST_PP_CAT(p,n)) - #define MEM_PARAMS(z,n,data) BOOST_PP_CAT(A,n) BOOST_PP_CAT(a,n); - #define TUPLE(z,n,unused) \ - template \ - struct tuple { \ - enum size_enum { size = n }; \ - template \ - explicit tuple( BOOST_PP_ENUM(n, RREF_PARAMS, unused ) )BOOST_PP_IF(n,:,BOOST_PP_EMPTY())BOOST_PP_ENUM( n, ILIST_PARAMS,unused){} \ - tuple( const tuple& t )BOOST_PP_IF(n,:,BOOST_PP_EMPTY())BOOST_PP_ENUM( n, ILIST_PARAMS_COPY,unused){} \ - tuple( tuple&& t )BOOST_PP_IF(n,:,BOOST_PP_EMPTY())BOOST_PP_ENUM( n, ILIST_PARAMS_COPY,unused){} \ - tuple(){}\ - template\ - void visit( V&& v ) { BOOST_PP_REPEAT(n,VISIT_PARAMS,a) }\ - template\ - void visit( V&& v )const { BOOST_PP_REPEAT(n,VISIT_PARAMS,a) }\ - BOOST_PP_REPEAT(n,MEM_PARAMS,a) \ - }; \ - template \ - tuple make_tuple( BOOST_PP_ENUM( n, RREF_PARAMS, unused) ) { \ - return tuple( BOOST_PP_ENUM( n, FORWARD_PARAMS,unused ) ); \ - } \ - template \ - auto call_fused( Functor f, tuple& t ) \ - -> decltype( f( BOOST_PP_ENUM( n, LIST_MEMBERS_ON, t) ) ) { \ - return f( BOOST_PP_ENUM( n, LIST_MEMBERS_ON, t) ); \ - } \ - template \ - auto call_fused( Functor f, const tuple& t ) \ - -> decltype( f( BOOST_PP_ENUM( n, LIST_MEMBERS_ON, t) ) ) { \ - return f( BOOST_PP_ENUM( n, LIST_MEMBERS_ON, t) ); \ - } \ - template \ - struct is_tuple > { \ - typedef fc::true_type type; \ - }; \ - template \ - struct deduce > { \ - typedef fc::tuple type; \ - }; - - BOOST_PP_REPEAT_FROM_TO( 1, 5, TUPLE, unused ) - - - #undef FORWARD_PARAMS - #undef DEDUCE_MEMBERS - #undef RREF_PARAMS - #undef LIST_MEMBERS_ON - #undef ILIST_PARAMS - #undef ILIST_PARAMS_COPY - #undef VISIT_PARAMS - #undef MEM_PARAMS - #undef TUPLE - - -} - diff --git a/include/fc/unique_ptr.hpp b/include/fc/unique_ptr.hpp deleted file mode 100644 index 609eff8..0000000 --- a/include/fc/unique_ptr.hpp +++ /dev/null @@ -1,60 +0,0 @@ -#pragma once -#include -#include - -namespace fc -{ - template - class unique_ptr - { - public: - typedef T* pointer; - - explicit unique_ptr( pointer t = nullptr ):_p(t){} - - unique_ptr( unique_ptr&& m ) - :_p(m._p){ m._p = nullptr; } - - ~unique_ptr() { delete _p; } - - operator bool()const { return _p != nullptr; } - friend bool operator==(const unique_ptr& p, nullptr_t) - { - return p._p == nullptr; - } - - friend bool operator!=(const unique_ptr& p, nullptr_t) - { - return p._p != nullptr; - } - - unique_ptr& operator=( nullptr_t ) - { - delete _p; _p = nullptr; - } - - unique_ptr& operator=( unique_ptr&& o ) - { - fc_swap( _p, o._p ); - return *this; - } - - pointer operator->()const { return _p; } - T& operator*()const { return *_p; } - - void reset( pointer v ) - { - delete _p; _p = v; - } - pointer release() - { - auto tmp = _p; - _p = nullptr; - return tmp; - } - - private: - pointer _p; - }; - -} diff --git a/include/fc/utility.hpp b/include/fc/utility.hpp index 0096ac5..e0e6371 100644 --- a/include/fc/utility.hpp +++ b/include/fc/utility.hpp @@ -19,19 +19,12 @@ namespace fc { using std::size_t; typedef decltype(nullptr) nullptr_t; - template struct remove_reference { typedef T type; }; - template struct remove_reference { typedef T type; }; - template struct remove_reference { typedef T type; }; - template struct deduce { typedef T type; }; template struct deduce { typedef T type; }; template struct deduce { typedef T type; }; template struct deduce { typedef T type; }; template struct deduce{ typedef T type; }; - template - typename fc::remove_reference::type&& move( T&& t ) { return static_cast::type&&>(t); } - template inline T&& forward( U&& u ) { return static_cast(u); } @@ -45,21 +38,4 @@ namespace fc { template struct is_class { typedef decltype(detail::is_class_helper(0)) type; enum value_enum { value = type::value }; }; -#ifdef min -#undef min -#endif - template - const T& min( const T& a, const T& b ) { return a < b ? a: b; } - } - // outside of namespace fc becuase of VC++ conflict with std::swap - template - void fc_swap( T& a, T& b ) { - T tmp = fc::move(a); - a = fc::move(b); - b = fc::move(tmp); - } - -#define LLCONST(constant) static_cast(constant##ll) -#define ULLCONST(constant) static_cast(constant##ull) - diff --git a/include/fc/variant.hpp b/include/fc/variant.hpp index 03b7eda..c98e796 100644 --- a/include/fc/variant.hpp +++ b/include/fc/variant.hpp @@ -11,8 +11,6 @@ #include // memset #include -#include -#include #include #include @@ -217,7 +215,7 @@ namespace fc variant( double val, uint32_t max_depth = 1 ); variant( bool val, uint32_t max_depth = 1 ); variant( blob val, uint32_t max_depth = 1 ); - variant( fc::string val, uint32_t max_depth = 1 ); + variant( std::string val, uint32_t max_depth = 1 ); variant( variant_object, uint32_t max_depth = 1 ); variant( mutable_variant_object, uint32_t max_depth = 1 ); variant( variants, uint32_t max_depth = 1 ); @@ -238,7 +236,7 @@ namespace fc virtual void handle( const uint64_t& v )const = 0; virtual void handle( const double& v )const = 0; virtual void handle( const bool& v )const = 0; - virtual void handle( const string& v )const = 0; + virtual void handle( const std::string& v )const = 0; virtual void handle( const variant_object& v)const = 0; virtual void handle( const variants& v)const = 0; }; @@ -277,10 +275,10 @@ namespace fc /** Convert's double, ints, bools, etc to a string * @throw if get_type() == array_type | get_type() == object_type */ - string as_string()const; + std::string as_string()const; /// @pre get_type() == string_type - const string& get_string()const; + const std::string& get_string()const; /// @throw if get_type() != array_type | null_type variants& get_array(); @@ -357,7 +355,7 @@ namespace fc typedef optional ovariant; /** @ingroup Serializable */ - void from_variant( const variant& var, string& vo, uint32_t max_depth = 1 ); + void from_variant( const variant& var, std::string& vo, uint32_t max_depth = 1 ); /** @ingroup Serializable */ void from_variant( const variant& var, variants& vo, uint32_t max_depth ); void from_variant( const variant& var, variant& vo, uint32_t max_depth ); diff --git a/include/fc/variant_object.hpp b/include/fc/variant_object.hpp index b516630..73edd26 100644 --- a/include/fc/variant_object.hpp +++ b/include/fc/variant_object.hpp @@ -1,7 +1,6 @@ #pragma once #include #include -#include namespace fc { diff --git a/include/fc/vector.hpp b/include/fc/vector.hpp deleted file mode 100644 index 29b3388..0000000 --- a/include/fc/vector.hpp +++ /dev/null @@ -1,2 +0,0 @@ -#pragma once -#include diff --git a/include/fc/vector_fwd.hpp b/include/fc/vector_fwd.hpp deleted file mode 100644 index a57fec7..0000000 --- a/include/fc/vector_fwd.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef _FC_VECTOR_FWD_HPP_ -#define _FC_VECTOR_FWD_HPP_ -#if 0 -#include -#else -namespace fc { - template class vector; -}; -#endif - -#endif // _FC_VECTOR_FWD_HPP_ diff --git a/include/fc/wait_any.hpp b/include/fc/wait_any.hpp deleted file mode 100644 index c3d6207..0000000 --- a/include/fc/wait_any.hpp +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once -#include -#include - -namespace fc { - template - int wait_any( fc::future& f1, fc::future& f2, const microseconds& timeout_us = microseconds::max() ) { - fc::vector p(2); - p[0] = static_pointer_cast(f1.promise()); - p[1] = static_pointer_cast(f2.promise()); - return wait( fc::move(p), timeout_us ); - } -} diff --git a/src/compress/zlib.cpp b/src/compress/zlib.cpp index dd9d23b..b883596 100644 --- a/src/compress/zlib.cpp +++ b/src/compress/zlib.cpp @@ -4,11 +4,11 @@ namespace fc { - string zlib_compress(const string& in) + std::string zlib_compress(const std::string& in) { size_t compressed_message_length; char* compressed_message = (char*)tdefl_compress_mem_to_heap(in.c_str(), in.size(), &compressed_message_length, TDEFL_WRITE_ZLIB_HEADER | TDEFL_DEFAULT_MAX_PROBES); - string result(compressed_message, compressed_message_length); + std::string result(compressed_message, compressed_message_length); free(compressed_message); return result; } diff --git a/src/crypto/base36.cpp b/src/crypto/base36.cpp index 5e3bd5b..c4a9d23 100644 --- a/src/crypto/base36.cpp +++ b/src/crypto/base36.cpp @@ -1,13 +1,14 @@ #include +#include #include #include #include namespace fc { - fc::string to_base36( const char* data, size_t len ) + std::string to_base36( const char* data, size_t len ) { - if( len == 0 ) return fc::string(); + if( len == 0 ) return std::string(); const char* src = data; int src_len = len; @@ -34,15 +35,15 @@ namespace fc while (len-- > 0 && *data++ == 0) { out[--pos] = '0'; } - return &out[pos]; //fc::string( &out[pos], out.size() - pos); + return &out[pos]; //std::string( &out[pos], out.size() - pos); } - fc::string to_base36( const std::vector& vec ) + std::string to_base36( const std::vector& vec ) { return to_base36( (const char*)vec.data(), vec.size() ); } - std::vector from_base36( const fc::string& b36 ) + std::vector from_base36( const std::string& b36 ) { if ( b36.empty() ) { std::vector empty; @@ -63,7 +64,7 @@ namespace fc value = value + (_36.exp(pos) * fc::bigint(10+*itr - 'A')); else { - wlog("unknown '${char}'", ("char",fc::string(&*itr,1)) ); + wlog("unknown '${char}'", ("char",std::string(&*itr,1)) ); } ++pos; } diff --git a/src/crypto/base58.cpp b/src/crypto/base58.cpp index 6cc7e58..929f387 100644 --- a/src/crypto/base58.cpp +++ b/src/crypto/base58.cpp @@ -22,7 +22,6 @@ #include #include -#include #include #include diff --git a/src/crypto/bigint.cpp b/src/crypto/bigint.cpp index 478f949..fd5a688 100644 --- a/src/crypto/bigint.cpp +++ b/src/crypto/bigint.cpp @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -145,7 +144,7 @@ namespace fc { BN_CTX* ctx = BN_CTX_new(); bigint tmp; BN_div( tmp.n, NULL, n, a.n, ctx ); - fc_swap( tmp.n, n ); + std::swap( tmp.n, n ); BN_CTX_free(ctx); return tmp; } @@ -190,7 +189,7 @@ namespace fc { bigint& bigint::operator = ( bigint&& a ) { if( &a == this ) return *this; - fc_swap( a.n, n ); + std::swap( a.n, n ); return *this; } bigint& bigint::operator = ( const bigint& a ) { @@ -199,7 +198,7 @@ namespace fc { BN_copy( n, a.n ); return *this; } - bigint::operator fc::string()const { + bigint::operator std::string()const { return BN_bn2dec(n); } diff --git a/src/crypto/elliptic_common.cpp b/src/crypto/elliptic_common.cpp index 7d8b6ed..1e74981 100644 --- a/src/crypto/elliptic_common.cpp +++ b/src/crypto/elliptic_common.cpp @@ -229,7 +229,7 @@ namespace fc { namespace ecc { return private_key( k ); } - static fc::string _to_base58( const extended_key_data& key ) + static std::string _to_base58( const extended_key_data& key ) { size_t buf_len = key.size() + 4; char *buffer = (char*)alloca(buf_len); @@ -239,7 +239,7 @@ namespace fc { namespace ecc { return fc::to_base58( buffer, buf_len ); } - static void _parse_extended_data( unsigned char* buffer, fc::string base58 ) + static void _parse_extended_data( unsigned char* buffer, std::string base58 ) { memset( buffer, 0, 78 ); std::vector decoded = fc::from_base58( base58 ); @@ -276,12 +276,12 @@ namespace fc { namespace ecc { return from_base58( _to_base58( data ) ); } - fc::string extended_public_key::str() const + std::string extended_public_key::str() const { return _to_base58( serialize_extended() ); } - extended_public_key extended_public_key::from_base58( const fc::string& base58 ) + extended_public_key extended_public_key::from_base58( const std::string& base58 ) { unsigned char buffer[78]; unsigned char* ptr = buffer; @@ -344,12 +344,12 @@ namespace fc { namespace ecc { return from_base58( _to_base58( data ) ); } - fc::string extended_private_key::str() const + std::string extended_private_key::str() const { return _to_base58( serialize_extended() ); } - extended_private_key extended_private_key::from_base58( const fc::string& base58 ) + extended_private_key extended_private_key::from_base58( const std::string& base58 ) { unsigned char buffer[78]; unsigned char* ptr = buffer; @@ -366,7 +366,7 @@ namespace fc { namespace ecc { return extended_private_key( private_key::regenerate(key), chain, cn, fp, d ); } - extended_private_key extended_private_key::generate_master( const fc::string& seed ) + extended_private_key extended_private_key::generate_master( const std::string& seed ) { return generate_master( seed.c_str(), seed.size() ); } diff --git a/src/crypto/hex.cpp b/src/crypto/hex.cpp index 67ef919..5a585bd 100644 --- a/src/crypto/hex.cpp +++ b/src/crypto/hex.cpp @@ -10,7 +10,7 @@ namespace fc { return c - 'a' + 10; if( c >= 'A' && c <= 'F' ) return c - 'A' + 10; - FC_THROW_EXCEPTION( exception, "Invalid hex character '${c}'", ("c", fc::string(&c,1) ) ); + FC_THROW_EXCEPTION( exception, "Invalid hex character '${c}'", ("c", std::string(&c,1) ) ); return 0; } @@ -24,8 +24,8 @@ namespace fc { return r; } - size_t from_hex( const fc::string& hex_str, char* out_data, size_t out_data_len ) { - fc::string::const_iterator i = hex_str.begin(); + size_t from_hex( const std::string& hex_str, char* out_data, size_t out_data_len ) { + std::string::const_iterator i = hex_str.begin(); uint8_t* out_pos = (uint8_t*)out_data; uint8_t* out_end = out_pos + out_data_len; while( i != hex_str.end() && out_end != out_pos ) { diff --git a/src/crypto/pke.cpp b/src/crypto/pke.cpp index 28cd60d..87ee25a 100644 --- a/src/crypto/pke.cpp +++ b/src/crypto/pke.cpp @@ -97,7 +97,7 @@ namespace fc { out.resize(rtn); return out; } - FC_THROW_EXCEPTION( exception, "openssl: ${message}", ("message",fc::string(ERR_error_string( ERR_get_error(),NULL))) ); + FC_THROW_EXCEPTION( exception, "openssl: ${message}", ("message",std::string(ERR_error_string( ERR_get_error(),NULL))) ); } bytes public_key::encrypt( const bytes& in )const @@ -113,7 +113,7 @@ namespace fc { out.resize(rtn); return out; } - FC_THROW_EXCEPTION( exception, "openssl: ${message}", ("message",fc::string(ERR_error_string( ERR_get_error(),NULL))) ); + FC_THROW_EXCEPTION( exception, "openssl: ${message}", ("message",std::string(ERR_error_string( ERR_get_error(),NULL))) ); } bytes public_key::decrypt( const bytes& in )const { @@ -127,7 +127,7 @@ namespace fc { out.resize(rtn); return out; } - FC_THROW_EXCEPTION( exception, "openssl: ${message}", ("message",fc::string(ERR_error_string( ERR_get_error(),NULL))) ); + FC_THROW_EXCEPTION( exception, "openssl: ${message}", ("message",std::string(ERR_error_string( ERR_get_error(),NULL))) ); } bytes public_key::serialize()const @@ -140,14 +140,14 @@ namespace fc { if( e != 1 ) { BIO_free(mem); - FC_THROW_EXCEPTION( exception, "openssl: ${message}", ("message",fc::string(ERR_error_string( ERR_get_error(),NULL))) ); + FC_THROW_EXCEPTION( exception, "openssl: ${message}", ("message",std::string(ERR_error_string( ERR_get_error(),NULL))) ); } char* dat; uint32_t l = BIO_get_mem_data( mem, &dat ); fc::stringstream ss( string( dat, l ) ); fc::stringstream key; - fc::string tmp; + std::string tmp; fc::getline( ss, tmp ); fc::getline( ss, tmp ); while( tmp.size() && tmp[0] != '-' ) @@ -210,7 +210,7 @@ namespace fc { if( 1 != RSA_sign( NID_sha1, (uint8_t*)&digest, 20, (unsigned char*)&sig, &slen, my->rsa ) ) { - FC_THROW_EXCEPTION( exception, "rsa sign failed with ${message}", ("message",fc::string(ERR_error_string( ERR_get_error(),NULL))) ); + FC_THROW_EXCEPTION( exception, "rsa sign failed with ${message}", ("message",std::string(ERR_error_string( ERR_get_error(),NULL))) ); } } @@ -224,7 +224,7 @@ namespace fc { if( 1 != RSA_sign( NID_sha1, (uint8_t*)digest.data(), 20, (unsigned char*)sig.data(), &slen, my->rsa ) ) { - FC_THROW_EXCEPTION( exception, "rsa sign failed with ${message}", ("message",fc::string(ERR_error_string( ERR_get_error(),NULL))) ); + FC_THROW_EXCEPTION( exception, "rsa sign failed with ${message}", ("message",std::string(ERR_error_string( ERR_get_error(),NULL))) ); } return sig; } @@ -238,7 +238,7 @@ namespace fc { if( 1 != RSA_sign( NID_sha256, (uint8_t*)digest.data(), 32, (unsigned char*)sig.data(), &slen, my->rsa ) ) { - FC_THROW_EXCEPTION( exception, "rsa sign failed with ${message}", ("message",fc::string(ERR_error_string( ERR_get_error(),NULL))) ); + FC_THROW_EXCEPTION( exception, "rsa sign failed with ${message}", ("message",std::string(ERR_error_string( ERR_get_error(),NULL))) ); } return sig; } @@ -302,7 +302,7 @@ namespace fc { if( e != 1 ) { BIO_free(mem); - FC_THROW_EXCEPTION( exception, "Error writing private key, ${message}", ("message",fc::string(ERR_error_string( ERR_get_error(),NULL))) ); + FC_THROW_EXCEPTION( exception, "Error writing private key, ${message}", ("message",std::string(ERR_error_string( ERR_get_error(),NULL))) ); } char* dat; uint32_t l = BIO_get_mem_data( mem, &dat ); diff --git a/src/crypto/ripemd160.cpp b/src/crypto/ripemd160.cpp index 3e96048..b76de34 100644 --- a/src/crypto/ripemd160.cpp +++ b/src/crypto/ripemd160.cpp @@ -108,7 +108,7 @@ bool operator == ( const ripemd160& h1, const ripemd160& h2 ) { std::vector ve = v.as< std::vector >( max_depth ); memset( &bi, char(0), sizeof(bi) ); if( ve.size() ) - memcpy( &bi, ve.data(), fc::min(ve.size(),sizeof(bi)) ); + memcpy( &bi, ve.data(), std::min(ve.size(),sizeof(bi)) ); } } // fc diff --git a/src/crypto/sha1.cpp b/src/crypto/sha1.cpp index f509f00..983edc4 100644 --- a/src/crypto/sha1.cpp +++ b/src/crypto/sha1.cpp @@ -92,7 +92,7 @@ bool operator == ( const sha1& h1, const sha1& h2 ) { std::vector ve = v.as< std::vector >( max_depth ); memset( &bi, char(0), sizeof(bi) ); if( ve.size() ) - memcpy( &bi, ve.data(), fc::min(ve.size(),sizeof(bi)) ); + memcpy( &bi, ve.data(), std::min(ve.size(),sizeof(bi)) ); } } // fc diff --git a/src/crypto/sha224.cpp b/src/crypto/sha224.cpp index 4015f90..b1096b6 100644 --- a/src/crypto/sha224.cpp +++ b/src/crypto/sha224.cpp @@ -88,7 +88,7 @@ namespace fc { std::vector ve = v.as< std::vector >( max_depth ); memset( &bi, char(0), sizeof(bi) ); if( ve.size() ) - memcpy( &bi, ve.data(), fc::min(ve.size(),sizeof(bi)) ); + memcpy( &bi, ve.data(), std::min(ve.size(),sizeof(bi)) ); } template<> diff --git a/src/crypto/sha256.cpp b/src/crypto/sha256.cpp index e27fd60..c3edd9b 100644 --- a/src/crypto/sha256.cpp +++ b/src/crypto/sha256.cpp @@ -202,7 +202,7 @@ namespace fc { std::vector ve = v.as< std::vector >( max_depth ); memset( &bi, char(0), sizeof(bi) ); if( ve.size() ) - memcpy( &bi, ve.data(), fc::min(ve.size(),sizeof(bi)) ); + memcpy( &bi, ve.data(), std::min(ve.size(),sizeof(bi)) ); } uint64_t hash64(const char* buf, size_t len) diff --git a/src/crypto/sha512.cpp b/src/crypto/sha512.cpp index 49bc8d9..4b82efe 100644 --- a/src/crypto/sha512.cpp +++ b/src/crypto/sha512.cpp @@ -94,7 +94,7 @@ namespace fc { std::vector ve = v.as< std::vector >( max_depth ); memset( &bi, char(0), sizeof(bi) ); if( ve.size() ) - memcpy( &bi, ve.data(), fc::min(ve.size(),sizeof(bi)) ); + memcpy( &bi, ve.data(), std::min(ve.size(),sizeof(bi)) ); } template<> diff --git a/src/exception.cpp b/src/exception.cpp index 1f84286..b957f2f 100644 --- a/src/exception.cpp +++ b/src/exception.cpp @@ -46,7 +46,7 @@ namespace fc my->_code = code; my->_what = what_value; my->_name = name_value; - my->_elog = fc::move(msgs); + my->_elog = std::move(msgs); } exception::exception( @@ -63,7 +63,7 @@ namespace fc } unhandled_exception::unhandled_exception( log_message&& m, std::exception_ptr e ) - :exception( fc::move(m) ) + :exception( std::move(m) ) { _inner = e; } @@ -73,7 +73,7 @@ namespace fc } unhandled_exception::unhandled_exception( log_messages m ) :exception() - { my->_elog = fc::move(m); } + { my->_elog = std::move(m); } std::exception_ptr unhandled_exception::get_inner_exception()const { return _inner; } @@ -109,13 +109,13 @@ namespace fc my->_code = code; my->_what = what_value; my->_name = name_value; - my->_elog.push_back( fc::move( msg ) ); + my->_elog.push_back( std::move( msg ) ); } exception::exception( const exception& c ) :my( new detail::exception_impl(*c.my) ) { } exception::exception( exception&& c ) - :my( fc::move(c.my) ){} + :my( std::move(c.my) ){} const char* exception::name()const throw() { return my->_name.c_str(); } const char* exception::what()const throw() { return my->_what.c_str(); } @@ -153,7 +153,7 @@ namespace fc const log_messages& exception::get_log()const { return my->_elog; } void exception::append_log( log_message m ) { - my->_elog.emplace_back( fc::move(m) ); + my->_elog.emplace_back( std::move(m) ); } /** @@ -220,7 +220,7 @@ namespace fc return std::make_shared(*this); } - fc::string except_str() + std::string except_str() { return boost::current_exception_diagnostic_information(); } diff --git a/src/filesystem.cpp b/src/filesystem.cpp index 5b89102..d6abc20 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include #include @@ -52,7 +51,7 @@ namespace fc { path::path( const char* p ) :_p(p){} - path::path( const fc::string& p ) + path::path( const std::string& p ) :_p(p.c_str()){} path::path(const std::wstring& p) @@ -69,7 +68,7 @@ namespace fc { return *this; } path& path::operator =( path&& p ) { - *_p = fc::move( *p._p ); + *_p = std::move( *p._p ); return *this; } @@ -93,11 +92,11 @@ namespace fc { path::operator const boost::filesystem::path& ()const { return *_p; } - fc::string path::generic_string()const { + std::string path::generic_string()const { return _p->generic_string(); } - fc::string path::preferred_string() const + std::string path::preferred_string() const { return boost::filesystem::path(*_p).make_preferred().string(); } @@ -140,13 +139,13 @@ namespace fc { * @todo use iterators instead of indexes for * faster performance */ - fc::string path::windows_string()const { + std::string path::windows_string()const { std::string result = _p->generic_string(); std::replace(result.begin(), result.end(), '/', '\\'); return result; } - fc::string path::string()const { + std::string path::string()const { return _p->string(); } fc::path path::filename()const { @@ -502,101 +501,4 @@ namespace fc { return appCurrentPath; } - -#ifdef FC_HAS_SIMPLE_FILE_LOCK - class simple_lock_file::impl - { - public: -#ifdef _WIN32 - HANDLE file_handle; -#else - int file_handle; -#endif - bool is_locked; - path lock_file_path; - - impl(const path& lock_file_path); - ~impl(); - - bool try_lock(); - void unlock(); - }; - - simple_lock_file::impl::impl(const path& lock_file_path) : -#ifdef _WIN32 - file_handle(INVALID_HANDLE_VALUE), -#else - file_handle(-1), -#endif - is_locked(false), - lock_file_path(lock_file_path) - {} - - simple_lock_file::impl::~impl() - { - unlock(); - } - - bool simple_lock_file::impl::try_lock() - { -#ifdef _WIN32 - HANDLE fh = CreateFileA(lock_file_path.to_native_ansi_path().c_str(), - GENERIC_READ | GENERIC_WRITE, - 0, 0, - OPEN_ALWAYS, 0, NULL); - if (fh == INVALID_HANDLE_VALUE) - return false; - is_locked = true; - file_handle = fh; - return true; -#else - int fd = open(lock_file_path.string().c_str(), O_RDWR|O_CREAT, 0644); - if (fd < 0) - return false; - if (flock(fd, LOCK_EX|LOCK_NB) == -1) - { - close(fd); - return false; - } - is_locked = true; - file_handle = fd; - return true; -#endif - } - - void simple_lock_file::impl::unlock() - { -#ifdef WIN32 - CloseHandle(file_handle); - file_handle = INVALID_HANDLE_VALUE; - is_locked = false; -#else - flock(file_handle, LOCK_UN); - close(file_handle); - file_handle = -1; - is_locked = false; -#endif - } - - - simple_lock_file::simple_lock_file(const path& lock_file_path) : - my(new impl(lock_file_path)) - { - } - - simple_lock_file::~simple_lock_file() - { - } - - bool simple_lock_file::try_lock() - { - return my->try_lock(); - } - - void simple_lock_file::unlock() - { - my->unlock(); - } -#endif // FC_HAS_SIMPLE_FILE_LOCK - } diff --git a/src/interprocess/file_mutex.cpp b/src/interprocess/file_mutex.cpp deleted file mode 100644 index 72b142d..0000000 --- a/src/interprocess/file_mutex.cpp +++ /dev/null @@ -1,100 +0,0 @@ -#include -//#include -#include -#include -#include -#include -#include - -#include -#include - -namespace fc { - namespace bip = boost::interprocess; - - void yield(); - - namespace detail { - class file_mutex_impl { - public: - file_mutex_impl( const char* f ) - :_file_mutex( f ),_reader_count(0){} - - fc::mutex _write_lock; - bip::file_lock _file_mutex; - boost::atomic _reader_count; - }; - } - - file_mutex::file_mutex( const fc::path& file ) - { - my.reset( new detail::file_mutex_impl( file.generic_string().c_str() ) ); - } - - file_mutex::~file_mutex() { - } - - int file_mutex::readers()const { - return my->_reader_count.load(); - } - - bool file_mutex::try_lock() { - return false; - if( my->_write_lock.try_lock() ) { - if( my->_file_mutex.try_lock() ) - return true; - } - if( my->_file_mutex.try_lock() ) { - if( my->_write_lock.try_lock() ) { - return true; - } else { - my->_file_mutex.unlock(); - } - } - return false; - } - - bool file_mutex::try_lock_for( const microseconds& rel_time ) { - return false; - } - - bool file_mutex::try_lock_until( const time_point& abs_time ) { - return false; - } - - void file_mutex::lock() { - my->_write_lock.lock(); - while( my->_reader_count.load() > 0 ) { - fc::usleep( fc::microseconds(10) ); - } - my->_file_mutex.lock(); - } - - void file_mutex::unlock() { - my->_file_mutex.unlock(); - my->_write_lock.unlock(); - } - - void file_mutex::lock_shared() { - bip::scoped_lock< fc::mutex > lock( my->_write_lock ); - if( 0 == my->_reader_count.fetch_add( 1, boost::memory_order_relaxed ) ) - my->_file_mutex.lock_sharable(); - } - - void file_mutex::unlock_shared() { - if( 1 == my->_reader_count.fetch_add( -1, boost::memory_order_relaxed ) ) - my->_file_mutex.unlock_sharable(); - } - - bool file_mutex::try_lock_shared() { - return false; - if( my->_write_lock.try_lock() ) { - if( my->_reader_count.load() == 0 && my->_file_mutex.try_lock_sharable() ) { - my->_reader_count++; - } - my->_write_lock.unlock(); - } - return false; - } - -} // namespace fc diff --git a/src/interprocess/mmap_struct.cpp b/src/interprocess/mmap_struct.cpp deleted file mode 100644 index 8e9206c..0000000 --- a/src/interprocess/mmap_struct.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include - -#include - -#include - -#include -#include - -namespace fc -{ - namespace detail - { - size_t mmap_struct_base::size()const { return _mapped_region->get_size(); } - void mmap_struct_base::flush() - { - _mapped_region->flush(); - } - - void mmap_struct_base::open( const fc::path& file, size_t s, bool create ) - { - if( !fc::exists( file ) || fc::file_size(file) != s ) - { - fc::ofstream out( file ); - char buffer[1024]; - memset( buffer, 0, sizeof(buffer) ); - - size_t bytes_left = s; - while( bytes_left > 0 ) - { - size_t to_write = std::min(bytes_left, sizeof(buffer) ); - out.write( buffer, to_write ); - bytes_left -= to_write; - } - } - - std::string filePath = file.to_native_ansi_path(); - - _file_mapping.reset( new fc::file_mapping( filePath.c_str(), fc::read_write ) ); - _mapped_region.reset( new fc::mapped_region( *_file_mapping, fc::read_write, 0, s ) ); - } - } // namespace fc - -} // namespace fc diff --git a/src/io/buffered_iostream.cpp b/src/io/buffered_iostream.cpp index 0760621..5064772 100644 --- a/src/io/buffered_iostream.cpp +++ b/src/io/buffered_iostream.cpp @@ -13,7 +13,7 @@ namespace fc { public: buffered_istream_impl( istream_ptr is ) : - _istr(fc::move(is)) + _istr(std::move(is)) #ifndef NDEBUG ,_shared_read_buffer_in_use(false) #endif @@ -30,17 +30,17 @@ namespace fc } buffered_istream::buffered_istream( istream_ptr is ) - :my( new detail::buffered_istream_impl( fc::move(is) ) ) + :my( new detail::buffered_istream_impl( std::move(is) ) ) { FC_ASSERT( my->_istr != nullptr, " this shouldn't be null" ); } buffered_istream::buffered_istream( buffered_istream&& o ) - :my( fc::move(o.my) ){} + :my( std::move(o.my) ){} buffered_istream& buffered_istream::operator=( buffered_istream&& i ) { - my = fc::move(i.my); + my = std::move(i.my); return *this; } @@ -135,7 +135,7 @@ namespace fc { public: buffered_ostream_impl( ostream_ptr os ) : - _ostr(fc::move(os)) + _ostr(std::move(os)) #ifndef NDEBUG ,_shared_write_buffer_in_use(false) #endif @@ -151,16 +151,16 @@ namespace fc } buffered_ostream::buffered_ostream( ostream_ptr os, size_t bufsize ) - :my( new detail::buffered_ostream_impl( fc::move(os) ) ) + :my( new detail::buffered_ostream_impl( std::move(os) ) ) { } buffered_ostream::buffered_ostream( buffered_ostream&& o ) - :my( fc::move(o.my) ){} + :my( std::move(o.my) ){} buffered_ostream& buffered_ostream::operator=( buffered_ostream&& i ) { - my = fc::move(i.my); + my = std::move(i.my); return *this; } diff --git a/src/io/console.cpp b/src/io/console.cpp deleted file mode 100644 index c0cda18..0000000 --- a/src/io/console.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include -#include - -namespace fc { - -#ifdef WIN32 -#include - -void set_console_echo( bool enable_echo ) -{ - auto stdin_handle = GetStdHandle( STD_INPUT_HANDLE ); - DWORD mode = 0; - GetConsoleMode( stdin_handle, &mode ); - if( enable_echo ) - { - SetConsoleMode( stdin_handle, mode | ENABLE_ECHO_INPUT ); - } - else - { - SetConsoleMode( stdin_handle, mode & (~ENABLE_ECHO_INPUT) ); - } -} - -#else // NOT WIN32 -#include -#include - -void set_console_echo( bool enable_echo ) -{ - termios oldt; - tcgetattr(STDIN_FILENO, &oldt); - termios newt = oldt; - if( enable_echo ) - { - newt.c_lflag |= ECHO; - } - else - { - newt.c_lflag &= ~ECHO; - } - tcsetattr(STDIN_FILENO, TCSANOW, &newt); -} - -#endif // WIN32 - -} // namespace fc diff --git a/src/io/datastream.cpp b/src/io/datastream.cpp index cfe1104..69fda0d 100644 --- a/src/io/datastream.cpp +++ b/src/io/datastream.cpp @@ -3,5 +3,5 @@ NO_RETURN void fc::detail::throw_datastream_range_error(char const* method, size_t len, int64_t over) { - FC_THROW_EXCEPTION( out_of_range_exception, "${method} datastream of length ${len} over by ${over}", ("method",fc::string(method))("len",len)("over",over) ); + FC_THROW_EXCEPTION( out_of_range_exception, "${method} datastream of length ${len} over by ${over}", ("method",std::string(method))("len",len)("over",over) ); } diff --git a/src/io/iostream.cpp b/src/io/iostream.cpp index 3a16027..4c4a0cc 100644 --- a/src/io/iostream.cpp +++ b/src/io/iostream.cpp @@ -3,7 +3,6 @@ #include #include #include -//#include #include #include #include @@ -76,7 +75,7 @@ namespace fc { fc::thread& cin_thread() { static fc::thread i("cin"); return i; } - fc::istream& getline( fc::istream& i, fc::string& s, char delim ) { + fc::istream& getline( fc::istream& i, std::string& s, char delim ) { fc::stringstream ss; char c; i.read( &c, 1 ); @@ -104,7 +103,7 @@ namespace fc { size_t cin_t::readsome( char* buf, size_t len ) { cin_buffer& b = get_cin_buffer(); int64_t avail = b.write_pos - b.read_pos; - avail = (fc::min)(int64_t(len),avail); + avail = (std::min)(int64_t(len),avail); int64_t u = 0; if( !((avail>0) && (len>0)) ) { @@ -194,13 +193,6 @@ namespace fc { o.write( v.c_str(), v.size() ); return o; } -#ifdef USE_FC_STRING - ostream& operator<<( ostream& o, const fc::string& v ) - { - o.write( v.c_str(), v.size() ); - return o; - } -#endif ostream& operator<<( ostream& o, const double& v ) { @@ -266,14 +258,6 @@ namespace fc { return o; } -#ifdef USE_FC_STRING - istream& operator>>( istream& o, fc::string& v ) - { - assert(false && "not implemented"); - return o; - } -#endif - istream& operator>>( istream& o, char& v ) { o.read(&v,1); diff --git a/src/io/json.cpp b/src/io/json.cpp index 712973c..c5a1fc9 100644 --- a/src/io/json.cpp +++ b/src/io/json.cpp @@ -17,9 +17,9 @@ namespace fc // forward declarations of provided functions template variant variant_from_stream( T& in, uint32_t max_depth ); template char parseEscape( T& in ); - template fc::string stringFromStream( T& in ); + template std::string stringFromStream( T& in ); template bool skip_white_space( T& in ); - template fc::string stringFromToken( T& in ); + template std::string stringFromToken( T& in ); template variant_object objectFromStreamBase( T& in, std::function& get_key, std::function& get_value ); template variant_object objectFromStream( T& in, uint32_t max_depth ); template variants arrayFromStreamBase( T& in, std::function& get_value ); @@ -30,7 +30,7 @@ namespace fc template void to_stream( T& os, const variants& a, json::output_formatting format, uint32_t max_depth ); template void to_stream( T& os, const variant_object& o, json::output_formatting format, uint32_t max_depth ); template void to_stream( T& os, const variant& v, json::output_formatting format, uint32_t max_depth ); - fc::string pretty_print( const fc::string& v, uint8_t indent ); + std::string pretty_print( const std::string& v, uint8_t indent ); } #if __cplusplus > 201402L @@ -94,7 +94,7 @@ namespace fc } template - fc::string stringFromStream( T& in ) + std::string stringFromStream( T& in ) { fc::stringstream token; try @@ -131,7 +131,7 @@ namespace fc ("token", token.str() ) ); } template - fc::string stringFromToken( T& in ) + std::string stringFromToken( T& in ) { fc::stringstream token; try @@ -324,7 +324,7 @@ namespace fc catch (const std::ios_base::failure&) { // read error ends the loop } - fc::string str = ss.str(); + std::string str = ss.str(); if (str == "-." || str == "." || str == "-") // check the obviously wrong things we could have encountered FC_THROW_EXCEPTION(parse_error_exception, "Can't parse token \"${token}\" as a JSON numeric constant", ("token", str)); if( dot ) @@ -380,7 +380,7 @@ namespace fc // we can get here either by processing a delimiter as in "null," // an EOF like "null", or an invalid token like "nullZ" - fc::string str = ss.str(); + std::string str = ss.str(); if( str == "null" ) return variant(); if( str == "true" ) @@ -547,7 +547,7 @@ namespace fc } os << '"'; } - ostream& json::to_stream( ostream& out, const fc::string& str ) + ostream& json::to_stream( ostream& out, const std::string& str ) { escape_string( str, out ); return out; @@ -635,7 +635,7 @@ namespace fc } } - fc::string json::to_string( const variant& v, output_formatting format, uint32_t max_depth ) + std::string json::to_string( const variant& v, output_formatting format, uint32_t max_depth ) { fc::stringstream ss; fc::to_stream( ss, v, format, max_depth ); @@ -643,7 +643,7 @@ namespace fc } - fc::string pretty_print( const fc::string& v, uint8_t indent ) { + std::string pretty_print( const std::string& v, uint8_t indent ) { int level = 0; fc::stringstream ss; bool first = false; @@ -733,7 +733,7 @@ namespace fc - fc::string json::to_pretty_string( const variant& v, output_formatting format, uint32_t max_depth ) + std::string json::to_pretty_string( const variant& v, output_formatting format, uint32_t max_depth ) { return pretty_print(to_string(v, format, max_depth), 2); } diff --git a/src/io/sstream.cpp b/src/io/sstream.cpp index 9c4739e..70977c0 100644 --- a/src/io/sstream.cpp +++ b/src/io/sstream.cpp @@ -7,11 +7,11 @@ namespace fc { class stringstream::impl { public: - impl( fc::string&s ) + impl( std::string&s ) :ss( s ) { ss.exceptions( std::stringstream::badbit ); } - impl( const fc::string&s ) + impl( const std::string&s ) :ss( s ) { ss.exceptions( std::stringstream::badbit ); } @@ -20,21 +20,21 @@ namespace fc { std::stringstream ss; }; - stringstream::stringstream( fc::string& s ) + stringstream::stringstream( std::string& s ) :my(s) { } - stringstream::stringstream( const fc::string& s ) + stringstream::stringstream( const std::string& s ) :my(s) { } stringstream::stringstream(){} stringstream::~stringstream(){} - fc::string stringstream::str(){ - return my->ss.str();//.c_str();//*reinterpret_cast(&st); + std::string stringstream::str(){ + return my->ss.str();//.c_str();//*reinterpret_cast(&st); } - void stringstream::str(const fc::string& s) { + void stringstream::str(const std::string& s) { my->ss.str(s); } @@ -93,9 +93,9 @@ namespace fc { istream& stringstream::read( double& v ) { my->ss >> v; return *this; } istream& stringstream::read( bool& v ) { my->ss >> v; return *this; } istream& stringstream::read( char& v ) { my->ss >> v; return *this; } - istream& stringstream::read( fc::string& v ) { my->ss >> *reinterpret_cast(&v); return *this; } + istream& stringstream::read( std::string& v ) { my->ss >> *reinterpret_cast(&v); return *this; } - ostream& stringstream::write( const fc::string& s) { + ostream& stringstream::write( const std::string& s) { my->ss.write( s.c_str(), s.size() ); return *this; } diff --git a/src/log/appender.cpp b/src/log/appender.cpp index c4d9c9e..3d88a2c 100644 --- a/src/log/appender.cpp +++ b/src/log/appender.cpp @@ -22,17 +22,17 @@ namespace fc { static std::unordered_map lm; return lm; } - appender::ptr appender::get( const fc::string& s ) { + appender::ptr appender::get( const std::string& s ) { static fc::spin_lock appender_spinlock; scoped_lock lock(appender_spinlock); return get_appender_map()[s]; } - bool appender::register_appender( const fc::string& type, const appender_factory::ptr& f ) + bool appender::register_appender( const std::string& type, const appender_factory::ptr& f ) { get_appender_factory_map()[type] = f; return true; } - appender::ptr appender::create( const fc::string& name, const fc::string& type, const variant& args ) + appender::ptr appender::create( const std::string& name, const std::string& type, const variant& args ) { auto fact_itr = get_appender_factory_map().find(type); if( fact_itr == get_appender_factory_map().end() ) { diff --git a/src/log/console_appender.cpp b/src/log/console_appender.cpp index af35564..41a9972 100644 --- a/src/log/console_appender.cpp +++ b/src/log/console_appender.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #ifndef WIN32 @@ -114,7 +113,7 @@ namespace fc { line << std::setw( 20 ) << std::left << m.get_context().get_method().substr(p,20).c_str() <<" "; } line << "] "; - fc::string message = fc::format_string( m.get_format(), m.get_data(), my->cfg.max_object_depth ); + std::string message = fc::format_string( m.get_format(), m.get_data(), my->cfg.max_object_depth ); line << message; fc::unique_lock lock(log_mutex()); diff --git a/src/log/file_appender.cpp b/src/log/file_appender.cpp index 270c3dd..6cc1d7e 100644 --- a/src/log/file_appender.cpp +++ b/src/log/file_appender.cpp @@ -172,7 +172,7 @@ namespace fc { } line << "] "; - fc::string message = fc::format_string( m.get_format(), m.get_data(), my->cfg.max_object_depth ); + std::string message = fc::format_string( m.get_format(), m.get_data(), my->cfg.max_object_depth ); line << message.c_str(); { diff --git a/src/log/log_message.cpp b/src/log/log_message.cpp index cf8d27a..b2873b7 100644 --- a/src/log/log_message.cpp +++ b/src/log/log_message.cpp @@ -75,13 +75,13 @@ namespace fc my->context = obj["context"].as(max_depth); } - fc::string log_context::to_string()const + std::string log_context::to_string()const { return my->thread_name + " " + my->file + ":" + fc::to_string(my->line) + " " + my->method; } - void log_context::append_context( const fc::string& s ) + void log_context::append_context( const std::string& s ) { if (!my->context.empty()) my->context += " -> "; diff --git a/src/log/logger.cpp b/src/log/logger.cpp index 8c4f149..93241f3 100644 --- a/src/log/logger.cpp +++ b/src/log/logger.cpp @@ -15,7 +15,7 @@ namespace fc { public: impl() :_parent(nullptr),_enabled(true),_additivity(false),_level(log_level::warn){} - fc::string _name; + std::string _name; logger _parent; bool _enabled; bool _additivity; @@ -42,7 +42,7 @@ namespace fc { :my(l.my){} logger::logger( logger&& l ) - :my(fc::move(l.my)){} + :my(std::move(l.my)){} logger::~logger(){} @@ -51,7 +51,7 @@ namespace fc { return *this; } logger& logger::operator=( logger&& l ){ - fc_swap(my,l.my); + std::swap(my,l.my); return *this; } bool operator==( const logger& l, std::nullptr_t ) { return !l.my; } @@ -71,8 +71,8 @@ namespace fc { my->_parent.log(m); } } - void logger::set_name( const fc::string& n ) { my->_name = n; } - const fc::string& logger::name()const { return my->_name; } + void logger::set_name( const std::string& n ) { my->_name = n; } + const std::string& logger::name()const { return my->_name; } extern bool do_default_config; @@ -84,7 +84,7 @@ namespace fc { return *lm; } - logger logger::get( const fc::string& s ) { + logger logger::get( const std::string& s ) { static fc::spin_lock logger_spinlock; scoped_lock lock(logger_spinlock); return get_logger_map()[s]; diff --git a/src/network/http/http_connection.cpp b/src/network/http/http_connection.cpp index 59fb39b..a870ff5 100644 --- a/src/network/http/http_connection.cpp +++ b/src/network/http/http_connection.cpp @@ -42,22 +42,22 @@ class fc::http::connection::impl std::vector line(1024*8); int s = read_until( line.data(), line.data()+line.size(), ' ' ); // HTTP/1.1 s = read_until( line.data(), line.data()+line.size(), ' ' ); // CODE - rep.status = static_cast(to_int64(fc::string(line.data()))); + rep.status = static_cast(to_int64(std::string(line.data()))); s = read_until( line.data(), line.data()+line.size(), '\n' ); // DESCRIPTION while( (s = read_until( line.data(), line.data()+line.size(), '\n' )) > 1 ) { fc::http::header h; char* end = line.data(); while( *end != ':' )++end; - h.key = fc::string(line.data(),end); + h.key = std::string(line.data(),end); ++end; // skip ':' ++end; // skip space char* skey = end; while( *end != '\r' ) ++end; - h.val = fc::string(skey,end); + h.val = std::string(skey,end); rep.headers.push_back(h); if( boost::iequals(h.key, "Content-Length") ) { - rep.body.resize( static_cast(to_uint64( fc::string(h.val) ) )); + rep.body.resize( static_cast(to_uint64( std::string(h.val) ) )); } } if( rep.body.size() ) { @@ -88,9 +88,9 @@ void connection::connect_to( const fc::ip::endpoint& ep ) { my->sock.connect_to( my->ep = ep ); } -http::reply connection::request( const fc::string& method, - const fc::string& url, - const fc::string& body, const headers& he ) { +http::reply connection::request( const std::string& method, + const std::string& url, + const std::string& body, const headers& he ) { fc::url parsed_url(url); if( !my->sock.is_open() ) { @@ -108,7 +108,7 @@ http::reply connection::request( const fc::string& method, } if( body.size() ) req << "Content-Length: "<< body.size() << "\r\n"; req << "\r\n"; - fc::string head = req.str(); + std::string head = req.str(); my->sock.write( head.c_str(), head.size() ); // fc::cerr.write( head.c_str() ); @@ -146,17 +146,17 @@ http::request connection::read_request()const { fc::http::header h; char* end = line.data(); while( *end != ':' )++end; - h.key = fc::string(line.data(),end); + h.key = std::string(line.data(),end); ++end; // skip ':' ++end; // skip space char* skey = end; while( *end != '\r' ) ++end; - h.val = fc::string(skey,end); + h.val = std::string(skey,end); req.headers.push_back(h); if( boost::iequals(h.key, "Content-Length")) { - auto s = static_cast(to_uint64( fc::string(h.val) ) ); + auto s = static_cast(to_uint64( std::string(h.val) ) ); FC_ASSERT( s < 1024*1024 ); - req.body.resize( static_cast(to_uint64( fc::string(h.val) ) )); + req.body.resize( static_cast(to_uint64( std::string(h.val) ) )); } if( boost::iequals(h.key, "Host") ) { req.domain = h.val; @@ -171,13 +171,13 @@ http::request connection::read_request()const { return req; } -fc::string request::get_header( const fc::string& key )const { +std::string request::get_header( const std::string& key )const { for( auto itr = headers.begin(); itr != headers.end(); ++itr ) { if( boost::iequals(itr->key, key) ) { return itr->val; } } - return fc::string(); + return std::string(); } -std::vector
parse_urlencoded_params( const fc::string& f ) { +std::vector
parse_urlencoded_params( const std::string& f ) { int num_args = 0; for( size_t i = 0; i < f.size(); ++i ) { if( f[i] == '=' ) ++num_args; diff --git a/src/network/http/http_server.cpp b/src/network/http/http_server.cpp index 648fc54..8a63734 100644 --- a/src/network/http/http_server.cpp +++ b/src/network/http/http_server.cpp @@ -135,9 +135,9 @@ namespace fc { namespace http { server::server():my( new impl() ){} server::server( uint16_t port ) :my( new impl(fc::ip::endpoint( fc::ip::address(),port)) ){} - server::server( server&& s ):my(fc::move(s.my)){} + server::server( server&& s ):my(std::move(s.my)){} - server& server::operator=(server&& s) { fc_swap(my,s.my); return *this; } + server& server::operator=(server&& s) { std::swap(my,s.my); return *this; } server::~server(){} @@ -154,13 +154,13 @@ namespace fc { namespace http { server::response::response(){} server::response::response( const server::response& s ):my(s.my){} - server::response::response( server::response&& s ):my(fc::move(s.my)){} + server::response::response( server::response&& s ):my(std::move(s.my)){} server::response::response( const fc::shared_ptr& m ):my(m){} server::response& server::response::operator=(const server::response& s) { my = s.my; return *this; } - server::response& server::response::operator=(server::response&& s) { fc_swap(my,s.my); return *this; } + server::response& server::response::operator=(server::response&& s) { std::swap(my,s.my); return *this; } - void server::response::add_header( const fc::string& key, const fc::string& val )const { + void server::response::add_header( const std::string& key, const std::string& val )const { my->rep.headers.push_back( fc::http::header( key, val ) ); } void server::response::set_status( const http::reply::status_code& s )const { diff --git a/src/network/ip.cpp b/src/network/ip.cpp index 570a621..daff7f9 100644 --- a/src/network/ip.cpp +++ b/src/network/ip.cpp @@ -10,7 +10,7 @@ namespace fc { namespace ip { address::address( uint32_t ip ) :_ip(ip){} - address::address( const fc::string& s ) + address::address( const std::string& s ) { try { @@ -26,7 +26,7 @@ namespace fc { namespace ip { return uint32_t(a) != uint32_t(b); } - address& address::operator=( const fc::string& s ) + address& address::operator=( const std::string& s ) { try { @@ -36,7 +36,7 @@ namespace fc { namespace ip { return *this; } - address::operator fc::string()const + address::operator std::string()const { try { @@ -88,7 +88,7 @@ namespace fc { namespace ip { { try { - return string(_ip) + ':' + fc::string(boost::lexical_cast(_port).c_str()); + return string(_ip) + ':' + std::string(boost::lexical_cast(_port).c_str()); } FC_RETHROW_EXCEPTIONS(warn, "error converting IP endpoint to string") } @@ -139,7 +139,7 @@ namespace fc { namespace ip { void to_variant( const ip::endpoint& var, variant& vo, uint32_t max_depth ) { - vo = fc::string(var); + vo = std::string(var); } void from_variant( const variant& var, ip::endpoint& vo, uint32_t max_depth ) { @@ -148,7 +148,7 @@ namespace fc { namespace ip { void to_variant( const ip::address& var, variant& vo, uint32_t max_depth ) { - vo = fc::string(var); + vo = std::string(var); } void from_variant( const variant& var, ip::address& vo, uint32_t max_depth ) { diff --git a/src/network/resolve.cpp b/src/network/resolve.cpp index 3e9ec28..508d292 100644 --- a/src/network/resolve.cpp +++ b/src/network/resolve.cpp @@ -4,7 +4,7 @@ namespace fc { - std::vector resolve( const fc::string& host, uint16_t port ) + std::vector resolve( const std::string& host, uint16_t port ) { auto ep = fc::asio::tcp::resolve( host, std::to_string(uint64_t(port)) ); std::vector eps; diff --git a/src/network/url.cpp b/src/network/url.cpp index 73d0536..403aaae 100644 --- a/src/network/url.cpp +++ b/src/network/url.cpp @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -12,7 +11,7 @@ namespace fc class url_impl { public: - void parse( const fc::string& s ) + void parse( const std::string& s ) { std::stringstream ss(s); std::string skip,_lpath,_largs,luser,lpass; @@ -20,23 +19,23 @@ namespace fc std::getline( ss, skip, '/' ); std::getline( ss, skip, '/' ); - if( s.find('@') != size_t(fc::string::npos) ) { - fc::string user_pass; + if( s.find('@') != size_t(std::string::npos) ) { + std::string user_pass; std::getline( ss, user_pass, '@' ); std::stringstream upss(user_pass); - if( user_pass.find( ':' ) != size_t(fc::string::npos) ) { + if( user_pass.find( ':' ) != size_t(std::string::npos) ) { std::getline( upss, luser, ':' ); std::getline( upss, lpass, ':' ); - _user = fc::move(luser); - _pass = fc::move(lpass); + _user = std::move(luser); + _pass = std::move(lpass); } else { - _user = fc::move(user_pass); + _user = std::move(user_pass); } } - fc::string host_port; + std::string host_port; std::getline( ss, host_port, '/' ); auto pos = host_port.find( ':' ); - if( pos != fc::string::npos ) { + if( pos != std::string::npos ) { try { _port = static_cast(to_uint64( host_port.substr( pos+1 ) )); } catch ( ... ) { @@ -44,7 +43,7 @@ namespace fc } _host = host_port.substr(0,pos); } else { - _host = fc::move(host_port); + _host = std::move(host_port); } std::getline( ss, _lpath, '?' ); #ifdef WIN32 @@ -62,7 +61,7 @@ namespace fc std::getline( ss, _largs ); if( _args.valid() && _args->size() ) { - // TODO: args = fc::move(_args); + // TODO: args = std::move(_args); } } @@ -78,7 +77,7 @@ namespace fc void to_variant( const url& u, fc::variant& v, uint32_t max_depth ) { - v = fc::string(u); + v = std::string(u); } void from_variant( const fc::variant& v, url& u, uint32_t max_depth ) { @@ -102,7 +101,7 @@ namespace fc return ss.str(); } - url::url( const fc::string& u ) + url::url( const std::string& u ) :my( std::make_shared() ) { my->parse(u); @@ -122,7 +121,7 @@ namespace fc :my(u.my){} url::url( url&& u ) - :my( fc::move(u.my) ) + :my( std::move(u.my) ) { u.my = get_null_url(); } @@ -133,7 +132,7 @@ namespace fc } url::url( mutable_url&& mu ) - :my( fc::move( mu.my ) ) + :my( std::move( mu.my ) ) { } url::~url(){} @@ -148,7 +147,7 @@ namespace fc { if( this != &u ) { - my = fc::move(u.my); + my = std::move(u.my); u.my= get_null_url(); } return *this; @@ -160,7 +159,7 @@ namespace fc } url& url::operator=(mutable_url&& u ) { - my = fc::move(u.my); + my = std::move(u.my); return *this; } diff --git a/src/rpc/bstate.cpp b/src/rpc/bstate.cpp index 832f8ce..b6b39fd 100644 --- a/src/rpc/bstate.cpp +++ b/src/rpc/bstate.cpp @@ -8,12 +8,12 @@ bstate::~bstate() close(); } -void bstate::add_method( const fc::string& name, method m ) +void bstate::add_method( const std::string& name, method m ) { - _methods.emplace(std::pair(name,fc::move(m))); + _methods.emplace(std::pair(name,std::move(m))); } -void bstate::remove_method( const fc::string& name ) +void bstate::remove_method( const std::string& name ) { _methods.erase(name); } diff --git a/src/rpc/cli.cpp b/src/rpc/cli.cpp index d106253..658a968 100644 --- a/src/rpc/cli.cpp +++ b/src/rpc/cli.cpp @@ -221,7 +221,7 @@ static int cli_check_secret(const char *source) * @param prompt the prompt to display * @param line what the user typed */ -void cli::getline( const fc::string& prompt, fc::string& line) +void cli::getline( const std::string& prompt, std::string& line) { // getting file descriptor for C++ streams is near impossible // so we just assume it's the same as the C stream... diff --git a/src/rpc/json_connection.cpp b/src/rpc/json_connection.cpp index fd19f27..8eea0df 100644 --- a/src/rpc/json_connection.cpp +++ b/src/rpc/json_connection.cpp @@ -15,7 +15,7 @@ namespace fc { namespace rpc { { public: json_connection_impl( fc::buffered_istream_ptr&& in, fc::buffered_ostream_ptr&& out, uint32_t max_depth ) - :_in(fc::move(in)),_out(fc::move(out)),_eof(false),_next_id(0),_logger("json_connection"),_max_depth(max_depth){} + :_in(std::move(in)),_out(std::move(out)),_eof(false),_next_id(0),_logger("json_connection"),_max_depth(max_depth){} fc::buffered_istream_ptr _in; fc::buffered_ostream_ptr _out; @@ -57,7 +57,7 @@ namespace fc { namespace rpc { *_out << "{\"id\":"; json::to_stream( *_out, id, json::stringify_large_ints_and_doubles, _max_depth ); *_out << ",\"error\":{\"message\":"; - json::to_stream( *_out, fc::string(e.what()) ); + json::to_stream( *_out, std::string(e.what()) ); *_out <<",\"code\":0,\"data\":"; json::to_stream( *_out, variant(e, _max_depth), json::stringify_large_ints_and_doubles, _max_depth ); *_out << "}}\n"; @@ -202,7 +202,7 @@ namespace fc { namespace rpc { fc::exception_ptr eptr; try { - fc::string line; + std::string line; while( !_done.canceled() ) { variant v = json::from_stream( *_in, json::legacy_parser, _max_depth ); @@ -241,7 +241,7 @@ namespace fc { namespace rpc { }//namespace detail json_connection::json_connection( fc::buffered_istream_ptr in, fc::buffered_ostream_ptr out, uint32_t max_depth ) - :_max_conversion_depth(max_depth),my( new detail::json_connection_impl(fc::move(in),fc::move(out),max_depth) ) + :_max_conversion_depth(max_depth),my( new detail::json_connection_impl(std::move(in),std::move(out),max_depth) ) {} json_connection::~json_connection() @@ -285,22 +285,22 @@ namespace fc { namespace rpc { my->_on_close = callback; } - void json_connection::add_method( const fc::string& name, method m ) + void json_connection::add_method( const std::string& name, method m ) { ilog( "add method ${name}", ("name",name) ); - my->_methods.emplace(std::pair(name,fc::move(m))); + my->_methods.emplace(std::pair(name,std::move(m))); } - void json_connection::add_named_param_method( const fc::string& name, named_param_method m ) + void json_connection::add_named_param_method( const std::string& name, named_param_method m ) { ilog( "add named param method ${name}", ("name",name) ); - my->_named_param_methods.emplace(std::pair(name,fc::move(m))); + my->_named_param_methods.emplace(std::pair(name,std::move(m))); } - void json_connection::remove_method( const fc::string& name ) + void json_connection::remove_method( const std::string& name ) { my->_methods.erase(name); my->_named_param_methods.erase(name); } - void json_connection::notice( const fc::string& method, const variants& args ) + void json_connection::notice( const std::string& method, const variants& args ) { fc::scoped_lock lock(my->_write_mutex); *my->_out << "{\"method\":"; @@ -316,7 +316,7 @@ namespace fc { namespace rpc { *my->_out << ",\"params\":[]}\n"; } } - void json_connection::notice( const fc::string& method, const variant_object& named_args ) + void json_connection::notice( const std::string& method, const variant_object& named_args ) { { fc::scoped_lock lock(my->_write_mutex); @@ -328,7 +328,7 @@ namespace fc { namespace rpc { my->_out->flush(); } } - void json_connection::notice( const fc::string& method ) + void json_connection::notice( const std::string& method ) { { fc::scoped_lock lock(my->_write_mutex); @@ -340,7 +340,7 @@ namespace fc { namespace rpc { } - future json_connection::async_call( const fc::string& method, const variants& args ) + future json_connection::async_call( const std::string& method, const variants& args ) { auto id = my->_next_id++; my->_awaiting[id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); @@ -366,7 +366,7 @@ namespace fc { namespace rpc { return my->_awaiting[id]; } - future json_connection::async_call( const fc::string& method, const variant& a1 ) + future json_connection::async_call( const std::string& method, const variant& a1 ) { auto id = my->_next_id++; my->_awaiting[id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); @@ -384,7 +384,7 @@ namespace fc { namespace rpc { } return my->_awaiting[id]; } - future json_connection::async_call( const fc::string& method, const variant& a1, const variant& a2 ) + future json_connection::async_call( const std::string& method, const variant& a1, const variant& a2 ) { auto id = my->_next_id++; my->_awaiting[id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); @@ -404,7 +404,7 @@ namespace fc { namespace rpc { } return my->_awaiting[id]; } - future json_connection::async_call( const fc::string& method, const variant& a1, const variant& a2, const variant& a3 ) + future json_connection::async_call( const std::string& method, const variant& a1, const variant& a2, const variant& a3 ) { auto id = my->_next_id++; my->_awaiting[id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); @@ -427,7 +427,7 @@ namespace fc { namespace rpc { return my->_awaiting[id]; } - future json_connection::async_call( const fc::string& method, const variant& a1, const variant& a2, const variant& a3, const variant& a4 ) + future json_connection::async_call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, const variant& a4 ) { auto id = my->_next_id++; my->_awaiting[id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); @@ -452,7 +452,7 @@ namespace fc { namespace rpc { return my->_awaiting[id]; } - future json_connection::async_call( const fc::string& method, const variant& a1, const variant& a2, const variant& a3, const variant& a4, const variant& a5 ) + future json_connection::async_call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, const variant& a4, const variant& a5 ) { auto id = my->_next_id++; my->_awaiting[id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); @@ -479,7 +479,7 @@ namespace fc { namespace rpc { return my->_awaiting[id]; } - future json_connection::async_call( const fc::string& method, const variant& a1, const variant& a2, const variant& a3, const variant& a4, const variant& a5, const variant& a6 ) + future json_connection::async_call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, const variant& a4, const variant& a5, const variant& a6 ) { auto id = my->_next_id++; my->_awaiting[id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); @@ -507,7 +507,7 @@ namespace fc { namespace rpc { } return my->_awaiting[id]; } - future json_connection::async_call( const fc::string& method, const variant& a1, const variant& a2, const variant& a3, const variant& a4, const variant& a5, const variant& a6, const variant& a7 ) + future json_connection::async_call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, const variant& a4, const variant& a5, const variant& a6, const variant& a7 ) { auto id = my->_next_id++; my->_awaiting[id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); @@ -537,7 +537,7 @@ namespace fc { namespace rpc { } return my->_awaiting[id]; } - future json_connection::async_call( const fc::string& method, + future json_connection::async_call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, @@ -577,7 +577,7 @@ namespace fc { namespace rpc { } return my->_awaiting[id]; } - future json_connection::async_call( const fc::string& method, + future json_connection::async_call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, @@ -620,7 +620,7 @@ namespace fc { namespace rpc { } return my->_awaiting[id]; } - future json_connection::async_call( const fc::string& method, + future json_connection::async_call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, @@ -667,11 +667,11 @@ namespace fc { namespace rpc { return my->_awaiting[id]; } - future json_connection::async_call( const fc::string& method, mutable_variant_object named_args ) + future json_connection::async_call( const std::string& method, mutable_variant_object named_args ) { - return async_call( method, variant_object( fc::move(named_args) ) ); + return async_call( method, variant_object( std::move(named_args) ) ); } - future json_connection::async_call( const fc::string& method, const variant_object& named_args ) + future json_connection::async_call( const std::string& method, const variant_object& named_args ) { auto id = my->_next_id++; my->_awaiting[id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); @@ -688,7 +688,7 @@ namespace fc { namespace rpc { } return my->_awaiting[id]; } - future json_connection::async_call( const fc::string& method ) + future json_connection::async_call( const std::string& method ) { auto id = my->_next_id++; my->_awaiting[id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); diff --git a/src/rpc/state.cpp b/src/rpc/state.cpp index 7d4063d..20823e3 100644 --- a/src/rpc/state.cpp +++ b/src/rpc/state.cpp @@ -8,12 +8,12 @@ state::~state() close(); } -void state::add_method( const fc::string& name, method m ) +void state::add_method( const std::string& name, method m ) { - _methods.emplace(std::pair(name,fc::move(m))); + _methods.emplace(std::pair(name,std::move(m))); } -void state::remove_method( const fc::string& name ) +void state::remove_method( const std::string& name ) { _methods.erase(name); } diff --git a/src/string.cpp b/src/string.cpp index 2432360..ea63732 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -1,5 +1,3 @@ -#include -#include #include #include #include @@ -33,65 +31,7 @@ namespace fc { return ss.str(); } -#ifdef USE_FC_STRING - string::string(const char* s, int l) :my(s,l){ } - string::string(){} - string::string( const fc::string& c ):my(*c.my) { } - string::string( string&& m ):my(fc::move(*m.my)) {} - string::string( const char* c ):my(c){} - string::string( const_iterator b, const_iterator e ):my(b,e){} - string::string( const std::string& s ):my(s) {} - string::string( std::string&& s ):my(fc::move(s)) {} - string::~string() { } - string::operator std::string&() { return *my; } - string::operator const std::string&()const { return *my; } - char* string::data() { return &my->front(); } - - string::iterator string::begin() { return &(*this)[0]; } - string::iterator string::end() { return &(*this)[size()]; } - string::const_iterator string::begin()const { return my->c_str(); } - string::const_iterator string::end()const { return my->c_str() + my->size(); } - - char& string::operator[](size_t idx) { return (*my)[idx]; } - const char& string::operator[](size_t idx)const { return (*my)[idx]; } - - void string::reserve(size_t r) { my->reserve(r); } - size_t string::size()const { return my->size(); } - size_t string::find(char c, size_t p)const { return my->find(c,p); } - size_t string::find(const fc::string& str, size_t pos /* = 0 */) const { return my->find(str, pos); } - size_t string::find(const char* s, size_t pos /* = 0 */) const { return my->find(s,pos); } - size_t string::rfind(char c, size_t p)const { return my->rfind(c,p); } - size_t string::rfind(const char* c, size_t p)const { return my->rfind(c,p); } - size_t string::rfind(const fc::string& c, size_t p)const { return my->rfind(c,p); } - size_t string::find_first_of(const fc::string& str, size_t pos /* = 0 */) const { return my->find_first_of(str, pos); } - size_t string::find_first_of(const char* s, size_t pos /* = 0 */) const { return my->find_first_of(s, pos); } - - fc::string& string::replace(size_t pos, size_t len, const string& str) { my->replace(pos, len, str); return *this; } - fc::string& string::replace(size_t pos, size_t len, const char* s) { my->replace(pos, len, s); return *this; } - - void string::clear() { my->clear(); } - void string::resize( size_t s ) { my->resize(s); } - - fc::string string::substr( size_t start, size_t len )const { return my->substr(start,len); } - const char* string::c_str()const { return my->c_str(); } - - bool string::operator == ( const char* s )const { return *my == s; } - bool string::operator == ( const string& s )const { return *my == *s.my; } - bool string::operator != ( const string& s )const { return *my != *s.my; } - - string& string::operator =( const string& c ) { *my = *c.my; return *this; } - string& string::operator =( string&& c ) { *my = fc::move( *c.my ); return *this; } - - string& string::operator+=( const string& s ) { *my += *s.my; return *this; } - string& string::operator+=( char c ) { *my += c; return *this; } - - bool operator < ( const string& a, const string& b ) { return *a.my < *b.my; } - string operator + ( const string& s, const string& c ) { return string(s) += c; } - string operator + ( const string& s, char c ) { return string(s) += c; } -#endif // USE_FC_STRING - - - int64_t to_int64( const fc::string& i ) + int64_t to_int64( const std::string& i ) { try { @@ -104,7 +44,7 @@ namespace fc { FC_RETHROW_EXCEPTIONS( warn, "${i} => int64_t", ("i",i) ) } - uint64_t to_uint64( const fc::string& i ) + uint64_t to_uint64( const std::string& i ) { try { try { @@ -117,7 +57,7 @@ namespace fc { FC_RETHROW_EXCEPTIONS( warn, "${i} => uint64_t", ("i",i) ) } FC_CAPTURE_AND_RETHROW( (i) ) } - double to_double( const fc::string& i) + double to_double( const std::string& i) { try { @@ -130,7 +70,7 @@ namespace fc { FC_RETHROW_EXCEPTIONS( warn, "${i} => double", ("i",i) ) } - fc::string to_string(double d) + std::string to_string(double d) { // +2 is required to ensure that the double is rounded correctly when read back in. http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html std::stringstream ss; @@ -138,27 +78,22 @@ namespace fc { return ss.str(); } - fc::string to_string( uint64_t d) + std::string to_string( uint64_t d) { return boost::lexical_cast(d); } - fc::string to_string( int64_t d) + std::string to_string( int64_t d) { return boost::lexical_cast(d); } - fc::string to_string( uint16_t d) + std::string to_string( uint16_t d) { return boost::lexical_cast(d); } std::string trim( const std::string& s ) { return boost::algorithm::trim_copy(s); - /* - std::string cpy(s); - boost::algorithm::trim(cpy); - return cpy; - */ } std::string to_lower( const std::string& s ) { @@ -314,5 +249,3 @@ namespace fc { } } // namespace fc - - diff --git a/src/thread/thread.cpp b/src/thread/thread.cpp index f1c1e99..0d1a9aa 100644 --- a/src/thread/thread.cpp +++ b/src/thread/thread.cpp @@ -1,5 +1,4 @@ #include -#include #include #include #include "thread_d.hpp" @@ -136,7 +135,7 @@ namespace fc { return my->name; } - void thread::set_name( const fc::string& n ) + void thread::set_name( const std::string& n ) { if (!is_current()) { @@ -154,7 +153,7 @@ namespace fc { return NULL; } - void thread::debug( const fc::string& d ) { /*my->debug(d);*/ } + void thread::debug( const std::string& d ) { /*my->debug(d);*/ } void thread::quit() { @@ -360,12 +359,12 @@ namespace fc { int wait_any( std::vector&& v, const microseconds& timeout_us ) { - return thread::current().wait_any_until( fc::move(v), time_point::now() + timeout_us ); + return thread::current().wait_any_until( std::move(v), time_point::now() + timeout_us ); } int wait_any_until( std::vector&& v, const time_point& tp ) { - return thread::current().wait_any_until( fc::move(v), tp ); + return thread::current().wait_any_until( std::move(v), tp ); } void thread::wait_until( promise_base::ptr&& p, const time_point& timeout ) diff --git a/src/thread/thread_d.hpp b/src/thread/thread_d.hpp index c11403a..5353606 100644 --- a/src/thread/thread_d.hpp +++ b/src/thread/thread_d.hpp @@ -1,5 +1,4 @@ #include -#include #include #include #include "context.hpp" @@ -46,7 +45,7 @@ namespace fc { #endif { static boost::atomic cnt(0); - name = fc::string("th_") + char('a'+cnt++); + name = std::string("th_") + char('a'+cnt++); // printf("thread=%p\n",this); } @@ -93,7 +92,7 @@ namespace fc { std::vector free_list; // list of unused contexts that are ready for deletion bool done; - fc::string name; + std::string name; fc::context* current; // the currently-executing task in this thread fc::context* pt_head; // list of contexts that can be reused for new tasks @@ -117,7 +116,7 @@ namespace fc { #endif #if 0 - void debug( const fc::string& s ) { + void debug( const std::string& s ) { return; //boost::unique_lock lock(log_mutex()); diff --git a/src/time.cpp b/src/time.cpp index bf8857f..9be1416 100644 --- a/src/time.cpp +++ b/src/time.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include #include @@ -16,24 +15,24 @@ namespace fc { return time_point( microseconds( bch::duration_cast( bch::system_clock::now().time_since_epoch() ).count() ) ); } - fc::string time_point_sec::to_non_delimited_iso_string()const + std::string time_point_sec::to_non_delimited_iso_string()const { const auto ptime = boost::posix_time::from_time_t( time_t( sec_since_epoch() ) ); return boost::posix_time::to_iso_string( ptime ); } - fc::string time_point_sec::to_iso_string()const + std::string time_point_sec::to_iso_string()const { const auto ptime = boost::posix_time::from_time_t( time_t( sec_since_epoch() ) ); return boost::posix_time::to_iso_extended_string( ptime ); } - time_point_sec::operator fc::string()const + time_point_sec::operator std::string()const { return this->to_iso_string(); } - time_point_sec time_point_sec::from_iso_string( const fc::string& s ) + time_point_sec time_point_sec::from_iso_string( const std::string& s ) { try { static boost::posix_time::ptime epoch = boost::posix_time::from_time_t( 0 ); boost::posix_time::ptime pt; @@ -44,24 +43,24 @@ namespace fc { return fc::time_point_sec( (pt - epoch).total_seconds() ); } FC_RETHROW_EXCEPTIONS( warn, "unable to convert ISO-formatted string to fc::time_point_sec" ) } - time_point::operator fc::string()const + time_point::operator std::string()const { - return fc::string( time_point_sec( *this ) ); + return std::string( time_point_sec( *this ) ); } - time_point time_point::from_iso_string( const fc::string& s ) + time_point time_point::from_iso_string( const std::string& s ) { try { return time_point( time_point_sec::from_iso_string( s ) ); } FC_RETHROW_EXCEPTIONS( warn, "unable to convert ISO-formatted string to fc::time_point" ) } void to_variant( const fc::time_point& t, variant& v, uint32_t max_depth ) { - to_variant( fc::string( t ), v, max_depth ); + to_variant( std::string( t ), v, max_depth ); } void from_variant( const fc::variant& v, fc::time_point& t, uint32_t max_depth ) { t = fc::time_point::from_iso_string( v.as_string() ); } void to_variant( const fc::time_point_sec& t, variant& v, uint32_t max_depth ) { - to_variant( fc::string( t ), v, max_depth ); + to_variant( std::string( t ), v, max_depth ); } void from_variant( const fc::variant& v, fc::time_point_sec& t, uint32_t max_depth ) { t = fc::time_point_sec::from_iso_string( v.as_string() ); diff --git a/src/variant.cpp b/src/variant.cpp index 2ee8256..74afb1d 100644 --- a/src/variant.cpp +++ b/src/variant.cpp @@ -140,31 +140,31 @@ variant::variant( const wchar_t* str, uint32_t max_depth ) set_variant_type( this, string_type ); } -variant::variant( fc::string val, uint32_t max_depth ) +variant::variant( std::string val, uint32_t max_depth ) { - *reinterpret_cast(this) = new string( fc::move(val) ); + *reinterpret_cast(this) = new string( std::move(val) ); set_variant_type( this, string_type ); } variant::variant( blob val, uint32_t max_depth ) { - *reinterpret_cast(this) = new blob( fc::move(val) ); + *reinterpret_cast(this) = new blob( std::move(val) ); set_variant_type( this, blob_type ); } variant::variant( variant_object obj, uint32_t max_depth ) { - *reinterpret_cast(this) = new variant_object(fc::move(obj)); + *reinterpret_cast(this) = new variant_object(std::move(obj)); set_variant_type(this, object_type ); } variant::variant( mutable_variant_object obj, uint32_t max_depth ) { - *reinterpret_cast(this) = new variant_object(fc::move(obj)); + *reinterpret_cast(this) = new variant_object(std::move(obj)); set_variant_type(this, object_type ); } variant::variant( variants arr, uint32_t max_depth ) { - *reinterpret_cast(this) = new variants(fc::move(arr)); + *reinterpret_cast(this) = new variants(std::move(arr)); set_variant_type(this, array_type ); } @@ -647,7 +647,7 @@ void from_variant( const variant& var, float& vo, uint32_t max_depth ) void to_variant( const std::string& s, variant& v, uint32_t max_depth ) { - v = variant( fc::string(s), max_depth ); + v = variant( std::string(s), max_depth ); } void from_variant( const variant& var, string& vo, uint32_t max_depth ) diff --git a/src/variant_object.cpp b/src/variant_object.cpp index 652fddd..cfc913f 100644 --- a/src/variant_object.cpp +++ b/src/variant_object.cpp @@ -9,8 +9,8 @@ namespace fc // entry variant_object::entry::entry() {} - variant_object::entry::entry( string k, variant v ) : _key(fc::move(k)),_value(fc::move(v)) {} - variant_object::entry::entry( entry&& e ) : _key(fc::move(e._key)),_value(fc::move(e._value)) {} + variant_object::entry::entry( string k, variant v ) : _key(std::move(k)),_value(std::move(v)) {} + variant_object::entry::entry( entry&& e ) : _key(std::move(e._key)),_value(std::move(e._value)) {} variant_object::entry::entry( const entry& e ) : _key(e._key),_value(e._value) {} variant_object::entry& variant_object::entry::operator=( const variant_object::entry& e ) { @@ -23,8 +23,8 @@ namespace fc } variant_object::entry& variant_object::entry::operator=( variant_object::entry&& e ) { - fc_swap( _key, e._key ); - fc_swap( _value, e._value ); + std::swap( _key, e._key ); + std::swap( _value, e._value ); return *this; } @@ -44,7 +44,7 @@ namespace fc void variant_object::entry::set( variant v ) { - fc_swap( _value, v ); + std::swap( _value, v ); } // --------------------------------------------------------------- @@ -103,7 +103,7 @@ namespace fc variant_object::variant_object( string key, variant val ) : _key_value(std::make_shared>()) { - _key_value->emplace_back(entry(fc::move(key), fc::move(val))); + _key_value->emplace_back(entry(std::move(key), std::move(val))); } variant_object::variant_object( const variant_object& obj ) @@ -113,7 +113,7 @@ namespace fc } variant_object::variant_object( variant_object&& obj) - : _key_value( fc::move(obj._key_value) ) + : _key_value( std::move(obj._key_value) ) { obj._key_value = std::make_shared>(); assert( _key_value != nullptr ); @@ -125,7 +125,7 @@ namespace fc } variant_object::variant_object( mutable_variant_object&& obj ) - : _key_value(fc::move(obj._key_value)) + : _key_value(std::move(obj._key_value)) { assert( _key_value != nullptr ); } @@ -134,7 +134,7 @@ namespace fc { if (this != &obj) { - fc_swap(_key_value, obj._key_value ); + std::swap(_key_value, obj._key_value ); assert( _key_value != nullptr ); } return *this; @@ -151,7 +151,7 @@ namespace fc variant_object& variant_object::operator=( mutable_variant_object&& obj ) { - _key_value = fc::move(obj._key_value); + _key_value = std::move(obj._key_value); obj._key_value.reset( new std::vector() ); return *this; } @@ -266,7 +266,7 @@ namespace fc mutable_variant_object::mutable_variant_object( string key, variant val ) : _key_value(new std::vector()) { - _key_value->push_back(entry(fc::move(key), fc::move(val))); + _key_value->push_back(entry(std::move(key), std::move(val))); } mutable_variant_object::mutable_variant_object( const variant_object& obj ) @@ -280,7 +280,7 @@ namespace fc } mutable_variant_object::mutable_variant_object( mutable_variant_object&& obj ) - : _key_value(fc::move(obj._key_value)) + : _key_value(std::move(obj._key_value)) { } @@ -294,7 +294,7 @@ namespace fc { if (this != &obj) { - _key_value = fc::move(obj._key_value); + _key_value = std::move(obj._key_value); } return *this; } @@ -331,11 +331,11 @@ namespace fc auto itr = find( key.c_str() ); if( itr != end() ) { - itr->set( fc::move(var) ); + itr->set( std::move(var) ); } else { - _key_value->push_back( entry( fc::move(key), fc::move(var) ) ); + _key_value->push_back( entry( std::move(key), std::move(var) ) ); } return *this; } @@ -345,7 +345,7 @@ namespace fc */ mutable_variant_object& mutable_variant_object::operator()( string key, variant var, uint32_t max_depth ) { - _key_value->push_back( entry( fc::move(key), fc::move(var) ) ); + _key_value->push_back( entry( std::move(key), std::move(var) ) ); return *this; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 17c8e5f..95d7242 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,7 +1,4 @@ -add_executable( bip_lock bip_lock.cpp ) -target_link_libraries( bip_lock fc ) - add_executable( api api.cpp ) target_link_libraries( api fc ) diff --git a/tests/bip_lock.cpp b/tests/bip_lock.cpp deleted file mode 100644 index 38bf684..0000000 --- a/tests/bip_lock.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include -#include -#include -#include -#include - -int main( int argc, char** argv ) { - if( argc < 2 ) return 0; - fc::file_mutex m( argv[1] ); - auto mptr = &m; - - fc::thread in("in"); - - std::string cmd; - std::cout << ">>> "; - std::cin >> cmd; - int i = 0; - while( !std::cin.eof() && cmd != "q" ) { - ++i; - fc::async( [i, cmd,mptr]() { - ilog( "start ${c} ${i}", ("c",cmd)("i",i) ); - if( cmd == "L" ) { - mptr->lock(); - } else if( cmd == "l" ) { - mptr->lock_shared(); - } else if( cmd == "U" ) { - mptr->unlock(); - } else if( cmd == "u" ) { - mptr->unlock_shared(); - } - ilog( "end ${c} ${i}", ("c",cmd)("i",i) ); - } ); - fc::usleep( fc::microseconds( 1000 ) ); - cmd = in.async( [&]() { - std::string tmp; - wdump((m.readers())); - std::cin >> tmp; - return tmp; - } ); - } - std::cout << "done"; - - return 0; -} diff --git a/tests/crypto/base_n_tests.cpp b/tests/crypto/base_n_tests.cpp index 9df7b91..97786a1 100644 --- a/tests/crypto/base_n_tests.cpp +++ b/tests/crypto/base_n_tests.cpp @@ -17,8 +17,8 @@ static const std::string TEST5("\0\0\0", 3); static void test_16( const std::string& test, const std::string& expected ) { std::vector vec( test.begin(), test.end() ); - fc::string enc1 = fc::to_hex( vec ); - fc::string enc2 = fc::to_hex( test.c_str(), test.size() ); + std::string enc1 = fc::to_hex( vec ); + std::string enc2 = fc::to_hex( test.c_str(), test.size() ); BOOST_CHECK_EQUAL( enc1, enc2 ); BOOST_CHECK_EQUAL( expected, enc2 ); @@ -46,8 +46,8 @@ BOOST_AUTO_TEST_CASE(hex_test) static void test_36( const std::string& test, const std::string& expected ) { std::vector vec( test.begin(), test.end() ); - fc::string enc1 = fc::to_base36( vec ); - fc::string enc2 = fc::to_base36( test.c_str(), test.size() ); + std::string enc1 = fc::to_base36( vec ); + std::string enc2 = fc::to_base36( test.c_str(), test.size() ); BOOST_CHECK_EQUAL( enc1, enc2 ); BOOST_CHECK_EQUAL( expected, enc2 ); @@ -69,8 +69,8 @@ BOOST_AUTO_TEST_CASE(base36_test) static void test_58( const std::string& test, const std::string& expected ) { std::vector vec( test.begin(), test.end() ); - fc::string enc1 = fc::to_base58( vec ); - fc::string enc2 = fc::to_base58( test.c_str(), test.size() ); + std::string enc1 = fc::to_base58( vec ); + std::string enc2 = fc::to_base58( test.c_str(), test.size() ); BOOST_CHECK_EQUAL( enc1, enc2 ); BOOST_CHECK_EQUAL( expected, enc2 ); @@ -100,8 +100,8 @@ BOOST_AUTO_TEST_CASE(base58_test) static void test_64( const std::string& test, const std::string& expected ) { - fc::string enc1 = fc::base64_encode( test ); - fc::string enc2 = fc::base64_encode( test.c_str(), test.size() ); + std::string enc1 = fc::base64_encode( test ); + std::string enc2 = fc::base64_encode( test.c_str(), test.size() ); BOOST_CHECK_EQUAL( enc1, enc2 ); BOOST_CHECK_EQUAL( expected, enc2 ); diff --git a/tests/hmac_test.cpp b/tests/hmac_test.cpp index 140b211..ea3e54b 100644 --- a/tests/hmac_test.cpp +++ b/tests/hmac_test.cpp @@ -9,63 +9,63 @@ // See http://tools.ietf.org/html/rfc4231 -static const fc::string TEST1_KEY = "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"; -static const fc::string TEST1_DATA = "4869205468657265"; -static const fc::string TEST1_224 = "896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22"; -static const fc::string TEST1_256 = "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7"; -static const fc::string TEST1_512 = "87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cde" +static const std::string TEST1_KEY = "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"; +static const std::string TEST1_DATA = "4869205468657265"; +static const std::string TEST1_224 = "896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22"; +static const std::string TEST1_256 = "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7"; +static const std::string TEST1_512 = "87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cde" "daa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854"; -static const fc::string TEST2_KEY = "4a656665"; -static const fc::string TEST2_DATA = "7768617420646f2079612077616e7420666f72206e6f7468696e673f"; -static const fc::string TEST2_224 = "a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44"; -static const fc::string TEST2_256 = "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843"; -static const fc::string TEST2_512 = "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea250554" +static const std::string TEST2_KEY = "4a656665"; +static const std::string TEST2_DATA = "7768617420646f2079612077616e7420666f72206e6f7468696e673f"; +static const std::string TEST2_224 = "a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44"; +static const std::string TEST2_256 = "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843"; +static const std::string TEST2_512 = "164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea250554" "9758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737"; -static const fc::string TEST3_KEY = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; -static const fc::string TEST3_DATA = "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" +static const std::string TEST3_KEY = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; +static const std::string TEST3_DATA = "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" "dddddddddddddddddddddddddddddddddddd"; -static const fc::string TEST3_224 = "7fb3cb3588c6c1f6ffa9694d7d6ad2649365b0c1f65d69d1ec8333ea"; -static const fc::string TEST3_256 = "773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe"; -static const fc::string TEST3_512 = "fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39" +static const std::string TEST3_224 = "7fb3cb3588c6c1f6ffa9694d7d6ad2649365b0c1f65d69d1ec8333ea"; +static const std::string TEST3_256 = "773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe"; +static const std::string TEST3_512 = "fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39" "bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fb"; -static const fc::string TEST4_KEY = "0102030405060708090a0b0c0d0e0f10111213141516171819"; -static const fc::string TEST4_DATA = "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd" +static const std::string TEST4_KEY = "0102030405060708090a0b0c0d0e0f10111213141516171819"; +static const std::string TEST4_DATA = "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd" "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"; -static const fc::string TEST4_224 = "6c11506874013cac6a2abc1bb382627cec6a90d86efc012de7afec5a"; -static const fc::string TEST4_256 = "82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b"; -static const fc::string TEST4_512 = "b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3db" +static const std::string TEST4_224 = "6c11506874013cac6a2abc1bb382627cec6a90d86efc012de7afec5a"; +static const std::string TEST4_256 = "82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b"; +static const std::string TEST4_512 = "b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3db" "a91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298dd"; // test 5 skipped - truncated -static const fc::string TEST6_KEY = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +static const std::string TEST6_KEY = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "aaaaaa"; -static const fc::string TEST6_DATA = "54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a" +static const std::string TEST6_DATA = "54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a" "65204b6579202d2048617368204b6579204669727374"; -static const fc::string TEST6_224 = "95e9a0db962095adaebe9b2d6f0dbce2d499f112f2d2b7273fa6870e"; -static const fc::string TEST6_256 = "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54"; -static const fc::string TEST6_512 = "80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f352" +static const std::string TEST6_224 = "95e9a0db962095adaebe9b2d6f0dbce2d499f112f2d2b7273fa6870e"; +static const std::string TEST6_256 = "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54"; +static const std::string TEST6_512 = "80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f352" "6b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598"; -static const fc::string TEST7_KEY = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +static const std::string TEST7_KEY = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "aaaaaa"; -static const fc::string TEST7_DATA = "5468697320697320612074657374207573696e672061206c6172676572207468" +static const std::string TEST7_DATA = "5468697320697320612074657374207573696e672061206c6172676572207468" "616e20626c6f636b2d73697a65206b657920616e642061206c61726765722074" "68616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565" "647320746f20626520686173686564206265666f7265206265696e6720757365" "642062792074686520484d414320616c676f726974686d2e"; -static const fc::string TEST7_224 = "3a854166ac5d9f023f54d517d0b39dbd946770db9c2b95c9f6f565d1"; -static const fc::string TEST7_256 = "9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2"; -static const fc::string TEST7_512 = "e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944" +static const std::string TEST7_224 = "3a854166ac5d9f023f54d517d0b39dbd946770db9c2b95c9f6f565d1"; +static const std::string TEST7_256 = "9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2"; +static const std::string TEST7_512 = "e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944" "b6022cac3c4982b10d5eeb55c3e4de15134676fb6de0446065c97440fa8c6a58"; static fc::hmac mac_224; @@ -73,8 +73,8 @@ static fc::hmac mac_256; static fc::hmac mac_512; template -static void run_test( const fc::string& key, const fc::string& data, const fc::string& expect_224, - const fc::string& expect_256, const fc::string& expect_512 ) +static void run_test( const std::string& key, const std::string& data, const std::string& expect_224, + const std::string& expect_256, const std::string& expect_512 ) { fc::array key_arr; diff --git a/tests/io/stream_tests.cpp b/tests/io/stream_tests.cpp index e3f3dc8..359ec9d 100644 --- a/tests/io/stream_tests.cpp +++ b/tests/io/stream_tests.cpp @@ -12,8 +12,8 @@ BOOST_AUTO_TEST_SUITE(stream_tests) BOOST_AUTO_TEST_CASE(stringstream_test) { - const fc::string constant( "Hello", 6 ); // includes trailing \0 - fc::string writable( "World" ); + const std::string constant( "Hello", 6 ); // includes trailing \0 + std::string writable( "World" ); fc::stringstream in1( constant ); fc::stringstream in2( writable ); fc::stringstream out; @@ -44,8 +44,8 @@ BOOST_AUTO_TEST_CASE(stringstream_test) BOOST_AUTO_TEST_CASE(buffered_stringstream_test) { - const fc::string constant( "Hello", 6 ); // includes trailing \0 - fc::string writable( "World" ); + const std::string constant( "Hello", 6 ); // includes trailing \0 + std::string writable( "World" ); fc::istream_ptr in1( new fc::stringstream( constant ) ); std::shared_ptr in2( new fc::stringstream( writable ) ); std::shared_ptr out1( new fc::stringstream() ); diff --git a/tests/logging_tests.cpp b/tests/logging_tests.cpp index 659c80d..ef66501 100644 --- a/tests/logging_tests.cpp +++ b/tests/logging_tests.cpp @@ -46,7 +46,7 @@ BOOST_AUTO_TEST_CASE(log_reboot) int64_t interval_seconds = conf.rotation_interval.to_seconds(); int64_t file_number = now.sec_since_epoch() / interval_seconds; fc::time_point_sec start_time = fc::time_point_sec( (uint32_t)(file_number * interval_seconds) ); - fc::string timestamp_string = start_time.to_non_delimited_iso_string(); + std::string timestamp_string = start_time.to_non_delimited_iso_string(); fc::path link_filename = conf.filename; fc::path log_filename = link_filename.parent_path() / (link_filename.filename().string() + "." + timestamp_string); From d2b7354c24c75a8b1c9e52bb88dfef00dcd53c26 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Thu, 4 Apr 2019 14:00:20 +0200 Subject: [PATCH 02/11] Remove unused base36 --- CMakeLists.txt | 1 - include/fc/crypto/base36.hpp | 9 ---- src/crypto/base36.cpp | 86 ----------------------------------- tests/crypto/base_n_tests.cpp | 24 ---------- 4 files changed, 120 deletions(-) delete mode 100644 include/fc/crypto/base36.hpp delete mode 100644 src/crypto/base36.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index fbf1ca0..f0d7f8e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -254,7 +254,6 @@ set( fc_sources src/crypto/aes.cpp src/crypto/crc.cpp src/crypto/city.cpp - src/crypto/base36.cpp src/crypto/base58.cpp src/crypto/base64.cpp src/crypto/bigint.cpp diff --git a/include/fc/crypto/base36.hpp b/include/fc/crypto/base36.hpp deleted file mode 100644 index 1cd158e..0000000 --- a/include/fc/crypto/base36.hpp +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include - -namespace fc -{ - std::vector from_base36( const std::string& b36 ); - std::string to_base36( const std::vector& vec ); - std::string to_base36( const char* data, size_t len ); -} diff --git a/src/crypto/base36.cpp b/src/crypto/base36.cpp deleted file mode 100644 index c4a9d23..0000000 --- a/src/crypto/base36.cpp +++ /dev/null @@ -1,86 +0,0 @@ -#include -#include -#include -#include -#include - -namespace fc -{ - std::string to_base36( const char* data, size_t len ) - { - if( len == 0 ) return std::string(); - - const char* src = data; - int src_len = len; - std::unique_ptr buffer(new char[len+1]); - if (*data & 0x80) { - buffer[0] = 0; - memcpy( buffer.get() + 1, data, len ); - src = buffer.get(); - src_len++; - } - fc::bigint value( src, src_len ); - - auto base36 = "0123456789abcdefghijklmnopqrstuvwxyz"; - std::vector out( static_cast(len * 1.6) + 2 ); - int pos = out.size() - 1; - out[pos] = '\0'; - fc::bigint _36(36); - do { - if( value ) { - --pos; - out[pos] = base36[(value % _36).to_int64()]; - } - } while (value /= _36); - while (len-- > 0 && *data++ == 0) { - out[--pos] = '0'; - } - return &out[pos]; //std::string( &out[pos], out.size() - pos); - } - - std::string to_base36( const std::vector& vec ) - { - return to_base36( (const char*)vec.data(), vec.size() ); - } - - std::vector from_base36( const std::string& b36 ) - { - if ( b36.empty() ) { - std::vector empty; - return empty; - } - - fc::bigint value; - - fc::bigint pos = 0; - fc::bigint _36(36); - for( auto itr = b36.rbegin(); itr != b36.rend(); ++itr ) - { - if( *itr >= '0' && *itr <= '9' ) - value = value + _36.exp(pos) * fc::bigint(*itr - '0'); - else if( *itr >= 'a' && *itr <= 'z' ) - value = value + (_36.exp(pos) * fc::bigint(10+*itr - 'a')); - else if( *itr >= 'A' && *itr <= 'Z' ) - value = value + (_36.exp(pos) * fc::bigint(10+*itr - 'A')); - else - { - wlog("unknown '${char}'", ("char",std::string(&*itr,1)) ); - } - ++pos; - } - - std::vector bytes = value; - int leading_zeros = 0, len = bytes.size(); - const char *in = b36.c_str(); - while (*in++ == '0') { leading_zeros++; } - char* first = bytes.data(); - while (len > 0 && *first == 0) { first++; len--; } - std::vector result; - result.resize(leading_zeros + len, 0); - if (len) - { - memcpy( result.data() + leading_zeros, first, len ); - } - return result; - } -} diff --git a/tests/crypto/base_n_tests.cpp b/tests/crypto/base_n_tests.cpp index 97786a1..fbc60ce 100644 --- a/tests/crypto/base_n_tests.cpp +++ b/tests/crypto/base_n_tests.cpp @@ -1,7 +1,6 @@ #include #include -#include #include #include #include @@ -43,29 +42,6 @@ BOOST_AUTO_TEST_CASE(hex_test) } -static void test_36( const std::string& test, const std::string& expected ) -{ - std::vector vec( test.begin(), test.end() ); - std::string enc1 = fc::to_base36( vec ); - std::string enc2 = fc::to_base36( test.c_str(), test.size() ); - BOOST_CHECK_EQUAL( enc1, enc2 ); - BOOST_CHECK_EQUAL( expected, enc2 ); - - std::vector dec = fc::from_base36( enc1 ); - BOOST_CHECK_EQUAL( vec.size(), dec.size() ); - BOOST_CHECK( vec == dec ); -} - -BOOST_AUTO_TEST_CASE(base36_test) -{ - test_36( TEST1, "" ); - test_36( TEST2, "01o35" ); - test_36( TEST3, "l4ksdleyi5pnl0un5raue268ptj43dwjwmz15ie2" ); - test_36( TEST4, "2rrrvpb7y4" ); - test_36( TEST5, "000" ); -} - - static void test_58( const std::string& test, const std::string& expected ) { std::vector vec( test.begin(), test.end() ); From 637226a2e4c9feb488b6680f29cf17cc685c1cbe Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Thu, 4 Apr 2019 14:03:09 +0200 Subject: [PATCH 03/11] Remove unused blowfish --- CMakeLists.txt | 1 - include/fc/crypto/blowfish.hpp | 178 ---------- src/crypto/blowfish.cpp | 624 --------------------------------- tests/CMakeLists.txt | 1 - tests/crypto/blowfish_test.cpp | 169 --------- 5 files changed, 973 deletions(-) delete mode 100644 include/fc/crypto/blowfish.hpp delete mode 100644 src/crypto/blowfish.cpp delete mode 100644 tests/crypto/blowfish_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f0d7f8e..19f8bae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -264,7 +264,6 @@ set( fc_sources src/crypto/sha224.cpp src/crypto/sha512.cpp src/crypto/dh.cpp - src/crypto/blowfish.cpp src/crypto/elliptic_common.cpp ${ECC_REST} src/crypto/elliptic_${ECC_IMPL}.cpp diff --git a/include/fc/crypto/blowfish.hpp b/include/fc/crypto/blowfish.hpp deleted file mode 100644 index fcda4e5..0000000 --- a/include/fc/crypto/blowfish.hpp +++ /dev/null @@ -1,178 +0,0 @@ -//////////////////////////////////////////////////////////////////////////// -/// -// Blowfish.h Header File -// -// BLOWFISH ENCRYPTION ALGORITHM -// -// encryption and decryption of Byte Strings using the Blowfish encryption Algorithm. -// Blowfish is a block cipher that encrypts data in 8-byte blocks. The algorithm consists -// of two parts: a key-expansion part and a data-ancryption part. Key expansion converts a -// variable key of at least 1 and at most 56 bytes into several subkey arrays totaling -// 4168 bytes. Blowfish has 16 rounds. Each round consists of a key-dependent permutation, -// and a key and data-dependent substitution. All operations are XORs and additions on 32-bit words. -// The only additional operations are four indexed array data lookups per round. -// Blowfish uses a large number of subkeys. These keys must be precomputed before any data -// encryption or decryption. The P-array consists of 18 32-bit subkeys: P0, P1,...,P17. -// There are also four 32-bit S-boxes with 256 entries each: S0,0, S0,1,...,S0,255; -// S1,0, S1,1,...,S1,255; S2,0, S2,1,...,S2,255; S3,0, S3,1,...,S3,255; -// -// The Electronic Code Book (ECB), Cipher Block Chaining (CBC) and Cipher Feedback modes -// are used: -// -// In ECB mode if the same block is encrypted twice with the same key, the resulting -// ciphertext blocks are the same. -// -// In CBC Mode a ciphertext block is obtained by first xoring the -// plaintext block with the previous ciphertext block, and encrypting the resulting value. -// -// In CFB mode a ciphertext block is obtained by encrypting the previous ciphertext block -// and xoring the resulting value with the plaintext -// -// The previous ciphertext block is usually stored in an Initialization Vector (IV). -// An Initialization Vector of zero is commonly used for the first block, though other -// arrangements are also in use. - -/* -http://www.counterpane.com/vectors.txt -Test vectors by Eric Young. These tests all assume Blowfish with 16 -rounds. - -All data is shown as a hex string with 012345 loading as -data[0]=0x01; -data[1]=0x23; -data[2]=0x45; -ecb test data (taken from the DES validation tests) - -key bytes clear bytes cipher bytes -0000000000000000 0000000000000000 4EF997456198DD78 -FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF 51866FD5B85ECB8A -3000000000000000 1000000000000001 7D856F9A613063F2 ??? -1111111111111111 1111111111111111 2466DD878B963C9D -0123456789ABCDEF 1111111111111111 61F9C3802281B096 -1111111111111111 0123456789ABCDEF 7D0CC630AFDA1EC7 -0000000000000000 0000000000000000 4EF997456198DD78 -FEDCBA9876543210 0123456789ABCDEF 0ACEAB0FC6A0A28D -7CA110454A1A6E57 01A1D6D039776742 59C68245EB05282B -0131D9619DC1376E 5CD54CA83DEF57DA B1B8CC0B250F09A0 -07A1133E4A0B2686 0248D43806F67172 1730E5778BEA1DA4 -3849674C2602319E 51454B582DDF440A A25E7856CF2651EB -04B915BA43FEB5B6 42FD443059577FA2 353882B109CE8F1A -0113B970FD34F2CE 059B5E0851CF143A 48F4D0884C379918 -0170F175468FB5E6 0756D8E0774761D2 432193B78951FC98 -43297FAD38E373FE 762514B829BF486A 13F04154D69D1AE5 -07A7137045DA2A16 3BDD119049372802 2EEDDA93FFD39C79 -04689104C2FD3B2F 26955F6835AF609A D887E0393C2DA6E3 -37D06BB516CB7546 164D5E404F275232 5F99D04F5B163969 -1F08260D1AC2465E 6B056E18759F5CCA 4A057A3B24D3977B -584023641ABA6176 004BD6EF09176062 452031C1E4FADA8E -025816164629B007 480D39006EE762F2 7555AE39F59B87BD -49793EBC79B3258F 437540C8698F3CFA 53C55F9CB49FC019 -4FB05E1515AB73A7 072D43A077075292 7A8E7BFA937E89A3 -49E95D6D4CA229BF 02FE55778117F12A CF9C5D7A4986ADB5 -018310DC409B26D6 1D9D5C5018F728C2 D1ABB290658BC778 -1C587F1C13924FEF 305532286D6F295A 55CB3774D13EF201 -0101010101010101 0123456789ABCDEF FA34EC4847B268B2 -1F1F1F1F0E0E0E0E 0123456789ABCDEF A790795108EA3CAE -E0FEE0FEF1FEF1FE 0123456789ABCDEF C39E072D9FAC631D -0000000000000000 FFFFFFFFFFFFFFFF 014933E0CDAFF6E4 -FFFFFFFFFFFFFFFF 0000000000000000 F21E9A77B71C49BC -0123456789ABCDEF 0000000000000000 245946885754369A -FEDCBA9876543210 FFFFFFFFFFFFFFFF 6B5C5A9C5D9E0A5A - -set_key test data -data[8]= FEDCBA9876543210 -c=F9AD597C49DB005E k[ 1]=F0 -c=E91D21C1D961A6D6 k[ 2]=F0E1 -c=E9C2B70A1BC65CF3 k[ 3]=F0E1D2 -c=BE1E639408640F05 k[ 4]=F0E1D2C3 -c=B39E44481BDB1E6E k[ 5]=F0E1D2C3B4 -c=9457AA83B1928C0D k[ 6]=F0E1D2C3B4A5 -c=8BB77032F960629D k[ 7]=F0E1D2C3B4A596 -c=E87A244E2CC85E82 k[ 8]=F0E1D2C3B4A59687 -c=15750E7A4F4EC577 k[ 9]=F0E1D2C3B4A5968778 -c=122BA70B3AB64AE0 k[10]=F0E1D2C3B4A596877869 -c=3A833C9AFFC537F6 k[11]=F0E1D2C3B4A5968778695A -c=9409DA87A90F6BF2 k[12]=F0E1D2C3B4A5968778695A4B -c=884F80625060B8B4 k[13]=F0E1D2C3B4A5968778695A4B3C -c=1F85031C19E11968 k[14]=F0E1D2C3B4A5968778695A4B3C2D -c=79D9373A714CA34F k[15]=F0E1D2C3B4A5968778695A4B3C2D1E ??? -c=93142887EE3BE15C k[16]=F0E1D2C3B4A5968778695A4B3C2D1E0F -c=03429E838CE2D14B k[17]=F0E1D2C3B4A5968778695A4B3C2D1E0F00 -c=A4299E27469FF67B k[18]=F0E1D2C3B4A5968778695A4B3C2D1E0F0011 -c=AFD5AED1C1BC96A8 k[19]=F0E1D2C3B4A5968778695A4B3C2D1E0F001122 -c=10851C0E3858DA9F k[20]=F0E1D2C3B4A5968778695A4B3C2D1E0F00112233 -c=E6F51ED79B9DB21F k[21]=F0E1D2C3B4A5968778695A4B3C2D1E0F0011223344 -c=64A6E14AFD36B46F k[22]=F0E1D2C3B4A5968778695A4B3C2D1E0F001122334455 -c=80C7D7D45A5479AD k[23]=F0E1D2C3B4A5968778695A4B3C2D1E0F00112233445566 -c=05044B62FA52D080 k[24]=F0E1D2C3B4A5968778695A4B3C2D1E0F0011223344556677 - -chaining mode test data -key[16] = 0123456789ABCDEFF0E1D2C3B4A59687 -iv[8] = FEDCBA9876543210 -data[29] = "7654321 Now is the time for " (includes trailing '\0') -data[29] = 37363534333231204E6F77206973207468652074696D6520666F722000 -cbc cipher text -cipher[32]= 6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC -cfb64 cipher text cipher[29]= -E73214A2822139CAF26ECF6D2EB9E76E3DA3DE04D1517200519D57A6C3 -ofb64 cipher text cipher[29]= -E73214A2822139CA62B343CC5B65587310DD908D0C241B2263C2CF80DA - -*/ - -#pragma once -#include - -namespace fc { - -//Block Structure -struct sblock -{ - //Constructors - sblock(unsigned int l=0, unsigned int r=0) : m_uil(l), m_uir(r) {} - //Copy Constructor - sblock(const sblock& roBlock) : m_uil(roBlock.m_uil), m_uir(roBlock.m_uir) {} - sblock& operator^=(sblock& b) { m_uil ^= b.m_uil; m_uir ^= b.m_uir; return *this; } - unsigned int m_uil, m_uir; -}; - -class blowfish -{ -public: - enum { ECB=0, CBC=1, CFB=2 }; - - //Constructor - Initialize the P and S boxes for a given Key - blowfish(); - void start(unsigned char* ucKey, uint64_t n, const sblock& roChain = sblock(0UL,0UL)); - - //Resetting the chaining block - void reset_chain() { m_oChain = m_oChain0; } - - // encrypt/decrypt Buffer in Place - void encrypt(unsigned char* buf, uint64_t n, int iMode=CFB); - void decrypt(unsigned char* buf, uint64_t n, int iMode=CFB); - - // encrypt/decrypt from Input Buffer to Output Buffer - void encrypt(const unsigned char* in, unsigned char* out, uint64_t n, int iMode=CFB); - void decrypt(const unsigned char* in, unsigned char* out, uint64_t n, int iMode=CFB); - -//Private Functions -private: - unsigned int F(unsigned int ui); - void encrypt(sblock&); - void decrypt(sblock&); - -private: - //The Initialization Vector, by default {0, 0} - sblock m_oChain0; - sblock m_oChain; - unsigned int m_auiP[18]; - unsigned int m_auiS[4][256]; - static const unsigned int scm_auiInitP[18]; - static const unsigned int scm_auiInitS[4][256]; -}; - - -} // namespace fc - - diff --git a/src/crypto/blowfish.cpp b/src/crypto/blowfish.cpp deleted file mode 100644 index 5d3cbb4..0000000 --- a/src/crypto/blowfish.cpp +++ /dev/null @@ -1,624 +0,0 @@ -//////////////////////////////////////////////////////////////////////////// -/// -// BlowFish.cpp -// -// Implementation of Bruce Schneier's BLOWFISH algorithm from "Applied -// Cryptography", Second Edition. - -#include -#include -#include - -namespace fc { -//Extract low order byte -inline unsigned char Byte(unsigned int ui) -{ - return (unsigned char)(ui & 0xff); -} - -//Function F -inline unsigned int blowfish::F(unsigned int ui) -{ - return ((m_auiS[0][Byte(ui>>24)] + m_auiS[1][Byte(ui>>16)]) ^ m_auiS[2][Byte(ui>>8)]) + m_auiS[3][Byte(ui)]; -} - -//Initialization with a fixed string which consists of the hexadecimal digits of PI (less the initial 3) -//P-array, 18 32-bit subkeys -const unsigned int blowfish::scm_auiInitP[18] = { - 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, - 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89, - 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, - 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, - 0x9216d5d9, 0x8979fb1b -}; - -//Four 32-bit S-boxes with 256 entries each -const unsigned int blowfish::scm_auiInitS[4][256] = { - //0 - {0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, - 0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99, - 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, - 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e, - 0x0d95748f, 0x728eb658, 0x718bcd58, 0x82154aee, - 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013, - 0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef, - 0x8e79dcb0, 0x603a180e, 0x6c9e0e8b, 0xb01e8a3e, - 0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60, - 0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440, - 0x55ca396a, 0x2aab10b6, 0xb4cc5c34, 0x1141e8ce, - 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a, - 0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e, - 0xafd6ba33, 0x6c24cf5c, 0x7a325381, 0x28958677, - 0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193, - 0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032, - 0xef845d5d, 0xe98575b1, 0xdc262302, 0xeb651b88, - 0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239, - 0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e, - 0x21c66842, 0xf6e96c9a, 0x670c9c61, 0xabd388f0, - 0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3, - 0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98, - 0xa1f1651d, 0x39af0176, 0x66ca593e, 0x82430e88, - 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe, - 0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6, - 0x4ed3aa62, 0x363f7706, 0x1bfedf72, 0x429b023d, - 0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b, - 0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7, - 0xe3fe501a, 0xb6794c3b, 0x976ce0bd, 0x04c006ba, - 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463, - 0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f, - 0x6dfc511f, 0x9b30952c, 0xcc814544, 0xaf5ebd09, - 0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3, - 0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb, - 0x5579c0bd, 0x1a60320a, 0xd6a100c6, 0x402c7279, - 0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8, - 0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab, - 0x323db5fa, 0xfd238760, 0x53317b48, 0x3e00df82, - 0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db, - 0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573, - 0x695b27b0, 0xbbca58c8, 0xe1ffa35d, 0xb8f011a0, - 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b, - 0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790, - 0xe1ddf2da, 0xa4cb7e33, 0x62fb1341, 0xcee4c6e8, - 0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4, - 0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0, - 0xd08ed1d0, 0xafc725e0, 0x8e3c5b2f, 0x8e7594b7, - 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c, - 0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad, - 0x2f2f2218, 0xbe0e1777, 0xea752dfe, 0x8b021fa1, - 0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299, - 0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9, - 0x165fa266, 0x80957705, 0x93cc7314, 0x211a1477, - 0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf, - 0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49, - 0x00250e2d, 0x2071b35e, 0x226800bb, 0x57b8e0af, - 0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa, - 0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5, - 0x83260376, 0x6295cfa9, 0x11c81968, 0x4e734a41, - 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915, - 0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400, - 0x08ba6fb5, 0x571be91f, 0xf296ec6b, 0x2a0dd915, - 0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664, - 0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a}, - - //1 - {0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623, - 0xad6ea6b0, 0x49a7df7d, 0x9cee60b8, 0x8fedb266, - 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1, - 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e, - 0x3f54989a, 0x5b429d65, 0x6b8fe4d6, 0x99f73fd6, - 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1, - 0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e, - 0x09686b3f, 0x3ebaefc9, 0x3c971814, 0x6b6a70a1, - 0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737, - 0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8, - 0xb03ada37, 0xf0500c0d, 0xf01c1f04, 0x0200b3ff, - 0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd, - 0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701, - 0x3ae5e581, 0x37c2dadc, 0xc8b57634, 0x9af3dda7, - 0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41, - 0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331, - 0x4e548b38, 0x4f6db908, 0x6f420d03, 0xf60a04bf, - 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af, - 0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e, - 0x5512721f, 0x2e6b7124, 0x501adde6, 0x9f84cd87, - 0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c, - 0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2, - 0xef1c1847, 0x3215d908, 0xdd433b37, 0x24c2ba16, - 0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd, - 0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b, - 0x043556f1, 0xd7a3c76b, 0x3c11183b, 0x5924a509, - 0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e, - 0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3, - 0x771fe71c, 0x4e3d06fa, 0x2965dcb9, 0x99e71d0f, - 0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a, - 0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4, - 0xf2f74ea7, 0x361d2b3d, 0x1939260f, 0x19c27960, - 0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66, - 0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28, - 0xc332ddef, 0xbe6c5aa5, 0x65582185, 0x68ab9802, - 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84, - 0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510, - 0x13cca830, 0xeb61bd96, 0x0334fe1e, 0xaa0363cf, - 0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14, - 0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e, - 0x648b1eaf, 0x19bdf0ca, 0xa02369b9, 0x655abb50, - 0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7, - 0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8, - 0xf837889a, 0x97e32d77, 0x11ed935f, 0x16681281, - 0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99, - 0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696, - 0xcdb30aeb, 0x532e3054, 0x8fd948e4, 0x6dbc3128, - 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73, - 0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0, - 0x45eee2b6, 0xa3aaabea, 0xdb6c4f15, 0xfacb4fd0, - 0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105, - 0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250, - 0xcf62a1f2, 0x5b8d2646, 0xfc8883a0, 0xc1c7b6a3, - 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285, - 0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00, - 0x58428d2a, 0x0c55f5ea, 0x1dadf43e, 0x233f7061, - 0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb, - 0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e, - 0xa6078084, 0x19f8509e, 0xe8efd855, 0x61d99735, - 0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc, - 0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9, - 0xdb73dbd3, 0x105588cd, 0x675fda79, 0xe3674340, - 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20, - 0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7}, - - //2 - {0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, - 0x411520f7, 0x7602d4f7, 0xbcf46b2e, 0xd4a20068, - 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af, - 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840, - 0x4d95fc1d, 0x96b591af, 0x70f4ddd3, 0x66a02f45, - 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504, - 0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a, - 0x28507825, 0x530429f4, 0x0a2c86da, 0xe9b66dfb, - 0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee, - 0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6, - 0xaace1e7c, 0xd3375fec, 0xce78a399, 0x406b2a42, - 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b, - 0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2, - 0x3a6efa74, 0xdd5b4332, 0x6841e7f7, 0xca7820fb, - 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527, - 0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b, - 0x55a867bc, 0xa1159a58, 0xcca92963, 0x99e1db33, - 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c, - 0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3, - 0x95c11548, 0xe4c66d22, 0x48c1133f, 0xc70f86dc, - 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17, - 0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564, - 0x257b7834, 0x602a9c60, 0xdff8e8a3, 0x1f636c1b, - 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115, - 0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922, - 0x85b2a20e, 0xe6ba0d99, 0xde720c8c, 0x2da2f728, - 0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0, - 0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e, - 0x0a476341, 0x992eff74, 0x3a6f6eab, 0xf4f8fd37, - 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d, - 0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804, - 0xf1290dc7, 0xcc00ffa3, 0xb5390f92, 0x690fed0b, - 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3, - 0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb, - 0x37392eb3, 0xcc115979, 0x8026e297, 0xf42e312d, - 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c, - 0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350, - 0x1a6b1018, 0x11caedfa, 0x3d25bdd8, 0xe2e1c3c9, - 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a, - 0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe, - 0x9dbc8057, 0xf0f7c086, 0x60787bf8, 0x6003604d, - 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc, - 0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f, - 0x77a057be, 0xbde8ae24, 0x55464299, 0xbf582e61, - 0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2, - 0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9, - 0x7aeb2661, 0x8b1ddf84, 0x846a0e79, 0x915f95e2, - 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c, - 0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e, - 0xb77f19b6, 0xe0a9dc09, 0x662d09a1, 0xc4324633, - 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10, - 0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169, - 0xdcb7da83, 0x573906fe, 0xa1e2ce9b, 0x4fcd7f52, - 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027, - 0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5, - 0xf0177a28, 0xc0f586e0, 0x006058aa, 0x30dc7d62, - 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634, - 0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76, - 0x6f05e409, 0x4b7c0188, 0x39720a3d, 0x7c927c24, - 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc, - 0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4, - 0x1e50ef5e, 0xb161e6f8, 0xa28514d9, 0x6c51133c, - 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837, - 0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0}, - - //3 - {0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, - 0x5cb0679e, 0x4fa33742, 0xd3822740, 0x99bc9bbe, - 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b, - 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4, - 0x5748ab2f, 0xbc946e79, 0xc6a376d2, 0x6549c2c8, - 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6, - 0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304, - 0xa1fad5f0, 0x6a2d519a, 0x63ef8ce2, 0x9a86ee22, - 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4, - 0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6, - 0x2826a2f9, 0xa73a3ae1, 0x4ba99586, 0xef5562e9, - 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59, - 0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593, - 0xe990fd5a, 0x9e34d797, 0x2cf0b7d9, 0x022b8b51, - 0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28, - 0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c, - 0xe029ac71, 0xe019a5e6, 0x47b0acfd, 0xed93fa9b, - 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28, - 0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c, - 0x15056dd4, 0x88f46dba, 0x03a16125, 0x0564f0bd, - 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a, - 0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319, - 0x7533d928, 0xb155fdf5, 0x03563482, 0x8aba3cbb, - 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f, - 0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991, - 0xea7a90c2, 0xfb3e7bce, 0x5121ce64, 0x774fbe32, - 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680, - 0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166, - 0xb39a460a, 0x6445c0dd, 0x586cdecf, 0x1c20c8ae, - 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb, - 0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, - 0x72eacea8, 0xfa6484bb, 0x8d6612ae, 0xbf3c6f47, - 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370, - 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d, - 0x4040cb08, 0x4eb4e2cc, 0x34d2466a, 0x0115af84, - 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048, - 0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8, - 0x611560b1, 0xe7933fdc, 0xbb3a792b, 0x344525bd, - 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9, - 0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7, - 0x1a908749, 0xd44fbd9a, 0xd0dadecb, 0xd50ada38, - 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f, - 0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, - 0xbf97222c, 0x15e6fc2a, 0x0f91fc71, 0x9b941525, - 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1, - 0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442, - 0xe0ec6e0e, 0x1698db3b, 0x4c98a0be, 0x3278e964, - 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e, - 0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, - 0xdf359f8d, 0x9b992f2e, 0xe60b6f47, 0x0fe3f11d, - 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f, - 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299, - 0xf523f357, 0xa6327623, 0x93a83531, 0x56cccd02, - 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc, - 0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614, - 0xe6c6c7bd, 0x327a140a, 0x45e1d006, 0xc3f27b9a, - 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6, - 0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b, - 0x53113ec0, 0x1640e3d3, 0x38abbd60, 0x2547adf0, - 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060, - 0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, - 0x1948c25c, 0x02fb8a8c, 0x01c36ae4, 0xd6ebe1f9, - 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f, - 0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6} -}; - -//Constructor - Initialize the P and S boxes for a given Key -blowfish::blowfish() -{} - -void blowfish::start(unsigned char* ucKey, uint64_t keysize, const sblock& roChain) -{ - m_oChain0 = roChain; - m_oChain = roChain; - - if(keysize<1) - FC_THROW_EXCEPTION( exception, "invalid key length" ); - //Check the Key - the key length should be between 1 and 56 bytes - if(keysize>56) - keysize = 56; - unsigned char aucLocalKey[56]; - unsigned int i, j; - memcpy(aucLocalKey, ucKey, static_cast(keysize)); - //Reflexive Initialization of the Blowfish. - //Generating the Subkeys from the Key flood P and S boxes with PI - memcpy(m_auiP, scm_auiInitP, sizeof m_auiP); - memcpy(m_auiS, scm_auiInitS, sizeof m_auiS); - //Load P boxes with key bytes - const unsigned char* p = aucLocalKey; - unsigned int x=0; - //Repeatedly cycle through the key bits until the entire P array has been XORed with key bits - uint32_t iCount = 0; - for(i=0; i<18; i++) - { - x=0; - for(int n=4; n--; ) - { - //int iVal = (int)(*p); - x <<= 8; - x |= *(p++); - iCount++; - if(iCount == keysize) - { - //All bytes used, so recycle bytes - iCount = 0; - p = aucLocalKey; - } - } - m_auiP[i] ^= x; - } - //Reflect P and S boxes through the evolving Blowfish - sblock block(0UL,0UL); //all-zero block - for(i=0; i<18; ) - encrypt(block), m_auiP[i++] = block.m_uil, m_auiP[i++] = block.m_uir; - for(j=0; j<4; j++) - for(int k=0; k<256; ) - encrypt(block), m_auiS[j][k++] = block.m_uil, m_auiS[j][k++] = block.m_uir; -} - -//Sixteen Round Encipher of Block -void blowfish::encrypt(sblock& block) -{ - unsigned int uiLeft = block.m_uil; - unsigned int uiRight = block.m_uir; - uiLeft ^= m_auiP[0]; - uiRight ^= F(uiLeft)^m_auiP[1]; uiLeft ^= F(uiRight)^m_auiP[2]; - uiRight ^= F(uiLeft)^m_auiP[3]; uiLeft ^= F(uiRight)^m_auiP[4]; - uiRight ^= F(uiLeft)^m_auiP[5]; uiLeft ^= F(uiRight)^m_auiP[6]; - uiRight ^= F(uiLeft)^m_auiP[7]; uiLeft ^= F(uiRight)^m_auiP[8]; - uiRight ^= F(uiLeft)^m_auiP[9]; uiLeft ^= F(uiRight)^m_auiP[10]; - uiRight ^= F(uiLeft)^m_auiP[11]; uiLeft ^= F(uiRight)^m_auiP[12]; - uiRight ^= F(uiLeft)^m_auiP[13]; uiLeft ^= F(uiRight)^m_auiP[14]; - uiRight ^= F(uiLeft)^m_auiP[15]; uiLeft ^= F(uiRight)^m_auiP[16]; - uiRight ^= m_auiP[17]; - block.m_uil = uiRight; - block.m_uir = uiLeft; -} - -//Sixteen Round Decipher of sblock -void blowfish::decrypt(sblock& block) -{ - unsigned int uiLeft = block.m_uil; - unsigned int uiRight = block.m_uir; - uiLeft ^= m_auiP[17]; - uiRight ^= F(uiLeft)^m_auiP[16]; uiLeft ^= F(uiRight)^m_auiP[15]; - uiRight ^= F(uiLeft)^m_auiP[14]; uiLeft ^= F(uiRight)^m_auiP[13]; - uiRight ^= F(uiLeft)^m_auiP[12]; uiLeft ^= F(uiRight)^m_auiP[11]; - uiRight ^= F(uiLeft)^m_auiP[10]; uiLeft ^= F(uiRight)^m_auiP[9]; - uiRight ^= F(uiLeft)^m_auiP[8]; uiLeft ^= F(uiRight)^m_auiP[7]; - uiRight ^= F(uiLeft)^m_auiP[6]; uiLeft ^= F(uiRight)^m_auiP[5]; - uiRight ^= F(uiLeft)^m_auiP[4]; uiLeft ^= F(uiRight)^m_auiP[3]; - uiRight ^= F(uiLeft)^m_auiP[2]; uiLeft ^= F(uiRight)^m_auiP[1]; - uiRight ^= m_auiP[0]; - block.m_uil = uiRight; - block.m_uir = uiLeft; -} - -//Semi-Portable Byte Shuffling -inline void BytesToBlock(unsigned char const* p, sblock& b) -{ - unsigned int y; - //Left - b.m_uil = 0; - y = *p++; - y <<= 24; - b.m_uil |= y; - y = *p++; - y <<= 16; - b.m_uil |= y; - y = *p++; - y <<= 8; - b.m_uil |= y; - y = *p++; - b.m_uil |= y; - //Right - b.m_uir = 0; - y = *p++; - y <<= 24; - b.m_uir |= y; - y = *p++; - y <<= 16; - b.m_uir |= y; - y = *p++; - y <<= 8; - b.m_uir |= y; - y = *p++; - b.m_uir |= y; -} - -inline void BlockToBytes(sblock const& b, unsigned char* p) -{ - unsigned int y; - //Right - y = b.m_uir; - *--p = Byte(y); - y = b.m_uir >> 8; - *--p = Byte(y); - y = b.m_uir >> 16; - *--p = Byte(y); - y = b.m_uir >> 24; - *--p = Byte(y); - //Left - y = b.m_uil; - *--p = Byte(y); - y = b.m_uil >> 8; - *--p = Byte(y); - y = b.m_uil >> 16; - *--p = Byte(y); - y = b.m_uil >> 24; - *--p = Byte(y); -} - -//encrypt Buffer in Place -//Returns false if n is multiple of 8 -void blowfish::encrypt(unsigned char* buf, uint64_t n, int iMode) -{ - //Check the buffer's length - should be > 0 and multiple of 8 - if((n==0)||(n%8!=0)) - FC_THROW_EXCEPTION( exception, "invalid buffer length ${n}, not multiple of 8", ("n", n) ); - sblock work; - if(iMode == CBC) //CBC mode, using the Chain - { - sblock chain(m_oChain); - for(; n >= 8; n -= 8) - { - BytesToBlock(buf, work); - work ^= chain; - encrypt(work); - chain = work; - BlockToBytes(work, buf+=8); - } - } - else if(iMode == CFB) //CFB mode, using the Chain - { - sblock chain(m_oChain); - for(; n >= 8; n -= 8) - { - encrypt(chain); - BytesToBlock(buf, work); - work ^= chain; - chain = work; - BlockToBytes(work, buf+=8); - } - } - else //ECB mode, not using the Chain - { - for(; n >= 8; n -= 8) - { - BytesToBlock(buf, work); - encrypt(work); - BlockToBytes(work, buf+=8); - } - } -} - -//decrypt Buffer in Place -//Returns false if n is multiple of 8 -void blowfish::decrypt(unsigned char* buf, uint64_t n, int iMode) -{ - //Check the buffer's length - should be > 0 and multiple of 8 - if((n==0)||(n%8!=0)) - FC_THROW_EXCEPTION( exception, "invalid buffer length ${n}, not multiple of 8", ("n", n) ); - sblock work; - if(iMode == CBC) //CBC mode, using the Chain - { - sblock crypt, chain(m_oChain); - for(; n >= 8; n -= 8) - { - BytesToBlock(buf, work); - crypt = work; - decrypt(work); - work ^= chain; - chain = crypt; - BlockToBytes(work, buf+=8); - } - } - else if(iMode == CFB) //CFB mode, using the Chain, not using decrypt() - { - sblock crypt, chain(m_oChain); - for(; n >= 8; n -= 8) - { - BytesToBlock(buf, work); - encrypt(chain); - crypt = work; - work ^= chain; - chain = crypt; - BlockToBytes(work, buf+=8); - } - } - else //ECB mode, not using the Chain - { - for(; n >= 8; n -= 8) - { - BytesToBlock(buf, work); - decrypt(work); - BlockToBytes(work, buf+=8); - } - } -} - -//encrypt from Input Buffer to Output Buffer -//Returns false if n is multiple of 8 -void blowfish::encrypt(const unsigned char* in, unsigned char* out, uint64_t n, int iMode) -{ - //Check the buffer's length - should be > 0 and multiple of 8 - if((n==0)||(n%8!=0)) - FC_THROW_EXCEPTION( exception, "invalid buffer length ${n}, not multiple of 8", ("n", n) ); - sblock work; - if(iMode == CBC) //CBC mode, using the Chain - { - sblock chain(m_oChain); - for(; n >= 8; n -= 8, in += 8) - { - BytesToBlock(in, work); - work ^= chain; - encrypt(work); - chain = work; - BlockToBytes(work, out+=8); - } - } - else if(iMode == CFB) //CFB mode, using the Chain - { - sblock chain(m_oChain); - for(; n >= 8; n -= 8, in += 8) - { - encrypt(chain); - BytesToBlock(in, work); - work ^= chain; - chain = work; - BlockToBytes(work, out+=8); - } - } - else //ECB mode, not using the Chain - { - for(; n >= 8; n -= 8, in += 8) - { - BytesToBlock(in, work); - encrypt(work); - BlockToBytes(work, out+=8); - } - } -} - -//decrypt from Input Buffer to Output Buffer -//Returns false if n is multiple of 8 -void blowfish::decrypt(const unsigned char* in, unsigned char* out, uint64_t n, int iMode) -{ - //Check the buffer's length - should be > 0 and multiple of 8 - if((n==0)||(n%8!=0)) - FC_THROW_EXCEPTION( exception, "invalid buffer length ${n}, not multiple of 8", ("n", n) ); - sblock work; - if(iMode == CBC) //CBC mode, using the Chain - { - sblock crypt, chain(m_oChain); - for(; n >= 8; n -= 8, in += 8) - { - BytesToBlock(in, work); - crypt = work; - decrypt(work); - work ^= chain; - chain = crypt; - BlockToBytes(work, out+=8); - } - } - else if(iMode == CFB) //CFB mode, using the Chain, not using decrypt() - { - sblock crypt, chain(m_oChain); - for(; n >= 8; n -= 8, in += 8) - { - BytesToBlock(in, work); - encrypt(chain); - crypt = work; - work ^= chain; - chain = crypt; - BlockToBytes(work, out+=8); - } - } - else //ECB mode, not using the Chain - { - for(; n >= 8; n -= 8, in += 8) - { - BytesToBlock(in, work); - decrypt(work); - BlockToBytes(work, out+=8); - } - } -} - -} // namespace fc diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 95d7242..708b3d5 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -39,7 +39,6 @@ add_executable( all_tests all_tests.cpp crypto/base_n_tests.cpp crypto/bigint_test.cpp crypto/blind.cpp - crypto/blowfish_test.cpp crypto/dh_test.cpp crypto/rand_test.cpp crypto/sha_tests.cpp diff --git a/tests/crypto/blowfish_test.cpp b/tests/crypto/blowfish_test.cpp deleted file mode 100644 index ca55bca..0000000 --- a/tests/crypto/blowfish_test.cpp +++ /dev/null @@ -1,169 +0,0 @@ -#include - -#include -#include - -// Test vectors from https://www.schneier.com/code/vectors.txt - -struct ecb_testdata { - char key[17], plain[17], cipher[17]; -} ecb_tests[] = { - { "0000000000000000", "0000000000000000", "4EF997456198DD78" }, - { "FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF", "51866FD5B85ECB8A" }, - { "3000000000000000", "1000000000000001", "7D856F9A613063F2" }, - { "1111111111111111", "1111111111111111", "2466DD878B963C9D" }, - { "0123456789ABCDEF", "1111111111111111", "61F9C3802281B096" }, - { "1111111111111111", "0123456789ABCDEF", "7D0CC630AFDA1EC7" }, - { "0000000000000000", "0000000000000000", "4EF997456198DD78" }, - { "FEDCBA9876543210", "0123456789ABCDEF", "0ACEAB0FC6A0A28D" }, - { "7CA110454A1A6E57", "01A1D6D039776742", "59C68245EB05282B" }, - { "0131D9619DC1376E", "5CD54CA83DEF57DA", "B1B8CC0B250F09A0" }, - { "07A1133E4A0B2686", "0248D43806F67172", "1730E5778BEA1DA4" }, - { "3849674C2602319E", "51454B582DDF440A", "A25E7856CF2651EB" }, - { "04B915BA43FEB5B6", "42FD443059577FA2", "353882B109CE8F1A" }, - { "0113B970FD34F2CE", "059B5E0851CF143A", "48F4D0884C379918" }, - { "0170F175468FB5E6", "0756D8E0774761D2", "432193B78951FC98" }, - { "43297FAD38E373FE", "762514B829BF486A", "13F04154D69D1AE5" }, - { "07A7137045DA2A16", "3BDD119049372802", "2EEDDA93FFD39C79" }, - { "04689104C2FD3B2F", "26955F6835AF609A", "D887E0393C2DA6E3" }, - { "37D06BB516CB7546", "164D5E404F275232", "5F99D04F5B163969" }, - { "1F08260D1AC2465E", "6B056E18759F5CCA", "4A057A3B24D3977B" }, - { "584023641ABA6176", "004BD6EF09176062", "452031C1E4FADA8E" }, - { "025816164629B007", "480D39006EE762F2", "7555AE39F59B87BD" }, - { "49793EBC79B3258F", "437540C8698F3CFA", "53C55F9CB49FC019" }, - { "4FB05E1515AB73A7", "072D43A077075292", "7A8E7BFA937E89A3" }, - { "49E95D6D4CA229BF", "02FE55778117F12A", "CF9C5D7A4986ADB5" }, - { "018310DC409B26D6", "1D9D5C5018F728C2", "D1ABB290658BC778" }, - { "1C587F1C13924FEF", "305532286D6F295A", "55CB3774D13EF201" }, - { "0101010101010101", "0123456789ABCDEF", "FA34EC4847B268B2" }, - { "1F1F1F1F0E0E0E0E", "0123456789ABCDEF", "A790795108EA3CAE" }, - { "E0FEE0FEF1FEF1FE", "0123456789ABCDEF", "C39E072D9FAC631D" }, - { "0000000000000000", "FFFFFFFFFFFFFFFF", "014933E0CDAFF6E4" }, - { "FFFFFFFFFFFFFFFF", "0000000000000000", "F21E9A77B71C49BC" }, - { "0123456789ABCDEF", "0000000000000000", "245946885754369A" }, - { "FEDCBA9876543210", "FFFFFFFFFFFFFFFF", "6B5C5A9C5D9E0A5A" } -}; - -const std::string key_test_key = "F0E1D2C3B4A5968778695A4B3C2D1E0F0011223344556677"; -const std::string key_test_plain = "FEDCBA9876543210"; -const char key_test_ciphers[][17] = { - "F9AD597C49DB005E", - "E91D21C1D961A6D6", - "E9C2B70A1BC65CF3", - "BE1E639408640F05", - "B39E44481BDB1E6E", - "9457AA83B1928C0D", - "8BB77032F960629D", - "E87A244E2CC85E82", - "15750E7A4F4EC577", - "122BA70B3AB64AE0", - "3A833C9AFFC537F6", - "9409DA87A90F6BF2", - "884F80625060B8B4", - "1F85031C19E11968", - "79D9373A714CA34F", - "93142887EE3BE15C", - "03429E838CE2D14B", - "A4299E27469FF67B", - "AFD5AED1C1BC96A8", - "10851C0E3858DA9F", - "E6F51ED79B9DB21F", - "64A6E14AFD36B46F", - "80C7D7D45A5479AD", - "05044B62FA52D080", -}; - -const std::string chain_test_key = "0123456789ABCDEFF0E1D2C3B4A59687"; -const std::string chain_test_iv = "FEDCBA9876543210"; -const std::string chain_test_cbc = "6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC"; -const std::string chain_test_cfb = "E73214A2822139CAF26ECF6D2EB9E76E3DA3DE04D1517200519D57A6C3"; -const char* chain_test_plain = "7654321 Now is the time for \0\0\0\0"; - -BOOST_AUTO_TEST_SUITE(fc_crypto) - -BOOST_AUTO_TEST_CASE(blowfish_ecb_test) -{ - for ( int i = 0; i < 34; i++ ) { - unsigned char key[8], plain[8], cipher[8], out[8]; - BOOST_CHECK_EQUAL( 8u, fc::from_hex( ecb_tests[i].key, (char*) key, sizeof(key) ) ); - BOOST_CHECK_EQUAL( 8u, fc::from_hex( ecb_tests[i].plain, (char*) plain, sizeof(plain) ) ); - BOOST_CHECK_EQUAL( 8u, fc::from_hex( ecb_tests[i].cipher, (char*) cipher, sizeof(cipher) ) ); - - fc::blowfish fish; - fish.start( key, 8 ); - fish.encrypt( plain, out, 8, fc::blowfish::ECB ); - BOOST_CHECK( !memcmp( cipher, out, 8) ); - fish.decrypt( out, 8, fc::blowfish::ECB ); - BOOST_CHECK( !memcmp( plain, out, 8) ); - fish.decrypt( cipher, out, 8, fc::blowfish::ECB ); - BOOST_CHECK( !memcmp( plain, out, 8) ); - fish.encrypt( out, 8, fc::blowfish::ECB ); - BOOST_CHECK( !memcmp( cipher, out, 8) ); - } -} - -BOOST_AUTO_TEST_CASE(blowfish_key_test) -{ - unsigned char key[24], plain[8], cipher[8], out[8]; - BOOST_CHECK_EQUAL( 24u, fc::from_hex( key_test_key.c_str(), (char*) key, sizeof(key) ) ); - BOOST_CHECK_EQUAL( 8u, fc::from_hex( key_test_plain.c_str(), (char*) plain, sizeof(plain) ) ); - - for ( unsigned int i = 0; i < sizeof(key); i++ ) { - BOOST_CHECK_EQUAL( 8u, fc::from_hex( key_test_ciphers[i], (char*) cipher, sizeof(cipher) ) ); - fc::blowfish fish; - fish.start( key, i + 1 ); - fish.encrypt( plain, out, 8, fc::blowfish::ECB ); - BOOST_CHECK( !memcmp( cipher, out, 8) ); - fish.decrypt( out, 8, fc::blowfish::ECB ); - BOOST_CHECK( !memcmp( plain, out, 8) ); - fish.decrypt( cipher, out, 8, fc::blowfish::ECB ); - BOOST_CHECK( !memcmp( plain, out, 8) ); - fish.encrypt( out, 8, fc::blowfish::ECB ); - BOOST_CHECK( !memcmp( cipher, out, 8) ); - } -} - -static unsigned int from_bytes( const unsigned char* p ) { - return (((unsigned int) p[0]) << 24) - | (((unsigned int) p[1]) << 16) - | (((unsigned int) p[2]) << 8) - | ((unsigned int) p[3]); -} - -BOOST_AUTO_TEST_CASE(blowfish_chain_test) -{ - unsigned char key[16], iv[8], cipher[32], out[32]; - BOOST_CHECK_EQUAL( 16u, fc::from_hex( chain_test_key.c_str(), (char*) key, sizeof(key) ) ); - BOOST_CHECK_EQUAL( 8u, fc::from_hex( chain_test_iv.c_str(), (char*) iv, sizeof(iv) ) ); - - BOOST_CHECK_EQUAL( 32u, fc::from_hex( chain_test_cbc.c_str(), (char*) cipher, sizeof(cipher) ) ); - fc::blowfish fish; - fish.start( key, sizeof(key), fc::sblock( from_bytes( iv ), from_bytes( iv + 4 ) ) ); - fish.encrypt( (unsigned char*) chain_test_plain, out, sizeof(out), fc::blowfish::CBC ); - BOOST_CHECK( !memcmp( cipher, out, sizeof(cipher) ) ); - fish.reset_chain(); - fish.decrypt( out, sizeof(out), fc::blowfish::CBC ); - BOOST_CHECK( !memcmp( chain_test_plain, out, 29 ) ); - fish.reset_chain(); - fish.encrypt( out, sizeof(out), fc::blowfish::CBC ); - BOOST_CHECK( !memcmp( cipher, out, sizeof(cipher) ) ); - fish.reset_chain(); - fish.decrypt( cipher, out, sizeof(cipher), fc::blowfish::CBC ); - BOOST_CHECK( !memcmp( chain_test_plain, out, 29 ) ); - - BOOST_CHECK_EQUAL( 29u, fc::from_hex( chain_test_cfb.c_str(), (char*) cipher, sizeof(cipher) ) ); - fish.reset_chain(); - fish.encrypt( (unsigned char*) chain_test_plain, out, sizeof(out), fc::blowfish::CFB ); - BOOST_CHECK( !memcmp( cipher, out, 29 ) ); - fish.reset_chain(); memset( out + 29, 0, 3 ); - fish.decrypt( out, sizeof(out), fc::blowfish::CFB ); - BOOST_CHECK( !memcmp( chain_test_plain, out, 29 ) ); - fish.reset_chain(); memset( out + 29, 0, 3 ); - fish.encrypt( out, sizeof(out), fc::blowfish::CFB ); - BOOST_CHECK( !memcmp( cipher, out, 29 ) ); - fish.reset_chain(); memset( out + 29, 0, 3 ); - fish.decrypt( cipher, out, sizeof(cipher), fc::blowfish::CFB ); - BOOST_CHECK( !memcmp( chain_test_plain, out, 29 ) ); -} - -BOOST_AUTO_TEST_SUITE_END() From 183a81ba94a1e69cb762ac29c93e152cf87b100b Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Thu, 4 Apr 2019 14:07:43 +0200 Subject: [PATCH 04/11] Remove unused pke --- include/fc/crypto/pke.hpp | 117 ------------ src/crypto/pke.cpp | 365 -------------------------------------- 2 files changed, 482 deletions(-) delete mode 100644 include/fc/crypto/pke.hpp delete mode 100644 src/crypto/pke.cpp diff --git a/include/fc/crypto/pke.hpp b/include/fc/crypto/pke.hpp deleted file mode 100644 index db33b46..0000000 --- a/include/fc/crypto/pke.hpp +++ /dev/null @@ -1,117 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include -#include - -namespace fc { - namespace detail { class pke_impl; } - - class private_key; - class public_key; - void generate_key_pair( public_key&, private_key& ); - - typedef std::vector bytes; - typedef bytes signature; - - class public_key - { - public: - public_key(); - explicit public_key( const bytes& d ); - public_key( const public_key& k ); - public_key( public_key&& k ); - ~public_key(); - - operator bool()const; - - public_key& operator=(const public_key& p ); - public_key& operator=(public_key&& p ); - - bool verify( const sha1& digest, const array& sig )const; - bool verify( const sha1& digest, const signature& sig )const; - bool verify( const sha256& digest, const signature& sig )const; - bytes encrypt( const char* data, size_t len )const; - bytes encrypt( const bytes& )const; - bytes decrypt( const bytes& )const; - - bytes serialize()const; - friend void generate_key_pair( public_key&, private_key& ); - private: - std::shared_ptr my; - }; - - class private_key - { - public: - private_key(); - explicit private_key( const bytes& d ); - private_key( const private_key& k ); - private_key( private_key&& k ); - ~private_key(); - - operator bool()const; - - private_key& operator=(const private_key& p ); - private_key& operator=(private_key&& p ); - - void sign( const sha1& digest, array& sig )const; - signature sign( const sha1& digest )const; - signature sign( const sha256& digest )const; - - bytes decrypt( const char* bytes, size_t len )const; - bytes decrypt( const bytes& )const; - bytes encrypt( const bytes& )const; - - bytes serialize()const; - friend void generate_key_pair( public_key&, private_key& ); - - private: - std::shared_ptr my; - }; - bool operator==( const private_key& a, const private_key& b ); - - namespace raw - { - template - void unpack( Stream& s, fc::public_key& pk, uint32_t _max_depth=FC_PACK_MAX_DEPTH ) - { - FC_ASSERT( _max_depth > 0 ); - bytes ser; - fc::raw::unpack( s, ser, _max_depth - 1 ); - pk = fc::public_key( ser ); - } - - template - void pack( Stream& s, const fc::public_key& pk, uint32_t _max_depth=FC_PACK_MAX_DEPTH ) - { - FC_ASSERT( _max_depth > 0 ); - fc::raw::pack( s, pk.serialize(), _max_depth - 1 ); - } - - template - void unpack( Stream& s, fc::private_key& pk, uint32_t _max_depth=FC_PACK_MAX_DEPTH ) - { - FC_ASSERT( _max_depth > 0 ); - bytes ser; - fc::raw::unpack( s, ser, _max_depth - 1 ); - pk = fc::private_key( ser ); - } - - template - void pack( Stream& s, const fc::private_key& pk, uint32_t _max_depth=FC_PACK_MAX_DEPTH ) - { - FC_ASSERT( _max_depth > 0 ); - fc::raw::pack( s, pk.serialize(), _max_depth - 1 ); - } - } - class variant; - 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/src/crypto/pke.cpp b/src/crypto/pke.cpp deleted file mode 100644 index 87ee25a..0000000 --- a/src/crypto/pke.cpp +++ /dev/null @@ -1,365 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace fc { - - namespace detail { - class pke_impl - { - public: - pke_impl():rsa(nullptr){} - ~pke_impl() - { - if( rsa != nullptr ) - RSA_free(rsa); - } - RSA* rsa; - }; - } // detail - - public_key::operator bool()const { return !!my; } - private_key::operator bool()const { return !!my; } - - public_key::public_key() - {} - - public_key::public_key( const bytes& d ) - :my( std::make_shared() ) - { - string pem = "-----BEGIN RSA PUBLIC KEY-----\n"; - auto b64 = fc::base64_encode( (const unsigned char*)d.data(), d.size() ); - for( size_t i = 0; i < b64.size(); i += 64 ) - pem += b64.substr( i, 64 ) + "\n"; - pem += "-----END RSA PUBLIC KEY-----\n"; - // fc::cerr<rsa = PEM_read_bio_RSAPublicKey(mem, NULL, NULL, NULL ); - BIO_free(mem); - } - public_key::public_key( const public_key& k ) - :my(k.my) - { - } - - public_key::public_key( public_key&& k ) - :my(std::move(k.my)) - { - } - - public_key::~public_key() { } - - public_key& public_key::operator=(const public_key& p ) - { - my = p.my; return *this; - } - public_key& public_key::operator=( public_key&& p ) - { - my = std::move(p.my); return *this; - } - bool public_key::verify( const sha1& digest, const array& sig )const - { - return 0 != RSA_verify( NID_sha1, (const uint8_t*)&digest, 20, - (uint8_t*)&sig, 2048/8, my->rsa ); - } - - bool public_key::verify( const sha1& digest, const signature& sig )const - { - static_assert( sig.size() == 2048/8, "Invalid signature size" ); - return 0 != RSA_verify( NID_sha1, (const uint8_t*)&digest, 20, - (uint8_t*)sig.data(), 2048/8, my->rsa ); - } - bool public_key::verify( const sha256& digest, const signature& sig )const - { - static_assert( sig.size() == 2048/8, "Invalid signature size" ); - return 0 != RSA_verify( NID_sha256, (const uint8_t*)&digest, 32, - (uint8_t*)sig.data(), 2048/8, my->rsa ); - } - bytes public_key::encrypt( const char* b, size_t l )const - { - FC_ASSERT( my && my->rsa ); - bytes out( RSA_size(my->rsa) ); //, char(0) ); - int rtn = RSA_public_encrypt( l, - (unsigned char*)b, - (unsigned char*)out.data(), - my->rsa, RSA_PKCS1_OAEP_PADDING ); - if( rtn >= 0 ) { - out.resize(rtn); - return out; - } - FC_THROW_EXCEPTION( exception, "openssl: ${message}", ("message",std::string(ERR_error_string( ERR_get_error(),NULL))) ); - } - - bytes public_key::encrypt( const bytes& in )const - { - FC_ASSERT( my && my->rsa ); - bytes out( RSA_size(my->rsa) ); //, char(0) ); - int rtn = RSA_public_encrypt( in.size(), - (unsigned char*)in.data(), - (unsigned char*)out.data(), - my->rsa, RSA_PKCS1_OAEP_PADDING ); - fc::cerr<<"rtn: "<= 0 ) { - out.resize(rtn); - return out; - } - FC_THROW_EXCEPTION( exception, "openssl: ${message}", ("message",std::string(ERR_error_string( ERR_get_error(),NULL))) ); - } - bytes public_key::decrypt( const bytes& in )const - { - FC_ASSERT( my && my->rsa ); - bytes out( RSA_size(my->rsa) );//, char(0) ); - int rtn = RSA_public_decrypt( in.size(), - (unsigned char*)in.data(), - (unsigned char*)out.data(), - my->rsa, RSA_PKCS1_OAEP_PADDING ); - if( rtn >= 0 ) { - out.resize(rtn); - return out; - } - FC_THROW_EXCEPTION( exception, "openssl: ${message}", ("message",std::string(ERR_error_string( ERR_get_error(),NULL))) ); - } - - bytes public_key::serialize()const - { - bytes ba; - if( !my ) { return ba; } - - BIO *mem = BIO_new(BIO_s_mem()); - int e = PEM_write_bio_RSAPublicKey( mem, my->rsa ); - if( e != 1 ) - { - BIO_free(mem); - FC_THROW_EXCEPTION( exception, "openssl: ${message}", ("message",std::string(ERR_error_string( ERR_get_error(),NULL))) ); - } - char* dat; - uint32_t l = BIO_get_mem_data( mem, &dat ); - - fc::stringstream ss( string( dat, l ) ); - fc::stringstream key; - std::string tmp; - fc::getline( ss, tmp ); - fc::getline( ss, tmp ); - while( tmp.size() && tmp[0] != '-' ) - { - key << tmp; - fc::getline( ss, tmp ); - } - auto str = key.str(); - str = fc::base64_decode( str ); - ba = bytes( str.begin(), str.end() ); - - BIO_free(mem); - return ba; - } - - private_key::private_key() - { - } - private_key::private_key( const bytes& d ) - :my( std::make_shared() ) - { - - string pem = "-----BEGIN RSA PRIVATE KEY-----\n"; - auto b64 = fc::base64_encode( (const unsigned char*)d.data(), d.size() ); - for( size_t i = 0; i < b64.size(); i += 64 ) - pem += b64.substr( i, 64 ) + "\n"; - pem += "-----END RSA PRIVATE KEY-----\n"; - // fc::cerr<rsa = PEM_read_bio_RSAPrivateKey(mem, NULL, NULL, NULL ); - BIO_free(mem); - - FC_ASSERT( my->rsa, "read private key" ); - } - - private_key::private_key( const private_key& k ) - :my(k.my) - { - } - private_key::private_key( private_key&& k ) - :my(std::move(k.my) ) - { - } - private_key::~private_key() { } - - private_key& private_key::operator=(const private_key& p ) - { - my = p.my; return *this; - } - private_key& private_key::operator=(private_key&& p ) - { - my = std::move(p.my); return *this; - } - - void private_key::sign( const sha1& digest, array& sig )const - { - FC_ASSERT( (size_t(RSA_size(my->rsa)) <= sizeof(sig)), "Invalid RSA size" ); - uint32_t slen = 0; - if( 1 != RSA_sign( NID_sha1, (uint8_t*)&digest, - 20, (unsigned char*)&sig, &slen, my->rsa ) ) - { - FC_THROW_EXCEPTION( exception, "rsa sign failed with ${message}", ("message",std::string(ERR_error_string( ERR_get_error(),NULL))) ); - } - } - - signature private_key::sign( const sha1& digest )const - { - if( !my ) FC_THROW_EXCEPTION( assert_exception, "!null" ); - signature sig; - sig.resize( RSA_size(my->rsa) ); - - uint32_t slen = 0; - if( 1 != RSA_sign( NID_sha1, (uint8_t*)digest.data(), - 20, (unsigned char*)sig.data(), &slen, my->rsa ) ) - { - FC_THROW_EXCEPTION( exception, "rsa sign failed with ${message}", ("message",std::string(ERR_error_string( ERR_get_error(),NULL))) ); - } - return sig; - } - signature private_key::sign( const sha256& digest )const - { - if( !my ) FC_THROW_EXCEPTION( assert_exception, "!null" ); - signature sig; - sig.resize( RSA_size(my->rsa) ); - - uint32_t slen = 0; - if( 1 != RSA_sign( NID_sha256, (uint8_t*)digest.data(), - 32, (unsigned char*)sig.data(), &slen, my->rsa ) ) - { - FC_THROW_EXCEPTION( exception, "rsa sign failed with ${message}", ("message",std::string(ERR_error_string( ERR_get_error(),NULL))) ); - } - return sig; - } - - - bytes private_key::encrypt( const bytes& in )const - { - if( !my ) FC_THROW_EXCEPTION( assert_exception, "!null" ); - bytes out; - out.resize( RSA_size(my->rsa) ); - int rtn = RSA_private_encrypt( in.size(), - (unsigned char*)in.data(), - (unsigned char*)out.data(), - my->rsa, RSA_PKCS1_OAEP_PADDING ); - if( rtn >= 0 ) { - out.resize(rtn); - return out; - } - - FC_THROW_EXCEPTION( exception, "encrypt failed" ); - } - - bytes private_key::decrypt( const char* in, size_t l )const - { - if( !my ) FC_THROW_EXCEPTION( assert_exception, "!null" ); - bytes out; - out.resize( RSA_size(my->rsa) ); - int rtn = RSA_private_decrypt( l, - (unsigned char*)in, - (unsigned char*)out.data(), - my->rsa, RSA_PKCS1_OAEP_PADDING ); - if( rtn >= 0 ) { - out.resize(rtn); - return out; - } - FC_THROW_EXCEPTION( exception, "decrypt failed" ); - } - bytes private_key::decrypt( const bytes& in )const - { - if( !my ) FC_THROW_EXCEPTION( assert_exception, "!null" ); - bytes out; - out.resize( RSA_size(my->rsa) ); - int rtn = RSA_private_decrypt( in.size(), - (unsigned char*)in.data(), - (unsigned char*)out.data(), - my->rsa, RSA_PKCS1_OAEP_PADDING ); - if( rtn >= 0 ) { - out.resize(rtn); - return out; - } - FC_THROW_EXCEPTION( exception, "decrypt failed" ); - } - - bytes private_key::serialize()const - { - bytes ba; - if( !my ) { return ba; } - - BIO *mem = BIO_new(BIO_s_mem()); - int e = PEM_write_bio_RSAPrivateKey( mem, my->rsa, NULL, NULL, 0, NULL, NULL ); - if( e != 1 ) - { - BIO_free(mem); - FC_THROW_EXCEPTION( exception, "Error writing private key, ${message}", ("message",std::string(ERR_error_string( ERR_get_error(),NULL))) ); - } - char* dat; - uint32_t l = BIO_get_mem_data( mem, &dat ); - // return bytes( dat, dat + l ); - - stringstream ss( string( dat, l ) ); - stringstream key; - string tmp; - fc::getline( ss, tmp ); - fc::getline( ss, tmp ); - - while( tmp.size() && tmp[0] != '-' ) - { - key << tmp; - fc::getline( ss, tmp ); - } - auto str = key.str(); - str = fc::base64_decode( str ); - ba = bytes( str.begin(), str.end() ); - // ba = bytes( dat, dat + l ); - BIO_free(mem); - return ba; - } - - void generate_key_pair( public_key& pub, private_key& priv ) - { - static bool init = true; - if( init ) { ERR_load_crypto_strings(); init = false; } - - pub.my = std::make_shared(); - priv.my = pub.my; - pub.my->rsa = RSA_generate_key( 2048, 65537, NULL, NULL ); - } - - /** encodes the big int as base64 string, or a number */ - 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, uint32_t max_depth ) - { - 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, 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, uint32_t max_depth ) - { - bi = private_key( v.as >(max_depth) ); - } - -} // fc From d5b817817a505f1e240d0b879b4eb2c0baf2e090 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Thu, 4 Apr 2019 14:14:40 +0200 Subject: [PATCH 05/11] Remove unused bstate --- CMakeLists.txt | 1 - include/fc/rpc/bstate.hpp | 58 --------------------------------- src/rpc/bstate.cpp | 68 --------------------------------------- 3 files changed, 127 deletions(-) delete mode 100644 include/fc/rpc/bstate.hpp delete mode 100644 src/rpc/bstate.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 19f8bae..1dbd63e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -240,7 +240,6 @@ set( fc_sources src/rpc/http_api.cpp src/rpc/json_connection.cpp src/rpc/state.cpp - src/rpc/bstate.cpp src/rpc/websocket_api.cpp src/log/log_message.cpp src/log/logger.cpp diff --git a/include/fc/rpc/bstate.hpp b/include/fc/rpc/bstate.hpp deleted file mode 100644 index 8b398c4..0000000 --- a/include/fc/rpc/bstate.hpp +++ /dev/null @@ -1,58 +0,0 @@ -#pragma once -#include -#include -#include -#include - -namespace fc { namespace rpc { - typedef std::vector params_type; - typedef std::vector result_type; - - struct brequest - { - optional id; - std::string method; - params_type params; - }; - - struct bresponse - { - bresponse(){} - bresponse( int64_t i, result_type r ):id(i),result(r){} - bresponse( int64_t i, error_object r ):id(i),error(r){} - int64_t id = 0; - optional result; - optional error; - }; - - /** binary RPC state */ - class bstate - { - public: - typedef std::function method; - ~bstate(); - - void add_method( const std::string& name, method m ); - void remove_method( const std::string& name ); - - result_type local_call( const string& method_name, const params_type& args ); - void handle_reply( const bresponse& response ); - - brequest start_remote_call( const string& method_name, params_type args ); - result_type wait_for_response( uint64_t request_id ); - - void close(); - - void on_unhandled( const std::function& unhandled ); - - private: - uint64_t _next_id = 1; - std::unordered_map::ptr> _awaiting; - std::unordered_map _methods; - std::function _unhandled; - }; -} } // namespace fc::rpc - -FC_REFLECT( fc::rpc::brequest, (id)(method)(params) ); -FC_REFLECT( fc::rpc::bresponse, (id)(result)(error) ) - diff --git a/src/rpc/bstate.cpp b/src/rpc/bstate.cpp deleted file mode 100644 index b6b39fd..0000000 --- a/src/rpc/bstate.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#include -#include -#include - -namespace fc { namespace rpc { -bstate::~bstate() -{ - close(); -} - -void bstate::add_method( const std::string& name, method m ) -{ - _methods.emplace(std::pair(name,std::move(m))); -} - -void bstate::remove_method( const std::string& name ) -{ - _methods.erase(name); -} - -result_type bstate::local_call( const string& method_name, const params_type& args ) -{ - auto method_itr = _methods.find(method_name); - if( method_itr == _methods.end() && _unhandled ) - return _unhandled( method_name, args ); - FC_ASSERT( method_itr != _methods.end(), "Unknown Method: ${name}", ("name",method_name) ); - return method_itr->second(args); -} - -void bstate::handle_reply( const bresponse& bresponse ) -{ - auto await = _awaiting.find( bresponse.id ); - FC_ASSERT( await != _awaiting.end(), "Unknown Response ID: ${id}", ("id",bresponse.id)("bresponse",bresponse) ); - if( bresponse.result ) - await->second->set_value( *bresponse.result ); - else if( bresponse.error ) - { - await->second->set_exception( exception_ptr(new FC_EXCEPTION( exception, "${error}", ("error",bresponse.error->message)("data",bresponse) ) ) ); - } - else - await->second->set_value( params_type() ); - _awaiting.erase(await); -} - -brequest bstate::start_remote_call( const string& method_name, params_type args ) -{ - brequest brequest{ _next_id++, method_name, std::move(args) }; - _awaiting[*brequest.id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); - return brequest; -} -result_type bstate::wait_for_response( uint64_t request_id ) -{ - auto itr = _awaiting.find(request_id); - FC_ASSERT( itr != _awaiting.end() ); - return fc::future( itr->second ).wait(); -} -void bstate::close() -{ - for( auto item : _awaiting ) - item.second->set_exception( fc::exception_ptr(new FC_EXCEPTION( eof_exception, "connection closed" )) ); - _awaiting.clear(); -} -void bstate::on_unhandled( const std::function& unhandled ) -{ - _unhandled = unhandled; -} - -} } // namespace fc::rpc From 406f1bdd615f446f4e51bcd1854a22b22ff7c867 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Thu, 4 Apr 2019 14:15:33 +0200 Subject: [PATCH 06/11] Remove unused json_connection --- CMakeLists.txt | 1 - include/fc/rpc/json_connection.hpp | 318 ------------- src/rpc/json_connection.cpp | 717 ----------------------------- 3 files changed, 1036 deletions(-) delete mode 100644 include/fc/rpc/json_connection.hpp delete mode 100644 src/rpc/json_connection.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1dbd63e..ca61666 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -238,7 +238,6 @@ set( fc_sources src/interprocess/file_mapping.cpp src/rpc/cli.cpp src/rpc/http_api.cpp - src/rpc/json_connection.cpp src/rpc/state.cpp src/rpc/websocket_api.cpp src/log/log_message.cpp diff --git a/include/fc/rpc/json_connection.hpp b/include/fc/rpc/json_connection.hpp deleted file mode 100644 index 0208173..0000000 --- a/include/fc/rpc/json_connection.hpp +++ /dev/null @@ -1,318 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include - -namespace fc { namespace rpc { - - namespace detail { class json_connection_impl; } - - /** - * @brief Implements JSON-RPC 2.0 over a set of io streams - * - * Each JSON RPC message is expected to be on its own line, violators - * will be prosecuted to the fullest extent of the law. - */ - class json_connection - { - public: - typedef std::function method; - typedef std::function named_param_method; - - json_connection( fc::buffered_istream_ptr in, fc::buffered_ostream_ptr out, uint32_t max_depth ); - ~json_connection(); - - /** - * Starts processing messages from input - */ - future exec(); - - bool is_open(); - void close(); - - void set_on_disconnected_callback(std::function callback); - - logger get_logger()const; - void set_logger( const logger& l ); - - /** - * @name server interface - * - * Adding methods to the interface allows the remote side - * to call them. - */ - ///@{ - void add_method( const std::string& name, method ); - void add_named_param_method( const std::string& name, named_param_method ); - void remove_method( const std::string& name ); - //@} - - /** - * @name client interface - */ - ///@{ - void notice( const std::string& method ); - void notice( const std::string& method, const variants& args ); - void notice( const std::string& method, const variant_object& named_args ); - - /// args will be handled as named params - future async_call( const std::string& method, - const variant_object& args ); - - future async_call( const std::string& method, mutable_variant_object args ); - - /// Sending in an array of variants will be handled as positional arguments - future async_call( const std::string& method, - const variants& args ); - - future async_call( const std::string& method ); - - future async_call( const std::string& method, - const variant& a1 ); - - future async_call( const std::string& method, - const variant& a1, - const variant& a2 ); - - future async_call( const std::string& method, - const variant& a1, - const variant& a2, - const variant& a3 ); - - future async_call( const std::string& method, - const variant& a1, - const variant& a2, - const variant& a3, - const variant& a4 ); - - future async_call( const std::string& method, - const variant& a1, - const variant& a2, - const variant& a3, - const variant& a4, - const variant& a5 ); - - future async_call( const std::string& method, - const variant& a1, - const variant& a2, - const variant& a3, - const variant& a4, - const variant& a5, - const variant& a6 ); - - future async_call( const std::string& method, - const variant& a1, - const variant& a2, - const variant& a3, - const variant& a4, - const variant& a5, - const variant& a6, - const variant& a7 - ); - - future async_call( const std::string& method, - const variant& a1, - const variant& a2, - const variant& a3, - const variant& a4, - const variant& a5, - const variant& a6, - const variant& a7, - const variant& a8 - ); - future async_call( const std::string& method, - const variant& a1, - const variant& a2, - const variant& a3, - const variant& a4, - const variant& a5, - const variant& a6, - const variant& a7, - const variant& a8, - const variant& a9 - ); - future async_call( const std::string& method, - const variant& a1, - const variant& a2, - const variant& a3, - const variant& a4, - const variant& a5, - const variant& a6, - const variant& a7, - const variant& a8, - const variant& a9, - const variant& a10 - ); - - template - Result call( const std::string& method, - const variants& args, - microseconds timeout = microseconds::maximum()) - { - return async_call( method, args ).wait(timeout).as(); - } - - template - Result call( const std::string& method, - const variant& a1, - const variant& a2, - const variant& a3, - microseconds timeout = microseconds::maximum()) - { - return async_call( method, a1, a2, a3 ).wait(timeout).as(); - } - - template - Result call( const std::string& method, - const variant& a1, - const variant& a2, - const variant& a3, - const variant& a4, - microseconds timeout = microseconds::maximum()) - { - return async_call( method, a1, a2, a3, a4).wait(timeout).as(); - } - - template - Result call( const std::string& method, - const variant& a1, - const variant& a2, - const variant& a3, - const variant& a4, - const variant& a5, - microseconds timeout = microseconds::maximum()) - { - return async_call( method, a1, a2, a3, a4, a5).wait(timeout).as(); - } - - template - Result call( const std::string& method, - const variant& a1, - const variant& a2, - const variant& a3, - const variant& a4, - const variant& a5, - const variant& a6, - microseconds timeout = microseconds::maximum()) - { - return async_call( method, a1, a2, a3, a4, a5, a6).wait(timeout).as(); - } - template - Result call( const std::string& method, - const variant& a1, - const variant& a2, - const variant& a3, - const variant& a4, - const variant& a5, - const variant& a6, - const variant& a7, - microseconds timeout = microseconds::maximum()) - { - return async_call( method, a1, a2, a3, a4, a5, a6, a7).wait(timeout).as(); - } - - template - Result call( const std::string& method, - const variant& a1, - const variant& a2, - const variant& a3, - const variant& a4, - const variant& a5, - const variant& a6, - const variant& a7, - const variant& a8, - microseconds timeout = microseconds::maximum()) - { - return async_call( method, a1, a2, a3, a4, a5, a6, a7, a8).wait(timeout).as(); - } - - template - Result call( const std::string& method, - const variant& a1, - const variant& a2, - const variant& a3, - const variant& a4, - const variant& a5, - const variant& a6, - const variant& a7, - const variant& a8, - const variant& a9, - microseconds timeout = microseconds::maximum()) - { - return async_call( method, a1, a2, a3, a4, a5, a6, a7, a8, a9).wait(timeout).as(); - } - - template - Result call( const std::string& method, - const variant& a1, - const variant& a2, - const variant& a3, - const variant& a4, - const variant& a5, - const variant& a6, - const variant& a7, - const variant& a8, - const variant& a9, - const variant& a10, - microseconds timeout = microseconds::maximum()) - { - return async_call( method, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10).wait(timeout).as(); - } - - template - Result call( const std::string& method, - const variant& a1, - const variant& a2, - microseconds timeout = microseconds::maximum()) - { - return async_call( method, a1, a2 ).wait(timeout).as(); - } - - template - Result call( const std::string& method, - const variant& a1, - microseconds timeout = microseconds::maximum()) - { - return async_call( method, a1 ).wait(timeout).as(); - } - - template - Result call( const std::string& method, - variant_object a1, - microseconds timeout = microseconds::maximum()) - { - return async_call( method, std::move(a1) ).wait(timeout).as(); - } - template - Result call( const std::string& method, - mutable_variant_object a1, - microseconds timeout = microseconds::maximum()) - { - return async_call( method, variant_object( std::move(a1) ) ).wait(timeout).as(); - } - - - template - Result call( const std::string& method, microseconds timeout = microseconds::maximum() ) - { - return async_call( method ).wait(timeout).as(); - } - - /// Sending in a variant_object will be issued as named parameters - variant call( const std::string& method, const variant_object& named_args ); - ///@} - - protected: - const uint32_t _max_conversion_depth; // for nested structures, json, variant etc. - - private: - std::unique_ptr my; - }; - typedef std::shared_ptr json_connection_ptr; - -}} // fc::rpc - - - diff --git a/src/rpc/json_connection.cpp b/src/rpc/json_connection.cpp deleted file mode 100644 index 8eea0df..0000000 --- a/src/rpc/json_connection.cpp +++ /dev/null @@ -1,717 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -namespace fc { namespace rpc { - - namespace detail - { - class json_connection_impl - { - public: - json_connection_impl( fc::buffered_istream_ptr&& in, fc::buffered_ostream_ptr&& out, uint32_t max_depth ) - :_in(std::move(in)),_out(std::move(out)),_eof(false),_next_id(0),_logger("json_connection"),_max_depth(max_depth){} - - fc::buffered_istream_ptr _in; - fc::buffered_ostream_ptr _out; - - fc::future _done; - fc::future _handle_message_future; - bool _eof; - - uint64_t _next_id; - boost::unordered_map::ptr> _awaiting; - boost::unordered_map _methods; - boost::unordered_map _named_param_methods; - - fc::mutex _write_mutex; - std::function _on_close; - - logger _logger; - uint32_t _max_depth; - - void send_result( variant id, variant result ) - { - ilog( "send: {\"id\": ${i}, \"result\": ${r}}", ("i",id)("r",result) ); - { - fc::scoped_lock lock(_write_mutex); - *_out << "{\"id\":"; - json::to_stream( *_out, id, json::stringify_large_ints_and_doubles, _max_depth ); - *_out << ",\"result\":"; - json::to_stream( *_out, result, json::stringify_large_ints_and_doubles, _max_depth ); - *_out << "}\n"; - _out->flush(); - } - } - void send_error( variant id, fc::exception& e ) - { - ilog( "send: {\"id\": ${i}, \"error\":{\"message\": ${what},\"code\":0,\"data\":${data}}}", - ("i",id)("what",e.what())("data", e) ); - { - fc::scoped_lock lock(_write_mutex); - *_out << "{\"id\":"; - json::to_stream( *_out, id, json::stringify_large_ints_and_doubles, _max_depth ); - *_out << ",\"error\":{\"message\":"; - json::to_stream( *_out, std::string(e.what()) ); - *_out <<",\"code\":0,\"data\":"; - json::to_stream( *_out, variant(e, _max_depth), json::stringify_large_ints_and_doubles, _max_depth ); - *_out << "}}\n"; - _out->flush(); - } - //wlog( "exception: ${except}", ("except", variant(e)) ); - } - - void handle_message( const variant_object& obj ) - { - wlog( "recv: ${msg}", ("msg", obj) ); - fc::exception_ptr eptr; - try - { - auto m = obj.find("method"); - auto i = obj.find("id"); - if( m != obj.end() ) - { - fc::exception except; - bool exception_caught = false; - try - { - auto p = obj.find("params"); - variant result; - if( p == obj.end() ) - { - auto pmi = _methods.find(m->value().as_string()); - auto nmi = _named_param_methods.find(m->value().as_string()); - if( pmi != _methods.end() ) - { - result = pmi->second( variants() ); - } - else if( nmi != _named_param_methods.end() ) - { - result = nmi->second( variant_object() ); - } - else // invalid method - { - FC_THROW_EXCEPTION( exception, "Invalid Method '${method}'", ("method",m->value().as_string())); - } - } - else if( p->value().is_array() ) - { - auto pmi = _methods.find(m->value().as_string()); - if( pmi != _methods.end() ) - { - result = pmi->second( p->value().get_array() ); - } - else // invalid method / param combo - { - FC_THROW_EXCEPTION( exception, "Invalid method or params '${method}'", - ("method",m->value().as_string())); - } - - } - else if( p->value().is_object() ) - { - auto nmi = _named_param_methods.find(m->value().as_string()); - if( nmi != _named_param_methods.end() ) - { - result = nmi->second( p->value().get_object() ); - } - else // invalid method / param combo? - { - FC_THROW_EXCEPTION( exception, "Invalid method or params '${method}'", - ("method",m->value().as_string())); - } - } - else // invalid params - { - FC_THROW_EXCEPTION( exception, "Invalid Params for method ${method}", - ("method",m->value().as_string())); - } - if( i != obj.end() ) - { - send_result( i->value(), result ); - } - } - catch ( fc::exception& e ) - { - exception_caught = true; - except = e; - } - if( exception_caught && i != obj.end() ) - send_error( i->value(), except ); - else - fc_wlog( _logger, "json rpc exception: ${exception}", ("exception",except) ); - } - else if( i != obj.end() ) //handle any received JSON response - { - uint64_t id = i->value().as_int64(); - auto await = _awaiting.find(id); - if( await != _awaiting.end() ) - { - auto r = obj.find("result"); - auto e = obj.find("error"); - if( r != obj.end() ) //if regular result response - { - await->second->set_value( r->value() ); - } - else if( e != obj.end() ) //if error response - { - fc::exception_ptr eptr; - try - { - auto err = e->value().get_object(); - auto data = err.find( "data" ); - if( data != err.end() ) - await->second->set_exception( data->value().as(_max_depth).dynamic_copy_exception() ); - else - await->second->set_exception( exception_ptr(new FC_EXCEPTION( exception, "${error}", ("error",e->value()) ) ) ); - } - catch ( fc::exception& e ) - { - elog( "Error parsing exception: ${e}", ("e", e.to_detail_string() ) ); - eptr = e.dynamic_copy_exception(); - } - if( eptr ) await->second->set_exception( eptr ); - } - else // id found without error, result, nor method field - { - fc_wlog( _logger, "no error or result specified in '${message}'", ("message",obj) ); - } - } - } - else // no method nor request id... invalid message - { - - } - } - catch ( fc::exception& e ) // catch all other errors... - { - fc_elog( _logger, "json rpc exception: ${exception}", ("exception",e )); - elog( "json rpc exception: ${exception}", ("exception",e )); - eptr = e.dynamic_copy_exception(); - } - if( eptr ) { close(eptr); } - } - - void read_loop() - { - fc::exception_ptr eptr; - try - { - std::string line; - while( !_done.canceled() ) - { - variant v = json::from_stream( *_in, json::legacy_parser, _max_depth ); - ///ilog( "input: ${in}", ("in", v ) ); - //wlog( "recv: ${line}", ("line", line) ); - _handle_message_future = fc::async([=](){ handle_message(v.get_object()); }, "json_connection handle_message"); - } - } - catch ( eof_exception& eof ) - { - _eof = true; - eptr = eof.dynamic_copy_exception(); - } - catch ( exception& e ) - { - eptr = e.dynamic_copy_exception(); - } - catch ( ... ) - { - eptr = fc::exception_ptr(new FC_EXCEPTION( unhandled_exception, "json connection read error" )); - } - if( eptr ) close( eptr ); - } - - void close( fc::exception_ptr e ) - { - wlog( "close ${reason}", ("reason", e->to_detail_string() ) ); - if( _on_close ) - _on_close(e); - for( auto itr = _awaiting.begin(); itr != _awaiting.end(); ++itr ) - { - itr->second->set_exception( e->dynamic_copy_exception() ); - } - } - }; - }//namespace detail - - json_connection::json_connection( fc::buffered_istream_ptr in, fc::buffered_ostream_ptr out, uint32_t max_depth ) - :_max_conversion_depth(max_depth),my( new detail::json_connection_impl(std::move(in),std::move(out),max_depth) ) - {} - - json_connection::~json_connection() - { - close(); - } - - fc::future json_connection::exec() - { - if( my->_done.valid() ) - { - FC_THROW_EXCEPTION( assert_exception, "start should only be called once" ); - } - return my->_done = fc::async( [=](){ my->read_loop(); }, "json_connection read_loop" ); - } - - void json_connection::close() - { - try - { - if( my->_handle_message_future.valid() && !my->_handle_message_future.ready() ) - my->_handle_message_future.cancel_and_wait(__FUNCTION__); - if( my->_done.valid() && !my->_done.ready() ) - { - my->_done.cancel("json_connection is destructing"); - my->_out->close(); - my->_done.wait(); - } - } - catch ( fc::canceled_exception& ){} // expected exception - catch ( fc::eof_exception& ){} // expected exception - catch ( fc::exception& e ) - { - // unhandled, unexpected exception cannot throw from destructor, so log it. - wlog( "${exception}", ("exception",e.to_detail_string()) ); - } - } - - void json_connection::set_on_disconnected_callback(std::function callback) - { - my->_on_close = callback; - } - - void json_connection::add_method( const std::string& name, method m ) - { - ilog( "add method ${name}", ("name",name) ); - my->_methods.emplace(std::pair(name,std::move(m))); - } - void json_connection::add_named_param_method( const std::string& name, named_param_method m ) - { - ilog( "add named param method ${name}", ("name",name) ); - my->_named_param_methods.emplace(std::pair(name,std::move(m))); - } - void json_connection::remove_method( const std::string& name ) - { - my->_methods.erase(name); - my->_named_param_methods.erase(name); - } - void json_connection::notice( const std::string& method, const variants& args ) - { - fc::scoped_lock lock(my->_write_mutex); - *my->_out << "{\"method\":"; - json::to_stream( *my->_out, method ); - if( args.size() ) - { - *my->_out << ",\"params\":"; - fc::json::to_stream( *my->_out, args ); - *my->_out << "}\n"; - } - else - { - *my->_out << ",\"params\":[]}\n"; - } - } - void json_connection::notice( const std::string& method, const variant_object& named_args ) - { - { - fc::scoped_lock lock(my->_write_mutex); - *my->_out << "{\"method\":"; - json::to_stream( *my->_out, method ); - *my->_out << ",\"params\":"; - fc::json::to_stream( *my->_out, named_args ); - *my->_out << "}\n"; - my->_out->flush(); - } - } - void json_connection::notice( const std::string& method ) - { - { - fc::scoped_lock lock(my->_write_mutex); - *my->_out << "{\"method\":"; - json::to_stream( *my->_out, method ); - *my->_out << "}\n"; - my->_out->flush(); - } - } - - - future json_connection::async_call( const std::string& method, const variants& args ) - { - auto id = my->_next_id++; - my->_awaiting[id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); - - { - fc::scoped_lock lock(my->_write_mutex); - *my->_out << "{\"id\":"; - *my->_out << id; - *my->_out << ",\"method\":"; - json::to_stream( *my->_out, method ); - if( args.size() ) - { - *my->_out << ",\"params\":"; - fc::json::to_stream( *my->_out, args ); - *my->_out << "}\n"; - } - else - { - *my->_out << ",\"params\":[]}\n"; - } - my->_out->flush(); - } - return my->_awaiting[id]; - } - - future json_connection::async_call( const std::string& method, const variant& a1 ) - { - auto id = my->_next_id++; - my->_awaiting[id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); - - { - fc::scoped_lock lock(my->_write_mutex); - *my->_out << "{\"id\":"; - *my->_out << id; - *my->_out << ",\"method\":"; - json::to_stream( *my->_out, method ); - *my->_out << ",\"params\":["; - fc::json::to_stream( *my->_out, a1 ); - *my->_out << "]}\n"; - my->_out->flush(); - } - return my->_awaiting[id]; - } - future json_connection::async_call( const std::string& method, const variant& a1, const variant& a2 ) - { - auto id = my->_next_id++; - my->_awaiting[id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); - - { - fc::scoped_lock lock(my->_write_mutex); - *my->_out << "{\"id\":"; - *my->_out << id; - *my->_out << ",\"method\":"; - json::to_stream( *my->_out, method ); - *my->_out << ",\"params\":["; - fc::json::to_stream( *my->_out, a1 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a2 ); - *my->_out << "]}\n"; - my->_out->flush(); - } - return my->_awaiting[id]; - } - future json_connection::async_call( const std::string& method, const variant& a1, const variant& a2, const variant& a3 ) - { - auto id = my->_next_id++; - my->_awaiting[id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); - - { - fc::scoped_lock lock(my->_write_mutex); - *my->_out << "{\"id\":"; - *my->_out << id; - *my->_out << ",\"method\":"; - json::to_stream( *my->_out, method ); - *my->_out << ",\"params\":["; - fc::json::to_stream( *my->_out, a1 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a2 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a3 ); - *my->_out << "]}\n"; - my->_out->flush(); - } - return my->_awaiting[id]; - } - - future json_connection::async_call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, const variant& a4 ) - { - auto id = my->_next_id++; - my->_awaiting[id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); - - { - fc::scoped_lock lock(my->_write_mutex); - *my->_out << "{\"id\":"; - *my->_out << id; - *my->_out << ",\"method\":"; - json::to_stream( *my->_out, method ); - *my->_out << ",\"params\":["; - fc::json::to_stream( *my->_out, a1 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a2 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a3 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a4 ); - *my->_out << "]}\n"; - my->_out->flush(); - } - return my->_awaiting[id]; - } - - future json_connection::async_call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, const variant& a4, const variant& a5 ) - { - auto id = my->_next_id++; - my->_awaiting[id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); - - { - fc::scoped_lock lock(my->_write_mutex); - *my->_out << "{\"id\":"; - *my->_out << id; - *my->_out << ",\"method\":"; - json::to_stream( *my->_out, method ); - *my->_out << ",\"params\":["; - fc::json::to_stream( *my->_out, a1 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a2 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a3 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a4 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a5 ); - *my->_out << "]}\n"; - my->_out->flush(); - } - return my->_awaiting[id]; - } - - future json_connection::async_call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, const variant& a4, const variant& a5, const variant& a6 ) - { - auto id = my->_next_id++; - my->_awaiting[id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); - - { - fc::scoped_lock lock(my->_write_mutex); - *my->_out << "{\"id\":"; - *my->_out << id; - *my->_out << ",\"method\":"; - json::to_stream( *my->_out, method ); - *my->_out << ",\"params\":["; - fc::json::to_stream( *my->_out, a1 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a2 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a3 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a4 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a5 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a6 ); - *my->_out << "]}\n"; - my->_out->flush(); - } - return my->_awaiting[id]; - } - future json_connection::async_call( const std::string& method, const variant& a1, const variant& a2, const variant& a3, const variant& a4, const variant& a5, const variant& a6, const variant& a7 ) - { - auto id = my->_next_id++; - my->_awaiting[id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); - - { - fc::scoped_lock lock(my->_write_mutex); - *my->_out << "{\"id\":"; - *my->_out << id; - *my->_out << ",\"method\":"; - json::to_stream( *my->_out, method ); - *my->_out << ",\"params\":["; - fc::json::to_stream( *my->_out, a1 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a2 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a3 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a4 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a5 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a6 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a7 ); - *my->_out << "]}\n"; - my->_out->flush(); - } - return my->_awaiting[id]; - } - future json_connection::async_call( const std::string& method, - const variant& a1, - const variant& a2, - const variant& a3, - const variant& a4, - const variant& a5, - const variant& a6, - const variant& a7, - const variant& a8 ) - { - auto id = my->_next_id++; - my->_awaiting[id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); - - { - fc::scoped_lock lock(my->_write_mutex); - *my->_out << "{\"id\":"; - *my->_out << id; - *my->_out << ",\"method\":"; - json::to_stream( *my->_out, method ); - *my->_out << ",\"params\":["; - fc::json::to_stream( *my->_out, a1 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a2 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a3 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a4 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a5 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a6 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a7 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a8 ); - *my->_out << "]}\n"; - my->_out->flush(); - } - return my->_awaiting[id]; - } - future json_connection::async_call( const std::string& method, - const variant& a1, - const variant& a2, - const variant& a3, - const variant& a4, - const variant& a5, - const variant& a6, - const variant& a7, - const variant& a8, - const variant& a9 ) - { - auto id = my->_next_id++; - my->_awaiting[id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); - - { - fc::scoped_lock lock(my->_write_mutex); - *my->_out << "{\"id\":"; - *my->_out << id; - *my->_out << ",\"method\":"; - json::to_stream( *my->_out, method ); - *my->_out << ",\"params\":["; - fc::json::to_stream( *my->_out, a1 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a2 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a3 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a4 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a5 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a6 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a7 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a8 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a9 ); - *my->_out << "]}\n"; - my->_out->flush(); - } - return my->_awaiting[id]; - } - future json_connection::async_call( const std::string& method, - const variant& a1, - const variant& a2, - const variant& a3, - const variant& a4, - const variant& a5, - const variant& a6, - const variant& a7, - const variant& a8, - const variant& a9, - const variant& a10 ) - { - auto id = my->_next_id++; - my->_awaiting[id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); - - { - fc::scoped_lock lock(my->_write_mutex); - *my->_out << "{\"id\":"; - *my->_out << id; - *my->_out << ",\"method\":"; - json::to_stream( *my->_out, method ); - *my->_out << ",\"params\":["; - fc::json::to_stream( *my->_out, a1 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a2 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a3 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a4 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a5 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a6 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a7 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a8 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a9 ); - *my->_out << ","; - fc::json::to_stream( *my->_out, a10 ); - *my->_out << "]}\n"; - my->_out->flush(); - } - return my->_awaiting[id]; - } - - future json_connection::async_call( const std::string& method, mutable_variant_object named_args ) - { - return async_call( method, variant_object( std::move(named_args) ) ); - } - future json_connection::async_call( const std::string& method, const variant_object& named_args ) - { - auto id = my->_next_id++; - my->_awaiting[id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); - fc::scoped_lock lock(my->_write_mutex); - { - *my->_out << "{\"id\":"; - *my->_out << id; - *my->_out << ",\"method\":"; - json::to_stream( *my->_out, method ); - *my->_out << ",\"params\":"; - fc::json::to_stream( *my->_out, named_args ); - *my->_out << "}\n"; - my->_out->flush(); - } - return my->_awaiting[id]; - } - future json_connection::async_call( const std::string& method ) - { - auto id = my->_next_id++; - my->_awaiting[id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); - fc::scoped_lock lock(my->_write_mutex); - { - *my->_out << "{\"id\":"; - *my->_out << id; - *my->_out << ",\"method\":"; - json::to_stream( *my->_out, method ); - *my->_out << "}\n"; - my->_out->flush(); - } - return my->_awaiting[id]; - } - - logger json_connection::get_logger()const - { - return my->_logger; - } - - void json_connection::set_logger( const logger& l ) - { - my->_logger = l; - } - -}} From a063b8d41be10febf45d43feb8f9793051afbdfb Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Thu, 4 Apr 2019 14:37:17 +0200 Subject: [PATCH 07/11] Removed openssl ECC implementation --- CMakeLists.txt | 18 +- src/crypto/elliptic_impl_pub.cpp | 358 ------------------------------- src/crypto/elliptic_mixed.cpp | 40 ---- src/crypto/elliptic_openssl.cpp | 261 ---------------------- 4 files changed, 3 insertions(+), 674 deletions(-) delete mode 100644 src/crypto/elliptic_impl_pub.cpp delete mode 100644 src/crypto/elliptic_mixed.cpp delete mode 100644 src/crypto/elliptic_openssl.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ca61666..1b62e3b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,6 @@ SET( DEFAULT_LIBRARY_INSTALL_DIR lib/ ) SET( DEFAULT_EXECUTABLE_INSTALL_DIR bin/ ) SET( CMAKE_DEBUG_POSTFIX _debug ) SET( BUILD_SHARED_LIBS NO ) -SET( ECC_IMPL secp256k1 CACHE STRING "secp256k1 or openssl or mixed" ) set(platformBitness 32) if(CMAKE_SIZEOF_VOID_P EQUAL 8) @@ -37,17 +36,6 @@ SET(BOOST_COMPONENTS) LIST(APPEND BOOST_COMPONENTS thread date_time filesystem system program_options chrono unit_test_framework context iostreams regex) SET( Boost_USE_STATIC_LIBS ON CACHE STRING "ON or OFF" ) -IF( ECC_IMPL STREQUAL openssl ) - SET( ECC_REST src/crypto/elliptic_impl_pub.cpp ) -ELSE( ECC_IMPL STREQUAL openssl ) - SET( ECC_LIB secp256k1 ) - IF( ECC_IMPL STREQUAL mixed ) - SET( ECC_REST src/crypto/elliptic_impl_priv.cpp src/crypto/elliptic_impl_pub.cpp ) - ELSE( ECC_IMPL STREQUAL mixed ) - SET( ECC_REST src/crypto/elliptic_impl_priv.cpp ) - ENDIF( ECC_IMPL STREQUAL mixed ) -ENDIF( ECC_IMPL STREQUAL openssl ) - # Configure secp256k1-zkp if ( MSVC ) # autoconf won't work here, hard code the defines @@ -263,8 +251,8 @@ set( fc_sources src/crypto/sha512.cpp src/crypto/dh.cpp src/crypto/elliptic_common.cpp - ${ECC_REST} - src/crypto/elliptic_${ECC_IMPL}.cpp + src/crypto/elliptic_impl_priv.cpp + src/crypto/elliptic_secp256k1.cpp src/crypto/rand.cpp src/network/tcp_socket.cpp src/network/udp_socket.cpp @@ -394,7 +382,7 @@ target_include_directories(fc IF(NOT WIN32) set(LINK_USR_LOCAL_LIB -L/usr/local/lib) ENDIF() -target_link_libraries( fc PUBLIC ${LINK_USR_LOCAL_LIB} ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} ${ZLIB_LIBRARIES} ${PLATFORM_SPECIFIC_LIBS} ${RPCRT4} ${CMAKE_DL_LIBS} ${rt_library} ${editline_libraries} ${ECC_LIB} ) +target_link_libraries( fc PUBLIC ${LINK_USR_LOCAL_LIB} ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} ${ZLIB_LIBRARIES} ${PLATFORM_SPECIFIC_LIBS} ${RPCRT4} ${CMAKE_DL_LIBS} ${rt_library} ${editline_libraries} secp256k1 ) if(MSVC) set_source_files_properties( src/network/http/websocket.cpp PROPERTIES COMPILE_FLAGS "/bigobj" ) diff --git a/src/crypto/elliptic_impl_pub.cpp b/src/crypto/elliptic_impl_pub.cpp deleted file mode 100644 index c9bbb08..0000000 --- a/src/crypto/elliptic_impl_pub.cpp +++ /dev/null @@ -1,358 +0,0 @@ -#include -#include - -#include "_elliptic_impl_pub.hpp" - -/* used by mixed + openssl */ - -namespace fc { namespace ecc { - namespace detail { - - public_key_impl::public_key_impl() BOOST_NOEXCEPT - { - _init_lib(); - } - - public_key_impl::public_key_impl( const public_key_impl& cpy ) BOOST_NOEXCEPT - { - _init_lib(); - *this = cpy; - } - - public_key_impl::public_key_impl( public_key_impl&& cpy ) BOOST_NOEXCEPT - { - _init_lib(); - *this = cpy; - } - - public_key_impl::~public_key_impl() BOOST_NOEXCEPT - { - free_key(); - } - - public_key_impl& public_key_impl::operator=( const public_key_impl& pk ) BOOST_NOEXCEPT - { - if (pk._key == nullptr) - { - free_key(); - } else if ( _key == nullptr ) { - _key = EC_KEY_dup( pk._key ); - } else { - EC_KEY_copy( _key, pk._key ); - } - return *this; - } - - public_key_impl& public_key_impl::operator=( public_key_impl&& pk ) BOOST_NOEXCEPT - { - if ( this != &pk ) { - free_key(); - _key = pk._key; - pk._key = nullptr; - } - return *this; - } - - void public_key_impl::free_key() BOOST_NOEXCEPT - { - if( _key != nullptr ) - { - EC_KEY_free(_key); - _key = nullptr; - } - } - - // Perform ECDSA key recovery (see SEC1 4.1.6) for curves over (mod p)-fields - // recid selects which key is recovered - // if check is non-zero, additional checks are performed - int public_key_impl::ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, ECDSA_SIG *ecsig, - const unsigned char *msg, - int msglen, int recid, int check) - { - if (!eckey) FC_THROW_EXCEPTION( exception, "null key" ); - - int ret = 0; - BN_CTX *ctx = NULL; - - BIGNUM *x = NULL; - BIGNUM *e = NULL; - BIGNUM *order = NULL; - BIGNUM *sor = NULL; - BIGNUM *eor = NULL; - BIGNUM *field = NULL; - EC_POINT *R = NULL; - EC_POINT *O = NULL; - EC_POINT *Q = NULL; - BIGNUM *rr = NULL; - BIGNUM *zero = NULL; - int n = 0; - int i = recid / 2; - - const EC_GROUP *group = EC_KEY_get0_group(eckey); - if ((ctx = BN_CTX_new()) == NULL) { ret = -1; goto err; } - BN_CTX_start(ctx); - order = BN_CTX_get(ctx); - if (!EC_GROUP_get_order(group, order, ctx)) { ret = -2; goto err; } - x = BN_CTX_get(ctx); - if (!BN_copy(x, order)) { ret=-1; goto err; } - if (!BN_mul_word(x, i)) { ret=-1; goto err; } - if (!BN_add(x, x, ecsig->r)) { ret=-1; goto err; } - field = BN_CTX_get(ctx); - if (!EC_GROUP_get_curve_GFp(group, field, NULL, NULL, ctx)) { ret=-2; goto err; } - if (BN_cmp(x, field) >= 0) { ret=0; goto err; } - if ((R = EC_POINT_new(group)) == NULL) { ret = -2; goto err; } - if (!EC_POINT_set_compressed_coordinates_GFp(group, R, x, recid % 2, ctx)) { ret=0; goto err; } - if (check) - { - if ((O = EC_POINT_new(group)) == NULL) { ret = -2; goto err; } - if (!EC_POINT_mul(group, O, NULL, R, order, ctx)) { ret=-2; goto err; } - if (!EC_POINT_is_at_infinity(group, O)) { ret = 0; goto err; } - } - if ((Q = EC_POINT_new(group)) == NULL) { ret = -2; goto err; } - n = EC_GROUP_get_degree(group); - e = BN_CTX_get(ctx); - if (!BN_bin2bn(msg, msglen, e)) { ret=-1; goto err; } - if (8*msglen > n) BN_rshift(e, e, 8-(n & 7)); - zero = BN_CTX_get(ctx); - if (!BN_zero(zero)) { ret=-1; goto err; } - if (!BN_mod_sub(e, zero, e, order, ctx)) { ret=-1; goto err; } - rr = BN_CTX_get(ctx); - if (!BN_mod_inverse(rr, ecsig->r, order, ctx)) { ret=-1; goto err; } - sor = BN_CTX_get(ctx); - if (!BN_mod_mul(sor, ecsig->s, rr, order, ctx)) { ret=-1; goto err; } - eor = BN_CTX_get(ctx); - if (!BN_mod_mul(eor, e, rr, order, ctx)) { ret=-1; goto err; } - if (!EC_POINT_mul(group, Q, eor, R, sor, ctx)) { ret=-2; goto err; } - if (!EC_KEY_set_public_key(eckey, Q)) { ret=-2; goto err; } - - ret = 1; - - err: - if (ctx) { - BN_CTX_end(ctx); - BN_CTX_free(ctx); - } - if (R != NULL) EC_POINT_free(R); - if (O != NULL) EC_POINT_free(O); - if (Q != NULL) EC_POINT_free(Q); - return ret; - } - } - - public_key::public_key() {} - - public_key::public_key( const public_key& pk ) : my( pk.my ) {} - - public_key::public_key( public_key&& pk ) : my( std::move( pk.my ) ) {} - - public_key::~public_key() {} - - public_key& public_key::operator=( public_key&& pk ) - { - my = std::move(pk.my); - return *this; - } - - public_key& public_key::operator=( const public_key& pk ) - { - my = pk.my; - return *this; - } - - bool public_key::valid()const - { - return my->_key != nullptr; - } - - /* WARNING! This implementation is broken, it is actually equivalent to - * public_key::add()! - */ -// public_key public_key::mult( const fc::sha256& digest ) const -// { -// // get point from this public key -// const EC_POINT* master_pub = EC_KEY_get0_public_key( my->_key ); -// ec_group group(EC_GROUP_new_by_curve_name(NID_secp256k1)); -// -// ssl_bignum z; -// BN_bin2bn((unsigned char*)&digest, sizeof(digest), z); -// -// // multiply by digest -// ssl_bignum one; -// BN_one(one); -// bn_ctx ctx(BN_CTX_new()); -// -// ec_point result(EC_POINT_new(group)); -// EC_POINT_mul(group, result, z, master_pub, one, ctx); -// -// public_key rtn; -// rtn.my->_key = EC_KEY_new_by_curve_name( NID_secp256k1 ); -// EC_KEY_set_public_key(rtn.my->_key,result); -// -// return rtn; -// } - public_key public_key::add( const fc::sha256& digest )const - { - try { - ec_group group(EC_GROUP_new_by_curve_name(NID_secp256k1)); - bn_ctx ctx(BN_CTX_new()); - - fc::bigint digest_bi( (char*)&digest, sizeof(digest) ); - - ssl_bignum order; - EC_GROUP_get_order(group, order, ctx); - if( digest_bi > fc::bigint(order) ) - { - FC_THROW_EXCEPTION( exception, "digest > group order" ); - } - - - public_key digest_key = private_key::regenerate(digest).get_public_key(); - const EC_POINT* digest_point = EC_KEY_get0_public_key( digest_key.my->_key ); - - // get point from this public key - const EC_POINT* master_pub = EC_KEY_get0_public_key( my->_key ); - -// ssl_bignum z; -// BN_bin2bn((unsigned char*)&digest, sizeof(digest), z); - - // multiply by digest -// ssl_bignum one; -// BN_one(one); - - ec_point result(EC_POINT_new(group)); - EC_POINT_add(group, result, digest_point, master_pub, ctx); - - if (EC_POINT_is_at_infinity(group, result)) - { - FC_THROW_EXCEPTION( exception, "point at infinity" ); - } - - - public_key rtn; - rtn.my->_key = EC_KEY_new_by_curve_name( NID_secp256k1 ); - EC_KEY_set_public_key(rtn.my->_key,result); - return rtn; - } FC_RETHROW_EXCEPTIONS( debug, "digest: ${digest}", ("digest",digest) ); - } - - std::string public_key::to_base58() const - { - public_key_data key = serialize(); - return to_base58( key ); - } - -// signature private_key::sign( const fc::sha256& digest )const -// { -// unsigned int buf_len = ECDSA_size(my->_key); -//// fprintf( stderr, "%d %d\n", buf_len, sizeof(sha256) ); -// signature sig; -// assert( buf_len == sizeof(sig) ); -// -// if( !ECDSA_sign( 0, -// (const unsigned char*)&digest, sizeof(digest), -// (unsigned char*)&sig, &buf_len, my->_key ) ) -// { -// FC_THROW_EXCEPTION( exception, "signing error" ); -// } -// -// -// return sig; -// } -// bool public_key::verify( const fc::sha256& digest, const fc::ecc::signature& sig ) -// { -// return 1 == ECDSA_verify( 0, (unsigned char*)&digest, sizeof(digest), (unsigned char*)&sig, sizeof(sig), my->_key ); -// } - - public_key_data public_key::serialize()const - { - public_key_data dat; - if( !my->_key ) return dat; - EC_KEY_set_conv_form( my->_key, POINT_CONVERSION_COMPRESSED ); - /*size_t nbytes = i2o_ECPublicKey( my->_key, nullptr ); */ - /*assert( nbytes == 33 )*/ - char* front = &dat.data[0]; - i2o_ECPublicKey( my->_key, (unsigned char**)&front ); // FIXME: questionable memory handling - return dat; - /* - EC_POINT* pub = EC_KEY_get0_public_key( my->_key ); - EC_GROUP* group = EC_KEY_get0_group( my->_key ); - EC_POINT_get_affine_coordinates_GFp( group, pub, self.my->_pub_x.get(), self.my->_pub_y.get(), nullptr ); - */ - } - public_key_point_data public_key::serialize_ecc_point()const - { - public_key_point_data dat; - if( !my->_key ) return dat; - EC_KEY_set_conv_form( my->_key, POINT_CONVERSION_UNCOMPRESSED ); - char* front = &dat.data[0]; - i2o_ECPublicKey( my->_key, (unsigned char**)&front ); // FIXME: questionable memory handling - return dat; - } - - public_key::public_key( const public_key_point_data& dat ) - { - const char* front = &dat.data[0]; - if( *front == 0 ){} - else - { - my->_key = EC_KEY_new_by_curve_name( NID_secp256k1 ); - my->_key = o2i_ECPublicKey( &my->_key, (const unsigned char**)&front, sizeof(dat) ); - if( !my->_key ) - { - FC_THROW_EXCEPTION( exception, "error decoding public key", ("s", ERR_error_string( ERR_get_error(), nullptr) ) ); - } - } - } - public_key::public_key( const public_key_data& dat ) - { - const char* front = &dat.data[0]; - if( *front == 0 ){} - else - { - my->_key = EC_KEY_new_by_curve_name( NID_secp256k1 ); - my->_key = o2i_ECPublicKey( &my->_key, (const unsigned char**)&front, sizeof(public_key_data) ); - if( !my->_key ) - { - FC_THROW_EXCEPTION( exception, "error decoding public key", ("s", ERR_error_string( ERR_get_error(), nullptr) ) ); - } - } - } - -// bool private_key::verify( const fc::sha256& digest, const fc::ecc::signature& sig ) -// { -// return 1 == ECDSA_verify( 0, (unsigned char*)&digest, sizeof(digest), (unsigned char*)&sig, sizeof(sig), my->_key ); -// } - - public_key::public_key( const compact_signature& c, const fc::sha256& digest, bool check_canonical ) - { - int nV = c.data[0]; - if (nV<27 || nV>=35) - FC_THROW_EXCEPTION( exception, "unable to reconstruct public key from signature" ); - - ECDSA_SIG *sig = ECDSA_SIG_new(); - BN_bin2bn(&c.data[1],32,sig->r); - BN_bin2bn(&c.data[33],32,sig->s); - - if( check_canonical ) - { - FC_ASSERT( is_canonical( c ), "signature is not canonical" ); - } - - my->_key = EC_KEY_new_by_curve_name(NID_secp256k1); - - if (nV >= 31) - { - EC_KEY_set_conv_form( my->_key, POINT_CONVERSION_COMPRESSED ); - nV -= 4; -// fprintf( stderr, "compressed\n" ); - } - - if (detail::public_key_impl::ECDSA_SIG_recover_key_GFp(my->_key, sig, (unsigned char*)&digest, sizeof(digest), nV - 27, 0) == 1) - { - ECDSA_SIG_free(sig); - return; - } - ECDSA_SIG_free(sig); - FC_THROW_EXCEPTION( exception, "unable to reconstruct public key from signature" ); - } -}} diff --git a/src/crypto/elliptic_mixed.cpp b/src/crypto/elliptic_mixed.cpp deleted file mode 100644 index e9af233..0000000 --- a/src/crypto/elliptic_mixed.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include - -#include -#include - -#include -#include -#include - -#include -#include - -#include "_elliptic_impl_priv.hpp" -#include "_elliptic_impl_pub.hpp" - -namespace fc { namespace ecc { - namespace detail - { - const secp256k1_context_t* _get_context() { - static secp256k1_context_t* ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN); - return ctx; - } - - void _init_lib() { - static const secp256k1_context_t* ctx = _get_context(); - static int init_o = init_openssl(); - } - } - - static const private_key_secret empty_priv; - fc::sha512 private_key::get_shared_secret( const public_key& other )const - { - FC_ASSERT( my->_key != empty_priv ); - FC_ASSERT( other.my->_key != nullptr ); - public_key_data pub(other.serialize()); - FC_ASSERT( secp256k1_ec_pubkey_tweak_mul( detail::_get_context(), (unsigned char*) pub.begin(), pub.size(), (unsigned char*) my->_key.data() ) ); - return fc::sha512::hash( pub.begin() + 1, pub.size() - 1 ); - } - -} } diff --git a/src/crypto/elliptic_openssl.cpp b/src/crypto/elliptic_openssl.cpp deleted file mode 100644 index 78170a7..0000000 --- a/src/crypto/elliptic_openssl.cpp +++ /dev/null @@ -1,261 +0,0 @@ -#include - -#include -#include - -#include -#include -#include - -#include - -#include "_elliptic_impl_pub.hpp" - -namespace fc { namespace ecc { - namespace detail - { - void _init_lib() { - static int init_o = init_openssl(); - } - - class private_key_impl - { - public: - private_key_impl() BOOST_NOEXCEPT - { - _init_lib(); - } - - private_key_impl( const private_key_impl& cpy ) BOOST_NOEXCEPT - { - _init_lib(); - *this = cpy; - } - - private_key_impl( private_key_impl&& cpy ) BOOST_NOEXCEPT - { - _init_lib(); - *this = cpy; - } - - ~private_key_impl() BOOST_NOEXCEPT - { - free_key(); - } - - private_key_impl& operator=( const private_key_impl& pk ) BOOST_NOEXCEPT - { - if (pk._key == nullptr) - { - free_key(); - } else if ( _key == nullptr ) { - _key = EC_KEY_dup( pk._key ); - } else { - EC_KEY_copy( _key, pk._key ); - } - return *this; - } - - private_key_impl& operator=( private_key_impl&& pk ) BOOST_NOEXCEPT - { - if ( this != &pk ) { - free_key(); - _key = pk._key; - pk._key = nullptr; - } - return *this; - } - - EC_KEY* _key = nullptr; - - private: - void free_key() BOOST_NOEXCEPT - { - if( _key != nullptr ) - { - EC_KEY_free(_key); - _key = nullptr; - } - } - }; - } - - private_key::private_key() {} - - private_key::private_key( const private_key& pk ) : my( pk.my ) {} - - private_key::private_key( private_key&& pk ) : my( std::move( pk.my ) ) {} - - private_key::~private_key() {} - - private_key& private_key::operator=( private_key&& pk ) - { - my = std::move(pk.my); - return *this; - } - - private_key& private_key::operator=( const private_key& pk ) - { - my = pk.my; - return *this; - } - static void * ecies_key_derivation(const void *input, size_t ilen, void *output, size_t *olen) - { - if (*olen < SHA512_DIGEST_LENGTH) { - return NULL; - } - *olen = SHA512_DIGEST_LENGTH; - return (void*)SHA512((const unsigned char*)input, ilen, (unsigned char*)output); - } - - int static inline EC_KEY_regenerate_key(EC_KEY *eckey, const BIGNUM *priv_key) - { - int ok = 0; - BN_CTX *ctx = NULL; - EC_POINT *pub_key = NULL; - - if (!eckey) return 0; - - const EC_GROUP *group = EC_KEY_get0_group(eckey); - - if ((ctx = BN_CTX_new()) == NULL) - goto err; - - pub_key = EC_POINT_new(group); - - if (pub_key == NULL) - goto err; - - if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx)) - goto err; - - EC_KEY_set_private_key(eckey,priv_key); - EC_KEY_set_public_key(eckey,pub_key); - - ok = 1; - - err: - - if (pub_key) EC_POINT_free(pub_key); - if (ctx != NULL) BN_CTX_free(ctx); - - return(ok); - } - - private_key private_key::regenerate( const fc::sha256& secret ) - { - private_key self; - self.my->_key = EC_KEY_new_by_curve_name( NID_secp256k1 ); - if( !self.my->_key ) FC_THROW_EXCEPTION( exception, "Unable to generate EC key" ); - - ssl_bignum bn; - BN_bin2bn( (const unsigned char*)&secret, 32, bn ); - - if( !EC_KEY_regenerate_key(self.my->_key,bn) ) - { - FC_THROW_EXCEPTION( exception, "unable to regenerate key" ); - } - return self; - } - - fc::sha256 private_key::get_secret()const - { - return get_secret( my->_key ); - } - - private_key::private_key( EC_KEY* k ) - { - my->_key = k; - } - - public_key private_key::get_public_key()const - { - public_key pub; - pub.my->_key = EC_KEY_new_by_curve_name( NID_secp256k1 ); - EC_KEY_set_public_key( pub.my->_key, EC_KEY_get0_public_key( my->_key ) ); - return pub; - } - - - fc::sha512 private_key::get_shared_secret( const public_key& other )const - { - FC_ASSERT( my->_key != nullptr ); - FC_ASSERT( other.my->_key != nullptr ); - fc::sha512 buf; - ECDH_compute_key( (unsigned char*)&buf, sizeof(buf), EC_KEY_get0_public_key(other.my->_key), my->_key, ecies_key_derivation ); - return buf; - } - - compact_signature private_key::sign_compact( const fc::sha256& digest )const - { - try { - FC_ASSERT( my->_key != nullptr ); - auto my_pub_key = get_public_key().serialize(); // just for good measure - //ECDSA_SIG *sig = ECDSA_do_sign((unsigned char*)&digest, sizeof(digest), my->_key); - public_key_data key_data; - while( true ) - { - ecdsa_sig sig = ECDSA_do_sign((unsigned char*)&digest, sizeof(digest), my->_key); - - if (sig==nullptr) - FC_THROW_EXCEPTION( exception, "Unable to sign" ); - - compact_signature csig; - // memset( csig.data, 0, sizeof(csig) ); - - int nBitsR = BN_num_bits(sig->r); - int nBitsS = BN_num_bits(sig->s); - if (nBitsR <= 256 && nBitsS <= 256) - { - int nRecId = -1; - EC_KEY* key = EC_KEY_new_by_curve_name( NID_secp256k1 ); - FC_ASSERT( key ); - EC_KEY_set_conv_form( key, POINT_CONVERSION_COMPRESSED ); - for (int i=0; i<4; i++) - { - if (detail::public_key_impl::ECDSA_SIG_recover_key_GFp(key, sig, (unsigned char*)&digest, sizeof(digest), i, 1) == 1) - { - unsigned char* buffer = (unsigned char*) key_data.begin(); - i2o_ECPublicKey( key, &buffer ); // FIXME: questionable memory handling - if ( key_data == my_pub_key ) - { - nRecId = i; - break; - } - } - } - EC_KEY_free( key ); - - if (nRecId == -1) - { - FC_THROW_EXCEPTION( exception, "unable to construct recoverable key"); - } - unsigned char* result = nullptr; - auto bytes = i2d_ECDSA_SIG( sig, &result ); - auto lenR = result[3]; - auto lenS = result[5+lenR]; - //idump( (result[0])(result[1])(result[2])(result[3])(result[3+lenR])(result[4+lenR])(bytes)(lenR)(lenS) ); - if( lenR != 32 ) { free(result); continue; } - if( lenS != 32 ) { free(result); continue; } - //idump( (33-(nBitsR+7)/8) ); - //idump( (65-(nBitsS+7)/8) ); - //idump( (sizeof(csig) ) ); - memcpy( &csig.data[1], &result[4], lenR ); - memcpy( &csig.data[33], &result[6+lenR], lenS ); - //idump( (csig.data[33]) ); - //idump( (csig.data[1]) ); - free(result); - //idump( (nRecId) ); - csig.data[0] = nRecId+27+4;//(fCompressedPubKey ? 4 : 0); - /* - idump( (csig) ); - auto rlen = BN_bn2bin(sig->r,&csig.data[33-(nBitsR+7)/8]); - auto slen = BN_bn2bin(sig->s,&csig.data[65-(nBitsS+7)/8]); - idump( (rlen)(slen) ); - */ - } - return csig; - } // while true - } FC_RETHROW_EXCEPTIONS( warn, "sign ${digest}", ("digest", digest)("private_key",*this) ); - } -} } From 8a5cbac7889e36b9e27a5a69df130c71a672f550 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Thu, 4 Apr 2019 18:16:23 +0200 Subject: [PATCH 08/11] Added missing include --- include/fc/fwd_impl.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/include/fc/fwd_impl.hpp b/include/fc/fwd_impl.hpp index 1c8155d..53a4440 100644 --- a/include/fc/fwd_impl.hpp +++ b/include/fc/fwd_impl.hpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace fc { From cf502aab92264e23ec369e033712d7e7ea28dbd4 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Thu, 4 Apr 2019 18:22:23 +0200 Subject: [PATCH 09/11] Added missing include --- include/fc/optional.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/include/fc/optional.hpp b/include/fc/optional.hpp index df6e1f2..ff6c789 100644 --- a/include/fc/optional.hpp +++ b/include/fc/optional.hpp @@ -1,6 +1,7 @@ #pragma once #include #include +#include namespace fc { From bfe7904485e8d36bbab3cc7a2f8ddbd0c6d8fae4 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Thu, 4 Apr 2019 18:48:42 +0200 Subject: [PATCH 10/11] Removed obsolete test from travis.yml --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 2fed025..a8cf4fc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -31,7 +31,6 @@ script: - set -o pipefail - tests/run-parallel-tests.sh tests/all_tests - "tests/api 2>&1 | grep -vE 'callback result 9|remote_calc->add. 4, 5 .: 9|set callback|] \\.$'" - - tests/bip_lock 2>&1 | cat - tests/hmac_test 2>&1 | cat - tests/ecc_test README.md 2>&1 | cat - tests/log_test 2>&1 | cat From 4511300081ea9cf2b773059d64cba78a6b7a0041 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Tue, 9 Apr 2019 08:46:56 +0200 Subject: [PATCH 11/11] Minor fixes for Windows --- include/fc/io/iostream.hpp | 1 + include/fc/network/http/connection.hpp | 1 + include/fc/network/tcp_socket.hpp | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/include/fc/io/iostream.hpp b/include/fc/io/iostream.hpp index 9fa592b..3e68440 100644 --- a/include/fc/io/iostream.hpp +++ b/include/fc/io/iostream.hpp @@ -1,5 +1,6 @@ #pragma once #include +#include namespace fc { diff --git a/include/fc/network/http/connection.hpp b/include/fc/network/http/connection.hpp index 8a34076..466d111 100644 --- a/include/fc/network/http/connection.hpp +++ b/include/fc/network/http/connection.hpp @@ -1,5 +1,6 @@ #pragma once #include +#include #include namespace fc { diff --git a/include/fc/network/tcp_socket.hpp b/include/fc/network/tcp_socket.hpp index 59fcb73..fe6b837 100644 --- a/include/fc/network/tcp_socket.hpp +++ b/include/fc/network/tcp_socket.hpp @@ -51,7 +51,7 @@ namespace fc { friend class tcp_server; class impl; #ifdef _WIN64 - fc::fwd my; + fc::fwd my; #else fc::fwd my; #endif