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 if (CallBack != NULL) {
145 CallBack(name, filename, partnum,
146 disposition, part_start,
147 content_type, length, encoding, userdata);
151 if ((strcasecmp(encoding, "base64"))
152 && (strcasecmp(encoding, "quoted-printable"))) {
153 lprintf(5, "ERROR: unknown MIME encoding '%s'\n", encoding);
157 * Allocate a buffer for the decoded data. The output buffer is the
158 * same size as the input buffer; this assumes that the decoded data
159 * will never be larger than the encoded data. This is a safe
160 * assumption with base64, uuencode, and quoted-printable. Just to
161 * be safe, we still pad the buffer a bit.
163 decoded = mallok(length + 1024);
164 if (decoded == NULL) {
165 lprintf(5, "ERROR: cannot allocate memory.\n");
168 if (pipe(sendpipe) != 0)
170 if (pipe(recvpipe) != 0)
180 /* send stdio to the pipes */
181 if (dup2(sendpipe[0], 0) < 0)
182 lprintf(5, "ERROR dup2()\n");
183 if (dup2(recvpipe[1], 1) < 0)
184 lprintf(5, "ERROR dup2()\n");
185 close(sendpipe[1]); /* Close the ends we're not using */
187 if (!strcasecmp(encoding, "base64"))
188 execlp("./base64", "base64", "-d", NULL);
189 else if (!strcasecmp(encoding, "quoted-printable"))
190 execlp("./qpdecode", "qpdecode", NULL);
191 lprintf(5, "ERROR: cannot exec decoder for %s\n", encoding);
194 close(sendpipe[0]); /* Close the ends we're not using */
197 while ((bytes_sent < length) && (write_error == 0)) {
198 /* Empty the input pipe FIRST */
199 while (fstat(recvpipe[0], &statbuf), (statbuf.st_size > 0)) {
200 blocksize = read(recvpipe[0], &decoded[bytes_recv],
203 lprintf(5, "ERROR: cannot read from pipe\n");
205 bytes_recv = bytes_recv + blocksize;
207 /* Then put some data into the output pipe */
208 blocksize = length - bytes_sent;
209 if (blocksize > 2048)
211 if (write(sendpipe[1], &part_start[bytes_sent], blocksize) < 0) {
212 lprintf(5, "ERROR: cannot write to pipe: %s\n",
216 bytes_sent = bytes_sent + blocksize;
219 /* Empty the input pipe */
220 while ((blocksize = read(recvpipe[0], &decoded[bytes_recv], 1)),
222 bytes_recv = bytes_recv + blocksize;
225 if (bytes_recv > 0) if (CallBack != NULL) {
226 CallBack(name, filename, partnum, disposition, decoded,
227 content_type, bytes_recv, "binary", userdata);
234 * Break out the components of a multipart message
235 * (This function expects to be fed HEADERS + CONTENT)
236 * Note: NULL can be supplied as content_end; in this case, the message is
237 * considered to have ended when the parser encounters a 0x00 byte.
239 void the_mime_parser(char *partnum,
240 char *content_start, char *content_end,
251 void (*PreMultiPartCallBack)
261 void (*PostMultiPartCallBack)
277 char *part_start, *part_end;
283 char content_type[SIZ];
285 char disposition[SIZ];
292 char nested_partnum[SIZ];
294 lprintf(9, "the_mime_parser() called\n");
296 memset(boundary, 0, sizeof boundary);
297 memset(content_type, 0, sizeof content_type);
298 memset(encoding, 0, sizeof encoding);
299 memset(name, 0, sizeof name);
300 memset(filename, 0, sizeof filename);
301 memset(disposition, 0, sizeof disposition);
303 /* Learn interesting things from the headers */
306 ptr = memreadline(ptr, buf, sizeof buf);
308 return; /* premature end of message */
309 if (content_end != NULL)
310 if (ptr >= content_end)
313 for (i = 0; i < strlen(buf); ++i)
316 if (!isspace(buf[0])) {
317 if (!strncasecmp(header, "Content-type: ", 14)) {
318 strcpy(content_type, &header[14]);
319 extract_key(name, content_type, "name");
321 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
322 strcpy(disposition, &header[21]);
323 extract_key(filename, disposition, "filename");
325 if (!strncasecmp(header,
326 "Content-transfer-encoding: ", 27))
327 strcpy(encoding, &header[27]);
328 if (strlen(boundary) == 0)
329 extract_key(boundary, header, "boundary");
332 if ((strlen(header) + strlen(buf) + 2) < sizeof(header))
334 } while ((strlen(buf) > 0) && (*ptr != 0));
336 for (i = 0; i < strlen(disposition); ++i)
337 if (disposition[i] == ';')
339 while (isspace(disposition[0]))
340 strcpy(disposition, &disposition[1]);
341 for (i = 0; i < strlen(content_type); ++i)
342 if (content_type[i] == ';')
344 while (isspace(content_type[0]))
345 strcpy(content_type, &content_type[1]);
347 if (strlen(boundary) > 0) {
353 /* If this is a multipart message, then recursively process it */
357 /* Tell the client about this message's multipartedness */
358 if (PreMultiPartCallBack != NULL) {
359 PreMultiPartCallBack("", "", partnum, "",
361 0, encoding, userdata);
364 if (CallBack != NULL) {
365 CallBack("", "", partnum, "", NULL, content_type,
366 0, encoding, userdata);
370 /* Figure out where the boundaries are */
371 sprintf(startary, "--%s", boundary);
372 sprintf(endary, "--%s--", boundary);
375 ptr = memreadline(ptr, buf, sizeof buf);
376 if (*ptr == 0) goto END_MULTI; /* premature end */
377 if (content_end != NULL)
378 if (ptr >= content_end) goto END_MULTI;
379 if ((!strcasecmp(buf, startary))
380 || (!strcasecmp(buf, endary))) {
381 if (part_start != NULL) {
382 if (strlen(partnum) > 0) {
383 sprintf(nested_partnum, "%s.%d",
384 partnum, ++part_seq);
387 sprintf(nested_partnum, "%d",
390 the_mime_parser(nested_partnum,
391 part_start, part_end,
393 PreMultiPartCallBack,
394 PostMultiPartCallBack,
400 } while (strcasecmp(buf, endary));
401 END_MULTI: if (PostMultiPartCallBack != NULL) {
402 PostMultiPartCallBack("", "", partnum, "", NULL,
403 content_type, 0, encoding, userdata);
408 /* If it's not a multipart message, then do something with it */
413 && ((content_end == NULL) || (ptr < content_end))) {
419 content_type, encoding, disposition,
421 CallBack, NULL, NULL,
422 userdata, dont_decode);
431 * Entry point for the MIME parser.
432 * (This function expects to be fed HEADERS + CONTENT)
433 * Note: NULL can be supplied as content_end; in this case, the message is
434 * considered to have ended when the parser encounters a 0x00 byte.
436 void mime_parser(char *content_start,
450 void (*PreMultiPartCallBack)
461 void (*PostMultiPartCallBack)
477 lprintf(9, "mime_parser() called\n");
478 the_mime_parser("", content_start, content_end,
480 PreMultiPartCallBack,
481 PostMultiPartCallBack,
482 userdata, dont_decode);