4 * This is the MIME parser for Citadel.
6 * Copyright (c) 1998-2005 by Art Cancro
7 * This code is distributed under the terms of the GNU General Public License.
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;
79 for (i = 0; i < sourcelen; ++i) {
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[strlen(buf)-1] == '\n') {
91 buf[strlen(buf)-1] = 0;
93 if (buf[strlen(buf)-1] == '\r') {
94 buf[strlen(buf)-1] = 0;
96 while (isspace(buf[strlen(buf)-1])) {
97 buf[strlen(buf)-1] = 0;
101 while (strlen(buf) > 0) {
102 if (!strcmp(buf, "=")) {
105 } else if ((strlen(buf)>=3) && (buf[0]=='=')) {
106 sscanf(&buf[1], "%02x", &ch);
107 decoded[decoded_length++] = ch;
108 strcpy(buf, &buf[3]);
110 decoded[decoded_length++] = buf[0];
111 strcpy(buf, &buf[1]);
114 if (soft_line_break == 0) {
115 decoded[decoded_length++] = '\r';
116 decoded[decoded_length++] = '\n';
119 /*** end -- process one line ***/
123 decoded[decoded_length++] = 0;
124 return(decoded_length);
128 * Given a message or message-part body and a length, handle any necessary
129 * decoding and pass the request up the stack.
131 void mime_decode(char *partnum,
132 char *part_start, size_t length,
133 char *content_type, char *charset, char *encoding,
135 char *name, char *filename,
147 void (*PreMultiPartCallBack)
158 void (*PostMultiPartCallBack)
175 size_t bytes_decoded = 0;
177 /* Some encodings aren't really encodings */
178 if (!strcasecmp(encoding, "7bit"))
179 strcpy(encoding, "");
180 if (!strcasecmp(encoding, "8bit"))
181 strcpy(encoding, "");
182 if (!strcasecmp(encoding, "binary"))
183 strcpy(encoding, "");
185 /* If this part is not encoded, send as-is */
186 if ( (strlen(encoding) == 0) || (dont_decode)) {
187 if (CallBack != NULL) {
188 CallBack(name, filename, fixed_partnum(partnum),
189 disposition, part_start,
190 content_type, charset, length, encoding, userdata);
195 if ((strcasecmp(encoding, "base64"))
196 && (strcasecmp(encoding, "quoted-printable"))) {
200 * Allocate a buffer for the decoded data. The output buffer is the
201 * same size as the input buffer; this assumes that the decoded data
202 * will never be larger than the encoded data. This is a safe
203 * assumption with base64, uuencode, and quoted-printable.
205 decoded = malloc(length+2048);
206 if (decoded == NULL) {
210 if (!strcasecmp(encoding, "base64")) {
211 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
213 else if (!strcasecmp(encoding, "quoted-printable")) {
214 bytes_decoded = CtdlDecodeQuotedPrintable(decoded,
218 if (bytes_decoded > 0) if (CallBack != NULL) {
219 CallBack(name, filename, fixed_partnum(partnum),
220 disposition, decoded,
221 content_type, charset, bytes_decoded, "binary", userdata);
228 * Break out the components of a multipart message
229 * (This function expects to be fed HEADERS + CONTENT)
230 * Note: NULL can be supplied as content_end; in this case, the message is
231 * considered to have ended when the parser encounters a 0x00 byte.
233 void the_mime_parser(char *partnum,
234 char *content_start, char *content_end,
246 void (*PreMultiPartCallBack)
257 void (*PostMultiPartCallBack)
275 char *part_start, *part_end = NULL;
280 size_t startary_len = 0;
285 size_t content_length;
289 char *content_type_name;
290 char *content_disposition_name;
296 char nested_partnum[SIZ];
301 boundary = malloc(SIZ);
302 memset(boundary, 0, SIZ);
304 startary = malloc(SIZ);
305 memset(startary, 0, SIZ);
307 endary = malloc(SIZ);
308 memset(endary, 0, SIZ);
310 header = malloc(SIZ);
311 memset(header, 0, SIZ);
313 content_type = malloc(SIZ);
314 memset(content_type, 0, SIZ);
316 charset = malloc(SIZ);
317 memset(charset, 0, SIZ);
319 encoding = malloc(SIZ);
320 memset(encoding, 0, SIZ);
322 content_type_name = malloc(SIZ);
323 memset(content_type_name, 0, SIZ);
325 content_disposition_name = malloc(SIZ);
326 memset(content_disposition_name, 0, SIZ);
328 filename = malloc(SIZ);
329 memset(filename, 0, SIZ);
331 disposition = malloc(SIZ);
332 memset(disposition, 0, SIZ);
334 /* If the caller didn't supply an endpointer, generate one by measure */
335 if (content_end == NULL) {
336 content_end = &content_start[strlen(content_start)];
339 /* Learn interesting things from the headers */
342 ptr = memreadline(ptr, buf, SIZ);
343 if (ptr >= content_end) {
347 for (i = 0; i < strlen(buf); ++i) {
348 if (isspace(buf[i])) {
353 if (!isspace(buf[0])) {
354 if (!strncasecmp(header, "Content-type: ", 14)) {
355 strcpy(content_type, &header[14]);
356 extract_key(content_type_name, content_type, "name");
357 extract_key(charset, content_type, "charset");
358 /* Deal with weird headers */
359 if (strchr(content_type, ' '))
360 *(strchr(content_type, ' ')) = '\0';
361 if (strchr(content_type, ';'))
362 *(strchr(content_type, ';')) = '\0';
364 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
365 strcpy(disposition, &header[21]);
366 extract_key(content_disposition_name, disposition, "name");
367 extract_key(filename, disposition, "filename");
369 if (!strncasecmp(header, "Content-length: ", 16)) {
370 content_length = (size_t) atol(&header[16]);
372 if (!strncasecmp(header,
373 "Content-transfer-encoding: ", 27))
374 strcpy(encoding, &header[27]);
375 if (strlen(boundary) == 0)
376 extract_key(boundary, header, "boundary");
379 if ((strlen(header) + strlen(buf) + 2) < SIZ)
381 } while ((strlen(buf) > 0) && (*ptr != 0));
383 if (strchr(disposition, ';'))
384 *(strchr(disposition, ';')) = '\0';
385 striplt(disposition);
386 if (strchr(content_type, ';'))
387 *(strchr(content_type, ';')) = '\0';
388 striplt(content_type);
390 if (strlen(boundary) > 0) {
396 /* If this is a multipart message, then recursively process it */
400 /* Tell the client about this message's multipartedness */
401 if (PreMultiPartCallBack != NULL) {
402 PreMultiPartCallBack("", "", partnum, "",
403 NULL, content_type, charset,
404 0, encoding, userdata);
407 /* Figure out where the boundaries are */
408 snprintf(startary, SIZ, "--%s", boundary);
409 snprintf(endary, SIZ, "--%s--", boundary);
410 startary_len = strlen(startary);
414 next_boundary = NULL;
415 for (srch=ptr; srch<content_end; ++srch) {
416 if (!memcmp(srch, startary, startary_len)) {
417 next_boundary = srch;
422 if ( (part_start != NULL) && (next_boundary != NULL) ) {
423 part_end = next_boundary;
426 if (strlen(partnum) > 0) {
427 snprintf(nested_partnum,
428 sizeof nested_partnum,
433 snprintf(nested_partnum,
434 sizeof nested_partnum,
437 the_mime_parser(nested_partnum,
438 part_start, part_end,
440 PreMultiPartCallBack,
441 PostMultiPartCallBack,
446 if (next_boundary != NULL) {
447 /* If we pass out of scope, don't attempt to
448 * read past the end boundary. */
449 if (!strcmp(next_boundary, endary)) {
453 /* Set up for the next part. */
454 part_start = strstr(next_boundary, "\n");
460 /* Invalid end of multipart. Bail out! */
463 } while ( (ptr < content_end) && (next_boundary != NULL) );
465 if (PostMultiPartCallBack != NULL) {
466 PostMultiPartCallBack("", "", partnum, "", NULL,
467 content_type, charset, 0, encoding, userdata);
472 /* If it's not a multipart message, then do something with it */
476 while (ptr < content_end) {
480 part_end = content_end;
481 /* fix an off-by-one error */
485 /* Truncate if the header told us to */
486 if ( (content_length > 0) && (length > content_length) ) {
487 length = content_length;
490 /* Sometimes the "name" field is tacked on to Content-type,
491 * and sometimes it's tacked on to Content-disposition. Use
492 * whichever one we have.
494 if (strlen(content_disposition_name) > strlen(content_type_name)) {
495 name = content_disposition_name;
498 name = content_type_name;
501 /* lprintf(CTDL_DEBUG, "mime_decode part=%s, len=%d, type=%s, charset=%s, encoding=%s\n",
502 partnum, length, content_type, charset, encoding); */
504 /* Ok, we've got a non-multipart part here, so do something with it.
508 content_type, charset, encoding, disposition,
510 CallBack, NULL, NULL,
511 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);