coreutils
[Top][All Lists]
Advanced

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

[PATCH 09/12] maint: simplify b2sum to only handle BLAKE2b


From: Pádraig Brady
Subject: [PATCH 09/12] maint: simplify b2sum to only handle BLAKE2b
Date: Sun, 12 Sep 2021 19:14:03 +0100

Any further variants will use the cksum -a table driven mechanism.

* src/digest.c: Remove BLAKE2 specific table driven code.
---
 src/digest.c | 55 +++++++++++++---------------------------------------
 1 file changed, 14 insertions(+), 41 deletions(-)

diff --git a/src/digest.c b/src/digest.c
index 07cc7c921..633e62487 100644
--- a/src/digest.c
+++ b/src/digest.c
@@ -83,7 +83,7 @@
 # define DIGEST_ALIGN 4
 #elif HASH_ALGO_BLAKE2
 # define PROGRAM_NAME "b2sum"
-# define DIGEST_TYPE_STRING "BLAKE2"
+# define DIGEST_TYPE_STRING "BLAKE2b"
 # define DIGEST_STREAM blake2b_stream
 # define DIGEST_BITS 512
 # define DIGEST_REFERENCE "RFC 7693"
@@ -202,27 +202,8 @@ static int bsd_reversed = -1;
 static unsigned char delim = '\n';
 
 #if HASH_ALGO_BLAKE2 || HASH_ALGO_CKSUM
-static char const *const algorithm_in_string[] =
-{
-  "blake2b", NULL
-};
-static char const *const algorithm_out_string[] =
-{
-  "BLAKE2b", NULL
-};
-enum blake2_Algorithm
-{
-  BLAKE2b
-};
-verify (ARRAY_CARDINALITY (algorithm_in_string) == 2);
-verify (ARRAY_CARDINALITY (algorithm_out_string) == 2);
-
-static enum blake2_Algorithm b2_algorithm;
+# define BLAKE2B_MAX_LEN BLAKE2B_OUTBYTES
 static uintmax_t b2_length;
-static uintmax_t blake2_max_len[]=
-{
-  BLAKE2B_OUTBYTES
-};
 #endif /* HASH_ALGO_BLAKE2 */
 
 typedef void (*digest_output_fn)(char const*, int, void const*,
@@ -681,20 +662,15 @@ split_3 (char *s, size_t s_len,
 # endif
       /* Terminate and match algorithm name.  */
       char const *algo_name = &s[i - algo_name_len];
-      /* Skip algorithm variants.  */
-      while (s[i] && ! ISWHITE (s[i]) && s[i] != '-' && s[i] != '(')
-        ++i;
       bool length_specified = s[i] == '-';
       bool openssl_format = s[i] == '('; /* and no length_specified */
       s[i++] = '\0';
-      ptrdiff_t algo = argmatch (algo_name, algorithm_out_string, NULL, 0);
-      if (algo < 0)
+      if (!STREQ (algo_name, DIGEST_TYPE_STRING))
         return false;
-      b2_algorithm = algo;
       if (openssl_format)
         s[--i] = '(';
 
-      b2_length = blake2_max_len[b2_algorithm] * 8;
+      b2_length = BLAKE2B_MAX_LEN * 8;
       if (length_specified)
         {
           uintmax_t length;
@@ -743,7 +719,7 @@ split_3 (char *s, size_t s_len,
   while (isxdigit (*hp++))
     digest_hex_bytes++;
   if (digest_hex_bytes < 2 || digest_hex_bytes % 2
-      || blake2_max_len[b2_algorithm] * 2 < digest_hex_bytes)
+      || BLAKE2B_MAX_LEN * 2 < digest_hex_bytes)
     return false;
   b2_length = digest_hex_bytes * 4;
 # if HASH_ALGO_CKSUM
@@ -921,19 +897,16 @@ output_file (char const *file, int binary_file, void 
const *digest,
       if (needs_escape)
         putchar ('\\');
 
+      fputs (DIGEST_TYPE_STRING, stdout);
 # if HASH_ALGO_BLAKE2
-      fputs (algorithm_out_string[b2_algorithm], stdout);
-      if (b2_length < blake2_max_len[b2_algorithm] * 8)
+      if (b2_length < BLAKE2B_MAX_LEN * 8)
         printf ("-%"PRIuMAX, b2_length);
-# else
-      fputs (DIGEST_TYPE_STRING, stdout);
-#  if HASH_ALGO_CKSUM
+# elif HASH_ALGO_CKSUM
       if (cksum_algorithm == blake2b)
         {
-          if (b2_length < blake2_max_len[b2_algorithm] * 8)
+          if (b2_length < BLAKE2B_MAX_LEN * 8)
             printf ("-%"PRIuMAX, b2_length);
         }
-#  endif
 # endif
       fputs (" (", stdout);
       print_filename (file, needs_escape);
@@ -1295,16 +1268,16 @@ main (int argc, char **argv)
     die (EXIT_FAILURE, 0,
          _("--length is only supported with --algorithm=blake2b"));
 # endif
-  if (b2_length > blake2_max_len[b2_algorithm] * 8)
+  if (b2_length > BLAKE2B_MAX_LEN * 8)
     {
       error (0, 0, _("invalid length: %s"), quote (b2_length_str));
       die (EXIT_FAILURE, 0,
-           _("maximum digest length for %s is %"PRIuMAX" bits"),
-           quote (algorithm_in_string[b2_algorithm]),
-           blake2_max_len[b2_algorithm] * 8);
+           _("maximum digest length for %s is %d bits"),
+           quote (DIGEST_TYPE_STRING),
+           BLAKE2B_MAX_LEN * 8);
     }
   if (b2_length == 0 && ! do_check)
-    b2_length = blake2_max_len[b2_algorithm] * 8;
+    b2_length = BLAKE2B_MAX_LEN * 8;
 # if HASH_ALGO_BLAKE2
   digest_hex_bytes = b2_length / 4;
 # else
-- 
2.26.2




reply via email to

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