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.
12 #include "webserver.h"
13 #include "mime_parser.h"
16 void extract_key(char *target, char *source, char *key)
20 strcpy(target, source);
21 for (a = 0; a < strlen(target); ++a) {
22 if ((!strncasecmp(&target[a], key, strlen(key)))
23 && (target[a + strlen(key)] == '=')) {
24 strcpy(target, &target[a + strlen(key) + 1]);
26 strcpy(target, &target[1]);
27 for (b = 0; b < strlen(target); ++b)
38 * For non-multipart messages, we need to generate a quickie partnum of "1"
39 * to return to callback functions. Some callbacks demand it.
41 char *fixed_partnum(char *supplied_partnum) {
42 if (supplied_partnum == NULL) return "1";
43 if (strlen(supplied_partnum)==0) return "1";
44 return supplied_partnum;
50 * Convert "quoted-printable" to binary. Returns number of bytes decoded.
52 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
55 int soft_line_break = 0;
57 int decoded_length = 0;
65 for (i = 0; i < sourcelen; ++i) {
67 buf[buf_length++] = encoded[i];
69 if ( (encoded[i] == '\n')
71 || (i == (sourcelen-1)) ) {
72 buf[buf_length++] = 0;
74 /*** begin -- process one line ***/
76 if (buf[strlen(buf)-1] == '\n') {
77 buf[strlen(buf)-1] = 0;
79 if (buf[strlen(buf)-1] == '\r') {
80 buf[strlen(buf)-1] = 0;
82 while (isspace(buf[strlen(buf)-1])) {
83 buf[strlen(buf)-1] = 0;
87 while (strlen(buf) > 0) {
88 if (!strcmp(buf, "=")) {
91 } else if ((strlen(buf)>=3) && (buf[0]=='=')) {
92 sscanf(&buf[1], "%02x", &ch);
93 decoded[decoded_length++] = ch;
96 decoded[decoded_length++] = buf[0];
100 if (soft_line_break == 0) {
101 decoded[decoded_length++] = '\r';
102 decoded[decoded_length++] = '\n';
105 /*** end -- process one line ***/
109 decoded[decoded_length++] = 0;
110 return(decoded_length);
114 * Given a message or message-part body and a length, handle any necessary
115 * decoding and pass the request up the stack.
117 void mime_decode(char *partnum,
118 char *part_start, size_t length,
119 char *content_type, char *charset, char *encoding,
121 char *name, char *filename,
133 void (*PreMultiPartCallBack)
144 void (*PostMultiPartCallBack)
161 size_t bytes_decoded = 0;
163 /* Some encodings aren't really encodings */
164 if (!strcasecmp(encoding, "7bit"))
165 strcpy(encoding, "");
166 if (!strcasecmp(encoding, "8bit"))
167 strcpy(encoding, "");
168 if (!strcasecmp(encoding, "binary"))
169 strcpy(encoding, "");
171 /* If this part is not encoded, send as-is */
172 if ( (strlen(encoding) == 0) || (dont_decode)) {
173 if (CallBack != NULL) {
174 CallBack(name, filename, fixed_partnum(partnum),
175 disposition, part_start,
176 content_type, charset, length, encoding, userdata);
181 if ((strcasecmp(encoding, "base64"))
182 && (strcasecmp(encoding, "quoted-printable"))) {
186 * Allocate a buffer for the decoded data. The output buffer is the
187 * same size as the input buffer; this assumes that the decoded data
188 * will never be larger than the encoded data. This is a safe
189 * assumption with base64, uuencode, and quoted-printable.
191 decoded = malloc(length+2048);
192 if (decoded == NULL) {
196 if (!strcasecmp(encoding, "base64")) {
197 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
199 else if (!strcasecmp(encoding, "quoted-printable")) {
200 bytes_decoded = CtdlDecodeQuotedPrintable(decoded,
204 if (bytes_decoded > 0) if (CallBack != NULL) {
205 CallBack(name, filename, fixed_partnum(partnum),
206 disposition, decoded,
207 content_type, charset, bytes_decoded, "binary", userdata);
214 * Break out the components of a multipart message
215 * (This function expects to be fed HEADERS + CONTENT)
216 * Note: NULL can be supplied as content_end; in this case, the message is
217 * considered to have ended when the parser encounters a 0x00 byte.
219 void the_mime_parser(char *partnum,
220 char *content_start, char *content_end,
232 void (*PreMultiPartCallBack)
243 void (*PostMultiPartCallBack)
261 char *part_start, *part_end = NULL;
266 size_t startary_len = 0;
271 size_t content_length;
275 char *content_type_name;
276 char *content_disposition_name;
282 char nested_partnum[SIZ];
287 boundary = malloc(SIZ);
288 memset(boundary, 0, SIZ);
290 startary = malloc(SIZ);
291 memset(startary, 0, SIZ);
293 endary = malloc(SIZ);
294 memset(endary, 0, SIZ);
296 header = malloc(SIZ);
297 memset(header, 0, SIZ);
299 content_type = malloc(SIZ);
300 memset(content_type, 0, SIZ);
302 charset = malloc(SIZ);
303 memset(charset, 0, SIZ);
305 encoding = malloc(SIZ);
306 memset(encoding, 0, SIZ);
308 content_type_name = malloc(SIZ);
309 memset(content_type_name, 0, SIZ);
311 content_disposition_name = malloc(SIZ);
312 memset(content_disposition_name, 0, SIZ);
314 filename = malloc(SIZ);
315 memset(filename, 0, SIZ);
317 disposition = malloc(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) {
334 if (isspace(buf[i])) {
339 if (!isspace(buf[0])) {
340 if (!strncasecmp(header, "Content-type: ", 14)) {
341 strcpy(content_type, &header[14]);
342 extract_key(content_type_name, content_type, "name");
343 extract_key(charset, content_type, "charset");
344 /* Deal with weird headers */
345 if (strchr(content_type, ' '))
346 *(strchr(content_type, ' ')) = '\0';
347 if (strchr(content_type, ';'))
348 *(strchr(content_type, ';')) = '\0';
350 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
351 strcpy(disposition, &header[21]);
352 extract_key(content_disposition_name, disposition, "name");
353 extract_key(filename, disposition, "filename");
355 if (!strncasecmp(header, "Content-length: ", 16)) {
356 content_length = (size_t) atol(&header[16]);
358 if (!strncasecmp(header,
359 "Content-transfer-encoding: ", 27))
360 strcpy(encoding, &header[27]);
361 if (strlen(boundary) == 0)
362 extract_key(boundary, header, "boundary");
365 if ((strlen(header) + strlen(buf) + 2) < SIZ)
367 } while ((strlen(buf) > 0) && (*ptr != 0));
369 if (strchr(disposition, ';'))
370 *(strchr(disposition, ';')) = '\0';
371 striplt(disposition);
372 if (strchr(content_type, ';'))
373 *(strchr(content_type, ';')) = '\0';
374 striplt(content_type);
376 if (strlen(boundary) > 0) {
382 /* If this is a multipart message, then recursively process it */
386 /* Tell the client about this message's multipartedness */
387 if (PreMultiPartCallBack != NULL) {
388 PreMultiPartCallBack("", "", partnum, "",
389 NULL, content_type, charset,
390 0, encoding, userdata);
393 /* Figure out where the boundaries are */
394 snprintf(startary, SIZ, "--%s", boundary);
395 snprintf(endary, SIZ, "--%s--", boundary);
396 startary_len = strlen(startary);
401 next_boundary = NULL;
402 for (srch=ptr; srch<content_end; ++srch) {
403 if (!memcmp(srch, startary, startary_len)) {
404 next_boundary = srch;
409 if ( (part_start != NULL) && (next_boundary != NULL) ) {
410 part_end = next_boundary;
413 if (strlen(partnum) > 0) {
414 snprintf(nested_partnum,
415 sizeof nested_partnum,
420 snprintf(nested_partnum,
421 sizeof nested_partnum,
424 the_mime_parser(nested_partnum,
425 part_start, part_end,
427 PreMultiPartCallBack,
428 PostMultiPartCallBack,
433 if (next_boundary != NULL) {
434 /* If we pass out of scope, don't attempt to
435 * read past the end boundary. */
436 if (!strcmp(next_boundary, endary)) {
440 /* Set up for the next part. */
441 part_start = strstr(next_boundary, "\n");
447 /* Invalid end of multipart. Bail out! */
450 } while ( (ptr < content_end) && (next_boundary != NULL) );
452 if (PostMultiPartCallBack != NULL) {
453 PostMultiPartCallBack("", "", partnum, "", NULL,
454 content_type, charset, 0, encoding, userdata);
459 /* If it's not a multipart message, then do something with it */
463 while (ptr < content_end) {
467 part_end = content_end;
468 /* fix an off-by-one error */
472 /* Truncate if the header told us to */
473 if ( (content_length > 0) && (length > content_length) ) {
474 length = content_length;
477 /* Sometimes the "name" field is tacked on to Content-type,
478 * and sometimes it's tacked on to Content-disposition. Use
479 * whichever one we have.
481 if (strlen(content_disposition_name) > strlen(content_type_name)) {
482 name = content_disposition_name;
485 name = content_type_name;
490 content_type, charset, encoding, disposition,
492 CallBack, NULL, NULL,
493 userdata, dont_decode);
496 end_parser: /* free the buffers! end the oppression!! */
504 free(content_type_name);
505 free(content_disposition_name);
513 * Entry point for the MIME parser.
514 * (This function expects to be fed HEADERS + CONTENT)
515 * Note: NULL can be supplied as content_end; in this case, the message is
516 * considered to have ended when the parser encounters a 0x00 byte.
518 void mime_parser(char *content_start,
533 void (*PreMultiPartCallBack)
545 void (*PostMultiPartCallBack)
562 the_mime_parser("", content_start, content_end,
564 PreMultiPartCallBack,
565 PostMultiPartCallBack,
566 userdata, dont_decode);