Skip to content

Commit 4414f5f

Browse files
committed
build: Endian compatibility
- Detect endian instead of stopping configure on big-endian - Add `byteswap.h` and `endian.h` header for compatibility with Windows and other operating systems that don't come with them - Update `crypto/common.h` functions to use compat endian header
1 parent 51377c2 commit 4414f5f

File tree

5 files changed

+263
-69
lines changed

5 files changed

+263
-69
lines changed

configure.ac

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,8 @@ if test x$use_lcov = xyes; then
350350
[AC_MSG_ERROR("lcov testing requested but --coverage flag does not work")])
351351
fi
352352

353-
dnl Require little endian
354-
AC_C_BIGENDIAN([AC_MSG_ERROR("Big Endian not supported")])
353+
dnl Check for endianness
354+
AC_C_BIGENDIAN
355355

356356
dnl Check for pthread compile/link requirements
357357
AX_PTHREAD
@@ -438,17 +438,22 @@ if test x$TARGET_OS = xdarwin; then
438438
AX_CHECK_LINK_FLAG([[-Wl,-dead_strip]], [LDFLAGS="$LDFLAGS -Wl,-dead_strip"])
439439
fi
440440

441-
AC_CHECK_HEADERS([endian.h stdio.h stdlib.h unistd.h strings.h sys/types.h sys/stat.h sys/select.h sys/prctl.h])
441+
AC_CHECK_HEADERS([endian.h byteswap.h stdio.h stdlib.h unistd.h strings.h sys/types.h sys/stat.h sys/select.h sys/prctl.h])
442442
AC_SEARCH_LIBS([getaddrinfo_a], [anl], [AC_DEFINE(HAVE_GETADDRINFO_A, 1, [Define this symbol if you have getaddrinfo_a])])
443443
AC_SEARCH_LIBS([inet_pton], [nsl resolv], [AC_DEFINE(HAVE_INET_PTON, 1, [Define this symbol if you have inet_pton])])
444444

445445
AC_CHECK_DECLS([strnlen])
446446

