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
This commit is contained in:
Piotr Pawel Stefaniak 2022-04-07 14:27:45 +02:00
parent 2ad786c63a
commit 540a99289b
5 changed files with 140 additions and 6 deletions

View File

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

View File

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

View File

@ -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);
}

View File

@ -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[])
{

View File

@ -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[])
{