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 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
|
/*
* Decode one or more uuencoded article back to binary form.
*
* UNIX/NN VERSION
* This version cannot be used as a stand-alone uud!
* This version is made: 16 June 1989.
*
* From the Berkeley original, modified by MSD, RDR, JPHD & WLS.
*/
#include <unistd.h>
#include <string.h>
#include "config.h"
#include "global.h"
#include "save.h"
#include "nn_term.h"
/* decode.c */
static int strncmp_skip(char *buf, char *str, int n);
static int decode_line(char *buf, register int len, int dont_write);
static void inittbls(void);
static int gettable(FILE * in);
static void new_file(void);
/* #define DEC_DEBUG *//* never define this */
char *decode_header_file = "Decode.Headers";
int decode_skip_prefix = 2;
int decode_keep = 0;
#define MAXCHAR 256
#define LINELEN 256
#define NORMLEN 60 /* allows for 80 encoded chars per line */
#define SEQMAX 'z'
#define SEQMIN 'a'
static char seqc, partn;
static int first, secnd, check;
#define MAX_PREFIX 10
static int prefix_lgt, set_prefix;
static char prefix_str[MAX_PREFIX];
static FILE *out;
static char *target;
static char blank;
static int chtbl[MAXCHAR], cdlen[NORMLEN + 3];
static char ofname[FILENAME], arcname[FILENAME];
static int state, arcpart;
#define NO_ADVANCE 0x10
#define DECODE_TEXT 1
#define FIND_BEGIN 2
#define FIND_BEGIN_AFTER_ERROR 3
#define FIND_BEGIN_AFTER_INCLUDE 4
#define NEW_BEGIN (5 | NO_ADVANCE)
#define FOUND_END (6 | NO_ADVANCE)
#define FOUND_INCLUDE (7 | NO_ADVANCE)
#define SKIP_LEADING 8
#define SKIP_TRAILING (9 | NO_ADVANCE)
#define DECODE_ERROR (10 | NO_ADVANCE)
#define OTHER_ERROR (11 | NO_ADVANCE)
#define MIN_DECODE_LEADING 8 /* lines to decode ok when doing skip-leading */
#ifdef DEC_DEBUG
char *state_tbl[] = {
"-", "decode", "find begin", "find a/error", "find a/include",
"new begin", "found end", "found include", "skip leading",
"skip trail", "error", "other error"
};
#endif
/*
* decode one line, write on out file
*/
static int
strncmp_skip(char *buf, char *str, int n)
{
register int i;
register char *line = buf;
if (!set_prefix)
return strncmp(line, str, n);
if (decode_skip_prefix > MAX_PREFIX)
decode_skip_prefix = MAX_PREFIX;
for (i = 0; i <= decode_skip_prefix; i++, line++) {
if (*line == NUL)
break;
if (*line == '#' || *line == ':')
break;
if (strncmp(line, str, n))
continue;
prefix_lgt = i;
if (i)
strncpy(prefix_str, buf, i);
set_prefix = 0;
#ifdef DEC_DEBUG
msg("match %s", str);
user_delay(1);
#endif
return 0;
}
return 1;
}
static int
decode_line(char *buf, register int len, int dont_write)
/* len actual input line length */
/* dont_write doing leading check */
{
char outl[LINELEN];
register char *bp, *ut;
register int *trtbl = chtbl;
register int n;
register int blen; /* binary length (from decoded file) */
register int rlen; /* calculated input line length */
/*
* Get the binary line length.
*/
if ((blen = trtbl[buf[0]]) < 0) {
if (strncmp(buf, "begin", 5) == 0 || strncmp(buf, "table", 5) == 0)
return NEW_BEGIN;
if (state == SKIP_LEADING)
return SKIP_LEADING;
/*
* end of uuencoded file ?
*/
if (strncmp(buf, "end", 3) == 0)
return FOUND_END;
/*
* end of current file ? : get next one.
*/
if (strncmp(buf, "include", 7) == 0)
return FOUND_INCLUDE;
/*
* trailing garbage
*/
return SKIP_TRAILING;
}
/*
* Some common endings that survive to this point. Could fail if a line
* really started with one of these, but this is sufficiently unlikely,
* plus it handles lots of news post formats.
*/
if (state != SKIP_LEADING &&
(strncmp(buf, "END", 3) == 0 ||
strncmp(buf, "CUT", 3) == 0))
return SKIP_TRAILING;
rlen = cdlen[blen];
if (len < rlen)
goto d_err;
if (len > (rlen + 2))
goto d_err; /* line too long */
/*
* Is it the empty line before the end line ?
*/
if (blen == 0)
return state;
/*
* Pad with blanks.
*/
for (bp = buf + len, n = rlen - len; --n >= 0;)
*bp++ = blank;
/*
* Verify
*/
for (n = rlen, bp = buf; --n >= 0; bp++)
if (trtbl[*bp] < 0) {
#ifdef DEC_DEBUG
msg("%s - verify failed %d '%.30s'",
state_tbl[state & 0xf], rlen - n, buf);
user_delay(2);
#endif
goto d_err;
}
/*
* Check for uuencodes that append a 'z' to each line....
*/
if (check) {
if (secnd) {
secnd = 0;
if (buf[rlen] == SEQMAX) {
check = 0;
}
} else if (first) {
first = 0;
secnd = 1;
if (buf[rlen] != SEQMAX) {
check = 0;
}
}
}
/*
* There we check.
*/
if (check) {
if (buf[rlen] != seqc) {
#ifdef DEC_DEBUG
msg("check failed %d != %d", buf[rlen], seqc);
user_delay(1);
#endif
goto d_err;
}
if (--seqc < SEQMIN)
seqc = SEQMAX;
}
if (dont_write)
return DECODE_TEXT;
/*
* output a group of 3 bytes (4 input characters).
*/
ut = outl;
n = blen;
bp = &buf[1];
while (--n >= 0) {
*(ut++) = trtbl[*bp] << 2 | trtbl[bp[1]] >> 4;
if (n > 0) {
*(ut++) = (trtbl[bp[1]] << 4) | (trtbl[bp[2]] >> 2);
n--;
}
if (n > 0) {
*(ut++) = trtbl[bp[2]] << 6 | trtbl[bp[3]];
n--;
}
bp += 4;
}
if ((int) fwrite(outl, 1, blen, out) <= 0) {
msg("Error on writing decoded file");
return OTHER_ERROR;
}
return DECODE_TEXT;
d_err:
if (state == SKIP_LEADING)
return SKIP_LEADING;
return DECODE_ERROR;
}
/*
* Install the table in memory for later use.
*/
static void
inittbls(void)
{
register int i, j;
/*
* Set up the default translation table.
*/
for (i = 0; i < ' '; i++)
chtbl[i] = -1;
for (i = ' ', j = 0; i < ' ' + 64; i++, j++)
chtbl[i] = j;
for (i = ' ' + 64; i < MAXCHAR; i++)
chtbl[i] = -1;
chtbl['`'] = chtbl[' ']; /* common mutation */
chtbl['~'] = chtbl['^']; /* an other common mutation */
blank = ' ';
/*
* set up the line length table, to avoid computing lotsa * and / ...
*/
cdlen[0] = 1;
for (i = 1, j = 5; i <= NORMLEN; i += 3, j += 4)
cdlen[i] = (cdlen[i + 1] = (cdlen[i + 2] = j));
}
static int
gettable(FILE * in)
{
char buf[LINELEN], *line;
register int c, n = 0;
register char *cpt;
for (c = 0; c < MAXCHAR; c++)
chtbl[c] = -1;
for (;;) {
if (fgets(buf, sizeof buf, in) == NULL) {
msg("EOF while in translation table.");
return -1;
}
line = buf + prefix_lgt;
if ((prefix_lgt > 0 && strncmp(buf, prefix_str, prefix_lgt)) ||
strncmp(line, "begin", 5) == 0) {
msg("Incomplete translation table.");
return -1;
}
cpt = line + strlen(line) - 1;
*cpt = ' ';
while (*(cpt) == ' ') {
*cpt = 0;
cpt--;
}
cpt = line;
while (*cpt) {
c = *cpt;
if (chtbl[c] != -1) {
msg("Duplicate char in translation table.");
return -1;
}
if (n == 0)
blank = c;
chtbl[c] = n++;
if (n >= 64)
return 0;
cpt++;
}
}
}
static void
new_file(void)
{
out = NULL;
seqc = SEQMAX;
partn = 'a';
check = 1;
first = 1;
secnd = 0;
state = FIND_BEGIN;
prefix_lgt = 0;
set_prefix = decode_skip_prefix;
arcpart = 0;
inittbls();
}
void
uud_start(char *dir)
{
target = dir;
new_file();
}
void
uud_end(void)
{
if (out != NULL) {
fclose(out);
if (decode_keep) {
msg("%s INCOMPLETE -- removed", arcname);
unlink(ofname);
user_delay(5);
}
out = NULL;
}
}
int
uudecode(register article_header * ah, FILE * in)
{
mode_t mode;
int onedone, len, lead_check = 0;
char buf[LINELEN], part[2], *line;
long real_size, start_offset;
long expect_size;
onedone = 0;
/*
* search for header or translation table line.
*/
start_offset = ftell(in);
for (;;) {
if ((state & NO_ADVANCE) == 0) {
if (ftell(in) >= ah->lpos)
break;
if (fgets(buf, sizeof buf, in) == NULL)
break;
}
if (s_keyboard)
return -1;
len = strlen(buf);
if (len > 0 && buf[len - 1] == NL)
buf[--len] = NUL;
#ifdef DEC_DEBUG
if (state != ostate) {
msg("%s->%s - '%.30s'", state_tbl[ostate & 0xf], state_tbl[state & 0xf], buf);
user_delay(2);
ostate = state;
}
#endif
switch (state) {
case NEW_BEGIN:
if (out != NULL) {
uud_end();
user_delay(5);
}
new_file();
/* FALLTHRU */
case FIND_BEGIN:
case FIND_BEGIN_AFTER_ERROR:
case FIND_BEGIN_AFTER_INCLUDE:
set_prefix = decode_skip_prefix;
if (strncmp_skip(buf, "table", 5) == 0) {
gettable(in);
continue;
}
if (strncmp_skip(buf, "begin", 5))
continue;
line = buf + prefix_lgt;
if (state == FIND_BEGIN_AFTER_INCLUDE) {
if (sscanf(line, "begin part %1s%s", part, arcname) != 2) {
msg("Invalid 'begin' line after 'include'");
continue;
}
partn++;
if (partn > 'z')
partn = 'a';
if (part[0] != partn) {
msg("PARTS NOT IN SEQUENCE: %s -- removed", arcname);
user_delay(5);
fclose(out);
unlink(ofname);
new_file();
state = FIND_BEGIN_AFTER_ERROR;
goto err;
}
} else {
if (sscanf(line, "begin %o %s", (unsigned int *) &mode, arcname) != 2)
continue;
if (target != NULL)
sprintf(ofname, "%s%s", target, arcname);
else
strcpy(ofname, arcname);
if ((out = open_file(ofname, OPEN_CREATE)) == NULL) {
msg("Cannot create file: %s", ofname);
goto err;
}
chmod(ofname, mode & 0666);
if (decode_header_file)
store_header(ah, in, target, decode_header_file);
}
state = DECODE_TEXT;
continue;
case SKIP_LEADING:
if (len > prefix_lgt &&
(!prefix_lgt || strncmp(buf, prefix_str, prefix_lgt) == 0)) {
state = decode_line(buf + prefix_lgt, len - prefix_lgt, 1);
if (state == DECODE_TEXT) {
if (++lead_check == MIN_DECODE_LEADING) {
fseek(in, start_offset, 0);
continue;
}
state = SKIP_LEADING;
continue;
}
} else {
set_prefix = decode_skip_prefix;
if (strncmp_skip(buf, "begin", 5) == 0 ||
strncmp_skip(buf, "table", 5) == 0)
state = NEW_BEGIN;
}
lead_check = 0;
start_offset = ftell(in);
continue;
case DECODE_TEXT:
if (len <= prefix_lgt ||
(prefix_lgt > 0 && strncmp(buf, prefix_str, prefix_lgt))) {
state = SKIP_TRAILING;
continue;
}
if (onedone == 0) {
msg("Decoding%s: %s (part %d)",
prefix_lgt ? " & Unsharing" : "", arcname, ++arcpart);
onedone = 1;
}
state = decode_line(buf + prefix_lgt, len - prefix_lgt, 0);
continue;
case FOUND_END:
real_size = ftell(out);
fclose(out);
if (ftell(in) >= ah->lpos || fgets(buf, sizeof buf, in) == NULL) {
new_file();
break;
}
if ((!prefix_lgt || strncmp(buf, prefix_str, prefix_lgt) == 0) &&
sscanf(buf + prefix_lgt, "size%ld", &expect_size) == 1 &&
real_size != expect_size) {
msg("%s decoded with wrong size %ld (exp. %ld)",
arcname, real_size, expect_size);
user_delay(3);
} else {
msg("%s complete", arcname);
user_delay(1);
}
new_file();
state = NEW_BEGIN;
continue;
case FOUND_INCLUDE:
state = FIND_BEGIN_AFTER_INCLUDE;
return 0;
case SKIP_TRAILING:
state = SKIP_LEADING;
return 0;
case DECODE_ERROR:
state = SKIP_TRAILING;
continue;
case OTHER_ERROR:
fclose(out);
new_file();
state = FIND_BEGIN_AFTER_ERROR;
goto err;
}
break; /* break in switch => break in loop */
}
if (onedone) {
if (state == DECODE_TEXT)
state = SKIP_LEADING;
return 0;
}
if (state == FIND_BEGIN_AFTER_ERROR)
return -1;
msg("No 'begin' line");
err:
user_delay(2);
return -1;
}
|