4 * This is the MIME parser for Citadel. Sometimes it actually works.
6 * Copyright (c) 1998-2001 by Art Cancro
7 * This code is distributed under the terms of the GNU General Public License.
15 #include <sys/types.h>
22 #include "mime_parser.h"
25 void extract_key(char *target, char *source, char *key)
29 strcpy(target, source);
30 for (a = 0; a < strlen(target); ++a) {
31 if ((!strncasecmp(&target[a], key, strlen(key)))
32 && (target[a + strlen(key)] == '=')) {
33 strcpy(target, &target[a + strlen(key) + 1]);
35 strcpy(target, &target[1]);
36 for (b = 0; b < strlen(target); ++b)
47 * For non-multipart messages, we need to generate a quickie partnum of "1"
48 * to return to callback functions. Some callbacks demand it.
50 char *fixed_partnum(char *supplied_partnum) {
51 if (supplied_partnum == NULL) return "1";
52 if (strlen(supplied_partnum)==0) return "1";
53 return supplied_partnum;
59 * Convert "quoted-printable" to binary. Returns number of bytes decoded.
61 int decode_quoted_printable(char *decoded, char *encoded, int sourcelen) {
64 int soft_line_break = 0;
66 int decoded_length = 0;
74 for (i = 0; i < sourcelen; ++i) {
76 buf[buf_length++] = encoded[i];
78 if ( (encoded[i] == '\n')
80 || (i == (sourcelen-1)) ) {
81 buf[buf_length++] = 0;
83 /*** begin -- process one line ***/
85 if (buf[strlen(buf)-1] == '\n') {
86 buf[strlen(buf)-1] = 0;
88 if (buf[strlen(buf)-1] == '\r') {
89 buf[strlen(buf)-1] = 0;
91 while (isspace(buf[strlen(buf)-1])) {
92 buf[strlen(buf)-1] = 0;
96 while (strlen(buf) > 0) {
97 if (!strcmp(buf, "=")) {
100 } else if ((strlen(buf)>=3) && (buf[0]=='=')) {
101 sscanf(&buf[1], "%02x", &ch);
102 decoded[decoded_length++] = ch;
103 strcpy(buf, &buf[3]);
105 decoded[decoded_length++] = buf[0];
106 strcpy(buf, &buf[1]);
109 if (soft_line_break == 0) {
110 decoded[decoded_length++] = '\r';
111 decoded[decoded_length++] = '\n';
114 /*** end -- process one line ***/
118 decoded[decoded_length++] = 0;
119 return(decoded_length);
123 * Given a message or message-part body and a length, handle any necessary
124 * decoding and pass the request up the stack.
126 void mime_decode(char *partnum,
127 char *part_start, size_t length,
128 char *content_type, char *encoding,
130 char *name, char *filename,
141 void (*PreMultiPartCallBack)
151 void (*PostMultiPartCallBack)
167 size_t bytes_decoded = 0;
169 /* Some encodings aren't really encodings */
170 if (!strcasecmp(encoding, "7bit"))
171 strcpy(encoding, "");
172 if (!strcasecmp(encoding, "8bit"))
173 strcpy(encoding, "");
174 if (!strcasecmp(encoding, "binary"))
175 strcpy(encoding, "");
177 /* If this part is not encoded, send as-is */
178 if ( (strlen(encoding) == 0) || (dont_decode)) {
179 if (CallBack != NULL) {
180 CallBack(name, filename, fixed_partnum(partnum),
181 disposition, part_start,
182 content_type, length, encoding, userdata);
186 if ((strcasecmp(encoding, "base64"))
187 && (strcasecmp(encoding, "quoted-printable"))) {
188 fprintf(stderr, "ERROR: unknown MIME encoding '%s'\n", encoding);
192 * Allocate a buffer for the decoded data. The output buffer is the
193 * same size as the input buffer; this assumes that the decoded data
194 * will never be larger than the encoded data. This is a safe
195 * assumption with base64, uuencode, and quoted-printable. Just to
196 * be safe, we still pad the buffer a bit.
198 decoded = malloc(length + 1024);
199 if (decoded == NULL) {
200 fprintf(stderr, "ERROR: cannot allocate memory.\n");
204 if (!strcasecmp(encoding, "base64")) {
205 bytes_decoded = decode_base64(decoded, part_start);
207 else if (!strcasecmp(encoding, "quoted-printable")) {
208 bytes_decoded = decode_quoted_printable(decoded,
212 if (bytes_decoded > 0) if (CallBack != NULL) {
213 CallBack(name, filename, fixed_partnum(partnum),
214 disposition, decoded,
215 content_type, bytes_decoded, "binary", userdata);
225 * Break out the components of a multipart message
226 * (This function expects to be fed HEADERS + CONTENT)
227 * Note: NULL can be supplied as content_end; in this case, the message is
228 * considered to have ended when the parser encounters a 0x00 byte.
230 void the_mime_parser(char *partnum,
231 char *content_start, char *content_end,
242 void (*PreMultiPartCallBack)
252 void (*PostMultiPartCallBack)
268 char *part_start, *part_end = NULL;
275 size_t content_length;
284 char nested_partnum[SIZ];
286 fprintf(stderr, "the_mime_parser() called\n");
290 boundary = malloc(SIZ);
291 memset(boundary, 0, SIZ);
293 startary = malloc(SIZ);
294 memset(startary, 0, SIZ);
296 endary = malloc(SIZ);
297 memset(endary, 0, SIZ);
299 header = malloc(SIZ);
300 memset(header, 0, SIZ);
302 content_type = malloc(SIZ);
303 memset(content_type, 0, SIZ);
305 encoding = malloc(SIZ);
306 memset(encoding, 0, SIZ);
309 memset(name, 0, SIZ);
311 filename = malloc(SIZ);
312 memset(filename, 0, SIZ);
314 disposition = malloc(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, sizeof buf);
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(name, content_type, "name");
337 fprintf(stderr, "Extracted content-type <%s>\n",
340 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
341 strcpy(disposition, &header[21]);
342 extract_key(filename, disposition, "filename");
344 if (!strncasecmp(header, "Content-length: ", 16)) {
345 content_length = (size_t) atol(&header[16]);
347 if (!strncasecmp(header,
348 "Content-transfer-encoding: ", 27))
349 strcpy(encoding, &header[27]);
350 if (strlen(boundary) == 0)
351 extract_key(boundary, header, "boundary");
354 if ((strlen(header) + strlen(buf) + 2) < sizeof(header))
356 } while ((strlen(buf) > 0) && (*ptr != 0));
358 for (i = 0; i < strlen(disposition); ++i)
359 if (disposition[i] == ';')
361 while (isspace(disposition[0]))
362 strcpy(disposition, &disposition[1]);
363 for (i = 0; i < strlen(content_type); ++i)
364 if (content_type[i] == ';')
366 while (isspace(content_type[0]))
367 strcpy(content_type, &content_type[1]);
369 if (strlen(boundary) > 0) {
375 fprintf(stderr, "is_multipart=%d, boundary=<%s>\n",
376 is_multipart, boundary);
378 /* If this is a multipart message, then recursively process it */
382 /* Tell the client about this message's multipartedness */
383 if (PreMultiPartCallBack != NULL) {
384 PreMultiPartCallBack("", "", partnum, "",
386 0, encoding, userdata);
389 /* Figure out where the boundaries are */
390 sprintf(startary, "--%s", boundary);
391 sprintf(endary, "--%s--", boundary);
393 if ( (!strncasecmp(ptr, startary, strlen(startary)))
394 || (!strncasecmp(ptr, endary, strlen(endary))) ) {
395 fprintf(stderr, "hit boundary!\n");
396 if (part_start != NULL) {
397 if (strlen(partnum) > 0) {
398 sprintf(nested_partnum, "%s.%d",
399 partnum, ++part_seq);
402 sprintf(nested_partnum, "%d",
405 the_mime_parser(nested_partnum,
406 part_start, part_end,
408 PreMultiPartCallBack,
409 PostMultiPartCallBack,
413 ptr = memreadline(ptr, buf, sizeof(buf));
420 } while ( (strcasecmp(ptr, endary)) && (ptr <= content_end) );
421 if (PostMultiPartCallBack != NULL) {
422 PostMultiPartCallBack("", "", partnum, "", NULL,
423 content_type, 0, encoding, userdata);
428 /* If it's not a multipart message, then do something with it */
430 fprintf(stderr, "doing non-multipart thing\n");
433 while (ptr < content_end) {
437 part_end = content_end;
439 /* Truncate if the header told us to */
440 if ( (content_length > 0) && (length > content_length) ) {
441 length = content_length;
442 fprintf(stderr, "truncated to %ld\n", (long)content_length);
447 content_type, encoding, disposition,
449 CallBack, NULL, NULL,
450 userdata, dont_decode);
453 end_parser: /* free the buffers! end the oppression!! */
472 * Entry point for the MIME parser.
473 * (This function expects to be fed HEADERS + CONTENT)
474 * Note: NULL can be supplied as content_end; in this case, the message is
475 * considered to have ended when the parser encounters a 0x00 byte.
477 void mime_parser(char *content_start,
491 void (*PreMultiPartCallBack)
502 void (*PostMultiPartCallBack)
518 fprintf(stderr, "mime_parser() called\n");
519 the_mime_parser("", content_start, content_end,
521 PreMultiPartCallBack,
522 PostMultiPartCallBack,
523 userdata, dont_decode);