2014-02-02 14:08:22 -05:00
|
|
|
/* 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 *);
|
2014-02-04 10:05:48 -05:00
|
|
|
static void parseheader(FILE *, const char *, const char *, mode_t *, char **);
|
|
|
|
static FILE *parsefile(const char *);
|
2014-02-02 14:08:22 -05:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: %s [file]\n", argv0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2014-02-04 10:14:12 -05:00
|
|
|
FILE *fp = NULL, *nfp = NULL;
|
2014-02-04 09:30:17 -05:00
|
|
|
char *fname;
|
2014-02-02 14:08:22 -05:00
|
|
|
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 10:05:48 -05:00
|
|
|
parseheader(stdin, "<stdin>", "begin ", &mode, &fname);
|
|
|
|
if ((nfp = parsefile(fname)) == NULL)
|
2014-02-02 14:08:22 -05:00
|
|
|
eprintf("fopen %s:", fname);
|
2014-02-04 09:30:17 -05:00
|
|
|
uudecode(stdin, nfp);
|
2014-02-02 14:08:22 -05:00
|
|
|
} else {
|
2014-02-04 09:44:43 -05:00
|
|
|
if ((fp = fopen(argv[0], "r")) == NULL)
|
2014-02-02 14:08:22 -05:00
|
|
|
eprintf("fopen %s:", argv[0]);
|
2014-02-04 10:05:48 -05:00
|
|
|
parseheader(fp, argv[0], "begin ", &mode, &fname);
|
|
|
|
if ((nfp = parsefile(fname)) == NULL)
|
2014-02-02 14:08:22 -05:00
|
|
|
eprintf("fopen %s:", fname);
|
2014-02-04 09:30:17 -05:00
|
|
|
uudecode(fp, nfp);
|
2014-02-02 14:08:22 -05:00
|
|
|
}
|
2014-02-04 10:14:12 -05:00
|
|
|
|
2014-02-04 10:05:31 -05:00
|
|
|
if (chmod(fname, mode) < 0)
|
2014-02-04 09:30:17 -05:00
|
|
|
eprintf("chmod %s:", fname);
|
2014-02-04 10:14:12 -05:00
|
|
|
if (fp)
|
|
|
|
fclose(fp);
|
|
|
|
if (nfp)
|
|
|
|
fclose(nfp);
|
2014-02-02 14:08:22 -05:00
|
|
|
|
2014-10-02 18:46:04 -04:00
|
|
|
return 0;
|
2014-02-02 14:08:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static FILE *
|
2014-02-04 10:05:48 -05:00
|
|
|
parsefile(const char *fname)
|
2014-02-02 14:08:22 -05:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
int ret;
|
2014-02-04 09:30:17 -05:00
|
|
|
|
2014-02-04 09:44:43 -05:00
|
|
|
if (strcmp(fname, "/dev/stdout") == 0)
|
2014-02-02 14:08:22 -05:00
|
|
|
return stdout;
|
|
|
|
ret = lstat(fname, &st);
|
2014-02-04 09:30:17 -05:00
|
|
|
/* if it is a new file, try to open it */
|
2014-02-02 14:08:22 -05:00
|
|
|
if (ret < 0 && errno == ENOENT)
|
2014-02-04 09:30:17 -05:00
|
|
|
goto tropen;
|
2014-02-02 14:08:22 -05:00
|
|
|
if (ret < 0) {
|
2014-02-04 09:30:17 -05:00
|
|
|
weprintf("lstat %s:", fname);
|
2014-02-02 14:08:22 -05:00
|
|
|
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
|
2014-02-04 10:05:48 -05:00
|
|
|
parseheader(FILE *fp, const char *s, const char *header, mode_t *mode, char **fname)
|
2014-02-02 14:08:22 -05:00
|
|
|
{
|
|
|
|
char bufs[PATH_MAX + 11]; /* len header + mode + maxname */
|
2014-02-04 09:30:17 -05:00
|
|
|
char *p, *q;
|
2014-02-02 14:08:22 -05:00
|
|
|
size_t n;
|
2014-02-04 09:30:17 -05:00
|
|
|
|
2014-02-04 09:36:58 -05:00
|
|
|
if (fgets(bufs, sizeof(bufs), fp) == NULL)
|
2014-02-04 09:30:17 -05:00
|
|
|
if (ferror(fp))
|
|
|
|
eprintf("%s: read error:", s);
|
2014-02-04 09:36:58 -05:00
|
|
|
if (bufs[0] == '\0' || feof(fp))
|
|
|
|
eprintf("empty or nil 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");
|
2014-02-02 14:08:22 -05:00
|
|
|
p = bufs;
|
2014-02-04 09:30:17 -05:00
|
|
|
if (strncmp(bufs, header, strlen(header)) != 0)
|
|
|
|
eprintf("malformed header prefix\n");
|
2014-02-02 14:08:22 -05:00
|
|
|
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 */
|
2014-04-23 15:28:59 -04:00
|
|
|
*mode = parsemode(p, *mode, 0);
|
2014-04-01 09:26:10 -04:00
|
|
|
n = strlen(q);
|
|
|
|
while (n > 0 && (q[n - 1] == '\n' || q[n - 1] == '\r'))
|
|
|
|
q[--n] = '\0';
|
|
|
|
if (n > 0)
|
2014-02-02 14:08:22 -05:00
|
|
|
*fname = q;
|
|
|
|
}
|
|
|
|
|
2014-02-04 09:30:17 -05:00
|
|
|
static void
|
2014-02-02 14:08:22 -05:00
|
|
|
uudecode(FILE *fp, FILE *outfp)
|
|
|
|
{
|
2014-06-01 09:04:32 -04:00
|
|
|
char *bufb = NULL, *p;
|
|
|
|
size_t n = 0;
|
|
|
|
ssize_t len;
|
2014-02-04 09:44:43 -05:00
|
|
|
int ch, i;
|
2014-02-04 09:30:17 -05:00
|
|
|
|
2014-02-04 09:44:43 -05:00
|
|
|
#define DEC(c) (((c) - ' ') & 077) /* single character decode */
|
2014-02-02 14:08:22 -05:00
|
|
|
#define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) )
|
2014-02-04 11:49:18 -05:00
|
|
|
#define OUT_OF_RANGE(c) eprintf("character %c out of range: [%d-%d]", (c), 1 + ' ', 077 + ' ' + 1)
|
2014-02-02 14:08:22 -05:00
|
|
|
|
2014-06-01 09:04:32 -04:00
|
|
|
while((len = agetline(&bufb, &n, fp)) != -1) {
|
2014-02-02 14:08:22 -05:00
|
|
|
p = bufb;
|
2014-02-04 09:30:17 -05:00
|
|
|
/* trim newlines */
|
2014-06-01 09:04:32 -04:00
|
|
|
if(len && bufb[len - 1] != '\n')
|
|
|
|
bufb[len - 1] = '\0';
|
2014-02-04 09:30:17 -05:00
|
|
|
else
|
|
|
|
eprintf("no newline found, aborting\n");
|
2014-02-04 09:44:43 -05:00
|
|
|
/* check for last line */
|
|
|
|
if ((i = DEC(*p)) <= 0)
|
2014-02-02 14:08:22 -05:00
|
|
|
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-02 14:08:22 -05:00
|
|
|
}
|
2014-02-04 09:30:17 -05:00
|
|
|
/* check for end or fail */
|
2014-06-01 09:04:32 -04:00
|
|
|
len = agetline(&bufb, &n, fp);
|
|
|
|
if(len < 3 || strncmp(bufb, "end", 3) != 0 || bufb[3] != '\n')
|
2014-02-04 10:19:02 -05:00
|
|
|
eprintf("invalid uudecode footer \"end\" not found\n");
|
2014-06-01 07:49:07 -04:00
|
|
|
free(bufb);
|
2014-02-02 14:08:22 -05:00
|
|
|
}
|