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