sbase/uudecode.c

210 lines
4.9 KiB
C
Raw Normal View History

/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include "util.h"
#include "text.h"
2014-02-04 09:30:17 -05:00
static void uudecode(FILE *, FILE *);
static void checkheader(FILE *, const char *, const char *, mode_t *, char **);
static void checkmode(const char *, mode_t *);
static FILE *checkfile(const char *);
static void
usage(void)
{
eprintf("usage: %s [file]\n", argv0);
}
int
main(int argc, char *argv[])
{
FILE *fp, *nfp;
2014-02-04 09:30:17 -05:00
char *fname;
mode_t mode = 0;
ARGBEGIN {
case 'm':
eprintf("-m not implemented\n");
default:
usage();
} ARGEND;
if (argc > 1)
usage();
if (argc == 0) {
2014-02-04 09:30:17 -05:00
checkheader(stdin, "<stdin>", "begin ", &mode, &fname);
nfp = checkfile(fname);
if (nfp == NULL)
eprintf("fopen %s:", fname);
2014-02-04 09:30:17 -05:00
uudecode(stdin, nfp);
fclose(nfp);
} else {
if (!(fp = fopen(argv[0], "r")))
eprintf("fopen %s:", argv[0]);
2014-02-04 09:30:17 -05:00
checkheader(fp, argv[0], "begin ", &mode, &fname);
nfp = checkfile(fname);
if (nfp == NULL) {
fclose(fp);
eprintf("fopen %s:", fname);
}
2014-02-04 09:30:17 -05:00
uudecode(fp, nfp);
fclose(fp);
}
2014-02-04 09:30:17 -05:00
if (fchmod(fileno(nfp), mode) < 0)
eprintf("chmod %s:", fname);
return EXIT_SUCCESS;
}
static FILE *
checkfile(const char *fname)
{
struct stat st;
int ret;
2014-02-04 09:30:17 -05:00
if (strcmp(fname,"/dev/stdout") == 0)
return stdout;
ret = lstat(fname, &st);
2014-02-04 09:30:17 -05:00
/* if it is a new file, try to open it */
if (ret < 0 && errno == ENOENT)
2014-02-04 09:30:17 -05:00
goto tropen;
if (ret < 0) {
2014-02-04 09:30:17 -05:00
weprintf("lstat %s:", fname);
return NULL;
}
if (!S_ISREG(st.st_mode)) {
weprintf("for safety uudecode operates only on regular files and /dev/stdout\n");
return NULL;
}
tropen:
return fopen(fname,"w");
}
2014-02-04 09:30:17 -05:00
static void
checkheader(FILE *fp, const char *s, const char *header, mode_t *mode, char **fname)
{
char bufs[PATH_MAX + 11]; /* len header + mode + maxname */
2014-02-04 09:30:17 -05:00
char *p, *q;
size_t n;
2014-02-04 09:30:17 -05:00
if (fgets(bufs, sizeof(bufs), fp) == NULL) {
if (ferror(fp))
eprintf("%s: read error:", s);
else
eprintf("empty or null header string\n");
}
2014-02-04 09:30:17 -05:00
if ((p = strchr(bufs, '\n')) == NULL)
eprintf("header string too long or non-newline terminated file\n");
p = bufs;
2014-02-04 09:30:17 -05:00
if (strncmp(bufs, header, strlen(header)) != 0)
eprintf("malformed header prefix\n");
p += strlen(header);
2014-02-04 09:30:17 -05:00
if ((q = strchr(p, ' ')) == NULL)
eprintf("malformed mode string in header\n");
*q++ = '\0';
/* now mode should be null terminated, q points to fname */
checkmode(p,mode);
n = strlen(q);
while (n > 0 && (q[n-1] == '\n' || q[n-1] == '\r'))
q[--n] = '\0';
if (n > 0)
*fname = q;
}
2014-02-04 09:30:17 -05:00
static void
checkmode(const char *str,mode_t *validmode)
{
char *end;
int octal;
2014-02-04 09:30:17 -05:00
if (str == NULL || str == '\0')
2014-02-04 09:30:17 -05:00
eprintf("invalid mode\n");
octal = strtol(str, &end, 8);
if (*end == '\0') { /* successful conversion from a valid str */
if (octal >= 0 && octal <= 07777) {
if(octal & 04000) *validmode |= S_ISUID;
if(octal & 02000) *validmode |= S_ISGID;
if(octal & 01000) *validmode |= S_ISVTX;
if(octal & 00400) *validmode |= S_IRUSR;
if(octal & 00200) *validmode |= S_IWUSR;
if(octal & 00100) *validmode |= S_IXUSR;
if(octal & 00040) *validmode |= S_IRGRP;
if(octal & 00020) *validmode |= S_IWGRP;
if(octal & 00010) *validmode |= S_IXGRP;
if(octal & 00004) *validmode |= S_IROTH;
if(octal & 00002) *validmode |= S_IWOTH;
if(octal & 00001) *validmode |= S_IXOTH;
*validmode &= 07777;
}
}
}
2014-02-04 09:30:17 -05:00
static void
uudecode(FILE *fp, FILE *outfp)
{
char *bufb=NULL, *p,*nl;
size_t n=0;
int ch , i;
2014-02-04 09:30:17 -05:00
#define DEC(c) (((c) - ' ') & 077) /* single character decode */
#define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) )
#define OUT_OF_RANGE(c) do { \
2014-02-04 09:30:17 -05:00
eprintf("character %c out of range: [%d-%d]",(c), 1+' ',077+' '+1); \
} while (0)
while (afgets(&bufb,&n,fp)) {
p = bufb;
2014-02-04 09:30:17 -05:00
/* trim newlines */
if ((nl=strchr(bufb, '\n')) != NULL)
*nl = '\0';
2014-02-04 09:30:17 -05:00
else
eprintf("no newline found, aborting\n");
if ((i = DEC(*p)) <= 0) /* check for last line */
break;
for (++p; i > 0; p += 4, i -= 3) {
if (i >= 3) {
if (!(IS_DEC(*p) && IS_DEC(*(p + 1)) &&
IS_DEC(*(p + 2)) && IS_DEC(*(p + 3))))
OUT_OF_RANGE(*p);
ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
putc(ch, outfp);
ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
putc(ch, outfp);
ch = DEC(p[2]) << 6 | DEC(p[3]);
putc(ch, outfp);
} else {
if (i >= 1) {
if (!(IS_DEC(*p) && IS_DEC(*(p + 1))))
OUT_OF_RANGE(*p);
ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4;
putc(ch, outfp);
}
if (i >= 2) {
if (!(IS_DEC(*(p + 1)) &&
IS_DEC(*(p + 2))))
OUT_OF_RANGE(*p);
ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2;
putc(ch, outfp);
}
}
}
2014-02-04 09:30:17 -05:00
if (ferror(fp))
eprintf("read error:");
}
2014-02-04 09:30:17 -05:00
/* check for end or fail */
afgets(&bufb,&n,fp);
2014-02-04 09:30:17 -05:00
if (strnlen(bufb,3) < 3 || strncmp(bufb, "end", 3) != 0 || bufb[3] != '\n')
eprintf("valid uudecode footer \"end\" not found\n");
}