c0acda79321d55c3de7af76b15ba80f455593d83
[novacoin.git] / src / util.cpp
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.
5
6 #include "util.h"
7 #include "sync.h"
8 #include "version.h"
9 #include "ui_interface.h"
10 #include <boost/algorithm/string/join.hpp>
11 #include <boost/algorithm/string/case_conv.hpp> // for to_lower()
12
13 // Work around clang compilation problem in Boost 1.46:
14 // /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
15 // See also: http://stackoverflow.com/questions/10020179/compilation-fail-in-boost-librairies-program-options
16 //           http://clang.debian.net/status.php?version=3.0&key=CANNOT_FIND_FUNCTION
17 namespace boost {
18     namespace program_options {
19         std::string to_internal(const std::string&);
20     }
21 }
22
23 #include <boost/program_options/detail/config_file.hpp>
24 #include <boost/program_options/parsers.hpp>
25 #include <boost/filesystem.hpp>
26 #include <boost/filesystem/fstream.hpp>
27
28 #include <boost/date_time/posix_time/posix_time.hpp>
29 #include <boost/thread.hpp>
30 #include <openssl/crypto.h>
31 #include <openssl/rand.h>
32
33 #ifdef WIN32
34 #ifdef _WIN32_WINNT
35 #undef _WIN32_WINNT
36 #endif
37 #define _WIN32_WINNT 0x0501
38 #ifdef _WIN32_IE
39 #undef _WIN32_IE
40 #endif
41 #define _WIN32_IE 0x0501
42 #define WIN32_LEAN_AND_MEAN 1
43 #ifndef NOMINMAX
44 #define NOMINMAX
45 #endif
46 #include <io.h> /* for _commit */
47 #include "shlobj.h"
48 #elif defined(__linux__)
49 # include <sys/prctl.h>
50 #endif
51
52 #if !defined(WIN32) && !defined(ANDROID)
53 #include <execinfo.h>
54 #endif
55
56
57 using namespace std;
58 namespace bt = boost::posix_time;
59
60 map<string, string> mapArgs;
61 map<string, vector<string> > mapMultiArgs;
62 bool fDebug = false;
63 bool fDebugNet = false;
64 bool fPrintToConsole = false;
65 bool fPrintToDebugger = false;
66 bool fRequestShutdown = false;
67 bool fShutdown = false;
68 bool fDaemon = false;
69 bool fServer = false;
70 bool fCommandLine = false;
71 string strMiscWarning;
72 bool fTestNet = false;
73 bool fNoListen = false;
74 bool fLogTimestamps = false;
75 CMedianFilter<int64_t> vTimeOffsets(200,0);
76 bool fReopenDebugLog = false;
77
78 // Extended DecodeDumpTime implementation, see this page for details:
79 // http://stackoverflow.com/questions/3786201/parsing-of-date-time-from-string-boost
80 const std::locale formats[] = {
81     std::locale(std::locale::classic(),new bt::time_input_facet("%Y-%m-%dT%H:%M:%SZ")),
82     std::locale(std::locale::classic(),new bt::time_input_facet("%Y-%m-%d %H:%M:%S")),
83     std::locale(std::locale::classic(),new bt::time_input_facet("%Y/%m/%d %H:%M:%S")),
84     std::locale(std::locale::classic(),new bt::time_input_facet("%d.%m.%Y %H:%M:%S")),
85     std::locale(std::locale::classic(),new bt::time_input_facet("%Y-%m-%d"))
86 };
87
88 const size_t formats_n = sizeof(formats)/sizeof(formats[0]);
89
90 std::time_t pt_to_time_t(const bt::ptime& pt)
91 {
92     bt::ptime timet_start(boost::gregorian::date(1970,1,1));
93     bt::time_duration diff = pt - timet_start;
94     return diff.ticks()/bt::time_duration::rep_type::ticks_per_second;
95 }
96
97 // Init OpenSSL library multithreading support
98 static CCriticalSection** ppmutexOpenSSL;
99 void locking_callback(int mode, int i, const char* file, int line)
100 {
101     if (mode & CRYPTO_LOCK) {
102         ENTER_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
103     } else {
104         LEAVE_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
105     }
106 }
107
108 LockedPageManager LockedPageManager::instance;
109
110 // Init
111 class CInit
112 {
113 public:
114     CInit()
115     {
116         // Init OpenSSL library multithreading support
117         ppmutexOpenSSL = (CCriticalSection**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(CCriticalSection*));
118         for (int i = 0; i < CRYPTO_num_locks(); i++)
119             ppmutexOpenSSL[i] = new CCriticalSection();
120         CRYPTO_set_locking_callback(locking_callback);
121
122 #ifdef WIN32
123         // Seed random number generator with screen scrape and other hardware sources
124         RAND_screen();
125 #endif
126
127         // Seed random number generator with performance counter
128         RandAddSeed();
129     }
130     ~CInit()
131     {
132         // Shutdown OpenSSL library multithreading support
133         CRYPTO_set_locking_callback(NULL);
134         for (int i = 0; i < CRYPTO_num_locks(); i++)
135             delete ppmutexOpenSSL[i];
136         OPENSSL_free(ppmutexOpenSSL);
137     }
138 }
139 instance_of_cinit;
140
141
142
143
144
145
146
147
148 void RandAddSeed()
149 {
150     // Seed with CPU performance counter
151     int64_t nCounter = GetPerformanceCounter();
152     RAND_add(&nCounter, sizeof(nCounter), 1.5);
153     memset(&nCounter, 0, sizeof(nCounter));
154 }
155
156 void RandAddSeedPerfmon()
157 {
158     RandAddSeed();
159
160     // This can take up to 2 seconds, so only do it every 10 minutes
161     static int64_t nLastPerfmon;
162     if (GetTime() < nLastPerfmon + 10 * 60)
163         return;
164     nLastPerfmon = GetTime();
165
166 #ifdef WIN32
167     // Don't need this on Linux, OpenSSL automatically uses /dev/urandom
168     // Seed with the entire set of perfmon data
169     unsigned char pdata[250000];
170     memset(pdata, 0, sizeof(pdata));
171     unsigned long nSize = sizeof(pdata);
172     long ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, pdata, &nSize);
173     RegCloseKey(HKEY_PERFORMANCE_DATA);
174     if (ret == ERROR_SUCCESS)
175     {
176         RAND_add(pdata, nSize, nSize/100.0);
177         OPENSSL_cleanse(pdata, nSize);
178         printf("RandAddSeed() %lu bytes\n", nSize);
179     }
180 #endif
181 }
182
183 uint64_t GetRand(uint64_t nMax)
184 {
185     if (nMax == 0)
186         return 0;
187
188     // The range of the random source must be a multiple of the modulus
189     // to give every possible output value an equal possibility
190     uint64_t nRange = (std::numeric_limits<uint64_t>::max() / nMax) * nMax;
191     uint64_t nRand = 0;
192     do
193         RAND_bytes((unsigned char*)&nRand, sizeof(nRand));
194     while (nRand >= nRange);
195     return (nRand % nMax);
196 }
197
198 int GetRandInt(int nMax)
199 {
200     return static_cast<int>(GetRand(nMax));
201 }
202
203 uint256 GetRandHash()
204 {
205     uint256 hash;
206     RAND_bytes((unsigned char*)&hash, sizeof(hash));
207     return hash;
208 }
209
210
211
212
213
214
215 static FILE* fileout = NULL;
216
217 inline int OutputDebugStringF(const char* pszFormat, ...)
218 {
219     int ret = 0;
220     if (fPrintToConsole)
221     {
222         // print to console
223         va_list arg_ptr;
224         va_start(arg_ptr, pszFormat);
225         ret = vprintf(pszFormat, arg_ptr);
226         va_end(arg_ptr);
227     }
228     else if (!fPrintToDebugger)
229     {
230         // print to debug.log
231
232         if (!fileout)
233         {
234             boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
235             fileout = fopen(pathDebug.string().c_str(), "a");
236             if (fileout) setbuf(fileout, NULL); // unbuffered
237         }
238         if (fileout)
239         {
240             static bool fStartedNewLine = true;
241
242             // This routine may be called by global destructors during shutdown.
243             // Since the order of destruction of static/global objects is undefined,
244             // allocate mutexDebugLog on the heap the first time this routine
245             // is called to avoid crashes during shutdown.
246             static boost::mutex* mutexDebugLog = NULL;
247             if (mutexDebugLog == NULL) mutexDebugLog = new boost::mutex();
248             boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
249
250             // reopen the log file, if requested
251             if (fReopenDebugLog) {
252                 fReopenDebugLog = false;
253                 boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
254                 if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
255                     setbuf(fileout, NULL); // unbuffered
256             }
257
258             // Debug print useful for profiling
259             if (fLogTimestamps && fStartedNewLine)
260                 fprintf(fileout, "%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
261             if (pszFormat[strlen(pszFormat) - 1] == '\n')
262                 fStartedNewLine = true;
263             else
264                 fStartedNewLine = false;
265
266             va_list arg_ptr;
267             va_start(arg_ptr, pszFormat);
268             ret = vfprintf(fileout, pszFormat, arg_ptr);
269             va_end(arg_ptr);
270         }
271     }
272
273 #ifdef WIN32
274     if (fPrintToDebugger)
275     {
276         static CCriticalSection cs_OutputDebugStringF;
277
278         // accumulate and output a line at a time
279         {
280             LOCK(cs_OutputDebugStringF);
281             static std::string buffer;
282
283             va_list arg_ptr;
284             va_start(arg_ptr, pszFormat);
285             buffer += vstrprintf(pszFormat, arg_ptr);
286             va_end(arg_ptr);
287
288             size_t line_start = 0, line_end;
289             while((line_end = buffer.find('\n', line_start)) != -1)
290             {
291                 OutputDebugStringA(buffer.substr(line_start, line_end - line_start).c_str());
292                 line_start = line_end + 1;
293             }
294             buffer.erase(0, line_start);
295         }
296     }
297 #endif
298     return ret;
299 }
300
301 string vstrprintf(const char *format, va_list ap)
302 {
303     char buffer[50000];
304     char* p = buffer;
305     int limit = sizeof(buffer);
306     int ret;
307     for ( ; ; )
308     {
309 #ifndef _MSC_VER
310         va_list arg_ptr;
311         va_copy(arg_ptr, ap);
312 #else
313         va_list arg_ptr = ap;
314 #endif
315 #ifdef WIN32
316         ret = _vsnprintf(p, limit, format, arg_ptr);
317 #else
318         ret = vsnprintf(p, limit, format, arg_ptr);
319 #endif
320         va_end(arg_ptr);
321         if (ret >= 0 && ret < limit)
322             break;
323         if (p != buffer)
324             delete[] p;
325         limit *= 2;
326         p = new(nothrow) char[limit];
327         if (p == NULL)
328             throw std::bad_alloc();
329     }
330     string str(p, p+ret);
331     if (p != buffer)
332         delete[] p;
333     return str;
334 }
335
336 string real_strprintf(const char *format, int dummy, ...)
337 {
338     va_list arg_ptr;
339     va_start(arg_ptr, dummy);
340     string str = vstrprintf(format, arg_ptr);
341     va_end(arg_ptr);
342     return str;
343 }
344
345 string real_strprintf(const std::string &format, int dummy, ...)
346 {
347     va_list arg_ptr;
348     va_start(arg_ptr, dummy);
349     string str = vstrprintf(format.c_str(), arg_ptr);
350     va_end(arg_ptr);
351     return str;
352 }
353
354 bool error(const char *format, ...)
355 {
356     va_list arg_ptr;
357     va_start(arg_ptr, format);
358     std::string str = vstrprintf(format, arg_ptr);
359     va_end(arg_ptr);
360     printf("ERROR: %s\n", str.c_str());
361     return false;
362 }
363
364
365 void ParseString(const string& str, char c, vector<string>& v)
366 {
367     if (str.empty())
368         return;
369     string::size_type i1 = 0;
370     string::size_type i2;
371     for ( ; ; )
372     {
373         i2 = str.find(c, i1);
374         if (i2 == str.npos)
375         {
376             v.push_back(str.substr(i1));
377             return;
378         }
379         v.push_back(str.substr(i1, i2-i1));
380         i1 = i2+1;
381     }
382 }
383
384
385 string FormatMoney(int64_t n, bool fPlus)
386 {
387     // Note: not using straight sprintf here because we do NOT want
388     // localized number formatting.
389     int64_t n_abs = (n > 0 ? n : -n);
390     int64_t quotient = n_abs/COIN;
391     int64_t remainder = n_abs%COIN;
392     string str = strprintf("%" PRId64 ".%06" PRId64, quotient, remainder);
393
394     // Right-trim excess zeros before the decimal point:
395     size_t nTrim = 0;
396     for (size_t i = str.size()-1; (str[i] == '0' && isdigit(str[i-2])); --i)
397         ++nTrim;
398     if (nTrim)
399         str.erase(str.size()-nTrim, nTrim);
400
401     if (n < 0)
402         str.insert(0u, 1, '-');
403     else if (fPlus && n > 0)
404         str.insert(0u, 1, '+');
405     return str;
406 }
407
408
409 bool ParseMoney(const string& str, int64_t& nRet)
410 {
411     return ParseMoney(str.c_str(), nRet);
412 }
413
414 bool ParseMoney(const char* pszIn, int64_t& nRet)
415 {
416     string strWhole;
417     int64_t nUnits = 0;
418     const char* p = pszIn;
419     while (isspace(*p))
420         p++;
421     for (; *p; p++)
422     {
423         if (*p == '.')
424         {
425             p++;
426             int64_t nMult = CENT*10;
427             while (isdigit(*p) && (nMult > 0))
428             {
429                 nUnits += nMult * (*p++ - '0');
430                 nMult /= 10;
431             }
432             break;
433         }
434         if (isspace(*p))
435             break;
436         if (!isdigit(*p))
437             return false;
438         strWhole.insert(strWhole.end(), *p);
439     }
440     for (; *p; p++)
441         if (!isspace(*p))
442             return false;
443     if (strWhole.size() > 10) // guard against 63 bit overflow
444         return false;
445     if (nUnits < 0 || nUnits > COIN)
446         return false;
447     int64_t nWhole = atoi64(strWhole);
448     int64_t nValue = nWhole*COIN + nUnits;
449
450     nRet = nValue;
451     return true;
452 }
453
454
455 static const signed char phexdigit[256] =
456 { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
457   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
458   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
459   0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
460   -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
461   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
462   -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
463   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
464   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
465   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
466   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
467   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
468   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
469   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
470   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
471   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, };
472
473 bool IsHex(const string& str)
474 {
475     for(unsigned char c :  str)
476     {
477         if (phexdigit[c] < 0)
478             return false;
479     }
480     return (str.size() > 0) && (str.size()%2 == 0);
481 }
482
483 vector<unsigned char> ParseHex(const char* psz)
484 {
485     // convert hex dump to vector
486     vector<unsigned char> vch;
487     for ( ; ; )
488     {
489         while (isspace(*psz))
490             psz++;
491         signed char c = phexdigit[(unsigned char)*psz++];
492         if (c == (signed char)-1)
493             break;
494         unsigned char n = (c << 4);
495         c = phexdigit[(unsigned char)*psz++];
496         if (c == (signed char)-1)
497             break;
498         n |= c;
499         vch.push_back(n);
500     }
501     return vch;
502 }
503
504 vector<unsigned char> ParseHex(const string& str)
505 {
506     return ParseHex(str.c_str());
507 }
508
509 static void InterpretNegativeSetting(string name, map<string, string>& mapSettingsRet)
510 {
511     // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
512     if (name.find("-no") == 0)
513     {
514         std::string positive("-");
515         positive.append(name.begin()+3, name.end());
516         if (mapSettingsRet.count(positive) == 0)
517         {
518             bool value = !GetBoolArg(name);
519             mapSettingsRet[positive] = (value ? "1" : "0");
520         }
521     }
522 }
523
524 void ParseParameters(int argc, const char* const argv[])
525 {
526     mapArgs.clear();
527     mapMultiArgs.clear();
528     for (int i = 1; i < argc; i++)
529     {
530         std::string str(argv[i]);
531         std::string strValue;
532         size_t is_index = str.find('=');
533         if (is_index != std::string::npos)
534         {
535             strValue = str.substr(is_index+1);
536             str = str.substr(0, is_index);
537         }
538 #ifdef WIN32
539         boost::to_lower(str);
540         if (str.compare(0,1, "/") == 0)
541             str = "-" + str.substr(1);
542 #endif
543         if (str[0] != '-')
544             break;
545
546         mapArgs[str] = strValue;
547         mapMultiArgs[str].push_back(strValue);
548     }
549
550     // New 0.6 features:
551     for(const auto& entry : mapArgs)
552     {
553         string name = entry.first;
554
555         //  interpret --foo as -foo (as long as both are not set)
556         if (name.find("--") == 0)
557         {
558             std::string singleDash(name.begin()+1, name.end());
559             if (mapArgs.count(singleDash) == 0)
560                 mapArgs[singleDash] = entry.second;
561             name = singleDash;
562         }
563
564         // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
565         InterpretNegativeSetting(name, mapArgs);
566     }
567 }
568
569 std::string GetArg(const std::string& strArg, const std::string& strDefault)
570 {
571     if (mapArgs.count(strArg))
572         return mapArgs[strArg];
573     return strDefault;
574 }
575
576 int64_t GetArg(const std::string& strArg, int64_t nDefault)
577 {
578     if (mapArgs.count(strArg))
579         return atoi64(mapArgs[strArg]);
580     return nDefault;
581 }
582
583 int32_t GetArgInt(const std::string& strArg, int32_t nDefault)
584 {
585     if (mapArgs.count(strArg))
586         return strtol(mapArgs[strArg]);
587     return nDefault;
588 }
589
590 uint32_t GetArgUInt(const std::string& strArg, uint32_t nDefault)
591 {
592     if (mapArgs.count(strArg))
593         return strtoul(mapArgs[strArg]);
594     return nDefault;
595 }
596
597 bool GetBoolArg(const std::string& strArg, bool fDefault)
598 {
599     if (mapArgs.count(strArg))
600     {
601         if (mapArgs[strArg].empty())
602             return true;
603         return (atoi(mapArgs[strArg]) != 0);
604     }
605     return fDefault;
606 }
607
608 bool SoftSetArg(const std::string& strArg, const std::string& strValue)
609 {
610     if (mapArgs.count(strArg) || mapMultiArgs.count(strArg))
611         return false;
612     mapArgs[strArg] = strValue;
613     mapMultiArgs[strArg].push_back(strValue);
614
615     return true;
616 }
617
618 bool SoftSetBoolArg(const std::string& strArg, bool fValue)
619 {
620     if (fValue)
621         return SoftSetArg(strArg, std::string("1"));
622     else
623         return SoftSetArg(strArg, std::string("0"));
624 }
625
626
627 string EncodeBase64(const unsigned char* pch, size_t len)
628 {
629     static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
630
631     string strRet="";
632     strRet.reserve((len+2)/3*4);
633
634     int mode=0, left=0;
635     const unsigned char *pchEnd = pch+len;
636
637     while (pch<pchEnd)
638     {
639         int enc = *(pch++);
640         switch (mode)
641         {
642             case 0: // we have no bits
643                 strRet += pbase64[enc >> 2];
644                 left = (enc & 3) << 4;
645                 mode = 1;
646                 break;
647
648             case 1: // we have two bits
649                 strRet += pbase64[left | (enc >> 4)];
650                 left = (enc & 15) << 2;
651                 mode = 2;
652                 break;
653
654             case 2: // we have four bits
655                 strRet += pbase64[left | (enc >> 6)];
656                 strRet += pbase64[enc & 63];
657                 mode = 0;
658                 break;
659         }
660     }
661
662     if (mode)
663     {
664         strRet += pbase64[left];
665         strRet += '=';
666         if (mode == 1)
667             strRet += '=';
668     }
669
670     return strRet;
671 }
672
673 string EncodeBase64(const string& str)
674 {
675     return EncodeBase64((const unsigned char*)str.c_str(), str.size());
676 }
677
678 vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid)
679 {
680     static const int decode64_table[256] =
681     {
682         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
683         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
684         -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
685         -1, -1, -1, -1, -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
686         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
687         29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
688         49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
689         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
690         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
691         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
692         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
693         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
694         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
695     };
696
697     if (pfInvalid)
698         *pfInvalid = false;
699
700     vector<unsigned char> vchRet;
701     vchRet.reserve(strlen(p)*3/4);
702
703     int mode = 0;
704     int left = 0;
705
706     for ( ; ; )
707     {
708          int dec = decode64_table[(unsigned char)*p];
709          if (dec == -1) break;
710          p++;
711          switch (mode)
712          {
713              case 0: // we have no bits and get 6
714                  left = dec;
715                  mode = 1;
716                  break;
717
718               case 1: // we have 6 bits and keep 4
719                   vchRet.push_back((left<<2) | (dec>>4));
720                   left = dec & 15;
721                   mode = 2;
722                   break;
723
724              case 2: // we have 4 bits and get 6, we keep 2
725                  vchRet.push_back((left<<4) | (dec>>2));
726                  left = dec & 3;
727                  mode = 3;
728                  break;
729
730              case 3: // we have 2 bits and get 6
731                  vchRet.push_back((left<<6) | dec);
732                  mode = 0;
733                  break;
734          }
735     }
736
737     if (pfInvalid)
738         switch (mode)
739         {
740             case 0: // 4n base64 characters processed: ok
741                 break;
742
743             case 1: // 4n+1 base64 character processed: impossible
744                 *pfInvalid = true;
745                 break;
746
747             case 2: // 4n+2 base64 characters processed: require '=='
748                 if (left || p[0] != '=' || p[1] != '=' || decode64_table[(unsigned char)p[2]] != -1)
749                     *pfInvalid = true;
750                 break;
751
752             case 3: // 4n+3 base64 characters processed: require '='
753                 if (left || p[0] != '=' || decode64_table[(unsigned char)p[1]] != -1)
754                     *pfInvalid = true;
755                 break;
756         }
757
758     return vchRet;
759 }
760
761 string DecodeBase64(const string& str)
762 {
763     vector<unsigned char> vchRet = DecodeBase64(str.c_str());
764     return string((const char*)&vchRet[0], vchRet.size());
765 }
766
767 string EncodeBase32(const unsigned char* pch, size_t len)
768 {
769     static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
770
771     string strRet="";
772     strRet.reserve((len+4)/5*8);
773
774     int mode=0, left=0;
775     const unsigned char *pchEnd = pch+len;
776
777     while (pch<pchEnd)
778     {
779         int enc = *(pch++);
780         switch (mode)
781         {
782             case 0: // we have no bits
783                 strRet += pbase32[enc >> 3];
784                 left = (enc & 7) << 2;
785                 mode = 1;
786                 break;
787
788             case 1: // we have three bits
789                 strRet += pbase32[left | (enc >> 6)];
790                 strRet += pbase32[(enc >> 1) & 31];
791                 left = (enc & 1) << 4;
792                 mode = 2;
793                 break;
794
795             case 2: // we have one bit
796                 strRet += pbase32[left | (enc >> 4)];
797                 left = (enc & 15) << 1;
798                 mode = 3;
799                 break;
800
801             case 3: // we have four bits
802                 strRet += pbase32[left | (enc >> 7)];
803                 strRet += pbase32[(enc >> 2) & 31];
804                 left = (enc & 3) << 3;
805                 mode = 4;
806                 break;
807
808             case 4: // we have two bits
809                 strRet += pbase32[left | (enc >> 5)];
810                 strRet += pbase32[enc & 31];
811                 mode = 0;
812         }
813     }
814
815     static const int nPadding[5] = {0, 6, 4, 3, 1};
816     if (mode)
817     {
818         strRet += pbase32[left];
819         for (int n=0; n<nPadding[mode]; n++)
820              strRet += '=';
821     }
822
823     return strRet;
824 }
825
826 string EncodeBase32(const string& str)
827 {
828     return EncodeBase32((const unsigned char*)str.c_str(), str.size());
829 }
830
831 vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid)
832 {
833     static const int decode32_table[256] =
834     {
835         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
836         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
837         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
838         -1, -1, -1, -1, -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
839         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1,  0,  1,  2,
840          3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
841         23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
842         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
843         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
844         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
845         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
846         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
847         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
848     };
849
850     if (pfInvalid)
851         *pfInvalid = false;
852
853     vector<unsigned char> vchRet;
854     vchRet.reserve((strlen(p))*5/8);
855
856     int mode = 0;
857     int left = 0;
858
859     for ( ; ; )
860     {
861          int dec = decode32_table[(unsigned char)*p];
862          if (dec == -1) break;
863          p++;
864          switch (mode)
865          {
866              case 0: // we have no bits and get 5
867                  left = dec;
868                  mode = 1;
869                  break;
870
871               case 1: // we have 5 bits and keep 2
872                   vchRet.push_back((left<<3) | (dec>>2));
873                   left = dec & 3;
874                   mode = 2;
875                   break;
876
877              case 2: // we have 2 bits and keep 7
878                  left = left << 5 | dec;
879                  mode = 3;
880                  break;
881
882              case 3: // we have 7 bits and keep 4
883                  vchRet.push_back((left<<1) | (dec>>4));
884                  left = dec & 15;
885                  mode = 4;
886                  break;
887
888              case 4: // we have 4 bits, and keep 1
889                  vchRet.push_back((left<<4) | (dec>>1));
890                  left = dec & 1;
891                  mode = 5;
892                  break;
893
894              case 5: // we have 1 bit, and keep 6
895                  left = left << 5 | dec;
896                  mode = 6;
897                  break;
898
899              case 6: // we have 6 bits, and keep 3
900                  vchRet.push_back((left<<2) | (dec>>3));
901                  left = dec & 7;
902                  mode = 7;
903                  break;
904
905              case 7: // we have 3 bits, and keep 0
906                  vchRet.push_back((left<<5) | dec);
907                  mode = 0;
908                  break;
909          }
910     }
911
912     if (pfInvalid)
913         switch (mode)
914         {
915             case 0: // 8n base32 characters processed: ok
916                 break;
917
918             case 1: // 8n+1 base32 characters processed: impossible
919             case 3: //   +3
920             case 6: //   +6
921                 *pfInvalid = true;
922                 break;
923
924             case 2: // 8n+2 base32 characters processed: require '======'
925                 if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || p[3] != '=' || p[4] != '=' || p[5] != '=' || decode32_table[(unsigned char)p[6]] != -1)
926                     *pfInvalid = true;
927                 break;
928
929             case 4: // 8n+4 base32 characters processed: require '===='
930                 if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || p[3] != '=' || decode32_table[(unsigned char)p[4]] != -1)
931                     *pfInvalid = true;
932                 break;
933
934             case 5: // 8n+5 base32 characters processed: require '==='
935                 if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || decode32_table[(unsigned char)p[3]] != -1)
936                     *pfInvalid = true;
937                 break;
938
939             case 7: // 8n+7 base32 characters processed: require '='
940                 if (left || p[0] != '=' || decode32_table[(unsigned char)p[1]] != -1)
941                     *pfInvalid = true;
942                 break;
943         }
944
945     return vchRet;
946 }
947
948 string DecodeBase32(const string& str)
949 {
950     vector<unsigned char> vchRet = DecodeBase32(str.c_str());
951     return string((const char*)&vchRet[0], vchRet.size());
952 }
953
954
955 int64_t DecodeDumpTime(const std::string& s)
956 {
957     bt::ptime pt;
958
959     for(size_t i=0; i<formats_n; ++i)
960     {
961         std::istringstream is(s);
962         is.imbue(formats[i]);
963         is >> pt;
964         if(pt != bt::ptime()) break;
965     }
966
967     return pt_to_time_t(pt);
968 }
969
970 std::string EncodeDumpTime(int64_t nTime) {
971     return DateTimeStrFormat("%Y-%m-%dT%H:%M:%SZ", nTime);
972 }
973
974 std::string EncodeDumpString(const std::string &str) {
975     std::stringstream ret;
976     for(unsigned char c :  str) {
977         if (c <= 32 || c >= 128 || c == '%') {
978             ret << '%' << HexStr(&c, &c + 1);
979         } else {
980             ret << c;
981         }
982     }
983     return ret.str();
984 }
985
986 std::string DecodeDumpString(const std::string &str) {
987     std::stringstream ret;
988     for (unsigned int pos = 0; pos < str.length(); pos++) {
989         unsigned char c = str[pos];
990         if (c == '%' && pos+2 < str.length()) {
991             c = (((str[pos+1]>>6)*9+((str[pos+1]-'0')&15)) << 4) | 
992                 ((str[pos+2]>>6)*9+((str[pos+2]-'0')&15));
993             pos += 2;
994         }
995         ret << c;
996     }
997     return ret.str();
998 }
999
1000 bool WildcardMatch(const char* psz, const char* mask)
1001 {
1002     for ( ; ; )
1003     {
1004         switch (*mask)
1005         {
1006         case '\0':
1007             return (*psz == '\0');
1008         case '*':
1009             return WildcardMatch(psz, mask+1) || (*psz && WildcardMatch(psz+1, mask));
1010         case '?':
1011             if (*psz == '\0')
1012                 return false;
1013             break;
1014         default:
1015             if (*psz != *mask)
1016                 return false;
1017             break;
1018         }
1019         psz++;
1020         mask++;
1021     }
1022 }
1023
1024 bool WildcardMatch(const string& str, const string& mask)
1025 {
1026     return WildcardMatch(str.c_str(), mask.c_str());
1027 }
1028
1029
1030
1031
1032
1033
1034
1035
1036 static std::string FormatException(std::exception* pex, const char* pszThread)
1037 {
1038 #ifdef WIN32
1039     char pszModule[MAX_PATH] = "";
1040     GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
1041 #else
1042     const char* pszModule = "novacoin";
1043 #endif
1044     if (pex)
1045         return strprintf(
1046             "EXCEPTION: %s       \n%s       \n%s in %s       \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
1047     else
1048         return strprintf(
1049             "UNKNOWN EXCEPTION       \n%s in %s       \n", pszModule, pszThread);
1050 }
1051
1052 void LogException(std::exception* pex, const char* pszThread)
1053 {
1054     std::string message = FormatException(pex, pszThread);
1055     printf("\n%s", message.c_str());
1056 }
1057
1058 void PrintException(std::exception* pex, const char* pszThread)
1059 {
1060     std::string message = FormatException(pex, pszThread);
1061     printf("\n\n************************\n%s\n", message.c_str());
1062     fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
1063     strMiscWarning = message;
1064     throw;
1065 }
1066
1067 void LogStackTrace() {
1068     printf("\n\n******* exception encountered *******\n");
1069     if (fileout)
1070     {
1071 #if !defined(WIN32) && !defined(ANDROID)
1072         void* pszBuffer[32];
1073         size_t size;
1074         size = backtrace(pszBuffer, 32);
1075         backtrace_symbols_fd(pszBuffer, size, fileno(fileout));
1076 #endif
1077     }
1078 }
1079
1080 void PrintExceptionContinue(std::exception* pex, const char* pszThread)
1081 {
1082     std::string message = FormatException(pex, pszThread);
1083     printf("\n\n************************\n%s\n", message.c_str());
1084     fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
1085     strMiscWarning = message;
1086 }
1087
1088 boost::filesystem::path GetDefaultDataDir()
1089 {
1090     namespace fs = boost::filesystem;
1091     // Windows < Vista: C:\Documents and Settings\Username\Application Data\NovaCoin
1092     // Windows >= Vista: C:\Users\Username\AppData\Roaming\NovaCoin
1093     // Mac: ~/Library/Application Support/NovaCoin
1094     // Unix: ~/.novacoin
1095 #ifdef WIN32
1096     // Windows
1097     return GetSpecialFolderPath(CSIDL_APPDATA) / "NovaCoin";
1098 #else
1099     fs::path pathRet;
1100     char* pszHome = getenv("HOME");
1101     if (pszHome == NULL || strlen(pszHome) == 0)
1102         pathRet = fs::path("/");
1103     else
1104         pathRet = fs::path(pszHome);
1105 #ifdef MAC_OSX
1106     // Mac
1107     pathRet /= "Library/Application Support";
1108     fs::create_directory(pathRet);
1109     return pathRet / "NovaCoin";
1110 #else
1111     // Unix
1112     return pathRet / ".novacoin";
1113 #endif
1114 #endif
1115 }
1116
1117 const boost::filesystem::path &GetDataDir(bool fNetSpecific)
1118 {
1119     namespace fs = boost::filesystem;
1120
1121     static fs::path pathCached[2];
1122     static CCriticalSection csPathCached;
1123     static bool cachedPath[2] = {false, false};
1124
1125     fs::path &path = pathCached[fNetSpecific];
1126
1127     // This can be called during exceptions by printf, so we cache the
1128     // value so we don't have to do memory allocations after that.
1129     if (cachedPath[fNetSpecific])
1130         return path;
1131
1132     LOCK(csPathCached);
1133
1134     if (mapArgs.count("-datadir")) {
1135         path = fs::system_complete(mapArgs["-datadir"]);
1136         if (!fs::is_directory(path)) {
1137             path = "";
1138             return path;
1139         }
1140     } else {
1141         path = GetDefaultDataDir();
1142     }
1143     if (fNetSpecific && GetBoolArg("-testnet", false))
1144         path /= "testnet2";
1145
1146     fs::create_directory(path);
1147
1148     cachedPath[fNetSpecific]=true;
1149     return path;
1150 }
1151
1152 string randomStrGen(int length) {
1153     static string charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
1154     string result;
1155     result.resize(length);
1156     for (int32_t i = 0; i < length; i++)
1157         result[i] = charset[rand() % charset.length()];
1158
1159     return result;
1160 }
1161
1162 void createConf()
1163 {
1164     srand(static_cast<unsigned int>(time(NULL)));
1165
1166     ofstream pConf;
1167 #if BOOST_FILESYSTEM_VERSION >= 3
1168     pConf.open(GetConfigFile().generic_string().c_str());
1169 #else
1170     pConf.open(GetConfigFile().string().c_str());
1171 #endif
1172     pConf << "rpcuser=user\nrpcpassword="
1173             + randomStrGen(15)
1174             + "\nrpcport=8344"
1175             + "\n#(0=off, 1=on) daemon - run in the background as a daemon and accept commands"
1176             + "\ndaemon=0"
1177             + "\n#(0=off, 1=on) server - accept command line and JSON-RPC commands"
1178             + "\nserver=0"
1179             + "\nrpcallowip=127.0.0.1";
1180     pConf.close();
1181 }
1182
1183 boost::filesystem::path GetConfigFile()
1184 {
1185     boost::filesystem::path pathConfigFile(GetArg("-conf", "novacoin.conf"));
1186     if (!pathConfigFile.is_complete()) pathConfigFile = GetDataDir(false) / pathConfigFile;
1187     return pathConfigFile;
1188 }
1189
1190 void ReadConfigFile(map<string, string>& mapSettingsRet,
1191                     map<string, vector<string> >& mapMultiSettingsRet)
1192 {
1193     boost::filesystem::ifstream streamConfig(GetConfigFile());
1194     if (!streamConfig.good())
1195     {
1196         createConf();
1197         new(&streamConfig) boost::filesystem::ifstream(GetConfigFile());
1198         if(!streamConfig.good())
1199             return;
1200     }
1201
1202     set<string> setOptions;
1203     setOptions.insert("*");
1204
1205     for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
1206     {
1207         // Don't overwrite existing settings so command line settings override bitcoin.conf
1208         string strKey = string("-") + it->string_key;
1209         if (mapSettingsRet.count(strKey) == 0)
1210         {
1211             mapSettingsRet[strKey] = it->value[0];
1212             // interpret nofoo=1 as foo=0 (and nofoo=0 as foo=1) as long as foo not set)
1213             InterpretNegativeSetting(strKey, mapSettingsRet);
1214         }
1215         mapMultiSettingsRet[strKey].push_back(it->value[0]);
1216     }
1217 }
1218
1219 boost::filesystem::path GetPidFile()
1220 {
1221     boost::filesystem::path pathPidFile(GetArg("-pid", "novacoind.pid"));
1222     if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
1223     return pathPidFile;
1224 }
1225
1226 #ifndef WIN32
1227 void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
1228 {
1229     FILE* file = fopen(path.string().c_str(), "w");
1230     if (file)
1231     {
1232         fprintf(file, "%d\n", pid);
1233         fclose(file);
1234     }
1235 }
1236 #endif
1237
1238 bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
1239 {
1240 #ifdef WIN32
1241     return MoveFileExA(src.string().c_str(), dest.string().c_str(),
1242                        MOVEFILE_REPLACE_EXISTING) != 0;
1243 #else
1244     int rc = std::rename(src.string().c_str(), dest.string().c_str());
1245     return (rc == 0);
1246 #endif /* WIN32 */
1247 }
1248
1249 void FileCommit(FILE *fileout)
1250 {
1251     fflush(fileout);                // harmless if redundantly called
1252 #ifdef WIN32
1253     _commit(_fileno(fileout));
1254 #else
1255     fsync(fileno(fileout));
1256 #endif
1257 }
1258
1259 int GetFilesize(FILE* file)
1260 {
1261     int nSavePos = ftell(file);
1262     int nFilesize = -1;
1263     if (fseek(file, 0, SEEK_END) == 0)
1264         nFilesize = ftell(file);
1265     fseek(file, nSavePos, SEEK_SET);
1266     return nFilesize;
1267 }
1268
1269 void ShrinkDebugFile()
1270 {
1271     // Scroll debug.log if it's getting too big
1272     boost::filesystem::path pathLog = GetDataDir() / "debug.log";
1273     FILE* file = fopen(pathLog.string().c_str(), "r");
1274     if (file && GetFilesize(file) > 10 * 1000000)
1275     {
1276         // Restart the file with some of the end
1277         try {
1278             vector<char>* vBuf = new vector <char>(200000, 0);
1279             fseek(file, -((long)(vBuf->size())), SEEK_END);
1280             size_t nBytes = fread(&vBuf->operator[](0), 1, vBuf->size(), file);
1281             fclose(file);
1282             file = fopen(pathLog.string().c_str(), "w");
1283             if (file)
1284             {
1285                 fwrite(&vBuf->operator[](0), 1, nBytes, file);
1286                 fclose(file);
1287             }
1288             delete vBuf;
1289         }
1290         catch (const bad_alloc& e) {
1291             // Bad things happen - no free memory in heap at program startup
1292             fclose(file);
1293             printf("Warning: %s in %s:%d\n ShrinkDebugFile failed - debug.log expands further", e.what(), __FILE__, __LINE__);
1294         }
1295     }
1296 }
1297
1298
1299
1300
1301
1302
1303
1304
1305 //
1306 // "Never go to sea with two chronometers; take one or three."
1307 // Our three time sources are:
1308 //  - System clock
1309 //  - Median of other nodes clocks
1310 //  - The user (asking the user to fix the system clock if the first two disagree)
1311 //
1312
1313 // System clock
1314 int64_t GetTime()
1315 {
1316     int64_t now = time(NULL);
1317     assert(now > 0);
1318     return now;
1319 }
1320
1321 // Trusted NTP offset or median of NTP samples.
1322 extern int64_t nNtpOffset;
1323
1324 // Median of time samples given by other nodes.
1325 static int64_t nNodesOffset = INT64_MAX;
1326
1327 // Select time offset:
1328 int64_t GetTimeOffset()
1329 {
1330     // If NTP and system clock are in agreement within 40 minutes, then use NTP.
1331     if (abs64(nNtpOffset) < 40 * 60)
1332         return nNtpOffset;
1333
1334     // If not, then choose between median peer time and system clock.
1335     if (abs64(nNodesOffset) < 70 * 60)
1336         return nNodesOffset;
1337
1338     return 0;
1339 }
1340
1341 int64_t GetNodesOffset()
1342 {
1343         return nNodesOffset;
1344 }
1345
1346 int64_t GetAdjustedTime()
1347 {
1348     return GetTime() + GetTimeOffset();
1349 }
1350
1351 void AddTimeData(const CNetAddr& ip, int64_t nTime)
1352 {
1353     int64_t nOffsetSample = nTime - GetTime();
1354
1355     // Ignore duplicates
1356     static set<CNetAddr> setKnown;
1357     if (!setKnown.insert(ip).second)
1358         return;
1359
1360     // Add data
1361     vTimeOffsets.input(nOffsetSample);
1362     printf("Added time data, samples %d, offset %+" PRId64 " (%+" PRId64 " minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample/60);
1363     if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1)
1364     {
1365         int64_t nMedian = vTimeOffsets.median();
1366         std::vector<int64_t> vSorted = vTimeOffsets.sorted();
1367         // Only let other nodes change our time by so much
1368         if (abs64(nMedian) < 70 * 60)
1369         {
1370             nNodesOffset = nMedian;
1371         }
1372         else
1373         {
1374             nNodesOffset = INT64_MAX;
1375
1376             static bool fDone;
1377             if (!fDone)
1378             {
1379                 bool fMatch = false;
1380
1381                 // If nobody has a time different than ours but within 5 minutes of ours, give a warning
1382                 for(int64_t nOffset :  vSorted)
1383                     if (nOffset != 0 && abs64(nOffset) < 5 * 60)
1384                         fMatch = true;
1385
1386                 if (!fMatch)
1387                 {
1388                     fDone = true;
1389                     string strMessage = _("Warning: Please check that your computer's date and time are correct! If your clock is wrong NovaCoin will not work properly.");
1390                     strMiscWarning = strMessage;
1391                     printf("*** %s\n", strMessage.c_str());
1392                     uiInterface.ThreadSafeMessageBox(strMessage+" ", string("NovaCoin"), CClientUIInterface::OK | CClientUIInterface::ICON_EXCLAMATION);
1393                 }
1394             }
1395         }
1396         if (fDebug) {
1397             for(int64_t n :  vSorted)
1398                 printf("%+" PRId64 "  ", n);
1399             printf("|  ");
1400         }
1401         if (nNodesOffset != INT64_MAX)
1402             printf("nNodesOffset = %+" PRId64 "  (%+" PRId64 " minutes)\n", nNodesOffset, nNodesOffset/60);
1403     }
1404 }
1405
1406 string FormatVersion(int nVersion)
1407 {
1408     if (nVersion%100 == 0)
1409         return strprintf("%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100);
1410     else
1411         return strprintf("%d.%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100, nVersion%100);
1412 }
1413
1414 string FormatFullVersion()
1415 {
1416     return CLIENT_BUILD;
1417 }
1418
1419 // Format the subversion field according to BIP 14 spec (https://en.bitcoin.it/wiki/BIP_0014)
1420 std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector<std::string>& comments)
1421 {
1422     std::ostringstream ss;
1423     ss << "/";
1424     ss << name << ":" << FormatVersion(nClientVersion);
1425     if (!comments.empty())
1426         ss << "(" << boost::algorithm::join(comments, "; ") << ")";
1427     ss << "/";
1428     return ss.str();
1429 }
1430
1431 #ifdef WIN32
1432 boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate)
1433 {
1434     namespace fs = boost::filesystem;
1435
1436     char pszPath[MAX_PATH] = "";
1437
1438     if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate))
1439     {
1440         return fs::path(pszPath);
1441     }
1442
1443     printf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
1444     return fs::path("");
1445 }
1446 #endif
1447
1448 void runCommand(std::string strCommand)
1449 {
1450     int nErr = ::system(strCommand.c_str());
1451     if (nErr)
1452         printf("runCommand error: system(%s) returned %d\n", strCommand.c_str(), nErr);
1453 }
1454
1455 void RenameThread(const char* name)
1456 {
1457 #if defined(PR_SET_NAME)
1458     // Only the first 15 characters are used (16 - NUL terminator)
1459     ::prctl(PR_SET_NAME, name, 0, 0, 0);
1460 #elif 0 && (defined(__FreeBSD__) || defined(__OpenBSD__))
1461     // TODO: This is currently disabled because it needs to be verified to work
1462     //       on FreeBSD or OpenBSD first. When verified the '0 &&' part can be
1463     //       removed.
1464     pthread_set_name_np(pthread_self(), name);
1465
1466 // This is XCode 10.6-and-later; bring back if we drop 10.5 support:
1467 // #elif defined(MAC_OSX)
1468 //    pthread_setname_np(name);
1469
1470 #else
1471     // Prevent warnings for unused parameters...
1472     (void)name;
1473 #endif
1474 }
1475
1476 bool NewThread(void(*pfn)(void*), void* parg)
1477 {
1478     try
1479     {
1480         boost::thread(pfn, parg); // thread detaches when out of scope
1481     } catch(boost::thread_resource_error &e) {
1482         printf("Error creating thread: %s\n", e.what());
1483         return false;
1484     }
1485     return true;
1486 }
1487
1488 std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime)
1489 {
1490     // std::locale takes ownership of the pointer
1491     std::locale loc(std::locale::classic(), new boost::posix_time::time_facet(pszFormat));
1492     std::stringstream ss;
1493     ss.imbue(loc);
1494     ss << boost::posix_time::from_time_t(nTime);
1495     return ss.str();
1496 }