endian.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* sys/endian.h - bsd-games compatibility with NetBSD <sys/endian.h> (not
  2. * a complete emulation).
  3. *
  4. * Copyright (c) 1999 Joseph Samuel Myers.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  19. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <features.h>
  31. #include <sys/types.h>
  32. #include <endian.h>
  33. #include <netinet/in.h>
  34. #if __BYTE_ORDER == __BIG_ENDIAN
  35. #ifndef be16toh
  36. #define be16toh(x) ((u_int16_t)(x))
  37. #endif
  38. #ifndef htobe16
  39. #define htobe16(x) ((u_int16_t)(x))
  40. #endif
  41. #ifndef be32toh
  42. #define be32toh(x) ((u_int32_t)(x))
  43. #endif
  44. #ifndef htobe32
  45. #define htobe32(x) ((u_int32_t)(x))
  46. #endif
  47. #ifndef be64toh
  48. #define be64toh(x) ((u_int64_t)(x))
  49. #endif
  50. #ifndef htobe64
  51. #define htobe64(x) ((u_int64_t)(x))
  52. #endif
  53. #ifndef BE16TOH
  54. #define BE16TOH(x) ((void)0)
  55. #endif
  56. #ifndef HTOBE16
  57. #define HTOBE16(x) ((void)0)
  58. #endif
  59. #ifndef BE32TOH
  60. #define BE32TOH(x) ((void)0)
  61. #endif
  62. #ifndef HTOBE32
  63. #define HTOBE32(x) ((void)0)
  64. #endif
  65. #ifndef BE64TOH
  66. #define BE64TOH(x) ((void)0)
  67. #endif
  68. #ifndef HTOBE64
  69. #define HTOBE64(x) ((void)0)
  70. #endif
  71. #else /* little-endian */
  72. #ifndef be16toh
  73. #define be16toh(x) ((u_int16_t)ntohs((u_int16_t)(x)))
  74. #endif
  75. #ifndef htobe16
  76. #define htobe16(x) ((u_int16_t)htons((u_int16_t)(x)))
  77. #endif
  78. #ifndef be32toh
  79. #define be32toh(x) ((u_int32_t)ntohl((u_int32_t)(x)))
  80. #endif
  81. #ifndef htobe32
  82. #define htobe32(x) ((u_int32_t)htonl((u_int32_t)(x)))
  83. #endif
  84. #ifndef be64toh
  85. #ifdef __bswap_64 /* glibc */
  86. #define be64toh(x) ((u_int64_t)__bswap_64((u_int64_t)(x)))
  87. #else /* no __bswap_64 */
  88. #ifdef __swab64 /* Linux kernel headers (libc5, at least with kernel 2.2) */
  89. #define be64toh(x) ((u_int64_t)__swab64((u_int64_t)(x)))
  90. #else /* no __bswap_64 or __swab64 */
  91. static __inline__ u_int64_t be64toh(u_int64_t __x);
  92. static __inline__ u_int64_t be64toh(u_int64_t __x) { return (((u_int64_t)be32toh(__x & (u_int64_t)0xFFFFFFFFULL)) << 32) | ((u_int64_t)be32toh((__x & (u_int64_t)0xFFFFFFFF00000000ULL) >> 32)); }
  93. #define be64toh(x) be64toh((x))
  94. #endif /* no __bswap_64 or __swab64 */
  95. #endif /* no __bswap_64 */
  96. #endif /* no be64toh */
  97. #ifndef htobe64
  98. #define htobe64(x) be64toh(x)
  99. #endif
  100. #ifndef BE16TOH
  101. #define BE16TOH(x) ((x) = be16toh((x)))
  102. #endif
  103. #ifndef HTOBE16
  104. #define HTOBE16(x) ((x) = htobe16((x)))
  105. #endif
  106. #ifndef BE32TOH
  107. #define BE32TOH(x) ((x) = be32toh((x)))
  108. #endif
  109. #ifndef HTOBE32
  110. #define HTOBE32(x) ((x) = htobe32((x)))
  111. #endif
  112. #ifndef BE64TOH
  113. #define BE64TOH(x) ((x) = be64toh((x)))
  114. #endif
  115. #ifndef HTOBE64
  116. #define HTOBE64(x) ((x) = htobe64((x)))
  117. #endif
  118. #endif /* little-endian */