endian.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*====================================================================*
  2. *
  3. * endian.h - integer byte order declarations and definitions;
  4. *
  5. * this header is included to support recent moves to expand and
  6. * standardize endian conversion functions on Linux, FreeBSD and
  7. * NetBSD; Linux has implemented the following functions but OSX
  8. * and Microsoft have not yet done so;
  9. *
  10. * These functions are similar to network byteorder functions.
  11. * For example, be32toh() is identical to ntohl().
  12. *
  13. * #define _BSD_SOURCE
  14. * #include <endian.h>
  15. *
  16. * uint16_t htobe16(uint16_t x);
  17. * uint16_t htole16(uint16_t x);
  18. * uint16_t be16toh(uint16_t x);
  19. * uint16_t le16toh(uint16_t x);
  20. *
  21. * uint32_t htobe32(uint32_t x);
  22. * uint32_t htole32(uint32_t x);
  23. * uint32_t be32toh(uint32_t x);
  24. * uint32_t le32toh(uint32_t x);
  25. *
  26. * uint64_t htobe64(uint64_t x);
  27. * uint64_t htole64(uint64_t x);
  28. * uint64_t be64toh(uint64_t x);
  29. * uint64_t le64toh(uint64_t x);
  30. *
  31. * The advantage of network byteorder functions is that they are
  32. * available on all Unix systems but they were meant for network
  33. * applications and lack little-endian and 64-bit variants;
  34. *
  35. * Motley Tools by Charles Maier;
  36. * Copyright 2001-2006 by Charles Maier Associates;
  37. * Licensed under the Internet Software Consortium License;
  38. *
  39. *--------------------------------------------------------------------*/
  40. #ifndef ENDIAN_HEADER
  41. #define ENDIAN_HEADER
  42. /*====================================================================*
  43. * system header files;
  44. *--------------------------------------------------------------------*/
  45. #include <unistd.h>
  46. #if defined (__linux__)
  47. # include <endian.h>
  48. # include <byteswap.h>
  49. #elif defined (__APPLE__)
  50. # include <netinet/in.h>
  51. # include <libkern/OSByteOrder.h>
  52. #elif defined (__OpenBSD__)
  53. # include <sys/types.h>
  54. #elif defined (__FreeBSD__)
  55. # include <sys/types.h>
  56. # include <sys/endian.h>
  57. #elif defined (__NetBSD__)
  58. # include <sys/types.h>
  59. # include <machine/bswap.h>
  60. #elif defined (WIN32)
  61. # include <stdint.h>
  62. # define BYTE_ORDER LITTLE_ENDIAN
  63. #elif defined (__vxworks)
  64. # include <netinet/in.h>
  65. #elif defined (__CYGWIN__)
  66. # error "Cygwin is unsupported"
  67. #else
  68. # error "Unknown environment"
  69. #endif
  70. /*====================================================================*
  71. * definitions;
  72. *--------------------------------------------------------------------*/
  73. /* Some systems may already define some of these, returning void */
  74. #undef HTOBE16
  75. #undef HTOBE32
  76. #undef HTOBE64
  77. #undef HTOLE16
  78. #undef HTOLE32
  79. #undef HTOLE64
  80. #undef BE64TOH
  81. #undef BE32TOH
  82. #undef BE16TOH
  83. #undef LE16TOH
  84. #undef LE32TOH
  85. #undef LE64TOH
  86. #if defined (BYTE_ORDER)
  87. # if BYTE_ORDER == LITTLE_ENDIAN
  88. # define BE16TOH(x) __bswap_16(x)
  89. # define BE32TOH(x) __bswap_32(x)
  90. # define BE64TOH(x) __bswap_64(x)
  91. # define HTOBE16(x) __bswap_16(x)
  92. # define HTOBE32(x) __bswap_32(x)
  93. # define HTOBE64(x) __bswap_64(x)
  94. # define LE16TOH(x) (x)
  95. # define LE32TOH(x) (x)
  96. # define LE64TOH(x) (x)
  97. # define HTOLE16(x) (x)
  98. # define HTOLE32(x) (x)
  99. # define HTOLE64(x) (x)
  100. # elif BYTE_ORDER == BIG_ENDIAN
  101. # define BE16TOH(x) (x)
  102. # define BE32TOH(x) (x)
  103. # define BE64TOH(x) (x)
  104. # define HTOBE16(x) (x)
  105. # define HTOBE32(x) (x)
  106. # define HTOBE64(x) (x)
  107. # define LE16TOH(x) __bswap_16(x)
  108. # define LE32TOH(x) __bswap_32(x)
  109. # define LE64TOH(x) __bswap_64(x)
  110. # define HTOLE16(x) __bswap_16(x)
  111. # define HTOLE32(x) __bswap_32(x)
  112. # define HTOLE64(x) __bswap_64(x)
  113. # else
  114. # error "Undefined host byte order (2)."
  115. # endif
  116. #elif defined (__vxworks) && defined (_BYTE_ORDER)
  117. # if _BYTE_ORDER == _LITTLE_ENDIAN
  118. # define BE16TOH(x) __bswap_16(x)
  119. # define BE32TOH(x) __bswap_32(x)
  120. # define BE64TOH(x) __bswap_64(x)
  121. # define HTOBE16(x) __bswap_16(x)
  122. # define HTOBE32(x) __bswap_32(x)
  123. # define HTOBE64(x) __bswap_64(x)
  124. # define LE16TOH(x) (x)
  125. # define LE32TOH(x) (x)
  126. # define LE64TOH(x) (x)
  127. # define HTOLE16(x) (x)
  128. # define HTOLE32(x) (x)
  129. # define HTOLE64(x) (x)
  130. # elif _BYTE_ORDER == _BIG_ENDIAN
  131. # define __bswap_32(x) ((((x) & 0x000000ff) << 24) | (((x) & 0x0000ff00) << 8) | (((x) & 0x00ff0000) >> 8) | (((x) & 0xff000000) >> 24))
  132. # define __bswap_16(x) ((((x) & 0x00ff) << 8) | (((x) & 0xff00) >> 8))
  133. # define BE16TOH(x) (x)
  134. # define BE32TOH(x) (x)
  135. # define BE64TOH(x) (x)
  136. # define HTOBE16(x) (x)
  137. # define HTOBE32(x) (x)
  138. # define HTOBE64(x) (x)
  139. # define LE16TOH(x) __bswap_16(x)
  140. # define LE32TOH(x) __bswap_32(x)
  141. # define LE64TOH(x) __bswap_64(x)
  142. # define HTOLE16(x) __bswap_16(x)
  143. # define HTOLE32(x) __bswap_32(x)
  144. # define HTOLE64(x) __bswap_64(x)
  145. # else
  146. # error "Undefined host byte order vxworks."
  147. # endif
  148. #else
  149. #error "Undefined host byte order (1)."
  150. #endif
  151. /*====================================================================*
  152. * endian conversion functions;
  153. *--------------------------------------------------------------------*/
  154. #if defined (WIN32)
  155. uint16_t __bswap_16 (uint16_t x);
  156. uint32_t __bswap_32 (uint32_t x);
  157. uint64_t __bswap_64 (uint64_t x);
  158. #elif defined (__OpenBSD__)
  159. #define __bswap_16(x) swap16(x)
  160. #define __bswap_32(x) swap32(x)
  161. #define __bswap_64(x) swap64(x)
  162. #elif defined (__NetBSD__) || defined(__FreeBSD__)
  163. #define __bswap_16(x) bswap16(x)
  164. #define __bswap_32(x) bswap32(x)
  165. #define __bswap_64(x) bswap64(x)
  166. #elif defined (__APPLE__)
  167. #define __bswap_16(x) OSSwapInt16(x)
  168. #define __bswap_32(x) OSSwapInt32(x)
  169. #define __bswap_64(x) OSSwapInt64(x)
  170. #elif defined (__linux__)
  171. #else
  172. #error "Unknown Environment"
  173. #endif
  174. /*====================================================================*
  175. *
  176. *--------------------------------------------------------------------*/
  177. #endif