I2PEndian.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #ifndef I2PENDIAN_H__
  2. #define I2PENDIAN_H__
  3. #include <inttypes.h>
  4. #include <string.h>
  5. #if defined(__FreeBSD__) || defined(__NetBSD__)
  6. #include <sys/endian.h>
  7. #elif defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) || defined(__GLIBC__)
  8. #include <endian.h>
  9. #elif defined(__APPLE__) && defined(__MACH__)
  10. #include <libkern/OSByteOrder.h>
  11. #define htobe16(x) OSSwapHostToBigInt16(x)
  12. #define htole16(x) OSSwapHostToLittleInt16(x)
  13. #define be16toh(x) OSSwapBigToHostInt16(x)
  14. #define le16toh(x) OSSwapLittleToHostInt16(x)
  15. #define htobe32(x) OSSwapHostToBigInt32(x)
  16. #define htole32(x) OSSwapHostToLittleInt32(x)
  17. #define be32toh(x) OSSwapBigToHostInt32(x)
  18. #define le32toh(x) OSSwapLittleToHostInt32(x)
  19. #define htobe64(x) OSSwapHostToBigInt64(x)
  20. #define htole64(x) OSSwapHostToLittleInt64(x)
  21. #define be64toh(x) OSSwapBigToHostInt64(x)
  22. #define le64toh(x) OSSwapLittleToHostInt64(x)
  23. #else
  24. #define NEEDS_LOCAL_ENDIAN
  25. #include <cstdint>
  26. uint16_t htobe16(uint16_t int16);
  27. uint32_t htobe32(uint32_t int32);
  28. uint64_t htobe64(uint64_t int64);
  29. uint16_t be16toh(uint16_t big16);
  30. uint32_t be32toh(uint32_t big32);
  31. uint64_t be64toh(uint64_t big64);
  32. // assume LittleEndine
  33. #define htole16
  34. #define htole32
  35. #define htole64
  36. #define le16toh
  37. #define le32toh
  38. #define le64toh
  39. #endif
  40. inline uint16_t buf16toh(const void *buf)
  41. {
  42. uint16_t b16;
  43. memcpy(&b16, buf, sizeof(uint16_t));
  44. return b16;
  45. }
  46. inline uint32_t buf32toh(const void *buf)
  47. {
  48. uint32_t b32;
  49. memcpy(&b32, buf, sizeof(uint32_t));
  50. return b32;
  51. }
  52. inline uint64_t buf64toh(const void *buf)
  53. {
  54. uint64_t b64;
  55. memcpy(&b64, buf, sizeof(uint64_t));
  56. return b64;
  57. }
  58. inline uint16_t bufbe16toh(const void *buf)
  59. {
  60. return be16toh(buf16toh(buf));
  61. }
  62. inline uint32_t bufbe32toh(const void *buf)
  63. {
  64. return be32toh(buf32toh(buf));
  65. }
  66. inline uint64_t bufbe64toh(const void *buf)
  67. {
  68. return be64toh(buf64toh(buf));
  69. }
  70. inline void htobuf16(void *buf, uint16_t b16)
  71. {
  72. memcpy(buf, &b16, sizeof(uint16_t));
  73. }
  74. inline void htobuf32(void *buf, uint32_t b32)
  75. {
  76. memcpy(buf, &b32, sizeof(uint32_t));
  77. }
  78. inline void htobuf64(void *buf, uint64_t b64)
  79. {
  80. memcpy(buf, &b64, sizeof(uint64_t));
  81. }
  82. inline void htobe16buf(void *buf, uint16_t big16)
  83. {
  84. htobuf16(buf, htobe16(big16));
  85. }
  86. inline void htobe32buf(void *buf, uint32_t big32)
  87. {
  88. htobuf32(buf, htobe32(big32));
  89. }
  90. inline void htobe64buf(void *buf, uint64_t big64)
  91. {
  92. htobuf64(buf, htobe64(big64));
  93. }
  94. inline void htole16buf(void *buf, uint16_t big16)
  95. {
  96. htobuf16(buf, htole16(big16));
  97. }
  98. inline void htole32buf(void *buf, uint32_t big32)
  99. {
  100. htobuf32(buf, htole32(big32));
  101. }
  102. inline void htole64buf(void *buf, uint64_t big64)
  103. {
  104. htobuf64(buf, htole64(big64));
  105. }
  106. #endif // I2PENDIAN_H__