summaryrefslogtreecommitdiff
path: root/ext/mailparse/rfc2045.c
blob: ea61cfdb2b72cbdf3c1c3ff18c45aea56a0f58c6 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
/* $Id$ */
/*
 ** Copyright 1998 - 1999 Double Precision, Inc.  See COPYING for
 ** distribution information.
 */

#include "php.h"
#include "php_mailparse.h"

#define	MAXLEVELS	20
#define	MAXPARTS	300

/*
	New RFC2045 structure.
 */

struct rfc2045 *rfc2045_alloc()
{
	struct rfc2045 *p=(struct rfc2045 *)emalloc(sizeof(struct rfc2045));

	/* Initialize everything to nulls, except for one thing */

	memset(p, 0, sizeof(*p));

	p->pindex=1;	/* Start with part #1 */

	/* Most of the time, we're about to read a header */
	p->workinheader=1;

	MAKE_STD_ZVAL(p->headerhash);
	array_init(p->headerhash);

	return (p);
}

const char *rfc2045_getattr(const struct rfc2045attr *p, const char *name)
{
	while (p)
	{
		if (p->name && strcmp(p->name, name) == 0)
			return (p->value);
		p=p->next;
	}
	return (0);
}

void rfc2045_setattr(struct rfc2045attr **p, const char *name, const char *val)
{
	char	*v;

	while (*p)
	{
		if (strcmp( (*p)->name, name) == 0)	break;
		p=&(*p)->next;
	}
	if (val == 0)
	{
		struct rfc2045attr *q= *p;

		if (q)
		{
			*p=q->next;
			if (q->name)	efree(q->name);
			if (q->value)	efree(q->value);
			efree(q);
		}
		return;
	}

	v = estrdup(val);

	if (!*p)
	{
		*p = (struct rfc2045attr *)emalloc(sizeof(**p));
		memset( (*p), 0, sizeof(**p));
		(*p)->name = estrdup(name);
	}
	if ( (*p)->value )
		efree ( (*p)->value );
	(*p)->value=v;
}

/* static const char cb_name[]="boundary"; */

/* #define	ContentBoundary(p)	(rfc2045_getattr( (p)->content_type_attr, cb_name)) */

#define	ContentBoundary(p)	( (p)->boundary )

/*
	Unallocate the RFC2045 structure.  Recursively unallocate
	all sub-structures.  Unallocate all associated buffers.
 */

static void rfc2045_freeattr(struct rfc2045attr *p)
{
	while (p)
	{
		struct rfc2045attr *q=p->next;

		if (p->name)	efree(p->name);
		if (p->value)	efree(p->value);
		efree(p);
		p=q;
	}
}

void rfc2045_free(struct rfc2045 *p)
{
	struct rfc2045 *q, *r;

	for (q=p->firstpart; q; )
	{
		r=q->next;
		rfc2045_free(q);
		q=r;
	}
	rfc2045_freeattr(p->content_type_attr);
	rfc2045_freeattr(p->content_disposition_attr);

	if (p->content_md5)	efree(p->content_md5);
	if (p->content_base)	efree(p->content_base);
	if (p->content_location)	efree(p->content_location);
	if (p->content_language)	efree(p->content_language);
	if (p->content_id)	efree(p->content_id);
	if (p->content_description)	efree(p->content_description);
	if (p->content_transfer_encoding) efree(p->content_transfer_encoding);
	if (p->boundary) efree(p->boundary);
	if (p->content_type)	efree(p->content_type);
	if (p->mime_version)	efree(p->mime_version);
	if (p->workbuf)		efree(p->workbuf);
	if (p->header)		efree(p->header);
	if (p->content_disposition) efree(p->content_disposition);
	if (p->rw_transfer_encoding) efree(p->rw_transfer_encoding);
	if (p->rfc2045acptr)
		efree(p->rfc2045acptr);
	zval_dtor(p->headerhash);
	efree(p->headerhash);
	efree(p);
}

