Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 18 Apr 2022 11:56:28 GMT
From:      Piotr Pawel Stefaniak <pstef@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 540a99289bb1 - main - Add the base64 command using b64encode and b64decode
Message-ID:  <202204181156.23IBuS6K007345@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by pstef:

URL: https://cgit.FreeBSD.org/src/commit/?id=540a99289bb142cba89891d715f6ac3e1f7c7cb1

commit 540a99289bb142cba89891d715f6ac3e1f7c7cb1
Author:     Piotr Pawel Stefaniak <pstef@FreeBSD.org>
AuthorDate: 2022-04-07 12:27:45 +0000
Commit:     Piotr Pawel Stefaniak <pstef@FreeBSD.org>
CommitDate: 2022-04-18 11:53:59 +0000

    Add the base64 command using b64encode and b64decode
    
    Various scripts expect the base64 command to be present. We've had the
    code and it's been exposed as b64encode/b64decode, but it wasn't widely
    known under these names and the syntax is much different.
    
    Reviewed by:    delphij
    Differential Revision:  https://reviews.freebsd.org/D32945
---
 usr.bin/bintrans/Makefile   |  4 ++-
 usr.bin/bintrans/bintrans.1 | 31 ++++++++++++++++++
 usr.bin/bintrans/bintrans.c | 77 ++++++++++++++++++++++++++++++++++++++++++---
 usr.bin/bintrans/uudecode.c | 18 +++++++++++
 usr.bin/bintrans/uuencode.c | 16 ++++++++++
 5 files changed, 140 insertions(+), 6 deletions(-)

diff --git a/usr.bin/bintrans/Makefile b/usr.bin/bintrans/Makefile
index ef9686aa290f..49acec470d76 100644
--- a/usr.bin/bintrans/Makefile
+++ b/usr.bin/bintrans/Makefile
@@ -10,11 +10,13 @@ LINKS+=	${BINDIR}/bintrans ${BINDIR}/uuencode
 LINKS+=	${BINDIR}/bintrans ${BINDIR}/b64encode
 LINKS+=	${BINDIR}/bintrans ${BINDIR}/uudecode
 LINKS+=	${BINDIR}/bintrans ${BINDIR}/b64decode
+LINKS+=	${BINDIR}/bintrans ${BINDIR}/base64
 MLINKS=	bintrans.1 uudecode.1 \
 	bintrans.1 uuencode.1 \
 	uuencode.format.5 uuencode.5 \
 	bintrans.1 b64encode.1 \
-	b64encode.1 b64decode.1
+	b64encode.1 b64decode.1 \
+	b64encode.1 base64.1
 
 HAS_TESTS=
 SUBDIR.${MK_TESTS}+= tests
diff --git a/usr.bin/bintrans/bintrans.1 b/usr.bin/bintrans/bintrans.1
index cdc819aa7a18..f489eb6fc258 100644
--- a/usr.bin/bintrans/bintrans.1
+++ b/usr.bin/bintrans/bintrans.1
@@ -67,6 +67,10 @@
 .Op Fl i
 .Fl o Ar output_file
 .Op Ar file
+.Nm base64
+.Op Fl d
+.Op Fl w Ar column
+.Op Ar file
 .Sh DESCRIPTION
 The
 .Nm uuencode
@@ -92,6 +96,33 @@ with the
 flag specified.
 .Pp
 The
+.Nm base64
+utility acts as a base64 decoder when passed the
+.Fl -decode
+.Po or
+.Fl d
+.Pc
+flag and as a base64 encoder otherwise.
+As a decoder it only accepts raw base64 input
+and as an encoder it does not produce the framing lines.
+.Nm base64
+reads standard input or
+.Ar file
+if it is provided and writes to standard output.
+Options
+.Fl -wrap
+.Po or
+.Fl w
+.Pc
+and
+.Fl -ignore-garbage
+.Po or
+.Fl i
+.Pc
+are accepted for compatibility with GNU base64,
+but the latter is unimplemented and silently ignored.
+.Pp
+The
 .Nm uuencode
 utility reads
 .Ar file
diff --git a/usr.bin/bintrans/bintrans.c b/usr.bin/bintrans/bintrans.c
index e158dd2c1b5c..435f982ac520 100644
--- a/usr.bin/bintrans/bintrans.c
+++ b/usr.bin/bintrans/bintrans.c
@@ -26,7 +26,9 @@
  */
 
 #include <sys/param.h>
+#include <getopt.h>
 #include <libgen.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -34,11 +36,16 @@
 
 extern int	main_decode(int, char *[]);
 extern int	main_encode(int, char *[]);
+extern int	main_base64_decode(const char *);
+extern int	main_base64_encode(const char *, const char *);
 
 static int	search(const char *const);
+static void	usage_base64(bool);
+static void	version_base64(void);
+static void	base64_encode_or_decode(int, char *[]);
 
 enum coders {
-	uuencode, uudecode, b64encode, b64decode
+	uuencode, uudecode, b64encode, b64decode, base64
 };
 
 int
