4 * This is the MIME parser for Citadel.
6 * Copyright (c) 1998-2007 by the citadel.org development team.
7 * This code is distributed under the GNU General Public License v3.
15 #include <sys/types.h>
21 #include "libcitadel.h"
23 void extract_key(char *target, char *source, char *key)
26 char looking_for[256];
27 int double_quotes = 0;
29 snprintf(looking_for, sizeof looking_for, "%s=", key);
31 ptr = bmstrcasestr(source, looking_for);
36 strcpy(target, (ptr + strlen(looking_for)));
38 for (ptr=target; (*ptr != 0); ++ptr) {
40 /* A semicolon means we've hit the end of the key, unless we're inside double quotes */
41 if ( (double_quotes != 1) && (*ptr == ';')) {
45 /* if we find double quotes, we've got a great set of string boundaries */
48 if (double_quotes == 1) {
60 * For non-multipart messages, we need to generate a quickie partnum of "1"
61 * to return to callback functions. Some callbacks demand it.
63 char *fixed_partnum(char *supplied_partnum) {
64 if (supplied_partnum == NULL) return "1";
65 if (strlen(supplied_partnum)==0) return "1";
66 return supplied_partnum;
72 * Convert "quoted-printable" to binary. Returns number of bytes decoded.
73 * according to RFC2045 section 6.7
75 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
77 int decoded_length = 0;
80 while (pos < sourcelen)
82 if (!strncmp(&encoded[pos], "=\r\n", 3))
86 else if (!strncmp(&encoded[pos], "=\n", 2))
90 else if (encoded[pos] == '=')
93 sscanf(&encoded[pos+1], "%02x", &ch);
95 decoded[decoded_length++] = ch;
99 decoded[decoded_length++] = encoded[pos];
103 decoded[decoded_length] = 0;
104 return(decoded_length);
109 * Given a message or message-part body and a length, handle any necessary
110 * decoding and pass the request up the stack.
112 void mime_decode(char *partnum,
113 char *part_start, size_t length,
114 char *content_type, char *charset, char *encoding,
116 char *name, char *filename,
128 void (*PreMultiPartCallBack)
139 void (*PostMultiPartCallBack)
156 size_t bytes_decoded = 0;
158 /* Some encodings aren't really encodings */
159 if (!strcasecmp(encoding, "7bit"))
160 strcpy(encoding, "");
161 if (!strcasecmp(encoding, "8bit"))
162 strcpy(encoding, "");
163 if (!strcasecmp(encoding, "binary"))
164 strcpy(encoding, "");
166 /* If this part is not encoded, send as-is */
167 if ( (strlen(encoding) == 0) || (dont_decode)) {
168 if (CallBack != NULL) {
169 CallBack(name, filename, fixed_partnum(partnum),
170 disposition, part_start,
171 content_type, charset, length, encoding, userdata);
176 /* Fail silently if we hit an unknown encoding. */
177 if ((strcasecmp(encoding, "base64"))
178 && (strcasecmp(encoding, "quoted-printable"))) {
183 * Allocate a buffer for the decoded data. The output buffer is slightly
184 * larger than the input buffer; this assumes that the decoded data
185 * will never be significantly larger than the encoded data. This is a
186 * safe assumption with base64, uuencode, and quoted-printable.
188 decoded = malloc(length + 32768);
189 if (decoded == NULL) {
193 if (!strcasecmp(encoding, "base64")) {
194 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
196 else if (!strcasecmp(encoding, "quoted-printable")) {
197 bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
200 if (bytes_decoded > 0) if (CallBack != NULL) {
201 CallBack(name, filename, fixed_partnum(partnum),
202 disposition, decoded,
203 content_type, charset, bytes_decoded, "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,
228 void (*PreMultiPartCallBack)
239 void (*PostMultiPartCallBack)
257 char *part_start, *part_end = NULL;
262 size_t startary_len = 0;
267 size_t content_length;
271 char *content_type_name;
272 char *content_disposition_name;
278 char nested_partnum[256];
280 char *evaluate_crlf_ptr = NULL;
287 boundary = malloc(SIZ);
288 memset(boundary, 0, SIZ);
290 startary = malloc(SIZ);
291 memset(startary, 0, SIZ);
293 endary = malloc(SIZ);
294 memset(endary, 0, SIZ);
296 header = malloc(SIZ);
297 memset(header, 0, SIZ);
299 content_type = malloc(SIZ);
300 memset(content_type, 0, SIZ);
302 charset = malloc(SIZ);
303 memset(charset, 0, SIZ);
305 encoding = malloc(SIZ);
306 memset(encoding, 0, SIZ);
308 content_type_name = malloc(SIZ);
309 memset(content_type_name, 0, SIZ);
311 content_disposition_name = malloc(SIZ);
312 memset(content_disposition_name, 0, SIZ);
314 filename = malloc(SIZ);
315 memset(filename, 0, SIZ);
317 disposition = malloc(SIZ);
318 memset(disposition, 0, SIZ);
320 /* If the caller didn't supply an endpointer, generate one by measure */
321 if (content_end == NULL) {
322 content_end = &content_start[strlen(content_start)];
325 /* Learn interesting things from the headers */
329 ptr = memreadlinelen(ptr, buf, SIZ, &buflen);
330 if (ptr >= content_end) {
334 for (i = 0; i < buflen; ++i) {
335 if (isspace(buf[i])) {
340 if (!isspace(buf[0])) {
341 if (!strncasecmp(header, "Content-type:", 13)) {
342 strcpy(content_type, &header[13]);
343 striplt(content_type);
344 extract_key(content_type_name, content_type, "name");
345 extract_key(charset, content_type, "charset");
346 extract_key(boundary, header, "boundary");
347 /* Deal with weird headers */
348 if (strchr(content_type, ' '))
349 *(strchr(content_type, ' ')) = '\0';
350 if (strchr(content_type, ';'))
351 *(strchr(content_type, ';')) = '\0';
353 if (!strncasecmp(header, "Content-Disposition:", 20)) {
354 strcpy(disposition, &header[20]);
355 striplt(disposition);
356 extract_key(content_disposition_name, disposition, "name");
357 extract_key(filename, disposition, "filename");
359 if (!strncasecmp(header, "Content-length: ", 15)) {
361 safestrncpy(clbuf, &header[15], sizeof clbuf);
363 content_length = (size_t) atol(clbuf);
365 if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
366 strcpy(encoding, &header[26]);
372 if ((headerlen + buflen + 2) < SIZ) {
373 memcpy(&header[headerlen], buf, buflen);
375 header[headerlen] = '\0';
377 } while ((!IsEmptyStr(buf)) && (*ptr != 0));
379 if (strchr(disposition, ';'))
380 *(strchr(disposition, ';')) = '\0';
381 striplt(disposition);
382 if (strchr(content_type, ';'))
383 *(strchr(content_type, ';')) = '\0';
384 striplt(content_type);
386 if (!IsEmptyStr(boundary)) {
392 /* If this is a multipart message, then recursively process it */
396 /* Tell the client about this message's multipartedness */
397 if (PreMultiPartCallBack != NULL) {
398 PreMultiPartCallBack("", "", partnum, "",
399 NULL, content_type, charset,
400 0, encoding, userdata);
403 /* Figure out where the boundaries are */
404 snprintf(startary, SIZ, "--%s", boundary);
405 snprintf(endary, SIZ, "--%s--", boundary);
406 startary_len = strlen(startary);
410 next_boundary = NULL;
411 for (srch=ptr; srch<content_end; ++srch) {
412 if (!memcmp(srch, startary, startary_len)) {
413 next_boundary = srch;
418 if ( (part_start != NULL) && (next_boundary != NULL) ) {
419 part_end = next_boundary;
420 --part_end; /* omit the trailing LF */
422 --part_end; /* omit the trailing CR */
425 if (!IsEmptyStr(partnum)) {
426 snprintf(nested_partnum,
427 sizeof nested_partnum,
432 snprintf(nested_partnum,
433 sizeof nested_partnum,
436 the_mime_parser(nested_partnum,
437 part_start, part_end,
439 PreMultiPartCallBack,
440 PostMultiPartCallBack,
445 if (next_boundary != NULL) {
446 /* If we pass out of scope, don't attempt to
447 * read past the end boundary. */
448 if (!strcmp(next_boundary, endary)) {
452 /* Set up for the next part. */
453 part_start = strstr(next_boundary, "\n");
455 /* Determine whether newlines are LF or CRLF */
456 evaluate_crlf_ptr = part_start;
458 if (!memcmp(evaluate_crlf_ptr, "\r\n", 2)) {
465 /* Advance past the LF ... now we're in the next part */
471 /* Invalid end of multipart. Bail out! */
474 } while ( (ptr < content_end) && (next_boundary != NULL) );
476 if (PostMultiPartCallBack != NULL) {
477 PostMultiPartCallBack("", "", partnum, "", NULL,
478 content_type, charset, 0, encoding, userdata);
483 /* If it's not a multipart message, then do something with it */
487 while (ptr < content_end) {
491 part_end = content_end;
494 * I thought there was an off-by-one error here, but there isn't.
495 * This probably means that there's an off-by-one error somewhere
496 * else ... or maybe only in certain messages?
501 /* Truncate if the header told us to */
502 if ( (content_length > 0) && (length > content_length) ) {
503 length = content_length;
506 /* Sometimes the "name" field is tacked on to Content-type,
507 * and sometimes it's tacked on to Content-disposition. Use
508 * whichever one we have.
510 if (strlen(content_disposition_name) > strlen(content_type_name)) {
511 name = content_disposition_name;
514 name = content_type_name;
517 /* Ok, we've got a non-multipart part here, so do something with it.
521 content_type, charset, encoding, disposition,
523 CallBack, NULL, NULL,
524 userdata, dont_decode
528 * Now if it's an encapsulated message/rfc822 then we have to recurse into it
530 if (!strcasecmp(content_type, "message/rfc822")) {
532 if (PreMultiPartCallBack != NULL) {
533 PreMultiPartCallBack("", "", partnum, "",
534 NULL, content_type, charset,
535 0, encoding, userdata);
537 if (CallBack != NULL) {
538 if (strlen(partnum) > 0) {
539 snprintf(nested_partnum,
540 sizeof nested_partnum,
545 snprintf(nested_partnum,
546 sizeof nested_partnum,
549 the_mime_parser(nested_partnum,
550 part_start, part_end,
552 PreMultiPartCallBack,
553 PostMultiPartCallBack,
558 if (PostMultiPartCallBack != NULL) {
559 PostMultiPartCallBack("", "", partnum, "", NULL,
560 content_type, charset, 0, encoding, userdata);
568 end_parser: /* free the buffers! end the oppression!! */
576 free(content_type_name);
577 free(content_disposition_name);
585 * Entry point for the MIME parser.
586 * (This function expects to be fed HEADERS + CONTENT)
587 * Note: NULL can be supplied as content_end; in this case, the message is
588 * considered to have ended when the parser encounters a 0x00 byte.
590 void mime_parser(char *content_start,
605 void (*PreMultiPartCallBack)
617 void (*PostMultiPartCallBack)
634 the_mime_parser("", content_start, content_end,
636 PreMultiPartCallBack,
637 PostMultiPartCallBack,
638 userdata, dont_decode);