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