1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
9 #include "ui_interface.h"
11 #include <boost/algorithm/string/join.hpp>
12 #include <boost/algorithm/string/case_conv.hpp> // for to_lower()
13 #include <boost/program_options/detail/config_file.hpp>
14 #include <boost/program_options/parsers.hpp>
15 #include <boost/filesystem.hpp>
16 #include <boost/filesystem/fstream.hpp>
17 #include <boost/date_time/posix_time/posix_time.hpp>
18 #include <boost/thread.hpp>
20 #include <openssl/crypto.h>
21 #include <openssl/rand.h>
27 #define _WIN32_WINNT 0x0501
31 #define _WIN32_IE 0x0501
32 #define WIN32_LEAN_AND_MEAN 1
36 #include <io.h> /* for _commit */
38 #elif defined(__linux__)
39 #include <sys/prctl.h>
42 #if !defined(WIN32) && !defined(ANDROID)
47 namespace bt = boost::posix_time;
49 map<string, string> mapArgs;
50 map<string, vector<string> > mapMultiArgs;
52 bool fDebugNet = false;
53 bool fPrintToConsole = false;
54 bool fPrintToDebugger = false;
55 bool fRequestShutdown = false;
56 bool fShutdown = false;
59 bool fCommandLine = false;
60 string strMiscWarning;
61 bool fTestNet = false;
62 bool fNoListen = false;
63 bool fLogTimestamps = false;
64 CMedianFilter<int64_t> vTimeOffsets(200,0);
65 bool fReopenDebugLog = false;
67 // Extended DecodeDumpTime implementation, see this page for details:
68 // http://stackoverflow.com/questions/3786201/parsing-of-date-time-from-string-boost
69 const locale formats[] = {
70 locale(locale::classic(),new bt::time_input_facet("%Y-%m-%dT%H:%M:%SZ")),
71 locale(locale::classic(),new bt::time_input_facet("%Y-%m-%d %H:%M:%S")),
72 locale(locale::classic(),new bt::time_input_facet("%Y/%m/%d %H:%M:%S")),
73 locale(locale::classic(),new bt::time_input_facet("%d.%m.%Y %H:%M:%S")),
74 locale(locale::classic(),new bt::time_input_facet("%Y-%m-%d"))
77 const size_t formats_n = sizeof(formats)/sizeof(formats[0]);
79 time_t pt_to_time_t(const bt::ptime& pt)
81 bt::ptime timet_start(boost::gregorian::date(1970,1,1));
82 bt::time_duration diff = pt - timet_start;
83 return diff.ticks()/bt::time_duration::rep_type::ticks_per_second;
86 // Init OpenSSL library multithreading support
87 static CCriticalSection** ppmutexOpenSSL;
88 void locking_callback(int mode, int i, const char* file, int line)
90 if (mode & CRYPTO_LOCK) {
91 ENTER_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
93 LEAVE_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
97 LockedPageManager LockedPageManager::instance;
105 // Init OpenSSL library multithreading support
106 ppmutexOpenSSL = (CCriticalSection**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(CCriticalSection*));
107 for (int i = 0; i < CRYPTO_num_locks(); i++)
108 ppmutexOpenSSL[i] = new CCriticalSection();
109 CRYPTO_set_locking_callback(locking_callback);
112 // Seed random number generator with screen scrape and other hardware sources
116 // Seed random number generator with performance counter
121 // Shutdown OpenSSL library multithreading support
122 CRYPTO_set_locking_callback(NULL);
123 for (int i = 0; i < CRYPTO_num_locks(); i++)
124 delete ppmutexOpenSSL[i];
125 OPENSSL_free(ppmutexOpenSSL);
132 // Seed with CPU performance counter
133 int64_t nCounter = GetPerformanceCounter();
134 RAND_add(&nCounter, sizeof(nCounter), 1.5);
135 memset(&nCounter, 0, sizeof(nCounter));
138 void RandAddSeedPerfmon()
142 // This can take up to 2 seconds, so only do it every 10 minutes
143 static int64_t nLastPerfmon;
144 if (GetTime() < nLastPerfmon + 10 * 60)
146 nLastPerfmon = GetTime();
149 // Don't need this on Linux, OpenSSL automatically uses /dev/urandom
150 // Seed with the entire set of perfmon data
151 unsigned char pdata[250000];
152 memset(pdata, 0, sizeof(pdata));
153 unsigned long nSize = sizeof(pdata);
154 long ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, pdata, &nSize);
155 RegCloseKey(HKEY_PERFORMANCE_DATA);
156 if (ret == ERROR_SUCCESS)
158 RAND_add(pdata, nSize, nSize/100.0);
159 OPENSSL_cleanse(pdata, nSize);
160 printf("RandAddSeed() %lu bytes\n", nSize);
165 uint64_t GetRand(uint64_t nMax)
170 // The range of the random source must be a multiple of the modulus
171 // to give every possible output value an equal possibility
172 uint64_t nRange = (numeric_limits<uint64_t>::max() / nMax) * nMax;
175 RAND_bytes((unsigned char*)&nRand, sizeof(nRand));
176 while (nRand >= nRange);
177 return (nRand % nMax);
180 int GetRandInt(int nMax)
182 return static_cast<int>(GetRand(nMax));
185 uint256 GetRandHash()
188 RAND_bytes((unsigned char*)&hash, sizeof(hash));
192 static FILE* fileout = NULL;
194 inline int OutputDebugStringF(const char* pszFormat, ...)
201 va_start(arg_ptr, pszFormat);
202 ret = vprintf(pszFormat, arg_ptr);
205 else if (!fPrintToDebugger)
207 // print to debug.log
211 boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
212 fileout = fopen(pathDebug.string().c_str(), "a");
213 if (fileout) setbuf(fileout, NULL); // unbuffered
217 static bool fStartedNewLine = true;
219 // This routine may be called by global destructors during shutdown.
220 // Since the order of destruction of static/global objects is undefined,
221 // allocate mutexDebugLog on the heap the first time this routine
222 // is called to avoid crashes during shutdown.
223 static boost::mutex* mutexDebugLog = NULL;
224 if (mutexDebugLog == NULL) mutexDebugLog = new boost::mutex();
225 boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
227 // reopen the log file, if requested
228 if (fReopenDebugLog) {
229 fReopenDebugLog = false;
230 boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
231 if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
232 setbuf(fileout, NULL); // unbuffered
235 // Debug print useful for profiling
236 if (fLogTimestamps && fStartedNewLine)
237 fprintf(fileout, "%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
238 if (pszFormat[strlen(pszFormat) - 1] == '\n')
239 fStartedNewLine = true;
241 fStartedNewLine = false;
244 va_start(arg_ptr, pszFormat);
245 ret = vfprintf(fileout, pszFormat, arg_ptr);
251 if (fPrintToDebugger)
253 static CCriticalSection cs_OutputDebugStringF;
255 // accumulate and output a line at a time
257 LOCK(cs_OutputDebugStringF);
258 static string buffer;
261 va_start(arg_ptr, pszFormat);
262 buffer += vstrprintf(pszFormat, arg_ptr);
265 size_t line_start = 0, line_end;
266 while((line_end = buffer.find('\n', line_start)) != string::npos)
268 OutputDebugStringA(buffer.substr(line_start, line_end - line_start).c_str());
269 line_start = line_end + 1;
271 buffer.erase(0, line_start);
278 string vstrprintf(const char *format, va_list ap)
282 int limit = sizeof(buffer);
288 va_copy(arg_ptr, ap);
290 va_list arg_ptr = ap;
293 ret = _vsnprintf(p, limit, format, arg_ptr);
295 ret = vsnprintf(p, limit, format, arg_ptr);
298 if (ret >= 0 && ret < limit)
303 p = new(nothrow) char[limit];
307 string str(p, p+ret);
313 string real_strprintf(const char *format, int dummy, ...)
316 va_start(arg_ptr, dummy);
317 string str = vstrprintf(format, arg_ptr);
322 string real_strprintf(const string &format, int dummy, ...)
325 va_start(arg_ptr, dummy);
326 string str = vstrprintf(format.c_str(), arg_ptr);
331 bool error(const char *format, ...)
334 va_start(arg_ptr, format);
335 string str = vstrprintf(format, arg_ptr);
337 printf("ERROR: %s\n", str.c_str());
342 void ParseString(const string& str, char c, vector<string>& v)
346 string::size_type i1 = 0;
347 string::size_type i2;
350 i2 = str.find(c, i1);
353 v.push_back(str.substr(i1));
356 v.push_back(str.substr(i1, i2-i1));
361 string FormatMoney(int64_t n, bool fPlus)
363 // Note: not using straight sprintf here because we do NOT want
364 // localized number formatting.
365 int64_t n_abs = (n > 0 ? n : -n);
366 int64_t quotient = n_abs/COIN;
367 int64_t remainder = n_abs%COIN;
368 string str = strprintf("%" PRId64 ".%06" PRId64, quotient, remainder);
370 // Right-trim excess zeros before the decimal point:
372 for (size_t i = str.size()-1; (str[i] == '0' && isdigit(str[i-2])); --i)
375 str.erase(str.size()-nTrim, nTrim);
378 str.insert(0u, 1, '-');
379 else if (fPlus && n > 0)
380 str.insert(0u, 1, '+');
384 bool ParseMoney(const string& str, int64_t& nRet)
386 return ParseMoney(str.c_str(), nRet);
389 bool ParseMoney(const char* pszIn, int64_t& nRet)
393 const char* p = pszIn;
401 int64_t nMult = CENT*10;
402 while (isdigit(*p) && (nMult > 0))
404 nUnits += nMult * (*p++ - '0');
413 strWhole.insert(strWhole.end(), *p);
418 if (strWhole.size() > 10) // guard against 63 bit overflow
420 if (nUnits < 0 || nUnits > COIN)
422 int64_t nWhole = strtoll(strWhole);
423 int64_t nValue = nWhole*COIN + nUnits;
429 static const int8_t phexdigit[256] =
431 -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
432 -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
433 -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
434 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
435 -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
436 -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
437 -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
438 -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
439 -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
440 -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
441 -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
442 -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
443 -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
444 -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
445 -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
446 -1, -1, -1, -1, -1, -1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
449 bool IsHex(const string& str)
451 for(unsigned char c : str)
453 if (phexdigit[c] < 0)
456 return (str.size() > 0) && (str.size()%2 == 0);
459 vector<unsigned char> ParseHex(const char* psz)
461 // convert hex dump to vector
462 vector<unsigned char> vch;
465 while (isspace(*psz))
467 auto c = phexdigit[(unsigned char)*psz++];
468 if (c == (signed char)-1)
471 c = phexdigit[(uint8_t)*psz++];
480 static void InterpretNegativeSetting(string name, map<string, string>& mapSettingsRet)
482 // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
483 if (name.find("-no") == 0)
485 string positive("-");
486 positive.append(name.begin()+3, name.end());
487 if (mapSettingsRet.count(positive) == 0)
489 bool value = !GetBoolArg(name);
490 mapSettingsRet[positive] = (value ? "1" : "0");
495 void ParseParameters(int argc, const char* const argv[])
498 mapMultiArgs.clear();
499 for (int i = 1; i < argc; i++)
503 size_t is_index = str.find('=');
504 if (is_index != string::npos)
506 strValue = str.substr(is_index+1);
507 str = str.substr(0, is_index);
510 boost::to_lower(str);
511 if (str.compare(0,1, "/") == 0)
512 str = "-" + str.substr(1);
517 mapArgs[str] = strValue;
518 mapMultiArgs[str].push_back(strValue);
522 for(const auto& entry : mapArgs)
524 string name = entry.first;
526 // interpret --foo as -foo (as long as both are not set)
527 if (name.find("--") == 0)
529 string singleDash(name.begin()+1, name.end());
530 if (mapArgs.count(singleDash) == 0)
531 mapArgs[singleDash] = entry.second;
535 // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
536 InterpretNegativeSetting(name, mapArgs);
540 string GetArg(const string& strArg, const string& strDefault)
542 if (mapArgs.count(strArg))
543 return mapArgs[strArg];
547 int64_t GetArg(const string& strArg, int64_t nDefault)
549 if (mapArgs.count(strArg))
550 return strtoll(mapArgs[strArg]);
554 int32_t GetArgInt(const string& strArg, int32_t nDefault)
556 if (mapArgs.count(strArg))
557 return strtol(mapArgs[strArg]);
561 uint32_t GetArgUInt(const string& strArg, uint32_t nDefault)
563 if (mapArgs.count(strArg))
564 return strtoul(mapArgs[strArg]);
568 bool GetBoolArg(const string& strArg, bool fDefault)
570 if (mapArgs.count(strArg))
572 if (mapArgs[strArg].empty())
574 return (atoi(mapArgs[strArg]) != 0);
579 bool SoftSetArg(const string& strArg, const string& strValue)
581 if (mapArgs.count(strArg) || mapMultiArgs.count(strArg))
583 mapArgs[strArg] = strValue;
584 mapMultiArgs[strArg].push_back(strValue);
589 bool SoftSetBoolArg(const string& strArg, bool fValue)
592 return SoftSetArg(strArg, string("1"));
594 return SoftSetArg(strArg, string("0"));
598 string EncodeBase64(const unsigned char* pch, size_t len)
600 static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
603 strRet.reserve((len+2)/3*4);
606 const unsigned char *pchEnd = pch+len;
613 case 0: // we have no bits
614 strRet += pbase64[enc >> 2];
615 left = (enc & 3) << 4;
619 case 1: // we have two bits
620 strRet += pbase64[left | (enc >> 4)];
621 left = (enc & 15) << 2;
625 case 2: // we have four bits
626 strRet += pbase64[left | (enc >> 6)];
627 strRet += pbase64[enc & 63];
635 strRet += pbase64[left];
644 string EncodeBase64(const string& str)
646 return EncodeBase64((const unsigned char*)str.c_str(), str.size());
649 vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid)
651 static const int decode64_table[256] =
653 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
654 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
655 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
656 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
657 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
658 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
659 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
660 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
661 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
662 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
663 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
664 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
665 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
671 vector<unsigned char> vchRet;
672 vchRet.reserve(strlen(p)*3/4);
679 int dec = decode64_table[(unsigned char)*p];
680 if (dec == -1) break;
684 case 0: // we have no bits and get 6
689 case 1: // we have 6 bits and keep 4
690 vchRet.push_back((left<<2) | (dec>>4));
695 case 2: // we have 4 bits and get 6, we keep 2
696 vchRet.push_back((left<<4) | (dec>>2));
701 case 3: // we have 2 bits and get 6
702 vchRet.push_back((left<<6) | dec);
711 case 0: // 4n base64 characters processed: ok
714 case 1: // 4n+1 base64 character processed: impossible
718 case 2: // 4n+2 base64 characters processed: require '=='
719 if (left || p[0] != '=' || p[1] != '=' || decode64_table[(unsigned char)p[2]] != -1)
723 case 3: // 4n+3 base64 characters processed: require '='
724 if (left || p[0] != '=' || decode64_table[(unsigned char)p[1]] != -1)
732 string DecodeBase64(const string& str)
734 vector<unsigned char> vchRet = DecodeBase64(str.c_str());
735 return string((const char*)&vchRet[0], vchRet.size());
738 string EncodeBase32(const unsigned char* pch, size_t len)
740 static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
743 strRet.reserve((len+4)/5*8);
746 const unsigned char *pchEnd = pch+len;
753 case 0: // we have no bits
754 strRet += pbase32[enc >> 3];
755 left = (enc & 7) << 2;
759 case 1: // we have three bits
760 strRet += pbase32[left | (enc >> 6)];
761 strRet += pbase32[(enc >> 1) & 31];
762 left = (enc & 1) << 4;
766 case 2: // we have one bit
767 strRet += pbase32[left | (enc >> 4)];
768 left = (enc & 15) << 1;
772 case 3: // we have four bits
773 strRet += pbase32[left | (enc >> 7)];
774 strRet += pbase32[(enc >> 2) & 31];
775 left = (enc & 3) << 3;
779 case 4: // we have two bits
780 strRet += pbase32[left | (enc >> 5)];
781 strRet += pbase32[enc & 31];
786 static const int nPadding[5] = {0, 6, 4, 3, 1};
789 strRet += pbase32[left];
790 for (int n=0; n<nPadding[mode]; n++)
797 string EncodeBase32(const string& str)
799 return EncodeBase32((const unsigned char*)str.c_str(), str.size());
802 vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid)
804 static const int decode32_table[256] =
806 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
807 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
808 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
809 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
810 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
811 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
812 23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
813 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
814 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
815 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
816 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
817 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
818 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
824 vector<unsigned char> vchRet;
825 vchRet.reserve((strlen(p))*5/8);
832 int dec = decode32_table[(unsigned char)*p];
833 if (dec == -1) break;
837 case 0: // we have no bits and get 5
842 case 1: // we have 5 bits and keep 2
843 vchRet.push_back((left<<3) | (dec>>2));
848 case 2: // we have 2 bits and keep 7
849 left = left << 5 | dec;
853 case 3: // we have 7 bits and keep 4
854 vchRet.push_back((left<<1) | (dec>>4));
859 case 4: // we have 4 bits, and keep 1
860 vchRet.push_back((left<<4) | (dec>>1));
865 case 5: // we have 1 bit, and keep 6
866 left = left << 5 | dec;
870 case 6: // we have 6 bits, and keep 3
871 vchRet.push_back((left<<2) | (dec>>3));
876 case 7: // we have 3 bits, and keep 0
877 vchRet.push_back((left<<5) | dec);
886 case 0: // 8n base32 characters processed: ok
889 case 1: // 8n+1 base32 characters processed: impossible
895 case 2: // 8n+2 base32 characters processed: require '======'
896 if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || p[3] != '=' || p[4] != '=' || p[5] != '=' || decode32_table[(unsigned char)p[6]] != -1)
900 case 4: // 8n+4 base32 characters processed: require '===='
901 if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || p[3] != '=' || decode32_table[(unsigned char)p[4]] != -1)
905 case 5: // 8n+5 base32 characters processed: require '==='
906 if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || decode32_table[(unsigned char)p[3]] != -1)
910 case 7: // 8n+7 base32 characters processed: require '='
911 if (left || p[0] != '=' || decode32_table[(unsigned char)p[1]] != -1)
919 string DecodeBase32(const string& str)
921 vector<unsigned char> vchRet = DecodeBase32(str.c_str());
922 return string((const char*)&vchRet[0], vchRet.size());
926 int64_t DecodeDumpTime(const string& s)
930 for(size_t i=0; i<formats_n; ++i)
933 is.imbue(formats[i]);
935 if(pt != bt::ptime()) break;
938 return pt_to_time_t(pt);
941 string EncodeDumpTime(int64_t nTime) {
942 return DateTimeStrFormat("%Y-%m-%dT%H:%M:%SZ", nTime);
945 string EncodeDumpString(const string &str) {
947 for(unsigned char c : str) {
948 if (c <= 32 || c >= 128 || c == '%') {
949 ret << '%' << HexStr(&c, &c + 1);
957 string DecodeDumpString(const string &str) {
959 for (unsigned int pos = 0; pos < str.length(); pos++) {
960 unsigned char c = str[pos];
961 if (c == '%' && pos+2 < str.length()) {
962 c = (((str[pos+1]>>6)*9+((str[pos+1]-'0')&15)) << 4) |
963 ((str[pos+2]>>6)*9+((str[pos+2]-'0')&15));
971 bool WildcardMatch(const char* psz, const char* mask)
978 return (*psz == '\0');
980 return WildcardMatch(psz, mask+1) || (*psz && WildcardMatch(psz+1, mask));
995 bool WildcardMatch(const string& str, const string& mask)
997 return WildcardMatch(str.c_str(), mask.c_str());
1001 static string FormatException(exception* pex, const char* pszThread)
1004 char pszModule[MAX_PATH] = "";
1005 GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
1007 const char* pszModule = "novacoin";
1011 "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
1014 "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
1017 void LogException(exception* pex, const char* pszThread)
1019 string message = FormatException(pex, pszThread);
1020 printf("\n%s", message.c_str());
1023 void PrintException(exception* pex, const char* pszThread)
1025 string message = FormatException(pex, pszThread);
1026 printf("\n\n************************\n%s\n", message.c_str());
1027 fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
1028 strMiscWarning = message;
1032 void LogStackTrace() {
1033 printf("\n\n******* exception encountered *******\n");
1036 #if !defined(WIN32) && !defined(ANDROID)
1037 void* pszBuffer[32];
1039 size = backtrace(pszBuffer, 32);
1040 backtrace_symbols_fd(pszBuffer, size, fileno(fileout));
1045 void PrintExceptionContinue(exception* pex, const char* pszThread)
1047 string message = FormatException(pex, pszThread);
1048 printf("\n\n************************\n%s\n", message.c_str());
1049 fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
1050 strMiscWarning = message;
1053 boost::filesystem::path GetDefaultDataDir()
1055 namespace fs = boost::filesystem;
1056 // Windows < Vista: C:\Documents and Settings\Username\Application Data\NovaCoin
1057 // Windows >= Vista: C:\Users\Username\AppData\Roaming\NovaCoin
1058 // Mac: ~/Library/Application Support/NovaCoin
1059 // Unix: ~/.novacoin
1062 return GetSpecialFolderPath(CSIDL_APPDATA) / "NovaCoin";
1065 char* pszHome = getenv("HOME");
1066 if (pszHome == NULL || strlen(pszHome) == 0)
1067 pathRet = fs::path("/");
1069 pathRet = fs::path(pszHome);
1072 pathRet /= "Library/Application Support";
1073 fs::create_directory(pathRet);
1074 return pathRet / "NovaCoin";
1077 return pathRet / ".novacoin";
1082 const boost::filesystem::path &GetDataDir(bool fNetSpecific)
1084 namespace fs = boost::filesystem;
1086 static fs::path pathCached[2];
1087 static CCriticalSection csPathCached;
1088 static bool cachedPath[2] = {false, false};
1090 fs::path &path = pathCached[fNetSpecific];
1092 // This can be called during exceptions by printf, so we cache the
1093 // value so we don't have to do memory allocations after that.
1094 if (cachedPath[fNetSpecific])
1099 if (mapArgs.count("-datadir")) {
1100 path = fs::system_complete(mapArgs["-datadir"]);
1101 if (!fs::is_directory(path)) {
1106 path = GetDefaultDataDir();
1108 if (fNetSpecific && GetBoolArg("-testnet", false))
1111 fs::create_directory(path);
1113 cachedPath[fNetSpecific]=true;
1117 string randomStrGen(int length) {
1118 static string charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
1120 result.resize(length);
1121 for (int32_t i = 0; i < length; i++)
1122 result[i] = charset[rand() % charset.length()];
1129 srand(static_cast<unsigned int>(time(NULL)));
1132 #if BOOST_FILESYSTEM_VERSION >= 3
1133 pConf.open(GetConfigFile().generic_string().c_str());
1135 pConf.open(GetConfigFile().string().c_str());
1137 pConf << "rpcuser=user\nrpcpassword="
1140 + "\n#(0=off, 1=on) daemon - run in the background as a daemon and accept commands"
1142 + "\n#(0=off, 1=on) server - accept command line and JSON-RPC commands"
1144 + "\nrpcallowip=127.0.0.1";
1148 boost::filesystem::path GetConfigFile()
1150 boost::filesystem::path pathConfigFile(GetArg("-conf", "novacoin.conf"));
1151 if (!pathConfigFile.is_complete()) pathConfigFile = GetDataDir(false) / pathConfigFile;
1152 return pathConfigFile;
1155 void ReadConfigFile(map<string, string>& mapSettingsRet,
1156 map<string, vector<string> >& mapMultiSettingsRet)
1158 boost::filesystem::ifstream streamConfig(GetConfigFile());
1159 if (!streamConfig.good())
1162 new(&streamConfig) boost::filesystem::ifstream(GetConfigFile());
1163 if(!streamConfig.good())
1167 set<string> setOptions;
1168 setOptions.insert("*");
1170 for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
1172 // Don't overwrite existing settings so command line settings override bitcoin.conf
1173 string strKey = string("-") + it->string_key;
1174 if (mapSettingsRet.count(strKey) == 0)
1176 mapSettingsRet[strKey] = it->value[0];
1177 // interpret nofoo=1 as foo=0 (and nofoo=0 as foo=1) as long as foo not set)
1178 InterpretNegativeSetting(strKey, mapSettingsRet);
1180 mapMultiSettingsRet[strKey].push_back(it->value[0]);
1184 boost::filesystem::path GetPidFile()
1186 boost::filesystem::path pathPidFile(GetArg("-pid", "novacoind.pid"));
1187 if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
1192 void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
1194 FILE* file = fopen(path.string().c_str(), "w");
1197 fprintf(file, "%d\n", pid);
1203 bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
1206 return MoveFileExA(src.string().c_str(), dest.string().c_str(),
1207 MOVEFILE_REPLACE_EXISTING) != 0;
1209 int rc = rename(src.string().c_str(), dest.string().c_str());
1214 void FileCommit(FILE *fileout)
1216 fflush(fileout); // harmless if redundantly called
1218 _commit(_fileno(fileout));
1220 fsync(fileno(fileout));
1224 int GetFilesize(FILE* file)
1226 int nSavePos = ftell(file);
1228 if (fseek(file, 0, SEEK_END) == 0)
1229 nFilesize = ftell(file);
1230 fseek(file, nSavePos, SEEK_SET);
1234 void ShrinkDebugFile()
1236 // Scroll debug.log if it's getting too big
1237 boost::filesystem::path pathLog = GetDataDir() / "debug.log";
1238 FILE* file = fopen(pathLog.string().c_str(), "r");
1239 if (file && GetFilesize(file) > 10 * 1000000)
1241 // Restart the file with some of the end
1243 vector<char>* vBuf = new vector <char>(200000, 0);
1244 fseek(file, -((long)(vBuf->size())), SEEK_END);
1245 size_t nBytes = fread(&vBuf->operator[](0), 1, vBuf->size(), file);
1247 file = fopen(pathLog.string().c_str(), "w");
1250 fwrite(&vBuf->operator[](0), 1, nBytes, file);
1255 catch (const bad_alloc& e) {
1256 // Bad things happen - no free memory in heap at program startup
1258 printf("Warning: %s in %s:%d\n ShrinkDebugFile failed - debug.log expands further", e.what(), __FILE__, __LINE__);
1264 // "Never go to sea with two chronometers; take one or three."
1265 // Our three time sources are:
1267 // - Median of other nodes clocks
1268 // - The user (asking the user to fix the system clock if the first two disagree)
1274 int64_t now = time(NULL);
1279 // Trusted NTP offset or median of NTP samples.
1280 extern int64_t nNtpOffset;
1282 // Median of time samples given by other nodes.
1283 static int64_t nNodesOffset = numeric_limits<int64_t>::max();
1285 // Select time offset:
1286 int64_t GetTimeOffset()
1288 // If NTP and system clock are in agreement within 40 minutes, then use NTP.
1289 if (abs(nNtpOffset) < 40 * 60)
1292 // If not, then choose between median peer time and system clock.
1293 if (abs(nNodesOffset) < 70 * 60)
1294 return nNodesOffset;
1299 int64_t GetNodesOffset()
1301 return nNodesOffset;
1304 int64_t GetAdjustedTime()
1306 return GetTime() + GetTimeOffset();
1309 void AddTimeData(const CNetAddr& ip, int64_t nTime)
1311 int64_t nOffsetSample = nTime - GetTime();
1313 // Ignore duplicates
1314 static set<CNetAddr> setKnown;
1315 if (!setKnown.insert(ip).second)
1319 vTimeOffsets.input(nOffsetSample);
1320 printf("Added time data, samples %d, offset %+" PRId64 " (%+" PRId64 " minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample/60);
1321 if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1)
1323 auto nMedian = vTimeOffsets.median();
1324 auto vSorted = vTimeOffsets.sorted();
1325 // Only let other nodes change our time by so much
1326 if (abs(nMedian) < 70 * 60)
1328 nNodesOffset = nMedian;
1332 nNodesOffset = numeric_limits<int64_t>::max();
1337 bool fMatch = false;
1339 // If nobody has a time different than ours but within 5 minutes of ours, give a warning
1340 for(auto nOffset : vSorted)
1341 if (nOffset != 0 && abs(nOffset) < 5 * 60)
1347 string strMessage = _("Warning: Please check that your computer's date and time are correct! If your clock is wrong NovaCoin will not work properly.");
1348 strMiscWarning = strMessage;
1349 printf("*** %s\n", strMessage.c_str());
1350 uiInterface.ThreadSafeMessageBox(strMessage+" ", string("NovaCoin"), CClientUIInterface::OK | CClientUIInterface::ICON_EXCLAMATION);
1355 for(int64_t n : vSorted)
1356 printf("%+" PRId64 " ", n);
1359 if (nNodesOffset != numeric_limits<int64_t>::max())
1360 printf("nNodesOffset = %+" PRId64 " (%+" PRId64 " minutes)\n", nNodesOffset, nNodesOffset/60);
1364 string FormatVersion(int nVersion)
1366 if (nVersion%100 == 0)
1367 return strprintf("%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100);
1369 return strprintf("%d.%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100, nVersion%100);
1372 string FormatFullVersion()
1374 return CLIENT_BUILD;
1377 // Format the subversion field according to BIP 14 spec (https://en.bitcoin.it/wiki/BIP_0014)
1378 string FormatSubVersion(const string& name, int nClientVersion, const vector<string>& comments)
1382 ss << name << ":" << FormatVersion(nClientVersion);
1383 if (!comments.empty())
1384 ss << "(" << boost::algorithm::join(comments, "; ") << ")";
1390 boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate)
1392 namespace fs = boost::filesystem;
1394 char pszPath[MAX_PATH] = "";
1396 if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate))
1398 return fs::path(pszPath);
1401 printf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
1402 return fs::path("");
1406 void runCommand(string strCommand)
1408 int nErr = ::system(strCommand.c_str());
1410 printf("runCommand error: system(%s) returned %d\n", strCommand.c_str(), nErr);
1413 void RenameThread(const char* name)
1415 #if defined(PR_SET_NAME)
1416 // Only the first 15 characters are used (16 - NUL terminator)
1417 ::prctl(PR_SET_NAME, name, 0, 0, 0);
1418 #elif 0 && (defined(__FreeBSD__) || defined(__OpenBSD__))
1419 // TODO: This is currently disabled because it needs to be verified to work
1420 // on FreeBSD or OpenBSD first. When verified the '0 &&' part can be
1422 pthread_set_name_np(pthread_self(), name);
1424 // This is XCode 10.6-and-later; bring back if we drop 10.5 support:
1425 // #elif defined(MAC_OSX)
1426 // pthread_setname_np(name);
1429 // Prevent warnings for unused parameters...
1434 bool NewThread(void(*pfn)(void*), void* parg)
1438 boost::thread(pfn, parg); // thread detaches when out of scope
1439 } catch(boost::thread_resource_error &e) {
1440 printf("Error creating thread: %s\n", e.what());
1446 string DateTimeStrFormat(const char* pszFormat, int64_t nTime)
1448 // locale takes ownership of the pointer
1449 locale loc(locale::classic(), new boost::posix_time::time_facet(pszFormat));
1452 ss << boost::posix_time::from_time_t(nTime);