ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Convert-UUlib/uulib/uucheck.c
Revision: 1.14
Committed: Sun Mar 25 00:18:07 2007 UTC (17 years, 2 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-1_11, rel-1_10, rel-1_09
Changes since 1.13: +0 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 * This file is part of uudeview, the simple and friendly multi-part multi-
3 * file uudecoder program (c) 1994-2001 by Frank Pilhofer. The author may
4 * be contacted at fp@fpx.de
5 *
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 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #ifdef SYSTEM_WINDLL
22 #include <windows.h>
23 #endif
24 #ifdef SYSTEM_OS2
25 #include <os2.h>
26 #endif
27
28 /*
29 * uucheck.c
30 *
31 * Various checking and processing of one input part
32 **/
33
34 #include <stdio.h>
35 #include <ctype.h>
36
37 #ifdef STDC_HEADERS
38 #include <stdlib.h>
39 #include <string.h>
40 #endif
41 #ifdef HAVE_MALLOC_H
42 #include <malloc.h>
43 #endif
44 #ifdef HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif
47 #ifdef HAVE_MEMORY_H
48 #include <memory.h>
49 #endif
50
51 #include <uudeview.h>
52 #include <uuint.h>
53 #include <fptools.h>
54 #include <uustring.h>
55
56 char * uucheck_id = "$Id$";
57
58 /*
59 * Arbitrary number. This is the maximum number of part numbers we
60 * store for our have-parts and missing-parts lists
61 */
62
63 #define MAXPLIST 256
64
65
66 /*
67 * forward declarations of local functions
68 */
69
70 static char * UUGetFileName (char *, char *, char *);
71 static int UUGetPartNo (char *, char **, char **);
72
73 /*
74 * State of Scanner function and PreProcessPart
75 */
76
77 int lastvalid, lastenc, nofnum;
78 char *uucheck_lastname;
79 char *uucheck_tempname;
80 static int lastpart = 0;
81 static char *nofname = "UNKNOWN";
82
83 /*
84 * special characters we allow an unquoted filename to have
85 */
86
87 static char *fnchars = "._-~!";
88
89 /*
90 * Policy for extracting a part number from the subject line.
91 * usually, look for part numbers in () brackets first, then in []
92 */
93
94 static char *brackchr[] = {
95 "()[]", "[]()"
96 };
97
98 /*
99 * Extract a filename from the subject line. We need anything to identify
100 * the name of the program for sorting. If a nice filename cannot be found,
101 * the subject line itself is used
102 * ptonum is, if not NULL, a pointer to the part number in the subject line,
103 * so that it won't be used as filename.
104 **/
105 static char *
106 UUGetFileName (char *subject, char *ptonum, char *ptonend)
107 {
108 char *ptr = subject, *iter, *result, *part;
109 int count, length=0, alflag=0;
110
111 /*
112 * If this file has no subject line, assume it is the next part of the
113 * previous file (this is done in UUPreProcessPart)
114 **/
115
116 if (subject == NULL)
117 return NULL;
118
119 /*
120 * If the subject starts with 'Re', it is ignored
121 * REPosts or RETries are not ignored!
122 **/
123
124 if (uu_ignreply &&
125 (subject[0] == 'R' || subject[0] == 'r') &&
126 (subject[1] == 'E' || subject[1] == 'e') &&
127 (subject[2] == ':' || subject[2] == ' ')) {
128 return uu_FileNameCallback ? uu_FileNameCallback(uu_FNCBArg, subject, NULL) : NULL;
129 }
130
131 /*
132 * Ignore a "Repost" prefix of the subject line. We don't want to get
133 * a file named "Repost" :-)
134 **/
135
136 if (_FP_strnicmp (subject, "repost", 6) == 0)
137 subject += 6;
138 if (_FP_strnicmp (subject, "re:", 3) == 0)
139 subject += 3;
140
141 while (*subject == ' ' || *subject == ':') subject++;
142
143 part = _FP_stristr (subject, "part");
144 if (part == subject) {
145 subject += 4;
146 while (*subject == ' ') subject++;
147 }
148
149 /*
150 * If the file was encoded by uuenview, then the filename is enclosed
151 * in [brackets]. But check what's inside these bracket's, try not to
152 * fall for something other than a filename
153 */
154
155 ptr = subject;
156 while ((iter = strchr (ptr, '[')) != NULL) {
157 if (strchr (iter, ']') == NULL) {
158 ptr = iter + 1;
159 continue;
160 }
161 iter++;
162 while (isspace (*iter))
163 iter++;
164 count = length = alflag = 0;
165 while (iter[count] &&
166 (isalnum (iter[count]) || strchr (fnchars, iter[count])!=NULL)) {
167 if (isalpha (iter[count]))
168 alflag++;
169 count++;
170 }
171 if (count<4 || alflag==0) {
172 ptr = iter + 1;
173 continue;
174 }
175 length = count;
176 while (isspace (iter[count]))
177 count++;
178 if (iter[count] == ']') {
179 ptr = iter;
180 break;
181 }
182 length = 0;
183 ptr = iter + 1;
184 }
185
186 /*
187 * new filename detection routine, fists mostly for files by ftp-by-email
188 * servers that create subject lines with ftp.host.address:/full/path/file
189 * on them. We look for slashes and take the filename from after the last
190 * one ... or at least we try to.
191 */
192
193 if (length == 0) {
194 ptr = subject;
195 while ((iter = strchr (ptr, '/')) != NULL) {
196 if (iter >= ptonum && iter <= ptonend) {
197 ptr = iter + 1;
198 continue;
199 }
200 count = length = 0;
201 iter++;
202 while (iter[count] &&
203 (isalnum(iter[count])||strchr(fnchars, iter[count])!=NULL))
204 count++;
205 if (iter[count] == ' ' && length > 4) {
206 length = count;
207 break;
208 }
209 ptr = iter + ((count)?count:1);
210 }
211 }
212
213 /*
214 * Look for two alphanumeric strings separated by a '.'
215 * (That's most likely a filename)
216 **/
217
218 if (length == 0) {
219 ptr = subject;
220 /* #warning another experimental change */
221 /*while (*ptr && *ptr != 0x0a && *ptr != 0x0d && ptr != part) {*/
222 while (*ptr && *ptr != 0x0a && *ptr != 0x0d) {
223 iter = ptr;
224 count = length = alflag = 0;
225
226 if (_FP_strnicmp (ptr, "ftp", 3) == 0) {
227 /* hey, that's an ftp address */
228 while (isalpha (*ptr) || isdigit (*ptr) || *ptr == '.')
229 ptr++;
230 continue;
231 }
232
233 while ((isalnum(*iter)||strchr(fnchars, *iter)!=NULL||
234 *iter=='/') && *iter && iter != ptonum && *iter != '.') {
235 if (isalpha (*iter))
236 alflag = 1;
237
238 count++; iter++;
239 }
240 if (*iter == '\0' || iter == ptonum) {
241 if (iter == ptonum)
242 ptr = ptonend;
243 else
244 ptr = iter;
245
246 length = 0;
247 continue;
248 }
249
250 /* #warning multi-part change experimental, make pluggable */
251 /* if (*iter++ != '.' || count > 32 || alflag == 0) { */
252 if (*iter++ != '.' || count > 32) {
253 ptr = iter;
254 length = 0;
255 continue;
256 }
257
258 /* a dot followed by sth. unesthetically doesn't look right */
259 if (*iter < '0') {
260 ptr = iter + 1;
261 length = 0;
262 continue;
263 }
264
265 if (_FP_strnicmp (iter, "edu", 3) == 0 ||
266 _FP_strnicmp (iter, "gov", 3) == 0) {
267 /* hey, that's an ftp address */
268 while (isalpha (*iter) || isdigit (*iter) || *iter == '.')
269 iter++;
270 ptr = iter;
271 length = 0;
272 continue;
273 }
274
275 length += count + 1;
276 count = 0;
277
278 while ((isalnum(iter[count])||strchr(fnchars, iter[count])!=NULL||
279 iter[count]=='/') && iter[count] && iter[count] != '.')
280 count++;
281
282 if (iter[count]==':' && iter[count+1]=='/') {
283 /* looks like stuff from a mail server */
284 ptr = iter + 1;
285 length = 0;
286 continue;
287 }
288
289 if (count > 8 || iter == ptonum) {
290 ptr = iter;
291 length = 0;
292 continue;
293 }
294
295 if (iter[count] != '.') {
296 length += count;
297 break;
298 }
299
300 while (iter[count] &&
301 (isalnum(iter[count])||strchr(fnchars, iter[count])!=NULL||
302 iter[count]=='/'))
303 count++;
304
305 if (iter[count]==':' && iter[count+1]=='/') {
306 /* looks like stuff from a mail server */
307 ptr = iter + 1;
308 length = 0;
309 continue;
310 }
311
312 if (count < 12 && iter != ptonum) {
313 length += count;
314 break;
315 }
316
317 ptr = iter;
318 length = 0;
319 }
320 }
321
322 if (length == 0) { /* No filename found, use subject line for ident */
323 ptr = subject;
324
325 while (*ptr && !isalpha (*ptr))
326 ptr++;
327
328 while ((isalnum(ptr[length])||strchr(fnchars,ptr[length])!=NULL||
329 ptr[length] == '/') &&
330 ptr[length] && ptr+length!=part && ptr+length!=ptonum)
331 length++;
332
333 if (length) {
334 if (ptr[length] == '\0' || ptr[length] == 0x0a || ptr[length] == 0x0d) {
335 length--;
336
337 /*
338 * I used to cut off digits from the end of the string, but
339 * let's try to live without. We want to distinguish
340 * DUTCH951 from DUTCH952
341 *
342 * while ((ptr[length] == ' ' || isdigit (ptr[length])) && length > 0)
343 * length--;
344 */
345 }
346 else {
347 length--;
348
349 while (ptr[length] == ' ' && length > 0)
350 length--;
351 }
352 length++;
353 }
354 }
355
356 if (length == 0) { /* Still found nothing? We need *something*! */
357 ptr = nofname;
358 length = strlen (nofname);
359 }
360
361 if ((result = (char *) malloc (length + 1)) == NULL) {
362 UUMessage (uucheck_id, __LINE__, UUMSG_ERROR,
363 uustring (S_OUT_OF_MEMORY), length+1);
364 return NULL;
365 }
366
367 memcpy (result, ptr, length);
368 result[length] = '\0';
369
370 return uu_FileNameCallback ? uu_FileNameCallback(uu_FNCBArg, subject, result) : result;
371 }
372
373 /*
374 * Extract the Part Number from the subject line.
375 * We look first for numbers in (#/#)'s, then for numbers in [#/#]'s
376 * and then for digits that are not part of a string.
377 * If we cannot find anything, assume it is the next part of the
378 * previous file.
379 * If we find a part number, we put a pointer to it in *where. This is
380 * done so that the UUGetFileName function doesn't accidentally use the
381 * part number as the file name. *whend points to the end of this part
382 * number.
383 **/
384
385 static int
386 UUGetPartNo (char *subject, char **where, char **whend)
387 {
388 char *ptr = subject, *iter, *delim, bdel[2]=" ";
389 int count, length=0, bpc;
390
391 *where = NULL; bdel[0] = ' ';
392 *whend = NULL; bdel[1] = '\0';
393
394 iter = NULL;
395 delim = "";
396
397 if (subject == NULL)
398 return -1;
399
400 if (uu_ignreply &&
401 (subject[0] == 'R' || subject[0] == 'r') && /* Ignore replies, but not */
402 (subject[1] == 'E' || subject[1] == 'e') && /* reposts */
403 (subject[2] == ':' || subject[2] == ' '))
404 return -2;
405
406 /*
407 * First try numbers in () or [] (or vice versa, according to bracket
408 * policy)
409 * For multiple occurences, give priority to the bracket with a slash
410 * or the last one, whichever is "found" first.
411 */
412
413 for (bpc=0, length=0; brackchr[uu_bracket_policy][bpc]; bpc+=2) {
414 iter = subject;
415 length = 0;
416 while ((iter = strchr (iter, brackchr[uu_bracket_policy][bpc])) != NULL) {
417 int plength;
418
419 count = 0; iter++;
420
421 while (*iter == ' ' || *iter == '#')
422 iter++;
423
424 if (!isdigit (*iter)) {
425 continue;
426 }
427 while (isdigit (iter[count]))
428 count++;
429
430 if (iter[count] == '\0') {
431 iter += count;
432 break;
433 }
434
435 plength = count;
436
437 if (iter[count] == brackchr[uu_bracket_policy][bpc+1]) {
438 *where = iter;
439 bdel[0] = brackchr[uu_bracket_policy][bpc+1];
440 delim = bdel;
441 length = plength;
442 continue;
443 }
444
445 while (iter[count] == ' ' || iter[count] == '#' ||
446 iter[count] == '/' || iter[count] == '\\') count++;
447
448 if (_FP_strnicmp (iter + count, "of", 2) == 0)
449 count += 2;
450
451 while (iter[count] == ' ') count++;
452 while (isdigit (iter[count])) count++;
453 while (iter[count] == ' ') count++;
454
455 if (iter[count] == brackchr[uu_bracket_policy][bpc+1]) {
456 *where = iter;
457 bdel[0] = brackchr[uu_bracket_policy][bpc+1];
458 delim = bdel;
459 length = plength;
460 break;
461 }
462 }
463 if (length)
464 {
465 iter = *where; /* strange control flow, but less changes == less hassle */
466 break;
467 }
468 }
469
470 /*
471 * look for the string "part " followed by a number
472 */
473
474 if (length == 0) {
475 if ((iter = _FP_stristr (subject, "part ")) != NULL) {
476 iter += 5;
477
478 while (isspace (*iter) || *iter == '.' || *iter == '-')
479 iter++;
480
481 while (isdigit (iter[length]))
482 length++;
483
484 if (length == 0) {
485 if (_FP_strnicmp (iter, "one", 3) == 0) length = 1;
486 else if (_FP_strnicmp (iter, "two", 3) == 0) length = 2;
487 else if (_FP_strnicmp (iter, "three", 5) == 0) length = 3;
488 else if (_FP_strnicmp (iter, "four", 4) == 0) length = 4;
489 else if (_FP_strnicmp (iter, "five", 4) == 0) length = 5;
490 else if (_FP_strnicmp (iter, "six", 3) == 0) length = 6;
491 else if (_FP_strnicmp (iter, "seven", 5) == 0) length = 7;
492 else if (_FP_strnicmp (iter, "eight", 5) == 0) length = 8;
493 else if (_FP_strnicmp (iter, "nine", 4) == 0) length = 9;
494 else if (_FP_strnicmp (iter, "ten", 3) == 0) length = 10;
495
496 if (length && (*whend = strchr (iter, ' '))) {
497 *where = iter;
498 return length;
499 }
500 else
501 length = 0;
502 }
503 else {
504 *where = iter;
505 delim = "of";
506 }
507 }
508 }
509
510 /*
511 * look for the string "part" followed by a number
512 */
513
514 if (length == 0) {
515 if ((iter = _FP_stristr (subject, "part")) != NULL) {
516 iter += 4;
517
518 while (isspace (*iter) || *iter == '.' || *iter == '-')
519 iter++;
520
521 while (isdigit (iter[length]))
522 length++;
523
524 if (length == 0) {
525 if (_FP_strnicmp (iter, "one", 3) == 0) length = 1;
526 else if (_FP_strnicmp (iter, "two", 3) == 0) length = 2;
527 else if (_FP_strnicmp (iter, "three", 5) == 0) length = 3;
528 else if (_FP_strnicmp (iter, "four", 4) == 0) length = 4;
529 else if (_FP_strnicmp (iter, "five", 4) == 0) length = 5;
530 else if (_FP_strnicmp (iter, "six", 3) == 0) length = 6;
531 else if (_FP_strnicmp (iter, "seven", 5) == 0) length = 7;
532 else if (_FP_strnicmp (iter, "eight", 5) == 0) length = 8;
533 else if (_FP_strnicmp (iter, "nine", 4) == 0) length = 9;
534 else if (_FP_strnicmp (iter, "ten", 3) == 0) length = 10;
535
536 if (length && (*whend = strchr (iter, ' '))) {
537 *where = iter;
538 return length;
539 }
540 else
541 length = 0;
542 }
543 else {
544 *where = iter;
545 delim = "of";
546 }
547 }
548 }
549
550 /*
551 * look for [0-9]* "of" [0-9]*
552 */
553
554 if (length == 0) {
555 if ((iter = _FP_strirstr (subject, "of")) != NULL) {
556 while (iter>subject && isspace (*(iter-1)))
557 iter--;
558 if (isdigit(*(iter-1))) {
559 while (iter>subject && isdigit (*(iter-1)))
560 iter--;
561 if (!isdigit (*iter) && !isalpha (*iter) && *iter != '.')
562 iter++;
563 ptr = iter;
564
565 while (isdigit (*ptr)) {
566 ptr++; length++;
567 }
568 *where = iter;
569 delim = "of";
570 }
571 }
572 }
573
574 /*
575 * look for whitespace-separated (or '/'-separated) digits
576 */
577
578 if (length == 0) {
579 ptr = subject;
580
581 while (*ptr && length==0) {
582 while (*ptr && !isdigit (*ptr))
583 ptr++;
584 if (isdigit (*ptr) && (ptr==subject || *ptr==' ' || *ptr=='/')) {
585 while (isdigit (ptr[length]))
586 length++;
587 if (ptr[length]!='\0' && ptr[length]!=' ' && ptr[length]!='/') {
588 ptr += length;
589 length = 0;
590 }
591 else {
592 iter = ptr;
593 bdel[0] = ptr[length];
594 delim = bdel;
595 }
596 }
597 else {
598 while (isdigit (*ptr))
599 ptr++;
600 }
601 }
602 }
603
604 /*
605 * look for _any_ digits -- currently disabled, because it also fell
606 * for "part numbers" in file names
607 */
608
609 #if 0
610 if (length == 0) {
611 count = strlen(subject) - 1;
612 ptr = subject;
613
614 while (count > 0) {
615 if (!isdigit(ptr[count])||isalpha(ptr[count+1])||ptr[count+1] == '.') {
616 count--;
617 continue;
618 }
619 length = 0;
620
621 while (count >= 0 && isdigit (ptr[count])) {
622 count--; length++;
623 }
624 if (count>=0 && ((isalpha (ptr[count]) &&
625 (ptr[count] != 's' || ptr[count+1] != 't') &&
626 (ptr[count] != 'n' || ptr[count+1] != 'd')) ||
627 ptr[count] == '/' || ptr[count] == '.' ||
628 ptr[count] == '-' || ptr[count] == '_')) {
629 length = 0;
630 continue;
631 }
632 count++;
633 iter = ptr + count;
634
635 if (length > 4) {
636 length = 0;
637 continue;
638 }
639 *where = iter;
640 delim = "of";
641 break;
642 }
643 }
644 #endif
645
646 /*
647 * look for part numbering as string
648 */
649
650 if (length == 0) {
651 /*
652 * some people use the strangest things, including spelling mistakes :-)
653 */
654 if ((iter = _FP_stristr (subject, "first")) != NULL) length = 1;
655 else if ((iter = _FP_stristr (subject, "second")) != NULL) length = 2;
656 else if ((iter = _FP_stristr (subject, "third")) != NULL) length = 3;
657 else if ((iter = _FP_stristr (subject, "forth")) != NULL) length = 4;
658 else if ((iter = _FP_stristr (subject, "fourth")) != NULL) length = 4;
659 else if ((iter = _FP_stristr (subject, "fifth")) != NULL) length = 5;
660 else if ((iter = _FP_stristr (subject, "sixth")) != NULL) length = 6;
661 else if ((iter = _FP_stristr (subject, "seventh")) != NULL) length = 7;
662 else if ((iter = _FP_stristr (subject, "eigth")) != NULL) length = 8;
663 else if ((iter = _FP_stristr (subject, "eighth")) != NULL) length = 8;
664 else if ((iter = _FP_stristr (subject, "nineth")) != NULL) length = 9;
665 else if ((iter = _FP_stristr (subject, "ninth")) != NULL) length = 9;
666 else if ((iter = _FP_stristr (subject, "tenth")) != NULL) length = 10;
667 else iter = NULL;
668
669 if (length && iter && (*whend = strchr (iter, ' '))) {
670 *where = iter;
671 return length;
672 }
673 else
674 length = 0;
675 }
676
677 if (iter == NULL || length == 0) /* should be equivalent */
678 return -1;
679
680 *where = iter;
681
682 if (delim && delim[0]) {
683 if ((*whend=_FP_stristr (iter, delim)) != NULL && (*whend - *where) < 12) {
684 ptr = (*whend += strlen (delim));
685
686 while (*ptr == ' ')
687 ptr++;
688
689 if (isdigit (*ptr)) {
690 *whend = ptr;
691 while (isdigit (**whend))
692 *whend += 1;
693 }
694 }
695 else {
696 *whend = iter + length;
697 }
698 }
699 else {
700 *whend = iter + length;
701 }
702
703 return atoi (iter);
704 }
705
706 /*
707 * Obtain and process some information about the data.
708 **/
709
710 uufile *
711 UUPreProcessPart (fileread *data, int *ret)
712 {
713 char *where, *whend, temp[80], *ptr, *p2;
714 uufile *result;
715
716 if ((result = (uufile *) malloc (sizeof (uufile))) == NULL) {
717 UUMessage (uucheck_id, __LINE__, UUMSG_ERROR,
718 uustring (S_OUT_OF_MEMORY), sizeof (uufile));
719 *ret = UURET_NOMEM;
720 return NULL;
721 }
722 memset (result, 0, sizeof (uufile));
723
724 if (data->partno) {
725 where = whend = NULL;
726 result->partno = data->partno;
727 }
728 else if (uu_dumbness) {
729 result->partno = -1;
730 where = whend = NULL;
731 }
732 else if ((result->partno=UUGetPartNo(data->subject,&where,&whend)) == -2) {
733 *ret = UURET_NODATA;
734 UUkillfile (result);
735 return NULL;
736 }
737
738 if (data->filename != NULL) {
739 if ((result->filename = _FP_strdup (data->filename)) == NULL) {
740 UUMessage (uucheck_id, __LINE__, UUMSG_ERROR,
741 uustring (S_OUT_OF_MEMORY),
742 strlen (data->filename)+1);
743 *ret = UURET_NOMEM;
744 UUkillfile (result);
745 return NULL;
746 }
747 }
748 else
749 result->filename = NULL;
750
751 if (uu_dumbness <= 1)
752 result->subfname = UUGetFileName (data->subject, where, whend);
753 else
754 result->subfname = NULL;
755
756 result->mimeid = _FP_strdup (data->mimeid);
757 result->mimetype = _FP_strdup (data->mimetype);
758
759 if (result->partno == -1 &&
760 (data->uudet == PT_ENCODED || data->uudet == QP_ENCODED))
761 result->partno = 1;
762
763 if (data->flags & FL_SINGLE) {
764 /*
765 * Don't touch this part. But it should really have a filename
766 */
767 if (result->filename == NULL) {
768 sprintf (temp, "%s.%03d", nofname, ++nofnum);
769 result->filename = _FP_strdup (temp);
770 }
771 if (result->subfname == NULL)
772 result->subfname = _FP_strdup (result->filename);
773
774 if (result->filename == NULL ||
775 result->subfname == NULL) {
776 UUMessage (uucheck_id, __LINE__, UUMSG_ERROR,
777 uustring (S_OUT_OF_MEMORY),
778 (result->filename==NULL)?
779 (strlen(temp)+1):(strlen(result->filename)+1));
780 *ret = UURET_NOMEM;
781 UUkillfile(result);
782 return NULL;
783 }
784 if (result->partno == -1)
785 result->partno = 1;
786 }
787 else if (result->subfname == NULL && data->uudet &&
788 (data->begin || result->partno == 1 ||
789 (!uu_dumbness && result->partno == -1 &&
790 (data->subject != NULL || result->filename != NULL)))) {
791 /*
792 * If it's the first part of something and has some valid data, but
793 * no subject or anything, initialize lastvalid
794 */
795 /*
796 * in this case, it really _should_ have a filename somewhere
797 */
798 if (result->filename != NULL && *result->filename)
799 result->subfname = _FP_strdup (result->filename);
800 else { /* if not, escape to UNKNOWN. We need to fill subfname */
801 sprintf (temp, "%s.%03d", nofname, ++nofnum);
802 result->subfname = _FP_strdup (temp);
803 }
804 /*
805 * in case the strdup failed
806 */
807 if (result->subfname == NULL) {
808 UUMessage (uucheck_id, __LINE__, UUMSG_ERROR,
809 uustring (S_OUT_OF_MEMORY),
810 (result->filename)?
811 (strlen(result->filename)+1):(strlen(temp)+1));
812 *ret = UURET_NOMEM;
813 UUkillfile (result);
814 return NULL;
815 }
816 /*
817 * if it's also got an 'end', or is the last part in a MIME-Mail,
818 * then don't set lastvalid
819 */
820 if (!data->end && (!data->partno || data->partno != data->maxpno)) {
821 /*
822 * initialize lastvalid
823 */
824 lastvalid = 1;
825 lastenc = data->uudet;
826 lastpart = result->partno = 1;
827 _FP_strncpy (uucheck_lastname, result->subfname, 256);
828 }
829 else
830 result->partno = 1;
831 }
832 else if (result->subfname == NULL && data->uudet && data->mimeid) {
833 /*
834 * if it's got a file name, use it. Else use the mime-id for identifying
835 * this part, and hope there's no other files encoded in the same message
836 * under the same id.
837 */
838 if (result->filename)
839 result->subfname = _FP_strdup (result->filename);
840 else
841 result->subfname = _FP_strdup (result->mimeid);
842 }
843 else if (result->subfname == NULL && data->uudet) {
844 /*
845 * ff we have lastvalid, use it. Make an exception for
846 * Base64-encoded files.
847 */
848 if (data->uudet == B64ENCODED) {
849 /*
850 * Assume it's the first part. I wonder why it's got no part number?
851 */
852 if (result->filename != NULL && *result->filename)
853 result->subfname = _FP_strdup (result->filename);
854 else { /* if not, escape to UNKNOWN. We need to fill subfname */
855 sprintf (temp, "%s.%03d", nofname, ++nofnum);
856 result->subfname = _FP_strdup (temp);
857 }
858 if (result->subfname == NULL) {
859 UUMessage (uucheck_id, __LINE__, UUMSG_ERROR,
860 uustring (S_OUT_OF_MEMORY),
861 (result->filename)?
862 (strlen(result->filename)+1):(strlen(temp)+1));
863 *ret = UURET_NOMEM;
864 UUkillfile (result);
865 return NULL;
866 }
867 lastvalid = 0;
868 }
869 else if (lastvalid && data->uudet == lastenc && result->partno == -1) {
870 result->subfname = _FP_strdup (uucheck_lastname);
871 result->partno = ++lastpart;
872
873 /*
874 * if it's the last part, invalidate lastvalid
875 */
876 if (data->end || (data->partno && data->partno == data->maxpno))
877 lastvalid = 0;
878 }
879 else if (data->partno != -1 && result->filename) {
880 result->subfname = _FP_strdup (result->filename);
881 }
882 else {
883 /*
884 * it's got no info, it's got no begin, and we don't know anything
885 * about this part. Let's forget all about it.
886 */
887 *ret = UURET_NODATA;
888 UUkillfile (result);
889 return NULL;
890 }
891 }
892 else if (result->subfname == NULL && result->partno == -1) {
893 /*
894 * This, too, is a part without any useful information that we
895 * should forget about.
896 */
897 *ret = UURET_NODATA;
898 UUkillfile (result);
899 return NULL;
900 }
901 else if (result->subfname == NULL) {
902 /*
903 * This is a part without useful subject name, a valid part number
904 * but no encoded data. It *could* be the zeroeth part of something,
905 * but we don't care here. Just forget it.
906 */
907 *ret = UURET_NODATA;
908 UUkillfile (result);
909 return NULL;
910 }
911
912 /*
913 * now, handle some cases where we have a useful subject but no
914 * useful part number
915 */
916
917 if (result->partno == -1 && data->begin) {
918 /*
919 * hmm, this is reason enough to initialize lastvalid, at least
920 * if we have no end
921 */
922 if (!data->end) {
923 _FP_strncpy (uucheck_lastname, result->subfname, 256);
924 result->partno = lastpart = 1;
925 lastenc = data->uudet;
926 lastvalid = 1;
927 }
928 else
929 result->partno = 1;
930 }
931 else if (result->partno == -1 && data->uudet) {
932 if (lastvalid && _FP_stricmp (uucheck_lastname, result->subfname) == 0) {
933 /*
934 * if the subject filename is the same as last time, use part no
935 * of lastvalid. If at end, invalidate lastvalid
936 */
937 result->partno = ++lastpart;
938
939 if (data->end)
940 lastvalid = 0;
941 }
942 else {
943 /*
944 * data but no part no. It's something UUInsertPartToList() should
945 * handle
946 */
947 goto skipcheck;
948 }
949 }
950 else if (result->partno == -1) {
951 /*
952 * it's got no data, so why should we need this one anyway?
953 */
954 *ret = UURET_NODATA;
955 UUkillfile (result);
956 return NULL;
957 }
958
959 /*
960 * at this point, the part should have a valid subfname and a valid
961 * part number. If it doesn't, then fail.
962 */
963 if (result->subfname == NULL || result->partno == -1) {
964 *ret = UURET_NODATA;
965 UUkillfile (result);
966 return NULL;
967 }
968
969 skipcheck:
970
971 if (result->filename) {
972 if (*(ptr = _FP_cutdir (result->filename))) {
973 p2 = _FP_strdup (ptr);
974 _FP_free (result->filename);
975 result->filename = p2;
976 }
977 }
978
979 result->data = data;
980 result->NEXT = NULL;
981
982 *ret = UURET_OK;
983
984 return result;
985 }
986
987 /*
988 * Insert one part of a file into the global list
989 **/
990
991 int
992 UUInsertPartToList (uufile *data)
993 {
994 uulist *iter = UUGlobalFileList, *unew;
995 uufile *fiter, *last;
996
997 /*
998 * Part belongs together, if
999 * (1) The MIME-IDs match, or
1000 * (2) The file name received from the subject lines match, and
1001 * (a) Not both parts have a begin line
1002 * (b) Not both parts have an end line
1003 * (c) Both parts don't have different MIME-IDs
1004 * (d) Both parts don't encode different files
1005 * (e) The other part wants to stay alone (FL_SINGLE)
1006 */
1007
1008 /*
1009 * check if this part wants to be left alone. If so, don't bother
1010 * to do all the checks
1011 */
1012
1013 while (iter) {
1014 if (data->data->flags & FL_SINGLE) {
1015 /* this space intentionally left blank */
1016 }
1017 else if ((data->mimeid && iter->mimeid &&
1018 strcmp (data->mimeid, iter->mimeid) == 0) ||
1019 (_FP_stricmp (data->subfname, iter->subfname) == 0 &&
1020 !(iter->begin && data->data->begin) &&
1021 !(iter->end && data->data->end) &&
1022 !(data->mimeid && iter->mimeid &&
1023 strcmp (data->mimeid, iter->mimeid) != 0) &&
1024 !(data->filename && iter->filename &&
1025 strcmp (data->filename, iter->filename) != 0) &&
1026 !(iter->flags & FL_SINGLE))) {
1027
1028 /*
1029 * Don't insert a part that is already there.
1030 *
1031 * Also don't add a part beyond the "end" marker (unless we
1032 * have a mimeid, which screws up the marker).
1033 */
1034
1035 for (fiter=iter->thisfile; fiter; fiter=fiter->NEXT) {
1036 if (data->partno == fiter->partno)
1037 goto goahead;
1038 if (!iter->mimeid) {
1039 if (data->partno > fiter->partno && fiter->data->end) {
1040 goto goahead;
1041 }
1042 }
1043 }
1044
1045 if (iter->filename == NULL && data->filename != NULL) {
1046 if ((iter->filename = _FP_strdup (data->filename)) == NULL)
1047 return UURET_NOMEM;
1048 }
1049
1050 /*
1051 * special case when we might have tagged a part as Base64 when the
1052 * file was really XX
1053 */
1054
1055 if (data->data->uudet == B64ENCODED &&
1056 iter->uudet == XX_ENCODED && iter->begin) {
1057 data->data->uudet = XX_ENCODED;
1058 }
1059 else if (data->data->uudet == XX_ENCODED && data->data->begin &&
1060 iter->uudet == B64ENCODED) {
1061 iter->uudet = XX_ENCODED;
1062
1063 fiter = iter->thisfile;
1064 while (fiter) {
1065 fiter->data->uudet = XX_ENCODED;
1066 fiter = fiter->NEXT;
1067 }
1068 }
1069
1070 /*
1071 * If this is from a Message/Partial, we believe only the
1072 * iter->uudet from the first part
1073 */
1074 if (data->data->flags & FL_PARTIAL) {
1075 if (data->partno == 1) {
1076 iter->uudet = data->data->uudet;
1077 iter->flags = data->data->flags;
1078 }
1079 }
1080 else {
1081 if (data->data->uudet) iter->uudet = data->data->uudet;
1082 if (data->data->flags) iter->flags = data->data->flags;
1083 }
1084
1085 if (iter->mode == 0 && data->data->mode != 0)
1086 iter->mode = data->data->mode;
1087 if (data->data->begin) iter->begin = (data->partno)?data->partno:1;
1088 if (data->data->end) iter->end = (data->partno)?data->partno:1;
1089
1090 if (data->mimetype) {
1091 _FP_free (iter->mimetype);
1092 iter->mimetype = _FP_strdup (data->mimetype);
1093 }
1094
1095 /*
1096 * insert part at the beginning
1097 */
1098
1099 if (data->partno != -1 && data->partno < iter->thisfile->partno) {
1100 iter->state = UUFILE_READ;
1101 data->NEXT = iter->thisfile;
1102 iter->thisfile = data;
1103 return UURET_OK;
1104 }
1105
1106 /*
1107 * insert part somewhere else
1108 */
1109
1110 iter->state = UUFILE_READ; /* prepare for re-checking */
1111 fiter = iter->thisfile;
1112 last = NULL;
1113
1114 while (fiter) {
1115 /*
1116 * if we find the same part no again, check which one looks better
1117 */
1118 if (data->partno == fiter->partno) {
1119 if (fiter->data->subject == NULL)
1120 return UURET_NODATA;
1121 else if (_FP_stristr (fiter->data->subject, "repost") != NULL &&
1122 _FP_stristr (data->data->subject, "repost") == NULL)
1123 return UURET_NODATA;
1124 else if (fiter->data->uudet && !data->data->uudet)
1125 return UURET_NODATA;
1126 else {
1127 /*
1128 * replace
1129 */
1130 data->NEXT = fiter->NEXT;
1131 fiter->NEXT = NULL;
1132 UUkillfile (fiter);
1133
1134 if (last == NULL)
1135 iter->thisfile = data;
1136 else
1137 last->NEXT = data;
1138
1139 return UURET_OK;
1140 }
1141 }
1142
1143 /*
1144 * if at the end of the part list, add it
1145 */
1146
1147 if (fiter->NEXT == NULL ||
1148 (data->partno != -1 && data->partno < fiter->NEXT->partno)) {
1149 data->NEXT = fiter->NEXT;
1150 fiter->NEXT = data;
1151
1152 if (data->partno == -1)
1153 data->partno = fiter->partno + 1;
1154
1155 return UURET_OK;
1156 }
1157 last = fiter;
1158 fiter = fiter->NEXT;
1159 }
1160
1161 return UURET_OK; /* Shouldn't get here */
1162 }
1163 goahead:
1164 /*
1165 * we need iter below
1166 */
1167 if (iter->NEXT == NULL)
1168 break;
1169
1170 iter = iter->NEXT;
1171 }
1172 /*
1173 * handle new entry
1174 */
1175
1176 if (data->partno == -1) {
1177 /*
1178 * if it's got no part no, and it's MIME mail, then assume this is
1179 * part no. 1. If it's not MIME, then we can't handle it; if it
1180 * had a 'begin', it'd have got a part number assigned by
1181 * UUPreProcessPart().
1182 */
1183 if (data->data->uudet == B64ENCODED || data->data->uudet == BH_ENCODED)
1184 data->partno = 1;
1185 else
1186 return UURET_NODATA;
1187 }
1188
1189 if ((unew = (uulist *) malloc (sizeof (uulist))) == NULL) {
1190 return UURET_NOMEM;
1191 }
1192
1193 {
1194 static uulist uulist_new;
1195 *unew = uulist_new; /* zero-initialise the structure */
1196 }
1197
1198 if ((unew->subfname = _FP_strdup (data->subfname)) == NULL) {
1199 _FP_free (unew);
1200 return UURET_NOMEM;
1201 }
1202
1203 if (data->filename != NULL) {
1204 if ((unew->filename = _FP_strdup (data->filename)) == NULL) {
1205 _FP_free (unew->subfname);
1206 _FP_free (unew);
1207 return UURET_NOMEM;
1208 }
1209 }
1210 else
1211 unew->filename = NULL;
1212
1213 if (data->mimeid != NULL) {
1214 if ((unew->mimeid = _FP_strdup (data->mimeid)) == NULL) {
1215 _FP_free (unew->subfname);
1216 _FP_free (unew->filename);
1217 _FP_free (unew);
1218 return UURET_NOMEM;
1219 }
1220 }
1221 else
1222 unew->mimeid = NULL;
1223
1224 if (data->mimetype != NULL) {
1225 if ((unew->mimetype = _FP_strdup (data->mimetype)) == NULL) {
1226 _FP_free (unew->mimeid);
1227 _FP_free (unew->subfname);
1228 _FP_free (unew->filename);
1229 _FP_free (unew);
1230 return UURET_NOMEM;
1231 }
1232 }
1233 else
1234 unew->mimetype = NULL;
1235
1236 unew->state = UUFILE_READ;
1237 unew->thisfile = data;
1238 unew->mode = data->data->mode;
1239 unew->uudet = data->data->uudet;
1240 unew->flags = data->data->flags;
1241 unew->begin = data->data->begin ? (data->partno ? data->partno : 1) : 0;
1242 unew->end = data->data->end ? (data->partno ? data->partno : 1) : 0;
1243
1244 if (iter == NULL)
1245 UUGlobalFileList = unew;
1246 else
1247 iter->NEXT = unew;
1248
1249 return UURET_OK;
1250 }
1251
1252 /*
1253 * At this point, all files are read in and stored in the
1254 * "UUGlobalFileList". Do some checking. All parts there?
1255 **/
1256
1257 uulist *
1258 UUCheckGlobalList (void)
1259 {
1260 int misparts[MAXPLIST], haveparts[MAXPLIST];
1261 int miscount, havecount, count, flag, part;
1262 uulist *liter=UUGlobalFileList, *prev;
1263 uufile *fiter;
1264 long thesize;
1265
1266 while (liter) {
1267 miscount = 0;
1268 thesize = 0;
1269
1270 if (liter->state & UUFILE_OK) {
1271 liter = liter->NEXT;
1272 continue;
1273 }
1274 else if ((liter->uudet == QP_ENCODED ||
1275 liter->uudet == PT_ENCODED) &&
1276 (liter->flags & FL_SINGLE)) {
1277 if ((liter->flags&FL_PROPER)==0)
1278 liter->size = -1;
1279 else
1280 liter->size = liter->thisfile->data->length;
1281
1282 liter->state = UUFILE_OK;
1283 continue;
1284 }
1285 else if ((fiter = liter->thisfile) == NULL) {
1286 liter->state = UUFILE_NODATA;
1287 liter = liter->NEXT;
1288 continue;
1289 }
1290
1291 /*
1292 * Re-Check this file
1293 */
1294
1295 flag = 0;
1296 miscount = 0;
1297 havecount = 0;
1298 thesize = 0;
1299 liter->state = UUFILE_READ;
1300
1301 /*
1302 * search encoded data
1303 */
1304
1305 while (fiter && !fiter->data->uudet) {
1306 if (havecount<MAXPLIST) {
1307 haveparts[havecount++] = fiter->partno;
1308 }
1309 fiter = fiter->NEXT;
1310 }
1311
1312 if (fiter == NULL) {
1313 liter->state = UUFILE_NODATA;
1314 liter = liter->NEXT;
1315 continue;
1316 }
1317
1318 if (havecount<MAXPLIST) {
1319 haveparts[havecount++] = fiter->partno;
1320 }
1321
1322 if ((part = fiter->partno) > 1) {
1323 if (!fiter->data->begin) {
1324 for (count=1; count < part && miscount < MAXPLIST; count++)
1325 misparts[miscount++] = count;
1326 }
1327 }
1328
1329 /*
1330 * don't care if so many parts are missing
1331 */
1332
1333 if (miscount >= MAXPLIST) {
1334 liter->state = UUFILE_MISPART;
1335 liter = liter->NEXT;
1336 continue;
1337 }
1338
1339 if (liter->uudet == B64ENCODED ||
1340 liter->uudet == QP_ENCODED ||
1341 liter->uudet == PT_ENCODED)
1342 flag |= 3; /* Don't need begin or end with Base64 or plain text*/
1343
1344 if (fiter->data->begin) flag |= 1;
1345 if (fiter->data->end) flag |= 2;
1346 if (fiter->data->uudet) flag |= 4;
1347
1348 /*
1349 * guess size of part
1350 */
1351
1352 switch (fiter->data->uudet) {
1353 case UU_ENCODED:
1354 case XX_ENCODED:
1355 thesize += 3*fiter->data->length/4;
1356 thesize -= 3*fiter->data->length/124; /* substract 2 of 62 chars */
1357 break;
1358 case B64ENCODED:
1359 thesize += 3*fiter->data->length/4;
1360 thesize -= fiter->data->length/52; /* substract 2 of 78 chars */
1361 break;
1362 case QP_ENCODED:
1363 case PT_ENCODED:
1364 thesize += fiter->data->length;
1365 break;
1366 }
1367
1368 fiter = fiter->NEXT;
1369
1370 while (fiter != NULL) {
1371 for (count=part+1; count<fiter->partno && miscount<MAXPLIST; count++)
1372 misparts[miscount++] = count;
1373
1374 part = fiter->partno;
1375
1376 if (havecount<MAXPLIST)
1377 haveparts[havecount++]=part;
1378
1379 if (fiter->data->begin) flag |= 1;
1380 if (fiter->data->end) flag |= 2;
1381 if (fiter->data->uudet) flag |= 4;
1382
1383 switch (fiter->data->uudet) {
1384 case UU_ENCODED:
1385 case XX_ENCODED:
1386 thesize += 3*fiter->data->length/4;
1387 thesize -= 3*fiter->data->length/124; /* substract 2 of 62 chars */
1388 break;
1389 case B64ENCODED:
1390 thesize += 3*fiter->data->length/4;
1391 thesize -= fiter->data->length/52; /* substract 2 of 78 chars */
1392 break;
1393 case QP_ENCODED:
1394 case PT_ENCODED:
1395 thesize += fiter->data->length;
1396 break;
1397 }
1398
1399 if (fiter->data->end)
1400 break;
1401
1402 fiter = fiter->NEXT;
1403 }
1404
1405 /*
1406 * if in fast mode, we don't notice an 'end'. So if its uu or xx
1407 * encoded, there's a begin line and encoded data, assume it's
1408 * there.
1409 */
1410
1411 if (uu_fast_scanning && (flag & 0x01) && (flag & 0x04) &&
1412 (liter->uudet == UU_ENCODED || liter->uudet == XX_ENCODED))
1413 flag |= 2;
1414
1415 /*
1416 * Set the parts we have and/or missing
1417 */
1418
1419 _FP_free (liter->haveparts);
1420 _FP_free (liter->misparts);
1421
1422 liter->haveparts = NULL;
1423 liter->misparts = NULL;
1424
1425 if (havecount) {
1426 if ((liter->haveparts=(int*)malloc((havecount+1)*sizeof(int)))!=NULL) {
1427 memcpy (liter->haveparts, haveparts, havecount*sizeof(int));
1428 liter->haveparts[havecount] = 0;
1429 }
1430 }
1431
1432 if (miscount) {
1433 if ((liter->misparts=(int*)malloc((miscount+1)*sizeof(int)))!=NULL) {
1434 memcpy (liter->misparts, misparts, miscount*sizeof(int));
1435 liter->misparts[miscount] = 0;
1436 }
1437 liter->state |= UUFILE_MISPART;
1438 }
1439
1440 /*
1441 * Finalize checking
1442 */
1443
1444 if ((flag & 4) == 0) liter->state |= UUFILE_NODATA;
1445 if ((flag & 1) == 0) liter->state |= UUFILE_NOBEGIN;
1446 if ((flag & 2) == 0) liter->state |= UUFILE_NOEND;
1447
1448 if ((flag & 7) == 7 && miscount==0) {
1449 liter->state = UUFILE_OK;
1450 }
1451
1452 if ((uu_fast_scanning && (liter->flags&FL_PROPER)==0) || thesize<=0)
1453 liter->size = -1;
1454 else
1455 liter->size = thesize;
1456
1457 if (liter->state==UUFILE_OK &&
1458 (liter->filename==NULL || liter->filename[0]=='\0')) {
1459 /*
1460 * Emergency backup if the file does not have a filename
1461 */
1462 _FP_free (liter->filename);
1463 if (liter->subfname && liter->subfname[0] &&
1464 _FP_strpbrk (liter->subfname, "()[];: ") == NULL)
1465 liter->filename = _FP_strdup (liter->subfname);
1466 else {
1467 sprintf (uucheck_tempname, "%s.%03d", nofname, ++nofnum);
1468 liter->filename = _FP_strdup (uucheck_tempname);
1469 }
1470 }
1471 liter = liter->NEXT;
1472 }
1473
1474 /*
1475 * Sets back (PREV) links
1476 */
1477
1478 liter = UUGlobalFileList;
1479 prev = NULL;
1480
1481 while (liter) {
1482 liter->PREV = prev;
1483 prev = liter;
1484 liter = liter->NEXT;
1485 }
1486
1487 return UUGlobalFileList;
1488 }
1489