12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364 |
- // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
- #include "util.h"
- #include "sync.h"
- #include "strlcpy.h"
- #include "version.h"
- #include "ui_interface.h"
- #include <boost/algorithm/string/join.hpp>
- namespace boost {
- namespace program_options {
- std::string to_internal(const std::string&);
- }
- }
- #include <boost/program_options/detail/config_file.hpp>
- #include <boost/program_options/parsers.hpp>
- #include <boost/filesystem.hpp>
- #include <boost/filesystem/fstream.hpp>
- #include <boost/foreach.hpp>
- #include <boost/thread.hpp>
- #include <openssl/crypto.h>
- #include <openssl/rand.h>
- #include <stdarg.h>
- #ifdef WIN32
- #ifdef _MSC_VER
- #pragma warning(disable:4786)
- #pragma warning(disable:4804)
- #pragma warning(disable:4805)
- #pragma warning(disable:4717)
- #endif
- #ifdef _WIN32_WINNT
- #undef _WIN32_WINNT
- #endif
- #define _WIN32_WINNT 0x0501
- #ifdef _WIN32_IE
- #undef _WIN32_IE
- #endif
- #define _WIN32_IE 0x0501
- #define WIN32_LEAN_AND_MEAN 1
- #ifndef NOMINMAX
- #define NOMINMAX
- #endif
- #include <io.h> /* for _commit */
- #include "shlobj.h"
- #elif defined(__linux__)
- # include <sys/prctl.h>
- #endif
- #ifndef WIN32
- #include <execinfo.h>
- #endif
- using namespace std;
- map<string, string> mapArgs;
- map<string, vector<string> > mapMultiArgs;
- bool fDebug = false;
- bool fDebugNet = false;
- bool fPrintToConsole = false;
- bool fPrintToDebugger = false;
- bool fRequestShutdown = false;
- bool fShutdown = false;
- bool fDaemon = false;
- bool fServer = false;
- bool fCommandLine = false;
- string strMiscWarning;
- bool fTestNet = false;
- bool fNoListen = false;
- bool fLogTimestamps = false;
- CMedianFilter<int64> vTimeOffsets(200,0);
- bool fReopenDebugLog = false;
- // Init OpenSSL library multithreading support
- static CCriticalSection** ppmutexOpenSSL;
- void locking_callback(int mode, int i, const char* file, int line)
- {
- if (mode & CRYPTO_LOCK) {
- ENTER_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
- } else {
- LEAVE_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
- }
- }
- LockedPageManager LockedPageManager::instance;
- // Init
- class CInit
- {
- public:
- CInit()
- {
- // Init OpenSSL library multithreading support
- ppmutexOpenSSL = (CCriticalSection**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(CCriticalSection*));
- for (int i = 0; i < CRYPTO_num_locks(); i++)
- ppmutexOpenSSL[i] = new CCriticalSection();
- CRYPTO_set_locking_callback(locking_callback);
- #ifdef WIN32
- // Seed random number generator with screen scrape and other hardware sources
- RAND_screen();
- #endif
- // Seed random number generator with performance counter
- RandAddSeed();
- }
- ~CInit()
- {
- // Shutdown OpenSSL library multithreading support
- CRYPTO_set_locking_callback(NULL);
- for (int i = 0; i < CRYPTO_num_locks(); i++)
- delete ppmutexOpenSSL[i];
- OPENSSL_free(ppmutexOpenSSL);
- }
- }
- instance_of_cinit;
- void RandAddSeed()
- {
- // Seed with CPU performance counter
- int64 nCounter = GetPerformanceCounter();
- RAND_add(&nCounter, sizeof(nCounter), 1.5);
- memset(&nCounter, 0, sizeof(nCounter));
- }
- void RandAddSeedPerfmon()
- {
- RandAddSeed();
- // This can take up to 2 seconds, so only do it every 10 minutes
- static int64 nLastPerfmon;
- if (GetTime() < nLastPerfmon + 10 * 60)
- return;
- nLastPerfmon = GetTime();
- #ifdef WIN32
- // Don't need this on Linux, OpenSSL automatically uses /dev/urandom
- // Seed with the entire set of perfmon data
- unsigned char pdata[250000];
- memset(pdata, 0, sizeof(pdata));
- unsigned long nSize = sizeof(pdata);
- long ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, pdata, &nSize);
- RegCloseKey(HKEY_PERFORMANCE_DATA);
- if (ret == ERROR_SUCCESS)
- {
- RAND_add(pdata, nSize, nSize/100.0);
- memset(pdata, 0, nSize);
- printf("RandAddSeed() %lu bytes\n", nSize);
- }
- #endif
- }
- uint64 GetRand(uint64 nMax)
- {
- if (nMax == 0)
- return 0;
- // The range of the random source must be a multiple of the modulus
- // to give every possible output value an equal possibility
- uint64 nRange = (std::numeric_limits<uint64>::max() / nMax) * nMax;
- uint64 nRand = 0;
- do
- RAND_bytes((unsigned char*)&nRand, sizeof(nRand));
- while (nRand >= nRange);
- return (nRand % nMax);
- }
- int GetRandInt(int nMax)
- {
- return GetRand(nMax);
- }
- uint256 GetRandHash()
- {
- uint256 hash;
- RAND_bytes((unsigned char*)&hash, sizeof(hash));
- return hash;
- }
- static FILE* fileout = NULL;
- inline int OutputDebugStringF(const char* pszFormat, ...)
- {
- int ret = 0;
- if (fPrintToConsole)
- {
- // print to console
- va_list arg_ptr;
- va_start(arg_ptr, pszFormat);
- ret = vprintf(pszFormat, arg_ptr);
- va_end(arg_ptr);
- }
- else if (!fPrintToDebugger)
- {
- // print to debug.log
- if (!fileout)
- {
- boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
- fileout = fopen(pathDebug.string().c_str(), "a");
- if (fileout) setbuf(fileout, NULL); // unbuffered
- }
- if (fileout)
- {
- static bool fStartedNewLine = true;
- // This routine may be called by global destructors during shutdown.
- // Since the order of destruction of static/global objects is undefined,
- // allocate mutexDebugLog on the heap the first time this routine
- // is called to avoid crashes during shutdown.
- static boost::mutex* mutexDebugLog = NULL;
- if (mutexDebugLog == NULL) mutexDebugLog = new boost::mutex();
- boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
- // reopen the log file, if requested
- if (fReopenDebugLog) {
- fReopenDebugLog = false;
- boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
- if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
- setbuf(fileout, NULL); // unbuffered
- }
- // Debug print useful for profiling
- if (fLogTimestamps && fStartedNewLine)
- fprintf(fileout, "%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
- if (pszFormat[strlen(pszFormat) - 1] == '\n')
- fStartedNewLine = true;
- else
- fStartedNewLine = false;
- va_list arg_ptr;
- va_start(arg_ptr, pszFormat);
- ret = vfprintf(fileout, pszFormat, arg_ptr);
- va_end(arg_ptr);
- }
- }
- #ifdef WIN32
- if (fPrintToDebugger)
- {
- static CCriticalSection cs_OutputDebugStringF;
- // accumulate and output a line at a time
- {
- LOCK(cs_OutputDebugStringF);
- static std::string buffer;
- va_list arg_ptr;
- va_start(arg_ptr, pszFormat);
- buffer += vstrprintf(pszFormat, arg_ptr);
- va_end(arg_ptr);
- int line_start = 0, line_end;
- while((line_end = buffer.find('\n', line_start)) != -1)
- {
- OutputDebugStringA(buffer.substr(line_start, line_end - line_start).c_str());
- line_start = line_end + 1;
- }
- buffer.erase(0, line_start);
- }
- }
- #endif
- return ret;
- }
- string vstrprintf(const char *format, va_list ap)
- {
- char buffer[50000];
- char* p = buffer;
- int limit = sizeof(buffer);
- int ret;
- while (true)
- {
- va_list arg_ptr;
- va_copy(arg_ptr, ap);
- #ifdef WIN32
- ret = _vsnprintf(p, limit, format, arg_ptr);
- #else
- ret = vsnprintf(p, limit, format, arg_ptr);
- #endif
- va_end(arg_ptr);
- if (ret >= 0 && ret < limit)
- break;
- if (p != buffer)
- delete[] p;
- limit *= 2;
- p = new char[limit];
- if (p == NULL)
- throw std::bad_alloc();
- }
- string str(p, p+ret);
- if (p != buffer)
- delete[] p;
- return str;
- }
- string real_strprintf(const char *format, int dummy, ...)
- {
- va_list arg_ptr;
- va_start(arg_ptr, dummy);
- string str = vstrprintf(format, arg_ptr);
- va_end(arg_ptr);
- return str;
- }
- string real_strprintf(const std::string &format, int dummy, ...)
- {
- va_list arg_ptr;
- va_start(arg_ptr, dummy);
- string str = vstrprintf(format.c_str(), arg_ptr);
- va_end(arg_ptr);
- return str;
- }
- bool error(const char *format, ...)
- {
- va_list arg_ptr;
- va_start(arg_ptr, format);
- std::string str = vstrprintf(format, arg_ptr);
- va_end(arg_ptr);
- printf("ERROR: %s\n", str.c_str());
- return false;
- }
- void ParseString(const string& str, char c, vector<string>& v)
- {
- if (str.empty())
- return;
- string::size_type i1 = 0;
- string::size_type i2;
- while (true)
- {
- i2 = str.find(c, i1);
- if (i2 == str.npos)
- {
- v.push_back(str.substr(i1));
- return;
- }
- v.push_back(str.substr(i1, i2-i1));
- i1 = i2+1;
- }
- }
- string FormatMoney(int64 n, bool fPlus)
- {
- // Note: not using straight sprintf here because we do NOT want
- // localized number formatting.
- int64 n_abs = (n > 0 ? n : -n);
- int64 quotient = n_abs/COIN;
- int64 remainder = n_abs%COIN;
- string str = strprintf("%" PRI64d".%08" PRI64d, quotient, remainder);
- // Right-trim excess zeros before the decimal point:
- int nTrim = 0;
- for (int i = str.size()-1; (str[i] == '0' && isdigit(str[i-2])); --i)
- ++nTrim;
- if (nTrim)
- str.erase(str.size()-nTrim, nTrim);
- if (n < 0)
- str.insert((unsigned int)0, 1, '-');
- else if (fPlus && n > 0)
- str.insert((unsigned int)0, 1, '+');
- return str;
- }
- bool ParseMoney(const string& str, int64& nRet)
- {
- return ParseMoney(str.c_str(), nRet);
- }
- bool ParseMoney(const char* pszIn, int64& nRet)
- {
- string strWhole;
- int64 nUnits = 0;
- const char* p = pszIn;
- while (isspace(*p))
- p++;
- for (; *p; p++)
- {
- if (*p == '.')
- {
- p++;
- int64 nMult = CENT*10;
- while (isdigit(*p) && (nMult > 0))
- {
- nUnits += nMult * (*p++ - '0');
- nMult /= 10;
- }
- break;
- }
- if (isspace(*p))
- break;
- if (!isdigit(*p))
- return false;
- strWhole.insert(strWhole.end(), *p);
- }
- for (; *p; p++)
- if (!isspace(*p))
- return false;
- if (strWhole.size() > 10) // guard against 63 bit overflow
- return false;
- if (nUnits < 0 || nUnits > COIN)
- return false;
- int64 nWhole = atoi64(strWhole);
- int64 nValue = nWhole*COIN + nUnits;
- nRet = nValue;
- return true;
- }
- static const signed char phexdigit[256] =
- { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
- -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
- -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
- 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
- -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
- -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
- -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
- -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
- -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
- -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
- -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
- -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
- -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
- -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
- -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
- -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, };
- static const long hexGrid[] =
- {
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10-19
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 30-39
- -1, -1, -1, -1, -1, -1, -1, -1, 0, 1,
- 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, // 50-59
- -1, -1, -1, -1, -1, 10, 11, 12, 13, 14,
- 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 70-79
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, // 90-99
- 13, 14, 15, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 110-109
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 130-139
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 150-159
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 170-179
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 190-199
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 210-219
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 230-239
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1
- };
- long ConvertLong(const char* cHex)
- {
- long lSeed = 0;
- while (*cHex && lSeed >= 0)
- {
- lSeed = (lSeed << 4) | hexGrid[(uint8_t)*cHex++];
- }
- return lSeed;
- }
- bool IsHex(const string& str)
- {
- BOOST_FOREACH(unsigned char c, str)
- {
- if (phexdigit[c] < 0)
- return false;
- }
- return (str.size() > 0) && (str.size()%2 == 0);
- }
- vector<unsigned char> ParseHex(const char* psz)
- {
- // convert hex dump to vector
- vector<unsigned char> vch;
- while (true)
- {
- while (isspace(*psz))
- psz++;
- signed char c = phexdigit[(unsigned char)*psz++];
- if (c == (signed char)-1)
- break;
- unsigned char n = (c << 4);
- c = phexdigit[(unsigned char)*psz++];
- if (c == (signed char)-1)
- break;
- n |= c;
- vch.push_back(n);
- }
- return vch;
- }
- vector<unsigned char> ParseHex(const string& str)
- {
- return ParseHex(str.c_str());
- }
- static void InterpretNegativeSetting(string name, map<string, string>& mapSettingsRet)
- {
- // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
- if (name.find("-no") == 0)
- {
- std::string positive("-");
- positive.append(name.begin()+3, name.end());
- if (mapSettingsRet.count(positive) == 0)
- {
- bool value = !GetBoolArg(name);
- mapSettingsRet[positive] = (value ? "1" : "0");
- }
- }
- }
- void ParseParameters(int argc, const char* const argv[])
- {
- mapArgs.clear();
- mapMultiArgs.clear();
- for (int i = 1; i < argc; i++)
- {
- char psz[10000];
- strlcpy(psz, argv[i], sizeof(psz));
- char* pszValue = (char*)"";
- if (strchr(psz, '='))
- {
- pszValue = strchr(psz, '=');
- *pszValue++ = '\0';
- }
- #ifdef WIN32
- _strlwr(psz);
- if (psz[0] == '/')
- psz[0] = '-';
- #endif
- if (psz[0] != '-')
- break;
- mapArgs[psz] = pszValue;
- mapMultiArgs[psz].push_back(pszValue);
- }
- // New 0.6 features:
- BOOST_FOREACH(const PAIRTYPE(string,string)& entry, mapArgs)
- {
- string name = entry.first;
- // interpret --foo as -foo (as long as both are not set)
- if (name.find("--") == 0)
- {
- std::string singleDash(name.begin()+1, name.end());
- if (mapArgs.count(singleDash) == 0)
- mapArgs[singleDash] = entry.second;
- name = singleDash;
- }
- // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
- InterpretNegativeSetting(name, mapArgs);
- }
- }
- std::string GetArg(const std::string& strArg, const std::string& strDefault)
- {
- if (mapArgs.count(strArg))
- return mapArgs[strArg];
- return strDefault;
- }
- int64 GetArg(const std::string& strArg, int64 nDefault)
- {
- if (mapArgs.count(strArg))
- return atoi64(mapArgs[strArg]);
- return nDefault;
- }
- bool GetBoolArg(const std::string& strArg, bool fDefault)
- {
- if (mapArgs.count(strArg))
- {
- if (mapArgs[strArg].empty())
- return true;
- return (atoi(mapArgs[strArg]) != 0);
- }
- return fDefault;
- }
- bool SoftSetArg(const std::string& strArg, const std::string& strValue)
- {
- if (mapArgs.count(strArg))
- return false;
- mapArgs[strArg] = strValue;
- return true;
- }
- bool SoftSetBoolArg(const std::string& strArg, bool fValue)
- {
- if (fValue)
- return SoftSetArg(strArg, std::string("1"));
- else
- return SoftSetArg(strArg, std::string("0"));
- }
- string EncodeBase64(const unsigned char* pch, size_t len)
- {
- static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- string strRet="";
- strRet.reserve((len+2)/3*4);
- int mode=0, left=0;
- const unsigned char *pchEnd = pch+len;
- while (pch<pchEnd)
- {
- int enc = *(pch++);
- switch (mode)
- {
- case 0: // we have no bits
- strRet += pbase64[enc >> 2];
- left = (enc & 3) << 4;
- mode = 1;
- break;
- case 1: // we have two bits
- strRet += pbase64[left | (enc >> 4)];
- left = (enc & 15) << 2;
- mode = 2;
- break;
- case 2: // we have four bits
- strRet += pbase64[left | (enc >> 6)];
- strRet += pbase64[enc & 63];
- mode = 0;
- break;
- }
- }
- if (mode)
- {
- strRet += pbase64[left];
- strRet += '=';
- if (mode == 1)
- strRet += '=';
- }
- return strRet;
- }
- string EncodeBase64(const string& str)
- {
- return EncodeBase64((const unsigned char*)str.c_str(), str.size());
- }
- vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid)
- {
- static const int decode64_table[256] =
- {
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
- -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
- 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
- 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
- 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
- };
- if (pfInvalid)
- *pfInvalid = false;
- vector<unsigned char> vchRet;
- vchRet.reserve(strlen(p)*3/4);
- int mode = 0;
- int left = 0;
- while (1)
- {
- int dec = decode64_table[(unsigned char)*p];
- if (dec == -1) break;
- p++;
- switch (mode)
- {
- case 0: // we have no bits and get 6
- left = dec;
- mode = 1;
- break;
- case 1: // we have 6 bits and keep 4
- vchRet.push_back((left<<2) | (dec>>4));
- left = dec & 15;
- mode = 2;
- break;
- case 2: // we have 4 bits and get 6, we keep 2
- vchRet.push_back((left<<4) | (dec>>2));
- left = dec & 3;
- mode = 3;
- break;
- case 3: // we have 2 bits and get 6
- vchRet.push_back((left<<6) | dec);
- mode = 0;
- break;
- }
- }
- if (pfInvalid)
- switch (mode)
- {
- case 0: // 4n base64 characters processed: ok
- break;
- case 1: // 4n+1 base64 character processed: impossible
- *pfInvalid = true;
- break;
- case 2: // 4n+2 base64 characters processed: require '=='
- if (left || p[0] != '=' || p[1] != '=' || decode64_table[(unsigned char)p[2]] != -1)
- *pfInvalid = true;
- break;
- case 3: // 4n+3 base64 characters processed: require '='
- if (left || p[0] != '=' || decode64_table[(unsigned char)p[1]] != -1)
- *pfInvalid = true;
- break;
- }
- return vchRet;
- }
- string DecodeBase64(const string& str)
- {
- vector<unsigned char> vchRet = DecodeBase64(str.c_str());
- return string((const char*)&vchRet[0], vchRet.size());
- }
- string EncodeBase32(const unsigned char* pch, size_t len)
- {
- static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
- string strRet="";
- strRet.reserve((len+4)/5*8);
- int mode=0, left=0;
- const unsigned char *pchEnd = pch+len;
- while (pch<pchEnd)
- {
- int enc = *(pch++);
- switch (mode)
- {
- case 0: // we have no bits
- strRet += pbase32[enc >> 3];
- left = (enc & 7) << 2;
- mode = 1;
- break;
- case 1: // we have three bits
- strRet += pbase32[left | (enc >> 6)];
- strRet += pbase32[(enc >> 1) & 31];
- left = (enc & 1) << 4;
- mode = 2;
- break;
- case 2: // we have one bit
- strRet += pbase32[left | (enc >> 4)];
- left = (enc & 15) << 1;
- mode = 3;
- break;
- case 3: // we have four bits
- strRet += pbase32[left | (enc >> 7)];
- strRet += pbase32[(enc >> 2) & 31];
- left = (enc & 3) << 3;
- mode = 4;
- break;
- case 4: // we have two bits
- strRet += pbase32[left | (enc >> 5)];
- strRet += pbase32[enc & 31];
- mode = 0;
- }
- }
- static const int nPadding[5] = {0, 6, 4, 3, 1};
- if (mode)
- {
- strRet += pbase32[left];
- for (int n=0; n<nPadding[mode]; n++)
- strRet += '=';
- }
- return strRet;
- }
- string EncodeBase32(const string& str)
- {
- return EncodeBase32((const unsigned char*)str.c_str(), str.size());
- }
- vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid)
- {
- static const int decode32_table[256] =
- {
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
- 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
- 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
- 23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
- };
- if (pfInvalid)
- *pfInvalid = false;
- vector<unsigned char> vchRet;
- vchRet.reserve((strlen(p))*5/8);
- int mode = 0;
- int left = 0;
- while (1)
- {
- int dec = decode32_table[(unsigned char)*p];
- if (dec == -1) break;
- p++;
- switch (mode)
- {
- case 0: // we have no bits and get 5
- left = dec;
- mode = 1;
- break;
- case 1: // we have 5 bits and keep 2
- vchRet.push_back((left<<3) | (dec>>2));
- left = dec & 3;
- mode = 2;
- break;
- case 2: // we have 2 bits and keep 7
- left = left << 5 | dec;
- mode = 3;
- break;
- case 3: // we have 7 bits and keep 4
- vchRet.push_back((left<<1) | (dec>>4));
- left = dec & 15;
- mode = 4;
- break;
- case 4: // we have 4 bits, and keep 1
- vchRet.push_back((left<<4) | (dec>>1));
- left = dec & 1;
- mode = 5;
- break;
- case 5: // we have 1 bit, and keep 6
- left = left << 5 | dec;
- mode = 6;
- break;
- case 6: // we have 6 bits, and keep 3
- vchRet.push_back((left<<2) | (dec>>3));
- left = dec & 7;
- mode = 7;
- break;
- case 7: // we have 3 bits, and keep 0
- vchRet.push_back((left<<5) | dec);
- mode = 0;
- break;
- }
- }
- if (pfInvalid)
- switch (mode)
- {
- case 0: // 8n base32 characters processed: ok
- break;
- case 1: // 8n+1 base32 characters processed: impossible
- case 3: // +3
- case 6: // +6
- *pfInvalid = true;
- break;
- case 2: // 8n+2 base32 characters processed: require '======'
- if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || p[3] != '=' || p[4] != '=' || p[5] != '=' || decode32_table[(unsigned char)p[6]] != -1)
- *pfInvalid = true;
- break;
- case 4: // 8n+4 base32 characters processed: require '===='
- if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || p[3] != '=' || decode32_table[(unsigned char)p[4]] != -1)
- *pfInvalid = true;
- break;
- case 5: // 8n+5 base32 characters processed: require '==='
- if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || decode32_table[(unsigned char)p[3]] != -1)
- *pfInvalid = true;
- break;
- case 7: // 8n+7 base32 characters processed: require '='
- if (left || p[0] != '=' || decode32_table[(unsigned char)p[1]] != -1)
- *pfInvalid = true;
- break;
- }
- return vchRet;
- }
- string DecodeBase32(const string& str)
- {
- vector<unsigned char> vchRet = DecodeBase32(str.c_str());
- return string((const char*)&vchRet[0], vchRet.size());
- }
- bool WildcardMatch(const char* psz, const char* mask)
- {
- while (true)
- {
- switch (*mask)
- {
- case '\0':
- return (*psz == '\0');
- case '*':
- return WildcardMatch(psz, mask+1) || (*psz && WildcardMatch(psz+1, mask));
- case '?':
- if (*psz == '\0')
- return false;
- break;
- default:
- if (*psz != *mask)
- return false;
- break;
- }
- psz++;
- mask++;
- }
- }
- bool WildcardMatch(const string& str, const string& mask)
- {
- return WildcardMatch(str.c_str(), mask.c_str());
- }
- static std::string FormatException(std::exception* pex, const char* pszThread)
- {
- #ifdef WIN32
- char pszModule[MAX_PATH] = "";
- GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
- #else
- const char* pszModule = "ecoin";
- #endif
- if (pex)
- return strprintf(
- "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
- else
- return strprintf(
- "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
- }
- void LogException(std::exception* pex, const char* pszThread)
- {
- std::string message = FormatException(pex, pszThread);
- printf("\n%s", message.c_str());
- }
- void PrintException(std::exception* pex, const char* pszThread)
- {
- std::string message = FormatException(pex, pszThread);
- printf("\n\n************************\n%s\n", message.c_str());
- fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
- strMiscWarning = message;
- throw;
- }
- void LogStackTrace() {
- printf("\n\n******* exception encountered *******\n");
- if (fileout)
- {
- #ifndef WIN32
- void* pszBuffer[32];
- size_t size;
- size = backtrace(pszBuffer, 32);
- backtrace_symbols_fd(pszBuffer, size, fileno(fileout));
- #endif
- }
- }
- void PrintExceptionContinue(std::exception* pex, const char* pszThread)
- {
- std::string message = FormatException(pex, pszThread);
- printf("\n\n************************\n%s\n", message.c_str());
- fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
- strMiscWarning = message;
- }
- boost::filesystem::path GetDefaultDataDir()
- {
- namespace fs = boost::filesystem;
- // Windows < Vista: C:\Documents and Settings\Username\Application Data\Ecoin
- // Windows >= Vista: C:\Users\Username\AppData\Roaming\Ecoin
- // Mac: ~/Library/Application Support/Ecoin
- // Unix: ~/.ecoin
- #ifdef WIN32
- // Windows
- return GetSpecialFolderPath(CSIDL_APPDATA) / "Ecoin";
- #else
- fs::path pathRet;
- char* pszHome = getenv("HOME");
- if (pszHome == NULL || strlen(pszHome) == 0)
- pathRet = fs::path("/");
- else
- pathRet = fs::path(pszHome);
- #ifdef MAC_OSX
- // Mac
- pathRet /= "Library/Application Support";
- fs::create_directory(pathRet);
- return pathRet / "Ecoin";
- #else
- // Unix
- return pathRet / ".ecoin";
- #endif
- #endif
- }
- const boost::filesystem::path &GetDataDir(bool fNetSpecific)
- {
- namespace fs = boost::filesystem;
- static fs::path pathCached[2];
- static CCriticalSection csPathCached;
- static bool cachedPath[2] = {false, false};
- fs::path &path = pathCached[fNetSpecific];
- // This can be called during exceptions by printf, so we cache the
- // value so we don't have to do memory allocations after that.
- if (cachedPath[fNetSpecific])
- return path;
- LOCK(csPathCached);
- if (mapArgs.count("-datadir")) {
- path = fs::system_complete(mapArgs["-datadir"]);
- if (!fs::is_directory(path)) {
- path = "";
- return path;
- }
- } else {
- path = GetDefaultDataDir();
- }
- if (fNetSpecific && GetBoolArg("-testnet", false))
- path /= "testnet";
- fs::create_directory(path);
- cachedPath[fNetSpecific]=true;
- return path;
- }
- boost::filesystem::path GetConfigFile()
- {
- boost::filesystem::path pathConfigFile(GetArg("-conf", "ecoin.conf"));
- if (!pathConfigFile.is_complete()) pathConfigFile = GetDataDir(false) / pathConfigFile;
- return pathConfigFile;
- }
- void ReadConfigFile(map<string, string>& mapSettingsRet,
- map<string, vector<string> >& mapMultiSettingsRet)
- {
- boost::filesystem::ifstream streamConfig(GetConfigFile());
- if (!streamConfig.good())
- return; // No ecoin.conf file is OK
- set<string> setOptions;
- setOptions.insert("*");
- for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
- {
- // Don't overwrite existing settings so command line settings override ecoin.conf
- string strKey = string("-") + it->string_key;
- if (mapSettingsRet.count(strKey) == 0)
- {
- mapSettingsRet[strKey] = it->value[0];
- // interpret nofoo=1 as foo=0 (and nofoo=0 as foo=1) as long as foo not set)
- InterpretNegativeSetting(strKey, mapSettingsRet);
- }
- mapMultiSettingsRet[strKey].push_back(it->value[0]);
- }
- }
- boost::filesystem::path GetPidFile()
- {
- boost::filesystem::path pathPidFile(GetArg("-pid", "ecoind.pid"));
- if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
- return pathPidFile;
- }
- #ifndef WIN32
- void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
- {
- FILE* file = fopen(path.string().c_str(), "w");
- if (file)
- {
- fprintf(file, "%d\n", pid);
- fclose(file);
- }
- }
- #endif
- bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
- {
- #ifdef WIN32
- return MoveFileExA(src.string().c_str(), dest.string().c_str(),
- MOVEFILE_REPLACE_EXISTING);
- #else
- int rc = std::rename(src.string().c_str(), dest.string().c_str());
- return (rc == 0);
- #endif /* WIN32 */
- }
- void FileCommit(FILE *fileout)
- {
- fflush(fileout); // harmless if redundantly called
- #ifdef WIN32
- _commit(_fileno(fileout));
- #else
- fsync(fileno(fileout));
- #endif
- }
- int GetFilesize(FILE* file)
- {
- int nSavePos = ftell(file);
- int nFilesize = -1;
- if (fseek(file, 0, SEEK_END) == 0)
- nFilesize = ftell(file);
- fseek(file, nSavePos, SEEK_SET);
- return nFilesize;
- }
- void ShrinkDebugFile()
- {
- // Scroll debug.log if it's getting too big
- boost::filesystem::path pathLog = GetDataDir() / "debug.log";
- FILE* file = fopen(pathLog.string().c_str(), "r");
- if (file && GetFilesize(file) > 10 * 1000000)
- {
- // Restart the file with some of the end
- char pch[200000];
- fseek(file, -sizeof(pch), SEEK_END);
- int nBytes = fread(pch, 1, sizeof(pch), file);
- fclose(file);
- file = fopen(pathLog.string().c_str(), "w");
- if (file)
- {
- fwrite(pch, 1, nBytes, file);
- fclose(file);
- }
- }
- }
- static int64 nMockTime = 0; // For unit testing
- int64 GetTime()
- {
- if (nMockTime) return nMockTime;
- return time(NULL);
- }
- void SetMockTime(int64 nMockTimeIn)
- {
- nMockTime = nMockTimeIn;
- }
- static int64 nTimeOffset = 0;
- int64 GetTimeOffset()
- {
- return nTimeOffset;
- }
- int64 GetAdjustedTime()
- {
- return GetTime() + GetTimeOffset();
- }
- void AddTimeData(const CNetAddr& ip, int64 nTime)
- {
- int64 nOffsetSample = nTime - GetTime();
- // Ignore duplicates
- static set<CNetAddr> setKnown;
- if (!setKnown.insert(ip).second)
- return;
- // Add data
- vTimeOffsets.input(nOffsetSample);
- printf("Added time data, samples %d, offset %+" PRI64d" (%+" PRI64d" minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample/60);
- if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1)
- {
- int64 nMedian = vTimeOffsets.median();
- std::vector<int64> vSorted = vTimeOffsets.sorted();
- // Only let other nodes change our time by so much
- if (abs64(nMedian) < 70 * 60)
- {
- nTimeOffset = nMedian;
- }
- else
- {
- nTimeOffset = 0;
- static bool fDone;
- if (!fDone)
- {
- // If nobody has a time different than ours but within 5 minutes of ours, give a warning
- bool fMatch = false;
- BOOST_FOREACH(int64 nOffset, vSorted)
- if (nOffset != 0 && abs64(nOffset) < 5 * 60)
- fMatch = true;
- if (!fMatch)
- {
- fDone = true;
- string strMessage = _("Warning: Please check that your computer's date and time are correct! If your clock is wrong Ecoin will not work properly.");
- strMiscWarning = strMessage;
- printf("*** %s\n", strMessage.c_str());
- uiInterface.ThreadSafeMessageBox(strMessage+" ", string("Ecoin"), CClientUIInterface::OK | CClientUIInterface::ICON_EXCLAMATION);
- }
- }
- }
- if (fDebug) {
- BOOST_FOREACH(int64 n, vSorted)
- printf("%+" PRI64d" ", n);
- printf("| ");
- }
- printf("nTimeOffset = %+" PRI64d" (%+" PRI64d" minutes)\n", nTimeOffset, nTimeOffset/60);
- }
- }
- string FormatVersion(int nVersion)
- {
- if (nVersion%100 == 0)
- return strprintf("%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100);
- else
- return strprintf("%d.%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100, nVersion%100);
- }
- string FormatFullVersion()
- {
- return CLIENT_BUILD;
- }
- // Format the subversion field according to BIP 14 spec (https://en.ecoin.it/wiki/BIP_0014)
- std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector<std::string>& comments)
- {
- std::ostringstream ss;
- ss << "/";
- ss << name << ":" << FormatVersion(nClientVersion);
- if (!comments.empty())
- ss << "(" << boost::algorithm::join(comments, "; ") << ")";
- ss << "/";
- return ss.str();
- }
- #ifdef WIN32
- boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate)
- {
- namespace fs = boost::filesystem;
- char pszPath[MAX_PATH] = "";
- if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate))
- {
- return fs::path(pszPath);
- }
- printf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
- return fs::path("");
- }
- #endif
- void runCommand(std::string strCommand)
- {
- int nErr = ::system(strCommand.c_str());
- if (nErr)
- printf("runCommand error: system(%s) returned %d\n", strCommand.c_str(), nErr);
- }
- void RenameThread(const char* name)
- {
- #if defined(PR_SET_NAME)
- // Only the first 15 characters are used (16 - NUL terminator)
- ::prctl(PR_SET_NAME, name, 0, 0, 0);
- #elif 0 && (defined(__FreeBSD__) || defined(__OpenBSD__))
- pthread_set_name_np(pthread_self(), name);
- #else
- // Prevent warnings for unused parameters...
- (void)name;
- #endif
- }
- bool NewThread(void(*pfn)(void*), void* parg)
- {
- try
- {
- boost::thread(pfn, parg); // thread detaches when out of scope
- } catch(boost::thread_resource_error &e) {
- printf("Error creating thread: %s\n", e.what());
- return false;
- }
- return true;
- }
|