coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 11/12] digest: support -length specifiers on all digest tags


From: Pádraig Brady
Subject: [PATCH 11/12] digest: support -length specifiers on all digest tags
Date: Sun, 12 Sep 2021 19:14:05 +0100

This will be generally useful going forward, for sha3-256 etc.

* src/digest.c: Rename b2_length to digest_length, and
adjust/simplify the code to operate on this for both
b2sum and cksum -a blake2b.
---
 src/digest.c | 72 ++++++++++++++++++++++------------------------------
 1 file changed, 31 insertions(+), 41 deletions(-)

diff --git a/src/digest.c b/src/digest.c
index 1a22e0a8d..aa672c916 100644
--- a/src/digest.c
+++ b/src/digest.c
@@ -203,7 +203,7 @@ static unsigned char delim = '\n';
 
 #if HASH_ALGO_BLAKE2 || HASH_ALGO_CKSUM
 # define BLAKE2B_MAX_LEN BLAKE2B_OUTBYTES
-static uintmax_t b2_length;
+static uintmax_t digest_length;
 #endif /* HASH_ALGO_BLAKE2 */
 
 typedef void (*digest_output_fn)(char const*, int, void const*,
@@ -712,9 +712,6 @@ split_3 (char *s, size_t s_len,
     {
       i += algo_name_len;
 #if HASH_ALGO_BLAKE2 || HASH_ALGO_CKSUM
-# if HASH_ALGO_CKSUM
-      if (cksum_algorithm == blake2b) {
-# endif
       /* Terminate and match algorithm name.  */
       char const *algo_name = &s[i - algo_name_len];
       bool length_specified = s[i] == '-';
@@ -725,30 +722,24 @@ split_3 (char *s, size_t s_len,
       if (openssl_format)
         s[--i] = '(';
 
-      b2_length = BLAKE2B_MAX_LEN * 8;
+# if HASH_ALGO_BLAKE2
+      digest_length = BLAKE2B_MAX_LEN * 8;
+# else
+      digest_length = algorithm_bits[cksum_algorithm];
+# endif
       if (length_specified)
         {
           uintmax_t length;
           char *siend;
           if (! (xstrtoumax (s + i, &siend, 0, &length, NULL) == LONGINT_OK
-                 && 0 < length && length <= b2_length
+                 && 0 < length && length <= digest_length
                  && length % 8 == 0))
             return false;
 
           i = siend - s;
-          b2_length = length;
+          digest_length = length;
         }
-# if HASH_ALGO_CKSUM
-      }
-# endif
-# if HASH_ALGO_CKSUM
-      if (cksum_algorithm == blake2b)
-        digest_hex_bytes = b2_length / 4;
-      else
-        digest_hex_bytes = algorithm_bits[cksum_algorithm] / 4;
-# else
-      digest_hex_bytes = b2_length / 4;
-# endif
+      digest_hex_bytes = digest_length / 4;
 #endif
       if (s[i] == ' ')
         ++i;
@@ -783,7 +774,7 @@ split_3 (char *s, size_t s_len,
   if (digest_hex_bytes < 2 || digest_hex_bytes % 2
       || BLAKE2B_MAX_LEN * 2 < digest_hex_bytes)
     return false;
-  b2_length = digest_hex_bytes * 4;
+  digest_length = digest_hex_bytes * 4;
 # if HASH_ALGO_CKSUM
   }
 # endif
@@ -914,12 +905,12 @@ digest_file (char const *filename, int *binary, unsigned 
char *bin_result,
 
 #if HASH_ALGO_CKSUM
   if (cksum_algorithm == blake2b)
-    *length = b2_length / 8;
+    *length = digest_length / 8;
   err = DIGEST_STREAM (fp, bin_result, length);
 #elif HASH_ALGO_SUM
   err = DIGEST_STREAM (fp, bin_result, length);
 #elif HASH_ALGO_BLAKE2
-  err = DIGEST_STREAM (fp, bin_result, b2_length / 8);
+  err = DIGEST_STREAM (fp, bin_result, digest_length / 8);
 #else
   err = DIGEST_STREAM (fp, bin_result);
 #endif
@@ -961,13 +952,13 @@ output_file (char const *file, int binary_file, void 
const *digest,
 
       fputs (DIGEST_TYPE_STRING, stdout);
 # if HASH_ALGO_BLAKE2
-      if (b2_length < BLAKE2B_MAX_LEN * 8)
-        printf ("-%"PRIuMAX, b2_length);
+      if (digest_length < BLAKE2B_MAX_LEN * 8)
+        printf ("-%"PRIuMAX, digest_length);
 # elif HASH_ALGO_CKSUM
       if (cksum_algorithm == blake2b)
         {
-          if (b2_length < BLAKE2B_MAX_LEN * 8)
-            printf ("-%"PRIuMAX, b2_length);
+          if (digest_length < BLAKE2B_MAX_LEN * 8)
+            printf ("-%"PRIuMAX, digest_length);
         }
 # endif
       fputs (" (", stdout);
@@ -1236,10 +1227,10 @@ main (int argc, char **argv)
   const char* short_opts = "rs";
 #elif HASH_ALGO_CKSUM
   const char* short_opts = "a:l:bctwz";
-  const char* b2_length_str = "";
+  const char* digest_length_str = "";
 #elif HASH_ALGO_BLAKE2
   const char* short_opts = "l:bctwz";
-  const char* b2_length_str = "";
+  const char* digest_length_str = "";
 #else
   const char* short_opts = "bctwz";
 #endif
@@ -1260,12 +1251,12 @@ main (int argc, char **argv)
 #endif
 #if HASH_ALGO_BLAKE2 || HASH_ALGO_CKSUM
       case 'l':
-        b2_length = xdectoumax (optarg, 0, UINTMAX_MAX, "",
+        digest_length = xdectoumax (optarg, 0, UINTMAX_MAX, "",
                                 _("invalid length"), 0);
-        b2_length_str = optarg;
-        if (b2_length % 8 != 0)
+        digest_length_str = optarg;
+        if (digest_length % 8 != 0)
           {
-            error (0, 0, _("invalid length: %s"), quote (b2_length_str));
+            error (0, 0, _("invalid length: %s"), quote (digest_length_str));
             die (EXIT_FAILURE, 0, _("length is not a multiple of 8"));
           }
         break;
@@ -1327,28 +1318,27 @@ main (int argc, char **argv)
   min_digest_line_length = MIN_DIGEST_LINE_LENGTH;
 #if HASH_ALGO_BLAKE2 || HASH_ALGO_CKSUM
 # if HASH_ALGO_CKSUM
-  if (b2_length && cksum_algorithm != blake2b)
+  if (digest_length && cksum_algorithm != blake2b)
     die (EXIT_FAILURE, 0,
          _("--length is only supported with --algorithm=blake2b"));
 # endif
-  if (b2_length > BLAKE2B_MAX_LEN * 8)
+  if (digest_length > BLAKE2B_MAX_LEN * 8)
     {
-      error (0, 0, _("invalid length: %s"), quote (b2_length_str));
+      error (0, 0, _("invalid length: %s"), quote (digest_length_str));
       die (EXIT_FAILURE, 0,
            _("maximum digest length for %s is %d bits"),
            quote (DIGEST_TYPE_STRING),
            BLAKE2B_MAX_LEN * 8);
     }
-  if (b2_length == 0 && ! do_check)
-    b2_length = BLAKE2B_MAX_LEN * 8;
+  if (digest_length == 0 && ! do_check)
+    {
 # if HASH_ALGO_BLAKE2
-  digest_hex_bytes = b2_length / 4;
+      digest_length = BLAKE2B_MAX_LEN * 8;
 # else
-  if (cksum_algorithm == blake2b)
-    digest_hex_bytes = b2_length / 4;
-  else
-    digest_hex_bytes = algorithm_bits[cksum_algorithm] / 4;
+      digest_length = algorithm_bits[cksum_algorithm];
 # endif
+    }
+  digest_hex_bytes = digest_length / 4;
 #else
   digest_hex_bytes = DIGEST_HEX_BYTES;
 #endif
-- 
2.26.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]