endian.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // "License": Public Domain
  2. // I, Mathias Panzenböck, place this file hereby into the public domain. Use it at your own risk for whatever you like.
  3. // In case there are jurisdictions that don't support putting things in the public domain you can also consider it to
  4. // be "dual licensed" under the BSD, MIT and Apache licenses, if you want to. This code is trivial anyway. Consider it
  5. // an example on how to get the endian conversion functions on different platforms.
  6. #ifndef PORTABLE_ENDIAN_H__
  7. #define PORTABLE_ENDIAN_H__
  8. #if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
  9. # define __WINDOWS__
  10. #endif
  11. #if defined(__linux__) || defined(__CYGWIN__)
  12. # include <endian.h>
  13. #elif defined(__APPLE__)
  14. # include <libkern/OSByteOrder.h>
  15. # define htobe16(x) OSSwapHostToBigInt16(x)
  16. # define htole16(x) OSSwapHostToLittleInt16(x)
  17. # define be16toh(x) OSSwapBigToHostInt16(x)
  18. # define le16toh(x) OSSwapLittleToHostInt16(x)
  19. # define htobe32(x) OSSwapHostToBigInt32(x)
  20. # define htole32(x) OSSwapHostToLittleInt32(x)
  21. # define be32toh(x) OSSwapBigToHostInt32(x)
  22. # define le32toh(x) OSSwapLittleToHostInt32(x)
  23. # define htobe64(x) OSSwapHostToBigInt64(x)
  24. # define htole64(x) OSSwapHostToLittleInt64(x)
  25. # define be64toh(x) OSSwapBigToHostInt64(x)
  26. # define le64toh(x) OSSwapLittleToHostInt64(x)
  27. # define __BYTE_ORDER BYTE_ORDER
  28. # define __BIG_ENDIAN BIG_ENDIAN
  29. # define __LITTLE_ENDIAN LITTLE_ENDIAN
  30. # define __PDP_ENDIAN PDP_ENDIAN
  31. #elif defined(__OpenBSD__)
  32. # include <sys/endian.h>
  33. #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
  34. # include <sys/endian.h>
  35. # define be16toh(x) betoh16(x)
  36. # define le16toh(x) letoh16(x)
  37. # define be32toh(x) betoh32(x)
  38. # define le32toh(x) letoh32(x)
  39. # define be64toh(x) betoh64(x)
  40. # define le64toh(x) letoh64(x)
  41. #elif defined(__WINDOWS__)
  42. # include <winsock2.h>
  43. # include <sys/param.h>
  44. # if BYTE_ORDER == LITTLE_ENDIAN
  45. # define htobe16(x) htons(x)
  46. # define htole16(x) (x)
  47. # define be16toh(x) ntohs(x)
  48. # define le16toh(x) (x)
  49. # define htobe32(x) htonl(x)
  50. # define htole32(x) (x)
  51. # define be32toh(x) ntohl(x)
  52. # define le32toh(x) (x)
  53. # define htobe64(x) htonll(x)
  54. # define htole64(x) (x)
  55. # define be64toh(x) ntohll(x)
  56. # define le64toh(x) (x)
  57. # elif BYTE_ORDER == BIG_ENDIAN
  58. /* that would be xbox 360 */
  59. # define htobe16(x) (x)
  60. # define htole16(x) __builtin_bswap16(x)
  61. # define be16toh(x) (x)
  62. # define le16toh(x) __builtin_bswap16(x)
  63. # define htobe32(x) (x)
  64. # define htole32(x) __builtin_bswap32(x)
  65. # define be32toh(x) (x)
  66. # define le32toh(x) __builtin_bswap32(x)
  67. # define htobe64(x) (x)
  68. # define htole64(x) __builtin_bswap64(x)
  69. # define be64toh(x) (x)
  70. # define le64toh(x) __builtin_bswap64(x)
  71. # else
  72. # error byte order not supported
  73. # endif
  74. # define __BYTE_ORDER BYTE_ORDER
  75. # define __BIG_ENDIAN BIG_ENDIAN
  76. # define __LITTLE_ENDIAN LITTLE_ENDIAN
  77. # define __PDP_ENDIAN PDP_ENDIAN
  78. #else
  79. # error platform not supported
  80. #endif
  81. #endif