/*
	Generic dynamic buffer append.
 */

void rfc2045_add_buf(
		char **bufptr,	/* Buffer */
		size_t *bufsize,	/* Buffer's maximum size */
		size_t *buflen,		/* Buffer's current size */

		const char *p, size_t len)	/* Append this data */
{
	if (len + *buflen > *bufsize)
	{
		size_t	newsize=len+*buflen+256;
		char	*p= *bufptr ? (char *)erealloc(*bufptr, newsize):
			(char *)emalloc(newsize);

		*bufptr=p;
		*bufsize=newsize;
	}

	memcpy(*bufptr + *buflen, p, len);
	*buflen += len;
}

/* Append to the work buffer */

void rfc2045_add_workbuf(struct rfc2045 *h, const char *p, size_t len)
{
	rfc2045_add_buf( &h->workbuf, &h->workbufsize, &h->workbuflen, p, len);
}

/* Append one character to the work buffer */

void rfc2045_add_workbufch(struct rfc2045 *h, int c)
{
	char cc= (char)c;

	rfc2045_add_workbuf(h, &cc, 1);
}

/*
	Generic function to duplicate contents of a string.
	The destination string may already be previously allocated,
	so unallocate it.
 */

static void set_string(char **p,
		const char *q)
{
	if (*p)	{
		efree(*p);
		*p=0;
	}
	if (!q)	return;

	*p = estrdup(q);
}

/* Update byte counts for this structure, and all the superstructures */

static void update_counts(struct rfc2045 *p, size_t newcnt, size_t newendcnt,
		unsigned nlines)
{
	while (p)
	{
		p->endpos = newcnt;
		p->endbody = newendcnt;
		p->nlines += nlines;
		if (!p->workinheader)
			p->nbodylines += nlines;
		p=p->parent;
	}
}

/*
	Main entry point for RFC2045 parsing.  External data is fed
	by repetitively calling rfc2045_parse().

	rfc2045_parse() breaks up input into lines, and calls doline()
	to process each line.
 */

static void doline(struct rfc2045 *);

void rfc2045_parse(struct rfc2045 *h, const char *buf, size_t s)
{
	size_t	l;

	while (s)
	{
		for (l=0; l<s; l++)
			if (buf[l] == '\n')	break;
		if (l < s && buf[l] == '\n')
		{
			++l;
			rfc2045_add_workbuf(h, buf, l);
			doline(h);
			h->workbuflen=0;
		}
		else
			rfc2045_add_workbuf(h, buf, l);
		buf += l;
		s -= l;
	}

	/*
	 ** Our buffer's getting pretty big.  Let's see if we can
	 ** partially handle it.
	 */

	if (h->workbuflen > 512)
	{
		struct	rfc2045 *p;
		int	l, i;

		for (p=h; p->lastpart && !p->lastpart->workclosed;
				p=p->lastpart)
			;

		/* If p->workinheader, we've got a mother of all headers
		 ** here.  Well, that's just too bad, we'll end up garbling
		 ** it.
		 */

		l=h->workbuflen;

		/* We do need to make sure that the final \r\n gets
		 ** stripped off, so don't gobble up everything if
		 ** the last character we see is a \r
		 */

		if (h->workbuf[l-1] == '\r')
			--l;

		/* If we'll be rewriting, make sure rwprep knows about
		 ** stuff that was skipped just now. */

		if (h->rfc2045acptr && !p->workinheader &&
				(!p->lastpart || !p->lastpart->workclosed))
			(*h->rfc2045acptr->section_contents)(h->rfc2045acptr, h->workbuf, l);

		update_counts(p, p->endpos+l, p->endpos+l, 0);
		p->informdata=1;
		for (i=0; l<h->workbuflen; l++)
			h->workbuf[i++]=h->workbuf[l];
		h->workbuflen=i;
	}
}

/*
	Append a new RFC2045 subpart.  Adds new RFC2045 structure to the
	end of the list of existing RFC2045 substructures.
 */

