summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--cmake/checks.cmake44
-rw-r--r--src/util.h22
2 files changed, 42 insertions, 24 deletions
diff --git a/cmake/checks.cmake b/cmake/checks.cmake
index d42f336..f9f0399 100644
--- a/cmake/checks.cmake
+++ b/cmake/checks.cmake
@@ -61,31 +61,33 @@ string(COMPARE NOTEQUAL "${SIZEOF_ETHHDR}" "" HAVE_ETHHDR)
set(CMAKE_REQUIRED_INCLUDES "sys/types.h")
-check_c_source_compiles("
-#include <endian.h>
-
-int main() {
- return 0;
-}
-" HAVE_ENDIAN_H)
-
-if(HAVE_ENDIAN_H)
- check_symbol_exists("be32toh" "endian.h" HAVE_LINUX_ENDIAN)
-
-else(HAVE_ENDIAN_H)
+if(NOT DARWIN)
check_c_source_compiles("
- #include <sys/types.h>
- #include <sys/endian.h>
+ #include <endian.h>
int main() {
return 0;
}
- " HAVE_SYS_ENDIAN_H)
+ " HAVE_ENDIAN_H)
+
+ if(HAVE_ENDIAN_H)
+ check_symbol_exists("be32toh" "endian.h" HAVE_LINUX_ENDIAN)
+
+ else(HAVE_ENDIAN_H)
+ check_c_source_compiles("
+ #include <sys/types.h>
+ #include <sys/endian.h>
- if(HAVE_SYS_ENDIAN_H)
- check_symbol_exists("be32toh" "sys/types.h;sys/endian.h" HAVE_LINUX_ENDIAN)
+ int main() {
+ return 0;
+ }
+ " HAVE_SYS_ENDIAN_H)
- else(HAVE_SYS_ENDIAN_H)
- message(FATAL_ERROR "Neither <endian.h> nor <sys/endian.h> found")
- endif(HAVE_SYS_ENDIAN_H)
-endif(HAVE_ENDIAN_H)
+ if(HAVE_SYS_ENDIAN_H)
+ check_symbol_exists("be32toh" "sys/types.h;sys/endian.h" HAVE_LINUX_ENDIAN)
+
+ else(HAVE_SYS_ENDIAN_H)
+ message(FATAL_ERROR "Neither <endian.h> nor <sys/endian.h> found")
+ endif(HAVE_SYS_ENDIAN_H)
+ endif(HAVE_ENDIAN_H)
+endif(NOT DARWIN)
diff --git a/src/util.h b/src/util.h
index 3acc42f..db4457d 100644
--- a/src/util.h
+++ b/src/util.h
@@ -105,12 +105,28 @@ static inline size_t min_size_t(size_t a, size_t b) {
}
-#ifndef HAVE_LINUX_ENDIAN
+#ifdef __APPLE__
-/** converts a 32bit integer from big endian to host byte order */
+#include <libkern/OSByteOrder.h>
+
+/** Converts a 32bit integer from host byte order to big endian */
+#define htobe32(x) OSSwapHostToBigInt32(x)
+
+/** Converts a 32bit integer from host byte order to little endian */
+#define htole32(x) OSSwapHostToLittleInt32(x)
+
+/** Converts a 32bit integer from big endian to host byte order */
+#define be32toh(x) OSSwapBigToHostInt32(x)
+
+/** Converts a 32bit integer from little endian to host byte order */
+#define le32toh(x) OSSwapLittleToHostInt32(x)
+
+#elif !defined(HAVE_LINUX_ENDIAN)
+
+/** Converts a 32bit integer from big endian to host byte order */
#define be32toh(x) betoh32(x)
-/** converts a 32bit integer from little endian to host byte order */
+/** Converts a 32bit integer from little endian to host byte order */
#define le32toh(x) letoh32(x)
#endif