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 #if !defined(QT_GUI)
genesis 138 inline const char* _(const char* psz)
genesis 139 {
genesis 140 return psz;
genesis 141 }
genesis 142 #endif
genesis 143
genesis 144
genesis 145
genesis 146
genesis 147
genesis 148
genesis 149
genesis 150
genesis 151
genesis 152 extern std::map<std::string, std::string> mapArgs;
genesis 153 extern std::map<std::string, std::vector<std::string> > mapMultiArgs;
genesis 154 extern bool fDebug;
genesis 155 extern bool fPrintToConsole;
genesis 156 extern bool fPrintToDebugger;
genesis 157 extern char pszSetDataDir[MAX_PATH];
genesis 158 extern bool fRequestShutdown;
genesis 159 extern bool fShutdown;
genesis 160 extern bool fDaemon;
genesis 161 extern bool fServer;
genesis 162 extern bool fCommandLine;
genesis 163 extern std::string strMiscWarning;
genesis 164 extern bool fTestNet;
genesis 165 extern bool fNoListen;
genesis 166 extern bool fLogTimestamps;
genesis 167
genesis 168 void RandAddSeed();
genesis 169 void RandAddSeedPerfmon();
genesis 170 int OutputDebugStringF(const char* pszFormat, ...);
genesis 171 int my_snprintf(char* buffer, size_t limit, const char* format, ...);
genesis 172 std::string strprintf(const std::string &format, ...);
genesis 173 bool error(const std::string &format, ...);
genesis 174 void LogException(std::exception* pex, const char* pszThread);
genesis 175 void PrintException(std::exception* pex, const char* pszThread);
genesis 176 void PrintExceptionContinue(std::exception* pex, const char* pszThread);
genesis 177 void ParseString(const std::string& str, char c, std::vector<std::string>& v);
genesis 178 std::string FormatMoney(int64 n, bool fPlus=false);
genesis 179 bool ParseMoney(const std::string& str, int64& nRet);
genesis 180 bool ParseMoney(const char* pszIn, int64& nRet);
genesis 181 std::vector<unsigned char> ParseHex(const char* psz);
genesis 182 std::vector<unsigned char> ParseHex(const std::string& str);
genesis 183 std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid = NULL);
genesis 184 std::string DecodeBase64(const std::string& str);
genesis 185 std::string EncodeBase64(const unsigned char* pch, size_t len);
genesis 186 std::string EncodeBase64(const std::string& str);
genesis 187 void ParseParameters(int argc, char* argv[]);
genesis 188 const char* wxGetTranslation(const char* psz);
genesis 189 bool WildcardMatch(const char* psz, const char* mask);
genesis 190 bool WildcardMatch(const std::string& str, const std::string& mask);
genesis 191 int GetFilesize(FILE* file);
genesis 192 void GetDataDir(char* pszDirRet);
genesis 193 std::string GetConfigFile();
genesis 194 std::string GetPidFile();
genesis 195 void CreatePidFile(std::string pidFile, pid_t pid);
genesis 196 void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet, std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet);
genesis 197 #ifdef WIN32
genesis 198 std::string MyGetSpecialFolderPath(int nFolder, bool fCreate);
genesis 199 #endif
genesis 200 std::string GetDefaultDataDir();
genesis 201 std::string GetDataDir();
genesis 202 void ShrinkDebugFile();
genesis 203 int GetRandInt(int nMax);
genesis 204 uint64 GetRand(uint64 nMax);
genesis 205 int64 GetTime();
genesis 206 void SetMockTime(int64 nMockTimeIn);
genesis 207 int64 GetAdjustedTime();
genesis 208 void AddTimeData(unsigned int ip, int64 nTime);
genesis 209 std::string FormatFullVersion();
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
genesis 224 class CCriticalSection
genesis 225 {
genesis 226 protected:
genesis 227 boost::interprocess::interprocess_recursive_mutex mutex;
genesis 228 public:
genesis 229 explicit CCriticalSection() { }
genesis 230 ~CCriticalSection() { }
genesis 231 void Enter(const char* pszName, const char* pszFile, int nLine);
genesis 232 void Leave();
genesis 233 bool TryEnter(const char* pszName, const char* pszFile, int nLine);
genesis 234 };
genesis 235
genesis 236
genesis 237 class CCriticalBlock
genesis 238 {
genesis 239 protected:
genesis 240 CCriticalSection* pcs;
genesis 241
genesis 242 public:
genesis 243 CCriticalBlock(CCriticalSection& csIn, const char* pszName, const char* pszFile, int nLine)
genesis 244 {
genesis 245 pcs = &csIn;
genesis 246 pcs->Enter(pszName, pszFile, nLine);
genesis 247 }
genesis 248
genesis 249 operator bool() const
genesis 250 {
genesis 251 return true;
genesis 252 }
genesis 253
genesis 254 ~CCriticalBlock()
genesis 255 {
genesis 256 pcs->Leave();
genesis 257 }
genesis 258 };
genesis 259
genesis 260 #define CRITICAL_BLOCK(cs) \
genesis 261 if (CCriticalBlock criticalblock = CCriticalBlock(cs, #cs, __FILE__, __LINE__))
genesis 262
genesis 263 #define ENTER_CRITICAL_SECTION(cs) \
genesis 264 (cs).Enter(#cs, __FILE__, __LINE__)
genesis 265
genesis 266 #define LEAVE_CRITICAL_SECTION(cs) \
genesis 267 (cs).Leave()
genesis 268
genesis 269 class CTryCriticalBlock
genesis 270 {
genesis 271 protected:
genesis 272 CCriticalSection* pcs;
genesis 273
genesis 274 public:
genesis 275 CTryCriticalBlock(CCriticalSection& csIn, const char* pszName, const char* pszFile, int nLine)
genesis 276 {
genesis 277 pcs = (csIn.TryEnter(pszName, pszFile, nLine) ? &csIn : NULL);
genesis 278 }
genesis 279
genesis 280 operator bool() const
genesis 281 {
genesis 282 return Entered();
genesis 283 }
genesis 284
genesis 285 ~CTryCriticalBlock()
genesis 286 {
genesis 287 if (pcs)
genesis 288 {
genesis 289 pcs->Leave();
genesis 290 }
genesis 291 }
genesis 292 bool Entered() const { return pcs != NULL; }
genesis 293 };
genesis 294
genesis 295 #define TRY_CRITICAL_BLOCK(cs) \
genesis 296 if (CTryCriticalBlock criticalblock = CTryCriticalBlock(cs, #cs, __FILE__, __LINE__))
genesis 297
genesis 298
genesis 299
genesis 300
genesis 301
genesis 302
genesis 303
genesis 304
genesis 305 typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;
genesis 306
genesis 307
genesis 308
genesis 309 typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;
genesis 310
genesis 311
genesis 312
genesis 313
genesis 314
genesis 315 inline std::string i64tostr(int64 n)
genesis 316 {
genesis 317 return strprintf("%"PRI64d, n);
genesis 318 }
genesis 319
genesis 320 inline std::string itostr(int n)
genesis 321 {
genesis 322 return strprintf("%d", n);
genesis 323 }
genesis 324
genesis 325 inline int64 atoi64(const char* psz)
genesis 326 {
genesis 327 #ifdef _MSC_VER
genesis 328 return _atoi64(psz);
genesis 329 #else
genesis 330 return strtoll(psz, NULL, 10);
genesis 331 #endif
genesis 332 }
genesis 333
genesis 334 inline int64 atoi64(const std::string& str)
genesis 335 {
genesis 336 #ifdef _MSC_VER
genesis 337 return _atoi64(str.c_str());
genesis 338 #else
genesis 339 return strtoll(str.c_str(), NULL, 10);
genesis 340 #endif
genesis 341 }
genesis 342
genesis 343 inline int atoi(const std::string& str)
genesis 344 {
genesis 345 return atoi(str.c_str());
genesis 346 }
genesis 347
genesis 348 inline int roundint(double d)
genesis 349 {
genesis 350 return (int)(d > 0 ? d + 0.5 : d - 0.5);
genesis 351 }
genesis 352
genesis 353 inline int64 roundint64(double d)
genesis 354 {
genesis 355 return (int64)(d > 0 ? d + 0.5 : d - 0.5);
genesis 356 }
genesis 357
genesis 358 inline int64 abs64(int64 n)
genesis 359 {
genesis 360 return (n >= 0 ? n : -n);
genesis 361 }
genesis 362
genesis 363 template<typename T>
genesis 364 std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
genesis 365 {
genesis 366 if (itbegin == itend)
genesis 367 return "";
genesis 368 const unsigned char* pbegin = (const unsigned char*)&itbegin[0];
genesis 369 const unsigned char* pend = pbegin + (itend - itbegin) * sizeof(itbegin[0]);
genesis 370 std::string str;
genesis 371 str.reserve((pend-pbegin) * (fSpaces ? 3 : 2));
genesis 372 for (const unsigned char* p = pbegin; p != pend; p++)
genesis 373 str += strprintf((fSpaces && p != pend-1 ? "%02x " : "%02x"), *p);
genesis 374 return str;
genesis 375 }
genesis 376
genesis 377 inline std::string HexStr(const std::vector<unsigned char>& vch, bool fSpaces=false)
genesis 378 {
genesis 379 return HexStr(vch.begin(), vch.end(), fSpaces);
genesis 380 }
genesis 381
genesis 382 template<typename T>
genesis 383 std::string HexNumStr(const T itbegin, const T itend, bool f0x=true)
genesis 384 {
genesis 385 if (itbegin == itend)
genesis 386 return "";
genesis 387 const unsigned char* pbegin = (const unsigned char*)&itbegin[0];
genesis 388 const unsigned char* pend = pbegin + (itend - itbegin) * sizeof(itbegin[0]);
genesis 389 std::string str = (f0x ? "0x" : "");
genesis 390 str.reserve(str.size() + (pend-pbegin) * 2);
genesis 391 for (const unsigned char* p = pend-1; p >= pbegin; p--)
genesis 392 str += strprintf("%02x", *p);
genesis 393 return str;
genesis 394 }
genesis 395
genesis 396 inline std::string HexNumStr(const std::vector<unsigned char>& vch, bool f0x=true)
genesis 397 {
genesis 398 return HexNumStr(vch.begin(), vch.end(), f0x);
genesis 399 }
genesis 400
genesis 401 template<typename T>
genesis 402 void PrintHex(const T pbegin, const T pend, const char* pszFormat="%s", bool fSpaces=true)
genesis 403 {
genesis 404 printf(pszFormat, HexStr(pbegin, pend, fSpaces).c_str());
genesis 405 }
genesis 406
genesis 407 inline void PrintHex(const std::vector<unsigned char>& vch, const char* pszFormat="%s", bool fSpaces=true)
genesis 408 {
genesis 409 printf(pszFormat, HexStr(vch, fSpaces).c_str());
genesis 410 }
genesis 411
genesis 412 inline int64 GetPerformanceCounter()
genesis 413 {
genesis 414 int64 nCounter = 0;
genesis 415 #ifdef WIN32
genesis 416 QueryPerformanceCounter((LARGE_INTEGER*)&nCounter);
genesis 417 #else
genesis 418 timeval t;
genesis 419 gettimeofday(&t, NULL);
genesis 420 nCounter = t.tv_sec * 1000000 + t.tv_usec;
genesis 421 #endif
genesis 422 return nCounter;
genesis 423 }
genesis 424
genesis 425 inline int64 GetTimeMillis()
genesis 426 {
genesis 427 return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) -
genesis 428 boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
genesis 429 }
genesis 430
genesis 431 inline std::string DateTimeStrFormat(const char* pszFormat, int64 nTime)
genesis 432 {
genesis 433 time_t n = nTime;
genesis 434 struct tm* ptmTime = gmtime(&n);
genesis 435 char pszTime[200];
genesis 436 strftime(pszTime, sizeof(pszTime), pszFormat, ptmTime);
genesis 437 return pszTime;
genesis 438 }
genesis 439
genesis 440 template<typename T>
genesis 441 void skipspaces(T& it)
genesis 442 {
genesis 443 while (isspace(*it))
genesis 444 ++it;
genesis 445 }
genesis 446
genesis 447 inline bool IsSwitchChar(char c)
genesis 448 {
genesis 449 #ifdef WIN32
genesis 450 return c == '-' || c == '/';
genesis 451 #else
genesis 452 return c == '-';
genesis 453 #endif
genesis 454 }
genesis 455
genesis 456 inline std::string GetArg(const std::string& strArg, const std::string& strDefault)
genesis 457 {
genesis 458 if (mapArgs.count(strArg))
genesis 459 return mapArgs[strArg];
genesis 460 return strDefault;
genesis 461 }
genesis 462
genesis 463 inline int64 GetArg(const std::string& strArg, int64 nDefault)
genesis 464 {
genesis 465 if (mapArgs.count(strArg))
genesis 466 return atoi64(mapArgs[strArg]);
genesis 467 return nDefault;
genesis 468 }
genesis 469
genesis 470 inline bool GetBoolArg(const std::string& strArg, bool fDefault=false)
genesis 471 {
genesis 472 if (mapArgs.count(strArg))
genesis 473 {
genesis 474 if (mapArgs[strArg].empty())
genesis 475 return true;
genesis 476 return (atoi(mapArgs[strArg]) != 0);
genesis 477 }
genesis 478 return fDefault;
genesis 479 }
genesis 480
genesis 481
genesis 482 * Set an argument if it doesn't already have a value
genesis 483 *
genesis 484 * @param strArg Argument to set (e.g. "-foo")
genesis 485 * @param strValue Value (e.g. "1")
genesis 486 * @return true if argument gets set, false if it already had a value
genesis 487 */
genesis 488 bool SoftSetArg(const std::string& strArg, const std::string& strValue);
genesis 489
genesis 490
genesis 491 * Set a boolean argument if it doesn't already have a value
genesis 492 *
genesis 493 * @param strArg Argument to set (e.g. "-foo")
genesis 494 * @param fValue Value (e.g. false)
genesis 495 * @return true if argument gets set, false if it already had a value
genesis 496 */
genesis 497 bool SoftSetArg(const std::string& strArg, bool fValue);
genesis 498
genesis 499
genesis 500
genesis 501
genesis 502
genesis 503
genesis 504
genesis 505
genesis 506
genesis 507 inline void heapchk()
genesis 508 {
genesis 509 #ifdef WIN32
genesis 510
genesis 511
genesis 512
genesis 513 #endif
genesis 514 }
genesis 515
genesis 516
genesis 517 #define IMPLEMENT_RANDOMIZE_STACK(ThreadFn) \
genesis 518 { \
genesis 519 static char nLoops; \
genesis 520 if (nLoops <= 0) \
genesis 521 nLoops = GetRand(20) + 1; \
genesis 522 if (nLoops-- > 1) \
genesis 523 { \
genesis 524 ThreadFn; \
genesis 525 return; \
genesis 526 } \
genesis 527 }
genesis 528
genesis 529 #define CATCH_PRINT_EXCEPTION(pszFn) \
genesis 530 catch (std::exception& e) { \
genesis 531 PrintException(&e, (pszFn)); \
genesis 532 } catch (...) { \
genesis 533 PrintException(NULL, (pszFn)); \
genesis 534 }
genesis 535
genesis 536
genesis 537
genesis 538
genesis 539
genesis 540
genesis 541
genesis 542
genesis 543
genesis 544
genesis 545 template<typename T1>
genesis 546 inline uint256 Hash(const T1 pbegin, const T1 pend)
genesis 547 {
genesis 548 static unsigned char pblank[1];
genesis 549 uint256 hash1;
genesis 550 SHA256((pbegin == pend ? pblank : (unsigned char*)&pbegin[0]), (pend - pbegin) * sizeof(pbegin[0]), (unsigned char*)&hash1);
genesis 551 uint256 hash2;
genesis 552 SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
genesis 553 return hash2;
genesis 554 }
genesis 555
genesis 556 template<typename T1, typename T2>
genesis 557 inline uint256 Hash(const T1 p1begin, const T1 p1end,
genesis 558 const T2 p2begin, const T2 p2end)
genesis 559 {
genesis 560 static unsigned char pblank[1];
genesis 561 uint256 hash1;
genesis 562 SHA256_CTX ctx;
genesis 563 SHA256_Init(&ctx);
genesis 564 SHA256_Update(&ctx, (p1begin == p1end ? pblank : (unsigned char*)&p1begin[0]), (p1end - p1begin) * sizeof(p1begin[0]));
genesis 565 SHA256_Update(&ctx, (p2begin == p2end ? pblank : (unsigned char*)&p2begin[0]), (p2end - p2begin) * sizeof(p2begin[0]));
genesis 566 SHA256_Final((unsigned char*)&hash1, &ctx);
genesis 567 uint256 hash2;
genesis 568 SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
genesis 569 return hash2;
genesis 570 }
genesis 571
genesis 572 template<typename T1, typename T2, typename T3>
genesis 573 inline uint256 Hash(const T1 p1begin, const T1 p1end,
genesis 574 const T2 p2begin, const T2 p2end,
genesis 575 const T3 p3begin, const T3 p3end)
genesis 576 {
genesis 577 static unsigned char pblank[1];
genesis 578 uint256 hash1;
genesis 579 SHA256_CTX ctx;
genesis 580 SHA256_Init(&ctx);
genesis 581 SHA256_Update(&ctx, (p1begin == p1end ? pblank : (unsigned char*)&p1begin[0]), (p1end - p1begin) * sizeof(p1begin[0]));
genesis 582 SHA256_Update(&ctx, (p2begin == p2end ? pblank : (unsigned char*)&p2begin[0]), (p2end - p2begin) * sizeof(p2begin[0]));
genesis 583 SHA256_Update(&ctx, (p3begin == p3end ? pblank : (unsigned char*)&p3begin[0]), (p3end - p3begin) * sizeof(p3begin[0]));
genesis 584 SHA256_Final((unsigned char*)&hash1, &ctx);
genesis 585 uint256 hash2;
genesis 586 SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
genesis 587 return hash2;
genesis 588 }
genesis 589
genesis 590 template<typename T>
genesis 591 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=VERSION)
genesis 592 {
genesis 593
genesis 594
genesis 595
genesis 596 CDataStream ss(nType, nVersion);
genesis 597 ss.reserve(10000);
genesis 598 ss << obj;
genesis 599 return Hash(ss.begin(), ss.end());
genesis 600 }
genesis 601
genesis 602 inline uint160 Hash160(const std::vector<unsigned char>& vch)
genesis 603 {
genesis 604 uint256 hash1;
genesis 605 SHA256(&vch[0], vch.size(), (unsigned char*)&hash1);
genesis 606 uint160 hash2;
genesis 607 RIPEMD160((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
genesis 608 return hash2;
genesis 609 }
genesis 610
genesis 611
genesis 612
genesis 613
genesis 614 template <typename T> class CMedianFilter
genesis 615 {
genesis 616 private:
genesis 617 std::vector<T> vValues;
genesis 618 std::vector<T> vSorted;
genesis 619 int nSize;
genesis 620 public:
genesis 621 CMedianFilter(int size, T initial_value):
genesis 622 nSize(size)
genesis 623 {
genesis 624 vValues.reserve(size);
genesis 625 vValues.push_back(initial_value);
genesis 626 vSorted = vValues;
genesis 627 }
genesis 628
genesis 629 void input(T value)
genesis 630 {
genesis 631 if(vValues.size() == nSize)
genesis 632 {
genesis 633 vValues.erase(vValues.begin());
genesis 634 }
genesis 635 vValues.push_back(value);
genesis 636
genesis 637 vSorted.resize(vValues.size());
genesis 638 std::copy(vValues.begin(), vValues.end(), vSorted.begin());
genesis 639 std::sort(vSorted.begin(), vSorted.end());
genesis 640 }
genesis 641
genesis 642 T median() const
genesis 643 {
genesis 644 int size = vSorted.size();
genesis 645 assert(size>0);
genesis 646 if(size & 1)
genesis 647 {
genesis 648 return vSorted[size/2];
genesis 649 }
genesis 650 else
genesis 651 {
genesis 652 return (vSorted[size/2-1] + vSorted[size/2]) / 2;
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
genesis 668 #ifdef WIN32
genesis 669 typedef HANDLE pthread_t;
genesis 670
genesis 671 inline pthread_t CreateThread(void(*pfn)(void*), void* parg, bool fWantHandle=false)
genesis 672 {
genesis 673 DWORD nUnused = 0;
genesis 674 HANDLE hthread =
genesis 675 CreateThread(
genesis 676 NULL,
genesis 677 0,
genesis 678 (LPTHREAD_START_ROUTINE)pfn,
genesis 679 parg,
genesis 680 0,
genesis 681 &nUnused);
genesis 682 if (hthread == NULL)
genesis 683 {
genesis 684 printf("Error: CreateThread() returned %d\n", GetLastError());
genesis 685 return (pthread_t)0;
genesis 686 }
genesis 687 if (!fWantHandle)
genesis 688 {
genesis 689 CloseHandle(hthread);
genesis 690 return (pthread_t)-1;
genesis 691 }
genesis 692 return hthread;
genesis 693 }
genesis 694
genesis 695 inline void SetThreadPriority(int nPriority)
genesis 696 {
genesis 697 SetThreadPriority(GetCurrentThread(), nPriority);
genesis 698 }
genesis 699 #else
genesis 700 inline pthread_t CreateThread(void(*pfn)(void*), void* parg, bool fWantHandle=false)
genesis 701 {
genesis 702 pthread_t hthread = 0;
genesis 703 int ret = pthread_create(&hthread, NULL, (void*(*)(void*))pfn, parg);
genesis 704 if (ret != 0)
genesis 705 {
genesis 706 printf("Error: pthread_create() returned %d\n", ret);
genesis 707 return (pthread_t)0;
genesis 708 }
genesis 709 if (!fWantHandle)
genesis 710 {
genesis 711 pthread_detach(hthread);
genesis 712 return (pthread_t)-1;
genesis 713 }
genesis 714 return hthread;
genesis 715 }
genesis 716
genesis 717 #define THREAD_PRIORITY_LOWEST PRIO_MAX
genesis 718 #define THREAD_PRIORITY_BELOW_NORMAL 2
genesis 719 #define THREAD_PRIORITY_NORMAL 0
genesis 720 #define THREAD_PRIORITY_ABOVE_NORMAL 0
genesis 721
genesis 722 inline void SetThreadPriority(int nPriority)
genesis 723 {
genesis 724
genesis 725
genesis 726 #ifdef PRIO_THREAD
genesis 727 setpriority(PRIO_THREAD, 0, nPriority);
genesis 728 #else
genesis 729 setpriority(PRIO_PROCESS, 0, nPriority);
genesis 730 #endif
genesis 731 }
genesis 732
genesis 733 inline bool TerminateThread(pthread_t hthread, unsigned int nExitCode)
genesis 734 {
genesis 735 return (pthread_cancel(hthread) == 0);
genesis 736 }
genesis 737
genesis 738 inline void ExitThread(size_t nExitCode)
genesis 739 {
genesis 740 pthread_exit((void*)nExitCode);
genesis 741 }
genesis 742 #endif
genesis 743
genesis 744
genesis 745
genesis 746
genesis 747
genesis 748 inline bool AffinityBugWorkaround(void(*pfn)(void*))
genesis 749 {
genesis 750 #ifdef WIN32
genesis 751
genesis 752 DWORD_PTR dwProcessAffinityMask = -1;
genesis 753 DWORD_PTR dwSystemAffinityMask = -1;
genesis 754 GetProcessAffinityMask(GetCurrentProcess(), &dwProcessAffinityMask, &dwSystemAffinityMask);
genesis 755 DWORD dwPrev1 = SetThreadAffinityMask(GetCurrentThread(), dwProcessAffinityMask);
genesis 756 DWORD dwPrev2 = SetThreadAffinityMask(GetCurrentThread(), dwProcessAffinityMask);
genesis 757 if (dwPrev2 != dwProcessAffinityMask)
genesis 758 {
genesis 759 printf("AffinityBugWorkaround() : SetThreadAffinityMask=%d, ProcessAffinityMask=%d, restarting thread\n", dwPrev2, dwProcessAffinityMask);
genesis 760 if (!CreateThread(pfn, NULL))
genesis 761 printf("Error: CreateThread() failed\n");
genesis 762 return true;
genesis 763 }
genesis 764 #endif
genesis 765 return false;
genesis 766 }
genesis 767
genesis 768 inline uint32_t ByteReverse(uint32_t value)
genesis 769 {
genesis 770 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
genesis 771 return (value<<16) | (value>>16);
genesis 772 }
genesis 773
genesis 774 #endif