diff --git a/usr.bin/bintrans/Makefile b/usr.bin/bintrans/Makefile --- a/usr.bin/bintrans/Makefile +++ b/usr.bin/bintrans/Makefile @@ -10,11 +10,13 @@ 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 --- 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 @@ 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 --- a/usr.bin/bintrans/bintrans.c +++ b/usr.bin/bintrans/bintrans.c @@ -26,7 +26,9 @@ */ #include +#include #include +#include #include #include #include @@ -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 @@ case b64decode: main_decode(argc, argv); break; + case base64: + base64_encode_or_decode(argc, argv); + break; default: (void)fprintf(stderr, - "usage: %s ...\n" - " %s ...\n", - progname, progname); + "usage: %1$s ...\n" + " %1$s ...\n" + " %1$s ...\n", + progname); exit(EX_USAGE); } } @@ -78,7 +89,8 @@ 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 @@ 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 --- a/usr.bin/bintrans/uudecode.c +++ b/usr.bin/bintrans/uudecode.c @@ -69,6 +69,7 @@ #include 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 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 --- a/usr.bin/bintrans/uuencode.c +++ b/usr.bin/bintrans/uuencode.c @@ -65,6 +65,7 @@ #include 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 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[]) {