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.

endian.h 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // Copyright (c) 2014-2017 The Bitcoin Core 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 BITCOIN_COMPAT_ENDIAN_H
  5. #define BITCOIN_COMPAT_ENDIAN_H
  6. #if defined(HAVE_CONFIG_H)
  7. #include <config/bitcoin-config.h>
  8. #endif
  9. #include <compat/byteswap.h>
  10. #include <stdint.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. #ifndef HAVE_CONFIG_H
  17. // While not technically a supported configuration, defaulting to defining these
  18. // DECLs when we were compiled without autotools makes it easier for other build
  19. // systems to build things like libbitcoinconsensus for strange targets.
  20. #ifdef htobe16
  21. #define HAVE_DECL_HTOBE16 1
  22. #endif
  23. #ifdef htole16
  24. #define HAVE_DECL_HTOLE16 1
  25. #endif
  26. #ifdef be16toh
  27. #define HAVE_DECL_BE16TOH 1
  28. #endif
  29. #ifdef le16toh
  30. #define HAVE_DECL_LE16TOH 1
  31. #endif
  32. #ifdef htobe32
  33. #define HAVE_DECL_HTOBE32 1
  34. #endif
  35. #ifdef htole32
  36. #define HAVE_DECL_HTOLE32 1
  37. #endif
  38. #ifdef be32toh
  39. #define HAVE_DECL_BE32TOH 1
  40. #endif
  41. #ifdef le32toh
  42. #define HAVE_DECL_LE32TOH 1
  43. #endif
  44. #ifdef htobe64
  45. #define HAVE_DECL_HTOBE64 1
  46. #endif
  47. #ifdef htole64
  48. #define HAVE_DECL_HTOLE64 1
  49. #endif
  50. #ifdef be64toh
  51. #define HAVE_DECL_BE64TOH 1
  52. #endif
  53. #ifdef le64toh
  54. #define HAVE_DECL_LE64TOH 1
  55. #endif
  56. #endif // HAVE_CONFIG_H
  57. #if defined(WORDS_BIGENDIAN)
  58. #if HAVE_DECL_HTOBE16 == 0
  59. inline uint16_t htobe16(uint16_t host_16bits)
  60. {
  61. return host_16bits;
  62. }
  63. #endif // HAVE_DECL_HTOBE16
  64. #if HAVE_DECL_HTOLE16 == 0
  65. inline uint16_t htole16(uint16_t host_16bits)
  66. {
  67. return bswap_16(host_16bits);
  68. }
  69. #endif // HAVE_DECL_HTOLE16
  70. #if HAVE_DECL_BE16TOH == 0
  71. inline uint16_t be16toh(uint16_t big_endian_16bits)
  72. {
  73. return big_endian_16bits;
  74. }
  75. #endif // HAVE_DECL_BE16TOH
  76. #if HAVE_DECL_LE16TOH == 0
  77. inline uint16_t le16toh(uint16_t little_endian_16bits)
  78. {
  79. return bswap_16(little_endian_16bits);
  80. }
  81. #endif // HAVE_DECL_LE16TOH
  82. #if HAVE_DECL_HTOBE32 == 0
  83. inline uint32_t htobe32(uint32_t host_32bits)
  84. {
  85. return host_32bits;
  86. }
  87. #endif // HAVE_DECL_HTOBE32
  88. #if HAVE_DECL_HTOLE32 == 0
  89. inline uint32_t htole32(uint32_t host_32bits)
  90. {
  91. return bswap_32(host_32bits);
  92. }
  93. #endif // HAVE_DECL_HTOLE32
  94. #if HAVE_DECL_BE32TOH == 0
  95. inline uint32_t be32toh(uint32_t big_endian_32bits)
  96. {
  97. return big_endian_32bits;
  98. }
  99. #endif // HAVE_DECL_BE32TOH
  100. #if HAVE_DECL_LE32TOH == 0
  101. inline uint32_t le32toh(uint32_t little_endian_32bits)
  102. {
  103. return bswap_32(little_endian_32bits);
  104. }
  105. #endif // HAVE_DECL_LE32TOH
  106. #if HAVE_DECL_HTOBE64 == 0
  107. inline uint64_t htobe64(uint64_t host_64bits)
  108. {
  109. return host_64bits;
  110. }
  111. #endif // HAVE_DECL_HTOBE64
  112. #if HAVE_DECL_HTOLE64 == 0
  113. inline uint64_t htole64(uint64_t host_64bits)
  114. {
  115. return bswap_64(host_64bits);
  116. }
  117. #endif // HAVE_DECL_HTOLE64
  118. #if HAVE_DECL_BE64TOH == 0
  119. inline uint64_t be64toh(uint64_t big_endian_64bits)
  120. {
  121. return big_endian_64bits;
  122. }
  123. #endif // HAVE_DECL_BE64TOH
  124. #if HAVE_DECL_LE64TOH == 0
  125. inline uint64_t le64toh(uint64_t little_endian_64bits)
  126. {
  127. return bswap_64(little_endian_64bits);
  128. }
  129. #endif // HAVE_DECL_LE64TOH
  130. #else // WORDS_BIGENDIAN
  131. #if HAVE_DECL_HTOBE16 == 0
  132. inline uint16_t htobe16(uint16_t host_16bits)
  133. {
  134. return bswap_16(host_16bits);
  135. }
  136. #endif // HAVE_DECL_HTOBE16
  137. #if HAVE_DECL_HTOLE16 == 0
  138. inline uint16_t htole16(uint16_t host_16bits)
  139. {
  140. return host_16bits;
  141. }
  142. #endif // HAVE_DECL_HTOLE16
  143. #if HAVE_DECL_BE16TOH == 0
  144. inline uint16_t be16toh(uint16_t big_endian_16bits)
  145. {
  146. return bswap_16(big_endian_16bits);
  147. }
  148. #endif // HAVE_DECL_BE16TOH
  149. #if HAVE_DECL_LE16TOH == 0
  150. inline uint16_t le16toh(uint16_t little_endian_16bits)
  151. {
  152. return little_endian_16bits;
  153. }
  154. #endif // HAVE_DECL_LE16TOH
  155. #if HAVE_DECL_HTOBE32 == 0
  156. inline uint32_t htobe32(uint32_t host_32bits)
  157. {
  158. return bswap_32(host_32bits);
  159. }
  160. #endif // HAVE_DECL_HTOBE32
  161. #if HAVE_DECL_HTOLE32 == 0
  162. inline uint32_t htole32(uint32_t host_32bits)
  163. {
  164. return host_32bits;
  165. }
  166. #endif // HAVE_DECL_HTOLE32
  167. #if HAVE_DECL_BE32TOH == 0
  168. inline uint32_t be32toh(uint32_t big_endian_32bits)
  169. {
  170. return bswap_32(big_endian_32bits);
  171. }
  172. #endif // HAVE_DECL_BE32TOH
  173. #if HAVE_DECL_LE32TOH == 0
  174. inline uint32_t le32toh(uint32_t little_endian_32bits)
  175. {
  176. return little_endian_32bits;
  177. }
  178. #endif // HAVE_DECL_LE32TOH
  179. #if HAVE_DECL_HTOBE64 == 0
  180. inline uint64_t htobe64(uint64_t host_64bits)
  181. {
  182. return bswap_64(host_64bits);
  183. }
  184. #endif // HAVE_DECL_HTOBE64
  185. #if HAVE_DECL_HTOLE64 == 0
  186. inline uint64_t htole64(uint64_t host_64bits)
  187. {
  188. return host_64bits;
  189. }
  190. #endif // HAVE_DECL_HTOLE64
  191. #if HAVE_DECL_BE64TOH == 0
  192. inline uint64_t be64toh(uint64_t big_endian_64bits)
  193. {
  194. return bswap_64(big_endian_64bits);
  195. }
  196. #endif // HAVE_DECL_BE64TOH
  197. #if HAVE_DECL_LE64TOH == 0
  198. inline uint64_t le64toh(uint64_t little_endian_64bits)
  199. {
  200. return little_endian_64bits;
  201. }
  202. #endif // HAVE_DECL_LE64TOH
  203. #endif // WORDS_BIGENDIAN
  204. #endif // BITCOIN_COMPAT_ENDIAN_H