static struct rfc2045 *append_part_noinherit(struct rfc2045 *p, size_t startpos)
{
	struct rfc2045 *newp;

	newp=rfc2045_alloc();
	if (p->lastpart)
	{
		p->lastpart->next=newp;
		newp->pindex=p->lastpart->pindex+1;
	}
	else
	{
		p->firstpart=newp;
		newp->pindex=0;
	}
	p->lastpart=newp;
	newp->parent=p;

	/* Initialize source pointers */
	newp->startpos = newp->endpos = newp->startbody = newp->endbody = startpos;

	while (p->parent)
		p=p->parent;
	++p->numparts;

	return (newp);
}

static struct rfc2045 *append_part(struct rfc2045 *p, size_t startpos)
{
	struct rfc2045 *newp=append_part_noinherit(p, startpos);

	/* Substructures inherit content transfer encoding and character set */

	set_string(&newp->content_transfer_encoding,
			p->content_transfer_encoding);
	rfc2045_setattr(&newp->content_type_attr, "charset",
			rfc2045_getattr(p->content_type_attr, "charset"));
	return (newp);
}

/*
	doline() processes next line in the RFC2045 message.

	Drills down the list of all the multipart messages currently open,
	and checks if the line is a boundary line for the given multipart.
	In theory the boundary line, if there is one, should be the boundary
	line only for the inner multipart only, but, this takes into account
	broken MIME messages.
 */

static void do_header(struct rfc2045 *);

