4 * This is a really bad attempt at writing a parser to handle MIME-encoded
7 * Copyright (c) 1998-1999 by Art Cancro
8 * This code is distributed under the terms of the GNU General Public License.
17 #include <sys/types.h>
23 #include "mime_parser.h"
24 #include "sysdep_decls.h"
29 void extract_key(char *target, char *source, char *key)
33 strcpy(target, source);
34 for (a = 0; a < strlen(target); ++a) {
35 if ((!strncasecmp(&target[a], key, strlen(key)))
36 && (target[a + strlen(key)] == '=')) {
37 strcpy(target, &target[a + strlen(key) + 1]);
39 strcpy(target, &target[1]);
40 for (b = 0; b < strlen(target); ++b)
52 * Utility function to "readline" from memory
53 * (returns new pointer)
55 char *memreadline(char *start, char *buf, int maxlen)
61 memset(buf, 0, maxlen);
65 if ((ch == 10) || (ch == 0)) {
67 if (buf[strlen(buf) - 1] == 13)
68 buf[strlen(buf) - 1] = 0;
71 if (strlen(buf) < (maxlen - 1)) {
72 buf[strlen(buf) + 1] = 0;
73 buf[strlen(buf)] = ch;
79 * Given a message or message-part body and a length, handle any necessary
80 * decoding and pass the request up the stack.
82 void mime_decode(char *partnum,
83 char *part_start, size_t length,
84 char *content_type, char *encoding,
86 char *name, char *filename,
103 size_t bytes_sent = 0;
104 size_t bytes_recv = 0;
108 lprintf(9, "mime_decode() called\n");
110 /* Some encodings aren't really encodings */
111 if (!strcasecmp(encoding, "7bit"))
112 strcpy(encoding, "");
113 if (!strcasecmp(encoding, "8bit"))
114 strcpy(encoding, "");
115 if (!strcasecmp(encoding, "binary"))
116 strcpy(encoding, "");
118 /* If this part is not encoded, send as-is */
119 if (strlen(encoding) == 0) {
120 CallBack(name, filename, partnum, disposition, part_start,
121 content_type, length);
124 if ((strcasecmp(encoding, "base64"))
125 && (strcasecmp(encoding, "quoted-printable"))) {
126 lprintf(5, "ERROR: unknown MIME encoding '%s'\n", encoding);
130 * Allocate a buffer for the decoded data. The output buffer is the
131 * same size as the input buffer; this assumes that the decoded data
132 * will never be larger than the encoded data. This is a safe
133 * assumption with base64, uuencode, and quoted-printable. Just to
134 * be safe, we still pad the buffer a bit.
136 decoded = mallok(length + 1024);
137 if (decoded == NULL) {
138 lprintf(5, "ERROR: cannot allocate memory.\n");
141 if (pipe(sendpipe) != 0)
143 if (pipe(recvpipe) != 0)
153 /* send stdio to the pipes */
154 if (dup2(sendpipe[0], 0) < 0)
155 lprintf(5, "ERROR dup2()\n");
156 if (dup2(recvpipe[1], 1) < 0)
157 lprintf(5, "ERROR dup2()\n");
158 close(sendpipe[1]); /* Close the ends we're not using */
160 if (!strcasecmp(encoding, "base64"))
161 execlp("./base64", "base64", "-d", NULL);
162 else if (!strcasecmp(encoding, "quoted-printable"))
163 execlp("./qpdecode", "qpdecode", NULL);
164 lprintf(5, "ERROR: cannot exec decoder for %s\n", encoding);
167 close(sendpipe[0]); /* Close the ends we're not using */
170 while ((bytes_sent < length) && (write_error == 0)) {
171 /* Empty the input pipe FIRST */
172 while (fstat(recvpipe[0], &statbuf), (statbuf.st_size > 0)) {
173 blocksize = read(recvpipe[0], &decoded[bytes_recv],
176 lprintf(5, "ERROR: cannot read from pipe\n");
178 bytes_recv = bytes_recv + blocksize;
180 /* Then put some data into the output pipe */
181 blocksize = length - bytes_sent;
182 if (blocksize > 2048)
184 if (write(sendpipe[1], &part_start[bytes_sent], blocksize) < 0) {
185 lprintf(5, "ERROR: cannot write to pipe: %s\n",
189 bytes_sent = bytes_sent + blocksize;
192 /* Empty the input pipe */
193 while ((blocksize = read(recvpipe[0], &decoded[bytes_recv], 1)),
195 bytes_recv = bytes_recv + blocksize;
199 CallBack(name, filename, partnum, disposition, decoded,
200 content_type, bytes_recv);
206 * Break out the components of a multipart message
207 * (This function expects to be fed HEADERS + CONTENT)
208 * Note: NULL can be supplied as content_end; in this case, the message is
209 * considered to have ended when the parser encounters a 0x00 byte.
211 void the_mime_parser(char *partnum,
212 char *content_start, char *content_end,
225 char *part_start, *part_end;
231 char content_type[256];
233 char disposition[256];
240 char nested_partnum[256];
242 lprintf(9, "the_mime_parser() called\n");
244 memset(boundary, 0, sizeof boundary);
245 memset(content_type, 0, sizeof content_type);
246 memset(encoding, 0, sizeof encoding);
247 memset(name, 0, sizeof name);
248 memset(filename, 0, sizeof filename);
249 memset(disposition, 0, sizeof disposition);
251 /* Learn interesting things from the headers */
254 ptr = memreadline(ptr, buf, sizeof buf);
256 return; /* premature end of message */
257 if (content_end != NULL)
258 if (ptr >= content_end)
261 for (i = 0; i < strlen(buf); ++i)
264 if (!isspace(buf[0])) {
265 if (!strncasecmp(header, "Content-type: ", 14)) {
266 strcpy(content_type, &header[14]);
267 extract_key(name, content_type, "name");
269 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
270 strcpy(disposition, &header[21]);
271 extract_key(filename, disposition, "filename");
273 if (!strncasecmp(header,
274 "Content-transfer-encoding: ", 27))
275 strcpy(encoding, &header[27]);
276 if (strlen(boundary) == 0)
277 extract_key(boundary, header, "boundary");
280 if ((strlen(header) + strlen(buf) + 2) < sizeof(header))
282 } while ((strlen(buf) > 0) && (*ptr != 0));
284 for (i = 0; i < strlen(disposition); ++i)
285 if (disposition[i] == ';')
287 while (isspace(disposition[0]))
288 strcpy(disposition, &disposition[1]);
289 for (i = 0; i < strlen(content_type); ++i)
290 if (content_type[i] == ';')
292 while (isspace(content_type[0]))
293 strcpy(content_type, &content_type[1]);
295 if (strlen(boundary) > 0) {
301 /* If this is a multipart message, then recursively process it */
305 /* Tell the client about this message's multipartedness */
306 CallBack("", "", partnum, "", NULL, content_type, 0);
308 /* Figure out where the boundaries are */
309 sprintf(startary, "--%s", boundary);
310 sprintf(endary, "--%s--", boundary);
313 ptr = memreadline(ptr, buf, sizeof buf);
315 return; /* premature end of message */
316 if (content_end != NULL)
317 if (ptr >= content_end)
319 if ((!strcasecmp(buf, startary))
320 || (!strcasecmp(buf, endary))) {
321 if (part_start != NULL) {
322 sprintf(nested_partnum, "%s.%d",
323 partnum, ++part_seq);
324 the_mime_parser(nested_partnum,
325 part_start, part_end,
330 } while (strcasecmp(buf, endary));
333 /* If it's not a multipart message, then do something with it */
338 && ((content_end == NULL) || (ptr < content_end))) {
344 content_type, encoding, disposition,
345 name, filename, CallBack);
354 * Entry point for the MIME parser.
355 * (This function expects to be fed HEADERS + CONTENT)
356 * Note: NULL can be supplied as content_end; in this case, the message is
357 * considered to have ended when the parser encounters a 0x00 byte.
359 void mime_parser(char *content_start, char *content_end,
371 lprintf(9, "mime_parser() called\n");
372 the_mime_parser("1", content_start, content_end, CallBack);