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,
105 size_t bytes_sent = 0;
106 size_t bytes_recv = 0;
110 lprintf(9, "mime_decode() called\n");
112 /* Some encodings aren't really encodings */
113 if (!strcasecmp(encoding, "7bit"))
114 strcpy(encoding, "");
115 if (!strcasecmp(encoding, "8bit"))
116 strcpy(encoding, "");
117 if (!strcasecmp(encoding, "binary"))
118 strcpy(encoding, "");
120 /* If this part is not encoded, send as-is */
121 if (strlen(encoding) == 0) {
122 CallBack(name, filename, partnum, disposition, part_start,
123 content_type, length, userdata);
126 if ((strcasecmp(encoding, "base64"))
127 && (strcasecmp(encoding, "quoted-printable"))) {
128 lprintf(5, "ERROR: unknown MIME encoding '%s'\n", encoding);
132 * Allocate a buffer for the decoded data. The output buffer is the
133 * same size as the input buffer; this assumes that the decoded data
134 * will never be larger than the encoded data. This is a safe
135 * assumption with base64, uuencode, and quoted-printable. Just to
136 * be safe, we still pad the buffer a bit.
138 decoded = mallok(length + 1024);
139 if (decoded == NULL) {
140 lprintf(5, "ERROR: cannot allocate memory.\n");
143 if (pipe(sendpipe) != 0)
145 if (pipe(recvpipe) != 0)
155 /* send stdio to the pipes */
156 if (dup2(sendpipe[0], 0) < 0)
157 lprintf(5, "ERROR dup2()\n");
158 if (dup2(recvpipe[1], 1) < 0)
159 lprintf(5, "ERROR dup2()\n");
160 close(sendpipe[1]); /* Close the ends we're not using */
162 if (!strcasecmp(encoding, "base64"))
163 execlp("./base64", "base64", "-d", NULL);
164 else if (!strcasecmp(encoding, "quoted-printable"))
165 execlp("./qpdecode", "qpdecode", NULL);
166 lprintf(5, "ERROR: cannot exec decoder for %s\n", encoding);
169 close(sendpipe[0]); /* Close the ends we're not using */
172 while ((bytes_sent < length) && (write_error == 0)) {
173 /* Empty the input pipe FIRST */
174 while (fstat(recvpipe[0], &statbuf), (statbuf.st_size > 0)) {
175 blocksize = read(recvpipe[0], &decoded[bytes_recv],
178 lprintf(5, "ERROR: cannot read from pipe\n");
180 bytes_recv = bytes_recv + blocksize;
182 /* Then put some data into the output pipe */
183 blocksize = length - bytes_sent;
184 if (blocksize > 2048)
186 if (write(sendpipe[1], &part_start[bytes_sent], blocksize) < 0) {
187 lprintf(5, "ERROR: cannot write to pipe: %s\n",
191 bytes_sent = bytes_sent + blocksize;
194 /* Empty the input pipe */
195 while ((blocksize = read(recvpipe[0], &decoded[bytes_recv], 1)),
197 bytes_recv = bytes_recv + blocksize;
201 CallBack(name, filename, partnum, disposition, decoded,
202 content_type, bytes_recv, userdata);
208 * Break out the components of a multipart message
209 * (This function expects to be fed HEADERS + CONTENT)
210 * Note: NULL can be supplied as content_end; in this case, the message is
211 * considered to have ended when the parser encounters a 0x00 byte.
213 void the_mime_parser(char *partnum,
214 char *content_start, char *content_end,
229 char *part_start, *part_end;
235 char content_type[256];
237 char disposition[256];
244 char nested_partnum[256];
246 lprintf(9, "the_mime_parser() called\n");
248 memset(boundary, 0, sizeof boundary);
249 memset(content_type, 0, sizeof content_type);
250 memset(encoding, 0, sizeof encoding);
251 memset(name, 0, sizeof name);
252 memset(filename, 0, sizeof filename);
253 memset(disposition, 0, sizeof disposition);
255 /* Learn interesting things from the headers */
258 ptr = memreadline(ptr, buf, sizeof buf);
260 return; /* premature end of message */
261 if (content_end != NULL)
262 if (ptr >= content_end)
265 for (i = 0; i < strlen(buf); ++i)
268 if (!isspace(buf[0])) {
269 if (!strncasecmp(header, "Content-type: ", 14)) {
270 strcpy(content_type, &header[14]);
271 extract_key(name, content_type, "name");
273 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
274 strcpy(disposition, &header[21]);
275 extract_key(filename, disposition, "filename");
277 if (!strncasecmp(header,
278 "Content-transfer-encoding: ", 27))
279 strcpy(encoding, &header[27]);
280 if (strlen(boundary) == 0)
281 extract_key(boundary, header, "boundary");
284 if ((strlen(header) + strlen(buf) + 2) < sizeof(header))
286 } while ((strlen(buf) > 0) && (*ptr != 0));
288 for (i = 0; i < strlen(disposition); ++i)
289 if (disposition[i] == ';')
291 while (isspace(disposition[0]))
292 strcpy(disposition, &disposition[1]);
293 for (i = 0; i < strlen(content_type); ++i)
294 if (content_type[i] == ';')
296 while (isspace(content_type[0]))
297 strcpy(content_type, &content_type[1]);
299 if (strlen(boundary) > 0) {
305 /* If this is a multipart message, then recursively process it */
309 /* Tell the client about this message's multipartedness */
310 CallBack("", "", partnum, "", NULL, content_type, 0, userdata);
312 /* Figure out where the boundaries are */
313 sprintf(startary, "--%s", boundary);
314 sprintf(endary, "--%s--", boundary);
317 ptr = memreadline(ptr, buf, sizeof buf);
319 return; /* premature end of message */
320 if (content_end != NULL)
321 if (ptr >= content_end)
323 if ((!strcasecmp(buf, startary))
324 || (!strcasecmp(buf, endary))) {
325 if (part_start != NULL) {
326 if (strlen(partnum) > 0) {
327 sprintf(nested_partnum, "%s.%d",
328 partnum, ++part_seq);
331 sprintf(nested_partnum, "%d",
334 the_mime_parser(nested_partnum,
335 part_start, part_end,
340 } while (strcasecmp(buf, endary));
343 /* If it's not a multipart message, then do something with it */
348 && ((content_end == NULL) || (ptr < content_end))) {
354 content_type, encoding, disposition,
355 name, filename, CallBack, userdata);
364 * Entry point for the MIME parser.
365 * (This function expects to be fed HEADERS + CONTENT)
366 * Note: NULL can be supplied as content_end; in this case, the message is
367 * considered to have ended when the parser encounters a 0x00 byte.
369 void mime_parser(char *content_start, char *content_end,
383 lprintf(9, "mime_parser() called\n");
384 the_mime_parser("", content_start, content_end, CallBack, userdata);