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.h 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2012 The Bitcoin developers
  3. // Distributed under the MIT/X11 software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #ifndef BITCOIN_UTIL_H
  6. #define BITCOIN_UTIL_H
  7. #include "uint256.h"
  8. #include <stdarg.h>
  9. #ifndef WIN32
  10. #include <sys/types.h>
  11. #include <sys/time.h>
  12. #include <sys/resource.h>
  13. #endif
  14. #include <map>
  15. #include <list>
  16. #include <utility>
  17. #include <vector>
  18. #include <string>
  19. #include <boost/version.hpp>
  20. #include <boost/thread.hpp>
  21. #include <boost/filesystem.hpp>
  22. #include <boost/filesystem/path.hpp>
  23. #include <boost/date_time/gregorian/gregorian_types.hpp>
  24. #include <boost/date_time/posix_time/posix_time_types.hpp>
  25. #include "netbase.h" // for AddTimeData
  26. typedef long long int64;
  27. typedef unsigned long long uint64;
  28. static const int64 COIN = 100000000;
  29. static const int64 CENT = 1000000;
  30. #define loop for (;;)
  31. #define BEGIN(a) ((char*)&(a))
  32. #define END(a) ((char*)&((&(a))[1]))
  33. #define UBEGIN(a) ((unsigned char*)&(a))
  34. #define UEND(a) ((unsigned char*)&((&(a))[1]))
  35. #define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0]))
  36. #ifndef PRI64d
  37. #if defined(_MSC_VER) || defined(__MSVCRT__)
  38. #define PRI64d "I64d"
  39. #define PRI64u "I64u"
  40. #define PRI64x "I64x"
  41. #else
  42. #define PRI64d "lld"
  43. #define PRI64u "llu"
  44. #define PRI64x "llx"
  45. #endif
  46. #endif
  47. /* Format characters for (s)size_t and ptrdiff_t */
  48. #if defined(_MSC_VER) || defined(__MSVCRT__)
  49. /* (s)size_t and ptrdiff_t have the same size specifier in MSVC:
  50. http://msdn.microsoft.com/en-us/library/tcxf1dw6%28v=vs.100%29.aspx
  51. */
  52. #define PRIszx "Ix"
  53. #define PRIszu "Iu"
  54. #define PRIszd "Id"
  55. #define PRIpdx "Ix"
  56. #define PRIpdu "Iu"
  57. #define PRIpdd "Id"
  58. #else /* C99 standard */
  59. #define PRIszx "zx"
  60. #define PRIszu "zu"
  61. #define PRIszd "zd"
  62. #define PRIpdx "tx"
  63. #define PRIpdu "tu"
  64. #define PRIpdd "td"
  65. #endif
  66. // This is needed because the foreach macro can't get over the comma in pair<t1, t2>
  67. #define PAIRTYPE(t1, t2) std::pair<t1, t2>
  68. // Align by increasing pointer, must have extra space at end of buffer
  69. template <size_t nBytes, typename T>
  70. T* alignup(T* p)
  71. {
  72. union
  73. {
  74. T* ptr;
  75. size_t n;
  76. } u;
  77. u.ptr = p;
  78. u.n = (u.n + (nBytes-1)) & ~(nBytes-1);
  79. return u.ptr;
  80. }
  81. #ifdef WIN32
  82. #define MSG_NOSIGNAL 0
  83. #define MSG_DONTWAIT 0
  84. #ifndef S_IRUSR
  85. #define S_IRUSR 0400
  86. #define S_IWUSR 0200
  87. #endif
  88. #else
  89. #define MAX_PATH 1024
  90. #endif
  91. inline void MilliSleep(int64 n)
  92. {
  93. // Boost's sleep_for was uninterruptable when backed by nanosleep from 1.50
  94. // until fixed in 1.52. Use the deprecated sleep method for the broken case.
  95. // See: https://svn.boost.org/trac/boost/ticket/7238
  96. #if BOOST_VERSION >= 105000 && (!defined(BOOST_HAS_NANOSLEEP) || BOOST_VERSION >= 105200)
  97. boost::this_thread::sleep_for(boost::chrono::milliseconds(n));
  98. #else
  99. boost::this_thread::sleep(boost::posix_time::milliseconds(n));
  100. #endif
  101. }
  102. /* This GNU C extension enables the compiler to check the format string against the parameters provided.
  103. * X is the number of the "format string" parameter, and Y is the number of the first variadic parameter.
  104. * Parameters count from 1.
  105. */
  106. #ifdef __GNUC__
  107. #define ATTR_WARN_PRINTF(X,Y) __attribute__((format(printf,X,Y)))
  108. #else
  109. #define ATTR_WARN_PRINTF(X,Y)
  110. #endif
  111. extern std::map<std::string, std::string> mapArgs;
  112. extern std::map<std::string, std::vector<std::string> > mapMultiArgs;
  113. extern bool fDebug;
  114. extern bool fDebugNet;
  115. extern bool fPrintToConsole;
  116. extern bool fPrintToDebugger;
  117. extern bool fDaemon;
  118. extern bool fServer;
  119. extern bool fCommandLine;
  120. extern std::string strMiscWarning;
  121. extern bool fNoListen;
  122. extern bool fLogTimestamps;
  123. extern volatile bool fReopenDebugLog;
  124. void RandAddSeed();
  125. void RandAddSeedPerfmon();
  126. int ATTR_WARN_PRINTF(1,2) OutputDebugStringF(const char* pszFormat, ...);
  127. /*
  128. Rationale for the real_strprintf / strprintf construction:
  129. It is not allowed to use va_start with a pass-by-reference argument.
  130. (C++ standard, 18.7, paragraph 3). Use a dummy argument to work around this, and use a
  131. macro to keep similar semantics.
  132. */
  133. /** Overload strprintf for char*, so that GCC format type warnings can be given */
  134. std::string ATTR_WARN_PRINTF(1,3) real_strprintf(const char *format, int dummy, ...);
  135. /** Overload strprintf for std::string, to be able to use it with _ (translation).
  136. * This will not support GCC format type warnings (-Wformat) so be careful.
  137. */
  138. std::string real_strprintf(const std::string &format, int dummy, ...);
  139. #define strprintf(format, ...) real_strprintf(format, 0, __VA_ARGS__)
  140. std::string vstrprintf(const char *format, va_list ap);
  141. bool ATTR_WARN_PRINTF(1,2) error(const char *format, ...);
  142. /* Redefine printf so that it directs output to debug.log
  143. *
  144. * Do this *after* defining the other printf-like functions, because otherwise the
  145. * __attribute__((format(printf,X,Y))) gets expanded to __attribute__((format(OutputDebugStringF,X,Y)))
  146. * which confuses gcc.
  147. */
  148. #define printf OutputDebugStringF
  149. void LogException(std::exception* pex, const char* pszThread);
  150. void PrintException(std::exception* pex, const char* pszThread);
  151. void PrintExceptionContinue(std::exception* pex, const char* pszThread);
  152. void ParseString(const std::string& str, char c, std::vector<std::string>& v);
  153. std::string FormatMoney(int64 n, bool fPlus=false);
  154. bool ParseMoney(const std::string& str, int64& nRet);
  155. bool ParseMoney(const char* pszIn, int64& nRet);
  156. std::vector<unsigned char> ParseHex(const char* psz);
  157. std::vector<unsigned char> ParseHex(const std::string& str);
  158. bool IsHex(const std::string& str);
  159. std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid = NULL);
  160. std::string DecodeBase64(const std::string& str);
  161. std::string EncodeBase64(const unsigned char* pch, size_t len);
  162. std::string EncodeBase64(const std::string& str);
  163. std::vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid = NULL);
  164. std::string DecodeBase32(const std::string& str);
  165. std::string EncodeBase32(const unsigned char* pch, size_t len);
  166. std::string EncodeBase32(const std::string& str);
  167. void ParseParameters(int argc, const char*const argv[]);
  168. bool WildcardMatch(const char* psz, const char* mask);
  169. bool WildcardMatch(const std::string& str, const std::string& mask);
  170. void FileCommit(FILE *fileout);
  171. int GetFilesize(FILE* file);
  172. bool TruncateFile(FILE *file, unsigned int length);
  173. int RaiseFileDescriptorLimit(int nMinFD);
  174. void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
  175. bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest);
  176. boost::filesystem::path GetDefaultDataDir();
  177. const boost::filesystem::path &GetDataDir(bool fNetSpecific = true);
  178. boost::filesystem::path GetConfigFile();
  179. boost::filesystem::path GetPidFile();
  180. void CreatePidFile(const boost::filesystem::path &path, pid_t pid);
  181. void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet, std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet);
  182. #ifdef WIN32
  183. boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
  184. #endif
  185. boost::filesystem::path GetTempPath();
  186. void ShrinkDebugFile();
  187. int GetRandInt(int nMax);
  188. uint64 GetRand(uint64 nMax);
  189. uint256 GetRandHash();
  190. int64 GetTime();
  191. void SetMockTime(int64 nMockTimeIn);
  192. int64 GetAdjustedTime();
  193. int64 GetTimeOffset();
  194. std::string FormatFullVersion();
  195. std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector<std::string>& comments);
  196. void AddTimeData(const CNetAddr& ip, int64 nTime);
  197. void runCommand(std::string strCommand);
  198. inline std::string i64tostr(int64 n)
  199. {
  200. return strprintf("%"PRI64d, n);
  201. }
  202. inline std::string itostr(int n)
  203. {
  204. return strprintf("%d", n);
  205. }
  206. inline int64 atoi64(const char* psz)
  207. {
  208. #ifdef _MSC_VER
  209. return _atoi64(psz);
  210. #else
  211. return strtoll(psz, NULL, 10);
  212. #endif
  213. }
  214. inline int64 atoi64(const std::string& str)
  215. {
  216. #ifdef _MSC_VER
  217. return _atoi64(str.c_str());
  218. #else
  219. return strtoll(str.c_str(), NULL, 10);
  220. #endif
  221. }
  222. inline int atoi(const std::string& str)
  223. {
  224. return atoi(str.c_str());
  225. }
  226. inline int roundint(double d)
  227. {
  228. return (int)(d > 0 ? d + 0.5 : d - 0.5);
  229. }
  230. inline int64 roundint64(double d)
  231. {
  232. return (int64)(d > 0 ? d + 0.5 : d - 0.5);
  233. }
  234. inline int64 abs64(int64 n)
  235. {
  236. return (n >= 0 ? n : -n);
  237. }
  238. template<typename T>
  239. std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
  240. {
  241. std::string rv;
  242. static const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
  243. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  244. rv.reserve((itend-itbegin)*3);
  245. for(T it = itbegin; it < itend; ++it)
  246. {
  247. unsigned char val = (unsigned char)(*it);
  248. if(fSpaces && it != itbegin)
  249. rv.push_back(' ');
  250. rv.push_back(hexmap[val>>4]);
  251. rv.push_back(hexmap[val&15]);
  252. }
  253. return rv;
  254. }
  255. template<typename T>
  256. inline std::string HexStr(const T& vch, bool fSpaces=false)
  257. {
  258. return HexStr(vch.begin(), vch.end(), fSpaces);
  259. }
  260. template<typename T>
  261. void PrintHex(const T pbegin, const T pend, const char* pszFormat="%s", bool fSpaces=true)
  262. {
  263. printf(pszFormat, HexStr(pbegin, pend, fSpaces).c_str());
  264. }
  265. inline void PrintHex(const std::vector<unsigned char>& vch, const char* pszFormat="%s", bool fSpaces=true)
  266. {
  267. printf(pszFormat, HexStr(vch, fSpaces).c_str());
  268. }
  269. inline int64 GetPerformanceCounter()
  270. {
  271. int64 nCounter = 0;
  272. #ifdef WIN32
  273. QueryPerformanceCounter((LARGE_INTEGER*)&nCounter);
  274. #else
  275. timeval t;
  276. gettimeofday(&t, NULL);
  277. nCounter = (int64) t.tv_sec * 1000000 + t.tv_usec;
  278. #endif
  279. return nCounter;
  280. }
  281. inline int64 GetTimeMillis()
  282. {
  283. return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) -
  284. boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
  285. }
  286. inline int64 GetTimeMicros()
  287. {
  288. return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) -
  289. boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
  290. }
  291. inline std::string DateTimeStrFormat(const char* pszFormat, int64 nTime)
  292. {
  293. time_t n = nTime;
  294. struct tm* ptmTime = gmtime(&n);
  295. char pszTime[200];
  296. strftime(pszTime, sizeof(pszTime), pszFormat, ptmTime);
  297. return pszTime;
  298. }
  299. template<typename T>
  300. void skipspaces(T& it)
  301. {
  302. while (isspace(*it))
  303. ++it;
  304. }
  305. inline bool IsSwitchChar(char c)
  306. {
  307. #ifdef WIN32
  308. return c == '-' || c == '/';
  309. #else
  310. return c == '-';
  311. #endif
  312. }
  313. /**
  314. * Return string argument or default value
  315. *
  316. * @param strArg Argument to get (e.g. "-foo")
  317. * @param default (e.g. "1")
  318. * @return command-line argument or default value
  319. */
  320. std::string GetArg(const std::string& strArg, const std::string& strDefault);
  321. /**
  322. * Return integer argument or default value
  323. *
  324. * @param strArg Argument to get (e.g. "-foo")
  325. * @param default (e.g. 1)
  326. * @return command-line argument (0 if invalid number) or default value
  327. */
  328. int64 GetArg(const std::string& strArg, int64 nDefault);
  329. /**
  330. * Return boolean argument or default value
  331. *
  332. * @param strArg Argument to get (e.g. "-foo")
  333. * @param default (true or false)
  334. * @return command-line argument or default value
  335. */
  336. bool GetBoolArg(const std::string& strArg, bool fDefault);
  337. /**
  338. * Set an argument if it doesn't already have a value
  339. *
  340. * @param strArg Argument to set (e.g. "-foo")
  341. * @param strValue Value (e.g. "1")
  342. * @return true if argument gets set, false if it already had a value
  343. */
  344. bool SoftSetArg(const std::string& strArg, const std::string& strValue);
  345. /**
  346. * Set a boolean argument if it doesn't already have a value
  347. *
  348. * @param strArg Argument to set (e.g. "-foo")
  349. * @param fValue Value (e.g. false)
  350. * @return true if argument gets set, false if it already had a value
  351. */
  352. bool SoftSetBoolArg(const std::string& strArg, bool fValue);
  353. /**
  354. * MWC RNG of George Marsaglia
  355. * This is intended to be fast. It has a period of 2^59.3, though the
  356. * least significant 16 bits only have a period of about 2^30.1.
  357. *
  358. * @return random value
  359. */
  360. extern uint32_t insecure_rand_Rz;
  361. extern uint32_t insecure_rand_Rw;
  362. static inline uint32_t insecure_rand(void)
  363. {
  364. insecure_rand_Rz = 36969 * (insecure_rand_Rz & 65535) + (insecure_rand_Rz >> 16);
  365. insecure_rand_Rw = 18000 * (insecure_rand_Rw & 65535) + (insecure_rand_Rw >> 16);
  366. return (insecure_rand_Rw << 16) + insecure_rand_Rz;
  367. }
  368. /**
  369. * Seed insecure_rand using the random pool.
  370. * @param Deterministic Use a determinstic seed
  371. */
  372. void seed_insecure_rand(bool fDeterministic=false);
  373. /** Median filter over a stream of values.
  374. * Returns the median of the last N numbers
  375. */
  376. template <typename T> class CMedianFilter
  377. {
  378. private:
  379. std::vector<T> vValues;
  380. std::vector<T> vSorted;
  381. unsigned int nSize;
  382. public:
  383. CMedianFilter(unsigned int size, T initial_value):
  384. nSize(size)
  385. {
  386. vValues.reserve(size);
  387. vValues.push_back(initial_value);
  388. vSorted = vValues;
  389. }
  390. void input(T value)
  391. {
  392. if(vValues.size() == nSize)
  393. {
  394. vValues.erase(vValues.begin());
  395. }
  396. vValues.push_back(value);
  397. vSorted.resize(vValues.size());
  398. std::copy(vValues.begin(), vValues.end(), vSorted.begin());
  399. std::sort(vSorted.begin(), vSorted.end());
  400. }
  401. T median() const
  402. {
  403. int size = vSorted.size();
  404. assert(size>0);
  405. if(size & 1) // Odd number of elements
  406. {
  407. return vSorted[size/2];
  408. }
  409. else // Even number of elements
  410. {
  411. return (vSorted[size/2-1] + vSorted[size/2]) / 2;
  412. }
  413. }
  414. int size() const
  415. {
  416. return vValues.size();
  417. }
  418. std::vector<T> sorted () const
  419. {
  420. return vSorted;
  421. }
  422. };
  423. #ifdef WIN32
  424. inline void SetThreadPriority(int nPriority)
  425. {
  426. SetThreadPriority(GetCurrentThread(), nPriority);
  427. }
  428. #else
  429. #define THREAD_PRIORITY_LOWEST PRIO_MAX
  430. #define THREAD_PRIORITY_BELOW_NORMAL 2
  431. #define THREAD_PRIORITY_NORMAL 0
  432. #define THREAD_PRIORITY_ABOVE_NORMAL (-2)
  433. inline void SetThreadPriority(int nPriority)
  434. {
  435. // It's unclear if it's even possible to change thread priorities on Linux,
  436. // but we really and truly need it for the generation threads.
  437. #ifdef PRIO_THREAD
  438. setpriority(PRIO_THREAD, 0, nPriority);
  439. #else
  440. setpriority(PRIO_PROCESS, 0, nPriority);
  441. #endif
  442. }
  443. #endif
  444. void RenameThread(const char* name);
  445. inline uint32_t ByteReverse(uint32_t value)
  446. {
  447. value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
  448. return (value<<16) | (value>>16);
  449. }
  450. // Standard wrapper for do-something-forever thread functions.
  451. // "Forever" really means until the thread is interrupted.
  452. // Use it like:
  453. // new boost::thread(boost::bind(&LoopForever<void (*)()>, "dumpaddr", &DumpAddresses, 10000));
  454. // or maybe:
  455. // boost::function<void()> f = boost::bind(&FunctionWithArg, argument);
  456. // threadGroup.create_thread(boost::bind(&LoopForever<boost::function<void()> >, "nothing", f, milliseconds));
  457. template <typename Callable> void LoopForever(const char* name, Callable func, int64 msecs)
  458. {
  459. std::string s = strprintf("bitcoin-%s", name);
  460. RenameThread(s.c_str());
  461. printf("%s thread start\n", name);
  462. try
  463. {
  464. while (1)
  465. {
  466. func();
  467. MilliSleep(msecs);
  468. }
  469. }
  470. catch (boost::thread_interrupted)
  471. {
  472. printf("%s thread stop\n", name);
  473. throw;
  474. }
  475. catch (std::exception& e) {
  476. PrintException(&e, name);
  477. }
  478. catch (...) {
  479. PrintException(NULL, name);
  480. }
  481. }
  482. // .. and a wrapper that just calls func once
  483. template <typename Callable> void TraceThread(const char* name, Callable func)
  484. {
  485. std::string s = strprintf("bitcoin-%s", name);
  486. RenameThread(s.c_str());
  487. try
  488. {
  489. printf("%s thread start\n", name);
  490. func();
  491. printf("%s thread exit\n", name);
  492. }
  493. catch (boost::thread_interrupted)
  494. {
  495. printf("%s thread interrupt\n", name);
  496. throw;
  497. }
  498. catch (std::exception& e) {
  499. PrintException(&e, name);
  500. }
  501. catch (...) {
  502. PrintException(NULL, name);
  503. }
  504. }
  505. #endif