From: Niels Möller Date: Wed, 5 Nov 2025 15:06:49 +0000 (+0100) Subject: tests: Simplify read_hex_file. X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2Fbase16-base64-decode-api;p=thirdparty%2Fnettle.git tests: Simplify read_hex_file. --- diff --git a/ChangeLog b/ChangeLog index f8a141f9..b85a37ee 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2025-11-05 Niels Möller + + * testsuite/slh-dsa-test.c (read_hex_file): Simplify, using + base16_decode_update. + 2025-10-30 Niels Möller * base16-decode.c (base16_decode_single): Rewrite as a call to diff --git a/testsuite/slh-dsa-test.c b/testsuite/slh-dsa-test.c index 4d0162b9..7653687b 100644 --- a/testsuite/slh-dsa-test.c +++ b/testsuite/slh-dsa-test.c @@ -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