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