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