diff options
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/epee/include/file_io_utils.h | 317 | ||||
-rw-r--r-- | contrib/epee/include/hex.h | 65 | ||||
-rw-r--r-- | contrib/epee/include/misc_log_ex.h | 11 | ||||
-rw-r--r-- | contrib/epee/include/span.h | 130 | ||||
-rw-r--r-- | contrib/epee/include/string_coding.h | 54 | ||||
-rw-r--r-- | contrib/epee/include/string_tools.h | 390 | ||||
-rw-r--r-- | contrib/epee/src/CMakeLists.txt | 3 | ||||
-rw-r--r-- | contrib/epee/src/hex.cpp | 82 | ||||
-rw-r--r-- | contrib/epee/src/http_auth.cpp | 22 | ||||
-rw-r--r-- | contrib/epee/src/mlog.cpp | 13 |
10 files changed, 298 insertions, 789 deletions
diff --git a/contrib/epee/include/file_io_utils.h b/contrib/epee/include/file_io_utils.h index 7e8521838..c387743a6 100644 --- a/contrib/epee/include/file_io_utils.h +++ b/contrib/epee/include/file_io_utils.h @@ -28,197 +28,13 @@ #ifndef _FILE_IO_UTILS_H_ #define _FILE_IO_UTILS_H_ - -//#include <sys/types.h> -//#include <sys/stat.h> - #include <iostream> #include <boost/filesystem.hpp> - -#ifndef MAKE64 - #define MAKE64(low,high) ((__int64)(((DWORD)(low)) | ((__int64)((DWORD)(high))) << 32)) -#endif - -#ifdef WINDOWS_PLATFORM -#include <psapi.h> -#include <strsafe.h> -#include <string.h> -#include <mbstring.h> - -#endif - - - namespace epee { namespace file_io_utils { -#ifdef WINDOWS_PLATFORM - - inline - std::string get_temp_file_name_a() - { - std::string str_result; - char sz_temp[MAX_PATH*2] = {0}; - if(!::GetTempPathA( sizeof( sz_temp ), sz_temp )) - return str_result; - - char sz_temp_file[MAX_PATH*2] = {0}; - if(!::GetTempFileNameA( sz_temp, "mail", 0, sz_temp_file)) - return str_result; - sz_temp_file[sizeof(sz_temp_file)-1] = 0; //be happy! - str_result = sz_temp_file; - return str_result; - } - - -#ifdef BOOST_LEXICAL_CAST_INCLUDED - inline - bool get_not_used_filename(const std::string& folder, OUT std::string& result_name) - { - DWORD folder_attr = ::GetFileAttributesA(folder.c_str()); - if(folder_attr == INVALID_FILE_ATTRIBUTES) - return false; - if(!(folder_attr&FILE_ATTRIBUTE_DIRECTORY)) - return false; - - - std::string base_name = folder + "\\tmp"; - std::string tmp_name; - bool name_found = false; - int current_index = 0; - tmp_name = base_name + boost::lexical_cast<std::string>(current_index) + ".tmp"; - while(!name_found) - { - if(INVALID_FILE_ATTRIBUTES == ::GetFileAttributesA(tmp_name.c_str())) - name_found = true; - else - { - current_index++; - tmp_name = base_name + boost::lexical_cast<std::string>(current_index) + ".tmp"; - } - } - result_name = tmp_name; - return true; - } -#endif - - inline - std::string get_temp_folder_a() - { - std::string str_result; - char sz_temp[MAX_PATH*2] = {0}; - if(!::GetTempPathA( sizeof( sz_temp ), sz_temp )) - return str_result; - sz_temp[(sizeof(sz_temp)/sizeof(sz_temp[0])) -1] = 0; - str_result = sz_temp; - return str_result; - } - - std::string convert_from_device_path_to_standart(const std::string& path) - { - - - STRSAFE_LPSTR pszFilename = (STRSAFE_LPSTR)path.c_str(); - - // Translate path with device name to drive letters. - char szTemp[4000] = {0}; - - if (::GetLogicalDriveStringsA(sizeof(szTemp)-1, szTemp)) - { - char szName[MAX_PATH]; - char szDrive[3] = " :"; - BOOL bFound = FALSE; - char* p = szTemp; - - do - { - // Copy the drive letter to the template string - *szDrive = *p; - - // Look up each device name - if (::QueryDosDeviceA(szDrive, szName, sizeof(szName))) - { - UINT uNameLen = strlen(szName); - - if (uNameLen < MAX_PATH) - { - bFound = _mbsnbicmp((const unsigned char*)pszFilename, (const unsigned char*)szName, - uNameLen) == 0; - - if (bFound) - { - // Reconstruct pszFilename using szTempFile - // Replace device path with DOS path - char szTempFile[MAX_PATH] = {0}; - StringCchPrintfA(szTempFile, - MAX_PATH, - "%s%s", - szDrive, - pszFilename+uNameLen); - return szTempFile; - //::StringCchCopyNA(pszFilename, MAX_PATH+1, szTempFile, strlen(szTempFile)); - } - } - } - - // Go to the next NULL character. - while (*p++); - } while (!bFound && *p); // end of string - } - - return ""; - } - - inline - std::string get_process_path_by_pid(DWORD pid) - { - std::string res; - - HANDLE hprocess = 0; - if( hprocess = ::OpenProcess( PROCESS_QUERY_INFORMATION|PROCESS_VM_READ, FALSE, pid) ) - { - char buff[MAX_PATH]= {0}; - if(!::GetModuleFileNameExA( hprocess, 0, buff, MAX_PATH - 1 )) - res = "Unknown_b"; - else - { - buff[MAX_PATH - 1]=0; //be happy! - res = buff; - std::string::size_type a = res.rfind( '\\' ); - if ( a != std::string::npos ) - res.erase( 0, a+1); - - } - ::CloseHandle( hprocess ); - }else - res = "Unknown_a"; - - return res; - } - - - - - - inline - std::wstring get_temp_file_name_w() - { - std::wstring str_result; - wchar_t sz_temp[MAX_PATH*2] = {0}; - if(!::GetTempPathW( sizeof(sz_temp)/sizeof(sz_temp[0]), sz_temp )) - return str_result; - - wchar_t sz_temp_file[MAX_PATH+1] = {0}; - if(!::GetTempFileNameW( sz_temp, L"mail", 0, sz_temp_file)) - return str_result; - - sz_temp_file[(sizeof(sz_temp_file)/sizeof(sz_temp_file[0]))-1] = 0; //be happy! - str_result = sz_temp_file; - return str_result; - } -#endif inline bool is_file_exist(const std::string& path) { @@ -226,35 +42,6 @@ namespace file_io_utils return boost::filesystem::exists(p); } - /* - inline - bool save_string_to_handle(HANDLE hfile, const std::string& str) - { - - - - if( INVALID_HANDLE_VALUE != hfile ) - { - DWORD dw; - if( !::WriteFile( hfile, str.data(), (DWORD) str.size(), &dw, NULL) ) - { - int err_code = GetLastError(); - //LOG_PRINT("Failed to write to file handle: " << hfile<< " Last error code:" << err_code << " : " << log_space::get_win32_err_descr(err_code), LOG_LEVEL_2); - return false; - } - ::CloseHandle(hfile); - return true; - }else - { - //LOG_WIN32_ERROR(::GetLastError()); - return false; - } - - return false; - }*/ - - - inline bool save_string_to_file(const std::string& path_to_file, const std::string& str) { @@ -275,32 +62,6 @@ namespace file_io_utils } } - /* - inline - bool load_form_handle(HANDLE hfile, std::string& str) - { - if( INVALID_HANDLE_VALUE != hfile ) - { - bool res = true; - DWORD dw = 0; - DWORD fsize = ::GetFileSize(hfile, &dw); - if(fsize > 300000000) - { - ::CloseHandle(hfile); - return false; - } - if(fsize) - { - str.resize(fsize); - if(!::ReadFile( hfile, (LPVOID)str.data(), (DWORD)str.size(), &dw, NULL)) - res = false; - } - ::CloseHandle(hfile); - return res; - } - return false; - } - */ inline bool get_file_time(const std::string& path_to_file, OUT time_t& ft) { @@ -371,84 +132,6 @@ namespace file_io_utils return false; } } - - /* - bool remove_dir_and_subirs(const char* path_to_dir); - - inline - bool clean_dir(const char* path_to_dir) - { - if(!path_to_dir) - return false; - - std::string folder = path_to_dir; - WIN32_FIND_DATAA find_data = {0}; - HANDLE hfind = ::FindFirstFileA((folder + "\\*.*").c_str(), &find_data); - if(INVALID_HANDLE_VALUE == hfind) - return false; - do{ - if(!strcmp("..", find_data.cFileName) || (!strcmp(".", find_data.cFileName))) - continue; - - if(find_data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) - { - if(!remove_dir_and_subirs((folder + "\\" + find_data.cFileName).c_str())) - return false; - }else - { - if(!::DeleteFileA((folder + "\\" + find_data.cFileName).c_str())) - return false; - } - - - }while(::FindNextFileA(hfind, &find_data)); - ::FindClose(hfind); - - return true; - } - */ -#ifdef WINDOWS_PLATFORM - inline bool get_folder_content(const std::string& path, std::list<WIN32_FIND_DATAA>& OUT target_list) - { - WIN32_FIND_DATAA find_data = {0}; - HANDLE hfind = ::FindFirstFileA((path + "\\*.*").c_str(), &find_data); - if(INVALID_HANDLE_VALUE == hfind) - return false; - do{ - if(!strcmp("..", find_data.cFileName) || (!strcmp(".", find_data.cFileName))) - continue; - - target_list.push_back(find_data); - - }while(::FindNextFileA(hfind, &find_data)); - ::FindClose(hfind); - - return true; - } -#endif - inline bool get_folder_content(const std::string& path, std::list<std::string>& OUT target_list, bool only_files = false) - { - try - { - - boost::filesystem::directory_iterator end_itr; // default construction yields past-the-end - for ( boost::filesystem::directory_iterator itr( path ); itr != end_itr; ++itr ) - { - if ( only_files && boost::filesystem::is_directory(itr->status()) ) - { - continue; - } - target_list.push_back(itr->path().filename().string()); - } - - } - - catch(...) - { - return false; - } - return true; - } } } diff --git a/contrib/epee/include/hex.h b/contrib/epee/include/hex.h new file mode 100644 index 000000000..f8d6be048 --- /dev/null +++ b/contrib/epee/include/hex.h @@ -0,0 +1,65 @@ +// Copyright (c) 2017, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#pragma once + +#include <array> +#include <cstdint> +#include <iosfwd> +#include <string> + +#include "span.h" + +namespace epee +{ + struct to_hex + { + //! \return A std::string containing hex of `src`. + static std::string string(const span<const std::uint8_t> src); + + //! \return An array containing hex of `src`. + template<std::size_t N> + static std::array<char, N * 2> array(const std::array<std::uint8_t, N>& src) noexcept + { + std::array<char, N * 2> out{{}}; + static_assert(N <= 128, "keep the stack size down"); + buffer_unchecked(out.data(), {src.data(), src.size()}); + return out; + } + + //! Append `src` as hex to `out`. + static void buffer(std::ostream& out, const span<const std::uint8_t> src); + + //! Append `< + src + >` as hex to `out`. + static void formatted(std::ostream& out, const span<const std::uint8_t> src); + + private: + //! Write `src` bytes as hex to `out`. `out` must be twice the length + static void buffer_unchecked(char* out, const span<const std::uint8_t> src) noexcept; + }; +} diff --git a/contrib/epee/include/misc_log_ex.h b/contrib/epee/include/misc_log_ex.h index ae61965fa..6eeb1441f 100644 --- a/contrib/epee/include/misc_log_ex.h +++ b/contrib/epee/include/misc_log_ex.h @@ -49,15 +49,6 @@ #include <boost/filesystem.hpp> #include <boost/algorithm/string.hpp> -#define ELPP_THREAD_SAFE -#define ELPP_DEFAULT_LOG_FILE "" -#if !defined __GNUC__ || defined __MINGW32__ || defined __MINGW64__ || defined __ANDROID__ -#else -#define ELPP_STACKTRACE_ON_CRASH 1 -#endif -#define ELPP_DISABLE_DEFAULT_CRASH_HANDLING -#define ELPP_FEATURE_CRASH_LOG 1 -#define ELPP_DISABLE_CHECK_MACROS #include "easylogging++.h" #define MONERO_DEFAULT_LOG_CATEGORY "default" @@ -118,7 +109,7 @@ #define _warn(x) MWARNING(x) #define _erro(x) MERROR(x) -#define MLOG_SET_THREAD_NAME(x) el::Loggers::setThreadName(x) +#define MLOG_SET_THREAD_NAME(x) el::Helpers::setThreadName(x) #ifndef LOCAL_ASSERT #include <assert.h> diff --git a/contrib/epee/include/span.h b/contrib/epee/include/span.h new file mode 100644 index 000000000..ea4ba63dd --- /dev/null +++ b/contrib/epee/include/span.h @@ -0,0 +1,130 @@ +// Copyright (c) 2017, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#pragma once + +#include <cstdint> +#include <memory> +#include <type_traits> + +namespace epee +{ + /*! + \brief Non-owning sequence of data. Does not deep copy + + Inspired by `gsl::span` and/or `boost::iterator_range`. This class is + intended to be used as a parameter type for functions that need to take a + writable or read-only sequence of data. Most common cases are `span<char>` + and `span<std::uint8_t>`. Using as a class member is only recommended if + clearly documented as not doing a deep-copy. C-arrays are easily convertible + to this type. + + \note Conversion from C string literal to `span<const char>` will include + the NULL-terminator. + \note Never allows derived-to-base pointer conversion; an array of derived + types is not an array of base types. + */ + template<typename T> + class span + { + /* Supporting class types is tricky - the {ptr,len} constructor will allow + derived-to-base conversions. This is NOT desireable because an array of + derived types is not an array of base types. It is possible to handle + this case, implement when/if needed. */ + static_assert(!std::is_class<T>(), "no class types are currently allowed"); + public: + using value_type = T; + using size_type = std::size_t; + using difference_type = std::ptrdiff_t; + using pointer = T*; + using const_pointer = const T*; + using reference = T&; + using const_reference = const T&; + using iterator = pointer; + using const_iterator = const_pointer; + + constexpr span() noexcept : ptr(nullptr), len(0) {} + constexpr span(std::nullptr_t) noexcept : span() {} + + constexpr span(T* const src_ptr, const std::size_t count) noexcept + : ptr(src_ptr), len(count) {} + + //! Conversion from C-array. Prevents common bugs with sizeof + arrays. + template<std::size_t N> + constexpr span(T (&src)[N]) noexcept : span(src, N) {} + + constexpr span(const span&) noexcept = default; + span& operator=(const span&) noexcept = default; + + constexpr iterator begin() const noexcept { return ptr; } + constexpr const_iterator cbegin() const noexcept { return ptr; } + + constexpr iterator end() const noexcept { return begin() + size(); } + constexpr const_iterator cend() const noexcept { return cbegin() + size(); } + + constexpr bool empty() const noexcept { return size() == 0; } + constexpr pointer data() const noexcept { return ptr; } + constexpr std::size_t size() const noexcept { return len; } + constexpr std::size_t size_bytes() const noexcept { return size() * sizeof(value_type); } + + private: + T* ptr; + std::size_t len; + }; + + //! \return `span<const T::value_type>` from a STL compatible `src`. + template<typename T> + constexpr span<const typename T::value_type> to_span(const T& src) + { + // compiler provides diagnostic if size() is not size_t. + return {src.data(), src.size()}; + } + + template<typename T> + constexpr bool has_padding() noexcept + { + return !std::is_pod<T>() || alignof(T) != 1; + } + + //! \return Cast data from `src` as `span<const std::uint8_t>`. + template<typename T> + span<const std::uint8_t> to_byte_span(const span<const T> src) noexcept + { + static_assert(!has_padding<T>(), "source type may have padding"); + return {reinterpret_cast<const std::uint8_t*>(src.data()), src.size_bytes()}; + } + + //! \return `span<const std::uint8_t>` which represents the bytes at `&src`. + template<typename T> + span<const std::uint8_t> as_byte_span(const T& src) noexcept + { + static_assert(!std::is_empty<T>(), "empty types will not work -> sizeof == 1"); + static_assert(!has_padding<T>(), "source type may have padding"); + return {reinterpret_cast<const std::uint8_t*>(std::addressof(src)), sizeof(T)}; + } +} diff --git a/contrib/epee/include/string_coding.h b/contrib/epee/include/string_coding.h index a2e3d6eb2..82050ef96 100644 --- a/contrib/epee/include/string_coding.h +++ b/contrib/epee/include/string_coding.h @@ -29,7 +29,7 @@ #define _STRING_CODING_H_ #include <string> -//#include "md5_l.h" + namespace epee { namespace string_encoding @@ -62,23 +62,6 @@ namespace string_encoding (char*)str_trgt.data(), (int)str_trgt.size(), 0, 0); return str_trgt;*/ } -#ifdef WINDOWS_PLATFORM_EX - inline std::string convert_to_ansii_win(const std::wstring& str_from) - { - - int code_page = CP_ACP; - std::string str_trgt; - if(!str_from.size()) - return str_trgt; - int cb = ::WideCharToMultiByte( code_page, 0, str_from.data(), (__int32)str_from.size(), 0, 0, 0, 0 ); - if(!cb) - return str_trgt; - str_trgt.resize(cb); - ::WideCharToMultiByte( code_page, 0, str_from.data(), (int)str_from.size(), - (char*)str_trgt.data(), (int)str_trgt.size(), 0, 0); - return str_trgt; - } -#endif inline std::string convert_to_ansii(const std::string& str_from) { @@ -254,41 +237,6 @@ namespace string_encoding return ret; } - //md5 -#ifdef MD5_H - inline - std::string get_buf_as_hex_string(const void* pbuf, size_t len) - { - std::ostringstream result; - - const unsigned char* p_buff = (const unsigned char*)pbuf; - - for(unsigned int i=0;i<len;i++) - { // convert md to hex-represented string (hex-letters in upper case!) - result << std::setw(2) << std::setfill('0') - << std::setbase(16) << std::nouppercase - << (int)*p_buff++; - } - - return result.str(); - } - - inline - std::string get_md5_as_hexstring(const void* pbuff, size_t len) - { - unsigned char output[16] = {0}; - md5::md5((unsigned char*)pbuff, static_cast<unsigned int>(len), output); - return get_buf_as_hex_string(output, sizeof(output)); - } - - inline - std::string get_md5_as_hexstring(const std::string& src) - { - return get_md5_as_hexstring(src.data(), src.size()); - } -#endif - - } } diff --git a/contrib/epee/include/string_tools.h b/contrib/epee/include/string_tools.h index 530000028..16e60e99a 100644 --- a/contrib/epee/include/string_tools.h +++ b/contrib/epee/include/string_tools.h @@ -35,16 +35,16 @@ # include <windows.h> #endif -//#include <objbase.h> #include <locale> #include <cstdlib> -#include <iomanip> -#include <map> +#include <string> #include <type_traits> #include <boost/uuid/uuid.hpp> #include <boost/uuid/uuid_io.hpp> #include <boost/lexical_cast.hpp> #include <boost/algorithm/string.hpp> +#include "hex.h" +#include "span.h" #include "warnings.h" @@ -60,38 +60,12 @@ namespace epee { namespace string_tools { - inline std::wstring get_str_from_guid(const boost::uuids::uuid& rid) - { - return boost::lexical_cast<std::wstring>(rid); - } //---------------------------------------------------------------------------- inline std::string get_str_from_guid_a(const boost::uuids::uuid& rid) { return boost::lexical_cast<std::string>(rid); } //---------------------------------------------------------------------------- - inline bool get_guid_from_string( boost::uuids::uuid& inetifer, std::wstring str_id) - { - if(str_id.size() < 36) - return false; - - if('{' == *str_id.begin()) - str_id.erase(0, 1); - - if('}' == *(--str_id.end())) - str_id.erase(--str_id.end()); - - try - { - inetifer = boost::lexical_cast<boost::uuids::uuid>(str_id); - return true; - } - catch(...) - { - return false; - } - } - //---------------------------------------------------------------------------- inline bool get_guid_from_string(OUT boost::uuids::uuid& inetifer, const std::string& str_id) { std::string local_str_id = str_id; @@ -114,33 +88,10 @@ namespace string_tools return false; } } - //---------------------------------------------------------------------------- - template<class CharT> - std::basic_string<CharT> buff_to_hex(const std::basic_string<CharT>& s) - { - using namespace std; - basic_stringstream<CharT> hexStream; - hexStream << hex << noshowbase << setw(2); - - for(typename std::basic_string<CharT>::const_iterator it = s.begin(); it != s.end(); it++) - { - hexStream << "0x"<< static_cast<unsigned int>(static_cast<unsigned char>(*it)) << " "; - } - return hexStream.str(); - } //---------------------------------------------------------------------------- - template<class CharT> - std::basic_string<CharT> buff_to_hex_nodelimer(const std::basic_string<CharT>& s) + inline std::string buff_to_hex_nodelimer(const std::string& src) { - using namespace std; - basic_stringstream<CharT> hexStream; - hexStream << hex << noshowbase; - - for(typename std::basic_string<CharT>::const_iterator it = s.begin(); it != s.end(); it++) - { - hexStream << setw(2) << setfill('0') << static_cast<unsigned int>(static_cast<unsigned char>(*it)); - } - return hexStream.str(); + return to_hex::string(to_byte_span(to_span(src))); } //---------------------------------------------------------------------------- template<class CharT> @@ -222,22 +173,6 @@ DISABLE_GCC_WARNING(maybe-uninitialized) return true; } POP_WARNINGS - //--------------------------------------------------- - template<typename int_t> - bool get_xnum_from_hex_string(const std::string str, int_t& res ) - { - try - { - std::stringstream ss; - ss << std::hex << str; - ss >> res; - return true; - } - catch(...) - { - return false; - } - } //---------------------------------------------------------------------------- template<class XType> inline bool xtype_to_string(const XType& val, std::string& str) @@ -253,83 +188,6 @@ POP_WARNINGS return true; } - - typedef std::map<std::string, std::string> command_line_params_a; - typedef std::map<std::wstring, std::wstring> command_line_params_w; - - template<class t_string> - bool parse_commandline(std::map<t_string, t_string>& res, int argc, char** argv) - { - t_string key; - for(int i = 1; i < argc; i++) - { - if(!argv[i]) - break; - t_string s = argv[i]; - std::string::size_type p = s.find('='); - if(std::string::npos == p) - { - res[s] = ""; - }else - { - std::string ss; - t_string nm = s.substr(0, p); - t_string vl = s.substr(p+1, s.size()); - res[nm] = vl; - } - } - return true; - } - -/* template<typename t_type> - bool get_xparam_from_command_line(const std::map<std::string, std::string>& res, const std::basic_string<typename t_string::value_type> & key, t_type& val) - { - - } - */ - - template<class t_string, typename t_type> - bool get_xparam_from_command_line(const std::map<t_string, t_string>& res, const t_string & key, t_type& val) - { - typename std::map<t_string, t_string>::const_iterator it = res.find(key); - if(it == res.end()) - return false; - - if(it->second.size()) - { - return get_xtype_from_string(val, it->second); - } - - return true; - } - - template<class t_string, typename t_type> - t_type get_xparam_from_command_line(const std::map<t_string, t_string>& res, const t_string & key, const t_type& default_value) - { - typename std::map<t_string, t_string>::const_iterator it = res.find(key); - if(it == res.end()) - return default_value; - - if(it->second.size()) - { - t_type s; - get_xtype_from_string(s, it->second); - return s; - } - - return default_value; - } - - template<class t_string> - bool have_in_command_line(const std::map<t_string, t_string>& res, const std::basic_string<typename t_string::value_type>& key) - { - typename std::map<t_string, t_string>::const_iterator it = res.find(key); - if(it == res.end()) - return false; - - return true; - } - //---------------------------------------------------------------------------- std::string get_ip_string_from_int32(uint32_t ip); //---------------------------------------------------------------------------- @@ -363,16 +221,6 @@ POP_WARNINGS return true; } - //---------------------------------------------------------------------------- - template<typename t> - inline std::string get_t_as_hex_nwidth(const t& v, std::streamsize w = 8) - { - std::stringstream ss; - ss << std::setfill ('0') << std::setw (w) << std::hex << std::noshowbase; - ss << v; - return ss.str(); - } - inline std::string num_to_string_fast(int64_t val) { /* @@ -382,68 +230,6 @@ POP_WARNINGS return boost::lexical_cast<std::string>(val); } //---------------------------------------------------------------------------- - inline bool string_to_num_fast(const std::string& buff, int64_t& val) - { - //return get_xtype_from_string(val, buff); -#if (defined _MSC_VER) - val = _atoi64(buff.c_str()); -#else - val = atoll(buff.c_str()); -#endif - /* - * val = atoi64(buff.c_str()); - */ - if(buff != "0" && val == 0) - return false; - return true; - } - //---------------------------------------------------------------------------- - inline bool string_to_num_fast(const std::string& buff, int& val) - { - val = atoi(buff.c_str()); - if(buff != "0" && val == 0) - return false; - - return true; - } - //---------------------------------------------------------------------------- -#ifdef WINDOWS_PLATFORM - inline std::string system_time_to_string(const SYSTEMTIME& st) - { - - /* - TIME_ZONE_INFORMATION tzi; - GetTimeZoneInformation(&tzi); - SystemTimeToTzSpecificLocalTime(&tzi, &stUTC, &stLocal); - */ - - char szTime[25], szDate[25]; - ::GetTimeFormatA( - LOCALE_USER_DEFAULT, // locale - TIME_FORCE24HOURFORMAT, // options - &st, // time - NULL, // time format string - szTime, // formatted string buffer - 25 // size of string buffer - ); - - ::GetDateFormatA( - LOCALE_USER_DEFAULT, // locale - NULL, // options - &st, // date - NULL, // date format - szDate, // formatted string buffer - 25 // size of buffer - ); - szTime[24] = szDate[24] = 0; //be happy :) - - std::string res = szDate; - (res += " " )+= szTime; - return res; - - } -#endif - //---------------------------------------------------------------------------- inline bool compare_no_case(const std::string& str1, const std::string& str2) { @@ -451,33 +237,6 @@ POP_WARNINGS return !boost::iequals(str1, str2); } //---------------------------------------------------------------------------- - inline bool compare_no_case(const std::wstring& str1, const std::wstring& str2) - { - return !boost::iequals(str1, str2); - } - //---------------------------------------------------------------------------- - inline bool is_match_prefix(const std::wstring& str1, const std::wstring& prefix) - { - if(prefix.size()>str1.size()) - return false; - - if(!compare_no_case(str1.substr(0, prefix.size()), prefix)) - return true; - else - return false; - } - //---------------------------------------------------------------------------- - inline bool is_match_prefix(const std::string& str1, const std::string& prefix) - { - if(prefix.size()>str1.size()) - return false; - - if(!compare_no_case(str1.substr(0, prefix.size()), prefix)) - return true; - else - return false; - } - //---------------------------------------------------------------------------- inline std::string& get_current_module_name() { static std::string module_name; @@ -559,9 +318,7 @@ POP_WARNINGS std::string pod_to_hex(const t_pod_type& s) { static_assert(std::is_pod<t_pod_type>::value, "expected pod type"); - std::string buff; - buff.assign(reinterpret_cast<const char*>(&s), sizeof(s)); - return buff_to_hex_nodelimer(buff); + return to_hex::string(as_byte_span(s)); } //---------------------------------------------------------------------------- template<class t_pod_type> @@ -592,20 +349,6 @@ POP_WARNINGS return res; } //---------------------------------------------------------------------------- - inline std::string get_filename_from_path(const std::string& str) - { - std::string res; - std::string::size_type pos = str.rfind('\\'); - if(std::string::npos == pos) - return str; - - res = str.substr(pos+1, str.size()-pos); - return res; - } - //---------------------------------------------------------------------------- - - - inline std::string cut_off_extension(const std::string& str) { std::string res; @@ -616,127 +359,6 @@ POP_WARNINGS res = str.substr(0, pos); return res; } - - //---------------------------------------------------------------------------- -#ifdef _WININET_ - inline std::string get_string_from_systemtime(const SYSTEMTIME& sys_time) - { - std::string result_string; - - char buff[100] = {0}; - BOOL res = ::InternetTimeFromSystemTimeA(&sys_time, INTERNET_RFC1123_FORMAT, buff, 99); - if(!res) - { - LOG_ERROR("Failed to load SytemTime to string"); - } - - result_string = buff; - return result_string; - - } - //------------------------------------------------------------------------------------- - inline SYSTEMTIME get_systemtime_from_string(const std::string& buff) - { - SYSTEMTIME result_time = {0}; - - BOOL res = ::InternetTimeToSystemTimeA(buff.c_str(), &result_time, NULL); - if(!res) - { - LOG_ERROR("Failed to load SytemTime from string " << buff << "interval set to 15 minutes"); - } - - return result_time; - } -#endif - -#ifdef WINDOWS_PLATFORM - static const DWORD INFO_BUFFER_SIZE = 10000; - - static const wchar_t* get_pc_name() - { - static wchar_t info[INFO_BUFFER_SIZE]; - static DWORD bufCharCount = INFO_BUFFER_SIZE; - static bool init = false; - - if (!init) { - if (!GetComputerNameW( info, &bufCharCount )) - info[0] = 0; - else - init = true; - } - - return info; - } - - static const wchar_t* get_user_name() - { - static wchar_t info[INFO_BUFFER_SIZE]; - static DWORD bufCharCount = INFO_BUFFER_SIZE; - static bool init = false; - - if (!init) { - if (!GetUserNameW( info, &bufCharCount )) - info[0] = 0; - else - init = true; - } - - return info; - } -#endif - -#ifdef _LM_ - static const wchar_t* get_domain_name() - { - static wchar_t info[INFO_BUFFER_SIZE]; - static DWORD bufCharCount = 0; - static bool init = false; - - if (!init) { - LPWSTR domain( NULL ); - NETSETUP_JOIN_STATUS status; - info[0] = 0; - - if (NET_API_STATUS result = NetGetJoinInformation( NULL, &domain, &status )) - { - LOG_ERROR("get_domain_name error: " << log_space::get_win32_err_descr(result)); - } else - { - StringCchCopyW( info, sizeof(info)/sizeof( info[0] ), domain ); - NetApiBufferFree((void*)domain); - init = true; - } - } - - return info; - } -#endif -#ifdef WINDOWS_PLATFORM - inline - std::string load_resource_string_a(int id, const char* pmodule_name = NULL) - { - //slow realization - HMODULE h = ::GetModuleHandleA( pmodule_name ); - - char buff[2000] = {0}; - - ::LoadStringA( h, id, buff, sizeof(buff)); - buff[sizeof(buff)-1] = 0; //be happy :) - return buff; - } - inline - std::wstring load_resource_string_w(int id, const char* pmodule_name = NULL) - { - //slow realization - HMODULE h = ::GetModuleHandleA( pmodule_name ); - - wchar_t buff[2000] = {0}; - - ::LoadStringW( h, id, buff, sizeof(buff) / sizeof( buff[0] ) ); - buff[(sizeof(buff)/sizeof(buff[0]))-1] = 0; //be happy :) - return buff; - } -#endif } } #endif //_STRING_TOOLS_H_ diff --git a/contrib/epee/src/CMakeLists.txt b/contrib/epee/src/CMakeLists.txt index 1e79755e5..1d5fa0394 100644 --- a/contrib/epee/src/CMakeLists.txt +++ b/contrib/epee/src/CMakeLists.txt @@ -26,7 +26,7 @@ # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -add_library(epee STATIC http_auth.cpp mlog.cpp string_tools.cpp) +add_library(epee STATIC hex.cpp http_auth.cpp mlog.cpp string_tools.cpp) # Build and install libepee if we're building for GUI if (BUILD_GUI_DEPS) if(IOS) @@ -41,6 +41,7 @@ endif() target_link_libraries(epee PUBLIC crypto + easylogging ${Boost_FILESYSTEM_LIBRARY} PRIVATE ${EXTRA_LIBRARIES}) diff --git a/contrib/epee/src/hex.cpp b/contrib/epee/src/hex.cpp new file mode 100644 index 000000000..cfbd3cf87 --- /dev/null +++ b/contrib/epee/src/hex.cpp @@ -0,0 +1,82 @@ +// Copyright (c) 2017, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "hex.h" + +#include <iterator> +#include <limits> +#include <ostream> +#include <stdexcept> + +namespace epee +{ + namespace + { + template<typename T> + void write_hex(T&& out, const span<const std::uint8_t> src) + { + static constexpr const char hex[] = u8"0123456789abcdef"; + static_assert(sizeof(hex) == 17, "bad string size"); + for (const std::uint8_t byte : src) + { + *out = hex[byte >> 4]; + ++out; + *out = hex[byte & 0x0F]; + ++out; + } + } + } + + std::string to_hex::string(const span<const std::uint8_t> src) + { + if (std::numeric_limits<std::size_t>::max() / 2 < src.size()) + throw std::range_error("hex_view::to_string exceeded maximum size"); + + std::string out{}; + out.resize(src.size() * 2); + buffer_unchecked(std::addressof(out[0]), src); + return out; + } + + void to_hex::buffer(std::ostream& out, const span<const std::uint8_t> src) + { + write_hex(std::ostreambuf_iterator<char>{out}, src); + } + + void to_hex::formatted(std::ostream& out, const span<const std::uint8_t> src) + { + out.put('<'); + buffer(out, src); + out.put('>'); + } + + void to_hex::buffer_unchecked(char* out, const span<const std::uint8_t> src) noexcept + { + return write_hex(out, src); + } +} diff --git a/contrib/epee/src/http_auth.cpp b/contrib/epee/src/http_auth.cpp index 5a1c2142a..30e562700 100644 --- a/contrib/epee/src/http_auth.cpp +++ b/contrib/epee/src/http_auth.cpp @@ -67,6 +67,7 @@ #include <type_traits> #include "crypto/crypto.h" +#include "hex.h" #include "md5_l.h" #include "string_coding.h" @@ -104,25 +105,6 @@ namespace //// Digest Algorithms - template<std::size_t N> - std::array<char, N * 2> to_hex(const std::array<std::uint8_t, N>& digest) noexcept - { - static constexpr const char alphabet[] = u8"0123456789abcdef"; - static_assert(sizeof(alphabet) == 17, "bad alphabet size"); - - // TODO upgrade (improve performance) of to hex in epee string tools - std::array<char, N * 2> out{{}}; - auto current = out.begin(); - for (const std::uint8_t byte : digest) - { - *current = alphabet[byte >> 4]; - ++current; - *current = alphabet[byte & 0x0F]; - ++current; - } - return out; - } - struct md5_ { static constexpr const boost::string_ref name = ceref(u8"MD5"); @@ -156,7 +138,7 @@ namespace std::array<std::uint8_t, 16> digest{{}}; md5::MD5Final(digest.data(), std::addressof(ctx)); - return to_hex(digest); + return epee::to_hex::array(digest); } }; constexpr const boost::string_ref md5_::name; diff --git a/contrib/epee/src/mlog.cpp b/contrib/epee/src/mlog.cpp index 205acccc9..7487fdbd2 100644 --- a/contrib/epee/src/mlog.cpp +++ b/contrib/epee/src/mlog.cpp @@ -33,8 +33,13 @@ INITIALIZE_EASYLOGGINGPP +#undef MONERO_DEFAULT_LOG_CATEGORY +#define MONERO_DEFAULT_LOG_CATEGORY "logging" + #define MLOG_BASE_FORMAT "%datetime{%Y-%M-%d %H:%m:%s.%g}\t%thread\t%level\t%logger\t%loc\t%msg" +#define MLOG_LOG(x) CINFO(el::base::Writer,el::base::DispatchAction::FileOnlyLog,MONERO_DEFAULT_LOG_CATEGORY) << x + using namespace epee; static std::string generate_log_filename(const char *base) @@ -88,10 +93,10 @@ static const char *get_default_categories(int level) switch (level) { case 0: - categories = "*:WARNING,net:FATAL,net.p2p:FATAL,net.cn:FATAL,global:INFO,verify:FATAL,stacktrace:INFO"; + categories = "*:WARNING,net:FATAL,net.p2p:FATAL,net.cn:FATAL,global:INFO,verify:FATAL,stacktrace:INFO,logging:INFO"; break; case 1: - categories = "*:WARNING,global:INFO,stacktrace:INFO"; + categories = "*:WARNING,global:INFO,stacktrace:INFO,logging:INFO"; break; case 2: categories = "*:DEBUG"; @@ -142,7 +147,7 @@ void mlog_configure(const std::string &filename_base, bool console) void mlog_set_categories(const char *categories) { el::Loggers::setCategories(categories); - MGINFO("New log categories: " << categories); + MLOG_LOG("New log categories: " << categories); } // maps epee style log level to new logging system @@ -150,7 +155,7 @@ void mlog_set_log_level(int level) { const char *categories = get_default_categories(level); el::Loggers::setCategories(categories); - MGINFO("New log categories: " << categories); + MLOG_LOG("New log categories: " << categories); } void mlog_set_log(const char *log) |