Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
4.6 KiB

  1. //+--------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (c) Microsoft Corporation. All rights reserved.
  5. //
  6. // File: byteordr.hxx
  7. //
  8. // Contents: Byte swapping functions.
  9. // These functions can swap all of the base types,
  10. // and some special cases. There is always an
  11. // overloaded version which accepts a pointer to the
  12. // data, and performs a swap in place. Where possible,
  13. // there is also an overload which returns the swapped
  14. // value without modifying the input.
  15. //
  16. //---------------------------------------------------------------
  17. #ifndef __BYTEORDER_HXX_
  18. #define __BYTEORDER_HXX_
  19. #include <limits.h>
  20. #if (defined(_M_IX86) && (_MSC_FULL_VER > 13009037)) || ((defined(_M_AMD64) || defined(_M_IA64)) && (_MSC_FULL_VER > 13009175))
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. unsigned short __cdecl _byteswap_ushort(unsigned short);
  25. unsigned long __cdecl _byteswap_ulong (unsigned long);
  26. unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64);
  27. #ifdef __cplusplus
  28. }
  29. #endif
  30. #pragma intrinsic(_byteswap_ushort)
  31. #pragma intrinsic(_byteswap_ulong)
  32. #pragma intrinsic(_byteswap_uint64)
  33. #endif
  34. //
  35. // BYTE Byte-Swap
  36. //
  37. // These exist so that you can call ByteSwap(OLECHAR) without
  38. // knowning if OLECHAR is a CHAR or a WCHAR.
  39. inline BYTE ByteSwap( BYTE b )
  40. {
  41. return(b);
  42. }
  43. inline VOID ByteSwap( BYTE *pb )
  44. {
  45. *pb = ByteSwap(*pb);
  46. }
  47. //
  48. // WORD Byte-Swap
  49. //
  50. inline WORD ByteSwap( WORD w )
  51. {
  52. #if (defined(_M_IX86) && (_MSC_FULL_VER > 13009037)) || ((defined(_M_AMD64) || defined(_M_IA64)) && (_MSC_FULL_VER > 13009175))
  53. return(_byteswap_ushort(w));
  54. #else
  55. return( (USHORT) ( (LOBYTE(w) << 8 )
  56. |
  57. HIBYTE(w)) );
  58. #endif
  59. }
  60. inline VOID ByteSwap( WORD *pw )
  61. {
  62. *pw = ByteSwap(*pw);
  63. }
  64. //
  65. // DWORD Byte-Swap
  66. //
  67. #define BYTE_MASK_A_C_ 0xff00ff00
  68. #define BYTE_MASK__B_D 0x00ff00ff
  69. #define BYTE_MASK_AB__ 0xffff0000
  70. #define BYTE_MASK___CD 0x0000ffff
  71. inline DWORD ByteSwap( DWORD dwOriginal )
  72. {
  73. #if (defined(_M_IX86) && (_MSC_FULL_VER > 13009037)) || ((defined(_M_AMD64) || defined(_M_IA64)) && (_MSC_FULL_VER > 13009175))
  74. return(_byteswap_ulong(dwOriginal));
  75. #else
  76. ULONG dwSwapped;
  77. // ABCD => BADC
  78. dwSwapped = (( (dwOriginal) & BYTE_MASK_A_C_ ) >> 8 )
  79. |
  80. (( (dwOriginal) & BYTE_MASK__B_D ) << 8 );
  81. // BADC => DCBA
  82. dwSwapped = (( dwSwapped & BYTE_MASK_AB__ ) >> 16 )
  83. |
  84. (( dwSwapped & BYTE_MASK___CD ) << 16 );
  85. return( dwSwapped );
  86. #endif
  87. }
  88. inline VOID ByteSwap( DWORD *pdw )
  89. {
  90. *pdw = ByteSwap(*pdw);
  91. }
  92. //
  93. // LONGLONG Byte-Swap
  94. //
  95. #define BYTE_MASK_A_C_E_G_ 0xff00ff00ff00ff00
  96. #define BYTE_MASK__B_D_F_H 0x00ff00ff00ff00ff
  97. #define BYTE_MASK_AB__EF__ 0xffff0000ffff0000
  98. #define BYTE_MASK___CD__GH 0x0000ffff0000ffff
  99. #define BYTE_MASK_ABCD____ 0xffffffff00000000
  100. #define BYTE_MASK_____EFGH 0x00000000ffffffff
  101. inline LONGLONG ByteSwap( LONGLONG llOriginal )
  102. {
  103. #ifdef _MAC
  104. LONGLONG llSwapped;
  105. *((DWORD*) &llSwapped) = *((DWORD*) &llOriginal + 1);
  106. *((DWORD*) &llSwapped + 1) = *((DWORD*) &llOriginal);
  107. ByteSwap( (DWORD*) &llSwapped );
  108. ByteSwap( (DWORD*) &llSwapped + 1 );
  109. return( llSwapped );
  110. #else
  111. #if (defined(_M_IX86) && (_MSC_FULL_VER > 13009037)) || ((defined(_M_AMD64) || defined(_M_IA64)) && (_MSC_FULL_VER > 13009175))
  112. return(_byteswap_uint64(llOriginal));
  113. #else
  114. LONGLONG llSwapped;
  115. // ABCDEFGH => BADCFEHG
  116. llSwapped = (( (llOriginal) & BYTE_MASK_A_C_E_G_ ) >> 8 )
  117. |
  118. (( (llOriginal) & BYTE_MASK__B_D_F_H ) << 8 );
  119. // BADCFEHG => DCBAHGFE
  120. llSwapped = (( llSwapped & BYTE_MASK_AB__EF__ ) >> 16 )
  121. |
  122. (( llSwapped & BYTE_MASK___CD__GH ) << 16 );
  123. // DCBAHGFE => HGFEDCBA
  124. llSwapped = (( llSwapped & BYTE_MASK_ABCD____ ) >> 32 )
  125. |
  126. (( llSwapped & BYTE_MASK_____EFGH ) << 32 );
  127. return( llSwapped );
  128. #endif
  129. #endif
  130. }
  131. inline VOID ByteSwap( LONGLONG *pll )
  132. {
  133. *pll = ByteSwap( *pll );
  134. }
  135. //
  136. // GUID Byte-swap
  137. //
  138. inline VOID ByteSwap( GUID *pguid)
  139. {
  140. ByteSwap(&pguid->Data1);
  141. ByteSwap(&pguid->Data2);
  142. ByteSwap(&pguid->Data3);
  143. }
  144. //
  145. // FILETIME Byte-Swap
  146. //
  147. inline VOID ByteSwap(FILETIME *pfileTime)
  148. {
  149. ByteSwap(&pfileTime->dwLowDateTime);
  150. ByteSwap(&pfileTime->dwHighDateTime);
  151. }
  152. #endif // !__BYTEORDER_HXX_