2014-02-02 14:08:22 -05:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-02-14 15:02:41 -05:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2014-11-13 13:54:28 -05:00
|
|
|
#include <errno.h>
|
2014-02-02 14:08:22 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-11-13 13:54:28 -05:00
|
|
|
#include <string.h>
|
2014-11-13 12:29:30 -05:00
|
|
|
|
2014-11-13 13:54:28 -05:00
|
|
|
#include "util.h"
|
2014-02-02 14:08:22 -05:00
|
|
|
|
2015-02-10 14:14:27 -05:00
|
|
|
static int mflag = 0;
|
|
|
|
static int oflag = 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
|
|
|
|
2015-03-17 19:10:36 -04:00
|
|
|
if (!strcmp(fname, "/dev/stdout") || !strcmp(fname, "-"))
|
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:
|
2015-03-17 19:10:36 -04:00
|
|
|
return fopen(fname, "w");
|
2014-02-02 14:08:22 -05:00
|
|
|
}
|
|
|
|
|
2014-02-04 09:30:17 -05:00
|
|
|
static void
|
2015-03-17 19:10:36 -04:00
|
|
|
parseheader(FILE *fp, const char *s, char **header, mode_t *mode, char **fname)
|
2014-02-02 14:08:22 -05:00
|
|
|
{
|
2015-02-10 14:14:27 -05:00
|
|
|
char bufs[PATH_MAX + 18]; /* 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-11-13 16:16:29 -05:00
|
|
|
if (!fgets(bufs, sizeof(bufs), fp))
|
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-11-13 16:16:29 -05:00
|
|
|
if (!(p = strchr(bufs, '\n')))
|
2014-02-04 09:30:17 -05:00
|
|
|
eprintf("header string too long or non-newline terminated file\n");
|
2014-02-02 14:08:22 -05:00
|
|
|
p = bufs;
|
2015-02-10 14:14:27 -05:00
|
|
|
if (!(q = strchr(p, ' ')))
|
2015-02-15 07:28:00 -05:00
|
|
|
eprintf("malformed mode string in header, expected ' '\n");
|
2015-02-10 14:14:27 -05:00
|
|
|
*header = bufs;
|
|
|
|
*q++ = '\0';
|
|
|
|
p = q;
|
|
|
|
/* now header should be null terminated, q points to mode */
|
2014-11-13 16:16:29 -05:00
|
|
|
if (!(q = strchr(p, ' ')))
|
2015-02-15 07:28:00 -05:00
|
|
|
eprintf("malformed mode string in header, expected ' '\n");
|
2014-02-04 09:30:17 -05:00
|
|
|
*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;
|
2015-02-15 07:28:00 -05:00
|
|
|
else
|
|
|
|
eprintf("header string does not contain output file\n");
|
2014-02-02 14:08:22 -05:00
|
|
|
}
|
2015-02-13 06:19:19 -05:00
|
|
|
|
2015-02-10 14:14:27 -05: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,
|
2015-03-17 19:10:36 -04:00
|
|
|
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,
|
2015-02-10 14:14:27 -05:00
|
|
|
-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 08:27:41 -05:00
|
|
|
int b = 0, e, t = -1, l = 1;
|
2015-02-10 14:14:27 -05: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 08:27:41 -05:00
|
|
|
if (*pb == '\n') {
|
|
|
|
l++;
|
|
|
|
continue;
|
|
|
|
} else if (*pb == '=') {
|
|
|
|
switch (b) {
|
2015-02-20 07:27:57 -05: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 14:14:27 -05:00
|
|
|
}
|
2015-02-11 08:27:41 -05:00
|
|
|
} else if ((e = b64dt[(int)*pb]) == -1)
|
2015-02-15 07:28:00 -05:00
|
|
|
eprintf("%d: invalid byte \"%c\"\n", l, *pb);
|
2015-02-11 08:27:41 -05:00
|
|
|
else if (e == -2) /* whitespace */
|
2015-02-10 14:14:27 -05:00
|
|
|
continue;
|
2015-02-11 08:27:41 -05:00
|
|
|
else if (t > 0) /* state is parsing pad/footer */
|
2015-02-15 07:28:00 -05:00
|
|
|
eprintf("%d: invalid byte \"%c\""
|
|
|
|
" after padding\n",
|
2015-02-11 08:27:41 -05:00
|
|
|
l, *pb);
|
|
|
|
switch (b) { /* decode next base64 chr based on state */
|
2015-02-10 14:14:27 -05: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 08:27:41 -05:00
|
|
|
if (++b == 4) { /* complete decoding an octet */
|
2015-02-10 14:14:27 -05:00
|
|
|
*po++ = b24[0];
|
|
|
|
*po++ = b24[1];
|
|
|
|
*po++ = b24[2];
|
|
|
|
b24[0] = b24[1] = b24[2] = 0;
|
|
|
|
b = 0;
|
|
|
|
}
|
|
|
|
}
|
2015-02-11 08:27:41 -05:00
|
|
|
fwrite(out, 1, (po - out), outfp);
|
2015-02-10 14:14:27 -05:00
|
|
|
}
|
2015-02-11 08:27:41 -05:00
|
|
|
eprintf("%d: invalid uudecode footer \"====\" not found\n", l);
|
2015-02-10 14:14:27 -05:00
|
|
|
}
|
2014-02-02 14:08:22 -05:00
|
|
|
|
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) )
|
2015-02-20 08:35:28 -05:00
|
|
|
#define OUT_OF_RANGE(c) eprintf("character %c out of range: [%d-%d]\n", (c), 1 + ' ', 077 + ' ' + 1)
|
2014-02-02 14:08:22 -05:00
|
|
|
|
2015-03-27 09:49:48 -04:00
|
|
|
while ((len = getline(&bufb, &n, fp)) > 0) {
|
2014-02-02 14:08:22 -05:00
|
|
|
p = bufb;
|
2014-02-04 09:30:17 -05:00
|
|
|
/* trim newlines */
|
2015-02-20 08:36:09 -05:00
|
|
|
if (!len || bufb[len - 1] != '\n')
|
2014-02-04 09:30:17 -05:00
|
|
|
eprintf("no newline found, aborting\n");
|
2015-02-20 08:36:09 -05:00
|
|
|
bufb[len - 1] = '\0';
|
|
|
|
|
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 */
|
2015-03-27 09:49:48 -04:00
|
|
|
if ((len = getline(&bufb, &n, fp)) < 0)
|
|
|
|
eprintf("getline:");
|
2015-03-17 19:10:36 -04:00
|
|
|
if (len < 3 || strncmp(bufb, "end", 3) || 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
|
|
|
}
|
2015-02-13 06:19:19 -05: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;
|
|
|
|
mode_t mode = 0;
|
2015-03-17 19:10:36 -04:00
|
|
|
char *fname, *header, *ifname, *ofname = NULL;
|
2015-02-13 06:19:19 -05:00
|
|
|
void (*d) (FILE *, FILE *) = 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();
|
|
|
|
|
2015-03-17 19:10:36 -04:00
|
|
|
if (!argc) {
|
2015-02-13 06:19:19 -05:00
|
|
|
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);
|
|
|
|
|
Add *fshut() functions to properly flush file streams
This has been a known issue for a long time. Example:
printf "word" > /dev/full
wouldn't report there's not enough space on the device.
This is due to the fact that every libc has internal buffers
for stdout which store fragments of written data until they reach
a certain size or on some callback to flush them all at once to the
kernel.
You can force the libc to flush them with fflush(). In case flushing
fails, you can check the return value of fflush() and report an error.
However, previously, sbase didn't have such checks and without fflush(),
the libc silently flushes the buffers on exit without checking the errors.
No offense, but there's no way for the libc to report errors in the exit-
condition.
GNU coreutils solve this by having onexit-callbacks to handle the flushing
and report issues, but they have obvious deficiencies.
After long discussions on IRC, we came to the conclusion that checking the
return value of every io-function would be a bit too much, and having a
general-purpose fclose-wrapper would be the best way to go.
It turned out that fclose() alone is not enough to detect errors. The right
way to do it is to fflush() + check ferror on the fp and then to a fclose().
This is what fshut does and that's how it's done before each return.
The return value is obviously affected, reporting an error in case a flush
or close failed, but also when reading failed for some reason, the error-
state is caught.
the !!( ... + ...) construction is used to call all functions inside the
brackets and not "terminating" on the first.
We want errors to be reported, but there's no reason to stop flushing buffers
when one other file buffer has issues.
Obviously, functionales come before the flush and ret-logic comes after to
prevent early exits as well without reporting warnings if there are any.
One more advantage of fshut() is that it is even able to report errors
on obscure NFS-setups which the other coreutils are unable to detect,
because they only check the return-value of fflush() and fclose(),
not ferror() as well.
2015-04-04 15:25:17 -04:00
|
|
|
return !!(fshut(fp, fp == stdin ? "<stdin>" : argv[0]) +
|
|
|
|
fshut(nfp, nfp == stdout ? "<stdout>" : fname));
|
2015-02-13 06:19:19 -05:00
|
|
|
}
|