6 #include <openssl/sha.h>
12 #define BEGIN(a) ((char*)&(a))
13 #define END(a) ((char*)&((&(a))[1]))
14 #define UBEGIN(a) ((unsigned char*)&(a))
15 #define UEND(a) ((unsigned char*)&((&(a))[1]))
16 #define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0]))
18 #define WSAGetLastError() errno
19 #define WSAEINVAL EINVAL
20 #define WSAEALREADY EALREADY
21 #define WSAEWOULDBLOCK EWOULDBLOCK
22 #define WSAEMSGSIZE EMSGSIZE
23 #define WSAEINTR EINTR
24 #define WSAEINPROGRESS EINPROGRESS
25 #define WSAEADDRINUSE EADDRINUSE
26 #define WSAENOTSOCK EBADF
27 #define INVALID_SOCKET (SOCKET)(~0)
28 #define SOCKET_ERROR -1
30 // Wrapper to automatically initialize mutex
31 class CCriticalSection
34 pthread_rwlock_t mutex;
36 explicit CCriticalSection() { pthread_rwlock_init(&mutex, NULL); }
37 ~CCriticalSection() { pthread_rwlock_destroy(&mutex); }
38 void Enter(bool fShared = false) {
40 pthread_rwlock_rdlock(&mutex);
42 pthread_rwlock_wrlock(&mutex);
45 void Leave() { pthread_rwlock_unlock(&mutex); }
48 // Automatically leave critical section when leaving block, needed for exception safety
52 CCriticalSection* pcs;
54 CCriticalBlock(CCriticalSection& cs, bool fShared = false) : pcs(&cs) { pcs->Enter(fShared); }
55 operator bool() const { return true; }
56 ~CCriticalBlock() { pcs->Leave(); }
59 #define CRITICAL_BLOCK(cs) \
60 if (CCriticalBlock criticalblock = CCriticalBlock(cs))
62 #define SHARED_CRITICAL_BLOCK(cs) \
63 if (CCriticalBlock criticalblock = CCriticalBlock(cs, true))
65 template<typename T1> inline uint256 Hash(const T1 pbegin, const T1 pend)
67 static unsigned char pblank[1];
69 SHA256((pbegin == pend ? pblank : (unsigned char*)&pbegin[0]), (pend - pbegin) * sizeof(pbegin[0]), (unsigned char*)&hash1);
71 SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
75 void static inline Sleep(int nMilliSec) {
77 wa.tv_sec = nMilliSec/1000;
78 wa.tv_nsec = (nMilliSec % 1000) * 1000000;
83 std::string vstrprintf(const std::string &format, va_list ap);
85 std::string static inline strprintf(const std::string &format, ...) {
87 va_start(arg_ptr, format);
88 std::string ret = vstrprintf(format, arg_ptr);
93 bool static inline error(std::string err, ...) {
97 bool static inline my_printf(std::string err, ...) {
101 std::vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid = NULL);
102 std::string DecodeBase32(const std::string& str);
103 std::string EncodeBase32(const unsigned char* pch, size_t len);
104 std::string EncodeBase32(const std::string& str);