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.fat...@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