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