Hi,

Now that <endian.h> is a part of POSIX 2024 it should become more
portable than <byteswap.h>. And it simplifies the code a bit. Patches
attached.

Side note, I feel like gnulib-tool should deal with adding the built
versions of the *.in.h headers. That way it doesn't have to be done
manually. I'll have a look at that.

Collin

>From ce463172d9340ae1b078808d8318fd893ae1445b Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Sat, 29 Jun 2024 04:23:11 -0700
Subject: [PATCH 1/2] build: update gnulib submodule to latest

---
 gnulib | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gnulib b/gnulib
index 15cd8edb6..95713c008 160000
--- a/gnulib
+++ b/gnulib
@@ -1 +1 @@
-Subproject commit 15cd8edb6ec9aed2585e10456d46eec09d5c1b8b
+Subproject commit 95713c0087424c99a6ba81d3c1b768814fd9371a
-- 
2.45.2

>From 1d6cf632ed932953c37656aa876e1d5a6ef3fc89 Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Sat, 29 Jun 2024 04:36:38 -0700
Subject: [PATCH 2/2] maint: Prefer endian.h for byte order conversions

* bootstrap.conf (gnulib_modules): Remove byteswap. Add endian.
* src/cksum.c: Include endian.h instead of byteswap.h.
(SWAP): Remove macro.
(cksum_slice8): Use htobe32 instead of SWAP.
(output_crc): Likewise.
* src/sum.c: Include endian.h instead of byteswap.h.
(SWAP): Remove macro.
(output_bsd): Use htobe16 instead of SWAP.
(output_sysv): Use htobe16 instead of SWAP.
* .gitignore: Add /lib/endian.h.
---
 .gitignore     |  1 +
 bootstrap.conf |  2 +-
 src/cksum.c    | 14 ++++----------
 src/sum.c      | 12 +++---------
 4 files changed, 9 insertions(+), 20 deletions(-)

diff --git a/.gitignore b/.gitignore
index 22d6b01df..407203dc3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -62,6 +62,7 @@
 /lib/configmake.h
 /lib/ctype.h
 /lib/dirent.h
+/lib/endian.h
 /lib/errno.h
 /lib/error.h
 /lib/fcntl.h
diff --git a/bootstrap.conf b/bootstrap.conf
index 43924c5b1..63bf192cc 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -47,7 +47,6 @@ gnulib_modules="
   base64
   btoc32
   buffer-lcm
-  byteswap
   c-strcase
   c32iscntrl
   c32isspace
@@ -78,6 +77,7 @@ gnulib_modules="
   do-release-commit-and-tag
   dtoastr
   dup2
+  endian
   environ
   error
   euidaccess
diff --git a/src/cksum.c b/src/cksum.c
index ca6775539..9909b14f5 100644
--- a/src/cksum.c
+++ b/src/cksum.c
@@ -37,15 +37,9 @@
 #include <stdio.h>
 #include <sys/types.h>
 #include <stdint.h>
+#include <endian.h>
 #include "system.h"
 
-#include <byteswap.h>
-#ifdef WORDS_BIGENDIAN
-# define SWAP(n) (n)
-#else
-# define SWAP(n) bswap_32 (n)
-#endif
-
 #ifdef CRCTAB
 
 # define BIT(x)	((uint_fast32_t) 1 << (x))
@@ -189,8 +183,8 @@ cksum_slice8 (FILE *fp, uint_fast32_t *crc_out, uintmax_t *length_out)
       while (bytes_read >= 8)
         {
           uint32_t first = *datap++, second = *datap++;
-          crc ^= SWAP (first);
-          second = SWAP (second);
+          crc ^= htobe32 (first);
+          second = htobe32 (second);
           crc = (crctab[7][(crc >> 24) & 0xFF]
                  ^ crctab[6][(crc >> 16) & 0xFF]
                  ^ crctab[5][(crc >> 8) & 0xFF]
@@ -258,7 +252,7 @@ output_crc (char const *file, int binary_file, void const *digest, bool raw,
   if (raw)
     {
       /* Output in network byte order (big endian).  */
-      uint32_t out_int = SWAP (*(uint32_t *)digest);
+      uint32_t out_int = htobe32 (*(uint32_t *)digest);
       fwrite (&out_int, 1, 32/8, stdout);
       return;
     }
diff --git a/src/sum.c b/src/sum.c
index 8c6979c99..2d07e6ef6 100644
--- a/src/sum.c
+++ b/src/sum.c
@@ -22,17 +22,11 @@
 
 #include <stdio.h>
 #include <sys/types.h>
+#include <endian.h>
 #include "system.h"
 #include "human.h"
 #include "sum.h"
 
-#include <byteswap.h>
-#ifdef WORDS_BIGENDIAN
-# define SWAP(n) (n)
-#else
-# define SWAP(n) bswap_16 (n)
-#endif
-
 /* Calculate the checksum and the size in bytes of stream STREAM.
    Return -1 on error, 0 on success.  */
 
@@ -198,7 +192,7 @@ output_bsd (char const *file, int binary_file, void const *digest,
     {
       /* Output in network byte order (big endian).  */
       uint16_t out_int = *(int *)digest;
-      out_int = SWAP (out_int);
+      out_int = htobe16 (out_int);
       fwrite (&out_int, 1, 16/8, stdout);
       return;
     }
@@ -223,7 +217,7 @@ output_sysv (char const *file, int binary_file, void const *digest,
     {
       /* Output in network byte order (big endian).  */
       uint16_t out_int = *(int *)digest;
-      out_int = SWAP (out_int);
+      out_int = htobe16 (out_int);
       fwrite (&out_int, 1, 16/8, stdout);
       return;
     }
-- 
2.45.2

Reply via email to