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,
107 size_t bytes_sent = 0;
108 size_t bytes_recv = 0;
112 lprintf(9, "mime_decode() called\n");
114 /* Some encodings aren't really encodings */
115 if (!strcasecmp(encoding, "7bit"))
116 strcpy(encoding, "");
117 if (!strcasecmp(encoding, "8bit"))
118 strcpy(encoding, "");
119 if (!strcasecmp(encoding, "binary"))
120 strcpy(encoding, "");
122 /* If this part is not encoded, send as-is */
123 if ( (strlen(encoding) == 0) || (dont_decode)) {
124 CallBack(name, filename, partnum, disposition, part_start,
125 content_type, length, encoding, userdata);
128 if ((strcasecmp(encoding, "base64"))
129 && (strcasecmp(encoding, "quoted-printable"))) {
130 lprintf(5, "ERROR: unknown MIME encoding '%s'\n", encoding);
134 * Allocate a buffer for the decoded data. The output buffer is the
135 * same size as the input buffer; this assumes that the decoded data
136 * will never be larger than the encoded data. This is a safe
137 * assumption with base64, uuencode, and quoted-printable. Just to
138 * be safe, we still pad the buffer a bit.
140 decoded = mallok(length + 1024);
141 if (decoded == NULL) {
142 lprintf(5, "ERROR: cannot allocate memory.\n");
145 if (pipe(sendpipe) != 0)
147 if (pipe(recvpipe) != 0)
157 /* send stdio to the pipes */
158 if (dup2(sendpipe[0], 0) < 0)
159 lprintf(5, "ERROR dup2()\n");
160 if (dup2(recvpipe[1], 1) < 0)
161 lprintf(5, "ERROR dup2()\n");
162 close(sendpipe[1]); /* Close the ends we're not using */
164 if (!strcasecmp(encoding, "base64"))
165 execlp("./base64", "base64", "-d", NULL);
166 else if (!strcasecmp(encoding, "quoted-printable"))
167 execlp("./qpdecode", "qpdecode", NULL);
168 lprintf(5, "ERROR: cannot exec decoder for %s\n", encoding);
171 close(sendpipe[0]); /* Close the ends we're not using */
174 while ((bytes_sent < length) && (write_error == 0)) {
175 /* Empty the input pipe FIRST */
176 while (fstat(recvpipe[0], &statbuf), (statbuf.st_size > 0)) {
177 blocksize = read(recvpipe[0], &decoded[bytes_recv],
180 lprintf(5, "ERROR: cannot read from pipe\n");
182 bytes_recv = bytes_recv + blocksize;
184 /* Then put some data into the output pipe */
185 blocksize = length - bytes_sent;
186 if (blocksize > 2048)
188 if (write(sendpipe[1], &part_start[bytes_sent], blocksize) < 0) {
189 lprintf(5, "ERROR: cannot write to pipe: %s\n",
193 bytes_sent = bytes_sent + blocksize;
196 /* Empty the input pipe */
197 while ((blocksize = read(recvpipe[0], &decoded[bytes_recv], 1)),
199 bytes_recv = bytes_recv + blocksize;
203 CallBack(name, filename, partnum, disposition, decoded,
204 content_type, bytes_recv, "binary", userdata);
210 * Break out the components of a multipart message
211 * (This function expects to be fed HEADERS + CONTENT)
212 * Note: NULL can be supplied as content_end; in this case, the message is
213 * considered to have ended when the parser encounters a 0x00 byte.
215 void the_mime_parser(char *partnum,
216 char *content_start, char *content_end,
233 char *part_start, *part_end;
239 char content_type[SIZ];
241 char disposition[SIZ];
248 char nested_partnum[SIZ];
250 lprintf(9, "the_mime_parser() called\n");
252 memset(boundary, 0, sizeof boundary);
253 memset(content_type, 0, sizeof content_type);
254 memset(encoding, 0, sizeof encoding);
255 memset(name, 0, sizeof name);
256 memset(filename, 0, sizeof filename);
257 memset(disposition, 0, sizeof disposition);
259 /* Learn interesting things from the headers */
262 ptr = memreadline(ptr, buf, sizeof buf);
264 return; /* premature end of message */
265 if (content_end != NULL)
266 if (ptr >= content_end)
269 for (i = 0; i < strlen(buf); ++i)
272 if (!isspace(buf[0])) {
273 if (!strncasecmp(header, "Content-type: ", 14)) {
274 strcpy(content_type, &header[14]);
275 extract_key(name, content_type, "name");
277 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
278 strcpy(disposition, &header[21]);
279 extract_key(filename, disposition, "filename");
281 if (!strncasecmp(header,
282 "Content-transfer-encoding: ", 27))
283 strcpy(encoding, &header[27]);
284 if (strlen(boundary) == 0)
285 extract_key(boundary, header, "boundary");
288 if ((strlen(header) + strlen(buf) + 2) < sizeof(header))
290 } while ((strlen(buf) > 0) && (*ptr != 0));
292 for (i = 0; i < strlen(disposition); ++i)
293 if (disposition[i] == ';')
295 while (isspace(disposition[0]))
296 strcpy(disposition, &disposition[1]);
297 for (i = 0; i < strlen(content_type); ++i)
298 if (content_type[i] == ';')
300 while (isspace(content_type[0]))
301 strcpy(content_type, &content_type[1]);
303 if (strlen(boundary) > 0) {
309 /* If this is a multipart message, then recursively process it */
313 /* Tell the client about this message's multipartedness */
314 CallBack("", "", partnum, "", NULL, content_type, 0,
317 /* Figure out where the boundaries are */
318 sprintf(startary, "--%s", boundary);
319 sprintf(endary, "--%s--", boundary);
322 ptr = memreadline(ptr, buf, sizeof buf);
324 return; /* premature end of message */
325 if (content_end != NULL)
326 if (ptr >= content_end)
328 if ((!strcasecmp(buf, startary))
329 || (!strcasecmp(buf, endary))) {
330 if (part_start != NULL) {
331 if (strlen(partnum) > 0) {
332 sprintf(nested_partnum, "%s.%d",
333 partnum, ++part_seq);
336 sprintf(nested_partnum, "%d",
339 the_mime_parser(nested_partnum,
340 part_start, part_end,
346 } while (strcasecmp(buf, endary));
349 /* If it's not a multipart message, then do something with it */
354 && ((content_end == NULL) || (ptr < content_end))) {
360 content_type, encoding, disposition,
361 name, filename, CallBack, userdata, dont_decode);
370 * Entry point for the MIME parser.
371 * (This function expects to be fed HEADERS + CONTENT)
372 * Note: NULL can be supplied as content_end; in this case, the message is
373 * considered to have ended when the parser encounters a 0x00 byte.
375 void mime_parser(char *content_start, char *content_end,
391 lprintf(9, "mime_parser() called\n");
392 the_mime_parser("", content_start, content_end, CallBack,
393 userdata, dont_decode);