Skip to content

Commit 704337b

Browse files
authored
Merge pull request #2014 from peternewman/mac-be
Tidy the Mac OS Endian behaviour
2 parents eaf937e + 6abc9e2 commit 704337b

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

common/network/NetworkUtils.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ typedef uint32_t in_addr_t;
6161
#ifdef HAVE_ENDIAN_H
6262
#include <endian.h>
6363
#endif // HAVE_ENDIAN_H
64+
#ifdef HAVE_LIBKERN_OSBYTEORDER_H
65+
#include <libkern/OSByteOrder.h>
66+
#endif // HAVE_LIBKERN_OSBYTEORDER_H
6467
#include <errno.h>
6568
#include <limits.h>
6669
#include <stdlib.h>
@@ -162,8 +165,10 @@ uint32_t NetworkToHost(uint32_t value) {
162165
uint64_t NetworkToHost(uint64_t value) {
163166
#ifdef HAVE_ENDIAN_H
164167
return be64toh(value);
168+
#elif defined(HAVE_LIBKERN_OSBYTEORDER_H)
169+
return OSSwapBigToHostInt64(value);
165170
#else
166-
#error "No be64toh for NetworkToHost, please report this."
171+
#error "No big endian 64 bit to host for NetworkToHost, please report this."
167172
#endif // HAVE_ENDIAN_H
168173
}
169174

@@ -178,8 +183,10 @@ int32_t NetworkToHost(int32_t value) {
178183
int64_t NetworkToHost(int64_t value) {
179184
#ifdef HAVE_ENDIAN_H
180185
return be64toh(value);
186+
#elif defined(HAVE_LIBKERN_OSBYTEORDER_H)
187+
return OSSwapBigToHostInt64(value);
181188
#else
182-
#error "No be64toh for NetworkToHost, please report this."
189+
#error "No big endian 64 bit to host for NetworkToHost, please report this."
183190
#endif // HAVE_ENDIAN_H
184191
}
185192

@@ -202,16 +209,20 @@ int32_t HostToNetwork(int32_t value) {
202209
uint64_t HostToNetwork(uint64_t value) {
203210
#ifdef HAVE_ENDIAN_H
204211
return htobe64(value);
212+
#elif defined(HAVE_LIBKERN_OSBYTEORDER_H)
213+
return OSSwapHostToBigInt64(value);
205214
#else
206-
#error "No htobe64 for HostToNetwork, please report this."
215+
#error "No host to big endian 64 bit for HostToNetwork, please report this."
207216
#endif // HAVE_ENDIAN_H
208217
}
209218

210219
int64_t HostToNetwork(int64_t value) {
211220
#ifdef HAVE_ENDIAN_H
212221
return htobe64(value);
222+
#elif defined(HAVE_LIBKERN_OSBYTEORDER_H)
223+
return OSSwapHostToBigInt64(value);
213224
#else
214-
#error "No htobe64 for HostToNetwork, please report this."
225+
#error "No host to big endian 64 bit for HostToNetwork, please report this."
215226
#endif // HAVE_ENDIAN_H
216227
}
217228

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)