mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH] commands: introduce simple base64 command
Date: Thu, 10 Apr 2025 11:57:18 +0200	[thread overview]
Message-ID: <20250410095718.2258908-1-a.fatoum@pengutronix.de> (raw)

We have base64 functionality in barebox as that's used as formatting for
sealed blobs and JWTs. Add a simple command-line interface to the
functionality for aid during development and to simplify copying short
binary strings via the console.

We do not have code to encode in barebox, so the command only supports
decoding for now.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 commands/Kconfig  | 15 ++++++++
 commands/Makefile |  1 +
 commands/base64.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 108 insertions(+)
 create mode 100644 commands/base64.c

diff --git a/commands/Kconfig b/commands/Kconfig
index f36dcf02a8ea..ca3f13d7e0cc 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -1163,6 +1163,21 @@ config CMD_SHA256SUM
 
 	  Calculate a SHA256 digest over a FILE or a memory area.
 
+config CMD_BASE64
+	tristate
+	select BASE64
+	prompt "base64"
+	help
+	  base64 - base64 decode data
+
+	  Usage: base64 [-u] -d -o OUT IN
+
+	  Decode normal and URL base64 data
+
+	    -d		decode to base64 instead of encoding
+	    -o OUT	output file name
+	    -u		Use base64 URL character set
+
 config CMD_SHA384SUM
 	tristate
 	select COMPILE_HASH
diff --git a/commands/Makefile b/commands/Makefile
index c32727aba1b3..8c2749b5ebdd 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 
 obj-$(CONFIG_STDDEV)		+= stddev.o
+obj-$(CONFIG_CMD_BASE64)	+= base64.o
 obj-$(CONFIG_CMD_DIGEST)	+= digest.o
 obj-$(CONFIG_COMPILE_HASH)	+= hashsum.o
 obj-$(CONFIG_CMD_AVB_PVALUE)	+= avb_pvalue.o
diff --git a/commands/base64.c b/commands/base64.c
new file mode 100644
index 000000000000..a549f1e5231f
--- /dev/null
+++ b/commands/base64.c
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <common.h>
+#include <command.h>
+#include <fs.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <xfuncs.h>
+#include <malloc.h>
+#include <libfile.h>
+#include <getopt.h>
+#include <base64.h>
+
+static int do_base64(int argc, char *argv[])
+{
+	char *input, *output = NULL;
+	bool url = false, decode = false;
+	int opt, ret;
+	size_t bufsz, outbufsz;
+	void *inbuf = NULL, *outbuf = NULL;
+
+	while ((opt = getopt(argc, argv, "duo:")) > 0) {
+		switch(opt) {
+		case 'd':
+			decode = true;
+			break;
+		case 'u':
+			url = true;
+			break;
+		case 'o':
+			output = optarg;
+			break;
+		default:
+			return COMMAND_ERROR_USAGE;
+		}
+	}
+
+	argc -= optind;
+	argv += optind;
+
+	if (argc != 1 || !output)
+		return COMMAND_ERROR_USAGE;
+
+	input = argv[0];
+
+	inbuf = read_file(input, &bufsz);
+	if (!inbuf) {
+		ret = -errno;
+		goto out;
+	}
+
+	outbuf = malloc(bufsz);
+	if (!outbuf) {
+		ret = -errno;
+		goto out;
+	}
+
+	if (!decode) {
+		printf("encoding currently not supported\n");
+		ret = -ENOSYS;
+		goto out;
+	}
+
+	if (url)
+		outbufsz = decode_base64url(outbuf, bufsz, inbuf);
+	else
+		outbufsz = decode_base64(outbuf, bufsz, inbuf);
+
+	ret = write_file(output, outbuf, outbufsz);
+out:
+	free(inbuf);
+	free(outbuf);
+
+	return ret;
+}
+
+BAREBOX_CMD_HELP_START(base64)
+BAREBOX_CMD_HELP_TEXT("decode normal and URL base64 data")
+BAREBOX_CMD_HELP_TEXT("")
+BAREBOX_CMD_HELP_TEXT("Options:")
+BAREBOX_CMD_HELP_OPT ("-d",  "decode to base64 instead of encoding")
+BAREBOX_CMD_HELP_OPT ("-o OUT",  "output file name")
+BAREBOX_CMD_HELP_OPT ("-u",  "Use base64 URL character set")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(base64)
+	.cmd		= do_base64,
+	BAREBOX_CMD_DESC("base64 decode data")
+	BAREBOX_CMD_OPTS("[-u] -d -o OUT IN")
+	BAREBOX_CMD_GROUP(CMD_GRP_FILE)
+	BAREBOX_CMD_HELP(cmd_base64_help)
+BAREBOX_CMD_END
-- 
2.39.5




             reply	other threads:[~2025-04-10 11:18 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-10  9:57 Ahmad Fatoum [this message]
2025-04-11  7:21 ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250410095718.2258908-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox