aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorAntonio Juarez <antonio.maria.juarez@live.com>2014-03-20 11:46:11 +0000
committerAntonio Juarez <antonio.maria.juarez@live.com>2014-03-20 11:46:11 +0000
commit8efa1313f3614f34ac0bac947314bb53e9a2412b (patch)
tree2752f8e6dfbb75bc53d56ea422482a8ec5870ffa /src/common
parentmoved all stuff to github (diff)
downloadmonero-8efa1313f3614f34ac0bac947314bb53e9a2412b.tar.xz
some fixes
Diffstat (limited to 'src/common')
-rw-r--r--src/common/base58.cpp1
-rw-r--r--src/common/int-util.h10
-rw-r--r--src/common/util.cpp1
-rw-r--r--src/common/util.h57
4 files changed, 64 insertions, 5 deletions
diff --git a/src/common/base58.cpp b/src/common/base58.cpp
index 30042eeba..454c0db67 100644
--- a/src/common/base58.cpp
+++ b/src/common/base58.cpp
@@ -227,6 +227,7 @@ namespace tools
std::string addr_data;
bool r = decode(addr, addr_data);
if (!r) return false;
+ if (addr_data.size() <= addr_checksum_size) return false;
std::string checksum(addr_checksum_size, '\0');
checksum = addr_data.substr(addr_data.size() - addr_checksum_size);
diff --git a/src/common/int-util.h b/src/common/int-util.h
index ad0ef60e0..db9e9bea7 100644
--- a/src/common/int-util.h
+++ b/src/common/int-util.h
@@ -34,15 +34,15 @@ static inline uint64_t rol64(uint64_t x, int r) {
#endif
-inline uint64_t hi_dword(uint64_t val) {
+static inline uint64_t hi_dword(uint64_t val) {
return val >> 32;
}
-inline uint64_t lo_dword(uint64_t val) {
+static inline uint64_t lo_dword(uint64_t val) {
return val & 0xFFFFFFFF;
}
-inline uint64_t mul128(uint64_t multiplier, uint64_t multiplicand, uint64_t* product_hi) {
+static inline uint64_t mul128(uint64_t multiplier, uint64_t multiplicand, uint64_t* product_hi) {
// multiplier = ab = a * 2^32 + b
// multiplicand = cd = c * 2^32 + d
// ab * cd = a * c * 2^64 + (a * d + b * c) * 2^32 + b * d
@@ -68,14 +68,14 @@ inline uint64_t mul128(uint64_t multiplier, uint64_t multiplicand, uint64_t* pro
return product_lo;
}
-inline uint64_t div_with_reminder(uint64_t dividend, uint32_t divisor, uint32_t* remainder) {
+static inline uint64_t div_with_reminder(uint64_t dividend, uint32_t divisor, uint32_t* remainder) {
dividend |= ((uint64_t)*remainder) << 32;
*remainder = dividend % divisor;
return dividend / divisor;
}
// Long division with 2^32 base
-inline uint32_t div128_32(uint64_t dividend_hi, uint64_t dividend_lo, uint32_t divisor, uint64_t* quotient_hi, uint64_t* quotient_lo) {
+static inline uint32_t div128_32(uint64_t dividend_hi, uint64_t dividend_lo, uint32_t divisor, uint64_t* quotient_hi, uint64_t* quotient_lo) {
uint64_t dividend_dwords[4];
uint32_t remainder = 0;
diff --git a/src/common/util.cpp b/src/common/util.cpp
index b24016cc3..c9c470851 100644
--- a/src/common/util.cpp
+++ b/src/common/util.cpp
@@ -21,6 +21,7 @@ using namespace epee;
namespace tools
{
+ std::function<void(void)> 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 a29a30fff..af92adf94 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -4,6 +4,7 @@
#pragma once
+#include <mutex>
#include <system_error>
#include <boost/filesystem.hpp>
@@ -26,4 +27,60 @@ namespace tools
s.append(reinterpret_cast<const char*>(&pot.time), sizeof(pot.time));
return crypto::cn_fast_hash(s.data(), s.size());
}
+
+
+ class signal_handler
+ {
+ public:
+ template<typename T>
+ static bool install(T t)
+ {
+#if defined(WIN32)
+ bool r = TRUE == ::SetConsoleCtrlHandler(&win_handler, TRUE);
+ if (r)
+ {
+ m_handler = t;
+ }
+ return r;
+#else
+ signal(SIGINT, posix_handler);
+ signal(SIGTERM, posix_handler);
+ m_handler = t;
+ return true;
+#endif
+ }
+
+ private:
+#if defined(WIN32)
+ static BOOL win_handler(DWORD type)
+ {
+ if (CTRL_C_EVENT == type || CTRL_BREAK_EVENT == type)
+ {
+ handle_signal();
+ return TRUE;
+ }
+ else
+ {
+ LOG_PRINT_RED_L0("Got control signal " << type << ". Exiting without saving...");
+ return FALSE;
+ }
+ return TRUE;
+ }
+#else
+ static void posix_handler(int /*type*/)
+ {
+ handle_signal();
+ }
+#endif
+
+ static void handle_signal()
+ {
+ static std::mutex m_mutex;
+ std::unique_lock<std::mutex> lock(m_mutex);
+ m_handler();
+ }
+
+ private:
+ static std::function<void(void)> m_handler;
+ };
}