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 "dynloader.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 = mallok(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;
279 size_t content_length;
288 char nested_partnum[SIZ];
293 boundary = mallok(SIZ);
294 memset(boundary, 0, SIZ);
296 startary = mallok(SIZ);
297 memset(startary, 0, SIZ);
299 endary = mallok(SIZ);
300 memset(endary, 0, SIZ);
302 header = mallok(SIZ);
303 memset(header, 0, SIZ);
305 content_type = mallok(SIZ);
306 memset(content_type, 0, SIZ);
308 encoding = mallok(SIZ);
309 memset(encoding, 0, SIZ);
312 memset(name, 0, SIZ);
314 filename = mallok(SIZ);
315 memset(filename, 0, SIZ);
317 disposition = mallok(SIZ);
318 memset(disposition, 0, SIZ);
320 /* If the caller didn't supply an endpointer, generate one by measure */
321 if (content_end == NULL) {
322 content_end = &content_start[strlen(content_start)];
325 /* Learn interesting things from the headers */
328 ptr = memreadline(ptr, buf, SIZ);
329 if (ptr >= content_end) {
333 for (i = 0; i < strlen(buf); ++i)
336 if (!isspace(buf[0])) {
337 if (!strncasecmp(header, "Content-type: ", 14)) {
338 strcpy(content_type, &header[14]);
339 extract_key(name, content_type, "name");
340 /* Deal with weird headers */
341 if (strchr(content_type, ' '))
342 *(strchr(content_type, ' ')) = '\0';
343 if (strchr(content_type, ';'))
344 *(strchr(content_type, ';')) = '\0';
346 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
347 strcpy(disposition, &header[21]);
348 extract_key(filename, disposition, "filename");
350 if (!strncasecmp(header, "Content-length: ", 16)) {
351 content_length = (size_t) atol(&header[16]);
353 if (!strncasecmp(header,
354 "Content-transfer-encoding: ", 27))
355 strcpy(encoding, &header[27]);
356 if (strlen(boundary) == 0)
357 extract_key(boundary, header, "boundary");
360 if ((strlen(header) + strlen(buf) + 2) < SIZ)
362 } while ((strlen(buf) > 0) && (*ptr != 0));
364 if (strchr(disposition, ';'))
365 *(strchr(disposition, ';')) = '\0';
366 striplt(disposition);
367 if (strchr(content_type, ';'))
368 *(strchr(content_type, ';')) = '\0';
369 striplt(content_type);
371 if (strlen(boundary) > 0) {
377 /* If this is a multipart message, then recursively process it */
381 /* Tell the client about this message's multipartedness */
382 if (PreMultiPartCallBack != NULL) {
383 PreMultiPartCallBack("", "", partnum, "",
385 0, encoding, userdata);
388 /* Figure out where the boundaries are */
389 snprintf(startary, SIZ, "--%s", boundary);
390 snprintf(endary, SIZ, "--%s--", boundary);
392 if ( (!strncasecmp(ptr, startary, strlen(startary)))
393 || (!strncasecmp(ptr, endary, strlen(endary))) ) {
394 if (part_start != NULL) {
395 if (strlen(partnum) > 0) {
396 snprintf(nested_partnum,
397 sizeof nested_partnum,
402 snprintf(nested_partnum,
403 sizeof nested_partnum,
406 the_mime_parser(nested_partnum,
407 part_start, part_end,
409 PreMultiPartCallBack,
410 PostMultiPartCallBack,
414 ptr = memreadline(ptr, buf, SIZ);
421 /* If we pass out of scope in the MIME multipart (by
422 * hitting the end boundary), force the pointer out
423 * of scope so this loop ends.
425 if (ptr < content_end) {
426 if (!strcasecmp(ptr, endary)) {
430 } while (ptr <= content_end);
431 if (PostMultiPartCallBack != NULL) {
432 PostMultiPartCallBack("", "", partnum, "", NULL,
433 content_type, 0, encoding, userdata);
438 /* If it's not a multipart message, then do something with it */
442 while (ptr < content_end) {
446 part_end = content_end;
448 /* Truncate if the header told us to */
449 if ( (content_length > 0) && (length > content_length) ) {
450 length = content_length;
455 content_type, encoding, disposition,
457 CallBack, NULL, NULL,
458 userdata, dont_decode);
461 end_parser: /* free the buffers! end the oppression!! */
476 * Entry point for the MIME parser.
477 * (This function expects to be fed HEADERS + CONTENT)
478 * Note: NULL can be supplied as content_end; in this case, the message is
479 * considered to have ended when the parser encounters a 0x00 byte.
481 void mime_parser(char *content_start,
495 void (*PreMultiPartCallBack)
506 void (*PostMultiPartCallBack)
522 the_mime_parser("", content_start, content_end,
524 PreMultiPartCallBack,
525 PostMultiPartCallBack,
526 userdata, dont_decode);