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 "serv_extensions.h"
24 #include "sysdep_decls.h"
27 #include "mime_parser.h"
30 void extract_key(char *target, char *source, char *key)
34 strcpy(target, source);
35 for (a = 0; a < strlen(target); ++a) {
36 if ((!strncasecmp(&target[a], key, strlen(key)))
37 && (target[a + strlen(key)] == '=')) {
38 strcpy(target, &target[a + strlen(key) + 1]);
40 strcpy(target, &target[1]);
41 for (b = 0; b < strlen(target); ++b)
52 * For non-multipart messages, we need to generate a quickie partnum of "1"
53 * to return to callback functions. Some callbacks demand it.
55 char *fixed_partnum(char *supplied_partnum) {
56 if (supplied_partnum == NULL) return "1";
57 if (strlen(supplied_partnum)==0) return "1";
58 return supplied_partnum;
64 * Convert "quoted-printable" to binary. Returns number of bytes decoded.
66 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
69 int soft_line_break = 0;
71 int decoded_length = 0;
76 memset(buf, '\0', SIZ);
79 for (i = 0; i < sourcelen; ++i) {
80 size_t buflen = strlen(buf);
81 buf[buf_length++] = encoded[i];
83 if ( (encoded[i] == '\n')
85 || (i == (sourcelen-1)) ) {
86 buf[buf_length++] = 0;
88 /*** begin -- process one line ***/
90 if (buf[buflen-1] == '\n') {
94 if (buf[buflen-1] == '\r') {
98 while (isspace(buf[buflen-1])) {
105 if ((buf[0] == '=') && (buf[1] == '\0')) {
109 } else if ((buflen>=3) && (buf[0]=='=')) {
110 sscanf(&buf[1], "%02x", &ch);
111 decoded[decoded_length++] = ch;
112 strcpy(buf, &buf[3]);
115 decoded[decoded_length++] = buf[0];
116 strcpy(buf, &buf[1]);
120 if (soft_line_break == 0) {
121 decoded[decoded_length++] = '\r';
122 decoded[decoded_length++] = '\n';
125 /*** end -- process one line ***/
129 decoded[decoded_length++] = 0;
130 return(decoded_length);
134 * Given a message or message-part body and a length, handle any necessary
135 * decoding and pass the request up the stack.
137 void mime_decode(char *partnum,
138 char *part_start, size_t length,
139 char *content_type, char *charset, char *encoding,
141 char *name, char *filename,
153 void (*PreMultiPartCallBack)
164 void (*PostMultiPartCallBack)
181 size_t bytes_decoded = 0;
183 /* Some encodings aren't really encodings */
184 if (!strcasecmp(encoding, "7bit"))
185 strcpy(encoding, "");
186 if (!strcasecmp(encoding, "8bit"))
187 strcpy(encoding, "");
188 if (!strcasecmp(encoding, "binary"))
189 strcpy(encoding, "");
191 /* If this part is not encoded, send as-is */
192 if ( (strlen(encoding) == 0) || (dont_decode)) {
193 if (CallBack != NULL) {
194 CallBack(name, filename, fixed_partnum(partnum),
195 disposition, part_start,
196 content_type, charset, length, encoding, userdata);
201 /* Fail silently if we hit an unknown encoding. */
202 if ((strcasecmp(encoding, "base64"))
203 && (strcasecmp(encoding, "quoted-printable"))) {
208 * Allocate a buffer for the decoded data. The output buffer is slightly
209 * larger than the input buffer; this assumes that the decoded data
210 * will never be significantly larger than the encoded data. This is a
211 * safe assumption with base64, uuencode, and quoted-printable.
213 decoded = malloc(length + 32768);
214 if (decoded == NULL) {
218 if (!strcasecmp(encoding, "base64")) {
219 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
221 else if (!strcasecmp(encoding, "quoted-printable")) {
222 bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
225 if (bytes_decoded > 0) if (CallBack != NULL) {
226 CallBack(name, filename, fixed_partnum(partnum),
227 disposition, decoded,
228 content_type, charset, bytes_decoded, "binary", userdata);
235 * Break out the components of a multipart message
236 * (This function expects to be fed HEADERS + CONTENT)
237 * Note: NULL can be supplied as content_end; in this case, the message is
238 * considered to have ended when the parser encounters a 0x00 byte.
240 void the_mime_parser(char *partnum,
241 char *content_start, char *content_end,
253 void (*PreMultiPartCallBack)
264 void (*PostMultiPartCallBack)
282 char *part_start, *part_end = NULL;
287 size_t startary_len = 0;
292 size_t content_length;
296 char *content_type_name;
297 char *content_disposition_name;
303 char nested_partnum[SIZ];
308 boundary = malloc(SIZ);
309 memset(boundary, 0, SIZ);
311 startary = malloc(SIZ);
312 memset(startary, 0, SIZ);
314 endary = malloc(SIZ);
315 memset(endary, 0, SIZ);
317 header = malloc(SIZ);
318 memset(header, 0, SIZ);
320 content_type = malloc(SIZ);
321 memset(content_type, 0, SIZ);
323 charset = malloc(SIZ);
324 memset(charset, 0, SIZ);
326 encoding = malloc(SIZ);
327 memset(encoding, 0, SIZ);
329 content_type_name = malloc(SIZ);
330 memset(content_type_name, 0, SIZ);
332 content_disposition_name = malloc(SIZ);
333 memset(content_disposition_name, 0, SIZ);
335 filename = malloc(SIZ);
336 memset(filename, 0, SIZ);
338 disposition = malloc(SIZ);
339 memset(disposition, 0, SIZ);
341 /* If the caller didn't supply an endpointer, generate one by measure */
342 if (content_end == NULL) {
343 content_end = &content_start[strlen(content_start)];
346 /* Learn interesting things from the headers */
349 ptr = memreadline(ptr, buf, SIZ);
350 if (ptr >= content_end) {
354 for (i = 0; i < strlen(buf); ++i) {
355 if (isspace(buf[i])) {
360 if (!isspace(buf[0])) {
361 if (!strncasecmp(header, "Content-type: ", 14)) {
362 strcpy(content_type, &header[14]);
363 extract_key(content_type_name, content_type, "name");
364 extract_key(charset, content_type, "charset");
365 /* Deal with weird headers */
366 if (strchr(content_type, ' '))
367 *(strchr(content_type, ' ')) = '\0';
368 if (strchr(content_type, ';'))
369 *(strchr(content_type, ';')) = '\0';
371 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
372 strcpy(disposition, &header[21]);
373 extract_key(content_disposition_name, disposition, "name");
374 extract_key(filename, disposition, "filename");
376 if (!strncasecmp(header, "Content-length: ", 16)) {
377 content_length = (size_t) atol(&header[16]);
379 if (!strncasecmp(header,
380 "Content-transfer-encoding: ", 27))
381 strcpy(encoding, &header[27]);
382 if (strlen(boundary) == 0)
383 extract_key(boundary, header, "boundary");
386 if ((strlen(header) + strlen(buf) + 2) < SIZ)
388 } while ((strlen(buf) > 0) && (*ptr != 0));
390 if (strchr(disposition, ';'))
391 *(strchr(disposition, ';')) = '\0';
392 striplt(disposition);
393 if (strchr(content_type, ';'))
394 *(strchr(content_type, ';')) = '\0';
395 striplt(content_type);
397 if (strlen(boundary) > 0) {
403 /* If this is a multipart message, then recursively process it */
407 /* Tell the client about this message's multipartedness */
408 if (PreMultiPartCallBack != NULL) {
409 PreMultiPartCallBack("", "", partnum, "",
410 NULL, content_type, charset,
411 0, encoding, userdata);
414 /* Figure out where the boundaries are */
415 snprintf(startary, SIZ, "--%s", boundary);
416 snprintf(endary, SIZ, "--%s--", boundary);
417 startary_len = strlen(startary);
421 next_boundary = NULL;
422 for (srch=ptr; srch<content_end; ++srch) {
423 if (!memcmp(srch, startary, startary_len)) {
424 next_boundary = srch;
429 if ( (part_start != NULL) && (next_boundary != NULL) ) {
430 part_end = next_boundary;
433 if (strlen(partnum) > 0) {
434 snprintf(nested_partnum,
435 sizeof nested_partnum,
440 snprintf(nested_partnum,
441 sizeof nested_partnum,
444 the_mime_parser(nested_partnum,
445 part_start, part_end,
447 PreMultiPartCallBack,
448 PostMultiPartCallBack,
453 if (next_boundary != NULL) {
454 /* If we pass out of scope, don't attempt to
455 * read past the end boundary. */
456 if (!strcmp(next_boundary, endary)) {
460 /* Set up for the next part. */
461 part_start = strstr(next_boundary, "\n");
467 /* Invalid end of multipart. Bail out! */
470 } while ( (ptr < content_end) && (next_boundary != NULL) );
472 if (PostMultiPartCallBack != NULL) {
473 PostMultiPartCallBack("", "", partnum, "", NULL,
474 content_type, charset, 0, encoding, userdata);
479 /* If it's not a multipart message, then do something with it */
483 while (ptr < content_end) {
487 part_end = content_end;
488 /* fix an off-by-one error */
492 /* Truncate if the header told us to */
493 if ( (content_length > 0) && (length > content_length) ) {
494 length = content_length;
497 /* Sometimes the "name" field is tacked on to Content-type,
498 * and sometimes it's tacked on to Content-disposition. Use
499 * whichever one we have.
501 if (strlen(content_disposition_name) > strlen(content_type_name)) {
502 name = content_disposition_name;
505 name = content_type_name;
508 /* lprintf(CTDL_DEBUG, "mime_decode part=%s, len=%d, type=%s, charset=%s, encoding=%s\n",
509 partnum, length, content_type, charset, encoding); */
511 /* Ok, we've got a non-multipart part here, so do something with it.
515 content_type, charset, encoding, disposition,
517 CallBack, NULL, NULL,
518 userdata, dont_decode
522 * Now if it's an encapsulated message/rfc822 then we have to recurse into it
524 if (!strcasecmp(content_type, "message/rfc822")) {
526 if (PreMultiPartCallBack != NULL) {
527 PreMultiPartCallBack("", "", partnum, "",
528 NULL, content_type, charset,
529 0, encoding, userdata);
531 if (CallBack != NULL) {
532 if (strlen(partnum) > 0) {
533 snprintf(nested_partnum,
534 sizeof nested_partnum,
539 snprintf(nested_partnum,
540 sizeof nested_partnum,
543 the_mime_parser(nested_partnum,
544 part_start, part_end,
546 PreMultiPartCallBack,
547 PostMultiPartCallBack,
552 if (PostMultiPartCallBack != NULL) {
553 PostMultiPartCallBack("", "", partnum, "", NULL,
554 content_type, charset, 0, encoding, userdata);
562 end_parser: /* free the buffers! end the oppression!! */
570 free(content_type_name);
571 free(content_disposition_name);
579 * Entry point for the MIME parser.
580 * (This function expects to be fed HEADERS + CONTENT)
581 * Note: NULL can be supplied as content_end; in this case, the message is
582 * considered to have ended when the parser encounters a 0x00 byte.
584 void mime_parser(char *content_start,
599 void (*PreMultiPartCallBack)
611 void (*PostMultiPartCallBack)
628 the_mime_parser("", content_start, content_end,
630 PreMultiPartCallBack,
631 PostMultiPartCallBack,
632 userdata, dont_decode);