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