Introduce a new "byte-swapping.h" header which encapsulates the
differences between platforms.
---
common/include/Makefile.am | 1 +
common/include/byte-swapping.h | 84 ++++++++++++++++++++++++++++++++++
configure.ac | 8 +++-
filters/partition/Makefile.am | 3 +-
filters/partition/partition.c | 3 +-
plugins/nbd/Makefile.am | 1 +
plugins/nbd/nbd.c | 1 +
plugins/pattern/Makefile.am | 3 +-
plugins/pattern/pattern.c | 2 +
src/connections.c | 2 +-
src/internal.h | 36 ---------------
tests/test-layers.c | 1 +
12 files changed, 104 insertions(+), 41 deletions(-)
diff --git a/common/include/Makefile.am b/common/include/Makefile.am
index a882ff9..07f28b3 100644
--- a/common/include/Makefile.am
+++ b/common/include/Makefile.am
@@ -35,6 +35,7 @@ include $(top_srcdir)/common-rules.mk
# These headers contain only common code shared by the core server,
# plugins and/or filters. They are not installed.
EXTRA_DIST = \
+ byte-swapping.h \
exit-with-parent.h \
ispowerof2.h \
iszero.h
diff --git a/common/include/byte-swapping.h b/common/include/byte-swapping.h
new file mode 100644
index 0000000..3f35aa5
--- /dev/null
+++ b/common/include/byte-swapping.h
@@ -0,0 +1,84 @@
+/* nbdkit
+ * Copyright (C) 2013-2018 Red Hat Inc.
+ * All rights reserved.
+ *
+ * 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.
+ */
+
+#ifndef NBDKIT_BYTE_SWAPPING_H
+#define NBDKIT_BYTE_SWAPPING_H
+
+#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/configure.ac b/configure.ac
index b9bb220..87f14d6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -129,7 +129,13 @@ dnl restore CFLAGS
CFLAGS="${acx_nbdkit_save_CFLAGS}"
dnl Check for other headers, all optional.
-AC_CHECK_HEADERS([selinux/selinux.h sys/prctl.h sys/procctl.h])
+AC_CHECK_HEADERS([\
+ byteswap.h \
+ endian.h \
+ selinux/selinux.h \
+ sys/endian.h \
+ sys/prctl.h \
+ sys/procctl.h])
dnl Check support for setsockcreatecon_raw (part of SELinux).
AC_CHECK_LIB([selinux], [setsockcreatecon_raw], [], [:])
diff --git a/filters/partition/Makefile.am b/filters/partition/Makefile.am
index 9ab8730..eebe9e1 100644
--- a/filters/partition/Makefile.am
+++ b/filters/partition/Makefile.am
@@ -41,7 +41,8 @@ nbdkit_partition_filter_la_SOURCES = \
$(top_srcdir)/include/nbdkit-filter.h
nbdkit_partition_filter_la_CPPFLAGS = \
- -I$(top_srcdir)/include
+ -I$(top_srcdir)/include \
+ -I$(top_srcdir)/common/include
nbdkit_partition_filter_la_CFLAGS = \
$(WARNINGS_CFLAGS)
nbdkit_partition_filter_la_LDFLAGS = \
diff --git a/filters/partition/partition.c b/filters/partition/partition.c
index 1fe0bc7..f6d2c5c 100644
--- a/filters/partition/partition.c
+++ b/filters/partition/partition.c
@@ -38,10 +38,11 @@
#include <stdint.h>
#include <string.h>
#include <inttypes.h>
-#include <endian.h>
#include <nbdkit-filter.h>
+#include "byte-swapping.h"
+
#define THREAD_MODEL NBDKIT_THREAD_MODEL_PARALLEL
static int partnum = -1;
diff --git a/plugins/nbd/Makefile.am b/plugins/nbd/Makefile.am
index f930b26..e998a28 100644
--- a/plugins/nbd/Makefile.am
+++ b/plugins/nbd/Makefile.am
@@ -42,6 +42,7 @@ nbdkit_nbd_plugin_la_SOURCES = \
nbdkit_nbd_plugin_la_CPPFLAGS = \
-I$(top_srcdir)/include \
+ -I$(top_srcdir)/common/include \
-I$(top_srcdir)/src
nbdkit_nbd_plugin_la_CFLAGS = \
$(WARNINGS_CFLAGS)
diff --git a/plugins/nbd/nbd.c b/plugins/nbd/nbd.c
index 2b5569b..b9a4523 100644
--- a/plugins/nbd/nbd.c
+++ b/plugins/nbd/nbd.c
@@ -50,6 +50,7 @@
#include <nbdkit-plugin.h>
#include "protocol.h"
+#include "byte-swapping.h"
static char *sockname = NULL;
static char *export = NULL;
diff --git a/plugins/pattern/Makefile.am b/plugins/pattern/Makefile.am
index f5b99a2..d6df76c 100644
--- a/plugins/pattern/Makefile.am
+++ b/plugins/pattern/Makefile.am
@@ -41,7 +41,8 @@ nbdkit_pattern_plugin_la_SOURCES = \
$(top_srcdir)/include/nbdkit-plugin.h
nbdkit_pattern_plugin_la_CPPFLAGS = \
- -I$(top_srcdir)/include
+ -I$(top_srcdir)/include \
+ -I$(top_srcdir)/common/include
nbdkit_pattern_plugin_la_CFLAGS = \
$(WARNINGS_CFLAGS)
nbdkit_pattern_plugin_la_LDFLAGS = \
diff --git a/plugins/pattern/pattern.c b/plugins/pattern/pattern.c
index 11b258d..46b5abd 100644
--- a/plugins/pattern/pattern.c
+++ b/plugins/pattern/pattern.c
@@ -44,6 +44,8 @@
#define NBDKIT_API_VERSION 2
#include <nbdkit-plugin.h>
+#include "byte-swapping.h"
+
/* The size of disk in bytes (initialized by size=<SIZE> parameter). */
static int64_t size = 0;
diff --git a/src/connections.c b/src/connections.c
index 398f89d..4f4a17f 100644
--- a/src/connections.c
+++ b/src/connections.c
@@ -40,7 +40,6 @@
#include <string.h>
#include <unistd.h>
#include <errno.h>
-#include <endian.h>
#include <sys/types.h>
#include <stddef.h>
#include <assert.h>
@@ -48,6 +47,7 @@
#include <pthread.h>
#include "internal.h"
+#include "byte-swapping.h"
#include "protocol.h"
/* Maximum read or write request that we will handle. */
diff --git a/src/internal.h b/src/internal.h
index f263c8a..55bb1eb 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -58,42 +58,6 @@
#define SOCK_CLOEXEC 0
#endif
-#ifndef htobe32
-#include <byteswap.h>
-# 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
-
#if HAVE_VALGRIND
# include <valgrind.h>
/*
http://valgrind.org/docs/manual/faq.html#faq.unhelpful */
diff --git a/tests/test-layers.c b/tests/test-layers.c
index ee425ea..baf046a 100644
--- a/tests/test-layers.c
+++ b/tests/test-layers.c
@@ -59,6 +59,7 @@
#include <pthread.h>
+#include "byte-swapping.h"
#include "exit-with-parent.h"
#include "protocol.h" /* From nbdkit core. */
--
2.18.0