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