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