447-
AC_CHECK_DECLS([le32toh, le64toh, htole32, htole64, be32toh, be64toh, htobe32, htobe64],,,
447+
AC_CHECK_DECLS([le16toh, le32toh, le64toh, htole16, htole32, htole64, be16toh, be32toh, be64toh, htobe16, htobe32, htobe64],,,
448448
[#if HAVE_ENDIAN_H
449449
#include <endian.h>
450450
#endif])
451451

452+
AC_CHECK_DECLS([bswap_16, bswap_32, bswap_64],,,
453+
[#if HAVE_BYTESWAP_H
454+
#include <byteswap.h>
455+
#endif])
456+
452457
dnl Check for MSG_NOSIGNAL
453458
AC_MSG_CHECKING(for MSG_NOSIGNAL)
454459
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <sys/socket.h>]],

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ BITCOIN_CORE_H = \
141141
walletdb.h \
142142
wallet.h \
143143
wallet_ismine.h \
144+
compat/byteswap.h \
145+
compat/endian.h \
144146
compat/sanity.h
145147

146148
JSON_H = \

src/compat/byteswap.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) 2014 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_BYTESWAP_H
6+
#define BITCOIN_COMPAT_BYTESWAP_H
7+
8+
#if defined(HAVE_CONFIG_H)
9+
#include "config/bitcoin-config.h"
10+
#endif
11+
12+
#include <stdint.h>
13+
14+
#if defined(HAVE_BYTESWAP_H)
15+
#include <byteswap.h>
16+
#endif
17+
18+
#if HAVE_DECL_BSWAP_16 == 0
19+
inline uint16_t bswap_16(uint16_t x)
20+
{
21+
return (x >> 8) | ((x & 0x00ff) << 8);
22+
}
23+
#endif // HAVE_DECL_BSWAP16
24+
25+
#if HAVE_DECL_BSWAP_32 == 0
26+
inline uint32_t bswap_32(uint32_t x)
27+
{
28+
return (((x & 0xff000000U) >> 24) | ((x & 0x00ff0000U) >> 8) |
29+
((x & 0x0000ff00U) << 8) | ((x & 0x000000ffU) << 24));
30+
}
31+
#endif // HAVE_DECL_BSWAP32
32+
33+
#if HAVE_DECL_BSWAP_64 == 0
34+
inline uint64_t bswap_64(uint64_t x)
35+
{
36+
return (((x & 0xff00000000000000ull) >> 56)
37+
| ((x & 0x00ff000000000000ull) >> 40)
38+
| ((x & 0x0000ff0000000000ull) >> 24)
39+
| ((x & 0x000000ff00000000ull) >> 8)
40+
| ((x & 0x00000000ff000000ull) << 8)
41+
| ((x & 0x0000000000ff0000ull) << 24)
42+
| ((x & 0x000000000000ff00ull) << 40)
43+
| ((x & 0x00000000000000ffull) << 56));
44+
}
45+
#endif // HAVE_DECL_BSWAP64
46+
47+
#endif // BITCOIN_COMPAT_BYTESWAP_H

src/compat/endian.h

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
// Copyright (c) 2014 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+
#endif
19+
20+
#if defined(WORDS_BIGENDIAN)
21+
22+
#if HAVE_DECL_HTOBE16 == 0
23+
inline uint16_t htobe16(uint16_t host_16bits)
24+
{
25+
return host_16bits;
26+
}
27+
#endif // HAVE_DECL_HTOBE16
28+
29+
#if HAVE_DECL_HTOLE16 == 0
30+
inline uint16_t htole16(uint16_t host_16bits)
31+
{
32+
return bswap_16(host_16bits);
33+
}
34+
#endif // HAVE_DECL_HTOLE16
35+
36+
#if HAVE_DECL_BE16TOH == 0
37+
inline uint16_t be16toh(uint16_t big_endian_16bits)
38+
{
39+
return big_endian_16bits;
40+
}
41+
#endif // HAVE_DECL_BE16TOH
42+
43+
#if HAVE_DECL_LE16TOH == 0
44+
inline uint16_t le16toh(uint16_t little_endian_16bits)
45+
{
46+
return bswap_16(little_endian_16bits);
47+
}
48+
#endif // HAVE_DECL_LE16TOH
49+
50+
#if HAVE_DECL_HTOBE32 == 0
51+
inline uint32_t htobe32(uint32_t host_32bits)
52+
{
53+
return host_32bits;
54+
}
55+
#endif // HAVE_DECL_HTOBE32
56+
57+
#if HAVE_DECL_HTOLE32 == 0
58+
inline uint32_t htole32(uint32_t host_32bits)
59+
{
60+
return bswap_32(host_32bits);
61+
}
62+
#endif // HAVE_DECL_HTOLE32
63+
64+
#if HAVE_DECL_BE32TOH == 0
65+
inline uint32_t be32toh(uint32_t big_endian_32bits)
66+
{
67+
return big_endian_32bits;
68+
}
69+
#endif // HAVE_DECL_BE32TOH
70+
71+
#if HAVE_DECL_LE32TOH == 0
72+
inline uint32_t le32toh(uint32_t little_endian_32bits)
73+
{
74+
return bswap_32(little_endian_32bits);
75+
}
76+
#endif // HAVE_DECL_LE32TOH
77+
78+
#if HAVE_DECL_HTOBE64 == 0
79+
inline uint64_t htobe64(uint64_t host_64bits)
80+
{
81+
return host_64bits;
82+
}
83+
#endif // HAVE_DECL_HTOBE64
84+
85+
#if HAVE_DECL_HTOLE64 == 0
86+
inline uint64_t htole64(uint64_t host_64bits)
87+
{
88+
return bswap_64(host_64bits);
89+
}
90+
#endif // HAVE_DECL_HTOLE64
91+
92+
#if HAVE_DECL_BE64TOH == 0
93+
inline uint64_t be64toh(uint64_t big_endian_64bits)
94+
{
95+
return big_endian_64bits;
96+
}
97+
#endif // HAVE_DECL_BE64TOH
98+
99+
#if HAVE_DECL_LE64TOH == 0
100+
inline uint64_t le64toh(uint64_t little_endian_64bits)
101+
{
102+
return bswap_64(little_endian_64bits);
103+
}
104+
#endif // HAVE_DECL_LE64TOH
105+
106+
#else // WORDS_BIGENDIAN
107+
108+
#if HAVE_DECL_HTOBE16 == 0
109+
inline uint16_t htobe16(uint16_t host_16bits)
110+
{
111+
return bswap_16(host_16bits);
112+
}
113+
#endif // HAVE_DECL_HTOBE16
114+
115+
#if HAVE_DECL_HTOLE16 == 0
116+
inline uint16_t htole16(uint16_t host_16bits)
117+
{
118+
return host_16bits;
119+
}
120+
#endif // HAVE_DECL_HTOLE16
121+
122+
#if HAVE_DECL_BE16TOH == 0
123+
inline uint16_t be16toh(uint16_t big_endian_16bits)
124+
{
125+
return bswap_16(big_endian_16bits);
126+
}
127+
#endif // HAVE_DECL_BE16TOH
128+
129+
#if HAVE_DECL_LE16TOH == 0
130+
inline uint16_t le16toh(uint16_t little_endian_16bits)
131+
{
132+
return little_endian_16bits;
133+
}
134+
#endif // HAVE_DECL_LE16TOH
135+
136+
#if HAVE_DECL_HTOBE32 == 0
137+
inline uint32_t htobe32(uint32_t host_32bits)
138+
{
139+
return bswap_32(host_32bits);
140+
}
141+
#endif // HAVE_DECL_HTOBE32
142+
143+
#if HAVE_DECL_HTOLE32 == 0
144+
inline uint32_t htole32(uint32_t host_32bits)
145+
{
146+
return host_32bits;
147+
}
148+
#endif // HAVE_DECL_HTOLE32
149+
150+
#if HAVE_DECL_BE32TOH == 0
151+
inline uint32_t be32toh(uint32_t big_endian_32bits)
152+
{
153+
return bswap_32(big_endian_32bits);
154+
}
155+
#endif // HAVE_DECL_BE32TOH
156+
157+
#if HAVE_DECL_LE32TOH == 0
158+
inline uint32_t le32toh(uint32_t little_endian_32bits)
159+
{
160+
return little_endian_32bits;
161+
}
162+
#endif // HAVE_DECL_LE32TOH
163+
164+
#if HAVE_DECL_HTOBE64 == 0
165+
inline uint64_t htobe64(uint64_t host_64bits)
166+
{
167+
return bswap_64(host_64bits);
168+
}
169+
#endif // HAVE_DECL_HTOBE64
170+
171+
#if HAVE_DECL_HTOLE64 == 0
172+
inline uint64_t htole64(uint64_t host_64bits)
173+
{
174+
return host_64bits;
175+
}
176+
#endif // HAVE_DECL_HTOLE64
177+
178+
#if HAVE_DECL_BE64TOH == 0
179+
inline uint64_t be64toh(uint64_t big_endian_64bits)
180+
{
181+
return bswap_64(big_endian_64bits);
182+
}
183+
#endif // HAVE_DECL_BE64TOH
184+
185+
#if HAVE_DECL_LE64TOH == 0
186+
inline uint64_t le64toh(uint64_t little_endian_64bits)
187+
{
188+
return little_endian_64bits;
189+
}
190+
#endif // HAVE_DECL_LE64TOH
191+
192+
#endif // WORDS_BIGENDIAN
193+
194+
#endif // BITCOIN_COMPAT_ENDIAN_H

0 commit comments

Comments
 (0)