]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
maint: prefer endian.h for byte order conversions
authorCollin Funk <collin.funk1@gmail.com>
Sat, 29 Jun 2024 11:36:38 +0000 (04:36 -0700)
committerPádraig Brady <P@draigBrady.com>
Sat, 29 Jun 2024 15:11:23 +0000 (16:11 +0100)
* 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
bootstrap.conf
src/cksum.c
src/sum.c

index 22d6b01df86f3fd4828248ed28174aac956bbb72..407203dc3910ffcfddc618885990a78eccdd010a 100644 (file)
@@ -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
index 43924c5b120f88c3ef3b16430f82dd8682395172..63bf192cc38b138ef55bf662b7db792965a32cd4 100644 (file)
@@ -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
index ca67755394f2d8a1d454b5abdc3dad0534db15b6..9909b14f5a9eb690d03ed50310ad6404533776d3 100644 (file)
 #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;
     }
index 8c6979c997eb485502d26cc589ab071ed71961eb..2d07e6ef6f6d052530726aaf365b1f726562e925 100644 (file)
--- a/src/sum.c
+++ b/src/sum.c
 
 #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;
     }