2014-02-02 19:08:22 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-02-14 20:02:41 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2014-11-13 18:54:28 +00:00
|
|
|
#include <errno.h>
|
2014-02-02 19:08:22 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-11-13 18:54:28 +00:00
|
|
|
#include <string.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2014-11-13 18:54:28 +00:00
|
|
|
#include "util.h"
|
2014-02-02 19:08:22 +00:00
|
|
|
|
2015-02-10 19:14:27 +00:00
|
|
|
static int mflag = 0;
|
|
|
|
static int oflag = 0;
|
|
|
|
|
2014-02-02 19:08:22 +00:00
|
|
|
static FILE *
|
2014-02-04 15:05:48 +00:00
|
|
|
parsefile(const char *fname)
|
2014-02-02 19:08:22 +00:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
int ret;
|
2014-02-04 14:30:17 +00:00
|
|
|
|
2015-02-10 19:14:27 +00:00
|
|
|
if (strcmp(fname, "/dev/stdout") == 0 || strcmp(fname, "-") == 0)
|
2014-02-02 19:08:22 +00:00
|
|
|
return stdout;
|
|
|
|
ret = lstat(fname, &st);
|
2014-02-04 14:30:17 +00:00
|
|
|
/* if it is a new file, try to open it */
|
2014-02-02 19:08:22 +00:00
|
|
|
if (ret < 0 && errno == ENOENT)
|
2014-02-04 14:30:17 +00:00
|
|
|
goto tropen;
|
2014-02-02 19:08:22 +00:00
|
|
|
if (ret < 0) {
|
2014-02-04 14:30:17 +00:00
|
|
|
weprintf("lstat %s:", fname);
|
2014-02-02 19:08:22 +00: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 14:30:17 +00:00
|
|
|
static void
|
2015-02-10 19:14:27 +00:00
|
|
|
parseheader(FILE *fp, const char *s, char **header, mode_t *mode,
|
|
|
|
char **fname)
|
2014-02-02 19:08:22 +00:00
|
|
|
{
|
2015-02-10 19:14:27 +00:00
|
|
|
char bufs[PATH_MAX + 18]; /* len header + mode + maxname */
|
2014-02-04 14:30:17 +00:00
|
|
|
char *p, *q;
|
2014-02-02 19:08:22 +00:00
|
|
|
size_t n;
|
2014-02-04 14:30:17 +00:00
|
|
|
|
2014-11-13 21:16:29 +00:00
|
|
|
if (!fgets(bufs, sizeof(bufs), fp))
|
2014-02-04 14:30:17 +00:00
|
|
|
if (ferror(fp))
|
|
|
|
eprintf("%s: read error:", s);
|
2014-02-04 14:36:58 +00:00
|
|
|
if (bufs[0] == '\0' || feof(fp))
|
|
|
|
eprintf("empty or nil header string\n");
|
2014-11-13 21:16:29 +00:00
|
|
|
if (!(p = strchr(bufs, '\n')))
|
2014-02-04 14:30:17 +00:00
|
|
|
eprintf("header string too long or non-newline terminated file\n");
|
2014-02-02 19:08:22 +00:00
|
|
|
p = bufs;
|
2015-02-10 19:14:27 +00:00
|
|
|
if (!(q = strchr(p, ' ')))
|
2015-02-15 12:28:00 +00:00
|
|
|
eprintf("malformed mode string in header, expected ' '\n");
|
2015-02-10 19:14:27 +00:00
|
|
|
*header = bufs;
|
|
|
|
*q++ = '\0';
|
|
|
|
p = q;
|
|
|
|
/* now header should be null terminated, q points to mode */
|
2014-11-13 21:16:29 +00:00
|
|
|
if (!(q = strchr(p, ' ')))
|
2015-02-15 12:28:00 +00:00
|
|
|
eprintf("malformed mode string in header, expected ' '\n");
|
2014-02-04 14:30:17 +00:00
|
|
|
*q++ = '\0';
|
|
|
|
/* now mode should be null terminated, q points to fname */
|
2014-04-23 19:28:59 +00:00
|
|
|
*mode = parsemode(p, *mode, 0);
|
2014-04-01 13:26:10 +00:00
|
|
|
n = strlen(q);
|
|
|
|
while (n > 0 && (q[n - 1] == '\n' || q[n - 1] == '\r'))
|
|
|
|
q[--n] = '\0';
|
|
|
|
if (n > 0)
|
2014-02-02 19:08:22 +00:00
|
|
|
*fname = q;
|
2015-02-15 12:28:00 +00:00
|
|
|
else
|
|
|
|
eprintf("header string does not contain output file\n");
|
2014-02-02 19:08:22 +00:00
|
|
|
}
|
2015-02-13 11:19:19 +00:00
|
|
|
|
2015-02-10 19:14:27 +00:00
|
|
|
static const char b64dt[] = {
|
|
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-2,-2,-2,-2,-2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,
|
|
|
|
52,53,54,55,56,57,58,59,60,61,-1,-1,-1,0,-1,-1,-1,0,1,2,3,4,5,6,
|
|
|
|
7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,
|
|
|
|
-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,
|
|
|
|
49,50,51,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
uudecodeb64(FILE *fp, FILE *outfp)
|
|
|
|
{
|
|
|
|
char bufb[60], *pb;
|
|
|
|
char out[45], *po;
|
|
|
|
size_t n;
|
2015-02-11 13:27:41 +00:00
|
|
|
int b = 0, e, t = -1, l = 1;
|
2015-02-10 19:14:27 +00:00
|
|
|
unsigned char b24[3] = {0, 0, 0};
|
|
|
|
|
|
|
|
while ((n = fread(bufb, 1, sizeof(bufb), fp))) {
|
|
|
|
for (pb = bufb, po = out; pb < bufb + n; pb++) {
|
2015-02-11 13:27:41 +00:00
|
|
|
if (*pb == '\n') {
|
|
|
|
l++;
|
|
|
|
continue;
|
|
|
|
} else if (*pb == '=') {
|
|
|
|
switch (b) {
|
2015-02-20 12:27:57 +00:00
|
|
|
case 0:
|
|
|
|
/* expected '=' remaining
|
|
|
|
* including footer */
|
|
|
|
if (--t) {
|
|
|
|
fwrite(out, 1,
|
|
|
|
(po - out),
|
|
|
|
outfp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case 1:
|
|
|
|
eprintf("%d: unexpected \"=\""
|
|
|
|
"appeared\n", l);
|
|
|
|
case 2:
|
|
|
|
*po++ = b24[0];
|
|
|
|
b = 0;
|
|
|
|
t = 5; /* expect 5 '=' */
|
|
|
|
continue;
|
|
|
|
case 3:
|
|
|
|
*po++ = b24[0];
|
|
|
|
*po++ = b24[1];
|
|
|
|
b = 0;
|
|
|
|
t = 6; /* expect 6 '=' */
|
|
|
|
continue;
|
2015-02-10 19:14:27 +00:00
|
|
|
}
|
2015-02-11 13:27:41 +00:00
|
|
|
} else if ((e = b64dt[(int)*pb]) == -1)
|
2015-02-15 12:28:00 +00:00
|
|
|
eprintf("%d: invalid byte \"%c\"\n", l, *pb);
|
2015-02-11 13:27:41 +00:00
|
|
|
else if (e == -2) /* whitespace */
|
2015-02-10 19:14:27 +00:00
|
|
|
continue;
|
2015-02-11 13:27:41 +00:00
|
|
|
else if (t > 0) /* state is parsing pad/footer */
|
2015-02-15 12:28:00 +00:00
|
|
|
eprintf("%d: invalid byte \"%c\""
|
|
|
|
" after padding\n",
|
2015-02-11 13:27:41 +00:00
|
|
|
l, *pb);
|
|
|
|
switch (b) { /* decode next base64 chr based on state */
|
2015-02-10 19:14:27 +00:00
|
|
|
case 0: b24[0] |= e << 2; break;
|
|
|
|
case 1: b24[0] |= (e >> 4) & 0x3;
|
|
|
|
b24[1] |= (e & 0xf) << 4; break;
|
|
|
|
case 2: b24[1] |= (e >> 2) & 0xf;
|
|
|
|
b24[2] |= (e & 0x3) << 6; break;
|
|
|
|
case 3: b24[2] |= e; break;
|
|
|
|
}
|
2015-02-11 13:27:41 +00:00
|
|
|
if (++b == 4) { /* complete decoding an octet */
|
2015-02-10 19:14:27 +00:00
|
|
|
*po++ = b24[0];
|
|
|
|
*po++ = b24[1];
|
|
|
|
*po++ = b24[2];
|
|
|
|
b24[0] = b24[1] = b24[2] = 0;
|
|
|
|
b = 0;
|
|
|
|
}
|
|
|
|
}
|
2015-02-11 13:27:41 +00:00
|
|
|
fwrite(out, 1, (po - out), outfp);
|
2015-02-10 19:14:27 +00:00
|
|
|
}
|
2015-02-11 13:27:41 +00:00
|
|
|
eprintf("%d: invalid uudecode footer \"====\" not found\n", l);
|
2015-02-10 19:14:27 +00:00
|
|
|
}
|
2014-02-02 19:08:22 +00:00
|
|
|
|
2014-02-04 14:30:17 +00:00
|
|
|
static void
|
2014-02-02 19:08:22 +00:00
|
|
|
uudecode(FILE *fp, FILE *outfp)
|
|
|
|
{
|
2014-06-01 13:04:32 +00:00
|
|
|
char *bufb = NULL, *p;
|
|
|
|
size_t n = 0;
|
|
|
|
ssize_t len;
|
2014-02-04 14:44:43 +00:00
|
|
|
int ch, i;
|
2014-02-04 14:30:17 +00:00
|
|
|
|
2014-02-04 14:44:43 +00:00
|
|
|
#define DEC(c) (((c) - ' ') & 077) /* single character decode */
|
2014-02-02 19:08:22 +00:00
|
|
|
#define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) )
|
2014-02-04 16:49:18 +00:00
|
|
|
#define OUT_OF_RANGE(c) eprintf("character %c out of range: [%d-%d]", (c), 1 + ' ', 077 + ' ' + 1)
|
2014-02-02 19:08:22 +00:00
|
|
|
|
2014-11-18 20:49:30 +00:00
|
|
|
while ((len = getline(&bufb, &n, fp)) != -1) {
|
2014-02-02 19:08:22 +00:00
|
|
|
p = bufb;
|
2014-02-04 14:30:17 +00:00
|
|
|
/* trim newlines */
|
2014-11-13 17:29:30 +00:00
|
|
|
if (len && bufb[len - 1] != '\n')
|
2014-06-01 13:04:32 +00:00
|
|
|
bufb[len - 1] = '\0';
|
2014-02-04 14:30:17 +00:00
|
|
|
else
|
|
|
|
eprintf("no newline found, aborting\n");
|
2014-02-04 14:44:43 +00:00
|
|
|
/* check for last line */
|
|
|
|
if ((i = DEC(*p)) <= 0)
|
2014-02-02 19:08:22 +00: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 14:30:17 +00:00
|
|
|
if (ferror(fp))
|
|
|
|
eprintf("read error:");
|
2014-02-02 19:08:22 +00:00
|
|
|
}
|
2014-02-04 14:30:17 +00:00
|
|
|
/* check for end or fail */
|
2014-11-18 20:49:30 +00:00
|
|
|
len = getline(&bufb, &n, fp);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (len < 3 || strncmp(bufb, "end", 3) != 0 || bufb[3] != '\n')
|
2014-02-04 15:19:02 +00:00
|
|
|
eprintf("invalid uudecode footer \"end\" not found\n");
|
2014-06-01 11:49:07 +00:00
|
|
|
free(bufb);
|
2014-02-02 19:08:22 +00:00
|
|
|
}
|
2015-02-13 11:19:19 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: %s [-m] [-o output] [file]\n", argv0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FILE *fp = NULL, *nfp = NULL;
|
|
|
|
char *fname, *header;
|
|
|
|
const char *ifname;
|
|
|
|
mode_t mode = 0;
|
|
|
|
void (*d) (FILE *, FILE *) = NULL;
|
|
|
|
char *ofname = NULL;
|
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'm':
|
|
|
|
mflag = 1; /* accepted but unused (autodetect file type) */
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
oflag = 1;
|
|
|
|
ofname = EARGF(usage());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
if (argc == 0) {
|
|
|
|
fp = stdin;
|
|
|
|
ifname = "<stdin>";
|
|
|
|
} else {
|
|
|
|
if (!(fp = fopen(argv[0], "r")))
|
|
|
|
eprintf("fopen %s:", argv[0]);
|
|
|
|
ifname = argv[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
parseheader(fp, ifname, &header, &mode, &fname);
|
|
|
|
|
|
|
|
if (!strncmp(header, "begin", sizeof("begin")))
|
|
|
|
d = uudecode;
|
|
|
|
else if (!strncmp(header, "begin-base64", sizeof("begin-base64")))
|
|
|
|
d = uudecodeb64;
|
|
|
|
else
|
|
|
|
eprintf("unknown header %s:", header);
|
|
|
|
|
|
|
|
if (oflag)
|
|
|
|
fname = ofname;
|
|
|
|
if (!(nfp = parsefile(fname)))
|
|
|
|
eprintf("fopen %s:", fname);
|
|
|
|
|
|
|
|
d(fp, nfp);
|
|
|
|
|
|
|
|
if (nfp != stdout && chmod(fname, mode) < 0)
|
|
|
|
eprintf("chmod %s:", fname);
|
|
|
|
if (fp)
|
|
|
|
fclose(fp);
|
|
|
|
if (nfp)
|
|
|
|
fclose(nfp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|