uudecode: fix flushing (again) through rewrite

This commit is contained in:
Tai Chi Minh Ralph Eastwood 2015-02-11 13:27:41 +00:00 committed by sin
parent b907e8747d
commit b64b51dc91

View File

@ -162,34 +162,49 @@ uudecodeb64(FILE *fp, FILE *outfp)
char bufb[60], *pb; char bufb[60], *pb;
char out[45], *po; char out[45], *po;
size_t n; size_t n;
int b = 0, e, t = 0; int b = 0, e, t = -1, l = 1;
unsigned char b24[3] = {0, 0, 0}; unsigned char b24[3] = {0, 0, 0};
while ((n = fread(bufb, 1, sizeof(bufb), fp))) { while ((n = fread(bufb, 1, sizeof(bufb), fp))) {
for (pb = bufb, po = out; pb < bufb + n; pb++) { for (pb = bufb, po = out; pb < bufb + n; pb++) {
if (*pb == '=') { if (*pb == '\n') {
if (b == 0 || t) { l++;
/* footer size is ==== is 4 */
if (++t < 4)
continue; continue;
else } else if (*pb == '=') {
goto flush; switch (b) {
} else if (b == 1) { case 0:
eprintf("unexpected \"=\" appeared."); /* expected '=' remaining
} else if (b == 2) { * including footer */
*po++ = b24[0]; if (--t) {
goto flush; fwrite(out, 1,
} else if (b == 3) { (po - out),
outfp);
return;
}
continue;
case 1:
eprintf("%d: unexpected \"=\""
"appeared.", l);
case 3:
*po++ = b24[0]; *po++ = b24[0];
*po++ = b24[1]; *po++ = b24[1];
goto flush; b = 0;
} t = 6; /* expect 6 '=' */
}
if ((e = b64dt[(int)*pb]) == -1) {
eprintf("invalid byte \"%c\"", *pb);
} else if (e == -2) /* whitespace */
continue; continue;
switch (b) { case 2:
*po++ = b24[0];
b = 0;
t = 5; /* expect 5 '=' */
continue;
}
} else if ((e = b64dt[(int)*pb]) == -1)
eprintf("%d: invalid byte \"%c\"", l, *pb);
else if (e == -2) /* whitespace */
continue;
else if (t > 0) /* state is parsing pad/footer */
eprintf("%d: invalid byte \"%c\" after padding",
l, *pb);
switch (b) { /* decode next base64 chr based on state */
case 0: b24[0] |= e << 2; break; case 0: b24[0] |= e << 2; break;
case 1: b24[0] |= (e >> 4) & 0x3; case 1: b24[0] |= (e >> 4) & 0x3;
b24[1] |= (e & 0xf) << 4; break; b24[1] |= (e & 0xf) << 4; break;
@ -197,7 +212,7 @@ uudecodeb64(FILE *fp, FILE *outfp)
b24[2] |= (e & 0x3) << 6; break; b24[2] |= (e & 0x3) << 6; break;
case 3: b24[2] |= e; break; case 3: b24[2] |= e; break;
} }
if (++b == 4) { if (++b == 4) { /* complete decoding an octet */
*po++ = b24[0]; *po++ = b24[0];
*po++ = b24[1]; *po++ = b24[1];
*po++ = b24[2]; *po++ = b24[2];
@ -205,12 +220,10 @@ uudecodeb64(FILE *fp, FILE *outfp)
b = 0; b = 0;
} }
} }
goto flush;
}
eprintf("invalid uudecode footer \"====\" not found\n");
flush:
fwrite(out, 1, (po - out), outfp); fwrite(out, 1, (po - out), outfp);
} }
eprintf("%d: invalid uudecode footer \"====\" not found\n", l);
}
static void static void
uudecode(FILE *fp, FILE *outfp) uudecode(FILE *fp, FILE *outfp)