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.
11 #include <boost/program_options/detail/config_file.hpp>
12 #include <boost/program_options/parsers.hpp>
13 #include <boost/filesystem.hpp>
14 #include <boost/filesystem/fstream.hpp>
15 #include <boost/date_time/posix_time/posix_time.hpp>
16 #include <boost/thread.hpp>
17 #include <openssl/crypto.h>
18 #include <openssl/rand.h>
21 #include <io.h> /* for _commit */
23 #elif defined(__linux__)
24 #include <sys/prctl.h>
27 #if !defined(WIN32) && !defined(ANDROID)
33 namespace bt = boost::posix_time;
35 map<string, string> mapArgs;
36 map<string, vector<string> > mapMultiArgs;
38 bool fDebugNet = false;
39 bool fPrintToConsole = false;
40 bool fPrintToDebugger = false;
41 bool fRequestShutdown = false;
42 bool fShutdown = false;
45 bool fCommandLine = false;
46 string strMiscWarning;
47 bool fTestNet = false;
48 bool fNoListen = false;
49 bool fLogTimestamps = false;
50 CMedianFilter<int64_t> vTimeOffsets(200,0);
51 bool fReopenDebugLog = false;
53 // Extended DecodeDumpTime implementation, see this page for details:
54 // http://stackoverflow.com/questions/3786201/parsing-of-date-time-from-string-boost
55 const std::locale formats[] = {
56 std::locale(std::locale::classic(),new bt::time_input_facet("%Y-%m-%dT%H:%M:%SZ")),
57 std::locale(std::locale::classic(),new bt::time_input_facet("%Y-%m-%d %H:%M:%S")),
58 std::locale(std::locale::classic(),new bt::time_input_facet("%Y/%m/%d %H:%M:%S")),
59 std::locale(std::locale::classic(),new bt::time_input_facet("%d.%m.%Y %H:%M:%S")),
60 std::locale(std::locale::classic(),new bt::time_input_facet("%Y-%m-%d"))
63 const size_t formats_n = sizeof(formats)/sizeof(formats[0]);
65 std::time_t pt_to_time_t(const bt::ptime& pt)
67 bt::ptime timet_start(boost::gregorian::date(1970,1,1));
68 bt::time_duration diff = pt - timet_start;
69 return diff.ticks()/bt::time_duration::rep_type::ticks_per_second;
72 // Init OpenSSL library multithreading support
73 static CCriticalSection** ppmutexOpenSSL;
74 void locking_callback(int mode, int i, const char* file, int line)
76 if (mode & CRYPTO_LOCK) {
77 ENTER_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
79 LEAVE_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
83 LockedPageManager LockedPageManager::instance;
91 // Init OpenSSL library multithreading support
92 ppmutexOpenSSL = (CCriticalSection**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(CCriticalSection*));
93 for (int i = 0; i < CRYPTO_num_locks(); i++)
94 ppmutexOpenSSL[i] = new CCriticalSection();
95 CRYPTO_set_locking_callback(locking_callback);
98 // Seed random number generator with screen scrape and other hardware sources
102 // Seed random number generator with performance counter
107 // Shutdown OpenSSL library multithreading support
108 CRYPTO_set_locking_callback(NULL);
109 for (int i = 0; i < CRYPTO_num_locks(); i++)
110 delete ppmutexOpenSSL[i];
111 OPENSSL_free(ppmutexOpenSSL);
125 // Seed with CPU performance counter
126 int64_t nCounter = GetPerformanceCounter();
127 RAND_add(&nCounter, sizeof(nCounter), 1.5);
128 memset(&nCounter, 0, sizeof(nCounter));
131 void RandAddSeedPerfmon()
135 // This can take up to 2 seconds, so only do it every 10 minutes
136 static int64_t nLastPerfmon;
137 if (GetTime() < nLastPerfmon + 10 * 60)
139 nLastPerfmon = GetTime();
142 // Don't need this on Linux, OpenSSL automatically uses /dev/urandom
143 // Seed with the entire set of perfmon data
144 unsigned char pdata[250000];
145 memset(pdata, 0, sizeof(pdata));
146 unsigned long nSize = sizeof(pdata);
147 long ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, pdata, &nSize);
148 RegCloseKey(HKEY_PERFORMANCE_DATA);
149 if (ret == ERROR_SUCCESS)
151 RAND_add(pdata, nSize, nSize/100.0);
152 OPENSSL_cleanse(pdata, nSize);
153 printf("RandAddSeed() %lu bytes\n", nSize);
158 uint64_t GetRand(uint64_t nMax)
163 // The range of the random source must be a multiple of the modulus
164 // to give every possible output value an equal possibility
165 uint64_t nRange = (std::numeric_limits<uint64_t>::max() / nMax) * nMax;
168 RAND_bytes((unsigned char*)&nRand, sizeof(nRand));
169 while (nRand >= nRange);
170 return (nRand % nMax);
173 int GetRandInt(int nMax)
175 return static_cast<int>(GetRand(nMax));
178 static FILE* fileout = NULL;
180 inline int OutputDebugStringF(const char* pszFormat, ...)
187 va_start(arg_ptr, pszFormat);
188 ret = vprintf(pszFormat, arg_ptr);
191 else if (!fPrintToDebugger)
193 // print to debug.log
197 boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
198 fileout = fopen(pathDebug.string().c_str(), "a");
199 if (fileout) setbuf(fileout, NULL); // unbuffered
203 static bool fStartedNewLine = true;
205 // This routine may be called by global destructors during shutdown.
206 // Since the order of destruction of static/global objects is undefined,
207 // allocate mutexDebugLog on the heap the first time this routine
208 // is called to avoid crashes during shutdown.
209 static std::mutex* mutexDebugLog = nullptr;
210 if (mutexDebugLog == nullptr) mutexDebugLog = new std::mutex();
211 std::scoped_lock scoped_lock(*mutexDebugLog);
213 // reopen the log file, if requested
214 if (fReopenDebugLog) {
215 fReopenDebugLog = false;
216 boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
217 if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
218 setbuf(fileout, NULL); // unbuffered
221 // Debug print useful for profiling
222 if (fLogTimestamps && fStartedNewLine)
223 fprintf(fileout, "%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
224 if (pszFormat[strlen(pszFormat) - 1] == '\n')
225 fStartedNewLine = true;
227 fStartedNewLine = false;
230 va_start(arg_ptr, pszFormat);
231 ret = vfprintf(fileout, pszFormat, arg_ptr);
237 if (fPrintToDebugger)
239 static CCriticalSection cs_OutputDebugStringF;
241 // accumulate and output a line at a time
243 LOCK(cs_OutputDebugStringF);
244 static std::string buffer;
247 va_start(arg_ptr, pszFormat);
248 buffer += vstrprintf(pszFormat, arg_ptr);
251 size_t line_start = 0, line_end;
252 while((line_end = buffer.find('\n', line_start)) != -1)
254 OutputDebugStringA(buffer.substr(line_start, line_end - line_start).c_str());
255 line_start = line_end + 1;
257 buffer.erase(0, line_start);
264 string vstrprintf(const char *format, va_list ap)
268 int limit = sizeof(buffer);
274 va_copy(arg_ptr, ap);
276 va_list arg_ptr = ap;
279 ret = _vsnprintf(p, limit, format, arg_ptr);
281 ret = vsnprintf(p, limit, format, arg_ptr);
284 if (ret >= 0 && ret < limit)
289 p = new(nothrow) char[limit];
291 throw std::bad_alloc();
293 string str(p, p+ret);
299 string real_strprintf(const char *format, int dummy, ...)
302 va_start(arg_ptr, dummy);
303 string str = vstrprintf(format, arg_ptr);
308 string real_strprintf(const std::string &format, int dummy, ...)
311 va_start(arg_ptr, dummy);
312 string str = vstrprintf(format.c_str(), arg_ptr);
317 bool error(const char *format, ...)
320 va_start(arg_ptr, format);
321 std::string str = vstrprintf(format, arg_ptr);
323 printf("ERROR: %s\n", str.c_str());
328 void ParseString(const string& str, char c, vector<string>& v)
332 string::size_type i1 = 0;
333 string::size_type i2;
336 i2 = str.find(c, i1);
339 v.push_back(str.substr(i1));
342 v.push_back(str.substr(i1, i2-i1));
348 string FormatMoney(int64_t n, bool fPlus)
350 // Note: not using straight sprintf here because we do NOT want
351 // localized number formatting.
352 int64_t n_abs = (n > 0 ? n : -n);
353 int64_t quotient = n_abs/COIN;
354 int64_t remainder = n_abs%COIN;
355 string str = strprintf("%" PRId64 ".%06" PRId64, quotient, remainder);
357 // Right-trim excess zeros before the decimal point:
359 for (size_t i = str.size()-1; (str[i] == '0' && isdigit(str[i-2])); --i)
362 str.erase(str.size()-nTrim, nTrim);
365 str.insert(0u, 1, '-');
366 else if (fPlus && n > 0)
367 str.insert(0u, 1, '+');
372 bool ParseMoney(const string& str, int64_t& nRet)
374 return ParseMoney(str.c_str(), nRet);
377 bool ParseMoney(const char* pszIn, int64_t& nRet)
381 const char* p = pszIn;
389 int64_t nMult = CENT*10;
390 while (isdigit(*p) && (nMult > 0))
392 nUnits += nMult * (*p++ - '0');
401 strWhole.insert(strWhole.end(), *p);
406 if (strWhole.size() > 10) // guard against 63 bit overflow
408 if (nUnits < 0 || nUnits > COIN)
410 int64_t nWhole = atoi64(strWhole);
411 int64_t nValue = nWhole*COIN + nUnits;
418 static const signed char phexdigit[256] =
419 { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
420 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
421 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
422 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
423 -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
424 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
425 -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
426 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
427 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
428 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
429 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
430 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
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 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, };
436 bool IsHex(const string& str)
438 for (unsigned char c : str)
440 if (phexdigit[c] < 0)
443 return (str.size() > 0) && (str.size()%2 == 0);
446 vector<unsigned char> ParseHex(const char* psz)
448 // convert hex dump to vector
449 vector<unsigned char> vch;
452 while (isspace(*psz))
454 signed char c = phexdigit[(unsigned char)*psz++];
455 if (c == (signed char)-1)
457 unsigned char n = (c << 4);
458 c = phexdigit[(unsigned char)*psz++];
459 if (c == (signed char)-1)
467 vector<unsigned char> ParseHex(const string& str)
469 return ParseHex(str.c_str());
472 static void InterpretNegativeSetting(string name, map<string, string>& mapSettingsRet)
474 // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
475 if (name.find("-no") == 0)
477 std::string positive("-");
478 positive.append(name.begin()+3, name.end());
479 if (mapSettingsRet.count(positive) == 0)
481 bool value = !GetBoolArg(name);
482 mapSettingsRet[positive] = (value ? "1" : "0");
487 void ParseParameters(int argc, const char* const argv[])
490 mapMultiArgs.clear();
491 for (int i = 1; i < argc; i++)
493 std::string str(argv[i]);
494 std::string strValue;
495 size_t is_index = str.find('=');
496 if (is_index != std::string::npos)
498 strValue = str.substr(is_index+1);
499 str = str.substr(0, is_index);
502 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
503 if (str.compare(0,1, "/") == 0)
504 str = "-" + str.substr(1);
509 mapArgs[str] = strValue;
510 mapMultiArgs[str].push_back(strValue);
514 for (const auto& entry : mapArgs)
516 string name = entry.first;
518 // interpret --foo as -foo (as long as both are not set)
519 if (name.find("--") == 0)
521 std::string singleDash(name.begin()+1, name.end());
522 if (mapArgs.count(singleDash) == 0)
523 mapArgs[singleDash] = entry.second;
527 // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
528 InterpretNegativeSetting(name, mapArgs);
532 std::string GetArg(const std::string& strArg, const std::string& strDefault)
534 if (mapArgs.count(strArg))
535 return mapArgs[strArg];
539 int64_t GetArg(const std::string& strArg, int64_t nDefault)
541 if (mapArgs.count(strArg))
542 return atoi64(mapArgs[strArg]);
546 int32_t GetArgInt(const std::string& strArg, int32_t nDefault)
548 if (mapArgs.count(strArg))
549 return strtol(mapArgs[strArg]);
553 uint32_t GetArgUInt(const std::string& strArg, uint32_t nDefault)
555 if (mapArgs.count(strArg))
556 return strtoul(mapArgs[strArg]);
560 bool GetBoolArg(const std::string& strArg, bool fDefault)
562 if (mapArgs.count(strArg))
564 if (mapArgs[strArg].empty())
566 return (atoi(mapArgs[strArg]) != 0);
571 bool SoftSetArg(const std::string& strArg, const std::string& strValue)
573 if (mapArgs.count(strArg) || mapMultiArgs.count(strArg))
575 mapArgs[strArg] = strValue;
576 mapMultiArgs[strArg].push_back(strValue);
581 bool SoftSetBoolArg(const std::string& strArg, bool fValue)
584 return SoftSetArg(strArg, std::string("1"));
586 return SoftSetArg(strArg, std::string("0"));
590 string EncodeBase64(const unsigned char* pch, size_t len)
592 static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
595 strRet.reserve((len+2)/3*4);
598 const unsigned char *pchEnd = pch+len;
605 case 0: // we have no bits
606 strRet += pbase64[enc >> 2];
607 left = (enc & 3) << 4;
611 case 1: // we have two bits
612 strRet += pbase64[left | (enc >> 4)];
613 left = (enc & 15) << 2;
617 case 2: // we have four bits
618 strRet += pbase64[left | (enc >> 6)];
619 strRet += pbase64[enc & 63];
627 strRet += pbase64[left];
636 string EncodeBase64(const string& str)
638 return EncodeBase64((const unsigned char*)str.c_str(), str.size());
641 vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid)
643 static const int decode64_table[256] =
645 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
646 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
647 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
648 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
649 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
650 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
651 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
652 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
656 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
657 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
663 vector<unsigned char> vchRet;
664 vchRet.reserve(strlen(p)*3/4);
671 int dec = decode64_table[(unsigned char)*p];
672 if (dec == -1) break;
676 case 0: // we have no bits and get 6
681 case 1: // we have 6 bits and keep 4
682 vchRet.push_back((left<<2) | (dec>>4));
687 case 2: // we have 4 bits and get 6, we keep 2
688 vchRet.push_back((left<<4) | (dec>>2));
693 case 3: // we have 2 bits and get 6
694 vchRet.push_back((left<<6) | dec);
703 case 0: // 4n base64 characters processed: ok
706 case 1: // 4n+1 base64 character processed: impossible
710 case 2: // 4n+2 base64 characters processed: require '=='
711 if (left || p[0] != '=' || p[1] != '=' || decode64_table[(unsigned char)p[2]] != -1)
715 case 3: // 4n+3 base64 characters processed: require '='
716 if (left || p[0] != '=' || decode64_table[(unsigned char)p[1]] != -1)
724 string DecodeBase64(const string& str)
726 vector<unsigned char> vchRet = DecodeBase64(str.c_str());
727 return string((const char*)&vchRet[0], vchRet.size());
730 string EncodeBase32(const unsigned char* pch, size_t len)
732 static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
735 strRet.reserve((len+4)/5*8);
738 const unsigned char *pchEnd = pch+len;
745 case 0: // we have no bits
746 strRet += pbase32[enc >> 3];
747 left = (enc & 7) << 2;
751 case 1: // we have three bits
752 strRet += pbase32[left | (enc >> 6)];
753 strRet += pbase32[(enc >> 1) & 31];
754 left = (enc & 1) << 4;
758 case 2: // we have one bit
759 strRet += pbase32[left | (enc >> 4)];
760 left = (enc & 15) << 1;
764 case 3: // we have four bits
765 strRet += pbase32[left | (enc >> 7)];
766 strRet += pbase32[(enc >> 2) & 31];
767 left = (enc & 3) << 3;
771 case 4: // we have two bits
772 strRet += pbase32[left | (enc >> 5)];
773 strRet += pbase32[enc & 31];
778 static const int nPadding[5] = {0, 6, 4, 3, 1};
781 strRet += pbase32[left];
782 for (int n=0; n<nPadding[mode]; n++)
789 string EncodeBase32(const string& str)
791 return EncodeBase32((const unsigned char*)str.c_str(), str.size());
794 vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid)
796 static const int decode32_table[256] =
798 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
799 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
800 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
801 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
802 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
803 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
804 23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
805 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
809 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
810 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
816 vector<unsigned char> vchRet;
817 vchRet.reserve((strlen(p))*5/8);
824 int dec = decode32_table[(unsigned char)*p];
825 if (dec == -1) break;
829 case 0: // we have no bits and get 5
834 case 1: // we have 5 bits and keep 2
835 vchRet.push_back((left<<3) | (dec>>2));
840 case 2: // we have 2 bits and keep 7
841 left = left << 5 | dec;
845 case 3: // we have 7 bits and keep 4
846 vchRet.push_back((left<<1) | (dec>>4));
851 case 4: // we have 4 bits, and keep 1
852 vchRet.push_back((left<<4) | (dec>>1));
857 case 5: // we have 1 bit, and keep 6
858 left = left << 5 | dec;
862 case 6: // we have 6 bits, and keep 3
863 vchRet.push_back((left<<2) | (dec>>3));
868 case 7: // we have 3 bits, and keep 0
869 vchRet.push_back((left<<5) | dec);
878 case 0: // 8n base32 characters processed: ok
881 case 1: // 8n+1 base32 characters processed: impossible
887 case 2: // 8n+2 base32 characters processed: require '======'
888 if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || p[3] != '=' || p[4] != '=' || p[5] != '=' || decode32_table[(unsigned char)p[6]] != -1)
892 case 4: // 8n+4 base32 characters processed: require '===='
893 if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || p[3] != '=' || decode32_table[(unsigned char)p[4]] != -1)
897 case 5: // 8n+5 base32 characters processed: require '==='
898 if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || decode32_table[(unsigned char)p[3]] != -1)
902 case 7: // 8n+7 base32 characters processed: require '='
903 if (left || p[0] != '=' || decode32_table[(unsigned char)p[1]] != -1)
911 string DecodeBase32(const string& str)
913 vector<unsigned char> vchRet = DecodeBase32(str.c_str());
914 return string((const char*)&vchRet[0], vchRet.size());
918 int64_t DecodeDumpTime(const std::string& s)
922 for(size_t i=0; i<formats_n; ++i)
924 std::istringstream is(s);
925 is.imbue(formats[i]);
927 if(pt != bt::ptime()) break;
930 return pt_to_time_t(pt);
933 std::string EncodeDumpTime(int64_t nTime) {
934 return DateTimeStrFormat("%Y-%m-%dT%H:%M:%SZ", nTime);
937 std::string EncodeDumpString(const std::string &str) {
938 std::stringstream ret;
939 for (unsigned char c : str) {
940 if (c <= 32 || c >= 128 || c == '%') {
941 ret << '%' << HexStr(&c, &c + 1);
949 std::string DecodeDumpString(const std::string &str) {
950 std::stringstream ret;
951 for (unsigned int pos = 0; pos < str.length(); pos++) {
952 unsigned char c = str[pos];
953 if (c == '%' && pos+2 < str.length()) {
954 c = (((str[pos+1]>>6)*9+((str[pos+1]-'0')&15)) << 4) |
955 ((str[pos+2]>>6)*9+((str[pos+2]-'0')&15));
963 bool WildcardMatch(const char* psz, const char* mask)
970 return (*psz == '\0');
972 return WildcardMatch(psz, mask+1) || (*psz && WildcardMatch(psz+1, mask));
987 bool WildcardMatch(const string& str, const string& mask)
989 return WildcardMatch(str.c_str(), mask.c_str());
999 static std::string FormatException(std::exception* pex, const char* pszThread)
1002 char pszModule[MAX_PATH] = "";
1003 GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
1005 const char* pszModule = "novacoin";
1009 "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
1012 "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
1015 void LogException(std::exception* pex, const char* pszThread)
1017 std::string message = FormatException(pex, pszThread);
1018 printf("\n%s", message.c_str());
1021 void PrintException(std::exception* pex, const char* pszThread)
1023 std::string message = FormatException(pex, pszThread);
1024 printf("\n\n************************\n%s\n", message.c_str());
1025 fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
1026 strMiscWarning = message;
1030 void LogStackTrace() {
1031 printf("\n\n******* exception encountered *******\n");
1034 #if !defined(WIN32) && !defined(ANDROID)
1035 void* pszBuffer[32];
1037 size = backtrace(pszBuffer, 32);
1038 backtrace_symbols_fd(pszBuffer, size, fileno(fileout));
1043 void PrintExceptionContinue(std::exception* pex, const char* pszThread)
1045 std::string message = FormatException(pex, pszThread);
1046 printf("\n\n************************\n%s\n", message.c_str());
1047 fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
1048 strMiscWarning = message;
1051 boost::filesystem::path GetDefaultDataDir()
1053 namespace fs = boost::filesystem;
1054 // Windows < Vista: C:\Documents and Settings\Username\Application Data\NovaCoin
1055 // Windows >= Vista: C:\Users\Username\AppData\Roaming\NovaCoin
1056 // Mac: ~/Library/Application Support/NovaCoin
1057 // Unix: ~/.novacoin
1060 return GetSpecialFolderPath(CSIDL_APPDATA) / "NovaCoin";
1063 char* pszHome = getenv("HOME");
1064 if (pszHome == NULL || strlen(pszHome) == 0)
1065 pathRet = fs::path("/");
1067 pathRet = fs::path(pszHome);
1070 pathRet /= "Library/Application Support";
1071 fs::create_directory(pathRet);
1072 return pathRet / "NovaCoin";
1075 return pathRet / ".novacoin";
1080 const boost::filesystem::path &GetDataDir(bool fNetSpecific)
1082 namespace fs = boost::filesystem;
1084 static fs::path pathCached[2];
1085 static CCriticalSection csPathCached;
1086 static bool cachedPath[2] = {false, false};
1088 fs::path &path = pathCached[fNetSpecific];
1090 // This can be called during exceptions by printf, so we cache the
1091 // value so we don't have to do memory allocations after that.
1092 if (cachedPath[fNetSpecific])
1097 if (mapArgs.count("-datadir")) {
1098 path = fs::system_complete(mapArgs["-datadir"]);
1099 if (!fs::is_directory(path)) {
1104 path = GetDefaultDataDir();
1106 if (fNetSpecific && GetBoolArg("-testnet", false))
1109 fs::create_directory(path);
1111 cachedPath[fNetSpecific]=true;
1115 string randomStrGen(int length) {
1116 static string charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
1118 result.resize(length);
1119 for (int32_t i = 0; i < length; i++)
1120 result[i] = charset[rand() % charset.length()];
1127 srand(static_cast<unsigned int>(time(NULL)));
1130 #if BOOST_FILESYSTEM_VERSION >= 3
1131 pConf.open(GetConfigFile().generic_string().c_str());
1133 pConf.open(GetConfigFile().string().c_str());
1135 pConf << "rpcuser=user\nrpcpassword="
1138 + "\n#(0=off, 1=on) daemon - run in the background as a daemon and accept commands"
1140 + "\n#(0=off, 1=on) server - accept command line and JSON-RPC commands"
1142 + "\nrpcallowip=127.0.0.1";
1146 boost::filesystem::path GetConfigFile()
1148 boost::filesystem::path pathConfigFile(GetArg("-conf", "novacoin.conf"));
1149 if (!pathConfigFile.is_complete()) pathConfigFile = GetDataDir(false) / pathConfigFile;
1150 return pathConfigFile;
1153 void ReadConfigFile(map<string, string>& mapSettingsRet,
1154 map<string, vector<string> >& mapMultiSettingsRet)
1156 boost::filesystem::ifstream streamConfig(GetConfigFile());
1157 if (!streamConfig.good())
1160 new(&streamConfig) boost::filesystem::ifstream(GetConfigFile());
1161 if(!streamConfig.good())
1165 set<string> setOptions;
1166 setOptions.insert("*");
1168 for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
1170 // Don't overwrite existing settings so command line settings override bitcoin.conf
1171 string strKey = string("-") + it->string_key;
1172 if (mapSettingsRet.count(strKey) == 0)
1174 mapSettingsRet[strKey] = it->value[0];
1175 // interpret nofoo=1 as foo=0 (and nofoo=0 as foo=1) as long as foo not set)
1176 InterpretNegativeSetting(strKey, mapSettingsRet);
1178 mapMultiSettingsRet[strKey].push_back(it->value[0]);
1182 boost::filesystem::path GetPidFile()
1184 boost::filesystem::path pathPidFile(GetArg("-pid", "novacoind.pid"));
1185 if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
1190 void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
1192 FILE* file = fopen(path.string().c_str(), "w");
1195 fprintf(file, "%d\n", pid);
1201 bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
1204 return MoveFileExA(src.string().c_str(), dest.string().c_str(),
1205 MOVEFILE_REPLACE_EXISTING) != 0;
1207 int rc = std::rename(src.string().c_str(), dest.string().c_str());
1212 void FileCommit(FILE *fileout)
1214 fflush(fileout); // harmless if redundantly called
1216 _commit(_fileno(fileout));
1218 fsync(fileno(fileout));
1222 int GetFilesize(FILE* file)
1224 int nSavePos = ftell(file);
1226 if (fseek(file, 0, SEEK_END) == 0)
1227 nFilesize = ftell(file);
1228 fseek(file, nSavePos, SEEK_SET);
1232 void ShrinkDebugFile()
1234 // Scroll debug.log if it's getting too big
1235 boost::filesystem::path pathLog = GetDataDir() / "debug.log";
1236 FILE* file = fopen(pathLog.string().c_str(), "r");
1237 if (file && GetFilesize(file) > 10 * 1000000)
1239 // Restart the file with some of the end
1241 vector<char>* vBuf = new vector <char>(200000, 0);
1242 fseek(file, -((long)(vBuf->size())), SEEK_END);
1243 size_t nBytes = fread(&vBuf->operator[](0), 1, vBuf->size(), file);
1245 file = fopen(pathLog.string().c_str(), "w");
1248 fwrite(&vBuf->operator[](0), 1, nBytes, file);
1253 catch (const bad_alloc& e) {
1254 // Bad things happen - no free memory in heap at program startup
1256 printf("Warning: %s in %s:%d\n ShrinkDebugFile failed - debug.log expands further", e.what(), __FILE__, __LINE__);
1269 // "Never go to sea with two chronometers; take one or three."
1270 // Our three time sources are:
1272 // - Median of other nodes clocks
1273 // - The user (asking the user to fix the system clock if the first two disagree)
1279 int64_t now = time(NULL);
1284 // Trusted NTP offset or median of NTP samples.
1285 extern int64_t nNtpOffset;
1287 // Median of time samples given by other nodes.
1288 static int64_t nNodesOffset = INT64_MAX;
1290 // Select time offset:
1291 int64_t GetTimeOffset()
1293 // If NTP and system clock are in agreement within 40 minutes, then use NTP.
1294 if (abs64(nNtpOffset) < 40 * 60)
1297 // If not, then choose between median peer time and system clock.
1298 if (abs64(nNodesOffset) < 70 * 60)
1299 return nNodesOffset;
1304 int64_t GetNodesOffset()
1306 return nNodesOffset;
1309 int64_t GetAdjustedTime()
1311 return GetTime() + GetTimeOffset();
1314 void AddTimeData(const CNetAddr& ip, int64_t nTime)
1316 int64_t nOffsetSample = nTime - GetTime();
1318 // Ignore duplicates
1319 static set<CNetAddr> setKnown;
1320 if (!setKnown.insert(ip).second)
1324 vTimeOffsets.input(nOffsetSample);
1325 printf("Added time data, samples %d, offset %+" PRId64 " (%+" PRId64 " minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample/60);
1326 if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1)
1328 int64_t nMedian = vTimeOffsets.median();
1329 std::vector<int64_t> vSorted = vTimeOffsets.sorted();
1330 // Only let other nodes change our time by so much
1331 if (abs64(nMedian) < 70 * 60)
1333 nNodesOffset = nMedian;
1337 nNodesOffset = INT64_MAX;
1342 bool fMatch = false;
1344 // If nobody has a time different than ours but within 5 minutes of ours, give a warning
1345 for (int64_t nOffset : vSorted)
1346 if (nOffset != 0 && abs64(nOffset) < 5 * 60)
1352 string strMessage = _("Warning: Please check that your computer's date and time are correct! If your clock is wrong NovaCoin will not work properly.");
1353 strMiscWarning = strMessage;
1354 printf("*** %s\n", strMessage.c_str());
1355 uiInterface.ThreadSafeMessageBox(strMessage+" ", string("NovaCoin"), CClientUIInterface::OK | CClientUIInterface::ICON_EXCLAMATION);
1360 for (int64_t n : vSorted)
1361 printf("%+" PRId64 " ", n);
1364 if (nNodesOffset != INT64_MAX)
1365 printf("nNodesOffset = %+" PRId64 " (%+" PRId64 " minutes)\n", nNodesOffset, nNodesOffset/60);
1369 string FormatVersion(int nVersion)
1371 if (nVersion%100 == 0)
1372 return strprintf("%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100);
1374 return strprintf("%d.%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100, nVersion%100);
1377 string FormatFullVersion()
1379 return CLIENT_BUILD;
1382 // Format the subversion field according to BIP 14 spec (https://en.bitcoin.it/wiki/BIP_0014)
1383 std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector<std::string>& comments)
1385 std::ostringstream ss;
1387 ss << name << ":" << FormatVersion(nClientVersion);
1388 if (!comments.empty())
1391 for (const auto& st : comments)
1394 if (st == comments.back()) break;
1404 boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate)
1406 namespace fs = boost::filesystem;
1408 char pszPath[MAX_PATH] = "";
1410 if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate))
1412 return fs::path(pszPath);
1415 printf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
1416 return fs::path("");
1420 void runCommand(std::string strCommand)
1422 int nErr = ::system(strCommand.c_str());
1424 printf("runCommand error: system(%s) returned %d\n", strCommand.c_str(), nErr);
1427 void RenameThread(const char* name)
1429 #if defined(PR_SET_NAME)
1430 // Only the first 15 characters are used (16 - NUL terminator)
1431 ::prctl(PR_SET_NAME, name, 0, 0, 0);
1432 #elif (defined(__FreeBSD__) || defined(__OpenBSD__))
1433 pthread_set_name_np(pthread_self(), name);
1434 #elif defined(__APPLE__)
1435 pthread_setname_np(name);
1437 // Prevent warnings for unused parameters...
1442 bool NewThread(void(*pfn)(void*), void* parg)
1446 boost::thread(pfn, parg); // thread detaches when out of scope
1447 } catch(boost::thread_resource_error &e) {
1448 printf("Error creating thread: %s\n", e.what());
1454 std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime)
1456 // std::locale takes ownership of the pointer
1457 std::locale loc(std::locale::classic(), new boost::posix_time::time_facet(pszFormat));
1458 std::stringstream ss;
1460 ss << boost::posix_time::from_time_t(nTime);