Simplify uudecode(1) and fix some bugs

This commit is contained in:
sin 2014-02-04 14:30:17 +00:00
parent 7008d751b2
commit 4d8c3d4dc2

View File

@ -10,9 +10,9 @@
#include "util.h" #include "util.h"
#include "text.h" #include "text.h"
static int uudecode(FILE *, FILE*); static void uudecode(FILE *, FILE *);
static int checkheader(FILE *, const char *, mode_t *, char **, char **); static void checkheader(FILE *, const char *, const char *, mode_t *, char **);
static int checkmode(const char *, mode_t *); static void checkmode(const char *, mode_t *);
static FILE *checkfile(const char *); static FILE *checkfile(const char *);
static void static void
@ -25,9 +25,8 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
FILE *fp, *nfp; FILE *fp, *nfp;
char *fname, *headerr; char *fname;
mode_t mode = 0; mode_t mode = 0;
int chmodtest;
ARGBEGIN { ARGBEGIN {
case 'm': case 'm':
@ -40,44 +39,28 @@ main(int argc, char *argv[])
usage(); usage();
if (argc == 0) { if (argc == 0) {
if (checkheader(stdin, "begin ", &mode, &fname, &headerr) < 0) checkheader(stdin, "<stdin>", "begin ", &mode, &fname);
eprintf("%s\n",headerr);
nfp = checkfile(fname); nfp = checkfile(fname);
if (nfp == NULL) if (nfp == NULL)
eprintf("fopen %s:", fname); eprintf("fopen %s:", fname);
uudecode(stdin, nfp);
if (uudecode(stdin, nfp) < 0) fclose(nfp);
goto fail;
} else { } else {
if (!(fp = fopen(argv[0], "r"))) if (!(fp = fopen(argv[0], "r")))
eprintf("fopen %s:", argv[0]); eprintf("fopen %s:", argv[0]);
checkheader(fp, argv[0], "begin ", &mode, &fname);
if (checkheader(fp, "begin ", &mode, &fname, &headerr) < 0) {
fclose(fp);
eprintf("%s\n",headerr);
}
nfp = checkfile(fname); nfp = checkfile(fname);
if (nfp == NULL) { if (nfp == NULL) {
fclose(fp); fclose(fp);
eprintf("fopen %s:", fname); eprintf("fopen %s:", fname);
} }
if (uudecode(fp, nfp) < 0) { uudecode(fp, nfp);
fclose(fp); fclose(fp);
goto fail;
} }
} if (fchmod(fileno(nfp), mode) < 0)
chmodtest=fchmod(fileno(nfp),mode); eprintf("chmod %s:", fname);
fclose(fp);
fclose(nfp);
if (chmodtest == -1)
eprintf("chmod %s:%s",fname,strerror(errno));
return EXIT_SUCCESS; return EXIT_SUCCESS;
fail:
fclose(nfp);
return EXIT_FAILURE;
} }
static FILE * static FILE *
@ -85,18 +68,17 @@ checkfile(const char *fname)
{ {
struct stat st; struct stat st;
int ret; int ret;
if (strcmp(fname,"/dev/stdout") == 0) if (strcmp(fname,"/dev/stdout") == 0)
return stdout; return stdout;
ret = lstat(fname, &st); ret = lstat(fname, &st);
/* if it is a new file, try to open it */
if (ret < 0 && errno == ENOENT) if (ret < 0 && errno == ENOENT)
goto tropen; /* new file, try to open it */ goto tropen;
if (ret < 0) { if (ret < 0) {
weprintf("stat: %s:", fname); weprintf("lstat %s:", fname);
return NULL; return NULL;
} }
if (!S_ISREG(st.st_mode)) { if (!S_ISREG(st.st_mode)) {
weprintf("for safety uudecode operates only on regular files and /dev/stdout\n"); weprintf("for safety uudecode operates only on regular files and /dev/stdout\n");
return NULL; return NULL;
@ -105,54 +87,45 @@ tropen:
return fopen(fname,"w"); return fopen(fname,"w");
} }
static int static void
checkheader(FILE *fp, const char *header, mode_t *mode, char **fname, char **headerr) checkheader(FILE *fp, const char *s, const char *header, mode_t *mode, char **fname)
{ {
char bufs[PATH_MAX + 11]; /* len header + mode + maxname */ char bufs[PATH_MAX + 11]; /* len header + mode + maxname */
char *p, *q, *bufp = NULL; char *p, *q;
size_t n; size_t n;
fgets(bufs, sizeof(bufs), fp);
if (bufp == NULL || bufp == '\0') { /* empty or null str */ if (fgets(bufs, sizeof(bufs), fp) == NULL) {
*headerr = "empty or null header string"; if (ferror(fp))
return -1; eprintf("%s: read error:", s);
} else
if ((p = strchr(bufs, '\n')) == NULL) { /* line too long or last line on file */ eprintf("empty or null header string\n");
*headerr = "header string too long or non-newline terminated file";
return -1;
} }
if ((p = strchr(bufs, '\n')) == NULL)
eprintf("header string too long or non-newline terminated file\n");
p = bufs; p = bufs;
if (strncmp(bufs, header, strlen(header)) != 0) { if (strncmp(bufs, header, strlen(header)) != 0)
*headerr = "malformed header prefix"; eprintf("malformed header prefix\n");
return -1;
}
p += strlen(header); p += strlen(header);
if ((q = strchr(p, ' ')) == NULL) { /* malformed mode */ if ((q = strchr(p, ' ')) == NULL)
*headerr = "malformed mode string in header"; eprintf("malformed mode string in header\n");
return -1; *q++ = '\0';
} /* now mode should be null terminated, q points to fname */
*q++ = '\0'; /* now mode should be null terminated,q points to fname */ checkmode(p,mode);
if (checkmode(p,mode) < 0) { /* error from checkmode */
*headerr = "invalid mode in header";
return -1;
}
n = strlen(q); n = strlen(q);
while (n > 0 && (q[n-1] == '\n' || q[n-1] == '\r')) while (n > 0 && (q[n-1] == '\n' || q[n-1] == '\r'))
q[--n] = '\0'; q[--n] = '\0';
if (n > 0) if (n > 0)
*fname = q; *fname = q;
return 1;
} }
static void
static int
checkmode(const char *str,mode_t *validmode) checkmode(const char *str,mode_t *validmode)
{ {
char *end; char *end;
int octal; int octal;
if (str == NULL || str == '\0') if (str == NULL || str == '\0')
return -1; /* null str */ eprintf("invalid mode\n");
octal = strtol(str, &end, 8); octal = strtol(str, &end, 8);
if (*end == '\0') { /* successful conversion from a valid str */ if (*end == '\0') { /* successful conversion from a valid str */
@ -170,36 +143,32 @@ checkmode(const char *str,mode_t *validmode)
if(octal & 00002) *validmode |= S_IWOTH; if(octal & 00002) *validmode |= S_IWOTH;
if(octal & 00001) *validmode |= S_IXOTH; if(octal & 00001) *validmode |= S_IXOTH;
*validmode &= 07777; *validmode &= 07777;
return 1;
} }
} }
return -1; /* malformed mode */
} }
static int static void
uudecode(FILE *fp, FILE *outfp) uudecode(FILE *fp, FILE *outfp)
{ {
char *bufb=NULL, *p,*nl; char *bufb=NULL, *p,*nl;
size_t n=0; size_t n=0;
int ch , i; int ch , i;
#define DEC(c) (((c) - ' ') & 077) /* single character decode */ #define DEC(c) (((c) - ' ') & 077) /* single character decode */
#define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) ) #define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) )
#define OUT_OF_RANGE(c) do { \ #define OUT_OF_RANGE(c) do { \
weprintf("character %c out of range: [%d-%d]",(c), 1+' ',077+' '+1); \ eprintf("character %c out of range: [%d-%d]",(c), 1+' ',077+' '+1); \
return -1; \
} while (0) } while (0)
while (afgets(&bufb,&n,fp)) { while (afgets(&bufb,&n,fp)) {
p = bufb; p = bufb;
if ((nl=strchr(bufb, '\n')) != NULL) { /* trim newlines */ /* trim newlines */
if ((nl=strchr(bufb, '\n')) != NULL)
*nl = '\0'; *nl = '\0';
} else { else
weprintf("no newline found, aborting\n"); eprintf("no newline found, aborting\n");
return -1;
}
if ((i = DEC(*p)) <= 0) /* check for last line */ if ((i = DEC(*p)) <= 0) /* check for last line */
break; break;
for (++p; i > 0; p += 4, i -= 3) { for (++p; i > 0; p += 4, i -= 3) {
if (i >= 3) { if (i >= 3) {
if (!(IS_DEC(*p) && IS_DEC(*(p + 1)) && if (!(IS_DEC(*p) && IS_DEC(*(p + 1)) &&
@ -230,16 +199,11 @@ uudecode(FILE *fp, FILE *outfp)
} }
} }
} }
if (ferror(fp)) { if (ferror(fp))
weprintf("read error"); eprintf("read error:");
return -1;
} }
} /* check for end or fail */
/* check for end or fail*/
afgets(&bufb,&n,fp); afgets(&bufb,&n,fp);
if (strnlen(bufb,3) < 3 || strncmp(bufb, "end", 3) != 0 || bufb[3] != '\n') { if (strnlen(bufb,3) < 3 || strncmp(bufb, "end", 3) != 0 || bufb[3] != '\n')
weprintf("valid uudecode footer \"end\" not found\n"); eprintf("valid uudecode footer \"end\" not found\n");
return -1;
}
return 1;
} }