Add a relaxed mode for handling raw input which is missing the

initial and possibly the final framing lines. This is particularly
handy for dealing with broken files or fragments of MIME messages.
This commit is contained in:
Tony Finch 2003-02-27 03:28:29 +00:00
parent 1d56863dd2
commit bff465ff80

View File

@ -61,6 +61,7 @@ __FBSDID("$FreeBSD$");
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <pwd.h>
#include <resolv.h>
#include <stdio.h>
@ -70,7 +71,7 @@ __FBSDID("$FreeBSD$");
static const char *infile, *outfile;
static FILE *infp, *outfp;
static int cflag, iflag, oflag, pflag, sflag;
static int base64, cflag, iflag, oflag, pflag, rflag, sflag;
static void usage(void);
static int decode(void);
@ -83,18 +84,24 @@ main(int argc, char *argv[])
{
int rval, ch;
while ((ch = getopt(argc, argv, "cio:ps")) != -1) {
if (strcmp(basename(argv[0]), "b64decode") == 0)
base64 = 1;
while ((ch = getopt(argc, argv, "cimo:prs")) != -1) {
switch(ch) {
case 'c':
if (oflag)
if (oflag || rflag)
usage();
cflag = 1; /* multiple uudecode'd files */
break;
case 'i':
iflag = 1; /* ask before override files */
break;
case 'm':
base64 = 1;
break;
case 'o':
if (cflag || pflag || sflag)
if (cflag || pflag || rflag || sflag)
usage();
oflag = 1; /* output to the specified file */
sflag = 1; /* do not strip pathnames for output */
@ -105,6 +112,11 @@ main(int argc, char *argv[])
usage();
pflag = 1; /* print output to stdout */
break;
case 'r':
if (cflag || oflag)
usage();
rflag = 1; /* decode raw data */
break;
case 's':
if (oflag)
usage();
@ -142,6 +154,15 @@ decode(void)
{
int r, v;
if (rflag) {
/* relaxed alternative to decode2() */
outfile = "/dev/stdout";
outfp = stdout;
if (base64)
return (base64_decode());
else
return (uu_decode());
}
v = decode2();
if (v == EOF) {
warnx("%s: missing or bad \"begin\" line", infile);
@ -158,7 +179,7 @@ decode(void)
static int
decode2(void)
{
int base64, flags, fd, mode;
int flags, fd, mode;
size_t n, m;
char *p, *q;
void *handle;
@ -292,7 +313,9 @@ uu_decode(void)
/* for each input line */
for (;;) {
if (fgets(p = buf, sizeof(buf), infp) == NULL) {
warnx("%s: short file", infile);
if (rflag)
return (0);
warnx("%s: %s: short file", infile, outfile);
return (1);
}
@ -355,7 +378,7 @@ uu_decode(void)
return (1);
}
if (fclose(outfp) != 0) {
warnx("%s: %s", infile, outfile);
warn("%s: %s", infile, outfile);
return (1);
}
return (0);
@ -370,14 +393,16 @@ base64_decode(void)
for (;;) {
if (fgets(inbuf, sizeof(inbuf), infp) == NULL) {
warnx("%s: short file", infile);
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) {
warnx("%s: %s", infile, outfile);
warn("%s: %s", infile, outfile);
return (1);
}
return (0);
@ -398,9 +423,9 @@ static void
usage(void)
{
(void)fprintf(stderr,
"usage: uudecode [-cips] [file ...]\n"
"usage: uudecode [-cimprs] [file ...]\n"
" uudecode [-i] -o output_file [file]\n"
" b64decode [-cips] [file ...]\n"
" b64decode [-cimprs] [file ...]\n"
" b64decode [-i] -o output_file [file]\n");
exit(1);
}