static void doline(struct rfc2045 *p)
{
	size_t	cnt=p->workbuflen;
	char *c=p->workbuf;
	size_t	n=cnt-1;	/* Strip \n (we always get at least a \n here) */
	struct rfc2045 *newp;
	struct rfc2045ac *rwp=p->rfc2045acptr;
	unsigned num_levels=0;

	size_t	k;
	int	bit8=0;

	if (p->numparts > MAXPARTS)
	{
		p->rfcviolation |= RFC2045_ERR2COMPLEX;
		return;
	}

	for (k=0; k<cnt; k++)
		if (c[k] & 0x80)	bit8=1;

	if (n && c[n-1] == '\r')	/* Strip trailing \r */
		--n;

	/* Before the main drill down loop before, look ahead and see if we're
	 ** in a middle of a form-data section.  */

	for (newp=p; newp->lastpart &&
			!newp->lastpart->workclosed; newp=newp->lastpart,
			++num_levels)
	{
		if (ContentBoundary(newp) == 0 || newp->workinheader)
			continue;

		if (newp->lastpart->informdata)
		{
			p=newp->lastpart;
			p->informdata=0;
			break;
		}
	}

	/* Drill down until we match a boundary, or until we've reached
	   the last RFC2045 section that has been opened.
	   */

	while (p->lastpart)
	{
		size_t l;
		const char *cb;

		if (p->lastpart->workclosed)
		{
			update_counts(p, p->endpos+cnt, p->endpos+cnt, 1);
			return;
		}
		/* Leftover trash -- workclosed is set when the final
		 ** terminating boundary has been seen */

		/* content_boundary may be set before the entire header
		 ** has been seen, so continue drilling down in that case
		 */

		cb=ContentBoundary(p);

		if (cb == 0 || p->workinheader)
		{
			p=p->lastpart;
			++num_levels;
			continue;
		}

		l=strlen(cb);

		if (c[0] == '-' && c[1] == '-' && n >= 2+l &&
				strncasecmp(cb, c+2, l) == 0)
		{

			if (rwp && (!p->lastpart || !p->lastpart->isdummy))
				(*rwp->end_section)(rwp);

			/* Ok, we've found a boundary */

			if (n >= 4+l && strncmp(c+2+l, "--", 2) == 0)
			{
				/* Last boundary */

				p->lastpart->workclosed=1;
				update_counts(p, p->endpos+cnt, p->endpos+cnt,
						1);
				return;
			}

			/* Create new RFC2045 section */

			newp=append_part(p, p->endpos+cnt);
			update_counts(p, p->endpos+cnt, p->endpos+n, 1);

			/* The new RFC2045 section is MIME compliant */

			newp->mime_version = estrdup(p->mime_version);
			return;
		}
		p=p->lastpart;
		++num_levels;
	}

	/* Ok, we've found the RFC2045 section that we're working with.
	 ** Now what?
	 */

	if (! p->workinheader)
	{
		/* Processing body, just update the counts. */

		size_t cnt_update=cnt;

		if (bit8 && !p->content_8bit &&
				(p->rfcviolation & RFC2045_ERR8BITCONTENT) == 0)
		{
			struct rfc2045 *q;

			for (q=p; q; q=q->parent)
				q->rfcviolation |= RFC2045_ERR8BITCONTENT;
		}

		/*
		 ** In multiparts, the final newline in a part belongs to the
		 ** boundary, otherwise, include it in the text.
		 */
		if (p->parent && p->parent->content_type &&
				strncasecmp(p->parent->content_type,
					"multipart/", 10) == 0)
			cnt_update=n;

		if (!p->lastpart || !p->lastpart->workclosed)
		{
			if (rwp && !p->isdummy)
				(*rwp->section_contents)(rwp, c, cnt);

			update_counts(p, p->endpos+cnt, p->endpos+cnt_update,
					1);
		}
		return;
	}

	if (bit8 && (p->rfcviolation & RFC2045_ERR8BITHEADER) == 0)
	{
		struct rfc2045 *q;

		for (q=p; q; q=q->parent)
			q->rfcviolation |= RFC2045_ERR8BITHEADER;
	}

	/* In the header */

	if ( n == 0 )	/* End of header, body begins.  Parse header. */
	{
		do_header(p);	/* Clean up any left over header line */
		p->workinheader=0;

		/* Message body starts right here */

		p->startbody=p->endpos+cnt;
		update_counts(p, p->startbody, p->startbody, 1);
		--p->nbodylines;	/* Don't count the blank line */

		/* Discard content type and boundary if I don't understand this MIME flavor.
		 * Allow broken messages that omit the Mime-Version header to still be
		 * parsed.
		 */

		if (p->mime_version == NULL && p->content_type != NULL)	{
			/* technically in violation of the spec, but there are some broken
			 * mailers out there that send this.  Sadly, they are so broken
			 * they don't set X-Mailer so we can't tell what they are...
			 * Lets be useful and allow it, but flag it as a boo-boo */
			p->mime_version = estrdup("1.0");
			p->rfcviolation |= RFC2045_ERRNOMIMEVERSION;
		}

		if (!RFC2045_ISMIME1(p->mime_version))
		{
			set_string(&p->content_type, 0);

			rfc2045_freeattr(p->content_type_attr);
			p->content_type_attr=0;
			set_string(&p->content_disposition, 0);
			rfc2045_freeattr(p->content_disposition_attr);
			p->content_disposition_attr=0;
			if (p->boundary)
			{
				efree(p->boundary);
				p->boundary=0;
			}
		}

		/* Normally, if we don't have a content_type, default it
		 ** to text/plain.  However, if the multipart type is
		 ** multipart/digest, it is message/rfc822.
		 */

		if (RFC2045_ISMIME1(p->mime_version) && !p->content_type)
		{
			char	*q="text/plain";

			if (p->parent && p->parent->content_type &&
					strcmp(p->parent->content_type,
						"multipart/digest") == 0)
				q="message/rfc822";
			set_string(&p->content_type, q);
		}

		/* If this is not a multipart section, we don't want to
		 ** hear about any boundaries
		 */

		if (!p->content_type ||
				strncmp(p->content_type, "multipart/", 10))
			rfc2045_setattr(&p->content_type_attr, "boundary", 0);

		/* If this section's a message, we will expect to see
		 ** more RFC2045 stuff, so create a nested RFC2045 structure,
		 ** and indicate that we expect to see headers.
		 */

		if (p->content_type &&
				strcmp(p->content_type, "message/rfc822") == 0)
		{
			newp=append_part_noinherit(p, p->startbody);
			newp->workinheader=1;
			return;
		}

		/*
		 ** If this is a multipart message (boundary defined),
		 ** create a RFC2045 structure for the pseudo-section
		 ** that precedes the first boundary line.
		 */

		if (ContentBoundary(p))
		{
			newp=append_part(p, p->startbody);
			newp->workinheader=0;
			newp->isdummy=1;
			/* It's easier just to create it. */
			return;
		}

		if (rwp)
			(*rwp->start_section)(rwp, p);
		return;
	}

	/* RFC822 header continues */

	update_counts(p, p->endpos + cnt, p->endpos+n, 1);

	/* If this header line starts with a space, append one space
	 ** to the saved contents of the previous line, and append this
	 ** line to it.
	 */

	if (isspace((int)(unsigned char)*c))
	{
		rfc2045_add_buf(&p->header, &p->headersize, &p->headerlen, " ", 1);
	}
	else
	{
		/* Otherwise the previous header line is complete, so process it */

		do_header(p);
		p->headerlen=0;
	}

	/* Save this line in the header buffer, because the next line
	 ** could be a continuation.
	 */

	rfc2045_add_buf( &p->header, &p->headersize, &p->headerlen, c, n);
}

