You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.cpp 40KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2014 The Bitcoin developers
  3. // Distributed under the MIT/X11 software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #include "util.h"
  6. #include "chainparamsbase.h"
  7. #include "sync.h"
  8. #include "ui_interface.h"
  9. #include "uint256.h"
  10. #include "version.h"
  11. #include <stdarg.h>
  12. #include <boost/date_time/posix_time/posix_time.hpp>
  13. #ifndef WIN32
  14. // for posix_fallocate
  15. #ifdef __linux_
  16. #ifdef _POSIX_C_SOURCE
  17. #undef _POSIX_C_SOURCE
  18. #endif
  19. #define _POSIX_C_SOURCE 200112L
  20. #include <sys/prctl.h>
  21. #endif
  22. #include <algorithm>
  23. #include <fcntl.h>
  24. #include <sys/resource.h>
  25. #include <sys/stat.h>
  26. #else
  27. #ifdef _MSC_VER
  28. #pragma warning(disable:4786)
  29. #pragma warning(disable:4804)
  30. #pragma warning(disable:4805)
  31. #pragma warning(disable:4717)
  32. #endif
  33. #ifdef _WIN32_WINNT
  34. #undef _WIN32_WINNT
  35. #endif
  36. #define _WIN32_WINNT 0x0501
  37. #ifdef _WIN32_IE
  38. #undef _WIN32_IE
  39. #endif
  40. #define _WIN32_IE 0x0501
  41. #define WIN32_LEAN_AND_MEAN 1
  42. #ifndef NOMINMAX
  43. #define NOMINMAX
  44. #endif
  45. #include <io.h> /* for _commit */
  46. #include <shlobj.h>
  47. #endif
  48. #include <boost/algorithm/string/case_conv.hpp> // for to_lower()
  49. #include <boost/algorithm/string/join.hpp>
  50. #include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith()
  51. #include <boost/filesystem.hpp>
  52. #include <boost/filesystem/fstream.hpp>
  53. #include <boost/foreach.hpp>
  54. #include <boost/program_options/detail/config_file.hpp>
  55. #include <boost/program_options/parsers.hpp>
  56. #include <openssl/crypto.h>
  57. #include <openssl/rand.h>
  58. // Work around clang compilation problem in Boost 1.46:
  59. // /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
  60. // See also: http://stackoverflow.com/questions/10020179/compilation-fail-in-boost-librairies-program-options
  61. // http://clang.debian.net/status.php?version=3.0&key=CANNOT_FIND_FUNCTION
  62. namespace boost {
  63. namespace program_options {
  64. std::string to_internal(const std::string&);
  65. }
  66. } // namespace boost
  67. using namespace std;
  68. map<string, string> mapArgs;
  69. map<string, vector<string> > mapMultiArgs;
  70. bool fDebug = false;
  71. bool fPrintToConsole = false;
  72. bool fPrintToDebugLog = true;
  73. bool fDaemon = false;
  74. bool fServer = false;
  75. string strMiscWarning;
  76. bool fLogTimestamps = false;
  77. volatile bool fReopenDebugLog = false;
  78. CClientUIInterface uiInterface;
  79. // Init OpenSSL library multithreading support
  80. static CCriticalSection** ppmutexOpenSSL;
  81. void locking_callback(int mode, int i, const char* file, int line)
  82. {
  83. if (mode & CRYPTO_LOCK) {
  84. ENTER_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
  85. } else {
  86. LEAVE_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
  87. }
  88. }
  89. // Init
  90. class CInit
  91. {
  92. public:
  93. CInit()
  94. {
  95. // Init OpenSSL library multithreading support
  96. ppmutexOpenSSL = (CCriticalSection**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(CCriticalSection*));
  97. for (int i = 0; i < CRYPTO_num_locks(); i++)
  98. ppmutexOpenSSL[i] = new CCriticalSection();
  99. CRYPTO_set_locking_callback(locking_callback);
  100. #ifdef WIN32
  101. // Seed random number generator with screen scrape and other hardware sources
  102. RAND_screen();
  103. #endif
  104. // Seed random number generator with performance counter
  105. RandAddSeed();
  106. }
  107. ~CInit()
  108. {
  109. // Shutdown OpenSSL library multithreading support
  110. CRYPTO_set_locking_callback(NULL);
  111. for (int i = 0; i < CRYPTO_num_locks(); i++)
  112. delete ppmutexOpenSSL[i];
  113. OPENSSL_free(ppmutexOpenSSL);
  114. }
  115. }
  116. instance_of_cinit;
  117. void RandAddSeed()
  118. {
  119. // Seed with CPU performance counter
  120. int64_t nCounter = GetPerformanceCounter();
  121. RAND_add(&nCounter, sizeof(nCounter), 1.5);
  122. memset(&nCounter, 0, sizeof(nCounter));
  123. }
  124. void RandAddSeedPerfmon()
  125. {
  126. RandAddSeed();
  127. // This can take up to 2 seconds, so only do it every 10 minutes
  128. static int64_t nLastPerfmon;
  129. if (GetTime() < nLastPerfmon + 10 * 60)
  130. return;
  131. nLastPerfmon = GetTime();
  132. #ifdef WIN32
  133. // Don't need this on Linux, OpenSSL automatically uses /dev/urandom
  134. // Seed with the entire set of perfmon data
  135. std::vector <unsigned char> vData(250000,0);
  136. long ret = 0;
  137. unsigned long nSize = 0;
  138. const size_t nMaxSize = 10000000; // Bail out at more than 10MB of performance data
  139. while (true)
  140. {
  141. nSize = vData.size();
  142. ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, begin_ptr(vData), &nSize);
  143. if (ret != ERROR_MORE_DATA || vData.size() >= nMaxSize)
  144. break;
  145. vData.resize(std::max((vData.size()*3)/2, nMaxSize)); // Grow size of buffer exponentially
  146. }
  147. RegCloseKey(HKEY_PERFORMANCE_DATA);
  148. if (ret == ERROR_SUCCESS)
  149. {
  150. RAND_add(begin_ptr(vData), nSize, nSize/100.0);
  151. OPENSSL_cleanse(begin_ptr(vData), nSize);
  152. LogPrint("rand", "%s: %lu bytes\n", __func__, nSize);
  153. } else {
  154. static bool warned = false; // Warn only once
  155. if (!warned)
  156. {
  157. LogPrintf("%s: Warning: RegQueryValueExA(HKEY_PERFORMANCE_DATA) failed with code %i\n", __func__, ret);
  158. warned = true;
  159. }
  160. }
  161. #endif
  162. }
  163. uint64_t GetRand(uint64_t nMax)
  164. {
  165. if (nMax == 0)
  166. return 0;
  167. // The range of the random source must be a multiple of the modulus
  168. // to give every possible output value an equal possibility
  169. uint64_t nRange = (std::numeric_limits<uint64_t>::max() / nMax) * nMax;
  170. uint64_t nRand = 0;
  171. do
  172. RAND_bytes((unsigned char*)&nRand, sizeof(nRand));
  173. while (nRand >= nRange);
  174. return (nRand % nMax);
  175. }
  176. int GetRandInt(int nMax)
  177. {
  178. return GetRand(nMax);
  179. }
  180. uint256 GetRandHash()
  181. {
  182. uint256 hash;
  183. RAND_bytes((unsigned char*)&hash, sizeof(hash));
  184. return hash;
  185. }
  186. // LogPrintf() has been broken a couple of times now
  187. // by well-meaning people adding mutexes in the most straightforward way.
  188. // It breaks because it may be called by global destructors during shutdown.
  189. // Since the order of destruction of static/global objects is undefined,
  190. // defining a mutex as a global object doesn't work (the mutex gets
  191. // destroyed, and then some later destructor calls OutputDebugStringF,
  192. // maybe indirectly, and you get a core dump at shutdown trying to lock
  193. // the mutex).
  194. static boost::once_flag debugPrintInitFlag = BOOST_ONCE_INIT;
  195. // We use boost::call_once() to make sure these are initialized in
  196. // in a thread-safe manner the first time it is called:
  197. static FILE* fileout = NULL;
  198. static boost::mutex* mutexDebugLog = NULL;
  199. static void DebugPrintInit()
  200. {
  201. assert(fileout == NULL);
  202. assert(mutexDebugLog == NULL);
  203. boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
  204. fileout = fopen(pathDebug.string().c_str(), "a");
  205. if (fileout) setbuf(fileout, NULL); // unbuffered
  206. mutexDebugLog = new boost::mutex();
  207. }
  208. bool LogAcceptCategory(const char* category)
  209. {
  210. if (category != NULL)
  211. {
  212. if (!fDebug)
  213. return false;
  214. // Give each thread quick access to -debug settings.
  215. // This helps prevent issues debugging global destructors,
  216. // where mapMultiArgs might be deleted before another
  217. // global destructor calls LogPrint()
  218. static boost::thread_specific_ptr<set<string> > ptrCategory;
  219. if (ptrCategory.get() == NULL)
  220. {
  221. const vector<string>& categories = mapMultiArgs["-debug"];
  222. ptrCategory.reset(new set<string>(categories.begin(), categories.end()));
  223. // thread_specific_ptr automatically deletes the set when the thread ends.
  224. }
  225. const set<string>& setCategories = *ptrCategory.get();
  226. // if not debugging everything and not debugging specific category, LogPrint does nothing.
  227. if (setCategories.count(string("")) == 0 &&
  228. setCategories.count(string(category)) == 0)
  229. return false;
  230. }
  231. return true;
  232. }
  233. int LogPrintStr(const std::string &str)
  234. {
  235. int ret = 0; // Returns total number of characters written
  236. if (fPrintToConsole)
  237. {
  238. // print to console
  239. ret = fwrite(str.data(), 1, str.size(), stdout);
  240. }
  241. else if (fPrintToDebugLog)
  242. {
  243. static bool fStartedNewLine = true;
  244. boost::call_once(&DebugPrintInit, debugPrintInitFlag);
  245. if (fileout == NULL)
  246. return ret;
  247. boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
  248. // reopen the log file, if requested
  249. if (fReopenDebugLog) {
  250. fReopenDebugLog = false;
  251. boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
  252. if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
  253. setbuf(fileout, NULL); // unbuffered
  254. }
  255. // Debug print useful for profiling
  256. if (fLogTimestamps && fStartedNewLine)
  257. ret += fprintf(fileout, "%s ", DateTimeStrFormat("%Y-%m-%d %H:%M:%S", GetTime()).c_str());
  258. if (!str.empty() && str[str.size()-1] == '\n')
  259. fStartedNewLine = true;
  260. else
  261. fStartedNewLine = false;
  262. ret = fwrite(str.data(), 1, str.size(), fileout);
  263. }
  264. return ret;
  265. }
  266. string FormatMoney(int64_t n, bool fPlus)
  267. {
  268. // Note: not using straight sprintf here because we do NOT want
  269. // localized number formatting.
  270. int64_t n_abs = (n > 0 ? n : -n);
  271. int64_t quotient = n_abs/COIN;
  272. int64_t remainder = n_abs%COIN;
  273. string str = strprintf("%d.%08d", quotient, remainder);
  274. // Right-trim excess zeros before the decimal point:
  275. int nTrim = 0;
  276. for (int i = str.size()-1; (str[i] == '0' && isdigit(str[i-2])); --i)
  277. ++nTrim;
  278. if (nTrim)
  279. str.erase(str.size()-nTrim, nTrim);
  280. if (n < 0)
  281. str.insert((unsigned int)0, 1, '-');
  282. else if (fPlus && n > 0)
  283. str.insert((unsigned int)0, 1, '+');
  284. return str;
  285. }
  286. bool ParseMoney(const string& str, int64_t& nRet)
  287. {
  288. return ParseMoney(str.c_str(), nRet);
  289. }
  290. bool ParseMoney(const char* pszIn, int64_t& nRet)
  291. {
  292. string strWhole;
  293. int64_t nUnits = 0;
  294. const char* p = pszIn;
  295. while (isspace(*p))
  296. p++;
  297. for (; *p; p++)
  298. {
  299. if (*p == '.')
  300. {
  301. p++;
  302. int64_t nMult = CENT*10;
  303. while (isdigit(*p) && (nMult > 0))
  304. {
  305. nUnits += nMult * (*p++ - '0');
  306. nMult /= 10;
  307. }
  308. break;
  309. }
  310. if (isspace(*p))
  311. break;
  312. if (!isdigit(*p))
  313. return false;
  314. strWhole.insert(strWhole.end(), *p);
  315. }
  316. for (; *p; p++)
  317. if (!isspace(*p))
  318. return false;
  319. if (strWhole.size() > 10) // guard against 63 bit overflow
  320. return false;
  321. if (nUnits < 0 || nUnits > COIN)
  322. return false;
  323. int64_t nWhole = atoi64(strWhole);
  324. int64_t nValue = nWhole*COIN + nUnits;
  325. nRet = nValue;
  326. return true;
  327. }
  328. // safeChars chosen to allow simple messages/URLs/email addresses, but avoid anything
  329. // even possibly remotely dangerous like & or >
  330. static string safeChars("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890 .,;_/:?@");
  331. string SanitizeString(const string& str)
  332. {
  333. string strResult;
  334. for (std::string::size_type i = 0; i < str.size(); i++)
  335. {
  336. if (safeChars.find(str[i]) != std::string::npos)
  337. strResult.push_back(str[i]);
  338. }
  339. return strResult;
  340. }
  341. const signed char p_util_hexdigit[256] =
  342. { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  343. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  344. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  345. 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
  346. -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  347. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  348. -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  349. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  350. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  351. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  352. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  353. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  354. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  355. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  356. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  357. -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, };
  358. bool IsHex(const string& str)
  359. {
  360. BOOST_FOREACH(char c, str)
  361. {
  362. if (HexDigit(c) < 0)
  363. return false;
  364. }
  365. return (str.size() > 0) && (str.size()%2 == 0);
  366. }
  367. vector<unsigned char> ParseHex(const char* psz)
  368. {
  369. // convert hex dump to vector
  370. vector<unsigned char> vch;
  371. while (true)
  372. {
  373. while (isspace(*psz))
  374. psz++;
  375. signed char c = HexDigit(*psz++);
  376. if (c == (signed char)-1)
  377. break;
  378. unsigned char n = (c << 4);
  379. c = HexDigit(*psz++);
  380. if (c == (signed char)-1)
  381. break;
  382. n |= c;
  383. vch.push_back(n);
  384. }
  385. return vch;
  386. }
  387. vector<unsigned char> ParseHex(const string& str)
  388. {
  389. return ParseHex(str.c_str());
  390. }
  391. static void InterpretNegativeSetting(string name, map<string, string>& mapSettingsRet)
  392. {
  393. // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
  394. if (name.find("-no") == 0)
  395. {
  396. std::string positive("-");
  397. positive.append(name.begin()+3, name.end());
  398. if (mapSettingsRet.count(positive) == 0)
  399. {
  400. bool value = !GetBoolArg(name, false);
  401. mapSettingsRet[positive] = (value ? "1" : "0");
  402. }
  403. }
  404. }
  405. void ParseParameters(int argc, const char* const argv[])
  406. {
  407. mapArgs.clear();
  408. mapMultiArgs.clear();
  409. for (int i = 1; i < argc; i++)
  410. {
  411. std::string str(argv[i]);
  412. std::string strValue;
  413. size_t is_index = str.find('=');
  414. if (is_index != std::string::npos)
  415. {
  416. strValue = str.substr(is_index+1);
  417. str = str.substr(0, is_index);
  418. }
  419. #ifdef WIN32
  420. boost::to_lower(str);
  421. if (boost::algorithm::starts_with(str, "/"))
  422. str = "-" + str.substr(1);
  423. #endif
  424. if (str[0] != '-')
  425. break;
  426. // Interpret --foo as -foo.
  427. // If both --foo and -foo are set, the last takes effect.
  428. if (str.length() > 1 && str[1] == '-')
  429. str = str.substr(1);
  430. mapArgs[str] = strValue;
  431. mapMultiArgs[str].push_back(strValue);
  432. }
  433. // New 0.6 features:
  434. BOOST_FOREACH(const PAIRTYPE(string,string)& entry, mapArgs)
  435. {
  436. // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
  437. InterpretNegativeSetting(entry.first, mapArgs);
  438. }
  439. }
  440. std::string GetArg(const std::string& strArg, const std::string& strDefault)
  441. {
  442. if (mapArgs.count(strArg))
  443. return mapArgs[strArg];
  444. return strDefault;
  445. }
  446. int64_t GetArg(const std::string& strArg, int64_t nDefault)
  447. {
  448. if (mapArgs.count(strArg))
  449. return atoi64(mapArgs[strArg]);
  450. return nDefault;
  451. }
  452. bool GetBoolArg(const std::string& strArg, bool fDefault)
  453. {
  454. if (mapArgs.count(strArg))
  455. {
  456. if (mapArgs[strArg].empty())
  457. return true;
  458. return (atoi(mapArgs[strArg]) != 0);
  459. }
  460. return fDefault;
  461. }
  462. bool SoftSetArg(const std::string& strArg, const std::string& strValue)
  463. {
  464. if (mapArgs.count(strArg))
  465. return false;
  466. mapArgs[strArg] = strValue;
  467. return true;
  468. }
  469. bool SoftSetBoolArg(const std::string& strArg, bool fValue)
  470. {
  471. if (fValue)
  472. return SoftSetArg(strArg, std::string("1"));
  473. else
  474. return SoftSetArg(strArg, std::string("0"));
  475. }
  476. string EncodeBase64(const unsigned char* pch, size_t len)
  477. {
  478. static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  479. string strRet="";
  480. strRet.reserve((len+2)/3*4);
  481. int mode=0, left=0;
  482. const unsigned char *pchEnd = pch+len;
  483. while (pch<pchEnd)
  484. {
  485. int enc = *(pch++);
  486. switch (mode)
  487. {
  488. case 0: // we have no bits
  489. strRet += pbase64[enc >> 2];
  490. left = (enc & 3) << 4;
  491. mode = 1;
  492. break;
  493. case 1: // we have two bits
  494. strRet += pbase64[left | (enc >> 4)];
  495. left = (enc & 15) << 2;
  496. mode = 2;
  497. break;
  498. case 2: // we have four bits
  499. strRet += pbase64[left | (enc >> 6)];
  500. strRet += pbase64[enc & 63];
  501. mode = 0;
  502. break;
  503. }
  504. }
  505. if (mode)
  506. {
  507. strRet += pbase64[left];
  508. strRet += '=';
  509. if (mode == 1)
  510. strRet += '=';
  511. }
  512. return strRet;
  513. }
  514. string EncodeBase64(const string& str)
  515. {
  516. return EncodeBase64((const unsigned char*)str.c_str(), str.size());
  517. }
  518. vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid)
  519. {
  520. static const int decode64_table[256] =
  521. {
  522. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  523. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  524. -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
  525. -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  526. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
  527. 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  528. 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  529. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  530. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  531. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  532. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  533. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  534. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  535. };
  536. if (pfInvalid)
  537. *pfInvalid = false;
  538. vector<unsigned char> vchRet;
  539. vchRet.reserve(strlen(p)*3/4);
  540. int mode = 0;
  541. int left = 0;
  542. while (1)
  543. {
  544. int dec = decode64_table[(unsigned char)*p];
  545. if (dec == -1) break;
  546. p++;
  547. switch (mode)
  548. {
  549. case 0: // we have no bits and get 6
  550. left = dec;
  551. mode = 1;
  552. break;
  553. case 1: // we have 6 bits and keep 4
  554. vchRet.push_back((left<<2) | (dec>>4));
  555. left = dec & 15;
  556. mode = 2;
  557. break;
  558. case 2: // we have 4 bits and get 6, we keep 2
  559. vchRet.push_back((left<<4) | (dec>>2));
  560. left = dec & 3;
  561. mode = 3;
  562. break;
  563. case 3: // we have 2 bits and get 6
  564. vchRet.push_back((left<<6) | dec);
  565. mode = 0;
  566. break;
  567. }
  568. }
  569. if (pfInvalid)
  570. switch (mode)
  571. {
  572. case 0: // 4n base64 characters processed: ok
  573. break;
  574. case 1: // 4n+1 base64 character processed: impossible
  575. *pfInvalid = true;
  576. break;
  577. case 2: // 4n+2 base64 characters processed: require '=='
  578. if (left || p[0] != '=' || p[1] != '=' || decode64_table[(unsigned char)p[2]] != -1)
  579. *pfInvalid = true;
  580. break;
  581. case 3: // 4n+3 base64 characters processed: require '='
  582. if (left || p[0] != '=' || decode64_table[(unsigned char)p[1]] != -1)
  583. *pfInvalid = true;
  584. break;
  585. }
  586. return vchRet;
  587. }
  588. string DecodeBase64(const string& str)
  589. {
  590. vector<unsigned char> vchRet = DecodeBase64(str.c_str());
  591. return string((const char*)&vchRet[0], vchRet.size());
  592. }
  593. string EncodeBase32(const unsigned char* pch, size_t len)
  594. {
  595. static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
  596. string strRet="";
  597. strRet.reserve((len+4)/5*8);
  598. int mode=0, left=0;
  599. const unsigned char *pchEnd = pch+len;
  600. while (pch<pchEnd)
  601. {
  602. int enc = *(pch++);
  603. switch (mode)
  604. {
  605. case 0: // we have no bits
  606. strRet += pbase32[enc >> 3];
  607. left = (enc & 7) << 2;
  608. mode = 1;
  609. break;
  610. case 1: // we have three bits
  611. strRet += pbase32[left | (enc >> 6)];
  612. strRet += pbase32[(enc >> 1) & 31];
  613. left = (enc & 1) << 4;
  614. mode = 2;
  615. break;
  616. case 2: // we have one bit
  617. strRet += pbase32[left | (enc >> 4)];
  618. left = (enc & 15) << 1;
  619. mode = 3;
  620. break;
  621. case 3: // we have four bits
  622. strRet += pbase32[left | (enc >> 7)];
  623. strRet += pbase32[(enc >> 2) & 31];
  624. left = (enc & 3) << 3;
  625. mode = 4;
  626. break;
  627. case 4: // we have two bits
  628. strRet += pbase32[left | (enc >> 5)];
  629. strRet += pbase32[enc & 31];
  630. mode = 0;
  631. }
  632. }
  633. static const int nPadding[5] = {0, 6, 4, 3, 1};
  634. if (mode)
  635. {
  636. strRet += pbase32[left];
  637. for (int n=0; n<nPadding[mode]; n++)
  638. strRet += '=';
  639. }
  640. return strRet;
  641. }
  642. string EncodeBase32(const string& str)
  643. {
  644. return EncodeBase32((const unsigned char*)str.c_str(), str.size());
  645. }
  646. vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid)
  647. {
  648. static const int decode32_table[256] =
  649. {
  650. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  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, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
  653. -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  654. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2,
  655. 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
  656. 23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  657. -1, -1, -1, -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
  663. };
  664. if (pfInvalid)
  665. *pfInvalid = false;
  666. vector<unsigned char> vchRet;
  667. vchRet.reserve((strlen(p))*5/8);
  668. int mode = 0;
  669. int left = 0;
  670. while (1)
  671. {
  672. int dec = decode32_table[(unsigned char)*p];
  673. if (dec == -1) break;
  674. p++;
  675. switch (mode)
  676. {
  677. case 0: // we have no bits and get 5
  678. left = dec;
  679. mode = 1;
  680. break;
  681. case 1: // we have 5 bits and keep 2
  682. vchRet.push_back((left<<3) | (dec>>2));
  683. left = dec & 3;
  684. mode = 2;
  685. break;
  686. case 2: // we have 2 bits and keep 7
  687. left = left << 5 | dec;
  688. mode = 3;
  689. break;
  690. case 3: // we have 7 bits and keep 4
  691. vchRet.push_back((left<<1) | (dec>>4));
  692. left = dec & 15;
  693. mode = 4;
  694. break;
  695. case 4: // we have 4 bits, and keep 1
  696. vchRet.push_back((left<<4) | (dec>>1));
  697. left = dec & 1;
  698. mode = 5;
  699. break;
  700. case 5: // we have 1 bit, and keep 6
  701. left = left << 5 | dec;
  702. mode = 6;
  703. break;
  704. case 6: // we have 6 bits, and keep 3
  705. vchRet.push_back((left<<2) | (dec>>3));
  706. left = dec & 7;
  707. mode = 7;
  708. break;
  709. case 7: // we have 3 bits, and keep 0
  710. vchRet.push_back((left<<5) | dec);
  711. mode = 0;
  712. break;
  713. }
  714. }
  715. if (pfInvalid)
  716. switch (mode)
  717. {
  718. case 0: // 8n base32 characters processed: ok
  719. break;
  720. case 1: // 8n+1 base32 characters processed: impossible
  721. case 3: // +3
  722. case 6: // +6
  723. *pfInvalid = true;
  724. break;
  725. case 2: // 8n+2 base32 characters processed: require '======'
  726. if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || p[3] != '=' || p[4] != '=' || p[5] != '=' || decode32_table[(unsigned char)p[6]] != -1)
  727. *pfInvalid = true;
  728. break;
  729. case 4: // 8n+4 base32 characters processed: require '===='
  730. if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || p[3] != '=' || decode32_table[(unsigned char)p[4]] != -1)
  731. *pfInvalid = true;
  732. break;
  733. case 5: // 8n+5 base32 characters processed: require '==='
  734. if (left || p[0] != '=' || p[1] != '=' || p[2] != '=' || decode32_table[(unsigned char)p[3]] != -1)
  735. *pfInvalid = true;
  736. break;
  737. case 7: // 8n+7 base32 characters processed: require '='
  738. if (left || p[0] != '=' || decode32_table[(unsigned char)p[1]] != -1)
  739. *pfInvalid = true;
  740. break;
  741. }
  742. return vchRet;
  743. }
  744. string DecodeBase32(const string& str)
  745. {
  746. vector<unsigned char> vchRet = DecodeBase32(str.c_str());
  747. return string((const char*)&vchRet[0], vchRet.size());
  748. }
  749. static std::string FormatException(std::exception* pex, const char* pszThread)
  750. {
  751. #ifdef WIN32
  752. char pszModule[MAX_PATH] = "";
  753. GetModuleFileNameA(NULL, pszModule, sizeof(pszModule));
  754. #else
  755. const char* pszModule = "bitcoin";
  756. #endif
  757. if (pex)
  758. return strprintf(
  759. "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread);
  760. else
  761. return strprintf(
  762. "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread);
  763. }
  764. void PrintExceptionContinue(std::exception* pex, const char* pszThread)
  765. {
  766. std::string message = FormatException(pex, pszThread);
  767. LogPrintf("\n\n************************\n%s\n", message);
  768. fprintf(stderr, "\n\n************************\n%s\n", message.c_str());
  769. strMiscWarning = message;
  770. }
  771. boost::filesystem::path GetDefaultDataDir()
  772. {
  773. namespace fs = boost::filesystem;
  774. // Windows < Vista: C:\Documents and Settings\Username\Application Data\Bitcoin
  775. // Windows >= Vista: C:\Users\Username\AppData\Roaming\Bitcoin
  776. // Mac: ~/Library/Application Support/Bitcoin
  777. // Unix: ~/.bitcoin
  778. #ifdef WIN32
  779. // Windows
  780. return GetSpecialFolderPath(CSIDL_APPDATA) / "Bitcoin";
  781. #else
  782. fs::path pathRet;
  783. char* pszHome = getenv("HOME");
  784. if (pszHome == NULL || strlen(pszHome) == 0)
  785. pathRet = fs::path("/");
  786. else
  787. pathRet = fs::path(pszHome);
  788. #ifdef MAC_OSX
  789. // Mac
  790. pathRet /= "Library/Application Support";
  791. TryCreateDirectory(pathRet);
  792. return pathRet / "Bitcoin";
  793. #else
  794. // Unix
  795. return pathRet / ".bitcoin";
  796. #endif
  797. #endif
  798. }
  799. static boost::filesystem::path pathCached[CBaseChainParams::MAX_NETWORK_TYPES+1];
  800. static CCriticalSection csPathCached;
  801. const boost::filesystem::path &GetDataDir(bool fNetSpecific)
  802. {
  803. namespace fs = boost::filesystem;
  804. LOCK(csPathCached);
  805. int nNet = CBaseChainParams::MAX_NETWORK_TYPES;
  806. if (fNetSpecific) nNet = BaseParams().NetworkID();
  807. fs::path &path = pathCached[nNet];
  808. // This can be called during exceptions by LogPrintf(), so we cache the
  809. // value so we don't have to do memory allocations after that.
  810. if (!path.empty())
  811. return path;
  812. if (mapArgs.count("-datadir")) {
  813. path = fs::system_complete(mapArgs["-datadir"]);
  814. if (!fs::is_directory(path)) {
  815. path = "";
  816. return path;
  817. }
  818. } else {
  819. path = GetDefaultDataDir();
  820. }
  821. if (fNetSpecific)
  822. path /= BaseParams().DataDir();
  823. fs::create_directories(path);
  824. return path;
  825. }
  826. void ClearDatadirCache()
  827. {
  828. std::fill(&pathCached[0], &pathCached[CBaseChainParams::MAX_NETWORK_TYPES+1],
  829. boost::filesystem::path());
  830. }
  831. boost::filesystem::path GetConfigFile()
  832. {
  833. boost::filesystem::path pathConfigFile(GetArg("-conf", "bitcoin.conf"));
  834. if (!pathConfigFile.is_complete())
  835. pathConfigFile = GetDataDir(false) / pathConfigFile;
  836. return pathConfigFile;
  837. }
  838. void ReadConfigFile(map<string, string>& mapSettingsRet,
  839. map<string, vector<string> >& mapMultiSettingsRet)
  840. {
  841. boost::filesystem::ifstream streamConfig(GetConfigFile());
  842. if (!streamConfig.good())
  843. return; // No bitcoin.conf file is OK
  844. set<string> setOptions;
  845. setOptions.insert("*");
  846. for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
  847. {
  848. // Don't overwrite existing settings so command line settings override bitcoin.conf
  849. string strKey = string("-") + it->string_key;
  850. if (mapSettingsRet.count(strKey) == 0)
  851. {
  852. mapSettingsRet[strKey] = it->value[0];
  853. // interpret nofoo=1 as foo=0 (and nofoo=0 as foo=1) as long as foo not set)
  854. InterpretNegativeSetting(strKey, mapSettingsRet);
  855. }
  856. mapMultiSettingsRet[strKey].push_back(it->value[0]);
  857. }
  858. // If datadir is changed in .conf file:
  859. ClearDatadirCache();
  860. }
  861. boost::filesystem::path GetPidFile()
  862. {
  863. boost::filesystem::path pathPidFile(GetArg("-pid", "bitcoind.pid"));
  864. if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
  865. return pathPidFile;
  866. }
  867. #ifndef WIN32
  868. void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
  869. {
  870. FILE* file = fopen(path.string().c_str(), "w");
  871. if (file)
  872. {
  873. fprintf(file, "%d\n", pid);
  874. fclose(file);
  875. }
  876. }
  877. #endif
  878. bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
  879. {
  880. #ifdef WIN32
  881. return MoveFileExA(src.string().c_str(), dest.string().c_str(),
  882. MOVEFILE_REPLACE_EXISTING);
  883. #else
  884. int rc = std::rename(src.string().c_str(), dest.string().c_str());
  885. return (rc == 0);
  886. #endif /* WIN32 */
  887. }
  888. // Ignores exceptions thrown by Boost's create_directory if the requested directory exists.
  889. // Specifically handles case where path p exists, but it wasn't possible for the user to
  890. // write to the parent directory.
  891. bool TryCreateDirectory(const boost::filesystem::path& p)
  892. {
  893. try
  894. {
  895. return boost::filesystem::create_directory(p);
  896. } catch (boost::filesystem::filesystem_error) {
  897. if (!boost::filesystem::exists(p) || !boost::filesystem::is_directory(p))
  898. throw;
  899. }
  900. // create_directory didn't create the directory, it had to have existed already
  901. return false;
  902. }
  903. void FileCommit(FILE *fileout)
  904. {
  905. fflush(fileout); // harmless if redundantly called
  906. #ifdef WIN32
  907. HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(fileout));
  908. FlushFileBuffers(hFile);
  909. #else
  910. #if defined(__linux__) || defined(__NetBSD__)
  911. fdatasync(fileno(fileout));
  912. #elif defined(__APPLE__) && defined(F_FULLFSYNC)
  913. fcntl(fileno(fileout), F_FULLFSYNC, 0);
  914. #else
  915. fsync(fileno(fileout));
  916. #endif
  917. #endif
  918. }
  919. bool TruncateFile(FILE *file, unsigned int length) {
  920. #if defined(WIN32)
  921. return _chsize(_fileno(file), length) == 0;
  922. #else
  923. return ftruncate(fileno(file), length) == 0;
  924. #endif
  925. }
  926. // this function tries to raise the file descriptor limit to the requested number.
  927. // It returns the actual file descriptor limit (which may be more or less than nMinFD)
  928. int RaiseFileDescriptorLimit(int nMinFD) {
  929. #if defined(WIN32)
  930. return 2048;
  931. #else
  932. struct rlimit limitFD;
  933. if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
  934. if (limitFD.rlim_cur < (rlim_t)nMinFD) {
  935. limitFD.rlim_cur = nMinFD;
  936. if (limitFD.rlim_cur > limitFD.rlim_max)
  937. limitFD.rlim_cur = limitFD.rlim_max;
  938. setrlimit(RLIMIT_NOFILE, &limitFD);
  939. getrlimit(RLIMIT_NOFILE, &limitFD);
  940. }
  941. return limitFD.rlim_cur;
  942. }
  943. return nMinFD; // getrlimit failed, assume it's fine
  944. #endif
  945. }
  946. // this function tries to make a particular range of a file allocated (corresponding to disk space)
  947. // it is advisory, and the range specified in the arguments will never contain live data
  948. void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
  949. #if defined(WIN32)
  950. // Windows-specific version
  951. HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
  952. LARGE_INTEGER nFileSize;
  953. int64_t nEndPos = (int64_t)offset + length;
  954. nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
  955. nFileSize.u.HighPart = nEndPos >> 32;
  956. SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
  957. SetEndOfFile(hFile);
  958. #elif defined(MAC_OSX)
  959. // OSX specific version
  960. fstore_t fst;
  961. fst.fst_flags = F_ALLOCATECONTIG;
  962. fst.fst_posmode = F_PEOFPOSMODE;
  963. fst.fst_offset = 0;
  964. fst.fst_length = (off_t)offset + length;
  965. fst.fst_bytesalloc = 0;
  966. if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
  967. fst.fst_flags = F_ALLOCATEALL;
  968. fcntl(fileno(file), F_PREALLOCATE, &fst);
  969. }
  970. ftruncate(fileno(file), fst.fst_length);
  971. #elif defined(__linux__)
  972. // Version using posix_fallocate
  973. off_t nEndPos = (off_t)offset + length;
  974. posix_fallocate(fileno(file), 0, nEndPos);
  975. #else
  976. // Fallback version
  977. // TODO: just write one byte per block
  978. static const char buf[65536] = {};
  979. fseek(file, offset, SEEK_SET);
  980. while (length > 0) {
  981. unsigned int now = 65536;
  982. if (length < now)
  983. now = length;
  984. fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
  985. length -= now;
  986. }
  987. #endif
  988. }
  989. void ShrinkDebugFile()
  990. {
  991. // Scroll debug.log if it's getting too big
  992. boost::filesystem::path pathLog = GetDataDir() / "debug.log";
  993. FILE* file = fopen(pathLog.string().c_str(), "r");
  994. if (file && boost::filesystem::file_size(pathLog) > 10 * 1000000)
  995. {
  996. // Restart the file with some of the end
  997. std::vector <char> vch(200000,0);
  998. fseek(file, -vch.size(), SEEK_END);
  999. int nBytes = fread(begin_ptr(vch), 1, vch.size(), file);
  1000. fclose(file);
  1001. file = fopen(pathLog.string().c_str(), "w");
  1002. if (file)
  1003. {
  1004. fwrite(begin_ptr(vch), 1, nBytes, file);
  1005. fclose(file);
  1006. }
  1007. }
  1008. else if (file != NULL)
  1009. fclose(file);
  1010. }
  1011. static int64_t nMockTime = 0; // For unit testing
  1012. int64_t GetTime()
  1013. {
  1014. if (nMockTime) return nMockTime;
  1015. return time(NULL);
  1016. }
  1017. void SetMockTime(int64_t nMockTimeIn)
  1018. {
  1019. nMockTime = nMockTimeIn;
  1020. }
  1021. uint32_t insecure_rand_Rz = 11;
  1022. uint32_t insecure_rand_Rw = 11;
  1023. void seed_insecure_rand(bool fDeterministic)
  1024. {
  1025. //The seed values have some unlikely fixed points which we avoid.
  1026. if(fDeterministic)
  1027. {
  1028. insecure_rand_Rz = insecure_rand_Rw = 11;
  1029. } else {
  1030. uint32_t tmp;
  1031. do {
  1032. RAND_bytes((unsigned char*)&tmp, 4);
  1033. } while(tmp == 0 || tmp == 0x9068ffffU);
  1034. insecure_rand_Rz = tmp;
  1035. do {
  1036. RAND_bytes((unsigned char*)&tmp, 4);
  1037. } while(tmp == 0 || tmp == 0x464fffffU);
  1038. insecure_rand_Rw = tmp;
  1039. }
  1040. }
  1041. string FormatVersion(int nVersion)
  1042. {
  1043. if (nVersion%100 == 0)
  1044. return strprintf("%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100);
  1045. else
  1046. return strprintf("%d.%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100, nVersion%100);
  1047. }
  1048. string FormatFullVersion()
  1049. {
  1050. return CLIENT_BUILD;
  1051. }
  1052. // Format the subversion field according to BIP 14 spec (https://en.bitcoin.it/wiki/BIP_0014)
  1053. std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector<std::string>& comments)
  1054. {
  1055. std::ostringstream ss;
  1056. ss << "/";
  1057. ss << name << ":" << FormatVersion(nClientVersion);
  1058. if (!comments.empty())
  1059. ss << "(" << boost::algorithm::join(comments, "; ") << ")";
  1060. ss << "/";
  1061. return ss.str();
  1062. }
  1063. #ifdef WIN32
  1064. boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate)
  1065. {
  1066. namespace fs = boost::filesystem;
  1067. char pszPath[MAX_PATH] = "";
  1068. if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate))
  1069. {
  1070. return fs::path(pszPath);
  1071. }
  1072. LogPrintf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
  1073. return fs::path("");
  1074. }
  1075. #endif
  1076. boost::filesystem::path GetTempPath() {
  1077. #if BOOST_FILESYSTEM_VERSION == 3
  1078. return boost::filesystem::temp_directory_path();
  1079. #else
  1080. // TODO: remove when we don't support filesystem v2 anymore
  1081. boost::filesystem::path path;
  1082. #ifdef WIN32
  1083. char pszPath[MAX_PATH] = "";
  1084. if (GetTempPathA(MAX_PATH, pszPath))
  1085. path = boost::filesystem::path(pszPath);
  1086. #else
  1087. path = boost::filesystem::path("/tmp");
  1088. #endif
  1089. if (path.empty() || !boost::filesystem::is_directory(path)) {
  1090. LogPrintf("GetTempPath(): failed to find temp path\n");
  1091. return boost::filesystem::path("");
  1092. }
  1093. return path;
  1094. #endif
  1095. }
  1096. void runCommand(std::string strCommand)
  1097. {
  1098. int nErr = ::system(strCommand.c_str());
  1099. if (nErr)
  1100. LogPrintf("runCommand error: system(%s) returned %d\n", strCommand, nErr);
  1101. }
  1102. void RenameThread(const char* name)
  1103. {
  1104. #if defined(PR_SET_NAME)
  1105. // Only the first 15 characters are used (16 - NUL terminator)
  1106. ::prctl(PR_SET_NAME, name, 0, 0, 0);
  1107. #elif 0 && (defined(__FreeBSD__) || defined(__OpenBSD__))
  1108. // TODO: This is currently disabled because it needs to be verified to work
  1109. // on FreeBSD or OpenBSD first. When verified the '0 &&' part can be
  1110. // removed.
  1111. pthread_set_name_np(pthread_self(), name);
  1112. #elif defined(MAC_OSX) && defined(__MAC_OS_X_VERSION_MAX_ALLOWED)
  1113. // pthread_setname_np is XCode 10.6-and-later
  1114. #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
  1115. pthread_setname_np(name);
  1116. #endif
  1117. #else
  1118. // Prevent warnings for unused parameters...
  1119. (void)name;
  1120. #endif
  1121. }
  1122. bool ParseInt32(const std::string& str, int32_t *out)
  1123. {
  1124. char *endp = NULL;
  1125. errno = 0; // strtol will not set errno if valid
  1126. long int n = strtol(str.c_str(), &endp, 10);
  1127. if(out) *out = (int)n;
  1128. // Note that strtol returns a *long int*, so even if strtol doesn't report a over/underflow
  1129. // we still have to check that the returned value is within the range of an *int32_t*. On 64-bit
  1130. // platforms the size of these types may be different.
  1131. return endp && *endp == 0 && !errno &&
  1132. n >= std::numeric_limits<int32_t>::min() &&
  1133. n <= std::numeric_limits<int32_t>::max();
  1134. }
  1135. void SetupEnvironment()
  1136. {
  1137. #ifndef WIN32
  1138. try
  1139. {
  1140. #if BOOST_FILESYSTEM_VERSION == 3
  1141. boost::filesystem::path::codecvt(); // Raises runtime error if current locale is invalid
  1142. #else // boost filesystem v2
  1143. std::locale(); // Raises runtime error if current locale is invalid
  1144. #endif
  1145. } catch(std::runtime_error &e)
  1146. {
  1147. setenv("LC_ALL", "C", 1); // Force C locale
  1148. }
  1149. #endif
  1150. }
  1151. std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime)
  1152. {
  1153. // std::locale takes ownership of the pointer
  1154. std::locale loc(std::locale::classic(), new boost::posix_time::time_facet(pszFormat));
  1155. std::stringstream ss;
  1156. ss.imbue(loc);
  1157. ss << boost::posix_time::from_time_t(nTime);
  1158. return ss.str();
  1159. }
  1160. std::string FormatParagraph(const std::string in, size_t width, size_t indent)
  1161. {
  1162. std::stringstream out;
  1163. size_t col = 0;
  1164. size_t ptr = 0;
  1165. while(ptr < in.size())
  1166. {
  1167. // Find beginning of next word
  1168. ptr = in.find_first_not_of(' ', ptr);
  1169. if (ptr == std::string::npos)
  1170. break;
  1171. // Find end of next word
  1172. size_t endword = in.find_first_of(' ', ptr);
  1173. if (endword == std::string::npos)
  1174. endword = in.size();
  1175. // Add newline and indentation if this wraps over the allowed width
  1176. if (col > 0)
  1177. {
  1178. if ((col + endword - ptr) > width)
  1179. {
  1180. out << '\n';
  1181. for(size_t i=0; i<indent; ++i)
  1182. out << ' ';
  1183. col = 0;
  1184. } else
  1185. out << ' ';
  1186. }
  1187. // Append word
  1188. out << in.substr(ptr, endword - ptr);
  1189. col += endword - ptr;
  1190. ptr = endword;
  1191. }
  1192. return out.str();
  1193. }