ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Convert-UUlib/uulib/uunconc.c
Revision: 1.22
Committed: Tue Feb 25 21:10:01 2020 UTC (4 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.21: +3 -9 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 * This file is part of uudeview, the simple and friendly multi-part multi-
3 * file uudecoder program (c) 1994-2001 by Frank Pilhofer. The author may
4 * be contacted at fp@fpx.de
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17 /*
18 * These are the functions that are responsible for decoding. The
19 * original idea is from a freeware utility called "uunconc", and
20 * few lines of this code may still bear a remote resemblance to
21 * its code. If you are the author or know him, contact me.
22 * This program could only decode one multi-part, uuencoded file
23 * where the parts were in order. Base64, XX and BinHex decoding,
24 * support for multi-files and part-ordering covered by myself.
25 **/
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #ifdef SYSTEM_WINDLL
32 #include <windows.h>
33 #endif
34 #ifdef SYSTEM_OS2
35 #include <os2.h>
36 #endif
37
38 #include <stdio.h>
39 #include <ctype.h>
40
41 #ifdef STDC_HEADERS
42 #include <stdlib.h>
43 #include <string.h>
44 #endif
45 #ifdef HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif
48 #ifdef HAVE_ERRNO_H
49 #include <errno.h>
50 #endif
51
52 #include <crc32.h>
53 #include <uudeview.h>
54 #include <uuint.h>
55 #include <fptools.h>
56 #include <uustring.h>
57
58 char * uunconc_id = "$Id$";
59
60 /* for braindead systems */
61 #ifndef SEEK_SET
62 #ifdef L_BEGIN
63 #define SEEK_SET L_BEGIN
64 #else
65 #define SEEK_SET 0
66 #endif
67 #endif
68
69 /*
70 * decoder states
71 */
72
73 #define BEGIN (1)
74 #define DATA (2)
75 #define END (3)
76 #define DONE (4)
77
78 /*
79 * mallocable areas
80 */
81
82 char *uunconc_UUxlat;
83 char *uunconc_UUxlen;
84 char *uunconc_B64xlat;
85 char *uunconc_XXxlat;
86 char *uunconc_BHxlat;
87 char *uunconc_save;
88
89 /*
90 * decoding translation tables and line length table
91 */
92
93 static int * UUxlen; /* initialized in UUInitConc() */
94 static int * UUxlat; /* from the malloc'ed areas above */
95 static int * B64xlat;
96 static int * XXxlat;
97 static int * BHxlat;
98
99 /*
100 * buffer for decoding
101 */
102
103 static char *save[3];
104
105 /*
106 * mallocable areas
107 */
108
109 char *uuncdl_fulline;
110 char *uuncdp_oline;
111
112 /*
113 * Return information for QuickDecode
114 */
115
116 static int uulboundary;
117
118 /*
119 * To prevent warnings when using a char as index into an array
120 */
121
122 #define ACAST(s) ((int)(uchar)(s))
123
124 /*
125 * Initialize decoding tables
126 */
127
128 void
129 UUInitConc (void)
130 {
131 int i, j;
132
133 /*
134 * Update pointers
135 */
136 UUxlen = (int *) uunconc_UUxlen;
137 UUxlat = (int *) uunconc_UUxlat;
138 B64xlat = (int *) uunconc_B64xlat;
139 XXxlat = (int *) uunconc_XXxlat;
140 BHxlat = (int *) uunconc_BHxlat;
141
142 save[0] = uunconc_save;
143 save[1] = uunconc_save + 1200;
144 save[2] = uunconc_save + 2400;
145
146 /* prepare decoding translation table */
147 for(i = 0; i < 256; i++)
148 UUxlat[i] = B64xlat[i] = XXxlat[i] = BHxlat[i] = -1;
149
150 /*
151 * At some time I received a file which used lowercase characters for
152 * uuencoding. This shouldn't be, but let's accept it. Must take special
153 * care that this doesn't break xxdecoding. This is giving me quite a
154 * headache. If this one file hadn't been a Pocahontas picture, I might
155 * have ignored it for good.
156 */
157
158 for (i = ' ', j = 0; i < ' ' + 64; i++, j++)
159 UUxlat[i] /* = UUxlat[i+64] */ = j;
160 for (i = '`', j = 0; i < '`' + 32; i++, j++)
161 UUxlat[i] = j;
162
163 /* add special cases */
164 UUxlat['`'] = UUxlat[' '];
165 UUxlat['~'] = UUxlat['^'];
166
167 /* prepare line length table */
168 UUxlen[0] = 1;
169 for(i = 1, j = 5; i <= 61; i += 3, j += 4)
170 UUxlen[i] = UUxlen[i+1] = UUxlen[i+2] = j;
171
172 /* prepare other tables */
173 for (i=0; i<64; i++) {
174 B64xlat[ACAST(B64EncodeTable[i])] = i;
175 XXxlat [ACAST(XXEncodeTable [i])] = i;
176 BHxlat [ACAST(BHEncodeTable [i])] = i;
177 }
178 }
179
180 /*
181 * Workaround for Netscape
182 */
183
184 /*
185 * Determines whether Netscape may have broken up a data line (by
186 * inserting a newline). This only seems to happen after <a in a
187 * href statement
188 */
189
190 int
191 UUBrokenByNetscape (char *string)
192 {
193 char *ptr;
194 int len;
195
196 if (string==NULL || (len=strlen(string))<3)
197 return 0;
198
199 if ((ptr = _FP_stristr (string, "<a href=")) != NULL) {
200 if (_FP_stristr (string, "</a>") > ptr)
201 return 2;
202 }
203
204 ptr = string + len;
205
206 if (len<3) return 0;
207 if (*--ptr == ' ') ptr--;
208 ptr--;
209
210 if (_FP_strnicmp (ptr, "<a", 2) == 0)
211 return 1;
212
213 return 0;
214 }
215
216 /*
217 * Try to repair a Netscape-corrupted line of data.
218 * This must only be called on corrupted lines, since non-Netscape
219 * data may even _get_ corrupted by this procedure.
220 *
221 * Some checks are included multiply to speed up the procedure. For
222 * example: (*p1!='<' || strnicmp(p1,"</a>",4)). If the first expression
223 * becomes true, the costly function isn't called :-)
224 *
225 * Since '<', '>', '&' might even be replaced by their html equivalents
226 * in href strings, I'm now using two passes, the first one for &amp; + co,
227 * the second one for hrefs.
228 */
229
230 int
231 UUNetscapeCollapse (char *string)
232 {
233 char *p1=string, *p2=string;
234 int res = 0;
235
236 if (string==NULL)
237 return 0;
238
239 /*
240 * First pass
241 */
242 while (*p1) {
243 if (*p1 == '&') {
244 if (_FP_strnicmp (p1, "&amp;", 5) == 0) { p1+=5; *p2++='&'; res=1; }
245 else if (_FP_strnicmp (p1, "&lt;", 4) == 0) { p1+=4; *p2++='<'; res=1; }
246 else if (_FP_strnicmp (p1, "&gt;", 4) == 0) { p1+=4; *p2++='>'; res=1; }
247 else *p2++ = *p1++;
248 res = 1;
249 }
250 else *p2++ = *p1++;
251 }
252 *p2 = '\0';
253 /*
254 * Second pass
255 */
256 p1 = p2 = string;
257
258 while (*p1) {
259 if (*p1 == '<') {
260 if ((_FP_strnicmp (p1, "<ahref=", 7) == 0 ||
261 _FP_strnicmp (p1, "<a href=",8) == 0) &&
262 (_FP_strstr (p1, "</a>") != 0 || _FP_strstr (p1, "</A>") != 0)) {
263 while (*p1 && *p1!='>') p1++;
264 if (*p1=='\0' || *(p1+1)!='<') return 0;
265 p1++;
266 while (*p1 && (*p1!='<' || _FP_strnicmp(p1,"</a>",4)!=0)) {
267 *p2++ = *p1++;
268 }
269 if (_FP_strnicmp(p1,"</a>",4) != 0)
270 return 0;
271 p1+=4;
272 res=1;
273 }
274 else
275 *p2++ = *p1++;
276 }
277 else
278 *p2++ = *p1++;
279 }
280 *p2 = '\0';
281
282 return res;
283 }
284
285 /*
286 * The second parameter is 0 if we are still searching for encoded data,
287 * otherwise it indicates the encoding we're using right now. If we're
288 * still in the searching stage, we must be a little more strict in
289 * deciding for or against encoding; there's too much plain text looking
290 * like encoded data :-(
291 */
292
293 int
294 UUValidData (char *ptr, int encoding, int *bhflag)
295 {
296 int i=0, j, len=0, suspicious=0, flag=0;
297 signed char *s = ptr;
298
299 if ((s == NULL) || (*s == '\0')) {
300 return 0; /* bad string */
301 }
302
303 if (encoding == YENC_ENCODED)
304 return YENC_ENCODED;
305
306 i = strlen (s);
307
308 switch (encoding) {
309 case UU_ENCODED:
310 goto _t_UU;
311 case XX_ENCODED:
312 goto _t_XX;
313 case B64ENCODED:
314 goto _t_B64;
315 case BH_ENCODED:
316 goto _t_Binhex;
317 }
318
319 _t_Binhex: /* Binhex Test */
320 len = i; s = ptr;
321
322 /*
323 * bhflag notes the state we're in. Within the data, it's 1. If we're
324 * still looking for the initial :, it's 0
325 */
326 if (*bhflag == 0 && *s != ':') {
327 if (encoding==BH_ENCODED) return 0;
328 goto _t_B64;
329 }
330 else if (*bhflag == 0 /* *s == ':' */) {
331 s++; len--;
332 }
333
334 while (len && BHxlat[ACAST(*s)] != -1) {
335 len--; s++;
336 }
337
338 /* allow space characters at the end of the line if we are sure */
339 /* that this is Binhex encoded data or the line was long enough */
340
341 flag = (*s == ':') ? 0 : 1;
342
343 if (*s == ':' && len>0) {
344 s++; len--;
345 }
346 if (((i>=60 && len<=10) || encoding) && *s==' ') {
347 while (len && *s==' ') {
348 s++; len--;
349 }
350 }
351
352 /*
353 * BinHex data shall have exactly 64 characters (except the last
354 * line). We ignore everything with less than 40 characters to
355 * be flexible
356 */
357
358 if (len != 0 || (flag && i < 40)) {
359 if (encoding==BH_ENCODED) return 0;
360 goto _t_B64;
361 }
362
363 *bhflag = flag;
364
365 return BH_ENCODED;
366
367 _t_B64: /* Base64 Test */
368 len = i; s = ptr;
369
370 /*
371 * Face it: there _are_ Base64 lines that are not a multiple of four
372 * in length :-(
373 *
374 * if (len%4)
375 * goto _t_UU;
376 */
377
378 while (len--) {
379 if (*s < 0 || (B64xlat[ACAST(*s)] == -1 && *s != '=')) {
380 /* allow space characters at the end of the line if we are sure */
381 /* that this is Base64 encoded data or the line was long enough */
382 if (((i>=60 && len<=10) || encoding) && *s++==' ') {
383 while (*s==' ' && len) s++;
384 if (len==0) return B64ENCODED;
385 }
386 if (encoding==B64ENCODED) return 0;
387 goto _t_UU;
388 }
389 else if (*s == '=') { /* special case at end */
390 /* if we know this is B64encoded, allow spaces at end of line */
391 s++;
392 if (*s=='=' && len>=1) {
393 len--; s++;
394 }
395 if (encoding && len && *s==' ') {
396 while (len && *s==' ') {
397 s++; len--;
398 }
399 }
400 if (len != 0) {
401 if (encoding==B64ENCODED) return 0;
402 goto _t_UU;
403 }
404 return B64ENCODED;
405 }
406 s++;
407 }
408 return B64ENCODED;
409
410 _t_UU:
411 len = i; s = ptr;
412
413 if (UUxlat[ACAST(*s)] == -1) { /* uutest */
414 if (encoding==UU_ENCODED) return 0;
415 goto _t_XX;
416 }
417
418 j = UUxlen[UUxlat[ACAST(*s)]];
419
420 if (len-1 == j) /* remove trailing character */
421 len--;
422 if (len != j) {
423 switch (UUxlat[ACAST(*s)]%3) {
424 case 1:
425 if (j-2 == len) j-=2;
426 break;
427 case 2:
428 if (j-1 == len) j-=1;
429 break;
430 }
431 }
432
433 /*
434 * some encoders are broken with respect to encoding the last line of
435 * a file and produce extraoneous characters beyond the expected EOL
436 * So were not too picky here about the last line, as long as it's longer
437 * than necessary and shorter than the maximum
438 * this tolerance broke the xxdecoding, because xxencoded data was
439 * detected as being uuencoded :( so don't accept 'h' as first character
440 * also, if the first character is lowercase, don't accept the line to
441 * have space characters. the only encoder I've heard of which uses
442 * lowercase characters at least accepts the special case of encoding
443 * 0 as `. The strchr() shouldn't be too expensive here as it's only
444 * evaluated if the first character is lowercase, which really shouldn't
445 * be in uuencoded text.
446 */
447 if (len != j &&
448 ((ptr[0] == '-' && ptr[1] == '-' && strstr(ptr,"part")!=NULL) ||
449 !(*ptr != 'M' && *ptr != 'h' &&
450 len > j && len <= UUxlen[UUxlat['M']]))) {
451 if (encoding==UU_ENCODED) return 0;
452 goto _t_XX; /* bad length */
453 }
454
455 if (len != j || islower (*ptr)) {
456 /*
457 * if we are not in a 'uuencoded' state, don't allow the line to have
458 * space characters at all. if we know we _are_ decoding uuencoded
459 * data, the rest of the line, beyond the length of encoded data, may
460 * have spaces.
461 */
462 if (encoding != UU_ENCODED)
463 if (strchr (ptr, ' ') != NULL)
464 goto _t_XX;
465
466 /* suspicious = 1; we're careful here REMOVED 0.4.15 __FP__ */
467 len = j;
468 }
469
470 while (len--) {
471 if (*s < 0 || UUxlat[ACAST(*s++)] < 0) {
472 if (encoding==UU_ENCODED) return 0;
473 goto _t_XX; /* bad code character */
474 }
475 if (*s == ' ' && suspicious) {
476 if (encoding==UU_ENCODED) return 0;
477 goto _t_XX; /* this line looks _too_ suspicious */
478 }
479 }
480 return UU_ENCODED; /* data is valid */
481
482 _t_XX: /* XX Test */
483 len = i; s = ptr;
484
485 if (XXxlat[ACAST(*s)] == -1)
486 return 0;
487
488 j = UUxlen[XXxlat[ACAST(*s)]]; /* Same line length table as UUencoding */
489
490 if (len-1 == j) /* remove trailing character */
491 len--;
492 if (len != j)
493 switch (UUxlat[ACAST(*s)]%3) {
494 case 1:
495 if (j-2 == len) j-=2;
496 break;
497 case 2:
498 if (j-1 == len) j-=1;
499 break;
500 }
501 /*
502 * some encoders are broken with respect to encoding the last line of
503 * a file and produce extraoneous characters beyond the expected EOL
504 * So were not too picky here about the last line, as long as it's longer
505 * than necessary and shorter than the maximum
506 */
507 if (len != j && !(*ptr != 'h' && len > j && len <= UUxlen[UUxlat['h']]))
508 return 0; /* bad length */
509
510 while(len--) {
511 if(*s < 0 || XXxlat[ACAST(*s++)] < 0) {
512 return 0; /* bad code character */
513 }
514 }
515 return XX_ENCODED; /* data is valid */
516 }
517
518 /*
519 * This function may be called upon a line that does not look like
520 * valid encoding on first sight, but might be erroneously encoded
521 * data from Netscape, Lynx or MS Exchange. We might need to read
522 * a new line from the stream, which is why we need the FILE.
523 * Returns the type of encoded data if successful or 0 otherwise.
524 */
525
526 int
527 UURepairData (FILE *datei, char *line, int encoding, int *bhflag)
528 {
529 int nflag, vflag=0, safety=42;
530 char *ptr;
531
532 nflag = UUBrokenByNetscape (line);
533
534 while (vflag == 0 && nflag && safety--) {
535 if (nflag == 1) { /* need next line to repair */
536 if (strlen (line) > 250)
537 break;
538 ptr = line + strlen (line);
539 if (_FP_fgets (ptr, 299-(ptr-line), datei) == NULL)
540 break;
541 }
542 else { /* don't need next line to repair */
543 }
544 if (UUNetscapeCollapse (line)) {
545 if ((vflag = UUValidData (line, encoding, bhflag)) == 0)
546 nflag = UUBrokenByNetscape (line);
547 }
548 else
549 nflag = 0;
550 }
551 /*
552 * Sometimes, a line is garbled even without it being split into
553 * the next line. Then we try this in our despair
554 */
555 if (vflag == 0) {
556 if (UUNetscapeCollapse (line))
557 vflag = UUValidData (line, encoding, bhflag);
558 }
559
560 /*
561 * If this line looks uuencoded, but the line is one character short
562 * of a valid line, it was probably broken by MS Exchange. According
563 * to my test cases, there is at most one space character missing;
564 * there are never two spaces together.
565 * If adding a space character helps making this line uuencoded, do
566 * it!
567 */
568
569 if (vflag == 0) {
570 ptr = line + strlen(line);
571 *ptr++ = ' ';
572 *ptr-- = '\0';
573 if ((vflag = UUValidData (line, encoding, bhflag)) != UU_ENCODED) {
574 *ptr = '\0';
575 vflag = 0;
576 }
577 }
578 return vflag;
579 }
580
581 /*
582 * Decode a single encoded line using method
583 */
584
585 size_t
586 UUDecodeLine (char *s, char *d, int method)
587 {
588 int i, j, c, cc, count=0, z1, z2, z3, z4;
589 static int leftover=0;
590 int *table;
591
592 /*
593 * for re-initialization
594 */
595
596 if (s == NULL || d == NULL) {
597 leftover = 0;
598 return 0;
599 }
600
601 /*
602 * To shut up gcc -Wall
603 */
604 z1 = z2 = z3 = z4 = 0;
605
606 if (method == UU_ENCODED || method == XX_ENCODED) {
607 if (method == UU_ENCODED)
608 table = UUxlat;
609 else
610 table = XXxlat;
611
612 i = table [ACAST(*s++)];
613 j = UUxlen[i] - 1;
614
615 while(j > 0) {
616 c = table[ACAST(*s++)] << 2;
617 cc = table[ACAST(*s++)];
618 c |= (cc >> 4);
619
620 if(i-- > 0)
621 d[count++] = c;
622
623 cc <<= 4;
624 c = table[ACAST(*s++)];
625 cc |= (c >> 2);
626
627 if(i-- > 0)
628 d[count++] = cc;
629
630 c <<= 6;
631 c |= table[ACAST(*s++)];
632
633 if(i-- > 0)
634 d[count++] = c;
635
636 j -= 4;
637 }
638 }
639 else if (method == B64ENCODED) {
640 if (leftover) {
641 strcpy (uuncdl_fulline + leftover, s);
642
643 leftover = 0;
644 s = uuncdl_fulline;
645 }
646
647 while ((z1 = B64xlat[ACAST(*s)]) != -1) {
648 if ((z2 = B64xlat[ACAST(*(s+1))]) == -1) break;
649 if ((z3 = B64xlat[ACAST(*(s+2))]) == -1) break;
650 if ((z4 = B64xlat[ACAST(*(s+3))]) == -1) break;
651
652 d[count++] = (z1 << 2) | (z2 >> 4);
653 d[count++] = (z2 << 4) | (z3 >> 2);
654 d[count++] = (z3 << 6) | (z4);
655
656 s += 4;
657 }
658 if (z1 != -1 && z2 != -1 && *(s+2) == '=') {
659 d[count++] = (z1 << 2) | (z2 >> 4);
660 s+=2;
661 }
662 else if (z1 != -1 && z2 != -1 && z3 != -1 && *(s+3) == '=') {
663 d[count++] = (z1 << 2) | (z2 >> 4);
664 d[count++] = (z2 << 4) | (z3 >> 2);
665 s+=3;
666 }
667 while (B64xlat[ACAST(*s)] != -1)
668 uuncdl_fulline[leftover++] = *s++;
669 }
670 else if (method == BH_ENCODED) {
671 if (leftover) {
672 strcpy (uuncdl_fulline + leftover, s);
673
674 leftover = 0;
675 s = uuncdl_fulline;
676 }
677 else if (*s == ':')
678 s++;
679
680 while ((z1 = BHxlat[ACAST(*s)]) != -1) {
681 if ((z2 = BHxlat[ACAST(*(s+1))]) == -1) break;
682 if ((z3 = BHxlat[ACAST(*(s+2))]) == -1) break;
683 if ((z4 = BHxlat[ACAST(*(s+3))]) == -1) break;
684
685 d[count++] = (z1 << 2) | (z2 >> 4);
686 d[count++] = (z2 << 4) | (z3 >> 2);
687 d[count++] = (z3 << 6) | (z4);
688
689 s += 4;
690 }
691 if (z1 != -1 && z2 != -1 && *(s+2) == ':') {
692 d[count++] = (z1 << 2) | (z2 >> 4);
693 s+=2;
694 }
695 else if (z1 != -1 && z2 != -1 && z3 != -1 && *(s+3) == ':') {
696 d[count++] = (z1 << 2) | (z2 >> 4);
697 d[count++] = (z2 << 4) | (z3 >> 2);
698 s+=3;
699 }
700 while (BHxlat[ACAST(*s)] != -1)
701 uuncdl_fulline[leftover++] = *s++;
702 }
703 else if (method == YENC_ENCODED) {
704 while (*s) {
705 if (*s == '=') {
706 if (*++s != '\0') {
707 d[count++] = (char) ((int) *s - 64 - 42);
708 s++;
709 }
710 }
711 else if (*s == '\n' || *s == '\r') {
712 s++; /* ignore */
713 }
714 else {
715 d[count++] = (char) ((int) *s++ - 42);
716 }
717 }
718 }
719
720 return count;
721 }
722
723 /*
724 * ``Decode'' Quoted-Printable text
725 */
726
727 int
728 UUDecodeQP (FILE *datain, FILE *dataout, int *state,
729 long maxpos, int method, int flags,
730 char *boundary)
731 {
732 char *line=uugen_inbuffer, *p1, *p2;
733 int val;
734
735 uulboundary = -1;
736
737 while (!feof (datain) &&
738 (ftell(datain)<maxpos || flags&FL_TOEND ||
739 (!(flags&FL_PROPER) && uu_fast_scanning))) {
740 if (_FP_fgets (line, 1023, datain) == NULL)
741 break;
742 if (ferror (datain)) {
743 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
744 uustring (S_SOURCE_READ_ERR),
745 strerror (uu_errno = errno));
746 return UURET_IOERR;
747 }
748 line[255] = '\0';
749
750 if (boundary && line[0]=='-' && line[1]=='-' &&
751 strncmp (line+2, boundary, strlen (boundary)) == 0) {
752 if (line[strlen(boundary)+2]=='-')
753 uulboundary = 1;
754 else
755 uulboundary = 0;
756 return UURET_OK;
757 }
758
759 if (UUBUSYPOLL(ftell(datain)-progress.foffset,progress.fsize)) {
760 UUMessage (uunconc_id, __LINE__, UUMSG_NOTE,
761 uustring (S_DECODE_CANCEL));
762 return UURET_CANCEL;
763 }
764
765 p1 = p2 = line;
766
767 while (*p2) {
768 while (*p2 && *p2 != '=')
769 p2++;
770 if (*p2 == '\0')
771 break;
772 *p2 = '\0';
773 fprintf (dataout, "%s", p1);
774 p1 = ++p2;
775
776 if (isxdigit (*p2) && isxdigit (*(p2+1))) {
777 val = ((isdigit(*p2)) ? (*p2-'0') : (tolower(*p2)-'a'+10)) << 4;
778 val |= ((isdigit(*(p2+1)))?(*(p2+1)-'0') : (tolower(*(p2+1))-'a'+10));
779
780 fputc (val, dataout);
781 p2 += 2;
782 p1 = p2;
783 }
784 else if (!*p2) {
785 /* soft line break */
786 goto skip_lbr;
787 break;
788 }
789 else {
790 /* huh? */
791 fputc ('=', dataout);
792 }
793 }
794 /*
795 * p2 points to a nullbyte right after the CR/LF/CRLF
796 */
797 val = 0;
798 while (p2>p1 && isspace (*(p2-1))) {
799 if (*(p2-1) == '\012' || *(p2-1) == '\015')
800 val = 1;
801 p2--;
802 }
803 *p2 = '\0';
804
805 /*
806 * If the part ends directly after this line, the data does not end
807 * with a linebreak. Or, as the docs put it, "the CRLF preceding the
808 * encapsulation line is conceptually attached to the boundary.
809 * So if the part ends here, don't print a line break"
810 */
811 /* something is broken here now, but it was broken before */
812 if (!feof (datain) &&
813 (ftell(datain)<maxpos || flags&FL_TOEND ||
814 (!(flags&FL_PROPER) && uu_fast_scanning)))
815 fprintf (dataout, "%s\n", p1);
816 else
817 fprintf (dataout, "%s", p1);
818
819 skip_lbr: ;
820 }
821 return UURET_OK;
822 }
823
824 /*
825 * ``Decode'' plain text. Our job is to properly handle the EOL sequence
826 */
827
828 int
829 UUDecodePT (FILE *datain, FILE *dataout, int *state,
830 long maxpos, int method, int flags,
831 char *boundary)
832 {
833 char *line=uugen_inbuffer, *ptr;
834
835 uulboundary = -1;
836
837 while (!feof (datain) &&
838 (ftell(datain)<maxpos || flags&FL_TOEND ||
839 (!(flags&FL_PROPER) && uu_fast_scanning))) {
840 if (_FP_fgets (line, 1023, datain) == NULL)
841 break;
842 if (ferror (datain)) {
843 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
844 uustring (S_SOURCE_READ_ERR),
845 strerror (uu_errno = errno));
846 return UURET_IOERR;
847 }
848 line[255] = '\0';
849
850 if (boundary && line[0]=='-' && line[1]=='-' &&
851 strncmp (line+2, boundary, strlen (boundary)) == 0) {
852 if (line[strlen(boundary)+2]=='-')
853 uulboundary = 1;
854 else
855 uulboundary = 0;
856 return UURET_OK;
857 }
858
859 if (UUBUSYPOLL(ftell(datain)-progress.foffset,progress.fsize)) {
860 UUMessage (uunconc_id, __LINE__, UUMSG_NOTE,
861 uustring (S_DECODE_CANCEL));
862 return UURET_CANCEL;
863 }
864
865 ptr = line + strlen (line);
866
867 /*
868 * If the part ends directly after this line, the data does not end
869 * with a linebreak. Or, as the docs put it, "the CRLF preceding the
870 * encapsulation line is conceptually attached to the boundary.
871 * So if the part ends here, don't print a line break"
872 */
873 if ((ftell(datain)<maxpos || flags&FL_TOEND || flags&FL_PARTIAL ||
874 !boundary || (!(flags&FL_PROPER) && uu_fast_scanning))) {
875 *ptr = '\0';
876 fprintf (dataout, "%s\n", line);
877 }
878 else {
879 *ptr = '\0';
880 fprintf (dataout, "%s", line);
881 }
882 }
883 return UURET_OK;
884 }
885
886 /*
887 * Decode a single field using method. For the moment, this supports
888 * Base64 and Quoted Printable only, to support RFC 1522 header decoding.
889 * Quit when seeing the RFC 1522 ?= end marker.
890 */
891
892 int
893 UUDecodeField (char *s, char *d, int method)
894 {
895 int z1, z2, z3, z4;
896 int count=0;
897
898 if (method == B64ENCODED) {
899 while ((z1 = B64xlat[ACAST(*s)]) != -1) {
900 if ((z2 = B64xlat[ACAST(*(s+1))]) == -1) break;
901 if ((z3 = B64xlat[ACAST(*(s+2))]) == -1) break;
902 if ((z4 = B64xlat[ACAST(*(s+3))]) == -1) break;
903
904 d[count++] = (z1 << 2) | (z2 >> 4);
905 d[count++] = (z2 << 4) | (z3 >> 2);
906 d[count++] = (z3 << 6) | (z4);
907
908 s+=4;
909 }
910 if (z1 != -1 && z2 != -1 && *(s+2) == '=') {
911 d[count++] = (z1 << 2) | (z2 >> 4);
912 s+=2;
913 }
914 else if (z1 != -1 && z2 != -1 && z3 != -1 && *(s+3) == '=') {
915 d[count++] = (z1 << 2) | (z2 >> 4);
916 d[count++] = (z2 << 4) | (z3 >> 2);
917 s+=3;
918 }
919 }
920 else if (method == QP_ENCODED) {
921 while (*s && (*s != '?' || *(s+1) != '=')) {
922 while (*s && *s != '=' && (*s != '?' || *(s+1) != '=')) {
923 d[count++] = *s++;
924 }
925 if (*s == '=') {
926 if (isxdigit (*(s+1)) && isxdigit (*(s+2))) {
927 d[count] = (isdigit (*(s+1)) ? (*(s+1)-'0') : (tolower (*(s+1))-'a'+10)) << 4;
928 d[count] |= (isdigit (*(s+2)) ? (*(s+2)-'0') : (tolower (*(s+2))-'a'+10));
929 count++;
930 s+=3;
931 }
932 else if (!s[1]) {
933 d[count++] = '\012';
934 }
935 else {
936 d[count++] = *s++;
937 }
938 }
939 }
940 }
941 else {
942 return -1;
943 }
944
945 d[count] = '\0';
946 return count;
947 }
948
949 int
950 UUDecodePart (FILE *datain, FILE *dataout, int *state,
951 long maxpos, int method, int flags,
952 char *boundary)
953 {
954 char *line, *oline=uuncdp_oline;
955 int warning=0, vlc=0, lc[2], hadct=0;
956 int tc=0, tf=0, vflag, haddata=0, haddh=0;
957 long yefilesize=0, yepartends=0, yenotlastpart=0;
958 crc32_t yepartcrc=crc32(0L, Z_NULL, 0);
959 static crc32_t yefilecrc=0;
960 static int bhflag=0;
961 size_t count=0;
962 size_t yepartsize=0;
963 char *ptr;
964
965 if (datain == NULL || dataout == NULL) {
966 yefilecrc = crc32(0L, Z_NULL, 0);
967 bhflag = 0;
968 return UURET_OK;
969 }
970
971 /*
972 * Use specialized functions for QP_ENCODED and PT_ENCODED plaintext
973 */
974
975 if (method == QP_ENCODED)
976 return UUDecodeQP (datain, dataout, state, maxpos,
977 method, flags, boundary);
978 else if (method == PT_ENCODED)
979 return UUDecodePT (datain, dataout, state, maxpos,
980 method, flags, boundary);
981
982 lc[0] = lc[1] = 0;
983 vflag = 0;
984
985 uulboundary = -1;
986
987 if (method == YENC_ENCODED) {
988 *state = BEGIN;
989 }
990
991 while (!feof (datain) && *state != DONE &&
992 (ftell(datain)<maxpos || flags&FL_TOEND || maxpos==-1 ||
993 (!(flags&FL_PROPER) && uu_fast_scanning))) {
994 if (_FP_fgets ((line = uugen_fnbuffer), 1200 - 5, datain) == NULL)
995 break;
996
997 /* optionally skip .. */
998 if (*line == '.' && uu_dotdot)
999 line++;
1000
1001 if (ferror (datain)) {
1002 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1003 uustring (S_SOURCE_READ_ERR),
1004 strerror (uu_errno = errno));
1005 return UURET_IOERR;
1006 }
1007
1008 if (!*line) { /* Empty line? */
1009 if (*state == DATA &&
1010 (method == UU_ENCODED || method == XX_ENCODED))
1011 *state = END;
1012
1013 /*
1014 * if we had a whole block of valid lines before, we reset our
1015 * 'valid data' flag, tf. Without this 'if', we'd break decoding
1016 * files with interleaved blank lines. The value of 5 is chosen
1017 * quite arbitrarly.
1018 */
1019
1020 if (vlc > 5)
1021 tf = tc = 0;
1022 vlc = 0;
1023 continue;
1024 }
1025
1026 /*
1027 * Busy Polls
1028 */
1029
1030 if (UUBUSYPOLL(ftell(datain)-progress.foffset,progress.fsize)) {
1031 UUMessage (uunconc_id, __LINE__, UUMSG_NOTE,
1032 uustring (S_DECODE_CANCEL));
1033 return UURET_CANCEL;
1034 }
1035
1036 /*
1037 * try to make sense of data
1038 */
1039
1040 line[1200 - 1] = '\0'; /* For Safety of string functions */
1041 count = 0;
1042
1043 if (boundary && line[0]=='-' && line[1]=='-' &&
1044 strncmp (line+2, boundary, strlen (boundary)) == 0) {
1045 if (line[strlen(boundary)+2]=='-')
1046 uulboundary = 1;
1047 else
1048 uulboundary = 0;
1049 return UURET_OK;
1050 }
1051
1052 /*
1053 * Use this pseudo-handling only if !FL_PROPER
1054 */
1055
1056 if ((flags&FL_PROPER) == 0) {
1057 if (strncmp (line, "BEGIN", 5) == 0 &&
1058 _FP_strstr (line, "CUT HERE") && !tf) { /* I hate these lines */
1059 tc = tf = vlc = 0;
1060 continue;
1061 }
1062 /* MIME body boundary */
1063 if (line[0] == '-' && line[1] == '-' && method == B64ENCODED) {
1064 if ((haddata || tc) && (haddh || hadct)) {
1065 *state = DONE;
1066 vlc = 0;
1067 lc[0] = lc[1] = 0;
1068 continue;
1069 }
1070 hadct = 0;
1071 haddh = 1;
1072 continue;
1073 }
1074 if (_FP_strnicmp (line, "Content-Type", 12) == 0)
1075 hadct = 1;
1076 }
1077
1078 if (*state == BEGIN) {
1079 if ((method == UU_ENCODED || method == XX_ENCODED) &&
1080 (strncmp (line, "begin ", 6) == 0 ||
1081 _FP_strnicmp (line, "<pre>begin ", 11) == 0)) { /* for LYNX */
1082 *state = DATA;
1083 continue;
1084 }
1085 else if (method == BH_ENCODED && line[0] == ':') {
1086 if (UUValidData (line, BH_ENCODED, &bhflag) == BH_ENCODED) {
1087 bhflag = 0;
1088 *state = DATA;
1089 }
1090 else
1091 continue;
1092 }
1093 else if (method == YENC_ENCODED &&
1094 strncmp (line, "=ybegin ", 8) == 0 &&
1095 _FP_strstr (line, " name=") != NULL) {
1096 *state = DATA;
1097
1098 if ((ptr = _FP_strstr (line, " size=")) != NULL) {
1099 ptr += 6;
1100 yefilesize = atoi (ptr);
1101 }
1102 else {
1103 yefilesize = -1;
1104 }
1105
1106 if ((ptr =_FP_strstr (line, " part="))) {
1107 int partno = atoi (ptr + 6);
1108
1109 if ((ptr = _FP_strstr (line, " total=")))
1110 yenotlastpart = atoi (ptr + 7) != partno;
1111
1112 if (_FP_fgets (line, 1200 - 5, datain) == NULL) {
1113 break;
1114 }
1115
1116 if ((ptr = _FP_strstr (line, " end=")) == NULL) {
1117 break;
1118 }
1119
1120 yepartends = atoi (ptr + 5);
1121 }
1122 tf = 1;
1123 continue;
1124 }
1125 else {
1126 continue;
1127 }
1128
1129 tc = tf = vlc = 0;
1130 lc[0] = lc[1] = 0;
1131 }
1132 else if ((*state == END || *state == DATA) &&
1133 (method == UU_ENCODED || method == XX_ENCODED)) {
1134 if (strncmp (line, "end", 3) == 0) {
1135 *state = DONE;
1136 break;
1137 }
1138 }
1139
1140 if (*state == DATA && method == YENC_ENCODED &&
1141 strncmp (line, "=yend ", 6) == 0) {
1142 if ((ptr = _FP_strstr (line, " pcrc32=")) != NULL) {
1143 crc32_t pcrc32 = strtoul (ptr + 8, NULL, 16);
1144 if (pcrc32 != yepartcrc) {
1145 UUMessage (uunconc_id, __LINE__, UUMSG_WARNING,
1146 uustring (S_PCRC_MISMATCH), progress.curfile, progress.partno);
1147 }
1148 }
1149 if ((ptr = _FP_strstr (line, " crc32=")) != NULL)
1150 {
1151 crc32_t fcrc32 = strtoul (ptr + 7, NULL, 16);
1152 if (fcrc32 != yefilecrc) {
1153 UUMessage (uunconc_id, __LINE__, UUMSG_WARNING,
1154 uustring (S_CRC_MISMATCH), progress.curfile);
1155 }
1156 }
1157 if ((ptr = _FP_strstr (line, " size=")) != NULL)
1158 {
1159 size_t size = atol(ptr + 6);
1160 if (size != yepartsize && yefilesize != -1) {
1161 if (size != yefilesize)
1162 UUMessage (uunconc_id, __LINE__, UUMSG_WARNING,
1163 uustring (S_PSIZE_MISMATCH), progress.curfile,
1164 progress.partno, yepartsize, size);
1165 else
1166 UUMessage (uunconc_id, __LINE__, UUMSG_WARNING,
1167 uustring (S_SIZE_MISMATCH), progress.curfile,
1168 yepartsize, size);
1169 }
1170 }
1171 if (!yenotlastpart && (yepartends == 0 || yepartends >= yefilesize)) {
1172 *state = DONE;
1173 }
1174 break;
1175 }
1176
1177 if (*state == DATA || *state == END) {
1178 if (method==B64ENCODED && line[0]=='-' && line[1]=='-' && tc) {
1179 break;
1180 }
1181
1182 if ((vflag = UUValidData (line, (tf)?method:0, &bhflag)) == 0)
1183 vflag = UURepairData (datain, line, (tf)?method:0, &bhflag);
1184
1185 /*
1186 * correct XX/UUencoded lines that were declared Base64
1187 */
1188
1189 if ((method == XX_ENCODED || method == UU_ENCODED) &&
1190 vflag == B64ENCODED) {
1191 if (UUValidData (line, method, &bhflag) == method)
1192 vflag = method;
1193 }
1194
1195 if (vflag == method) {
1196 if (tf) {
1197 count = UUDecodeLine (line, oline, method);
1198 if (method == YENC_ENCODED) {
1199 yepartcrc = crc32(yepartcrc, oline, count);
1200 yefilecrc = crc32(yefilecrc, oline, count);
1201 yepartsize += count;
1202 }
1203 vlc++; lc[1]++;
1204 }
1205 else if (tc == 3) {
1206 count = UUDecodeLine (save[0], oline, method);
1207 count += UUDecodeLine (save[1], oline + count, method);
1208 count += UUDecodeLine (save[2], oline + count, method);
1209 count += UUDecodeLine (line, oline + count, method);
1210 tf = 1;
1211 tc = 0;
1212
1213 /*
1214 * complain if we had one or two invalid lines amidst of
1215 * correctly encoded data. This usually means that the
1216 * file is in error
1217 */
1218
1219 if (lc[1] > 10 && (lc[0] >= 1 && lc[0] <= 2) && !warning) {
1220 UUMessage (uunconc_id, __LINE__, UUMSG_WARNING,
1221 uustring (S_DATA_SUSPICIOUS));
1222 warning=1;
1223 }
1224 lc[0] = 0;
1225 lc[1] = 3;
1226 }
1227 else {
1228 _FP_strncpy (save[tc++], line, 1200);
1229 }
1230
1231 if (method == UU_ENCODED)
1232 *state = (line[0] == 'M') ? DATA : END;
1233 else if (method == XX_ENCODED)
1234 *state = (line[0] == 'h') ? DATA : END;
1235 else if (method == B64ENCODED)
1236 *state = (strchr (line, '=') == NULL) ? DATA : DONE;
1237 else if (method == BH_ENCODED)
1238 *state = (!line[0] || strchr(line+1,':')==NULL)?DATA:DONE;
1239 }
1240 else {
1241 vlc = tf = tc = 0;
1242 haddh = 0;
1243 lc[0]++;
1244 }
1245 }
1246 else if (*state != DONE) {
1247 return UURET_NOEND;
1248 }
1249
1250 if (count) {
1251 if (method == BH_ENCODED) {
1252 if (UUbhwrite (oline, 1, count, dataout) != count) {
1253 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1254 uustring (S_WR_ERR_TEMP),
1255 strerror (uu_errno = errno));
1256 return UURET_IOERR;
1257 }
1258 }
1259 else if (fwrite (oline, 1, count, dataout) != count) {
1260 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1261 uustring (S_WR_ERR_TEMP),
1262 strerror (uu_errno = errno));
1263 return UURET_IOERR;
1264 }
1265 haddata++;
1266 count = 0;
1267 }
1268 }
1269
1270 if (*state == DONE ||
1271 (*state == DATA && method == B64ENCODED &&
1272 vflag == B64ENCODED && (flags&FL_PROPER || haddh))) {
1273 for (tf=0; tf<tc; tf++)
1274 count += UUDecodeLine (save[tf], oline + count, method);
1275 if (count) {
1276 if (method == BH_ENCODED) {
1277 if (UUbhwrite (oline, 1, count, dataout) != count) {
1278 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1279 uustring (S_WR_ERR_TEMP),
1280 strerror (uu_errno = errno));
1281 return UURET_IOERR;
1282 }
1283 }
1284 else if (fwrite (oline, 1, count, dataout) != count) {
1285 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1286 uustring (S_WR_ERR_TEMP),
1287 strerror (uu_errno = errno));
1288 return UURET_IOERR;
1289 }
1290 }
1291 }
1292 return UURET_OK;
1293 }
1294
1295 /*
1296 * this function decodes the file into a temporary file
1297 */
1298
1299 int
1300 UUDecode (uulist *data)
1301 {
1302 int state=BEGIN, part=-1, res=0, hb;
1303 unsigned long rsize, dsize, numbytes;
1304 FILE *datain, *dataout;
1305 void *datain_buf, *dataout_buf;
1306 unsigned char r[8];
1307 char *mode, *ntmp;
1308 uufile *iter;
1309 size_t bytes;
1310 #ifdef HAVE_MKSTEMP
1311 int tmpfd;
1312 const char *tmpprefix = "uuXXXXXX";
1313 char *tmpdir = NULL;
1314 #endif /* HAVE_MKSTEMP */
1315
1316 if (data == NULL || data->thisfile == NULL)
1317 return UURET_ILLVAL;
1318
1319 if (data->state & UUFILE_TMPFILE)
1320 return UURET_OK;
1321
1322 if (data->state & UUFILE_NODATA)
1323 return UURET_NODATA;
1324
1325 if (data->state & UUFILE_NOBEGIN && !uu_desperate)
1326 return UURET_NODATA;
1327
1328 if (data->uudet == PT_ENCODED)
1329 mode = "wt"; /* open text files in text mode */
1330 else
1331 mode = "wb"; /* otherwise in binary */
1332
1333 #ifdef HAVE_MKSTEMP
1334 if ((getuid()==geteuid()) && (getgid()==getegid())) {
1335 tmpdir=getenv("TMPDIR");
1336 }
1337
1338 if (!tmpdir) {
1339 tmpdir = "/tmp";
1340 }
1341 data->binfile = malloc(strlen(tmpdir)+strlen(tmpprefix)+2);
1342
1343 if (!data->binfile) {
1344 #else
1345 if ((data->binfile = tempnam (NULL, "uu")) == NULL) {
1346 #endif /* HAVE_MKSTEMP */
1347 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1348 uustring (S_NO_TEMP_NAME));
1349 return UURET_NOMEM;
1350 }
1351
1352 #ifdef HAVE_MKSTEMP
1353 strcpy(data->binfile, tmpdir);
1354 strcat(data->binfile, "/");
1355 strcat(data->binfile, tmpprefix);
1356
1357 if ((tmpfd = mkstemp(data->binfile)) == -1 ||
1358 (dataout = fdopen(tmpfd, mode)) == NULL) {
1359 #else
1360 if ((dataout = fopen (data->binfile, mode)) == NULL) {
1361 #endif /* HAVE_MKSTEMP */
1362 /*
1363 * we couldn't create a temporary file. Usually this means that TMP
1364 * and TEMP aren't set
1365 */
1366 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1367 uustring (S_WR_ERR_TARGET),
1368 data->binfile, strerror (uu_errno = errno));
1369 #ifdef HAVE_MKSTEMP
1370 if (tmpfd != -1) {
1371 unlink(data->binfile);
1372 close(tmpfd);
1373 }
1374 #endif /* HAVE_MKSTEMP */
1375 _FP_free (data->binfile);
1376 data->binfile = NULL;
1377 uu_errno = errno;
1378 return UURET_IOERR;
1379 }
1380 UUSETBUF (dataout, dataout_buf, uu_wbuf);
1381
1382 /*
1383 * we don't have begin lines in Base64 or plain text files.
1384 */
1385 if (data->uudet == B64ENCODED || data->uudet == QP_ENCODED ||
1386 data->uudet == PT_ENCODED)
1387 state = DATA;
1388
1389 /*
1390 * If we know that the file does not have a begin, we simulate
1391 * it in desperate mode
1392 */
1393
1394 if ((data->state & UUFILE_NOBEGIN) && uu_desperate)
1395 state = DATA;
1396
1397 (void) UUDecodeLine (NULL, NULL, 0); /* init */
1398 (void) UUbhwrite (NULL, 0, 0, NULL); /* dito */
1399 (void) UUDecodePart (NULL, NULL, NULL, 0, 0, 0, NULL); /* yep */
1400
1401 /*
1402 * initialize progress information
1403 */
1404 progress.action = 0;
1405 if (data->filename != NULL) {
1406 _FP_strncpy (progress.curfile,
1407 (strlen(data->filename)>255)?
1408 (data->filename+strlen(data->filename)-255):data->filename,
1409 256);
1410 }
1411 else {
1412 _FP_strncpy (progress.curfile,
1413 (strlen(data->binfile)>255)?
1414 (data->binfile+strlen(data->binfile)-255):data->binfile,
1415 256);
1416 }
1417 progress.partno = 0;
1418 progress.numparts = 0;
1419 progress.fsize = -1;
1420 progress.percent = 0;
1421 progress.action = UUACT_DECODING;
1422
1423 iter = data->thisfile;
1424 while (iter) {
1425 progress.numparts = (iter->partno)?iter->partno:1;
1426 iter = iter->NEXT;
1427 }
1428
1429 /*
1430 * let's rock!
1431 */
1432
1433 iter = data->thisfile;
1434 while (iter) {
1435 if (part != -1 && iter->partno != part+1 && !uu_desperate)
1436 break;
1437 else
1438 part = iter->partno;
1439
1440 if (iter->data->sfname == NULL) {
1441 iter = iter->NEXT;
1442 continue;
1443 }
1444
1445 /*
1446 * call our FileCallback to retrieve the file
1447 */
1448
1449 if (uu_FileCallback) {
1450 if ((res = (*uu_FileCallback) (uu_FileCBArg, iter->data->sfname,
1451 uugen_fnbuffer, 1)) != UURET_OK)
1452 break;
1453 if ((datain = fopen (uugen_fnbuffer, "rb")) == NULL) {
1454 (*uu_FileCallback) (uu_FileCBArg, iter->data->sfname,
1455 uugen_fnbuffer, 0);
1456 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1457 uustring (S_NOT_OPEN_FILE),
1458 uugen_fnbuffer, strerror (uu_errno = errno));
1459 res = UURET_IOERR;
1460 break;
1461 }
1462 }
1463 else {
1464 if ((datain = fopen (iter->data->sfname, "rb")) == NULL) {
1465 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1466 uustring (S_NOT_OPEN_FILE),
1467 iter->data->sfname, strerror (uu_errno = errno));
1468 res = UURET_IOERR;
1469 break;
1470 }
1471 _FP_strncpy (uugen_fnbuffer, iter->data->sfname, 1024);
1472 }
1473 UUSETBUF (datain, datain_buf, uu_rbuf);
1474
1475 progress.partno = part;
1476 progress.fsize = (iter->data->length)?iter->data->length:-1;
1477 progress.percent = 0;
1478 progress.foffset = iter->data->startpos;
1479
1480 fseek (datain, iter->data->startpos, SEEK_SET);
1481 res = UUDecodePart (datain, dataout, &state,
1482 iter->data->startpos+iter->data->length,
1483 data->uudet, iter->data->flags, NULL);
1484 fclose (datain);
1485 UUCLRBUF (uu_rbuf, datain_buf);
1486
1487 if (uu_FileCallback)
1488 (*uu_FileCallback) (uu_FileCBArg, iter->data->sfname, uugen_fnbuffer, 0);
1489
1490 if (state == DONE || res != UURET_OK)
1491 break;
1492
1493 iter = iter->NEXT;
1494 }
1495
1496 if (state == DATA &&
1497 (data->uudet == B64ENCODED || data->uudet == QP_ENCODED ||
1498 data->uudet == PT_ENCODED))
1499 state = DONE; /* assume we're done */
1500
1501 if (fclose (dataout)) {
1502 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1503 uustring (S_WR_ERR_TEMP),
1504 strerror (uu_errno = errno));
1505 res = UURET_IOERR;
1506 }
1507 UUCLRBUF (uu_wbuf, dataout_buf);
1508
1509 if (res != UURET_OK || (state != DONE && !uu_desperate)) {
1510 unlink (data->binfile);
1511 _FP_free (data->binfile);
1512 data->binfile = NULL;
1513 data->state &= ~UUFILE_TMPFILE;
1514 data->state |= UUFILE_ERROR;
1515
1516 if (res == UURET_OK && state != DONE)
1517 res = UURET_NOEND;
1518 }
1519 else if (res != UURET_OK) {
1520 data->state &= ~UUFILE_DECODED;
1521 data->state |= UUFILE_ERROR | UUFILE_TMPFILE;
1522 }
1523 else {
1524 data->state &= ~UUFILE_ERROR;
1525 data->state |= UUFILE_TMPFILE;
1526 }
1527
1528 /*
1529 * If this was a BinHex file, we must extract its data or resource fork
1530 */
1531
1532 if (data->uudet == BH_ENCODED && data->binfile) {
1533 #ifdef HAVE_MKSTEMP
1534 ntmp = malloc(strlen(tmpdir)+strlen(tmpprefix)+2);
1535
1536 if (ntmp == NULL) {
1537 #else
1538 if ((ntmp = tempnam (NULL, "uu")) == NULL) {
1539 #endif /* HAVE_MKSTEMP */
1540 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1541 uustring (S_NO_TEMP_NAME));
1542 progress.action = 0;
1543 return UURET_NOMEM;
1544 }
1545 if ((datain = fopen (data->binfile, "rb")) == NULL) {
1546 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1547 uustring (S_NOT_OPEN_FILE),
1548 data->binfile, strerror (uu_errno = errno));
1549 progress.action = 0;
1550 free (ntmp);
1551 return UURET_IOERR;
1552 }
1553 UUSETBUF (datain, datain_buf, uu_rbuf);
1554
1555 #ifdef HAVE_MKSTEMP
1556 strcpy(ntmp, tmpdir);
1557 strcat(ntmp, "/");
1558 strcat(ntmp, tmpprefix);
1559 if ((tmpfd = mkstemp(ntmp)) == -1 ||
1560 (dataout = fdopen(tmpfd, "wb")) == NULL) {
1561 #else
1562 if ((dataout = fopen (ntmp, "wb")) == NULL) {
1563 #endif /* HAVE_MKSTEMP */
1564 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1565 uustring (S_NOT_OPEN_TARGET),
1566 ntmp, strerror (uu_errno = errno));
1567 progress.action = 0;
1568 fclose (datain);
1569 UUCLRBUF (uu_rbuf, datain_buf);
1570 #ifdef HAVE_MKSTEMP
1571 if (tmpfd != -1) {
1572 unlink(ntmp);
1573 close(tmpfd);
1574 }
1575 #endif /* HAVE_MKSTEMP */
1576 free (ntmp);
1577 return UURET_IOERR;
1578 }
1579 UUSETBUF (dataout, dataout_buf, uu_wbuf);
1580
1581 /*
1582 * read fork lengths. remember they're in Motorola format
1583 */
1584 r[0] = _FP_fgetc (datain);
1585 hb = (int) r[0] + 22;
1586 fseek (datain, (int) r[0] + 12, SEEK_SET);
1587 fread (r, 1, 8, datain);
1588
1589 dsize = (((long) 1 << 24) * (long) r[0]) +
1590 (((long) 1 << 16) * (long) r[1]) +
1591 (((long) 1 << 8) * (long) r[2]) +
1592 ( (long) r[3]);
1593 rsize = (((long) 1 << 24) * (long) r[4]) +
1594 (((long) 1 << 16) * (long) r[5]) +
1595 (((long) 1 << 8) * (long) r[6]) +
1596 ( (long) r[7]);
1597
1598 UUMessage (uunconc_id, __LINE__, UUMSG_MESSAGE,
1599 uustring (S_BINHEX_SIZES),
1600 dsize, rsize);
1601
1602 if (dsize == 0) {
1603 fseek (datain, dsize + hb + 2, SEEK_SET);
1604 numbytes = rsize;
1605 }
1606 else if (rsize == 0) {
1607 fseek (datain, hb, SEEK_SET);
1608 numbytes = dsize;
1609 }
1610 else {
1611 /* we should let the user have the choice here */
1612 UUMessage (uunconc_id, __LINE__, UUMSG_NOTE,
1613 uustring (S_BINHEX_BOTH));
1614 fseek (datain, hb, SEEK_SET);
1615 numbytes = dsize;
1616 }
1617
1618 progress.action = 0;
1619 progress.partno = 0;
1620 progress.numparts = 1;
1621 progress.fsize = numbytes ? numbytes : -1;
1622 progress.foffset = hb;
1623 progress.percent = 0;
1624 progress.action = UUACT_COPYING;
1625
1626 /*
1627 * copy the chosen fork
1628 */
1629
1630 while (!feof (datain) && numbytes) {
1631 if (UUBUSYPOLL(ftell(datain)-progress.foffset,progress.fsize)) {
1632 UUMessage (uunconc_id, __LINE__, UUMSG_NOTE,
1633 uustring (S_DECODE_CANCEL));
1634 fclose (datain);
1635 UUCLRBUF (uu_rbuf, datain_buf);
1636 fclose (dataout);
1637 UUCLRBUF (uu_wbuf, dataout_buf);
1638 unlink (ntmp);
1639 free (ntmp);
1640 return UURET_CANCEL;
1641 }
1642
1643 bytes = fread (uugen_inbuffer, 1,
1644 (size_t) ((numbytes>1024)?1024:numbytes), datain);
1645
1646 if (ferror (datain) || (bytes == 0 && !feof (datain))) {
1647 progress.action = 0;
1648 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1649 uustring (S_SOURCE_READ_ERR),
1650 data->binfile, strerror (uu_errno = errno));
1651 fclose (datain);
1652 UUCLRBUF (uu_rbuf, datain_buf);
1653 fclose (dataout);
1654 UUCLRBUF (uu_wbuf, dataout_buf);
1655 unlink (ntmp);
1656 free (ntmp);
1657 return UURET_IOERR;
1658 }
1659 if (fwrite (uugen_inbuffer, 1, bytes, dataout) != bytes) {
1660 progress.action = 0;
1661 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1662 uustring (S_WR_ERR_TARGET),
1663 ntmp, strerror (uu_errno = errno));
1664 fclose (datain);
1665 UUCLRBUF (uu_rbuf, datain_buf);
1666 fclose (dataout);
1667 UUCLRBUF (uu_wbuf, dataout_buf);
1668 unlink (ntmp);
1669 free (ntmp);
1670 return UURET_IOERR;
1671 }
1672 numbytes -= bytes;
1673 }
1674
1675 if (numbytes) {
1676 UUMessage (uunconc_id, __LINE__, UUMSG_WARNING,
1677 uustring (S_SHORT_BINHEX),
1678 (data->filename)?data->filename:
1679 (data->subfname)?data->subfname:"???",
1680 numbytes);
1681 }
1682
1683 /*
1684 * replace temp file
1685 */
1686
1687 fclose (datain);
1688 UUCLRBUF (uu_rbuf, datain_buf);
1689 if (fclose (dataout)) {
1690 UUCLRBUF (uu_wbuf, dataout_buf);
1691 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1692 uustring (S_WR_ERR_TARGET),
1693 ntmp, strerror (uu_errno = errno));
1694 unlink (ntmp);
1695 free (ntmp);
1696 return UURET_IOERR;
1697 }
1698 UUCLRBUF (uu_wbuf, dataout_buf);
1699
1700 if (unlink (data->binfile)) {
1701 UUMessage (uunconc_id, __LINE__, UUMSG_WARNING,
1702 uustring (S_TMP_NOT_REMOVED),
1703 data->binfile, strerror (uu_errno = errno));
1704 }
1705
1706 free (data->binfile);
1707 data->binfile = ntmp;
1708 }
1709
1710 progress.action = 0;
1711 return res;
1712 }
1713
1714 /*
1715 * QuickDecode for proper MIME attachments. We expect the pointer to
1716 * be on the first header line.
1717 */
1718
1719 int
1720 UUQuickDecode (FILE *datain, FILE *dataout, char *boundary, long maxpos)
1721 {
1722 int state=BEGIN, encoding=-1;
1723 headers myenv;
1724
1725 /*
1726 * Read header and find out about encoding.
1727 */
1728
1729 memset (&myenv, 0, sizeof (headers));
1730 UUScanHeader (datain, &myenv);
1731
1732 if (_FP_stristr (myenv.ctenc, "uu") != NULL)
1733 encoding = UU_ENCODED;
1734 else if (_FP_stristr (myenv.ctenc, "xx") != NULL)
1735 encoding = XX_ENCODED;
1736 else if (_FP_stricmp (myenv.ctenc, "base64") == 0)
1737 encoding = B64ENCODED;
1738 else if (_FP_stricmp (myenv.ctenc, "quoted-printable") == 0)
1739 encoding = QP_ENCODED;
1740 else
1741 encoding = PT_ENCODED;
1742
1743 UUkillheaders (&myenv);
1744
1745 /*
1746 * okay, so decode this one
1747 */
1748
1749 (void) UUDecodePart (NULL, NULL, NULL, 0, 0, 0, NULL); /* init */
1750 return UUDecodePart (datain, dataout, &state, maxpos,
1751 encoding, FL_PROPER|FL_TOEND,
1752 boundary);
1753 }