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