Subject: Re: bug#66698: I think hex decoding with basenc -d --base16 should be case-insensitive
From: Pádraig Brady <[email-masked]>
To: Niels Möller <[email-masked]>, [email-masked]
Date: Mon, 23 Oct 2023 13:01:17 +0100
List-Id: <debbugs-submit.debbugs.gnu.org>

On 23/10/2023 10:37, Niels Möller wrote:
> Hi,
> 
> the docs for basenc --base16 says "hex encoding (RFC4648 section 8)".
> The referenced section in that RFC says
> 
>    Essentially, Base 16 encoding is the standard case-insensitive hex
>    encoding and may be referred to as "base16" or "hex".
> 
> I think it would be both more useful, and consistent with docs, if
> basenc -d --base16 accepted either upper- or lowercase hex digits.
> 
> Current behavior, with basenc (GNU coreutils) 9.1:
> 
>    $ echo 666F6F0A |basenc --base16 -d
>    foo
>    $ echo 666F6f0A |basenc --base16 -d
>    fobasenc: invalid input
> 
> I think both inputs should give the same output, "foo\n", at least by
> default. Possibly configurable with options like --strict, --upper,
> --lower, etc (--upper/--lower would be useful also for the --base16
> encoding, i.e., no -d).

Agreed.
Will apply the attached later.
Marking this as done.

thanks,
Pádraig

From 69f8e90185e518d1722ed6a036f4b18779553e49 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <[email-masked]>
Date: Mon, 23 Oct 2023 12:51:19 +0100
Subject: [PATCH] basenc: --base16: support lower case hex digits

* src/basenc.c (base16_decode_ctx): Convert to uppercase
before converting from hex.
* tests/basenc/basenc.pl: Add a test case.
* NEWS: Mention the change in behavior.
Addresses https://bugs.gnu.org/66698
---
 NEWS                   | 3 +++
 src/basenc.c           | 2 +-
 tests/basenc/basenc.pl | 1 +
 3 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index 93f98b99d..56c2a4785 100644
--- a/NEWS
+++ b/NEWS
@@ -19,6 +19,9 @@ GNU coreutils NEWS                                    -*- outline -*-
   base32 and base64 no longer require padding when decoding.
   Previously an error was given for non padded encoded data.
 
+  basenc --base16 -d no supports lower case hexadecimal characters.
+  Previously an error was given for lower case hex digits.
+
   ls --dired now implies long format output without hyperlinks enabled,
   and will take precedence over previously specified formats or hyperlink mode.
 
diff --git a/src/basenc.c b/src/basenc.c
index 12021e900..74cf03a49 100644
--- a/src/basenc.c
+++ b/src/basenc.c
@@ -577,7 +577,7 @@ base16_decode_ctx (struct base_decode_context *ctx,
           continue;
         }
 
-      int nib = *in++;
+      int nib = c_toupper (*in++);
       if ('0' <= nib && nib <= '9')
         nib -= '0';
       else if ('A' <= nib && nib <= 'F')
diff --git a/tests/basenc/basenc.pl b/tests/basenc/basenc.pl
index de20d2dbc..2b0e79e93 100755
--- a/tests/basenc/basenc.pl
+++ b/tests/basenc/basenc.pl
@@ -159,6 +159,7 @@ my @Tests =
  ['b16_7', '--base16 -d',     {IN=>'G'}, {EXIT=>1},
   {ERR=>"$prog: invalid input\n"}],
  ['b16_8', '--base16 -d',     {IN=>"AB\nCD"}, {OUT=>"\xAB\xCD"}],
+ ['b16_9', '--base16 -d',     {IN=>lc ($base16_out)},  {OUT=>$base16_in}],

 
-- 
2.41.0