Skip to content

Commit 6abc9e2

Browse files
committed
Tidy the Mac OS Endian behaviour
1 parent 1ffb2cc commit 6abc9e2

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

common/network/NetworkUtils.cpp

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,10 @@ typedef uint32_t in_addr_t;
6060

6161
#ifdef HAVE_ENDIAN_H
6262
#include <endian.h>
63-
#elif defined(__APPLE__)
64-
#include <libkern/OSByteOrder.h>
65-
#define be64toh(x) OSSwapBigToHostInt64(x)
66-
#define htobe64(x) OSSwapHostToBigInt64(x)
6763
#endif // HAVE_ENDIAN_H
64+
#ifdef HAVE_LIBKERN_OSBYTEORDER_H
65+
#include <libkern/OSByteOrder.h>
66+
#endif // HAVE_LIBKERN_OSBYTEORDER_H
6867
#include <errno.h>
6968
#include <limits.h>
7069
#include <stdlib.h>
@@ -164,11 +163,13 @@ uint32_t NetworkToHost(uint32_t value) {
164163
}
165164

166165
uint64_t NetworkToHost(uint64_t value) {
167-
#if defined(HAVE_ENDIAN_H) || defined(__APPLE__)
166+
#ifdef HAVE_ENDIAN_H
168167
return be64toh(value);
168+
#elif defined(HAVE_LIBKERN_OSBYTEORDER_H)
169+
return OSSwapBigToHostInt64(value);
169170
#else
170-
#error "No be64toh for NetworkToHost, please report this."
171-
#endif // defined(HAVE_ENDIAN_H) || defined(__APPLE__)
171+
#error "No big endian 64 bit to host for NetworkToHost, please report this."
172+
#endif // HAVE_ENDIAN_H
172173
}
173174

174175
int16_t NetworkToHost(int16_t value) {
@@ -180,11 +181,13 @@ int32_t NetworkToHost(int32_t value) {
180181
}
181182

182183
int64_t NetworkToHost(int64_t value) {
183-
#if defined(HAVE_ENDIAN_H) || defined(__APPLE__)
184+
#ifdef HAVE_ENDIAN_H
184185
return be64toh(value);
186+
#elif defined(HAVE_LIBKERN_OSBYTEORDER_H)
187+
return OSSwapBigToHostInt64(value);
185188
#else
186-
#error "No be64toh for NetworkToHost, please report this."
187-
#endif // defined(HAVE_ENDIAN_H) || defined(__APPLE__)
189+
#error "No big endian 64 bit to host for NetworkToHost, please report this."
190+
#endif // HAVE_ENDIAN_H
188191
}
189192

190193
uint16_t HostToNetwork(uint16_t value) {
@@ -204,19 +207,23 @@ int32_t HostToNetwork(int32_t value) {
204207
}
205208

206209
uint64_t HostToNetwork(uint64_t value) {
207-
#if defined(HAVE_ENDIAN_H) || defined(__APPLE__)
210+
#ifdef HAVE_ENDIAN_H
208211
return htobe64(value);
212+
#elif defined(HAVE_LIBKERN_OSBYTEORDER_H)
213+
return OSSwapHostToBigInt64(value);
209214
#else
210-
#error "No htobe64 for HostToNetwork, please report this."
211-
#endif // defined(HAVE_ENDIAN_H) || defined(__APPLE__)
215+
#error "No host to big endian 64 bit for HostToNetwork, please report this."
216+
#endif // HAVE_ENDIAN_H
212217
}
213218

214219
int64_t HostToNetwork(int64_t value) {
215-
#if defined(HAVE_ENDIAN_H) || defined(__APPLE__)
220+
#ifdef HAVE_ENDIAN_H
216221
return htobe64(value);
222+
#elif defined(HAVE_LIBKERN_OSBYTEORDER_H)
223+
return OSSwapHostToBigInt64(value);
217224
#else
218-
#error "No htobe64 for HostToNetwork, please report this."
219-
#endif // defined(HAVE_ENDIAN_H) || defined(__APPLE__)
225+
#error "No host to big endian 64 bit for HostToNetwork, please report this."
226+
#endif // HAVE_ENDIAN_H
220227
}
221228

222229
uint16_t HostToLittleEndian(uint16_t value) {

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ AC_CHECK_HEADERS([asm/termbits.h asm/termios.h assert.h dlfcn.h endian.h \
149149
execinfo.h linux/if_packet.h math.h net/ethernet.h \
150150
stropts.h sys/ioctl.h sys/param.h sys/types.h sys/uio.h \
151151
sysexits.h])
152+
AC_CHECK_HEADERS([libkern/OSByteOrder.h])
152153
AC_CHECK_HEADERS([winsock2.h winerror.h])
153154
AC_CHECK_HEADERS([random])
154155

0 commit comments

Comments
 (0)