/***********************************************************************/

/*
 ** paste_tokens() - recombine an array of RFC822 tokens back as a string.
 ** (Comments) are ignored.
 */

static char *paste_tokens(struct rfc822t *h, int start, int cnt)
{
	int	l;
	int	i;
	char	*p;

	/* Calculate string size */

	l=1;
	for (i=0; i<cnt; i++)
	{
		if (h->tokens[start+i].token == '(')
			continue;

		if (mailparse_rfc822_is_atom(h->tokens[start+i].token))
			l += h->tokens[start+i].len;
		else
			l++;
	}

	/* Do it */

	p=( char *)emalloc(l);
	l=0;

	for (i=0; i<cnt; i++)
	{
		if (h->tokens[start+i].token == '(')
			continue;

		if (mailparse_rfc822_is_atom(h->tokens[start+i].token))
		{
			int l2=h->tokens[start+i].len;

			memcpy(p+l, h->tokens[start+i].ptr, l2);
			l += l2;
		}
		else	p[l++]=h->tokens[start+i].token;
	}
	p[l]=0;
	return (p);
}

/* Various permutations of the above, including forcing the string to
 ** lowercase
 */

static char *lower_paste_tokens(struct rfc822t *h, int start, int cnt)
{
	char	*p=paste_tokens(h, start, cnt);
	char	*q;

	for (q=p; q && *q; q++)
		*q=tolower(*q);
	return (p);
}

static char *paste_token(struct rfc822t *h, int i)
{
	if (i >= h->ntokens)	return (0);
	return (paste_tokens(h, i, 1));
}

static char *lower_paste_token(struct rfc822t *h, int i)
{
	char *p=paste_token(h, i);
	char *q;

	for (q=p; q && *q; q++)
		*q=tolower(*q);
	return (p);
}

/*
	do_header() - process completed RFC822 header.
 */

static void mime_version(struct rfc2045 *, struct rfc822t *);
static void content_type(struct rfc2045 *, struct rfc822t *);
static void content_transfer_encoding(struct rfc2045 *, struct rfc822t *);
static void content_disposition(struct rfc2045 *, struct rfc822t *);
static void content_id(struct rfc2045 *, struct rfc822t *);
static void content_description(struct rfc2045 *, const char *);
static void content_language(struct rfc2045 *, const char *);
static void content_md5(struct rfc2045 *, const char *);
static void content_base(struct rfc2045 *, struct rfc822t *);
static void content_location(struct rfc2045 *, struct rfc822t *);

