From ce51fff754f20521ce32eaf888c654ec967b9781 Mon Sep 17 00:00:00 2001 From: jmallett Date: Tue, 5 Mar 2002 03:27:47 +0000 Subject: Add base64 support to uuencode(1) and uudecode(1), as specified by SUSv3. Add `-o' option to uuencode(1) to pipe the uuencoded output to an arbitrary file, instead of just stdout. Reviewed by: -standards, mike Approved by: mike --- usr.bin/uudecode/uudecode.c | 49 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) (limited to 'usr.bin/uudecode/uudecode.c') diff --git a/usr.bin/uudecode/uudecode.c b/usr.bin/uudecode/uudecode.c index 3630427..b0cc073 100644 --- a/usr.bin/uudecode/uudecode.c +++ b/usr.bin/uudecode/uudecode.c @@ -52,11 +52,14 @@ static const char rcsid[] = * used with uuencode. */ #include +#include #include +#include + #include -#include #include +#include #include #include #include @@ -69,6 +72,7 @@ int cflag, iflag, oflag, pflag, sflag; static void usage __P((void)); int decode __P((void)); int decode2 __P((int)); +void base64_decode __P((const char *)); int main(argc, argv) @@ -155,11 +159,11 @@ decode2(flag) struct passwd *pw; register int n; register char ch, *p; - int ignore, mode, n1; + int base64, ignore, mode, n1; char buf[MAXPATHLEN]; char buffn[MAXPATHLEN]; /* file name buffer */ - ignore = 0; + base64 = ignore = 0; /* search for header line */ do { if (!fgets(buf, sizeof(buf), stdin)) { @@ -169,17 +173,26 @@ decode2(flag) warnx("%s: no \"begin\" line", filename); return(1); } - } while (strncmp(buf, "begin ", 6) || - fnmatch("begin [0-7]* *", buf, 0)); + } while (strncmp(buf, "begin", 5) != 0); + + if (strncmp(buf, "begin-base64", 12) == 0) + base64 = 1; if (oflag) { - (void)sscanf(buf, "begin %o ", &mode); + if (base64) + (void)sscanf(buf, "begin-base64 %o ", &mode); + else + (void)sscanf(buf, "begin %o ", &mode); if (strlcpy(buf, outfile, sizeof(buf)) >= sizeof(buf)) { warnx("%s: filename too long", outfile); return (1); } - } else - (void)sscanf(buf, "begin %o %[^\n\r]", &mode, buf); + } else { + if (base64) + (void)sscanf(buf, "begin-base64 %o %[^\n\r]", &mode, buf); + else + (void)sscanf(buf, "begin %o %[^\n\r]", &mode, buf); + } if (!sflag && !pflag) { strncpy(buffn, buf, sizeof(buffn)); @@ -230,11 +243,18 @@ decode2(flag) strcpy(buffn, buf); /* store file name from header line */ /* for each input line */ +next: for (;;) { if (!fgets(p = buf, sizeof(buf), stdin)) { warnx("%s: short file", filename); return(1); } + if (base64) { + if (strncmp(buf, "====", 4) == 0) + return (0); + base64_decode(buf); + goto next; + } #define DEC(c) (((c) - ' ') & 077) /* single character decode */ #define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) ) /* #define IS_DEC(c) (1) */ @@ -304,6 +324,19 @@ if (!ignore) \ return(0); } +void +base64_decode(stream) + const char *stream; +{ + unsigned char out[MAXPATHLEN * 4]; + int rv; + + rv = b64_pton(stream, out, (sizeof(out) / sizeof(out[0]))); + if (rv == -1) + errx(1, "b64_pton: error decoding base64 input stream"); + printf("%s", out); +} + static void usage() { -- cgit v1.1