From c085e9294f096ebcfe3e02f1c9a99959f66ba1a2 Mon Sep 17 00:00:00 2001 From: jebes Date: Tue, 9 Sep 2014 20:18:23 -0400 Subject: [PATCH 1/5] commented util.h --- .gitignore | 16 +++++++ src/common/util.cpp | 8 ++-- src/common/util.h | 45 ++++++++++++++++-- src/daemon/daemon.cpp | 69 ++++++++++++++-------------- src/daemon/daemon_commands_handler.h | 8 +++- 5 files changed, 102 insertions(+), 44 deletions(-) diff --git a/.gitignore b/.gitignore index 527864624..4f8766a43 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,23 @@ .DS_Store +/doc /build /tags # vim swap files *.swp *.swo +TAGS +!TAGS/ +tags +!tags/ +gtags.files +GTAGS +GRTAGS +GPATH +cscope.files +cscope.out +cscope.in.out +cscope.po.out + + + diff --git a/src/common/util.cpp b/src/common/util.cpp index e5276d1e4..d2ff90679 100644 --- a/src/common/util.cpp +++ b/src/common/util.cpp @@ -313,17 +313,19 @@ std::string get_nix_version_display_string() return ""; } #endif - + std::string get_default_data_dir() { - //namespace fs = boost::filesystem; + /* Please for the love of god refactor the ifdefs out of this */ + + // namespace fs = boost::filesystem; // Windows < Vista: C:\Documents and Settings\Username\Application Data\CRYPTONOTE_NAME // Windows >= Vista: C:\Users\Username\AppData\Roaming\CRYPTONOTE_NAME // Mac: ~/Library/Application Support/CRYPTONOTE_NAME // Unix: ~/.CRYPTONOTE_NAME std::string config_folder; + #ifdef WIN32 - // Windows config_folder = get_special_folder_path(CSIDL_APPDATA, true) + "/" + CRYPTONOTE_NAME; #else std::string pathRet; diff --git a/src/common/util.h b/src/common/util.h index 92ce7177a..876673d0a 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -39,11 +39,41 @@ #include "misc_language.h" #include "p2p/p2p_protocol_defs.h" +/*! \brief Various Tools + * + * + * + */ namespace tools { + /*! \brief Returns the default data directory. + * + * \details Windows < Vista: C:\\Documents and Settings\\Username\\Application Data\\CRYPTONOTE_NAME + * + * Windows >= Vista: C:\\Users\\Username\\AppData\\Roaming\\CRYPTONOTE_NAME + * + * Mac: ~/Library/Application Support/CRYPTONOTE_NAME + * + * Unix: ~/.CRYPTONOTE_NAME + */ std::string get_default_data_dir(); + + /*! \breif Returns the OS version string + * + * \details This is a wrapper around the primitives + * get_windows_version_display_string() and + * get_nix_version_display_string() + */ std::string get_os_version_string(); + + /*! \breif creates directories for a path + * + * wrapper around boost::filesyste::create_directories. + * (ensure-directory-exists): greenspun's tenth rule in action! + */ bool create_directories_if_necessary(const std::string& path); + /*! \brief std::rename wrapper for nix and something strange for windows. + */ std::error_code replace_file(const std::string& replacement_name, const std::string& replaced_name); inline crypto::hash get_proof_of_trust_hash(const nodetool::proof_of_trust& pot) @@ -54,10 +84,12 @@ namespace tools return crypto::cn_fast_hash(s.data(), s.size()); } - + /*! \breif Defines a signal handler for win32 and *nix + */ class signal_handler { public: + /*! \brief installs a signal handler */ template static bool install(T t) { @@ -69,6 +101,7 @@ namespace tools } return r; #else + /* Only blocks SIGINT and SIGTERM */ signal(SIGINT, posix_handler); signal(SIGTERM, posix_handler); m_handler = t; @@ -78,12 +111,12 @@ namespace tools private: #if defined(WIN32) + /*! \breif Handler for win */ static BOOL WINAPI win_handler(DWORD type) { if (CTRL_C_EVENT == type || CTRL_BREAK_EVENT == type) { handle_signal(); - return TRUE; } else { @@ -93,20 +126,22 @@ namespace tools return TRUE; } #else + /*! \breif handler for NIX */ static void posix_handler(int /*type*/) { handle_signal(); } #endif + /*! \breif calles m_handler */ static void handle_signal() { - static std::mutex m_mutex; - std::unique_lock lock(m_mutex); + /* static std::mutex m_mutex; */ + /* std::unique_lock lock(m_mutex); */ m_handler(); } - private: + /*! \breif where the installed handler is stored */ static std::function m_handler; }; } diff --git a/src/daemon/daemon.cpp b/src/daemon/daemon.cpp index 3462227dc..55c6bffef 100644 --- a/src/daemon/daemon.cpp +++ b/src/daemon/daemon.cpp @@ -29,7 +29,7 @@ // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers // node.cpp : Defines the entry point for the console application. -// +// Does this file exist? #include "include_base_utils.h" @@ -64,11 +64,42 @@ namespace const command_line::arg_descriptor arg_console = {"no-console", "Disable daemon console commands"}; } -bool command_line_preprocessor(const boost::program_options::variables_map& vm); +bool command_line_preprocessor(const boost::program_options::variables_map& vm) +{ + bool exit = false; + if (command_line::get_arg(vm, command_line::arg_version)) + { + std::cout << CRYPTONOTE_NAME << " v" << PROJECT_VERSION_LONG << ENDL; + exit = true; + } + if (command_line::get_arg(vm, arg_os_version)) + { + std::cout << "OS: " << tools::get_os_version_string() << ENDL; + exit = true; + } + + if (exit) + { + return true; + } + + int new_log_level = command_line::get_arg(vm, arg_log_level); + if(new_log_level < LOG_LEVEL_MIN || new_log_level > LOG_LEVEL_MAX) + { + LOG_PRINT_L0("Wrong log level value: "); + } + else if (log_space::get_set_log_detalisation_level(false) != new_log_level) + { + log_space::get_set_log_detalisation_level(true, new_log_level); + LOG_PRINT_L0("LOG_LEVEL set to " << new_log_level); + } + + return false; +} int main(int argc, char* argv[]) { - + string_tools::set_module_name_and_folder(argv[0]); #ifdef WIN32 _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); @@ -235,35 +266,3 @@ int main(int argc, char* argv[]) CATCH_ENTRY_L0("main", 1); } -bool command_line_preprocessor(const boost::program_options::variables_map& vm) -{ - bool exit = false; - if (command_line::get_arg(vm, command_line::arg_version)) - { - std::cout << CRYPTONOTE_NAME << " v" << PROJECT_VERSION_LONG << ENDL; - exit = true; - } - if (command_line::get_arg(vm, arg_os_version)) - { - std::cout << "OS: " << tools::get_os_version_string() << ENDL; - exit = true; - } - - if (exit) - { - return true; - } - - int new_log_level = command_line::get_arg(vm, arg_log_level); - if(new_log_level < LOG_LEVEL_MIN || new_log_level > LOG_LEVEL_MAX) - { - LOG_PRINT_L0("Wrong log level value: "); - } - else if (log_space::get_set_log_detalisation_level(false) != new_log_level) - { - log_space::get_set_log_detalisation_level(true, new_log_level); - LOG_PRINT_L0("LOG_LEVEL set to " << new_log_level); - } - - return false; -} diff --git a/src/daemon/daemon_commands_handler.h b/src/daemon/daemon_commands_handler.h index 165cded21..6b6e46e83 100644 --- a/src/daemon/daemon_commands_handler.h +++ b/src/daemon/daemon_commands_handler.h @@ -28,6 +28,8 @@ // // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers + +/* This isn't a header file, may want to refactor this... */ #pragma once #include @@ -39,7 +41,11 @@ #include "crypto/hash.h" #include "version.h" - +/*! + * \brief I don't really know right now + * + * + */ class daemon_cmmands_handler { nodetool::node_server >& m_srv; From a70bf86037575ecb1bb2c8b1103deeb2fd1c0ae5 Mon Sep 17 00:00:00 2001 From: jebes Date: Thu, 11 Sep 2014 10:14:05 -0400 Subject: [PATCH 2/5] Documented varint --- src/common/base58.cpp | 2 +- src/common/util.cpp | 2 +- src/common/util.h | 4 +- src/common/varint.h | 116 ++++++++++++++++++++++++++++-------------- 4 files changed, 82 insertions(+), 42 deletions(-) diff --git a/src/common/base58.cpp b/src/common/base58.cpp index 9ea6ba1ba..b1b427ebf 100644 --- a/src/common/base58.cpp +++ b/src/common/base58.cpp @@ -99,7 +99,7 @@ namespace tools static decoded_block_sizes instance; private: - std::vector m_data; + std::vector m_data;p }; decoded_block_sizes decoded_block_sizes::instance; diff --git a/src/common/util.cpp b/src/common/util.cpp index d2ff90679..0ad1f0791 100644 --- a/src/common/util.cpp +++ b/src/common/util.cpp @@ -47,7 +47,7 @@ using namespace epee; namespace tools { - std::function signal_handler::m_handler; + std::function signal_handler::m_handler; #ifdef WIN32 std::string get_windows_version_display_string() diff --git a/src/common/util.h b/src/common/util.h index 876673d0a..a512eb0a3 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -136,8 +136,8 @@ namespace tools /*! \breif calles m_handler */ static void handle_signal() { - /* static std::mutex m_mutex; */ - /* std::unique_lock lock(m_mutex); */ + static std::mutex m_mutex; + std::unique_lock lock(m_mutex); m_handler(); } diff --git a/src/common/varint.h b/src/common/varint.h index dcb70d686..8d4b4cce8 100644 --- a/src/common/varint.h +++ b/src/common/varint.h @@ -35,54 +35,94 @@ #include #include #include +/*! \file varint.h + * \breif provides the implementation of varint's + * + * The representation of varints is rather odd. The first bit of each + * octet is significant, it represents wheter there is another part + * waiting to be read. For example 0x8002 would return 0x200, even + * though 0x02 does not have its msb set. The actual way they are read + * is as follows: Strip the msb of each byte, then from left to right, + * read in what remains, placing it in reverse, into the buffer. Thus, + * the following bit stream: 0xff02 would return 0x027f. 0xff turns + * into 0x7f, is placed on the beggining of the buffer, then 0x02 is + * unchanged, since its msb is not set, and placed at the end of the + * buffer. + */ namespace tools { - template - typename std::enable_if::value && std::is_unsigned::value, void>::type - write_varint(OutputIt &&dest, T i) { - while (i >= 0x80) { - *dest++ = (static_cast(i) & 0x7f) | 0x80; - i >>= 7; - } - *dest++ = static_cast(i); - } + /*! \breif Error codes for varint + */ + enum { + /* \breif Represents the overflow error */ + EVARINT_OVERFLOW = -1, + /* \breif Represents a non conical represnetation */ + EVARINT_REPRESENT = -2, + }; - template - std::string get_varint_data(const t_type& v) + /*! \breif writes a varint to a stream. + */ + template + /* Requires T to be both an integral type and unsigned, should be a compile error if it is not */ + typename std::enable_if::value && std::is_unsigned::value, void>::type + write_varint(OutputIt &&dest, T i) { + /* Make sure that there is one after this */ + while (i >= 0x80) { + *dest = (static_cast(i) & 0x7f) | 0x80; + ++dest; + i >>= 7; /* I should be in multiples of 7, this should just get the next part */ + } + /* writes the last one to dest */ + *dest = static_cast(i); + dest++; /* Seems kinda pointless... */ + } + + /*! \breif Returns the string that represents the varint + */ + template + std::string get_varint_data(const T& v) { std::stringstream ss; write_varint(std::ostreambuf_iterator(ss), v); return ss.str(); } - - template + /*! \breif reads in the varint as pointer to by InputIt into i + */ + template typename std::enable_if::value && std::is_unsigned::value && 0 <= bits && bits <= std::numeric_limits::digits, int>::type - read_varint(InputIt &&first, InputIt &&last, T &i) { - int read = 0; - i = 0; - for (int shift = 0;; shift += 7) { - if (first == last) { - return read; // End of input. - } - unsigned char byte = *first++; - ++read; - if (shift + 7 >= bits && byte >= 1 << (bits - shift)) { - return -1; // Overflow. - } - if (byte == 0 && shift != 0) { - return -2; // Non-canonical representation. - } - i |= static_cast(byte & 0x7f) << shift; - if ((byte & 0x80) == 0) { - break; - } - } - return read; - } + read_varint(InputIt &&first, InputIt &&last, T &write) { + int read = 0; + write = 0; + for (int shift = 0;; shift += 7) { + if (first == last) { + return read; + } + unsigned char byte = *first; + ++first; + ++read; + if (shift + 7 >= bits && byte >= 1 << (bits - shift)) { + return EVARINT_OVERFLOW; + } + if (byte == 0 && shift != 0) { + return EVARINT_REPRESENT; + } - template - int read_varint(InputIt &&first, InputIt &&last, T &i) { - return read_varint::digits, InputIt, T>(std::move(first), std::move(last), i); + write |= static_cast(byte & 0x7f) << shift; /* Does the actualy placing into write, stripping the first bit */ + + /* If there is no next */ + if ((byte & 0x80) == 0) { + break; + } } + return read; + } + + /*! \breif Wrapper around the other read_varint, + * Sets template parameters for you. + */ + template + int read_varint(InputIt &&first, InputIt &&last, T &i) { + return read_varint::digits, InputIt, T>(std::move(first), std::move(last), i); + } } From 9d6f9335d1c3d7f9324e905ddd8a0eecb438a32b Mon Sep 17 00:00:00 2001 From: jebes Date: Thu, 11 Sep 2014 10:20:17 -0400 Subject: [PATCH 3/5] HOW DO I GIT? --- src/common/base58.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/base58.cpp b/src/common/base58.cpp index b1b427ebf..9ea6ba1ba 100644 --- a/src/common/base58.cpp +++ b/src/common/base58.cpp @@ -99,7 +99,7 @@ namespace tools static decoded_block_sizes instance; private: - std::vector m_data;p + std::vector m_data; }; decoded_block_sizes decoded_block_sizes::instance; From f7900ccfc15dad1054b19eec72022d778e3c1b84 Mon Sep 17 00:00:00 2001 From: jebes Date: Thu, 11 Sep 2014 10:36:39 -0400 Subject: [PATCH 4/5] mispelled brief, corrected it --- src/common/util.h | 14 +++++++------- src/common/varint.h | 14 +++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/common/util.h b/src/common/util.h index a512eb0a3..51f0dad90 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -58,7 +58,7 @@ namespace tools */ std::string get_default_data_dir(); - /*! \breif Returns the OS version string + /*! \brief Returns the OS version string * * \details This is a wrapper around the primitives * get_windows_version_display_string() and @@ -66,7 +66,7 @@ namespace tools */ std::string get_os_version_string(); - /*! \breif creates directories for a path + /*! \brief creates directories for a path * * wrapper around boost::filesyste::create_directories. * (ensure-directory-exists): greenspun's tenth rule in action! @@ -84,7 +84,7 @@ namespace tools return crypto::cn_fast_hash(s.data(), s.size()); } - /*! \breif Defines a signal handler for win32 and *nix + /*! \brief Defines a signal handler for win32 and *nix */ class signal_handler { @@ -111,7 +111,7 @@ namespace tools private: #if defined(WIN32) - /*! \breif Handler for win */ + /*! \brief Handler for win */ static BOOL WINAPI win_handler(DWORD type) { if (CTRL_C_EVENT == type || CTRL_BREAK_EVENT == type) @@ -126,14 +126,14 @@ namespace tools return TRUE; } #else - /*! \breif handler for NIX */ + /*! \brief handler for NIX */ static void posix_handler(int /*type*/) { handle_signal(); } #endif - /*! \breif calles m_handler */ + /*! \brief calles m_handler */ static void handle_signal() { static std::mutex m_mutex; @@ -141,7 +141,7 @@ namespace tools m_handler(); } - /*! \breif where the installed handler is stored */ + /*! \brief where the installed handler is stored */ static std::function m_handler; }; } diff --git a/src/common/varint.h b/src/common/varint.h index 8d4b4cce8..0e5110d7a 100644 --- a/src/common/varint.h +++ b/src/common/varint.h @@ -52,16 +52,16 @@ namespace tools { - /*! \breif Error codes for varint + /*! \brief Error codes for varint */ enum { - /* \breif Represents the overflow error */ + /* \brief Represents the overflow error */ EVARINT_OVERFLOW = -1, - /* \breif Represents a non conical represnetation */ + /* \brief Represents a non conical represnetation */ EVARINT_REPRESENT = -2, }; - /*! \breif writes a varint to a stream. + /*! \brief writes a varint to a stream. */ template /* Requires T to be both an integral type and unsigned, should be a compile error if it is not */ @@ -78,7 +78,7 @@ namespace tools { dest++; /* Seems kinda pointless... */ } - /*! \breif Returns the string that represents the varint + /*! \brief Returns the string that represents the varint */ template std::string get_varint_data(const T& v) @@ -87,7 +87,7 @@ namespace tools { write_varint(std::ostreambuf_iterator(ss), v); return ss.str(); } - /*! \breif reads in the varint as pointer to by InputIt into i + /*! \brief reads in the varint as pointer to by InputIt into i */ template typename std::enable_if::value && std::is_unsigned::value && 0 <= bits && bits <= std::numeric_limits::digits, int>::type @@ -118,7 +118,7 @@ namespace tools { return read; } - /*! \breif Wrapper around the other read_varint, + /*! \brief Wrapper around the other read_varint, * Sets template parameters for you. */ template From a13e879251209268f291d47426a2d37a5e031ee7 Mon Sep 17 00:00:00 2001 From: jebes Date: Thu, 11 Sep 2014 10:45:05 -0400 Subject: [PATCH 5/5] HOW DO I ENGLISH? --- src/common/varint.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/varint.h b/src/common/varint.h index 0e5110d7a..5a73c2746 100644 --- a/src/common/varint.h +++ b/src/common/varint.h @@ -87,7 +87,7 @@ namespace tools { write_varint(std::ostreambuf_iterator(ss), v); return ss.str(); } - /*! \brief reads in the varint as pointer to by InputIt into i + /*! \brief reads in the varint that is pointed to by InputIt into write */ template typename std::enable_if::value && std::is_unsigned::value && 0 <= bits && bits <= std::numeric_limits::digits, int>::type