static void do_header(struct rfc2045 *p)
{
	struct rfc822t *header;
	char	*t;
	char * val;

	if (p->headerlen == 0)	return;
	rfc2045_add_buf( &p->header, &p->headersize, &p->headerlen, "", 1);
	/* 0 terminate */

	/* Parse the header line according to RFC822 */

	header=mailparse_rfc822t_alloc(p->header, NULL);

	if (!header)	return;	/* Broken header */

	if (header->ntokens < 2 ||
			header->tokens[0].token ||
			header->tokens[1].token != ':')
	{
		mailparse_rfc822t_free(header);
		return;	/* Broken header */
	}

	t=lower_paste_token(header, 0);


	if (t != 0)	{

		/* add the header to the hash */
		val = strchr(p->header, ':');
		if (val)	{
			val++;
			while(isspace(*val))
				val++;
			add_assoc_string(p->headerhash, t, val, 1);
		}
		if (strcmp(t, "mime-version") == 0)
		{
			efree(t);
			mime_version(p, header);
		}
		else if (strcmp(t, "content-type") == 0)
		{
			efree(t);
			content_type(p, header);
		} else if (strcmp(t, "content-transfer-encoding") == 0)
		{
			efree(t);
			content_transfer_encoding(p, header);
		} else if (strcmp(t, "content-disposition") == 0)
		{
			efree(t);
			content_disposition(p, header);
		} else if (strcmp(t, "content-id") == 0)
		{
			efree(t);
			content_id(p, header);
		} else if (strcmp(t, "content-description") == 0)
		{
			efree(t);
			t=strchr(p->header, ':');
			if (t)	++t;
			while (t && isspace((int)(unsigned char)*t))
				++t;
			content_description(p, t);
		} else if (strcmp(t, "content-language") == 0)
		{
			efree(t);
			t=strchr(p->header, ':');
			if (t)	++t;
			while (t && isspace((int)(unsigned char)*t))
				++t;
			content_language(p, t);
		} else if (strcmp(t, "content-base") == 0)
		{
			efree(t);
			content_base(p, header);
		} else if (strcmp(t, "content-location") == 0)
		{
			efree(t);
			content_location(p, header);
		} else if (strcmp(t, "content-md5") == 0)
		{
			efree(t);
			t=strchr(p->header, ':');
			if (t)	++t;
			while (t && isspace((int)(unsigned char)*t))
				++t;
			content_md5(p, t);
		}
		else	efree(t);
	}
	mailparse_rfc822t_free(header);
}

/* Mime-Version: and Content-Transfer-Encoding: headers are easy */

static void mime_version(struct rfc2045 *p, struct rfc822t *header)
{
	char	*vers=paste_tokens(header, 2, header->ntokens-2);

	if (!vers)	return;

	if (p->mime_version)	efree(p->mime_version);
	p->mime_version=vers;
}

static void content_transfer_encoding(struct rfc2045 *r,
		struct rfc822t *header)
{
	char	*p;

	p=lower_paste_tokens(header, 2, header->ntokens-2);
	if (!p)	return;

	if (r->content_transfer_encoding)
		efree(r->content_transfer_encoding);
	r->content_transfer_encoding=p;

	if (strcmp(p, "8bit") == 0)
		r->content_8bit=1;
}

/* Dig into the content_type header */

static void parse_content_header(struct rfc2045 *r, struct rfc822t *header,
		void (*init_token)(struct rfc2045 *, char *),
		void (*init_parameter)(struct rfc2045 *, const char *,
			struct rfc822t *, int, int))
{
	int	start;
	int	i, j;
	char	*p;

	/* Look for the 1st ; */

	for (start=2; start < header->ntokens; start++)
		if (header->tokens[start].token == ';')
			break;

	/* Everything up to the 1st ; is the content type */

	p=lower_paste_tokens(header, 2, start-2);
	if (!p)	return;

	(*init_token)(r, p);
	if (start < header->ntokens) start++;

	/* Handle the remainder of the Content-Type: header */

