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