├── .gitignore ├── .gitmodules ├── CMakeLists.txt ├── CMakeModules ├── ArgumentParser.cmake ├── FindBoost.cmake ├── FindReadline.cmake ├── FindVLD.cmake ├── FindWt.cmake ├── ParseLibraryList.cmake ├── SetupTargetMacros.cmake ├── UseLibraryMacros.cmake └── VersionMacros.cmake ├── GitVersionGen ├── GetGitRevisionDescription.cmake └── GetGitRevisionDescription.cmake.in ├── README-ecc.md ├── README.md ├── fc.natvis ├── include └── fc │ ├── actor.hpp │ ├── aligned.hpp │ ├── any.hpp │ ├── api.hpp │ ├── array.hpp │ ├── asio.hpp │ ├── bitutil.hpp │ ├── bloom_filter.hpp │ ├── compress │ ├── smaz.hpp │ └── zlib.hpp │ ├── container │ ├── deque.hpp │ ├── deque_fwd.hpp │ ├── flat.hpp │ └── flat_fwd.hpp │ ├── crypto │ ├── aes.hpp │ ├── base32.hpp │ ├── base36.hpp │ ├── base58.hpp │ ├── base64.hpp │ ├── bigint.hpp │ ├── blowfish.hpp │ ├── city.hpp │ ├── dh.hpp │ ├── digest.hpp │ ├── elliptic.hpp │ ├── hex.hpp │ ├── hmac.hpp │ ├── openssl.hpp │ ├── pke.hpp │ ├── rand.hpp │ ├── ripemd160.hpp │ ├── sha1.hpp │ ├── sha224.hpp │ ├── sha256.hpp │ └── sha512.hpp │ ├── exception │ └── exception.hpp │ ├── filesystem.hpp │ ├── fwd.hpp │ ├── fwd_impl.hpp │ ├── git_revision.hpp │ ├── interprocess │ ├── file_mapping.hpp │ ├── iprocess.hpp │ ├── mmap_struct.hpp │ ├── process.hpp │ └── signals.hpp │ ├── io │ ├── buffered_iostream.hpp │ ├── console.hpp │ ├── datastream.hpp │ ├── datastream_back.hpp │ ├── enum_type.hpp │ ├── fstream.hpp │ ├── iobuffer.hpp │ ├── iostream.hpp │ ├── json.hpp │ ├── json_relaxed.hpp │ ├── raw.hpp │ ├── raw_fwd.hpp │ ├── raw_unpack_file.hpp │ ├── raw_variant.hpp │ ├── sstream.hpp │ ├── stdio.hpp │ └── varint.hpp │ ├── log │ ├── appender.hpp │ ├── console_appender.hpp │ ├── file_appender.hpp │ ├── gelf_appender.hpp │ ├── log_message.hpp │ ├── logger.hpp │ └── logger_config.hpp │ ├── make_fused.hpp │ ├── network │ ├── gntp.hpp │ ├── http │ │ ├── connection.hpp │ │ ├── server.hpp │ │ └── websocket.hpp │ ├── ip.hpp │ ├── ntp.hpp │ ├── rate_limiting.hpp │ ├── resolve.hpp │ ├── tcp_socket.hpp │ ├── tcp_socket_io_hooks.hpp │ ├── udp_socket.hpp │ ├── udt_socket.hpp │ └── url.hpp │ ├── noncopyable.hpp │ ├── optional.hpp │ ├── platform_independence.hpp │ ├── real128.hpp │ ├── reflect │ ├── reflect.hpp │ ├── typename.hpp │ └── variant.hpp │ ├── rpc │ ├── api_connection.hpp │ ├── cli.hpp │ ├── http_api.hpp │ ├── json_connection.hpp │ ├── state.hpp │ ├── variant_connection.hpp │ ├── variant_stream.hpp │ └── websocket_api.hpp │ ├── safe.hpp │ ├── shared_ptr.hpp │ ├── signal.hpp │ ├── signals.hpp │ ├── smart_ref_fwd.hpp │ ├── smart_ref_impl.hpp │ ├── ssh │ ├── client.hpp │ ├── error.hpp │ └── process.hpp │ ├── static_variant.hpp │ ├── string.hpp │ ├── thread │ ├── future.hpp │ ├── mutex.hpp │ ├── non_preemptable_scope_check.hpp │ ├── priority.hpp │ ├── scoped_lock.hpp │ ├── spin_lock.hpp │ ├── spin_yield_lock.hpp │ ├── task.hpp │ ├── thread.hpp │ ├── thread_specific.hpp │ ├── unique_lock.hpp │ └── wait_condition.hpp │ ├── time.hpp │ ├── tuple.hpp │ ├── uint128.hpp │ ├── unique_ptr.hpp │ ├── utf8.hpp │ ├── utility.hpp │ ├── variant.hpp │ ├── variant_object.hpp │ ├── vector.hpp │ ├── vector_fwd.hpp │ └── wait_any.hpp ├── src ├── asio.cpp ├── byteswap.hpp ├── compress │ ├── miniz.c │ ├── smaz.cpp │ └── zlib.cpp ├── crypto │ ├── _digest_common.cpp │ ├── _digest_common.hpp │ ├── _elliptic_impl_priv.hpp │ ├── _elliptic_impl_pub.hpp │ ├── aes.cpp │ ├── base32.cpp │ ├── base36.cpp │ ├── base58.cpp │ ├── base64.cpp │ ├── bigint.cpp │ ├── blowfish.cpp │ ├── city.cpp │ ├── crc.cpp │ ├── dh.cpp │ ├── elliptic_common.cpp │ ├── elliptic_impl_priv.cpp │ ├── elliptic_impl_pub.cpp │ ├── elliptic_mixed.cpp │ ├── elliptic_openssl.cpp │ ├── elliptic_secp256k1.cpp │ ├── hex.cpp │ ├── openssl.cpp │ ├── pke.cpp │ ├── rand.cpp │ ├── ripemd160.cpp │ ├── sha1.cpp │ ├── sha224.cpp │ ├── sha256.cpp │ └── sha512.cpp ├── exception.cpp ├── filesystem.cpp ├── git_revision.cpp.in ├── interprocess │ ├── file_mapping.cpp │ ├── mmap_struct.cpp │ ├── process.cpp │ └── signals.cpp ├── io │ ├── buffered_iostream.cpp │ ├── console.cpp │ ├── datastream.cpp │ ├── fstream.cpp │ ├── iostream.cpp │ ├── json.cpp │ ├── sstream.cpp │ └── varint.cpp ├── log │ ├── appender.cpp │ ├── console_appender.cpp │ ├── console_defines.h │ ├── file_appender.cpp │ ├── gelf_appender.cpp │ ├── log_message.cpp │ ├── logger.cpp │ └── logger_config.cpp ├── network │ ├── gntp.cpp │ ├── http │ │ ├── http_connection.cpp │ │ ├── http_server.cpp │ │ └── websocket.cpp │ ├── ip.cpp │ ├── ntp.cpp │ ├── rate_limiting.cpp │ ├── resolve.cpp │ ├── tcp_socket.cpp │ ├── udp_socket.cpp │ ├── udt_socket.cpp │ └── url.cpp ├── real128.cpp ├── rpc │ ├── cli.cpp │ ├── http_api.cpp │ ├── json_connection.cpp │ ├── state.cpp │ └── websocket_api.cpp ├── shared_ptr.cpp ├── ssh │ ├── client.cpp │ ├── client_impl.hpp │ └── process.cpp ├── string.cpp ├── thread │ ├── context.hpp │ ├── future.cpp │ ├── mutex.cpp │ ├── non_preemptable_scope_check.cpp │ ├── spin_lock.cpp │ ├── spin_yield_lock.cpp │ ├── task.cpp │ ├── thread.cpp │ ├── thread_d.hpp │ └── thread_specific.cpp ├── time.cpp ├── uint128.cpp ├── utf8.cpp ├── utf8 │ ├── ReleaseNotes │ ├── checked.h │ ├── core.h │ ├── unchecked.h │ └── utf8cpp.html ├── variant.cpp └── variant_object.cpp ├── tests ├── all_tests.cpp ├── api.cpp ├── blinding_test.cpp ├── bloom_test.cpp ├── compress │ └── compress.cpp ├── crypto │ ├── aes_test.cpp │ ├── base_n_tests.cpp │ ├── bigint_test.cpp │ ├── blind.cpp │ ├── blowfish_test.cpp │ ├── dh_test.cpp │ ├── ecc-interop.sh │ ├── ecc_test.cpp │ ├── rand_test.cpp │ └── sha_tests.cpp ├── hmac_test.cpp ├── network │ ├── http │ │ └── websocket_test.cpp │ └── ntp_test.cpp ├── rate_limiting.cpp ├── real128_test.cpp ├── rpc.cpp ├── sleep.cpp ├── thread │ └── task_cancel.cpp ├── udt_client.cpp ├── udt_server.cpp ├── udtc.cpp ├── udts.cpp └── utf8_test.cpp └── vendor ├── boost_1.51 ├── include │ └── boost │ │ ├── process.hpp │ │ └── process │ │ ├── all.hpp │ │ ├── child.hpp │ │ ├── config.hpp │ │ ├── context.hpp │ │ ├── detail │ │ ├── basic_status.hpp │ │ ├── basic_status_service.hpp │ │ ├── posix_helpers.hpp │ │ ├── status_impl.hpp │ │ ├── systembuf.hpp │ │ └── windows_helpers.hpp │ │ ├── environment.hpp │ │ ├── handle.hpp │ │ ├── operations.hpp │ │ ├── pid_type.hpp │ │ ├── pipe.hpp │ │ ├── pistream.hpp │ │ ├── postream.hpp │ │ ├── process.hpp │ │ ├── self.hpp │ │ ├── status.hpp │ │ ├── stream_behavior.hpp │ │ ├── stream_ends.hpp │ │ ├── stream_id.hpp │ │ └── stream_type.hpp └── libs │ └── context │ ├── asm │ ├── fcontext_arm_aapcs_elf_gas.S │ ├── fcontext_i386_ms_pe_masm.asm │ ├── fcontext_i386_sysv_elf_gas.S │ ├── fcontext_i386_sysv_macho_gas.S │ ├── fcontext_mips32_o32_elf_gas.S │ ├── fcontext_ppc32_sysv_elf_gas.S │ ├── fcontext_ppc64_sysv_elf_gas.S │ ├── fcontext_x86_64_ms_pe_masm.asm │ ├── fcontext_x86_64_sysv_elf_gas.S │ └── fcontext_x86_64_sysv_macho_gas.S │ ├── fcontext.cpp │ ├── seh.cpp │ ├── stack_allocator_posix.cpp │ ├── stack_allocator_windows.cpp │ ├── stack_utils_posix.cpp │ └── stack_utils_windows.cpp ├── cyoencode-1.0.2 ├── LICENSE.TXT ├── README.TXT └── src │ ├── CyoDecode.c │ ├── CyoDecode.h │ ├── CyoEncode.c │ ├── CyoEncode.h │ ├── build.sh │ ├── cyoencode-vc100.vcxproj │ ├── cyoencode-vc71.vcproj │ ├── cyoencode-vc80.vcproj │ ├── cyoencode-vc90.vcproj │ └── test.c └── udt4 ├── CMakeLists.txt ├── LICENSE.txt ├── README.txt └── src ├── api.cpp ├── api.h ├── buffer.cpp ├── buffer.h ├── cache.cpp ├── cache.h ├── ccc.cpp ├── ccc.h ├── channel.cpp ├── channel.h ├── common.cpp ├── common.h ├── core.cpp ├── core.h ├── epoll.cpp ├── epoll.h ├── list.cpp ├── list.h ├── md5.cpp ├── md5.h ├── packet.cpp ├── packet.h ├── queue.cpp ├── queue.h ├── udt.h ├── window.cpp └── window.h /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Compiled Dynamic libraries 8 | *.so 9 | *.dylib 10 | *.dll 11 | 12 | # Compiled Static libraries 13 | *.lai 14 | *.la 15 | *.a 16 | *.lib 17 | 18 | # CMake->MSVC artifacts 19 | *.sln 20 | *.vcxproj 21 | ALL_BUILD 22 | ZERO_CHECK 23 | 24 | # MSVC secondary artifacts 25 | *.suo 26 | *.vcxproj.filters 27 | *.vcxproj.user 28 | *.pdb 29 | *.ilk 30 | *.lastbuildstate 31 | *.sdf 32 | *.opensdf 33 | Debug/ 34 | Release/ 35 | 36 | CMakeCache.txt 37 | CMakeFiles 38 | Makefile 39 | *.cmake 40 | *.cbp 41 | 42 | libfc.a 43 | libfc_debug.a 44 | 45 | *.sw* 46 | 47 | fc_automoc.cpp 48 | git_revision.cpp 49 | GitSHA3.cpp 50 | 51 | ntp_test 52 | task_cancel_test 53 | udt_client 54 | udt_server 55 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "vendor/secp256k1-zkp"] 2 | path = vendor/secp256k1-zkp 3 | url = https://github.com/cryptonomex/secp256k1-zkp.git 4 | [submodule "vendor/websocketpp"] 5 | path = vendor/websocketpp 6 | url = https://github.com/zaphoyd/websocketpp.git 7 | -------------------------------------------------------------------------------- /CMakeModules/FindReadline.cmake: -------------------------------------------------------------------------------- 1 | # - Try to find readline include dirs and libraries 2 | # 3 | # Usage of this module as follows: 4 | # 5 | # find_package(Readline) 6 | # 7 | # Variables used by this module, they can change the default behaviour and need 8 | # to be set before calling find_package: 9 | # 10 | # Readline_ROOT_DIR Set this variable to the root installation of 11 | # readline if the module has problems finding the 12 | # proper installation path. 13 | # 14 | # Variables defined by this module: 15 | # 16 | # READLINE_FOUND System has readline, include and lib dirs found 17 | # Readline_INCLUDE_DIR The readline include directories. 18 | # Readline_LIBRARY The readline library. 19 | 20 | find_path(Readline_ROOT_DIR 21 | NAMES include/readline/readline.h 22 | ) 23 | 24 | find_path(Readline_INCLUDE_DIR 25 | NAMES readline/readline.h 26 | HINTS ${Readline_ROOT_DIR}/include 27 | ) 28 | 29 | find_library(Readline_LIBRARY 30 | NAMES readline 31 | HINTS ${Readline_ROOT_DIR}/lib 32 | ) 33 | 34 | if(Readline_INCLUDE_DIR AND Readline_LIBRARY AND Ncurses_LIBRARY) 35 | set(READLINE_FOUND TRUE) 36 | else(Readline_INCLUDE_DIR AND Readline_LIBRARY AND Ncurses_LIBRARY) 37 | FIND_LIBRARY(Readline_LIBRARY NAMES readline) 38 | include(FindPackageHandleStandardArgs) 39 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(Readline DEFAULT_MSG Readline_INCLUDE_DIR Readline_LIBRARY ) 40 | MARK_AS_ADVANCED(Readline_INCLUDE_DIR Readline_LIBRARY) 41 | endif(Readline_INCLUDE_DIR AND Readline_LIBRARY AND Ncurses_LIBRARY) 42 | 43 | mark_as_advanced( 44 | Readline_ROOT_DIR 45 | Readline_INCLUDE_DIR 46 | Readline_LIBRARY 47 | ) 48 | -------------------------------------------------------------------------------- /CMakeModules/UseLibraryMacros.cmake: -------------------------------------------------------------------------------- 1 | # This module defines macros that are useful for using libraries in a build. The 2 | # macros in this module are typically used along with the FindDependencyMacros. 3 | 4 | # ADD_LIBRARY_TO_LIST Macro 5 | # Adds a library to a list of libraries if it is found. Otherwise, reports an 6 | # error. 7 | # 8 | # Usage: 9 | # ADD_LIBRARY_TO_LIST( libraries found lib lib_name ) 10 | # 11 | # Parameters: 12 | # libraries The list of libraries to add the library to. 13 | # found Whether or not the library to add was found. 14 | # lib The library to add to the list. 15 | # lib_name The name of the library to add to the list. 16 | MACRO( ADD_LIBRARY_TO_LIST libraries found lib lib_name ) 17 | 18 | # Setting found to itself is necessary for the conditional to work. 19 | SET( found ${found} ) 20 | 21 | # IF found, then add the library to the list, else report an error. 22 | IF( found ) 23 | LIST( REMOVE_ITEM ${libraries} ${lib} ) 24 | SET( ${libraries} ${${libraries}} ${lib} ) 25 | ENDIF( found ) 26 | IF( NOT found ) 27 | MESSAGE( "Using ${lib_name} failed." ) 28 | ENDIF( NOT found ) 29 | 30 | ENDMACRO( ADD_LIBRARY_TO_LIST ) 31 | 32 | 33 | # USE_LIBRARY_GLOBALS Macro 34 | # If ${prefix}_USE_${LIB} is true, then ${prefix}_${LIB}_LIBRARY will be added 35 | # to ${prefix}_LIBRARIES (assuming the library was correctly found). All of the 36 | # dependencies will also be added to ${prefix}_LIBRARIES. 37 | # 38 | # Usage: 39 | # USE_LIBRARY_GLOBALS( prefix lib 40 | # DEPS dependency1 [dependency2...] ) 41 | # 42 | # Parameters: 43 | # prefix The prefix for the global variables. 44 | # lib The library to try to use. 45 | # DEPS Follow with the list of dependencies that should be added with 46 | # the given library. 47 | MACRO( USE_LIBRARY_GLOBALS prefix lib ) 48 | 49 | STRING( TOUPPER ${lib} upper ) 50 | 51 | # If the library should be used... 52 | IF( ${prefix}_USE_${upper} ) 53 | 54 | # Parse the arguments into variables. 55 | ARGUMENT_PARSER( "" "DEPS" "" ${ARGN} ) 56 | 57 | # Add the library to the list. 58 | ADD_LIBRARY_TO_LIST( ${prefix}_LIBRARIES "${${prefix}_${upper}_FOUND}" 59 | "${${prefix}_${upper}_LIBRARY}" ${lib} ) 60 | 61 | # For each of the library's dependencies. 62 | FOREACH( dep_itr ${deps} ) 63 | STRING( TOUPPER ${dep_itr} upper ) 64 | 65 | # Add the dependency to the list. 66 | ADD_LIBRARY_TO_LIST( ${prefix}_LIBRARIES 67 | "${${prefix}_${upper}_FOUND}" 68 | "${${prefix}_${upper}_LIBRARY}" ${dep_itr} ) 69 | ENDFOREACH( dep_itr ) 70 | ENDIF( ${prefix}_USE_${upper} ) 71 | 72 | ENDMACRO( USE_LIBRARY_GLOBALS ) 73 | -------------------------------------------------------------------------------- /GitVersionGen/GetGitRevisionDescription.cmake.in: -------------------------------------------------------------------------------- 1 | # 2 | # Internal file for GetGitRevisionDescription.cmake 3 | # 4 | # Requires CMake 2.6 or newer (uses the 'function' command) 5 | # 6 | # Original Author: 7 | # 2009-2010 Ryan Pavlik 8 | # http://academic.cleardefinition.com 9 | # Iowa State University HCI Graduate Program/VRAC 10 | # 11 | # Copyright Iowa State University 2009-2010. 12 | # Distributed under the Boost Software License, Version 1.0. 13 | # (See accompanying file LICENSE_1_0.txt or copy at 14 | # http://www.boost.org/LICENSE_1_0.txt) 15 | 16 | set(HEAD_HASH) 17 | 18 | file(READ "@HEAD_FILE@" HEAD_CONTENTS LIMIT 1024) 19 | 20 | string(STRIP "${HEAD_CONTENTS}" HEAD_CONTENTS) 21 | if(HEAD_CONTENTS MATCHES "ref") 22 | # named branch 23 | string(REPLACE "ref: " "" HEAD_REF "${HEAD_CONTENTS}") 24 | if(EXISTS "@GIT_DIR@/${HEAD_REF}") 25 | configure_file("@GIT_DIR@/${HEAD_REF}" "@GIT_DATA@/head-ref" COPYONLY) 26 | elseif(EXISTS "@GIT_DIR@/logs/${HEAD_REF}") 27 | configure_file("@GIT_DIR@/logs/${HEAD_REF}" "@GIT_DATA@/head-ref" COPYONLY) 28 | set(HEAD_HASH "${HEAD_REF}") 29 | endif() 30 | else() 31 | # detached HEAD 32 | configure_file("@GIT_DIR@/HEAD" "@GIT_DATA@/head-ref" COPYONLY) 33 | endif() 34 | 35 | if(NOT HEAD_HASH) 36 | file(READ "@GIT_DATA@/head-ref" HEAD_HASH LIMIT 1024) 37 | string(STRIP "${HEAD_HASH}" HEAD_HASH) 38 | endif() 39 | -------------------------------------------------------------------------------- /README-ecc.md: -------------------------------------------------------------------------------- 1 | ECC Support 2 | =========== 3 | 4 | include/fc/crypto/elliptic.hpp defines an interface for some cryptographic 5 | wrapper classes handling elliptic curve cryptography. 6 | 7 | Three implementations of this interface exist. One is based on OpenSSL, the 8 | others are based on libsecp256k1 (see https://github.com/bitcoin/secp256k1 ). 9 | The implementation to be used is selected at compile time using the 10 | cmake variable "ECC_IMPL". It can take one of three values, openssl or 11 | secp256k1 or mixed . 12 | The default is "openssl". The alternatives can be configured when invoking 13 | cmake, for example 14 | 15 | cmake -D ECC_IMPL=secp256k1 . 16 | 17 | If secp256k1 or mixed is chosen, the secp256k1 library and its include file 18 | must already be installed in the appropriate library / include directories on 19 | your system. 20 | 21 | 22 | Testing 23 | ------- 24 | 25 | Type "make ecc_test" to build the ecc_test executable from 26 | tests/crypto/ecc_test.cpp with the currently configured ECC implementation. 27 | 28 | ecc_test expects two arguments: 29 | 30 | ecc_test 31 | 32 | is a somewhat arbitrary password used for testing. 33 | 34 | is a data file containing intermediate test results. 35 | If the file does not exist, it will be created and intermediate results from 36 | the current ECC backend are written to it. 37 | If the file does exist, intermediate results from the current ECC backend 38 | are compared with the file contents. 39 | 40 | For a full round of interoperability testing, you can use the script 41 | tests/crypto/ecc-interop.sh . 42 | None of the test runs should produce any output. 43 | 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | fc 2 | == 3 | 4 | FC stands for fast-compiling c++ library and provides a set of utility libraries useful 5 | for the development of asynchronous libraries. Some of the highlights include: 6 | 7 | - Cooperative Multi-Tasking Library with support for Futures, mutexes, signals. 8 | - Wrapper on Boost ASIO for handling async opperations cooperatively with easy to code synchronous style. 9 | - Reflection for C++ allowing automatic serialization in Json & Binary of C++ Structs 10 | - Automatic generation of client / server stubs for reflected interfaces with support for JSON-RPC 11 | - Cryptographic Primitives for a variaty of hashes and encryption algorithms 12 | - Logging Infrastructure 13 | - Wraps many Boost APIs, such as boost::filesystem, boost::thread, and boost::exception to acceleate compiles 14 | - Support for unofficial Boost.Process library. 15 | -------------------------------------------------------------------------------- /include/fc/aligned.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | namespace fc { 3 | 4 | template 5 | struct aligned { 6 | union { 7 | T _align; 8 | char _data[S]; 9 | } _store; 10 | operator char*() { return _store._data; } 11 | operator const char*()const { return _store._data; } 12 | }; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /include/fc/any.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | namespace fc { 5 | // TODO: define this without using boost 6 | typedef boost::any any; 7 | } 8 | -------------------------------------------------------------------------------- /include/fc/bitutil.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | namespace fc { 5 | 6 | inline uint64_t endian_reverse_u64( uint64_t x ) 7 | { 8 | return (((x >> 0x38) & 0xFF) ) 9 | | (((x >> 0x30) & 0xFF) << 0x08) 10 | | (((x >> 0x28) & 0xFF) << 0x10) 11 | | (((x >> 0x20) & 0xFF) << 0x18) 12 | | (((x >> 0x18) & 0xFF) << 0x20) 13 | | (((x >> 0x10) & 0xFF) << 0x28) 14 | | (((x >> 0x08) & 0xFF) << 0x30) 15 | | (((x ) & 0xFF) << 0x38) 16 | ; 17 | } 18 | 19 | inline uint32_t endian_reverse_u32( uint32_t x ) 20 | { 21 | return (((x >> 0x18) & 0xFF) ) 22 | | (((x >> 0x10) & 0xFF) << 0x08) 23 | | (((x >> 0x08) & 0xFF) << 0x10) 24 | | (((x ) & 0xFF) << 0x18) 25 | ; 26 | } 27 | 28 | } // namespace fc 29 | -------------------------------------------------------------------------------- /include/fc/compress/smaz.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | namespace fc { 5 | 6 | std::string smaz_compress( const std::string& in ); 7 | std::string smaz_decompress( const std::string& compressed ); 8 | 9 | } // namespace fc 10 | -------------------------------------------------------------------------------- /include/fc/compress/zlib.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | namespace fc 6 | { 7 | 8 | string zlib_compress(const string& in); 9 | 10 | } // namespace fc 11 | -------------------------------------------------------------------------------- /include/fc/container/deque.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | namespace fc { 7 | namespace raw { 8 | 9 | 10 | } // namespace raw 11 | 12 | } // namespace fc 13 | -------------------------------------------------------------------------------- /include/fc/container/deque_fwd.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | namespace fc { 6 | 7 | namespace raw { 8 | template 9 | void pack( Stream& s, const std::deque& value ); 10 | template 11 | void unpack( Stream& s, std::deque& value ); 12 | } 13 | } // namespace fc 14 | -------------------------------------------------------------------------------- /include/fc/container/flat_fwd.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | namespace fc { 6 | 7 | using boost::container::flat_map; 8 | using boost::container::flat_set; 9 | 10 | namespace raw { 11 | template 12 | void pack( Stream& s, const flat_set& value ); 13 | template 14 | void unpack( Stream& s, flat_set& value ); 15 | template 16 | void pack( Stream& s, const flat_map& value ); 17 | template 18 | void unpack( Stream& s, flat_map& value ) ; 19 | } // namespace raw 20 | 21 | } // fc 22 | -------------------------------------------------------------------------------- /include/fc/crypto/aes.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | namespace fc { 9 | class path; 10 | 11 | class aes_encoder 12 | { 13 | public: 14 | aes_encoder(); 15 | ~aes_encoder(); 16 | 17 | void init( const fc::sha256& key, const fc::uint128& init_value ); 18 | uint32_t encode( const char* plaintxt, uint32_t len, char* ciphertxt ); 19 | // uint32_t final_encode( char* ciphertxt ); 20 | 21 | private: 22 | struct impl; 23 | fc::fwd my; 24 | }; 25 | class aes_decoder 26 | { 27 | public: 28 | aes_decoder(); 29 | ~aes_decoder(); 30 | 31 | void init( const fc::sha256& key, const fc::uint128& init_value ); 32 | uint32_t decode( const char* ciphertxt, uint32_t len, char* plaintext ); 33 | // uint32_t final_decode( char* plaintext ); 34 | 35 | private: 36 | struct impl; 37 | fc::fwd my; 38 | }; 39 | 40 | unsigned aes_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, 41 | unsigned char *iv, unsigned char *ciphertext); 42 | unsigned aes_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, 43 | unsigned char *iv, unsigned char *plaintext); 44 | unsigned aes_cfb_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, 45 | unsigned char *iv, unsigned char *plaintext); 46 | 47 | std::vector aes_encrypt( const fc::sha512& key, const std::vector& plain_text ); 48 | std::vector aes_decrypt( const fc::sha512& key, const std::vector& cipher_text ); 49 | 50 | /** encrypts plain_text and then includes a checksum that enables us to verify the integrety of 51 | * the file / key prior to decryption. 52 | */ 53 | void aes_save( const fc::path& file, const fc::sha512& key, std::vector plain_text ); 54 | 55 | /** 56 | * recovers the plain_text saved via aes_save() 57 | */ 58 | std::vector aes_load( const fc::path& file, const fc::sha512& key ); 59 | 60 | } // namespace fc 61 | -------------------------------------------------------------------------------- /include/fc/crypto/base32.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | namespace fc 6 | { 7 | std::vector from_base32( const fc::string& b32 ); 8 | fc::string to_base32( const std::vector& vec ); 9 | fc::string to_base32( const char* data, size_t len ); 10 | } 11 | -------------------------------------------------------------------------------- /include/fc/crypto/base36.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | namespace fc 6 | { 7 | std::vector from_base36( const fc::string& b36 ); 8 | fc::string to_base36( const std::vector& vec ); 9 | fc::string to_base36( const char* data, size_t len ); 10 | } 11 | -------------------------------------------------------------------------------- /include/fc/crypto/base58.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | namespace fc { 6 | std::string to_base58( const char* d, size_t s ); 7 | std::string to_base58( const std::vector& data ); 8 | std::vector from_base58( const std::string& base58_str ); 9 | size_t from_base58( const std::string& base58_str, char* out_data, size_t out_data_len ); 10 | } 11 | -------------------------------------------------------------------------------- /include/fc/crypto/base64.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | namespace fc { 5 | std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len); 6 | inline std::string base64_encode(char const* bytes_to_encode, unsigned int in_len) { return base64_encode( (unsigned char const*)bytes_to_encode, in_len); } 7 | std::string base64_encode( const std::string& enc ); 8 | std::string base64_decode( const std::string& encoded_string); 9 | } // namespace fc 10 | -------------------------------------------------------------------------------- /include/fc/crypto/bigint.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | 6 | struct bignum_st; 7 | typedef bignum_st BIGNUM; 8 | 9 | namespace fc { 10 | class bigint { 11 | public: 12 | bigint( const std::vector& bige ); 13 | bigint( const char* bige, uint32_t l ); 14 | bigint(uint64_t value); 15 | bigint( ); 16 | bigint( const bigint& c ); 17 | bigint( bigint&& c ); 18 | explicit bigint( BIGNUM* n ); 19 | ~bigint(); 20 | 21 | bigint& operator = ( const bigint& a ); 22 | bigint& operator = ( bigint&& a ); 23 | 24 | explicit operator bool()const; 25 | 26 | bool is_negative()const; 27 | int64_t to_int64()const; 28 | 29 | int64_t log2()const; 30 | bigint exp( const bigint& c )const; 31 | 32 | static bigint random( uint32_t bits, int t, int ); 33 | 34 | bool operator < ( const bigint& c )const; 35 | bool operator > ( const bigint& c )const; 36 | bool operator >= ( const bigint& c )const; 37 | bool operator == ( const bigint& c )const; 38 | bool operator != ( const bigint& c )const; 39 | 40 | bigint operator + ( const bigint& a )const; 41 | bigint operator * ( const bigint& a )const; 42 | bigint operator / ( const bigint& a )const; 43 | bigint operator % ( const bigint& a )const; 44 | bigint operator /= ( const bigint& a ); 45 | bigint operator *= ( const bigint& a ); 46 | bigint& operator += ( const bigint& a ); 47 | bigint& operator -= ( const bigint& a ); 48 | bigint& operator <<= ( uint32_t i ); 49 | bigint& operator >>= ( uint32_t i ); 50 | bigint operator - ( const bigint& a )const; 51 | 52 | 53 | bigint operator++(int); 54 | bigint& operator++(); 55 | bigint operator--(int); 56 | bigint& operator--(); 57 | 58 | operator fc::string()const; 59 | 60 | // returns bignum as bigendian bytes 61 | operator std::vector()const; 62 | 63 | BIGNUM* dup()const; 64 | 65 | BIGNUM* get()const { return n; } 66 | private: 67 | BIGNUM* n; 68 | }; 69 | 70 | class variant; 71 | /** encodes the big int as base64 string, or a number */ 72 | void to_variant( const bigint& bi, variant& v ); 73 | /** decodes the big int as base64 string, or a number */ 74 | void from_variant( const variant& v, bigint& bi ); 75 | } // namespace fc 76 | 77 | -------------------------------------------------------------------------------- /include/fc/crypto/dh.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | 6 | namespace fc { 7 | 8 | struct diffie_hellman { 9 | diffie_hellman():valid(0),g(5){ fc::init_openssl(); } 10 | bool generate_params( int s, uint8_t g ); 11 | bool generate_pub_key(); 12 | bool compute_shared_key( const char* buf, uint32_t s ); 13 | bool compute_shared_key( const std::vector& pubk); 14 | bool validate(); 15 | 16 | std::vector p; 17 | std::vector pub_key; 18 | std::vector priv_key; 19 | std::vector shared_key; 20 | bool valid; 21 | uint8_t g; 22 | }; 23 | 24 | } // namespace fc 25 | 26 | 27 | -------------------------------------------------------------------------------- /include/fc/crypto/digest.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | 6 | namespace fc { 7 | 8 | template 9 | fc::sha256 digest( const T& value ) 10 | { 11 | fc::sha256::encoder enc; 12 | fc::raw::pack( enc, value ); 13 | return enc.result(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /include/fc/crypto/hex.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | 6 | namespace fc { 7 | uint8_t from_hex( char c ); 8 | fc::string to_hex( const char* d, uint32_t s ); 9 | std::string to_hex( const std::vector& data ); 10 | 11 | /** 12 | * @return the number of bytes decoded 13 | */ 14 | size_t from_hex( const fc::string& hex_str, char* out_data, size_t out_data_len ); 15 | } 16 | -------------------------------------------------------------------------------- /include/fc/crypto/hmac.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * File: hmac.hpp 3 | * Author: Peter Conrad 4 | * 5 | * Created on 1. Juli 2015, 21:48 6 | */ 7 | 8 | #ifndef HMAC_HPP 9 | #define HMAC_HPP 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | namespace fc { 16 | 17 | template 18 | class hmac 19 | { 20 | public: 21 | hmac() {} 22 | 23 | H digest( const char* c, uint32_t c_len, const char* d, uint32_t d_len ) 24 | { 25 | encoder.reset(); 26 | add_key(c, c_len, 0x36); 27 | encoder.write( d, d_len ); 28 | H intermediate = encoder.result(); 29 | 30 | encoder.reset(); 31 | add_key(c, c_len, 0x5c); 32 | encoder.write( intermediate.data(), intermediate.data_size() ); 33 | return encoder.result(); 34 | } 35 | 36 | private: 37 | void add_key( const char* c, const uint32_t c_len, char pad ) 38 | { 39 | if ( c_len > internal_block_size() ) 40 | { 41 | H hash = H::hash( c, c_len ); 42 | add_key( hash.data(), hash.data_size(), pad ); 43 | } 44 | else 45 | for (unsigned int i = 0; i < internal_block_size(); i++ ) 46 | { 47 | encoder.put( pad ^ ((i < c_len) ? *c++ : 0) ); 48 | } 49 | } 50 | 51 | unsigned int internal_block_size() const; 52 | 53 | H dummy; 54 | typename H::encoder encoder; 55 | }; 56 | 57 | typedef hmac hmac_sha224; 58 | typedef hmac hmac_sha256; 59 | typedef hmac hmac_sha512; 60 | } 61 | 62 | #endif /* HMAC_HPP */ 63 | 64 | -------------------------------------------------------------------------------- /include/fc/crypto/openssl.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | /** 13 | * @file openssl.hpp 14 | * Provides common utility calls for wrapping openssl c api. 15 | */ 16 | namespace fc 17 | { 18 | class path; 19 | 20 | template 21 | struct ssl_wrapper 22 | { 23 | ssl_wrapper(ssl_type* obj):obj(obj) {} 24 | 25 | operator ssl_type*() { return obj; } 26 | operator const ssl_type*() const { return obj; } 27 | ssl_type* operator->() { return obj; } 28 | const ssl_type* operator->() const { return obj; } 29 | 30 | ssl_type* obj; 31 | }; 32 | 33 | #define SSL_TYPE(name, ssl_type, free_func) \ 34 | struct name : public ssl_wrapper \ 35 | { \ 36 | name(ssl_type* obj=nullptr) \ 37 | : ssl_wrapper(obj) {} \ 38 | ~name() \ 39 | { \ 40 | if( obj != nullptr ) \ 41 | free_func(obj); \ 42 | } \ 43 | }; 44 | 45 | SSL_TYPE(ec_group, EC_GROUP, EC_GROUP_free) 46 | SSL_TYPE(ec_point, EC_POINT, EC_POINT_free) 47 | SSL_TYPE(ecdsa_sig, ECDSA_SIG, ECDSA_SIG_free) 48 | SSL_TYPE(bn_ctx, BN_CTX, BN_CTX_free) 49 | SSL_TYPE(evp_cipher_ctx, EVP_CIPHER_CTX, EVP_CIPHER_CTX_free ) 50 | 51 | /** allocates a bignum by default.. */ 52 | struct ssl_bignum : public ssl_wrapper 53 | { 54 | ssl_bignum() : ssl_wrapper(BN_new()) {} 55 | ~ssl_bignum() { BN_free(obj); } 56 | }; 57 | 58 | /** Allows to explicitly specify OpenSSL configuration file path to be loaded at OpenSSL library init. 59 | If not set OpenSSL will try to load the conf. file (openssl.cnf) from the path it was 60 | configured with what caused serious Keyhotee startup bugs on some Win7, Win8 machines. 61 | \warning to be effective this method should be used before any part using OpenSSL, especially 62 | before init_openssl call 63 | */ 64 | void store_configuration_path(const path& filePath); 65 | int init_openssl(); 66 | 67 | } // namespace fc 68 | -------------------------------------------------------------------------------- /include/fc/crypto/rand.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace fc { 4 | 5 | /* provides access to the OpenSSL random number generator */ 6 | void rand_bytes(char* buf, int count); 7 | void rand_pseudo_bytes(char* buf, int count); 8 | } // namespace fc 9 | -------------------------------------------------------------------------------- /include/fc/crypto/ripemd160.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | namespace fc{ 8 | class sha512; 9 | class sha256; 10 | 11 | class ripemd160 12 | { 13 | public: 14 | ripemd160(); 15 | explicit ripemd160( const string& hex_str ); 16 | 17 | string str()const; 18 | explicit operator string()const; 19 | 20 | char* data()const; 21 | size_t data_size()const { return 160/8; } 22 | 23 | static ripemd160 hash( const fc::sha512& h ); 24 | static ripemd160 hash( const fc::sha256& h ); 25 | static ripemd160 hash( const char* d, uint32_t dlen ); 26 | static ripemd160 hash( const string& ); 27 | 28 | template 29 | static ripemd160 hash( const T& t ) 30 | { 31 | ripemd160::encoder e; 32 | fc::raw::pack(e,t); 33 | return e.result(); 34 | } 35 | 36 | class encoder 37 | { 38 | public: 39 | encoder(); 40 | ~encoder(); 41 | 42 | void write( const char* d, uint32_t dlen ); 43 | void put( char c ) { write( &c, 1 ); } 44 | void reset(); 45 | ripemd160 result(); 46 | 47 | private: 48 | struct impl; 49 | fc::fwd my; 50 | }; 51 | 52 | template 53 | inline friend T& operator<<( T& ds, const ripemd160& ep ) { 54 | ds.write( ep.data(), sizeof(ep) ); 55 | return ds; 56 | } 57 | 58 | template 59 | inline friend T& operator>>( T& ds, ripemd160& ep ) { 60 | ds.read( ep.data(), sizeof(ep) ); 61 | return ds; 62 | } 63 | friend ripemd160 operator << ( const ripemd160& h1, uint32_t i ); 64 | friend bool operator == ( const ripemd160& h1, const ripemd160& h2 ); 65 | friend bool operator != ( const ripemd160& h1, const ripemd160& h2 ); 66 | friend ripemd160 operator ^ ( const ripemd160& h1, const ripemd160& h2 ); 67 | friend bool operator >= ( const ripemd160& h1, const ripemd160& h2 ); 68 | friend bool operator > ( const ripemd160& h1, const ripemd160& h2 ); 69 | friend bool operator < ( const ripemd160& h1, const ripemd160& h2 ); 70 | 71 | uint32_t _hash[5]; 72 | }; 73 | 74 | class variant; 75 | void to_variant( const ripemd160& bi, variant& v ); 76 | void from_variant( const variant& v, ripemd160& bi ); 77 | 78 | typedef ripemd160 uint160_t; 79 | typedef ripemd160 uint160; 80 | 81 | template<> struct get_typename { static const char* name() { return "uint160_t"; } }; 82 | 83 | } // namespace fc 84 | 85 | namespace std 86 | { 87 | template<> 88 | struct hash 89 | { 90 | size_t operator()( const fc::ripemd160& s )const 91 | { 92 | return *((size_t*)&s); 93 | } 94 | }; 95 | } 96 | -------------------------------------------------------------------------------- /include/fc/crypto/sha1.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | namespace fc{ 6 | 7 | class sha1 8 | { 9 | public: 10 | sha1(); 11 | explicit sha1( const string& hex_str ); 12 | 13 | string str()const; 14 | operator string()const; 15 | 16 | char* data()const; 17 | size_t data_size()const { return 20; } 18 | 19 | static sha1 hash( const char* d, uint32_t dlen ); 20 | static sha1 hash( const string& ); 21 | 22 | template 23 | static sha1 hash( const T& t ) 24 | { 25 | sha1::encoder e; 26 | e << t; 27 | return e.result(); 28 | } 29 | 30 | class encoder 31 | { 32 | public: 33 | encoder(); 34 | ~encoder(); 35 | 36 | void write( const char* d, uint32_t dlen ); 37 | void put( char c ) { write( &c, 1 ); } 38 | void reset(); 39 | sha1 result(); 40 | 41 | private: 42 | struct impl; 43 | fc::fwd my; 44 | }; 45 | 46 | template 47 | inline friend T& operator<<( T& ds, const sha1& ep ) { 48 | ds.write( ep.data(), sizeof(ep) ); 49 | return ds; 50 | } 51 | 52 | template 53 | inline friend T& operator>>( T& ds, sha1& ep ) { 54 | ds.read( ep.data(), sizeof(ep) ); 55 | return ds; 56 | } 57 | friend sha1 operator << ( const sha1& h1, uint32_t i ); 58 | friend bool operator == ( const sha1& h1, const sha1& h2 ); 59 | friend bool operator != ( const sha1& h1, const sha1& h2 ); 60 | friend sha1 operator ^ ( const sha1& h1, const sha1& h2 ); 61 | friend bool operator >= ( const sha1& h1, const sha1& h2 ); 62 | friend bool operator > ( const sha1& h1, const sha1& h2 ); 63 | friend bool operator < ( const sha1& h1, const sha1& h2 ); 64 | 65 | uint32_t _hash[5]; 66 | }; 67 | 68 | class variant; 69 | void to_variant( const sha1& bi, variant& v ); 70 | void from_variant( const variant& v, sha1& bi ); 71 | 72 | } // namespace fc 73 | 74 | namespace std 75 | { 76 | template<> 77 | struct hash 78 | { 79 | size_t operator()( const fc::sha1& s )const 80 | { 81 | return *((size_t*)&s); 82 | } 83 | }; 84 | } 85 | -------------------------------------------------------------------------------- /include/fc/crypto/sha224.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | namespace fc 8 | { 9 | 10 | class sha224 11 | { 12 | public: 13 | sha224(); 14 | explicit sha224( const string& hex_str ); 15 | 16 | string str()const; 17 | operator string()const; 18 | 19 | char* data()const; 20 | size_t data_size()const { return 224 / 8; } 21 | 22 | static sha224 hash( const char* d, uint32_t dlen ); 23 | static sha224 hash( const string& ); 24 | 25 | template 26 | static sha224 hash( const T& t ) 27 | { 28 | sha224::encoder e; 29 | fc::raw::pack(e,t); 30 | return e.result(); 31 | } 32 | 33 | class encoder 34 | { 35 | public: 36 | encoder(); 37 | ~encoder(); 38 | 39 | void write( const char* d, uint32_t dlen ); 40 | void put( char c ) { write( &c, 1 ); } 41 | void reset(); 42 | sha224 result(); 43 | 44 | private: 45 | struct impl; 46 | fc::fwd my; 47 | }; 48 | 49 | template 50 | inline friend T& operator<<( T& ds, const sha224& ep ) { 51 | static_assert( sizeof(ep) == (8*3+4), "sha224 size mismatch" ); 52 | ds.write( ep.data(), sizeof(ep) ); 53 | return ds; 54 | } 55 | 56 | template 57 | inline friend T& operator>>( T& ds, sha224& ep ) { 58 | ds.read( ep.data(), sizeof(ep) ); 59 | return ds; 60 | } 61 | friend sha224 operator << ( const sha224& h1, uint32_t i ); 62 | friend bool operator == ( const sha224& h1, const sha224& h2 ); 63 | friend bool operator != ( const sha224& h1, const sha224& h2 ); 64 | friend sha224 operator ^ ( const sha224& h1, const sha224& h2 ); 65 | friend bool operator >= ( const sha224& h1, const sha224& h2 ); 66 | friend bool operator > ( const sha224& h1, const sha224& h2 ); 67 | friend bool operator < ( const sha224& h1, const sha224& h2 ); 68 | friend std::size_t hash_value( const sha224& v ) { return uint64_t(v._hash[1])<<32 | v._hash[2]; } 69 | 70 | uint32_t _hash[7]; 71 | }; 72 | 73 | class variant; 74 | void to_variant( const sha224& bi, variant& v ); 75 | void from_variant( const variant& v, sha224& bi ); 76 | 77 | } // fc 78 | namespace std 79 | { 80 | template<> 81 | struct hash 82 | { 83 | size_t operator()( const fc::sha224& s )const 84 | { 85 | return *((size_t*)&s); 86 | } 87 | }; 88 | } 89 | #include 90 | FC_REFLECT_TYPENAME( fc::sha224 ) 91 | -------------------------------------------------------------------------------- /include/fc/crypto/sha512.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | namespace fc 6 | { 7 | 8 | class sha512 9 | { 10 | public: 11 | sha512(); 12 | explicit sha512( const string& hex_str ); 13 | 14 | string str()const; 15 | operator string()const; 16 | 17 | char* data()const; 18 | size_t data_size()const { return 512 / 8; } 19 | 20 | static sha512 hash( const char* d, uint32_t dlen ); 21 | static sha512 hash( const string& ); 22 | 23 | template 24 | static sha512 hash( const T& t ) 25 | { 26 | sha512::encoder e; 27 | e << t; 28 | return e.result(); 29 | } 30 | 31 | class encoder 32 | { 33 | public: 34 | encoder(); 35 | ~encoder(); 36 | 37 | void write( const char* d, uint32_t dlen ); 38 | void put( char c ) { write( &c, 1 ); } 39 | void reset(); 40 | sha512 result(); 41 | 42 | private: 43 | struct impl; 44 | fc::fwd my; 45 | }; 46 | 47 | template 48 | inline friend T& operator<<( T& ds, const sha512& ep ) { 49 | ds.write( ep.data(), sizeof(ep) ); 50 | return ds; 51 | } 52 | 53 | template 54 | inline friend T& operator>>( T& ds, sha512& ep ) { 55 | ds.read( ep.data(), sizeof(ep) ); 56 | return ds; 57 | } 58 | friend sha512 operator << ( const sha512& h1, uint32_t i ); 59 | friend bool operator == ( const sha512& h1, const sha512& h2 ); 60 | friend bool operator != ( const sha512& h1, const sha512& h2 ); 61 | friend sha512 operator ^ ( const sha512& h1, const sha512& h2 ); 62 | friend bool operator >= ( const sha512& h1, const sha512& h2 ); 63 | friend bool operator > ( const sha512& h1, const sha512& h2 ); 64 | friend bool operator < ( const sha512& h1, const sha512& h2 ); 65 | 66 | uint64_t _hash[8]; 67 | }; 68 | 69 | typedef fc::sha512 uint512; 70 | 71 | class variant; 72 | void to_variant( const sha512& bi, variant& v ); 73 | void from_variant( const variant& v, sha512& bi ); 74 | 75 | } // fc 76 | 77 | #include 78 | FC_REFLECT_TYPENAME( fc::sha512 ) 79 | -------------------------------------------------------------------------------- /include/fc/fwd.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | namespace fc { 5 | 6 | /** 7 | * @brief Used to forward declare value types. 8 | * 9 | */ 10 | template 11 | class fwd { 12 | public: 13 | template fwd( U&& u ); 14 | template fwd( U&& u, V&& v ); 15 | template fwd( U&& u, V&& v, X&&, Y&& ); 16 | fwd(); 17 | 18 | fwd( const fwd& f ); 19 | fwd( fwd&& f ); 20 | 21 | operator const T&()const; 22 | operator T&(); 23 | 24 | T& operator*(); 25 | const T& operator*()const; 26 | const T* operator->()const; 27 | 28 | T* operator->(); 29 | bool operator !()const; 30 | 31 | template 32 | T& operator = ( U&& u ); 33 | 34 | T& operator = ( fwd&& u ); 35 | T& operator = ( const fwd& u ); 36 | 37 | ~fwd(); 38 | 39 | private: 40 | aligned _store; 41 | }; 42 | 43 | 44 | } // namespace fc 45 | 46 | -------------------------------------------------------------------------------- /include/fc/git_revision.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | namespace fc { 5 | 6 | extern const char* const git_revision_sha; 7 | extern const uint32_t git_revision_unix_timestamp; 8 | 9 | } // end namespace fc 10 | -------------------------------------------------------------------------------- /include/fc/interprocess/file_mapping.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | namespace boost { 6 | namespace interprocess { 7 | class file_mapping; 8 | class mapped_region; 9 | } 10 | } 11 | namespace fc { 12 | enum mode_t { 13 | read_only, 14 | write_only, 15 | read_write 16 | }; 17 | 18 | class file_mapping { 19 | public: 20 | file_mapping( const char* file, mode_t ); 21 | ~file_mapping(); 22 | private: 23 | friend class mapped_region; 24 | #ifdef _WIN64 25 | fc::fwd my; 26 | #else 27 | fc::fwd my; 28 | #endif 29 | }; 30 | 31 | class mapped_region { 32 | public: 33 | mapped_region( const file_mapping& fm, mode_t m, uint64_t start, size_t size ); 34 | mapped_region( const file_mapping& fm, mode_t m ); 35 | ~mapped_region(); 36 | void flush(); 37 | void* get_address()const; 38 | size_t get_size()const; 39 | private: 40 | fc::fwd my; 41 | }; 42 | } 43 | -------------------------------------------------------------------------------- /include/fc/interprocess/iprocess.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | namespace fc 9 | { 10 | /** 11 | * @brief abstract interface for interacting with external processes 12 | * 13 | * At the very least we have ssh::process and direct child processes, and 14 | * there may be other processes that need to implement this protocol. 15 | */ 16 | class iprocess 17 | { 18 | public: 19 | enum exec_opts { 20 | open_none = 0, 21 | open_stdin = 0x01, 22 | open_stdout = 0x02, 23 | open_stderr = 0x04, 24 | open_all = open_stdin|open_stdout|open_stderr, 25 | suppress_console = 0x08 26 | }; 27 | 28 | virtual ~iprocess(){} 29 | 30 | /** 31 | * 32 | * @return *this 33 | */ 34 | virtual iprocess& exec( const path& exe, std::vector args, 35 | const path& work_dir = path(), int opts = open_all ) = 0; 36 | 37 | /** 38 | * @return blocks until the process exits 39 | */ 40 | virtual int result(const microseconds& timeout = microseconds::maximum()) = 0; 41 | 42 | 43 | /** 44 | * Forcefully kills the process. 45 | */ 46 | virtual void kill() = 0; 47 | 48 | /** 49 | * @brief returns a stream that writes to the process' stdin 50 | */ 51 | virtual buffered_ostream_ptr in_stream() = 0; 52 | 53 | /** 54 | * @brief returns a stream that reads from the process' stdout 55 | */ 56 | virtual buffered_istream_ptr out_stream() = 0; 57 | /** 58 | * @brief returns a stream that reads from the process' stderr 59 | */ 60 | virtual buffered_istream_ptr err_stream() = 0; 61 | 62 | }; 63 | 64 | typedef std::shared_ptr iprocess_ptr; 65 | 66 | 67 | } // namespace fc 68 | -------------------------------------------------------------------------------- /include/fc/interprocess/mmap_struct.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | namespace fc 6 | { 7 | class path; 8 | namespace detail 9 | { 10 | /** 11 | * Base class used to hide common implementation details. 12 | */ 13 | class mmap_struct_base 14 | { 15 | public: 16 | size_t size()const; 17 | void flush(); 18 | 19 | protected: 20 | void open( const fc::path& file, size_t s, bool create ); 21 | std::unique_ptr _file_mapping; 22 | std::unique_ptr _mapped_region; 23 | }; 24 | }; 25 | 26 | /** 27 | * @class mmap_struct 28 | * @brief A struct that has been mapped from a file. 29 | * 30 | * @note T must be POD 31 | */ 32 | template 33 | class mmap_struct : public detail::mmap_struct_base 34 | { 35 | public: 36 | mmap_struct():_mapped_struct(nullptr){} 37 | /** 38 | * Create the file if it does not exist or is of the wrong size if create is true, then maps 39 | * the file to memory. 40 | * 41 | * @throw an exception if the file does not exist or is the wrong size and create is false 42 | */ 43 | void open( const fc::path& file, bool create = false ) 44 | { 45 | detail::mmap_struct_base::open( file, sizeof(T), create ); 46 | _mapped_struct = (T*)_mapped_region->get_address(); 47 | } 48 | 49 | T* operator->() { return _mapped_struct; } 50 | const T* operator->()const { return _mapped_struct; } 51 | 52 | T& operator*() { return *_mapped_struct; } 53 | const T& operator*()const { return *_mapped_struct; } 54 | 55 | private: 56 | T* _mapped_struct; 57 | }; 58 | 59 | } 60 | -------------------------------------------------------------------------------- /include/fc/interprocess/process.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | namespace fc { 5 | 6 | fc::path find_executable_in_path( const fc::string name ); 7 | 8 | /** 9 | * @brief start and manage an local process 10 | * @note this class implements reference semantics. 11 | */ 12 | class process : public iprocess 13 | { 14 | public: 15 | process(); 16 | ~process(); 17 | 18 | virtual iprocess& exec( const fc::path& exe, 19 | std::vector args, 20 | const fc::path& work_dir = fc::path(), 21 | int opts = open_all ); 22 | 23 | 24 | virtual int result(const microseconds& timeout = microseconds::maximum()); 25 | virtual void kill(); 26 | virtual fc::buffered_ostream_ptr in_stream(); 27 | virtual fc::buffered_istream_ptr out_stream(); 28 | virtual fc::buffered_istream_ptr err_stream(); 29 | 30 | class impl; 31 | private: 32 | std::unique_ptr my; 33 | }; 34 | 35 | typedef std::shared_ptr process_ptr; 36 | 37 | } // namespace fc 38 | -------------------------------------------------------------------------------- /include/fc/interprocess/signals.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | namespace fc 5 | { 6 | /// handler will be called from ASIO thread 7 | void set_signal_handler( std::function handler, int signal_num ); 8 | } 9 | -------------------------------------------------------------------------------- /include/fc/io/buffered_iostream.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | namespace fc 5 | { 6 | namespace detail 7 | { 8 | class buffered_istream_impl; 9 | class buffered_ostream_impl; 10 | } 11 | 12 | /** 13 | * @brief Reads data from an unbuffered stream 14 | * and enables peek functionality. 15 | */ 16 | class buffered_istream : public virtual istream 17 | { 18 | public: 19 | buffered_istream( istream_ptr is ); 20 | buffered_istream( buffered_istream&& o ); 21 | 22 | buffered_istream& operator=( buffered_istream&& i ); 23 | 24 | virtual ~buffered_istream(); 25 | 26 | /** read at least 1 byte or throw, if no data is available 27 | * this method should block cooperatively until data is 28 | * available or fc::eof_exception is thrown. 29 | * 30 | * @pre len > 0 31 | * @pre buf != nullptr 32 | * @throws fc::eof if at least 1 byte cannot be read 33 | **/ 34 | virtual std::size_t readsome( char* buf, std::size_t len ); 35 | virtual size_t readsome( const std::shared_ptr& buf, size_t len, size_t offset ); 36 | 37 | /** 38 | * This method may block until at least 1 character is 39 | * available. 40 | */ 41 | virtual char peek() const; 42 | 43 | private: 44 | std::unique_ptr my; 45 | }; 46 | typedef std::shared_ptr buffered_istream_ptr; 47 | 48 | 49 | /** 50 | * 51 | */ 52 | class buffered_ostream : public virtual ostream 53 | { 54 | public: 55 | buffered_ostream( ostream_ptr o, size_t bufsize = 4096 ); 56 | buffered_ostream( buffered_ostream&& m ); 57 | ~buffered_ostream(); 58 | 59 | buffered_ostream& operator=( buffered_ostream&& m ); 60 | /** 61 | * This method will return immediately unless the buffer 62 | * is full, in which case it will flush which may block. 63 | */ 64 | virtual size_t writesome( const char* buf, size_t len ); 65 | virtual size_t writesome( const std::shared_ptr& buf, size_t len, size_t offset ); 66 | 67 | virtual void close(); 68 | virtual void flush(); 69 | private: 70 | std::unique_ptr my; 71 | }; 72 | typedef std::shared_ptr buffered_ostream_ptr; 73 | } 74 | -------------------------------------------------------------------------------- /include/fc/io/console.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | namespace fc 3 | { 4 | /** enables / disables echoing of console input, useful for 5 | * entering passwords on the console. 6 | */ 7 | void set_console_echo( bool enable_echo ); 8 | } // namespace fc 9 | -------------------------------------------------------------------------------- /include/fc/io/fstream.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | 6 | namespace fc { 7 | class path; 8 | class ofstream : virtual public ostream { 9 | public: 10 | enum mode { out, binary }; 11 | ofstream(); 12 | ofstream( const fc::path& file, int m = binary ); 13 | ~ofstream(); 14 | 15 | void open( const fc::path& file, int m = binary ); 16 | size_t writesome( const char* buf, size_t len ); 17 | size_t writesome(const std::shared_ptr& buffer, size_t len, size_t offset); 18 | void put( char c ); 19 | void close(); 20 | void flush(); 21 | 22 | private: 23 | class impl; 24 | fc::shared_ptr my; 25 | }; 26 | 27 | class ifstream : virtual public istream { 28 | public: 29 | enum mode { in, binary }; 30 | enum seekdir { beg, cur, end }; 31 | 32 | ifstream(); 33 | ifstream( const fc::path& file, int m = binary); 34 | ~ifstream(); 35 | 36 | void open( const fc::path& file, int m ); 37 | size_t readsome( char* buf, size_t len ); 38 | size_t readsome(const std::shared_ptr& buffer, size_t max, size_t offset); 39 | ifstream& read( char* buf, size_t len ); 40 | ifstream& seekg( size_t p, seekdir d = beg ); 41 | using istream::get; 42 | void get( char& c ) { read( &c, 1 ); } 43 | void close(); 44 | bool eof()const; 45 | private: 46 | class impl; 47 | fc::shared_ptr my; 48 | }; 49 | 50 | /** 51 | * Grab the full contents of a file into a string object. 52 | * NB reading a full file into memory is a poor choice 53 | * if the file may be very large. 54 | */ 55 | void read_file_contents( const fc::path& filename, std::string& result ); 56 | 57 | } // namespace fc 58 | -------------------------------------------------------------------------------- /include/fc/io/iobuffer.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | namespace fc 6 | { 7 | /** 8 | * Records the size, but discards the data. 9 | */ 10 | class size_stream : public virtual fc::ostream 11 | { 12 | public: 13 | size_stream( size_t s = 0):_size(s){} 14 | 15 | size_t size()const { return _size; } 16 | size_t seek( size_t pos ) { return _size = pos; } 17 | 18 | virtual size_t writesome( const char* /*ignored buf*/, size_t len ) 19 | { 20 | _size += len; 21 | return len; 22 | } 23 | 24 | virtual void close(){} 25 | virtual void flush(){} 26 | 27 | private: 28 | size_t _size; 29 | }; 30 | 31 | 32 | class iobuffer : public virtual fc::iostream 33 | { 34 | public: 35 | iobuffer( size_t s ) 36 | :_data(s){} 37 | 38 | size_t size()const { return _data.size(); } 39 | size_t pos()const { return _pos; } 40 | size_t seek( size_t pos ) 41 | { 42 | return _pos = std::min(_data.size(),pos); 43 | } 44 | 45 | virtual size_t readsome( char* buf, size_t len ) 46 | { 47 | auto avail = std::min( _data.size()-_pos, len ); 48 | if( avail == 0 ) throw fc::eof_exception(); 49 | memcpy( buf, _data.data()+_pos, avail ); 50 | _pos += avail; 51 | return avail; 52 | } 53 | /** 54 | * This method may block until at least 1 character is 55 | * available. 56 | */ 57 | char peek()const 58 | { 59 | if( _pos == _data.size() ) throw fc::eof_exception(); 60 | return _data[_pos]; 61 | } 62 | 63 | virtual size_t writesome( const char* buf, size_t len ) 64 | { 65 | auto avail = std::max( _data.size(), _pos + len ); 66 | _data.resize(avail); 67 | memcpy( _data.data()+_pos, buf, len ); 68 | _pos += avail; 69 | return avail; 70 | } 71 | char* data() { return _data.data(); } 72 | 73 | virtual void close(){} 74 | virtual void flush(){} 75 | 76 | private: 77 | std::vector _data; 78 | size_t _pos; 79 | }; 80 | 81 | } 82 | -------------------------------------------------------------------------------- /include/fc/io/raw_unpack_file.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | namespace fc 8 | { 9 | namespace raw 10 | { 11 | template 12 | void unpack_file( const fc::path& filename, T& obj ) 13 | { 14 | try { 15 | fc::file_mapping fmap( filename.generic_string().c_str(), fc::read_only); 16 | fc::mapped_region mapr( fmap, fc::read_only, 0, fc::file_size(filename) ); 17 | auto cs = (const char*)mapr.get_address(); 18 | 19 | fc::datastream ds( cs, mapr.get_size() ); 20 | fc::raw::unpack(ds,obj); 21 | } FC_RETHROW_EXCEPTIONS( info, "unpacking file ${file}", ("file",filename) ); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /include/fc/io/sstream.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | namespace fc { 6 | 7 | class stringstream : virtual public iostream { 8 | public: 9 | stringstream(); 10 | stringstream( fc::string& s); 11 | stringstream( const fc::string& s); 12 | ~stringstream(); 13 | 14 | fc::string str(); 15 | void str(const fc::string& s); 16 | 17 | void clear(); 18 | 19 | virtual bool eof()const; 20 | virtual size_t writesome( const char* buf, size_t len ); 21 | virtual size_t writesome( const std::shared_ptr& buf, size_t len, size_t offset ); 22 | virtual size_t readsome( char* buf, size_t len ); 23 | virtual size_t readsome( const std::shared_ptr& buf, size_t len, size_t offset ); 24 | virtual void close(); 25 | virtual void flush(); 26 | char peek(); 27 | 28 | private: 29 | class impl; 30 | fwd my; 31 | }; 32 | 33 | } 34 | -------------------------------------------------------------------------------- /include/fc/io/stdio.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | namespace fc 5 | { 6 | 7 | class cout_t : virtual public ostream { 8 | public: 9 | virtual size_t writesome( const char* buf, size_t len ); 10 | virtual size_t writesome( const std::shared_ptr& buf, size_t len, size_t offset ); 11 | virtual void close(); 12 | virtual void flush(); 13 | }; 14 | 15 | class cerr_t : virtual public ostream { 16 | public: 17 | virtual size_t writesome( const char* buf, size_t len ); 18 | virtual size_t writesome( const std::shared_ptr& buf, size_t len, size_t offset ); 19 | virtual void close(); 20 | virtual void flush(); 21 | }; 22 | 23 | class cin_t : virtual public istream { 24 | public: 25 | ~cin_t(); 26 | virtual size_t readsome( char* buf, size_t len ); 27 | virtual size_t readsome( const std::shared_ptr& buf, size_t len, size_t offset ); 28 | virtual istream& read( char* buf, size_t len ); 29 | virtual bool eof()const; 30 | }; 31 | 32 | extern cout_t& cout; 33 | extern cerr_t& cerr; 34 | extern cin_t& cin; 35 | 36 | extern std::shared_ptr cin_ptr; 37 | extern std::shared_ptr cout_ptr; 38 | extern std::shared_ptr cerr_ptr; 39 | } 40 | -------------------------------------------------------------------------------- /include/fc/log/appender.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | namespace fc { 6 | class appender; 7 | class log_message; 8 | class variant; 9 | 10 | class appender_factory : public fc::retainable { 11 | public: 12 | typedef fc::shared_ptr ptr; 13 | 14 | virtual ~appender_factory(){}; 15 | virtual fc::shared_ptr create( const variant& args ) = 0; 16 | }; 17 | 18 | namespace detail { 19 | template 20 | class appender_factory_impl : public appender_factory { 21 | public: 22 | virtual fc::shared_ptr create( const variant& args ) { 23 | return fc::shared_ptr(new T(args)); 24 | } 25 | }; 26 | } 27 | 28 | class appender : public fc::retainable { 29 | public: 30 | typedef fc::shared_ptr ptr; 31 | 32 | template 33 | static bool register_appender(const fc::string& type) { 34 | return register_appender( type, new detail::appender_factory_impl() ); 35 | } 36 | 37 | static appender::ptr create( const fc::string& name, const fc::string& type, const variant& args ); 38 | static appender::ptr get( const fc::string& name ); 39 | static bool register_appender( const fc::string& type, const appender_factory::ptr& f ); 40 | 41 | virtual void log( const log_message& m ) = 0; 42 | }; 43 | } 44 | -------------------------------------------------------------------------------- /include/fc/log/console_appender.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | 6 | namespace fc 7 | { 8 | class console_appender : public appender 9 | { 10 | public: 11 | struct color 12 | { 13 | enum type { 14 | red, 15 | green, 16 | brown, 17 | blue, 18 | magenta, 19 | cyan, 20 | white, 21 | console_default, 22 | }; 23 | }; 24 | 25 | struct stream { enum type { std_out, std_error }; }; 26 | 27 | struct level_color 28 | { 29 | level_color( log_level l=log_level::all, 30 | color::type c=color::console_default ) 31 | :level(l),color(c){} 32 | 33 | log_level level; 34 | console_appender::color::type color; 35 | }; 36 | 37 | struct config 38 | { 39 | config() 40 | :format( "${timestamp} ${thread_name} ${context} ${file}:${line} ${method} ${level}] ${message}" ), 41 | stream(console_appender::stream::std_error),flush(true){} 42 | 43 | fc::string format; 44 | console_appender::stream::type stream; 45 | std::vector level_colors; 46 | bool flush; 47 | }; 48 | 49 | 50 | console_appender( const variant& args ); 51 | console_appender( const config& cfg ); 52 | console_appender(); 53 | 54 | ~console_appender(); 55 | virtual void log( const log_message& m ); 56 | 57 | void print( const std::string& text_to_print, 58 | color::type text_color = color::console_default ); 59 | 60 | void configure( const config& cfg ); 61 | 62 | private: 63 | class impl; 64 | std::unique_ptr my; 65 | }; 66 | } // namespace fc 67 | 68 | #include 69 | FC_REFLECT_ENUM( fc::console_appender::stream::type, (std_out)(std_error) ) 70 | FC_REFLECT_ENUM( fc::console_appender::color::type, (red)(green)(brown)(blue)(magenta)(cyan)(white)(console_default) ) 71 | FC_REFLECT( fc::console_appender::level_color, (level)(color) ) 72 | FC_REFLECT( fc::console_appender::config, (format)(stream)(level_colors)(flush) ) 73 | -------------------------------------------------------------------------------- /include/fc/log/file_appender.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | namespace fc { 9 | 10 | class file_appender : public appender { 11 | public: 12 | struct config { 13 | config( const fc::path& p = "log.txt" ); 14 | 15 | fc::string format; 16 | fc::path filename; 17 | bool flush = true; 18 | bool rotate = false; 19 | microseconds rotation_interval; 20 | microseconds rotation_limit; 21 | }; 22 | file_appender( const variant& args ); 23 | ~file_appender(); 24 | virtual void log( const log_message& m )override; 25 | 26 | private: 27 | class impl; 28 | fc::shared_ptr my; 29 | }; 30 | } // namespace fc 31 | 32 | #include 33 | FC_REFLECT( fc::file_appender::config, 34 | (format)(filename)(flush)(rotate)(rotation_interval)(rotation_limit) ) 35 | -------------------------------------------------------------------------------- /include/fc/log/gelf_appender.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | namespace fc 8 | { 9 | // Log appender that sends log messages in JSON format over UDP 10 | // https://www.graylog2.org/resources/gelf/specification 11 | class gelf_appender : public appender 12 | { 13 | public: 14 | struct config 15 | { 16 | string endpoint = "127.0.0.1:12201"; 17 | string host = "fc"; // the name of the host, source or application that sent this message (just passed through to GELF server) 18 | }; 19 | 20 | gelf_appender(const variant& args); 21 | ~gelf_appender(); 22 | virtual void log(const log_message& m) override; 23 | 24 | private: 25 | class impl; 26 | fc::shared_ptr my; 27 | }; 28 | } // namespace fc 29 | 30 | #include 31 | FC_REFLECT(fc::gelf_appender::config, 32 | (endpoint)(host)) 33 | -------------------------------------------------------------------------------- /include/fc/log/logger_config.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | namespace fc { 5 | class path; 6 | struct appender_config { 7 | appender_config(const string& name = "", 8 | const string& type = "", 9 | variant args = variant()) : 10 | name(name), 11 | type(type), 12 | args(fc::move(args)), 13 | enabled(true) 14 | {} 15 | string name; 16 | string type; 17 | variant args; 18 | bool enabled; 19 | }; 20 | 21 | struct logger_config { 22 | logger_config(const fc::string& name = ""):name(name),enabled(true),additivity(false){} 23 | string name; 24 | ostring parent; 25 | /// if not set, then parents level is used. 26 | fc::optional level; 27 | bool enabled; 28 | /// if any appenders are sepecified, then parent's appenders are not set. 29 | bool additivity; 30 | std::vector appenders; 31 | 32 | logger_config& add_appender( const string& s ); 33 | }; 34 | 35 | struct logging_config { 36 | static logging_config default_config(); 37 | std::vector includes; 38 | std::vector appenders; 39 | std::vector loggers; 40 | }; 41 | 42 | void configure_logging( const fc::path& log_config ); 43 | bool configure_logging( const logging_config& l ); 44 | } 45 | 46 | #include 47 | FC_REFLECT( fc::appender_config, (name)(type)(args)(enabled) ) 48 | FC_REFLECT( fc::logger_config, (name)(parent)(level)(enabled)(additivity)(appenders) ) 49 | FC_REFLECT( fc::logging_config, (includes)(appenders)(loggers) ) 50 | -------------------------------------------------------------------------------- /include/fc/make_fused.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | namespace fc { 6 | template 7 | std::function > make_fused( const std::function& f ) { 8 | return [=]( fc::tuple<> ){ return f(); }; 9 | } 10 | template 11 | std::function) > make_fused( const std::function& f ) { 12 | return [f]( fc::tuple t){ return f(t.a); }; 13 | } 14 | template 15 | std::function) > make_fused( const std::function& f ) { 16 | return [f]( fc::tuple t){ return f(t.a,t.b); }; 17 | } 18 | template 19 | std::function) > make_fused( const std::function& f ) { 20 | return [f]( fc::tuple t){ return f(t.a,t.b,t.c); }; 21 | } 22 | template 23 | std::function) > make_fused( const std::function& f ) { 24 | return [f]( fc::tuple t){ return f(t.a,t.b,t.c,t.d); }; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /include/fc/network/gntp.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | #include 9 | 10 | namespace fc 11 | { 12 | namespace detail { 13 | class gntp_icon_impl; 14 | } 15 | class gntp_notifier; 16 | 17 | class gntp_icon { 18 | public: 19 | gntp_icon(const char* buffer, size_t length); 20 | ~gntp_icon(); 21 | private: 22 | std::unique_ptr my; 23 | friend class gntp_notifier; 24 | }; 25 | typedef std::shared_ptr gntp_icon_ptr; 26 | 27 | class gntp_notification_type { 28 | public: 29 | std::string name; 30 | std::string display_name; 31 | bool enabled; 32 | gntp_icon_ptr icon; 33 | }; 34 | typedef std::vector gntp_notification_type_list; 35 | 36 | namespace detail { 37 | class gntp_notifier_impl; 38 | } 39 | 40 | typedef uint160_t gntp_guid; 41 | 42 | class gntp_notifier { 43 | public: 44 | gntp_notifier(const std::string& host_to_notify = "127.0.0.1", uint16_t port = 23053, 45 | const optional& password = optional()); 46 | ~gntp_notifier(); 47 | void set_application_name(std::string application_name); 48 | void set_application_icon(const gntp_icon_ptr& icon); 49 | void register_notifications(); 50 | gntp_guid send_notification(std::string name, std::string title, std::string text, const gntp_icon_ptr& icon = gntp_icon_ptr(), optional coalescingId = optional()); 51 | void add_notification_type(const gntp_notification_type& notificationType); 52 | private: 53 | std::unique_ptr my; 54 | }; 55 | 56 | 57 | } // namespace fc 58 | -------------------------------------------------------------------------------- /include/fc/network/http/connection.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | 6 | namespace fc { 7 | namespace ip { class endpoint; } 8 | class tcp_socket; 9 | 10 | namespace http { 11 | 12 | struct header 13 | { 14 | header( fc::string k, fc::string v ) 15 | :key(fc::move(k)),val(fc::move(v)){} 16 | header(){} 17 | fc::string key; 18 | fc::string val; 19 | }; 20 | 21 | typedef std::vector
headers; 22 | 23 | struct reply 24 | { 25 | enum status_code { 26 | OK = 200, 27 | RecordCreated = 201, 28 | BadRequest = 400, 29 | NotAuthorized = 401, 30 | NotFound = 404, 31 | Found = 302, 32 | InternalServerError = 500 33 | }; 34 | reply( status_code c = OK):status(c){} 35 | int status; 36 | std::vector
headers; 37 | std::vector body; 38 | }; 39 | 40 | struct request 41 | { 42 | fc::string get_header( const fc::string& key )const; 43 | fc::string method; 44 | fc::string domain; 45 | fc::string path; 46 | std::vector
headers; 47 | std::vector body; 48 | }; 49 | 50 | std::vector
parse_urlencoded_params( const fc::string& f ); 51 | 52 | /** 53 | * Connections have reference semantics, all copies refer to the same 54 | * underlying socket. 55 | */ 56 | class connection 57 | { 58 | public: 59 | connection(); 60 | ~connection(); 61 | // used for clients 62 | void connect_to( const fc::ip::endpoint& ep ); 63 | http::reply request( const fc::string& method, const fc::string& url, const fc::string& body = std::string(), const headers& = headers()); 64 | 65 | // used for servers 66 | fc::tcp_socket& get_socket()const; 67 | 68 | http::request read_request()const; 69 | 70 | class impl; 71 | private: 72 | std::unique_ptr my; 73 | }; 74 | 75 | typedef std::shared_ptr connection_ptr; 76 | 77 | } } // fc::http 78 | 79 | #include 80 | FC_REFLECT( fc::http::header, (key)(val) ) 81 | 82 | -------------------------------------------------------------------------------- /include/fc/network/http/server.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | namespace fc { namespace http { 8 | 9 | /** 10 | * Listens on a given port for incomming http 11 | * connections and then calls a user provided callback 12 | * function for every http request. 13 | * 14 | */ 15 | class server 16 | { 17 | public: 18 | server(); 19 | server( uint16_t port ); 20 | server( server&& s ); 21 | ~server(); 22 | 23 | server& operator=(server&& s); 24 | 25 | class response 26 | { 27 | public: 28 | class impl; 29 | 30 | response(); 31 | response( const fc::shared_ptr& my); 32 | response( const response& r); 33 | response( response&& r ); 34 | ~response(); 35 | response& operator=(const response& ); 36 | response& operator=( response&& ); 37 | 38 | void add_header( const fc::string& key, const fc::string& val )const; 39 | void set_status( const http::reply::status_code& s )const; 40 | void set_length( uint64_t s )const; 41 | 42 | void write( const char* data, uint64_t len )const; 43 | 44 | private: 45 | fc::shared_ptr my; 46 | }; 47 | 48 | void listen( const fc::ip::endpoint& p ); 49 | fc::ip::endpoint get_local_endpoint() const; 50 | 51 | /** 52 | * Set the callback to be called for every http request made. 53 | */ 54 | void on_request( const std::function& cb ); 55 | 56 | private: 57 | class impl; 58 | std::unique_ptr my; 59 | }; 60 | typedef std::shared_ptr server_ptr; 61 | 62 | } } 63 | -------------------------------------------------------------------------------- /include/fc/network/ntp.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | namespace fc { 9 | 10 | namespace detail { class ntp_impl; } 11 | 12 | class ntp 13 | { 14 | public: 15 | ntp(); 16 | ~ntp(); 17 | 18 | void add_server( const std::string& hostname, uint16_t port = 123 ); 19 | void set_request_interval( uint32_t interval_sec ); 20 | void request_now(); 21 | optional get_time()const; 22 | 23 | private: 24 | std::unique_ptr my; 25 | }; 26 | 27 | } // namespace fc 28 | -------------------------------------------------------------------------------- /include/fc/network/rate_limiting.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | #include 5 | 6 | #include 7 | 8 | namespace fc 9 | { 10 | namespace detail 11 | { 12 | class rate_limiting_group_impl; 13 | } 14 | 15 | class tcp_socket; 16 | 17 | class rate_limiting_group 18 | { 19 | public: 20 | rate_limiting_group(uint32_t upload_bytes_per_second, uint32_t download_bytes_per_second, uint32_t burstiness_in_seconds = 1); 21 | ~rate_limiting_group(); 22 | 23 | void set_upload_limit(uint32_t upload_bytes_per_second); 24 | uint32_t get_upload_limit() const; 25 | 26 | void set_download_limit(uint32_t download_bytes_per_second); 27 | uint32_t get_download_limit() const; 28 | 29 | uint32_t get_actual_upload_rate() const; 30 | uint32_t get_actual_download_rate() const; 31 | void set_actual_rate_time_constant(microseconds time_constant); 32 | 33 | void add_tcp_socket(tcp_socket* tcp_socket_to_limit); 34 | void remove_tcp_socket(tcp_socket* tcp_socket_to_stop_limiting); 35 | private: 36 | std::unique_ptr my; 37 | }; 38 | typedef std::shared_ptr rate_limiting_group_ptr; 39 | 40 | } // namesapce fc 41 | 42 | -------------------------------------------------------------------------------- /include/fc/network/resolve.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | namespace fc 6 | { 7 | std::vector resolve( const std::string& host, uint16_t port ); 8 | } 9 | -------------------------------------------------------------------------------- /include/fc/network/tcp_socket.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | namespace fc { 8 | namespace ip { class endpoint; } 9 | 10 | class tcp_socket_io_hooks; 11 | 12 | class tcp_socket : public virtual iostream 13 | { 14 | public: 15 | tcp_socket(); 16 | ~tcp_socket(); 17 | 18 | void connect_to( const fc::ip::endpoint& remote_endpoint ); 19 | void bind( const fc::ip::endpoint& local_endpoint ); 20 | void enable_keep_alives(const fc::microseconds& interval); 21 | void set_io_hooks(tcp_socket_io_hooks* new_hooks); 22 | void set_reuse_address(bool enable = true); // set SO_REUSEADDR 23 | fc::ip::endpoint remote_endpoint() const; 24 | fc::ip::endpoint local_endpoint() const; 25 | 26 | using istream::get; 27 | void get( char& c ) 28 | { 29 | read( &c, 1 ); 30 | } 31 | 32 | 33 | /// istream interface 34 | /// @{ 35 | virtual size_t readsome( char* buffer, size_t max ); 36 | virtual size_t readsome(const std::shared_ptr& buffer, size_t max, size_t offset); 37 | virtual bool eof()const; 38 | /// @} 39 | 40 | /// ostream interface 41 | /// @{ 42 | virtual size_t writesome( const char* buffer, size_t len ); 43 | virtual size_t writesome(const std::shared_ptr& buffer, size_t len, size_t offset); 44 | virtual void flush(); 45 | virtual void close(); 46 | /// @} 47 | 48 | void open(); 49 | bool is_open()const; 50 | 51 | private: 52 | friend class tcp_server; 53 | class impl; 54 | #ifdef _WIN64 55 | fc::fwd my; 56 | #else 57 | fc::fwd my; 58 | #endif 59 | }; 60 | typedef std::shared_ptr tcp_socket_ptr; 61 | 62 | 63 | class tcp_server 64 | { 65 | public: 66 | tcp_server(); 67 | ~tcp_server(); 68 | 69 | void close(); 70 | void accept( tcp_socket& s ); 71 | void set_reuse_address(bool enable = true); // set SO_REUSEADDR, call before listen 72 | void listen( uint16_t port ); 73 | void listen( const fc::ip::endpoint& ep ); 74 | fc::ip::endpoint get_local_endpoint() const; 75 | uint16_t get_port()const; 76 | private: 77 | // non copyable 78 | tcp_server( const tcp_server& ); 79 | tcp_server& operator=(const tcp_server& s ); 80 | 81 | class impl; 82 | impl* my; 83 | }; 84 | 85 | } // namesapce fc 86 | 87 | -------------------------------------------------------------------------------- /include/fc/network/tcp_socket_io_hooks.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace fc 5 | { 6 | class tcp_socket_io_hooks 7 | { 8 | public: 9 | virtual ~tcp_socket_io_hooks() {} 10 | virtual size_t readsome(boost::asio::ip::tcp::socket& socket, char* buffer, size_t length) = 0; 11 | virtual size_t readsome(boost::asio::ip::tcp::socket& socket, const std::shared_ptr& buffer, size_t length, size_t offset) = 0; 12 | virtual size_t writesome(boost::asio::ip::tcp::socket& socket, const char* buffer, size_t length) = 0; 13 | virtual size_t writesome(boost::asio::ip::tcp::socket& socket, const std::shared_ptr& buffer, size_t length, size_t offset) = 0; 14 | }; 15 | } // namesapce fc 16 | -------------------------------------------------------------------------------- /include/fc/network/udp_socket.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | 6 | namespace fc { 7 | namespace ip { 8 | class endpoint; 9 | class address; 10 | } 11 | 12 | /** 13 | * The udp_socket class has reference semantics, all copies will 14 | * refer to the same underlying socket. 15 | */ 16 | class udp_socket { 17 | public: 18 | udp_socket(); 19 | udp_socket( const udp_socket& s ); 20 | ~udp_socket(); 21 | 22 | void open(); 23 | void set_receive_buffer_size( size_t s ); 24 | void bind( const fc::ip::endpoint& ); 25 | size_t receive_from( char* b, size_t l, fc::ip::endpoint& from ); 26 | size_t receive_from( const std::shared_ptr& b, size_t l, fc::ip::endpoint& from ); 27 | size_t send_to( const char* b, size_t l, const fc::ip::endpoint& to ); 28 | size_t send_to( const std::shared_ptr& b, size_t l, const fc::ip::endpoint& to ); 29 | void close(); 30 | 31 | void set_multicast_enable_loopback( bool ); 32 | void set_reuse_address( bool ); 33 | void join_multicast_group( const fc::ip::address& a ); 34 | 35 | void connect( const fc::ip::endpoint& e ); 36 | fc::ip::endpoint local_endpoint()const; 37 | 38 | private: 39 | class impl; 40 | fc::shared_ptr my; 41 | }; 42 | 43 | } 44 | -------------------------------------------------------------------------------- /include/fc/network/udt_socket.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | namespace fc { 9 | namespace ip { class endpoint; } 10 | 11 | class udt_socket : public virtual iostream, public noncopyable 12 | { 13 | public: 14 | udt_socket(); 15 | ~udt_socket(); 16 | 17 | void bind( const fc::ip::endpoint& local_endpoint ); 18 | void connect_to( const fc::ip::endpoint& remote_endpoint ); 19 | 20 | fc::ip::endpoint remote_endpoint() const; 21 | fc::ip::endpoint local_endpoint() const; 22 | 23 | using istream::get; 24 | void get( char& c ) 25 | { 26 | read( &c, 1 ); 27 | } 28 | 29 | 30 | /// istream interface 31 | /// @{ 32 | virtual size_t readsome( char* buffer, size_t max ); 33 | virtual size_t readsome( const std::shared_ptr& buf, size_t len, size_t offset ); 34 | virtual bool eof()const; 35 | /// @} 36 | 37 | /// ostream interface 38 | /// @{ 39 | virtual size_t writesome( const char* buffer, size_t len ); 40 | virtual size_t writesome( const std::shared_ptr& buf, size_t len, size_t offset ); 41 | virtual void flush(); 42 | virtual void close(); 43 | /// @} 44 | 45 | void open(); 46 | bool is_open()const; 47 | 48 | private: 49 | friend class udt_server; 50 | int _udt_socket_id; 51 | }; 52 | typedef std::shared_ptr udt_socket_ptr; 53 | 54 | class udt_server : public noncopyable 55 | { 56 | public: 57 | udt_server(); 58 | ~udt_server(); 59 | 60 | void close(); 61 | void accept( udt_socket& s ); 62 | 63 | void listen( const fc::ip::endpoint& ep ); 64 | fc::ip::endpoint local_endpoint() const; 65 | 66 | private: 67 | int _udt_socket_id; 68 | }; 69 | 70 | } // fc 71 | -------------------------------------------------------------------------------- /include/fc/noncopyable.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace fc 4 | { 5 | class noncopyable 6 | { 7 | public: 8 | noncopyable(){} 9 | private: 10 | noncopyable( const noncopyable& ) = delete; 11 | noncopyable& operator=( const noncopyable& ) = delete; 12 | }; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /include/fc/platform_independence.hpp: -------------------------------------------------------------------------------- 1 | #ifdef _MSC_VER 2 | #include 3 | #ifdef _M_X64 4 | #define __builtin_popcountll __popcnt64 5 | #else 6 | inline int __builtin_popcountll(unsigned __int64 value) 7 | { 8 | unsigned int lowBits = (unsigned int)value; 9 | int count = __popcnt(lowBits); 10 | unsigned int highBits = (unsigned int)(value >> 32); 11 | count += __popcnt(highBits); 12 | return count; 13 | } 14 | #endif 15 | #endif -------------------------------------------------------------------------------- /include/fc/real128.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | #define FC_REAL128_PRECISION (uint64_t(1000000) * uint64_t(1000000) * uint64_t(1000000)) 5 | 6 | namespace fc { 7 | class variant; 8 | 9 | /** 10 | * Provides fixed point math operations based on decimal fractions 11 | * with 18 places. 12 | * Delegates to fc::bigint for multiplication and division. 13 | */ 14 | class real128 15 | { 16 | public: 17 | real128( uint64_t integer = 0); 18 | explicit real128( const std::string& str ); 19 | operator std::string()const; 20 | 21 | friend real128 operator * ( real128 a, const real128& b ) { a *= b; return a; } 22 | friend real128 operator / ( real128 a, const real128& b ) { a /= b; return a; } 23 | friend real128 operator + ( real128 a, const real128& b ) { a += b; return a; } 24 | friend real128 operator - ( real128 a, const real128& b ) { a -= b; return a; } 25 | 26 | real128& operator += ( const real128& o ); 27 | real128& operator -= ( const real128& o ); 28 | real128& operator /= ( const real128& o ); 29 | real128& operator *= ( const real128& o ); 30 | 31 | static real128 from_fixed( const uint128& fixed ); 32 | 33 | uint64_t to_uint64()const; 34 | 35 | private: 36 | uint128 fixed; 37 | }; 38 | 39 | void to_variant( const real128& var, variant& vo ); 40 | void from_variant( const variant& var, real128& vo ); 41 | 42 | namespace raw 43 | { 44 | template 45 | inline void pack( Stream& s, const real128& value_to_pack ) { s.write( (char*)&value_to_pack, sizeof(value_to_pack) ); } 46 | template 47 | inline void unpack( Stream& s, real128& value_to_unpack ) { s.read( (char*)&value_to_unpack, sizeof(value_to_unpack) ); } 48 | } 49 | 50 | 51 | 52 | } // namespace fc 53 | -------------------------------------------------------------------------------- /include/fc/rpc/cli.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | 11 | namespace fc { namespace rpc { 12 | 13 | /** 14 | * Provides a simple wrapper for RPC calls to a given interface. 15 | */ 16 | class cli : public api_connection 17 | { 18 | public: 19 | ~cli(); 20 | 21 | virtual variant send_call( api_id_type api_id, string method_name, variants args = variants() ); 22 | virtual variant send_callback( uint64_t callback_id, variants args = variants() ); 23 | virtual void send_notice( uint64_t callback_id, variants args = variants() ); 24 | 25 | void start(); 26 | void stop(); 27 | void wait(); 28 | void format_result( const string& method, std::function formatter); 29 | 30 | virtual void getline( const fc::string& prompt, fc::string& line ); 31 | 32 | void set_prompt( const string& prompt ); 33 | 34 | private: 35 | void run(); 36 | 37 | std::string _prompt = ">>>"; 38 | std::map > _result_formatters; 39 | fc::future _run_complete; 40 | }; 41 | } } 42 | -------------------------------------------------------------------------------- /include/fc/rpc/http_api.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | namespace fc { namespace rpc { 10 | 11 | class http_api_connection : public api_connection 12 | { 13 | public: 14 | http_api_connection(); 15 | ~http_api_connection(); 16 | 17 | virtual variant send_call( 18 | api_id_type api_id, 19 | string method_name, 20 | variants args = variants() ) override; 21 | virtual variant send_callback( 22 | uint64_t callback_id, 23 | variants args = variants() ) override; 24 | virtual void send_notice( 25 | uint64_t callback_id, 26 | variants args = variants() ) override; 27 | 28 | void on_request( 29 | const fc::http::request& req, 30 | const fc::http::server::response& resp ); 31 | 32 | fc::rpc::state _rpc_state; 33 | }; 34 | 35 | } } // namespace fc::rpc 36 | -------------------------------------------------------------------------------- /include/fc/rpc/state.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | 6 | namespace fc { namespace rpc { 7 | struct request 8 | { 9 | optional id; 10 | std::string method; 11 | variants params; 12 | }; 13 | 14 | struct error_object 15 | { 16 | int64_t code; 17 | std::string message; 18 | optional data; 19 | }; 20 | 21 | struct response 22 | { 23 | response(){} 24 | response( int64_t i, fc::variant r ):id(i),result(r){} 25 | response( int64_t i, error_object r ):id(i),error(r){} 26 | int64_t id = 0; 27 | optional result; 28 | optional error; 29 | }; 30 | 31 | class state 32 | { 33 | public: 34 | typedef std::function method; 35 | ~state(); 36 | 37 | void add_method( const fc::string& name, method m ); 38 | void remove_method( const fc::string& name ); 39 | 40 | variant local_call( const string& method_name, const variants& args ); 41 | void handle_reply( const response& response ); 42 | 43 | request start_remote_call( const string& method_name, variants args ); 44 | variant wait_for_response( uint64_t request_id ); 45 | 46 | void close(); 47 | 48 | void on_unhandled( const std::function& unhandled ); 49 | 50 | private: 51 | uint64_t _next_id = 1; 52 | std::unordered_map::ptr> _awaiting; 53 | std::unordered_map _methods; 54 | std::function _unhandled; 55 | }; 56 | } } // namespace fc::rpc 57 | 58 | FC_REFLECT( fc::rpc::request, (id)(method)(params) ); 59 | FC_REFLECT( fc::rpc::error_object, (code)(message)(data) ) 60 | FC_REFLECT( fc::rpc::response, (id)(result)(error) ) 61 | -------------------------------------------------------------------------------- /include/fc/rpc/variant_stream.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace fc 4 | { 5 | 6 | /** 7 | * Thread-safe, circular buffer for passing variants 8 | * between threads. 9 | */ 10 | class variant_stream 11 | { 12 | public: 13 | variant_stream( size_t s ); 14 | ~variant_stream(); 15 | 16 | /** producer api */ 17 | int64_t free(); // number of spaces available 18 | int64_t claim( int64_t num ); 19 | int64_t publish( int64_t pos ); 20 | int64_t wait_free(); // wait for free space 21 | 22 | // producer/consumer api 23 | variant& get( int64_t pos ); 24 | 25 | /** consumer api */ 26 | int64_t begin(); // returns the first index ready to be read 27 | int64_t end(); // returns the first index that cannot be read 28 | int64_t wait(); // wait for variants to be posted 29 | 30 | private: 31 | std::vector _variants; 32 | uint64_t _read_pos; 33 | uint64_t _write_pos; 34 | }; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /include/fc/rpc/websocket_api.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | namespace fc { namespace rpc { 9 | 10 | class websocket_api_connection : public api_connection 11 | { 12 | public: 13 | websocket_api_connection( fc::http::websocket_connection& c ); 14 | ~websocket_api_connection(); 15 | 16 | virtual variant send_call( 17 | api_id_type api_id, 18 | string method_name, 19 | variants args = variants() ) override; 20 | virtual variant send_callback( 21 | uint64_t callback_id, 22 | variants args = variants() ) override; 23 | virtual void send_notice( 24 | uint64_t callback_id, 25 | variants args = variants() ) override; 26 | 27 | protected: 28 | std::string on_message( 29 | const std::string& message, 30 | bool send_message = true ); 31 | 32 | fc::http::websocket_connection& _connection; 33 | fc::rpc::state _rpc_state; 34 | }; 35 | 36 | } } // namespace fc::rpc 37 | -------------------------------------------------------------------------------- /include/fc/shared_ptr.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | namespace fc { 5 | 6 | /** 7 | * @brief used to create reference counted types. 8 | * 9 | * Must be a virtual base class that is initialized with the 10 | * 11 | */ 12 | class retainable { 13 | public: 14 | retainable(); 15 | void retain(); 16 | void release(); 17 | int32_t retain_count()const; 18 | 19 | protected: 20 | virtual ~retainable(); 21 | private: 22 | volatile int32_t _ref_count; 23 | }; 24 | 25 | template 26 | class shared_ptr { 27 | public: 28 | template 29 | shared_ptr( const shared_ptr& o ) 30 | :_ptr(o.get()) { 31 | if(_ptr != nullptr ) _ptr->retain(); 32 | } 33 | shared_ptr( const shared_ptr& o ) 34 | :_ptr(o.get()) { 35 | if(_ptr != nullptr ) _ptr->retain(); 36 | } 37 | 38 | shared_ptr( T* t, bool inc = false ) 39 | :_ptr(t) { if( inc && t != nullptr) t->retain(); } 40 | 41 | shared_ptr():_ptr(nullptr){} 42 | 43 | 44 | shared_ptr( shared_ptr&& p ) 45 | :_ptr(p._ptr){ p._ptr = nullptr; } 46 | 47 | ~shared_ptr() { if( nullptr != _ptr ) { _ptr->release(); } } 48 | 49 | shared_ptr& reset( T* v = nullptr, bool inc = false ) { 50 | if( v == _ptr ) return *this; 51 | if( inc && nullptr != v ) v->retain(); 52 | if( nullptr != _ptr ) _ptr->release(); 53 | _ptr = v; 54 | return *this; 55 | } 56 | 57 | shared_ptr& operator=(const shared_ptr& p ) { 58 | if( _ptr == p._ptr ) return *this; 59 | if( p._ptr != nullptr ) p._ptr->retain(); 60 | if( _ptr != nullptr ) _ptr->release(); 61 | _ptr = p._ptr; 62 | return *this; 63 | } 64 | shared_ptr& operator=(shared_ptr&& p ) { 65 | fc_swap(_ptr,p._ptr); 66 | return *this; 67 | } 68 | T& operator* ()const { return *_ptr; } 69 | T* operator-> ()const { return _ptr; } 70 | 71 | bool operator==( const shared_ptr& p )const { return get() == p.get(); } 72 | bool operator<( const shared_ptr& p )const { return get() < p.get(); } 73 | T * get() const { return _ptr; } 74 | 75 | bool operator!()const { return _ptr == 0; } 76 | operator bool()const { return _ptr != 0; } 77 | private: 78 | T* _ptr; 79 | }; 80 | 81 | template 82 | fc::shared_ptr dynamic_pointer_cast( const fc::shared_ptr& t ) { 83 | return fc::shared_ptr( dynamic_cast(t.get()), true ); 84 | } 85 | template 86 | fc::shared_ptr static_pointer_cast( const fc::shared_ptr& t ) { 87 | return fc::shared_ptr( static_cast(t.get()), true ); 88 | } 89 | } 90 | 91 | -------------------------------------------------------------------------------- /include/fc/signals.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #if defined(_MSC_VER) && _MSC_VER >= 1400 4 | #pragma warning(push) 5 | #pragma warning(disable:4996) 6 | #endif 7 | 8 | #include 9 | 10 | #if defined(_MSC_VER) && _MSC_VER >= 1400 11 | #pragma warning(pop) 12 | #endif 13 | 14 | #include 15 | #include 16 | 17 | namespace fc { 18 | #if !defined(BOOST_NO_TEMPLATE_ALIASES) 19 | template 20 | using signal = boost::signals2::signal; 21 | 22 | using scoped_connection = boost::signals2::scoped_connection; 23 | #else 24 | /** Workaround for missing Template Aliases feature in the VS 2012. 25 | \warning Class defined below cannot have defined constructor (even base class has it) 26 | since it is impossible to reference directly template class arguments outside this class. 27 | This code will work until someone will use non-default constructor as it is defined in 28 | boost::signals2::signal. 29 | */ 30 | template 31 | class signal : public boost::signals2::signal 32 | { 33 | public: 34 | }; 35 | #endif 36 | 37 | template 38 | inline T wait( boost::signals2::signal& sig, const microseconds& timeout_us=microseconds::maximum() ) { 39 | typename promise::ptr p(new promise("fc::signal::wait")); 40 | boost::signals2::scoped_connection c( sig.connect( [=]( T t ) { p->set_value(t); } )); 41 | return p->wait( timeout_us ); 42 | } 43 | 44 | inline void wait( boost::signals2::signal& sig, const microseconds& timeout_us=microseconds::maximum() ) { 45 | promise::ptr p(new promise("fc::signal::wait")); 46 | boost::signals2::scoped_connection c( sig.connect( [=]() { p->set_value(); } )); 47 | p->wait( timeout_us ); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /include/fc/smart_ref_fwd.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace fc { 4 | /** 5 | * @brief Used to forward declare value types and break circular dependencies or use heap allocation 6 | * 7 | * A smart reference is heap allocated, move-aware, and is gauranteed to never be null (except after a move) 8 | */ 9 | template 10 | class smart_ref { 11 | public: 12 | template smart_ref( U&& u ); 13 | template smart_ref( U&& u, V&& v ); 14 | template smart_ref( U&& u, V&& v, X&&, Y&& ); 15 | smart_ref(); 16 | 17 | smart_ref( const smart_ref& f ); 18 | smart_ref( smart_ref&& f ); 19 | 20 | operator const T&()const; 21 | operator T&(); 22 | 23 | T& operator*(); 24 | const T& operator*()const; 25 | const T* operator->()const; 26 | 27 | T* operator->(); 28 | bool operator !()const; 29 | 30 | template 31 | T& operator = ( U&& u ); 32 | 33 | T& operator = ( smart_ref&& u ); 34 | T& operator = ( const smart_ref& u ); 35 | 36 | ~smart_ref(); 37 | 38 | private: 39 | T* impl; 40 | }; 41 | } 42 | -------------------------------------------------------------------------------- /include/fc/ssh/error.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MACE_SSH_ERROR_HPP 2 | #define MACE_SSH_ERROR_HPP 3 | #include 4 | #include 5 | 6 | namespace mace { namespace ssh { 7 | typedef boost::error_info err_msg; 8 | 9 | struct exception : public virtual boost::exception, public virtual std::exception { 10 | const char* what()const throw() { return "exception"; } 11 | virtual void rethrow()const { BOOST_THROW_EXCEPTION(*this); } 12 | const std::string& message()const { return *boost::get_error_info(*this); } 13 | }; 14 | 15 | } } // mace::ssh 16 | 17 | /** 18 | * Helper macro for throwing exceptions with a message: THROW( "Hello World %1%, %2%", %"Hello" %"World" ) 19 | */ 20 | #define MACE_SSH_THROW( MSG, ... ) \ 21 | do { \ 22 | BOOST_THROW_EXCEPTION( mace::ssh::exception() << mace::ssh::err_msg( (boost::format( MSG ) __VA_ARGS__ ).str() ) );\ 23 | } while(0) 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /include/fc/ssh/process.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | namespace fc { namespace ssh 5 | { 6 | 7 | class client; 8 | 9 | namespace detail { 10 | class process_impl; 11 | }; 12 | 13 | /** 14 | * Enables communication with a process executed via 15 | * client::exec(). 16 | * 17 | * Process can only be created by mace::ssh::client. 18 | */ 19 | class process : public iprocess 20 | { 21 | public: 22 | virtual iprocess& exec( const fc::path& exe, std::vector args, 23 | const fc::path& work_dir = fc::path(), exec_opts opts = open_all ); 24 | 25 | /** 26 | * Blocks until the result code of the process has been returned. 27 | */ 28 | virtual int result(); 29 | 30 | 31 | /** 32 | * Not supported. libssh2 does not support sending signals to remote processes. 33 | * closing in_stream() is the best you can do 34 | */ 35 | virtual void kill(); 36 | 37 | 38 | /** 39 | * @brief returns a stream that writes to the process' stdin 40 | */ 41 | virtual fc::buffered_ostream_ptr in_stream(); 42 | 43 | /** 44 | * @brief returns a stream that reads from the process' stdout 45 | */ 46 | virtual fc::buffered_istream_ptr out_stream(); 47 | /** 48 | * @brief returns a stream that reads from the process' stderr 49 | */ 50 | virtual fc::buffered_istream_ptr err_stream(); 51 | 52 | process( fc::ssh::client_ptr c ); 53 | ~process(); 54 | private: 55 | std::unique_ptr my; 56 | }; 57 | 58 | } } // fc::ssh 59 | -------------------------------------------------------------------------------- /include/fc/thread/non_preemptable_scope_check.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | // This file defines a macro: 6 | // ASSERT_TASK_NOT_PREEMPTED() 7 | // This macro is used to declare that the current scope is not expected to yield. 8 | // If the task does yield, it will generate an assertion. 9 | // 10 | // Use this when you you're coding somethign that must not yield, and you think you 11 | // have done it (just by virtue of the fact that you don't think you've called any 12 | // functions that could yield). This will help detect when your assumptions are 13 | // wrong and you accidentally call something that yields. 14 | // 15 | // This has no cost in release mode, and is extremely cheap in debug mode. 16 | 17 | #define FC_NON_PREEMPTABLE_SCOPE_COMBINE_HELPER(x,y) x ## y 18 | #define FC_NON_PREEMPTABLE_SCOPE_COMBINE(x,y) FC_NON_PREEMPTABLE_SCOPE_COMBINE_HELPER(x,y) 19 | 20 | #ifdef NDEBUG 21 | # define ASSERT_TASK_NOT_PREEMPTED() do {} while (0) 22 | #else 23 | # define ASSERT_TASK_NOT_PREEMPTED() fc::non_preemptable_scope_check FC_NON_PREEMPTABLE_SCOPE_COMBINE(scope_checker_, __LINE__) 24 | 25 | namespace fc 26 | { 27 | class non_preemptable_scope_check 28 | { 29 | public: 30 | non_preemptable_scope_check(); 31 | ~non_preemptable_scope_check(); 32 | }; 33 | } // namespace fc 34 | #endif 35 | 36 | -------------------------------------------------------------------------------- /include/fc/thread/priority.hpp: -------------------------------------------------------------------------------- 1 | #ifndef _FC_PRIORITY_HPP_ 2 | #define _FC_PRIORITY_HPP_ 3 | 4 | namespace fc { 5 | /** 6 | * An integer value used to sort asynchronous tasks. The higher the 7 | * prioirty the sooner it will be run. 8 | */ 9 | class priority { 10 | public: 11 | explicit priority( int v = 0):value(v){} 12 | priority( const priority& p ):value(p.value){} 13 | bool operator < ( const priority& p )const { 14 | return value < p.value; 15 | } 16 | static priority max() { return priority(10000); } 17 | static priority min() { return priority(-10000); } 18 | static priority _internal__priority_for_short_sleeps() { return priority(-100000); } 19 | int value; 20 | }; 21 | } 22 | #endif // _FC_PRIORITY_HPP_ 23 | -------------------------------------------------------------------------------- /include/fc/thread/scoped_lock.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace fc { 4 | template 5 | class scoped_lock { 6 | public: 7 | scoped_lock( T& l ):_lock(l) { _lock.lock(); } 8 | ~scoped_lock() { _lock.unlock(); } 9 | T& _lock; 10 | }; 11 | } 12 | -------------------------------------------------------------------------------- /include/fc/thread/spin_lock.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace fc { 4 | class microseconds; 5 | class time_point; 6 | 7 | /** 8 | * @class spin_lock 9 | * @brief modified spin-lock that yields on failure, but becomes a 'spin lock' 10 | * if there are no other tasks to yield to. 11 | * 12 | * This kind of lock is lighter weight than a full mutex, but potentially slower 13 | * than a staight spin_lock. 14 | * 15 | * This spin_lock does not block the current thread, but instead attempts to use 16 | * an atomic operation to aquire the lock. If unsuccessful, then it yields to 17 | * other tasks before trying again. If there are no other tasks then yield is 18 | * a no-op and spin_lock becomes a spin-lock. 19 | */ 20 | class spin_lock { 21 | public: 22 | spin_lock(); 23 | bool try_lock(); 24 | bool try_lock_for( const microseconds& rel_time ); 25 | bool try_lock_until( const time_point& abs_time ); 26 | void lock(); 27 | void unlock(); 28 | 29 | private: 30 | enum lock_store {locked,unlocked}; 31 | int _lock; 32 | }; 33 | 34 | } // namespace fc 35 | 36 | -------------------------------------------------------------------------------- /include/fc/thread/spin_yield_lock.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace fc { 4 | class microseconds; 5 | class time_point; 6 | 7 | /** 8 | * @class spin_yield_lock 9 | * @brief modified spin-lock that yields on failure, but becomes a 'spin lock' 10 | * if there are no other tasks to yield to. 11 | * 12 | * This kind of lock is lighter weight than a full mutex, but potentially slower 13 | * than a staight spin_lock. 14 | * 15 | * This spin_yield_lock does not block the current thread, but instead attempts to use 16 | * an atomic operation to aquire the lock. If unsuccessful, then it yields to 17 | * other tasks before trying again. If there are no other tasks then yield is 18 | * a no-op and spin_yield_lock becomes a spin-lock. 19 | */ 20 | class spin_yield_lock { 21 | public: 22 | spin_yield_lock(); 23 | bool try_lock(); 24 | bool try_lock_for( const microseconds& rel_time ); 25 | bool try_lock_until( const time_point& abs_time ); 26 | void lock(); 27 | void unlock(); 28 | 29 | private: 30 | enum lock_store {locked,unlocked}; 31 | int _lock; 32 | }; 33 | 34 | } // namespace fc 35 | 36 | -------------------------------------------------------------------------------- /include/fc/thread/thread_specific.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "thread.hpp" 3 | 4 | namespace fc 5 | { 6 | namespace detail 7 | { 8 | unsigned get_next_unused_thread_storage_slot(); 9 | unsigned get_next_unused_task_storage_slot(); 10 | } 11 | 12 | template 13 | class thread_specific_ptr 14 | { 15 | private: 16 | unsigned slot; 17 | public: 18 | thread_specific_ptr() : 19 | slot(detail::get_next_unused_thread_storage_slot()) 20 | {} 21 | 22 | T* get() const 23 | { 24 | return static_cast(detail::get_thread_specific_data(slot)); 25 | } 26 | T* operator->() const 27 | { 28 | return get(); 29 | } 30 | T& operator*() const 31 | { 32 | return *get(); 33 | } 34 | operator bool() const 35 | { 36 | return get() != static_cast(0); 37 | } 38 | static void cleanup_function(void* obj) 39 | { 40 | delete static_cast(obj); 41 | } 42 | void reset(T* new_value = 0) 43 | { 44 | detail::set_thread_specific_data(slot, new_value, cleanup_function); 45 | } 46 | }; 47 | 48 | template 49 | class task_specific_ptr 50 | { 51 | private: 52 | unsigned slot; 53 | public: 54 | task_specific_ptr() : 55 | slot(detail::get_next_unused_task_storage_slot()) 56 | {} 57 | 58 | T* get() const 59 | { 60 | return static_cast(detail::get_task_specific_data(slot)); 61 | } 62 | T* operator->() const 63 | { 64 | return get(); 65 | } 66 | T& operator*() const 67 | { 68 | return *get(); 69 | } 70 | operator bool() const 71 | { 72 | return get() != static_cast(0); 73 | } 74 | static void cleanup_function(void* obj) 75 | { 76 | delete static_cast(obj); 77 | } 78 | void reset(T* new_value = 0) 79 | { 80 | detail::set_task_specific_data(slot, new_value, cleanup_function); 81 | } 82 | }; 83 | 84 | } 85 | -------------------------------------------------------------------------------- /include/fc/thread/unique_lock.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | namespace fc { 5 | struct try_to_lock_t{}; 6 | class time_point; 7 | 8 | /** 9 | * Including Boost's unique lock drastically increases compile times 10 | * for something that is this trivial! 11 | */ 12 | template 13 | class unique_lock { 14 | public: 15 | unique_lock( T& l, const fc::time_point& abs ):_lock(l) { _locked = _lock.try_lock_until(abs); } 16 | unique_lock( T& l, try_to_lock_t ):_lock(l) { _locked = _lock.try_lock(); } 17 | unique_lock( T& l ): _locked(false), _lock(l) { lock(); } 18 | ~unique_lock() { if (_locked) unlock(); } 19 | operator bool() const { return _locked; } 20 | void unlock() { assert(_locked); if (_locked) { _lock.unlock(); _locked = false;} } 21 | void lock() { assert(!_locked); if (!_locked) { _lock.lock(); _locked = true; } } 22 | private: 23 | unique_lock( const unique_lock& ); 24 | unique_lock& operator=( const unique_lock& ); 25 | bool _locked; 26 | T& _lock; 27 | }; 28 | 29 | } 30 | 31 | /** 32 | * Emulate java with the one quirk that the open bracket must come before 33 | * the synchronized 'keyword'. 34 | * 35 | * 36 | { synchronized( lock_type ) 37 | 38 | } 39 | * 40 | */ 41 | #define synchronized(X) fc::unique_lock __lock(((X))); 42 | 43 | -------------------------------------------------------------------------------- /include/fc/thread/wait_condition.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | namespace fc 8 | { 9 | /** 10 | * A thread-safe, fiber-aware condition variable that 11 | * can be used to signal/wait on a certain condition between 12 | * threads. 13 | */ 14 | template 15 | class wait_condition 16 | { 17 | public: 18 | wait_condition(const char* name) : _name(name) {} 19 | 20 | void wait( const microseconds& timeout = microseconds::maximum() ) 21 | { 22 | typename fc::promise::ptr p = new fc::promise(_name); 23 | { synchronized( _prom_lock ) 24 | _promises.push_back( p ); 25 | } 26 | p->wait(timeout); 27 | } 28 | 29 | template 30 | T wait( LockType& l, const microseconds& timeout = microseconds::maximum() ) 31 | { 32 | typename fc::promise::ptr p( new fc::promise(_name)); 33 | { synchronized( _prom_lock ) 34 | _promises.push_back( p ); 35 | } 36 | l.unlock(); 37 | struct relocker { 38 | LockType& _lock; 39 | relocker(LockType& l) : _lock(l) {} 40 | ~relocker() { _lock.lock(); } 41 | } lock_on_exit(l); 42 | return p->wait(timeout); 43 | } 44 | 45 | void notify_one( const T& t=T()) 46 | { 47 | typename fc::promise::ptr prom; 48 | { synchronized( _prom_lock ) 49 | if( _promises.size() ) 50 | { 51 | prom = _promises.front(); 52 | _promises.pop_front(); 53 | } 54 | } 55 | 56 | if( prom && prom->retain_count() > 1 ) 57 | prom->set_value(t); 58 | } 59 | void notify_all(const T& t=T()) 60 | { 61 | std::deque::ptr> all; 62 | { synchronized( _prom_lock ) 63 | std::swap(all, _promises); 64 | } 65 | for( auto itr = all.begin(); itr != all.end(); ++itr ) 66 | { 67 | if( (*itr)->retain_count() > 1 ) 68 | (*itr)->set_value(t); 69 | } 70 | } 71 | private: 72 | fc::spin_yield_lock _prom_lock; 73 | std::deque::ptr> _promises; 74 | const char *const _name; 75 | }; 76 | } 77 | -------------------------------------------------------------------------------- /include/fc/unique_ptr.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | namespace fc 6 | { 7 | template 8 | class unique_ptr 9 | { 10 | public: 11 | typedef T* pointer; 12 | 13 | explicit unique_ptr( pointer t = nullptr ):_p(t){} 14 | 15 | unique_ptr( unique_ptr&& m ) 16 | :_p(m._p){ m._p = nullptr; } 17 | 18 | ~unique_ptr() { delete _p; } 19 | 20 | operator bool()const { return _p != nullptr; } 21 | friend bool operator==(const unique_ptr& p, nullptr_t) 22 | { 23 | return p._p == nullptr; 24 | } 25 | 26 | friend bool operator!=(const unique_ptr& p, nullptr_t) 27 | { 28 | return p._p != nullptr; 29 | } 30 | 31 | unique_ptr& operator=( nullptr_t ) 32 | { 33 | delete _p; _p = nullptr; 34 | } 35 | 36 | unique_ptr& operator=( unique_ptr&& o ) 37 | { 38 | fc_swap( _p, o._p ); 39 | return *this; 40 | } 41 | 42 | pointer operator->()const { return _p; } 43 | T& operator*()const { return *_p; } 44 | 45 | void reset( pointer v ) 46 | { 47 | delete _p; _p = v; 48 | } 49 | pointer release() 50 | { 51 | auto tmp = _p; 52 | _p = nullptr; 53 | return tmp; 54 | } 55 | 56 | private: 57 | pointer _p; 58 | }; 59 | 60 | } 61 | -------------------------------------------------------------------------------- /include/fc/utf8.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __UTF8_HPP 2 | #define __UTF8_HPP 3 | 4 | #include 5 | 6 | /// This file contains general purpose utilities related to UTF-8 <-> Unicode conversions 7 | 8 | namespace fc 9 | { 10 | 11 | bool is_utf8( const std::string& str ); 12 | 13 | /** Decodes utf 8 std::string into unicode string. 14 | @param input - input string to be decoded and stored in 'storage' 15 | @param storage - buffer for converted text. Cannot be nullptr. 16 | */ 17 | void decodeUtf8(const std::string& input, std::wstring* storage); 18 | 19 | /** Encodes given wide (unicode) string into UTF-8 representation. 20 | @param input - input string to be encoded and stored in 'storage' 21 | @param storage - buffer for converted text. Cannot be nullptr. 22 | */ 23 | void encodeUtf8(const std::wstring& input, std::string* storage); 24 | 25 | } /// namespace fc 26 | 27 | #endif ///__UTF8_HPP 28 | 29 | -------------------------------------------------------------------------------- /include/fc/utility.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | #ifdef _MSC_VER 6 | #pragma warning(disable: 4482) // nonstandard extension used enum Name::Val, standard in C++11 7 | #define NO_RETURN __declspec(noreturn) 8 | #else 9 | #define NO_RETURN __attribute__((noreturn)) 10 | #endif 11 | 12 | 13 | //namespace std { 14 | // typedef decltype(sizeof(int)) size_t; 15 | // typedef decltype(nullptr) nullptr_t; 16 | //} 17 | 18 | namespace fc { 19 | using std::size_t; 20 | typedef decltype(nullptr) nullptr_t; 21 | 22 | template struct remove_reference { typedef T type; }; 23 | template struct remove_reference { typedef T type; }; 24 | template struct remove_reference { typedef T type; }; 25 | 26 | template struct deduce { typedef T type; }; 27 | template struct deduce { typedef T type; }; 28 | template struct deduce { typedef T type; }; 29 | template struct deduce { typedef T type; }; 30 | template struct deduce{ typedef T type; }; 31 | 32 | template 33 | typename fc::remove_reference::type&& move( T&& t ) { return static_cast::type&&>(t); } 34 | 35 | template 36 | inline T&& forward( U&& u ) { return static_cast(u); } 37 | 38 | struct true_type { enum _value { value = 1 }; }; 39 | struct false_type { enum _value { value = 0 }; }; 40 | 41 | namespace detail { 42 | template fc::true_type is_class_helper(void(T::*)()); 43 | template fc::false_type is_class_helper(...); 44 | } 45 | 46 | template 47 | struct is_class { typedef decltype(detail::is_class_helper(0)) type; enum value_enum { value = type::value }; }; 48 | #ifdef min 49 | #undef min 50 | #endif 51 | template 52 | const T& min( const T& a, const T& b ) { return a < b ? a: b; } 53 | 54 | } 55 | // outside of namespace fc becuase of VC++ conflict with std::swap 56 | template 57 | void fc_swap( T& a, T& b ) { 58 | T tmp = fc::move(a); 59 | a = fc::move(b); 60 | b = fc::move(tmp); 61 | } 62 | 63 | #define LLCONST(constant) static_cast(constant##ll) 64 | #define ULLCONST(constant) static_cast(constant##ull) 65 | 66 | -------------------------------------------------------------------------------- /include/fc/vector.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | -------------------------------------------------------------------------------- /include/fc/vector_fwd.hpp: -------------------------------------------------------------------------------- 1 | #ifndef _FC_VECTOR_FWD_HPP_ 2 | #define _FC_VECTOR_FWD_HPP_ 3 | #if 0 4 | #include 5 | #else 6 | namespace fc { 7 | template class vector; 8 | }; 9 | #endif 10 | 11 | #endif // _FC_VECTOR_FWD_HPP_ 12 | -------------------------------------------------------------------------------- /include/fc/wait_any.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | namespace fc { 6 | template 7 | int wait_any( fc::future& f1, fc::future& f2, const microseconds& timeout_us = microseconds::max() ) { 8 | fc::vector p(2); 9 | p[0] = static_pointer_cast(f1.promise()); 10 | p[1] = static_pointer_cast(f2.promise()); 11 | return wait( fc::move(p), timeout_us ); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/byteswap.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #ifdef _MSC_VER 4 | # include 5 | # define bswap_64(x) _byteswap_uint64(x) 6 | #elif defined(__APPLE__) 7 | # include 8 | # define bswap_64(x) OSSwapInt64(x) 9 | #else 10 | # include 11 | #endif 12 | -------------------------------------------------------------------------------- /src/compress/zlib.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "miniz.c" 4 | 5 | namespace fc 6 | { 7 | string zlib_compress(const string& in) 8 | { 9 | size_t compressed_message_length; 10 | 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); 11 | string result(compressed_message, compressed_message_length); 12 | free(compressed_message); 13 | return result; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/crypto/_digest_common.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "_digest_common.hpp" 3 | 4 | namespace fc { namespace detail { 5 | static void shift_l( const uint8_t* in, uint8_t* out, std::size_t n, unsigned int i) { 6 | if (i < n) { 7 | memcpy( out, in + i, n-i ); 8 | } else { 9 | i = n; 10 | } 11 | memset( out + (n-i), 0, i ); 12 | } 13 | 14 | void shift_l( const char* in, char* out, std::size_t n, unsigned int i) { 15 | const uint8_t* in8 = (uint8_t*) in; 16 | uint8_t* out8 = (uint8_t*) out; 17 | 18 | if (i >= 8) { 19 | shift_l( in8, out8, n, i / 8 ); 20 | i &= 7; 21 | in8 = out8; 22 | } 23 | 24 | std::size_t p; 25 | for( p = 0; p < n-1; ++p ) 26 | out8[p] = (in8[p] << i) | (in8[p+1]>>(8-i)); 27 | out8[p] = in8[p] << i; 28 | } 29 | static void shift_r( const uint8_t* in, uint8_t* out, std::size_t n, unsigned int i) { 30 | if (i < n) { 31 | memcpy( out+i, in, n-i ); 32 | } else { 33 | i = n; 34 | } 35 | memset( out, 0, i ); 36 | } 37 | 38 | void shift_r( const char* in, char* out, std::size_t n, unsigned int i) { 39 | const uint8_t* in8 = (uint8_t*) in; 40 | uint8_t* out8 = (uint8_t*) out; 41 | 42 | if (i >= 8) { 43 | shift_r( in8, out8, n, i / 8 ); 44 | i &= 7; 45 | in8 = out8; 46 | } 47 | 48 | std::size_t p; 49 | for( p = n-1; p > 0; --p ) 50 | out8[p] = (in8[p] >> i) | (in8[p-1]<<(8-i)); 51 | out8[p] = in8[p] >> i; 52 | } 53 | }} 54 | -------------------------------------------------------------------------------- /src/crypto/_digest_common.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | /* Common stuff for cryptographic hashes 4 | */ 5 | namespace fc { namespace detail { 6 | void shift_l( const char* in, char* out, std::size_t n, unsigned int i); 7 | void shift_r( const char* in, char* out, std::size_t n, unsigned int i); 8 | }} 9 | -------------------------------------------------------------------------------- /src/crypto/_elliptic_impl_priv.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | /* private_key_impl based on libsecp256k1 5 | * used by mixed + secp256k1 6 | */ 7 | 8 | namespace fc { namespace ecc { namespace detail { 9 | 10 | 11 | const secp256k1_context_t* _get_context(); 12 | void _init_lib(); 13 | 14 | class private_key_impl 15 | { 16 | public: 17 | private_key_impl() BOOST_NOEXCEPT; 18 | private_key_impl( const private_key_impl& cpy ) BOOST_NOEXCEPT; 19 | 20 | private_key_impl& operator=( const private_key_impl& pk ) BOOST_NOEXCEPT; 21 | 22 | private_key_secret _key; 23 | }; 24 | 25 | }}} 26 | -------------------------------------------------------------------------------- /src/crypto/_elliptic_impl_pub.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | /* public_key_impl implementation based on openssl 6 | * used by mixed + openssl 7 | */ 8 | 9 | namespace fc { namespace ecc { namespace detail { 10 | 11 | void _init_lib(); 12 | 13 | class public_key_impl 14 | { 15 | public: 16 | public_key_impl() BOOST_NOEXCEPT; 17 | public_key_impl( const public_key_impl& cpy ) BOOST_NOEXCEPT; 18 | public_key_impl( public_key_impl&& cpy ) BOOST_NOEXCEPT; 19 | ~public_key_impl() BOOST_NOEXCEPT; 20 | 21 | public_key_impl& operator=( const public_key_impl& pk ) BOOST_NOEXCEPT; 22 | 23 | public_key_impl& operator=( public_key_impl&& pk ) BOOST_NOEXCEPT; 24 | 25 | static int ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, ECDSA_SIG *ecsig, const unsigned char *msg, int msglen, int recid, int check); 26 | 27 | EC_KEY* _key = nullptr; 28 | 29 | private: 30 | void free_key() BOOST_NOEXCEPT; 31 | }; 32 | 33 | }}} 34 | -------------------------------------------------------------------------------- /src/crypto/base32.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | namespace fc 5 | { 6 | std::vector from_base32( const std::string& b32 ) 7 | { 8 | auto len = cyoBase32DecodeGetLength( b32.size() ); 9 | std::vector v(len); 10 | len = cyoBase32Decode( v.data(), b32.c_str(), b32.size() ); 11 | v.resize( len ); 12 | return v; 13 | } 14 | 15 | std::string to_base32( const char* data, size_t len ) 16 | { 17 | auto s = cyoBase32EncodeGetLength(len); 18 | std::vector b32; 19 | b32.resize(s); 20 | cyoBase32Encode( b32.data(), data, len ); 21 | b32.resize( b32.size()-1); // strip the nullterm 22 | return std::string(b32.begin(),b32.end()); 23 | } 24 | 25 | std::string to_base32( const std::vector& vec ) 26 | { 27 | return to_base32( vec.data(), vec.size() ); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/crypto/base36.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | namespace fc 7 | { 8 | fc::string to_base36( const char* data, size_t len ) 9 | { 10 | if( len == 0 ) return fc::string(); 11 | 12 | const char* src = data; 13 | int src_len = len; 14 | std::unique_ptr buffer(new char[len+1]); 15 | if (*data & 0x80) { 16 | buffer[0] = 0; 17 | memcpy( buffer.get() + 1, data, len ); 18 | src = buffer.get(); 19 | src_len++; 20 | } 21 | fc::bigint value( src, src_len ); 22 | 23 | auto base36 = "0123456789abcdefghijklmnopqrstuvwxyz"; 24 | std::vector out( static_cast(len * 1.6) + 2 ); 25 | int pos = out.size() - 1; 26 | out[pos] = '\0'; 27 | fc::bigint _36(36); 28 | do { 29 | if( value ) { 30 | --pos; 31 | out[pos] = base36[(value % _36).to_int64()]; 32 | } 33 | } while (value /= _36); 34 | while (len-- > 0 && *data++ == 0) { 35 | out[--pos] = '0'; 36 | } 37 | return &out[pos]; //fc::string( &out[pos], out.size() - pos); 38 | } 39 | 40 | fc::string to_base36( const std::vector& vec ) 41 | { 42 | return to_base36( (const char*)vec.data(), vec.size() ); 43 | } 44 | 45 | std::vector from_base36( const fc::string& b36 ) 46 | { 47 | if ( b36.empty() ) { 48 | std::vector empty; 49 | return empty; 50 | } 51 | 52 | fc::bigint value; 53 | 54 | fc::bigint pos = 0; 55 | fc::bigint _36(36); 56 | for( auto itr = b36.rbegin(); itr != b36.rend(); ++itr ) 57 | { 58 | if( *itr >= '0' && *itr <= '9' ) 59 | value = value + _36.exp(pos) * fc::bigint(*itr - '0'); 60 | else if( *itr >= 'a' && *itr <= 'z' ) 61 | value = value + (_36.exp(pos) * fc::bigint(10+*itr - 'a')); 62 | else if( *itr >= 'A' && *itr <= 'Z' ) 63 | value = value + (_36.exp(pos) * fc::bigint(10+*itr - 'A')); 64 | else 65 | { 66 | wlog("unknown '${char}'", ("char",fc::string(&*itr,1)) ); 67 | } 68 | ++pos; 69 | } 70 | 71 | std::vector bytes = value; 72 | int leading_zeros = 0, len = bytes.size(); 73 | const char *in = b36.c_str(); 74 | while (*in++ == '0') { leading_zeros++; } 75 | char* first = bytes.data(); 76 | while (len > 0 && *first == 0) { first++; len--; } 77 | std::vector result; 78 | result.resize(leading_zeros + len, 0); 79 | memcpy( result.data() + leading_zeros, first, len ); 80 | return result; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/crypto/dh.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace fc { 5 | SSL_TYPE(ssl_dh, DH, DH_free) 6 | 7 | static bool validate( const ssl_dh& dh, bool& valid ) { 8 | int check; 9 | DH_check(dh,&check); 10 | return valid = !(check /*& DH_CHECK_P_NOT_SAFE_PRIME*/); 11 | } 12 | 13 | bool diffie_hellman::generate_params( int s, uint8_t g ) 14 | { 15 | ssl_dh dh = DH_generate_parameters( s, g, NULL, NULL ); 16 | p.resize( BN_num_bytes( dh->p ) ); 17 | if( p.size() ) 18 | BN_bn2bin( dh->p, (unsigned char*)&p.front() ); 19 | this->g = g; 20 | return fc::validate( dh, valid ); 21 | } 22 | 23 | bool diffie_hellman::validate() 24 | { 25 | if( !p.size() ) 26 | return valid = false; 27 | ssl_dh dh = DH_new(); 28 | dh->p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL ); 29 | dh->g = BN_bin2bn( (unsigned char*)&g, 1, NULL ); 30 | return fc::validate( dh, valid ); 31 | } 32 | 33 | bool diffie_hellman::generate_pub_key() 34 | { 35 | if( !p.size() ) 36 | return valid = false; 37 | ssl_dh dh = DH_new(); 38 | dh->p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL ); 39 | dh->g = BN_bin2bn( (unsigned char*)&g, 1, NULL ); 40 | 41 | if( !fc::validate( dh, valid ) ) 42 | { 43 | return false; 44 | } 45 | DH_generate_key(dh); 46 | 47 | pub_key.resize( BN_num_bytes( dh->pub_key ) ); 48 | priv_key.resize( BN_num_bytes( dh->priv_key ) ); 49 | if( pub_key.size() ) 50 | BN_bn2bin( dh->pub_key, (unsigned char*)&pub_key.front() ); 51 | if( priv_key.size() ) 52 | BN_bn2bin( dh->priv_key, (unsigned char*)&priv_key.front() ); 53 | 54 | return true; 55 | } 56 | bool diffie_hellman::compute_shared_key( const char* buf, uint32_t s ) { 57 | ssl_dh dh = DH_new(); 58 | dh->p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL ); 59 | dh->pub_key = BN_bin2bn( (unsigned char*)&pub_key.front(), pub_key.size(), NULL ); 60 | dh->priv_key = BN_bin2bn( (unsigned char*)&priv_key.front(), priv_key.size(), NULL ); 61 | dh->g = BN_bin2bn( (unsigned char*)&g, 1, NULL ); 62 | 63 | int check; 64 | DH_check(dh,&check); 65 | if( !fc::validate( dh, valid ) ) 66 | { 67 | return false; 68 | } 69 | 70 | ssl_bignum pk; 71 | BN_bin2bn( (unsigned char*)buf, s, pk ); 72 | shared_key.resize( DH_size(dh) ); 73 | DH_compute_key( (unsigned char*)&shared_key.front(), pk, dh ); 74 | 75 | return true; 76 | } 77 | bool diffie_hellman::compute_shared_key( const std::vector& pubk ) { 78 | return compute_shared_key( &pubk.front(), pubk.size() ); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/crypto/elliptic_mixed.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | 13 | #include "_elliptic_impl_priv.hpp" 14 | #include "_elliptic_impl_pub.hpp" 15 | 16 | namespace fc { namespace ecc { 17 | namespace detail 18 | { 19 | const secp256k1_context_t* _get_context() { 20 | static secp256k1_context_t* ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN); 21 | return ctx; 22 | } 23 | 24 | void _init_lib() { 25 | static const secp256k1_context_t* ctx = _get_context(); 26 | static int init_o = init_openssl(); 27 | } 28 | } 29 | 30 | static const private_key_secret empty_priv; 31 | fc::sha512 private_key::get_shared_secret( const public_key& other )const 32 | { 33 | FC_ASSERT( my->_key != empty_priv ); 34 | FC_ASSERT( other.my->_key != nullptr ); 35 | public_key_data pub(other.serialize()); 36 | FC_ASSERT( secp256k1_ec_pubkey_tweak_mul( detail::_get_context(), (unsigned char*) pub.begin(), pub.size(), (unsigned char*) my->_key.data() ) ); 37 | return fc::sha512::hash( pub.begin() + 1, pub.size() - 1 ); 38 | } 39 | 40 | } } 41 | -------------------------------------------------------------------------------- /src/crypto/hex.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace fc { 5 | 6 | uint8_t from_hex( char c ) { 7 | if( c >= '0' && c <= '9' ) 8 | return c - '0'; 9 | if( c >= 'a' && c <= 'f' ) 10 | return c - 'a' + 10; 11 | if( c >= 'A' && c <= 'F' ) 12 | return c - 'A' + 10; 13 | FC_THROW_EXCEPTION( exception, "Invalid hex character '${c}'", ("c", fc::string(&c,1) ) ); 14 | return 0; 15 | } 16 | 17 | std::string to_hex( const char* d, uint32_t s ) 18 | { 19 | std::string r; 20 | const char* to_hex="0123456789abcdef"; 21 | uint8_t* c = (uint8_t*)d; 22 | for( uint32_t i = 0; i < s; ++i ) 23 | (r += to_hex[(c[i]>>4)]) += to_hex[(c[i] &0x0f)]; 24 | return r; 25 | } 26 | 27 | size_t from_hex( const fc::string& hex_str, char* out_data, size_t out_data_len ) { 28 | fc::string::const_iterator i = hex_str.begin(); 29 | uint8_t* out_pos = (uint8_t*)out_data; 30 | uint8_t* out_end = out_pos + out_data_len; 31 | while( i != hex_str.end() && out_end != out_pos ) { 32 | *out_pos = from_hex( *i ) << 4; 33 | ++i; 34 | if( i != hex_str.end() ) { 35 | *out_pos |= from_hex( *i ); 36 | ++i; 37 | } 38 | ++out_pos; 39 | } 40 | return out_pos - (uint8_t*)out_data; 41 | } 42 | std::string to_hex( const std::vector& data ) 43 | { 44 | if( data.size() ) 45 | return to_hex( data.data(), data.size() ); 46 | return ""; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/crypto/openssl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | #include 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | namespace fc 12 | { 13 | struct openssl_scope 14 | { 15 | static path _configurationFilePath; 16 | openssl_scope() 17 | { 18 | ERR_load_crypto_strings(); 19 | OpenSSL_add_all_algorithms(); 20 | 21 | const boost::filesystem::path& boostPath = _configurationFilePath; 22 | if(boostPath.empty() == false) 23 | { 24 | std::string varSetting("OPENSSL_CONF="); 25 | varSetting += _configurationFilePath.to_native_ansi_path(); 26 | #if defined(WIN32) 27 | _putenv((char*)varSetting.c_str()); 28 | #else 29 | putenv((char*)varSetting.c_str()); 30 | #endif 31 | } 32 | 33 | OPENSSL_config(nullptr); 34 | } 35 | 36 | ~openssl_scope() 37 | { 38 | EVP_cleanup(); 39 | ERR_free_strings(); 40 | } 41 | }; 42 | 43 | path openssl_scope::_configurationFilePath; 44 | 45 | void store_configuration_path(const path& filePath) 46 | { 47 | openssl_scope::_configurationFilePath = filePath; 48 | } 49 | 50 | int init_openssl() 51 | { 52 | static openssl_scope ossl; 53 | return 0; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/crypto/rand.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | namespace fc { 8 | 9 | void rand_bytes(char* buf, int count) 10 | { 11 | static int init = init_openssl(); 12 | 13 | int result = RAND_bytes((unsigned char*)buf, count); 14 | if (result != 1) 15 | FC_THROW("Error calling OpenSSL's RAND_bytes(): ${code}", ("code", (uint32_t)ERR_get_error())); 16 | } 17 | 18 | void rand_pseudo_bytes(char* buf, int count) 19 | { 20 | static int init = init_openssl(); 21 | 22 | int result = RAND_pseudo_bytes((unsigned char*)buf, count); 23 | if (result == -1) 24 | FC_THROW("Error calling OpenSSL's RAND_pseudo_bytes(): ${code}", ("code", (uint32_t)ERR_get_error())); 25 | } 26 | 27 | } // namespace fc 28 | -------------------------------------------------------------------------------- /src/git_revision.cpp.in: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define FC_GIT_REVISION_SHA "@FC_GIT_REVISION_SHA@" 4 | #define FC_GIT_REVISION_UNIX_TIMESTAMP @FC_GIT_REVISION_UNIX_TIMESTAMP@ 5 | 6 | namespace fc { 7 | 8 | const char* const git_revision_sha = FC_GIT_REVISION_SHA; 9 | const uint32_t git_revision_unix_timestamp = FC_GIT_REVISION_UNIX_TIMESTAMP; 10 | 11 | } // end namespace fc 12 | -------------------------------------------------------------------------------- /src/interprocess/file_mapping.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | namespace fc { 7 | 8 | 9 | file_mapping::file_mapping( const char* file, mode_t m ) : 10 | my(file, m == read_only ? boost::interprocess::read_only : boost::interprocess::read_write ) 11 | {} 12 | 13 | file_mapping::~file_mapping() {} 14 | 15 | 16 | 17 | mapped_region::mapped_region( const file_mapping& fm, mode_t m, uint64_t start, size_t size ) : 18 | my( *fm.my, m == read_only ? boost::interprocess::read_only : boost::interprocess::read_write ,start, size) 19 | {} 20 | 21 | mapped_region::mapped_region( const file_mapping& fm, mode_t m ) : 22 | my( *fm.my, m == read_only ? boost::interprocess::read_only : boost::interprocess::read_write) 23 | {} 24 | 25 | mapped_region::~mapped_region(){} 26 | 27 | void* mapped_region::get_address() const 28 | { 29 | return my->get_address(); 30 | } 31 | 32 | void mapped_region::flush() 33 | { 34 | my->flush(); 35 | } 36 | 37 | size_t mapped_region::get_size() const 38 | { 39 | return my->get_size(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/interprocess/mmap_struct.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | #include 6 | 7 | #include 8 | #include 9 | 10 | namespace fc 11 | { 12 | namespace detail 13 | { 14 | size_t mmap_struct_base::size()const { return _mapped_region->get_size(); } 15 | void mmap_struct_base::flush() 16 | { 17 | _mapped_region->flush(); 18 | } 19 | 20 | void mmap_struct_base::open( const fc::path& file, size_t s, bool create ) 21 | { 22 | if( !fc::exists( file ) || fc::file_size(file) != s ) 23 | { 24 | fc::ofstream out( file ); 25 | char buffer[1024]; 26 | memset( buffer, 0, sizeof(buffer) ); 27 | 28 | size_t bytes_left = s; 29 | while( bytes_left > 0 ) 30 | { 31 | size_t to_write = std::min(bytes_left, sizeof(buffer) ); 32 | out.write( buffer, to_write ); 33 | bytes_left -= to_write; 34 | } 35 | } 36 | 37 | std::string filePath = file.to_native_ansi_path(); 38 | 39 | _file_mapping.reset( new fc::file_mapping( filePath.c_str(), fc::read_write ) ); 40 | _mapped_region.reset( new fc::mapped_region( *_file_mapping, fc::read_write, 0, s ) ); 41 | } 42 | } // namespace fc 43 | 44 | } // namespace fc 45 | -------------------------------------------------------------------------------- /src/interprocess/signals.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace fc 5 | { 6 | void set_signal_handler( std::function handler, int signal_num ) 7 | { 8 | std::shared_ptr sig_set(new boost::asio::signal_set(fc::asio::default_io_service(), signal_num)); 9 | sig_set->async_wait( 10 | [sig_set,handler]( const boost::system::error_code& err, int num ) 11 | { 12 | handler( num ); 13 | sig_set->cancel(); 14 | // set_signal_handler( handler, signal_num ); 15 | } ); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/io/console.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace fc { 5 | 6 | #ifdef WIN32 7 | #include 8 | 9 | void set_console_echo( bool enable_echo ) 10 | { 11 | auto stdin_handle = GetStdHandle( STD_INPUT_HANDLE ); 12 | DWORD mode = 0; 13 | GetConsoleMode( stdin_handle, &mode ); 14 | if( enable_echo ) 15 | { 16 | SetConsoleMode( stdin_handle, mode | ENABLE_ECHO_INPUT ); 17 | } 18 | else 19 | { 20 | SetConsoleMode( stdin_handle, mode & (~ENABLE_ECHO_INPUT) ); 21 | } 22 | } 23 | 24 | #else // NOT WIN32 25 | #include 26 | #include 27 | 28 | void set_console_echo( bool enable_echo ) 29 | { 30 | termios oldt; 31 | tcgetattr(STDIN_FILENO, &oldt); 32 | termios newt = oldt; 33 | if( enable_echo ) 34 | { 35 | newt.c_lflag |= ECHO; 36 | } 37 | else 38 | { 39 | newt.c_lflag &= ~ECHO; 40 | } 41 | tcsetattr(STDIN_FILENO, TCSANOW, &newt); 42 | } 43 | 44 | #endif // WIN32 45 | 46 | } // namespace fc 47 | -------------------------------------------------------------------------------- /src/io/datastream.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | NO_RETURN void fc::detail::throw_datastream_range_error(char const* method, size_t len, int64_t over) 5 | { 6 | FC_THROW_EXCEPTION( out_of_range_exception, "${method} datastream of length ${len} over by ${over}", ("method",fc::string(method))("len",len)("over",over) ); 7 | } 8 | -------------------------------------------------------------------------------- /src/io/varint.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace fc 5 | { 6 | void to_variant( const signed_int& var, variant& vo ) { vo = var.value; } 7 | void from_variant( const variant& var, signed_int& vo ) { vo.value = static_cast(var.as_int64()); } 8 | void to_variant( const unsigned_int& var, variant& vo ) { vo = var.value; } 9 | void from_variant( const variant& var, unsigned_int& vo ) { vo.value = static_cast(var.as_uint64()); } 10 | } 11 | -------------------------------------------------------------------------------- /src/log/appender.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "console_defines.h" 13 | 14 | 15 | namespace fc { 16 | 17 | std::unordered_map& get_appender_map() { 18 | static std::unordered_map lm; 19 | return lm; 20 | } 21 | std::unordered_map& get_appender_factory_map() { 22 | static std::unordered_map lm; 23 | return lm; 24 | } 25 | appender::ptr appender::get( const fc::string& s ) { 26 | static fc::spin_lock appender_spinlock; 27 | scoped_lock lock(appender_spinlock); 28 | return get_appender_map()[s]; 29 | } 30 | bool appender::register_appender( const fc::string& type, const appender_factory::ptr& f ) 31 | { 32 | get_appender_factory_map()[type] = f; 33 | return true; 34 | } 35 | appender::ptr appender::create( const fc::string& name, const fc::string& type, const variant& args ) 36 | { 37 | auto fact_itr = get_appender_factory_map().find(type); 38 | if( fact_itr == get_appender_factory_map().end() ) { 39 | //wlog( "Unknown appender type '%s'", type.c_str() ); 40 | return appender::ptr(); 41 | } 42 | auto ap = fact_itr->second->create( args ); 43 | get_appender_map()[name] = ap; 44 | return ap; 45 | } 46 | 47 | static bool reg_console_appender = appender::register_appender( "console" ); 48 | static bool reg_file_appender = appender::register_appender( "file" ); 49 | static bool reg_gelf_appender = appender::register_appender( "gelf" ); 50 | 51 | } // namespace fc 52 | -------------------------------------------------------------------------------- /src/network/resolve.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | namespace fc 6 | { 7 | std::vector resolve( const fc::string& host, uint16_t port ) 8 | { 9 | auto ep = fc::asio::tcp::resolve( host, std::to_string(uint64_t(port)) ); 10 | std::vector eps; 11 | eps.reserve(ep.size()); 12 | for( auto itr = ep.begin(); itr != ep.end(); ++itr ) 13 | { 14 | if( itr->address().is_v4() ) 15 | { 16 | eps.push_back( fc::ip::endpoint(itr->address().to_v4().to_ulong(), itr->port()) ); 17 | } 18 | // TODO: add support for v6 19 | } 20 | return eps; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/rpc/state.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | namespace fc { namespace rpc { 6 | state::~state() 7 | { 8 | close(); 9 | } 10 | 11 | void state::add_method( const fc::string& name, method m ) 12 | { 13 | _methods.emplace(std::pair(name,fc::move(m))); 14 | } 15 | 16 | void state::remove_method( const fc::string& name ) 17 | { 18 | _methods.erase(name); 19 | } 20 | 21 | variant state::local_call( const string& method_name, const variants& args ) 22 | { 23 | auto method_itr = _methods.find(method_name); 24 | if( method_itr == _methods.end() && _unhandled ) 25 | return _unhandled( method_name, args ); 26 | FC_ASSERT( method_itr != _methods.end(), "Unknown Method: ${name}", ("name",method_name) ); 27 | return method_itr->second(args); 28 | } 29 | 30 | void state::handle_reply( const response& response ) 31 | { 32 | auto await = _awaiting.find( response.id ); 33 | FC_ASSERT( await != _awaiting.end(), "Unknown Response ID: ${id}", ("id",response.id)("response",response) ); 34 | if( response.result ) 35 | await->second->set_value( *response.result ); 36 | else if( response.error ) 37 | { 38 | await->second->set_exception( exception_ptr(new FC_EXCEPTION( exception, "${error}", ("error",response.error->message)("data",response) ) ) ); 39 | } 40 | else 41 | await->second->set_value( fc::variant() ); 42 | _awaiting.erase(await); 43 | } 44 | 45 | request state::start_remote_call( const string& method_name, variants args ) 46 | { 47 | request request{ _next_id++, method_name, std::move(args) }; 48 | _awaiting[*request.id] = fc::promise::ptr( new fc::promise("json_connection::async_call") ); 49 | return request; 50 | } 51 | variant state::wait_for_response( uint64_t request_id ) 52 | { 53 | auto itr = _awaiting.find(request_id); 54 | FC_ASSERT( itr != _awaiting.end() ); 55 | return fc::future( itr->second ).wait(); 56 | } 57 | void state::close() 58 | { 59 | for( auto item : _awaiting ) 60 | item.second->set_exception( fc::exception_ptr(new FC_EXCEPTION( eof_exception, "connection closed" )) ); 61 | _awaiting.clear(); 62 | } 63 | void state::on_unhandled( const std::function& unhandled ) 64 | { 65 | _unhandled = unhandled; 66 | } 67 | 68 | } } // namespace fc::rpc 69 | -------------------------------------------------------------------------------- /src/shared_ptr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | namespace fc { 7 | retainable::retainable() 8 | :_ref_count(1) { 9 | static_assert( sizeof(_ref_count) == sizeof(boost::atomic), "failed to reserve enough space" ); 10 | } 11 | 12 | retainable::~retainable() { 13 | assert( _ref_count <= 0 ); 14 | assert( _ref_count == 0 ); 15 | } 16 | void retainable::retain() { 17 | ((boost::atomic*)&_ref_count)->fetch_add(1, boost::memory_order_relaxed ); 18 | } 19 | 20 | void retainable::release() { 21 | boost::atomic_thread_fence(boost::memory_order_acquire); 22 | if( 1 == ((boost::atomic*)&_ref_count)->fetch_sub(1, boost::memory_order_release ) ) { 23 | delete this; 24 | } 25 | } 26 | 27 | int32_t retainable::retain_count()const { 28 | return _ref_count; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/thread/non_preemptable_scope_check.cpp: -------------------------------------------------------------------------------- 1 | #ifndef NDEBUG 2 | #include 3 | #include 4 | #include "thread_d.hpp" 5 | 6 | 7 | namespace fc 8 | { 9 | non_preemptable_scope_check::non_preemptable_scope_check() 10 | { 11 | ++thread::current().my->non_preemptable_scope_count; 12 | } 13 | 14 | non_preemptable_scope_check::~non_preemptable_scope_check() 15 | { 16 | assert(thread::current().my->non_preemptable_scope_count > 0); 17 | --thread::current().my->non_preemptable_scope_count; 18 | } 19 | } // fc 20 | #endif -------------------------------------------------------------------------------- /src/thread/spin_lock.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | namespace fc { 8 | #define define_self boost::atomic* self = (boost::atomic*)&_lock 9 | spin_lock::spin_lock() 10 | { 11 | define_self; 12 | new (self) boost::atomic(); 13 | static_assert( sizeof(boost::atomic) == sizeof(_lock), "" ); 14 | self->store(unlocked); 15 | } 16 | 17 | bool spin_lock::try_lock() { 18 | define_self; 19 | return self->exchange(locked, boost::memory_order_acquire)!=locked; 20 | } 21 | 22 | bool spin_lock::try_lock_for( const fc::microseconds& us ) { 23 | return try_lock_until( fc::time_point::now() + us ); 24 | } 25 | 26 | bool spin_lock::try_lock_until( const fc::time_point& abs_time ) { 27 | while( abs_time > time_point::now() ) { 28 | if( try_lock() ) 29 | return true; 30 | } 31 | return false; 32 | } 33 | 34 | void spin_lock::lock() { 35 | define_self; 36 | while( self->exchange(locked, boost::memory_order_acquire)==locked) { } 37 | } 38 | 39 | void spin_lock::unlock() { 40 | define_self; 41 | self->store(unlocked, boost::memory_order_release); 42 | } 43 | 44 | #undef define_self 45 | 46 | } // namespace fc 47 | -------------------------------------------------------------------------------- /src/thread/spin_yield_lock.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | namespace fc { 8 | void yield(); 9 | 10 | #define define_self boost::atomic* self = (boost::atomic*)&_lock 11 | 12 | spin_yield_lock::spin_yield_lock() 13 | { 14 | define_self; 15 | new (self) boost::atomic(); 16 | static_assert( sizeof(boost::atomic) == sizeof(_lock), "" ); 17 | self->store(unlocked); 18 | } 19 | 20 | bool spin_yield_lock::try_lock() { 21 | define_self; 22 | return self->exchange(locked, boost::memory_order_acquire)!=locked; 23 | } 24 | 25 | bool spin_yield_lock::try_lock_for( const fc::microseconds& us ) { 26 | return try_lock_until( fc::time_point::now() + us ); 27 | } 28 | 29 | bool spin_yield_lock::try_lock_until( const fc::time_point& abs_time ) { 30 | while( abs_time > time_point::now() ) { 31 | if( try_lock() ) 32 | return true; 33 | yield(); 34 | } 35 | return false; 36 | } 37 | 38 | void spin_yield_lock::lock() { 39 | define_self; 40 | while( self->exchange(locked, boost::memory_order_acquire)==locked) { 41 | yield(); 42 | } 43 | } 44 | 45 | void spin_yield_lock::unlock() { 46 | define_self; 47 | self->store(unlocked, boost::memory_order_release); 48 | } 49 | #undef define_self 50 | 51 | } // namespace fc 52 | -------------------------------------------------------------------------------- /src/thread/thread_specific.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "thread_d.hpp" 4 | #include 5 | 6 | namespace fc 7 | { 8 | namespace detail 9 | { 10 | boost::atomic thread_specific_slot_counter; 11 | unsigned get_next_unused_thread_storage_slot() 12 | { 13 | return thread_specific_slot_counter.fetch_add(1); 14 | } 15 | 16 | void* get_specific_data(std::vector *specific_data, unsigned slot) 17 | { 18 | return slot < specific_data->size() ? 19 | (*specific_data)[slot].value : nullptr; 20 | } 21 | void set_specific_data(std::vector *specific_data, unsigned slot, void* new_value, void(*cleanup)(void*)) 22 | { 23 | if (slot + 1 > specific_data->size()) 24 | specific_data->resize(slot + 1); 25 | (*specific_data)[slot] = std::move(detail::specific_data_info(new_value, cleanup)); 26 | } 27 | 28 | void* get_thread_specific_data(unsigned slot) 29 | { 30 | return get_specific_data(&thread::current().my->thread_specific_data, slot); 31 | } 32 | void set_thread_specific_data(unsigned slot, void* new_value, void(*cleanup)(void*)) 33 | { 34 | return set_specific_data(&thread::current().my->thread_specific_data, slot, new_value, cleanup); 35 | } 36 | 37 | unsigned get_next_unused_task_storage_slot() 38 | { 39 | return thread::current().my->next_unused_task_storage_slot++; 40 | } 41 | void* get_task_specific_data(unsigned slot) 42 | { 43 | context* current_context = thread::current().my->current; 44 | if (!current_context || 45 | !current_context->cur_task) 46 | return get_specific_data(&thread::current().my->non_task_specific_data, slot); 47 | if (current_context->cur_task->_task_specific_data) 48 | return get_specific_data(current_context->cur_task->_task_specific_data, slot); 49 | return nullptr; 50 | } 51 | void set_task_specific_data(unsigned slot, void* new_value, void(*cleanup)(void*)) 52 | { 53 | context* current_context = thread::current().my->current; 54 | if (!current_context || 55 | !current_context->cur_task) 56 | set_specific_data(&thread::current().my->non_task_specific_data, slot, new_value, cleanup); 57 | else 58 | { 59 | if (!current_context->cur_task->_task_specific_data) 60 | current_context->cur_task->_task_specific_data = new std::vector; 61 | set_specific_data(current_context->cur_task->_task_specific_data, slot, new_value, cleanup); 62 | } 63 | } 64 | } 65 | } // end namespace fc -------------------------------------------------------------------------------- /src/utf8.cpp: -------------------------------------------------------------------------------- 1 | #include "fc/utf8.hpp" 2 | 3 | #include "utf8/checked.h" 4 | #include "utf8/core.h" 5 | #include "utf8/unchecked.h" 6 | 7 | #include 8 | 9 | namespace fc { 10 | 11 | bool is_utf8( const std::string& str ) 12 | { 13 | return utf8::is_valid( str.begin(), str.end() ); 14 | } 15 | 16 | void decodeUtf8(const std::string& input, std::wstring* storage) 17 | { 18 | assert(storage != nullptr); 19 | 20 | utf8::utf8to32(input.begin(), input.end(), std::back_inserter(*storage)); 21 | } 22 | 23 | void encodeUtf8(const std::wstring& input, std::string* storage) 24 | { 25 | assert(storage != nullptr); 26 | 27 | utf8::utf32to8(input.begin(), input.end(), std::back_inserter(*storage)); 28 | } 29 | 30 | } ///namespace fc 31 | 32 | 33 | -------------------------------------------------------------------------------- /src/utf8/ReleaseNotes: -------------------------------------------------------------------------------- 1 | utf8 cpp library 2 | Release 2.3.4 3 | 4 | A minor bug fix release. Thanks to all who reported bugs. 5 | 6 | Note: Version 2.3.3 contained a regression, and therefore was removed. 7 | 8 | Changes from version 2.3.2 9 | - Bug fix [39]: checked.h Line 273 and unchecked.h Line 182 have an extra ';' 10 | - Bug fix [36]: replace_invalid() only works with back_inserter 11 | 12 | Files included in the release: utf8.h, core.h, checked.h, unchecked.h, utf8cpp.html, ReleaseNotes 13 | -------------------------------------------------------------------------------- /tests/all_tests.cpp: -------------------------------------------------------------------------------- 1 | #define BOOST_TEST_MODULE AllTests 2 | #include 3 | 4 | -------------------------------------------------------------------------------- /tests/compress/compress.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | BOOST_AUTO_TEST_SUITE(compress) 10 | 11 | BOOST_AUTO_TEST_CASE(smaz_test) 12 | { 13 | std::ifstream testfile; 14 | testfile.open("README.md"); 15 | 16 | std::stringstream buffer; 17 | std::string line; 18 | std::getline( testfile, line ); 19 | while( testfile.good() ) 20 | { 21 | buffer << line << "\n"; 22 | try { 23 | std::string compressed = fc::smaz_compress( line ); 24 | std::string decomp = fc::smaz_decompress( compressed ); 25 | BOOST_CHECK_EQUAL( decomp, line ); 26 | } 27 | catch ( fc::exception& e ) 28 | { 29 | std::cout< 2 | 3 | #include 4 | #include 5 | 6 | BOOST_AUTO_TEST_SUITE(fc_crypto) 7 | 8 | BOOST_AUTO_TEST_CASE(bigint_test_1) 9 | { 10 | int64_t counter = 0, accu = 0, c_sq; 11 | fc::bigint bi_accu(accu); 12 | do { 13 | c_sq = counter * counter; 14 | fc::bigint add(c_sq); 15 | bi_accu += add; 16 | accu += c_sq; 17 | BOOST_CHECK( fc::bigint(accu) == bi_accu ); 18 | 19 | bi_accu = bi_accu + add; 20 | accu = accu + c_sq; 21 | BOOST_CHECK_EQUAL( accu, bi_accu.to_int64() ); 22 | 23 | bi_accu = fc::bigint( bi_accu.dup() ); 24 | 25 | counter++; 26 | } while (c_sq < 1000000); 27 | 28 | fc::variant test; 29 | fc::to_variant( bi_accu, test ); 30 | fc::bigint other; 31 | fc::from_variant( test, other ); 32 | BOOST_CHECK( other == bi_accu ); 33 | } 34 | 35 | BOOST_AUTO_TEST_CASE(bigint_test_2) 36 | { 37 | const fc::bigint bi_1(1), bi_3(3), bi_17(17), bi_65537(65537); 38 | fc::bigint bi_accu(bi_1); 39 | do { 40 | std::vector bytes = bi_accu; 41 | fc::bigint a_1( bytes ); 42 | a_1 = a_1 + bi_1; 43 | BOOST_CHECK( bi_accu < a_1 ); 44 | 45 | bi_accu = a_1 * bi_accu; 46 | BOOST_CHECK( bi_accu >= a_1 ); 47 | } while ( bi_accu.log2() <= 128 ); 48 | 49 | bi_accu = bi_accu; 50 | 51 | BOOST_CHECK( bi_accu && !bi_accu.is_negative() && bi_accu != bi_1 ); 52 | 53 | BOOST_CHECK( bi_3.exp( bi_accu.log2() ) > bi_accu ); 54 | 55 | fc::bigint big(1); 56 | big <<= 30; big += bi_17; big <<= 30; big++; 57 | big <<= 30; big -= bi_17; big >>= 5; big--; 58 | fc::bigint rest = bi_accu % big; 59 | BOOST_CHECK( (bi_accu - rest) / big == bi_accu / big ); 60 | 61 | fc::bigint big2; big2 = big; 62 | big2 *= bi_65537.exp(3); 63 | big2 /= bi_65537.exp(3); 64 | BOOST_CHECK( big2 == big ); 65 | big--; 66 | 67 | BOOST_CHECK_EQUAL( (std::string) bi_1, "1" ); 68 | BOOST_CHECK_EQUAL( (std::string) bi_3, "3" ); 69 | BOOST_CHECK_EQUAL( (std::string) bi_17, "17" ); 70 | BOOST_CHECK_EQUAL( (std::string) bi_65537, "65537" ); 71 | BOOST_CHECK_EQUAL( (std::string) bi_65537.exp(3), "281487861809153" ); 72 | BOOST_CHECK_EQUAL( (std::string) bi_accu, "12864938683278671740537145998360961546653259485195806" ); 73 | BOOST_CHECK_EQUAL( (std::string) big, "38685626840157682946539517" ); 74 | } 75 | 76 | BOOST_AUTO_TEST_SUITE_END() 77 | 78 | -------------------------------------------------------------------------------- /tests/crypto/ecc-interop.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | #TIME=time 4 | 5 | cd "`dirname $0`"/.. 6 | 7 | echo Building ecc_test with openssl... 8 | ( 9 | cmake -D ECC_IMPL=openssl . 10 | make ecc_test 11 | mv ecc_test ecc_test.openssl 12 | ) >/dev/null 2>&1 13 | 14 | echo Building ecc_test with secp256k1... 15 | ( 16 | cmake -D ECC_IMPL=secp256k1 . 17 | make ecc_test 18 | mv ecc_test ecc_test.secp256k1 19 | ) >/dev/null 2>&1 20 | 21 | echo Building ecc_test with mixed... 22 | ( 23 | cmake -D ECC_IMPL=mixed . 24 | make ecc_test 25 | mv ecc_test ecc_test.mixed 26 | ) >/dev/null 2>&1 27 | 28 | run () { 29 | echo "Running ecc_test.$1 test ecc.interop.$2 ..." 30 | $TIME "./ecc_test.$1" test "ecc.interop.$2" 31 | } 32 | 33 | run openssl openssl 34 | run openssl openssl 35 | run secp256k1 secp256k1 36 | run secp256k1 secp256k1 37 | run mixed mixed 38 | run mixed mixed 39 | run openssl secp256k1 40 | run openssl mixed 41 | run secp256k1 openssl 42 | run secp256k1 mixed 43 | run mixed openssl 44 | run mixed secp256k1 45 | 46 | echo Done. 47 | 48 | rm -f ecc_test.openssl ecc_test.secp256k1 ecc_test.mixed ecc.interop.openssl ecc.interop.secp256k1 ecc.interop.mixed 49 | -------------------------------------------------------------------------------- /tests/crypto/rand_test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | #include 6 | 7 | static void check_randomness( const char* buffer, size_t len ) { 8 | if (len == 0) { return; } 9 | // count bit runs and 0's / 1's 10 | unsigned int zc = 0, oc = 0, rc = 0, last = 2; 11 | for (size_t k = len; k; k--) { 12 | char c = *buffer++; 13 | for (int i = 0; i < 8; i++) { 14 | unsigned int bit = c & 1; 15 | c >>= 1; 16 | if (bit) { oc++; } else { zc++; } 17 | if (bit != last) { rc++; last = bit; } 18 | } 19 | } 20 | BOOST_CHECK_EQUAL( 8*len, zc + oc ); 21 | double E = 1 + (zc + oc) / 2.0; 22 | double variance = (E - 1) * (E - 2) / (oc + zc - 1); 23 | double sigma = sqrt(variance); 24 | BOOST_CHECK( rc > E - sigma && rc < E + sigma); 25 | } 26 | 27 | BOOST_AUTO_TEST_SUITE(fc_crypto) 28 | 29 | BOOST_AUTO_TEST_CASE(rand_test) 30 | { 31 | char buffer[128]; 32 | fc::rand_bytes( buffer, sizeof(buffer) ); 33 | check_randomness( buffer, sizeof(buffer) ); 34 | } 35 | 36 | BOOST_AUTO_TEST_CASE(pseudo_rand_test) 37 | { 38 | char buffer[10013]; 39 | fc::rand_pseudo_bytes( buffer, sizeof(buffer) ); 40 | check_randomness( buffer, sizeof(buffer) ); 41 | } 42 | 43 | BOOST_AUTO_TEST_SUITE_END() 44 | -------------------------------------------------------------------------------- /tests/network/http/websocket_test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | #include 6 | 7 | BOOST_AUTO_TEST_SUITE(fc_network) 8 | 9 | BOOST_AUTO_TEST_CASE(websocket_test) 10 | { 11 | fc::http::websocket_client client; 12 | fc::http::websocket_connection_ptr s_conn, c_conn; 13 | { 14 | fc::http::websocket_server server; 15 | server.on_connection([&]( const fc::http::websocket_connection_ptr& c ){ 16 | s_conn = c; 17 | c->on_message_handler([&](const std::string& s){ 18 | c->send_message("echo: " + s); 19 | }); 20 | }); 21 | 22 | server.listen( 8090 ); 23 | server.start_accept(); 24 | 25 | std::string echo; 26 | c_conn = client.connect( "ws://localhost:8090" ); 27 | c_conn->on_message_handler([&](const std::string& s){ 28 | echo = s; 29 | }); 30 | c_conn->send_message( "hello world" ); 31 | fc::usleep( fc::seconds(1) ); 32 | BOOST_CHECK_EQUAL("echo: hello world", echo); 33 | c_conn->send_message( "again" ); 34 | fc::usleep( fc::seconds(1) ); 35 | BOOST_CHECK_EQUAL("echo: again", echo); 36 | 37 | s_conn->close(0, "test"); 38 | fc::usleep( fc::seconds(1) ); 39 | try { 40 | c_conn->send_message( "again" ); 41 | BOOST_FAIL("expected assertion failure"); 42 | } catch (const fc::assert_exception& e) { 43 | //std::cerr << e.to_string() << "\n"; 44 | } 45 | 46 | c_conn = client.connect( "ws://localhost:8090" ); 47 | c_conn->on_message_handler([&](const std::string& s){ 48 | echo = s; 49 | }); 50 | c_conn->send_message( "hello world" ); 51 | fc::usleep( fc::seconds(1) ); 52 | BOOST_CHECK_EQUAL("echo: hello world", echo); 53 | } 54 | 55 | try { 56 | c_conn->send_message( "again" ); 57 | BOOST_FAIL("expected assertion failure"); 58 | } catch (const fc::assert_exception& e) { 59 | std::cerr << e.to_string() << "\n"; 60 | } 61 | 62 | try { 63 | c_conn = client.connect( "ws://localhost:8090" ); 64 | BOOST_FAIL("expected assertion failure"); 65 | } catch (const fc::assert_exception& e) { 66 | std::cerr << e.to_string() << "\n"; 67 | } 68 | } 69 | 70 | BOOST_AUTO_TEST_SUITE_END() 71 | -------------------------------------------------------------------------------- /tests/network/ntp_test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | BOOST_AUTO_TEST_SUITE(fc_network) 8 | 9 | BOOST_AUTO_TEST_CASE( ntp_test ) 10 | { 11 | fc::ntp ntp_service; 12 | ntp_service.set_request_interval(5); 13 | fc::usleep(fc::seconds(4) ); 14 | auto time = ntp_service.get_time(); 15 | BOOST_CHECK( time ); 16 | auto ntp_time = *time; 17 | auto delta = ntp_time - fc::time_point::now(); 18 | // auto minutes = delta.count() / 1000000 / 60; 19 | // auto hours = delta.count() / 1000000 / 60 / 60; 20 | // auto seconds = delta.count() / 1000000; 21 | auto msec= delta.count() / 1000; 22 | BOOST_CHECK( msec < 100 ); 23 | } 24 | 25 | BOOST_AUTO_TEST_SUITE_END() 26 | -------------------------------------------------------------------------------- /tests/rate_limiting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | fc::rate_limiting_group rate_limiter(1000000, 1000000); 9 | 10 | void download_url(const std::string& ip_address, const std::string& url) 11 | { 12 | fc::http::connection http_connection; 13 | rate_limiter.add_tcp_socket(&http_connection.get_socket()); 14 | http_connection.connect_to(fc::ip::endpoint(fc::ip::address(ip_address.c_str()), 80)); 15 | std::cout << "Starting download...\n"; 16 | fc::time_point start_time(fc::time_point::now()); 17 | fc::http::reply reply = http_connection.request("GET", "http://mirror.cs.vt.edu/pub/cygwin/glibc/releases/glibc-2.9.tar.gz"); 18 | fc::time_point end_time(fc::time_point::now()); 19 | 20 | std::cout << "HTTP return code: " << reply.status << "\n"; 21 | std::cout << "Retreived " << reply.body.size() << " bytes in " << ((end_time - start_time).count() / fc::milliseconds(1).count()) << "ms\n"; 22 | std::cout << "Average speed " << ((1000 * (uint64_t)reply.body.size()) / ((end_time - start_time).count() / fc::milliseconds(1).count())) << " bytes per second"; 23 | } 24 | 25 | int main( int argc, char** argv ) 26 | { 27 | rate_limiter.set_actual_rate_time_constant(fc::seconds(1)); 28 | 29 | std::vector > download_futures; 30 | download_futures.push_back(fc::async([](){ download_url("198.82.184.145", "http://mirror.cs.vt.edu/pub/cygwin/glibc/releases/glibc-2.9.tar.gz"); })); 31 | download_futures.push_back(fc::async([](){ download_url("198.82.184.145", "http://mirror.cs.vt.edu/pub/cygwin/glibc/releases/glibc-2.7.tar.gz"); })); 32 | 33 | while (1) 34 | { 35 | bool all_done = true; 36 | for (unsigned i = 0; i < download_futures.size(); ++i) 37 | if (!download_futures[i].ready()) 38 | all_done = false; 39 | if (all_done) 40 | break; 41 | std::cout << "Current measurement of actual transfer rate: upload " << rate_limiter.get_actual_upload_rate() << ", download " << rate_limiter.get_actual_download_rate() << "\n"; 42 | fc::usleep(fc::seconds(1)); 43 | } 44 | 45 | for (unsigned i = 0; i < download_futures.size(); ++i) 46 | download_futures[i].wait(); 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /tests/real128_test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | BOOST_AUTO_TEST_SUITE(fc) 6 | 7 | using fc::real128; 8 | using std::string; 9 | 10 | BOOST_AUTO_TEST_CASE(real128_test) 11 | { 12 | BOOST_CHECK_EQUAL(string(real128()), string("0.")); 13 | BOOST_CHECK_EQUAL(string(real128(0)), string("0.")); 14 | BOOST_CHECK_EQUAL(real128(8).to_uint64(), 8); 15 | BOOST_CHECK_EQUAL(real128(6789).to_uint64(), 6789); 16 | BOOST_CHECK_EQUAL(real128(10000).to_uint64(), 10000); 17 | BOOST_CHECK_EQUAL(string(real128(1)), string("1.")); 18 | BOOST_CHECK_EQUAL(string(real128(5)), string("5.")); 19 | BOOST_CHECK_EQUAL(string(real128(12345)), string("12345.")); 20 | BOOST_CHECK_EQUAL(string(real128(0)), string(real128("0"))); 21 | 22 | real128 ten(10); 23 | real128 two(2); 24 | real128 twenty(20); 25 | real128 pi(31415926535); 26 | pi /= 10000000000; 27 | 28 | BOOST_CHECK_EQUAL( string(ten), "10." ); 29 | BOOST_CHECK_EQUAL( string(two), "2." ); 30 | BOOST_CHECK_EQUAL( string(ten+two), "12." ); 31 | BOOST_CHECK_EQUAL( string(ten-two), "8." ); 32 | BOOST_CHECK_EQUAL( string(ten*two), "20." ); 33 | BOOST_CHECK_EQUAL( string(ten/two), "5." ); 34 | BOOST_CHECK_EQUAL( string(ten/two/two/two*two*two*two), "10." ); 35 | BOOST_CHECK_EQUAL( string(ten/two/two/two*two*two*two), string(ten) ); 36 | BOOST_CHECK_EQUAL( string(twenty/ten), string(two) ); 37 | BOOST_CHECK_EQUAL( string(pi), "3.1415926535" ); 38 | BOOST_CHECK_EQUAL( string(pi*10), "31.415926535" ); 39 | BOOST_CHECK_EQUAL( string(pi*20), "62.83185307" ); 40 | BOOST_CHECK_EQUAL( string(real128("62.83185307")/twenty), string(pi) ); 41 | BOOST_CHECK_EQUAL( string(pi*1), "3.1415926535" ); 42 | BOOST_CHECK_EQUAL( string(pi*0), "0." ); 43 | 44 | BOOST_CHECK_EQUAL(real128("12345.6789").to_uint64(), 12345); 45 | BOOST_CHECK_EQUAL((real128("12345.6789")*10000).to_uint64(), 123456789); 46 | BOOST_CHECK_EQUAL(string(real128("12345.6789")), string("12345.6789")); 47 | 48 | BOOST_CHECK_EQUAL( real128(uint64_t(-1)).to_uint64(), uint64_t(-1) ); 49 | 50 | wdump( (ten)(two)(twenty) ); 51 | wdump((real128("12345.6789")) ); 52 | wdump( (ten/3*3) ); 53 | } 54 | 55 | BOOST_AUTO_TEST_SUITE_END() 56 | -------------------------------------------------------------------------------- /tests/rpc.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | int main( int argc, char** argv ) 7 | { 8 | } 9 | -------------------------------------------------------------------------------- /tests/sleep.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main( int argc, char** argv ) 5 | { 6 | fc::thread test("test"); 7 | auto result = test.async( [=]() { 8 | while( true ) 9 | { 10 | fc::usleep( fc::microseconds(1000) ); 11 | } 12 | }); 13 | char c; 14 | std::cin >> c; 15 | test.quit(); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /tests/udt_client.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | using namespace UDT; 7 | 8 | int main() 9 | { 10 | UDTSOCKET client = UDT::socket(AF_INET, SOCK_STREAM, 0); 11 | 12 | sockaddr_in serv_addr; 13 | serv_addr.sin_family = AF_INET; 14 | serv_addr.sin_port = htons(9000); 15 | inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr); 16 | 17 | memset(&(serv_addr.sin_zero), '\0', 8); 18 | 19 | // connect to the server, implict bind 20 | if (UDT::ERROR == UDT::connect(client, (sockaddr*)&serv_addr, sizeof(serv_addr))) 21 | { 22 | cout << "connect: " << UDT::getlasterror().getErrorMessage(); 23 | return 0; 24 | } 25 | 26 | char* hello = "hello world! 3\n"; 27 | if (UDT::ERROR == UDT::send(client, hello, strlen(hello) + 1, 0)) 28 | { 29 | cout << "send: " << UDT::getlasterror().getErrorMessage(); 30 | return 0; 31 | } 32 | 33 | UDT::close(client); 34 | 35 | return 1; 36 | } 37 | -------------------------------------------------------------------------------- /tests/udt_server.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main( int argc, char** argv ) 8 | { 9 | UDTSOCKET serv = UDT::socket(AF_INET, SOCK_STREAM, 0); 10 | bool block = false; 11 | 12 | sockaddr_in my_addr; 13 | my_addr.sin_family = AF_INET; 14 | my_addr.sin_port = htons(9000); 15 | my_addr.sin_addr.s_addr = INADDR_ANY; 16 | memset(&(my_addr.sin_zero), '\0', 8); 17 | 18 | if (UDT::ERROR == UDT::bind(serv, (sockaddr*)&my_addr, sizeof(my_addr))) 19 | { 20 | cout << "bind: " << UDT::getlasterror().getErrorMessage(); 21 | return 0; 22 | } 23 | UDT::listen(serv, 10); 24 | 25 | int namelen; 26 | sockaddr_in their_addr; 27 | 28 | 29 | UDT::setsockopt(serv, 0, UDT_SNDSYN, &block, sizeof(bool)); 30 | UDT::setsockopt(serv, 0, UDT_RCVSYN, &block, sizeof(bool)); 31 | UDTSOCKET recver = UDT::accept(serv, (sockaddr*)&their_addr, &namelen); 32 | if( recver == UDT::INVALID_SOCK ) 33 | { 34 | if( UDT::getlasterror_code() == CUDTException::EASYNCRCV ) 35 | { 36 | std::cout << "nothing yet... better luck next time\n"; 37 | } 38 | } 39 | auto pollid = UDT::epoll_create(); 40 | UDT::epoll_add_usock(pollid, serv, nullptr );// const int* events = NULL); 41 | std::set readready; 42 | std::set writeready; 43 | std::cout << "waiting for 5 seconds\n"; 44 | UDT::epoll_wait( pollid, &readready, &writeready, 10000 ); 45 | 46 | 47 | recver = UDT::accept(serv, (sockaddr*)&their_addr, &namelen); 48 | if( recver == UDT::INVALID_SOCK ) 49 | { 50 | if( UDT::getlasterror_code() == CUDTException::EASYNCRCV ) 51 | { 52 | std::cout << "nothing yet... better luck next time\n"; 53 | } 54 | return 0; 55 | } 56 | UDT::setsockopt(recver, 0, UDT_SNDSYN, &block, sizeof(bool)); 57 | UDT::setsockopt(recver, 0, UDT_RCVSYN, &block, sizeof(bool)); 58 | UDT::epoll_remove_usock(pollid, serv );// const int* events = NULL); 59 | int events = UDT_EPOLL_IN; 60 | 61 | UDT::epoll_add_usock(pollid, recver, &events );// const int* events = NULL); 62 | 63 | readready.clear(); 64 | UDT::epoll_wait( pollid, &readready, &writeready, 5000 ); 65 | 66 | char ip[16]; 67 | cout << "new connection: " << inet_ntoa(their_addr.sin_addr) << ":" << ntohs(their_addr.sin_port) << endl; 68 | 69 | char data[100]; 70 | 71 | while (UDT::ERROR == UDT::recv(recver, data, 100, 0)) 72 | { 73 | cout << "recv:" << UDT::getlasterror().getErrorMessage() << endl; 74 | UDT::epoll_wait( pollid, &readready, &writeready, 5000 ); 75 | } 76 | 77 | cout << data << endl; 78 | 79 | UDT::close(recver); 80 | UDT::close(serv); 81 | 82 | return 1; 83 | } 84 | -------------------------------------------------------------------------------- /tests/udtc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace fc; 9 | 10 | int main( int argc, char** argv ) 11 | { 12 | try { 13 | udt_socket sock; 14 | sock.bind( fc::ip::endpoint::from_string( "127.0.0.1:6666" ) ); 15 | ilog( "." ); 16 | sock.connect_to( fc::ip::endpoint::from_string( "127.0.0.1:7777" ) ); 17 | ilog( "after connect to..." ); 18 | 19 | std::cout << "local endpoint: " < response; 32 | response.resize(1024); 33 | int r = sock.readsome( response.data(), response.size() ); 34 | while( r ) 35 | { 36 | std::cout.write( response.data(), r ); 37 | r = sock.readsome( response.data(), response.size() ); 38 | } 39 | */ 40 | // if we exit too quickly, UDT will not have a chance to 41 | // send the graceful close message. 42 | //fc::usleep( fc::seconds(1) ); 43 | } catch ( const fc::exception& e ) 44 | { 45 | elog( "${e}", ("e",e.to_detail_string() ) ); 46 | } 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /tests/udts.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace fc; 8 | 9 | int main( int argc, char** argv ) 10 | { 11 | try { 12 | udt_server serv; 13 | serv.listen( fc::ip::endpoint::from_string( "127.0.0.1:7777" ) ); 14 | 15 | while( true ) 16 | { 17 | udt_socket sock; 18 | serv.accept( sock ); 19 | 20 | std::vector response; 21 | response.resize(1024); 22 | int r = sock.readsome( response.data(), response.size() ); 23 | while( r ) 24 | { 25 | std::cout.write( response.data(), r ); 26 | r = sock.readsome( response.data(), response.size() ); 27 | //sock.write( response.data(), response.size() ); 28 | } 29 | 30 | std::string goodbye = "goodbye cruel world"; 31 | sock.write( goodbye.c_str(), goodbye.size() ); 32 | } 33 | } catch ( const fc::exception& e ) 34 | { 35 | elog( "${e}", ("e",e.to_detail_string() ) ); 36 | } 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /vendor/boost_1.51/include/boost/process.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Boost.Process 3 | // ~~~~~~~~~~~~~ 4 | // 5 | // Copyright (c) 2006, 2007 Julio M. Merino Vidal 6 | // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling 7 | // Copyright (c) 2009 Boris Schaeling 8 | // Copyright (c) 2010 Felipe Tanus, Boris Schaeling 9 | // 10 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 11 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 12 | // 13 | 14 | /** 15 | * \file boost/process.hpp 16 | * 17 | * Convenience header that includes all public Boost.Process header files. 18 | */ 19 | 20 | #ifndef BOOST_PROCESS_HPP 21 | #define BOOST_PROCESS_HPP 22 | 23 | #include 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /vendor/boost_1.51/include/boost/process/all.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Boost.Process 3 | // ~~~~~~~~~~~~~ 4 | // 5 | // Copyright (c) 2006, 2007 Julio M. Merino Vidal 6 | // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling 7 | // Copyright (c) 2009 Boris Schaeling 8 | // Copyright (c) 2010 Felipe Tanus, Boris Schaeling 9 | // 10 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 11 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 12 | // 13 | 14 | /** 15 | * \file boost/process/all.hpp 16 | * 17 | * Convenience header that includes all public Boost.Process header files. 18 | */ 19 | 20 | #ifndef BOOST_PROCESS_ALL_HPP 21 | #define BOOST_PROCESS_ALL_HPP 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /vendor/boost_1.51/include/boost/process/child.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Boost.Process 3 | // ~~~~~~~~~~~~~ 4 | // 5 | // Copyright (c) 2006, 2007 Julio M. Merino Vidal 6 | // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling 7 | // Copyright (c) 2009 Boris Schaeling 8 | // Copyright (c) 2010 Felipe Tanus, Boris Schaeling 9 | // 10 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 11 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 12 | // 13 | 14 | /** 15 | * \file boost/process/child.hpp 16 | * 17 | * Includes the declaration of the child class. 18 | */ 19 | 20 | #ifndef BOOST_PROCESS_CHILD_HPP 21 | #define BOOST_PROCESS_CHILD_HPP 22 | 23 | #include 24 | 25 | #if defined(BOOST_POSIX_API) 26 | #elif defined(BOOST_WINDOWS_API) 27 | # include 28 | #else 29 | # error "Unsupported platform." 30 | #endif 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | namespace boost { 39 | namespace process { 40 | 41 | /** 42 | * The child class provides access to a child process. 43 | */ 44 | class child : public process 45 | { 46 | public: 47 | /** 48 | * Creates a new child object that represents the just spawned child 49 | * process \a id. 50 | */ 51 | child(pid_type id, std::map handles) 52 | : process(id), 53 | handles_(handles) 54 | { 55 | } 56 | 57 | #if defined(BOOST_WINDOWS_API) 58 | /** 59 | * Creates a new child object that represents the just spawned child 60 | * process \a id. 61 | * 62 | * This operation is only available on Windows systems. 63 | */ 64 | child(handle hprocess, std::map handles) 65 | : process(hprocess), 66 | handles_(handles) 67 | { 68 | } 69 | #endif 70 | 71 | /** 72 | * Gets a handle to a stream attached to the child. 73 | * 74 | * If the handle doesn't exist an invalid handle is returned. 75 | */ 76 | handle get_handle(stream_id id) const 77 | { 78 | std::map::const_iterator it = handles_.find(id); 79 | return (it != handles_.end()) ? it->second : handle(); 80 | } 81 | 82 | #if defined(BOOST_WINDOWS_API) 83 | handle::native_type get_os_handle() const 84 | { 85 | return handle_.native(); 86 | } 87 | #endif 88 | 89 | 90 | private: 91 | /** 92 | * Handles providing access to streams attached to the child process. 93 | */ 94 | std::map handles_; 95 | }; 96 | 97 | } 98 | } 99 | 100 | #endif 101 | -------------------------------------------------------------------------------- /vendor/boost_1.51/include/boost/process/config.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Boost.Process 3 | // ~~~~~~~~~~~~~ 4 | // 5 | // Copyright (c) 2006, 2007 Julio M. Merino Vidal 6 | // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling 7 | // Copyright (c) 2009 Boris Schaeling 8 | // Copyright (c) 2010 Felipe Tanus, Boris Schaeling 9 | // 10 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 11 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 12 | // 13 | 14 | /** 15 | * \file boost/process/config.hpp 16 | * 17 | * Defines macros that are used by the library to determine the operating 18 | * system it is running under and the features it supports. 19 | */ 20 | 21 | #ifndef BOOST_PROCESS_CONFIG_HPP 22 | #define BOOST_PROCESS_CONFIG_HPP 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #if defined(BOOST_POSIX_API) 30 | # include 31 | #elif defined(BOOST_WINDOWS_API) 32 | # include 33 | #endif 34 | 35 | #if defined(BOOST_POSIX_API) || defined(BOOST_PROCESS_DOXYGEN) 36 | # if !defined(BOOST_PROCESS_POSIX_PATH_MAX) || defined(BOOST_PROCESS_DOXYGEN) 37 | /** 38 | * Specifies the system's maximal supported path length. 39 | * 40 | * The macro BOOST_PROCESS_POSIX_PATH_MAX is set to a positive integer 41 | * value which specifies the system's maximal supported path length. It is 42 | * only used if neither PATH_MAX nor _PC_PATH_MAX and HAVE_PATHCONF are defined. 43 | * The maximal supported path length is required by 44 | * boost::process::self::get_work_dir(). Please note that this function is 45 | * also called by the constructor of boost::process::context. 46 | */ 47 | # define BOOST_PROCESS_POSIX_PATH_MAX 259 48 | # endif 49 | #endif 50 | 51 | /** \cond */ 52 | #define BOOST_PROCESS_SOURCE_LOCATION \ 53 | "in file '" __FILE__ "', line " BOOST_STRINGIZE(__LINE__) ": " 54 | 55 | #if defined(BOOST_POSIX_API) 56 | # define BOOST_PROCESS_LAST_ERROR errno 57 | #elif defined(BOOST_WINDOWS_API) 58 | # define BOOST_PROCESS_LAST_ERROR GetLastError() 59 | #endif 60 | 61 | #define BOOST_PROCESS_THROW_LAST_SYSTEM_ERROR(what) \ 62 | boost::throw_exception(boost::system::system_error( \ 63 | boost::system::error_code(BOOST_PROCESS_LAST_ERROR, \ 64 | boost::system::get_system_category()), \ 65 | BOOST_PROCESS_SOURCE_LOCATION what)) 66 | 67 | #define BOOST_PROCESS_THROW_ERROR(error, what) \ 68 | boost::throw_exception(boost::system::system_error( \ 69 | boost::system::error_code(error, \ 70 | boost::system::get_system_category()), \ 71 | BOOST_PROCESS_SOURCE_LOCATION what)) 72 | /** \endcond */ 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /vendor/boost_1.51/include/boost/process/detail/basic_status.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Boost.Process 3 | // ~~~~~~~~~~~~~ 4 | // 5 | // Copyright (c) 2006, 2007 Julio M. Merino Vidal 6 | // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling 7 | // Copyright (c) 2009 Boris Schaeling 8 | // Copyright (c) 2010 Felipe Tanus, Boris Schaeling 9 | // 10 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 11 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 12 | // 13 | 14 | /** 15 | * \file boost/process/detail/basic_status.hpp 16 | * 17 | * Includes the declaration of the basic status class. 18 | */ 19 | 20 | #ifndef BOOST_PROCESS_DETAIL_BASIC_STATUS_HPP 21 | #define BOOST_PROCESS_DETAIL_BASIC_STATUS_HPP 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | namespace boost { 28 | namespace process { 29 | namespace detail { 30 | 31 | /** 32 | * The basic_status class to wait for processes to exit. 33 | * 34 | * The basic_status class is a Boost.Asio I/O object and supports synchronous 35 | * and asynchronous wait operations. It must be instantiated with a Service. 36 | */ 37 | template 38 | class basic_status 39 | : public boost::asio::basic_io_object 40 | { 41 | public: 42 | explicit basic_status(boost::asio::io_service &io_service) 43 | : boost::asio::basic_io_object(io_service) 44 | { 45 | } 46 | 47 | /** 48 | * Waits synchronously for a process to exit. 49 | */ 50 | int wait(pid_type pid) 51 | { 52 | return this->service.wait(this->implementation, pid); 53 | } 54 | 55 | /** 56 | * Waits asynchronously for a process to exit. 57 | */ 58 | template 59 | void async_wait(pid_type pid, Handler handler) 60 | { 61 | this->service.async_wait(this->implementation, pid, handler); 62 | } 63 | }; 64 | 65 | } 66 | } 67 | } 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /vendor/boost_1.51/include/boost/process/environment.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Boost.Process 3 | // ~~~~~~~~~~~~~ 4 | // 5 | // Copyright (c) 2006, 2007 Julio M. Merino Vidal 6 | // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling 7 | // Copyright (c) 2009 Boris Schaeling 8 | // Copyright (c) 2010 Felipe Tanus, Boris Schaeling 9 | // 10 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 11 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 12 | // 13 | 14 | /** 15 | * \file boost/process/environment.hpp 16 | * 17 | * Includes the declaration of the environment class. 18 | */ 19 | 20 | #ifndef BOOST_PROCESS_ENVIRONMENT_HPP 21 | #define BOOST_PROCESS_ENVIRONMENT_HPP 22 | 23 | #include 24 | #include 25 | 26 | namespace boost { 27 | namespace process { 28 | 29 | /** 30 | * Representation of a process' environment variables. 31 | * 32 | * The environment is a map that establishes an unidirectional 33 | * association between variable names and their values and is 34 | * represented by a string to string map. 35 | * 36 | * Variables may be defined to the empty string. Be aware that doing so 37 | * is not portable: POSIX systems will treat such variables as being 38 | * defined to the empty value, but Windows systems are not able to 39 | * distinguish them from undefined variables. 40 | * 41 | * Neither POSIX nor Windows systems support a variable with no name. 42 | * 43 | * It is worthy to note that the environment is sorted alphabetically. 44 | * This is provided for-free by the map container used to implement this 45 | * type, and this behavior is required by Windows systems. 46 | */ 47 | typedef std::map environment; 48 | 49 | } 50 | } 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /vendor/boost_1.51/include/boost/process/pid_type.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Boost.Process 3 | // ~~~~~~~~~~~~~ 4 | // 5 | // Copyright (c) 2006, 2007 Julio M. Merino Vidal 6 | // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling 7 | // Copyright (c) 2009 Boris Schaeling 8 | // Copyright (c) 2010 Felipe Tanus, Boris Schaeling 9 | // 10 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 11 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 12 | // 13 | 14 | /** 15 | * \file boost/process/pid_type.hpp 16 | * 17 | * Includes the declaration of the pid type. 18 | */ 19 | 20 | #ifndef BOOST_PROCESS_PID_TYPE_HPP 21 | #define BOOST_PROCESS_PID_TYPE_HPP 22 | 23 | #include 24 | 25 | #if defined(BOOST_POSIX_API) 26 | # include 27 | #elif defined(BOOST_WINDOWS_API) 28 | # include 29 | #endif 30 | 31 | namespace boost { 32 | namespace process { 33 | 34 | #if defined(BOOST_PROCESS_DOXYGEN) 35 | /** 36 | * Opaque name for the process identifier type. 37 | * 38 | * Each operating system identifies processes using a specific type. 39 | * The \a pid_type type is used to transparently refer to a process 40 | * regardless of the operating system. 41 | * 42 | * This type is guaranteed to be an integral type on all supported 43 | * platforms. On POSIX systems it is defined as pid_t, on Windows systems as 44 | * DWORD. 45 | */ 46 | typedef NativeProcessId pid_type; 47 | #elif defined(BOOST_POSIX_API) 48 | typedef pid_t pid_type; 49 | #elif defined(BOOST_WINDOWS_API) 50 | typedef DWORD pid_type; 51 | #endif 52 | 53 | } 54 | } 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /vendor/boost_1.51/include/boost/process/pipe.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Boost.Process 3 | // ~~~~~~~~~~~~~ 4 | // 5 | // Copyright (c) 2006, 2007 Julio M. Merino Vidal 6 | // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling 7 | // Copyright (c) 2009 Boris Schaeling 8 | // Copyright (c) 2010 Felipe Tanus, Boris Schaeling 9 | // 10 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 11 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 12 | // 13 | 14 | /** 15 | * \file boost/process/pipe.hpp 16 | * 17 | * Includes the declaration of the pipe class. 18 | */ 19 | 20 | #ifndef BOOST_PROCESS_PIPE_HPP 21 | #define BOOST_PROCESS_PIPE_HPP 22 | 23 | #include 24 | #include 25 | 26 | namespace boost { 27 | namespace process { 28 | 29 | #if defined(BOOST_PROCESS_DOXYGEN) 30 | /** 31 | * The pipe class is a type definition for stream-based classes defined by 32 | * Boost.Asio. 33 | * 34 | * The type definition is provided for convenience. You can also use Boost.Asio 35 | * classes directly for asynchronous I/O operations. 36 | */ 37 | typedef BoostAsioPipe pipe; 38 | #elif defined(BOOST_POSIX_API) 39 | typedef boost::asio::posix::stream_descriptor pipe; 40 | #elif defined(BOOST_WINDOWS_API) 41 | typedef boost::asio::windows::stream_handle pipe; 42 | #else 43 | # error "Unsupported platform." 44 | #endif 45 | 46 | } 47 | } 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /vendor/boost_1.51/include/boost/process/status.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Boost.Process 3 | // ~~~~~~~~~~~~~ 4 | // 5 | // Copyright (c) 2006, 2007 Julio M. Merino Vidal 6 | // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling 7 | // Copyright (c) 2009 Boris Schaeling 8 | // Copyright (c) 2010 Felipe Tanus, Boris Schaeling 9 | // 10 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 11 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 12 | // 13 | 14 | /** 15 | * \file boost/process/status.hpp 16 | * 17 | * Includes the declaration of the status class. 18 | */ 19 | 20 | #ifndef BOOST_PROCESS_STATUS_HPP 21 | #define BOOST_PROCESS_STATUS_HPP 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | namespace boost { 28 | namespace process { 29 | 30 | /** 31 | * The status class to wait for processes to exit. 32 | * 33 | * The status class is a Boost.Asio I/O object and supports synchronous 34 | * and asynchronous wait operations. 35 | */ 36 | typedef detail::basic_status > status; 37 | 38 | } 39 | } 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /vendor/boost_1.51/include/boost/process/stream_ends.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Boost.Process 3 | // ~~~~~~~~~~~~~ 4 | // 5 | // Copyright (c) 2006, 2007 Julio M. Merino Vidal 6 | // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling 7 | // Copyright (c) 2009 Boris Schaeling 8 | // Copyright (c) 2010 Felipe Tanus, Boris Schaeling 9 | // 10 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 11 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 12 | // 13 | 14 | /** 15 | * \file boost/process/stream_ends.hpp 16 | * 17 | * Includes the declaration of the stream_ends class. 18 | */ 19 | 20 | #ifndef BOOST_PROCESS_STREAM_ENDS_HPP 21 | #define BOOST_PROCESS_STREAM_ENDS_HPP 22 | 23 | #include 24 | #include 25 | 26 | namespace boost { 27 | namespace process { 28 | 29 | /** 30 | * A pair of handles to configure streams. 31 | * 32 | * Stream behaviors return a pair of handles to specify how a child's stream 33 | * should be configured and possibly the opposite end of a child's end. This 34 | * is the end remaining in the parent process and which can be used for example 35 | * to communicate with a child process through its standard streams. 36 | */ 37 | struct stream_ends { 38 | /** 39 | * The child's end. 40 | */ 41 | handle child; 42 | 43 | /** 44 | * The parent's end. 45 | */ 46 | handle parent; 47 | 48 | /** 49 | * Standard constructor creating two invalid handles. 50 | */ 51 | stream_ends() 52 | { 53 | } 54 | 55 | /** 56 | * Helper constructor to easily initialize handles. 57 | */ 58 | stream_ends(handle c, handle p) 59 | : child(c), 60 | parent(p) 61 | { 62 | } 63 | }; 64 | 65 | } 66 | } 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /vendor/boost_1.51/include/boost/process/stream_id.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Boost.Process 3 | // ~~~~~~~~~~~~~ 4 | // 5 | // Copyright (c) 2006, 2007 Julio M. Merino Vidal 6 | // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling 7 | // Copyright (c) 2009 Boris Schaeling 8 | // Copyright (c) 2010 Felipe Tanus, Boris Schaeling 9 | // 10 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 11 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 12 | // 13 | 14 | /** 15 | * \file boost/process/stream_id.hpp 16 | * 17 | * Includes the declaration of the stream_id type. 18 | */ 19 | 20 | #ifndef BOOST_PROCESS_STREAM_ID_HPP 21 | #define BOOST_PROCESS_STREAM_ID_HPP 22 | 23 | #include 24 | 25 | namespace boost { 26 | namespace process { 27 | 28 | /** 29 | * Standard stream id to refer to standard streams in a cross-platform manner. 30 | */ 31 | enum std_stream_id { stdin_id, stdout_id, stderr_id }; 32 | 33 | #if defined(BOOST_PROCESS_DOXYGEN) 34 | /** 35 | * Stream id type. 36 | * 37 | * Depending on the platform the stream id type is defined to refer to standard 38 | * streams only or to support more streams. 39 | */ 40 | typedef NativeStreamId stream_id; 41 | #elif defined(BOOST_POSIX_API) 42 | typedef int stream_id; 43 | #elif defined(BOOST_WINDOWS_API) 44 | typedef std_stream_id stream_id; 45 | #endif 46 | 47 | } 48 | } 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /vendor/boost_1.51/include/boost/process/stream_type.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // Boost.Process 3 | // ~~~~~~~~~~~~~ 4 | // 5 | // Copyright (c) 2006, 2007 Julio M. Merino Vidal 6 | // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling 7 | // Copyright (c) 2009 Boris Schaeling 8 | // Copyright (c) 2010 Felipe Tanus, Boris Schaeling 9 | // 10 | // Distributed under the Boost Software License, Version 1.0. (See accompanying 11 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 12 | // 13 | 14 | /** 15 | * \file boost/process/stream_type.hpp 16 | * 17 | * Includes the declaration of the stream_type enumeration. 18 | */ 19 | 20 | #ifndef BOOST_PROCESS_STREAM_TYPE_HPP 21 | #define BOOST_PROCESS_STREAM_TYPE_HPP 22 | 23 | #include 24 | 25 | namespace boost { 26 | namespace process { 27 | 28 | /** 29 | * Stream type to differentiate between input and output streams. 30 | * 31 | * On POSIX systems another value unknown_stream is defined. It is passed 32 | * to stream behaviors for file descriptors greater than 2. 33 | */ 34 | enum stream_type { 35 | input_stream, 36 | output_stream 37 | #if defined(BOOST_POSIX_API) 38 | , unknown_stream 39 | #endif 40 | }; 41 | 42 | } 43 | } 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /vendor/boost_1.51/libs/context/fcontext.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Copyright Oliver Kowalke 2009. 3 | // Distributed under the Boost Software License, Version 1.0. 4 | // (See accompanying file LICENSE_1_0.txt or copy at 5 | // http://www.boost.org/LICENSE_1_0.txt) 6 | 7 | #define BOOST_CONTEXT_SOURCE 8 | 9 | #include 10 | 11 | #include 12 | 13 | #ifdef BOOST_HAS_ABI_HEADERS 14 | # include BOOST_ABI_PREFIX 15 | #endif 16 | 17 | namespace boost { 18 | namespace ctx { 19 | namespace detail { 20 | 21 | extern "C" BOOST_CONTEXT_DECL 22 | void * BOOST_CONTEXT_CALLDECL align_stack( void * vp) 23 | { 24 | void * base = vp; 25 | if ( 0 != ( ( ( uintptr_t) base) & 15) ) 26 | base = ( char * ) ( ( ( ( uintptr_t) base) - 15) & ~0x0F); 27 | return base; 28 | } 29 | 30 | } 31 | 32 | }} 33 | 34 | #ifdef BOOST_HAS_ABI_HEADERS 35 | # include BOOST_ABI_SUFFIX 36 | #endif 37 | -------------------------------------------------------------------------------- /vendor/boost_1.51/libs/context/stack_allocator_posix.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Copyright Oliver Kowalke 2009. 3 | // Distributed under the Boost Software License, Version 1.0. 4 | // (See accompanying file LICENSE_1_0.txt or copy at 5 | // http://www.boost.org/LICENSE_1_0.txt) 6 | 7 | #define BOOST_CONTEXT_SOURCE 8 | 9 | #include 10 | 11 | extern "C" { 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | } 18 | 19 | #include 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | #include 26 | 27 | #ifdef BOOST_HAS_ABI_HEADERS 28 | # include BOOST_ABI_PREFIX 29 | #endif 30 | 31 | namespace boost { 32 | namespace ctx { 33 | 34 | void * 35 | stack_allocator::allocate( std::size_t size) const 36 | { 37 | if ( minimum_stacksize() > size) 38 | throw std::invalid_argument( 39 | boost::str( boost::format("invalid stack size: must be at least %d bytes") 40 | % minimum_stacksize() ) ); 41 | 42 | if ( ! is_stack_unbound() && ( maximum_stacksize() < size) ) 43 | throw std::invalid_argument( 44 | boost::str( boost::format("invalid stack size: must not be larger than %d bytes") 45 | % maximum_stacksize() ) ); 46 | 47 | const std::size_t pages( page_count( size) + 1); // add +1 for guard page 48 | std::size_t size_ = pages * pagesize(); 49 | 50 | 51 | # if defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__) 52 | void * limit = ::mmap( 0, size_, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); 53 | # else 54 | const int fd( ::open("/dev/zero", O_RDONLY) ); 55 | BOOST_ASSERT( -1 != fd); 56 | void * limit = ::mmap( 0, size_, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); 57 | ::close( fd); 58 | # endif 59 | if ( ! limit) throw std::bad_alloc(); 60 | 61 | const int result( ::mprotect( limit, pagesize(), PROT_NONE) ); 62 | BOOST_ASSERT( 0 == result); 63 | (void)result; // unused in release build 64 | 65 | return static_cast< char * >( limit) + size_; 66 | } 67 | 68 | void 69 | stack_allocator::deallocate( void * vp, std::size_t size) const 70 | { 71 | if ( vp) 72 | { 73 | const std::size_t pages( page_count( size) + 1); // add +1 for guard page 74 | std::size_t size_ = pages * pagesize(); 75 | BOOST_ASSERT( 0 < size && 0 < size_); 76 | void * limit = static_cast< char * >( vp) - size_; 77 | ::munmap( limit, size_); 78 | } 79 | } 80 | 81 | }} 82 | 83 | #ifdef BOOST_HAS_ABI_HEADERS 84 | # include BOOST_ABI_SUFFIX 85 | #endif 86 | -------------------------------------------------------------------------------- /vendor/boost_1.51/libs/context/stack_allocator_windows.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Copyright Oliver Kowalke 2009. 3 | // Distributed under the Boost Software License, Version 1.0. 4 | // (See accompanying file LICENSE_1_0.txt or copy at 5 | // http://www.boost.org/LICENSE_1_0.txt) 6 | 7 | #define BOOST_CONTEXT_SOURCE 8 | 9 | #include 10 | 11 | extern "C" { 12 | #include 13 | } 14 | 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include 23 | 24 | #ifdef BOOST_HAS_ABI_HEADERS 25 | # include BOOST_ABI_PREFIX 26 | #endif 27 | 28 | # if defined(BOOST_MSVC) 29 | # pragma warning(push) 30 | # pragma warning(disable:4244 4267) 31 | # endif 32 | 33 | namespace boost { 34 | namespace ctx { 35 | 36 | void * 37 | stack_allocator::allocate( std::size_t size) const 38 | { 39 | if ( minimum_stacksize() > size) 40 | throw std::invalid_argument( 41 | boost::str( boost::format("invalid stack size: must be at least %d bytes") 42 | % minimum_stacksize() ) ); 43 | 44 | if ( ! is_stack_unbound() && ( maximum_stacksize() < size) ) 45 | throw std::invalid_argument( 46 | boost::str( boost::format("invalid stack size: must not be larger than %d bytes") 47 | % maximum_stacksize() ) ); 48 | 49 | const std::size_t pages( page_count( size) + 1); // add +1 for guard page 50 | std::size_t size_ = pages * pagesize(); 51 | 52 | #ifndef BOOST_CONTEXT_FIBER 53 | void * limit = ::VirtualAlloc( 0, size_, MEM_COMMIT, PAGE_READWRITE); 54 | if ( ! limit) throw std::bad_alloc(); 55 | 56 | DWORD old_options; 57 | const BOOL result = ::VirtualProtect( 58 | limit, pagesize(), PAGE_READWRITE | PAGE_GUARD /*PAGE_NOACCESS*/, & old_options); 59 | BOOST_ASSERT( FALSE != result); 60 | 61 | return static_cast< char * >( limit) + size_; 62 | #endif 63 | } 64 | 65 | void 66 | stack_allocator::deallocate( void * vp, std::size_t size) const 67 | { 68 | if ( vp) 69 | { 70 | const std::size_t pages( page_count( size) + 1); // add +1 for guard page 71 | std::size_t size_ = pages * pagesize(); 72 | BOOST_ASSERT( 0 < size && 0 < size_); 73 | void * limit = static_cast< char * >( vp) - size_; 74 | ::VirtualFree( limit, 0, MEM_RELEASE); 75 | } 76 | } 77 | 78 | }} 79 | 80 | # if defined(BOOST_MSVC) 81 | # pragma warning(pop) 82 | # endif 83 | 84 | #ifdef BOOST_HAS_ABI_HEADERS 85 | # include BOOST_ABI_SUFFIX 86 | #endif 87 | -------------------------------------------------------------------------------- /vendor/boost_1.51/libs/context/stack_utils_posix.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Copyright Oliver Kowalke 2009. 3 | // Distributed under the Boost Software License, Version 1.0. 4 | // (See accompanying file LICENSE_1_0.txt or copy at 5 | // http://www.boost.org/LICENSE_1_0.txt) 6 | 7 | #define BOOST_CONTEXT_SOURCE 8 | 9 | #include 10 | 11 | extern "C" { 12 | #include 13 | #include 14 | #include 15 | #include 16 | } 17 | 18 | #include 19 | 20 | #include 21 | 22 | namespace { 23 | 24 | static rlimit stacksize_limit_() 25 | { 26 | rlimit limit; 27 | const int result = ::getrlimit( RLIMIT_STACK, & limit); 28 | BOOST_ASSERT( 0 == result); 29 | (void)result; // unused when in release mode... 30 | return limit; 31 | } 32 | 33 | static rlimit stacksize_limit() 34 | { 35 | static rlimit limit = stacksize_limit_(); 36 | return limit; 37 | } 38 | 39 | } 40 | 41 | namespace boost { 42 | namespace ctx { 43 | 44 | BOOST_CONTEXT_DECL 45 | std::size_t default_stacksize() 46 | { 47 | static std::size_t size = 256 * 1024; 48 | return size; 49 | } 50 | 51 | BOOST_CONTEXT_DECL 52 | std::size_t minimum_stacksize() 53 | { return SIGSTKSZ; } 54 | 55 | BOOST_CONTEXT_DECL 56 | std::size_t maximum_stacksize() 57 | { 58 | BOOST_ASSERT( ! is_stack_unbound() ); 59 | return static_cast< std::size_t >( stacksize_limit().rlim_max); 60 | } 61 | 62 | BOOST_CONTEXT_DECL 63 | bool is_stack_unbound() 64 | { return RLIM_INFINITY == stacksize_limit().rlim_max; } 65 | 66 | BOOST_CONTEXT_DECL 67 | std::size_t pagesize() 68 | { 69 | static std::size_t pagesize( ::getpagesize() ); 70 | return pagesize; 71 | } 72 | 73 | BOOST_CONTEXT_DECL 74 | std::size_t page_count( std::size_t stacksize) 75 | { 76 | return static_cast< std::size_t >( 77 | std::ceil( 78 | static_cast< float >( stacksize) / pagesize() ) ); 79 | } 80 | 81 | }} 82 | -------------------------------------------------------------------------------- /vendor/boost_1.51/libs/context/stack_utils_windows.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Copyright Oliver Kowalke 2009. 3 | // Distributed under the Boost Software License, Version 1.0. 4 | // (See accompanying file LICENSE_1_0.txt or copy at 5 | // http://www.boost.org/LICENSE_1_0.txt) 6 | 7 | #define BOOST_CONTEXT_SOURCE 8 | 9 | #include 10 | 11 | extern "C" { 12 | #include 13 | } 14 | 15 | #include 16 | #include 17 | 18 | #include 19 | 20 | namespace { 21 | 22 | static SYSTEM_INFO system_info_() 23 | { 24 | SYSTEM_INFO si; 25 | ::GetSystemInfo( & si); 26 | return si; 27 | } 28 | 29 | static SYSTEM_INFO system_info() 30 | { 31 | static SYSTEM_INFO si = system_info_(); 32 | return si; 33 | } 34 | 35 | } 36 | 37 | namespace boost { 38 | namespace ctx { 39 | 40 | BOOST_CONTEXT_DECL 41 | std::size_t default_stacksize() 42 | { 43 | static std::size_t size = 256 * 1024; 44 | return size; 45 | } 46 | 47 | BOOST_CONTEXT_DECL 48 | std::size_t minimum_stacksize() 49 | { 50 | static std::size_t stacksize( 51 | static_cast< std::size_t >( system_info().dwAllocationGranularity) ); 52 | return stacksize; 53 | } 54 | 55 | BOOST_CONTEXT_DECL 56 | std::size_t maximum_stacksize() 57 | { 58 | BOOST_ASSERT( ! is_stack_unbound() ); 59 | static std::size_t stacksize = 8 * 1024 * 1024; 60 | return stacksize; 61 | } 62 | 63 | // Windows seams not to provide a limit for the stacksize 64 | BOOST_CONTEXT_DECL 65 | bool is_stack_unbound() 66 | { return true; } 67 | 68 | BOOST_CONTEXT_DECL 69 | std::size_t pagesize() 70 | { 71 | static std::size_t pagesize( 72 | static_cast< std::size_t >( system_info().dwPageSize) ); 73 | return pagesize; 74 | } 75 | 76 | BOOST_CONTEXT_DECL 77 | std::size_t page_count( std::size_t stacksize) 78 | { 79 | return static_cast< std::size_t >( 80 | std::ceil( 81 | static_cast< float >( stacksize) / pagesize() ) ); 82 | } 83 | 84 | }} 85 | -------------------------------------------------------------------------------- /vendor/cyoencode-1.0.2/LICENSE.TXT: -------------------------------------------------------------------------------- 1 | All the files in this library are covered under the terms of the Berkeley 2 | Software Distribution (BSD) License: 3 | 4 | Copyright (c) 2009-2012, Graham Bull. 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are met: 9 | 10 | * Redistributions of source code must retain the above copyright notice, this 11 | list of conditions and the following disclaimer. 12 | 13 | * Redistributions in binary form must reproduce the above copyright notice, 14 | this list of conditions and the following disclaimer in the documentation 15 | and/or other materials provided with the distribution. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 21 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 23 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 24 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | 28 | -------------------------------------------------------------------------------- /vendor/cyoencode-1.0.2/README.TXT: -------------------------------------------------------------------------------- 1 | =============================================================================== 2 | CyoEncode 3 | http://cyoencode.sourceforge.net/ 4 | 5 | Copyright (c) 2009-2012, Graham Bull. All rights reserved. 6 | =============================================================================== 7 | 8 | Version 1.0.2 9 | Release Date 5th January 2012 10 | 11 | ------------------------------------------------------------------------------- 12 | 1. License 13 | ------------------------------------------------------------------------------- 14 | 15 | CyoEncode is made available under the terms of the Berkeley Software 16 | Distribution (BSD) licence, as detailed in LICENSE.TXT. This allows you 17 | complete freedom to use and distribute the code in source and/or binary form, 18 | as long as you respect the original copyright. 19 | 20 | ------------------------------------------------------------------------------- 21 | 2. Instructions 22 | ------------------------------------------------------------------------------- 23 | 24 | Simply copy the required source files (CyoEncode.h/cpp and CyoDecode.h/cpp) 25 | into your C/C++ project. 26 | 27 | Examples of usage can be found in the test.c file. 28 | 29 | For Unix/Linux developers, there's a shell script that will build the test 30 | using GCC. 31 | 32 | For Windows developers, Visual Studio projects are included. 33 | 34 | ------------------------------------------------------------------------------- 35 | 3. Release Notes 36 | ------------------------------------------------------------------------------- 37 | 38 | 1.0.2 - 5th January 2012 39 | - A little refactoring, added some shared functions. 40 | - Added VS42010 project file. 41 | - Added x64 build configurations. 42 | 43 | 1.0.1 - 25th September 2009 44 | - Added the cyoBase??Validate() functions. 45 | - Added detection of invalid encodings in the cyoBase??Decode() functions, 46 | rather than relying on assertions. 47 | 48 | 1.0.0 - 19th August 2009 49 | - First release. 50 | 51 | -------------------------------------------------------------------------------- /vendor/cyoencode-1.0.2/src/CyoDecode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * CyoDecode.h - part of the CyoEncode library 3 | * 4 | * Copyright (c) 2009-2012, Graham Bull. 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * * Redistributions of source code must retain the above copyright notice, this 11 | * list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above copyright notice, 14 | * this list of conditions and the following disclaimer in the documentation 15 | * and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | #ifndef __CYODECODE_H 30 | #define __CYODECODE_H 31 | 32 | #include 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | /* Base16 Decoding */ 39 | int cyoBase16Validate( const char* src, size_t size ); 40 | size_t cyoBase16DecodeGetLength( size_t size ); 41 | size_t cyoBase16Decode( void* dest, const char* src, size_t size ); 42 | 43 | /* Base32 Decoding */ 44 | int cyoBase32Validate( const char* src, size_t size ); 45 | size_t cyoBase32DecodeGetLength( size_t size ); 46 | size_t cyoBase32Decode( void* dest, const char* src, size_t size ); 47 | 48 | /* Base64 Decoding */ 49 | int cyoBase64Validate( const char* src, size_t size ); 50 | size_t cyoBase64DecodeGetLength( size_t size ); 51 | size_t cyoBase64Decode( void* dest, const char* src, size_t size ); 52 | 53 | #ifdef __cplusplus 54 | } 55 | #endif 56 | 57 | #endif /*__CYODECODE_H*/ 58 | 59 | -------------------------------------------------------------------------------- /vendor/cyoencode-1.0.2/src/CyoEncode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * CyoEncode.h - part of the CyoEncode library 3 | * 4 | * Copyright (c) 2009-2012, Graham Bull. 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * * Redistributions of source code must retain the above copyright notice, this 11 | * list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above copyright notice, 14 | * this list of conditions and the following disclaimer in the documentation 15 | * and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | */ 28 | 29 | #ifndef __CYOENCODE_H 30 | #define __CYOENCODE_H 31 | 32 | #include 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | /* Base16 Encoding */ 39 | size_t cyoBase16EncodeGetLength( size_t size ); 40 | size_t cyoBase16Encode( char* dest, const void* src, size_t size ); 41 | 42 | /* Base32 Encoding */ 43 | size_t cyoBase32EncodeGetLength( size_t size ); 44 | size_t cyoBase32Encode( char* dest, const void* src, size_t size ); 45 | 46 | /* Base64 Encoding */ 47 | size_t cyoBase64EncodeGetLength( size_t size ); 48 | size_t cyoBase64Encode( char* dest, const void* src, size_t size ); 49 | 50 | #ifdef __cplusplus 51 | } 52 | #endif 53 | 54 | #endif /*__CYOENCODE_H*/ 55 | 56 | -------------------------------------------------------------------------------- /vendor/cyoencode-1.0.2/src/build.sh: -------------------------------------------------------------------------------- 1 | gcc test.c CyoEncode.c CyoDecode.c -o test 2 | 3 | -------------------------------------------------------------------------------- /vendor/udt4/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | IF( APPLE ) 3 | add_definitions( -DOSX ) 4 | ELSEIF( WIN32 ) 5 | 6 | ELSE() 7 | add_definitions( -DLINUX ) 8 | ENDIF() 9 | 10 | set( udt_sources 11 | src/api.cpp 12 | src/buffer.cpp 13 | src/cache.cpp 14 | src/ccc.cpp 15 | src/channel.cpp 16 | src/common.cpp 17 | src/core.cpp 18 | src/epoll.cpp 19 | src/list.cpp 20 | src/md5.cpp 21 | src/packet.cpp 22 | src/queue.cpp 23 | src/window.cpp 24 | ) 25 | 26 | add_library( udt ${udt_sources} ) 27 | -------------------------------------------------------------------------------- /vendor/udt4/LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2001 - 2011, The Board of Trustees of the University of Illinois. 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are 6 | met: 7 | 8 | * Redistributions of source code must retain the above 9 | copyright notice, this list of conditions and the 10 | following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the 13 | above copyright notice, this list of conditions 14 | and the following disclaimer in the documentation 15 | and/or other materials provided with the distribution. 16 | 17 | * Neither the name of the University of Illinois 18 | nor the names of its contributors may be used to 19 | endorse or promote products derived from this 20 | software without specific prior written permission. 21 | 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 23 | IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 24 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 26 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 27 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 28 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 29 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 30 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 31 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 32 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | -------------------------------------------------------------------------------- /vendor/udt4/README.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2001 - 2011, The Board of Trustees of the University of Illinois. 2 | All Rights Reserved. 3 | Copyright (c) 2011 - 2012, Google, Inc. All Rights Reserved. 4 | 5 | UDP-based Data Transfer (UDT) Library - version 4 6 | Author: Yunhong Gu [yunhong.gu @ gmail.com] 7 | 8 | UDT version 4 is free software under BSD License. See ./LICENSE.txt. 9 | 10 | ============================================================================ 11 | 12 | UDT Website: 13 | http://udt.sf.net 14 | http://sf.net/projects/udt/ 15 | 16 | 17 | CONTENT: 18 | ./src: UDT source code 19 | ./app: Example programs 20 | ./doc: UDT documentation (HTML) 21 | ./win: Visual C++ project files for the Windows version of UDT 22 | 23 | 24 | To make: 25 | make -e os=XXX arch=YYY 26 | 27 | XXX: [LINUX(default), BSD, OSX] 28 | YYY: [IA32(default), POWERPC, IA64, AMD64] 29 | 30 | For example, on OS X, you may need to do "make -e os=OSX arch=POWERPC"; 31 | on 32-bit i386 Linux system, simply use "make". 32 | 33 | On Windows systems, use the Visual C++ project files in ./win directory. 34 | 35 | Note for BSD users, please use GNU Make. 36 | 37 | To use UDT in your application: 38 | Read index.htm in ./doc. The documentation is in HTML format and requires your 39 | browser to support JavaScript. 40 | 41 | 42 | Questions? please post to the UDT project forum: 43 | https://sourceforge.net/projects/udt/forums 44 | --------------------------------------------------------------------------------