ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Convert-UUlib/uulib/uunconc.c
Revision: 1.5
Committed: Sun Mar 31 20:08:42 2002 UTC (22 years, 2 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.4: +36 -36 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.4 2002/03/31 20:04:30 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 strcpy (uuncdl_fulline+leftover, s);
653 leftover = 0;
654 s = uuncdl_fulline;
655 }
656
657 while ((z1 = B64xlat[ACAST(*s)]) != -1) {
658 if ((z2 = B64xlat[ACAST(*(s+1))]) == -1) break;
659 if ((z3 = B64xlat[ACAST(*(s+2))]) == -1) break;
660 if ((z4 = B64xlat[ACAST(*(s+3))]) == -1) break;
661
662 d[count++] = (z1 << 2) | (z2 >> 4);
663 d[count++] = (z2 << 4) | (z3 >> 2);
664 d[count++] = (z3 << 6) | (z4);
665
666 s += 4;
667 }
668 if (z1 != -1 && z2 != -1 && *(s+2) == '=') {
669 d[count++] = (z1 << 2) | (z2 >> 4);
670 s+=2;
671 }
672 else if (z1 != -1 && z2 != -1 && z3 != -1 && *(s+3) == '=') {
673 d[count++] = (z1 << 2) | (z2 >> 4);
674 d[count++] = (z2 << 4) | (z3 >> 2);
675 s+=3;
676 }
677 while (B64xlat[ACAST(*s)] != -1)
678 uuncdl_fulline[leftover++] = *s++;
679 }
680 else if (method == BH_ENCODED) {
681 if (leftover) {
682 strcpy (uuncdl_fulline+leftover, s);
683 leftover = 0;
684 s = uuncdl_fulline;
685 }
686 else if (*s == ':')
687 s++;
688
689 while ((z1 = BHxlat[ACAST(*s)]) != -1) {
690 if ((z2 = BHxlat[ACAST(*(s+1))]) == -1) break;
691 if ((z3 = BHxlat[ACAST(*(s+2))]) == -1) break;
692 if ((z4 = BHxlat[ACAST(*(s+3))]) == -1) break;
693
694 d[count++] = (z1 << 2) | (z2 >> 4);
695 d[count++] = (z2 << 4) | (z3 >> 2);
696 d[count++] = (z3 << 6) | (z4);
697
698 s += 4;
699 }
700 if (z1 != -1 && z2 != -1 && *(s+2) == ':') {
701 d[count++] = (z1 << 2) | (z2 >> 4);
702 s+=2;
703 }
704 else if (z1 != -1 && z2 != -1 && z3 != -1 && *(s+3) == ':') {
705 d[count++] = (z1 << 2) | (z2 >> 4);
706 d[count++] = (z2 << 4) | (z3 >> 2);
707 s+=3;
708 }
709 while (BHxlat[ACAST(*s)] != -1)
710 uuncdl_fulline[leftover++] = *s++;
711 }
712 else if (method == YENC_ENCODED) {
713 while (*s) {
714 if (*s == '=') {
715 if (*++s != '\0') {
716 d[count++] = (char) ((int) *s - 64 - 42);
717 s++;
718 }
719 }
720 else if (*s == '\t' || *s == '\n' || *s == '\r') {
721 s++; /* ignore */
722 }
723 else {
724 d[count++] = (char) ((int) *s++ - 42);
725 }
726 }
727 }
728
729 return count;
730 }
731
732 /*
733 * ``Decode'' Quoted-Printable text
734 */
735
736 int
737 UUDecodeQP (FILE *datain, FILE *dataout, int *state,
738 long maxpos, int method, int flags,
739 char *boundary)
740 {
741 char *line=uugen_inbuffer, *p1, *p2;
742 int val;
743
744 uulboundary = -1;
745
746 while (!feof (datain) &&
747 (ftell(datain)<maxpos || flags&FL_TOEND ||
748 (!(flags&FL_PROPER) && uu_fast_scanning))) {
749 if (_FP_fgets (line, 255, datain) == NULL)
750 break;
751 if (ferror (datain)) {
752 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
753 uustring (S_SOURCE_READ_ERR),
754 strerror (uu_errno = errno));
755 return UURET_IOERR;
756 }
757 line[255] = '\0';
758
759 if (boundary && line[0]=='-' && line[1]=='-' &&
760 strncmp (line+2, boundary, strlen (boundary)) == 0) {
761 if (line[strlen(boundary)+2]=='-')
762 uulboundary = 1;
763 else
764 uulboundary = 0;
765 return UURET_OK;
766 }
767
768 if (UUBUSYPOLL(ftell(datain)-progress.foffset,progress.fsize)) {
769 UUMessage (uunconc_id, __LINE__, UUMSG_NOTE,
770 uustring (S_DECODE_CANCEL));
771 return UURET_CANCEL;
772 }
773
774 p1 = p2 = line;
775
776 while (*p2) {
777 while (*p2 && *p2 != '=')
778 p2++;
779 if (*p2 == '\0')
780 break;
781 *p2 = '\0';
782 fprintf (dataout, "%s", p1);
783 p1 = ++p2;
784
785 if (isxdigit (*p2) && isxdigit (*(p2+1))) {
786 val = ((isdigit(*p2)) ? (*p2-'0') : (tolower(*p2)-'a'+10)) << 4;
787 val |= ((isdigit(*(p2+1)))?(*(p2+1)-'0') : (tolower(*(p2+1))-'a'+10));
788
789 fputc (val, dataout);
790 p2 += 2;
791 p1 = p2;
792 }
793 else if (*p2 == '\012' || *(p2+1) == '\015') {
794 /* soft line break */
795 *p2 = '\0';
796 break;
797 }
798 else {
799 /* huh? */
800 fputc ('=', dataout);
801 }
802 }
803 /*
804 * p2 points to a nullbyte right after the CR/LF/CRLF
805 */
806 val = 0;
807 while (p2>p1 && isspace (*(p2-1))) {
808 if (*(p2-1) == '\012' || *(p2-1) == '\015')
809 val = 1;
810 p2--;
811 }
812 *p2 = '\0';
813
814 /*
815 * If the part ends directly after this line, the data does not end
816 * with a linebreak. Or, as the docs put it, "the CRLF preceding the
817 * encapsulation line is conceptually attached to the boundary.
818 * So if the part ends here, don't print a line break"
819 */
820 if (val && (!feof (datain) &&
821 (ftell(datain)<maxpos || flags&FL_TOEND ||
822 (!(flags&FL_PROPER) && uu_fast_scanning))))
823 fprintf (dataout, "%s\n", p1);
824 else
825 fprintf (dataout, "%s", p1);
826 }
827 return UURET_OK;
828 }
829
830 /*
831 * ``Decode'' plain text. Our job is to properly handle the EOL sequence
832 */
833
834 int
835 UUDecodePT (FILE *datain, FILE *dataout, int *state,
836 long maxpos, int method, int flags,
837 char *boundary)
838 {
839 char *line=uugen_inbuffer, *ptr;
840
841 uulboundary = -1;
842
843 while (!feof (datain) &&
844 (ftell(datain)<maxpos || flags&FL_TOEND ||
845 (!(flags&FL_PROPER) && uu_fast_scanning))) {
846 if (_FP_fgets (line, 255, datain) == NULL)
847 break;
848 if (ferror (datain)) {
849 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
850 uustring (S_SOURCE_READ_ERR),
851 strerror (uu_errno = errno));
852 return UURET_IOERR;
853 }
854 line[255] = '\0';
855
856 if (boundary && line[0]=='-' && line[1]=='-' &&
857 strncmp (line+2, boundary, strlen (boundary)) == 0) {
858 if (line[strlen(boundary)+2]=='-')
859 uulboundary = 1;
860 else
861 uulboundary = 0;
862 return UURET_OK;
863 }
864
865 if (UUBUSYPOLL(ftell(datain)-progress.foffset,progress.fsize)) {
866 UUMessage (uunconc_id, __LINE__, UUMSG_NOTE,
867 uustring (S_DECODE_CANCEL));
868 return UURET_CANCEL;
869 }
870
871 ptr = line + strlen (line);
872
873 while (ptr>line && (*(ptr-1) == '\012' || *(ptr-1) == '\015'))
874 ptr--;
875
876
877 /*
878 * If the part ends directly after this line, the data does not end
879 * with a linebreak. Or, as the docs put it, "the CRLF preceding the
880 * encapsulation line is conceptually attached to the boundary.
881 * So if the part ends here, don't print a line break"
882 */
883 if ((*ptr == '\012' || *ptr == '\015') &&
884 (ftell(datain)<maxpos || flags&FL_TOEND || flags&FL_PARTIAL ||
885 !boundary || (!(flags&FL_PROPER) && uu_fast_scanning))) {
886 *ptr = '\0';
887 fprintf (dataout, "%s\n", line);
888 }
889 else {
890 *ptr = '\0';
891 fprintf (dataout, "%s", line);
892 }
893 }
894 return UURET_OK;
895 }
896
897 int
898 UUDecodePart (FILE *datain, FILE *dataout, int *state,
899 long maxpos, int method, int flags,
900 char *boundary)
901 {
902 char *line=uugen_fnbuffer, *oline=uuncdp_oline;
903 int warning=0, vlc=0, lc[2], hadct=0;
904 int tc=0, tf=0, vflag, haddata=0, haddh=0;
905 long yefilesize=0, yepartends=0;
906 static int bhflag=0;
907 size_t count=0;
908 char *ptr;
909
910 if (datain == NULL || dataout == NULL) {
911 bhflag = 0;
912 return UURET_OK;
913 }
914
915 /*
916 * Use specialized functions for QP_ENCODED and PT_ENCODED plaintext
917 */
918
919 if (method == QP_ENCODED)
920 return UUDecodeQP (datain, dataout, state, maxpos,
921 method, flags, boundary);
922 else if (method == PT_ENCODED)
923 return UUDecodePT (datain, dataout, state, maxpos,
924 method, flags, boundary);
925
926 lc[0] = lc[1] = 0;
927 vflag = 0;
928
929 uulboundary = -1;
930
931 if (method == YENC_ENCODED) {
932 *state = BEGIN;
933 }
934
935 while (!feof (datain) && *state != DONE &&
936 (ftell(datain)<maxpos || flags&FL_TOEND || maxpos==-1 ||
937 (!(flags&FL_PROPER) && uu_fast_scanning))) {
938 if (_FP_fgets (line, 299, datain) == NULL)
939 break;
940
941 if (ferror (datain)) {
942 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
943 uustring (S_SOURCE_READ_ERR),
944 strerror (uu_errno = errno));
945 return UURET_IOERR;
946 }
947
948 if (line[0]=='\015' || line[0]=='\012') { /* Empty line? */
949 if (*state == DATA &&
950 (method == UU_ENCODED || method == XX_ENCODED))
951 *state = END;
952
953 /*
954 * if we had a whole block of valid lines before, we reset our
955 * 'valid data' flag, tf. Without this 'if', we'd break decoding
956 * files with interleaved blank lines. The value of 5 is chosen
957 * quite arbitrarly.
958 */
959
960 if (vlc > 5)
961 tf = tc = 0;
962 vlc = 0;
963 continue;
964 }
965
966 /*
967 * Busy Polls
968 */
969
970 if (UUBUSYPOLL(ftell(datain)-progress.foffset,progress.fsize)) {
971 UUMessage (uunconc_id, __LINE__, UUMSG_NOTE,
972 uustring (S_DECODE_CANCEL));
973 return UURET_CANCEL;
974 }
975
976 /*
977 * try to make sense of data
978 */
979
980 line[299] = '\0'; /* For Safety of string functions */
981 count = 0;
982
983 if (boundary && line[0]=='-' && line[1]=='-' &&
984 strncmp (line+2, boundary, strlen (boundary)) == 0) {
985 if (line[strlen(boundary)+2]=='-')
986 uulboundary = 1;
987 else
988 uulboundary = 0;
989 return UURET_OK;
990 }
991
992 /*
993 * Use this pseudo-handling only if !FL_PROPER
994 */
995
996 if ((flags&FL_PROPER) == 0) {
997 if (strncmp (line, "BEGIN", 5) == 0 &&
998 _FP_strstr (line, "CUT HERE") && !tf) { /* I hate these lines */
999 tc = tf = vlc = 0;
1000 continue;
1001 }
1002 /* MIME body boundary */
1003 if (line[0] == '-' && line[1] == '-' && method == B64ENCODED) {
1004 if ((haddata || tc) && (haddh || hadct)) {
1005 *state = DONE;
1006 vlc = 0;
1007 lc[0] = lc[1] = 0;
1008 continue;
1009 }
1010 hadct = 0;
1011 haddh = 1;
1012 continue;
1013 }
1014 if (_FP_strnicmp (line, "Content-Type", 12) == 0)
1015 hadct = 1;
1016 }
1017
1018 if (*state == BEGIN) {
1019 if ((method == UU_ENCODED || method == XX_ENCODED) &&
1020 (strncmp (line, "begin ", 6) == 0 ||
1021 _FP_strnicmp (line, "<pre>begin ", 11) == 0)) { /* for LYNX */
1022 *state = DATA;
1023 continue;
1024 }
1025 else if (method == BH_ENCODED && line[0] == ':') {
1026 if (UUValidData (line, BH_ENCODED, &bhflag) == BH_ENCODED) {
1027 bhflag = 0;
1028 *state = DATA;
1029 }
1030 else
1031 continue;
1032 }
1033 else if (method == YENC_ENCODED &&
1034 strncmp (line, "=ybegin ", 8) == 0 &&
1035 _FP_strstr (line, " size=") != NULL &&
1036 _FP_strstr (line, " name=") != NULL) {
1037 *state = DATA;
1038
1039 ptr = _FP_strstr (line, " size=") + 6;
1040 yefilesize = atoi (ptr);
1041
1042 if (_FP_strstr (line, " part=") != NULL) {
1043 if (_FP_fgets (line, 299, datain) == NULL) {
1044 break;
1045 }
1046
1047 if ((ptr = _FP_strstr (line, " end=")) == NULL) {
1048 break;
1049 }
1050
1051 yepartends = atoi (ptr + 5);
1052 }
1053 tf = 1;
1054 continue;
1055 }
1056 else {
1057 continue;
1058 }
1059
1060 tc = tf = vlc = 0;
1061 lc[0] = lc[1] = 0;
1062 }
1063 else if ((*state == END) &&
1064 (method == UU_ENCODED || method == XX_ENCODED)) {
1065 if (strncmp (line, "end", 3) == 0) {
1066 *state = DONE;
1067 break;
1068 }
1069 }
1070
1071 if (*state == DATA && method == YENC_ENCODED &&
1072 strncmp (line, "=yend ", 6) == 0) {
1073 if (yepartends == 0 || yepartends >= yefilesize) {
1074 *state = DONE;
1075 }
1076 break;
1077 }
1078
1079 if (*state == DATA || *state == END) {
1080 if (method==B64ENCODED && line[0]=='-' && line[1]=='-' && tc) {
1081 break;
1082 }
1083
1084 if ((vflag = UUValidData (line, (tf)?method:0, &bhflag)) == 0)
1085 vflag = UURepairData (datain, line, (tf)?method:0, &bhflag);
1086
1087 /*
1088 * correct XX/UUencoded lines that were declared Base64
1089 */
1090
1091 if ((method == XX_ENCODED || method == UU_ENCODED) &&
1092 vflag == B64ENCODED) {
1093 if (UUValidData (line, method, &bhflag) == method)
1094 vflag = method;
1095 }
1096
1097 if (vflag == method) {
1098 if (tf) {
1099 count = UUDecodeLine (line, oline, method);
1100 vlc++; lc[1]++;
1101 }
1102 else if (tc == 3) {
1103 count = UUDecodeLine (save[0], oline, method);
1104 count += UUDecodeLine (save[1], oline + count, method);
1105 count += UUDecodeLine (save[2], oline + count, method);
1106 count += UUDecodeLine (line, oline + count, method);
1107 tf = 1;
1108 tc = 0;
1109
1110 /*
1111 * complain if we had one or two invalid lines amidst of
1112 * correctly encoded data. This usually means that the
1113 * file is in error
1114 */
1115
1116 if (lc[1] > 10 && (lc[0] >= 1 && lc[0] <= 2) && !warning) {
1117 UUMessage (uunconc_id, __LINE__, UUMSG_WARNING,
1118 uustring (S_DATA_SUSPICIOUS));
1119 warning=1;
1120 }
1121 lc[0] = 0;
1122 lc[1] = 3;
1123 }
1124 else {
1125 _FP_strncpy (save[tc++], line, 256);
1126 }
1127
1128 if (method == UU_ENCODED)
1129 *state = (line[0] == 'M') ? DATA : END;
1130 else if (method == XX_ENCODED)
1131 *state = (line[0] == 'h') ? DATA : END;
1132 else if (method == B64ENCODED)
1133 *state = (strchr (line, '=') == NULL) ? DATA : DONE;
1134 else if (method == BH_ENCODED)
1135 *state = (!line[0] || strchr(line+1,':')==NULL)?DATA:DONE;
1136 }
1137 else {
1138 vlc = tf = tc = 0;
1139 haddh = 0;
1140 lc[0]++;
1141 }
1142 }
1143 else if (*state != DONE) {
1144 return UURET_NOEND;
1145 }
1146
1147 if (count) {
1148 if (method == BH_ENCODED) {
1149 if (UUbhwrite (oline, 1, count, dataout) != count) {
1150 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1151 uustring (S_WR_ERR_TEMP),
1152 strerror (uu_errno = errno));
1153 return UURET_IOERR;
1154 }
1155 }
1156 else if (fwrite (oline, 1, count, dataout) != count) {
1157 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1158 uustring (S_WR_ERR_TEMP),
1159 strerror (uu_errno = errno));
1160 return UURET_IOERR;
1161 }
1162 haddata++;
1163 count = 0;
1164 }
1165 }
1166
1167 if (*state == DONE ||
1168 (*state == DATA && method == B64ENCODED &&
1169 vflag == B64ENCODED && (flags&FL_PROPER || haddh))) {
1170 for (tf=0; tf<tc; tf++)
1171 count += UUDecodeLine (save[tf], oline + count, method);
1172 if (count) {
1173 if (method == BH_ENCODED) {
1174 if (UUbhwrite (oline, 1, count, dataout) != count) {
1175 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1176 uustring (S_WR_ERR_TEMP),
1177 strerror (uu_errno = errno));
1178 return UURET_IOERR;
1179 }
1180 }
1181 else if (fwrite (oline, 1, count, dataout) != count) {
1182 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1183 uustring (S_WR_ERR_TEMP),
1184 strerror (uu_errno = errno));
1185 return UURET_IOERR;
1186 }
1187 }
1188 }
1189 return UURET_OK;
1190 }
1191
1192 /*
1193 * this function decodes the file into a temporary file
1194 */
1195
1196 int
1197 UUDecode (uulist *data)
1198 {
1199 int state=BEGIN, part=-1, res=0, hb;
1200 long rsize, dsize, numbytes;
1201 FILE *datain, *dataout;
1202 unsigned char r[8];
1203 char *mode, *ntmp;
1204 uufile *iter;
1205 size_t bytes;
1206 #ifdef HAVE_MKSTEMP
1207 int tmpfd;
1208 const char *tmpprefix = "uuXXXXXX";
1209 char *tmpdir = NULL;
1210 #endif /* HAVE_MKSTEMP */
1211
1212 if (data == NULL || data->thisfile == NULL)
1213 return UURET_ILLVAL;
1214
1215 if (data->state & UUFILE_TMPFILE)
1216 return UURET_OK;
1217
1218 if (data->state & UUFILE_NODATA)
1219 return UURET_NODATA;
1220
1221 if (data->state & UUFILE_NOBEGIN && !uu_desperate)
1222 return UURET_NODATA;
1223
1224 if (data->uudet == PT_ENCODED)
1225 mode = "wt"; /* open text files in text mode */
1226 else
1227 mode = "wb"; /* otherwise in binary */
1228
1229 #ifdef HAVE_MKSTEMP
1230 if ((getuid()==geteuid()) && (getgid()==getegid())) {
1231 tmpdir=getenv("TMPDIR");
1232 }
1233
1234 if (!tmpdir) {
1235 tmpdir = "/tmp";
1236 }
1237 data->binfile = malloc(strlen(tmpdir)+strlen(tmpprefix)+2);
1238
1239 if (!data->binfile) {
1240 #else
1241 if ((data->binfile = tempnam (NULL, "uu")) == NULL) {
1242 #endif /* HAVE_MKSTEMP */
1243 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1244 uustring (S_NO_TEMP_NAME));
1245 return UURET_NOMEM;
1246 }
1247
1248 #ifdef HAVE_MKSTEMP
1249 strcpy(data->binfile, tmpdir);
1250 strcat(data->binfile, "/");
1251 strcat(data->binfile, tmpprefix);
1252
1253 if ((tmpfd = mkstemp(data->binfile)) == -1 ||
1254 (dataout = fdopen(tmpfd, mode)) == NULL) {
1255 #else
1256 if ((dataout = fopen (data->binfile, mode)) == NULL) {
1257 #endif /* HAVE_MKSTEMP */
1258 /*
1259 * we couldn't create a temporary file. Usually this means that TMP
1260 * and TEMP aren't set
1261 */
1262 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1263 uustring (S_WR_ERR_TARGET),
1264 data->binfile, strerror (uu_errno = errno));
1265 #ifdef HAVE_MKSTEMP
1266 if (tmpfd != -1) {
1267 unlink(data->binfile);
1268 close(tmpfd);
1269 }
1270 #endif /* HAVE_MKSTEMP */
1271 _FP_free (data->binfile);
1272 data->binfile = NULL;
1273 uu_errno = errno;
1274 return UURET_IOERR;
1275 }
1276
1277 /*
1278 * we don't have begin lines in Base64 or plain text files.
1279 */
1280 if (data->uudet == B64ENCODED || data->uudet == QP_ENCODED ||
1281 data->uudet == PT_ENCODED)
1282 state = DATA;
1283
1284 /*
1285 * If we know that the file does not have a begin, we simulate
1286 * it in desperate mode
1287 */
1288
1289 if ((data->state & UUFILE_NOBEGIN) && uu_desperate)
1290 state = DATA;
1291
1292 (void) UUDecodeLine (NULL, NULL, 0); /* init */
1293 (void) UUbhwrite (NULL, 0, 0, NULL); /* dito */
1294 (void) UUDecodePart (NULL, NULL, NULL, 0, 0, 0, NULL); /* yep */
1295
1296 /*
1297 * initialize progress information
1298 */
1299 progress.action = 0;
1300 if (data->filename != NULL) {
1301 _FP_strncpy (progress.curfile,
1302 (strlen(data->filename)>255)?
1303 (data->filename+strlen(data->filename)-255):data->filename,
1304 256);
1305 }
1306 else {
1307 _FP_strncpy (progress.curfile,
1308 (strlen(data->binfile)>255)?
1309 (data->binfile+strlen(data->binfile)-255):data->binfile,
1310 256);
1311 }
1312 progress.partno = 0;
1313 progress.numparts = 0;
1314 progress.fsize = -1;
1315 progress.percent = 0;
1316 progress.action = UUACT_DECODING;
1317
1318 iter = data->thisfile;
1319 while (iter) {
1320 progress.numparts = (iter->partno)?iter->partno:1;
1321 iter = iter->NEXT;
1322 }
1323
1324 /*
1325 * let's rock!
1326 */
1327
1328 iter = data->thisfile;
1329 while (iter) {
1330 if (part != -1 && iter->partno != part+1)
1331 break;
1332 else
1333 part = iter->partno;
1334
1335 if (iter->data->sfname == NULL) {
1336 iter = iter->NEXT;
1337 continue;
1338 }
1339
1340 /*
1341 * call our FileCallback to retrieve the file
1342 */
1343
1344 if (uu_FileCallback) {
1345 if ((res = (*uu_FileCallback) (uu_FileCBArg, iter->data->sfname,
1346 uugen_fnbuffer, 1)) != UURET_OK)
1347 break;
1348 if ((datain = fopen (uugen_fnbuffer, "rb")) == NULL) {
1349 (*uu_FileCallback) (uu_FileCBArg, iter->data->sfname,
1350 uugen_fnbuffer, 0);
1351 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1352 uustring (S_NOT_OPEN_FILE),
1353 uugen_fnbuffer, strerror (uu_errno = errno));
1354 res = UURET_IOERR;
1355 break;
1356 }
1357 }
1358 else {
1359 if ((datain = fopen (iter->data->sfname, "rb")) == NULL) {
1360 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1361 uustring (S_NOT_OPEN_FILE),
1362 iter->data->sfname, strerror (uu_errno = errno));
1363 res = UURET_IOERR;
1364 break;
1365 }
1366 _FP_strncpy (uugen_fnbuffer, iter->data->sfname, 1024);
1367 }
1368
1369 progress.partno = part;
1370 progress.fsize = (iter->data->length)?iter->data->length:-1;
1371 progress.percent = 0;
1372 progress.foffset = iter->data->startpos;
1373
1374 fseek (datain, iter->data->startpos, SEEK_SET);
1375 res = UUDecodePart (datain, dataout, &state,
1376 iter->data->startpos+iter->data->length,
1377 data->uudet, iter->data->flags, NULL);
1378 fclose (datain);
1379
1380 if (uu_FileCallback)
1381 (*uu_FileCallback) (uu_FileCBArg, iter->data->sfname, uugen_fnbuffer, 0);
1382
1383 if (state == DONE || res != UURET_OK)
1384 break;
1385
1386 iter = iter->NEXT;
1387 }
1388
1389 if (state == DATA &&
1390 (data->uudet == B64ENCODED || data->uudet == QP_ENCODED ||
1391 data->uudet == PT_ENCODED))
1392 state = DONE; /* assume we're done */
1393
1394 fclose (dataout);
1395
1396 if (res != UURET_OK || (state != DONE && !uu_desperate)) {
1397 unlink (data->binfile);
1398 _FP_free (data->binfile);
1399 data->binfile = NULL;
1400 data->state &= ~UUFILE_TMPFILE;
1401 data->state |= UUFILE_ERROR;
1402
1403 if (res == UURET_OK && state != DONE)
1404 res = UURET_NOEND;
1405 }
1406 else if (res != UURET_OK) {
1407 data->state &= ~UUFILE_DECODED;
1408 data->state |= UUFILE_ERROR | UUFILE_TMPFILE;
1409 }
1410 else {
1411 data->state &= ~UUFILE_ERROR;
1412 data->state |= UUFILE_TMPFILE;
1413 }
1414
1415 /*
1416 * If this was a BinHex file, we must extract its data or resource fork
1417 */
1418
1419 if (data->uudet == BH_ENCODED && data->binfile) {
1420 #ifdef HAVE_MKSTEMP
1421 ntmp = malloc(strlen(tmpdir)+strlen(tmpprefix)+2);
1422
1423 if (ntmp == NULL) {
1424 #else
1425 if ((ntmp = tempnam (NULL, "uu")) == NULL) {
1426 #endif /* HAVE_MKSTEMP */
1427 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1428 uustring (S_NO_TEMP_NAME));
1429 progress.action = 0;
1430 return UURET_NOMEM;
1431 }
1432 if ((datain = fopen (data->binfile, "rb")) == NULL) {
1433 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1434 uustring (S_NOT_OPEN_FILE),
1435 data->binfile, strerror (uu_errno = errno));
1436 progress.action = 0;
1437 free (ntmp);
1438 return UURET_IOERR;
1439 }
1440 #ifdef HAVE_MKSTEMP
1441 strcpy(ntmp, tmpdir);
1442 strcat(ntmp, "/");
1443 strcat(ntmp, tmpprefix);
1444 if ((tmpfd = mkstemp(ntmp)) == -1 ||
1445 (dataout = fdopen(tmpfd, "wb")) == NULL) {
1446 #else
1447 if ((dataout = fopen (ntmp, "wb")) == NULL) {
1448 #endif /* HAVE_MKSTEMP */
1449 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1450 uustring (S_NOT_OPEN_TARGET),
1451 ntmp, strerror (uu_errno = errno));
1452 progress.action = 0;
1453 fclose (datain);
1454 #ifdef HAVE_MKSTEMP
1455 if (tmpfd != -1) {
1456 unlink(ntmp);
1457 close(tmpfd);
1458 }
1459 #endif /* HAVE_MKSTEMP */
1460 free (ntmp);
1461 return UURET_IOERR;
1462 }
1463
1464 /*
1465 * read fork lengths. remember they're in Motorola format
1466 */
1467 r[0] = fgetc (datain);
1468 hb = (int) r[0] + 22;
1469 fseek (datain, (int) r[0] + 12, SEEK_SET);
1470 fread (r, 1, 8, datain);
1471
1472 dsize = (((long) 1 << 24) * (long) r[0]) +
1473 (((long) 1 << 16) * (long) r[1]) +
1474 (((long) 1 << 8) * (long) r[2]) +
1475 ( (long) r[3]);
1476 rsize = (((long) 1 << 24) * (long) r[4]) +
1477 (((long) 1 << 16) * (long) r[5]) +
1478 (((long) 1 << 8) * (long) r[6]) +
1479 ( (long) r[7]);
1480
1481 UUMessage (uunconc_id, __LINE__, UUMSG_MESSAGE,
1482 uustring (S_BINHEX_SIZES),
1483 dsize, rsize);
1484
1485 if (dsize == 0) {
1486 fseek (datain, dsize + hb + 2, SEEK_SET);
1487 numbytes = rsize;
1488 }
1489 else if (rsize == 0) {
1490 fseek (datain, hb, SEEK_SET);
1491 numbytes = dsize;
1492 }
1493 else {
1494 /* we should let the user have the choice here */
1495 UUMessage (uunconc_id, __LINE__, UUMSG_NOTE,
1496 uustring (S_BINHEX_BOTH));
1497 fseek (datain, hb, SEEK_SET);
1498 numbytes = dsize;
1499 }
1500
1501 progress.action = 0;
1502 progress.partno = 0;
1503 progress.numparts = 1;
1504 progress.fsize = (numbytes)?numbytes:-1;
1505 progress.foffset = hb;
1506 progress.percent = 0;
1507 progress.action = UUACT_COPYING;
1508
1509 /*
1510 * copy the chosen fork
1511 */
1512
1513 while (!feof (datain) && numbytes) {
1514 if (UUBUSYPOLL(ftell(datain)-progress.foffset,progress.fsize)) {
1515 UUMessage (uunconc_id, __LINE__, UUMSG_NOTE,
1516 uustring (S_DECODE_CANCEL));
1517 fclose (datain);
1518 fclose (dataout);
1519 unlink (ntmp);
1520 free (ntmp);
1521 return UURET_CANCEL;
1522 }
1523
1524 bytes = fread (uugen_inbuffer, 1,
1525 (size_t) ((numbytes>1024)?1024:numbytes), datain);
1526
1527 if (ferror (datain) || (bytes == 0 && !feof (datain))) {
1528 progress.action = 0;
1529 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1530 uustring (S_SOURCE_READ_ERR),
1531 data->binfile, strerror (uu_errno = errno));
1532 fclose (datain);
1533 fclose (dataout);
1534 unlink (ntmp);
1535 free (ntmp);
1536 return UURET_IOERR;
1537 }
1538 if (fwrite (uugen_inbuffer, 1, bytes, dataout) != bytes) {
1539 progress.action = 0;
1540 UUMessage (uunconc_id, __LINE__, UUMSG_ERROR,
1541 uustring (S_WR_ERR_TARGET),
1542 ntmp, strerror (uu_errno = errno));
1543 fclose (datain);
1544 fclose (dataout);
1545 unlink (ntmp);
1546 free (ntmp);
1547 return UURET_IOERR;
1548 }
1549 numbytes -= bytes;
1550 }
1551
1552 if (numbytes) {
1553 UUMessage (uunconc_id, __LINE__, UUMSG_WARNING,
1554 uustring (S_SHORT_BINHEX),
1555 (data->filename)?data->filename:
1556 (data->subfname)?data->subfname:"???",
1557 numbytes);
1558 }
1559
1560 /*
1561 * replace temp file
1562 */
1563
1564 fclose (datain);
1565 fclose (dataout);
1566
1567 if (unlink (data->binfile)) {
1568 UUMessage (uunconc_id, __LINE__, UUMSG_WARNING,
1569 uustring (S_TMP_NOT_REMOVED),
1570 data->binfile, strerror (uu_errno = errno));
1571 }
1572
1573 free (data->binfile);
1574 data->binfile = ntmp;
1575 }
1576
1577 progress.action = 0;
1578 return res;
1579 }
1580
1581 /*
1582 * QuickDecode for proper MIME attachments. We expect the pointer to
1583 * be on the first header line.
1584 */
1585
1586 int
1587 UUQuickDecode (FILE *datain, FILE *dataout, char *boundary, long maxpos)
1588 {
1589 int state=BEGIN, encoding=-1;
1590 headers myenv;
1591
1592 /*
1593 * Read header and find out about encoding.
1594 */
1595
1596 memset (&myenv, 0, sizeof (headers));
1597 UUScanHeader (datain, &myenv);
1598
1599 if (_FP_stristr (myenv.ctenc, "uu") != NULL)
1600 encoding = UU_ENCODED;
1601 else if (_FP_stristr (myenv.ctenc, "xx") != NULL)
1602 encoding = XX_ENCODED;
1603 else if (_FP_stricmp (myenv.ctenc, "base64") == 0)
1604 encoding = B64ENCODED;
1605 else if (_FP_stricmp (myenv.ctenc, "quoted-printable") == 0)
1606 encoding = QP_ENCODED;
1607 else
1608 encoding = PT_ENCODED;
1609
1610 UUkillheaders (&myenv);
1611
1612 /*
1613 * okay, so decode this one
1614 */
1615
1616 (void) UUDecodePart (NULL, NULL, NULL, 0, 0, 0, NULL); /* init */
1617 return UUDecodePart (datain, dataout, &state, maxpos,
1618 encoding, FL_PROPER|FL_TOEND,
1619 boundary);
1620 }