Repository navigation
Expand file tree
/
Copy pathstruct.c
More file actions
2868 lines (2464 loc) · 79.9 KB
/
Copy pathstruct.c
File metadata and controls
2868 lines (2464 loc) · 79.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** Copyright (C) 1994, 1995 Enterprise Integration Technologies Corp.
** VeriFone Inc./Hewlett-Packard. All Rights Reserved.
** Kevin Hughes, kev@kevcom.com 3/11/94
** Kent Landfield, kent@landfield.com 4/6/97
** Hypermail Project 1998-2023
**
** This program and library is free software; you can redistribute it and/or
** modify it under the terms of the GNU (Library) General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU (Library) General Public License for more details.
**
** You should have received a copy of the GNU (Library) General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
/* Define the following define to enable better date debugging */
/*#define PH_DATE_DEBUG */
int rbs = 0;
int rbs_bigtime = 0;
#include "hypermail.h"
#include "dmatch.h"
#include "setup.h"
#include "struct.h"
#include "parse.h"
#include "getname.h"
#define HAVE_PCRE2
#ifdef HAVE_PCRE2
#ifdef __LCC__
#include "../lcc/pcre2.h"
#else
#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>
#endif
#endif
struct body *hashnumlookup(int, struct emailinfo **);
typedef struct message_node_dump_stack {
int parent_index;
int sibling_index;
} message_node_dump_stack;
/*
** Ladies and germs - the hash function.
*/
unsigned hash(char *s)
{
unsigned hashval;
/* We MUST deal with NULL pointers passed to this */
if (!s)
return HASHSIZE - 1; /* (Daniel) I think a high value is fun ;-) */
for (hashval = 0; *s; s++)
hashval = *s + 31 * hashval;
return (hashval % HASHSIZE);
}
void reinit_structs()
{
int i;
for (i = 0; i < HASHSIZE; ++i) {
if (etable[i]) {
free(etable[i]);
etable[i] = NULL;
}
}
}
void fill_email_dates(struct emailinfo *e, char *date, char *fromdate, char *isodate, char *isofromdate)
{
bool fromdate_valid = 1, date_valid = 1;
/* fromdate may be empty (zero length string rather than NULL)
* date will always have something in it - either a date or
* the NODATE string "(no date)"
*/
if (isodate != NULL && isofromdate != NULL) {
e->date = iso_to_secs(isodate);
e->fromdate = iso_to_secs(isofromdate);
e->fromdatestr = strsav(fromdate);
e->datestr = strsav(date);
}
else {
e->date = e->fromdate = -1;
if (date == NULL)
date = NODATE;
if (!strcmp(date, NODATE)) {
date_valid = 0;
}
else {
if ((e->date = convtoyearsecs(date)) == -1)
date_valid = 0;
}
if (!*fromdate) {
fromdate_valid = 0;
}
else {
if ((e->fromdate = convtoyearsecs(fromdate)) == -1)
fromdate_valid = 0;
}
if (fromdate_valid && !date_valid) {
#ifdef PH_DATE_DEBUG
fprintf(stderr, "%d: %s: using fromdate '%s' for both (date '%s')\n", num, msgid, fromdate, date);
#endif
e->fromdatestr = strsav(fromdate);
e->datestr = strsav(fromdate);
e->date = e->fromdate;
}
else if (!fromdate_valid && date_valid) {
#ifdef PH_DATE_DEBUG
fprintf(stderr, "%d: %s: using date '%s' for both (fromdate '%s')\n", num, msgid, date, fromdate);
#endif
e->fromdatestr = strsav(date);
e->datestr = strsav(date);
e->fromdate = e->date;
}
else if (!fromdate_valid && !date_valid) {
#ifdef PH_DATE_DEBUG
fprintf(stderr, "%d: %s: fromdate '%s' and date '%s' both bad\n", num, msgid, fromdate, date);
#endif
e->fromdatestr = strsav(fromdate);
e->datestr = strsav(date);
}
else {
e->fromdatestr = strsav(fromdate);
e->datestr = strsav(date);
#ifdef PH_DATE_DEBUG
if (e->date > e->fromdate) {
rbs++;
if ((e->date - e->fromdate) > 4200) { /* if more than 1:10 off */
char fromdate_parsed[26];
char date_parsed[26];
strcpy(fromdate_parsed, ctime(&(e->fromdate)));
strcpy(date_parsed, ctime(&(e->date)));
date_parsed[24] = '\0';
fromdate_parsed[24] = '\0';
rbs_bigtime++;
fprintf(stderr, "%d [%d:%d]: %s: received before sent\n %-38.38s %-30.38s\n %-38.38s %-38.38s\n", num, rbs, rbs_bigtime, msgid, fromdate, date, fromdate_parsed, date_parsed);
}
}
#endif
}
}
}
/*
** The structure most of everything else depends on.
** Hashes a message - header info, pointer to a list of body lines -
** by number, message ID, date, and subject, so an article can be
** handily looked up and retrieved using any of these criteria.
*/
struct emailinfo *addhash(int num, char *date, char *name, char *email, char *msgid, char *subject, char *inreply, char *fromdate, char *charset, char *isodate, char *isofromdate, struct body *sp)
{
struct emailinfo *e;
struct hashemail *h;
unsigned hashval;
char numstr[NUMSTRLEN];
bool msgid_dup = 0;
bool msgid_missing = 0;
static int freedummy = 0;
char *newmsgid;
if (!msgid) {
/* SEVERE ERROR, all mails MUST have a Message-ID, ignore it! */
if (set_require_msgids) {
if (set_warn_suppressions)
printf("Message-ID is missing, ignoring message with subject '%s'.\n", subject ? subject : "<unknown>");
return NULL;
}
else {
msgid_missing = 1;
}
}
if (!msgid_missing) {
h = (struct hashemail *)etable[hash(msgid)];
while (h) {
if (!strcmp(h->data->msgid, msgid)) {
msgid_dup = 1;
break;
}
h = h->next;
}
}
if (msgid_dup && set_discard_dup_msgids) {
if (set_showprogress)
printf("%d: Message-ID collision, '%s' already present - discarding message.\n", num, msgid);
return NULL;
}
if (msgid_dup || msgid_missing) {
/*
* This is broken. It needs to be corrected so that
* the original message id is a component of the
* generated message-id instead of a total unrelated
* dummy msg-id. Good enough for now though...
*/
newmsgid = emalloc(MSGDSTRLEN);
do {
msgid_dup = 0;
sprintf(newmsgid, "%d.%4.4d@hypermail.dummy", time(NULL), freedummy);
h = (struct hashemail *)etable[hash(newmsgid)];
while (h) {
if (!strcmp(h->data->msgid, newmsgid)) {
msgid_dup = 1;
break;
}
h = h->next;
}
freedummy++;
} while (msgid_dup && (freedummy < 1000));
if (!msgid_dup) {
if (set_showprogress) {
if (msgid_missing)
printf("\n%d Message-ID missing, using %s instead.\n", num, newmsgid);
else
printf("\n%d Message-ID collision, '%s' already present - using %s instead.\n", num, msgid, newmsgid);
}
msgid = newmsgid;
}
else {
if (set_showprogress) {
if (msgid_missing)
printf("\n%d Message-ID missing, failed to find free id - dropping message.\n", num);
else
printf("\n%d Message-ID collision, '%s' already present - failed to find free id - dropping message.\n", num, msgid);
}
return NULL;
}
}
e = (struct emailinfo *)emalloc(sizeof(struct emailinfo));
#ifdef FASTREPLYCODE
e->replylist = NULL;
e->isreply = 0;
#endif
e->msgnum = num;
if (num > max_msgnum)
max_msgnum = num;
e->emailaddr = strsav(email);
if ((name == NULL) || (*name == '\0'))
e->name = strsav(email);
else
e->name = strsav(name);
fill_email_dates(e, date, fromdate, isodate, isofromdate);
e->subdir = msg_subdir(e->msgnum, set_use_sender_date ? e->date
: e->fromdate);
if (e->subdir && set_increment != -1) {
if (!e->subdir->first_email)
e->subdir->first_email = e;
e->subdir->last_email = e;
++e->subdir->count;
}
e->msgid = strsav(msgid);
e->subject = strsav(subject);
e->unre_subject = unre(subject);
e->inreplyto = strsav(inreply);
e->charset = strsav(charset);
e->flags = 0;
e->is_deleted = 0;
e->deletion_completed = -1;
e->exp_time = -1;
e->bodylist = sp;
e->initial_next_in_thread = -1;
/* Added by Daniel 1999-03-19, we need this hash later to find the mail
we replied to */
h = (struct hashemail *)emalloc(sizeof(struct hashemail));
hashval = hash(inreply);
h->next = etable[hashval];
h->data = e;
etable[hashval] = h;
h = (struct hashemail *)emalloc(sizeof(struct hashemail));
hashval = hash(date);
h->next = etable[hashval];
h->data = e;
etable[hashval] = h;
h = (struct hashemail *)emalloc(sizeof(struct hashemail));
#if 0
printf("ADD msgid %s to HASH!\n", msgid);
#endif
hashval = hash(msgid);
h->next = etable[hashval];
h->data = e;
etable[hashval] = h;
h = (struct hashemail *)emalloc(sizeof(struct hashemail));
hashval = hash(subject);
h->next = etable[hashval];
h->data = e;
etable[hashval] = h;
h = (struct hashemail *)emalloc(sizeof(struct hashemail));
sprintf(numstr, "%d", num);
hashval = hash(numstr);
h->next = etable[hashval];
h->data = e;
etable[hashval] = h;
return e; /* the actual mail struct pointer */
}
int insert_in_lists(struct emailinfo *emp, const bool * require_filter, int rlen)
{
int i;
if (set_delete_msgnum) {
char num_str[32];
sprintf(num_str, "%d", emp->msgnum);
if (inlist(set_delete_msgnum, num_str)) {
#if 0
if (!emp->is_deleted) {
if (emp->subdir)
--emp->subdir->count;
}
#endif
emp->is_deleted = FILTERED_DELETE;
}
}
for (i = 0; i < rlen; ++i) {
if (!require_filter[i]) {
emp->is_deleted = FILTERED_REQUIRED;
}
}
if (emp->is_deleted) {
struct hashemail *h;
h = (struct hashemail *)malloc(sizeof(struct hashemail));
h->data = emp;
h->next = deletedlist;
deletedlist = h;
if (set_warn_suppressions && require_filter) {
const char *option = "??";
switch (emp->is_deleted) {
case FILTERED_DELETE:
option = "deleted or delete_msgnum";
break;
case FILTERED_EXPIRE:
option = "expires";
break;
case FILTERED_OUT:
option = "filter_out or filter_out_full_body";
break;
case FILTERED_REQUIRED:
option = "filter_require or filter_require_full_body";
break;
case FILTERED_OLD:
option = "delete_older";
break;
case FILTERED_NEW:
option = "delete_newer";
break;
}
printf("message %d deleted under option %s. msgid: %s\n", emp->msgnum + 1, option, emp->msgid);
}
}
else {
authorlist = addheader(authorlist, emp, 1, 0);
subjectlist = addheader(subjectlist, emp, 0, 0);
}
datelist = addheader(datelist, emp, 2, 0);
return !emp->is_deleted;
}
/*
* Given an "in-reply-to:" field and a message number, this function
* retrieves information about the message that this message is a
* reply to.
* If all else fails but a reply is
* found by comparing subjects, issubjmatch is set to 1.
*
* Daniel 1999-03-19. Changed return type and parameters.
*/
#ifdef USE_OBSOLETE_HASHREPLYLOOKUP
struct emailinfo *hashreplylookup(int msgnum, char *inreply, int *issubjmatch)
{
struct hashemail *ep;
#if DEBUG_THREAD
fprintf(stderr, "hashreplylookup(%d, '%s'...)\n", msgnum, inreply);
#endif
*issubjmatch = 0;
ep = etable[hash(inreply)];
while (ep) {
if ((msgnum != ep->data->msgnum) && (strcmp(inreply, ep->data->msgid) == 0)) {
#if DEBUG_THREAD
fprintf(stderr, "match on msgid [%d]\n", ep->data->msgnum);
#endif
return ep->data;
}
ep = ep->next;
}
ep = etable[hash(inreply)];
while (ep != NULL) {
if ((msgnum != ep->data->msgnum) && (strstr(inreply, ep->data->datestr))) {
#if DEBUG_THREAD
fprintf(stderr, "match on date [%d]\n", ep->data->msgnum);
#endif
return ep->data;
}
ep = ep->next;
}
ep = etable[hash(inreply)];
while (ep != NULL) {
if ((msgnum != ep->data->msgnum) && (strcmp(inreply, ep->data->subject) == 0)) {
*issubjmatch = 1;
#if DEBUG_THREAD
fprintf(stderr, "match on subject [%d]\n", ep->data->msgnum);
#endif
return ep->data;
}
ep = ep->next;
}
return NULL;
}
#endif
/*
* Given an "message-id:" field, this tries to retrieve information
* about an article that has replied. If all else fails but a reply is
* found by comparing subjects, issubjmatch is set to 1.
*
* Daniel 1999-03-24. New.
*/
struct emailinfo *hashmsgidlookup(char *msgid, int *issubjmatch)
{
struct hashemail *ep;
*issubjmatch = 0;
ep = etable[hash(msgid)];
while (ep) {
if (strcmp(msgid, ep->data->inreplyto) == 0) {
return ep->data;
}
ep = ep->next;
}
return NULL;
}
/*
* Given an "in-reply-to:" field and a message number, this function
* retrieves information about the message that this message is a
* reply to.
* If all else fails but a reply is
* found by comparing subjects, maybereply is set to 1.
*/
struct emailinfo *hashreplylookup(int msgnum, char *inreply, char *subject, int *maybereply)
{
struct hashemail *ep;
#if DEBUG_THREAD > 1
fprintf(stderr, "hashreplynumlookup(%d, '%s'...)\n", msgnum, (inreply == NULL) ? "" : inreply);
#endif
*maybereply = 0;
if ((inreply != NULL) && *inreply) {
ep = etable[hash(inreply)];
while (ep) {
if (strcmp(inreply, ep->data->msgid) == 0) {
#if DEBUG_THREAD
fprintf(stderr, "match on msgid %4d %4d\n", msgnum, ep->data->msgnum);
#endif
return ep->data;
}
ep = ep->next;
}
ep = etable[hash(inreply)];
while (ep) {
if ((msgnum != ep->data->msgnum) && (strstr(inreply, ep->data->datestr))) {
#if DEBUG_THREAD
fprintf(stderr, "match on date %4d %4d\n", msgnum, ep->data->msgnum);
#endif
return ep->data;
}
ep = ep->next;
}
ep = etable[hash(inreply)];
while (ep != NULL) {
if ((msgnum != ep->data->msgnum) && (strcmp(inreply, ep->data->subject) == 0)) {
*maybereply = 1;
#if DEBUG_THREAD
fprintf(stderr, "match on subject %4d %4d\n", msgnum, ep->data->msgnum);
#endif
return ep->data;
}
ep = ep->next;
}
} /* end of matching on inreply */
/* No match so far. Now try matching on the subject, removing
* one instance of "re: " from the front of the subject each
* time round the loop.
*/
{
#define MAX_SUBJ_LEN 300
char *s, *saved_s;
struct emailinfo *lowest_so_far = NULL;
int match = 0;
size_t subj_len;
subj_len = strlen(subject) > MAX_SUBJ_LEN ? MAX_SUBJ_LEN : strlen(subject);
s = emalloc(subj_len + 1);
saved_s = strncpy(s, subject, subj_len);
s[subj_len] = '\0';
#ifdef NOTDEF
/* AUDIT biege: can we trigger a DoS here by using a very long "Subject"? */
s = emalloc(strlen(subject) + 1);
saved_s = strcpy(s, subject);
#endif
if (isre(s, NULL)) {
do {
char *new_s;
#if DEBUG_THREAD > 1
fprintf(stderr, "extra %s\n", s);
#endif
ep = etable[hash(s)];
while (ep != NULL) {
if ((strcasecmp(s, ep->data->subject) == 0) && (msgnum != ep->data->msgnum)) {
match = 1;
if (lowest_so_far == NULL || ep->data->msgnum < lowest_so_far->msgnum)
lowest_so_far = ep->data;
}
ep = ep->next;
}
new_s = oneunre(s);
free(s);
s = new_s;
} while (s != NULL);
} else {
free(saved_s);
}
if (match) {
*maybereply = 1;
if (lowest_so_far != NULL && lowest_so_far->msgnum < msgnum) {
#if DEBUG_THREAD
fprintf(stderr, "match on extra %4d %4d\n", msgnum, lowest_so_far->msgnum);
#endif
return lowest_so_far;
}
else {
#if DEBUG_THREAD
fprintf(stderr, "match on extra %4d %4d discarded - less than %d\n", msgnum, lowest_so_far ? lowest_so_far->msgnum : -1, msgnum);
#endif
return NULL;
}
}
}
#if DEBUG_THREAD
fprintf(stderr, "match NO MATCH %4d\n", msgnum);
#endif
return NULL;
}
/*
** Same as the above function, but only returns the article number.
*/
int hashreplynumlookup(int msgnum, char *inreply, char *subject, int *maybereply)
{
struct emailinfo *email = hashreplylookup(msgnum, inreply, subject,
maybereply);
return email != NULL ? email->msgnum : -1;
}
/*
* From an article's number, retrieve all information associated with
* that article.
*/
struct body *hashnumlookup(int num, struct emailinfo **emailp)
{
struct hashemail *ep;
struct body *lp_tmp;
char numstr[NUMSTRLEN];
sprintf(numstr, "%d", num);
for (ep = etable[hash(numstr)]; ep != NULL; ep = ep->next) {
if (ep->data && (num == ep->data->msgnum)) {
/* return a mere pointer to it! */
*emailp = ep->data;
if (!ep->data->bodylist)
ep->data->bodylist = addbody(NULL, &lp_tmp, "\n", 0);
return ep->data->bodylist;
}
}
return NULL;
}
/*
* returns info about the first message associated with the given msgid.
*/
struct emailinfo *hashlookupbymsgid(char *msgid)
{
struct hashemail *ep;
if (!msgid || !*msgid)
return NULL;
ep = etable[hash(msgid)];
while (ep) {
if (strcmp(msgid, ep->data->msgid) == 0) {
return ep->data;
}
ep = ep->next;
}
return NULL;
}
int insert_older_msgs(int num)
{
int i;
int num_added = 0;
for(i = set_startmsgnum; i < num; ++i) {
struct emailinfo *emp;
if (hashnumlookup(i, &emp)) {
authorlist = addheader(authorlist, emp, 1, 0);
subjectlist = addheader(subjectlist, emp, 0, 0);
datelist = addheader(datelist, emp, 2, 0);
++num_added;
}
}
return num_added;
}
/*
* Find the nearest non-deleted email to num by adding direction to num.
*/
struct emailinfo *neighborlookup(int num, int direction)
{
struct hashemail *ep;
char numstr[NUMSTRLEN];
num += direction;
while (num >= 0 && num <= max_msgnum) {
sprintf(numstr, "%d", num);
for (ep = etable[hash(numstr)]; ep != NULL; ep = ep->next)
if (ep->data && (num == ep->data->msgnum)) {
if (ep->data->is_deleted)
break;
/* return a mere pointer to it! */
return ep->data;
}
num += direction;
}
return NULL;
}
/*
** Push a boundary-id string to the boundary stack. The top
** of the list represents the current "active" boundary.
*/
struct boundary_stack *boundary_stack_push(struct boundary_stack *bnd, char *boundary_id)
{
struct boundary_stack *newnode = NULL;
if (boundary_id) {
newnode = (struct boundary_stack *)emalloc(sizeof(struct boundary_stack));
memset(newnode, 0, sizeof(struct boundary_stack));
newnode->boundary_id = strsav(boundary_id);
newnode->next = NULL;
newnode->prev = bnd;
if (bnd)
bnd->next = newnode;
bnd = newnode;
}
return bnd; /* the new "active" boundary */
}
/* Removes and frees a boundary from the stack.
** Returns NULL when the stack is empty.
*/
struct boundary_stack *boundary_stack_pop(struct boundary_stack *bnd)
{
struct boundary_stack *tmp_bnd;
if (!bnd)
return NULL;
tmp_bnd = bnd;
if (bnd->prev) {
/* go back to the previous */
bnd = bnd->prev;
bnd->next = NULL;
}
else {
/* this is the last node */
bnd = NULL;
}
/* JK: MAKE SURE WE ARE FREEING ALL MEMORY ASSOCIATED TO BOUNDARY */
/* free allocated memory */
free(tmp_bnd->boundary_id);
free(tmp_bnd);
return bnd; /* the new "active" boundary */
}
char *boundary_stack_peek_id(struct boundary_stack *bnd)
{
if (!bnd)
return NULL;
return bnd->boundary_id;
}
bool boundary_stack_top_has_id(struct boundary_stack *bnd, const char *boundary_id)
{
bool rv = FALSE;
char *stripped_boundary_id;
stripped_boundary_id = strip_boundary_id(boundary_id, 0);
if (stripped_boundary_id) {
if (bnd
&& !strcasecmp (bnd->boundary_id, stripped_boundary_id)) {
rv = TRUE;
}
free(stripped_boundary_id);
}
return rv;
}
/*
** Frees the memory allocated to a boundary structure/
** Returns the number of elements freed.
*/
int boundary_stack_free(struct boundary_stack *bnd)
{
struct boundary_stack *cursor = bnd;
struct boundary_stack *tmp;
int counter = 0;
if (bnd && bnd->next) {
fprintf(stderr, "boundary_stack_free(): Error: boundary has a non-empty next element.\n"
"BOUNDARY_ID : %s\n",bnd->boundary_id);
progerr("aborting");
}
cursor = bnd;
while (cursor) {
tmp = cursor->prev;
counter++;
if (cursor->boundary_id) {
#ifdef DEBUG_PARSE
fprintf (stderr, "boundary_stack_free(): freeing %s\n", cursor->boundary_id);
#endif
free(cursor->boundary_id);
}
free (cursor);
cursor = tmp;
}
return counter;
}
/*
** Returns a pointer to a boundary id if a given boundary stack has an
** element with boundary_id
** Returns NULL if not found
*/
char *boundary_stack_has_id(struct boundary_stack *boundaryp, const char *boundary_id)
{
struct boundary_stack *tempnode;
char *rv = NULL;
char *stripped_boundary_id;
if (!boundaryp || boundary_id == NULL || *boundary_id == '\0') {
return NULL;
}
stripped_boundary_id = strip_boundary_id(boundary_id, 0);
if (stripped_boundary_id != NULL && *stripped_boundary_id != '\0') {
int tbl;
for (tempnode = boundaryp; tempnode; tempnode = tempnode->prev) {
tbl = strlen(tempnode->boundary_id);
if (!strncmp(tempnode->boundary_id, stripped_boundary_id, tbl)
&& (stripped_boundary_id[tbl] == '\0'
|| !strcmp(&stripped_boundary_id[tbl], "--"))) {
rv = tempnode->boundary_id;
break;
}
}
}
free(stripped_boundary_id);
return rv;
}
/* If a boundary_id exists in the stack, but it's not at the top,
pop all the boundaries in the stack to make it the top.
Returns TRUE if boundary_id was found and is now on top
*/
bool boundary_stack_pop_to_id(struct boundary_stack **boundaryp, const char *boundary_id)
{
struct boundary_stack *s = *boundaryp;
char *stripped_boundary_id;
if (!boundary_stack_has_id(s, boundary_id)) {
return FALSE;
}
stripped_boundary_id = strip_boundary_id(boundary_id, 0);
while (s && strncasecmp(s->boundary_id, stripped_boundary_id, strlen(s->boundary_id))) {
s = boundary_stack_pop(s);
}
*boundaryp = s;
free(stripped_boundary_id);
return TRUE;
}
/*
** multipart_stack stores the mime types of a message as part of the message
** parsing process. It overloads the hm_stack generic stack.
*/
/* pushes a new type to the stack.
** returns the current value of the stack */
struct hm_stack *multipart_stack_push(struct hm_stack *s, char *type)
{
return hm_stack_push(s, (void *) strsav(type));
}
/* removes and frees the first element of the stack,
** returns the current stack or NULL if it's empty */
struct hm_stack *multipart_stack_pop(struct hm_stack *s)
{
void *p;
if (!s) {
return NULL;
}
s = hm_stack_pop(s, &p);
free((char *) p);
return s;
}
/* frees all the memory associated with multipart_stack s */
int multipart_stack_free(struct hm_stack *s)
{
int t = 0;
while (s) {
void *p;
s = hm_stack_pop(s, &p);
free( (char *) p);
t++;
}
#ifdef DEBUG_PARSE
fprintf (stderr, "multipart_stack_free: freed %d elements\n", t);
#endif
return t;
}
/*
** returns TRUE if multipart stack s contains the value of the mime_type
*/
bool multipart_stack_has_type(const struct hm_stack *s, char *mime_type)
{
const struct hm_stack *cursor;
bool rv = FALSE;
if (mime_type != NULL && *mime_type != '\0') {
for (cursor = s; cursor; cursor = cursor->prev) {
if (!strcasecmp ((char *) cursor->value, mime_type)) {
rv = TRUE;
break;
}
}
}
return rv;
}
/*
** returns TRUE if top of multipart stack s contains the value of the mime_type
*/
bool multipart_stack_top_has_type(const struct hm_stack *s, char *mime_type)
{
bool rv = FALSE;
if (mime_type != NULL && *mime_type != '\0' && s) {
char *top_type = (char *) s->value;
int tt_len;
int mime_type_len = strlen(mime_type);
if (top_type) {
tt_len = strlen(top_type);
}
if (top_type && tt_len == mime_type_len
&& !strcasecmp(top_type, mime_type)) {
rv = TRUE;
}
}
return rv;
}
/* returns the string stored in the head of a multipart stack */
const char *multipart_stack_top_type(const struct hm_stack *s)
{
return (s) ? (const char *) s->value : NULL;
}
/*
** Some basic generic stack functions to push / pull any kind of data
*/
struct hm_stack *hm_stack_push(struct hm_stack *s, void *value)
{
struct hm_stack *new_element;
new_element = (struct hm_stack *)emalloc(sizeof(struct hm_stack));
new_element->value = value;
if (s) {
new_element->prev = s;
} else {
new_element->prev = NULL;
}
return new_element;
}
struct hm_stack *hm_stack_pop(struct hm_stack *s, void **value)
{
struct hm_stack *tmp_element;
if (!s) {
return NULL;
}
*value = s->value;
tmp_element = s;
s = s->prev;
free(tmp_element);
return s;
}
static struct hm_stack *dump_index_stack_push(struct hm_stack *s, int parent_index, int sibling_index)
{
message_node_dump_stack *p;