-14 Removals
+15 Additions
// Copyright (c) 2009-2010 Satoshi Nakamoto// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2012 The Bitcoin developers// Copyright (c) 2009-2012 The Bitcoin developers
// Copyright (c) 2011-2012 The Peercoin developers// Copyright (c) 2011-2012 The Peercoin developers
// Copyright (c) 2013-2014 The Peershares developers// Copyright (c) 2013-2014 The Peershares developers
// Copyright (c) 2015-2015 The Decent developers
// Distributed under the MIT/X11 software license, see the accompanying// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "util.h"#include "util.h"
#include "strlcpy.h"#include "strlcpy.h"
#include "version.h"#include "version.h"
#include "ui_interface.h"#include "ui_interface.h"
#include <boost/algorithm/string/join.hpp>#include <boost/algorithm/string/join.hpp>
// Work around clang compilation problem in Boost 1.46:// Work around clang compilation problem in Boost 1.46:
// /usr/include/boost/program_options/detail/config_file.hpp:163:17: error: call to function 'to_internal' that is neither visible in the template definition nor found by argument-dependent lookup// /usr/include/boost/program_options/detail/config_file.hpp:163:17: error: call to function 'to_internal' that is neither visible in the template definition nor found by argument-dependent lookup
// See also: http://stackoverflow.com/questions/10020179/compilation-fail-in-boost-librairies-program-options// See also: http://stackoverflow.com/questions/10020179/compilation-fail-in-boost-librairies-program-options
// http://clang.debian.net/status.php?version=3.0&key=CANNOT_FIND_FUNCTION// http://clang.debian.net/status.php?version=3.0&key=CANNOT_FIND_FUNCTION
namespace boost {namespace boost {
namespace program_options { namespace program_options {
std::string to_internal(const std::string&); std::string to_internal(const std::string&);
} }
}}
#include <boost/program_options/detail/config_file.hpp>#include <boost/program_options/detail/config_file.hpp>
#include <boost/program_options/parsers.hpp>#include <boost/program_options/parsers.hpp>
#include <boost/filesystem.hpp>#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>#include <boost/filesystem/fstream.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>#include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>
#include <boost/foreach.hpp>#include <boost/foreach.hpp>
#include <boost/thread.hpp>#include <boost/thread.hpp>
#include <openssl/crypto.h>#include <openssl/crypto.h>
#include <openssl/rand.h>#include <openssl/rand.h>
#ifdef WIN32#ifdef WIN32
#ifdef _MSC_VER#ifdef _MSC_VER
#pragma warning(disable:4786)#pragma warning(disable:4786)
#pragma warning(disable:4804)#pragma warning(disable:4804)
#pragma warning(disable:4805)#pragma warning(disable:4805)
#pragma warning(disable:4717)#pragma warning(disable:4717)
#endif#endif
#ifdef _WIN32_WINNT#ifdef _WIN32_WINNT
#undef _WIN32_WINNT#undef _WIN32_WINNT
#endif#endif
#define _WIN32_WINNT 0x0501#define _WIN32_WINNT 0x0501
#ifdef _WIN32_IE#ifdef _WIN32_IE
#undef _WIN32_IE#undef _WIN32_IE
#endif#endif
#define _WIN32_IE 0x0501#define _WIN32_IE 0x0501
#define WIN32_LEAN_AND_MEAN 1#define WIN32_LEAN_AND_MEAN 1
#ifndef NOMINMAX#ifndef NOMINMAX
#define NOMINMAX#define NOMINMAX
#endif#endif
#include "shlobj.h"#include "shlobj.h"
#include "shlwapi.h"#include "shlwapi.h"
#endif#endif
#ifndef WIN32#ifndef WIN32
#include <execinfo.h>#include <execinfo.h>
#endif#endif
using namespace std;using namespace std;
using namespace boost;using namespace boost;
map<string, string> mapArgs;map<string, string> mapArgs;
map<string, vector<string> > mapMultiArgs;map<string, vector<string> > mapMultiArgs;
map<string, string> mapPeercoinArgs;map<string, string> mapPeercoinArgs;
bool fDebug = false;bool fDebug = false;
bool fPrintToConsole = false;bool fPrintToConsole = false;
bool fPrintToDebugger = false;bool fPrintToDebugger = false;
bool fRequestShutdown = false;bool fRequestShutdown = false;
bool fShutdown = false;bool fShutdown = false;
bool fDaemon = false;bool fDaemon = false;
bool fServer = false;bool fServer = false;
bool fCommandLine = false;bool fCommandLine = false;
string strMiscWarning;string strMiscWarning;
bool fTestNet = false;bool fTestNet = false;
bool fNoListen = false;bool fNoListen = false;
bool fLogTimestamps = false;bool fLogTimestamps = false;
CMedianFilter<int64> vTimeOffsets(200,0);CMedianFilter<int64> vTimeOffsets(200,0);
// Init openssl library multithreading support// Init openssl library multithreading support
static boost::interprocess::interprocess_mutex** ppmutexOpenSSL;static boost::interprocess::interprocess_mutex** ppmutexOpenSSL;
void locking_callback(int mode, int i, const char* file, int line)void locking_callback(int mode, int i, const char* file, int line)
{{
if (mode & CRYPTO_LOCK) if (mode & CRYPTO_LOCK)
ppmutexOpenSSL[i]->lock(); ppmutexOpenSSL[i]->lock();
else else
ppmutexOpenSSL[i]->unlock(); ppmutexOpenSSL[i]->unlock();
}}
// Init// Init
class CInitclass CInit
{{
public:public:
CInit() CInit()
{ {
// Init openssl library multithreading support // Init openssl library multithreading support
ppmutexOpenSSL = (boost::interprocess::interprocess_mutex**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(boost::interprocess::interprocess_mutex*)); ppmutexOpenSSL = (boost::interprocess::interprocess_mutex**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(boost::interprocess::interprocess_mutex*));
for (int i = 0; i < CRYPTO_num_locks(); i++) for (int i = 0; i < CRYPTO_num_locks(); i++)
ppmutexOpenSSL[i] = new boost::interprocess::interprocess_mutex(); ppmutexOpenSSL[i] = new boost::interprocess::interprocess_mutex();
CRYPTO_set_locking_callback(locking_callback); CRYPTO_set_locking_callback(locking_callback);
#ifdef WIN32#ifdef WIN32
// Seed random number generator with screen scrape and other hardware sources // Seed random number generator with screen scrape and other hardware sources
RAND_screen(); RAND_screen();
#endif#endif
// Seed random number generator with performance counter // Seed random number generator with performance counter
RandAddSeed(); RandAddSeed();
} }
~CInit() ~CInit()
{ {
// Shutdown openssl library multithreading support // Shutdown openssl library multithreading support
CRYPTO_set_locking_callback(NULL); CRYPTO_set_locking_callback(NULL);
for (int i = 0; i < CRYPTO_num_locks(); i++) for (int i = 0; i < CRYPTO_num_locks(); i++)
delete ppmutexOpenSSL[i]; delete ppmutexOpenSSL[i];
OPENSSL_free(ppmutexOpenSSL); OPENSSL_free(ppmutexOpenSSL);
} }
}}
instance_of_cinit;instance_of_cinit;
void RandAddSeed()void RandAddSeed()
{{
// Seed with CPU performance counter // Seed with CPU performance counter
int64 nCounter = GetPerformanceCounter(); int64 nCounter = GetPerformanceCounter();
RAND_add(&nCounter, sizeof(nCounter), 1.5); RAND_add(&nCounter, sizeof(nCounter), 1.5);
memset(&nCounter, 0, sizeof(nCounter)); memset(&nCounter, 0, sizeof(nCounter));
}}
void RandAddSeedPerfmon()void RandAddSeedPerfmon()
{{
RandAddSeed(); RandAddSeed();
// This can take up to 2 seconds, so only do it every 10 minutes // This can take up to 2 seconds, so only do it every 10 minutes
static int64 nLastPerfmon; static int64 nLastPerfmon;
if (GetTime() < nLastPerfmon + 10 * 60) if (GetTime() < nLastPerfmon + 10 * 60)
return; return;
nLastPerfmon = GetTime(); nLastPerfmon = GetTime();
#ifdef WIN32#ifdef WIN32
// Don't need this on Linux, OpenSSL automatically uses /dev/urandom // Don't need this on Linux, OpenSSL automatically uses /dev/urandom
// Seed with the entire set of perfmon data // Seed with the entire set of perfmon data
unsigned char pdata[250000]; unsigned char pdata[250000];
memset(pdata, 0, sizeof(pdata)); memset(pdata, 0, sizeof(pdata));
unsigned long nSize = sizeof(pdata); unsigned long nSize = sizeof(pdata);
long ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, pdata, &nSize); long ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, pdata, &nSize);
RegCloseKey(HKEY_PERFORMANCE_DATA); RegCloseKey(HKEY_PERFORMANCE_DATA);
if (ret == ERROR_SUCCESS) if (ret == ERROR_SUCCESS)
{ {
RAND_add(pdata, nSize, nSize/100.0); RAND_add(pdata, nSize, nSize/100.0);
memset(pdata, 0, nSize); memset(pdata, 0, nSize);
printf("%s RandAddSeed() %d bytes\n", DateTimeStrFormat(GetTime()).c_str(), nSize); printf("%s RandAddSeed() %d bytes\n", DateTimeStrFormat(GetTime()).c_str(), nSize);
} }
#endif#endif
}}
uint64 GetRand(uint64 nMax)uint64 GetRand(uint64 nMax)
{{
if (nMax == 0) if (nMax == 0)
return 0; return 0;
// The range of the random source must be a multiple of the modulus // The range of the random source must be a multiple of the modulus
// to give every possible output value an equal possibility // to give every possible output value an equal possibility
uint64 nRange = (std::numeric_limits<uint64>::max() / nMax) * nMax; uint64 nRange = (std::numeric_limits<uint64>::max() / nMax) * nMax;
uint64 nRand = 0; uint64 nRand = 0;
do do
RAND_bytes((unsigned char*)&nRand, sizeof(nRand)); RAND_bytes((unsigned char*)&nRand, sizeof(nRand));
while (nRand >= nRange); while (nRand >= nRange);
return (nRand % nMax); return (nRand % nMax);
}}
int GetRandInt(int nMax)int GetRandInt(int nMax)
{{
return GetRand(nMax); return GetRand(nMax);
}}
uint256 GetRandHash()uint256 GetRandHash()
{{
uint256 hash; uint256 hash;
RAND_bytes((unsigned char*)&hash, sizeof(hash)); RAND_bytes((unsigned char*)&hash, sizeof(hash));
return hash; return hash;
}}
static FILE* fileout = NULL;static FILE* fileout = NULL;
inline int OutputDebugStringF(const char* pszFormat, ...)inline int OutputDebugStringF(const char* pszFormat, ...)
{{
int ret = 0; int ret = 0;
if (fPrintToConsole) if (fPrintToConsole)
{ {
// print to console // print to console
va_list arg_ptr; va_list arg_ptr;
va_start(arg_ptr, pszFormat); va_start(arg_ptr, pszFormat);
ret = vprintf(pszFormat, arg_ptr); ret = vprintf(pszFormat, arg_ptr);
va_end(arg_ptr); va_end(arg_ptr);
} }
else else
{ {
// print to debug.log // print to debug.log
if (!fileout) if (!fileout)
{ {
boost::filesystem::path pathDebug = GetDataDir() / "debug.log"; boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
fileout = fopen(pathDebug.string().c_str(), "a"); fileout = fopen(pathDebug.string().c_str(), "a");
if (fileout) setbuf(fileout, NULL); // unbuffered if (fileout) setbuf(fileout, NULL); // unbuffered
} }
if (fileout) if (fileout)
{ {
static bool fStartedNewLine = true; static bool fStartedNewLine = true;
static boost::mutex mutexDebugLog; static boost::mutex mutexDebugLog;
boost::mutex::scoped_lock scoped_lock(mutexDebugLog); boost::mutex::scoped_lock scoped_lock(mutexDebugLog);
// Debug print useful for profiling // Debug print useful for profiling
if (fLogTimestamps && fStartedNewLine) if (fLogTimestamps && fStartedNewLine)
fprintf(fileout, "%s ", DateTimeStrFormat(GetTime()).c_str()); fprintf(fileout, "%s ", DateTimeStrFormat(GetTime()).c_str());
if (pszFormat[strlen(pszFormat) - 1] == '\n') if (pszFormat[strlen(pszFormat) - 1] == '\n')
fStartedNewLine = true; fStartedNewLine = true;
else else
fStartedNewLine = false; fStartedNewLine = false;
va_list arg_ptr; va_list arg_ptr;
va_start(arg_ptr, pszFormat); va_start(arg_ptr, pszFormat);
ret = vfprintf(fileout, pszFormat, arg_ptr); ret = vfprintf(fileout, pszFormat, arg_ptr);
va_end(arg_ptr); va_end(arg_ptr);
} }
} }
#ifdef WIN32#ifdef WIN32
if (fPrintToDebugger) if (fPrintToDebugger)
{ {
static CCriticalSection cs_OutputDebugStringF; static CCriticalSection cs_OutputDebugStringF;
// accumulate a line at a time // accumulate a line at a time
{ {
LOCK(cs_OutputDebugStringF); LOCK(cs_OutputDebugStringF);
static char pszBuffer[50000]; static char pszBuffer[50000];
static char* pend; static char* pend;
if (pend == NULL) if (pend == NULL)
pend = pszBuffer; pend = pszBuffer;
va_list arg_ptr; va_list arg_ptr;
va_start(arg_ptr, pszFormat); va_start(arg_ptr, pszFormat);
int limit = END(pszBuffer) - pend - 2; int limit = END(pszBuffer) - pend - 2;
int ret = _vsnprintf(pend, limit, pszFormat, arg_ptr); int ret = _vsnprintf(pend, limit, pszFormat, arg_ptr);
va_end(arg_ptr); va_end(arg_ptr);
if (ret < 0 || ret >= limit) if (ret < 0 || ret >= limit)
{ {
pend = END(pszBuffer) - 2; pend = END(pszBuffer) - 2;
*pend++ = '\n'; *pend++ = '\n';
} }
else else
pend += ret; pend += ret;
*pend = '\0'; *pend = '\0';
char* p1 = pszBuffer; char* p1 = pszBuffer;
char* p2; char* p2;
while ((p2 = strchr(p1, '\n'))) while ((p2 = strchr(p1, '\n')))
{ {
p2++; p2++;
char c = *p2; char c = *p2;
*p2 = '\0'; *p2 = '\0';
OutputDebugStringA(p1); OutputDebugStringA(p1);
*p2 = c; *p2 = c;
p1 = p2; p1 = p2;
} }
if (p1 != pszBuffer) if (p1 != pszBuffer)
memmove(pszBuffer, p1, pend - p1 + 1); memmove(pszBuffer, p1, pend - p1 + 1);
pend -= (p1 - pszBuffer); pend -= (p1 - pszBuffer);
} }
} }
#endif#endif
return ret; return ret;
}}
// Safer snprintf// Safer snprintf
// - prints up to limit-1 characters// - prints up to limit-1 characters
// - output string is always null terminated even if limit reached// - output string is always null terminated even if limit reached
// - return value is the number of characters actually printed// - return value is the number of characters actually printed
int my_snprintf(char* buffer, size_t limit, const char* format, ...)int my_snprintf(char* buffer, size_t limit, const char* format, ...)
{{
if (limit == 0) if (limit == 0)
return 0; return 0;
va_list arg_ptr; va_list arg_ptr;
va_start(arg_ptr, format); va_start(arg_ptr, format);
int ret = _vsnprintf(buffer, limit, format, arg_ptr); int ret = _vsnprintf(buffer, limit, format, arg_ptr);
va_end(arg_ptr); va_end(arg_ptr);
if (ret < 0 || ret >= (int)limit) if (ret < 0 || ret >= (int)limit)
{ {
ret = limit - 1; ret = limit - 1;
buffer[limit-1] = 0; buffer[limit-1] = 0;
} }
return ret; return ret;
}}
string real_strprintf(const std::string &format, int dummy, ...)string real_strprintf(const std::string &format, int dummy, ...)
{{
char buffer[50000]; char buffer[50000];
char* p = buffer; char* p = buffer;
int limit = sizeof(buffer); int limit = sizeof(buffer);
int ret; int ret;
loop loop
{ {
va_list arg_ptr; va_list arg_ptr;
va_start(arg_ptr, dummy); va_start(arg_ptr, dummy);
ret = _vsnprintf(p, limit, format.c_str(), arg_ptr); ret = _vsnprintf(p, limit, format.c_str(), arg_ptr);
va_end(arg_ptr); va_end(arg_ptr);
if (ret >= 0 && ret < limit) if (ret >= 0 && ret < limit)
break; break;
if (p != buffer) if (p != buffer)
delete[] p; delete[] p;
limit *= 2; limit *= 2;
p = new char[limit]; p = new char[limit];
if (p == NULL) if (p == NULL)
throw std::bad_alloc(); throw std::bad_alloc();
} }
string str(p, p+ret); string str(p, p+ret);
if (p != buffer) if (p != buffer)
delete[] p; delete[] p;
return str; return str;
}}
bool error(const char *format, ...)bool error(const char *format, ...)
{{
char buffer[50000]; char buffer[50000];
int limit = sizeof(buffer); int limit = sizeof(buffer);
va_list arg_ptr; va_list arg_ptr;
va_start(arg_ptr, format); va_start(arg_ptr, format);
int ret = _vsnprintf(buffer, limit, format, arg_ptr); int ret = _vsnprintf(buffer, limit, format, arg_ptr);
va_end(arg_ptr); va_end(arg_ptr);
if (ret < 0 || ret >= limit) if (ret < 0 || ret >= limit)
{ {
buffer[limit-1] = 0; buffer[limit-1] = 0;
} }
printf("ERROR: %s\n", buffer); printf("ERROR: %s\n", buffer);
return false; return false;
}}
void ParseString(const string& str, char c, vector<string>& v)void ParseString(const string& str, char c, vector<string>& v)
{{
if (str.empty()) if (str.empty())
return; return;
string::size_type i1 = 0; string::size_type i1 = 0;
string::size_type i2; string::size_type i2;
loop loop
{ {
i2 = str.find(c, i1); i2 = str.find(c, i1);
if (i2 == str.npos) if (i2 == str.npos)
{ {
v.push_back(str.substr(i1)); v.push_back(str.substr(i1));
return; return;
} }
v.push_back(str.substr(i1, i2-i1)); v.push_back(str.substr(i1, i2-i1));
i1 = i2+1; i1 = i2+1;
} }
}}
string FormatMoney(int64 n, bool fPlus)string FormatMoney(int64 n, bool fPlus)
{{
// Note: not using straight sprintf here because we do NOT want // Note: not using straight sprintf here because we do NOT want
// localized number formatting. // localized number formatting.
int64 n_abs = (n > 0 ? n : -n); int64 n_abs = (n > 0 ? n : -n);
int64 quotient = n_abs/COIN; int64 quotient = n_abs/COIN;
int64 remainder = n_abs%COIN; int64 remainder = n_abs%COIN;
string str = strprintf("%"PRI64d".%06"PRI64d, quotient, remainder); string str = strprintf("%"PRI64d".%06"PRI64d, quotient, remainder);
// Right-trim excess 0's before the decimal point: // Right-trim excess 0's before the decimal point:
int nTrim = 0; int nTrim = 0;
for (int i = str.size()-1; (str[i] == '0' && isdigit(str[i-2])); --i) for (int i = str.size()-1; (str[i] == '0' && isdigit(str[i-2])); --i)
++nTrim; ++nTrim;
if (nTrim) if (nTrim)
str.erase(str.size()-nTrim, nTrim); str.erase(str.size()-nTrim, nTrim);
if (n < 0) if (n < 0)
str.insert((unsigned int)0, 1, '-'); str.insert((unsigned int)0, 1, '-');
else if (fPlus && n > 0) else if (fPlus && n > 0)
str.insert((unsigned int)0, 1, '+'); str.insert((unsigned int)0, 1, '+');
return str; return str;
}}
bool ParseMoney(const string& str, int64& nRet)bool ParseMoney(const string& str, int64& nRet)
{{
return ParseMoney(str.c_str(), nRet); return ParseMoney(str.c_str(), nRet);
}}
bool ParseMoney(const char* pszIn, int64& nRet)bool ParseMoney(const char* pszIn, int64& nRet)
{{
string strWhole; string strWhole;
int64 nUnits = 0; int64 nUnits = 0;
const char* p = pszIn; const char* p = pszIn;
while (isspace(*p)) while (isspace(*p))
p++; p++;
for (; *p; p++) for (; *p; p++)
{ {
if (*p == '.') if (*p == '.')
{ {
p++; p++;
int64 nMult = CENT*10; int64 nMult = CENT*10;
while (isdigit(*p) && (nMult > 0)) while (isdigit(*p) && (nMult > 0))
{ {
nUnits += nMult * (*p++ - '0'); nUnits += nMult * (*p++ - '0');
nMult /= 10; nMult /= 10;
} }
break; break;
} }
if (isspace(*p)) if (isspace(*p))
break; break;
if (!isdigit(*p)) if (!isdigit(*p))
return false; return false;
strWhole.insert(strWhole.end(), *p); strWhole.insert(strWhole.end(), *p);
} }
for (; *p; p++) for (; *p; p++)
if (!isspace(*p)) if (!isspace(*p))
return false; return false;
if (strWhole.size() > 10) // guard against 63 bit overflow if (strWhole.size() > 10) // guard against 63 bit overflow
return false; return false;
if (nUnits < 0 || nUnits > COIN) if (nUnits < 0 || nUnits > COIN)
return false; return false;
int64 nWhole = atoi64(strWhole); int64 nWhole = atoi64(strWhole);
int64 nValue = nWhole*COIN + nUnits; int64 nValue = nWhole*COIN + nUnits;
nRet = nValue; nRet = nValue;
return true; return true;
}}
static signed char phexdigit[256] =static signed char phexdigit[256] =
{ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,{ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1, 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
-1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, }; -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, };
bool IsHex(const string& str)bool IsHex(const string& str)
{{
BOOST_FOREACH(unsigned char c, str) BOOST_FOREACH(unsigned char c, str)
{ {
if (phexdigit[c] < 0) if (phexdigit[c] < 0)
return false; return false;
} }
return (str.size() > 0) && (str.size()%2 == 0); return (str.size() > 0) && (str.size()%2 == 0);
}}
vector<unsigned char> ParseHex(const char* psz)vector<unsigned char> ParseHex(const char* psz)
{{
// convert hex dump to vector // convert hex dump to vector
vector<unsigned char> vch; vector<unsigned char> vch;
loop loop
{ {
while (isspace(*psz)) while (isspace(*psz))
psz++; psz++;
signed char c = phexdigit[(unsigned char)*psz++]; signed char c = phexdigit[(unsigned char)*psz++];
if (c == (signed char)-1) if (c == (signed char)-1)
break; break;
unsigned char n = (c << 4); unsigned char n = (c << 4);
c = phexdigit[(unsigned char)*psz++]; c = phexdigit[(unsigned char)*psz++];
if (c == (signed char)-1) if (c == (signed char)-1)
break; break;
n |= c; n |= c;
vch.push_back(n); vch.push_back(n);
} }
return vch; return vch;
}}
vector<unsigned char> ParseHex(const string& str)vector<unsigned char> ParseHex(const string& str)
{{
return ParseHex(str.c_str()); return ParseHex(str.c_str());
}}
static void InterpretNegativeSetting(string name, map<string, string>& mapSettingsRet)static void InterpretNegativeSetting(string name, map<string, string>& mapSettingsRet)
{{
// interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
if (name.find("-no") == 0) if (name.find("-no") == 0)
{ {
std::string positive("-"); std::string positive("-");
positive.append(name.begin()+3, name.end()); positive.append(name.begin()+3, name.end());
if (mapSettingsRet.count(positive) == 0) if (mapSettingsRet.count(positive) == 0)
{ {
bool value = !GetBoolArg(name); bool value = !GetBoolArg(name);
mapSettingsRet[positive] = (value ? "1" : "0"); mapSettingsRet[positive] = (value ? "1" : "0");
} }
} }
}}
void ParseParameters(int argc, const char*const argv[])void ParseParameters(int argc, const char*const argv[])
{{
mapArgs.clear(); mapArgs.clear();
mapMultiArgs.clear(); mapMultiArgs.clear();
for (int i = 1; i < argc; i++) for (int i = 1; i < argc; i++)
{ {
char psz[10000]; char psz[10000];
strlcpy(psz, argv[i], sizeof(psz)); strlcpy(psz, argv[i], sizeof(psz));
char* pszValue = (char*)""; char* pszValue = (char*)"";
if (strchr(psz, '=')) if (strchr(psz, '='))
{ {
pszValue = strchr(psz, '='); pszValue = strchr(psz, '=');
*pszValue++ = '\0'; *pszValue++ = '\0';
} }
#ifdef WIN32 #ifdef WIN32
_strlwr(psz); _strlwr(psz);
if (psz[0] == '/') if (psz[0] == '/')
psz[0] = '-'; psz[0] = '-';
#endif #endif
if (psz[0] != '-') if (psz[0] != '-')
break; break;
mapArgs[psz] = pszValue; mapArgs[psz] = pszValue;
mapMultiArgs[psz].push_back(pszValue); mapMultiArgs[psz].push_back(pszValue);
} }
// New 0.6 features: // New 0.6 features:
BOOST_FOREACH(const PAIRTYPE(string,string)& entry, mapArgs) BOOST_FOREACH(const PAIRTYPE(string,string)& entry, mapArgs)
{ {
string name = entry.first; string name = entry.first;
// interpret --foo as -foo (as long as both are not set) // interpret --foo as -foo (as long as both are not set)
if (name.find("--") == 0) if (name.find("--") == 0)
{ {
std::string singleDash(name.begin()+1, name.end()); std::string singleDash(name.begin()+1, name.end());
if (mapArgs.count(singleDash) == 0) if (mapArgs.count(singleDash) == 0)
mapArgs[singleDash] = entry.second; mapArgs[singleDash] = entry.second;
name = singleDash; name = singleDash;
} }
// interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
InterpretNegativeSetting(name, mapArgs); InterpretNegativeSetting(name, mapArgs);
} }
}}
std::string GetArg(const std::string& strArg, const std::string& strDefault)std::string GetArg(const std::string& strArg, const std::string& strDefault)
{{
if (mapArgs.count(strArg)) if (mapArgs.count(strArg))
return mapArgs[strArg]; return mapArgs[strArg];
return strDefault; return strDefault;
}}
std::string GetPeercoinArg(const std::string& strArg, const std::string& strDefault)std::string GetPeercoinArg(const std::string& strArg, const std::string& strDefault)
{{
if (mapPeercoinArgs.count(strArg)) if (mapPeercoinArgs.count(strArg))
return mapPeercoinArgs[strArg]; return mapPeercoinArgs[strArg];
return strDefault; return strDefault;
}}
int64 GetArg(const std::string& strArg, int64 nDefault)int64 GetArg(const std::string& strArg, int64 nDefault)
{{
if (mapArgs.count(strArg)) if (mapArgs.count(strArg))
return atoi64(mapArgs[strArg]); return atoi64(mapArgs[strArg]);
return nDefault; return nDefault;
}}
int64 GetPeercoinArg(const std::string& strArg, int64 nDefault)int64 GetPeercoinArg(const std::string& strArg, int64 nDefault)
{{
if (mapPeercoinArgs.count(strArg)) if (mapPeercoinArgs.count(strArg))
return atoi64(mapPeercoinArgs[strArg]); return atoi64(mapPeercoinArgs[strArg]);
return nDefault; return nDefault;
}}
bool GetBoolArg(const std::string& strArg, bool fDefault)bool GetBoolArg(const std::string& strArg, bool fDefault)
{{
if (mapArgs.count(strArg)) if (mapArgs.count(strArg))
{ {
if (mapArgs[strArg].empty()) if (mapArgs[strArg].empty())
return true; return true;
return (atoi(mapArgs[strArg]) != 0); return (atoi(mapArgs[strArg]) != 0);
} }
return fDefault; return fDefault;
}}
bool GetPeercoinBoolArg(const std::string& strArg, bool fDefault)bool GetPeercoinBoolArg(const std::string& strArg, bool fDefault)
{{
if (mapPeercoinArgs.count(strArg)) if (mapPeercoinArgs.count(strArg))
{ {
if (mapPeercoinArgs[strArg].empty()) if (mapPeercoinArgs[strArg].empty())
return true; return true;
return (atoi(mapPeercoinArgs[strArg]) != 0); return (atoi(mapPeercoinArgs[strArg]) != 0);
} }
return fDefault; return fDefault;
}}
bool SoftSetArg(const std::string& strArg, const std::string& strValue)bool SoftSetArg(const std::string& strArg, const std::string& strValue)
{{
if (mapArgs.count(strArg)) if (mapArgs.count(strArg))
return false; return false;
mapArgs[strArg] = strValue; mapArgs[strArg] = strValue;
return true; return true;
}}
bool SoftSetBoolArg(const std::string& strArg, bool fValue)bool SoftSetBoolArg(const std::string& strArg, bool fValue)
{{
if (fValue) if (fValue)
return SoftSetArg(strArg, std::string("1")); return SoftSetArg(strArg, std::string("1"));
else else
return SoftSetArg(strArg, std::string("0")); return SoftSetArg(strArg, std::string("0"));
}}
string EncodeBase64(const unsigned char* pch, size_t len)string EncodeBase64(const unsigned char* pch, size_t len)
{{
static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
string strRet=""; string strRet="";
strRet.reserve((len+2)/3*4); strRet.reserve((len+2)/3*4);
int mode=0, left=0; int mode=0, left=0;
const unsigned char *pchEnd = pch+len; const unsigned char *pchEnd = pch+len;
while (pch<pchEnd) while (pch<pchEnd)
{ {
int enc = *(pch++); int enc = *(pch++);
switch (mode) switch (mode)
{ {
case 0: // we have no bits case 0: // we have no bits
strRet += pbase64[enc >> 2]; strRet += pbase64[enc >> 2];
left = (enc & 3) << 4; left = (enc & 3) << 4;
mode = 1; mode = 1;
break; break;
case 1: // we have two bits case 1: // we have two bits
strRet += pbase64[left | (enc >> 4)]; strRet += pbase64[left | (enc >> 4)];
left = (enc & 15) << 2; left = (enc & 15) << 2;
mode = 2; mode = 2;
break; break;
case 2: // we have four bits case 2: // we have four bits
strRet += pbase64[left | (enc >> 6)]; strRet += pbase64[left | (enc >> 6)];
strRet += pbase64[enc & 63]; strRet += pbase64[enc & 63];
mode = 0; mode = 0;
break; break;
} }
} }
if (mode) if (mode)
{ {
strRet += pbase64[left]; strRet += pbase64[left];
strRet += '='; strRet += '=';
if (mode == 1) if (mode == 1)
strRet += '='; strRet += '=';
} }
return strRet; return strRet;
}}
string EncodeBase64(const string& str)string EncodeBase64(const string& str)
{{
return EncodeBase64((const unsigned char*)str.c_str(), str.size()); return EncodeBase64((const unsigned char*)str.c_str(), str.size());
}}
vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid)vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid)
{{
static const int decode64_table[256] = static const int decode64_table[256] =
{ {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
-1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
}; };
if (pfInvalid) if (pfInvalid)
*pfInvalid = false; *pfInvalid = false;
vector<unsigned char> vchRet; vector<unsigned char> vchRet;
vchRet.reserve(strlen(p)*3/4); vchRet.reserve(strlen(p)*3/4);
int mode = 0; int mode = 0;
int left = 0; int left = 0;
while (1) while (1)
{ {
int dec = decode64_table[(unsigned char)*p]; int dec = decode64_table[(unsigned char)*p];
if (dec == -1) break; if (dec == -1) break;
p++; p++;
switch (mode) switch (mode)
{ {
case 0: // we have no bits and get 6 case 0: // we have no bits and get 6
left = dec; left = dec;
mode = 1; mode = 1;
break; break;
case 1: // we have 6 bits and keep 4 case 1: // we have 6 bits and keep 4
vchRet.push_back((left<<2) | (dec>>4)); vchRet.push_back((left<<2) | (dec>>4));
left = dec & 15; left = dec & 15;
mode = 2; mode = 2;
break; break;
case 2: // we have 4 bits and get 6, we keep 2 case 2: // we have 4 bits and get 6, we keep 2
vchRet.push_back((left<<4) | (dec>>2)); vchRet.push_back((left<<4) | (dec>>2));
left = dec & 3; left = dec & 3;
mode = 3; mode = 3;
break; break;
case 3: // we have 2 bits and get 6 case 3: // we have 2 bits and get 6
vchRet.push_back((left<<6) | dec); vchRet.push_back((left<<6) | dec);
mode = 0; mode = 0;
break; break;
} }
} }
if (pfInvalid) if (pfInvalid)
switch (mode) switch (mode)
{ {
case 0: // 4n base64 characters processed: ok case 0: // 4n base64 characters processed: ok
break; break;
case 1: // 4n+1 base64 character processed: impossible case 1: // 4n+1 base64 character processed: impossible
*pfInvalid = true; *pfInvalid = true;
break; break;
case 2: // 4n+2 base64 characters processed: require '==' case 2: // 4n+2 base64 characters processed: require '=='
if (left || p[0] != '=' || p[1] != '=' || decode64_table[(unsigned char)p[2]] != -1) if (left || p[0] != '=' || p[1] != '=' || decode64_table[(unsigned char)p[2]] != -1)
*pfInvalid = true; *pfInvalid = true;
break; break;
case 3: // 4n+3 base64 characters processed: require '=' case 3: // 4n+3 base64 characters processed: require '='
if (left || p[0] != '=' || decode64_table[(unsigned char)p[1]] != -1) if (left || p[0] != '=' || decode64_table[(unsigned char)p[1]] != -1)
*pfInvalid = true; *pfInvalid = true;
break; break;
} }
return vchRet; return vchRet;
}}
string DecodeBase64(const string& str)string DecodeBase64(const string& str)
{{
vector<unsigned char> vchRet = DecodeBase64(str.c_str()); vector<unsigned char> vchRet = DecodeBase64(str.c_str());
return string((const char*)&vchRet[0], vchRet.size()); return string((const char*)&vchRet[0], vchRet.size());
}}
bool WildcardMatch(const char* psz, const char* mask)bool WildcardMatch(const char* psz, const char* mask)
{{
loop loop
{ {
switch (*mask) switch (*mask)
{ {
case '\0': case '\0':
return (*psz == '\0'); return (*psz == '\0');
case '*': case '*':
return WildcardMatch(psz, mask+1) || (*psz && WildcardMatch(psz+1, mask)); return WildcardMatch(psz, mask+1) || (*psz && WildcardMatch(psz+1, mask));
case '?': case '?':
if (*psz == '\0') if (*psz == '\0')
return false; return false;
break; break;
default: default:
if (*psz != *mask) if (*psz != *mask)
return false; return false;
break; break;
} }
psz++; psz++;
mask++; mask++;
} }
}}
bool WildcardMatch(const string& str, const string& mask)bool WildcardMatch(const string& str, const string& mask)
{{
return WildcardMatch(str.c_str(), mask.c_str()); return WildcardMatch(str.c_str(), mask.c_str());
}}
void FormatException(char* pszMessage, std::exception* pex, const char* pszThread)void FormatException(char* pszMessage, std::exception* pex, const char* pszThread)
{{
#ifdef WIN32#ifdef WIN32
char pszModule[MAX_PATH]; char pszModule[MAX_PATH];
pszModule[0] = '\0'; pszModule[0] = '\0';
GetModuleFileNameA(NULL, pszModule, sizeof(pszModule)); GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
#else#else
const char* pszModule = "peershares"; const char* pszModule = "decent";
#endif#endif
if (pex) if (pex)
snprintf(pszMessage, 1000, snprintf(pszMessage, 1000,
"EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread); "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
else else
snprintf(pszMessage, 1000, snprintf(pszMessage, 1000,
"UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread); "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
}}
void LogException(std::exception* pex, const char* pszThread)void LogException(std::exception* pex, const char* pszThread)
{{
char pszMessage[10000]; char pszMessage[10000];
FormatException(pszMessage, pex, pszThread); FormatException(pszMessage, pex, pszThread);
printf("\n%s", pszMessage); printf("\n%s", pszMessage);
}}
void PrintException(std::exception* pex, const char* pszThread)void PrintException(std::exception* pex, const char* pszThread)
{{
char pszMessage[10000]; char pszMessage[10000];
FormatException(pszMessage, pex, pszThread); FormatException(pszMessage, pex, pszThread);
printf("\n\n************************\n%s\n", pszMessage); printf("\n\n************************\n%s\n", pszMessage);
fprintf(stderr, "\n\n************************\n%s\n", pszMessage); fprintf(stderr, "\n\n************************\n%s\n", pszMessage);
strMiscWarning = pszMessage; strMiscWarning = pszMessage;
throw; throw;
}}
void LogStackTrace() {void LogStackTrace() {
printf("\n\n******* exception encountered *******\n"); printf("\n\n******* exception encountered *******\n");
if (fileout) if (fileout)
{ {
#ifndef WIN32#ifndef WIN32
void* pszBuffer[32]; void* pszBuffer[32];
size_t size; size_t size;
size = backtrace(pszBuffer, 32); size = backtrace(pszBuffer, 32);
backtrace_symbols_fd(pszBuffer, size, fileno(fileout)); backtrace_symbols_fd(pszBuffer, size, fileno(fileout));
#endif#endif
} }
}}
void PrintExceptionContinue(std::exception* pex, const char* pszThread)void PrintExceptionContinue(std::exception* pex, const char* pszThread)
{{
char pszMessage[10000]; char pszMessage[10000];
FormatException(pszMessage, pex, pszThread); FormatException(pszMessage, pex, pszThread);
printf("\n\n************************\n%s\n", pszMessage); printf("\n\n************************\n%s\n", pszMessage);
fprintf(stderr, "\n\n************************\n%s\n", pszMessage); fprintf(stderr, "\n\n************************\n%s\n", pszMessage);
strMiscWarning = pszMessage; strMiscWarning = pszMessage;
}}
#ifdef WIN32#ifdef WIN32
boost::filesystem::path MyGetSpecialFolderPath(int nFolder, bool fCreate)boost::filesystem::path MyGetSpecialFolderPath(int nFolder, bool fCreate)
{{
namespace fs = boost::filesystem; namespace fs = boost::filesystem;
char pszPath[MAX_PATH] = ""; char pszPath[MAX_PATH] = "";
if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate)) if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate))
{ {
return fs::path(pszPath); return fs::path(pszPath);
} }
else if (nFolder == CSIDL_STARTUP) else if (nFolder == CSIDL_STARTUP)
{ {
return fs::path(getenv("USERPROFILE")) / "Start Menu" / "Programs" / "Startup"; return fs::path(getenv("USERPROFILE")) / "Start Menu" / "Programs" / "Startup";
} }
else if (nFolder == CSIDL_APPDATA) else if (nFolder == CSIDL_APPDATA)
{ {
return fs::path(getenv("APPDATA")); return fs::path(getenv("APPDATA"));
} }
return fs::path(""); return fs::path("");
}}
#endif#endif
boost::filesystem::path GetDefaultDataDir()boost::filesystem::path GetDefaultDataDir()
{{
namespace fs = boost::filesystem; namespace fs = boost::filesystem;
// Windows: C:\Documents and Settings\username\Application Data\PPCoin // Windows: C:\Documents and Settings\username\Application Data\Decent
// Mac: ~/Library/Application Support/PPCoin // Mac: ~/Library/Application Support/Decent
// Unix: ~/.peershares // Unix: ~/.decent
#ifdef WIN32#ifdef WIN32
// Windows // Windows
return MyGetSpecialFolderPath(CSIDL_APPDATA, true) / "Peershares"; return MyGetSpecialFolderPath(CSIDL_APPDATA, true) / "Decent";
#else#else
fs::path pathRet; fs::path pathRet;
char* pszHome = getenv("HOME"); char* pszHome = getenv("HOME");
if (pszHome == NULL || strlen(pszHome) == 0) if (pszHome == NULL || strlen(pszHome) == 0)
pathRet = fs::path("/"); pathRet = fs::path("/");
else else
pathRet = fs::path(pszHome); pathRet = fs::path(pszHome);
#ifdef MAC_OSX#ifdef MAC_OSX
// Mac // Mac
pathRet /= "Library/Application Support"; pathRet /= "Library/Application Support";
fs::create_directory(pathRet); fs::create_directory(pathRet);
return pathRet / "Peershares"; return pathRet / "Decent";
#else#else
// Unix // Unix
return pathRet / ".peershares"; return pathRet / ".decent";
#endif#endif
#endif#endif
}}
boost::filesystem::path GetDefaultPeercoinDataDir()boost::filesystem::path GetDefaultPeercoinDataDir()
{{
namespace fs = boost::filesystem; namespace fs = boost::filesystem;
// Windows: C:\Documents and Settings\username\Application Data\PPCoin // Windows: C:\Documents and Settings\username\Application Data\PPCoin
// Mac: ~/Library/Application Support/PPCoin // Mac: ~/Library/Application Support/PPCoin
// Unix: ~/.peershares // Unix: ~/.peershares
#ifdef WIN32#ifdef WIN32
// Windows // Windows
return MyGetSpecialFolderPath(CSIDL_APPDATA, true) / "PPCoin"; return MyGetSpecialFolderPath(CSIDL_APPDATA, true) / "PPCoin";
#else#else
fs::path pathRet; fs::path pathRet;
char* pszHome = getenv("HOME"); char* pszHome = getenv("HOME");
if (pszHome == NULL || strlen(pszHome) == 0) if (pszHome == NULL || strlen(pszHome) == 0)
pathRet = fs::path("/"); pathRet = fs::path("/");
else else
pathRet = fs::path(pszHome); pathRet = fs::path(pszHome);
#ifdef MAC_OSX#ifdef MAC_OSX
// Mac // Mac
pathRet /= "Library/Application Support"; pathRet /= "Library/Application Support";
fs::create_directory(pathRet); fs::create_directory(pathRet);
return pathRet / "PPCoin"; return pathRet / "PPCoin";
#else#else
// Unix // Unix
return pathRet / ".ppcoin"; return pathRet / ".ppcoin";
#endif#endif
#endif#endif
}}
const boost::filesystem::path &GetDataDir(bool fNetSpecific)const boost::filesystem::path &GetDataDir(bool fNetSpecific)
{{
namespace fs = boost::filesystem; namespace fs = boost::filesystem;
static fs::path pathCached[2]; static fs::path pathCached[2];
static CCriticalSection csPathCached; static CCriticalSection csPathCached;
static bool cachedPath[2] = {false, false}; static bool cachedPath[2] = {false, false};
fs::path &path = pathCached[fNetSpecific]; fs::path &path = pathCached[fNetSpecific];
// This can be called during exceptions by printf, so we cache the // This can be called during exceptions by printf, so we cache the
// value so we don't have to do memory allocations after that. // value so we don't have to do memory allocations after that.
if (cachedPath[fNetSpecific]) if (cachedPath[fNetSpecific])
return path; return path;
LOCK(csPathCached); LOCK(csPathCached);
if (mapArgs.count("-datadir")) { if (mapArgs.count("-datadir")) {
path = fs::system_complete(mapArgs["-datadir"]); path = fs::system_complete(mapArgs["-datadir"]);
if (!fs::is_directory(path)) { if (!fs::is_directory(path)) {
path = ""; path = "";
return path; return path;
} }
} else { } else {
path = GetDefaultDataDir(); path = GetDefaultDataDir();
} }
if (fNetSpecific && GetBoolArg("-testnet", false)) if (fNetSpecific && GetBoolArg("-testnet", false))
path /= "testnet"; path /= "testnet";
fs::create_directory(path); fs::create_directory(path);
cachedPath[fNetSpecific]=true; cachedPath[fNetSpecific]=true;
return path; return path;
}}
const boost::filesystem::path &GetPeercoinDataDir(bool fNetSpecific)const boost::filesystem::path &GetPeercoinDataDir(bool fNetSpecific)
{{
namespace fs = boost::filesystem; namespace fs = boost::filesystem;
static fs::path pathCached[2]; static fs::path pathCached[2];
static CCriticalSection csPathCached; static CCriticalSection csPathCached;
static bool cachedPath[2] = {false, false}; static bool cachedPath[2] = {false, false};
fs::path &path = pathCached[fNetSpecific]; fs::path &path = pathCached[fNetSpecific];
// This can be called during exceptions by printf, so we cache the // This can be called during exceptions by printf, so we cache the
// value so we don't have to do memory allocations after that. // value so we don't have to do memory allocations after that.
if (cachedPath[fNetSpecific]) if (cachedPath[fNetSpecific])
return path; return path;
LOCK(csPathCached); LOCK(csPathCached);
if (mapArgs.count("-peercoindatadir")) { if (mapArgs.count("-peercoindatadir")) {
path = fs::system_complete(mapArgs["-peercoindatadir"]); path = fs::system_complete(mapArgs["-peercoindatadir"]);
if (!fs::is_directory(path)) { if (!fs::is_directory(path)) {
path = ""; path = "";
return path; return path;
} }
} else { } else {
path = GetDefaultPeercoinDataDir(); path = GetDefaultPeercoinDataDir();
} }
if (fNetSpecific && GetBoolArg("-testnet", false)) if (fNetSpecific && GetBoolArg("-testnet", false))
path /= "testnet"; path /= "testnet";
cachedPath[fNetSpecific]=true; cachedPath[fNetSpecific]=true;
return path; return path;
}}
boost::filesystem::path GetConfigFile()boost::filesystem::path GetConfigFile()
{{
namespace fs = boost::filesystem; namespace fs = boost::filesystem;
fs::path pathConfigFile(GetArg("-conf", "peershares.conf")); fs::path pathConfigFile(GetArg("-conf", "decent.conf"));
if (!pathConfigFile.is_complete()) pathConfigFile = GetDataDir(false) / pathConfigFile; if (!pathConfigFile.is_complete()) pathConfigFile = GetDataDir(false) / pathConfigFile;
return pathConfigFile; return pathConfigFile;
}}
boost::filesystem::path GetPeercoinConfigFile()boost::filesystem::path GetPeercoinConfigFile()
{{
namespace fs = boost::filesystem; namespace fs = boost::filesystem;
fs::path pathConfigFile(GetArg("-peercoinconf", "ppcoin.conf")); fs::path pathConfigFile(GetArg("-peercoinconf", "ppcoin.conf"));
if (!pathConfigFile.is_complete()) pathConfigFile = GetPeercoinDataDir(false) / pathConfigFile; if (!pathConfigFile.is_complete()) pathConfigFile = GetPeercoinDataDir(false) / pathConfigFile;
return pathConfigFile; return pathConfigFile;
}}
void ReadConfigFile(map<string, string>& mapSettingsRet,void ReadConfigFile(map<string, string>& mapSettingsRet,
map<string, vector<string> >& mapMultiSettingsRet) map<string, vector<string> >& mapMultiSettingsRet)
{{
namespace fs = boost::filesystem; namespace fs = boost::filesystem;
namespace pod = boost::program_options::detail; namespace pod = boost::program_options::detail;
fs::ifstream streamConfig(GetConfigFile()); fs::ifstream streamConfig(GetConfigFile());
if (!streamConfig.good()) if (!streamConfig.good())
return; // No bitcoin.conf file is OK return; // No bitcoin.conf file is OK
set<string> setOptions; set<string> setOptions;
setOptions.insert("*"); setOptions.insert("*");
for (pod::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it) for (pod::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
{ {
// Don't overwrite existing settings so command line settings override bitcoin.conf // Don't overwrite existing settings so command line settings override bitcoin.conf
string strKey = string("-") + it->string_key; string strKey = string("-") + it->string_key;
if (mapSettingsRet.count(strKey) == 0) if (mapSettingsRet.count(strKey) == 0)
{ {
mapSettingsRet[strKey] = it->value[0]; mapSettingsRet[strKey] = it->value[0];
// interpret nofoo=1 as foo=0 (and nofoo=0 as foo=1) as long as foo not set) // interpret nofoo=1 as foo=0 (and nofoo=0 as foo=1) as long as foo not set)
InterpretNegativeSetting(strKey, mapSettingsRet); InterpretNegativeSetting(strKey, mapSettingsRet);
} }
mapMultiSettingsRet[strKey].push_back(it->value[0]); mapMultiSettingsRet[strKey].push_back(it->value[0]);
} }
}}
void ReadPeercoinConfigFile(map<string, string>& mapSettingsRet)void ReadPeercoinConfigFile(map<string, string>& mapSettingsRet)
{{
namespace fs = boost::filesystem; namespace fs = boost::filesystem;
namespace pod = boost::program_options::detail; namespace pod = boost::program_options::detail;
fs::ifstream streamConfig(GetPeercoinConfigFile()); fs::ifstream streamConfig(GetPeercoinConfigFile());
if (!streamConfig.good()) if (!streamConfig.good())
return; // No Peercoin config file is OK return; // No Peercoin config file is OK
set<string> setOptions; set<string> setOptions;
setOptions.insert("*"); setOptions.insert("*");
for (pod::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it) for (pod::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
{ {
// Don't overwrite existing settings so command line settings override bitcoin.conf // Don't overwrite existing settings so command line settings override bitcoin.conf
string strKey = string("-") + it->string_key; string strKey = string("-") + it->string_key;
if (mapSettingsRet.count(strKey) == 0) if (mapSettingsRet.count(strKey) == 0)
{ {
mapSettingsRet[strKey] = it->value[0]; mapSettingsRet[strKey] = it->value[0];
// interpret nofoo=1 as foo=0 (and nofoo=0 as foo=1) as long as foo not set) // interpret nofoo=1 as foo=0 (and nofoo=0 as foo=1) as long as foo not set)
InterpretNegativeSetting(strKey, mapSettingsRet); InterpretNegativeSetting(strKey, mapSettingsRet);
} }
} }
}}
boost::filesystem::path GetPidFile()boost::filesystem::path GetPidFile()
{{
namespace fs = boost::filesystem; namespace fs = boost::filesystem;
fs::path pathPidFile(GetArg("-pid", "peershares.pid")); fs::path pathPidFile(GetArg("-pid", "decent.pid"));
if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile; if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
return pathPidFile; return pathPidFile;
}}
void CreatePidFile(const boost::filesystem::path &path, pid_t pid)void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
{{
FILE* file = fopen(path.string().c_str(), "w"); FILE* file = fopen(path.string().c_str(), "w");
if (file) if (file)
{ {
fprintf(file, "%d\n", pid); fprintf(file, "%d\n", pid);
fclose(file); fclose(file);
} }
}}
int GetFilesize(FILE* file)int GetFilesize(FILE* file)
{{
int nSavePos = ftell(file); int nSavePos = ftell(file);
int nFilesize = -1; int nFilesize = -1;
if (fseek(file, 0, SEEK_END) == 0) if (fseek(file, 0, SEEK_END) == 0)
nFilesize = ftell(file); nFilesize = ftell(file);
fseek(file, nSavePos, SEEK_SET); fseek(file, nSavePos, SEEK_SET);
return nFilesize; return nFilesize;
}}
void ShrinkDebugFile()void ShrinkDebugFile()
{{
// Scroll debug.log if it's getting too big // Scroll debug.log if it's getting too big
boost::filesystem::path pathLog = GetDataDir() / "debug.log"; boost::filesystem::path pathLog = GetDataDir() / "debug.log";
FILE* file = fopen(pathLog.string().c_str(), "r"); FILE* file = fopen(pathLog.string().c_str(), "r");
if (file && GetFilesize(file) > 10 * 1000000) if (file && GetFilesize(file) > 10 * 1000000)
{ {
// Restart the file with some of the end // Restart the file with some of the end
char pch[200000]; char pch[200000];
fseek(file, -sizeof(pch), SEEK_END); fseek(file, -sizeof(pch), SEEK_END);
int nBytes = fread(pch, 1, sizeof(pch), file); int nBytes = fread(pch, 1, sizeof(pch), file);
fclose(file); fclose(file);
file = fopen(pathLog.string().c_str(), "w"); file = fopen(pathLog.string().c_str(), "w");
if (file) if (file)
{ {
fwrite(pch, 1, nBytes, file); fwrite(pch, 1, nBytes, file);
fclose(file); fclose(file);
} }
} }
}}
////
// "Never go to sea with two chronometers; take one or three."// "Never go to sea with two chronometers; take one or three."
// Our three time sources are:// Our three time sources are:
// - System clock// - System clock
// - Median of other nodes's clocks// - Median of other nodes's clocks
// - The user (asking the user to fix the system clock if the first two disagree)// - The user (asking the user to fix the system clock if the first two disagree)
////
static int64 nMockTime = 0; // For unit testingstatic int64 nMockTime = 0; // For unit testing
int64 GetTime()int64 GetTime()
{{
if (nMockTime) return nMockTime; if (nMockTime) return nMockTime;
return time(NULL); return time(NULL);
}}
void SetMockTime(int64 nMockTimeIn)void SetMockTime(int64 nMockTimeIn)
{{
nMockTime = nMockTimeIn; nMockTime = nMockTimeIn;
}}
static int64 nTimeOffset = 0;static int64 nTimeOffset = 0;
int64 GetAdjustedTime()int64 GetAdjustedTime()
{{
return GetTime() + nTimeOffset; return GetTime() + nTimeOffset;
}}
void AddTimeData(const CNetAddr& ip, int64 nTime)void AddTimeData(const CNetAddr& ip, int64 nTime)
{{
int64 nOffsetSample = nTime - GetTime(); int64 nOffsetSample = nTime - GetTime();
// Ignore duplicates // Ignore duplicates
static set<CNetAddr> setKnown; static set<CNetAddr> setKnown;
if (!setKnown.insert(ip).second) if (!setKnown.insert(ip).second)
return; return;
// Add data // Add data
vTimeOffsets.input(nOffsetSample); vTimeOffsets.input(nOffsetSample);
printf("Added time data, samples %d, offset %+"PRI64d" (%+"PRI64d" minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample/60); printf("Added time data, samples %d, offset %+"PRI64d" (%+"PRI64d" minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample/60);
if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1) if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1)
{ {
int64 nMedian = vTimeOffsets.median(); int64 nMedian = vTimeOffsets.median();
std::vector<int64> vSorted = vTimeOffsets.sorted(); std::vector<int64> vSorted = vTimeOffsets.sorted();
// Only let other nodes change our time by so much // Only let other nodes change our time by so much
if (abs64(nMedian) < 70 * 60) if (abs64(nMedian) < 70 * 60)
{ {
nTimeOffset = nMedian; nTimeOffset = nMedian;
} }
else else
{ {
nTimeOffset = 0; nTimeOffset = 0;
static bool fDone; static bool fDone;
if (!fDone) if (!fDone)
{ {
// If nobody has a time different than ours but within 5 minutes of ours, give a warning // If nobody has a time different than ours but within 5 minutes of ours, give a warning
bool fMatch = false; bool fMatch = false;
BOOST_FOREACH(int64 nOffset, vSorted) BOOST_FOREACH(int64 nOffset, vSorted)
if (nOffset != 0 && abs64(nOffset) < 5 * 60) if (nOffset != 0 && abs64(nOffset) < 5 * 60)
fMatch = true; fMatch = true;
if (!fMatch) if (!fMatch)
{ {
fDone = true; fDone = true;
string strMessage = _("Warning: Please check that your computer's date and time are correct. If your clock is wrong Peershares will not work properly."); string strMessage = _("Warning: Please check that your computer's date and time are correct. If your clock is wrong Decent will not work properly.");
strMiscWarning = strMessage; strMiscWarning = strMessage;
printf("*** %s\n", strMessage.c_str()); printf("*** %s\n", strMessage.c_str());
ThreadSafeMessageBox(strMessage+" ", string("Peershares"), wxOK | wxICON_EXCLAMATION); ThreadSafeMessageBox(strMessage+" ", string("Decent"), wxOK | wxICON_EXCLAMATION);
} }
} }
} }
if (fDebug) { if (fDebug) {
BOOST_FOREACH(int64 n, vSorted) BOOST_FOREACH(int64 n, vSorted)
printf("%+"PRI64d" ", n); printf("%+"PRI64d" ", n);
printf("| "); printf("| ");
} }
printf("nTimeOffset = %+"PRI64d" (%+"PRI64d" minutes)\n", nTimeOffset, nTimeOffset/60); printf("nTimeOffset = %+"PRI64d" (%+"PRI64d" minutes)\n", nTimeOffset, nTimeOffset/60);
} }
}}
string FormatVersion(int nVersion)string FormatVersion(int nVersion)
{{
if (nVersion%100 == 0) if (nVersion%100 == 0)
return strprintf("%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100); return strprintf("%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100);
else else
return strprintf("%d.%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100, nVersion%100); return strprintf("%d.%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100, nVersion%100);
}}
string FormatFullVersion()string FormatFullVersion()
{{
return CLIENT_BUILD; return CLIENT_BUILD;
}}
// Format the subversion field according to BIP 14 spec (https://en.bitcoin.it/wiki/BIP_0014)// Format the subversion field according to BIP 14 spec (https://en.bitcoin.it/wiki/BIP_0014)
std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector<std::string>& comments)std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector<std::string>& comments)
{{
std::ostringstream ss; std::ostringstream ss;
ss << "/"; ss << "/";
ss << name << ":" << FormatVersion(nClientVersion); ss << name << ":" << FormatVersion(nClientVersion);
if (!comments.empty()) if (!comments.empty())
ss << "(" << boost::algorithm::join(comments, "; ") << ")"; ss << "(" << boost::algorithm::join(comments, "; ") << ")";
ss << "/"; ss << "/";
ss << "Peercoin:" << FormatVersion(PEERSHARES_VERSION); ss << "Peercoin:" << FormatVersion(PEERSHARES_VERSION);
ss << "(" << CLIENT_BUILD << ")/"; ss << "(" << CLIENT_BUILD << ")/";
return ss.str(); return ss.str();
}}
#ifdef WIN32#ifdef WIN32
boost::filesystem::path static StartupShortcutPath()boost::filesystem::path static StartupShortcutPath()
{{
return MyGetSpecialFolderPath(CSIDL_STARTUP, true) / "PPCoin.lnk"; return MyGetSpecialFolderPath(CSIDL_STARTUP, true) / "PPCoin.lnk";
}}
bool GetStartOnSystemStartup()bool GetStartOnSystemStartup()
{{
return filesystem::exists(StartupShortcutPath()); return filesystem::exists(StartupShortcutPath());
}}
bool SetStartOnSystemStartup(bool fAutoStart)bool SetStartOnSystemStartup(bool fAutoStart)
{{
// If the shortcut exists already, remove it for updating // If the shortcut exists already, remove it for updating
boost::filesystem::remove(StartupShortcutPath()); boost::filesystem::remove(StartupShortcutPath());
if (fAutoStart) if (fAutoStart)
{ {
CoInitialize(NULL); CoInitialize(NULL);
// Get a pointer to the IShellLink interface. // Get a pointer to the IShellLink interface.
IShellLink* psl = NULL; IShellLink* psl = NULL;
HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER, IID_IShellLink, CLSCTX_INPROC_SERVER, IID_IShellLink,
reinterpret_cast<void**>(&psl)); reinterpret_cast<void**>(&psl));
if (SUCCEEDED(hres)) if (SUCCEEDED(hres))
{ {
// Get the current executable path // Get the current executable path
TCHAR pszExePath[MAX_PATH]; TCHAR pszExePath[MAX_PATH];
GetModuleFileName(NULL, pszExePath, sizeof(pszExePath)); GetModuleFileName(NULL, pszExePath, sizeof(pszExePath));
TCHAR pszArgs[5] = TEXT("-min"); TCHAR pszArgs[5] = TEXT("-min");
// Set the path to the shortcut target // Set the path to the shortcut target
psl->SetPath(pszExePath); psl->SetPath(pszExePath);
PathRemoveFileSpec(pszExePath); PathRemoveFileSpec(pszExePath);
psl->SetWorkingDirectory(pszExePath); psl->SetWorkingDirectory(pszExePath);
psl->SetShowCmd(SW_SHOWMINNOACTIVE); psl->SetShowCmd(SW_SHOWMINNOACTIVE);
psl->SetArguments(pszArgs); psl->SetArguments(pszArgs);
// Query IShellLink for the IPersistFile interface for // Query IShellLink for the IPersistFile interface for
// saving the shortcut in persistent storage. // saving the shortcut in persistent storage.
IPersistFile* ppf = NULL; IPersistFile* ppf = NULL;
hres = psl->QueryInterface(IID_IPersistFile, hres = psl->QueryInterface(IID_IPersistFile,
reinterpret_cast<void**>(&ppf)); reinterpret_cast<void**>(&ppf));
if (SUCCEEDED(hres)) if (SUCCEEDED(hres))
{ {
WCHAR pwsz[MAX_PATH]; WCHAR pwsz[MAX_PATH];
// Ensure that the string is ANSI. // Ensure that the string is ANSI.
MultiByteToWideChar(CP_ACP, 0, StartupShortcutPath().string().c_str(), -1, pwsz, MAX_PATH); MultiByteToWideChar(CP_ACP, 0, StartupShortcutPath().string().c_str(), -1, pwsz, MAX_PATH);
// Save the link by calling IPersistFile::Save. // Save the link by calling IPersistFile::Save.
hres = ppf->Save(pwsz, TRUE); hres = ppf->Save(pwsz, TRUE);
ppf->Release(); ppf->Release();
psl->Release(); psl->Release();
CoUninitialize(); CoUninitialize();
return true; return true;
} }
psl->Release(); psl->Release();
} }
CoUninitialize(); CoUninitialize();
return false; return false;
} }
return true; return true;
}}
#elif defined(LINUX)#elif defined(LINUX)
// Follow the Desktop Application Autostart Spec:// Follow the Desktop Application Autostart Spec:
// http://standards.freedesktop.org/autostart-spec/autostart-spec-latest.html// http://standards.freedesktop.org/autostart-spec/autostart-spec-latest.html
boost::filesystem::path static GetAutostartDir()boost::filesystem::path static GetAutostartDir()
{{
namespace fs = boost::filesystem; namespace fs = boost::filesystem;
char* pszConfigHome = getenv("XDG_CONFIG_HOME"); char* pszConfigHome = getenv("XDG_CONFIG_HOME");
if (pszConfigHome) return fs::path(pszConfigHome) / "autostart"; if (pszConfigHome) return fs::path(pszConfigHome) / "autostart";
char* pszHome = getenv("HOME"); char* pszHome = getenv("HOME");
if (pszHome) return fs::path(pszHome) / ".config" / "autostart"; if (pszHome) return fs::path(pszHome) / ".config" / "autostart";
return fs::path(); return fs::path();
}}
boost::filesystem::path static GetAutostartFilePath()boost::filesystem::path static GetAutostartFilePath()
{{
return GetAutostartDir() / "peershares.desktop"; return GetAutostartDir() / "decent.desktop";
}}
bool GetStartOnSystemStartup()bool GetStartOnSystemStartup()
{{
boost::filesystem::ifstream optionFile(GetAutostartFilePath()); boost::filesystem::ifstream optionFile(GetAutostartFilePath());
if (!optionFile.good()) if (!optionFile.good())
return false; return false;
// Scan through file for "Hidden=true": // Scan through file for "Hidden=true":
string line; string line;
while (!optionFile.eof()) while (!optionFile.eof())
{ {
getline(optionFile, line); getline(optionFile, line);
if (line.find("Hidden") != string::npos && if (line.find("Hidden") != string::npos &&
line.find("true") != string::npos) line.find("true") != string::npos)
return false; return false;
} }
optionFile.close(); optionFile.close();
return true; return true;
}}
bool SetStartOnSystemStartup(bool fAutoStart)bool SetStartOnSystemStartup(bool fAutoStart)
{{
if (!fAutoStart) if (!fAutoStart)
boost::filesystem::remove(GetAutostartFilePath()); boost::filesystem::remove(GetAutostartFilePath());
else else
{ {
char pszExePath[MAX_PATH+1]; char pszExePath[MAX_PATH+1];
memset(pszExePath, 0, sizeof(pszExePath)); memset(pszExePath, 0, sizeof(pszExePath));
if (readlink("/proc/self/exe", pszExePath, sizeof(pszExePath)-1) == -1) if (readlink("/proc/self/exe", pszExePath, sizeof(pszExePath)-1) == -1)
return false; return false;
boost::filesystem::create_directories(GetAutostartDir()); boost::filesystem::create_directories(GetAutostartDir());
boost::filesystem::ofstream optionFile(GetAutostartFilePath(), ios_base::out|ios_base::trunc); boost::filesystem::ofstream optionFile(GetAutostartFilePath(), ios_base::out|ios_base::trunc);
if (!optionFile.good()) if (!optionFile.good())
return false; return false;
// Write a bitcoin.desktop file to the autostart directory: // Write a bitcoin.desktop file to the autostart directory:
optionFile << "[Desktop Entry]\n"; optionFile << "[Desktop Entry]\n";
optionFile << "Type=Application\n"; optionFile << "Type=Application\n";
optionFile << "Name=PPCoin\n"; optionFile << "Name=PPCoin\n";
optionFile << "Exec=" << pszExePath << " -min\n"; optionFile << "Exec=" << pszExePath << " -min\n";
optionFile << "Terminal=false\n"; optionFile << "Terminal=false\n";
optionFile << "Hidden=false\n"; optionFile << "Hidden=false\n";
optionFile.close(); optionFile.close();
} }
return true; return true;
}}
#else#else
// TODO: OSX startup stuff; see:// TODO: OSX startup stuff; see:
// http://developer.apple.com/mac/library/documentation/MacOSX/Conceptual/BPSystemStartup/Articles/CustomLogin.html// http://developer.apple.com/mac/library/documentation/MacOSX/Conceptual/BPSystemStartup/Articles/CustomLogin.html
bool GetStartOnSystemStartup() { return false; }bool GetStartOnSystemStartup() { return false; }
bool SetStartOnSystemStartup(bool fAutoStart) { return false; }bool SetStartOnSystemStartup(bool fAutoStart) { return false; }
#endif#endif
#ifdef DEBUG_LOCKORDER#ifdef DEBUG_LOCKORDER
////
// Early deadlock detection.// Early deadlock detection.
// Problem being solved:// Problem being solved:
// Thread 1 locks A, then B, then C// Thread 1 locks A, then B, then C
// Thread 2 locks D, then C, then A// Thread 2 locks D, then C, then A
// --> may result in deadlock between the two threads, depending on when they run.// --> may result in deadlock between the two threads, depending on when they run.
// Solution implemented here:// Solution implemented here:
// Keep track of pairs of locks: (A before B), (A before C), etc.// Keep track of pairs of locks: (A before B), (A before C), etc.
// Complain if any thread trys to lock in a different order.// Complain if any thread trys to lock in a different order.
////
struct CLockLocationstruct CLockLocation
{{
CLockLocation(const char* pszName, const char* pszFile, int nLine) CLockLocation(const char* pszName, const char* pszFile, int nLine)
{ {
mutexName = pszName; mutexName = pszName;
sourceFile = pszFile; sourceFile = pszFile;
sourceLine = nLine; sourceLine = nLine;
} }
std::string ToString() const std::string ToString() const
{ {
return mutexName+" "+sourceFile+":"+itostr(sourceLine); return mutexName+" "+sourceFile+":"+itostr(sourceLine);
} }
private:private:
std::string mutexName; std::string mutexName;
std::string sourceFile; std::string sourceFile;
int sourceLine; int sourceLine;
};};
typedef std::vector< std::pair<void*, CLockLocation> > LockStack;typedef std::vector< std::pair<void*, CLockLocation> > LockStack;
static boost::interprocess::interprocess_mutex dd_mutex;static boost::interprocess::interprocess_mutex dd_mutex;
static std::map<std::pair<void*, void*>, LockStack> lockorders;static std::map<std::pair<void*, void*>, LockStack> lockorders;
static boost::thread_specific_ptr<LockStack> lockstack;static boost::thread_specific_ptr<LockStack> lockstack;
static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch, const LockStack& s1, const LockStack& s2)static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch, const LockStack& s1, const LockStack& s2)
{{
printf("POTENTIAL DEADLOCK DETECTED\n"); printf("POTENTIAL DEADLOCK DETECTED\n");
printf("Previous lock order was:\n"); printf("Previous lock order was:\n");
BOOST_FOREACH(const PAIRTYPE(void*, CLockLocation)& i, s2) BOOST_FOREACH(const PAIRTYPE(void*, CLockLocation)& i, s2)
{ {
if (i.first == mismatch.first) printf(" (1)"); if (i.first == mismatch.first) printf(" (1)");
if (i.first == mismatch.second) printf(" (2)"); if (i.first == mismatch.second) printf(" (2)");
printf(" %s\n", i.second.ToString().c_str()); printf(" %s\n", i.second.ToString().c_str());
} }
printf("Current lock order is:\n"); printf("Current lock order is:\n");
BOOST_FOREACH(const PAIRTYPE(void*, CLockLocation)& i, s1) BOOST_FOREACH(const PAIRTYPE(void*, CLockLocation)& i, s1)
{ {
if (i.first == mismatch.first) printf(" (1)"); if (i.first == mismatch.first) printf(" (1)");
if (i.first == mismatch.second) printf(" (2)"); if (i.first == mismatch.second) printf(" (2)");
printf(" %s\n", i.second.ToString().c_str()); printf(" %s\n", i.second.ToString().c_str());
} }
}}
static void push_lock(void* c, const CLockLocation& locklocation, bool fTry)static void push_lock(void* c, const CLockLocation& locklocation, bool fTry)
{{
bool fOrderOK = true; bool fOrderOK = true;
if (lockstack.get() == NULL) if (lockstack.get() == NULL)
lockstack.reset(new LockStack); lockstack.reset(new LockStack);
if (fDebug) printf("Locking: %s\n", locklocation.ToString().c_str()); if (fDebug) printf("Locking: %s\n", locklocation.ToString().c_str());
dd_mutex.lock(); dd_mutex.lock();
(*lockstack).push_back(std::make_pair(c, locklocation)); (*lockstack).push_back(std::make_pair(c, locklocation));
if (!fTry) BOOST_FOREACH(const PAIRTYPE(void*, CLockLocation)& i, (*lockstack)) if (!fTry) BOOST_FOREACH(const PAIRTYPE(void*, CLockLocation)& i, (*lockstack))
{ {
if (i.first == c) break; if (i.first == c) break;
std::pair<void*, void*> p1 = std::make_pair(i.first, c); std::pair<void*, void*> p1 = std::make_pair(i.first, c);
if (lockorders.count(p1)) if (lockorders.count(p1))
continue; continue;
lockorders[p1] = (*lockstack); lockorders[p1] = (*lockstack);
std::pair<void*, void*> p2 = std::make_pair(c, i.first); std::pair<void*, void*> p2 = std::make_pair(c, i.first);
if (lockorders.count(p2)) if (lockorders.count(p2))
{ {
potential_deadlock_detected(p1, lockorders[p2], lockorders[p1]); potential_deadlock_detected(p1, lockorders[p2], lockorders[p1]);
break; break;
} }
} }
dd_mutex.unlock(); dd_mutex.unlock();
}}
static void pop_lock()static void pop_lock()
{{
if (fDebug) if (fDebug)
{ {
const CLockLocation& locklocation = (*lockstack).rbegin()->second; const CLockLocation& locklocation = (*lockstack).rbegin()->second;
printf("Unlocked: %s\n", locklocation.ToString().c_str()); printf("Unlocked: %s\n", locklocation.ToString().c_str());
} }
dd_mutex.lock(); dd_mutex.lock();
(*lockstack).pop_back(); (*lockstack).pop_back();
dd_mutex.unlock(); dd_mutex.unlock();
}}
void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry)void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry)
{{
push_lock(cs, CLockLocation(pszName, pszFile, nLine), fTry); push_lock(cs, CLockLocation(pszName, pszFile, nLine), fTry);
}}
void LeaveCritical()void LeaveCritical()
{{
pop_lock(); pop_lock();
}}
#endif /* DEBUG_LOCKORDER */#endif /* DEBUG_LOCKORDER */
Editor
Original Text
Changed Text