endian.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 <cmaier@cmassoc.net>;
  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 (WIN32)
  55. # include <stdint.h>
  56. # define BYTE_ORDER LITTLE_ENDIAN
  57. #elif defined (__vxworks)
  58. # include <netinet/in.h>
  59. #elif defined (__CYGWIN__)
  60. # error "Cygwin is unsupported!"
  61. #else
  62. # error "Unknown environment!"
  63. #endif
  64. /*====================================================================*
  65. * definitions;
  66. *--------------------------------------------------------------------*/
  67. #if defined (BYTE_ORDER)
  68. # if BYTE_ORDER == LITTLE_ENDIAN
  69. # define BE16TOH(x) __bswap_16(x)
  70. # define BE32TOH(x) __bswap_32(x)
  71. # define BE64TOH(x) __bswap_64(x)
  72. # define HTOBE16(x) __bswap_16(x)
  73. # define HTOBE32(x) __bswap_32(x)
  74. # define HTOBE64(x) __bswap_64(x)
  75. # define LE16TOH(x) (x)
  76. # define LE32TOH(x) (x)
  77. # define LE64TOH(x) (x)
  78. # define HTOLE16(x) (x)
  79. # define HTOLE32(x) (x)
  80. # define HTOLE64(x) (x)
  81. # elif BYTE_ORDER == BIG_ENDIAN
  82. # define BE16TOH(x) (x)
  83. # define BE32TOH(x) (x)
  84. # define BE64TOH(x) (x)
  85. # define HTOBE16(x) (x)
  86. # define HTOBE32(x) (x)
  87. # define HTOBE64(x) (x)
  88. # define LE16TOH(x) __bswap_16(x)
  89. # define LE32TOH(x) __bswap_32(x)
  90. # define LE64TOH(x) __bswap_64(x)
  91. # define HTOLE16(x) __bswap_16(x)
  92. # define HTOLE32(x) __bswap_32(x)
  93. # define HTOLE64(x) __bswap_64(x)
  94. # else
  95. # error "Undefined host byte order (2)."
  96. # endif
  97. #elif defined (__vxworks) && defined (_BYTE_ORDER)
  98. # if _BYTE_ORDER == _LITTLE_ENDIAN
  99. # define BE16TOH(x) __bswap_16(x)
  100. # define BE32TOH(x) __bswap_32(x)
  101. # define BE64TOH(x) __bswap_64(x)
  102. # define HTOBE16(x) __bswap_16(x)
  103. # define HTOBE32(x) __bswap_32(x)
  104. # define HTOBE64(x) __bswap_64(x)
  105. # define LE16TOH(x) (x)
  106. # define LE32TOH(x) (x)
  107. # define LE64TOH(x) (x)
  108. # define HTOLE16(x) (x)
  109. # define HTOLE32(x) (x)
  110. # define HTOLE64(x) (x)
  111. # elif _BYTE_ORDER == _BIG_ENDIAN
  112. # define __bswap_32(x) ((((x) & 0x000000ff) << 24) | (((x) & 0x0000ff00) << 8) | (((x) & 0x00ff0000) >> 8) | (((x) & 0xff000000) >> 24))
  113. # define __bswap_16(x) ((((x) & 0x00ff) << 8) | (((x) & 0xff00) >> 8))
  114. # define BE16TOH(x) (x)
  115. # define BE32TOH(x) (x)
  116. # define BE64TOH(x) (x)
  117. # define HTOBE16(x) (x)
  118. # define HTOBE32(x) (x)
  119. # define HTOBE64(x) (x)
  120. # define LE16TOH(x) __bswap_16(x)
  121. # define LE32TOH(x) __bswap_32(x)
  122. # define LE64TOH(x) __bswap_64(x)
  123. # define HTOLE16(x) __bswap_16(x)
  124. # define HTOLE32(x) __bswap_32(x)
  125. # define HTOLE64(x) __bswap_64(x)
  126. # else
  127. # error "Undefined host byte order vxworks."
  128. # endif
  129. #else
  130. #error "Undefined host byte order (1)."
  131. #endif
  132. /*====================================================================*
  133. * endian conversion functions;
  134. *--------------------------------------------------------------------*/
  135. #if defined (WIN32)
  136. uint16_t __bswap_16 (uint16_t x);
  137. uint32_t __bswap_32 (uint32_t x);
  138. uint64_t __bswap_64 (uint64_t x);
  139. #elif defined (__OpenBSD__)
  140. #define __bswap_16(x) swap16(x)
  141. #define __bswap_32(x) swap32(x)
  142. #define __bswap_64(x) swap64(x)
  143. #elif defined (__APPLE__)
  144. #define __bswap_16(x) OSSwapInt16(x)
  145. #define __bswap_32(x) OSSwapInt32(x)
  146. #define __bswap_64(x) OSSwapInt64(x)
  147. #elif defined (__linux__)
  148. #else
  149. #error "Unknown Environment"
  150. #endif
  151. /*====================================================================*
  152. * endian conversion functions;
  153. *--------------------------------------------------------------------*/
  154. void endian (void * memory, size_t extent);
  155. /*====================================================================*
  156. *
  157. *--------------------------------------------------------------------*/
  158. #endif