ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Convert-UUlib/uulib/uunconc.c
Revision: 1.10
Committed: Mon Aug 19 23:25:36 2002 UTC (21 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.9: +4 -4 lines
Log Message:
*** empty log message ***

File Contents

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