experimental-genesis 1
experimental-genesis 2
experimental-genesis 3
experimental-genesis 4
experimental-genesis 5
experimental-genesis 6
experimental-genesis 7
experimental-genesis 8
experimental-genesis 9
experimental-genesis 10
experimental-genesis 11
experimental-genesis 12
experimental-genesis 13
experimental-genesis 14
experimental-genesis 15
experimental-genesis 16
experimental-genesis 17
experimental-genesis 18
experimental-genesis 19
experimental-genesis 20
experimental-genesis 21
experimental-genesis 22
experimental-genesis 23
experimental-genesis 24
experimental-genesis 25
experimental-genesis 26
experimental-genesis 27
experimental-genesis 28
experimental-genesis 29
experimental-genesis 30
experimental-genesis 31
experimental-genesis 32
experimental-genesis 33
experimental-genesis 34
experimental-genesis 35
experimental-genesis 36 #ifndef BITCOIN_UTIL_H
experimental-genesis 37 #define BITCOIN_UTIL_H
experimental-genesis 38
experimental-genesis 39 #include "uint256.h"
experimental-genesis 40
experimental-genesis 41 #ifndef WIN32
experimental-genesis 42 #include <sys/types.h>
experimental-genesis 43 #include <sys/time.h>
experimental-genesis 44 #include <sys/resource.h>
experimental-genesis 45 #endif
experimental-genesis 46 #include <map>
experimental-genesis 47 #include <vector>
experimental-genesis 48 #include <string>
experimental-genesis 49
experimental-genesis 50 #include <boost/thread.hpp>
experimental-genesis 51 #include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>
experimental-genesis 52 #include <boost/date_time/gregorian/gregorian_types.hpp>
experimental-genesis 53 #include <boost/date_time/posix_time/posix_time_types.hpp>
experimental-genesis 54
experimental-genesis 55 #include <openssl/sha.h>
experimental-genesis 56 #include <openssl/ripemd.h>
experimental-genesis 57
experimental-genesis 58
experimental-genesis 59 #if defined(_MSC_VER) || defined(__BORLANDC__)
experimental-genesis 60 typedef __int64 int64;
experimental-genesis 61 typedef unsigned __int64 uint64;
experimental-genesis 62 #else
experimental-genesis 63 typedef long long int64;
experimental-genesis 64 typedef unsigned long long uint64;
experimental-genesis 65 #endif
experimental-genesis 66 #if defined(_MSC_VER) && _MSC_VER < 1300
experimental-genesis 67 #define for if (false) ; else for
experimental-genesis 68 #endif
experimental-genesis 69 #ifndef _MSC_VER
experimental-genesis 70 #define __forceinline inline
experimental-genesis 71 #endif
experimental-genesis 72
experimental-genesis 73 #define loop for (;;)
experimental-genesis 74 #define BEGIN(a) ((char*)&(a))
experimental-genesis 75 #define END(a) ((char*)&((&(a))[1]))
experimental-genesis 76 #define UBEGIN(a) ((unsigned char*)&(a))
experimental-genesis 77 #define UEND(a) ((unsigned char*)&((&(a))[1]))
experimental-genesis 78 #define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0]))
experimental-genesis 79 #define printf OutputDebugStringF
experimental-genesis 80
experimental-genesis 81 #ifdef snprintf
experimental-genesis 82 #undef snprintf
experimental-genesis 83 #endif
experimental-genesis 84 #define snprintf my_snprintf
experimental-genesis 85
experimental-genesis 86 #ifndef PRI64d
experimental-genesis 87 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MSVCRT__)
experimental-genesis 88 #define PRI64d "I64d"
experimental-genesis 89 #define PRI64u "I64u"
experimental-genesis 90 #define PRI64x "I64x"
experimental-genesis 91 #else
experimental-genesis 92 #define PRI64d "lld"
experimental-genesis 93 #define PRI64u "llu"
experimental-genesis 94 #define PRI64x "llx"
experimental-genesis 95 #endif
experimental-genesis 96 #endif
experimental-genesis 97
experimental-genesis 98
experimental-genesis 99 #define PAIRTYPE(t1, t2) std::pair<t1, t2>
experimental-genesis 100
experimental-genesis 101
experimental-genesis 102 template <size_t nBytes, typename T>
experimental-genesis 103 T* alignup(T* p)
experimental-genesis 104 {
experimental-genesis 105 union
experimental-genesis 106 {
experimental-genesis 107 T* ptr;
experimental-genesis 108 size_t n;
experimental-genesis 109 } u;
experimental-genesis 110 u.ptr = p;
experimental-genesis 111 u.n = (u.n + (nBytes-1)) & ~(nBytes-1);
experimental-genesis 112 return u.ptr;
experimental-genesis 113 }
experimental-genesis 114
experimental-genesis 115 #ifdef WIN32
experimental-genesis 116 #define MSG_NOSIGNAL 0
experimental-genesis 117 #define MSG_DONTWAIT 0
experimental-genesis 118 #ifndef UINT64_MAX
experimental-genesis 119 #define UINT64_MAX _UI64_MAX
experimental-genesis 120 #define INT64_MAX _I64_MAX
experimental-genesis 121 #define INT64_MIN _I64_MIN
experimental-genesis 122 #endif
experimental-genesis 123 #ifndef S_IRUSR
experimental-genesis 124 #define S_IRUSR 0400
experimental-genesis 125 #define S_IWUSR 0200
experimental-genesis 126 #endif
experimental-genesis 127 #define unlink _unlink
experimental-genesis 128 typedef int socklen_t;
experimental-genesis 129 #else
experimental-genesis 130 #define WSAGetLastError() errno
experimental-genesis 131 #define WSAEINVAL EINVAL
experimental-genesis 132 #define WSAEALREADY EALREADY
experimental-genesis 133 #define WSAEWOULDBLOCK EWOULDBLOCK
experimental-genesis 134 #define WSAEMSGSIZE EMSGSIZE
experimental-genesis 135 #define WSAEINTR EINTR
experimental-genesis 136 #define WSAEINPROGRESS EINPROGRESS
experimental-genesis 137 #define WSAEADDRINUSE EADDRINUSE
experimental-genesis 138 #define WSAENOTSOCK EBADF
experimental-genesis 139 #define INVALID_SOCKET (SOCKET)(~0)
experimental-genesis 140 #define SOCKET_ERROR -1
experimental-genesis 141 typedef u_int SOCKET;
experimental-genesis 142 #define _vsnprintf(a,b,c,d) vsnprintf(a,b,c,d)
experimental-genesis 143 #define strlwr(psz) to_lower(psz)
experimental-genesis 144 #define _strlwr(psz) to_lower(psz)
experimental-genesis 145 #define MAX_PATH 1024
experimental-genesis 146 #define Beep(n1,n2) (0)
experimental-genesis 147 inline void Sleep(int64 n)
experimental-genesis 148 {
experimental-genesis 149
experimental-genesis 150 So we clamp our sleeps here to 10 years and hope that boost is fixed by 2028.*/
experimental-genesis 151 boost::thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(n>315576000000LL?315576000000LL:n));
experimental-genesis 152 }
experimental-genesis 153 #endif
experimental-genesis 154
experimental-genesis 155 inline int myclosesocket(SOCKET& hSocket)
experimental-genesis 156 {
experimental-genesis 157 if (hSocket == INVALID_SOCKET)
experimental-genesis 158 return WSAENOTSOCK;
experimental-genesis 159 #ifdef WIN32
experimental-genesis 160 int ret = closesocket(hSocket);
experimental-genesis 161 #else
experimental-genesis 162 int ret = close(hSocket);
experimental-genesis 163 #endif
experimental-genesis 164 hSocket = INVALID_SOCKET;
experimental-genesis 165 return ret;
experimental-genesis 166 }
experimental-genesis 167 #define closesocket(s) myclosesocket(s)
experimental-genesis 168 #if !defined(QT_GUI)
experimental-genesis 169 inline const char* _(const char* psz)
experimental-genesis 170 {
experimental-genesis 171 return psz;
experimental-genesis 172 }
experimental-genesis 173 #endif
experimental-genesis 174
experimental-genesis 175
experimental-genesis 176
experimental-genesis 177
experimental-genesis 178
experimental-genesis 179
experimental-genesis 180
experimental-genesis 181
experimental-genesis 182
experimental-genesis 183 extern std::map<std::string, std::string> mapArgs;
experimental-genesis 184 extern std::map<std::string, std::vector<std::string> > mapMultiArgs;
experimental-genesis 185 extern bool fDebug;
experimental-genesis 186 extern bool fPrintToConsole;
experimental-genesis 187 extern bool fPrintToDebugger;
experimental-genesis 188 extern char pszSetDataDir[MAX_PATH];
experimental-genesis 189 extern bool fRequestShutdown;
experimental-genesis 190 extern bool fShutdown;
experimental-genesis 191 extern bool fDaemon;
experimental-genesis 192 extern bool fServer;
experimental-genesis 193 extern bool fCommandLine;
experimental-genesis 194 extern std::string strMiscWarning;
experimental-genesis 195 extern bool fTestNet;
experimental-genesis 196 extern bool fNoListen;
experimental-genesis 197 extern bool fLogTimestamps;
experimental-genesis 198
experimental-genesis 199 void RandAddSeed();
experimental-genesis 200 void RandAddSeedPerfmon();
experimental-genesis 201 int OutputDebugStringF(const char* pszFormat, ...);
experimental-genesis 202 int my_snprintf(char* buffer, size_t limit, const char* format, ...);
experimental-genesis 203 std::string strprintf(const std::string &format, ...);
experimental-genesis 204 bool error(const std::string &format, ...);
experimental-genesis 205 void LogException(std::exception* pex, const char* pszThread);
experimental-genesis 206 void PrintException(std::exception* pex, const char* pszThread);
experimental-genesis 207 void PrintExceptionContinue(std::exception* pex, const char* pszThread);
experimental-genesis 208 void ParseString(const std::string& str, char c, std::vector<std::string>& v);
experimental-genesis 209 std::string FormatMoney(int64 n, bool fPlus=false);
experimental-genesis 210 bool ParseMoney(const std::string& str, int64& nRet);
experimental-genesis 211 bool ParseMoney(const char* pszIn, int64& nRet);
experimental-genesis 212 std::vector<unsigned char> ParseHex(const char* psz);
experimental-genesis 213 std::vector<unsigned char> ParseHex(const std::string& str);
experimental-genesis 214 std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid = NULL);
experimental-genesis 215 std::string DecodeBase64(const std::string& str);
experimental-genesis 216 std::string EncodeBase64(const unsigned char* pch, size_t len);
experimental-genesis 217 std::string EncodeBase64(const std::string& str);
experimental-genesis 218 void ParseParameters(int argc, char* argv[]);
experimental-genesis 219 const char* wxGetTranslation(const char* psz);
experimental-genesis 220 bool WildcardMatch(const char* psz, const char* mask);
experimental-genesis 221 bool WildcardMatch(const std::string& str, const std::string& mask);
experimental-genesis 222 int GetFilesize(FILE* file);
experimental-genesis 223 void GetDataDir(char* pszDirRet);
experimental-genesis 224 std::string GetConfigFile();
experimental-genesis 225 std::string GetPidFile();
experimental-genesis 226 void CreatePidFile(std::string pidFile, pid_t pid);
experimental-genesis 227 void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet, std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet);
experimental-genesis 228 #ifdef WIN32
experimental-genesis 229 std::string MyGetSpecialFolderPath(int nFolder, bool fCreate);
experimental-genesis 230 #endif
experimental-genesis 231 std::string GetDefaultDataDir();
experimental-genesis 232 std::string GetDataDir();
experimental-genesis 233 void ShrinkDebugFile();
experimental-genesis 234 int GetRandInt(int nMax);
experimental-genesis 235 uint64 GetRand(uint64 nMax);
experimental-genesis 236 int64 GetTime();
experimental-genesis 237 void SetMockTime(int64 nMockTimeIn);
experimental-genesis 238 int64 GetAdjustedTime();
experimental-genesis 239 void AddTimeData(unsigned int ip, int64 nTime);
experimental-genesis 240 std::string FormatFullVersion();
experimental-genesis 241
experimental-genesis 242
experimental-genesis 243
experimental-genesis 244
experimental-genesis 245
experimental-genesis 246
experimental-genesis 247
experimental-genesis 248
experimental-genesis 249
experimental-genesis 250
experimental-genesis 251
experimental-genesis 252
experimental-genesis 253
experimental-genesis 254
experimental-genesis 255 class CCriticalSection
experimental-genesis 256 {
experimental-genesis 257 protected:
experimental-genesis 258 boost::interprocess::interprocess_recursive_mutex mutex;
experimental-genesis 259 public:
experimental-genesis 260 explicit CCriticalSection() { }
experimental-genesis 261 ~CCriticalSection() { }
experimental-genesis 262 void Enter(const char* pszName, const char* pszFile, int nLine);
experimental-genesis 263 void Leave();
experimental-genesis 264 bool TryEnter(const char* pszName, const char* pszFile, int nLine);
experimental-genesis 265 };
experimental-genesis 266
experimental-genesis 267
experimental-genesis 268 class CCriticalBlock
experimental-genesis 269 {
experimental-genesis 270 protected:
experimental-genesis 271 CCriticalSection* pcs;
experimental-genesis 272
experimental-genesis 273 public:
experimental-genesis 274 CCriticalBlock(CCriticalSection& csIn, const char* pszName, const char* pszFile, int nLine)
experimental-genesis 275 {
experimental-genesis 276 pcs = &csIn;
experimental-genesis 277 pcs->Enter(pszName, pszFile, nLine);
experimental-genesis 278 }
experimental-genesis 279
experimental-genesis 280 operator bool() const
experimental-genesis 281 {
experimental-genesis 282 return true;
experimental-genesis 283 }
experimental-genesis 284
experimental-genesis 285 ~CCriticalBlock()
experimental-genesis 286 {
experimental-genesis 287 pcs->Leave();
experimental-genesis 288 }
experimental-genesis 289 };
experimental-genesis 290
experimental-genesis 291 #define CRITICAL_BLOCK(cs) \
experimental-genesis 292 if (CCriticalBlock criticalblock = CCriticalBlock(cs, #cs, __FILE__, __LINE__))
experimental-genesis 293
experimental-genesis 294 #define ENTER_CRITICAL_SECTION(cs) \
experimental-genesis 295 (cs).Enter(#cs, __FILE__, __LINE__)
experimental-genesis 296
experimental-genesis 297 #define LEAVE_CRITICAL_SECTION(cs) \
experimental-genesis 298 (cs).Leave()
experimental-genesis 299
experimental-genesis 300 class CTryCriticalBlock
experimental-genesis 301 {
experimental-genesis 302 protected:
experimental-genesis 303 CCriticalSection* pcs;
experimental-genesis 304
experimental-genesis 305 public:
experimental-genesis 306 CTryCriticalBlock(CCriticalSection& csIn, const char* pszName, const char* pszFile, int nLine)
experimental-genesis 307 {
experimental-genesis 308 pcs = (csIn.TryEnter(pszName, pszFile, nLine) ? &csIn : NULL);
experimental-genesis 309 }
experimental-genesis 310
experimental-genesis 311 operator bool() const
experimental-genesis 312 {
experimental-genesis 313 return Entered();
experimental-genesis 314 }
experimental-genesis 315
experimental-genesis 316 ~CTryCriticalBlock()
experimental-genesis 317 {
experimental-genesis 318 if (pcs)
experimental-genesis 319 {
experimental-genesis 320 pcs->Leave();
experimental-genesis 321 }
experimental-genesis 322 }
experimental-genesis 323 bool Entered() const { return pcs != NULL; }
experimental-genesis 324 };
experimental-genesis 325
experimental-genesis 326 #define TRY_CRITICAL_BLOCK(cs) \
experimental-genesis 327 if (CTryCriticalBlock criticalblock = CTryCriticalBlock(cs, #cs, __FILE__, __LINE__))
experimental-genesis 328
experimental-genesis 329
experimental-genesis 330
experimental-genesis 331
experimental-genesis 332
experimental-genesis 333
experimental-genesis 334
experimental-genesis 335
experimental-genesis 336 typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;
experimental-genesis 337
experimental-genesis 338
experimental-genesis 339
experimental-genesis 340 typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;
experimental-genesis 341
experimental-genesis 342
experimental-genesis 343
experimental-genesis 344
experimental-genesis 345
experimental-genesis 346 inline std::string i64tostr(int64 n)
experimental-genesis 347 {
experimental-genesis 348 return strprintf("%"PRI64d, n);
experimental-genesis 349 }
experimental-genesis 350
experimental-genesis 351 inline std::string itostr(int n)
experimental-genesis 352 {
experimental-genesis 353 return strprintf("%d", n);
experimental-genesis 354 }
experimental-genesis 355
experimental-genesis 356 inline int64 atoi64(const char* psz)
experimental-genesis 357 {
experimental-genesis 358 #ifdef _MSC_VER
experimental-genesis 359 return _atoi64(psz);
experimental-genesis 360 #else
experimental-genesis 361 return strtoll(psz, NULL, 10);
experimental-genesis 362 #endif
experimental-genesis 363 }
experimental-genesis 364
experimental-genesis 365 inline int64 atoi64(const std::string& str)
experimental-genesis 366 {
experimental-genesis 367 #ifdef _MSC_VER
experimental-genesis 368 return _atoi64(str.c_str());
experimental-genesis 369 #else
experimental-genesis 370 return strtoll(str.c_str(), NULL, 10);
experimental-genesis 371 #endif
experimental-genesis 372 }
experimental-genesis 373
experimental-genesis 374 inline int atoi(const std::string& str)
experimental-genesis 375 {
experimental-genesis 376 return atoi(str.c_str());
experimental-genesis 377 }
experimental-genesis 378
experimental-genesis 379 inline int roundint(double d)
experimental-genesis 380 {
experimental-genesis 381 return (int)(d > 0 ? d + 0.5 : d - 0.5);
experimental-genesis 382 }
experimental-genesis 383
experimental-genesis 384 inline int64 roundint64(double d)
experimental-genesis 385 {
experimental-genesis 386 return (int64)(d > 0 ? d + 0.5 : d - 0.5);
experimental-genesis 387 }
experimental-genesis 388
experimental-genesis 389 inline int64 abs64(int64 n)
experimental-genesis 390 {
experimental-genesis 391 return (n >= 0 ? n : -n);
experimental-genesis 392 }
experimental-genesis 393
experimental-genesis 394 template<typename T>
experimental-genesis 395 std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
experimental-genesis 396 {
experimental-genesis 397 if (itbegin == itend)
experimental-genesis 398 return "";
experimental-genesis 399 const unsigned char* pbegin = (const unsigned char*)&itbegin[0];
experimental-genesis 400 const unsigned char* pend = pbegin + (itend - itbegin) * sizeof(itbegin[0]);
experimental-genesis 401 std::string str;
experimental-genesis 402 str.reserve((pend-pbegin) * (fSpaces ? 3 : 2));
experimental-genesis 403 for (const unsigned char* p = pbegin; p != pend; p++)
experimental-genesis 404 str += strprintf((fSpaces && p != pend-1 ? "%02x " : "%02x"), *p);
experimental-genesis 405 return str;
experimental-genesis 406 }
experimental-genesis 407
experimental-genesis 408 inline std::string HexStr(const std::vector<unsigned char>& vch, bool fSpaces=false)
experimental-genesis 409 {
experimental-genesis 410 return HexStr(vch.begin(), vch.end(), fSpaces);
experimental-genesis 411 }
experimental-genesis 412
experimental-genesis 413 template<typename T>
experimental-genesis 414 std::string HexNumStr(const T itbegin, const T itend, bool f0x=true)
experimental-genesis 415 {
experimental-genesis 416 if (itbegin == itend)
experimental-genesis 417 return "";
experimental-genesis 418 const unsigned char* pbegin = (const unsigned char*)&itbegin[0];
experimental-genesis 419 const unsigned char* pend = pbegin + (itend - itbegin) * sizeof(itbegin[0]);
experimental-genesis 420 std::string str = (f0x ? "0x" : "");
experimental-genesis 421 str.reserve(str.size() + (pend-pbegin) * 2);
experimental-genesis 422 for (const unsigned char* p = pend-1; p >= pbegin; p--)
experimental-genesis 423 str += strprintf("%02x", *p);
experimental-genesis 424 return str;
experimental-genesis 425 }
experimental-genesis 426
experimental-genesis 427 inline std::string HexNumStr(const std::vector<unsigned char>& vch, bool f0x=true)
experimental-genesis 428 {
experimental-genesis 429 return HexNumStr(vch.begin(), vch.end(), f0x);
experimental-genesis 430 }
experimental-genesis 431
experimental-genesis 432 template<typename T>
experimental-genesis 433 void PrintHex(const T pbegin, const T pend, const char* pszFormat="%s", bool fSpaces=true)
experimental-genesis 434 {
experimental-genesis 435 printf(pszFormat, HexStr(pbegin, pend, fSpaces).c_str());
experimental-genesis 436 }
experimental-genesis 437
experimental-genesis 438 inline void PrintHex(const std::vector<unsigned char>& vch, const char* pszFormat="%s", bool fSpaces=true)
experimental-genesis 439 {
experimental-genesis 440 printf(pszFormat, HexStr(vch, fSpaces).c_str());
experimental-genesis 441 }
experimental-genesis 442
experimental-genesis 443 inline int64 GetPerformanceCounter()
experimental-genesis 444 {
experimental-genesis 445 int64 nCounter = 0;
experimental-genesis 446 #ifdef WIN32
experimental-genesis 447 QueryPerformanceCounter((LARGE_INTEGER*)&nCounter);
experimental-genesis 448 #else
experimental-genesis 449 timeval t;
experimental-genesis 450 gettimeofday(&t, NULL);
experimental-genesis 451 nCounter = t.tv_sec * 1000000 + t.tv_usec;
experimental-genesis 452 #endif
experimental-genesis 453 return nCounter;
experimental-genesis 454 }
experimental-genesis 455
experimental-genesis 456 inline int64 GetTimeMillis()
experimental-genesis 457 {
experimental-genesis 458 return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) -
experimental-genesis 459 boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
experimental-genesis 460 }
experimental-genesis 461
experimental-genesis 462 inline std::string DateTimeStrFormat(const char* pszFormat, int64 nTime)
experimental-genesis 463 {
experimental-genesis 464 time_t n = nTime;
experimental-genesis 465 struct tm* ptmTime = gmtime(&n);
experimental-genesis 466 char pszTime[200];
experimental-genesis 467 strftime(pszTime, sizeof(pszTime), pszFormat, ptmTime);
experimental-genesis 468 return pszTime;
experimental-genesis 469 }
experimental-genesis 470
experimental-genesis 471 template<typename T>
experimental-genesis 472 void skipspaces(T& it)
experimental-genesis 473 {
experimental-genesis 474 while (isspace(*it))
experimental-genesis 475 ++it;
experimental-genesis 476 }
experimental-genesis 477
experimental-genesis 478 inline bool IsSwitchChar(char c)
experimental-genesis 479 {
experimental-genesis 480 #ifdef WIN32
experimental-genesis 481 return c == '-' || c == '/';
experimental-genesis 482 #else
experimental-genesis 483 return c == '-';
experimental-genesis 484 #endif
experimental-genesis 485 }
experimental-genesis 486
experimental-genesis 487 inline std::string GetArg(const std::string& strArg, const std::string& strDefault)
experimental-genesis 488 {
experimental-genesis 489 if (mapArgs.count(strArg))
experimental-genesis 490 return mapArgs[strArg];
experimental-genesis 491 return strDefault;
experimental-genesis 492 }
experimental-genesis 493
experimental-genesis 494 inline int64 GetArg(const std::string& strArg, int64 nDefault)
experimental-genesis 495 {
experimental-genesis 496 if (mapArgs.count(strArg))
experimental-genesis 497 return atoi64(mapArgs[strArg]);
experimental-genesis 498 return nDefault;
experimental-genesis 499 }
experimental-genesis 500
experimental-genesis 501 inline bool GetBoolArg(const std::string& strArg, bool fDefault=false)
experimental-genesis 502 {
experimental-genesis 503 if (mapArgs.count(strArg))
experimental-genesis 504 {
experimental-genesis 505 if (mapArgs[strArg].empty())
experimental-genesis 506 return true;
experimental-genesis 507 return (atoi(mapArgs[strArg]) != 0);
experimental-genesis 508 }
experimental-genesis 509 return fDefault;
experimental-genesis 510 }
experimental-genesis 511
experimental-genesis 512
experimental-genesis 513 * Set an argument if it doesn't already have a value
experimental-genesis 514 *
experimental-genesis 515 * @param strArg Argument to set (e.g. "-foo")
experimental-genesis 516 * @param strValue Value (e.g. "1")
experimental-genesis 517 * @return true if argument gets set, false if it already had a value
experimental-genesis 518 */
experimental-genesis 519 bool SoftSetArg(const std::string& strArg, const std::string& strValue);
experimental-genesis 520
experimental-genesis 521
experimental-genesis 522 * Set a boolean argument if it doesn't already have a value
experimental-genesis 523 *
experimental-genesis 524 * @param strArg Argument to set (e.g. "-foo")
experimental-genesis 525 * @param fValue Value (e.g. false)
experimental-genesis 526 * @return true if argument gets set, false if it already had a value
experimental-genesis 527 */
experimental-genesis 528 bool SoftSetArg(const std::string& strArg, bool fValue);
experimental-genesis 529
experimental-genesis 530
experimental-genesis 531
experimental-genesis 532
experimental-genesis 533
experimental-genesis 534
experimental-genesis 535
experimental-genesis 536
experimental-genesis 537
experimental-genesis 538 inline void heapchk()
experimental-genesis 539 {
experimental-genesis 540 #ifdef WIN32
experimental-genesis 541
experimental-genesis 542
experimental-genesis 543
experimental-genesis 544 #endif
experimental-genesis 545 }
experimental-genesis 546
experimental-genesis 547
experimental-genesis 548 #define IMPLEMENT_RANDOMIZE_STACK(ThreadFn) \
experimental-genesis 549 { \
experimental-genesis 550 static char nLoops; \
experimental-genesis 551 if (nLoops <= 0) \
experimental-genesis 552 nLoops = GetRand(20) + 1; \
experimental-genesis 553 if (nLoops-- > 1) \
experimental-genesis 554 { \
experimental-genesis 555 ThreadFn; \
experimental-genesis 556 return; \
experimental-genesis 557 } \
experimental-genesis 558 }
experimental-genesis 559
experimental-genesis 560 #define CATCH_PRINT_EXCEPTION(pszFn) \
experimental-genesis 561 catch (std::exception& e) { \
experimental-genesis 562 PrintException(&e, (pszFn)); \
experimental-genesis 563 } catch (...) { \
experimental-genesis 564 PrintException(NULL, (pszFn)); \
experimental-genesis 565 }
experimental-genesis 566
experimental-genesis 567
experimental-genesis 568
experimental-genesis 569
experimental-genesis 570
experimental-genesis 571
experimental-genesis 572
experimental-genesis 573
experimental-genesis 574
experimental-genesis 575
experimental-genesis 576 template<typename T1>
experimental-genesis 577 inline uint256 Hash(const T1 pbegin, const T1 pend)
experimental-genesis 578 {
experimental-genesis 579 static unsigned char pblank[1];
experimental-genesis 580 uint256 hash1;
experimental-genesis 581 SHA256((pbegin == pend ? pblank : (unsigned char*)&pbegin[0]), (pend - pbegin) * sizeof(pbegin[0]), (unsigned char*)&hash1);
experimental-genesis 582 uint256 hash2;
experimental-genesis 583 SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
experimental-genesis 584 return hash2;
experimental-genesis 585 }
experimental-genesis 586
experimental-genesis 587 template<typename T1, typename T2>
experimental-genesis 588 inline uint256 Hash(const T1 p1begin, const T1 p1end,
experimental-genesis 589 const T2 p2begin, const T2 p2end)
experimental-genesis 590 {
experimental-genesis 591 static unsigned char pblank[1];
experimental-genesis 592 uint256 hash1;
experimental-genesis 593 SHA256_CTX ctx;
experimental-genesis 594 SHA256_Init(&ctx);
experimental-genesis 595 SHA256_Update(&ctx, (p1begin == p1end ? pblank : (unsigned char*)&p1begin[0]), (p1end - p1begin) * sizeof(p1begin[0]));
experimental-genesis 596 SHA256_Update(&ctx, (p2begin == p2end ? pblank : (unsigned char*)&p2begin[0]), (p2end - p2begin) * sizeof(p2begin[0]));
experimental-genesis 597 SHA256_Final((unsigned char*)&hash1, &ctx);
experimental-genesis 598 uint256 hash2;
experimental-genesis 599 SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
experimental-genesis 600 return hash2;
experimental-genesis 601 }
experimental-genesis 602
experimental-genesis 603 template<typename T1, typename T2, typename T3>
experimental-genesis 604 inline uint256 Hash(const T1 p1begin, const T1 p1end,
experimental-genesis 605 const T2 p2begin, const T2 p2end,
experimental-genesis 606 const T3 p3begin, const T3 p3end)
experimental-genesis 607 {
experimental-genesis 608 static unsigned char pblank[1];
experimental-genesis 609 uint256 hash1;
experimental-genesis 610 SHA256_CTX ctx;
experimental-genesis 611 SHA256_Init(&ctx);
experimental-genesis 612 SHA256_Update(&ctx, (p1begin == p1end ? pblank : (unsigned char*)&p1begin[0]), (p1end - p1begin) * sizeof(p1begin[0]));
experimental-genesis 613 SHA256_Update(&ctx, (p2begin == p2end ? pblank : (unsigned char*)&p2begin[0]), (p2end - p2begin) * sizeof(p2begin[0]));
experimental-genesis 614 SHA256_Update(&ctx, (p3begin == p3end ? pblank : (unsigned char*)&p3begin[0]), (p3end - p3begin) * sizeof(p3begin[0]));
experimental-genesis 615 SHA256_Final((unsigned char*)&hash1, &ctx);
experimental-genesis 616 uint256 hash2;
experimental-genesis 617 SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
experimental-genesis 618 return hash2;
experimental-genesis 619 }
experimental-genesis 620
experimental-genesis 621 template<typename T>
experimental-genesis 622 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=VERSION)
experimental-genesis 623 {
experimental-genesis 624
experimental-genesis 625
experimental-genesis 626
experimental-genesis 627 CDataStream ss(nType, nVersion);
experimental-genesis 628 ss.reserve(10000);
experimental-genesis 629 ss << obj;
experimental-genesis 630 return Hash(ss.begin(), ss.end());
experimental-genesis 631 }
experimental-genesis 632
experimental-genesis 633 inline uint160 Hash160(const std::vector<unsigned char>& vch)
experimental-genesis 634 {
experimental-genesis 635 uint256 hash1;
experimental-genesis 636 SHA256(&vch[0], vch.size(), (unsigned char*)&hash1);
experimental-genesis 637 uint160 hash2;
experimental-genesis 638 RIPEMD160((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
experimental-genesis 639 return hash2;
experimental-genesis 640 }
experimental-genesis 641
experimental-genesis 642
experimental-genesis 643
experimental-genesis 644
experimental-genesis 645 template <typename T> class CMedianFilter
experimental-genesis 646 {
experimental-genesis 647 private:
experimental-genesis 648 std::vector<T> vValues;
experimental-genesis 649 std::vector<T> vSorted;
experimental-genesis 650 int nSize;
experimental-genesis 651 public:
experimental-genesis 652 CMedianFilter(int size, T initial_value):
experimental-genesis 653 nSize(size)
experimental-genesis 654 {
experimental-genesis 655 vValues.reserve(size);
experimental-genesis 656 vValues.push_back(initial_value);
experimental-genesis 657 vSorted = vValues;
experimental-genesis 658 }
experimental-genesis 659
experimental-genesis 660 void input(T value)
experimental-genesis 661 {
experimental-genesis 662 if(vValues.size() == nSize)
experimental-genesis 663 {
experimental-genesis 664 vValues.erase(vValues.begin());
experimental-genesis 665 }
experimental-genesis 666 vValues.push_back(value);
experimental-genesis 667
experimental-genesis 668 vSorted.resize(vValues.size());
experimental-genesis 669 std::copy(vValues.begin(), vValues.end(), vSorted.begin());
experimental-genesis 670 std::sort(vSorted.begin(), vSorted.end());
experimental-genesis 671 }
experimental-genesis 672
experimental-genesis 673 T median() const
experimental-genesis 674 {
experimental-genesis 675 int size = vSorted.size();
experimental-genesis 676 assert(size>0);
experimental-genesis 677 if(size & 1)
experimental-genesis 678 {
experimental-genesis 679 return vSorted[size/2];
experimental-genesis 680 }
experimental-genesis 681 else
experimental-genesis 682 {
experimental-genesis 683 return (vSorted[size/2-1] + vSorted[size/2]) / 2;
experimental-genesis 684 }
experimental-genesis 685 }
experimental-genesis 686 };
experimental-genesis 687
experimental-genesis 688
experimental-genesis 689
experimental-genesis 690
experimental-genesis 691
experimental-genesis 692
experimental-genesis 693
experimental-genesis 694
experimental-genesis 695
experimental-genesis 696
experimental-genesis 697
experimental-genesis 698
experimental-genesis 699 #ifdef WIN32
experimental-genesis 700 typedef HANDLE pthread_t;
experimental-genesis 701
experimental-genesis 702 inline pthread_t CreateThread(void(*pfn)(void*), void* parg, bool fWantHandle=false)
experimental-genesis 703 {
experimental-genesis 704 DWORD nUnused = 0;
experimental-genesis 705 HANDLE hthread =
experimental-genesis 706 CreateThread(
experimental-genesis 707 NULL,
experimental-genesis 708 0,
experimental-genesis 709 (LPTHREAD_START_ROUTINE)pfn,
experimental-genesis 710 parg,
experimental-genesis 711 0,
experimental-genesis 712 &nUnused);
experimental-genesis 713 if (hthread == NULL)
experimental-genesis 714 {
experimental-genesis 715 printf("Error: CreateThread() returned %d\n", GetLastError());
experimental-genesis 716 return (pthread_t)0;
experimental-genesis 717 }
experimental-genesis 718 if (!fWantHandle)
experimental-genesis 719 {
experimental-genesis 720 CloseHandle(hthread);
experimental-genesis 721 return (pthread_t)-1;
experimental-genesis 722 }
experimental-genesis 723 return hthread;
experimental-genesis 724 }
experimental-genesis 725
experimental-genesis 726 inline void SetThreadPriority(int nPriority)
experimental-genesis 727 {
experimental-genesis 728 SetThreadPriority(GetCurrentThread(), nPriority);
experimental-genesis 729 }
experimental-genesis 730 #else
experimental-genesis 731 inline pthread_t CreateThread(void(*pfn)(void*), void* parg, bool fWantHandle=false)
experimental-genesis 732 {
experimental-genesis 733 pthread_t hthread = 0;
experimental-genesis 734 int ret = pthread_create(&hthread, NULL, (void*(*)(void*))pfn, parg);
experimental-genesis 735 if (ret != 0)
experimental-genesis 736 {
experimental-genesis 737 printf("Error: pthread_create() returned %d\n", ret);
experimental-genesis 738 return (pthread_t)0;
experimental-genesis 739 }
experimental-genesis 740 if (!fWantHandle)
experimental-genesis 741 {
experimental-genesis 742 pthread_detach(hthread);
experimental-genesis 743 return (pthread_t)-1;
experimental-genesis 744 }
experimental-genesis 745 return hthread;
experimental-genesis 746 }
experimental-genesis 747
experimental-genesis 748 #define THREAD_PRIORITY_LOWEST PRIO_MAX
experimental-genesis 749 #define THREAD_PRIORITY_BELOW_NORMAL 2
experimental-genesis 750 #define THREAD_PRIORITY_NORMAL 0
experimental-genesis 751 #define THREAD_PRIORITY_ABOVE_NORMAL 0
experimental-genesis 752
experimental-genesis 753 inline void SetThreadPriority(int nPriority)
experimental-genesis 754 {
experimental-genesis 755
experimental-genesis 756
experimental-genesis 757 #ifdef PRIO_THREAD
experimental-genesis 758 setpriority(PRIO_THREAD, 0, nPriority);
experimental-genesis 759 #else
experimental-genesis 760 setpriority(PRIO_PROCESS, 0, nPriority);
experimental-genesis 761 #endif
experimental-genesis 762 }
experimental-genesis 763
experimental-genesis 764 inline bool TerminateThread(pthread_t hthread, unsigned int nExitCode)
experimental-genesis 765 {
experimental-genesis 766 return (pthread_cancel(hthread) == 0);
experimental-genesis 767 }
experimental-genesis 768
experimental-genesis 769 inline void ExitThread(size_t nExitCode)
experimental-genesis 770 {
experimental-genesis 771 pthread_exit((void*)nExitCode);
experimental-genesis 772 }
experimental-genesis 773 #endif
experimental-genesis 774
experimental-genesis 775
experimental-genesis 776
experimental-genesis 777
experimental-genesis 778
experimental-genesis 779 inline bool AffinityBugWorkaround(void(*pfn)(void*))
experimental-genesis 780 {
experimental-genesis 781 #ifdef WIN32
experimental-genesis 782
experimental-genesis 783 DWORD_PTR dwProcessAffinityMask = -1;
experimental-genesis 784 DWORD_PTR dwSystemAffinityMask = -1;
experimental-genesis 785 GetProcessAffinityMask(GetCurrentProcess(), &dwProcessAffinityMask, &dwSystemAffinityMask);
experimental-genesis 786 DWORD dwPrev1 = SetThreadAffinityMask(GetCurrentThread(), dwProcessAffinityMask);
experimental-genesis 787 DWORD dwPrev2 = SetThreadAffinityMask(GetCurrentThread(), dwProcessAffinityMask);
experimental-genesis 788 if (dwPrev2 != dwProcessAffinityMask)
experimental-genesis 789 {
experimental-genesis 790 printf("AffinityBugWorkaround() : SetThreadAffinityMask=%d, ProcessAffinityMask=%d, restarting thread\n", dwPrev2, dwProcessAffinityMask);
experimental-genesis 791 if (!CreateThread(pfn, NULL))
experimental-genesis 792 printf("Error: CreateThread() failed\n");
experimental-genesis 793 return true;
experimental-genesis 794 }
experimental-genesis 795 #endif
experimental-genesis 796 return false;
experimental-genesis 797 }
experimental-genesis 798
experimental-genesis 799 inline uint32_t ByteReverse(uint32_t value)
experimental-genesis 800 {
experimental-genesis 801 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
experimental-genesis 802 return (value<<16) | (value>>16);
experimental-genesis 803 }
experimental-genesis 804
experimental-genesis 805 #endif