Source code of Windows XP (NT5)
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.

132 lines
2.1 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. byteswap.c
  5. Abstract:
  6. This module defines functions for performing endian conversions.
  7. Author:
  8. Forrest Foltz (forrestf) 10-Dec-1997
  9. Revision History:
  10. --*/
  11. #include "nt.h"
  12. #include "ntrtlp.h"
  13. #undef RtlUshortByteSwap
  14. USHORT
  15. FASTCALL
  16. RtlUshortByteSwap(
  17. IN USHORT Source
  18. )
  19. /*++
  20. Routine Description:
  21. The RtlUshortByteSwap function exchanges bytes 0 and 1 of Source
  22. and returns the resulting USHORT.
  23. Arguments:
  24. Source - 16-bit value to byteswap.
  25. Return Value:
  26. Swapped 16-bit value.
  27. --*/
  28. {
  29. USHORT swapped;
  30. swapped = ((Source) << (8 * 1)) |
  31. ((Source) >> (8 * 1));
  32. return swapped;
  33. }
  34. #undef RtlUlongByteSwap
  35. ULONG
  36. FASTCALL
  37. RtlUlongByteSwap(
  38. IN ULONG Source
  39. )
  40. /*++
  41. Routine Description:
  42. The RtlUlongByteSwap function exchanges byte pairs 0:3 and 1:2 of
  43. Source and returns the resulting ULONG.
  44. Arguments:
  45. Source - 32-bit value to byteswap.
  46. Return Value:
  47. Swapped 32-bit value.
  48. --*/
  49. {
  50. ULONG swapped;
  51. swapped = ((Source) << (8 * 3)) |
  52. ((Source & 0x0000FF00) << (8 * 1)) |
  53. ((Source & 0x00FF0000) >> (8 * 1)) |
  54. ((Source) >> (8 * 3));
  55. return swapped;
  56. }
  57. #undef RtlUlonglongByteSwap
  58. ULONGLONG
  59. FASTCALL
  60. RtlUlonglongByteSwap(
  61. IN ULONGLONG Source
  62. )
  63. /*++
  64. Routine Description:
  65. The RtlUlongByteSwap function exchanges byte pairs 0:7, 1:6, 2:5, and
  66. 3:4 of Source and returns the resulting ULONGLONG.
  67. Arguments:
  68. Source - 64-bit value to byteswap.
  69. Return Value:
  70. Swapped 64-bit value.
  71. --*/
  72. {
  73. ULONGLONG swapped;
  74. swapped = ((Source) << (8 * 7)) |
  75. ((Source & 0x000000000000FF00) << (8 * 5)) |
  76. ((Source & 0x0000000000FF0000) << (8 * 3)) |
  77. ((Source & 0x00000000FF000000) << (8 * 1)) |
  78. ((Source & 0x000000FF00000000) >> (8 * 1)) |
  79. ((Source & 0x0000FF0000000000) >> (8 * 3)) |
  80. ((Source & 0x00FF000000000000) >> (8 * 5)) |
  81. ((Source) >> (8 * 7));
  82. return swapped;
  83. }