	while (start < header->ntokens)
	{
		/* Look for next ; */

		for (i=start; i<header->ntokens; i++)
			if (header->tokens[i].token == ';')
				break;
		j=start;
		if (j < i)
		{
			++j;

			/* We only understand <atom>= */

			while (j < i && header->tokens[j].token == '(')
				++j;
			if (j < i && header->tokens[j].token == '=')
			{
				++j;
				p=lower_paste_token(header, start);
				if (!p)	return;
				(*init_parameter)(r, p, header, j, i-j);
				efree(p);
			}
		}
		if ( i<header->ntokens ) ++i;	/* Skip over ; */
		start=i;
	}
}

/* Dig into the content_type header */

static void save_content_type(struct rfc2045 *, char *);
static void save_content_type_parameter( struct rfc2045 *, const char *,
		struct rfc822t *, int, int);

static void content_type(struct rfc2045 *r, struct rfc822t *header)
{
	parse_content_header(r, header, &save_content_type,
			&save_content_type_parameter);
}

static void save_content_type(struct rfc2045 *r, char *content_type)
{
	if (r->content_type)	efree(r->content_type);
	r->content_type=content_type;
}

static void save_content_type_parameter(
		struct rfc2045 *r, const char *name,
		struct rfc822t *header, int start, int len)
{
	char	*p;

	p=strcmp(name, "charset") == 0 ?
		lower_paste_tokens(header, start, len):
		paste_tokens(header, start, len);
	if (!p)	return;

	rfc2045_setattr(&r->content_type_attr, name, p);
	efree(p);

	if (strcmp(name, "boundary") == 0)
	{
		if (r->boundary)
			efree(r->boundary);
		p=lower_paste_tokens(header, start, len);
		r->boundary=p;
	}
}

/* Dig into content-disposition */

static void save_content_disposition(struct rfc2045 *, char *);
static void save_content_disposition_parameter( struct rfc2045 *, const char *,
		struct rfc822t *, int, int);

static void content_disposition(struct rfc2045 *r, struct rfc822t *header)
{
	parse_content_header(r, header, &save_content_disposition,
			&save_content_disposition_parameter);
}

static void save_content_disposition(struct rfc2045 *r,
		char *content_disposition)
{
	if (r->content_disposition)	efree(r->content_disposition);
	r->content_disposition=content_disposition;
}

static void save_content_disposition_parameter(
		struct rfc2045 *r, const char *name,
		struct rfc822t *header, int start, int len)
{
	char	*p;

	p=paste_tokens(header, start, len);
	if (!p)	return;

	rfc2045_setattr(&r->content_disposition_attr, name, p);
	efree(p);
}

char *rfc2045_related_start(const struct rfc2045 *p)
{
	const char *cb=rfc2045_getattr( p->content_type_attr, "start");
	struct	rfc822t *t;
	struct	rfc822a	*a;
	int	i;

	if (!cb || !*cb)	return (0);

	t=mailparse_rfc822t_alloc(cb, 0);
	a=mailparse_rfc822a_alloc(t);
	for (i=0; i<a->naddrs; i++)
		if (a->addrs[i].tokens)
		{
			char	*s=mailparse_rfc822_getaddr(a, i);

			mailparse_rfc822a_free(a);
			mailparse_rfc822t_free(t);
			return (s);
		}

	mailparse_rfc822a_free(a);
	mailparse_rfc822t_free(t);
	return (0);
}

static void content_id(struct rfc2045 *p, struct rfc822t *t)
{
	struct	rfc822a	*a=mailparse_rfc822a_alloc(t);
	int	i;


	for (i=0; i<a->naddrs; i++)
		if (a->addrs[i].tokens)
		{
			char	*s=mailparse_rfc822_getaddr(a, i);
			if (p->content_id)
				efree(p->content_id);
			p->content_id=s;
			break;
		}

	mailparse_rfc822a_free(a);
}

