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,
97 void (*PreMultiPartCallBack)
107 void (*PostMultiPartCallBack)
127 size_t bytes_sent = 0;
128 size_t bytes_recv = 0;
132 lprintf(9, "mime_decode() called\n");
134 /* Some encodings aren't really encodings */
135 if (!strcasecmp(encoding, "7bit"))
136 strcpy(encoding, "");
137 if (!strcasecmp(encoding, "8bit"))
138 strcpy(encoding, "");
139 if (!strcasecmp(encoding, "binary"))
140 strcpy(encoding, "");
142 /* If this part is not encoded, send as-is */
143 if ( (strlen(encoding) == 0) || (dont_decode)) {
144 CallBack(name, filename, partnum, disposition, part_start,
145 content_type, length, encoding, userdata);
148 if ((strcasecmp(encoding, "base64"))
149 && (strcasecmp(encoding, "quoted-printable"))) {
150 lprintf(5, "ERROR: unknown MIME encoding '%s'\n", encoding);
154 * Allocate a buffer for the decoded data. The output buffer is the
155 * same size as the input buffer; this assumes that the decoded data
156 * will never be larger than the encoded data. This is a safe
157 * assumption with base64, uuencode, and quoted-printable. Just to
158 * be safe, we still pad the buffer a bit.
160 decoded = mallok(length + 1024);
161 if (decoded == NULL) {
162 lprintf(5, "ERROR: cannot allocate memory.\n");
165 if (pipe(sendpipe) != 0)
167 if (pipe(recvpipe) != 0)
177 /* send stdio to the pipes */
178 if (dup2(sendpipe[0], 0) < 0)
179 lprintf(5, "ERROR dup2()\n");
180 if (dup2(recvpipe[1], 1) < 0)
181 lprintf(5, "ERROR dup2()\n");
182 close(sendpipe[1]); /* Close the ends we're not using */
184 if (!strcasecmp(encoding, "base64"))
185 execlp("./base64", "base64", "-d", NULL);
186 else if (!strcasecmp(encoding, "quoted-printable"))
187 execlp("./qpdecode", "qpdecode", NULL);
188 lprintf(5, "ERROR: cannot exec decoder for %s\n", encoding);
191 close(sendpipe[0]); /* Close the ends we're not using */
194 while ((bytes_sent < length) && (write_error == 0)) {
195 /* Empty the input pipe FIRST */
196 while (fstat(recvpipe[0], &statbuf), (statbuf.st_size > 0)) {
197 blocksize = read(recvpipe[0], &decoded[bytes_recv],
200 lprintf(5, "ERROR: cannot read from pipe\n");
202 bytes_recv = bytes_recv + blocksize;
204 /* Then put some data into the output pipe */
205 blocksize = length - bytes_sent;
206 if (blocksize > 2048)
208 if (write(sendpipe[1], &part_start[bytes_sent], blocksize) < 0) {
209 lprintf(5, "ERROR: cannot write to pipe: %s\n",
213 bytes_sent = bytes_sent + blocksize;
216 /* Empty the input pipe */
217 while ((blocksize = read(recvpipe[0], &decoded[bytes_recv], 1)),
219 bytes_recv = bytes_recv + blocksize;
223 CallBack(name, filename, partnum, disposition, decoded,
224 content_type, bytes_recv, "binary", userdata);
230 * Break out the components of a multipart message
231 * (This function expects to be fed HEADERS + CONTENT)
232 * Note: NULL can be supplied as content_end; in this case, the message is
233 * considered to have ended when the parser encounters a 0x00 byte.
235 void the_mime_parser(char *partnum,
236 char *content_start, char *content_end,
247 void (*PreMultiPartCallBack)
257 void (*PostMultiPartCallBack)
273 char *part_start, *part_end;
279 char content_type[SIZ];
281 char disposition[SIZ];
288 char nested_partnum[SIZ];
290 lprintf(9, "the_mime_parser() called\n");
292 memset(boundary, 0, sizeof boundary);
293 memset(content_type, 0, sizeof content_type);
294 memset(encoding, 0, sizeof encoding);
295 memset(name, 0, sizeof name);
296 memset(filename, 0, sizeof filename);
297 memset(disposition, 0, sizeof disposition);
299 /* Learn interesting things from the headers */
302 ptr = memreadline(ptr, buf, sizeof buf);
304 return; /* premature end of message */
305 if (content_end != NULL)
306 if (ptr >= content_end)
309 for (i = 0; i < strlen(buf); ++i)
312 if (!isspace(buf[0])) {
313 if (!strncasecmp(header, "Content-type: ", 14)) {
314 strcpy(content_type, &header[14]);
315 extract_key(name, content_type, "name");
317 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
318 strcpy(disposition, &header[21]);
319 extract_key(filename, disposition, "filename");
321 if (!strncasecmp(header,
322 "Content-transfer-encoding: ", 27))
323 strcpy(encoding, &header[27]);
324 if (strlen(boundary) == 0)
325 extract_key(boundary, header, "boundary");
328 if ((strlen(header) + strlen(buf) + 2) < sizeof(header))
330 } while ((strlen(buf) > 0) && (*ptr != 0));
332 for (i = 0; i < strlen(disposition); ++i)
333 if (disposition[i] == ';')
335 while (isspace(disposition[0]))
336 strcpy(disposition, &disposition[1]);
337 for (i = 0; i < strlen(content_type); ++i)
338 if (content_type[i] == ';')
340 while (isspace(content_type[0]))
341 strcpy(content_type, &content_type[1]);
343 if (strlen(boundary) > 0) {
349 /* If this is a multipart message, then recursively process it */
353 /* Tell the client about this message's multipartedness */
354 PreMultiPartCallBack("", "", partnum, "", NULL, content_type,
355 0, encoding, userdata);
357 CallBack("", "", partnum, "", NULL, content_type,
358 0, encoding, userdata);
361 /* Figure out where the boundaries are */
362 sprintf(startary, "--%s", boundary);
363 sprintf(endary, "--%s--", boundary);
366 ptr = memreadline(ptr, buf, sizeof buf);
367 if (*ptr == 0) goto END_MULTI; /* premature end */
368 if (content_end != NULL)
369 if (ptr >= content_end) goto END_MULTI;
370 if ((!strcasecmp(buf, startary))
371 || (!strcasecmp(buf, endary))) {
372 if (part_start != NULL) {
373 if (strlen(partnum) > 0) {
374 sprintf(nested_partnum, "%s.%d",
375 partnum, ++part_seq);
378 sprintf(nested_partnum, "%d",
381 the_mime_parser(nested_partnum,
382 part_start, part_end,
384 PreMultiPartCallBack,
385 PostMultiPartCallBack,
391 } while (strcasecmp(buf, endary));
392 END_MULTI: PostMultiPartCallBack("", "", partnum, "", NULL, content_type,
393 0, encoding, userdata);
397 /* If it's not a multipart message, then do something with it */
402 && ((content_end == NULL) || (ptr < content_end))) {
408 content_type, encoding, disposition,
410 CallBack, NULL, NULL,
411 userdata, dont_decode);
420 * Entry point for the MIME parser.
421 * (This function expects to be fed HEADERS + CONTENT)
422 * Note: NULL can be supplied as content_end; in this case, the message is
423 * considered to have ended when the parser encounters a 0x00 byte.
425 void mime_parser(char *content_start,
439 void (*PreMultiPartCallBack)
450 void (*PostMultiPartCallBack)
466 lprintf(9, "mime_parser() called\n");
467 the_mime_parser("", content_start, content_end,
469 PreMultiPartCallBack,
470 PostMultiPartCallBack,
471 userdata, dont_decode);