ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Convert-UUlib/uulib/uuscan.c
Revision: 1.11
Committed: Sat May 26 15:14:10 2007 UTC (17 years ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-1_09
Changes since 1.10: +10 -4 lines
Log Message:
*** empty log message ***

File Contents

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