]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
tests: Simplify read_hex_file. base16-base64-decode-api
authorNiels Möller <nisse@lysator.liu.se>
Wed, 5 Nov 2025 15:06:49 +0000 (16:06 +0100)
committerNiels Möller <nisse@lysator.liu.se>
Wed, 5 Nov 2025 15:06:49 +0000 (16:06 +0100)
ChangeLog
testsuite/slh-dsa-test.c

index f8a141f9dab81c4068b68c1dec7cd00944407f93..b85a37ee58f9c38a9c3650fc28840c831be309cf 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2025-11-05  Niels Möller  <nisse@lysator.liu.se>
+
+       * testsuite/slh-dsa-test.c (read_hex_file): Simplify, using
+       base16_decode_update.
+
 2025-10-30  Niels Möller  <nisse@lysator.liu.se>
 
        * base16-decode.c (base16_decode_single): Rewrite as a call to
index 4d0162b98d3a8a7a2dd56447805e9010d0fb049e..7653687b73eaa1e954182e35e7709be9d18b7a86 100644 (file)
@@ -51,15 +51,15 @@ read_hex_file (const char *name, size_t max_size)
   struct base16_decode_ctx ctx;
   base16_decode_init (&ctx);
 
-  /* This function expects to read up to EOF without exhausting the
-     buffer, and fails if the file is too large. */
-  s = tstring_alloc (max_size + 1);
+  /* This function expects to read up to EOF, and fails if the amount
+     of data exceeds the max size. */
+  s = tstring_alloc (max_size);
 
   for (done = 0;;)
     {
       size_t left = s->length - done;
       size_t got = fread (input_buf, 1, MIN (left, sizeof (input_buf)), input);
-      size_t i;
+      size_t res;
 
       if (!got)
        {
@@ -74,27 +74,14 @@ read_hex_file (const char *name, size_t max_size)
          return s;
        }
 
-      /* Use base16_decode_single, to make handling of excess input
-        straight-forward. */
-      for (i = 0; i < got; i++)
+      res = s->length - done;
+      if (!base16_decode_update (&ctx, &res, s->data + done, got, input_buf))
        {
-         int res = base16_decode_single (&ctx, s->data + done, input_buf[i]);
-         if (res < 0)
-           {
-             fprintf (stderr, "hex decoding %s failed\n", name);
-             FAIL ();
-           }
-         done += res;
-         if (done == s->length)
-           {
-             fprintf (stderr, "hex file %s exceeds maximum size %ld\n",
-                      name, (long) s->length - 1);
-             FAIL ();
-           }
+         fprintf (stderr, "hex decoding %s failed (possibly exceeding max size)\n", name);
+         FAIL ();
        }
+      done += res;
     }
-  fprintf (stderr, "file %s larger than expected\n", name);
-  FAIL ();
 }
 
 static void