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;
80 * For non-multipart messages, we need to generate a quickie partnum of "1"
81 * to return to callback functions. Some callbacks demand it.
83 char *fixed_partnum(char *supplied_partnum) {
84 if (supplied_partnum == NULL) return "1";
85 if (strlen(supplied_partnum)==0) return "1";
86 return supplied_partnum;
91 * Given a message or message-part body and a length, handle any necessary
92 * decoding and pass the request up the stack.
94 void mime_decode(char *partnum,
95 char *part_start, size_t length,
96 char *content_type, char *encoding,
98 char *name, char *filename,
109 void (*PreMultiPartCallBack)
119 void (*PostMultiPartCallBack)
139 size_t bytes_sent = 0;
140 size_t bytes_recv = 0;
144 lprintf(9, "mime_decode() called\n");
146 /* Some encodings aren't really encodings */
147 if (!strcasecmp(encoding, "7bit"))
148 strcpy(encoding, "");
149 if (!strcasecmp(encoding, "8bit"))
150 strcpy(encoding, "");
151 if (!strcasecmp(encoding, "binary"))
152 strcpy(encoding, "");
154 /* If this part is not encoded, send as-is */
155 if ( (strlen(encoding) == 0) || (dont_decode)) {
156 if (CallBack != NULL) {
157 CallBack(name, filename, fixed_partnum(partnum),
158 disposition, part_start,
159 content_type, length, encoding, userdata);
163 if ((strcasecmp(encoding, "base64"))
164 && (strcasecmp(encoding, "quoted-printable"))) {
165 lprintf(5, "ERROR: unknown MIME encoding '%s'\n", encoding);
169 * Allocate a buffer for the decoded data. The output buffer is the
170 * same size as the input buffer; this assumes that the decoded data
171 * will never be larger than the encoded data. This is a safe
172 * assumption with base64, uuencode, and quoted-printable. Just to
173 * be safe, we still pad the buffer a bit.
175 decoded = mallok(length + 1024);
176 if (decoded == NULL) {
177 lprintf(5, "ERROR: cannot allocate memory.\n");
180 if (pipe(sendpipe) != 0)
182 if (pipe(recvpipe) != 0)
192 /* send stdio to the pipes */
193 if (dup2(sendpipe[0], 0) < 0)
194 lprintf(5, "ERROR dup2()\n");
195 if (dup2(recvpipe[1], 1) < 0)
196 lprintf(5, "ERROR dup2()\n");
197 close(sendpipe[1]); /* Close the ends we're not using */
199 if (!strcasecmp(encoding, "base64"))
200 execlp("./base64", "base64", "-d", NULL);
201 else if (!strcasecmp(encoding, "quoted-printable"))
202 execlp("./qpdecode", "qpdecode", NULL);
203 lprintf(5, "ERROR: cannot exec decoder for %s\n", encoding);
206 close(sendpipe[0]); /* Close the ends we're not using */
209 while ((bytes_sent < length) && (write_error == 0)) {
210 /* Empty the input pipe FIRST */
211 while (fstat(recvpipe[0], &statbuf), (statbuf.st_size > 0)) {
212 blocksize = read(recvpipe[0], &decoded[bytes_recv],
215 lprintf(5, "ERROR: cannot read from pipe\n");
217 bytes_recv = bytes_recv + blocksize;
219 /* Then put some data into the output pipe */
220 blocksize = length - bytes_sent;
221 if (blocksize > 2048)
223 if (write(sendpipe[1], &part_start[bytes_sent], blocksize) < 0) {
224 lprintf(5, "ERROR: cannot write to pipe: %s\n",
228 bytes_sent = bytes_sent + blocksize;
231 /* Empty the input pipe */
232 while ((blocksize = read(recvpipe[0], &decoded[bytes_recv], 1)),
234 bytes_recv = bytes_recv + blocksize;
237 if (bytes_recv > 0) if (CallBack != NULL) {
238 CallBack(name, filename, fixed_partnum(partnum),
239 disposition, decoded,
240 content_type, bytes_recv, "binary", userdata);
247 * Break out the components of a multipart message
248 * (This function expects to be fed HEADERS + CONTENT)
249 * Note: NULL can be supplied as content_end; in this case, the message is
250 * considered to have ended when the parser encounters a 0x00 byte.
252 void the_mime_parser(char *partnum,
253 char *content_start, char *content_end,
264 void (*PreMultiPartCallBack)
274 void (*PostMultiPartCallBack)
290 char *part_start, *part_end;
296 char content_type[SIZ];
298 char disposition[SIZ];
305 char nested_partnum[SIZ];
307 lprintf(9, "the_mime_parser() called\n");
309 memset(boundary, 0, sizeof boundary);
310 memset(content_type, 0, sizeof content_type);
311 memset(encoding, 0, sizeof encoding);
312 memset(name, 0, sizeof name);
313 memset(filename, 0, sizeof filename);
314 memset(disposition, 0, sizeof disposition);
316 /* Learn interesting things from the headers */
319 ptr = memreadline(ptr, buf, sizeof buf);
321 return; /* premature end of message */
322 if (content_end != NULL)
323 if (ptr >= content_end)
326 for (i = 0; i < strlen(buf); ++i)
329 if (!isspace(buf[0])) {
330 if (!strncasecmp(header, "Content-type: ", 14)) {
331 strcpy(content_type, &header[14]);
332 extract_key(name, content_type, "name");
334 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
335 strcpy(disposition, &header[21]);
336 extract_key(filename, disposition, "filename");
338 if (!strncasecmp(header,
339 "Content-transfer-encoding: ", 27))
340 strcpy(encoding, &header[27]);
341 if (strlen(boundary) == 0)
342 extract_key(boundary, header, "boundary");
345 if ((strlen(header) + strlen(buf) + 2) < sizeof(header))
347 } while ((strlen(buf) > 0) && (*ptr != 0));
349 for (i = 0; i < strlen(disposition); ++i)
350 if (disposition[i] == ';')
352 while (isspace(disposition[0]))
353 strcpy(disposition, &disposition[1]);
354 for (i = 0; i < strlen(content_type); ++i)
355 if (content_type[i] == ';')
357 while (isspace(content_type[0]))
358 strcpy(content_type, &content_type[1]);
360 if (strlen(boundary) > 0) {
366 /* If this is a multipart message, then recursively process it */
370 /* Tell the client about this message's multipartedness */
371 if (PreMultiPartCallBack != NULL) {
372 PreMultiPartCallBack("", "", partnum, "",
374 0, encoding, userdata);
377 if (CallBack != NULL) {
378 CallBack("", "", fixed_partnum(partnum),
379 "", NULL, content_type,
380 0, encoding, userdata);
384 /* Figure out where the boundaries are */
385 sprintf(startary, "--%s", boundary);
386 sprintf(endary, "--%s--", boundary);
389 ptr = memreadline(ptr, buf, sizeof buf);
390 if (*ptr == 0) goto END_MULTI; /* premature end */
391 if (content_end != NULL)
392 if (ptr >= content_end) goto END_MULTI;
393 if ((!strcasecmp(buf, startary))
394 || (!strcasecmp(buf, endary))) {
395 if (part_start != NULL) {
396 if (strlen(partnum) > 0) {
397 sprintf(nested_partnum, "%s.%d",
398 partnum, ++part_seq);
401 sprintf(nested_partnum, "%d",
404 the_mime_parser(nested_partnum,
405 part_start, part_end,
407 PreMultiPartCallBack,
408 PostMultiPartCallBack,
414 } while (strcasecmp(buf, endary));
415 END_MULTI: if (PostMultiPartCallBack != NULL) {
416 PostMultiPartCallBack("", "", partnum, "", NULL,
417 content_type, 0, encoding, userdata);
422 /* If it's not a multipart message, then do something with it */
427 && ((content_end == NULL) || (ptr < content_end))) {
433 content_type, encoding, disposition,
435 CallBack, NULL, NULL,
436 userdata, dont_decode);
445 * Entry point for the MIME parser.
446 * (This function expects to be fed HEADERS + CONTENT)
447 * Note: NULL can be supplied as content_end; in this case, the message is
448 * considered to have ended when the parser encounters a 0x00 byte.
450 void mime_parser(char *content_start,
464 void (*PreMultiPartCallBack)
475 void (*PostMultiPartCallBack)
491 lprintf(9, "mime_parser() called\n");
492 the_mime_parser("", content_start, content_end,
494 PreMultiPartCallBack,
495 PostMultiPartCallBack,
496 userdata, dont_decode);