static void content_description(struct rfc2045 *p, const char *s)
{
	if (s && *s)
		set_string(&p->content_description, s);
}

static void content_language(struct rfc2045 *p, const char *s)
{
	if (s && *s)
		set_string(&p->content_language, s);
}

static void content_md5(struct rfc2045 *p, const char *s)
{
	if (s && *s)
		set_string(&p->content_md5, s);
}

static void content_base(struct rfc2045 *p, struct rfc822t *t)
{
	char	*s;
	int	i;

	for (i=0; i<t->ntokens; i++)
		if (t->tokens[i].token == '"')
			t->tokens[i].token=0;

	s=paste_tokens(t, 2, t->ntokens-2);
	set_string(&p->content_base, s);
}

static void content_location(struct rfc2045 *p, struct rfc822t *t)
{
	char	*s;
	int	i;

	for (i=0; i<t->ntokens; i++)
		if (t->tokens[i].token == '"')
			t->tokens[i].token=0;

	s=paste_tokens(t, 2, t->ntokens-2);
	set_string(&p->content_location, s);
}

/* -------------------- */

#define	GETINFO(s, def) ( (s) && (*s) ? (s):def)

void rfc2045_mimeinfo(const struct rfc2045 *p,
		const char **content_type_s,
		const char **content_transfer_encoding_s,
		const char **charset_s)
{
	const char *c;
	MAILPARSELS_FETCH();

	*content_type_s=GETINFO(p->content_type, "text/plain");
	*content_transfer_encoding_s=GETINFO(p->content_transfer_encoding,
			"8bit");

	c=rfc2045_getattr(p->content_type_attr, "charset");
	if (!c)
		c = MAILPARSEG(def_charset);

	*charset_s=c;
}

const char *rfc2045_boundary(const struct rfc2045 *p)
{
	const char *cb=rfc2045_getattr( p->content_type_attr, "boundary");

	if (!cb)	cb="";
	return (cb);
}

void rfc2045_dispositioninfo(const struct rfc2045 *p,
		const char **disposition_s,
		const char **disposition_name_s,
		const char **disposition_filename_s)
{
	*disposition_s=p->content_disposition;
	*disposition_name_s=rfc2045_getattr(p->content_disposition_attr,
			"name");
	*disposition_filename_s=rfc2045_getattr(p->content_disposition_attr,
			"filename");
}

const char *rfc2045_contentname(const struct rfc2045 *p)
{
	const	char *q=rfc2045_getattr(p->content_type_attr, "name");

	if (!q)	q="";
	return (q);
}

const char *rfc2045_content_id(const struct rfc2045 *p)
{
	return (p->content_id ? p->content_id:"");
}

const char *rfc2045_content_description(const struct rfc2045 *p)
{
	return (p->content_description ? p->content_description:"");
}

const char *rfc2045_content_language(const struct rfc2045 *p)
{
	return (p->content_language ? p->content_language:"");
}

const char *rfc2045_content_md5(const struct rfc2045 *p)
{
	return (p->content_md5 ? p->content_md5:"");
}

void rfc2045_mimepos(const struct rfc2045 *p,
		off_t *start_pos, off_t *end_pos, off_t *start_body,
		off_t *nlines, off_t *nbodylines)
{
	*start_pos=p->startpos;
	*end_pos=p->endpos;

	*nlines=p->nlines;
	*nbodylines=p->nbodylines;
	if (p->parent)	/* MIME parts do not have the trailing CRLF */
	{
		*end_pos=p->endbody;
		if (*nlines)	--*nlines;
		if (*nbodylines) --*nbodylines;
	}
	*start_body=p->startbody;
}

unsigned rfc2045_mimepartcount(const struct rfc2045 *p)
{
	const struct rfc2045 *q;
	unsigned n=0;

	for (q=p->firstpart; q; q=q->next)	++n;
	return (n);
}

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: sw=4 ts=4 tw=78 fdm=marker
 * vim<600: sw=4 ts=4 tw=78
 */