@@ -61,11 +68,15 @@ main(int argc, char *argv[])
 	case b64decode:
 		main_decode(argc, argv);
 		break;
+	case base64:
+		base64_encode_or_decode(argc, argv);
+		break;
 	default:
 		(void)fprintf(stderr,
-		    "usage: %s <uuencode | uudecode> ...\n"
-		    "       %s <b64encode | b64decode> ...\n",
-		    progname, progname);
+		    "usage: %1$s <uuencode | uudecode> ...\n"
+		    "       %1$s <b64encode | b64decode> ...\n"
+		    "       %1$s <base64> ...\n",
+		    progname);
 		exit(EX_USAGE);
 	}
 }
@@ -78,7 +89,8 @@ search(const char *const progname)
 		DESIGNATE(uuencode),
 		DESIGNATE(uudecode),
 		DESIGNATE(b64encode),
-		DESIGNATE(b64decode)
+		DESIGNATE(b64decode),
+		DESIGNATE(base64)
 	};
 
 	for (size_t i = 0; i < nitems(known); i++)
@@ -86,3 +98,58 @@ search(const char *const progname)
 			return ((int)i);
 	return (-1);
 }
+
+static void
+usage_base64(bool failure)
+{
+	(void)fputs("usage: base64 [-w col | --wrap=col] "
+	    "[-d | --decode] [FILE]\n"
+	    "       base64 --help\n"
+	    "       base64 --version\n", stderr);
+	exit(failure ? EXIT_FAILURE : EXIT_SUCCESS);
+}
+
+static void
+version_base64(void)
+{
+	(void)fputs("FreeBSD base64\n", stderr);
+	exit(EXIT_SUCCESS);
+}
+
+static void
+base64_encode_or_decode(int argc, char *argv[])
+{
+	int ch;
+	bool decode = false;
+	const char *w = NULL;
+	enum { HELP, VERSION };
+	static const struct option opts[] =
+	{
+		{"decode",	no_argument,		NULL, 'd'},
+		{"ignore-garbage",no_argument,		NULL, 'i'},
+		{"wrap",	required_argument,	NULL, 'w'},
+		{"help",	no_argument,		NULL, HELP},
+		{"version",	no_argument,		NULL, VERSION},
+		{NULL,		no_argument,		NULL, 0}
+	};
+
+	while ((ch = getopt_long(argc, argv, "diw:", opts, NULL)) != -1)
+		switch (ch) {
+		case 'd':
+			decode = true;
+			break;
+		case 'w':
+			w = optarg;
+			break;
+		case VERSION:
+			version_base64();
+		case HELP:
+		default:
+			usage_base64(ch == '?');
+		}
+
+	if (decode)
+		main_base64_decode(argv[optind]);
+	else
+		main_base64_encode(argv[optind], w);
+}
diff --git a/usr.bin/bintrans/uudecode.c b/usr.bin/bintrans/uudecode.c
index cd97f7406cc2..29e31d474aae 100644
--- a/usr.bin/bintrans/uudecode.c
+++ b/usr.bin/bintrans/uudecode.c
@@ -69,6 +69,7 @@ __FBSDID("$FreeBSD$");
 #include <unistd.h>
 
 extern int main_decode(int, char *[]);
+extern int main_base64_decode(const char *);
 
 static const char *infile, *outfile;
 static FILE *infp, *outfp;
@@ -80,6 +81,23 @@ static int	decode2(void);
 static int	uu_decode(void);
 static int	base64_decode(void);
 
+int
+main_base64_decode(const char *in)
+{
+	base64 = 1;
+	rflag = 1;
+	if (in != NULL) {
+		infile = in;
+		infp = fopen(infile, "r");
+		if (infp == NULL)
+			err(1, "%s", in);
+	} else {
+		infile = "stdin";
+		infp = stdin;
+	}
+	exit(decode());
+}
+
 int
 main_decode(int argc, char *argv[])
 {
diff --git a/usr.bin/bintrans/uuencode.c b/usr.bin/bintrans/uuencode.c
index 4837d3310578..cb3669dd55d0 100644
--- a/usr.bin/bintrans/uuencode.c
+++ b/usr.bin/bintrans/uuencode.c
@@ -65,6 +65,7 @@ __FBSDID("$FreeBSD$");
 #include <unistd.h>
 
 extern int main_encode(int, char *[]);
+extern int main_base64_encode(const char *, const char *);
 
 static void encode(void);
 static void base64_encode(void);
@@ -77,6 +78,21 @@ static bool raw;
 static char **av;
 static int columns = 76;
 
+int
+main_base64_encode(const char *in, const char *w)
+{
+	raw = 1;
+	if (in != NULL && freopen(in, "r", stdin) == NULL)
+		err(1, "%s", in);
+	output = stdout;
+	if (w != NULL)
+		columns = arg_to_col(w);
+	base64_encode();
+	if (ferror(output))
+		errx(1, "write error");
+	exit(0);
+}
+
 int
 main_encode(int argc, char *argv[])
 {



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202204181156.23IBuS6K007345>