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 = 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;
283 char *content_type_name;
284 char *content_disposition_name;
290 char nested_partnum[SIZ];
295 boundary = mallok(SIZ);
296 memset(boundary, 0, SIZ);
298 startary = mallok(SIZ);
299 memset(startary, 0, SIZ);
301 endary = mallok(SIZ);
302 memset(endary, 0, SIZ);
304 header = mallok(SIZ);
305 memset(header, 0, SIZ);
307 content_type = mallok(SIZ);
308 memset(content_type, 0, SIZ);
310 encoding = mallok(SIZ);
311 memset(encoding, 0, SIZ);
313 content_type_name = mallok(SIZ);
314 memset(content_type_name, 0, SIZ);
316 content_disposition_name = mallok(SIZ);
317 memset(content_disposition_name, 0, SIZ);
319 filename = mallok(SIZ);
320 memset(filename, 0, SIZ);
322 disposition = mallok(SIZ);
323 memset(disposition, 0, SIZ);
325 /* If the caller didn't supply an endpointer, generate one by measure */
326 if (content_end == NULL) {
327 content_end = &content_start[strlen(content_start)];
330 /* Learn interesting things from the headers */
333 ptr = memreadline(ptr, buf, SIZ);
334 if (ptr >= content_end) {
338 for (i = 0; i < strlen(buf); ++i)
341 if (!isspace(buf[0])) {
342 if (!strncasecmp(header, "Content-type: ", 14)) {
343 strcpy(content_type, &header[14]);
344 extract_key(content_type_name, content_type, "name");
345 /* Deal with weird headers */
346 if (strchr(content_type, ' '))
347 *(strchr(content_type, ' ')) = '\0';
348 if (strchr(content_type, ';'))
349 *(strchr(content_type, ';')) = '\0';
351 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
352 strcpy(disposition, &header[21]);
353 extract_key(content_disposition_name, disposition, "name");
354 extract_key(filename, disposition, "filename");
356 if (!strncasecmp(header, "Content-length: ", 16)) {
357 content_length = (size_t) atol(&header[16]);
359 if (!strncasecmp(header,
360 "Content-transfer-encoding: ", 27))
361 strcpy(encoding, &header[27]);
362 if (strlen(boundary) == 0)
363 extract_key(boundary, header, "boundary");
366 if ((strlen(header) + strlen(buf) + 2) < SIZ)
368 } while ((strlen(buf) > 0) && (*ptr != 0));
370 if (strchr(disposition, ';'))
371 *(strchr(disposition, ';')) = '\0';
372 striplt(disposition);
373 if (strchr(content_type, ';'))
374 *(strchr(content_type, ';')) = '\0';
375 striplt(content_type);
377 if (strlen(boundary) > 0) {
383 /* If this is a multipart message, then recursively process it */
387 /* Tell the client about this message's multipartedness */
388 if (PreMultiPartCallBack != NULL) {
389 PreMultiPartCallBack("", "", partnum, "",
391 0, encoding, userdata);
394 /* Figure out where the boundaries are */
395 snprintf(startary, SIZ, "--%s", boundary);
396 snprintf(endary, SIZ, "--%s--", boundary);
398 if ( (!strncasecmp(ptr, startary, strlen(startary)))
399 || (!strncasecmp(ptr, endary, strlen(endary))) ) {
400 if (part_start != NULL) {
401 if (strlen(partnum) > 0) {
402 snprintf(nested_partnum,
403 sizeof nested_partnum,
408 snprintf(nested_partnum,
409 sizeof nested_partnum,
412 the_mime_parser(nested_partnum,
413 part_start, part_end,
415 PreMultiPartCallBack,
416 PostMultiPartCallBack,
420 ptr = memreadline(ptr, buf, SIZ);
427 /* If we pass out of scope in the MIME multipart (by
428 * hitting the end boundary), force the pointer out
429 * of scope so this loop ends.
431 if (ptr < content_end) {
432 if (!strcasecmp(ptr, endary)) {
436 } while (ptr <= content_end);
437 if (PostMultiPartCallBack != NULL) {
438 PostMultiPartCallBack("", "", partnum, "", NULL,
439 content_type, 0, encoding, userdata);
444 /* If it's not a multipart message, then do something with it */
448 while (ptr < content_end) {
452 part_end = content_end;
454 /* Truncate if the header told us to */
455 if ( (content_length > 0) && (length > content_length) ) {
456 length = content_length;
459 /* Sometimes the "name" field is tacked on to Content-type,
460 * and sometimes it's tacked on to Content-disposition. Use
461 * whichever one we have.
463 if (strlen(content_disposition_name) > strlen(content_type_name)) {
464 name = content_disposition_name;
467 name = content_type_name;
472 content_type, encoding, disposition,
474 CallBack, NULL, NULL,
475 userdata, dont_decode);
478 end_parser: /* free the buffers! end the oppression!! */
485 phree(content_type_name);
486 phree(content_disposition_name);
494 * Entry point for the MIME parser.
495 * (This function expects to be fed HEADERS + CONTENT)
496 * Note: NULL can be supplied as content_end; in this case, the message is
497 * considered to have ended when the parser encounters a 0x00 byte.
499 void mime_parser(char *content_start,
513 void (*PreMultiPartCallBack)
524 void (*PostMultiPartCallBack)
540 the_mime_parser("", content_start, content_end,
542 PreMultiPartCallBack,
543 PostMultiPartCallBack,
544 userdata, dont_decode);