portable_endian.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Thanks to Mathias Panzenböck aka panzi
  3. * https://gist.github.com/panzi/6856583
  4. */
  5. #ifndef PORTABLE_ENDIAN_H__
  6. #define PORTABLE_ENDIAN_H__
  7. #if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
  8. # define __WINDOWS__
  9. #endif
  10. #if defined(__linux__) || defined(__CYGWIN__)
  11. # include <endian.h>
  12. #elif defined(__APPLE__)
  13. # include <libkern/OSByteOrder.h>
  14. # define htobe16 OSSwapHostToBigInt16
  15. # define htole16 OSSwapHostToLittleInt16
  16. # define be16toh OSSwapBigToHostInt16
  17. # define le16toh OSSwapLittleToHostInt16
  18. # define htobe32 OSSwapHostToBigInt32
  19. # define htole32 OSSwapHostToLittleInt32
  20. # define be32toh OSSwapBigToHostInt32
  21. # define le32toh OSSwapLittleToHostInt32
  22. # define htobe64 OSSwapHostToBigInt64
  23. # define htole64 OSSwapHostToLittleInt64
  24. # define be64toh OSSwapBigToHostInt64
  25. # define le64toh OSSwapLittleToHostInt64
  26. # define __BYTE_ORDER BYTE_ORDER
  27. # define __BIG_ENDIAN BIG_ENDIAN
  28. # define __LITTLE_ENDIAN LITTLE_ENDIAN
  29. # define __PDP_ENDIAN PDP_ENDIAN
  30. #elif defined(__OpenBSD__)
  31. # include <sys/endian.h>
  32. #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
  33. # include <sys/endian.h>
  34. # define be16toh betoh16
  35. # define le16toh letoh16
  36. # define be32toh betoh32
  37. # define le32toh letoh32
  38. # define be64toh betoh64
  39. # define le64toh letoh64
  40. #elif defined(__WINDOWS__)
  41. # include <winsock2.h>
  42. # if BYTE_ORDER == LITTLE_ENDIAN
  43. # define htobe16 htons
  44. # define htole16(x) (x)
  45. # define be16toh ntohs
  46. # define le16toh(x) (x)
  47. # define htobe32 htonl
  48. # define htole32(x) (x)
  49. # define be32toh ntohl
  50. # define le32toh(x) (x)
  51. # define htobe64 htonll
  52. # define htole64(x) (x)
  53. # define be64toh ntohll
  54. # define le64toh(x) (x)
  55. # elif BYTE_ORDER == BIG_ENDIAN
  56. /* that would be xbox 360 */
  57. # define htobe16(x) (x)
  58. # define htole16(x) __builtin_bswap16(x)
  59. # define be16toh(x) (x)
  60. # define le16toh(x) __builtin_bswap16(x)
  61. # define htobe32(x) (x)
  62. # define htole32(x) __builtin_bswap32(x)
  63. # define be32toh(x) (x)
  64. # define le32toh(x) __builtin_bswap32(x)
  65. # define htobe64(x) (x)
  66. # define htole64(x) __builtin_bswap64(x)
  67. # define be64toh(x) (x)
  68. # define le64toh(x) __builtin_bswap64(x)
  69. # else
  70. # error byte order not supported
  71. # endif
  72. # define __BYTE_ORDER BYTE_ORDER
  73. # define __BIG_ENDIAN BIG_ENDIAN
  74. # define __LITTLE_ENDIAN LITTLE_ENDIAN
  75. # define __PDP_ENDIAN PDP_ENDIAN
  76. #elif defined(__QNXNTO__)
  77. # include <gulliver.h>
  78. # define htobe16 ENDIAN_BE16
  79. # define htole16 ENDIAN_LE16
  80. # define htobe32 ENDIAN_BE32
  81. # define htole32 ENDIAN_LE32
  82. # define htobe64 ENDIAN_BE64
  83. # define htole64 ENDIAN_LE64
  84. #elif defined (__vxworks)
  85. # include <arch/arm/vxbAccessArchLib.h>
  86. #else
  87. # error platform not supported
  88. #endif
  89. #endif //#ifndef PORTABLE_ENDIAN_H__