This allows functions such as beto64h to be used on FreeBSD.
---
configure.ac | 6 +++
lib/Makefile.am | 1 +
lib/byte-swapping.h | 96 +++++++++++++++++++++++++++++++++++++++++++++
lib/internal.h | 1 +
4 files changed, 104 insertions(+)
diff --git a/configure.ac b/configure.ac
index 50e83a7..8b6d99a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -59,6 +59,12 @@ if test "x$gcc_warnings" = "xyes"; then
AC_SUBST([WARNINGS_CFLAGS])
fi
+dnl Check for other headers, all optional.
+AC_CHECK_HEADERS([\
+ byteswap.h \
+ endian.h \
+ sys/endian.h])
+
dnl Check for GnuTLS (optional, for TLS support).
AC_ARG_WITH([gnutls],
[AS_HELP_STRING([--without-gnutls],
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 7939375..9d31d92 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -36,6 +36,7 @@ BUILT_SOURCES = $(generator_built)
libnbd_la_SOURCES = \
aio.c \
api.c \
+ byte-swapping.h \
connect.c \
crypto.c \
debug.c \
diff --git a/lib/byte-swapping.h b/lib/byte-swapping.h
new file mode 100644
index 0000000..0dd1754
--- /dev/null
+++ b/lib/byte-swapping.h
@@ -0,0 +1,96 @@
+/* nbdkit
+ * Copyright (C) 2013-2018 Red Hat Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/* The job of this header is to define macros (or functions) called
+ * things like 'htobe32' and 'le64toh' which byte swap N-bit integers
+ * between host representation, and little and big endian. The core
+ * code and plugins in nbdkit uses these names and relies on this
+ * header to provide the platform-specific implementation. On Linux
+ * these are defined in <endian.h> but other platforms have other
+ * requirements.
+ */
+
+#ifndef NBDKIT_BYTE_SWAPPING_H
+#define NBDKIT_BYTE_SWAPPING_H
+
+#ifdef __HAIKU__
+#define _BSD_SOURCE
+#endif
+
+#ifdef HAVE_BYTESWAP_H
+#include <byteswap.h>
+#endif
+
+#ifdef HAVE_ENDIAN_H
+#include <endian.h>
+#endif
+
+#ifdef HAVE_SYS_ENDIAN_H
+#include <sys/endian.h>
+#endif
+
+#ifndef htobe32
+# if __BYTE_ORDER == __LITTLE_ENDIAN
+# define htobe16(x) __bswap_16 (x)
+# define htole16(x) (x)
+# define be16toh(x) __bswap_16 (x)
+# define le16toh(x) (x)
+
+# define htobe32(x) __bswap_32 (x)
+# define htole32(x) (x)
+# define be32toh(x) __bswap_32 (x)
+# define le32toh(x) (x)
+
+# define htobe64(x) __bswap_64 (x)
+# define htole64(x) (x)
+# define be64toh(x) __bswap_64 (x)
+# define le64toh(x) (x)
+
+# else
+# define htobe16(x) (x)
+# define htole16(x) __bswap_16 (x)
+# define be16toh(x) (x)
+# define le16toh(x) __bswap_16 (x)
+
+# define htobe32(x) (x)
+# define htole32(x) __bswap_32 (x)
+# define be32toh(x) (x)
+# define le32toh(x) __bswap_32 (x)
+
+# define htobe64(x) (x)
+# define htole64(x) __bswap_64 (x)
+# define be64toh(x) (x)
+# define le64toh(x) __bswap_64 (x)
+# endif
+#endif
+
+#endif /* NBDKIT_BYTE_SWAPPING_H */
diff --git a/lib/internal.h b/lib/internal.h
index 0bb8411..0b4364a 100644
--- a/lib/internal.h
+++ b/lib/internal.h
@@ -30,6 +30,7 @@
#include "libnbd.h"
#include "nbd-protocol.h"
+#include "byte-swapping.h"
#include "states.h"
#include "unlocked.h"
--
2.22.0