4 * This is the MIME parser for Citadel.
6 * Copyright (c) 1998-2006 by Art Cancro
7 * This code is distributed under the GNU General Public License v2.
15 #include <sys/types.h>
23 #include "sysdep_decls.h"
26 #include "mime_parser.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)
51 * For non-multipart messages, we need to generate a quickie partnum of "1"
52 * to return to callback functions. Some callbacks demand it.
54 char *fixed_partnum(char *supplied_partnum) {
55 if (supplied_partnum == NULL) return "1";
56 if (strlen(supplied_partnum)==0) return "1";
57 return supplied_partnum;
63 * Convert "quoted-printable" to binary. Returns number of bytes decoded.
64 * according to RFC2045 section 6.7
66 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
68 int decoded_length = 0;
71 while (pos < sourcelen)
73 if (!strncmp(&encoded[pos], "=\r\n", 3))
77 else if (!strncmp(&encoded[pos], "=\n", 2))
81 else if (encoded[pos] == '=')
84 sscanf(&encoded[pos+1], "%02x", &ch);
86 decoded[decoded_length++] = ch;
90 decoded[decoded_length++] = encoded[pos];
94 decoded[decoded_length] = 0;
95 return(decoded_length);
100 * Given a message or message-part body and a length, handle any necessary
101 * decoding and pass the request up the stack.
103 void mime_decode(char *partnum,
104 char *part_start, size_t length,
105 char *content_type, char *charset, char *encoding,
107 char *name, char *filename,
119 void (*PreMultiPartCallBack)
130 void (*PostMultiPartCallBack)
147 size_t bytes_decoded = 0;
149 /* Some encodings aren't really encodings */
150 if (!strcasecmp(encoding, "7bit"))
151 strcpy(encoding, "");
152 if (!strcasecmp(encoding, "8bit"))
153 strcpy(encoding, "");
154 if (!strcasecmp(encoding, "binary"))
155 strcpy(encoding, "");
157 /* If this part is not encoded, send as-is */
158 if ( (strlen(encoding) == 0) || (dont_decode)) {
159 if (CallBack != NULL) {
160 CallBack(name, filename, fixed_partnum(partnum),
161 disposition, part_start,
162 content_type, charset, length, encoding, userdata);
167 /* Fail silently if we hit an unknown encoding. */
168 if ((strcasecmp(encoding, "base64"))
169 && (strcasecmp(encoding, "quoted-printable"))) {
174 * Allocate a buffer for the decoded data. The output buffer is slightly
175 * larger than the input buffer; this assumes that the decoded data
176 * will never be significantly larger than the encoded data. This is a
177 * safe assumption with base64, uuencode, and quoted-printable.
179 decoded = malloc(length + 32768);
180 if (decoded == NULL) {
184 if (!strcasecmp(encoding, "base64")) {
185 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
187 else if (!strcasecmp(encoding, "quoted-printable")) {
188 bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
191 if (bytes_decoded > 0) if (CallBack != NULL) {
192 CallBack(name, filename, fixed_partnum(partnum),
193 disposition, decoded,
194 content_type, charset, bytes_decoded, "binary", userdata);
201 * Break out the components of a multipart message
202 * (This function expects to be fed HEADERS + CONTENT)
203 * Note: NULL can be supplied as content_end; in this case, the message is
204 * considered to have ended when the parser encounters a 0x00 byte.
206 void the_mime_parser(char *partnum,
207 char *content_start, char *content_end,
219 void (*PreMultiPartCallBack)
230 void (*PostMultiPartCallBack)
248 char *part_start, *part_end = NULL;
253 size_t startary_len = 0;
258 size_t content_length;
262 char *content_type_name;
263 char *content_disposition_name;
269 char nested_partnum[256];
271 char *evaluate_crlf_ptr = NULL;
276 boundary = malloc(SIZ);
277 memset(boundary, 0, SIZ);
279 startary = malloc(SIZ);
280 memset(startary, 0, SIZ);
282 endary = malloc(SIZ);
283 memset(endary, 0, SIZ);
285 header = malloc(SIZ);
286 memset(header, 0, SIZ);
288 content_type = malloc(SIZ);
289 memset(content_type, 0, SIZ);
291 charset = malloc(SIZ);
292 memset(charset, 0, SIZ);
294 encoding = malloc(SIZ);
295 memset(encoding, 0, SIZ);
297 content_type_name = malloc(SIZ);
298 memset(content_type_name, 0, SIZ);
300 content_disposition_name = malloc(SIZ);
301 memset(content_disposition_name, 0, SIZ);
303 filename = malloc(SIZ);
304 memset(filename, 0, SIZ);
306 disposition = malloc(SIZ);
307 memset(disposition, 0, SIZ);
309 /* If the caller didn't supply an endpointer, generate one by measure */
310 if (content_end == NULL) {
311 content_end = &content_start[strlen(content_start)];
314 /* Learn interesting things from the headers */
317 ptr = memreadline(ptr, buf, SIZ);
318 if (ptr >= content_end) {
322 for (i = 0; i < strlen(buf); ++i) {
323 if (isspace(buf[i])) {
328 if (!isspace(buf[0])) {
329 if (!strncasecmp(header, "Content-type:", 13)) {
330 strcpy(content_type, &header[13]);
331 striplt(content_type);
332 extract_key(content_type_name, content_type, "name");
333 extract_key(charset, content_type, "charset");
334 /* Deal with weird headers */
335 if (strchr(content_type, ' '))
336 *(strchr(content_type, ' ')) = '\0';
337 if (strchr(content_type, ';'))
338 *(strchr(content_type, ';')) = '\0';
340 if (!strncasecmp(header, "Content-Disposition:", 20)) {
341 strcpy(disposition, &header[20]);
342 striplt(disposition);
343 extract_key(content_disposition_name, disposition, "name");
344 extract_key(filename, disposition, "filename");
346 if (!strncasecmp(header, "Content-length: ", 15)) {
348 safestrncpy(clbuf, &header[15], sizeof clbuf);
350 content_length = (size_t) atol(clbuf);
352 if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
353 strcpy(encoding, &header[26]);
356 if (strlen(boundary) == 0)
357 extract_key(boundary, header, "boundary");
360 if ((strlen(header) + strlen(buf) + 2) < SIZ) {
363 } while ((strlen(buf) > 0) && (*ptr != 0));
365 if (strchr(disposition, ';'))
366 *(strchr(disposition, ';')) = '\0';
367 striplt(disposition);
368 if (strchr(content_type, ';'))
369 *(strchr(content_type, ';')) = '\0';
370 striplt(content_type);
372 if (strlen(boundary) > 0) {
378 /* If this is a multipart message, then recursively process it */
382 /* Tell the client about this message's multipartedness */
383 if (PreMultiPartCallBack != NULL) {
384 PreMultiPartCallBack("", "", partnum, "",
385 NULL, content_type, charset,
386 0, encoding, userdata);
389 /* Figure out where the boundaries are */
390 snprintf(startary, SIZ, "--%s", boundary);
391 snprintf(endary, SIZ, "--%s--", boundary);
392 startary_len = strlen(startary);
396 next_boundary = NULL;
397 for (srch=ptr; srch<content_end; ++srch) {
398 if (!memcmp(srch, startary, startary_len)) {
399 next_boundary = srch;
404 if ( (part_start != NULL) && (next_boundary != NULL) ) {
405 part_end = next_boundary;
406 --part_end; /* omit the trailing LF */
408 --part_end; /* omit the trailing CR */
411 if (strlen(partnum) > 0) {
412 snprintf(nested_partnum,
413 sizeof nested_partnum,
418 snprintf(nested_partnum,
419 sizeof nested_partnum,
422 the_mime_parser(nested_partnum,
423 part_start, part_end,
425 PreMultiPartCallBack,
426 PostMultiPartCallBack,
431 if (next_boundary != NULL) {
432 /* If we pass out of scope, don't attempt to
433 * read past the end boundary. */
434 if (!strcmp(next_boundary, endary)) {
438 /* Set up for the next part. */
439 part_start = strstr(next_boundary, "\n");
441 /* Determine whether newlines are LF or CRLF */
442 evaluate_crlf_ptr = part_start;
444 if (!memcmp(evaluate_crlf_ptr, "\r\n", 2)) {
451 /* Advance past the LF ... now we're in the next part */
457 /* Invalid end of multipart. Bail out! */
460 } while ( (ptr < content_end) && (next_boundary != NULL) );
462 if (PostMultiPartCallBack != NULL) {
463 PostMultiPartCallBack("", "", partnum, "", NULL,
464 content_type, charset, 0, encoding, userdata);
469 /* If it's not a multipart message, then do something with it */
473 while (ptr < content_end) {
477 part_end = content_end;
480 * I thought there was an off-by-one error here, but there isn't.
481 * This probably means that there's an off-by-one error somewhere
482 * else ... or maybe only in certain messages?
487 /* Truncate if the header told us to */
488 if ( (content_length > 0) && (length > content_length) ) {
489 length = content_length;
492 /* Sometimes the "name" field is tacked on to Content-type,
493 * and sometimes it's tacked on to Content-disposition. Use
494 * whichever one we have.
496 if (strlen(content_disposition_name) > strlen(content_type_name)) {
497 name = content_disposition_name;
500 name = content_type_name;
503 /* lprintf(CTDL_DEBUG, "mime_decode part=%s, len=%d, type=%s, charset=%s, encoding=%s\n",
504 partnum, length, content_type, charset, encoding); */
506 /* Ok, we've got a non-multipart part here, so do something with it.
510 content_type, charset, encoding, disposition,
512 CallBack, NULL, NULL,
513 userdata, dont_decode
517 * Now if it's an encapsulated message/rfc822 then we have to recurse into it
519 if (!strcasecmp(content_type, "message/rfc822")) {
521 if (PreMultiPartCallBack != NULL) {
522 PreMultiPartCallBack("", "", partnum, "",
523 NULL, content_type, charset,
524 0, encoding, userdata);
526 if (CallBack != NULL) {
527 if (strlen(partnum) > 0) {
528 snprintf(nested_partnum,
529 sizeof nested_partnum,
534 snprintf(nested_partnum,
535 sizeof nested_partnum,
538 the_mime_parser(nested_partnum,
539 part_start, part_end,
541 PreMultiPartCallBack,
542 PostMultiPartCallBack,
547 if (PostMultiPartCallBack != NULL) {
548 PostMultiPartCallBack("", "", partnum, "", NULL,
549 content_type, charset, 0, encoding, userdata);
557 end_parser: /* free the buffers! end the oppression!! */
565 free(content_type_name);
566 free(content_disposition_name);
574 * Entry point for the MIME parser.
575 * (This function expects to be fed HEADERS + CONTENT)
576 * Note: NULL can be supplied as content_end; in this case, the message is
577 * considered to have ended when the parser encounters a 0x00 byte.
579 void mime_parser(char *content_start,
594 void (*PreMultiPartCallBack)
606 void (*PostMultiPartCallBack)
623 the_mime_parser("", content_start, content_end,
625 PreMultiPartCallBack,
626 PostMultiPartCallBack,
627 userdata, dont_decode);