[Libguestfs] [libnbd PATCH 3/6] common: include: Define bswap_16, bswap_32 and bswap_64 [finish port]

Laszlo Ersek lersek at redhat.com
Wed Mar 1 11:40:24 UTC 2023


From: "Richard W.M. Jones" <rjones at redhat.com>

Complete porting nbdkit commit 2bc1f3383e00 ("common: include: Define
bswap_16, bswap_32 and bswap_64.", 2020-05-30). Libnbd commit def468c2a9e5
("common/include/byte-swapping.h: Synchronize with nbdkit.", 2020-10-27)
had ported nbdkit commit 2bc1f3383e00 earlier, but it contains the
following omissions / mistakes:

- In the header file, it starts using the WORDS_BIGENDIAN macro, but it
  does not port the AC_C_BIGENDIAN stanza from nbdkit's "configure.ac".

- It does not port the test case extension.

Fill those gaps now.

Signed-off-by: Laszlo Ersek <lersek at redhat.com>
(cherry picked from nbdkit commit 2bc1f3383e00bd5bbe2e1bb911ab56e1e42c4db3)
---
 configure.ac                        |  3 +++
 common/include/test-byte-swapping.c | 18 ++++++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/configure.ac b/configure.ac
index d967277ea308..6919b2782d0a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -43,6 +43,9 @@ AM_PROG_CC_C_O
 
 AX_PTHREAD
 
+dnl Defines WORDS_BIGENDIAN on big endian platforms.
+AC_C_BIGENDIAN
+
 dnl Check for C++ (optional, we just use this to test the header
 dnl can be included from C++ code).
 AC_PROG_CXX
diff --git a/common/include/test-byte-swapping.c b/common/include/test-byte-swapping.c
index fcb83c574bfc..741b3d11517e 100644
--- a/common/include/test-byte-swapping.c
+++ b/common/include/test-byte-swapping.c
@@ -85,5 +85,23 @@ main (void)
   i64 = htobe64 (0x123456789abcdef0);
   assert (memcmp (&i64, be64, 8) == 0);
 
+  memcpy (&i16, le16, 2);
+  i16 = bswap_16 (i16);
+  assert (memcmp (&i16, be16, 2) == 0);
+  i16 = bswap_16 (i16);
+  assert (memcmp (&i16, le16, 2) == 0);
+
+  memcpy (&i32, le32, 4);
+  i32 = bswap_32 (i32);
+  assert (memcmp (&i32, be32, 4) == 0);
+  i32 = bswap_32 (i32);
+  assert (memcmp (&i32, le32, 4) == 0);
+
+  memcpy (&i64, le64, 8);
+  i64 = bswap_64 (i64);
+  assert (memcmp (&i64, be64, 8) == 0);
+  i64 = bswap_64 (i64);
+  assert (memcmp (&i64, le64, 8) == 0);
+
   exit (EXIT_SUCCESS);
 }



More information about the Libguestfs mailing list