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.

192 lines
4.6 KiB

  1. //+--------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1996.
  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. #include <assert.h>
  21. //
  22. // BYTE Byte-Swap
  23. //
  24. // These exist so that you can call ByteSwap(OLECHAR) without
  25. // knowning if OLECHAR is a CHAR or a WCHAR.
  26. inline BYTE ByteSwap( char b )
  27. {
  28. return(b);
  29. }
  30. inline VOID ByteSwap( char *pb )
  31. {
  32. *pb = ByteSwap(*pb);
  33. }
  34. //
  35. // WORD Byte-Swap
  36. //
  37. #ifndef LOBYTE
  38. # define LOBYTE(a) (unsigned char)((a) & ((unsigned)~0 >> sizeof(BYTE)*8 ))
  39. # define HIBYTE(a) (unsigned char)((unsigned)(a) >> sizeof(BYTE)*8 )
  40. # define LOWORD(a) (WORD)( (a) & ( (WORD)~0 >> sizeof(WORD)*8 ))
  41. # define HIWORD(a) (WORD)( (WORD)(a) >> sizeof(WORD)*8 )
  42. # define LODWORD(a) (DWORD)( (a) & ( (DWORD)~0 >> sizeof(DWORD)*8 ))
  43. # define HIDWORD(a) (DWORD)( (DWORD)(a) >> sizeof(DWORD)*8 )
  44. #endif // #ifndef LOBYTE
  45. inline WORD ByteSwap( WORD w )
  46. {
  47. return( (USHORT) ( (LOBYTE(w) << 8 )
  48. |
  49. HIBYTE(w)) );
  50. }
  51. inline VOID ByteSwap( WORD *pw )
  52. {
  53. *pw = ByteSwap(*pw);
  54. }
  55. //
  56. // DWORD Byte-Swap
  57. //
  58. #define BYTE_MASK_A_C_ 0xff00ff00
  59. #define BYTE_MASK__B_D 0x00ff00ff
  60. #define BYTE_MASK_AB__ 0xffff0000
  61. #define BYTE_MASK___CD 0x0000ffff
  62. inline DWORD ByteSwap( DWORD dwOriginal )
  63. {
  64. ULONG dwSwapped;
  65. // ABCD => BADC
  66. dwSwapped = (( (dwOriginal) & BYTE_MASK_A_C_ ) >> 8 )
  67. |
  68. (( (dwOriginal) & BYTE_MASK__B_D ) << 8 );
  69. // BADC => DCBA
  70. dwSwapped = (( dwSwapped & BYTE_MASK_AB__ ) >> 16 )
  71. |
  72. (( dwSwapped & BYTE_MASK___CD ) << 16 );
  73. return( dwSwapped );
  74. }
  75. inline VOID ByteSwap( DWORD *pdw )
  76. {
  77. *pdw = ByteSwap(*pdw);
  78. }
  79. //
  80. // LONGLONG Byte-Swap
  81. //
  82. #ifndef __GNUC__
  83. #define BYTE_MASK_A_C_E_G_ 0xff00ff00ff00ff00
  84. #define BYTE_MASK__B_D_F_H 0x00ff00ff00ff00ff
  85. #define BYTE_MASK_AB__EF__ 0xffff0000ffff0000
  86. #define BYTE_MASK___CD__GH 0x0000ffff0000ffff
  87. #define BYTE_MASK_ABCD____ 0xffffffff00000000
  88. #define BYTE_MASK_____EFGH 0x00000000ffffffff
  89. #else //#ifndef GNUC
  90. #define BYTE_MASK_A_C_E_G_ 0xff00ff00ff00ff00LL
  91. #define BYTE_MASK__B_D_F_H 0x00ff00ff00ff00ffLL
  92. #define BYTE_MASK_AB__EF__ 0xffff0000ffff0000LL
  93. #define BYTE_MASK___CD__GH 0x0000ffff0000ffffLL
  94. #define BYTE_MASK_ABCD____ 0xffffffff00000000LL
  95. #define BYTE_MASK_____EFGH 0x00000000ffffffffLL
  96. #endif //#ifndef GNUC, else
  97. inline LONGLONG ByteSwap( LONGLONG llOriginal )
  98. {
  99. LONGLONG llSwapped;
  100. // ABCDEFGH => BADCFEHG
  101. llSwapped = (( (llOriginal) & BYTE_MASK_A_C_E_G_ ) >> 8 )
  102. |
  103. (( (llOriginal) & BYTE_MASK__B_D_F_H ) << 8 );
  104. // BADCFEHG => DCBAHGFE
  105. llSwapped = (( llSwapped & BYTE_MASK_AB__EF__ ) >> 16 )
  106. |
  107. (( llSwapped & BYTE_MASK___CD__GH ) << 16 );
  108. // DCBAHGFE => HGFEDCBA
  109. llSwapped = (( llSwapped & BYTE_MASK_ABCD____ ) >> 32 )
  110. |
  111. (( llSwapped & BYTE_MASK_____EFGH ) << 32 );
  112. return( llSwapped );
  113. }
  114. inline VOID ByteSwap( LONGLONG *pll )
  115. {
  116. // we have to deal with two DWORDs instead of one LONG LONG
  117. // because the pointer might not be 8 word aligned.
  118. // (it should be DWORD (4 bytes) aligned though)
  119. assert( sizeof(LONGLONG) == 2 * sizeof (DWORD));
  120. DWORD *pdw = (DWORD*)pll;
  121. DWORD dwTemp = ByteSwap( *pdw ); // temp = swapped(dw1)
  122. ByteSwap( pdw+1 ); // swap dw2
  123. *pdw = *(pdw+1); // dw1 = dw2(swapped)
  124. *(pdw+1) = dwTemp; // dw2 = temp
  125. return;
  126. }
  127. //
  128. // GUID Byte-swap
  129. //
  130. inline VOID ByteSwap( GUID *pguid)
  131. {
  132. ByteSwap(&pguid->Data1);
  133. ByteSwap(&pguid->Data2);
  134. ByteSwap(&pguid->Data3);
  135. }
  136. //
  137. // FILETIME Byte-Swap
  138. //
  139. // Note that we treat FILETIME as two DWORD's instead of
  140. // a single 8 byte integer, in memory or in disk
  141. // FILETIME should not be casted into a LONGLONG, or else
  142. // it will fail on a Big Endian machine.
  143. //
  144. inline VOID ByteSwap(FILETIME *pfileTime)
  145. {
  146. ByteSwap(&pfileTime->dwLowDateTime);
  147. ByteSwap(&pfileTime->dwHighDateTime);
  148. }
  149. #endif // !__BYTEORDER_HXX_