1 // Copyright (c) 2014-2016 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.
5 #ifndef BITCOIN_COMPAT_ENDIAN_H
6 #define BITCOIN_COMPAT_ENDIAN_H
8 #if defined(HAVE_CONFIG_H)
9 #include <config/bitcoin-config.h>
12 #include <compat/byteswap.h>
16 #if defined(HAVE_ENDIAN_H)
18 #elif defined(HAVE_SYS_ENDIAN_H)
19 #include <sys/endian.h>
22 #if defined(WORDS_BIGENDIAN)
24 #if HAVE_DECL_HTOBE16 == 0
25 inline uint16_t htobe16(uint16_t host_16bits
)
29 #endif // HAVE_DECL_HTOBE16
31 #if HAVE_DECL_HTOLE16 == 0
32 inline uint16_t htole16(uint16_t host_16bits
)
34 return bswap_16(host_16bits
);
36 #endif // HAVE_DECL_HTOLE16
38 #if HAVE_DECL_BE16TOH == 0
39 inline uint16_t be16toh(uint16_t big_endian_16bits
)
41 return big_endian_16bits
;
43 #endif // HAVE_DECL_BE16TOH
45 #if HAVE_DECL_LE16TOH == 0
46 inline uint16_t le16toh(uint16_t little_endian_16bits
)
48 return bswap_16(little_endian_16bits
);
50 #endif // HAVE_DECL_LE16TOH
52 #if HAVE_DECL_HTOBE32 == 0
53 inline uint32_t htobe32(uint32_t host_32bits
)
57 #endif // HAVE_DECL_HTOBE32
59 #if HAVE_DECL_HTOLE32 == 0
60 inline uint32_t htole32(uint32_t host_32bits
)
62 return bswap_32(host_32bits
);
64 #endif // HAVE_DECL_HTOLE32
66 #if HAVE_DECL_BE32TOH == 0
67 inline uint32_t be32toh(uint32_t big_endian_32bits
)
69 return big_endian_32bits
;
71 #endif // HAVE_DECL_BE32TOH
73 #if HAVE_DECL_LE32TOH == 0
74 inline uint32_t le32toh(uint32_t little_endian_32bits
)
76 return bswap_32(little_endian_32bits
);
78 #endif // HAVE_DECL_LE32TOH
80 #if HAVE_DECL_HTOBE64 == 0
81 inline uint64_t htobe64(uint64_t host_64bits
)
85 #endif // HAVE_DECL_HTOBE64
87 #if HAVE_DECL_HTOLE64 == 0
88 inline uint64_t htole64(uint64_t host_64bits
)
90 return bswap_64(host_64bits
);
92 #endif // HAVE_DECL_HTOLE64
94 #if HAVE_DECL_BE64TOH == 0
95 inline uint64_t be64toh(uint64_t big_endian_64bits
)
97 return big_endian_64bits
;
99 #endif // HAVE_DECL_BE64TOH
101 #if HAVE_DECL_LE64TOH == 0
102 inline uint64_t le64toh(uint64_t little_endian_64bits
)
104 return bswap_64(little_endian_64bits
);
106 #endif // HAVE_DECL_LE64TOH
108 #else // WORDS_BIGENDIAN
110 #if HAVE_DECL_HTOBE16 == 0
111 inline uint16_t htobe16(uint16_t host_16bits
)
113 return bswap_16(host_16bits
);
115 #endif // HAVE_DECL_HTOBE16
117 #if HAVE_DECL_HTOLE16 == 0
118 inline uint16_t htole16(uint16_t host_16bits
)
122 #endif // HAVE_DECL_HTOLE16
124 #if HAVE_DECL_BE16TOH == 0
125 inline uint16_t be16toh(uint16_t big_endian_16bits
)
127 return bswap_16(big_endian_16bits
);
129 #endif // HAVE_DECL_BE16TOH
131 #if HAVE_DECL_LE16TOH == 0
132 inline uint16_t le16toh(uint16_t little_endian_16bits
)
134 return little_endian_16bits
;
136 #endif // HAVE_DECL_LE16TOH
138 #if HAVE_DECL_HTOBE32 == 0
139 inline uint32_t htobe32(uint32_t host_32bits
)
141 return bswap_32(host_32bits
);
143 #endif // HAVE_DECL_HTOBE32
145 #if HAVE_DECL_HTOLE32 == 0
146 inline uint32_t htole32(uint32_t host_32bits
)
150 #endif // HAVE_DECL_HTOLE32
152 #if HAVE_DECL_BE32TOH == 0
153 inline uint32_t be32toh(uint32_t big_endian_32bits
)
155 return bswap_32(big_endian_32bits
);
157 #endif // HAVE_DECL_BE32TOH
159 #if HAVE_DECL_LE32TOH == 0
160 inline uint32_t le32toh(uint32_t little_endian_32bits
)
162 return little_endian_32bits
;
164 #endif // HAVE_DECL_LE32TOH
166 #if HAVE_DECL_HTOBE64 == 0
167 inline uint64_t htobe64(uint64_t host_64bits
)
169 return bswap_64(host_64bits
);
171 #endif // HAVE_DECL_HTOBE64
173 #if HAVE_DECL_HTOLE64 == 0
174 inline uint64_t htole64(uint64_t host_64bits
)
178 #endif // HAVE_DECL_HTOLE64
180 #if HAVE_DECL_BE64TOH == 0
181 inline uint64_t be64toh(uint64_t big_endian_64bits
)
183 return bswap_64(big_endian_64bits
);
185 #endif // HAVE_DECL_BE64TOH
187 #if HAVE_DECL_LE64TOH == 0
188 inline uint64_t le64toh(uint64_t little_endian_64bits
)
190 return little_endian_64bits
;
192 #endif // HAVE_DECL_LE64TOH
194 #endif // WORDS_BIGENDIAN
196 #endif // BITCOIN_COMPAT_ENDIAN_H