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;
278 boundary = malloc(SIZ);
279 memset(boundary, 0, SIZ);
281 startary = malloc(SIZ);
282 memset(startary, 0, SIZ);
284 endary = malloc(SIZ);
285 memset(endary, 0, SIZ);
287 header = malloc(SIZ);
288 memset(header, 0, SIZ);
290 content_type = malloc(SIZ);
291 memset(content_type, 0, SIZ);
293 charset = malloc(SIZ);
294 memset(charset, 0, SIZ);
296 encoding = malloc(SIZ);
297 memset(encoding, 0, SIZ);
299 content_type_name = malloc(SIZ);
300 memset(content_type_name, 0, SIZ);
302 content_disposition_name = malloc(SIZ);
303 memset(content_disposition_name, 0, SIZ);
305 filename = malloc(SIZ);
306 memset(filename, 0, SIZ);
308 disposition = malloc(SIZ);
309 memset(disposition, 0, SIZ);
311 /* If the caller didn't supply an endpointer, generate one by measure */
312 if (content_end == NULL) {
313 content_end = &content_start[strlen(content_start)];
316 /* Learn interesting things from the headers */
320 ptr = memreadlinelen(ptr, buf, SIZ, &buflen);
321 if (ptr >= content_end) {
325 for (i = 0; i < buflen; ++i) {
326 if (isspace(buf[i])) {
331 if (!isspace(buf[0])) {
332 if (!strncasecmp(header, "Content-type:", 13)) {
333 strcpy(content_type, &header[13]);
334 striplt(content_type);
335 extract_key(content_type_name, content_type, "name");
336 extract_key(charset, content_type, "charset");
337 /* Deal with weird headers */
338 if (strchr(content_type, ' '))
339 *(strchr(content_type, ' ')) = '\0';
340 if (strchr(content_type, ';'))
341 *(strchr(content_type, ';')) = '\0';
343 if (!strncasecmp(header, "Content-Disposition:", 20)) {
344 strcpy(disposition, &header[20]);
345 striplt(disposition);
346 extract_key(content_disposition_name, disposition, "name");
347 extract_key(filename, disposition, "filename");
349 if (!strncasecmp(header, "Content-length: ", 15)) {
351 safestrncpy(clbuf, &header[15], sizeof clbuf);
353 content_length = (size_t) atol(clbuf);
355 if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
356 strcpy(encoding, &header[26]);
359 if (strlen(boundary) == 0)
360 extract_key(boundary, header, "boundary");
364 if ((headerlen + buflen + 2) < SIZ) {
365 memcpy(&header[headerlen], buf, buflen);
367 header[headerlen] = '\0';
369 } while ((!IsEmptyStr(buf)) && (*ptr != 0));
371 if (strchr(disposition, ';'))
372 *(strchr(disposition, ';')) = '\0';
373 striplt(disposition);
374 if (strchr(content_type, ';'))
375 *(strchr(content_type, ';')) = '\0';
376 striplt(content_type);
378 if (!IsEmptyStr(boundary)) {
384 /* If this is a multipart message, then recursively process it */
388 /* Tell the client about this message's multipartedness */
389 if (PreMultiPartCallBack != NULL) {
390 PreMultiPartCallBack("", "", partnum, "",
391 NULL, content_type, charset,
392 0, encoding, userdata);
395 /* Figure out where the boundaries are */
396 snprintf(startary, SIZ, "--%s", boundary);
397 snprintf(endary, SIZ, "--%s--", boundary);
398 startary_len = strlen(startary);
402 next_boundary = NULL;
403 for (srch=ptr; srch<content_end; ++srch) {
404 if (!memcmp(srch, startary, startary_len)) {
405 next_boundary = srch;
410 if ( (part_start != NULL) && (next_boundary != NULL) ) {
411 part_end = next_boundary;
412 --part_end; /* omit the trailing LF */
414 --part_end; /* omit the trailing CR */
417 if (!IsEmptyStr(partnum)) {
418 snprintf(nested_partnum,
419 sizeof nested_partnum,
424 snprintf(nested_partnum,
425 sizeof nested_partnum,
428 the_mime_parser(nested_partnum,
429 part_start, part_end,
431 PreMultiPartCallBack,
432 PostMultiPartCallBack,
437 if (next_boundary != NULL) {
438 /* If we pass out of scope, don't attempt to
439 * read past the end boundary. */
440 if (!strcmp(next_boundary, endary)) {
444 /* Set up for the next part. */
445 part_start = strstr(next_boundary, "\n");
447 /* Determine whether newlines are LF or CRLF */
448 evaluate_crlf_ptr = part_start;
450 if (!memcmp(evaluate_crlf_ptr, "\r\n", 2)) {
457 /* Advance past the LF ... now we're in the next part */
463 /* Invalid end of multipart. Bail out! */
466 } while ( (ptr < content_end) && (next_boundary != NULL) );
468 if (PostMultiPartCallBack != NULL) {
469 PostMultiPartCallBack("", "", partnum, "", NULL,
470 content_type, charset, 0, encoding, userdata);
475 /* If it's not a multipart message, then do something with it */
479 while (ptr < content_end) {
483 part_end = content_end;
486 * I thought there was an off-by-one error here, but there isn't.
487 * This probably means that there's an off-by-one error somewhere
488 * else ... or maybe only in certain messages?
493 /* Truncate if the header told us to */
494 if ( (content_length > 0) && (length > content_length) ) {
495 length = content_length;
498 /* Sometimes the "name" field is tacked on to Content-type,
499 * and sometimes it's tacked on to Content-disposition. Use
500 * whichever one we have.
502 if (strlen(content_disposition_name) > strlen(content_type_name)) {
503 name = content_disposition_name;
506 name = content_type_name;
509 /* lprintf(CTDL_DEBUG, "mime_decode part=%s, len=%d, type=%s, charset=%s, encoding=%s\n",
510 partnum, length, content_type, charset, encoding); */
512 /* Ok, we've got a non-multipart part here, so do something with it.
516 content_type, charset, encoding, disposition,
518 CallBack, NULL, NULL,
519 userdata, dont_decode
523 * Now if it's an encapsulated message/rfc822 then we have to recurse into it
525 if (!strcasecmp(content_type, "message/rfc822")) {
527 if (PreMultiPartCallBack != NULL) {
528 PreMultiPartCallBack("", "", partnum, "",
529 NULL, content_type, charset,
530 0, encoding, userdata);
532 if (CallBack != NULL) {
533 if (strlen(partnum) > 0) {
534 snprintf(nested_partnum,
535 sizeof nested_partnum,
540 snprintf(nested_partnum,
541 sizeof nested_partnum,
544 the_mime_parser(nested_partnum,
545 part_start, part_end,
547 PreMultiPartCallBack,
548 PostMultiPartCallBack,
553 if (PostMultiPartCallBack != NULL) {
554 PostMultiPartCallBack("", "", partnum, "", NULL,
555 content_type, charset, 0, encoding, userdata);
563 end_parser: /* free the buffers! end the oppression!! */
571 free(content_type_name);
572 free(content_disposition_name);
580 * Entry point for the MIME parser.
581 * (This function expects to be fed HEADERS + CONTENT)
582 * Note: NULL can be supplied as content_end; in this case, the message is
583 * considered to have ended when the parser encounters a 0x00 byte.
585 void mime_parser(char *content_start,
600 void (*PreMultiPartCallBack)
612 void (*PostMultiPartCallBack)
629 the_mime_parser("", content_start, content_end,
631 PreMultiPartCallBack,
632 PostMultiPartCallBack,
633 userdata, dont_decode);