endian.h.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?xml version='1.0' encoding='iso-8859-1'?>
  2. <!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  3. <html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
  4. <head>
  5. <title>
  6. endian.h
  7. </title>
  8. <meta http-equiv='content-type' content='text/html;iso-8859-1'/>
  9. <meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
  10. <meta name='author' content='cmaier@cmassoc.net'/>
  11. <meta name='robots' content='noindex,nofollow'/>
  12. <link href='toolkit.css' rel='stylesheet' type='text/css'/>
  13. </head>
  14. <body>
  15. <div class='headerlink'>
  16. [<a href='debug.h.html' title=' debug.h '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='error.h.html' title=' error.h '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * endian.h - integer byte order declarations and definitions;
  24. *
  25. * this header is included to support recent moves to expand and
  26. * standardize endian conversion functions on Linux, FreeBSD and
  27. * NetBSD; Linux has implemented the following functions but OSX
  28. * and Microsoft have not yet done so;
  29. *
  30. * These functions are similar to network byteorder functions.
  31. * For example, be32toh() is identical to ntohl().
  32. *
  33. * #define _BSD_SOURCE
  34. * #include &lt;endian.h&gt;
  35. *
  36. * uint16_t htobe16(uint16_t x);
  37. * uint16_t htole16(uint16_t x);
  38. * uint16_t be16toh(uint16_t x);
  39. * uint16_t le16toh(uint16_t x);
  40. *
  41. * uint32_t htobe32(uint32_t x);
  42. * uint32_t htole32(uint32_t x);
  43. * uint32_t be32toh(uint32_t x);
  44. * uint32_t le32toh(uint32_t x);
  45. *
  46. * uint64_t htobe64(uint64_t x);
  47. * uint64_t htole64(uint64_t x);
  48. * uint64_t be64toh(uint64_t x);
  49. * uint64_t le64toh(uint64_t x);
  50. *
  51. * The advantage of network byteorder functions is that they are
  52. * available on all Unix systems but they were meant for network
  53. * applications and lack little-endian and 64-bit variants;
  54. *
  55. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  56. * Copyright 2001-2006 by Charles Maier Associates;
  57. * Licensed under the Internet Software Consortium License;
  58. *
  59. *--------------------------------------------------------------------*/
  60. #ifndef ENDIAN_HEADER
  61. #define ENDIAN_HEADER
  62. /*====================================================================*
  63. * system header files;
  64. *--------------------------------------------------------------------*/
  65. #include &lt;unistd.h&gt;
  66. #if defined (__linux__)
  67. # include &lt;endian.h&gt;
  68. # include &lt;byteswap.h&gt;
  69. #elif defined (__APPLE__)
  70. # include &lt;netinet/in.h&gt;
  71. # include &lt;libkern/OSByteOrder.h&gt;
  72. #elif defined (__OpenBSD__)
  73. # include &lt;sys/types.h&gt;
  74. #elif defined (WIN32)
  75. # include &lt;stdint.h&gt;
  76. # define BYTE_ORDER LITTLE_ENDIAN
  77. #elif defined (__vxworks)
  78. # include &lt;netinet/in.h&gt;
  79. #elif defined (__CYGWIN__)
  80. # error &quot;Cygwin is unsupported&quot;
  81. #else
  82. # error &quot;Unknown environment&quot;
  83. #endif
  84. /*====================================================================*
  85. * definitions;
  86. *--------------------------------------------------------------------*/
  87. #if defined (BYTE_ORDER)
  88. # if BYTE_ORDER == LITTLE_ENDIAN
  89. # define BE16TOH(x) __bswap_16(x)
  90. # define BE32TOH(x) __bswap_32(x)
  91. # define BE64TOH(x) __bswap_64(x)
  92. # define HTOBE16(x) __bswap_16(x)
  93. # define HTOBE32(x) __bswap_32(x)
  94. # define HTOBE64(x) __bswap_64(x)
  95. # define LE16TOH(x) (x)
  96. # define LE32TOH(x) (x)
  97. # define LE64TOH(x) (x)
  98. # define HTOLE16(x) (x)
  99. # define HTOLE32(x) (x)
  100. # define HTOLE64(x) (x)
  101. # elif BYTE_ORDER == BIG_ENDIAN
  102. # define BE16TOH(x) (x)
  103. # define BE32TOH(x) (x)
  104. # define BE64TOH(x) (x)
  105. # define HTOBE16(x) (x)
  106. # define HTOBE32(x) (x)
  107. # define HTOBE64(x) (x)
  108. # define LE16TOH(x) __bswap_16(x)
  109. # define LE32TOH(x) __bswap_32(x)
  110. # define LE64TOH(x) __bswap_64(x)
  111. # define HTOLE16(x) __bswap_16(x)
  112. # define HTOLE32(x) __bswap_32(x)
  113. # define HTOLE64(x) __bswap_64(x)
  114. # else
  115. # error &quot;Undefined host byte order (2).&quot;
  116. # endif
  117. #elif defined (__vxworks) &amp;&amp; defined (_BYTE_ORDER)
  118. # if _BYTE_ORDER == _LITTLE_ENDIAN
  119. # define BE16TOH(x) __bswap_16(x)
  120. # define BE32TOH(x) __bswap_32(x)
  121. # define BE64TOH(x) __bswap_64(x)
  122. # define HTOBE16(x) __bswap_16(x)
  123. # define HTOBE32(x) __bswap_32(x)
  124. # define HTOBE64(x) __bswap_64(x)
  125. # define LE16TOH(x) (x)
  126. # define LE32TOH(x) (x)
  127. # define LE64TOH(x) (x)
  128. # define HTOLE16(x) (x)
  129. # define HTOLE32(x) (x)
  130. # define HTOLE64(x) (x)
  131. # elif _BYTE_ORDER == _BIG_ENDIAN
  132. # define __bswap_32(x) ((((x) &amp; 0x000000ff) &lt;&lt; 24) | (((x) &amp; 0x0000ff00) &lt;&lt; 8) | (((x) &amp; 0x00ff0000) &gt;&gt; 8) | (((x) &amp; 0xff000000) &gt;&gt; 24))
  133. # define __bswap_16(x) ((((x) &amp; 0x00ff) &lt;&lt; 8) | (((x) &amp; 0xff00) &gt;&gt; 8))
  134. # define BE16TOH(x) (x)
  135. # define BE32TOH(x) (x)
  136. # define BE64TOH(x) (x)
  137. # define HTOBE16(x) (x)
  138. # define HTOBE32(x) (x)
  139. # define HTOBE64(x) (x)
  140. # define LE16TOH(x) __bswap_16(x)
  141. # define LE32TOH(x) __bswap_32(x)
  142. # define LE64TOH(x) __bswap_64(x)
  143. # define HTOLE16(x) __bswap_16(x)
  144. # define HTOLE32(x) __bswap_32(x)
  145. # define HTOLE64(x) __bswap_64(x)
  146. # else
  147. # error &quot;Undefined host byte order vxworks.&quot;
  148. # endif
  149. #else
  150. #error &quot;Undefined host byte order (1).&quot;
  151. #endif
  152. /*====================================================================*
  153. * endian conversion functions;
  154. *--------------------------------------------------------------------*/
  155. #if defined (WIN32)
  156. uint16_t __bswap_16 (uint16_t x);
  157. uint32_t __bswap_32 (uint32_t x);
  158. uint64_t __bswap_64 (uint64_t x);
  159. #elif defined (__OpenBSD__)
  160. #define __bswap_16(x) swap16(x)
  161. #define __bswap_32(x) swap32(x)
  162. #define __bswap_64(x) swap64(x)
  163. #elif defined (__APPLE__)
  164. #define __bswap_16(x) OSSwapInt16(x)
  165. #define __bswap_32(x) OSSwapInt32(x)
  166. #define __bswap_64(x) OSSwapInt64(x)
  167. #elif defined (__linux__)
  168. #else
  169. #error &quot;Unknown Environment&quot;
  170. #endif
  171. /*====================================================================*
  172. *
  173. *--------------------------------------------------------------------*/
  174. #endif
  175. </pre>
  176. <div class='footerlink'>
  177. [<a href='debug.h.html' title=' debug.h '>PREV</a>]
  178. [<a href='toolkit.html' title=' Index '>HOME</a>]
  179. [<a href='error.h.html' title=' error.h '>NEXT</a>]
  180. </div>
  181. </body>
  182. </html>