Skip to content

Commit 86b7f28

Browse files
committed
serialization: use internal endian conversion functions
These replace our platform-specific mess in favor of c++20 endian detection via std::endian and internal byteswap functions when necessary. They no longer rely on autoconf detection.
1 parent 432b18c commit 86b7f28

File tree

5 files changed

+64
-238
lines changed

5 files changed

+64
-238
lines changed

configure.ac

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ if test "$TARGET_OS" = "darwin"; then
975975
AX_CHECK_LINK_FLAG([-Wl,-fixup_chains], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-fixup_chains"], [], [$LDFLAG_WERROR])
976976
fi
977977

978-
AC_CHECK_HEADERS([endian.h sys/endian.h sys/select.h sys/prctl.h sys/sysctl.h vm/vm_param.h sys/vmmeter.h sys/resources.h])
978+
AC_CHECK_HEADERS([sys/select.h sys/prctl.h sys/sysctl.h vm/vm_param.h sys/vmmeter.h sys/resources.h])
979979

980980
AC_CHECK_DECLS([getifaddrs, freeifaddrs],[CHECK_SOCKET],,
981981
[#include <sys/types.h>
@@ -990,13 +990,6 @@ AC_CHECK_DECLS([pipe2])
990990

991991
AC_CHECK_FUNCS([timingsafe_bcmp])
992992

993-
AC_CHECK_DECLS([le16toh, le32toh, le64toh, htole16, htole32, htole64, be16toh, be32toh, be64toh, htobe16, htobe32, htobe64],,,
994-
[#if HAVE_ENDIAN_H
995-
#include <endian.h>
996-
#elif HAVE_SYS_ENDIAN_H
997-
#include <sys/endian.h>
998-
#endif])
999-
1000993
AC_MSG_CHECKING([for __builtin_clzl])
1001994
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ ]], [[
1002995
(void) __builtin_clzl(0);

src/compat/endian.h

Lines changed: 37 additions & 204 deletions
Original file line numberDiff line numberDiff line change
@@ -5,237 +5,70 @@
55
#ifndef BITCOIN_COMPAT_ENDIAN_H
66
#define BITCOIN_COMPAT_ENDIAN_H
77

8-
#if defined(HAVE_CONFIG_H)
9-
#include <config/bitcoin-config.h>
10-
#endif
11-
128
#include <compat/byteswap.h>
139

10+
#include <bit>
1411
#include <cstdint>
1512

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
70-
inline uint16_t htobe16(uint16_t host_16bits)
71-
{
72-
return host_16bits;
73-
}
74-
#endif // HAVE_DECL_HTOBE16
75-
76-
#if HAVE_DECL_HTOLE16 == 0
77-
inline uint16_t htole16(uint16_t host_16bits)
78-
{
79-
return internal_bswap_16(host_16bits);
80-
}
81-
#endif // HAVE_DECL_HTOLE16
82-
83-
#if HAVE_DECL_BE16TOH == 0
84-
inline uint16_t be16toh(uint16_t big_endian_16bits)
85-
{
86-
return big_endian_16bits;
87-
}
88-
#endif // HAVE_DECL_BE16TOH
89-
90-
#if HAVE_DECL_LE16TOH == 0
91-
inline uint16_t le16toh(uint16_t little_endian_16bits)
92-
{
93-
return internal_bswap_16(little_endian_16bits);
94-
}
95-
#endif // HAVE_DECL_LE16TOH
96-
97-
#if HAVE_DECL_HTOBE32 == 0
98-
inline uint32_t htobe32(uint32_t host_32bits)
99-
{
100-
return host_32bits;
101-
}
102-
#endif // HAVE_DECL_HTOBE32
103-
104-
#if HAVE_DECL_HTOLE32 == 0
105-
inline uint32_t htole32(uint32_t host_32bits)
106-
{
107-
return internal_bswap_32(host_32bits);
108-
}
109-
#endif // HAVE_DECL_HTOLE32
110-
111-
#if HAVE_DECL_BE32TOH == 0
112-
inline uint32_t be32toh(uint32_t big_endian_32bits)
113-
{
114-
return big_endian_32bits;
115-
}
116-
#endif // HAVE_DECL_BE32TOH
117-
118-
#if HAVE_DECL_LE32TOH == 0
119-
inline uint32_t le32toh(uint32_t little_endian_32bits)
13+
inline BSWAP_CONSTEXPR uint16_t htobe16_internal(uint16_t host_16bits)
12014
{
121-
return internal_bswap_32(little_endian_32bits);
15+
if constexpr (std::endian::native == std::endian::little) return internal_bswap_16(host_16bits);
16+
else return host_16bits;
12217
}
123-
#endif // HAVE_DECL_LE32TOH
124-
125-
#if HAVE_DECL_HTOBE64 == 0
126-
inline uint64_t htobe64(uint64_t host_64bits)
127-
{
128-
return host_64bits;
129-
}
130-
#endif // HAVE_DECL_HTOBE64
131-
132-
#if HAVE_DECL_HTOLE64 == 0
133-
inline uint64_t htole64(uint64_t host_64bits)
134-
{
135-
return internal_bswap_64(host_64bits);
136-
}
137-
#endif // HAVE_DECL_HTOLE64
138-
139-
#if HAVE_DECL_BE64TOH == 0
140-
inline uint64_t be64toh(uint64_t big_endian_64bits)
141-
{
142-
return big_endian_64bits;
143-
}
144-
#endif // HAVE_DECL_BE64TOH
145-
146-
#if HAVE_DECL_LE64TOH == 0
147-
inline uint64_t le64toh(uint64_t little_endian_64bits)
148-
{
149-
return internal_bswap_64(little_endian_64bits);
150-
}
151-
#endif // HAVE_DECL_LE64TOH
152-
153-
#else // WORDS_BIGENDIAN
154-
155-
#if HAVE_DECL_HTOBE16 == 0
156-
inline uint16_t htobe16(uint16_t host_16bits)
18+
inline BSWAP_CONSTEXPR uint16_t htole16_internal(uint16_t host_16bits)
15719
{
158-
return internal_bswap_16(host_16bits);
20+
if constexpr (std::endian::native == std::endian::big) return internal_bswap_16(host_16bits);
21+
else return host_16bits;
15922
}
160-
#endif // HAVE_DECL_HTOBE16
161-
162-
#if HAVE_DECL_HTOLE16 == 0
163-
inline uint16_t htole16(uint16_t host_16bits)
23+
inline BSWAP_CONSTEXPR uint16_t be16toh_internal(uint16_t big_endian_16bits)
16424
{
165-
return host_16bits;
25+
if constexpr (std::endian::native == std::endian::little) return internal_bswap_16(big_endian_16bits);
26+
else return big_endian_16bits;
16627
}
167-
#endif // HAVE_DECL_HTOLE16
168-
169-
#if HAVE_DECL_BE16TOH == 0
170-
inline uint16_t be16toh(uint16_t big_endian_16bits)
28+
inline BSWAP_CONSTEXPR uint16_t le16toh_internal(uint16_t little_endian_16bits)
17129
{
172-
return internal_bswap_16(big_endian_16bits);
30+
if constexpr (std::endian::native == std::endian::big) return internal_bswap_16(little_endian_16bits);
31+
else return little_endian_16bits;
17332
}
174-
#endif // HAVE_DECL_BE16TOH
175-
176-
#if HAVE_DECL_LE16TOH == 0
177-
inline uint16_t le16toh(uint16_t little_endian_16bits)
33+
inline BSWAP_CONSTEXPR uint32_t htobe32_internal(uint32_t host_32bits)
17834
{
179-
return little_endian_16bits;
35+
if constexpr (std::endian::native == std::endian::little) return internal_bswap_32(host_32bits);
36+
else return host_32bits;
18037
}
181-
#endif // HAVE_DECL_LE16TOH
182-
183-
#if HAVE_DECL_HTOBE32 == 0
184-
inline uint32_t htobe32(uint32_t host_32bits)
185-
{
186-
return internal_bswap_32(host_32bits);
187-
}
188-
#endif // HAVE_DECL_HTOBE32
189-
190-
#if HAVE_DECL_HTOLE32 == 0
191-
inline uint32_t htole32(uint32_t host_32bits)
38+
inline BSWAP_CONSTEXPR uint32_t htole32_internal(uint32_t host_32bits)
19239
{
193-
return host_32bits;
40+
if constexpr (std::endian::native == std::endian::big) return internal_bswap_32(host_32bits);
41+
else return host_32bits;
19442
}
195-
#endif // HAVE_DECL_HTOLE32
196-
197-
#if HAVE_DECL_BE32TOH == 0
198-
inline uint32_t be32toh(uint32_t big_endian_32bits)
43+
inline BSWAP_CONSTEXPR uint32_t be32toh_internal(uint32_t big_endian_32bits)
19944
{
200-
return internal_bswap_32(big_endian_32bits);
45+
if constexpr (std::endian::native == std::endian::little) return internal_bswap_32(big_endian_32bits);
46+
else return big_endian_32bits;
20147
}
202-
#endif // HAVE_DECL_BE32TOH
203-
204-
#if HAVE_DECL_LE32TOH == 0
205-
inline uint32_t le32toh(uint32_t little_endian_32bits)
48+
inline BSWAP_CONSTEXPR uint32_t le32toh_internal(uint32_t little_endian_32bits)
20649
{
207-
return little_endian_32bits;
50+
if constexpr (std::endian::native == std::endian::big) return internal_bswap_32(little_endian_32bits);
51+
else return little_endian_32bits;
20852
}
209-
#endif // HAVE_DECL_LE32TOH
210-
211-
#if HAVE_DECL_HTOBE64 == 0
212-
inline uint64_t htobe64(uint64_t host_64bits)
53+
inline BSWAP_CONSTEXPR uint64_t htobe64_internal(uint64_t host_64bits)
21354
{
214-
return internal_bswap_64(host_64bits);
55+
if constexpr (std::endian::native == std::endian::little) return internal_bswap_64(host_64bits);
56+
else return host_64bits;
21557
}
216-
#endif // HAVE_DECL_HTOBE64
217-
218-
#if HAVE_DECL_HTOLE64 == 0
219-
inline uint64_t htole64(uint64_t host_64bits)
58+
inline BSWAP_CONSTEXPR uint64_t htole64_internal(uint64_t host_64bits)
22059
{
221-
return host_64bits;
60+
if constexpr (std::endian::native == std::endian::big) return internal_bswap_64(host_64bits);
61+
else return host_64bits;
22262
}
223-
#endif // HAVE_DECL_HTOLE64
224-
225-
#if HAVE_DECL_BE64TOH == 0
226-
inline uint64_t be64toh(uint64_t big_endian_64bits)
63+
inline BSWAP_CONSTEXPR uint64_t be64toh_internal(uint64_t big_endian_64bits)
22764
{
228-
return internal_bswap_64(big_endian_64bits);
65+
if constexpr (std::endian::native == std::endian::little) return internal_bswap_64(big_endian_64bits);
66+
else return big_endian_64bits;
22967
}
230-
#endif // HAVE_DECL_BE64TOH
231-
232-
#if HAVE_DECL_LE64TOH == 0
233-
inline uint64_t le64toh(uint64_t little_endian_64bits)
68+
inline BSWAP_CONSTEXPR uint64_t le64toh_internal(uint64_t little_endian_64bits)
23469
{
235-
return little_endian_64bits;
70+
if constexpr (std::endian::native == std::endian::big) return internal_bswap_64(little_endian_64bits);
71+
else return little_endian_64bits;
23672
}
237-
#endif // HAVE_DECL_LE64TOH
238-
239-
#endif // WORDS_BIGENDIAN
24073

24174
#endif // BITCOIN_COMPAT_ENDIAN_H

src/crypto/common.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,71 +14,71 @@ uint16_t static inline ReadLE16(const unsigned char* ptr)
1414
{
1515
uint16_t x;
1616
memcpy(&x, ptr, 2);
17-
return le16toh(x);
17+
return le16toh_internal(x);
1818
}
1919

2020
uint32_t static inline ReadLE32(const unsigned char* ptr)
2121
{
2222
uint32_t x;
2323
memcpy(&x, ptr, 4);
24-
return le32toh(x);
24+
return le32toh_internal(x);
2525
}
2626

2727
uint64_t static inline ReadLE64(const unsigned char* ptr)
2828
{
2929
uint64_t x;
3030
memcpy(&x, ptr, 8);
31-
return le64toh(x);
31+
return le64toh_internal(x);
3232
}
3333

3434
void static inline WriteLE16(unsigned char* ptr, uint16_t x)
3535
{
36-
uint16_t v = htole16(x);
36+
uint16_t v = htole16_internal(x);
3737
memcpy(ptr, &v, 2);
3838
}
3939

4040
void static inline WriteLE32(unsigned char* ptr, uint32_t x)
4141
{
42-
uint32_t v = htole32(x);
42+
uint32_t v = htole32_internal(x);
4343
memcpy(ptr, &v, 4);
4444
}
4545

4646
void static inline WriteLE64(unsigned char* ptr, uint64_t x)
4747
{
48-
uint64_t v = htole64(x);
48+
uint64_t v = htole64_internal(x);
4949
memcpy(ptr, &v, 8);
5050
}
5151

5252
uint16_t static inline ReadBE16(const unsigned char* ptr)
5353
{
5454
uint16_t x;
5555
memcpy(&x, ptr, 2);
56-
return be16toh(x);
56+
return be16toh_internal(x);
5757
}
5858

5959
uint32_t static inline ReadBE32(const unsigned char* ptr)
6060
{
6161
uint32_t x;
6262
memcpy(&x, ptr, 4);
63-
return be32toh(x);
63+
return be32toh_internal(x);
6464
}
6565

6666
uint64_t static inline ReadBE64(const unsigned char* ptr)
6767
{
6868
uint64_t x;
6969
memcpy(&x, ptr, 8);
70-
return be64toh(x);
70+
return be64toh_internal(x);
7171
}
7272

7373
void static inline WriteBE32(unsigned char* ptr, uint32_t x)
7474
{
75-
uint32_t v = htobe32(x);
75+
uint32_t v = htobe32_internal(x);
7676
memcpy(ptr, &v, 4);
7777
}
7878

7979
void static inline WriteBE64(unsigned char* ptr, uint64_t x)
8080
{
81-
uint64_t v = htobe64(x);
81+
uint64_t v = htobe64_internal(x);
8282
memcpy(ptr, &v, 8);
8383
}
8484

src/i2p.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ Binary Session::MyDestination() const
392392
}
393393

394394
memcpy(&cert_len, &m_private_key.at(CERT_LEN_POS), sizeof(cert_len));
395-
cert_len = be16toh(cert_len);
395+
cert_len = be16toh_internal(cert_len);
396396

397397
const size_t dest_len = DEST_LEN_BASE + cert_len;
398398

0 commit comments

Comments
 (0)