4 * This is the MIME parser for Citadel. Sometimes it actually works.
6 * Copyright (c) 1998-2003 by Art Cancro
7 * This code is distributed under the terms of the GNU General Public License.
16 #include <sys/types.h>
23 #include "mime_parser.h"
26 void extract_key(char *target, char *source, char *key)
30 strcpy(target, source);
31 for (a = 0; a < strlen(target); ++a) {
32 if ((!strncasecmp(&target[a], key, strlen(key)))
33 && (target[a + strlen(key)] == '=')) {
34 strcpy(target, &target[a + strlen(key) + 1]);
36 strcpy(target, &target[1]);
37 for (b = 0; b < strlen(target); ++b)
48 * For non-multipart messages, we need to generate a quickie partnum of "1"
49 * to return to callback functions. Some callbacks demand it.
51 char *fixed_partnum(char *supplied_partnum) {
52 if (supplied_partnum == NULL) return "1";
53 if (strlen(supplied_partnum)==0) return "1";
54 return supplied_partnum;
60 * Convert "quoted-printable" to binary. Returns number of bytes decoded.
62 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
65 int soft_line_break = 0;
67 int decoded_length = 0;
75 for (i = 0; i < sourcelen; ++i) {
77 buf[buf_length++] = encoded[i];
79 if ( (encoded[i] == '\n')
81 || (i == (sourcelen-1)) ) {
82 buf[buf_length++] = 0;
84 /*** begin -- process one line ***/
86 if (buf[strlen(buf)-1] == '\n') {
87 buf[strlen(buf)-1] = 0;
89 if (buf[strlen(buf)-1] == '\r') {
90 buf[strlen(buf)-1] = 0;
92 while (isspace(buf[strlen(buf)-1])) {
93 buf[strlen(buf)-1] = 0;
97 while (strlen(buf) > 0) {
98 if (!strcmp(buf, "=")) {
101 } else if ((strlen(buf)>=3) && (buf[0]=='=')) {
102 sscanf(&buf[1], "%02x", &ch);
103 decoded[decoded_length++] = ch;
104 strcpy(buf, &buf[3]);
106 decoded[decoded_length++] = buf[0];
107 strcpy(buf, &buf[1]);
110 if (soft_line_break == 0) {
111 decoded[decoded_length++] = '\r';
112 decoded[decoded_length++] = '\n';
115 /*** end -- process one line ***/
119 decoded[decoded_length++] = 0;
120 return(decoded_length);
124 * Given a message or message-part body and a length, handle any necessary
125 * decoding and pass the request up the stack.
127 void mime_decode(char *partnum,
128 char *part_start, size_t length,
129 char *content_type, char *encoding,
131 char *name, char *filename,
142 void (*PreMultiPartCallBack)
152 void (*PostMultiPartCallBack)
168 size_t bytes_decoded = 0;
170 /* Some encodings aren't really encodings */
171 if (!strcasecmp(encoding, "7bit"))
172 strcpy(encoding, "");
173 if (!strcasecmp(encoding, "8bit"))
174 strcpy(encoding, "");
175 if (!strcasecmp(encoding, "binary"))
176 strcpy(encoding, "");
178 /* If this part is not encoded, send as-is */
179 if ( (strlen(encoding) == 0) || (dont_decode)) {
180 if (CallBack != NULL) {
181 CallBack(name, filename, fixed_partnum(partnum),
182 disposition, part_start,
183 content_type, length, encoding, userdata);
188 if ((strcasecmp(encoding, "base64"))
189 && (strcasecmp(encoding, "quoted-printable"))) {
193 * Allocate a buffer for the decoded data. The output buffer is the
194 * same size as the input buffer; this assumes that the decoded data
195 * will never be larger than the encoded data. This is a safe
196 * assumption with base64, uuencode, and quoted-printable.
198 decoded = mallok(length+2048);
199 if (decoded == NULL) {
203 if (!strcasecmp(encoding, "base64")) {
204 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
206 else if (!strcasecmp(encoding, "quoted-printable")) {
207 bytes_decoded = CtdlDecodeQuotedPrintable(decoded,
211 if (bytes_decoded > 0) if (CallBack != NULL) {
212 CallBack(name, filename, fixed_partnum(partnum),
213 disposition, decoded,
214 content_type, bytes_decoded, "binary", userdata);
221 * Break out the components of a multipart message
222 * (This function expects to be fed HEADERS + CONTENT)
223 * Note: NULL can be supplied as content_end; in this case, the message is
224 * considered to have ended when the parser encounters a 0x00 byte.
226 void the_mime_parser(char *partnum,
227 char *content_start, char *content_end,
238 void (*PreMultiPartCallBack)
248 void (*PostMultiPartCallBack)
264 char *part_start, *part_end = NULL;
271 size_t content_length;
275 char *content_type_name;
276 char *content_disposition_name;
282 char nested_partnum[SIZ];
287 boundary = mallok(SIZ);
288 memset(boundary, 0, SIZ);
290 startary = mallok(SIZ);
291 memset(startary, 0, SIZ);
293 endary = mallok(SIZ);
294 memset(endary, 0, SIZ);
296 header = mallok(SIZ);
297 memset(header, 0, SIZ);
299 content_type = mallok(SIZ);
300 memset(content_type, 0, SIZ);
302 encoding = mallok(SIZ);
303 memset(encoding, 0, SIZ);
305 content_type_name = mallok(SIZ);
306 memset(content_type_name, 0, SIZ);
308 content_disposition_name = mallok(SIZ);
309 memset(content_disposition_name, 0, SIZ);
311 filename = mallok(SIZ);
312 memset(filename, 0, SIZ);
314 disposition = mallok(SIZ);
315 memset(disposition, 0, SIZ);
317 /* If the caller didn't supply an endpointer, generate one by measure */
318 if (content_end == NULL) {
319 content_end = &content_start[strlen(content_start)];
322 /* Learn interesting things from the headers */
325 ptr = memreadline(ptr, buf, SIZ);
326 if (ptr >= content_end) {
330 for (i = 0; i < strlen(buf); ++i)
333 if (!isspace(buf[0])) {
334 if (!strncasecmp(header, "Content-type: ", 14)) {
335 strcpy(content_type, &header[14]);
336 extract_key(content_type_name, content_type, "name");
337 /* Deal with weird headers */
338 if (strchr(content_type, ' '))
339 *(strchr(content_type, ' ')) = '\0';
340 if (strchr(content_type, ';'))
341 *(strchr(content_type, ';')) = '\0';
343 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
344 strcpy(disposition, &header[21]);
345 extract_key(content_disposition_name, disposition, "name");
346 extract_key(filename, disposition, "filename");
348 if (!strncasecmp(header, "Content-length: ", 16)) {
349 content_length = (size_t) atol(&header[16]);
351 if (!strncasecmp(header,
352 "Content-transfer-encoding: ", 27))
353 strcpy(encoding, &header[27]);
354 if (strlen(boundary) == 0)
355 extract_key(boundary, header, "boundary");
358 if ((strlen(header) + strlen(buf) + 2) < SIZ)
360 } while ((strlen(buf) > 0) && (*ptr != 0));
362 if (strchr(disposition, ';'))
363 *(strchr(disposition, ';')) = '\0';
364 striplt(disposition);
365 if (strchr(content_type, ';'))
366 *(strchr(content_type, ';')) = '\0';
367 striplt(content_type);
369 if (strlen(boundary) > 0) {
375 /* If this is a multipart message, then recursively process it */
379 /* Tell the client about this message's multipartedness */
380 if (PreMultiPartCallBack != NULL) {
381 PreMultiPartCallBack("", "", partnum, "",
383 0, encoding, userdata);
386 /* Figure out where the boundaries are */
387 snprintf(startary, SIZ, "--%s", boundary);
388 snprintf(endary, SIZ, "--%s--", boundary);
390 if ( (!strncasecmp(ptr, startary, strlen(startary)))
391 || (!strncasecmp(ptr, endary, strlen(endary))) ) {
392 if (part_start != NULL) {
393 if (strlen(partnum) > 0) {
394 snprintf(nested_partnum,
395 sizeof nested_partnum,
400 snprintf(nested_partnum,
401 sizeof nested_partnum,
404 the_mime_parser(nested_partnum,
405 part_start, part_end,
407 PreMultiPartCallBack,
408 PostMultiPartCallBack,
412 ptr = memreadline(ptr, buf, SIZ);
419 /* If we pass out of scope in the MIME multipart (by
420 * hitting the end boundary), force the pointer out
421 * of scope so this loop ends.
423 if (ptr < content_end) {
424 if (!strcasecmp(ptr, endary)) {
428 } while (ptr <= content_end);
429 if (PostMultiPartCallBack != NULL) {
430 PostMultiPartCallBack("", "", partnum, "", NULL,
431 content_type, 0, encoding, userdata);
436 /* If it's not a multipart message, then do something with it */
440 while (ptr < content_end) {
444 part_end = content_end;
445 /* fix an off-by-one error */
449 /* Truncate if the header told us to */
450 if ( (content_length > 0) && (length > content_length) ) {
451 length = content_length;
454 /* Sometimes the "name" field is tacked on to Content-type,
455 * and sometimes it's tacked on to Content-disposition. Use
456 * whichever one we have.
458 if (strlen(content_disposition_name) > strlen(content_type_name)) {
459 name = content_disposition_name;
462 name = content_type_name;
467 content_type, encoding, disposition,
469 CallBack, NULL, NULL,
470 userdata, dont_decode);
473 end_parser: /* free the buffers! end the oppression!! */
480 phree(content_type_name);
481 phree(content_disposition_name);
489 * Entry point for the MIME parser.
490 * (This function expects to be fed HEADERS + CONTENT)
491 * Note: NULL can be supplied as content_end; in this case, the message is
492 * considered to have ended when the parser encounters a 0x00 byte.
494 void mime_parser(char *content_start,
508 void (*PreMultiPartCallBack)
519 void (*PostMultiPartCallBack)
535 the_mime_parser("", content_start, content_end,
537 PreMultiPartCallBack,
538 PostMultiPartCallBack,
539 userdata, dont_decode);