ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Convert-UUlib/uulib/uuscan.c
Revision: 1.17
Committed: Fri Aug 28 23:25:18 2009 UTC (14 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-1_32, rel-1_31, rel-1_3
Changes since 1.16: +42 -32 lines
Log Message:
1.3

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 very central functions of UUDeview. Here, we scan a file
19     * and decide whether it contains encoded data or not. ScanPart() must
20     * be called repeatedly on the same file until feof(file). Each time,
21     * it returns information about the next part found within.
22     */
23    
24     #ifdef HAVE_CONFIG_H
25     #include "config.h"
26     #endif
27    
28     #ifdef SYSTEM_WINDLL
29     #include <windows.h>
30     #endif
31     #ifdef SYSTEM_OS2
32     #include <os2.h>
33     #endif
34    
35     #include <stdio.h>
36     #include <ctype.h>
37    
38     #ifdef STDC_HEADERS
39     #include <stdlib.h>
40     #include <string.h>
41     #endif
42     #ifdef HAVE_MALLOC_H
43     #include <malloc.h>
44     #endif
45     #ifdef HAVE_UNISTD_H
46     #include <unistd.h>
47     #endif
48     #ifdef HAVE_MEMORY_H
49     #include <memory.h>
50     #endif
51     #ifdef HAVE_ERRNO_H
52     #include <errno.h>
53     #endif
54    
55 root 1.2 #include <uudeview.h>
56 root 1.1 #include <uuint.h>
57     #include <fptools.h>
58     #include <uustring.h>
59    
60 root 1.7 char * uuscan_id = "$Id$";
61 root 1.1
62     /*
63     * Header fields we recognize as such. See RFC822. We add "From ",
64     * the usual marker for a beginning of a new message, and a couple
65     * of usual MDA, News and MIME headers.
66     * We make a distinction of MIME headers as we need the difference
67     * to scan the bodies from partial multipart messages.
68     */
69    
70 root 1.17 #define LSTR(s) { sizeof (s) - 1, s }
71    
72     struct lstr {
73     int len;
74     const char *str;
75     };
76    
77     #define MAX_KNOWNHEADERLEN 28 /* max. length of a known header */
78    
79     static struct lstr knownheaders[] = {
80     /* "From " handled in IsKnownHeader */
81    
82     /* knownmsgheaders */
83     LSTR ("Return-Path"), LSTR ("Received"), LSTR ("Reply-To"),
84     LSTR ("From"), LSTR ("Sender"), LSTR ("Resent-Reply-To"), LSTR ("Resent-From"),
85     LSTR ("Resent-Sender"), LSTR ("Date"), LSTR ("Resent-Date"), LSTR ("To"),
86     LSTR ("Resent-To"), LSTR ("Cc"), LSTR ("Bcc"), LSTR ("Resent-bcc"),
87     LSTR ("Message-ID"), LSTR ("Resent-Message-Id"), LSTR ("In-Reply-To"),
88     LSTR ("References"), LSTR ("Keywords"), LSTR ("Subject"), LSTR ("Comments"),
89 root 1.1
90 root 1.17 LSTR ("Delivery-Date"), LSTR ("Posted-Date"), LSTR ("Received-Date"),
91     LSTR ("Precedence"),
92 root 1.1
93 root 1.17 LSTR ("Path"), LSTR ("Newsgroups"), LSTR ("Organization"), LSTR ("Lines"),
94     LSTR ("NNTP-Posting-Host"),
95 root 1.1
96 root 1.17 /* knownminehaders */
97     LSTR ("Mime-Version"), LSTR ("Content-Transfer-Encoding"),
98     LSTR ("Content-Type"), LSTR ("Content-Disposition"),
99     LSTR ("Content-Description"), LSTR ("Content-Length")
100 root 1.1 };
101    
102     /*
103     * for MIME (plaintext) parts without filename
104     */
105     int mimseqno;
106    
107     /*
108     * how many lines do we read when checking for headers
109     */
110     #define WAITHEADER 10
111    
112     /*
113     * The stack for encapsulated Multipart messages
114     */
115     #define MSMAXDEPTH 3
116    
117     int mssdepth = 0;
118     scanstate multistack[MSMAXDEPTH+1];
119    
120     /*
121     * The state and the local envelope
122     */
123     headers localenv;
124     scanstate sstate;
125    
126     /*
127     * mallocable areas
128     */
129    
130     char *uuscan_shlline;
131 root 1.7 char *uuscan_shlline2;
132 root 1.1 char *uuscan_pvvalue;
133     char *uuscan_phtext;
134     char *uuscan_sdline;
135     char *uuscan_sdbhds1;
136     char *uuscan_sdbhds2;
137     char *uuscan_spline;
138    
139     /*
140     * Macro: print cancellation message in UUScanPart
141     */
142    
143     #define SPCANCEL() {UUMessage(uuscan_id,__LINE__,UUMSG_NOTE,uustring(S_SCAN_CANCEL));*errcode=UURET_CANCEL;goto ScanPartEmergency;}
144    
145     /*
146     * Is line empty? A line is empty if it is composed of whitespace.
147     */
148    
149     static int
150     IsLineEmpty (char *data)
151     {
152     if (data == NULL) return 0;
153     while (*data && isspace (*data)) data++;
154     return ((*data)?0:1);
155     }
156    
157     /*
158 root 1.7 * Is this a header line? A header line has alphanumeric characters
159     * followed by a colon.
160     */
161    
162     static int
163     IsHeaderLine (char *data)
164     {
165     if (data == NULL) return 0;
166     if (*data == ':') return 0;
167 root 1.9 while (*data && (isalnum (*data) || *data=='-')) data++;
168 root 1.7 return (*data == ':') ? 1 : 0;
169     }
170    
171     /*
172 root 1.1 * Scans a potentially folded header line from the input file. If
173     * initial is non-NULL, it is the first line of the header, useful
174     * if the calling function just coincidentally noticed that this is
175     * a header.
176     * RFC0822 does not specify a maximum length for headers, but we
177     * truncate everything beyond an insane value of 1024 characters.
178     */
179 root 1.16 /* (schmorp)every later rfc says 998 octets max */
180 root 1.1
181     static char *
182     ScanHeaderLine (FILE *datei, char *initial)
183     {
184     char *ptr=uuscan_shlline;
185 root 1.7 char *ptr2, *p1, *p2, *p3;
186 root 1.1 int llength, c;
187     long curpos;
188    
189     if (initial) {
190 root 1.5 _FP_strncpy (uuscan_shlline, initial, 1024);
191 root 1.15 } else {
192 root 1.1 /* read first line */
193     if (feof (datei) || ferror (datei))
194     return NULL;
195 root 1.15 if (_FP_fgets (uuscan_shlline, 1024, datei) == NULL)
196 root 1.1 return NULL;
197     }
198    
199     llength = strlen (uuscan_shlline);
200    
201     /* strip whitespace at end */
202     ptr = uuscan_shlline + llength;
203     while (llength && isspace(*(ptr-1))) {
204     ptr--; llength--;
205     }
206 root 1.16 if (llength == 0)
207 root 1.1 return uuscan_shlline;
208    
209     while (!feof (datei)) {
210 root 1.16 c = _FP_fgetc (datei);
211     if (c == EOF)
212 root 1.1 break;
213    
214 root 1.16 if (c != ' ' && c != '\t')
215     {
216     ungetc (c, datei);
217     break;
218     }
219 root 1.1
220     while (!feof (datei) && (c == ' ' || c == '\t'))
221 root 1.16 c = _FP_fgetc (datei);
222 root 1.1
223     if (!feof (datei))
224     ungetc (c, datei); /* push back for fgets() */
225 root 1.16 else
226 root 1.1 break;
227    
228     /* read next line */
229     curpos = ftell (datei);
230 root 1.15 if (_FP_fgets (uugen_inbuffer, 1024, datei) == NULL)
231 root 1.1 break;
232    
233     if (IsLineEmpty (uugen_inbuffer)) { /* oops */
234     fseek (datei, curpos, SEEK_SET);
235     break;
236     }
237    
238 root 1.5 _FP_strncpy (ptr, uugen_inbuffer, 1024-llength);
239 root 1.1
240     c = strlen (ptr);
241    
242     /*
243     * strip whitespace
244     */
245    
246     ptr += c;
247     llength += c;
248     while (llength && isspace(*(ptr-1))) {
249     ptr--; llength--;
250     }
251     }
252 root 1.7
253 root 1.1 *ptr = '\0';
254    
255     if (llength == 0)
256     return NULL;
257    
258 root 1.7 /*
259     * Now that we've read the header line, we can RFC 1522-decode it
260     */
261    
262     ptr = uuscan_shlline;
263     ptr2 = uuscan_shlline2;
264    
265     while (*ptr) {
266     /*
267     * Look for =? magic
268     */
269    
270     if (*ptr == '=' && *(ptr+1) == '?') {
271     /*
272     * Let p1 point to the charset, look for next question mark
273     */
274    
275     p1 = p2 = ptr+2;
276    
277     while (*p2 && *p2 != '?') {
278     p2++;
279     }
280    
281     if (*p2 == '?' &&
282     (*(p2+1) == 'q' || *(p2+1) == 'Q' ||
283     *(p2+1) == 'b' || *(p2+1) == 'B') &&
284     *(p2+2) == '?') {
285     /*
286     * Let p2 point to the encoding, look for ?= magic
287     */
288    
289     p2++;
290     p3=p2+2;
291    
292     while (*p3 && (*p3 != '?' || *(p3+1) != '=')) {
293     p3++;
294     }
295    
296     if (*p3 == '?' && *(p3+1) == '=') {
297     /*
298     * Alright, we've found an RFC 1522 header field
299     */
300     if (*p2 == 'q' || *p2 == 'Q') {
301     c = UUDecodeField (p2+2, ptr2, QP_ENCODED);
302     }
303     else if (*p2 == 'b' || *p2 == 'B') {
304     c = UUDecodeField (p2+2, ptr2, B64ENCODED);
305     }
306     if (c >= 0) {
307     ptr2 += c;
308     ptr = p3+2;
309     continue;
310     }
311     }
312     }
313     }
314    
315     *ptr2++ = *ptr++;
316     }
317    
318     *ptr2 = 0;
319    
320     return uuscan_shlline2;
321 root 1.1 }
322    
323     /*
324     * Extract the value from a MIME attribute=value pair. This function
325     * receives a pointer to the attribute.
326     */
327     static char *
328     ParseValue (char *attribute)
329     {
330     char *ptr=uuscan_pvvalue;
331     int length=0;
332    
333     if (attribute == NULL)
334     return NULL;
335    
336     while ((isalnum(*attribute) || *attribute=='_') && *attribute != '=')
337     attribute++;
338    
339     while (isspace(*attribute))
340     attribute++;
341    
342     if (*attribute == '=') {
343     attribute++;
344     while (isspace (*attribute))
345     attribute++;
346     }
347     else
348     return NULL;
349    
350     if (*attribute == '"') {
351     /* quoted-string */
352     attribute++;
353     while (*attribute && *attribute != '"' && length < 255) {
354     if (*attribute == '\\'
355     && (attribute[1] == '"'
356     || attribute[1] == '\015'
357     || attribute[1] == '\\'))
358     /* we dequote only the three characters that MUST be quoted, since
359     * microsoft is obviously unable to correctly implement even mime headers:
360     * filename="c:\xxx". *sigh*
361     */
362     *ptr++ = *++attribute;
363     else
364     *ptr++ = *attribute;
365     attribute++;
366     length++;
367     }
368     *ptr = '\0';
369     }
370     else {
371     /* tspecials from RFC1521 */
372 root 1.7 /*
373     * Note - exclude '[', ']' and ';' on popular request; these are
374     * used in some Content-Type fields by the Klez virus, and people
375     * who feed their virus scanners with the output of UUDeview would
376     * like to catch it!
377     */
378 root 1.1
379     while (*attribute && !isspace (*attribute) &&
380     *attribute != '(' && *attribute != ')' &&
381     *attribute != '<' && *attribute != '>' &&
382     *attribute != '@' && *attribute != ',' &&
383 root 1.7 /* *attribute != ';' && */ *attribute != ':' &&
384 root 1.1 *attribute != '\\' &&*attribute != '"' &&
385 root 1.7 *attribute != '/' && /* *attribute != '[' &&
386     *attribute != ']' && */ *attribute != '?' &&
387 root 1.9 *attribute != '=' && length < 255) {
388 root 1.1 *ptr++ = *attribute++;
389 root 1.9 length++;
390     }
391 root 1.1
392     *ptr = '\0';
393     }
394     return uuscan_pvvalue;
395     }
396    
397     /*
398     * Extract the information we need from header fields
399     */
400    
401     static headers *
402     ParseHeader (headers *theheaders, char *line)
403     {
404     char **variable=NULL;
405     char *value, *ptr, *thenew;
406     int delimit, length;
407    
408 root 1.2 value = 0; delimit = 0; /* calm down gcc */
409 root 1.1
410     if (line == NULL)
411     return theheaders;
412    
413 root 1.5 if (_FP_strnicmp (line, "From:", 5) == 0) {
414 root 1.1 if (theheaders->from) return theheaders;
415     variable = &theheaders->from;
416     value = line+5;
417     delimit = 0;
418     }
419 root 1.5 else if (_FP_strnicmp (line, "Subject:", 8) == 0) {
420 root 1.1 if (theheaders->subject) return theheaders;
421     variable = &theheaders->subject;
422     value = line+8;
423     delimit = 0;
424     }
425 root 1.5 else if (_FP_strnicmp (line, "To:", 3) == 0) {
426 root 1.1 if (theheaders->rcpt) return theheaders;
427     variable = &theheaders->rcpt;
428     value = line+3;
429     delimit = 0;
430     }
431 root 1.5 else if (_FP_strnicmp (line, "Date:", 5) == 0) {
432 root 1.1 if (theheaders->date) return theheaders;
433     variable = &theheaders->date;
434     value = line+5;
435     delimit = 0;
436     }
437 root 1.5 else if (_FP_strnicmp (line, "Mime-Version:", 13) == 0) {
438 root 1.1 if (theheaders->mimevers) return theheaders;
439     variable = &theheaders->mimevers;
440     value = line+13;
441     delimit = 0;
442     }
443 root 1.5 else if (_FP_strnicmp (line, "Content-Type:", 13) == 0) {
444 root 1.1 if (theheaders->ctype) return theheaders;
445     variable = &theheaders->ctype;
446     value = line+13;
447     delimit = ';';
448    
449     /* we can probably extract more information */
450 root 1.5 if ((ptr = _FP_stristr (line, "boundary")) != NULL) {
451 root 1.1 if ((thenew = ParseValue (ptr))) {
452     if (theheaders->boundary) free (theheaders->boundary);
453 root 1.5 theheaders->boundary = _FP_strdup (thenew);
454 root 1.1 }
455     }
456 root 1.5 if ((ptr = _FP_stristr (line, "name")) != NULL) {
457 root 1.1 if ((thenew = ParseValue (ptr))) {
458     if (theheaders->fname) free (theheaders->fname);
459 root 1.5 theheaders->fname = _FP_strdup (thenew);
460 root 1.1 }
461     }
462 root 1.5 if ((ptr = _FP_stristr (line, "id")) != NULL) {
463 root 1.1 if ((thenew = ParseValue (ptr))) {
464     if (theheaders->mimeid) free (theheaders->mimeid);
465 root 1.5 theheaders->mimeid = _FP_strdup (thenew);
466 root 1.1 }
467     }
468 root 1.5 if ((ptr = _FP_stristr (line, "number")) != NULL) {
469 root 1.1 if ((thenew = ParseValue (ptr))) {
470     theheaders->partno = atoi (thenew);
471     }
472     }
473 root 1.5 if ((ptr = _FP_stristr (line, "total")) != NULL) {
474 root 1.1 if ((thenew = ParseValue (ptr))) {
475     theheaders->numparts = atoi (thenew);
476     }
477     }
478     }
479 root 1.5 else if (_FP_strnicmp (line, "Content-Transfer-Encoding:", 26) == 0) {
480 root 1.1 if (theheaders->ctenc) return theheaders;
481     variable = &theheaders->ctenc;
482     value = line+26;
483     delimit = ';';
484     }
485 root 1.5 else if (_FP_strnicmp (line, "Content-Disposition:", 20) == 0) {
486 root 1.1 /*
487     * Some encoders mention the original filename as parameter to
488     * Content-Type, others as parameter to Content-Disposition. We
489     * do prefer the first solution, but accept this situation, too.
490     * TODO: Read RFC1806
491     */
492 root 1.5 if ((ptr = _FP_stristr (line, "name")) != NULL) {
493 root 1.1 if (theheaders->fname == NULL && (thenew=ParseValue(ptr)) != NULL) {
494 root 1.5 theheaders->fname = _FP_strdup (thenew);
495 root 1.1 }
496     }
497     variable = NULL;
498     }
499     else {
500     /*
501     * nothing interesting
502     */
503 root 1.4 return theheaders;
504 root 1.1 }
505    
506     /*
507     * okay, so extract the actual data
508     */
509     if (variable) {
510     length = 0;
511     ptr = uuscan_phtext;
512    
513     while (isspace (*value))
514     value++;
515     while (*value && (delimit==0 || *value!=delimit) &&
516 root 1.16 *value && length < 255) {
517 root 1.1 *ptr++ = *value++;
518     length++;
519     }
520     while (length && isspace(*(ptr-1))) {
521     ptr--; length--;
522     }
523     *ptr = '\0';
524    
525 root 1.5 if ((*variable = _FP_strdup (uuscan_phtext)) == NULL)
526 root 1.1 return NULL;
527     }
528    
529     return theheaders;
530     }
531    
532     /*
533     * is this a header line we know about?
534     */
535    
536     static int
537     IsKnownHeader (char *line)
538     {
539 root 1.17 const char *sep;
540     int len, i;
541    
542     /* "From " handled specially */
543     /* we assume the buffer is at least 5 bytes long */
544     if (line [4] == ' ' && line [1] == 'r' && line [2] == 'o' && line [3] == 'm'
545     && (line [0] == 'f' || line [0] == 'F'))
546     return 1;
547    
548     sep = memchr (line, ':', MAX_KNOWNHEADERLEN);
549 root 1.1
550 root 1.15 /* fast reject, the majority of calls are simple rejects */
551 root 1.17 if (!sep)
552 root 1.15 return 0;
553    
554 root 1.17 len = sep - line; /* length of part before ':' */
555    
556     for (i = 0; i < sizeof (knownheaders) / sizeof (knownheaders [0]); ++i)
557     if (len == knownheaders [i].len && _FP_strnicmp (line, knownheaders [i].str, len) == 0)
558 root 1.1 return 1;
559    
560     return 0;
561     }
562    
563     /*
564     * Scan a header
565     */
566    
567     int
568     UUScanHeader (FILE *datei, headers *envelope)
569     {
570     char *ptr;
571    
572     while (!feof (datei)) {
573     if ((ptr = ScanHeaderLine (datei, NULL)) == NULL)
574     break;
575 root 1.16 if (*ptr == '\0')
576 root 1.1 break;
577     ParseHeader (envelope, ptr);
578     }
579     return 0;
580     }
581    
582     /*
583     * Scan something for encoded data and fill the fileread* structure.
584     * If boundary is non-NULL, we stop after having read it. If Check-
585     * Headers != 0, we also stop after we've found uu_headercount recog-
586     * nized header lines.
587     * If we have a boundary, then we also don't accept Base64; MIME mails
588     * really should handle this properly.
589     * We return -1 if something went wrong, 0 if everything is normal,
590     * 1 if we found a new header and 2 if we found a boundary.
591     * In MIME message bodies (not multiparts), we also disable our reduced
592     * MIME handling.
593     */
594    
595     static int
596     ScanData (FILE *datei, char *fname, int *errcode,
597     char *boundary, int ismime, int checkheaders,
598     fileread *result)
599     {
600     char *line=uuscan_sdline, *bhds1=uuscan_sdbhds1, *bhds2=uuscan_sdbhds2;
601     static char *ptr, *p2, *p3=NULL, *bhdsp, bhl;
602 root 1.7 int islen[10], isb64[10], isuue[10], isxxe[10], isbhx[10], iscnt;
603 root 1.2 int cbb64, cbuue, cbxxe, cbbhx;
604 root 1.1 int bhflag=0, vflag, haddh=0, hadct=0;
605 root 1.4 int bhrpc=0, bhnf=0, c, hcount, lcount, blen=0;
606 root 1.1 int encoding=0, dflag=0, ctline=42;
607     int dontcare=0, hadnl=0;
608 root 1.4 long preheaders=0, oldposition;
609 root 1.14 long yepartends=0;
610 root 1.1 size_t dcc, bhopc;
611    
612     *errcode = UURET_OK;
613     (void) UUDecodeLine (NULL, NULL, 0); /* init */
614     bhdsp = bhds2;
615    
616     if (datei == NULL || feof (datei))
617     return -1;
618    
619     result->startpos = ftell (datei);
620     hcount = lcount = 0;
621    
622 root 1.2 for (iscnt=0; iscnt<10; iscnt++) {
623     isb64[iscnt] = isuue[iscnt] = isxxe[iscnt] = isbhx[iscnt] = 0;
624 root 1.7 islen[iscnt] = -1;
625 root 1.2 }
626    
627     iscnt = 0;
628    
629 root 1.1 if (boundary)
630     blen = strlen (boundary);
631    
632     while (!feof (datei)) {
633     oldposition = ftell (datei);
634 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL)
635 root 1.1 break;
636     if (ferror (datei))
637     break;
638    
639     /*
640     * Make Busy Polls
641     */
642 root 1.2
643 root 1.1 if (UUBUSYPOLL(ftell(datei),progress.fsize)) {
644     UUMessage (uuscan_id, __LINE__, UUMSG_NOTE,
645     uustring (S_SCAN_CANCEL));
646     *errcode = UURET_CANCEL;
647     break;
648     }
649    
650 root 1.2 if (IsLineEmpty (line)) { /* line empty? */
651     hcount = 0;
652     hadnl = 1;
653     continue; /* then ignore */
654     }
655    
656 root 1.1 if (checkheaders) {
657     if (IsKnownHeader (line)) {
658     (void) ScanHeaderLine (datei, line);
659    
660     if (hcount == 0) {
661     preheaders = oldposition;
662     lcount = 0;
663     }
664     hcount++;
665     lcount++;
666    
667     /*
668     * check for the various restart counters
669     */
670    
671     if ((hcount >= hlcount.restart) ||
672     (hcount >= hlcount.afterdata && ismime == 0) ||
673     (hcount >= hlcount.afterdata && result->uudet) ||
674     (hcount >= hlcount.afternl && result->uudet && hadnl)) {
675     /*
676     * Hey, a new header starts here
677     */
678     fseek (datei, preheaders, SEEK_SET);
679     break;
680     }
681     /* continue; */
682     }
683     else if (lcount > WAITHEADER) {
684     hcount = 0;
685     lcount = 0;
686     dontcare=0;
687     }
688     else if (hcount) {
689     lcount++;
690     dontcare=1;
691     }
692     else {
693     dontcare=0;
694     }
695     }
696     else {
697     dontcare=0;
698     }
699 root 1.2
700 root 1.1 if (boundary != NULL &&
701     line[0] == '-' && line[1] == '-' &&
702     strncmp (line+2, boundary, blen) == 0) {
703     fseek (datei, oldposition, SEEK_SET);
704     break;
705     }
706     if (boundary != NULL && line[0] == 'C' && line[1] == 'o' &&
707 root 1.5 _FP_strnicmp (line, "Content-Type:", 13) == 0) {
708 root 1.1 ptr = ScanHeaderLine (datei, line);
709 root 1.5 p2 = (ptr)?_FP_stristr(ptr,"boundary"):NULL;
710 root 1.1 p3 = (p2)?ParseValue(p2):NULL;
711    
712     if (p3 && strcmp (p3, boundary) == 0) {
713     fseek (datei, oldposition, SEEK_SET);
714     break;
715     }
716     else {
717     p3 = NULL;
718     }
719     }
720    
721     if (strncmp (line, "begin ", 6) == 0 ||
722 root 1.5 _FP_strnicmp (line, "<pre>begin ", 11) == 0) {
723 root 1.2 if ((result->begin || result->end ||
724     result->uudet == B64ENCODED ||
725     result->uudet == BH_ENCODED) && !uu_more_mime) {
726 root 1.1 fseek (datei, oldposition, SEEK_SET);
727     break;
728     }
729    
730     if (*line == '<')
731     ptr = line + 10;
732     else
733     ptr = line + 5;
734    
735     while (*ptr == ' ') ptr++;
736     while (isdigit (*ptr))
737     result->mode = result->mode * 8 + *ptr++ - '0';
738     while (*ptr == ' ') ptr++;
739    
740     /*
741     * We may have picked up a filename from a uuenview-style header
742     */
743 root 1.5 _FP_free (result->filename);
744     result->filename = _FP_strdup (ptr);
745 root 1.1 result->begin = 1;
746    
747     while (isspace (result->filename[strlen(result->filename)-1]))
748     result->filename[strlen(result->filename)-1] = '\0';
749    
750     continue;
751     }
752    
753 root 1.4 if ((strncmp (line, "end", 3) == 0) &&
754     result->uudet != BH_ENCODED &&
755     result->uudet != YENC_ENCODED) {
756 root 1.1 if (result->uudet == B64ENCODED && result->begin)
757     result->uudet = XX_ENCODED;
758    
759     if (result->uudet != B64ENCODED) {
760     result->end = 1;
761     if (dflag && encoding)
762     result->uudet = encoding;
763     continue;
764     }
765     }
766    
767     hadnl = 0;
768    
769     /*
770     * Detect a UUDeview-Style header
771     */
772    
773 root 1.5 if (_FP_strnicmp (line, "_=_ Part ", 9) == 0 &&
774 root 1.4 result->uudet != YENC_ENCODED) {
775 root 1.1 if (result->uudet) {
776     fseek (datei, oldposition, SEEK_SET);
777     break;
778     }
779     result->partno = atoi (line + 8);
780 root 1.5 if ((ptr = _FP_stristr (line, "of file ")) != NULL) {
781 root 1.1 ptr += 8;
782     while (isspace (*ptr)) ptr++;
783     p2 = ptr;
784     while (isalnum(*p2) ||
785     *p2 == '.' || *p2=='_' || *p2 == '-' ||
786     *p2 == '!' || *p2=='@' || *p2 == '$')
787     p2++;
788     c = *p2; *p2 = '\0';
789     if (p2 != ptr && result->filename == NULL)
790 root 1.5 result->filename = _FP_strdup (ptr);
791 root 1.1 else if (p2 - ptr > 5 && strchr (ptr, '.') != NULL) {
792     /*
793     * This file name looks good, too. Let's use it
794     */
795 root 1.5 _FP_free (result->filename);
796     result->filename = _FP_strdup (ptr);
797 root 1.1 }
798     *p2 = c;
799     }
800     }
801    
802     /*
803     * Some reduced MIME handling. Only use if boundary == NULL. Also
804     * accept the "X-Orcl-Content-Type" used by some braindead program.
805     */
806 root 1.4 if (boundary == NULL && !ismime && !uu_more_mime &&
807     result->uudet != YENC_ENCODED) {
808 root 1.5 if (_FP_strnicmp (line, "Content-Type", 12) == 0 ||
809     _FP_strnicmp (line, "X-Orcl-Content-Type", 19) == 0) {
810 root 1.1 /*
811     * We use Content-Type to mark a new attachment and split the file.
812     * However, we do not split if we haven't found anything encoded yet.
813     */
814     if (result->uudet) {
815     fseek (datei, oldposition, SEEK_SET);
816     break;
817     }
818     if ((ptr = strchr (line, ':')) != NULL) {
819     ptr++;
820     while (isspace (*ptr)) ptr++; p2 = ptr;
821     while (!isspace (*p2) && *p2 != ';') p2++;
822     c = *p2; *p2 = '\0';
823     if (p2 != ptr) {
824 root 1.5 _FP_free (result->mimetype);
825     result->mimetype = _FP_strdup (ptr);
826 root 1.1 }
827     *p2 = c;
828     }
829     ctline=0;
830     hadct=1;
831     }
832 root 1.5 if ((ptr = _FP_stristr (line, "number=")) && ctline<4) {
833 root 1.1 ptr += 7; if (*ptr == '"') ptr++;
834     result->partno = atoi (ptr);
835     }
836 root 1.5 if ((ptr = _FP_stristr (line, "total=")) && ctline<4) {
837 root 1.1 ptr += 6; if (*ptr == '"') ptr++;
838     result->maxpno = atoi (ptr);
839     }
840 root 1.5 if ((ptr = _FP_stristr (line, "name=")) && ctline<4) {
841 root 1.1 ptr += 5;
842     while (isspace (*ptr)) ptr++;
843     if (*ptr == '"' && *(ptr+1) && (p2 = strchr (ptr+2, '"')) != NULL) {
844     c = *p2; *p2 = '\0';
845 root 1.5 _FP_free (result->filename);
846     result->filename = _FP_strdup (ptr+1);
847 root 1.1 *p2 = c;
848     }
849     else if (*ptr=='\''&&*(ptr+1)&&(p2 = strchr(ptr+2, '\'')) != NULL) {
850     c = *p2; *p2 = '\0';
851 root 1.5 _FP_free (result->filename);
852     result->filename = _FP_strdup (ptr+1);
853 root 1.1 *p2 = c;
854     }
855     else {
856     p2 = ptr;
857     while (isalnum(*p2) ||
858     *p2 == '.' || *p2=='_' || *p2 == '-' ||
859     *p2 == '!' || *p2=='@' || *p2 == '$')
860     p2++;
861     c = *p2; *p2 = '\0';
862     if (p2 != ptr && result->filename == NULL)
863 root 1.5 result->filename = _FP_strdup (ptr);
864 root 1.1 else if (p2 - ptr > 5 && strchr (ptr, '.') != NULL) {
865     /*
866     * This file name looks good, too. Let's use it
867     */
868 root 1.5 _FP_free (result->filename);
869     result->filename = _FP_strdup (ptr);
870 root 1.1 }
871     *p2 = c;
872     }
873     }
874 root 1.5 if ((ptr = _FP_stristr (line, "id=")) && ctline<4) {
875 root 1.1 p2 = ptr += 3;
876     if (*p2 == '"') {
877     p2 = strchr (++ptr, '"');
878     }
879     else {
880     while (*p2 && isprint(*p2) && !isspace(*p2) && *p2 != ';')
881     p2++;
882     }
883     if (p2 && *p2 && p2!=ptr) {
884     c = *p2; *p2 = '\0';
885     if (result->mimeid)
886 root 1.5 _FP_free (result->mimeid);
887     result->mimeid = _FP_strdup (ptr);
888 root 1.1 *p2 = c;
889     }
890     }
891    
892     /*
893     * Handling for very short Base64 files.
894     */
895 root 1.2 if (uu_tinyb64 && !ismime && !uu_more_mime) {
896 root 1.1 if (line[0] == '-' && line[1] == '-') {
897     if (dflag && (encoding==B64ENCODED || result->uudet==B64ENCODED)) {
898     if (encoding==B64ENCODED && result->uudet==0 && (haddh||hadct)) {
899     result->uudet = encoding;
900     encoding = dflag = 0;
901     }
902     haddh = 1;
903     continue;
904     }
905     hadct = 0;
906     }
907     }
908     } /* end of reduced MIME handling */
909    
910     /*
911 root 1.2 * If we're in "freestyle" mode, have not encountered anything
912     * interesting yet, and stumble upon something that looks like
913     * a boundary, followed by a Content-* line, try to use it.
914     */
915    
916     if (boundary == NULL && !ismime && !uu_more_mime && dflag <= 1 &&
917     line[0] == '-' && line[1] == '-' && strlen(line+2)>10 &&
918 root 1.5 (((ptr = _FP_strrstr (line+2, "--")) == NULL) ||
919 root 1.16 ptr[2]) &&
920 root 1.5 _FP_strstr (line+2, "_=_") != NULL) {
921 root 1.12
922     long oldposition = ftell (datei); /* refresh oldpositition so the comment below becomes true */
923    
924 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL) {
925 root 1.2 break;
926     }
927 root 1.5 if (_FP_strnicmp (line, "Content-", 8) == 0) {
928 root 1.2 /*
929     * Okay, let's do it. This breaks out of ScanData. ScanPart will
930     * recognize the boundary on the next call and use it.
931     */
932     fseek (datei, oldposition, SEEK_SET);
933     break;
934     }
935     }
936    
937     /*
938 root 1.4 * Detection for yEnc encoding
939     */
940    
941     if (strncmp (line, "=ybegin ", 8) == 0 &&
942 root 1.5 _FP_strstr (line, " name=") != NULL) {
943 root 1.7 if ((result->begin || result->end || result->uudet) && !uu_more_mime) {
944 root 1.4 fseek (datei, oldposition, SEEK_SET);
945     break;
946     }
947    
948     /*
949     * name continues to the end of the line
950     */
951    
952 root 1.5 ptr = _FP_strstr (line, " name=") + 6;
953 root 1.4
954 root 1.11 /* newsbin pro 5.0 (at least) is braindamaged enough to put (null) here */
955     /* create something sensible, trust a windows program to fuck it up */
956     if (strncmp (ptr, "(null)", 6))
957     {
958     _FP_free (result->filename);
959     result->filename = _FP_strdup (ptr);
960    
961     while (isspace (result->filename[strlen(result->filename)-1]))
962     result->filename[strlen(result->filename)-1] = '\0';
963     }
964 root 1.4
965     /*
966     * Determine size
967     */
968    
969 root 1.6 if ((ptr = _FP_strstr (line, " size=")) != NULL) {
970     ptr += 6;
971 root 1.14 result->yefilesize = atoi (ptr);
972 root 1.6 }
973     else {
974 root 1.14 result->yefilesize = -1;
975 root 1.6 }
976 root 1.4
977     /*
978     * check for multipart file and read =ypart line
979     */
980    
981 root 1.5 if ((ptr = _FP_strstr (line, " part=")) != NULL) {
982 root 1.4 result->partno = atoi (ptr + 6);
983    
984     if (result->partno == 1) {
985     result->begin = 1;
986     }
987    
988 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL) {
989 root 1.4 break;
990     }
991    
992     if (strncmp (line, "=ypart ", 7) != 0) {
993     break;
994     }
995    
996 root 1.5 if ((ptr = _FP_strstr (line, " end=")) == NULL) {
997 root 1.4 break;
998     }
999    
1000     yepartends = atoi (ptr + 5);
1001     }
1002     else {
1003     result->partno = 1;
1004     result->begin = 1;
1005     }
1006    
1007     /*
1008     * Don't want auto-detection
1009     */
1010    
1011     result->uudet = YENC_ENCODED;
1012     continue;
1013     }
1014    
1015     if (strncmp (line, "=yend ", 6) == 0 &&
1016     result->uudet == YENC_ENCODED) {
1017 root 1.14 if (yepartends == 0 || yepartends >= result->yefilesize) {
1018 root 1.4 result->end = 1;
1019     }
1020 root 1.7 #if 0
1021 root 1.4 if (!uu_more_mime)
1022     break;
1023 root 1.7 #endif
1024     continue;
1025 root 1.4 }
1026    
1027     /*
1028 root 1.1 * if we haven't yet found anything encoded, try to find something
1029     */
1030    
1031     if (!(result->uudet)) {
1032     /*
1033     * Netscape-Repair code is the same as in uunconc.c
1034     */
1035    
1036 root 1.2 if ((vflag = UUValidData (line, 0, &bhflag)) == 0 && !ismime)
1037 root 1.1 vflag = UURepairData (datei, line, 0, &bhflag);
1038    
1039     /*
1040 root 1.2 * Check data against all possible encodings
1041     */
1042    
1043 root 1.7 islen[iscnt%10] = strlen(line);
1044 root 1.2 isb64[iscnt%10] = (UUValidData (line, B64ENCODED, &bhflag)==B64ENCODED);
1045     isuue[iscnt%10] = (UUValidData (line, UU_ENCODED, &bhflag)==UU_ENCODED);
1046     isxxe[iscnt%10] = (UUValidData (line, XX_ENCODED, &bhflag)==XX_ENCODED);
1047     isbhx[iscnt%10] = (UUValidData (line, BH_ENCODED, &bhflag)==BH_ENCODED);
1048    
1049     /*
1050     * If we've got a first valid encoded line, we get suspicious if
1051     * it's shorter than, say, 40 characters.
1052     */
1053    
1054     if (vflag == B64ENCODED &&
1055     (dflag == 0 || encoding != B64ENCODED) &&
1056     strlen (line) < 40 && !result->begin && !uu_tinyb64) {
1057     isb64[iscnt%10] = 0;
1058     vflag = 0;
1059 root 1.1 }
1060    
1061 root 1.2 if ((vflag == UU_ENCODED || vflag == XX_ENCODED) &&
1062     (dflag == 0 || encoding != vflag) &&
1063     strlen (line) < 40 && !result->begin) {
1064     isuue[iscnt%10] = isxxe[iscnt%10] = 0;
1065     vflag = 0;
1066     }
1067    
1068     iscnt++;
1069    
1070     /*
1071     * Ah, so we got an encoded line? How interesting!
1072     */
1073    
1074 root 1.1 if (vflag) {
1075     /*
1076     * For BinHex data, we can use the initial colon ':' as begin
1077     * and the terminating colon as ':'.
1078     * If (vflag && !bhflag), this is the last line,
1079     */
1080     if (vflag == BH_ENCODED) {
1081 root 1.2 if (line[0] == ':' && result->end) {
1082     fseek (datei, oldposition, SEEK_SET);
1083     break;
1084     }
1085 root 1.1 if (line[0] == ':')
1086     result->begin = 1;
1087     if (bhflag == 0) {
1088     result->uudet = BH_ENCODED;
1089     result->end = 1;
1090     }
1091     }
1092     /*
1093     * For BinHex files, the file name is encoded in the first encoded
1094     * data bytes. We try to extract it here
1095     */
1096     if (vflag == BH_ENCODED && bhnf == 0 && result->filename == NULL) {
1097     if (bhdsp == bhds2 ||
1098     ((bhdsp-bhds2) <= (int) bhds2[0] &&
1099     (bhdsp-bhds2) < 256)) {
1100     dcc = UUDecodeLine (line, bhds1, BH_ENCODED);
1101     UUbhdecomp (bhds1, bhdsp, &bhl, &bhrpc,
1102     dcc, 256-(bhdsp-bhds2), &bhopc);
1103     bhdsp += bhopc;
1104     }
1105     if ((bhdsp-bhds2) > (int) bhds2[0] && bhds2[0]>0 &&
1106     result->filename==NULL) {
1107     memcpy (bhds1, bhds2+1, (int) bhds2[0]);
1108     bhds1[(int)bhds2[0]]='\0';
1109 root 1.5 result->filename = _FP_strdup (bhds1);
1110 root 1.1 bhnf = 1;
1111     }
1112     else if (bhdsp-bhds2 >= 256 && bhds2[0]>0) {
1113     memcpy (bhds1, bhds2+1, 255);
1114     bhds1[255] = '\0';
1115 root 1.5 result->filename = _FP_strdup (bhds1);
1116 root 1.1 bhnf = 1;
1117     }
1118     else if (bhds2[0] <= 0)
1119     bhnf = 1;
1120     }
1121 root 1.2
1122 root 1.1 /*
1123 root 1.2 * We accept an encoding if it has been true for four consecutive
1124     * lines. Check the is<enc> arrays to avoid mistaking one encoding
1125     * for the other. Uuencoded data is rather easily mistaken for
1126     * Base 64. If the data matches more than one encoding, we need to
1127     * scan further.
1128 root 1.7 *
1129     * Since text can also rather easily be mistaken for UUencoded
1130     * data if it just happens to have 4 lines in a row that have the
1131     * correct first character for the length of the line, we also add
1132     * a check that the first 3 lines must be the same length, and the
1133     * 4th line must be less than or equal to that length. (since
1134     * uuencoders use the same length for all lines except the last,
1135     * this shouldn't increase the minimum size of UUdata we can
1136     * detect, as it would if we tested all 4 lines for being the same
1137     * length.) - Matthew Mueller, 20030109
1138 root 1.1 */
1139 root 1.2
1140     if (iscnt > 3) {
1141     cbb64 = (isb64[(iscnt-1)%10] && isb64[(iscnt-2)%10] &&
1142     isb64[(iscnt-3)%10] && isb64[(iscnt-4)%10]);
1143     cbuue = (isuue[(iscnt-1)%10] && isuue[(iscnt-2)%10] &&
1144 root 1.7 isuue[(iscnt-3)%10] && isuue[(iscnt-4)%10] &&
1145     islen[(iscnt-1)%10] <= islen[(iscnt-2)%10] &&
1146     islen[(iscnt-2)%10] == islen[(iscnt-3)%10] &&
1147     islen[(iscnt-3)%10] == islen[(iscnt-4)%10]);
1148 root 1.2 cbxxe = (isxxe[(iscnt-1)%10] && isxxe[(iscnt-2)%10] &&
1149 root 1.7 isxxe[(iscnt-3)%10] && isxxe[(iscnt-4)%10] &&
1150     islen[(iscnt-1)%10] <= islen[(iscnt-2)%10] &&
1151     islen[(iscnt-2)%10] == islen[(iscnt-3)%10] &&
1152     islen[(iscnt-3)%10] == islen[(iscnt-4)%10]);
1153 root 1.2 cbbhx = (isbhx[(iscnt-1)%10] && isbhx[(iscnt-2)%10] &&
1154     isbhx[(iscnt-3)%10] && isbhx[(iscnt-4)%10]);
1155     }
1156     else {
1157     cbb64 = cbuue = cbxxe = cbbhx = 0;
1158     }
1159    
1160     if (cbb64 && !cbuue && !cbxxe && !cbbhx) {
1161     result->uudet = B64ENCODED;
1162     }
1163     else if (!cbb64 && cbuue && !cbxxe && !cbbhx) {
1164     result->uudet = UU_ENCODED;
1165     }
1166     else if (!cbb64 && !cbuue && cbxxe && !cbbhx) {
1167     result->uudet = XX_ENCODED;
1168     }
1169     else if (!cbb64 && !cbuue && !cbxxe && cbbhx) {
1170     result->uudet = BH_ENCODED;
1171     }
1172    
1173     if (result->uudet) {
1174 root 1.1 encoding = dflag = 0;
1175    
1176     /*
1177     * If we're in fast mode, we're told we don't have to look
1178     * for anything below, so we can as well break out of every-
1179     * thing
1180     * We cannot fast-scan if we have a boundary to look after.
1181     */
1182    
1183     if (uu_fast_scanning && boundary == NULL)
1184     break;
1185    
1186     /*
1187     * Skip the encoded data. We simply wait for a boundary, for
1188     * a header or for an empty line. But we also try to pick up
1189     * an "end"
1190     */
1191    
1192     hcount = lcount = 0;
1193    
1194     while (!feof (datei)) {
1195     /*
1196     * Make Busy Polls
1197     */
1198     if (UUBUSYPOLL(ftell(datei),progress.fsize)) {
1199     UUMessage (uuscan_id, __LINE__, UUMSG_NOTE,
1200     uustring (S_SCAN_CANCEL));
1201     *errcode = UURET_CANCEL;
1202     break;
1203     }
1204    
1205     oldposition = ftell (datei);
1206 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL)
1207 root 1.1 break;
1208     if (ferror (datei))
1209     break;
1210    
1211     /*
1212     * Stop scanning at an empty line or a MIME-boundary.
1213     */
1214     if (IsLineEmpty (line))
1215     break;
1216     if (boundary && line[0] == '-' && line[1] == '-' &&
1217     strncmp (line+2, boundary, blen) == 0) {
1218     fseek (datei, oldposition, SEEK_SET);
1219     break;
1220     }
1221     else if (line[0] == 'e' && (result->uudet == UU_ENCODED ||
1222     result->uudet == XX_ENCODED)) {
1223     if (strncmp (line, "end", 3) == 0) {
1224     result->end = 1;
1225     break;
1226     }
1227     }
1228     else if (line[0] == 'b') {
1229     if (strncmp (line, "begin ", 6) == 0) {
1230     fseek (datei, oldposition, SEEK_SET);
1231     break;
1232     }
1233     }
1234    
1235     if (checkheaders) {
1236     if (IsKnownHeader (line)) {
1237     (void) ScanHeaderLine (datei, line);
1238     if (hcount == 0)
1239     preheaders = oldposition;
1240     hcount++;
1241     lcount++;
1242     if ((hcount >= hlcount.restart) ||
1243     (hcount >= hlcount.afterdata && result->uudet)) {
1244     /*
1245     * Hey, a new header starts here
1246     */
1247     fseek (datei, preheaders, SEEK_SET);
1248     break;
1249     }
1250     }
1251     else if (lcount > WAITHEADER) {
1252     hcount = 0;
1253     lcount = 0;
1254     }
1255     else if (hcount) {
1256     lcount++;
1257     }
1258     }
1259     if (result->uudet == BH_ENCODED) {
1260     /* pick up ``EOF'' for BinHex files. Slow :-< */
1261     if (line[0] && strchr (line+1, ':') != NULL) {
1262     result->end = 1;
1263 root 1.2 bhflag = 0;
1264 root 1.1 break;
1265     }
1266     }
1267     }
1268 root 1.2
1269 root 1.1 if (ferror (datei) || *errcode == UURET_CANCEL)
1270     break;
1271    
1272     if (line[0] == '-' && line[1] == '-')
1273     haddh = 1;
1274    
1275     /*
1276     * Okay, got everything we need. If we had headers or a
1277     * boundary, we break out of the outer loop immediately.
1278     */
1279    
1280     if (IsKnownHeader (line) ||
1281     (boundary && line[0] == '-' && line[1] == '-' &&
1282     strncmp (line+2, boundary, blen) == 0)) {
1283     break;
1284     }
1285 root 1.2
1286 root 1.1 /*
1287     * Otherwise, we wait until finding something more interesting
1288     * in the outer loop
1289     */
1290 root 1.2
1291     continue;
1292 root 1.1 }
1293 root 1.2
1294     /*
1295     * Select the encoding with the best "history"
1296     */
1297    
1298     cbb64 = isb64[(iscnt-1)%10];
1299     cbuue = isuue[(iscnt-1)%10];
1300     cbxxe = isxxe[(iscnt-1)%10];
1301     cbbhx = isbhx[(iscnt-1)%10];
1302     dflag = 0;
1303    
1304     if (cbb64 || cbuue || cbxxe || cbbhx) {
1305     for (dflag=2; dflag<iscnt && dflag<4; dflag++) {
1306     if ((!cbb64 || !isb64[(iscnt-dflag)%10]) &&
1307     (!cbuue || !isuue[(iscnt-dflag)%10]) &&
1308     (!cbxxe || !isxxe[(iscnt-dflag)%10]) &&
1309     (!cbbhx || !isbhx[(iscnt-dflag)%10])) {
1310     dflag--;
1311     break;
1312     }
1313     cbb64 &= isb64[(iscnt-dflag)%10];
1314     cbuue &= isuue[(iscnt-dflag)%10];
1315     cbxxe &= isxxe[(iscnt-dflag)%10];
1316     cbbhx &= isbhx[(iscnt-dflag)%10];
1317     }
1318     }
1319    
1320     /*
1321     * clear-cut cases
1322     */
1323    
1324     if (cbb64 && !cbuue && !cbxxe && !cbbhx) {
1325     encoding = B64ENCODED;
1326     }
1327     else if (!cbb64 && cbuue && !cbxxe && !cbbhx) {
1328     encoding = UU_ENCODED;
1329     }
1330     else if (!cbb64 && !cbuue && cbxxe && !cbbhx) {
1331     encoding = XX_ENCODED;
1332     }
1333     else if (!cbb64 && !cbuue && !cbxxe && cbbhx) {
1334     encoding = BH_ENCODED;
1335     }
1336     else {
1337     encoding = 0;
1338     }
1339    
1340     /*
1341     * Check a few common non-clear-cut cases
1342     */
1343    
1344     if (!encoding && cbuue && result->begin) {
1345     encoding = UU_ENCODED;
1346     }
1347     else if (!encoding && cbxxe && result->begin) {
1348     encoding = XX_ENCODED;
1349     }
1350     else if (!encoding && cbb64) {
1351     encoding = B64ENCODED;
1352     }
1353     else if (!encoding && cbuue) {
1354     encoding = UU_ENCODED;
1355     }
1356     else if (!encoding && cbxxe) {
1357     encoding = XX_ENCODED;
1358     }
1359     else if (!encoding && cbbhx) {
1360     encoding = BH_ENCODED;
1361 root 1.1 }
1362     }
1363     else if (!dontcare) {
1364     encoding = 0;
1365     dflag = 0;
1366     haddh = 0;
1367     }
1368     } /* if (!uudet) */
1369     /*
1370     * End of scanning loop
1371     */
1372     } /* while (!feof (datei)) */
1373    
1374     if (feof (datei))
1375     oldposition = ftell (datei);
1376    
1377     if (dflag && encoding == B64ENCODED && haddh)
1378     result->uudet = B64ENCODED;
1379     else if (dflag && encoding == BH_ENCODED)
1380     result->uudet = BH_ENCODED;
1381    
1382     /* Base64 doesn't have begin or end, so it was probably XX */
1383     if (result->uudet == B64ENCODED && result->begin && result->end)
1384     result->uudet = XX_ENCODED;
1385    
1386     /* Base64 doesn't have begin or end */
1387     if (result->uudet == B64ENCODED)
1388     result->begin = result->end = 0;
1389    
1390     /* Base64 and BinHex don't have a file mode */
1391 root 1.4 if (result->uudet == B64ENCODED || result->uudet == BH_ENCODED ||
1392     result->uudet == YENC_ENCODED)
1393 root 1.1 result->mode = 6*64+4*8+4;
1394    
1395     /*
1396 root 1.2 * When strict MIME adherance is set, throw out suspicious attachments
1397     */
1398    
1399     if (uu_more_mime) {
1400     /*
1401     * In a MIME message, Base64 should be appropriately tagged
1402     */
1403    
1404     if (result->uudet == B64ENCODED) {
1405     result->uudet = 0;
1406     }
1407    
1408     /*
1409     * Do not accept incomplete UU or XX encoded messages
1410     */
1411    
1412     if ((result->uudet != 0 && result->uudet != B64ENCODED) &&
1413     (!result->begin || !result->end)) {
1414     result->uudet = 0;
1415     }
1416     }
1417    
1418     /*
1419 root 1.1 * In fast mode, this length will yield a false value. We don't care.
1420     * This must be checked for in uunconc(), where we usually stop decoding
1421     * after reaching startpos+length
1422     */
1423    
1424     if (uu_fast_scanning)
1425     result->length = progress.fsize-result->startpos;
1426     else
1427     result->length = ftell(datei)-result->startpos;
1428    
1429     if (ferror (datei)) {
1430     *errcode = UURET_IOERR;
1431     uu_errno = errno;
1432     return -1;
1433     }
1434     if (*errcode != UURET_OK) {
1435     return -1;
1436     }
1437    
1438     if (boundary && line[0] == '-' && line[1] == '-' &&
1439     strncmp (line+2, boundary, blen) == 0)
1440     return 2;
1441     else if (boundary && p3 &&
1442     line[0] == 'C' && line[1] == 'o' &&
1443 root 1.5 _FP_strnicmp (line, "Content-Type:", 13) == 0 &&
1444 root 1.1 strcmp (p3, boundary) == 0)
1445     return 2;
1446     else if (IsKnownHeader (line))
1447     return 1;
1448    
1449     return 0;
1450     }
1451    
1452     /*
1453     * This is the main scanning function.
1454     */
1455    
1456     fileread *
1457     ScanPart (FILE *datei, char *fname, int *errcode)
1458     {
1459     int ecount, hcount, lcount;
1460 root 1.4 int bhflag, begflag, vflag, blen=0, res;
1461     long preheaders, prevpos=0, preenc, before;
1462 root 1.1 char *line=uuscan_spline;
1463     fileread *result;
1464     char *ptr1, *ptr2;
1465    
1466     (void) UUDecodeLine (NULL, NULL, 0); /* init */
1467     if (datei == NULL || feof (datei)) {
1468     *errcode = UURET_OK;
1469     return NULL;
1470     }
1471    
1472     *errcode = UURET_OK;
1473    
1474     if ((result = (fileread *) malloc (sizeof (fileread))) == NULL) {
1475     *errcode = UURET_NOMEM;
1476     return NULL;
1477     }
1478     memset (result, 0, sizeof (fileread));
1479     result->startpos = ftell (datei);
1480     preheaders = result->startpos;
1481     before = result->startpos;
1482    
1483     /* if this is a new file, reset our scanning state */
1484     if (sstate.source == NULL || strcmp (fname, sstate.source) != 0) {
1485     sstate.isfolder = 1; /* assume yes */
1486     sstate.ismime = 0; /* wait for MIME-Version */
1487     sstate.mimestate = MS_HEADERS; /* assume headers follow */
1488     /* mimseqno = 1; */
1489    
1490     while (mssdepth) {
1491     mssdepth--;
1492     UUkillheaders (&(multistack[mssdepth].envelope));
1493 root 1.5 _FP_free (multistack[mssdepth].source);
1494 root 1.1 }
1495    
1496     UUkillheaders (&sstate.envelope);
1497     memset (&sstate.envelope, 0, sizeof (headers));
1498    
1499 root 1.5 _FP_free (sstate.source);
1500     if ((sstate.source = _FP_strdup (fname)) == NULL) {
1501 root 1.1 *errcode = UURET_NOMEM;
1502 root 1.5 _FP_free (result);
1503 root 1.1 return NULL;
1504     }
1505    
1506     /* ignore empty lines at the beginning of a file */
1507     preheaders = ftell (datei);
1508     while (!feof (datei)) {
1509 root 1.12 if (UUBUSYPOLL(preheaders,progress.fsize)) SPCANCEL();
1510 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL)
1511 root 1.1 break;
1512     if (!IsLineEmpty (line)) {
1513     fseek (datei, preheaders, SEEK_SET);
1514     break;
1515     }
1516     preheaders = ftell (datei);
1517     }
1518     }
1519    
1520     if (ferror(datei) || feof(datei)) {
1521 root 1.5 _FP_free (result);
1522 root 1.1 return NULL;
1523     }
1524    
1525     /*
1526     * If we are confident that this is a mail folder and are at the
1527     * beginning of the file, expecting to read some headers, scan
1528     * the envelope.
1529     */
1530    
1531     if (sstate.isfolder && sstate.mimestate == MS_HEADERS) {
1532     hcount = 0;
1533     lcount = 0;
1534     UUkillheaders (&sstate.envelope);
1535    
1536     /*
1537     * clean up leftovers from invalid messages
1538     */
1539    
1540     while (mssdepth) {
1541     mssdepth--;
1542     UUkillheaders (&(multistack[mssdepth].envelope));
1543 root 1.5 _FP_free (multistack[mssdepth].source);
1544 root 1.1 }
1545    
1546 root 1.7 prevpos = ftell (datei);
1547 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL) {
1548 root 1.5 _FP_free (result);
1549 root 1.1 return NULL;
1550     }
1551    
1552 root 1.2 /*
1553     * Special handling for AOL folder files, which start off with a boundary.
1554     * We recognize them by a valid boundary line as the first line of a file.
1555     * Note that the rest of the scanning code becomes suspicious if a boun-
1556     * dary does never appear in a file -- this should save us from grave
1557     * false detection errors
1558     */
1559    
1560     if (!feof (datei) && line[0] == '-' && line[1] == '-' && line[2]) {
1561     sstate.ismime = 1;
1562 root 1.5 sstate.envelope.mimevers = _FP_strdup ("1.0");
1563     sstate.envelope.boundary = _FP_strdup (line+2);
1564     sstate.envelope.ctype = _FP_strdup ("multipart/mixed");
1565 root 1.2 sstate.mimestate = MS_SUBPART;
1566    
1567     *errcode = UURET_CONT;
1568 root 1.5 _FP_free (result);
1569 root 1.2 return NULL;
1570     }
1571    
1572     /*
1573     * Normal behavior: look for a RFC 822 header
1574     */
1575    
1576 root 1.1 while (!feof (datei) && !IsLineEmpty (line)) {
1577     if (IsKnownHeader (line))
1578     hcount++;
1579     if (UUBUSYPOLL(ftell(datei),progress.fsize)) SPCANCEL();
1580 root 1.7 if (IsHeaderLine (line)) {
1581     ptr1 = ScanHeaderLine (datei, line);
1582     if (ParseHeader (&sstate.envelope, ptr1) == NULL) {
1583     *errcode = UURET_NOMEM;
1584     _FP_free (result);
1585     return NULL;
1586     }
1587 root 1.1 }
1588     /*
1589     * if we've read too many lines without finding headers, then
1590     * this probably isn't a mail folder after all
1591     */
1592     lcount++;
1593 root 1.7 if (lcount > WAITHEADER && hcount < hlcount.afternl) {
1594     fseek (datei, prevpos, SEEK_SET);
1595     line[0] = '\0';
1596 root 1.1 break;
1597 root 1.7 }
1598 root 1.1
1599 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL) {
1600 root 1.8 /* If we are at eof without finding headers, there probably isn't */
1601 root 1.15 if (hcount < hlcount.afternl)
1602 root 1.8 fseek (datei, prevpos, SEEK_SET);
1603     break;
1604     }
1605 root 1.1 }
1606 root 1.7
1607 root 1.1 /* skip empty lines */
1608     prevpos = ftell (datei);
1609 root 1.7 if (IsLineEmpty (line)) {
1610     while (!feof (datei)) {
1611 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL)
1612 root 1.7 break;
1613     if (UUBUSYPOLL(ftell(datei),progress.fsize)) SPCANCEL();
1614     if (!IsLineEmpty (line)) {
1615     fseek (datei, prevpos, SEEK_SET);
1616     break;
1617     }
1618     prevpos = ftell (datei);
1619 root 1.1 }
1620     }
1621 root 1.7
1622 root 1.1 /*
1623     * If we don't have all valid MIME headers yet, but the following
1624     * line is a MIME header, accept it anyway.
1625     */
1626 root 1.2
1627     if (!uu_more_mime &&
1628     sstate.envelope.mimevers == NULL &&
1629     sstate.envelope.ctype == NULL &&
1630     sstate.envelope.ctenc == NULL &&
1631     IsKnownHeader (line)) {
1632 root 1.1 /*
1633     * see above
1634     */
1635 root 1.15 _FP_fgets (line, 1024, datei);
1636 root 1.1
1637     while (!feof (datei) && !IsLineEmpty (line)) {
1638     if (IsKnownHeader (line))
1639     hcount++;
1640     if (UUBUSYPOLL(ftell(datei),progress.fsize)) SPCANCEL();
1641     ptr1 = ScanHeaderLine (datei, line);
1642     if (ParseHeader (&sstate.envelope, ptr1) == NULL) {
1643     *errcode = UURET_NOMEM;
1644 root 1.5 _FP_free (result);
1645 root 1.1 return NULL;
1646     }
1647 root 1.2
1648 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL)
1649 root 1.1 break;
1650     }
1651     /* skip empty lines */
1652     prevpos = ftell (datei);
1653     while (!feof (datei)) {
1654 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL)
1655 root 1.1 break;
1656     if (UUBUSYPOLL(ftell(datei),progress.fsize)) SPCANCEL();
1657     if (!IsLineEmpty (line)) {
1658     fseek (datei, prevpos, SEEK_SET);
1659     break;
1660     }
1661     prevpos = ftell (datei);
1662     }
1663     }
1664 root 1.2
1665 root 1.1 /*
1666     * A partial multipart message probably has only a Content-Type
1667     * header but nothing else. In this case, at least simulate a
1668     * MIME message
1669     * if mimevers is not set but there are other well-known MIME
1670     * headers, don't be too picky about it.
1671     */
1672     if (sstate.envelope.ctype && sstate.envelope.mimevers==NULL &&
1673 root 1.5 _FP_stristr (sstate.envelope.ctype, "multipart") != NULL &&
1674 root 1.1 sstate.envelope.boundary != NULL) {
1675 root 1.5 sstate.envelope.mimevers = _FP_strdup ("1.0");
1676 root 1.1 hcount = hlcount.afternl;
1677     }
1678     else if (sstate.envelope.mimevers==NULL && sstate.envelope.ctype &&
1679     sstate.envelope.fname && sstate.envelope.ctenc) {
1680 root 1.5 sstate.envelope.mimevers = _FP_strdup ("1.0");
1681 root 1.1 hcount = hlcount.afternl;
1682     }
1683    
1684 root 1.7 if (sstate.envelope.mimevers != NULL) {
1685 root 1.1 /* this is a MIME file. check the Content-Type */
1686     sstate.ismime = 1;
1687 root 1.5 if (_FP_stristr (sstate.envelope.ctype, "multipart") != NULL) {
1688 root 1.1 if (sstate.envelope.boundary == NULL) {
1689     UUMessage (uuscan_id, __LINE__, UUMSG_WARNING,
1690     uustring (S_MIME_NO_BOUNDARY));
1691     sstate.mimestate = MS_BODY;
1692 root 1.5 _FP_free (sstate.envelope.ctype);
1693     sstate.envelope.ctype = _FP_strdup ("text/plain");
1694 root 1.1 }
1695     else {
1696     sstate.mimestate = MS_PREAMBLE;
1697     }
1698     }
1699     else {
1700     sstate.mimestate = MS_BODY; /* just a `simple' message */
1701     }
1702 root 1.7 }
1703     else {
1704     /* not a folder after all */
1705     fseek (datei, prevpos, SEEK_SET);
1706     sstate.isfolder = 0;
1707     sstate.ismime = 0;
1708 root 1.1 }
1709     }
1710    
1711     if (feof (datei) || ferror (datei)) { /* oops */
1712 root 1.5 _FP_free (result);
1713 root 1.1 return NULL;
1714     }
1715    
1716     /*
1717     * Handle MIME stuff
1718     */
1719    
1720     /*
1721     * Read Preamble. This must be ended by a sstate.envelope.boundary line.
1722     * If uu_usepreamble is set, we produce a result from this one
1723     */
1724    
1725     if (sstate.ismime && sstate.mimestate == MS_PREAMBLE) {
1726     result->startpos = ftell (datei); /* remember start of preamble */
1727     prevpos = ftell (datei);
1728     preheaders = ftell (datei);
1729    
1730     blen = strlen (sstate.envelope.boundary);
1731     lcount = 0;
1732    
1733     while (!feof (datei)) {
1734 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL)
1735 root 1.1 break;
1736     if (UUBUSYPOLL(ftell(datei),progress.fsize)) SPCANCEL();
1737     if (line[0] == '-' && line[1] == '-' &&
1738     strncmp (line+2, sstate.envelope.boundary, blen) == 0)
1739     break;
1740     if (!IsLineEmpty (line))
1741     lcount++;
1742    
1743     prevpos = ftell (datei);
1744     }
1745     if (feof (datei) || ferror (datei)) {
1746     UUMessage (uuscan_id, __LINE__, UUMSG_WARNING,
1747     uustring (S_MIME_B_NOT_FOUND));
1748     /*
1749     * restart and try again; don't restart if uu_fast_scanning
1750     */
1751     sstate.isfolder = 0;
1752     sstate.ismime = 0;
1753     sstate.mimestate = MS_BODY;
1754    
1755     if (!uu_fast_scanning) {
1756     *errcode = UURET_CONT;
1757     fseek (datei, preheaders, SEEK_SET);
1758     }
1759 root 1.5 _FP_free (result);
1760 root 1.1 return NULL;
1761     }
1762     if (line[0] == '-' && line[1] == '-' &&
1763     strncmp (line+2, sstate.envelope.boundary, blen) == 0) {
1764     ptr1 = line + 2 + blen;
1765     if (*ptr1 == '-' && *(ptr1+1) == '-') {
1766     /* Empty Multipart Message. Duh. */
1767     sstate.mimestate = MS_EPILOGUE;
1768     }
1769     else {
1770     sstate.mimestate = MS_SUBPART;
1771     }
1772     }
1773     else { /* shouldn't happen */
1774     UUMessage (uuscan_id, __LINE__, UUMSG_WARNING,
1775     uustring (S_MIME_B_NOT_FOUND));
1776     /*
1777     * restart and try again; don't restart if uu_fast_scanning
1778     */
1779     sstate.isfolder = 0;
1780     sstate.ismime = 0;
1781     sstate.mimestate = MS_BODY;
1782    
1783     if (!uu_fast_scanning) {
1784     *errcode = UURET_CONT;
1785     fseek (datei, preheaders, SEEK_SET);
1786     }
1787 root 1.5 _FP_free (result);
1788 root 1.1 return NULL;
1789     }
1790     /* produce result if uu_usepreamble is set */
1791     if (uu_usepreamble && lcount) {
1792     sprintf (line, "%04d.txt", ++mimseqno);
1793 root 1.5 result->subject = _FP_strdup (sstate.envelope.subject);
1794     result->filename = _FP_strdup (line);
1795     result->origin = _FP_strdup (sstate.envelope.from);
1796     result->mimeid = _FP_strdup (sstate.envelope.mimeid);
1797     result->mimetype = _FP_strdup ("text/plain");
1798 root 1.1 result->mode = 0644;
1799     result->uudet = PT_ENCODED; /* plain text */
1800 root 1.5 result->sfname = _FP_strdup (fname);
1801 root 1.1 result->flags = FL_SINGLE | FL_PROPER;
1802     /* result->startpos set from above */
1803     result->length = prevpos - result->startpos;
1804     result->partno = 1;
1805    
1806     /* MIME message, let's continue */
1807     *errcode = UURET_CONT;
1808    
1809     if ((sstate.envelope.subject != NULL && result->subject == NULL) ||
1810     result->filename == NULL || result->sfname == NULL) {
1811     *errcode = UURET_NOMEM;
1812     }
1813    
1814     return result;
1815     }
1816     /* MIME message, let's continue */
1817     if (*errcode == UURET_OK)
1818     *errcode = UURET_CONT;
1819    
1820     /* otherwise, just return NULL */
1821 root 1.5 _FP_free (result);
1822 root 1.1 return NULL;
1823     }
1824    
1825     /*
1826     * Read Epilogue, the plain text after the last boundary.
1827     * This can either end with new headers from the next message of a
1828     * mail folder or with a `parent' boundary if we are inside an
1829     * encapsulated Multipart message. Oh yes, and of course the file
1830     * may also simply end :-)
1831     * Another possibility is that we might find plain encoded data
1832     * without encapsulating message. We're not _too_ flexible here,
1833     * we won't detect Base64, and require a proper `begin' line for
1834     * uuencoding and xxencoding
1835     * If uu_usepreamble is set, we produce a result from this one
1836     */
1837    
1838     if (sstate.ismime && sstate.mimestate == MS_EPILOGUE) {
1839     result->startpos = ftell (datei); /* remember start of epilogue */
1840     prevpos = ftell (datei);
1841     preheaders = ftell (datei);
1842     preenc = ftell (datei);
1843     hcount = lcount = 0;
1844     ecount = bhflag = 0;
1845     begflag = vflag = 0;
1846     res = 0;
1847    
1848     /*
1849     * If we are in the outermost message and uu_fast_scanning, we
1850     * know (or assume) that no more messages will follow, so there's
1851     * no need to scan the rest.
1852     */
1853     if (uu_fast_scanning && mssdepth == 0) {
1854     /*
1855     * check if the epilogue is empty
1856     */
1857     while (!feof (datei) && !ferror (datei) && lcount<10 && res==0) {
1858 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL)
1859 root 1.1 break;
1860     if (!IsLineEmpty (line))
1861     res++;
1862     lcount++;
1863     }
1864     if (uu_usepreamble && res) {
1865     sprintf (line, "%04d.txt", ++mimseqno);
1866 root 1.5 result->subject = _FP_strdup (sstate.envelope.subject);
1867     result->filename = _FP_strdup (line);
1868     result->origin = _FP_strdup (sstate.envelope.from);
1869     result->mimeid = _FP_strdup (sstate.envelope.mimeid);
1870     result->mimetype = _FP_strdup ("text/plain");
1871 root 1.1 result->mode = 0644;
1872     result->uudet = PT_ENCODED; /* plain text */
1873 root 1.5 result->sfname = _FP_strdup (fname);
1874 root 1.1 result->flags = FL_SINGLE | FL_PROPER | FL_TOEND;
1875     result->partno = 1;
1876     /* result->startpos set from above */
1877     result->length = progress.fsize - result->startpos;
1878    
1879     if ((sstate.envelope.subject != NULL && result->subject == NULL) ||
1880     result->filename == NULL || result->sfname == NULL) {
1881     *errcode = UURET_NOMEM;
1882     }
1883    
1884     return result;
1885     }
1886 root 1.5 _FP_free (result);
1887 root 1.1 return NULL;
1888     }
1889    
1890     if (mssdepth > 0)
1891     blen = strlen (multistack[mssdepth-1].envelope.boundary);
1892    
1893     while (!feof (datei)) {
1894 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL)
1895 root 1.1 break;
1896     if (UUBUSYPOLL(ftell(datei),progress.fsize)) SPCANCEL();
1897     /* check for parent boundary */
1898     if (mssdepth > 0 && line[0] == '-' && line[1] == '-' &&
1899     strncmp (line+2,
1900     multistack[mssdepth-1].envelope.boundary, blen) == 0)
1901     break;
1902    
1903     /* check for next headers only at outermost level */
1904     if (mssdepth == 0 && IsKnownHeader (line)) {
1905     (void) ScanHeaderLine (datei, line);
1906     if (hcount == 0) {
1907     preheaders = prevpos;
1908     lcount = 0;
1909     }
1910     hcount++;
1911     lcount++;
1912    
1913     if (hcount >= hlcount.restart) {
1914     /* okay, new headers */
1915     break;
1916     }
1917     }
1918     else if (lcount > WAITHEADER) {
1919     hcount = 0;
1920     lcount = 0;
1921     }
1922     else if (hcount) {
1923     lcount++;
1924     }
1925     else {
1926     hcount = lcount = 0;
1927     }
1928    
1929     /* check for begin and encoded data only at outermost level */
1930 root 1.2 if (mssdepth == 0 && !uu_more_mime) {
1931 root 1.1 if (strncmp (line, "begin ", 6) == 0 ||
1932 root 1.5 _FP_strnicmp (line, "<pre>begin ", 11) == 0) {
1933 root 1.1 preenc = prevpos;
1934     begflag = 1;
1935     }
1936     else if (strncmp (line, "end", 3) == 0 && begflag) {
1937     ecount = ELC_COUNT;
1938     break;
1939     }
1940     else if ((vflag = UUValidData (line, 0, &bhflag)) != 0) {
1941     if (vflag == BH_ENCODED && bhflag == 0) {
1942     /* very short BinHex file follows */
1943     preenc = prevpos;
1944     break;
1945     }
1946     /* remember that XX can easily be mistaken as Base64 */
1947 root 1.10 if ((vflag == UU_ENCODED || vflag == XX_ENCODED || vflag == B64ENCODED) && begflag) {
1948 root 1.1 if (++ecount >= ELC_COUNT)
1949     break;
1950     }
1951     else {
1952     begflag = 0;
1953     ecount = 0;
1954     }
1955     }
1956     else {
1957     begflag = 0;
1958     ecount = 0;
1959     }
1960     }
1961    
1962     if (!IsLineEmpty (line))
1963     res++;
1964    
1965     prevpos = ftell (datei);
1966     }
1967 root 1.2
1968 root 1.1 if (mssdepth > 0 && line[0] == '-' && line[1] == '-' &&
1969     strncmp (line+2,
1970     multistack[mssdepth-1].envelope.boundary, blen) == 0) {
1971     /* restore previous state */
1972     mssdepth--;
1973     UUkillheaders (&sstate.envelope);
1974 root 1.5 _FP_free (sstate.source);
1975 root 1.1 memcpy (&sstate, &(multistack[mssdepth]), sizeof (scanstate));
1976    
1977     ptr1 = line + 2 + strlen (sstate.envelope.boundary);
1978    
1979     if (*ptr1 == '-' && *(ptr1+1) == '-') {
1980     sstate.mimestate = MS_EPILOGUE;
1981     }
1982     else {
1983     sstate.mimestate = MS_SUBPART;
1984     }
1985     result->length = prevpos - result->startpos;
1986     *errcode = UURET_CONT;
1987     }
1988     else if (mssdepth > 0) {
1989     UUMessage (uuscan_id, __LINE__, UUMSG_WARNING,
1990     uustring (S_MIME_B_NOT_FOUND));
1991     /*
1992     * restart and try again; don't restart if uu_fast_scanning
1993     */
1994     sstate.isfolder = 0;
1995     sstate.ismime = 0;
1996     sstate.mimestate = MS_BODY;
1997    
1998     while (mssdepth) {
1999     mssdepth--;
2000     UUkillheaders (&(multistack[mssdepth].envelope));
2001 root 1.5 _FP_free (multistack[mssdepth].source);
2002 root 1.1 }
2003    
2004     if (!uu_fast_scanning) {
2005     *errcode = UURET_CONT;
2006     fseek (datei, preheaders, SEEK_SET);
2007     }
2008 root 1.5 _FP_free (result);
2009 root 1.1 return NULL;
2010     }
2011     else if (IsKnownHeader (line)) {
2012     /* new message follows */
2013     sstate.isfolder = 1;
2014     sstate.ismime = 0;
2015     sstate.mimestate = MS_HEADERS;
2016     result->length = preheaders - result->startpos;
2017     fseek (datei, preheaders, SEEK_SET);
2018     }
2019     else if (ecount >= ELC_COUNT) {
2020     /* new plain encoding */
2021     sstate.isfolder = 0;
2022     sstate.ismime = 0;
2023     sstate.mimestate = MS_BODY;
2024     result->length = preenc - result->startpos;
2025     fseek (datei, preenc, SEEK_SET);
2026     }
2027    
2028     /* produce result if uu_usepreamble is set */
2029     if (uu_usepreamble && res) {
2030     sprintf (line, "%04d.txt", ++mimseqno);
2031 root 1.5 result->subject = _FP_strdup (sstate.envelope.subject);
2032     result->filename = _FP_strdup (line);
2033     result->origin = _FP_strdup (sstate.envelope.from);
2034     result->mimeid = _FP_strdup (sstate.envelope.mimeid);
2035     result->mimetype = _FP_strdup ("text/plain");
2036 root 1.1 result->mode = 0644;
2037     result->uudet = PT_ENCODED; /* plain text */
2038 root 1.5 result->sfname = _FP_strdup (fname);
2039 root 1.1 result->flags = FL_SINGLE | FL_PROPER;
2040     result->partno = 1;
2041     /* result->startpos set from above */
2042     /* result->length set from above */
2043    
2044     if ((sstate.envelope.subject != NULL && result->subject == NULL) ||
2045     result->filename == NULL || result->sfname == NULL) {
2046     *errcode = UURET_NOMEM;
2047     }
2048    
2049     return result;
2050     }
2051     /* otherwise, just return NULL */
2052 root 1.5 _FP_free (result);
2053 root 1.1 return NULL;
2054     }
2055    
2056     /*
2057     * Scan a new part from a Multipart message. Check for a new local
2058     * envelope (which defaults to `Content-Type: text/plain') and
2059     * evaluate its Content-Type and Content-Transfer-Encoding. If this
2060     * is another Multipart/something, push the current state onto our
2061     * stack and dive into the new environment, starting with another
2062     * preamble.
2063     */
2064    
2065     if (sstate.ismime && sstate.mimestate == MS_SUBPART) {
2066     memset (&localenv, 0, sizeof (headers));
2067     result->startpos = ftell (datei);
2068     prevpos = ftell (datei);
2069     hcount = 0;
2070     lcount = 0;
2071    
2072     /*
2073     * Duplicate some data from outer envelope
2074     */
2075    
2076 root 1.5 localenv.mimevers = _FP_strdup (sstate.envelope.mimevers);
2077     localenv.from = _FP_strdup (sstate.envelope.from);
2078     localenv.subject = _FP_strdup (sstate.envelope.subject);
2079     localenv.rcpt = _FP_strdup (sstate.envelope.rcpt);
2080     localenv.date = _FP_strdup (sstate.envelope.date);
2081 root 1.1
2082     if ((sstate.envelope.mimevers != NULL && localenv.mimevers == NULL) ||
2083     (sstate.envelope.from != NULL && localenv.from == NULL) ||
2084     (sstate.envelope.subject != NULL && localenv.subject == NULL) ||
2085     (sstate.envelope.rcpt != NULL && localenv.rcpt == NULL) ||
2086     (sstate.envelope.date != NULL && localenv.date == NULL)) {
2087    
2088     while (mssdepth) {
2089     mssdepth--;
2090     UUkillheaders (&(multistack[mssdepth].envelope));
2091 root 1.5 _FP_free (multistack[mssdepth].source);
2092 root 1.1 }
2093     sstate.isfolder = 0;
2094     sstate.ismime = 0;
2095    
2096     UUkillheaders (&localenv);
2097     *errcode = UURET_NOMEM;
2098 root 1.5 _FP_free (result);
2099 root 1.1 return NULL;
2100     }
2101    
2102     /* Scan subheader. But what if there is no subheader? */
2103     hcount = 0;
2104     lcount = 0;
2105     preheaders = prevpos;
2106    
2107 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL) {
2108 root 1.1 sstate.isfolder = 0;
2109     sstate.ismime = 0;
2110     while (mssdepth) {
2111     mssdepth--;
2112     UUkillheaders (&(multistack[mssdepth].envelope));
2113 root 1.5 _FP_free (multistack[mssdepth].source);
2114 root 1.1 }
2115     UUkillheaders (&localenv);
2116 root 1.5 _FP_free (result);
2117 root 1.1 return NULL;
2118     }
2119    
2120     while (!feof (datei) && !IsLineEmpty (line)) {
2121     if (IsKnownHeader (line))
2122     hcount++;
2123     if (UUBUSYPOLL(ftell(datei),progress.fsize)) SPCANCEL();
2124     if (lcount > WAITHEADER && hcount == 0) {
2125     fseek (datei, preheaders, SEEK_SET);
2126     prevpos = preheaders;
2127     break;
2128     }
2129     ptr1 = ScanHeaderLine (datei, line);
2130     if (ParseHeader (&localenv, ptr1) == NULL)
2131     *errcode = UURET_NOMEM;
2132    
2133     if (line[0] == '-' && line[1] == '-')
2134     break;
2135    
2136     prevpos = ftell (datei);
2137    
2138 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL)
2139 root 1.1 break;
2140     lcount++;
2141     }
2142     if (line[0] == '-' && line[1] == '-') {
2143     /*
2144     * this shouldn't happen, there must always be an empty line,
2145     * but let's accept it anyway. Just skip back to before the
2146     * boundary, so that it gets handled below
2147     */
2148     fseek (datei, prevpos, SEEK_SET);
2149     }
2150    
2151 root 1.5 if (_FP_stristr (localenv.ctype, "multipart") != NULL) {
2152 root 1.1 /* oh no, not again */
2153     if (mssdepth >= MSMAXDEPTH) {
2154     /* Argh, what an isane message. Treat as plain text */
2155     UUMessage (uuscan_id, __LINE__, UUMSG_WARNING,
2156     uustring (S_MIME_MULTI_DEPTH));
2157     }
2158     else if (localenv.boundary == NULL) {
2159     UUMessage (uuscan_id, __LINE__, UUMSG_WARNING,
2160     uustring (S_MIME_NO_BOUNDARY));
2161     }
2162     else {
2163     memcpy (&multistack[mssdepth], &sstate, sizeof (scanstate));
2164     memcpy (&sstate.envelope, &localenv, sizeof (headers));
2165     memset (&localenv, 0, sizeof (headers));
2166     sstate.mimestate = MS_PREAMBLE;
2167 root 1.5 if ((sstate.source = _FP_strdup (sstate.source)) == NULL)
2168 root 1.1 *errcode = UURET_NOMEM;
2169    
2170     if (*errcode == UURET_OK)
2171     *errcode = UURET_CONT;
2172    
2173     mssdepth++;
2174     /* need a restart */
2175 root 1.5 _FP_free (result);
2176 root 1.1 return NULL;
2177     }
2178     }
2179 root 1.4
2180 root 1.1 /*
2181     * So this subpart is either plain text or something else. Check
2182     * the Content-Type and Content-Transfer-Encoding. If the latter
2183     * is a defined value, we know what to do and just copy everything
2184     * up to the boundary.
2185     * If Content-Transfer-Encoding is unknown or missing, look at the
2186     * Content-Type. If it's "text/plain" or undefined, we subject the
2187     * message to our encoding detection. Otherwise, treat as plain
2188     * text.
2189     * This is done because users might `attach' a uuencoded file, which
2190     * would then be correctly typed as `text/plain'.
2191     */
2192 root 1.4
2193 root 1.5 if (_FP_stristr (localenv.ctenc, "base64") != NULL)
2194 root 1.1 result->uudet = B64ENCODED;
2195 root 1.5 else if (_FP_stristr (localenv.ctenc, "x-uue") != NULL) {
2196 root 1.2 result->uudet = UU_ENCODED;
2197 root 1.4 result->begin = result->end = 1;
2198     }
2199 root 1.5 else if (_FP_stristr (localenv.ctenc, "x-yenc") != NULL) {
2200 root 1.4 result->uudet = YENC_ENCODED;
2201     result->begin = result->end = 1;
2202     }
2203 root 1.5 else if (_FP_stristr (localenv.ctenc, "quoted-printable") != NULL)
2204 root 1.1 result->uudet = QP_ENCODED;
2205 root 1.5 else if (_FP_stristr (localenv.ctenc, "7bit") != NULL ||
2206     _FP_stristr (localenv.ctenc, "8bit") != NULL)
2207 root 1.1 result->uudet = PT_ENCODED;
2208 root 1.5 else if (_FP_stristr (localenv.ctype, "multipart") != NULL ||
2209     _FP_stristr (localenv.ctype, "message") != NULL)
2210 root 1.2 result->uudet = PT_ENCODED;
2211    
2212     /*
2213     * If we're switched to MIME-only mode, handle as text
2214     */
2215    
2216     if (uu_more_mime >= 2 && !result->uudet) {
2217 root 1.1 result->uudet = PT_ENCODED;
2218 root 1.2 }
2219 root 1.1
2220     if (result->uudet) {
2221     /*
2222     * Oh-kay, go ahead. Just read and wait for the boundary
2223     */
2224     result->startpos = ftell (datei);
2225     prevpos = ftell (datei);
2226     blen = strlen (sstate.envelope.boundary);
2227     lcount = 0;
2228    
2229     while (!feof (datei)) {
2230 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL)
2231 root 1.1 break;
2232     if (UUBUSYPOLL(ftell(datei),progress.fsize)) SPCANCEL();
2233     line[255] = '\0';
2234     if (line[0] == '-' && line[1] == '-' &&
2235     strncmp (line+2, sstate.envelope.boundary, blen) == 0)
2236     break;
2237     /*
2238     * I've had a report of someone who tried to decode a huge file
2239     * that had an early truncated multipart message and later another
2240     * multipart message with the *same* boundary. Consequently, all
2241     * some hundred messages inbetween were ignored ...
2242     * This check here doesn't cover folded header lines, but we don't
2243     * want to slow down scanning too much. We just check for
2244     * Content-Type: multipart/... boundary="same-boundary"
2245     */
2246     if (line[0] == 'C' && line[1] == 'o' &&
2247 root 1.5 _FP_strnicmp (line, "Content-Type:", 13) == 0) {
2248 root 1.1 ptr1 = ScanHeaderLine (datei, line);
2249 root 1.5 ptr2 = (ptr1)?_FP_stristr(ptr1,"boundary"):NULL;
2250 root 1.1 ptr1 = (ptr2)?ParseValue(ptr2):NULL;
2251     if (ptr1 && strcmp (ptr1, sstate.envelope.boundary) == 0)
2252     break;
2253     for (res=0; ptr1 && res<mssdepth; res++)
2254     if (strcmp (ptr1, multistack[res].envelope.boundary) == 0)
2255     break;
2256     if (res<mssdepth)
2257     break;
2258     }
2259     if (!IsLineEmpty (line))
2260     lcount++;
2261     prevpos = ftell (datei);
2262     }
2263     if (line[0] == '-' && line[1] == '-' &&
2264     strncmp (line+2, sstate.envelope.boundary, blen) == 0) {
2265     ptr1 = line + 2 + blen;
2266     if (*ptr1 == '-' && *(ptr1+1) == '-')
2267     sstate.mimestate = MS_EPILOGUE;
2268     else
2269     sstate.mimestate = MS_SUBPART;
2270     }
2271     else {
2272     UUMessage (uuscan_id, __LINE__, UUMSG_WARNING,
2273     uustring (S_MIME_B_NOT_FOUND));
2274    
2275     while (mssdepth) {
2276     mssdepth--;
2277     UUkillheaders (&(multistack[mssdepth].envelope));
2278 root 1.5 _FP_free (multistack[mssdepth].source);
2279 root 1.1 }
2280     /*
2281     * Don't retry if uu_fast_scanning
2282     */
2283    
2284     if (uu_fast_scanning) {
2285     UUkillheaders (&localenv);
2286     sstate.isfolder = 0;
2287     sstate.ismime = 0;
2288     sstate.mimestate = MS_BODY;
2289 root 1.5 _FP_free (result);
2290 root 1.1 return NULL;
2291     }
2292    
2293     /*
2294     * Retry, but listening to headers this time
2295     */
2296     fseek (datei, result->startpos, SEEK_SET);
2297    
2298     UUkillfread (result);
2299     if ((result = (fileread *) malloc (sizeof (fileread))) == NULL) {
2300     *errcode = UURET_NOMEM;
2301     sstate.isfolder = 0;
2302     sstate.ismime = 0;
2303     UUkillheaders (&localenv);
2304     return NULL;
2305     }
2306     memset (result, 0, sizeof (fileread));
2307    
2308     if ((res = ScanData (datei, fname, errcode, NULL, 1, 1, result))==-1) {
2309     /* oops, something went wrong */
2310     sstate.isfolder = 0;
2311     sstate.ismime = 0;
2312     UUkillfread (result);
2313     UUkillheaders (&localenv);
2314     return NULL;
2315     }
2316     if (res == 1) {
2317     /*
2318     * new headers found
2319     */
2320     sstate.isfolder = 1;
2321     sstate.ismime = 0;
2322     sstate.mimestate = MS_HEADERS;
2323     }
2324     else {
2325     sstate.isfolder = 0;
2326     sstate.ismime = 0;
2327     }
2328     }
2329     /* produce result if uu_handletext is set */
2330 root 1.2 /* or if the file is explicitely named */
2331     if (result->uudet == B64ENCODED || lcount) {
2332 root 1.1 if (localenv.fname) {
2333 root 1.5 _FP_free (result->filename);
2334     if ((result->filename = _FP_strdup (localenv.fname)) == NULL)
2335 root 1.1 *errcode = UURET_NOMEM;
2336     }
2337     else if ((result->uudet==QP_ENCODED||result->uudet==PT_ENCODED) &&
2338 root 1.2 result->filename == NULL && uu_handletext) {
2339 root 1.1 sprintf (line, "%04d.txt", ++mimseqno);
2340 root 1.5 if ((result->filename = _FP_strdup (line)) == NULL)
2341 root 1.1 *errcode = UURET_NOMEM;
2342     }
2343 root 1.5 result->subject = _FP_strdup (localenv.subject);
2344     result->origin = _FP_strdup (localenv.from);
2345     result->mimeid = _FP_strdup (localenv.mimeid);
2346     result->mimetype = _FP_strdup (localenv.ctype);
2347 root 1.1 result->mode = 0644;
2348 root 1.5 result->sfname = _FP_strdup (fname);
2349 root 1.1 result->flags = FL_SINGLE | FL_PROPER;
2350     result->partno = 1;
2351     /* result->uudet determined above */
2352     /* result->startpos set from above */
2353     result->length = prevpos - result->startpos;
2354    
2355     if ((localenv.subject != NULL && result->subject == NULL) ||
2356     result->filename == NULL || result->sfname == NULL) {
2357     *errcode = UURET_NOMEM;
2358     }
2359     }
2360     else {
2361     /* don't produce a result */
2362 root 1.5 _FP_free (result);
2363 root 1.1 result = NULL;
2364     }
2365     if (*errcode == UURET_OK)
2366     *errcode = UURET_CONT;
2367     /*
2368     * destroy local envelope
2369     */
2370     UUkillheaders (&localenv);
2371     return result;
2372     }
2373 root 1.2
2374 root 1.1 /*
2375     * we're in a subpart, but the local headers don't give us any
2376     * clue about what's to find here. So look for encoded data by
2377     * ourselves.
2378     */
2379 root 1.2
2380 root 1.1 if ((res = ScanData (datei, fname, errcode,
2381 root 1.2 sstate.envelope.boundary,
2382     1, 0, result)) == -1) {
2383 root 1.1 /* oops, something went wrong */
2384     sstate.isfolder = 0;
2385     sstate.ismime = 0;
2386     UUkillfread (result);
2387     UUkillheaders (&localenv);
2388     return NULL;
2389     }
2390     /*
2391     * we should really be at a boundary here, but check again
2392     */
2393     blen = strlen (sstate.envelope.boundary);
2394     prevpos = ftell (datei);
2395    
2396     while (!feof (datei)) {
2397 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL)
2398 root 1.1 break;
2399     if (UUBUSYPOLL(ftell(datei),progress.fsize)) SPCANCEL();
2400     if (line[0] == '-' && line[1] == '-' &&
2401     strncmp (line+2, sstate.envelope.boundary, blen) == 0)
2402     break;
2403     if (line[0] == 'C' && line[1] == 'o' &&
2404 root 1.5 _FP_strnicmp (line, "Content-Type:", 13) == 0) {
2405 root 1.1 ptr1 = ScanHeaderLine (datei, line);
2406 root 1.5 ptr2 = (ptr1)?_FP_stristr(ptr1,"boundary"):NULL;
2407 root 1.1 ptr1 = (ptr2)?ParseValue(ptr2):NULL;
2408     if (ptr1 && strcmp (ptr1, sstate.envelope.boundary) == 0)
2409     break;
2410     }
2411     prevpos = ftell (datei);
2412     }
2413     /*
2414     * check if this was the last subpart
2415     */
2416     if (line[0] == '-' && line[1] == '-' &&
2417     strncmp (line+2, sstate.envelope.boundary, blen) == 0) {
2418     ptr1 = line + 2 + blen;
2419     if (*ptr1 == '-' && *(ptr1+1) == '-')
2420     sstate.mimestate = MS_EPILOGUE;
2421     else
2422     sstate.mimestate = MS_SUBPART;
2423     }
2424     else {
2425     UUMessage (uuscan_id, __LINE__, UUMSG_WARNING,
2426     uustring (S_MIME_B_NOT_FOUND));
2427    
2428     while (mssdepth) {
2429     mssdepth--;
2430     UUkillheaders (&(multistack[mssdepth].envelope));
2431 root 1.5 _FP_free (multistack[mssdepth].source);
2432 root 1.1 }
2433    
2434     if (uu_fast_scanning) {
2435     UUkillheaders (&localenv);
2436     sstate.isfolder = 0;
2437     sstate.ismime = 0;
2438     sstate.mimestate = MS_BODY;
2439 root 1.5 _FP_free (result);
2440 root 1.1 return NULL;
2441     }
2442    
2443     /*
2444     * Retry, listening to headers this time
2445     */
2446     fseek (datei, result->startpos, SEEK_SET);
2447    
2448     UUkillfread (result);
2449     if ((result = (fileread *) malloc (sizeof (fileread))) == NULL) {
2450     *errcode = UURET_NOMEM;
2451     sstate.isfolder = 0;
2452     sstate.ismime = 0;
2453     UUkillheaders (&localenv);
2454     return NULL;
2455     }
2456     memset (result, 0, sizeof (fileread));
2457    
2458     if ((res = ScanData (datei, fname, errcode, NULL, 1, 1, result))==-1) {
2459     /* oops, something went wrong */
2460     sstate.isfolder = 0;
2461     sstate.ismime = 0;
2462     UUkillfread (result);
2463     UUkillheaders (&localenv);
2464     return NULL;
2465     }
2466     if (res == 1) {
2467     /*
2468     * new headers found
2469     */
2470     sstate.isfolder = 1;
2471     sstate.ismime = 0;
2472     sstate.mimestate = MS_HEADERS;
2473     }
2474     else {
2475     sstate.isfolder = 0;
2476     sstate.ismime = 0;
2477     }
2478     }
2479 root 1.2
2480     /*
2481     * If this file has been nicely MIME so far, then be very suspicious
2482     * if ScanData reports anything else. So do a double check, and if
2483     * it doesn't hold up, handle as plain text instead.
2484     */
2485    
2486 root 1.3 if (sstate.ismime && sstate.mimestate == MS_SUBPART &&
2487     strcmp (localenv.mimevers, "1.0") == 0 &&
2488 root 1.5 _FP_stristr (localenv.ctype, "text") != NULL &&
2489 root 1.2 !uu_desperate) {
2490     if (result->uudet == UU_ENCODED && !(result->begin || result->end)) {
2491     result->uudet = 0;
2492     }
2493     }
2494    
2495 root 1.1 /*
2496     * produce result
2497     */
2498 root 1.2
2499     if (result->uudet == 0) {
2500 root 1.1 result->uudet = PT_ENCODED; /* plain text */
2501     }
2502    
2503     if (localenv.fname) {
2504 root 1.5 _FP_free (result->filename);
2505     if ((result->filename = _FP_strdup (localenv.fname)) == NULL)
2506 root 1.1 *errcode = UURET_NOMEM;
2507     }
2508     else if ((result->uudet==QP_ENCODED || result->uudet==PT_ENCODED) &&
2509 root 1.2 result->filename==NULL && uu_handletext) {
2510 root 1.1 sprintf (line, "%04d.txt", ++mimseqno);
2511 root 1.5 if ((result->filename = _FP_strdup (line)) == NULL)
2512 root 1.1 *errcode = UURET_NOMEM;
2513     }
2514     else {
2515     /* assign a filename lateron */
2516     }
2517 root 1.5 if (result->mimetype) _FP_free (result->mimetype);
2518 root 1.1 if (result->uudet) {
2519 root 1.5 if (_FP_stristr (localenv.ctype, "text") != NULL &&
2520 root 1.1 result->uudet != QP_ENCODED && result->uudet != PT_ENCODED)
2521     result->mimetype = NULL; /* better don't set it */
2522     else
2523 root 1.5 result->mimetype = _FP_strdup (localenv.ctype);
2524 root 1.1 }
2525 root 1.5 if (result->origin) _FP_free (result->origin);
2526     result->origin = _FP_strdup (localenv.from);
2527 root 1.1
2528 root 1.5 if (result->subject) _FP_free (result->subject);
2529     result->subject = _FP_strdup (localenv.subject);
2530 root 1.1
2531     if (result->sfname == NULL)
2532 root 1.5 if ((result->sfname = _FP_strdup (fname)) == NULL)
2533 root 1.1 *errcode = UURET_NOMEM;
2534    
2535     result->length = prevpos - result->startpos;
2536     result->flags = FL_SINGLE | FL_PROPER;
2537     result->partno = 1;
2538    
2539     if (result->mode == 0)
2540     result->mode = 0644;
2541    
2542     /*
2543     * the other fields should already be set appropriately
2544     */
2545    
2546     if (*errcode == UURET_OK)
2547     *errcode = UURET_CONT;
2548    
2549     /*
2550     * kill local envelope
2551     */
2552     UUkillheaders (&localenv);
2553    
2554     return result;
2555     }
2556    
2557     /*
2558     * All right, so we're not in a Multipart message. Phew, took quite
2559     * long to figure this out. But this might still be a MIME message
2560     * body. And if it's a message/partial, we need more special handling
2561     */
2562    
2563     if (sstate.isfolder && sstate.ismime && sstate.mimestate == MS_BODY &&
2564 root 1.5 _FP_stristr (sstate.envelope.ctype, "message") != NULL &&
2565     _FP_stristr (sstate.envelope.ctype, "partial") != NULL) {
2566 root 1.1
2567     result->startpos = ftell (datei);
2568    
2569     if (sstate.envelope.partno == 1) {
2570     /* read local envelope */
2571     UUkillheaders (&localenv);
2572     memset (&localenv, 0, sizeof (headers));
2573    
2574     /* skip over blank lines first */
2575     prevpos = ftell (datei);
2576     while (!feof (datei)) {
2577 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL)
2578 root 1.1 break;
2579     if (UUBUSYPOLL(ftell(datei),progress.fsize)) SPCANCEL();
2580     if (!IsLineEmpty (line))
2581     break;
2582     prevpos = ftell (datei);
2583     }
2584     /* Next, read header. But what if there is no subheader? */
2585     hcount = 0;
2586     lcount = 0;
2587     preheaders = prevpos;
2588    
2589     while (!feof (datei) && !IsLineEmpty (line)) {
2590     if (IsKnownHeader (line))
2591     hcount++;
2592     if (UUBUSYPOLL(ftell(datei),progress.fsize)) SPCANCEL();
2593     if (lcount > WAITHEADER && hcount == 0) {
2594     fseek (datei, preheaders, SEEK_SET);
2595     break;
2596     }
2597     ptr1 = ScanHeaderLine (datei, line);
2598     if (ParseHeader (&localenv, ptr1) == NULL)
2599     *errcode = UURET_NOMEM;
2600    
2601 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL)
2602 root 1.1 break;
2603     lcount++;
2604     }
2605     prevpos = ftell (datei);
2606     /*
2607     * Examine local header. We're mostly interested in the Content-Type
2608     * and the Content-Transfer-Encoding.
2609     */
2610 root 1.5 if (_FP_stristr (localenv.ctype, "multipart") != NULL) {
2611 root 1.1 UUMessage (uuscan_id, __LINE__, UUMSG_WARNING,
2612     uustring (S_MIME_PART_MULTI));
2613     }
2614     if (localenv.subject)
2615 root 1.5 result->subject = _FP_strdup (localenv.subject);
2616 root 1.1 else
2617 root 1.5 result->subject = _FP_strdup (sstate.envelope.subject);
2618 root 1.1
2619     if (localenv.from)
2620 root 1.5 result->origin = _FP_strdup (localenv.from);
2621 root 1.1 else
2622 root 1.5 result->origin = _FP_strdup (sstate.envelope.from);
2623 root 1.1
2624     if (localenv.ctype)
2625 root 1.5 result->mimetype = _FP_strdup (localenv.ctype);
2626 root 1.1 else
2627 root 1.5 result->mimetype = _FP_strdup ("text/plain");
2628 root 1.1
2629 root 1.5 if (_FP_stristr (localenv.ctenc, "quoted-printable") != NULL)
2630 root 1.1 result->uudet = QP_ENCODED;
2631 root 1.5 else if (_FP_stristr (localenv.ctenc, "base64") != NULL)
2632 root 1.1 result->uudet = B64ENCODED;
2633 root 1.5 else if (_FP_stristr (localenv.ctenc, "x-uue") != NULL) {
2634 root 1.2 result->uudet = UU_ENCODED;
2635 root 1.4 result->begin = result->end = 1;
2636     }
2637 root 1.5 else if (_FP_stristr (localenv.ctenc, "x-yenc") != NULL) {
2638 root 1.4 result->uudet = YENC_ENCODED;
2639     result->begin = result->end = 1;
2640     }
2641 root 1.5 else if (_FP_stristr (localenv.ctenc, "7bit") != NULL ||
2642     _FP_stristr (localenv.ctenc, "8bit") != NULL)
2643 root 1.2 result->uudet = PT_ENCODED;
2644 root 1.5 else if (_FP_stristr (localenv.ctype, "multipart") != NULL ||
2645     _FP_stristr (localenv.ctype, "message") != NULL)
2646 root 1.1 result->uudet = PT_ENCODED;
2647 root 1.2
2648     /*
2649     * If we're switched to MIME-only mode, handle as text
2650     */
2651    
2652     if (uu_more_mime >= 2 && !result->uudet) {
2653     result->uudet = PT_ENCODED;
2654     }
2655 root 1.1 }
2656     else {
2657     memset (&localenv, 0, sizeof (headers));
2658     }
2659    
2660     /*
2661     * If this is Quoted-Printable or Plain Text, just try looking
2662     * for the next message header. If uu_fast_scanning, and the
2663     * encoding is known, there's no need to look below. Otherwise,
2664     * we check the type of encoding first.
2665     * The encoding type is determined on the first part; in all
2666     * others, we also don't read on.
2667     * If we have a partial multipart message, scan for headers, but
2668     * do not react on standard MIME headers, as they are probably
2669     * from the subparts. However, we're stuck if there's an embedded
2670     * message/rfc822 :-(
2671     * If it is a "trivial" (non-embedded) message/rfc822, skip over
2672     * the message header and then start looking for the next header.
2673     */
2674     if (uu_fast_scanning && (result->uudet!=0||sstate.envelope.partno!=1)) {
2675     /* do nothing */
2676     res = 0;
2677     }
2678     else if (result->uudet != 0) {
2679     hcount = lcount = 0;
2680     prevpos = ftell (datei);
2681    
2682 root 1.5 if (_FP_stristr (localenv.ctype, "message") != NULL &&
2683     _FP_stristr (localenv.ctype, "rfc822") != NULL) {
2684 root 1.1 /*
2685     * skip over empty lines and local header
2686     */
2687     preheaders = ftell (datei);
2688     while (!feof (datei)) {
2689 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL)
2690 root 1.1 break;
2691     if (!IsLineEmpty (line)) {
2692     break;
2693     }
2694     }
2695    
2696     while (!feof (datei) && !IsLineEmpty (line)) {
2697     if (IsKnownHeader (line))
2698     hcount++;
2699     lcount++;
2700     if (lcount > WAITHEADER && hcount < hlcount.afternl)
2701     break;
2702    
2703 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL)
2704 root 1.1 break;
2705     }
2706     if (hcount < hlcount.afternl)
2707     fseek (datei, preheaders, SEEK_SET);
2708     hcount = lcount = 0;
2709     }
2710    
2711     /*
2712     * look for next header
2713     */
2714    
2715     while (!feof (datei)) {
2716 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL)
2717 root 1.1 break;
2718     if (UUBUSYPOLL(ftell(datei),progress.fsize)) SPCANCEL();
2719     if (ferror (datei))
2720     break;
2721    
2722     if ((vflag = IsKnownHeader (line))) {
2723     (void) ScanHeaderLine (datei, line);
2724    
2725     if (result->uudet != PT_ENCODED || vflag == 1) {
2726     if (hcount == 0)
2727     preheaders = prevpos;
2728     hcount++;
2729     lcount++;
2730     if (hcount >= hlcount.restart) {
2731     /*
2732     * Hey, a new header starts here
2733     */
2734     fseek (datei, preheaders, SEEK_SET);
2735     prevpos = preheaders;
2736     break;
2737     }
2738     }
2739     }
2740     else if (lcount > WAITHEADER) {
2741     hcount = 0;
2742     lcount = 0;
2743     }
2744     else if (hcount) {
2745     lcount++;
2746     }
2747     prevpos = ftell (datei);
2748     }
2749     res = 1;
2750     }
2751     else {
2752     /*
2753     * Otherwise, let's see what we can find ourself. No
2754     * boundary (NULL) but MIME, and respect new headers.
2755     */
2756     if ((res = ScanData (datei, fname, errcode, NULL, 1, 1, result)) == -1) {
2757     /* oops, something went wrong */
2758     sstate.isfolder = 0;
2759     sstate.ismime = 0;
2760     UUkillfread (result);
2761     UUkillheaders (&localenv);
2762     return NULL;
2763     }
2764     if (result->uudet == 0 && uu_handletext)
2765     result->uudet = PT_ENCODED;
2766    
2767     prevpos = ftell (datei);
2768     }
2769     /*
2770     * produce result
2771     */
2772     if (localenv.fname) {
2773 root 1.5 _FP_free (result->filename);
2774     if ((result->filename = _FP_strdup (localenv.fname)) == NULL)
2775 root 1.1 *errcode = UURET_NOMEM;
2776     }
2777     else if (sstate.envelope.fname) {
2778 root 1.5 _FP_free (result->filename);
2779     if ((result->filename = _FP_strdup (sstate.envelope.fname)) == NULL)
2780 root 1.1 *errcode = UURET_NOMEM;
2781     }
2782     else if ((result->uudet==QP_ENCODED || result->uudet==PT_ENCODED) &&
2783     result->filename == NULL) {
2784     sprintf (line, "%04d.txt", ++mimseqno);
2785 root 1.5 if ((result->filename = _FP_strdup (line)) == NULL)
2786 root 1.1 *errcode = UURET_NOMEM;
2787     }
2788     else {
2789     /* assign a filename lateron */
2790     }
2791     if (result->subject == NULL) {
2792     if (sstate.envelope.subject)
2793 root 1.5 result->subject = _FP_strdup (sstate.envelope.subject);
2794 root 1.1 }
2795     result->partno = sstate.envelope.partno;
2796     result->maxpno = sstate.envelope.numparts;
2797     result->flags = FL_PARTIAL |
2798     ((res==1 || uu_fast_scanning) ? FL_PROPER : 0) |
2799     ((uu_fast_scanning) ? FL_TOEND : 0);
2800 root 1.5 result->mimeid = _FP_strdup (sstate.envelope.mimeid);
2801 root 1.4 if (result->partno == 1)
2802     result->begin = 1;
2803 root 1.1
2804     if (uu_fast_scanning)
2805     result->length = progress.fsize - result->startpos;
2806     else
2807     result->length = prevpos - result->startpos;
2808    
2809     if (result->sfname == NULL)
2810 root 1.5 result->sfname = _FP_strdup (fname);
2811 root 1.1
2812     if (result->mode == 0)
2813     result->mode = 0644;
2814    
2815     /*
2816     * the other fields should already be set appropriately
2817     */
2818    
2819     if (res == 1) {
2820     /*
2821     * new headers found
2822     */
2823     sstate.isfolder = 1;
2824     sstate.ismime = 0;
2825     sstate.mimestate = MS_HEADERS;
2826    
2827     UUkillheaders (&sstate.envelope);
2828     memset (&sstate.envelope, 0, sizeof (headers));
2829     }
2830     else {
2831     /*
2832     * otherwise, this can't be a mail folder
2833     */
2834     sstate.isfolder = 0;
2835     sstate.ismime = 0;
2836     }
2837     /*
2838     * kill local envelope
2839     */
2840     UUkillheaders (&localenv);
2841     return result;
2842     }
2843    
2844     /*
2845 root 1.2 * If this is a MIME body, honor a Content-Type different than
2846 root 1.1 * text/plain or a proper Content-Transfer-Encoding.
2847 root 1.2 * We also go in here if we have an assigned filename - this means
2848     * that we've had a Content-Disposition field, and we should probably
2849     * decode a plain-text segment with a filename.
2850 root 1.1 */
2851 root 1.4
2852 root 1.1 if (sstate.isfolder && sstate.ismime &&
2853     sstate.mimestate == MS_BODY &&
2854 root 1.5 (_FP_stristr (sstate.envelope.ctenc, "quoted-printable") != NULL ||
2855     _FP_stristr (sstate.envelope.ctenc, "base64") != NULL ||
2856     _FP_stristr (sstate.envelope.ctenc, "x-uue") != NULL ||
2857     _FP_stristr (sstate.envelope.ctenc, "x-yenc") != NULL ||
2858     _FP_stristr (sstate.envelope.ctype, "message") != NULL ||
2859 root 1.2 sstate.envelope.fname != NULL)) {
2860 root 1.1
2861     if (sstate.envelope.subject)
2862 root 1.5 result->subject = _FP_strdup (sstate.envelope.subject);
2863 root 1.1 if (sstate.envelope.from)
2864 root 1.5 result->origin = _FP_strdup (sstate.envelope.from);
2865 root 1.1
2866     if (sstate.envelope.ctype)
2867 root 1.5 result->mimetype = _FP_strdup (sstate.envelope.ctype);
2868 root 1.1 else
2869 root 1.5 result->mimetype = _FP_strdup ("text/plain");
2870 root 1.1
2871 root 1.5 if (_FP_stristr (sstate.envelope.ctenc, "quoted-printable") != NULL)
2872 root 1.1 result->uudet = QP_ENCODED;
2873 root 1.5 else if (_FP_stristr (sstate.envelope.ctenc, "base64") != NULL)
2874 root 1.1 result->uudet = B64ENCODED;
2875 root 1.5 else if (_FP_stristr (sstate.envelope.ctenc, "x-uue") != NULL) {
2876 root 1.2 result->uudet = UU_ENCODED;
2877 root 1.4 result->begin = result->end = 1;
2878     }
2879 root 1.5 else if (_FP_stristr (sstate.envelope.ctenc, "x-yenc") != NULL) {
2880 root 1.4 result->uudet = YENC_ENCODED;
2881     }
2882 root 1.5 else if (_FP_stristr (sstate.envelope.ctenc, "7bit") != NULL ||
2883     _FP_stristr (sstate.envelope.ctenc, "8bit") != NULL)
2884 root 1.2 result->uudet = PT_ENCODED;
2885 root 1.5 else if (_FP_stristr (sstate.envelope.ctype, "multipart") != NULL ||
2886     _FP_stristr (sstate.envelope.ctype, "message") != NULL ||
2887 root 1.2 sstate.envelope.fname != NULL)
2888     result->uudet = PT_ENCODED;
2889    
2890     /*
2891     * If we're switched to MIME-only mode, handle as text
2892     */
2893    
2894     if (uu_more_mime >= 2 && !result->uudet) {
2895 root 1.1 result->uudet = PT_ENCODED;
2896 root 1.2 }
2897    
2898     result->startpos = prevpos = ftell (datei);
2899 root 1.1
2900     /*
2901     * If this is Quoted-Printable or Plain Text, just try looking
2902     * for the next message header. If uu_fast_scanning, we know
2903     * there won't be more headers.
2904 root 1.2 * If it is a "trivial" (non-embedded) message/rfc822, skip over
2905     * the message header and then start looking for the next header.
2906 root 1.1 */
2907     if (result->uudet != 0 && uu_fast_scanning) {
2908     /* do nothing */
2909     res = 0;
2910     }
2911     else if (result->uudet != 0) {
2912     hcount = lcount = 0;
2913 root 1.2 prevpos = ftell (datei);
2914    
2915 root 1.5 if (_FP_stristr (sstate.envelope.ctype, "message") != NULL &&
2916     _FP_stristr (sstate.envelope.ctype, "rfc822") != NULL) {
2917 root 1.2 /*
2918     * skip over empty lines and local header
2919     */
2920     preheaders = ftell (datei);
2921     while (!feof (datei)) {
2922 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL)
2923 root 1.2 break;
2924     if (!IsLineEmpty (line)) {
2925     break;
2926     }
2927     }
2928    
2929     while (!feof (datei) && !IsLineEmpty (line)) {
2930     if (IsKnownHeader (line))
2931     hcount++;
2932     lcount++;
2933     if (lcount > WAITHEADER && hcount < hlcount.afternl)
2934     break;
2935    
2936 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL)
2937 root 1.2 break;
2938     }
2939     if (hcount < hlcount.afternl)
2940     fseek (datei, preheaders, SEEK_SET);
2941     hcount = lcount = 0;
2942     }
2943    
2944     /*
2945     * look for next header
2946     */
2947 root 1.1
2948     while (!feof (datei)) {
2949 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL)
2950 root 1.1 break;
2951     if (UUBUSYPOLL(ftell(datei),progress.fsize)) SPCANCEL();
2952     if (ferror (datei))
2953     break;
2954    
2955     if (IsKnownHeader (line)) {
2956     (void) ScanHeaderLine (datei, line);
2957     if (hcount == 0)
2958     preheaders = prevpos;
2959     hcount++;
2960     lcount++;
2961     if (hcount >= hlcount.restart) {
2962     /*
2963     * Hey, a new header starts here
2964     */
2965     fseek (datei, preheaders, SEEK_SET);
2966     prevpos = preheaders;
2967     break;
2968     }
2969     }
2970     else if (lcount > WAITHEADER) {
2971     hcount = 0;
2972     lcount = 0;
2973     }
2974     else if (hcount) {
2975     lcount++;
2976     }
2977     prevpos = ftell (datei);
2978     }
2979     res = 1;
2980     }
2981     else {
2982     /*
2983     * Otherwise, let's see what we can find ourself. No
2984     * boundary (NULL) but MIME, and respect new headers.
2985     */
2986     if ((res = ScanData (datei, fname, errcode, NULL, 1, 1, result)) == -1) {
2987     /* oops, something went wrong */
2988     sstate.isfolder = 0;
2989     sstate.ismime = 0;
2990     UUkillfread (result);
2991     return NULL;
2992     }
2993     if (result->uudet == 0 && uu_handletext) {
2994     result->startpos = before; /* display headers */
2995     result->uudet = PT_ENCODED;
2996     }
2997    
2998     prevpos = ftell (datei);
2999     }
3000     /*
3001     * produce result
3002     */
3003     if (sstate.envelope.fname) {
3004 root 1.5 _FP_free (result->filename);
3005     if ((result->filename = _FP_strdup (sstate.envelope.fname)) == NULL)
3006 root 1.1 *errcode = UURET_NOMEM;
3007     }
3008     else if ((result->uudet==QP_ENCODED||result->uudet==PT_ENCODED) &&
3009     result->filename == NULL) {
3010     sprintf (line, "%04d.txt", ++mimseqno);
3011 root 1.5 if ((result->filename = _FP_strdup (line)) == NULL)
3012 root 1.1 *errcode = UURET_NOMEM;
3013     }
3014     else {
3015     /* assign a filename lateron */
3016     }
3017     if (result->subject == NULL) {
3018     if (sstate.envelope.subject)
3019 root 1.5 result->subject = _FP_strdup (sstate.envelope.subject);
3020 root 1.1 }
3021     result->flags = ((res==1||uu_fast_scanning)?FL_PROPER:0) |
3022     ((uu_fast_scanning) ? FL_TOEND : 0);
3023 root 1.5 result->mimeid = _FP_strdup (sstate.envelope.mimeid);
3024 root 1.1
3025     if (uu_fast_scanning)
3026     result->length = progress.fsize - result->startpos;
3027     else
3028     result->length = prevpos - result->startpos;
3029    
3030     if (result->sfname == NULL)
3031 root 1.5 result->sfname = _FP_strdup (fname);
3032 root 1.1
3033     if (result->mode == 0)
3034     result->mode = 0644;
3035    
3036     /*
3037     * the other fields should already be set appropriately
3038     */
3039    
3040     if (res == 1) {
3041     /*
3042     * new headers found
3043     */
3044     sstate.isfolder = 1;
3045     sstate.ismime = 0;
3046     sstate.mimestate = MS_HEADERS;
3047    
3048     UUkillheaders (&sstate.envelope);
3049     memset (&sstate.envelope, 0, sizeof (headers));
3050     }
3051     else {
3052     /*
3053     * otherwise, this can't be a mail folder
3054     */
3055     sstate.isfolder = 0;
3056     sstate.ismime = 0;
3057     }
3058    
3059     return result;
3060     }
3061    
3062     /*
3063     * Some files have reduced headers, and what should be a multipart
3064     * message is missing the proper Content-Type. If the first thing
3065     * we find after a couple of empty lines is a boundary, try it!
3066     * But make sure that this is indeed intended as being a boundary.
3067     *
3068     * Only accept it if there was indeed no Content-Type header line
3069     * and if the following line is a proper Content-Type header. BTW,
3070     * we know that sstate.envelope.boundary is NULL, or we wouldn't
3071     * be here!
3072     */
3073 root 1.2
3074 root 1.12 if ((!sstate.envelope.ctype || _FP_stristr (sstate.envelope.ctype, "multipart"))
3075     && !uu_more_mime) {
3076 root 1.1 prevpos = ftell (datei);
3077     while (!feof (datei)) {
3078 root 1.15 if (_FP_fgets (line, 1024, datei) == NULL)
3079 root 1.1 break;
3080     if (UUBUSYPOLL(ftell(datei),progress.fsize)) SPCANCEL();
3081     if (!IsLineEmpty (line))
3082     break;
3083     }
3084     if (line[0] == '-' && line[1] == '-' &&
3085     !IsLineEmpty (line+2) && !feof (datei)) {
3086 root 1.5 ptr1 = _FP_strrstr (line+2, "--");
3087 root 1.1 ptr2 = ScanHeaderLine (datei, NULL);
3088 root 1.16 if ((ptr1 == NULL || ptr1[2]) &&
3089 root 1.5 ptr2 && _FP_strnicmp (ptr2, "Content-", 8) == 0) {
3090 root 1.1 /*
3091     * hmm, okay, let's do it!
3092     */
3093     sstate.isfolder = 1;
3094     sstate.ismime = 1;
3095     sstate.mimestate = MS_PREAMBLE;
3096     /*
3097     * get boundary
3098     */
3099     ptr1 = line+2;
3100     while (*ptr1 && !isspace(*ptr1))
3101     ptr1++;
3102     *ptr1 = '\0';
3103 root 1.4
3104 root 1.5 sstate.envelope.mimevers = _FP_strdup ("1.0");
3105     sstate.envelope.boundary = _FP_strdup (line+2);
3106 root 1.1
3107     /*
3108     * need restart
3109     */
3110    
3111     fseek (datei, prevpos, SEEK_SET);
3112    
3113 root 1.5 _FP_free (result);
3114 root 1.1 return NULL;
3115     }
3116     }
3117     fseek (datei, prevpos, SEEK_SET);
3118     }
3119    
3120     /*
3121     * Hmm, we're not in a ''special'' state, so it's more or less
3122     * Freestyle time. Anyway, if this seems to be a Mime message,
3123     * don't allow the minimal Base64 handling.
3124     */
3125    
3126     if (sstate.envelope.subject)
3127 root 1.5 result->subject = _FP_strdup (sstate.envelope.subject);
3128 root 1.12
3129 root 1.1 if (sstate.envelope.from)
3130 root 1.5 result->origin = _FP_strdup (sstate.envelope.from);
3131 root 1.1
3132     if (sstate.envelope.ctype)
3133 root 1.5 result->mimetype = _FP_strdup (sstate.envelope.ctype);
3134 root 1.1
3135     if ((res=ScanData (datei, fname, errcode, NULL,
3136     sstate.ismime, 1, result))==-1) {
3137     /* oops, something went wrong */
3138     sstate.isfolder = 0;
3139     sstate.ismime = 0;
3140     UUkillfread (result);
3141     return NULL;
3142     }
3143 root 1.4
3144 root 1.1 /*
3145     * produce result
3146     */
3147 root 1.4
3148 root 1.1 if (result->uudet == 0 && uu_handletext) {
3149     result->startpos = before; /* display headers */
3150     result->uudet = PT_ENCODED;
3151     result->partno = 1;
3152     }
3153    
3154 root 1.4 if (result->uudet == YENC_ENCODED && result->filename != NULL) {
3155     /*
3156     * prevent replacing the filename found on the =ybegin line
3157     */
3158     }
3159     else if (sstate.envelope.fname) {
3160 root 1.5 _FP_free (result->filename);
3161     if ((result->filename = _FP_strdup (sstate.envelope.fname)) == NULL)
3162 root 1.1 *errcode = UURET_NOMEM;
3163     }
3164     else if ((result->uudet==QP_ENCODED||result->uudet==PT_ENCODED) &&
3165     result->filename == NULL) {
3166     sprintf (line, "%04d.txt", ++mimseqno);
3167 root 1.5 if ((result->filename = _FP_strdup (line)) == NULL)
3168 root 1.1 *errcode = UURET_NOMEM;
3169     }
3170     else {
3171     /* assign a filename lateron */
3172     }
3173 root 1.4
3174 root 1.1 if (result->subject == NULL) {
3175     if (sstate.envelope.subject)
3176 root 1.5 result->subject = _FP_strdup (sstate.envelope.subject);
3177 root 1.1 }
3178    
3179     result->flags = (result->uudet==PT_ENCODED)?FL_SINGLE:0;
3180 root 1.5 result->mimeid = _FP_strdup (sstate.envelope.mimeid);
3181 root 1.1 result->length = ftell (datei) - result->startpos;
3182    
3183     if (result->mode == 0)
3184     result->mode = 0644;
3185    
3186     if (result->sfname == NULL)
3187 root 1.5 result->sfname = _FP_strdup (fname);
3188 root 1.1
3189     if (res == 1) {
3190     /*
3191     * new headers found
3192     */
3193     sstate.isfolder = 1;
3194     sstate.ismime = 0;
3195     sstate.mimestate = MS_HEADERS;
3196    
3197     UUkillheaders (&sstate.envelope);
3198     memset (&sstate.envelope, 0, sizeof (headers));
3199     }
3200     else {
3201     /*
3202     * otherwise, this can't be a mail folder
3203     */
3204     sstate.isfolder = 0;
3205     sstate.ismime = 0;
3206     }
3207    
3208     return result;
3209    
3210     /*
3211     * Emergency handling. Set errcode before jumping here.
3212     */
3213     ScanPartEmergency:
3214     UUkillfread (result);
3215     UUkillheaders (&localenv);
3216    
3217     while (mssdepth) {
3218     mssdepth--;
3219     UUkillheaders (&(multistack[mssdepth].envelope));
3220 root 1.5 _FP_free (multistack[mssdepth].source);
3221 root 1.1 }
3222    
3223     return NULL;
3224     }
3225