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