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)
59 int len = 0; /* tally our own length to avoid strlen() delays */
62 memset(buf, 0, maxlen);
66 if ( (len < (maxlen - 1)) && (ch != 13) && (ch != 10) ) {
67 buf[strlen(buf) + 1] = 0;
68 buf[strlen(buf)] = ch;
71 if ((ch == 10) || (ch == 0)) {
79 * For non-multipart messages, we need to generate a quickie partnum of "1"
80 * to return to callback functions. Some callbacks demand it.
82 char *fixed_partnum(char *supplied_partnum) {
83 if (supplied_partnum == NULL) return "1";
84 if (strlen(supplied_partnum)==0) return "1";
85 return supplied_partnum;
90 * Given a message or message-part body and a length, handle any necessary
91 * decoding and pass the request up the stack.
93 void mime_decode(char *partnum,
94 char *part_start, size_t length,
95 char *content_type, char *encoding,
97 char *name, char *filename,
108 void (*PreMultiPartCallBack)
118 void (*PostMultiPartCallBack)
138 size_t bytes_sent = 0;
139 size_t bytes_recv = 0;
143 lprintf(9, "mime_decode() called\n");
145 /* Some encodings aren't really encodings */
146 if (!strcasecmp(encoding, "7bit"))
147 strcpy(encoding, "");
148 if (!strcasecmp(encoding, "8bit"))
149 strcpy(encoding, "");
150 if (!strcasecmp(encoding, "binary"))
151 strcpy(encoding, "");
153 /* If this part is not encoded, send as-is */
154 if ( (strlen(encoding) == 0) || (dont_decode)) {
155 if (CallBack != NULL) {
156 CallBack(name, filename, fixed_partnum(partnum),
157 disposition, part_start,
158 content_type, length, encoding, userdata);
162 if ((strcasecmp(encoding, "base64"))
163 && (strcasecmp(encoding, "quoted-printable"))) {
164 lprintf(5, "ERROR: unknown MIME encoding '%s'\n", encoding);
168 * Allocate a buffer for the decoded data. The output buffer is the
169 * same size as the input buffer; this assumes that the decoded data
170 * will never be larger than the encoded data. This is a safe
171 * assumption with base64, uuencode, and quoted-printable. Just to
172 * be safe, we still pad the buffer a bit.
174 decoded = mallok(length + 1024);
175 if (decoded == NULL) {
176 lprintf(5, "ERROR: cannot allocate memory.\n");
179 if (pipe(sendpipe) != 0)
181 if (pipe(recvpipe) != 0)
191 /* send stdio to the pipes */
192 if (dup2(sendpipe[0], 0) < 0)
193 lprintf(5, "ERROR dup2()\n");
194 if (dup2(recvpipe[1], 1) < 0)
195 lprintf(5, "ERROR dup2()\n");
196 close(sendpipe[1]); /* Close the ends we're not using */
198 if (!strcasecmp(encoding, "base64"))
199 execlp("./base64", "base64", "-d", NULL);
200 else if (!strcasecmp(encoding, "quoted-printable"))
201 execlp("./qpdecode", "qpdecode", NULL);
202 lprintf(5, "ERROR: cannot exec decoder for %s\n", encoding);
205 close(sendpipe[0]); /* Close the ends we're not using */
208 while ((bytes_sent < length) && (write_error == 0)) {
209 /* Empty the input pipe FIRST */
210 while (fstat(recvpipe[0], &statbuf), (statbuf.st_size > 0)) {
211 blocksize = read(recvpipe[0], &decoded[bytes_recv],
214 lprintf(5, "ERROR: cannot read from pipe\n");
216 bytes_recv = bytes_recv + blocksize;
218 /* Then put some data into the output pipe */
219 blocksize = length - bytes_sent;
220 if (blocksize > 2048)
222 if (write(sendpipe[1], &part_start[bytes_sent], blocksize) < 0) {
223 lprintf(5, "ERROR: cannot write to pipe: %s\n",
227 bytes_sent = bytes_sent + blocksize;
230 /* Empty the input pipe */
231 while ((blocksize = read(recvpipe[0], &decoded[bytes_recv], 1)),
233 bytes_recv = bytes_recv + blocksize;
236 if (bytes_recv > 0) if (CallBack != NULL) {
237 CallBack(name, filename, fixed_partnum(partnum),
238 disposition, decoded,
239 content_type, bytes_recv, "binary", userdata);
246 * Break out the components of a multipart message
247 * (This function expects to be fed HEADERS + CONTENT)
248 * Note: NULL can be supplied as content_end; in this case, the message is
249 * considered to have ended when the parser encounters a 0x00 byte.
251 void the_mime_parser(char *partnum,
252 char *content_start, char *content_end,
263 void (*PreMultiPartCallBack)
273 void (*PostMultiPartCallBack)
289 char *part_start, *part_end;
295 char content_type[SIZ];
297 char disposition[SIZ];
304 char nested_partnum[SIZ];
306 lprintf(9, "the_mime_parser() called\n");
308 memset(boundary, 0, sizeof boundary);
309 memset(content_type, 0, sizeof content_type);
310 memset(encoding, 0, sizeof encoding);
311 memset(name, 0, sizeof name);
312 memset(filename, 0, sizeof filename);
313 memset(disposition, 0, sizeof disposition);
315 /* Learn interesting things from the headers */
318 ptr = memreadline(ptr, buf, sizeof buf);
320 return; /* premature end of message */
321 if (content_end != NULL)
322 if (ptr >= content_end)
325 for (i = 0; i < strlen(buf); ++i)
328 if (!isspace(buf[0])) {
329 if (!strncasecmp(header, "Content-type: ", 14)) {
330 strcpy(content_type, &header[14]);
331 extract_key(name, content_type, "name");
333 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
334 strcpy(disposition, &header[21]);
335 extract_key(filename, disposition, "filename");
337 if (!strncasecmp(header,
338 "Content-transfer-encoding: ", 27))
339 strcpy(encoding, &header[27]);
340 if (strlen(boundary) == 0)
341 extract_key(boundary, header, "boundary");
344 if ((strlen(header) + strlen(buf) + 2) < sizeof(header))
346 } while ((strlen(buf) > 0) && (*ptr != 0));
348 for (i = 0; i < strlen(disposition); ++i)
349 if (disposition[i] == ';')
351 while (isspace(disposition[0]))
352 strcpy(disposition, &disposition[1]);
353 for (i = 0; i < strlen(content_type); ++i)
354 if (content_type[i] == ';')
356 while (isspace(content_type[0]))
357 strcpy(content_type, &content_type[1]);
359 if (strlen(boundary) > 0) {
365 /* If this is a multipart message, then recursively process it */
369 /* Tell the client about this message's multipartedness */
370 if (PreMultiPartCallBack != NULL) {
371 PreMultiPartCallBack("", "", partnum, "",
373 0, encoding, userdata);
376 if (CallBack != NULL) {
377 CallBack("", "", fixed_partnum(partnum),
378 "", NULL, content_type,
379 0, encoding, userdata);
383 /* Figure out where the boundaries are */
384 sprintf(startary, "--%s", boundary);
385 sprintf(endary, "--%s--", boundary);
388 ptr = memreadline(ptr, buf, sizeof buf);
389 if (content_end != NULL)
390 if (ptr >= content_end) goto END_MULTI;
392 if ( (!strcasecmp(buf, startary))
393 || (!strcasecmp(buf, endary)) ) {
394 if (part_start != NULL) {
395 if (strlen(partnum) > 0) {
396 sprintf(nested_partnum, "%s.%d",
397 partnum, ++part_seq);
400 sprintf(nested_partnum, "%d",
403 the_mime_parser(nested_partnum,
404 part_start, part_end,
406 PreMultiPartCallBack,
407 PostMultiPartCallBack,
413 } while ( (strcasecmp(buf, endary)) && (ptr != 0) );
414 END_MULTI: if (PostMultiPartCallBack != NULL) {
415 PostMultiPartCallBack("", "", partnum, "", NULL,
416 content_type, 0, encoding, userdata);
421 /* If it's not a multipart message, then do something with it */
426 && ((content_end == NULL) || (ptr < content_end))) {
432 content_type, encoding, disposition,
434 CallBack, NULL, NULL,
435 userdata, dont_decode);
444 * Entry point for the MIME parser.
445 * (This function expects to be fed HEADERS + CONTENT)
446 * Note: NULL can be supplied as content_end; in this case, the message is
447 * considered to have ended when the parser encounters a 0x00 byte.
449 void mime_parser(char *content_start,
463 void (*PreMultiPartCallBack)
474 void (*PostMultiPartCallBack)
490 lprintf(9, "mime_parser() called\n");
491 the_mime_parser("", content_start, content_end,
493 PreMultiPartCallBack,
494 PostMultiPartCallBack,
495 userdata, dont_decode);