util.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #include "util.h"
  3. #include "sync.h"
  4. #include "strlcpy.h"
  5. #include "version.h"
  6. #include "ui_interface.h"
  7. #include <boost/algorithm/string/join.hpp>
  8. namespace boost {
  9. namespace program_options {
  10. std::string to_internal(const std::string&);
  11. }
  12. }
  13. #include <boost/program_options/detail/config_file.hpp>
  14. #include <boost/program_options/parsers.hpp>
  15. #include <boost/filesystem.hpp>
  16. #include <boost/filesystem/fstream.hpp>
  17. #include <boost/foreach.hpp>
  18. #include <boost/thread.hpp>
  19. #include <openssl/crypto.h>
  20. #include <openssl/rand.h>
  21. #include <stdarg.h>
  22. #ifdef WIN32
  23. #ifdef _MSC_VER
  24. #pragma warning(disable:4786)
  25. #pragma warning(disable:4804)
  26. #pragma warning(disable:4805)
  27. #pragma warning(disable:4717)
  28. #endif
  29. #ifdef _WIN32_WINNT
  30. #undef _WIN32_WINNT
  31. #endif
  32. #define _WIN32_WINNT 0x0501
  33. #ifdef _WIN32_IE
  34. #undef _WIN32_IE
  35. #endif
  36. #define _WIN32_IE 0x0501
  37. #define WIN32_LEAN_AND_MEAN 1
  38. #ifndef NOMINMAX
  39. #define NOMINMAX
  40. #endif
  41. #include <io.h> /* for _commit */
  42. #include "shlobj.h"
  43. #elif defined(__linux__)
  44. # include <sys/prctl.h>
  45. #endif
  46. #ifndef WIN32
  47. #include <execinfo.h>
  48. #endif
  49. using namespace std;
  50. map<string, string> mapArgs;
  51. map<string, vector<string> > mapMultiArgs;
  52. bool fDebug = false;
  53. bool fDebugNet = false;
  54. bool fPrintToConsole = false;
  55. bool fPrintToDebugger = false;
  56. bool fRequestShutdown = false;
  57. bool fShutdown = false;
  58. bool fDaemon = false;
  59. bool fServer = false;
  60. bool fCommandLine = false;
  61. string strMiscWarning;
  62. bool fTestNet = false;
  63. bool fNoListen = false;
  64. bool fLogTimestamps = false;
  65. CMedianFilter<int64> vTimeOffsets(200,0);
  66. bool fReopenDebugLog = false;
  67. // Init OpenSSL library multithreading support
  68. static CCriticalSection** ppmutexOpenSSL;
  69. void locking_callback(int mode, int i, const char* file, int line)
  70. {
  71. if (mode & CRYPTO_LOCK) {
  72. ENTER_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
  73. } else {
  74. LEAVE_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
  75. }
  76. }
  77. LockedPageManager LockedPageManager::instance;
  78. // Init
  79. class CInit
  80. {
  81. public:
  82. CInit()
  83. {
  84. // Init OpenSSL library multithreading support
  85. ppmutexOpenSSL = (CCriticalSection**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(CCriticalSection*));
  86. for (int i = 0; i < CRYPTO_num_locks(); i++)
  87. ppmutexOpenSSL[i] = new CCriticalSection();
  88. CRYPTO_set_locking_callback(locking_callback);
  89. #ifdef WIN32
  90. // Seed random number generator with screen scrape and other hardware sources
  91. RAND_screen();
  92. #endif
  93. // Seed random number generator with performance counter
  94. RandAddSeed();
  95. }
  96. ~CInit()
  97. {
  98. // Shutdown OpenSSL library multithreading support
  99. CRYPTO_set_locking_callback(NULL);
  100. for (int i = 0; i < CRYPTO_num_locks(); i++)
  101. delete ppmutexOpenSSL[i];
  102. OPENSSL_free(ppmutexOpenSSL);
  103. }
  104. }
  105. instance_of_cinit;
  106. void RandAddSeed()
  107. {
  108. // Seed with CPU performance counter
  109. int64 nCounter = GetPerformanceCounter();
  110. RAND_add(&nCounter, sizeof(nCounter), 1.5);
  111. memset(&nCounter, 0, sizeof(nCounter));
  112. }
  113. void RandAddSeedPerfmon()
  114. {
  115. RandAddSeed();
  116. // This can take up to 2 seconds, so only do it every 10 minutes
  117. static int64 nLastPerfmon;
  118. if (GetTime() < nLastPerfmon + 10 * 60)
  119. return;
  120. nLastPerfmon = GetTime();
  121. #ifdef WIN32
  122. // Don't need this on Linux, OpenSSL automatically uses /dev/urandom
  123. // Seed with the entire set of perfmon data
  124. unsigned char pdata[250000];
  125. memset(pdata, 0, sizeof(pdata));
  126. unsigned long nSize = sizeof(pdata);
  127. long ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, pdata, &nSize);
  128. RegCloseKey(HKEY_PERFORMANCE_DATA);
  129. if (ret == ERROR_SUCCESS)
  130. {
  131. RAND_add(pdata, nSize, nSize/100.0);
  132. memset(pdata, 0, nSize);
  133. printf("RandAddSeed() %lu bytes\n", nSize);
  134. }
  135. #endif
  136. }
  137. uint64 GetRand(uint64 nMax)
  138. {
  139. if (nMax == 0)
  140. return 0;
  141. // The range of the random source must be a multiple of the modulus
  142. // to give every possible output value an equal possibility
  143. uint64 nRange = (std::numeric_limits<uint64>::max() / nMax) * nMax;
  144. uint64 nRand = 0;
  145. do
  146. RAND_bytes((unsigned char*)&nRand, sizeof(nRand));
  147. while (nRand >= nRange);
  148. return (nRand % nMax);
  149. }
  150. int GetRandInt(int nMax)
  151. {
  152. return GetRand(nMax);
  153. }
  154. uint256 GetRandHash()
  155. {
  156. uint256 hash;
  157. RAND_bytes((unsigned char*)&hash, sizeof(hash));
  158. return hash;
  159. }
  160. static FILE* fileout = NULL;
  161. inline int OutputDebugStringF(const char* pszFormat, ...)
  162. {
  163. int ret = 0;
  164. if (fPrintToConsole)
  165. {
  166. // print to console
  167. va_list arg_ptr;
  168. va_start(arg_ptr, pszFormat);
  169. ret = vprintf(pszFormat, arg_ptr);
  170. va_end(arg_ptr);
  171. }
  172. else if (!fPrintToDebugger)
  173. {
  174. // print to debug.log
  175. if (!fileout)
  176. {
  177. boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
  178. fileout = fopen(pathDebug.string().c_str(), "a");
  179. if (fileout) setbuf(fileout, NULL); // unbuffered
  180. }
  181. if (fileout)
  182. {
  183. static bool fStartedNewLine = true;
  184. // This routine may be called by global destructors during shutdown.
  185. // Since the order of destruction of static/global objects is undefined,
  186. // allocate mutexDebugLog on the heap the first time this routine
  187. // is called to avoid crashes during shutdown.
  188. static boost::mutex* mutexDebugLog = NULL;
  189. if (mutexDebugLog == NULL) mutexDebugLog = new boost::mutex();
  190. boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
  191. // reopen the log file, if requested
  192. if (fReopenDebugLog) {
  193. fReopenDebugLog = false;
  194. boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
  195. if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
  196. setbuf(fileout, NULL); // unbuffered
  197. }
  198. // Debug print useful for profiling
  199. if (fLogTimestamps && fStartedNewLine)
  200. fprintf(fileout, "%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
  201. if (pszFormat[strlen(pszFormat) - 1] == '\n')
  202. fStartedNewLine = true;
  203. else
  204. fStartedNewLine = false;
  205. va_list arg_ptr;
  206. va_start(arg_ptr, pszFormat);
  207. ret = vfprintf(fileout, pszFormat, arg_ptr);
  208. va_end(arg_ptr);
  209. }
  210. }
  211. #ifdef WIN32
  212. if (fPrintToDebugger)
  213. {
  214. static CCriticalSection cs_OutputDebugStringF;
  215. // accumulate and output a line at a time
  216. {
  217. LOCK(cs_OutputDebugStringF);
  218. static std::string buffer;
  219. va_list arg_ptr;
  220. va_start(arg_ptr, pszFormat);
  221. buffer += vstrprintf(pszFormat, arg_ptr);
  222. va_end(arg_ptr);
  223. int line_start = 0, line_end;
  224. while((line_end = buffer.find('\n', line_start)) != -1)
  225. {
  226. OutputDebugStringA(buffer.substr(line_start, line_end - line_start).c_str());
  227. line_start = line_end + 1;
  228. }
  229. buffer.erase(0, line_start);
  230. }
  231. }
  232. #endif
  233. return ret;
  234. }
  235. string vstrprintf(const char *format, va_list ap)
  236. {
  237. char buffer[50000];
  238. char* p = buffer;
  239. int limit = sizeof(buffer);
  240. int ret;
  241. while (true)
  242. {
  243. va_list arg_ptr;
  244. va_copy(arg_ptr, ap);
  245. #ifdef WIN32
  246. ret = _vsnprintf(p, limit, format, arg_ptr);
  247. #else
  248. ret = vsnprintf(p, limit, format, arg_ptr);
  249. #endif
  250. va_end(arg_ptr);
  251. if (ret >= 0 && ret < limit)
  252. break;
  253. if (p != buffer)
  254. delete[] p;
  255. limit *= 2;
  256. p = new char[limit];
  257. if (p == NULL)
  258. throw std::bad_alloc();
  259. }
  260. string str(p, p+ret);
  261. if (p != buffer)
  262. delete[] p;
  263. return str;
  264. }
  265. string real_strprintf(const char *format, int dummy, ...)
  266. {
  267. va_list arg_ptr;
  268. va_start(arg_ptr, dummy);
  269. string str = vstrprintf(format, arg_ptr);
  270. va_end(arg_ptr);
  271. return str;
  272. }
  273. string real_strprintf(const std::string &format, int dummy, ...)
  274. {
  275. va_list arg_ptr;
  276. va_start(arg_ptr, dummy);
  277. string str = vstrprintf(format.c_str(), arg_ptr);
  278. va_end(arg_ptr);
  279. return str;
  280. }
  281. bool error(const char *format, ...)
  282. {
  283. va_list arg_ptr;
  284. va_start(arg_ptr, format);
  285. std::string str = vstrprintf(format, arg_ptr);
  286. va_end(arg_ptr);
  287. printf("ERROR: %s\n", str.c_str());
  288. return false;
  289. }
  290. void ParseString(const string& str, char c, vector<string>& v)
  291. {
  292. if (str.empty())
  293. return;
  294. string::size_type i1 = 0;
  295. string::size_type i2;
  296. while (true)
  297. {
  298. i2 = str.find(c, i1);
  299. if (i2 == str.npos)
  300. {
  301. v.push_back(str.substr(i1));
  302. return;
  303. }
  304. v.push_back(str.substr(i1, i2-i1));
  305. i1 = i2+1;
  306. }
  307. }
  308. string FormatMoney(int64 n, bool fPlus)
  309. {
  310. // Note: not using straight sprintf here because we do NOT want
  311. // localized number formatting.
  312. int64 n_abs = (n > 0 ? n : -n);
  313. int64 quotient = n_abs/COIN;
  314. int64 remainder = n_abs%COIN;
  315. string str = strprintf("%" PRI64d".%08" PRI64d, quotient, remainder);
  316. // Right-trim excess zeros before the decimal point:
  317. int nTrim = 0;
  318. for (int i = str.size()-1; (str[i] == '0' && isdigit(str[i-2])); --i)
  319. ++nTrim;
  320. if (nTrim)
  321. str.erase(str.size()-nTrim, nTrim);
  322. if (n < 0)
  323. str.insert((unsigned int)0, 1, '-');
  324. else if (fPlus && n > 0)
  325. str.insert((unsigned int)0, 1, '+');
  326. return str;
  327. }
  328. bool ParseMoney(const string& str, int64& nRet)
  329. {
  330. return ParseMoney(str.c_str(), nRet);
  331. }
  332. bool ParseMoney(const char* pszIn, int64& nRet)
  333. {
  334. string strWhole;
  335. int64 nUnits = 0;
  336. const char* p = pszIn;
  337. while (isspace(*p))
  338. p++;
  339. for (; *p; p++)
  340. {
  341. if (*p == '.')
  342. {
  343. p++;
  344. int64 nMult = CENT*10;
  345. while (isdigit(*p) && (nMult > 0))
  346. {
  347. nUnits += nMult * (*p++ - '0');
  348. nMult /= 10;
  349. }
  350. break;
  351. }
  352. if (isspace(*p))
  353. break;
  354. if (!isdigit(*p))
  355. return false;
  356. strWhole.insert(strWhole.end(), *p);
  357. }
  358. for (; *p; p++)
  359. if (!isspace(*p))
  360. return false;
  361. if (strWhole.size() > 10) // guard against 63 bit overflow
  362. return false;
  363. if (nUnits < 0 || nUnits > COIN)
  364. return false;
  365. int64 nWhole = atoi64(strWhole);
  366. int64 nValue = nWhole*COIN + nUnits;
  367. nRet = nValue;
  368. return true;
  369. }
  370. static const signed char phexdigit[256] =
  371. { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  372. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  373. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  374. 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
  375. -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  376. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  377. -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  378. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  379. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  380. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  381. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  382. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  383. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  384. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  385. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  386. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, };
  387. static const long hexGrid[] =
  388. {
  389. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  390. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10-19
  391. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  392. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 30-39
  393. -1, -1, -1, -1, -1, -1, -1, -1, 0, 1,
  394. 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, // 50-59
  395. -1, -1, -1, -1, -1, 10, 11, 12, 13, 14,
  396. 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 70-79
  397. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  398. -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, // 90-99
  399. 13, 14, 15, -1, -1, -1, -1, -1, -1, -1,
  400. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 110-109
  401. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  402. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 130-139
  403. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  404. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 150-159
  405. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  406. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 170-179
  407. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  408. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 190-199
  409. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  410. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 210-219
  411. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  412. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 230-239
  413. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  414. -1, -1, -1, -1, -1, -1
  415. };
  416. long ConvertLong(const char* cHex)
  417. {
  418. long lSeed = 0;
  419. while (*cHex && lSeed >= 0)
  420. {
  421. lSeed = (lSeed << 4) | hexGrid[(uint8_t)*cHex++];
  422. }
  423. return lSeed;
  424. }
  425. bool IsHex(const string& str)
  426. {
  427. BOOST_FOREACH(unsigned char c, str)
  428. {
  429. if (phexdigit[c] < 0)
  430. return false;
  431. }
  432. return (str.size() > 0) && (str.size()%2 == 0);
  433. }
  434. vector<unsigned char> ParseHex(const char* psz)
  435. {
  436. // convert hex dump to vector
  437. vector<unsigned char> vch;
  438. while (true)
  439. {
  440. while (isspace(*psz))
  441. psz++;
  442. signed char c = phexdigit[(unsigned char)*psz++];
  443. if (c == (signed char)-1)
  444. break;
  445. unsigned char n = (c << 4);
  446. c = phexdigit[(unsigned char)*psz++];
  447. if (c == (signed char)-1)
  448. break;
  449. n |= c;
  450. vch.push_back(n);
  451. }
  452. return vch;
  453. }
  454. vector<unsigned char> ParseHex(const string& str)
  455. {
  456. return ParseHex(str.c_str());
  457. }
  458. static void InterpretNegativeSetting(string name, map<string, string>& mapSettingsRet)
  459. {
  460. // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
  461. if (name.find("-no") == 0)
  462. {
  463. std::string positive("-");
  464. positive.append(name.begin()+3, name.end());
  465. if (mapSettingsRet.count(positive) == 0)
  466. {
  467. bool value = !GetBoolArg(name);
  468. mapSettingsRet[positive] = (value ? "1" : "0");
  469. }
  470. }
  471. }
  472. void ParseParameters(int argc, const char* const argv[])
  473. {
  474. mapArgs.clear();
  475. mapMultiArgs.clear();
  476. for (int i = 1; i < argc; i++)
  477. {
  478. char psz[10000];
  479. strlcpy(psz, argv[i], sizeof(psz));
  480. char* pszValue = (char*)"";
  481. if (strchr(psz, '='))
  482. {
  483. pszValue = strchr(psz, '=');
  484. *pszValue++ = '\0';
  485. }
  486. #ifdef WIN32
  487. _strlwr(psz);
  488. if (psz[0] == '/')
  489. psz[0] = '-';
  490. #endif
  491. if (psz[0] != '-')
  492. break;
  493. mapArgs[psz] = pszValue;
  494. mapMultiArgs[psz].push_back(pszValue);
  495. }
  496. // New 0.6 features:
  497. BOOST_FOREACH(const PAIRTYPE(string,string)& entry, mapArgs)
  498. {
  499. string name = entry.first;
  500. // interpret --foo as -foo (as long as both are not set)
  501. if (name.find("--") == 0)
  502. {
  503. std::string singleDash(name.begin()+1, name.end());
  504. if (mapArgs.count(singleDash) == 0)
  505. mapArgs[singleDash] = entry.second;
  506. name = singleDash;
  507. }
  508. // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
  509. InterpretNegativeSetting(name, mapArgs);
  510. }
  511. }
  512. std::string GetArg(const std::string& strArg, const std::string& strDefault)
  513. {
  514. if (mapArgs.count(strArg))
  515. return mapArgs[strArg];
  516. return strDefault;
  517. }
  518. int64 GetArg(const std::string& strArg, int64 nDefault)
  519. {
  520. if (mapArgs.count(strArg))
  521. return atoi64(mapArgs[strArg]);
  522. return nDefault;
  523. }
  524. bool GetBoolArg(const std::string& strArg, bool fDefault)
  525. {
  526. if (mapArgs.count(strArg))
  527. {
  528. if (mapArgs[strArg].empty())
  529. return true;
  530. return (atoi(mapArgs[strArg]) != 0);
  531. }
  532. return fDefault;
  533. }
  534. bool SoftSetArg(const std::string& strArg, const std::string& strValue)
  535. {
  536. if (mapArgs.count(strArg))
  537. return false;
  538. mapArgs[strArg] = strValue;
  539. return true;
  540. }
  541. bool SoftSetBoolArg(const std::string& strArg, bool fValue)
  542. {
  543. if (fValue)
  544. return SoftSetArg(strArg, std::string("1"));
  545. else
  546. return SoftSetArg(strArg, std::string("0"));
  547. }
  548. string EncodeBase64(const unsigned char* pch, size_t len)
  549. {
  550. static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  551. string strRet="";
  552. strRet.reserve((len+2)/3*4);
  553. int mode=0, left=0;
  554. const unsigned char *pchEnd = pch+len;
  555. while (pch<pchEnd)
  556. {
  557. int enc = *(pch++);
  558. switch (mode)
  559. {
  560. case 0: // we have no bits
  561. strRet += pbase64[enc >> 2];
  562. left = (enc & 3) << 4;
  563. mode = 1;
  564. break;
  565. case 1: // we have two bits
  566. strRet += pbase64[left | (enc >> 4)];
  567. left = (enc & 15) << 2;
  568. mode = 2;
  569. break;
  570. case 2: // we have four bits
  571. strRet += pbase64[left | (enc >> 6)];
  572. strRet += pbase64[enc & 63];
  573. mode = 0;
  574. break;
  575. }
  576. }
  577. if (mode)
  578. {
  579. strRet += pbase64[left];
  580. strRet += '=';
  581. if (mode == 1)
  582. strRet += '=';
  583. }
  584. return strRet;
  585. }
  586. string EncodeBase64(const string& str)
  587. {
  588. return EncodeBase64((const unsigned char*)str.c_str(), str.size());
  589. }
  590. vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid)
  591. {
  592. static const int decode64_table[256] =
  593. {
  594. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  595. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  596. -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
  597. -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  598. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
  599. 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  600. 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  601. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  602. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  603. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  604. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  605. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  606. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  607. };
  608. if (pfInvalid)
  609. *pfInvalid = false;
  610. vector<unsigned char> vchRet;
  611. vchRet.reserve(strlen(p)*3/4);
  612. int mode = 0;
  613. int left = 0;
  614. while (1)
  615. {
  616. int dec = decode64_table[(unsigned char)*p];
  617. if (dec == -1) break;
  618. p++;
  619. switch (mode)
  620. {
  621. case 0: // we have no bits and get 6
  622. left = dec;
  623. mode = 1;
  624. break;
  625. case 1: // we have 6 bits and keep 4
  626. vchRet.push_back((left<<2) | (dec>>4));
  627. left = dec & 15;
  628. mode = 2;
  629. break;
  630. case 2: // we have 4 bits and get 6, we keep 2
  631. vchRet.push_back((left<<4) | (dec>>2));
  632. left = dec & 3;
  633. mode = 3;
  634. break;
  635. case 3: // we have 2 bits and get 6
  636. vchRet.push_back((left<<6) | dec);
  637. mode = 0;
  638. break;
  639. }
  640. }
  641. if (pfInvalid)
  642. switch (mode)
  643. {
  644. case 0: // 4n base64 characters processed: ok
  645. break;
  646. case 1: // 4n+1 base64 character processed: impossible
  647. *pfInvalid = true;
  648. break;
  649. case 2: // 4n+2 base64 characters processed: require '=='
  650. if (left || p[0] != '=' || p[1] != '=' || decode64_table[(unsigned char)p[2]] != -1)
  651. *pfInvalid = true;
  652. break;
  653. case 3: // 4n+3 base64 characters processed: require '='
  654. if (left || p[0] != '=' || decode64_table[(unsigned char)p[1]] != -1)
  655. *pfInvalid = true;
  656. break;
  657. }
  658. return vchRet;
  659. }
  660. string DecodeBase64(const string& str)
  661. {
  662. vector<unsigned char> vchRet = DecodeBase64(str.c_str());
  663. return string((const char*)&vchRet[0], vchRet.size());
  664. }
  665. string EncodeBase32(const unsigned char* pch, size_t len)
  666. {
  667. static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
  668. string strRet="";
  669. strRet.reserve((len+4)/5*8);
  670. int mode=0, left=0;
  671. const unsigned char *pchEnd = pch+len;
  672. while (pch<pchEnd)
  673. {
  674. int enc = *(pch++);
  675. switch (mode)
  676. {
  677. case 0: // we have no bits
  678. strRet += pbase32[enc >> 3];
  679. left = (enc & 7) << 2;
  680. mode = 1;
  681. break;
  682. case 1: // we have three bits
  683. strRet += pbase32[left | (enc >> 6)];
  684. strRet += pbase32[(enc >> 1) & 31];
  685. left = (enc & 1) << 4;
  686. mode = 2;
  687. break;
  688. case 2: // we have one bit
  689. strRet += pbase32[left | (enc >> 4)];
  690. left = (enc & 15) << 1;
  691. mode = 3;
  692. break;
  693. case 3: // we have four bits
  694. strRet += pbase32[left | (enc >> 7)];
  695. strRet += pbase32[(enc >> 2) & 31];
  696. left = (enc & 3) << 3;
  697. mode = 4;
  698. break;
  699. case 4: // we have two bits
  700. strRet += pbase32[left | (enc >> 5)];
  701. strRet += pbase32[enc & 31];
  702. mode = 0;
  703. }
  704. }
  705. static const int nPadding[5] = {0, 6, 4, 3, 1};
  706. if (mode)
  707. {
  708. strRet += pbase32[left];
  709. for (int n=0; n<nPadding[mode]; n++)
  710. strRet += '=';
  711. }
  712. return strRet;
  713. }
  714. string EncodeBase32(const string& str)
  715. {
  716. return EncodeBase32((const unsigned char*)str.c_str(), str.size());
  717. }
  718. vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid)
  719. {
  720. static const int decode32_table[256] =
  721. {
  722. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  723. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  724. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
  725. -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  726. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
  727. 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
  728. 23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  729. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  730. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  731. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  732. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  733. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  734. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  735. };
  736. if (pfInvalid)
  737. *pfInvalid = false;
  738. vector<unsigned char> vchRet;
  739. vchRet.reserve((strlen(p))*5/8);
  740. int mode = 0;
  741. int left = 0;
  742. while (1)
  743. {
  744. int dec = decode32_table[(unsigned char)*p];
  745. if (dec == -1) break;
  746. p++;
  747. switch (mode)
  748. {
  749. case 0: // we have no bits and get 5
  750. left = dec;
  751. mode = 1;
  752. break;
  753. case 1: // we have 5 bits and keep 2
  754. vchRet.push_back((left<<3) | (dec>>2));
  755. left = dec & 3;
  756. mode = 2;
  757. break;
  758. case 2: // we have 2 bits and keep 7
  759. left = left << 5 | dec;
  760. mode = 3;
  761. break;
  762. case 3: // we have 7 bits and keep 4
  763. vchRet.push_back((left<<1) | (dec>>4));
  764. left = dec & 15;
  765. mode = 4;
  766. break;
  767. case 4: // we have 4 bits, and keep 1
  768. vchRet.push_back((left<<4) | (dec>>1));
  769. left = dec & 1;
  770. mode = 5;
  771. break;
  772. case 5: // we have 1 bit, and keep 6
  773. left = left << 5 | dec;
  774. mode = 6;
  775. break;
  776. case 6: // we have 6 bits, and keep 3
  777. vchRet.push_back((left<<2) | (dec>>3));
  778. left = dec & 7;
  779. mode = 7;
  780. break;
  781. case 7: // we have 3 bits, and keep 0
  782. vchRet.push_back((left<<5) | dec);
  783. mode = 0;
  784. break;
  785. }
  786. }
  787. if (pfInvalid)
  788. switch (mode)
  789. {
  790. case 0: // 8n base32 characters processed: ok
  791. break;
  792. case 1: // 8n+1 base32 characters processed: impossible
  793. case 3: // +3
  794. case 6: // +6
  795. *pfInvalid = true;
  796. break;
  797. case 2: // 8n+2 base32 characters processed: require '======'
  798. if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || p[3] != '=' || p[4] != '=' || p[5] != '=' || decode32_table[(unsigned char)p[6]] != -1)
  799. *pfInvalid = true;
  800. break;
  801. case 4: // 8n+4 base32 characters processed: require '===='
  802. if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || p[3] != '=' || decode32_table[(unsigned char)p[4]] != -1)
  803. *pfInvalid = true;
  804. break;
  805. case 5: // 8n+5 base32 characters processed: require '==='
  806. if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || decode32_table[(unsigned char)p[3]] != -1)
  807. *pfInvalid = true;
  808. break;
  809. case 7: // 8n+7 base32 characters processed: require '='
  810. if (left || p[0] != '=' || decode32_table[(unsigned char)p[1]] != -1)
  811. *pfInvalid = true;
  812. break;
  813. }
  814. return vchRet;
  815. }
  816. string DecodeBase32(const string& str)
  817. {
  818. vector<unsigned char> vchRet = DecodeBase32(str.c_str());
  819. return string((const char*)&vchRet[0], vchRet.size());
  820. }
  821. bool WildcardMatch(const char* psz, const char* mask)
  822. {
  823. while (true)
  824. {
  825. switch (*mask)
  826. {
  827. case '\0':
  828. return (*psz == '\0');
  829. case '*':
  830. return WildcardMatch(psz, mask+1) || (*psz && WildcardMatch(psz+1, mask));
  831. case '?':
  832. if (*psz == '\0')
  833. return false;
  834. break;
  835. default:
  836. if (*psz != *mask)
  837. return false;
  838. break;
  839. }
  840. psz++;
  841. mask++;
  842. }
  843. }
  844. bool WildcardMatch(const string& str, const string& mask)
  845. {
  846. return WildcardMatch(str.c_str(), mask.c_str());
  847. }
  848. static std::string FormatException(std::exception* pex, const char* pszThread)
  849. {
  850. #ifdef WIN32
  851. char pszModule[MAX_PATH] = "";
  852. GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
  853. #else
  854. const char* pszModule = "ecoin";
  855. #endif
  856. if (pex)
  857. return strprintf(
  858. "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
  859. else
  860. return strprintf(
  861. "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
  862. }
  863. void LogException(std::exception* pex, const char* pszThread)
  864. {
  865. std::string message = FormatException(pex, pszThread);
  866. printf("\n%s", message.c_str());
  867. }
  868. void PrintException(std::exception* pex, const char* pszThread)
  869. {
  870. std::string message = FormatException(pex, pszThread);
  871. printf("\n\n************************\n%s\n", message.c_str());
  872. fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
  873. strMiscWarning = message;
  874. throw;
  875. }
  876. void LogStackTrace() {
  877. printf("\n\n******* exception encountered *******\n");
  878. if (fileout)
  879. {
  880. #ifndef WIN32
  881. void* pszBuffer[32];
  882. size_t size;
  883. size = backtrace(pszBuffer, 32);
  884. backtrace_symbols_fd(pszBuffer, size, fileno(fileout));
  885. #endif
  886. }
  887. }
  888. void PrintExceptionContinue(std::exception* pex, const char* pszThread)
  889. {
  890. std::string message = FormatException(pex, pszThread);
  891. printf("\n\n************************\n%s\n", message.c_str());
  892. fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
  893. strMiscWarning = message;
  894. }
  895. boost::filesystem::path GetDefaultDataDir()
  896. {
  897. namespace fs = boost::filesystem;
  898. // Windows < Vista: C:\Documents and Settings\Username\Application Data\Ecoin
  899. // Windows >= Vista: C:\Users\Username\AppData\Roaming\Ecoin
  900. // Mac: ~/Library/Application Support/Ecoin
  901. // Unix: ~/.ecoin
  902. #ifdef WIN32
  903. // Windows
  904. return GetSpecialFolderPath(CSIDL_APPDATA) / "Ecoin";
  905. #else
  906. fs::path pathRet;
  907. char* pszHome = getenv("HOME");
  908. if (pszHome == NULL || strlen(pszHome) == 0)
  909. pathRet = fs::path("/");
  910. else
  911. pathRet = fs::path(pszHome);
  912. #ifdef MAC_OSX
  913. // Mac
  914. pathRet /= "Library/Application Support";
  915. fs::create_directory(pathRet);
  916. return pathRet / "Ecoin";
  917. #else
  918. // Unix
  919. return pathRet / ".ecoin";
  920. #endif
  921. #endif
  922. }
  923. const boost::filesystem::path &GetDataDir(bool fNetSpecific)
  924. {
  925. namespace fs = boost::filesystem;
  926. static fs::path pathCached[2];
  927. static CCriticalSection csPathCached;
  928. static bool cachedPath[2] = {false, false};
  929. fs::path &path = pathCached[fNetSpecific];
  930. // This can be called during exceptions by printf, so we cache the
  931. // value so we don't have to do memory allocations after that.
  932. if (cachedPath[fNetSpecific])
  933. return path;
  934. LOCK(csPathCached);
  935. if (mapArgs.count("-datadir")) {
  936. path = fs::system_complete(mapArgs["-datadir"]);
  937. if (!fs::is_directory(path)) {
  938. path = "";
  939. return path;
  940. }
  941. } else {
  942. path = GetDefaultDataDir();
  943. }
  944. if (fNetSpecific && GetBoolArg("-testnet", false))
  945. path /= "testnet";
  946. fs::create_directory(path);
  947. cachedPath[fNetSpecific]=true;
  948. return path;
  949. }
  950. boost::filesystem::path GetConfigFile()
  951. {
  952. boost::filesystem::path pathConfigFile(GetArg("-conf", "ecoin.conf"));
  953. if (!pathConfigFile.is_complete()) pathConfigFile = GetDataDir(false) / pathConfigFile;
  954. return pathConfigFile;
  955. }
  956. void ReadConfigFile(map<string, string>& mapSettingsRet,
  957. map<string, vector<string> >& mapMultiSettingsRet)
  958. {
  959. boost::filesystem::ifstream streamConfig(GetConfigFile());
  960. if (!streamConfig.good())
  961. return; // No ecoin.conf file is OK
  962. set<string> setOptions;
  963. setOptions.insert("*");
  964. for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
  965. {
  966. // Don't overwrite existing settings so command line settings override ecoin.conf
  967. string strKey = string("-") + it->string_key;
  968. if (mapSettingsRet.count(strKey) == 0)
  969. {
  970. mapSettingsRet[strKey] = it->value[0];
  971. // interpret nofoo=1 as foo=0 (and nofoo=0 as foo=1) as long as foo not set)
  972. InterpretNegativeSetting(strKey, mapSettingsRet);
  973. }
  974. mapMultiSettingsRet[strKey].push_back(it->value[0]);
  975. }
  976. }
  977. boost::filesystem::path GetPidFile()
  978. {
  979. boost::filesystem::path pathPidFile(GetArg("-pid", "ecoind.pid"));
  980. if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
  981. return pathPidFile;
  982. }
  983. #ifndef WIN32
  984. void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
  985. {
  986. FILE* file = fopen(path.string().c_str(), "w");
  987. if (file)
  988. {
  989. fprintf(file, "%d\n", pid);
  990. fclose(file);
  991. }
  992. }
  993. #endif
  994. bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
  995. {
  996. #ifdef WIN32
  997. return MoveFileExA(src.string().c_str(), dest.string().c_str(),
  998. MOVEFILE_REPLACE_EXISTING);
  999. #else
  1000. int rc = std::rename(src.string().c_str(), dest.string().c_str());
  1001. return (rc == 0);
  1002. #endif /* WIN32 */
  1003. }
  1004. void FileCommit(FILE *fileout)
  1005. {
  1006. fflush(fileout); // harmless if redundantly called
  1007. #ifdef WIN32
  1008. _commit(_fileno(fileout));
  1009. #else
  1010. fsync(fileno(fileout));
  1011. #endif
  1012. }
  1013. int GetFilesize(FILE* file)
  1014. {
  1015. int nSavePos = ftell(file);
  1016. int nFilesize = -1;
  1017. if (fseek(file, 0, SEEK_END) == 0)
  1018. nFilesize = ftell(file);
  1019. fseek(file, nSavePos, SEEK_SET);
  1020. return nFilesize;
  1021. }
  1022. void ShrinkDebugFile()
  1023. {
  1024. // Scroll debug.log if it's getting too big
  1025. boost::filesystem::path pathLog = GetDataDir() / "debug.log";
  1026. FILE* file = fopen(pathLog.string().c_str(), "r");
  1027. if (file && GetFilesize(file) > 10 * 1000000)
  1028. {
  1029. // Restart the file with some of the end
  1030. char pch[200000];
  1031. fseek(file, -sizeof(pch), SEEK_END);
  1032. int nBytes = fread(pch, 1, sizeof(pch), file);
  1033. fclose(file);
  1034. file = fopen(pathLog.string().c_str(), "w");
  1035. if (file)
  1036. {
  1037. fwrite(pch, 1, nBytes, file);
  1038. fclose(file);
  1039. }
  1040. }
  1041. }
  1042. static int64 nMockTime = 0; // For unit testing
  1043. int64 GetTime()
  1044. {
  1045. if (nMockTime) return nMockTime;
  1046. return time(NULL);
  1047. }
  1048. void SetMockTime(int64 nMockTimeIn)
  1049. {
  1050. nMockTime = nMockTimeIn;
  1051. }
  1052. static int64 nTimeOffset = 0;
  1053. int64 GetTimeOffset()
  1054. {
  1055. return nTimeOffset;
  1056. }
  1057. int64 GetAdjustedTime()
  1058. {
  1059. return GetTime() + GetTimeOffset();
  1060. }
  1061. void AddTimeData(const CNetAddr& ip, int64 nTime)
  1062. {
  1063. int64 nOffsetSample = nTime - GetTime();
  1064. // Ignore duplicates
  1065. static set<CNetAddr> setKnown;
  1066. if (!setKnown.insert(ip).second)
  1067. return;
  1068. // Add data
  1069. vTimeOffsets.input(nOffsetSample);
  1070. printf("Added time data, samples %d, offset %+" PRI64d" (%+" PRI64d" minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample/60);
  1071. if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1)
  1072. {
  1073. int64 nMedian = vTimeOffsets.median();
  1074. std::vector<int64> vSorted = vTimeOffsets.sorted();
  1075. // Only let other nodes change our time by so much
  1076. if (abs64(nMedian) < 70 * 60)
  1077. {
  1078. nTimeOffset = nMedian;
  1079. }
  1080. else
  1081. {
  1082. nTimeOffset = 0;
  1083. static bool fDone;
  1084. if (!fDone)
  1085. {
  1086. // If nobody has a time different than ours but within 5 minutes of ours, give a warning
  1087. bool fMatch = false;
  1088. BOOST_FOREACH(int64 nOffset, vSorted)
  1089. if (nOffset != 0 && abs64(nOffset) < 5 * 60)
  1090. fMatch = true;
  1091. if (!fMatch)
  1092. {
  1093. fDone = true;
  1094. string strMessage = _("Warning: Please check that your computer's date and time are correct! If your clock is wrong Ecoin will not work properly.");
  1095. strMiscWarning = strMessage;
  1096. printf("*** %s\n", strMessage.c_str());
  1097. uiInterface.ThreadSafeMessageBox(strMessage+" ", string("Ecoin"), CClientUIInterface::OK | CClientUIInterface::ICON_EXCLAMATION);
  1098. }
  1099. }
  1100. }
  1101. if (fDebug) {
  1102. BOOST_FOREACH(int64 n, vSorted)
  1103. printf("%+" PRI64d" ", n);
  1104. printf("| ");
  1105. }
  1106. printf("nTimeOffset = %+" PRI64d" (%+" PRI64d" minutes)\n", nTimeOffset, nTimeOffset/60);
  1107. }
  1108. }
  1109. string FormatVersion(int nVersion)
  1110. {
  1111. if (nVersion%100 == 0)
  1112. return strprintf("%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100);
  1113. else
  1114. return strprintf("%d.%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100, nVersion%100);
  1115. }
  1116. string FormatFullVersion()
  1117. {
  1118. return CLIENT_BUILD;
  1119. }
  1120. // Format the subversion field according to BIP 14 spec (https://en.ecoin.it/wiki/BIP_0014)
  1121. std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector<std::string>& comments)
  1122. {
  1123. std::ostringstream ss;
  1124. ss << "/";
  1125. ss << name << ":" << FormatVersion(nClientVersion);
  1126. if (!comments.empty())
  1127. ss << "(" << boost::algorithm::join(comments, "; ") << ")";
  1128. ss << "/";
  1129. return ss.str();
  1130. }
  1131. #ifdef WIN32
  1132. boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate)
  1133. {
  1134. namespace fs = boost::filesystem;
  1135. char pszPath[MAX_PATH] = "";
  1136. if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate))
  1137. {
  1138. return fs::path(pszPath);
  1139. }
  1140. printf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
  1141. return fs::path("");
  1142. }
  1143. #endif
  1144. void runCommand(std::string strCommand)
  1145. {
  1146. int nErr = ::system(strCommand.c_str());
  1147. if (nErr)
  1148. printf("runCommand error: system(%s) returned %d\n", strCommand.c_str(), nErr);
  1149. }
  1150. void RenameThread(const char* name)
  1151. {
  1152. #if defined(PR_SET_NAME)
  1153. // Only the first 15 characters are used (16 - NUL terminator)
  1154. ::prctl(PR_SET_NAME, name, 0, 0, 0);
  1155. #elif 0 && (defined(__FreeBSD__) || defined(__OpenBSD__))
  1156. pthread_set_name_np(pthread_self(), name);
  1157. #else
  1158. // Prevent warnings for unused parameters...
  1159. (void)name;
  1160. #endif
  1161. }
  1162. bool NewThread(void(*pfn)(void*), void* parg)
  1163. {
  1164. try
  1165. {
  1166. boost::thread(pfn, parg); // thread detaches when out of scope
  1167. } catch(boost::thread_resource_error &e) {
  1168. printf("Error creating thread: %s\n", e.what());
  1169. return false;
  1170. }
  1171. return true;
  1172. }