From 860333542f7f24fc0ae9092753a92db396e976f6 Mon Sep 17 00:00:00 2001 From: fanf Date: Thu, 27 Feb 2003 05:59:18 +0000 Subject: Pull some common uu/b64 code out into shared functions. Move some end-of-file checks out of the inner base64 loop, and remove the trailing whitespace stripper. The latter was added in rev 1.23 but the actual fix for the problem was in 1.24 -- b64_pton doesn't mind extra whitespace. (However there's a bogus comment in OpenSSH's uuencode.c that also mentions problems with trailing whitespace and b64_pton, but their real problem is the comment field in the key file.) --- usr.bin/uudecode/uudecode.c | 80 +++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 36 deletions(-) (limited to 'usr.bin/uudecode') diff --git a/usr.bin/uudecode/uudecode.c b/usr.bin/uudecode/uudecode.c index b4697ee..c28208a8 100644 --- a/usr.bin/uudecode/uudecode.c +++ b/usr.bin/uudecode/uudecode.c @@ -304,6 +304,35 @@ decode2(void) } static int +getline(char *buf, size_t size) +{ + if (fgets(buf, size, infp) != NULL) + return (2); + if (!rflag) + return (0); + warnx("%s: %s: short file", infile, outfile); + return (1); +} + +static int +checkend(const char *ptr, const char *end, const char *msg) +{ + size_t n; + + n = strlen(end); + if (strncmp(ptr, end, n) != 0 || + strspn(ptr + n, " \t\r\n") != strlen(ptr + n)) { + warnx("%s: %s: %s", infile, outfile, msg); + return (1); + } + if (fclose(outfp) != 0) { + warn("%s: %s", infile, outfile); + return (1); + } + return (0); +} + +static int uu_decode(void) { int i, ch; @@ -312,11 +341,9 @@ uu_decode(void) /* for each input line */ for (;;) { - if (fgets(p = buf, sizeof(buf), infp) == NULL) { - if (rflag) - return (0); - warnx("%s: %s: short file", infile, outfile); - return (1); + switch (getline(buf, sizeof(buf))) { + case 0: return (0); + case 1: return (1); } #define DEC(c) (((c) - ' ') & 077) /* single character decode */ @@ -332,6 +359,7 @@ uu_decode(void) * `i' is used to avoid writing out all the characters * at the end of the file. */ + p = buf; if ((i = DEC(*p)) <= 0) break; for (++p; i > 0; p += 4, i -= 3) @@ -371,17 +399,11 @@ uu_decode(void) } } } - if (fgets(buf, sizeof(buf), infp) == NULL || - (strcmp(buf, "end") && strcmp(buf, "end\n") && - strcmp(buf, "end\r\n"))) { - warnx("%s: no \"end\" line", infile); - return (1); - } - if (fclose(outfp) != 0) { - warn("%s: %s", infile, outfile); - return (1); + switch (getline(buf, sizeof(buf))) { + case 0: return (0); + case 1: return (1); + default: return (checkend(buf, "end", "no \"end\" line")); } - return (0); } static int @@ -392,31 +414,17 @@ base64_decode(void) unsigned char outbuf[MAXPATHLEN * 4]; for (;;) { - if (fgets(inbuf, sizeof(inbuf), infp) == NULL) { - if (rflag) - return (0); - warnx("%s: %s: short file", infile, outfile); - return (1); - } - if (strcmp(inbuf, "====") == 0 || - strcmp(inbuf, "====\n") == 0 || - strcmp(inbuf, "====\r\n") == 0) { - if (fclose(outfp) != 0) { - warn("%s: %s", infile, outfile); - return (1); - } - return (0); + switch (getline(inbuf, sizeof(inbuf))) { + case 0: return (0); + case 1: return (1); } - n = strlen(inbuf); - while (n > 0 && (inbuf[n-1] == '\n' || inbuf[n-1] == '\r')) - inbuf[--n] = '\0'; n = b64_pton(inbuf, outbuf, sizeof(outbuf)); - if (n < 0) { - warnx("%s: %s: error decoding base64 input stream", infile, outfile); - return (1); - } + if (n < 0) + break; fwrite(outbuf, 1, n, outfp); } + return (checkend(inbuf, "====", + "error decoding base64 input stream")); } static void -- cgit v1.1