4 * This is the MIME parser for Citadel. Sometimes it actually works.
6 * Copyright (c) 1998-2005 by Art Cancro
7 * This code is distributed under the terms of the GNU General Public License.
19 #include <sys/types.h>
27 #include "serv_extensions.h"
28 #include "sysdep_decls.h"
31 #include "mime_parser.h"
34 void extract_key(char *target, char *source, char *key)
38 strcpy(target, source);
39 for (a = 0; a < strlen(target); ++a) {
40 if ((!strncasecmp(&target[a], key, strlen(key)))
41 && (target[a + strlen(key)] == '=')) {
42 strcpy(target, &target[a + strlen(key) + 1]);
44 strcpy(target, &target[1]);
45 for (b = 0; b < strlen(target); ++b)
56 * For non-multipart messages, we need to generate a quickie partnum of "1"
57 * to return to callback functions. Some callbacks demand it.
59 char *fixed_partnum(char *supplied_partnum) {
60 if (supplied_partnum == NULL) return "1";
61 if (strlen(supplied_partnum)==0) return "1";
62 return supplied_partnum;
68 * Convert "quoted-printable" to binary. Returns number of bytes decoded.
70 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
73 int soft_line_break = 0;
75 int decoded_length = 0;
83 for (i = 0; i < sourcelen; ++i) {
85 buf[buf_length++] = encoded[i];
87 if ( (encoded[i] == '\n')
89 || (i == (sourcelen-1)) ) {
90 buf[buf_length++] = 0;
92 /*** begin -- process one line ***/
94 if (buf[strlen(buf)-1] == '\n') {
95 buf[strlen(buf)-1] = 0;
97 if (buf[strlen(buf)-1] == '\r') {
98 buf[strlen(buf)-1] = 0;
100 while (isspace(buf[strlen(buf)-1])) {
101 buf[strlen(buf)-1] = 0;
105 while (strlen(buf) > 0) {
106 if (!strcmp(buf, "=")) {
109 } else if ((strlen(buf)>=3) && (buf[0]=='=')) {
110 sscanf(&buf[1], "%02x", &ch);
111 decoded[decoded_length++] = ch;
112 strcpy(buf, &buf[3]);
114 decoded[decoded_length++] = buf[0];
115 strcpy(buf, &buf[1]);
118 if (soft_line_break == 0) {
119 decoded[decoded_length++] = '\r';
120 decoded[decoded_length++] = '\n';
123 /*** end -- process one line ***/
127 decoded[decoded_length++] = 0;
128 return(decoded_length);
132 * Given a message or message-part body and a length, handle any necessary
133 * decoding and pass the request up the stack.
135 void mime_decode(char *partnum,
136 char *part_start, size_t length,
137 char *content_type, char *charset, char *encoding,
139 char *name, char *filename,
151 void (*PreMultiPartCallBack)
162 void (*PostMultiPartCallBack)
179 size_t bytes_decoded = 0;
181 /* Some encodings aren't really encodings */
182 if (!strcasecmp(encoding, "7bit"))
183 strcpy(encoding, "");
184 if (!strcasecmp(encoding, "8bit"))
185 strcpy(encoding, "");
186 if (!strcasecmp(encoding, "binary"))
187 strcpy(encoding, "");
189 /* If this part is not encoded, send as-is */
190 if ( (strlen(encoding) == 0) || (dont_decode)) {
191 if (CallBack != NULL) {
192 CallBack(name, filename, fixed_partnum(partnum),
193 disposition, part_start,
194 content_type, charset, length, encoding, userdata);
199 if ((strcasecmp(encoding, "base64"))
200 && (strcasecmp(encoding, "quoted-printable"))) {
204 * Allocate a buffer for the decoded data. The output buffer is the
205 * same size as the input buffer; this assumes that the decoded data
206 * will never be larger than the encoded data. This is a safe
207 * assumption with base64, uuencode, and quoted-printable.
209 decoded = malloc(length+2048);
210 if (decoded == NULL) {
214 if (!strcasecmp(encoding, "base64")) {
215 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
217 else if (!strcasecmp(encoding, "quoted-printable")) {
218 bytes_decoded = CtdlDecodeQuotedPrintable(decoded,
222 if (bytes_decoded > 0) if (CallBack != NULL) {
223 CallBack(name, filename, fixed_partnum(partnum),
224 disposition, decoded,
225 content_type, charset, bytes_decoded, "binary", userdata);
232 * Break out the components of a multipart message
233 * (This function expects to be fed HEADERS + CONTENT)
234 * Note: NULL can be supplied as content_end; in this case, the message is
235 * considered to have ended when the parser encounters a 0x00 byte.
237 void the_mime_parser(char *partnum,
238 char *content_start, char *content_end,
250 void (*PreMultiPartCallBack)
261 void (*PostMultiPartCallBack)
279 char *part_start, *part_end = NULL;
284 size_t startary_len = 0;
289 size_t content_length;
293 char *content_type_name;
294 char *content_disposition_name;
300 char nested_partnum[SIZ];
305 boundary = malloc(SIZ);
306 memset(boundary, 0, SIZ);
308 startary = malloc(SIZ);
309 memset(startary, 0, SIZ);
311 endary = malloc(SIZ);
312 memset(endary, 0, SIZ);
314 header = malloc(SIZ);
315 memset(header, 0, SIZ);
317 content_type = malloc(SIZ);
318 memset(content_type, 0, SIZ);
320 charset = malloc(SIZ);
321 memset(charset, 0, SIZ);
323 encoding = malloc(SIZ);
324 memset(encoding, 0, SIZ);
326 content_type_name = malloc(SIZ);
327 memset(content_type_name, 0, SIZ);
329 content_disposition_name = malloc(SIZ);
330 memset(content_disposition_name, 0, SIZ);
332 filename = malloc(SIZ);
333 memset(filename, 0, SIZ);
335 disposition = malloc(SIZ);
336 memset(disposition, 0, SIZ);
338 /* If the caller didn't supply an endpointer, generate one by measure */
339 if (content_end == NULL) {
340 content_end = &content_start[strlen(content_start)];
343 /* Learn interesting things from the headers */
346 ptr = memreadline(ptr, buf, SIZ);
347 if (ptr >= content_end) {
351 for (i = 0; i < strlen(buf); ++i) {
352 if (isspace(buf[i])) {
357 if (!isspace(buf[0])) {
358 if (!strncasecmp(header, "Content-type: ", 14)) {
359 strcpy(content_type, &header[14]);
360 extract_key(content_type_name, content_type, "name");
361 extract_key(charset, content_type, "charset");
362 /* Deal with weird headers */
363 if (strchr(content_type, ' '))
364 *(strchr(content_type, ' ')) = '\0';
365 if (strchr(content_type, ';'))
366 *(strchr(content_type, ';')) = '\0';
368 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
369 strcpy(disposition, &header[21]);
370 extract_key(content_disposition_name, disposition, "name");
371 extract_key(filename, disposition, "filename");
373 if (!strncasecmp(header, "Content-length: ", 16)) {
374 content_length = (size_t) atol(&header[16]);
376 if (!strncasecmp(header,
377 "Content-transfer-encoding: ", 27))
378 strcpy(encoding, &header[27]);
379 if (strlen(boundary) == 0)
380 extract_key(boundary, header, "boundary");
383 if ((strlen(header) + strlen(buf) + 2) < SIZ)
385 } while ((strlen(buf) > 0) && (*ptr != 0));
387 if (strchr(disposition, ';'))
388 *(strchr(disposition, ';')) = '\0';
389 striplt(disposition);
390 if (strchr(content_type, ';'))
391 *(strchr(content_type, ';')) = '\0';
392 striplt(content_type);
394 if (strlen(boundary) > 0) {
400 /* If this is a multipart message, then recursively process it */
404 /* Tell the client about this message's multipartedness */
405 if (PreMultiPartCallBack != NULL) {
406 PreMultiPartCallBack("", "", partnum, "",
407 NULL, content_type, charset,
408 0, encoding, userdata);
411 /* Figure out where the boundaries are */
412 snprintf(startary, SIZ, "--%s", boundary);
413 snprintf(endary, SIZ, "--%s--", boundary);
414 startary_len = strlen(startary);
418 next_boundary = NULL;
419 for (srch=ptr; srch<content_end; ++srch) {
420 if (!memcmp(srch, startary, startary_len)) {
421 next_boundary = srch;
426 if ( (part_start != NULL) && (next_boundary != NULL) ) {
427 part_end = next_boundary;
430 if (strlen(partnum) > 0) {
431 snprintf(nested_partnum,
432 sizeof nested_partnum,
437 snprintf(nested_partnum,
438 sizeof nested_partnum,
441 the_mime_parser(nested_partnum,
442 part_start, part_end,
444 PreMultiPartCallBack,
445 PostMultiPartCallBack,
450 if (next_boundary != NULL) {
451 /* If we pass out of scope, don't attempt to
452 * read past the end boundary. */
453 if (!strcmp(next_boundary, endary)) {
457 /* Set up for the next part. */
458 part_start = strstr(next_boundary, "\n");
464 /* Invalid end of multipart. Bail out! */
467 } while ( (ptr < content_end) && (next_boundary != NULL) );
469 if (PostMultiPartCallBack != NULL) {
470 PostMultiPartCallBack("", "", partnum, "", NULL,
471 content_type, charset, 0, encoding, userdata);
476 /* If it's not a multipart message, then do something with it */
480 while (ptr < content_end) {
484 part_end = content_end;
485 /* fix an off-by-one error */
489 /* Truncate if the header told us to */
490 if ( (content_length > 0) && (length > content_length) ) {
491 length = content_length;
494 /* Sometimes the "name" field is tacked on to Content-type,
495 * and sometimes it's tacked on to Content-disposition. Use
496 * whichever one we have.
498 if (strlen(content_disposition_name) > strlen(content_type_name)) {
499 name = content_disposition_name;
502 name = content_type_name;
505 /* lprintf(CTDL_DEBUG, "mime_decode part=%s, len=%d, type=%s, charset=%s, encoding=%s\n",
506 partnum, length, content_type, charset, encoding); */
509 content_type, charset, encoding, disposition,
511 CallBack, NULL, NULL,
512 userdata, dont_decode);
515 end_parser: /* free the buffers! end the oppression!! */
523 free(content_type_name);
524 free(content_disposition_name);
532 * Entry point for the MIME parser.
533 * (This function expects to be fed HEADERS + CONTENT)
534 * Note: NULL can be supplied as content_end; in this case, the message is
535 * considered to have ended when the parser encounters a 0x00 byte.
537 void mime_parser(char *content_start,
552 void (*PreMultiPartCallBack)
564 void (*PostMultiPartCallBack)
581 the_mime_parser("", content_start, content_end,
583 PreMultiPartCallBack,
584 PostMultiPartCallBack,
585 userdata, dont_decode);