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.
19 #include <sys/types.h>
28 #include "mime_parser.h"
29 #include "sysdep_decls.h"
34 void extract_key(char *target, char *source, char *key)
38 strcpy(target, source);
39 for (a = 0; a < strlen(target); ++a) {
40 if ((!strncasecmp(&target[a], key, strlen(key)))
41 && (target[a + strlen(key)] == '=')) {
42 strcpy(target, &target[a + strlen(key) + 1]);
44 strcpy(target, &target[1]);
45 for (b = 0; b < strlen(target); ++b)
57 * Utility function to "readline" from memory
58 * (returns new pointer)
60 char *memreadline(char *start, char *buf, int maxlen)
66 memset(buf, 0, maxlen);
70 if ((ch == 10) || (ch == 0)) {
72 if (buf[strlen(buf) - 1] == 13)
73 buf[strlen(buf) - 1] = 0;
76 if (strlen(buf) < (maxlen - 1)) {
77 buf[strlen(buf) + 1] = 0;
78 buf[strlen(buf)] = ch;
84 * Given a message or message-part body and a length, handle any necessary
85 * decoding and pass the request up the stack.
87 void mime_decode(char *partnum,
88 char *part_start, size_t length,
89 char *content_type, char *encoding,
91 char *name, char *filename,
108 size_t bytes_sent = 0;
109 size_t bytes_recv = 0;
113 lprintf(9, "mime_decode() called\n");
115 /* Some encodings aren't really encodings */
116 if (!strcasecmp(encoding, "7bit"))
117 strcpy(encoding, "");
118 if (!strcasecmp(encoding, "8bit"))
119 strcpy(encoding, "");
120 if (!strcasecmp(encoding, "binary"))
121 strcpy(encoding, "");
123 /* If this part is not encoded, send as-is */
124 if (strlen(encoding) == 0) {
125 CallBack(name, filename, partnum, disposition, part_start,
126 content_type, length);
129 if ((strcasecmp(encoding, "base64"))
130 && (strcasecmp(encoding, "quoted-printable"))) {
131 lprintf(5, "ERROR: unknown MIME encoding '%s'\n", encoding);
135 * Allocate a buffer for the decoded data. The output buffer is the
136 * same size as the input buffer; this assumes that the decoded data
137 * will never be larger than the encoded data. This is a safe
138 * assumption with base64, uuencode, and quoted-printable. Just to
139 * be safe, we still pad the buffer a bit.
141 decoded = mallok(length + 1024);
142 if (decoded == NULL) {
143 lprintf(5, "ERROR: cannot allocate memory.\n");
146 if (pipe(sendpipe) != 0)
148 if (pipe(recvpipe) != 0)
158 /* send stdio to the pipes */
159 if (dup2(sendpipe[0], 0) < 0)
160 lprintf(5, "ERROR dup2()\n");
161 if (dup2(recvpipe[1], 1) < 0)
162 lprintf(5, "ERROR dup2()\n");
163 close(sendpipe[1]); /* Close the ends we're not using */
165 if (!strcasecmp(encoding, "base64"))
166 execlp("./base64", "base64", "-d", NULL);
167 else if (!strcasecmp(encoding, "quoted-printable"))
168 execlp("./qpdecode", "qpdecode", NULL);
169 lprintf(5, "ERROR: cannot exec decoder for %s\n", encoding);
172 close(sendpipe[0]); /* Close the ends we're not using */
175 while ((bytes_sent < length) && (write_error == 0)) {
176 /* Empty the input pipe FIRST */
177 while (fstat(recvpipe[0], &statbuf), (statbuf.st_size > 0)) {
178 blocksize = read(recvpipe[0], &decoded[bytes_recv],
181 lprintf(5, "ERROR: cannot read from pipe\n");
183 bytes_recv = bytes_recv + blocksize;
185 /* Then put some data into the output pipe */
186 blocksize = length - bytes_sent;
187 if (blocksize > 2048)
189 if (write(sendpipe[1], &part_start[bytes_sent], blocksize) < 0) {
190 lprintf(5, "ERROR: cannot write to pipe: %s\n",
194 bytes_sent = bytes_sent + blocksize;
197 /* Empty the input pipe */
198 while ((blocksize = read(recvpipe[0], &decoded[bytes_recv], 1)),
200 bytes_recv = bytes_recv + blocksize;
204 CallBack(name, filename, partnum, disposition, decoded,
205 content_type, bytes_recv);
211 * Break out the components of a multipart message
212 * (This function expects to be fed HEADERS + CONTENT)
213 * Note: NULL can be supplied as content_end; in this case, the message is
214 * considered to have ended when the parser encounters a 0x00 byte.
216 void the_mime_parser(char *partnum,
217 char *content_start, char *content_end,
230 char *part_start, *part_end;
236 char content_type[256];
238 char disposition[256];
245 char nested_partnum[256];
247 lprintf(9, "the_mime_parser() called\n");
249 memset(boundary, 0, sizeof boundary);
250 memset(content_type, 0, sizeof content_type);
251 memset(encoding, 0, sizeof encoding);
252 memset(name, 0, sizeof name);
253 memset(filename, 0, sizeof filename);
254 memset(disposition, 0, sizeof disposition);
256 /* Learn interesting things from the headers */
259 ptr = memreadline(ptr, buf, sizeof buf);
261 return; /* premature end of message */
262 if (content_end != NULL)
263 if (ptr >= content_end)
266 for (i = 0; i < strlen(buf); ++i)
269 if (!isspace(buf[0])) {
270 if (!strncasecmp(header, "Content-type: ", 14)) {
271 strcpy(content_type, &header[14]);
272 extract_key(name, content_type, "name");
274 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
275 strcpy(disposition, &header[21]);
276 extract_key(filename, disposition, "filename");
278 if (!strncasecmp(header,
279 "Content-transfer-encoding: ", 27))
280 strcpy(encoding, &header[27]);
281 if (strlen(boundary) == 0)
282 extract_key(boundary, header, "boundary");
285 if ((strlen(header) + strlen(buf) + 2) < sizeof(header))
287 } while ((strlen(buf) > 0) && (*ptr != 0));
289 for (i = 0; i < strlen(disposition); ++i)
290 if (disposition[i] == ';')
292 while (isspace(disposition[0]))
293 strcpy(disposition, &disposition[1]);
294 for (i = 0; i < strlen(content_type); ++i)
295 if (content_type[i] == ';')
297 while (isspace(content_type[0]))
298 strcpy(content_type, &content_type[1]);
300 if (strlen(boundary) > 0) {
306 /* If this is a multipart message, then recursively process it */
310 /* Tell the client about this message's multipartedness */
311 CallBack("", "", partnum, "", NULL, content_type, 0);
313 /* Figure out where the boundaries are */
314 sprintf(startary, "--%s", boundary);
315 sprintf(endary, "--%s--", boundary);
318 ptr = memreadline(ptr, buf, sizeof buf);
320 return; /* premature end of message */
321 if (content_end != NULL)
322 if (ptr >= content_end)
324 if ((!strcasecmp(buf, startary))
325 || (!strcasecmp(buf, endary))) {
326 if (part_start != NULL) {
327 sprintf(nested_partnum, "%s.%d",
328 partnum, ++part_seq);
329 the_mime_parser(nested_partnum,
330 part_start, part_end,
335 } while (strcasecmp(buf, endary));
338 /* If it's not a multipart message, then do something with it */
343 && ((content_end == NULL) || (ptr < content_end))) {
349 content_type, encoding, disposition,
350 name, filename, CallBack);
359 * Entry point for the MIME parser.
360 * (This function expects to be fed HEADERS + CONTENT)
361 * Note: NULL can be supplied as content_end; in this case, the message is
362 * considered to have ended when the parser encounters a 0x00 byte.
364 void mime_parser(char *content_start, char *content_end,
376 lprintf(9, "mime_parser() called\n");
377 the_mime_parser("1", content_start, content_end, CallBack);