From e2471b32d60f51b890f122148089b479b9cfc13f Mon Sep 17 00:00:00 2001 From: edwin Date: Mon, 18 Oct 2010 05:44:11 +0000 Subject: "b64decode -r" did not handle arbitary breaks in base64 encoded data. White space should be accepted anywhere in a base64 encoded stream, not just after every chunk (4 characters). Test-scenario: VmVsb2NpdHkgUmV3YXJkcw== and VmVsb2NpdHkgUmV3YXJkcw == should both produce "Velocity Rewards" PR: bin/124739 Submitted by: Mark Andrews MFC after: 2 weeks --- usr.bin/uudecode/uudecode.c | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/usr.bin/uudecode/uudecode.c b/usr.bin/uudecode/uudecode.c index 9217dd9..58c0878 100644 --- a/usr.bin/uudecode/uudecode.c +++ b/usr.bin/uudecode/uudecode.c @@ -57,6 +57,7 @@ __FBSDID("$FreeBSD$"); #include +#include #include #include #include @@ -413,15 +414,40 @@ uu_decode(void) static int base64_decode(void) { - int n; - char inbuf[MAXPATHLEN + 1]; + int n, count, count4; + char inbuf[MAXPATHLEN + 1], *p; unsigned char outbuf[MAXPATHLEN * 4]; + char leftover[MAXPATHLEN + 1]; + leftover[0] = '\0'; for (;;) { - switch (getline(inbuf, sizeof(inbuf))) { - case 0: return (0); - case 1: return (1); + strcpy(inbuf, leftover); + switch (getline(inbuf + strlen(inbuf), + sizeof(inbuf) - strlen(inbuf))) { + case 0: + return (0); + case 1: + return (1); } + + count = 0; + count4 = -1; + p = inbuf; + while (*p != '\0') { + /* + * Base64 encoded strings have the following + * characters in them: A-Z, a-z, 0-9 and +, / and = + */ + if (isalnum(*p) || *p == '+' || *p == '/' || *p == '=') + count++; + if (count % 4 == 0) + count4 = p - inbuf; + p++; + } + + strcpy(leftover, inbuf + count4 + 1); + inbuf[count4 + 1] = 0; + n = b64_pton(inbuf, outbuf, sizeof(outbuf)); if (n < 0) -- cgit v1.1