4 * This is the MIME parser for Citadel. Sometimes it actually works.
6 * Copyright (c) 1998-2002 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 *encoding,
139 char *name, char *filename,
150 void (*PreMultiPartCallBack)
160 void (*PostMultiPartCallBack)
176 size_t bytes_decoded = 0;
178 /* Some encodings aren't really encodings */
179 if (!strcasecmp(encoding, "7bit"))
180 strcpy(encoding, "");
181 if (!strcasecmp(encoding, "8bit"))
182 strcpy(encoding, "");
183 if (!strcasecmp(encoding, "binary"))
184 strcpy(encoding, "");
186 /* If this part is not encoded, send as-is */
187 if ( (strlen(encoding) == 0) || (dont_decode)) {
188 if (CallBack != NULL) {
189 CallBack(name, filename, fixed_partnum(partnum),
190 disposition, part_start,
191 content_type, length, encoding, userdata);
196 if ((strcasecmp(encoding, "base64"))
197 && (strcasecmp(encoding, "quoted-printable"))) {
201 * Allocate a buffer for the decoded data. The output buffer is the
202 * same size as the input buffer; this assumes that the decoded data
203 * will never be larger than the encoded data. This is a safe
204 * assumption with base64, uuencode, and quoted-printable.
206 decoded = malloc(length+2048);
207 if (decoded == NULL) {
211 if (!strcasecmp(encoding, "base64")) {
212 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
214 else if (!strcasecmp(encoding, "quoted-printable")) {
215 bytes_decoded = CtdlDecodeQuotedPrintable(decoded,
219 if (bytes_decoded > 0) if (CallBack != NULL) {
220 CallBack(name, filename, fixed_partnum(partnum),
221 disposition, decoded,
222 content_type, bytes_decoded, "binary", userdata);
229 * Break out the components of a multipart message
230 * (This function expects to be fed HEADERS + CONTENT)
231 * Note: NULL can be supplied as content_end; in this case, the message is
232 * considered to have ended when the parser encounters a 0x00 byte.
234 void the_mime_parser(char *partnum,
235 char *content_start, char *content_end,
246 void (*PreMultiPartCallBack)
256 void (*PostMultiPartCallBack)
272 char *part_start, *part_end = NULL;
280 size_t content_length;
284 char *content_type_name;
285 char *content_disposition_name;
291 char nested_partnum[SIZ];
296 boundary = malloc(SIZ);
297 memset(boundary, 0, SIZ);
299 startary = malloc(SIZ);
300 memset(startary, 0, SIZ);
302 endary = malloc(SIZ);
303 memset(endary, 0, SIZ);
305 header = malloc(SIZ);
306 memset(header, 0, SIZ);
308 content_type = malloc(SIZ);
309 memset(content_type, 0, SIZ);
311 encoding = malloc(SIZ);
312 memset(encoding, 0, SIZ);
314 content_type_name = malloc(SIZ);
315 memset(content_type_name, 0, SIZ);
317 content_disposition_name = malloc(SIZ);
318 memset(content_disposition_name, 0, SIZ);
320 filename = malloc(SIZ);
321 memset(filename, 0, SIZ);
323 disposition = malloc(SIZ);
324 memset(disposition, 0, SIZ);
326 /* If the caller didn't supply an endpointer, generate one by measure */
327 if (content_end == NULL) {
328 content_end = &content_start[strlen(content_start)];
331 /* Learn interesting things from the headers */
334 ptr = memreadline(ptr, buf, SIZ);
335 if (ptr >= content_end) {
339 for (i = 0; i < strlen(buf); ++i)
342 if (!isspace(buf[0])) {
343 if (!strncasecmp(header, "Content-type: ", 14)) {
344 strcpy(content_type, &header[14]);
345 extract_key(content_type_name, content_type, "name");
346 /* Deal with weird headers */
347 if (strchr(content_type, ' '))
348 *(strchr(content_type, ' ')) = '\0';
349 if (strchr(content_type, ';'))
350 *(strchr(content_type, ';')) = '\0';
352 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
353 strcpy(disposition, &header[21]);
354 extract_key(content_disposition_name, disposition, "name");
355 extract_key(filename, disposition, "filename");
357 if (!strncasecmp(header, "Content-length: ", 16)) {
358 content_length = (size_t) atol(&header[16]);
360 if (!strncasecmp(header,
361 "Content-transfer-encoding: ", 27))
362 strcpy(encoding, &header[27]);
363 if (strlen(boundary) == 0)
364 extract_key(boundary, header, "boundary");
367 if ((strlen(header) + strlen(buf) + 2) < SIZ)
369 } while ((strlen(buf) > 0) && (*ptr != 0));
371 if (strchr(disposition, ';'))
372 *(strchr(disposition, ';')) = '\0';
373 striplt(disposition);
374 if (strchr(content_type, ';'))
375 *(strchr(content_type, ';')) = '\0';
376 striplt(content_type);
378 if (strlen(boundary) > 0) {
384 /* If this is a multipart message, then recursively process it */
388 /* Tell the client about this message's multipartedness */
389 if (PreMultiPartCallBack != NULL) {
390 PreMultiPartCallBack("", "", partnum, "",
392 0, encoding, userdata);
395 /* Figure out where the boundaries are */
396 snprintf(startary, SIZ, "--%s", boundary);
397 snprintf(endary, SIZ, "--%s--", boundary);
401 next_boundary = bmstrstr(ptr, startary, strncmp);
402 if ( (part_start != NULL) && (next_boundary != NULL) ) {
403 part_end = next_boundary;
406 if (strlen(partnum) > 0) {
407 snprintf(nested_partnum,
408 sizeof nested_partnum,
413 snprintf(nested_partnum,
414 sizeof nested_partnum,
417 the_mime_parser(nested_partnum,
418 part_start, part_end,
420 PreMultiPartCallBack,
421 PostMultiPartCallBack,
426 if (next_boundary != NULL) {
427 /* If we pass out of scope, don't attempt to read
428 * past the end boundary. */
429 if (!strcmp(next_boundary, endary)) {
433 /* Set up for the next part. */
434 part_start = strstr(next_boundary, "\n");
440 /* Invalid end of multipart. Bail out! */
443 } while ( (ptr < content_end) && (next_boundary != NULL) );
445 if (PostMultiPartCallBack != NULL) {
446 PostMultiPartCallBack("", "", partnum, "", NULL,
447 content_type, 0, encoding, userdata);
452 /* If it's not a multipart message, then do something with it */
456 while (ptr < content_end) {
460 part_end = content_end;
461 /* fix an off-by-one error */
465 /* Truncate if the header told us to */
466 if ( (content_length > 0) && (length > content_length) ) {
467 length = content_length;
470 /* Sometimes the "name" field is tacked on to Content-type,
471 * and sometimes it's tacked on to Content-disposition. Use
472 * whichever one we have.
474 if (strlen(content_disposition_name) > strlen(content_type_name)) {
475 name = content_disposition_name;
478 name = content_type_name;
483 content_type, encoding, disposition,
485 CallBack, NULL, NULL,
486 userdata, dont_decode);
489 end_parser: /* free the buffers! end the oppression!! */
496 free(content_type_name);
497 free(content_disposition_name);
505 * Entry point for the MIME parser.
506 * (This function expects to be fed HEADERS + CONTENT)
507 * Note: NULL can be supplied as content_end; in this case, the message is
508 * considered to have ended when the parser encounters a 0x00 byte.
510 void mime_parser(char *content_start,
524 void (*PreMultiPartCallBack)
535 void (*PostMultiPartCallBack)
551 the_mime_parser("", content_start, content_end,
553 PreMultiPartCallBack,
554 PostMultiPartCallBack,
555 userdata, dont_decode);