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 * For non-multipart messages, we need to generate a quickie partnum of "1"
53 * to return to callback functions. Some callbacks demand it.
55 char *fixed_partnum(char *supplied_partnum) {
56 if (supplied_partnum == NULL) return "1";
57 if (strlen(supplied_partnum)==0) return "1";
58 return supplied_partnum;
63 * Given a message or message-part body and a length, handle any necessary
64 * decoding and pass the request up the stack.
66 void mime_decode(char *partnum,
67 char *part_start, size_t length,
68 char *content_type, char *encoding,
70 char *name, char *filename,
81 void (*PreMultiPartCallBack)
91 void (*PostMultiPartCallBack)
111 size_t bytes_sent = 0;
112 size_t bytes_recv = 0;
116 lprintf(9, "mime_decode() called\n");
118 /* Some encodings aren't really encodings */
119 if (!strcasecmp(encoding, "7bit"))
120 strcpy(encoding, "");
121 if (!strcasecmp(encoding, "8bit"))
122 strcpy(encoding, "");
123 if (!strcasecmp(encoding, "binary"))
124 strcpy(encoding, "");
126 /* If this part is not encoded, send as-is */
127 if ( (strlen(encoding) == 0) || (dont_decode)) {
128 if (CallBack != NULL) {
129 CallBack(name, filename, fixed_partnum(partnum),
130 disposition, part_start,
131 content_type, length, encoding, userdata);
135 if ((strcasecmp(encoding, "base64"))
136 && (strcasecmp(encoding, "quoted-printable"))) {
137 lprintf(5, "ERROR: unknown MIME encoding '%s'\n", encoding);
141 * Allocate a buffer for the decoded data. The output buffer is the
142 * same size as the input buffer; this assumes that the decoded data
143 * will never be larger than the encoded data. This is a safe
144 * assumption with base64, uuencode, and quoted-printable. Just to
145 * be safe, we still pad the buffer a bit.
147 decoded = mallok(length + 1024);
148 if (decoded == NULL) {
149 lprintf(5, "ERROR: cannot allocate memory.\n");
152 if (pipe(sendpipe) != 0)
154 if (pipe(recvpipe) != 0)
164 /* send stdio to the pipes */
165 if (dup2(sendpipe[0], 0) < 0)
166 lprintf(5, "ERROR dup2()\n");
167 if (dup2(recvpipe[1], 1) < 0)
168 lprintf(5, "ERROR dup2()\n");
169 close(sendpipe[1]); /* Close the ends we're not using */
171 if (!strcasecmp(encoding, "base64"))
172 execlp("./base64", "base64", "-d", NULL);
173 else if (!strcasecmp(encoding, "quoted-printable"))
174 execlp("./qpdecode", "qpdecode", NULL);
175 lprintf(5, "ERROR: cannot exec decoder for %s\n", encoding);
178 close(sendpipe[0]); /* Close the ends we're not using */
181 while ((bytes_sent < length) && (write_error == 0)) {
182 /* Empty the input pipe FIRST */
183 while (fstat(recvpipe[0], &statbuf), (statbuf.st_size > 0)) {
184 blocksize = read(recvpipe[0], &decoded[bytes_recv],
187 lprintf(5, "ERROR: cannot read from pipe\n");
189 bytes_recv = bytes_recv + blocksize;
191 /* Then put some data into the output pipe */
192 blocksize = length - bytes_sent;
193 if (blocksize > 2048)
195 if (write(sendpipe[1], &part_start[bytes_sent], blocksize) < 0) {
196 lprintf(5, "ERROR: cannot write to pipe: %s\n",
200 bytes_sent = bytes_sent + blocksize;
203 /* Empty the input pipe */
204 while ((blocksize = read(recvpipe[0], &decoded[bytes_recv], 1)),
206 bytes_recv = bytes_recv + blocksize;
209 if (bytes_recv > 0) if (CallBack != NULL) {
210 CallBack(name, filename, fixed_partnum(partnum),
211 disposition, decoded,
212 content_type, bytes_recv, "binary", userdata);
219 * Break out the components of a multipart message
220 * (This function expects to be fed HEADERS + CONTENT)
221 * Note: NULL can be supplied as content_end; in this case, the message is
222 * considered to have ended when the parser encounters a 0x00 byte.
224 void the_mime_parser(char *partnum,
225 char *content_start, char *content_end,
236 void (*PreMultiPartCallBack)
246 void (*PostMultiPartCallBack)
262 char *part_start, *part_end;
268 char content_type[SIZ];
270 char disposition[SIZ];
277 char nested_partnum[SIZ];
279 lprintf(9, "the_mime_parser() called\n");
281 memset(boundary, 0, sizeof boundary);
282 memset(content_type, 0, sizeof content_type);
283 memset(encoding, 0, sizeof encoding);
284 memset(name, 0, sizeof name);
285 memset(filename, 0, sizeof filename);
286 memset(disposition, 0, sizeof disposition);
288 /* Learn interesting things from the headers */
291 ptr = memreadline(ptr, buf, sizeof buf);
293 return; /* premature end of message */
294 if (content_end != NULL)
295 if (ptr >= content_end)
298 for (i = 0; i < strlen(buf); ++i)
301 if (!isspace(buf[0])) {
302 if (!strncasecmp(header, "Content-type: ", 14)) {
303 strcpy(content_type, &header[14]);
304 extract_key(name, content_type, "name");
306 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
307 strcpy(disposition, &header[21]);
308 extract_key(filename, disposition, "filename");
310 if (!strncasecmp(header,
311 "Content-transfer-encoding: ", 27))
312 strcpy(encoding, &header[27]);
313 if (strlen(boundary) == 0)
314 extract_key(boundary, header, "boundary");
317 if ((strlen(header) + strlen(buf) + 2) < sizeof(header))
319 } while ((strlen(buf) > 0) && (*ptr != 0));
321 for (i = 0; i < strlen(disposition); ++i)
322 if (disposition[i] == ';')
324 while (isspace(disposition[0]))
325 strcpy(disposition, &disposition[1]);
326 for (i = 0; i < strlen(content_type); ++i)
327 if (content_type[i] == ';')
329 while (isspace(content_type[0]))
330 strcpy(content_type, &content_type[1]);
332 if (strlen(boundary) > 0) {
338 /* If this is a multipart message, then recursively process it */
342 /* Tell the client about this message's multipartedness */
343 if (PreMultiPartCallBack != NULL) {
344 PreMultiPartCallBack("", "", partnum, "",
346 0, encoding, userdata);
349 if (CallBack != NULL) {
350 CallBack("", "", fixed_partnum(partnum),
351 "", NULL, content_type,
352 0, encoding, userdata);
356 /* Figure out where the boundaries are */
357 sprintf(startary, "--%s", boundary);
358 sprintf(endary, "--%s--", boundary);
361 ptr = memreadline(ptr, buf, sizeof buf);
362 if (content_end != NULL)
363 if (ptr >= content_end) goto END_MULTI;
365 if ( (!strcasecmp(buf, startary))
366 || (!strcasecmp(buf, endary)) ) {
367 if (part_start != NULL) {
368 if (strlen(partnum) > 0) {
369 sprintf(nested_partnum, "%s.%d",
370 partnum, ++part_seq);
373 sprintf(nested_partnum, "%d",
376 the_mime_parser(nested_partnum,
377 part_start, part_end,
379 PreMultiPartCallBack,
380 PostMultiPartCallBack,
386 } while ( (strcasecmp(buf, endary)) && (ptr != 0) );
387 END_MULTI: if (PostMultiPartCallBack != NULL) {
388 PostMultiPartCallBack("", "", partnum, "", NULL,
389 content_type, 0, encoding, userdata);
394 /* If it's not a multipart message, then do something with it */
399 && ((content_end == NULL) || (ptr < content_end))) {
405 content_type, encoding, disposition,
407 CallBack, NULL, NULL,
408 userdata, dont_decode);
417 * Entry point for the MIME parser.
418 * (This function expects to be fed HEADERS + CONTENT)
419 * Note: NULL can be supplied as content_end; in this case, the message is
420 * considered to have ended when the parser encounters a 0x00 byte.
422 void mime_parser(char *content_start,
436 void (*PreMultiPartCallBack)
447 void (*PostMultiPartCallBack)
463 lprintf(9, "mime_parser() called\n");
464 the_mime_parser("", content_start, content_end,
466 PreMultiPartCallBack,
467 PostMultiPartCallBack,
468 userdata, dont_decode);