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.
16 #include <sys/types.h>
23 #include "webserver.h"
25 #include "mime_parser.h"
28 void extract_key(char *target, char *source, char *key)
32 strcpy(target, source);
33 for (a = 0; a < strlen(target); ++a) {
34 if ((!strncasecmp(&target[a], key, strlen(key)))
35 && (target[a + strlen(key)] == '=')) {
36 strcpy(target, &target[a + strlen(key) + 1]);
38 strcpy(target, &target[1]);
39 for (b = 0; b < strlen(target); ++b)
50 * For non-multipart messages, we need to generate a quickie partnum of "1"
51 * to return to callback functions. Some callbacks demand it.
53 char *fixed_partnum(char *supplied_partnum) {
54 if (supplied_partnum == NULL) return "1";
55 if (strlen(supplied_partnum)==0) return "1";
56 return supplied_partnum;
62 * Convert "quoted-printable" to binary. Returns number of bytes decoded.
64 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
67 int soft_line_break = 0;
69 int decoded_length = 0;
77 for (i = 0; i < sourcelen; ++i) {
79 buf[buf_length++] = encoded[i];
81 if ( (encoded[i] == '\n')
83 || (i == (sourcelen-1)) ) {
84 buf[buf_length++] = 0;
86 /*** begin -- process one line ***/
88 if (buf[strlen(buf)-1] == '\n') {
89 buf[strlen(buf)-1] = 0;
91 if (buf[strlen(buf)-1] == '\r') {
92 buf[strlen(buf)-1] = 0;
94 while (isspace(buf[strlen(buf)-1])) {
95 buf[strlen(buf)-1] = 0;
99 while (strlen(buf) > 0) {
100 if (!strcmp(buf, "=")) {
103 } else if ((strlen(buf)>=3) && (buf[0]=='=')) {
104 sscanf(&buf[1], "%02x", &ch);
105 decoded[decoded_length++] = ch;
106 strcpy(buf, &buf[3]);
108 decoded[decoded_length++] = buf[0];
109 strcpy(buf, &buf[1]);
112 if (soft_line_break == 0) {
113 decoded[decoded_length++] = '\r';
114 decoded[decoded_length++] = '\n';
117 /*** end -- process one line ***/
121 decoded[decoded_length++] = 0;
122 return(decoded_length);
126 * Given a message or message-part body and a length, handle any necessary
127 * decoding and pass the request up the stack.
129 void mime_decode(char *partnum,
130 char *part_start, size_t length,
131 char *content_type, char *charset, char *encoding,
133 char *name, char *filename,
145 void (*PreMultiPartCallBack)
156 void (*PostMultiPartCallBack)
173 size_t bytes_decoded = 0;
175 /* Some encodings aren't really encodings */
176 if (!strcasecmp(encoding, "7bit"))
177 strcpy(encoding, "");
178 if (!strcasecmp(encoding, "8bit"))
179 strcpy(encoding, "");
180 if (!strcasecmp(encoding, "binary"))
181 strcpy(encoding, "");
183 /* If this part is not encoded, send as-is */
184 if ( (strlen(encoding) == 0) || (dont_decode)) {
185 if (CallBack != NULL) {
186 CallBack(name, filename, fixed_partnum(partnum),
187 disposition, part_start,
188 content_type, charset, length, encoding, userdata);
193 if ((strcasecmp(encoding, "base64"))
194 && (strcasecmp(encoding, "quoted-printable"))) {
198 * Allocate a buffer for the decoded data. The output buffer is the
199 * same size as the input buffer; this assumes that the decoded data
200 * will never be larger than the encoded data. This is a safe
201 * assumption with base64, uuencode, and quoted-printable.
203 decoded = malloc(length+2048);
204 if (decoded == NULL) {
208 if (!strcasecmp(encoding, "base64")) {
209 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
211 else if (!strcasecmp(encoding, "quoted-printable")) {
212 bytes_decoded = CtdlDecodeQuotedPrintable(decoded,
216 if (bytes_decoded > 0) if (CallBack != NULL) {
217 CallBack(name, filename, fixed_partnum(partnum),
218 disposition, decoded,
219 content_type, charset, bytes_decoded, "binary", userdata);
226 * Break out the components of a multipart message
227 * (This function expects to be fed HEADERS + CONTENT)
228 * Note: NULL can be supplied as content_end; in this case, the message is
229 * considered to have ended when the parser encounters a 0x00 byte.
231 void the_mime_parser(char *partnum,
232 char *content_start, char *content_end,
244 void (*PreMultiPartCallBack)
255 void (*PostMultiPartCallBack)
273 char *part_start, *part_end = NULL;
278 size_t startary_len = 0;
283 size_t content_length;
287 char *content_type_name;
288 char *content_disposition_name;
294 char nested_partnum[SIZ];
299 boundary = malloc(SIZ);
300 memset(boundary, 0, SIZ);
302 startary = malloc(SIZ);
303 memset(startary, 0, SIZ);
305 endary = malloc(SIZ);
306 memset(endary, 0, SIZ);
308 header = malloc(SIZ);
309 memset(header, 0, SIZ);
311 content_type = malloc(SIZ);
312 memset(content_type, 0, SIZ);
314 charset = malloc(SIZ);
315 memset(charset, 0, SIZ);
317 encoding = malloc(SIZ);
318 memset(encoding, 0, SIZ);
320 content_type_name = malloc(SIZ);
321 memset(content_type_name, 0, SIZ);
323 content_disposition_name = malloc(SIZ);
324 memset(content_disposition_name, 0, SIZ);
326 filename = malloc(SIZ);
327 memset(filename, 0, SIZ);
329 disposition = malloc(SIZ);
330 memset(disposition, 0, SIZ);
332 /* If the caller didn't supply an endpointer, generate one by measure */
333 if (content_end == NULL) {
334 content_end = &content_start[strlen(content_start)];
337 /* Learn interesting things from the headers */
340 ptr = memreadline(ptr, buf, SIZ);
341 if (ptr >= content_end) {
345 for (i = 0; i < strlen(buf); ++i) {
346 if (isspace(buf[i])) {
351 if (!isspace(buf[0])) {
352 if (!strncasecmp(header, "Content-type: ", 14)) {
353 strcpy(content_type, &header[14]);
354 extract_key(content_type_name, content_type, "name");
355 extract_key(charset, content_type, "charset");
356 /* Deal with weird headers */
357 if (strchr(content_type, ' '))
358 *(strchr(content_type, ' ')) = '\0';
359 if (strchr(content_type, ';'))
360 *(strchr(content_type, ';')) = '\0';
362 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
363 strcpy(disposition, &header[21]);
364 extract_key(content_disposition_name, disposition, "name");
365 extract_key(filename, disposition, "filename");
367 if (!strncasecmp(header, "Content-length: ", 16)) {
368 content_length = (size_t) atol(&header[16]);
370 if (!strncasecmp(header,
371 "Content-transfer-encoding: ", 27))
372 strcpy(encoding, &header[27]);
373 if (strlen(boundary) == 0)
374 extract_key(boundary, header, "boundary");
377 if ((strlen(header) + strlen(buf) + 2) < SIZ)
379 } while ((strlen(buf) > 0) && (*ptr != 0));
381 if (strchr(disposition, ';'))
382 *(strchr(disposition, ';')) = '\0';
383 striplt(disposition);
384 if (strchr(content_type, ';'))
385 *(strchr(content_type, ';')) = '\0';
386 striplt(content_type);
388 if (strlen(boundary) > 0) {
394 /* If this is a multipart message, then recursively process it */
398 /* Tell the client about this message's multipartedness */
399 if (PreMultiPartCallBack != NULL) {
400 PreMultiPartCallBack("", "", partnum, "",
401 NULL, content_type, charset,
402 0, encoding, userdata);
405 /* Figure out where the boundaries are */
406 snprintf(startary, SIZ, "--%s", boundary);
407 snprintf(endary, SIZ, "--%s--", boundary);
408 startary_len = strlen(startary);
413 /* next_boundary = bmstrstr(ptr, startary, memcmp); */
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;
503 content_type, charset, encoding, disposition,
505 CallBack, NULL, NULL,
506 userdata, dont_decode);
509 end_parser: /* free the buffers! end the oppression!! */
517 free(content_type_name);
518 free(content_disposition_name);
526 * Entry point for the MIME parser.
527 * (This function expects to be fed HEADERS + CONTENT)
528 * Note: NULL can be supplied as content_end; in this case, the message is
529 * considered to have ended when the parser encounters a 0x00 byte.
531 void mime_parser(char *content_start,
546 void (*PreMultiPartCallBack)
558 void (*PostMultiPartCallBack)
575 the_mime_parser("", content_start, content_end,
577 PreMultiPartCallBack,
578 PostMultiPartCallBack,
579 userdata, dont_decode);