about summary refs log tree commit diff
path: root/rfc2045.c
blob: e5e19ff4d3082595c6d14fda5f77d0647f56db3e (plain) (blame)
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
#define _GNU_SOURCE
#include <string.h>

#include "blaze822.h"
#include "blaze822_priv.h"

int
blaze822_check_mime(struct message *msg)
{
        char *v = blaze822_hdr(msg, "mime-version");
	return (v &&
	    v[0] && v[0] == '1' &&
	    v[1] && v[1] == '.' &&
	    v[2] && v[2] == '0' &&
	    (!v[3] || iswsp(v[3])));
}

int
blaze822_mime_body(struct message *msg,
    char **cto, char **bodyo, size_t *bodyleno, char **bodychunko)
{
	if (!msg->body || !msg->bodyend) {
		*bodyo = 0;
		*bodyleno = 0;
		return -1;
        }

	char *ct = blaze822_hdr(msg, "content-type");
	char *cte = blaze822_hdr(msg, "content-transfer-encoding");

	if (!ct)
		ct = "text/plain; charset=US-ASCII";

	char *s = ct;
	while (*s && *s != ';')
		s++;

	*cto = ct;

	if (cte) {
		if (strncasecmp(cte, "quoted-printable", 16) == 0) {
			blaze822_decode_qp(msg->body, msg->bodyend, bodyo, bodyleno);
			*bodychunko = *bodyo;
		} else if (strncasecmp(cte, "base64", 6) == 0) {
			blaze822_decode_b64(msg->body, msg->bodyend, bodyo, bodyleno);
			*bodychunko = *bodyo;
		} else {
			cte = 0;
		}
	}
	if (!cte) {
		*bodyo = msg->body;
		*bodyleno = msg->bodyend - msg->body;
		*bodychunko = 0;
	}

	return 1;
}

int
blaze822_multipart(struct message *msg, struct message **imsg)
{
	char *s = blaze822_hdr(msg, "content-type");
	if (!s)
		return 0;
	while (*s && *s != ';')
		s++;
	if (!*s)
		return 0;

	// XXX scan boundary only once
	char *boundary = s+1;
	while (*boundary) {
		while (iswsp(*boundary))
			boundary++;
		if (strncasecmp(boundary, "boundary=", 9) == 0) {
			boundary += 9;
			break;
		}
		boundary = strchr(boundary+1, ';');
		if (!boundary)
			break;
		boundary++;
	}
	if (!boundary || !*boundary)
		return 0;
	char *e;
	if (*boundary == '"') {
		boundary++;
		e = strchr(boundary, '"');
		if (!e)
			return 0;
	} else {
		e = boundary;
		// XXX    bchars := bcharsnospace / " "
		// bcharsnospace := DIGIT / ALPHA / "'" / "(" / ")" /
		//              "+" / "_" / "," / "-" / "." /
		//              "/" / ":" / "=" / "?"
		while (*e && !iswsp(*e) && *e != ';')
			e++;
		e++;
	}

	char mboundary[256];
	mboundary[0] = '-';
	mboundary[1] = '-';
	memcpy(mboundary+2, boundary, e-boundary);  // XXX overflow
	mboundary[e-boundary+2] = 0;

	int boundarylen = e-boundary+2;

//	printf("boundary: %s %s %s\n", ct, cte, boundary);

	char *prevpart;
	if (*imsg)
		prevpart = (*imsg)->bodyend;
	else
		prevpart = msg->body;

	char *part = memmem(prevpart, msg->bodyend - prevpart, mboundary, boundarylen);
	if (!part)
		return 0;
	/// XXX access to stuff before first boundary?
	part += boundarylen;
	if (*part == '\r')
		part++;
	if (*part == '\n')
		part++;
	else if (*part == '-' && part < msg->bodyend && *(part+1) == '-')
		return 0;
	else
		return 0;   // XXX error condition?

	char *nextpart = memmem(part, msg->bodyend - part, mboundary, boundarylen);
	if (!nextpart)
		return 0;   // XXX error condition

	*imsg = blaze822_mem(part, nextpart-part);

	return 1;
}