You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright (c) 2014-2016 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #ifndef STARWELS_COMPAT_ENDIAN_H
  5. #define STARWELS_COMPAT_ENDIAN_H
  6. #if defined(HAVE_CONFIG_H)
  7. #include "config/starwels-config.h"
  8. #endif
  9. #include <stdint.h>
  10. #include "compat/byteswap.h"
  11. #if defined(HAVE_ENDIAN_H)
  12. #include <endian.h>
  13. #elif defined(HAVE_SYS_ENDIAN_H)
  14. #include <sys/endian.h>
  15. #endif
  16. #if defined(WORDS_BIGENDIAN)
  17. #if HAVE_DECL_HTOBE16 == 0
  18. inline uint16_t htobe16(uint16_t host_16bits)
  19. {
  20. return host_16bits;
  21. }
  22. #endif // HAVE_DECL_HTOBE16
  23. #if HAVE_DECL_HTOLE16 == 0
  24. inline uint16_t htole16(uint16_t host_16bits)
  25. {
  26. return bswap_16(host_16bits);
  27. }
  28. #endif // HAVE_DECL_HTOLE16
  29. #if HAVE_DECL_BE16TOH == 0
  30. inline uint16_t be16toh(uint16_t big_endian_16bits)
  31. {
  32. return big_endian_16bits;
  33. }
  34. #endif // HAVE_DECL_BE16TOH
  35. #if HAVE_DECL_LE16TOH == 0
  36. inline uint16_t le16toh(uint16_t little_endian_16bits)
  37. {
  38. return bswap_16(little_endian_16bits);
  39. }
  40. #endif // HAVE_DECL_LE16TOH
  41. #if HAVE_DECL_HTOBE32 == 0
  42. inline uint32_t htobe32(uint32_t host_32bits)
  43. {
  44. return host_32bits;
  45. }
  46. #endif // HAVE_DECL_HTOBE32
  47. #if HAVE_DECL_HTOLE32 == 0
  48. inline uint32_t htole32(uint32_t host_32bits)
  49. {
  50. return bswap_32(host_32bits);
  51. }
  52. #endif // HAVE_DECL_HTOLE32
  53. #if HAVE_DECL_BE32TOH == 0
  54. inline uint32_t be32toh(uint32_t big_endian_32bits)
  55. {
  56. return big_endian_32bits;
  57. }
  58. #endif // HAVE_DECL_BE32TOH
  59. #if HAVE_DECL_LE32TOH == 0
  60. inline uint32_t le32toh(uint32_t little_endian_32bits)
  61. {
  62. return bswap_32(little_endian_32bits);
  63. }
  64. #endif // HAVE_DECL_LE32TOH
  65. #if HAVE_DECL_HTOBE64 == 0
  66. inline uint64_t htobe64(uint64_t host_64bits)
  67. {
  68. return host_64bits;
  69. }
  70. #endif // HAVE_DECL_HTOBE64
  71. #if HAVE_DECL_HTOLE64 == 0
  72. inline uint64_t htole64(uint64_t host_64bits)
  73. {
  74. return bswap_64(host_64bits);
  75. }
  76. #endif // HAVE_DECL_HTOLE64
  77. #if HAVE_DECL_BE64TOH == 0
  78. inline uint64_t be64toh(uint64_t big_endian_64bits)
  79. {
  80. return big_endian_64bits;
  81. }
  82. #endif // HAVE_DECL_BE64TOH
  83. #if HAVE_DECL_LE64TOH == 0
  84. inline uint64_t le64toh(uint64_t little_endian_64bits)
  85. {
  86. return bswap_64(little_endian_64bits);
  87. }
  88. #endif // HAVE_DECL_LE64TOH
  89. #else // WORDS_BIGENDIAN
  90. #if HAVE_DECL_HTOBE16 == 0
  91. inline uint16_t htobe16(uint16_t host_16bits)
  92. {
  93. return bswap_16(host_16bits);
  94. }
  95. #endif // HAVE_DECL_HTOBE16
  96. #if HAVE_DECL_HTOLE16 == 0
  97. inline uint16_t htole16(uint16_t host_16bits)
  98. {
  99. return host_16bits;
  100. }
  101. #endif // HAVE_DECL_HTOLE16
  102. #if HAVE_DECL_BE16TOH == 0
  103. inline uint16_t be16toh(uint16_t big_endian_16bits)
  104. {
  105. return bswap_16(big_endian_16bits);
  106. }
  107. #endif // HAVE_DECL_BE16TOH
  108. #if HAVE_DECL_LE16TOH == 0
  109. inline uint16_t le16toh(uint16_t little_endian_16bits)
  110. {
  111. return little_endian_16bits;
  112. }
  113. #endif // HAVE_DECL_LE16TOH
  114. #if HAVE_DECL_HTOBE32 == 0
  115. inline uint32_t htobe32(uint32_t host_32bits)
  116. {
  117. return bswap_32(host_32bits);
  118. }
  119. #endif // HAVE_DECL_HTOBE32
  120. #if HAVE_DECL_HTOLE32 == 0
  121. inline uint32_t htole32(uint32_t host_32bits)
  122. {
  123. return host_32bits;
  124. }
  125. #endif // HAVE_DECL_HTOLE32
  126. #if HAVE_DECL_BE32TOH == 0
  127. inline uint32_t be32toh(uint32_t big_endian_32bits)
  128. {
  129. return bswap_32(big_endian_32bits);
  130. }
  131. #endif // HAVE_DECL_BE32TOH
  132. #if HAVE_DECL_LE32TOH == 0
  133. inline uint32_t le32toh(uint32_t little_endian_32bits)
  134. {
  135. return little_endian_32bits;
  136. }
  137. #endif // HAVE_DECL_LE32TOH
  138. #if HAVE_DECL_HTOBE64 == 0
  139. inline uint64_t htobe64(uint64_t host_64bits)
  140. {
  141. return bswap_64(host_64bits);
  142. }
  143. #endif // HAVE_DECL_HTOBE64
  144. #if HAVE_DECL_HTOLE64 == 0
  145. inline uint64_t htole64(uint64_t host_64bits)
  146. {
  147. return host_64bits;
  148. }
  149. #endif // HAVE_DECL_HTOLE64
  150. #if HAVE_DECL_BE64TOH == 0
  151. inline uint64_t be64toh(uint64_t big_endian_64bits)
  152. {
  153. return bswap_64(big_endian_64bits);
  154. }
  155. #endif // HAVE_DECL_BE64TOH
  156. #if HAVE_DECL_LE64TOH == 0
  157. inline uint64_t le64toh(uint64_t little_endian_64bits)
  158. {
  159. return little_endian_64bits;
  160. }
  161. #endif // HAVE_DECL_LE64TOH
  162. #endif // WORDS_BIGENDIAN
  163. #endif // STARWELS_COMPAT_ENDIAN_H