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>
25 #include "mime_parser.h"
26 #include "sysdep_decls.h"
31 void extract_key(char *target, char *source, char *key)
35 strcpy(target, source);
36 for (a = 0; a < strlen(target); ++a) {
37 if ((!strncasecmp(&target[a], key, strlen(key)))
38 && (target[a + strlen(key)] == '=')) {
39 strcpy(target, &target[a + strlen(key) + 1]);
41 strcpy(target, &target[1]);
42 for (b = 0; b < strlen(target); ++b)
54 * Utility function to "readline" from memory
55 * (returns new pointer)
57 char *memreadline(char *start, char *buf, int maxlen)
63 memset(buf, 0, maxlen);
67 if ((ch == 10) || (ch == 0)) {
69 if (buf[strlen(buf) - 1] == 13)
70 buf[strlen(buf) - 1] = 0;
73 if (strlen(buf) < (maxlen - 1)) {
74 buf[strlen(buf) + 1] = 0;
75 buf[strlen(buf)] = ch;
81 * Given a message or message-part body and a length, handle any necessary
82 * decoding and pass the request up the stack.
84 void mime_decode(char *partnum,
85 char *part_start, size_t length,
86 char *content_type, char *encoding,
88 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);
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);
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,
227 char *part_start, *part_end;
233 char content_type[256];
235 char disposition[256];
242 char nested_partnum[256];
244 lprintf(9, "the_mime_parser() called\n");
246 memset(boundary, 0, sizeof boundary);
247 memset(content_type, 0, sizeof content_type);
248 memset(encoding, 0, sizeof encoding);
249 memset(name, 0, sizeof name);
250 memset(filename, 0, sizeof filename);
251 memset(disposition, 0, sizeof disposition);
253 /* Learn interesting things from the headers */
256 ptr = memreadline(ptr, buf, sizeof buf);
258 return; /* premature end of message */
259 if (content_end != NULL)
260 if (ptr >= content_end)
263 for (i = 0; i < strlen(buf); ++i)
266 if (!isspace(buf[0])) {
267 if (!strncasecmp(header, "Content-type: ", 14)) {
268 strcpy(content_type, &header[14]);
269 extract_key(name, content_type, "name");
271 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
272 strcpy(disposition, &header[21]);
273 extract_key(filename, disposition, "filename");
275 if (!strncasecmp(header,
276 "Content-transfer-encoding: ", 27))
277 strcpy(encoding, &header[27]);
278 if (strlen(boundary) == 0)
279 extract_key(boundary, header, "boundary");
282 if ((strlen(header) + strlen(buf) + 2) < sizeof(header))
284 } while ((strlen(buf) > 0) && (*ptr != 0));
286 for (i = 0; i < strlen(disposition); ++i)
287 if (disposition[i] == ';')
289 while (isspace(disposition[0]))
290 strcpy(disposition, &disposition[1]);
291 for (i = 0; i < strlen(content_type); ++i)
292 if (content_type[i] == ';')
294 while (isspace(content_type[0]))
295 strcpy(content_type, &content_type[1]);
297 if (strlen(boundary) > 0) {
303 /* If this is a multipart message, then recursively process it */
307 /* Tell the client about this message's multipartedness */
308 CallBack("", "", partnum, "", NULL, content_type, 0);
310 /* Figure out where the boundaries are */
311 sprintf(startary, "--%s", boundary);
312 sprintf(endary, "--%s--", boundary);
315 ptr = memreadline(ptr, buf, sizeof buf);
317 return; /* premature end of message */
318 if (content_end != NULL)
319 if (ptr >= content_end)
321 if ((!strcasecmp(buf, startary))
322 || (!strcasecmp(buf, endary))) {
323 if (part_start != NULL) {
324 sprintf(nested_partnum, "%s.%d",
325 partnum, ++part_seq);
326 the_mime_parser(nested_partnum,
327 part_start, part_end,
332 } while (strcasecmp(buf, endary));
335 /* If it's not a multipart message, then do something with it */
340 && ((content_end == NULL) || (ptr < content_end))) {
346 content_type, encoding, disposition,
347 name, filename, CallBack);
356 * Entry point for the MIME parser.
357 * (This function expects to be fed HEADERS + CONTENT)
358 * Note: NULL can be supplied as content_end; in this case, the message is
359 * considered to have ended when the parser encounters a 0x00 byte.
361 void mime_parser(char *content_start, char *content_end,
373 lprintf(9, "mime_parser() called\n");
374 the_mime_parser("1", content_start, content_end, CallBack);