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)
278 char *part_start, *part_end = NULL;
287 size_t content_length;
291 char *content_type_name;
292 char *content_disposition_name;
298 char nested_partnum[SIZ];
303 boundary = malloc(SIZ);
304 memset(boundary, 0, SIZ);
306 startary = malloc(SIZ);
307 memset(startary, 0, SIZ);
309 endary = malloc(SIZ);
310 memset(endary, 0, SIZ);
312 header = malloc(SIZ);
313 memset(header, 0, SIZ);
315 content_type = malloc(SIZ);
316 memset(content_type, 0, SIZ);
318 charset = malloc(SIZ);
319 memset(charset, 0, SIZ);
321 encoding = malloc(SIZ);
322 memset(encoding, 0, SIZ);
324 content_type_name = malloc(SIZ);
325 memset(content_type_name, 0, SIZ);
327 content_disposition_name = malloc(SIZ);
328 memset(content_disposition_name, 0, SIZ);
330 filename = malloc(SIZ);
331 memset(filename, 0, SIZ);
333 disposition = malloc(SIZ);
334 memset(disposition, 0, SIZ);
336 /* If the caller didn't supply an endpointer, generate one by measure */
337 if (content_end == NULL) {
338 content_end = &content_start[strlen(content_start)];
341 /* Learn interesting things from the headers */
344 ptr = memreadline(ptr, buf, SIZ);
345 if (ptr >= content_end) {
349 for (i = 0; i < strlen(buf); ++i) {
350 if (isspace(buf[i])) {
355 if (!isspace(buf[0])) {
356 if (!strncasecmp(header, "Content-type: ", 14)) {
357 strcpy(content_type, &header[14]);
358 extract_key(content_type_name, content_type, "name");
359 extract_key(charset, content_type, "charset");
360 /* Deal with weird headers */
361 if (strchr(content_type, ' '))
362 *(strchr(content_type, ' ')) = '\0';
363 if (strchr(content_type, ';'))
364 *(strchr(content_type, ';')) = '\0';
366 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
367 strcpy(disposition, &header[21]);
368 extract_key(content_disposition_name, disposition, "name");
369 extract_key(filename, disposition, "filename");
371 if (!strncasecmp(header, "Content-length: ", 16)) {
372 content_length = (size_t) atol(&header[16]);
374 if (!strncasecmp(header,
375 "Content-transfer-encoding: ", 27))
376 strcpy(encoding, &header[27]);
377 if (strlen(boundary) == 0)
378 extract_key(boundary, header, "boundary");
381 if ((strlen(header) + strlen(buf) + 2) < SIZ)
383 } while ((strlen(buf) > 0) && (*ptr != 0));
385 if (strchr(disposition, ';'))
386 *(strchr(disposition, ';')) = '\0';
387 striplt(disposition);
388 if (strchr(content_type, ';'))
389 *(strchr(content_type, ';')) = '\0';
390 striplt(content_type);
392 if (!strlen(content_type)) {
393 strcpy(content_type, "text/plain");
395 if (!strlen(charset)) {
396 strcpy(charset, "us-ascii");
399 if (strlen(boundary) > 0) {
405 /* If this is a multipart message, then recursively process it */
409 /* Tell the client about this message's multipartedness */
410 if (PreMultiPartCallBack != NULL) {
411 PreMultiPartCallBack("", "", partnum, "",
412 NULL, content_type, charset,
413 0, encoding, userdata);
416 /* Figure out where the boundaries are */
417 snprintf(startary, SIZ, "--%s", boundary);
418 snprintf(endary, SIZ, "--%s--", boundary);
422 next_boundary = bmstrstr(ptr, startary, strncmp);
423 if ( (part_start != NULL) && (next_boundary != NULL) ) {
424 part_end = next_boundary;
427 if (strlen(partnum) > 0) {
428 snprintf(nested_partnum,
429 sizeof nested_partnum,
434 snprintf(nested_partnum,
435 sizeof nested_partnum,
438 the_mime_parser(nested_partnum,
439 part_start, part_end,
441 PreMultiPartCallBack,
442 PostMultiPartCallBack,
447 if (next_boundary != NULL) {
448 /* If we pass out of scope, don't attempt to read
449 * past the end boundary. */
450 if (!strcmp(next_boundary, endary)) {
454 /* Set up for the next part. */
455 part_start = strstr(next_boundary, "\n");
461 /* Invalid end of multipart. Bail out! */
464 } while ( (ptr < content_end) && (next_boundary != NULL) );
466 if (PostMultiPartCallBack != NULL) {
467 PostMultiPartCallBack("", "", partnum, "", NULL,
468 content_type, charset, 0, encoding, userdata);
473 /* If it's not a multipart message, then do something with it */
477 while (ptr < content_end) {
481 part_end = content_end;
482 /* fix an off-by-one error */
486 /* Truncate if the header told us to */
487 if ( (content_length > 0) && (length > content_length) ) {
488 length = content_length;
491 /* Sometimes the "name" field is tacked on to Content-type,
492 * and sometimes it's tacked on to Content-disposition. Use
493 * whichever one we have.
495 if (strlen(content_disposition_name) > strlen(content_type_name)) {
496 name = content_disposition_name;
499 name = content_type_name;
504 content_type, charset, encoding, disposition,
506 CallBack, NULL, NULL,
507 userdata, dont_decode);
510 end_parser: /* free the buffers! end the oppression!! */
518 free(content_type_name);
519 free(content_disposition_name);
527 * Entry point for the MIME parser.
528 * (This function expects to be fed HEADERS + CONTENT)
529 * Note: NULL can be supplied as content_end; in this case, the message is
530 * considered to have ended when the parser encounters a 0x00 byte.
532 void mime_parser(char *content_start,
547 void (*PreMultiPartCallBack)
559 void (*PostMultiPartCallBack)
576 the_mime_parser("", content_start, content_end,
578 PreMultiPartCallBack,
579 PostMultiPartCallBack,
580 userdata, dont_decode);