portable_endian.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. # include <sys/param.h>
  43. # if BYTE_ORDER == LITTLE_ENDIAN
  44. # define htobe16 htons
  45. # define htole16(x) (x)
  46. # define be16toh ntohs
  47. # define le16toh(x) (x)
  48. # define htobe32 htonl
  49. # define htole32(x) (x)
  50. # define be32toh ntohl
  51. # define le32toh(x) (x)
  52. # define htobe64 htonll
  53. # define htole64(x) (x)
  54. # define be64toh ntohll
  55. # define le64toh(x) (x)
  56. # elif BYTE_ORDER == BIG_ENDIAN
  57. /* that would be xbox 360 */
  58. # define htobe16(x) (x)
  59. # define htole16(x) __builtin_bswap16(x)
  60. # define be16toh(x) (x)
  61. # define le16toh(x) __builtin_bswap16(x)
  62. # define htobe32(x) (x)
  63. # define htole32(x) __builtin_bswap32(x)
  64. # define be32toh(x) (x)
  65. # define le32toh(x) __builtin_bswap32(x)
  66. # define htobe64(x) (x)
  67. # define htole64(x) __builtin_bswap64(x)
  68. # define be64toh(x) (x)
  69. # define le64toh(x) __builtin_bswap64(x)
  70. # else
  71. # error byte order not supported
  72. # endif
  73. # define __BYTE_ORDER BYTE_ORDER
  74. # define __BIG_ENDIAN BIG_ENDIAN
  75. # define __LITTLE_ENDIAN LITTLE_ENDIAN
  76. # define __PDP_ENDIAN PDP_ENDIAN
  77. #elif defined(__QNXNTO__)
  78. # include <gulliver.h>
  79. # define htobe16 ENDIAN_BE16
  80. # define htole16 ENDIAN_LE16
  81. # define htobe32 ENDIAN_BE32
  82. # define htole32 ENDIAN_LE32
  83. # define htobe64 ENDIAN_BE64
  84. # define htole64 ENDIAN_LE64
  85. #elif defined (__vxworks)
  86. # include <arch/arm/vxbAccessArchLib.h>
  87. #else
  88. # error platform not supported
  89. #endif
  90. #endif //#ifndef PORTABLE_ENDIAN_H__