Merge with Bitcoin v0.6.3
[novacoin.git] / src / util.cpp
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Copyright (c) 2011-2012 The PPCoin developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6
7 #include "util.h"
8 #include "strlcpy.h"
9 #include "version.h"
10 #include "ui_interface.h"
11 #include <boost/algorithm/string/join.hpp>
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 #include <boost/interprocess/sync/interprocess_mutex.hpp>
28 #include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>
29 #include <boost/foreach.hpp>
30 #include <boost/thread.hpp>
31 #include <openssl/crypto.h>
32 #include <openssl/rand.h>
33
34 #ifdef WIN32
35 #ifdef _MSC_VER
36 #pragma warning(disable:4786)
37 #pragma warning(disable:4804)
38 #pragma warning(disable:4805)
39 #pragma warning(disable:4717)
40 #endif
41 #ifdef _WIN32_WINNT
42 #undef _WIN32_WINNT
43 #endif
44 #define _WIN32_WINNT 0x0501
45 #ifdef _WIN32_IE
46 #undef _WIN32_IE
47 #endif
48 #define _WIN32_IE 0x0400
49 #define WIN32_LEAN_AND_MEAN 1
50 #ifndef NOMINMAX
51 #define NOMINMAX
52 #endif
53 #include "shlobj.h"
54 #include "shlwapi.h"
55 #endif
56
57 #ifndef WIN32
58 #include <execinfo.h>
59 #endif
60
61 using namespace std;
62 using namespace boost;
63
64 map<string, string> mapArgs;
65 map<string, vector<string> > mapMultiArgs;
66 bool fDebug = false;
67 bool fPrintToConsole = false;
68 bool fPrintToDebugger = false;
69 bool fRequestShutdown = false;
70 bool fShutdown = false;
71 bool fDaemon = false;
72 bool fServer = false;
73 bool fCommandLine = false;
74 string strMiscWarning;
75 bool fTestNet = false;
76 bool fNoListen = false;
77 bool fLogTimestamps = false;
78 CMedianFilter<int64> vTimeOffsets(200,0);
79
80 // Init openssl library multithreading support
81 static boost::interprocess::interprocess_mutex** ppmutexOpenSSL;
82 void locking_callback(int mode, int i, const char* file, int line)
83 {
84     if (mode & CRYPTO_LOCK)
85         ppmutexOpenSSL[i]->lock();
86     else
87         ppmutexOpenSSL[i]->unlock();
88 }
89
90 // Init
91 class CInit
92 {
93 public:
94     CInit()
95     {
96         // Init openssl library multithreading support
97         ppmutexOpenSSL = (boost::interprocess::interprocess_mutex**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(boost::interprocess::interprocess_mutex*));
98         for (int i = 0; i < CRYPTO_num_locks(); i++)
99             ppmutexOpenSSL[i] = new boost::interprocess::interprocess_mutex();
100         CRYPTO_set_locking_callback(locking_callback);
101
102 #ifdef WIN32
103         // Seed random number generator with screen scrape and other hardware sources
104         RAND_screen();
105 #endif
106
107         // Seed random number generator with performance counter
108         RandAddSeed();
109     }
110     ~CInit()
111     {
112         // Shutdown openssl library multithreading support
113         CRYPTO_set_locking_callback(NULL);
114         for (int i = 0; i < CRYPTO_num_locks(); i++)
115             delete ppmutexOpenSSL[i];
116         OPENSSL_free(ppmutexOpenSSL);
117     }
118 }
119 instance_of_cinit;
120
121
122
123
124
125
126
127
128 void RandAddSeed()
129 {
130     // Seed with CPU performance counter
131     int64 nCounter = GetPerformanceCounter();
132     RAND_add(&nCounter, sizeof(nCounter), 1.5);
133     memset(&nCounter, 0, sizeof(nCounter));
134 }
135
136 void RandAddSeedPerfmon()
137 {
138     RandAddSeed();
139
140     // This can take up to 2 seconds, so only do it every 10 minutes
141     static int64 nLastPerfmon;
142     if (GetTime() < nLastPerfmon + 10 * 60)
143         return;
144     nLastPerfmon = GetTime();
145
146 #ifdef WIN32
147     // Don't need this on Linux, OpenSSL automatically uses /dev/urandom
148     // Seed with the entire set of perfmon data
149     unsigned char pdata[250000];
150     memset(pdata, 0, sizeof(pdata));
151     unsigned long nSize = sizeof(pdata);
152     long ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, pdata, &nSize);
153     RegCloseKey(HKEY_PERFORMANCE_DATA);
154     if (ret == ERROR_SUCCESS)
155     {
156         RAND_add(pdata, nSize, nSize/100.0);
157         memset(pdata, 0, nSize);
158         printf("%s RandAddSeed() %d bytes\n", DateTimeStrFormat("%x %H:%M", GetTime()).c_str(), nSize);
159     }
160 #endif
161 }
162
163 uint64 GetRand(uint64 nMax)
164 {
165     if (nMax == 0)
166         return 0;
167
168     // The range of the random source must be a multiple of the modulus
169     // to give every possible output value an equal possibility
170     uint64 nRange = (std::numeric_limits<uint64>::max() / nMax) * nMax;
171     uint64 nRand = 0;
172     do
173         RAND_bytes((unsigned char*)&nRand, sizeof(nRand));
174     while (nRand >= nRange);
175     return (nRand % nMax);
176 }
177
178 int GetRandInt(int nMax)
179 {
180     return GetRand(nMax);
181 }
182
183 uint256 GetRandHash()
184 {
185     uint256 hash;
186     RAND_bytes((unsigned char*)&hash, sizeof(hash));
187     return hash;
188 }
189
190
191
192
193
194
195
196
197 static FILE* fileout = NULL;
198
199 inline int OutputDebugStringF(const char* pszFormat, ...)
200 {
201     int ret = 0;
202     if (fPrintToConsole)
203     {
204         // print to console
205         va_list arg_ptr;
206         va_start(arg_ptr, pszFormat);
207         ret = vprintf(pszFormat, arg_ptr);
208         va_end(arg_ptr);
209     }
210     else
211     {
212         // print to debug.log
213         if (!fileout)
214         {
215             boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
216             fileout = fopen(pathDebug.string().c_str(), "a");
217             if (fileout) setbuf(fileout, NULL); // unbuffered
218         }
219         if (fileout)
220         {
221             static bool fStartedNewLine = true;
222             static boost::mutex mutexDebugLog;
223             boost::mutex::scoped_lock scoped_lock(mutexDebugLog);
224
225             // Debug print useful for profiling
226             if (fLogTimestamps && fStartedNewLine)
227                 fprintf(fileout, "%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
228             if (pszFormat[strlen(pszFormat) - 1] == '\n')
229                 fStartedNewLine = true;
230             else
231                 fStartedNewLine = false;
232
233             va_list arg_ptr;
234             va_start(arg_ptr, pszFormat);
235             ret = vfprintf(fileout, pszFormat, arg_ptr);
236             va_end(arg_ptr);
237         }
238     }
239
240 #ifdef WIN32
241     if (fPrintToDebugger)
242     {
243         static CCriticalSection cs_OutputDebugStringF;
244
245         // accumulate a line at a time
246         {
247             LOCK(cs_OutputDebugStringF);
248             static char pszBuffer[50000];
249             static char* pend;
250             if (pend == NULL)
251                 pend = pszBuffer;
252             va_list arg_ptr;
253             va_start(arg_ptr, pszFormat);
254             int limit = END(pszBuffer) - pend - 2;
255             int ret = _vsnprintf(pend, limit, pszFormat, arg_ptr);
256             va_end(arg_ptr);
257             if (ret < 0 || ret >= limit)
258             {
259                 pend = END(pszBuffer) - 2;
260                 *pend++ = '\n';
261             }
262             else
263                 pend += ret;
264             *pend = '\0';
265             char* p1 = pszBuffer;
266             char* p2;
267             while ((p2 = strchr(p1, '\n')))
268             {
269                 p2++;
270                 char c = *p2;
271                 *p2 = '\0';
272                 OutputDebugStringA(p1);
273                 *p2 = c;
274                 p1 = p2;
275             }
276             if (p1 != pszBuffer)
277                 memmove(pszBuffer, p1, pend - p1 + 1);
278             pend -= (p1 - pszBuffer);
279         }
280     }
281 #endif
282     return ret;
283 }
284
285
286 // Safer snprintf
287 //  - prints up to limit-1 characters
288 //  - output string is always null terminated even if limit reached
289 //  - return value is the number of characters actually printed
290 int my_snprintf(char* buffer, size_t limit, const char* format, ...)
291 {
292     if (limit == 0)
293         return 0;
294     va_list arg_ptr;
295     va_start(arg_ptr, format);
296     int ret = _vsnprintf(buffer, limit, format, arg_ptr);
297     va_end(arg_ptr);
298     if (ret < 0 || ret >= (int)limit)
299     {
300         ret = limit - 1;
301         buffer[limit-1] = 0;
302     }
303     return ret;
304 }
305
306 string real_strprintf(const std::string &format, int dummy, ...)
307 {
308     char buffer[50000];
309     char* p = buffer;
310     int limit = sizeof(buffer);
311     int ret;
312     loop
313     {
314         va_list arg_ptr;
315         va_start(arg_ptr, dummy);
316         ret = _vsnprintf(p, limit, format.c_str(), arg_ptr);
317         va_end(arg_ptr);
318         if (ret >= 0 && ret < limit)
319             break;
320         if (p != buffer)
321             delete[] p;
322         limit *= 2;
323         p = new char[limit];
324         if (p == NULL)
325             throw std::bad_alloc();
326     }
327     string str(p, p+ret);
328     if (p != buffer)
329         delete[] p;
330     return str;
331 }
332
333 bool error(const char *format, ...)
334 {
335     char buffer[50000];
336     int limit = sizeof(buffer);
337     va_list arg_ptr;
338     va_start(arg_ptr, format);
339     int ret = _vsnprintf(buffer, limit, format, arg_ptr);
340     va_end(arg_ptr);
341     if (ret < 0 || ret >= limit)
342     {
343         buffer[limit-1] = 0;
344     }
345     printf("ERROR: %s\n", buffer);
346     return false;
347 }
348
349
350 void ParseString(const string& str, char c, vector<string>& v)
351 {
352     if (str.empty())
353         return;
354     string::size_type i1 = 0;
355     string::size_type i2;
356     loop
357     {
358         i2 = str.find(c, i1);
359         if (i2 == str.npos)
360         {
361             v.push_back(str.substr(i1));
362             return;
363         }
364         v.push_back(str.substr(i1, i2-i1));
365         i1 = i2+1;
366     }
367 }
368
369
370 string FormatMoney(int64 n, bool fPlus)
371 {
372     // Note: not using straight sprintf here because we do NOT want
373     // localized number formatting.
374     int64 n_abs = (n > 0 ? n : -n);
375     int64 quotient = n_abs/COIN;
376     int64 remainder = n_abs%COIN;
377     string str = strprintf("%"PRI64d".%06"PRI64d, quotient, remainder);
378
379     // Right-trim excess 0's before the decimal point:
380     int nTrim = 0;
381     for (int i = str.size()-1; (str[i] == '0' && isdigit(str[i-2])); --i)
382         ++nTrim;
383     if (nTrim)
384         str.erase(str.size()-nTrim, nTrim);
385
386     if (n < 0)
387         str.insert((unsigned int)0, 1, '-');
388     else if (fPlus && n > 0)
389         str.insert((unsigned int)0, 1, '+');
390     return str;
391 }
392
393
394 bool ParseMoney(const string& str, int64& nRet)
395 {
396     return ParseMoney(str.c_str(), nRet);
397 }
398
399 bool ParseMoney(const char* pszIn, int64& nRet)
400 {
401     string strWhole;
402     int64 nUnits = 0;
403     const char* p = pszIn;
404     while (isspace(*p))
405         p++;
406     for (; *p; p++)
407     {
408         if (*p == '.')
409         {
410             p++;
411             int64 nMult = CENT*10;
412             while (isdigit(*p) && (nMult > 0))
413             {
414                 nUnits += nMult * (*p++ - '0');
415                 nMult /= 10;
416             }
417             break;
418         }
419         if (isspace(*p))
420             break;
421         if (!isdigit(*p))
422             return false;
423         strWhole.insert(strWhole.end(), *p);
424     }
425     for (; *p; p++)
426         if (!isspace(*p))
427             return false;
428     if (strWhole.size() > 10) // guard against 63 bit overflow
429         return false;
430     if (nUnits < 0 || nUnits > COIN)
431         return false;
432     int64 nWhole = atoi64(strWhole);
433     int64 nValue = nWhole*COIN + nUnits;
434
435     nRet = nValue;
436     return true;
437 }
438
439
440 static signed char phexdigit[256] =
441 { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
442   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
443   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
444   0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
445   -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
446   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
447   -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
448   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
449   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
450   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
451   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
452   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
453   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
454   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
455   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
456   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, };
457
458 bool IsHex(const string& str)
459 {
460     BOOST_FOREACH(unsigned char c, str)
461     {
462         if (phexdigit[c] < 0)
463             return false;
464     }
465     return (str.size() > 0) && (str.size()%2 == 0);
466 }
467
468 vector<unsigned char> ParseHex(const char* psz)
469 {
470     // convert hex dump to vector
471     vector<unsigned char> vch;
472     loop
473     {
474         while (isspace(*psz))
475             psz++;
476         signed char c = phexdigit[(unsigned char)*psz++];
477         if (c == (signed char)-1)
478             break;
479         unsigned char n = (c << 4);
480         c = phexdigit[(unsigned char)*psz++];
481         if (c == (signed char)-1)
482             break;
483         n |= c;
484         vch.push_back(n);
485     }
486     return vch;
487 }
488
489 vector<unsigned char> ParseHex(const string& str)
490 {
491     return ParseHex(str.c_str());
492 }
493
494 static void InterpretNegativeSetting(string name, map<string, string>& mapSettingsRet)
495 {
496     // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
497     if (name.find("-no") == 0)
498     {
499         std::string positive("-");
500         positive.append(name.begin()+3, name.end());
501         if (mapSettingsRet.count(positive) == 0)
502         {
503             bool value = !GetBoolArg(name);
504             mapSettingsRet[positive] = (value ? "1" : "0");
505         }
506     }
507 }
508
509 void ParseParameters(int argc, const char*const argv[])
510 {
511     mapArgs.clear();
512     mapMultiArgs.clear();
513     for (int i = 1; i < argc; i++)
514     {
515         char psz[10000];
516         strlcpy(psz, argv[i], sizeof(psz));
517         char* pszValue = (char*)"";
518         if (strchr(psz, '='))
519         {
520             pszValue = strchr(psz, '=');
521             *pszValue++ = '\0';
522         }
523         #ifdef WIN32
524         _strlwr(psz);
525         if (psz[0] == '/')
526             psz[0] = '-';
527         #endif
528         if (psz[0] != '-')
529             break;
530
531         mapArgs[psz] = pszValue;
532         mapMultiArgs[psz].push_back(pszValue);
533     }
534
535     // New 0.6 features:
536     BOOST_FOREACH(const PAIRTYPE(string,string)& entry, mapArgs)
537     {
538         string name = entry.first;
539
540         //  interpret --foo as -foo (as long as both are not set)
541         if (name.find("--") == 0)
542         {
543             std::string singleDash(name.begin()+1, name.end());
544             if (mapArgs.count(singleDash) == 0)
545                 mapArgs[singleDash] = entry.second;
546             name = singleDash;
547         }
548
549         // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
550         InterpretNegativeSetting(name, mapArgs);
551     }
552 }
553
554 std::string GetArg(const std::string& strArg, const std::string& strDefault)
555 {
556     if (mapArgs.count(strArg))
557         return mapArgs[strArg];
558     return strDefault;
559 }
560
561 int64 GetArg(const std::string& strArg, int64 nDefault)
562 {
563     if (mapArgs.count(strArg))
564         return atoi64(mapArgs[strArg]);
565     return nDefault;
566 }
567
568 bool GetBoolArg(const std::string& strArg, bool fDefault)
569 {
570     if (mapArgs.count(strArg))
571     {
572         if (mapArgs[strArg].empty())
573             return true;
574         return (atoi(mapArgs[strArg]) != 0);
575     }
576     return fDefault;
577 }
578
579 bool SoftSetArg(const std::string& strArg, const std::string& strValue)
580 {
581     if (mapArgs.count(strArg))
582         return false;
583     mapArgs[strArg] = strValue;
584     return true;
585 }
586
587 bool SoftSetBoolArg(const std::string& strArg, bool fValue)
588 {
589     if (fValue)
590         return SoftSetArg(strArg, std::string("1"));
591     else
592         return SoftSetArg(strArg, std::string("0"));
593 }
594
595
596 string EncodeBase64(const unsigned char* pch, size_t len)
597 {
598     static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
599
600     string strRet="";
601     strRet.reserve((len+2)/3*4);
602
603     int mode=0, left=0;
604     const unsigned char *pchEnd = pch+len;
605
606     while (pch<pchEnd)
607     {
608         int enc = *(pch++);
609         switch (mode)
610         {
611             case 0: // we have no bits
612                 strRet += pbase64[enc >> 2];
613                 left = (enc & 3) << 4;
614                 mode = 1;
615                 break;
616
617             case 1: // we have two bits
618                 strRet += pbase64[left | (enc >> 4)];
619                 left = (enc & 15) << 2;
620                 mode = 2;
621                 break;
622
623             case 2: // we have four bits
624                 strRet += pbase64[left | (enc >> 6)];
625                 strRet += pbase64[enc & 63];
626                 mode = 0;
627                 break;
628         }
629     }
630
631     if (mode)
632     {
633         strRet += pbase64[left];
634         strRet += '=';
635         if (mode == 1)
636             strRet += '=';
637     }
638
639     return strRet;
640 }
641
642 string EncodeBase64(const string& str)
643 {
644     return EncodeBase64((const unsigned char*)str.c_str(), str.size());
645 }
646
647 vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid)
648 {
649     static const int decode64_table[256] =
650     {
651         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
652         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
653         -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
654         -1, -1, -1, -1, -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
655         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
656         29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
657         49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
658         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
659         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
660         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
661         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
662         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
663         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
664     };
665
666     if (pfInvalid)
667         *pfInvalid = false;
668
669     vector<unsigned char> vchRet;
670     vchRet.reserve(strlen(p)*3/4);
671
672     int mode = 0;
673     int left = 0;
674
675     while (1)
676     {
677          int dec = decode64_table[(unsigned char)*p];
678          if (dec == -1) break;
679          p++;
680          switch (mode)
681          {
682              case 0: // we have no bits and get 6
683                  left = dec;
684                  mode = 1;
685                  break;
686
687               case 1: // we have 6 bits and keep 4
688                   vchRet.push_back((left<<2) | (dec>>4));
689                   left = dec & 15;
690                   mode = 2;
691                   break;
692
693              case 2: // we have 4 bits and get 6, we keep 2
694                  vchRet.push_back((left<<4) | (dec>>2));
695                  left = dec & 3;
696                  mode = 3;
697                  break;
698
699              case 3: // we have 2 bits and get 6
700                  vchRet.push_back((left<<6) | dec);
701                  mode = 0;
702                  break;
703          }
704     }
705
706     if (pfInvalid)
707         switch (mode)
708         {
709             case 0: // 4n base64 characters processed: ok
710                 break;
711
712             case 1: // 4n+1 base64 character processed: impossible
713                 *pfInvalid = true;
714                 break;
715
716             case 2: // 4n+2 base64 characters processed: require '=='
717                 if (left || p[0] != '=' || p[1] != '=' || decode64_table[(unsigned char)p[2]] != -1)
718                     *pfInvalid = true;
719                 break;
720
721             case 3: // 4n+3 base64 characters processed: require '='
722                 if (left || p[0] != '=' || decode64_table[(unsigned char)p[1]] != -1)
723                     *pfInvalid = true;
724                 break;
725         }
726
727     return vchRet;
728 }
729
730 string DecodeBase64(const string& str)
731 {
732     vector<unsigned char> vchRet = DecodeBase64(str.c_str());
733     return string((const char*)&vchRet[0], vchRet.size());
734 }
735
736
737 bool WildcardMatch(const char* psz, const char* mask)
738 {
739     loop
740     {
741         switch (*mask)
742         {
743         case '\0':
744             return (*psz == '\0');
745         case '*':
746             return WildcardMatch(psz, mask+1) || (*psz && WildcardMatch(psz+1, mask));
747         case '?':
748             if (*psz == '\0')
749                 return false;
750             break;
751         default:
752             if (*psz != *mask)
753                 return false;
754             break;
755         }
756         psz++;
757         mask++;
758     }
759 }
760
761 bool WildcardMatch(const string& str, const string& mask)
762 {
763     return WildcardMatch(str.c_str(), mask.c_str());
764 }
765
766
767
768
769
770
771
772
773 void FormatException(char* pszMessage, std::exception* pex, const char* pszThread)
774 {
775 #ifdef WIN32
776     char pszModule[MAX_PATH];
777     pszModule[0] = '\0';
778     GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
779 #else
780     const char* pszModule = "bitcoin";
781 #endif
782     if (pex)
783         snprintf(pszMessage, 1000,
784             "EXCEPTION: %s       \n%s       \n%s in %s       \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
785     else
786         snprintf(pszMessage, 1000,
787             "UNKNOWN EXCEPTION       \n%s in %s       \n", pszModule, pszThread);
788 }
789
790 void LogException(std::exception* pex, const char* pszThread)
791 {
792     char pszMessage[10000];
793     FormatException(pszMessage, pex, pszThread);
794     printf("\n%s", pszMessage);
795 }
796
797 void PrintException(std::exception* pex, const char* pszThread)
798 {
799     char pszMessage[10000];
800     FormatException(pszMessage, pex, pszThread);
801     printf("\n\n************************\n%s\n", pszMessage);
802     fprintf(stderr, "\n\n************************\n%s\n", pszMessage);
803     strMiscWarning = pszMessage;
804     throw;
805 }
806
807 void LogStackTrace() {
808     printf("\n\n******* exception encountered *******\n");
809     if (fileout)
810     {
811 #ifndef WIN32
812         void* pszBuffer[32];
813         size_t size;
814         size = backtrace(pszBuffer, 32);
815         backtrace_symbols_fd(pszBuffer, size, fileno(fileout));
816 #endif
817     }
818 }
819
820 void PrintExceptionContinue(std::exception* pex, const char* pszThread)
821 {
822     char pszMessage[10000];
823     FormatException(pszMessage, pex, pszThread);
824     printf("\n\n************************\n%s\n", pszMessage);
825     fprintf(stderr, "\n\n************************\n%s\n", pszMessage);
826     strMiscWarning = pszMessage;
827 }
828
829 #ifdef WIN32
830 boost::filesystem::path MyGetSpecialFolderPath(int nFolder, bool fCreate)
831 {
832     namespace fs = boost::filesystem;
833
834     char pszPath[MAX_PATH] = "";
835     if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate))
836     {
837         return fs::path(pszPath);
838     }
839     else if (nFolder == CSIDL_STARTUP)
840     {
841         return fs::path(getenv("USERPROFILE")) / "Start Menu" / "Programs" / "Startup";
842     }
843     else if (nFolder == CSIDL_APPDATA)
844     {
845         return fs::path(getenv("APPDATA"));
846     }
847     return fs::path("");
848 }
849 #endif
850
851 boost::filesystem::path GetDefaultDataDir()
852 {
853     namespace fs = boost::filesystem;
854
855     // Windows: C:\Documents and Settings\username\Application Data\PPCoin
856     // Mac: ~/Library/Application Support/PPCoin
857     // Unix: ~/.ppcoin
858 #ifdef WIN32
859     // Windows
860     return MyGetSpecialFolderPath(CSIDL_APPDATA, true) / "PPCoin";
861 #else
862     fs::path pathRet;
863     char* pszHome = getenv("HOME");
864     if (pszHome == NULL || strlen(pszHome) == 0)
865         pathRet = fs::path("/");
866     else
867         pathRet = fs::path(pszHome);
868 #ifdef MAC_OSX
869     // Mac
870     pathRet /= "Library/Application Support";
871     fs::create_directory(pathRet);
872     return pathRet / "PPCoin";
873 #else
874     // Unix
875     return pathRet / ".ppcoin";
876 #endif
877 #endif
878 }
879
880 const boost::filesystem::path &GetDataDir(bool fNetSpecific)
881 {
882     namespace fs = boost::filesystem;
883
884     static fs::path pathCached[2];
885     static CCriticalSection csPathCached;
886     static bool cachedPath[2] = {false, false};
887
888     fs::path &path = pathCached[fNetSpecific];
889
890     // This can be called during exceptions by printf, so we cache the
891     // value so we don't have to do memory allocations after that.
892     if (cachedPath[fNetSpecific])
893         return path;
894
895     LOCK(csPathCached);
896
897     if (mapArgs.count("-datadir")) {
898         path = fs::system_complete(mapArgs["-datadir"]);
899         if (!fs::is_directory(path)) {
900             path = "";
901             return path;
902         }
903     } else {
904         path = GetDefaultDataDir();
905     }
906     if (fNetSpecific && GetBoolArg("-testnet", false))
907         path /= "testnet";
908
909     fs::create_directory(path);
910
911     cachedPath[fNetSpecific]=true;
912     return path;
913 }
914
915 boost::filesystem::path GetConfigFile()
916 {
917     namespace fs = boost::filesystem;
918
919     fs::path pathConfigFile(GetArg("-conf", "ppcoin.conf"));
920     if (!pathConfigFile.is_complete()) pathConfigFile = GetDataDir(false) / pathConfigFile;
921     return pathConfigFile;
922 }
923
924 void ReadConfigFile(map<string, string>& mapSettingsRet,
925                     map<string, vector<string> >& mapMultiSettingsRet)
926 {
927     namespace fs = boost::filesystem;
928     namespace pod = boost::program_options::detail;
929
930     fs::ifstream streamConfig(GetConfigFile());
931     if (!streamConfig.good())
932         return; // No bitcoin.conf file is OK
933
934     set<string> setOptions;
935     setOptions.insert("*");
936
937     for (pod::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
938     {
939         // Don't overwrite existing settings so command line settings override bitcoin.conf
940         string strKey = string("-") + it->string_key;
941         if (mapSettingsRet.count(strKey) == 0)
942         {
943             mapSettingsRet[strKey] = it->value[0];
944             //  interpret nofoo=1 as foo=0 (and nofoo=0 as foo=1) as long as foo not set)
945             InterpretNegativeSetting(strKey, mapSettingsRet);
946         }
947         mapMultiSettingsRet[strKey].push_back(it->value[0]);
948     }
949 }
950
951 boost::filesystem::path GetPidFile()
952 {
953     namespace fs = boost::filesystem;
954
955     fs::path pathPidFile(GetArg("-pid", "ppcoind.pid"));
956     if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
957     return pathPidFile;
958 }
959
960 void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
961 {
962     FILE* file = fopen(path.string().c_str(), "w");
963     if (file)
964     {
965         fprintf(file, "%d\n", pid);
966         fclose(file);
967     }
968 }
969
970 int GetFilesize(FILE* file)
971 {
972     int nSavePos = ftell(file);
973     int nFilesize = -1;
974     if (fseek(file, 0, SEEK_END) == 0)
975         nFilesize = ftell(file);
976     fseek(file, nSavePos, SEEK_SET);
977     return nFilesize;
978 }
979
980 void ShrinkDebugFile()
981 {
982     // Scroll debug.log if it's getting too big
983     boost::filesystem::path pathLog = GetDataDir() / "debug.log";
984     FILE* file = fopen(pathLog.string().c_str(), "r");
985     if (file && GetFilesize(file) > 10 * 1000000)
986     {
987         // Restart the file with some of the end
988         char pch[200000];
989         fseek(file, -sizeof(pch), SEEK_END);
990         int nBytes = fread(pch, 1, sizeof(pch), file);
991         fclose(file);
992
993         file = fopen(pathLog.string().c_str(), "w");
994         if (file)
995         {
996             fwrite(pch, 1, nBytes, file);
997             fclose(file);
998         }
999     }
1000 }
1001
1002
1003
1004
1005
1006
1007
1008
1009 //
1010 // "Never go to sea with two chronometers; take one or three."
1011 // Our three time sources are:
1012 //  - System clock
1013 //  - Median of other nodes's clocks
1014 //  - The user (asking the user to fix the system clock if the first two disagree)
1015 //
1016 static int64 nMockTime = 0;  // For unit testing
1017
1018 int64 GetTime()
1019 {
1020     if (nMockTime) return nMockTime;
1021
1022     return time(NULL);
1023 }
1024
1025 void SetMockTime(int64 nMockTimeIn)
1026 {
1027     nMockTime = nMockTimeIn;
1028 }
1029
1030 static int64 nTimeOffset = 0;
1031
1032 int64 GetAdjustedTime()
1033 {
1034     return GetTime() + nTimeOffset;
1035 }
1036
1037 void AddTimeData(const CNetAddr& ip, int64 nTime)
1038 {
1039     int64 nOffsetSample = nTime - GetTime();
1040
1041     // Ignore duplicates
1042     static set<CNetAddr> setKnown;
1043     if (!setKnown.insert(ip).second)
1044         return;
1045
1046     // Add data
1047     vTimeOffsets.input(nOffsetSample);
1048     printf("Added time data, samples %d, offset %+"PRI64d" (%+"PRI64d" minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample/60);
1049     if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1)
1050     {
1051         int64 nMedian = vTimeOffsets.median();
1052         std::vector<int64> vSorted = vTimeOffsets.sorted();
1053         // Only let other nodes change our time by so much
1054         if (abs64(nMedian) < 70 * 60)
1055         {
1056             nTimeOffset = nMedian;
1057         }
1058         else
1059         {
1060             nTimeOffset = 0;
1061
1062             static bool fDone;
1063             if (!fDone)
1064             {
1065                 // If nobody has a time different than ours but within 5 minutes of ours, give a warning
1066                 bool fMatch = false;
1067                 BOOST_FOREACH(int64 nOffset, vSorted)
1068                     if (nOffset != 0 && abs64(nOffset) < 5 * 60)
1069                         fMatch = true;
1070
1071                 if (!fMatch)
1072                 {
1073                     fDone = true;
1074                     string strMessage = _("Warning: Please check that your computer's date and time are correct.  If your clock is wrong Bitcoin will not work properly.");
1075                     strMiscWarning = strMessage;
1076                     printf("*** %s\n", strMessage.c_str());
1077                     ThreadSafeMessageBox(strMessage+" ", string("Bitcoin"), wxOK | wxICON_EXCLAMATION);
1078                 }
1079             }
1080         }
1081         if (fDebug) {
1082             BOOST_FOREACH(int64 n, vSorted)
1083                 printf("%+"PRI64d"  ", n);
1084             printf("|  ");
1085         }
1086         printf("nTimeOffset = %+"PRI64d"  (%+"PRI64d" minutes)\n", nTimeOffset, nTimeOffset/60);
1087     }
1088 }
1089
1090
1091
1092
1093
1094
1095
1096
1097 string FormatVersion(int nVersion)
1098 {
1099     if (nVersion%100 == 0)
1100         return strprintf("%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100);
1101     else
1102         return strprintf("%d.%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100, nVersion%100);
1103 }
1104
1105 string FormatFullVersion()
1106 {
1107     return CLIENT_BUILD;
1108 }
1109
1110 // Format the subversion field according to BIP 14 spec (https://en.bitcoin.it/wiki/BIP_0014)
1111 std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector<std::string>& comments)
1112 {
1113     std::ostringstream ss;
1114     ss << "/";
1115     ss << name << ":" << FormatVersion(nClientVersion);
1116     if (!comments.empty())
1117         ss << "(" << boost::algorithm::join(comments, "; ") << ")";
1118     ss << "/";
1119     return ss.str();
1120 }
1121
1122 #ifdef WIN32
1123 boost::filesystem::path static StartupShortcutPath()
1124 {
1125     return MyGetSpecialFolderPath(CSIDL_STARTUP, true) / "Bitcoin.lnk";
1126 }
1127
1128 bool GetStartOnSystemStartup()
1129 {
1130     return filesystem::exists(StartupShortcutPath());
1131 }
1132
1133 bool SetStartOnSystemStartup(bool fAutoStart)
1134 {
1135     // If the shortcut exists already, remove it for updating
1136     boost::filesystem::remove(StartupShortcutPath());
1137
1138     if (fAutoStart)
1139     {
1140         CoInitialize(NULL);
1141
1142         // Get a pointer to the IShellLink interface.
1143         IShellLink* psl = NULL;
1144         HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL,
1145                                 CLSCTX_INPROC_SERVER, IID_IShellLink,
1146                                 reinterpret_cast<void**>(&psl));
1147
1148         if (SUCCEEDED(hres))
1149         {
1150             // Get the current executable path
1151             TCHAR pszExePath[MAX_PATH];
1152             GetModuleFileName(NULL, pszExePath, sizeof(pszExePath));
1153
1154             TCHAR pszArgs[5] = TEXT("-min");
1155
1156             // Set the path to the shortcut target
1157             psl->SetPath(pszExePath);
1158             PathRemoveFileSpec(pszExePath);
1159             psl->SetWorkingDirectory(pszExePath);
1160             psl->SetShowCmd(SW_SHOWMINNOACTIVE);
1161             psl->SetArguments(pszArgs);
1162
1163             // Query IShellLink for the IPersistFile interface for
1164             // saving the shortcut in persistent storage.
1165             IPersistFile* ppf = NULL;
1166             hres = psl->QueryInterface(IID_IPersistFile,
1167                                        reinterpret_cast<void**>(&ppf));
1168             if (SUCCEEDED(hres))
1169             {
1170                 WCHAR pwsz[MAX_PATH];
1171                 // Ensure that the string is ANSI.
1172                 MultiByteToWideChar(CP_ACP, 0, StartupShortcutPath().string().c_str(), -1, pwsz, MAX_PATH);
1173                 // Save the link by calling IPersistFile::Save.
1174                 hres = ppf->Save(pwsz, TRUE);
1175                 ppf->Release();
1176                 psl->Release();
1177                 CoUninitialize();
1178                 return true;
1179             }
1180             psl->Release();
1181         }
1182         CoUninitialize();
1183         return false;
1184     }
1185     return true;
1186 }
1187
1188 #elif defined(LINUX)
1189
1190 // Follow the Desktop Application Autostart Spec:
1191 //  http://standards.freedesktop.org/autostart-spec/autostart-spec-latest.html
1192
1193 boost::filesystem::path static GetAutostartDir()
1194 {
1195     namespace fs = boost::filesystem;
1196
1197     char* pszConfigHome = getenv("XDG_CONFIG_HOME");
1198     if (pszConfigHome) return fs::path(pszConfigHome) / "autostart";
1199     char* pszHome = getenv("HOME");
1200     if (pszHome) return fs::path(pszHome) / ".config" / "autostart";
1201     return fs::path();
1202 }
1203
1204 boost::filesystem::path static GetAutostartFilePath()
1205 {
1206     return GetAutostartDir() / "bitcoin.desktop";
1207 }
1208
1209 bool GetStartOnSystemStartup()
1210 {
1211     boost::filesystem::ifstream optionFile(GetAutostartFilePath());
1212     if (!optionFile.good())
1213         return false;
1214     // Scan through file for "Hidden=true":
1215     string line;
1216     while (!optionFile.eof())
1217     {
1218         getline(optionFile, line);
1219         if (line.find("Hidden") != string::npos &&
1220             line.find("true") != string::npos)
1221             return false;
1222 >>>>>>> bitcoin
1223     }
1224     optionFile.close();
1225
1226     return true;
1227 }
1228
1229 bool SetStartOnSystemStartup(bool fAutoStart)
1230 {
1231     if (!fAutoStart)
1232         boost::filesystem::remove(GetAutostartFilePath());
1233     else
1234     {
1235         char pszExePath[MAX_PATH+1];
1236         memset(pszExePath, 0, sizeof(pszExePath));
1237         if (readlink("/proc/self/exe", pszExePath, sizeof(pszExePath)-1) == -1)
1238             return false;
1239
1240         boost::filesystem::create_directories(GetAutostartDir());
1241
1242         boost::filesystem::ofstream optionFile(GetAutostartFilePath(), ios_base::out|ios_base::trunc);
1243         if (!optionFile.good())
1244             return false;
1245         // Write a bitcoin.desktop file to the autostart directory:
1246         optionFile << "[Desktop Entry]\n";
1247         optionFile << "Type=Application\n";
1248         optionFile << "Name=Bitcoin\n";
1249         optionFile << "Exec=" << pszExePath << " -min\n";
1250         optionFile << "Terminal=false\n";
1251         optionFile << "Hidden=false\n";
1252         optionFile.close();
1253     }
1254     return true;
1255 }
1256 #else
1257
1258 // TODO: OSX startup stuff; see:
1259 // http://developer.apple.com/mac/library/documentation/MacOSX/Conceptual/BPSystemStartup/Articles/CustomLogin.html
1260
1261 bool GetStartOnSystemStartup() { return false; }
1262 bool SetStartOnSystemStartup(bool fAutoStart) { return false; }
1263
1264 #endif
1265
1266
1267
1268 #ifdef DEBUG_LOCKORDER
1269 //
1270 // Early deadlock detection.
1271 // Problem being solved:
1272 //    Thread 1 locks  A, then B, then C
1273 //    Thread 2 locks  D, then C, then A
1274 //     --> may result in deadlock between the two threads, depending on when they run.
1275 // Solution implemented here:
1276 // Keep track of pairs of locks: (A before B), (A before C), etc.
1277 // Complain if any thread trys to lock in a different order.
1278 //
1279
1280 struct CLockLocation
1281 {
1282     CLockLocation(const char* pszName, const char* pszFile, int nLine)
1283     {
1284         mutexName = pszName;
1285         sourceFile = pszFile;
1286         sourceLine = nLine;
1287     }
1288
1289     std::string ToString() const
1290     {
1291         return mutexName+"  "+sourceFile+":"+itostr(sourceLine);
1292     }
1293
1294 private:
1295     std::string mutexName;
1296     std::string sourceFile;
1297     int sourceLine;
1298 };
1299
1300 typedef std::vector< std::pair<void*, CLockLocation> > LockStack;
1301
1302 static boost::interprocess::interprocess_mutex dd_mutex;
1303 static std::map<std::pair<void*, void*>, LockStack> lockorders;
1304 static boost::thread_specific_ptr<LockStack> lockstack;
1305
1306
1307 static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch, const LockStack& s1, const LockStack& s2)
1308 {
1309     printf("POTENTIAL DEADLOCK DETECTED\n");
1310     printf("Previous lock order was:\n");
1311     BOOST_FOREACH(const PAIRTYPE(void*, CLockLocation)& i, s2)
1312     {
1313         if (i.first == mismatch.first) printf(" (1)");
1314         if (i.first == mismatch.second) printf(" (2)");
1315         printf(" %s\n", i.second.ToString().c_str());
1316     }
1317     printf("Current lock order is:\n");
1318     BOOST_FOREACH(const PAIRTYPE(void*, CLockLocation)& i, s1)
1319     {
1320         if (i.first == mismatch.first) printf(" (1)");
1321         if (i.first == mismatch.second) printf(" (2)");
1322         printf(" %s\n", i.second.ToString().c_str());
1323     }
1324 }
1325
1326 static void push_lock(void* c, const CLockLocation& locklocation, bool fTry)
1327 {
1328     bool fOrderOK = true;
1329     if (lockstack.get() == NULL)
1330         lockstack.reset(new LockStack);
1331
1332     if (fDebug) printf("Locking: %s\n", locklocation.ToString().c_str());
1333     dd_mutex.lock();
1334
1335     (*lockstack).push_back(std::make_pair(c, locklocation));
1336
1337     if (!fTry) BOOST_FOREACH(const PAIRTYPE(void*, CLockLocation)& i, (*lockstack))
1338     {
1339         if (i.first == c) break;
1340
1341         std::pair<void*, void*> p1 = std::make_pair(i.first, c);
1342         if (lockorders.count(p1))
1343             continue;
1344         lockorders[p1] = (*lockstack);
1345
1346         std::pair<void*, void*> p2 = std::make_pair(c, i.first);
1347         if (lockorders.count(p2))
1348         {
1349             potential_deadlock_detected(p1, lockorders[p2], lockorders[p1]);
1350             break;
1351         }
1352     }
1353     dd_mutex.unlock();
1354 }
1355
1356 static void pop_lock()
1357 {
1358     if (fDebug) 
1359     {
1360         const CLockLocation& locklocation = (*lockstack).rbegin()->second;
1361         printf("Unlocked: %s\n", locklocation.ToString().c_str());
1362     }
1363     dd_mutex.lock();
1364     (*lockstack).pop_back();
1365     dd_mutex.unlock();
1366 }
1367
1368 void EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry)
1369 {
1370     push_lock(cs, CLockLocation(pszName, pszFile, nLine), fTry);
1371 }
1372
1373 void LeaveCritical()
1374 {
1375     pop_lock();
1376 }
1377
1378 #endif /* DEBUG_LOCKORDER */