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.
15 #include <sys/types.h>
23 void extract_key(char *target, char *source, char *key)
27 strcpy(target, source);
28 for (a = 0; a < strlen(target); ++a) {
29 if ((!strncasecmp(&target[a], key, strlen(key)))
30 && (target[a + strlen(key)] == '=')) {
31 strcpy(target, &target[a + strlen(key) + 1]);
33 strcpy(target, &target[1]);
34 for (b = 0; b < strlen(target); ++b)
45 * For non-multipart messages, we need to generate a quickie partnum of "1"
46 * to return to callback functions. Some callbacks demand it.
48 char *fixed_partnum(char *supplied_partnum) {
49 if (supplied_partnum == NULL) return "1";
50 if (strlen(supplied_partnum)==0) return "1";
51 return supplied_partnum;
57 * Convert "quoted-printable" to binary. Returns number of bytes decoded.
59 int decode_quoted_printable(char *decoded, char *encoded, int sourcelen) {
62 int soft_line_break = 0;
64 int decoded_length = 0;
72 for (i = 0; i < sourcelen; ++i) {
74 buf[buf_length++] = encoded[i];
76 if ( (encoded[i] == '\n')
78 || (i == (sourcelen-1)) ) {
79 buf[buf_length++] = 0;
81 /*** begin -- process one line ***/
83 if (buf[strlen(buf)-1] == '\n') {
84 buf[strlen(buf)-1] = 0;
86 if (buf[strlen(buf)-1] == '\r') {
87 buf[strlen(buf)-1] = 0;
89 while (isspace(buf[strlen(buf)-1])) {
90 buf[strlen(buf)-1] = 0;
94 while (strlen(buf) > 0) {
95 if (!strcmp(buf, "=")) {
98 } else if ((strlen(buf)>=3) && (buf[0]=='=')) {
99 sscanf(&buf[1], "%02x", &ch);
100 decoded[decoded_length++] = ch;
101 strcpy(buf, &buf[3]);
103 decoded[decoded_length++] = buf[0];
104 strcpy(buf, &buf[1]);
107 if (soft_line_break == 0) {
108 decoded[decoded_length++] = '\r';
109 decoded[decoded_length++] = '\n';
112 /*** end -- process one line ***/
116 decoded[decoded_length++] = 0;
117 return(decoded_length);
121 * Given a message or message-part body and a length, handle any necessary
122 * decoding and pass the request up the stack.
124 void mime_decode(char *partnum,
125 char *part_start, size_t length,
126 char *content_type, char *encoding,
128 char *name, char *filename,
139 void (*PreMultiPartCallBack)
149 void (*PostMultiPartCallBack)
165 size_t bytes_decoded = 0;
167 fprintf(stderr, "mime_decode() called\n");
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);
187 if ((strcasecmp(encoding, "base64"))
188 && (strcasecmp(encoding, "quoted-printable"))) {
189 fprintf(stderr, "ERROR: unknown MIME encoding '%s'\n", encoding);
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 fprintf(stderr, "About to allocate %d bytes for decoded part\n",
200 decoded = malloc(length+2048);
201 if (decoded == NULL) {
202 fprintf(stderr, "ERROR: cannot allocate memory.\n");
205 fprintf(stderr, "Got it!\n");
207 fprintf(stderr, "Decoding %s\n", encoding);
208 if (!strcasecmp(encoding, "base64")) {
209 bytes_decoded = decode_base64(decoded, part_start);
211 else if (!strcasecmp(encoding, "quoted-printable")) {
212 bytes_decoded = decode_quoted_printable(decoded,
215 fprintf(stderr, "Bytes decoded: %d\n", bytes_decoded);
217 if (bytes_decoded > 0) if (CallBack != NULL) {
218 CallBack(name, filename, fixed_partnum(partnum),
219 disposition, decoded,
220 content_type, bytes_decoded, "binary", userdata);
227 * Break out the components of a multipart message
228 * (This function expects to be fed HEADERS + CONTENT)
229 * Note: NULL can be supplied as content_end; in this case, the message is
230 * considered to have ended when the parser encounters a 0x00 byte.
232 void the_mime_parser(char *partnum,
233 char *content_start, char *content_end,
244 void (*PreMultiPartCallBack)
254 void (*PostMultiPartCallBack)
270 char *part_start, *part_end = NULL;
277 size_t content_length;
286 char nested_partnum[SIZ];
288 fprintf(stderr, "the_mime_parser() called\n");
292 boundary = malloc(SIZ);
293 memset(boundary, 0, SIZ);
295 startary = malloc(SIZ);
296 memset(startary, 0, SIZ);
298 endary = malloc(SIZ);
299 memset(endary, 0, SIZ);
301 header = malloc(SIZ);
302 memset(header, 0, SIZ);
304 content_type = malloc(SIZ);
305 memset(content_type, 0, SIZ);
307 encoding = malloc(SIZ);
308 memset(encoding, 0, SIZ);
311 memset(name, 0, SIZ);
313 filename = malloc(SIZ);
314 memset(filename, 0, SIZ);
316 disposition = malloc(SIZ);
317 memset(disposition, 0, SIZ);
319 /* If the caller didn't supply an endpointer, generate one by measure */
320 if (content_end == NULL) {
321 content_end = &content_start[strlen(content_start)];
324 /* Learn interesting things from the headers */
327 ptr = memreadline(ptr, buf, SIZ);
328 if (ptr >= content_end) {
332 for (i = 0; i < strlen(buf); ++i)
335 if (!isspace(buf[0])) {
336 if (!strncasecmp(header, "Content-type: ", 14)) {
337 strcpy(content_type, &header[14]);
338 extract_key(name, content_type, "name");
339 fprintf(stderr, "Extracted content-type <%s>\n",
342 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
343 strcpy(disposition, &header[21]);
344 extract_key(filename, disposition, "filename");
346 if (!strncasecmp(header, "Content-length: ", 16)) {
347 content_length = (size_t) atol(&header[16]);
349 if (!strncasecmp(header,
350 "Content-transfer-encoding: ", 27))
351 strcpy(encoding, &header[27]);
352 if (strlen(boundary) == 0)
353 extract_key(boundary, header, "boundary");
356 if ((strlen(header) + strlen(buf) + 2) < SIZ)
358 } while ((strlen(buf) > 0) && (*ptr != 0));
360 for (i = 0; i < strlen(disposition); ++i)
361 if (disposition[i] == ';')
363 while (isspace(disposition[0]))
364 strcpy(disposition, &disposition[1]);
365 for (i = 0; i < strlen(content_type); ++i)
366 if (content_type[i] == ';')
368 while (isspace(content_type[0]))
369 strcpy(content_type, &content_type[1]);
371 if (strlen(boundary) > 0) {
377 fprintf(stderr, "is_multipart=%d, boundary=<%s>\n",
378 is_multipart, boundary);
380 /* If this is a multipart message, then recursively process it */
384 /* Tell the client about this message's multipartedness */
385 if (PreMultiPartCallBack != NULL) {
386 PreMultiPartCallBack("", "", partnum, "",
388 0, encoding, userdata);
391 /* Figure out where the boundaries are */
392 sprintf(startary, "--%s", boundary);
393 sprintf(endary, "--%s--", boundary);
395 if ( (!strncasecmp(ptr, startary, strlen(startary)))
396 || (!strncasecmp(ptr, endary, strlen(endary))) ) {
397 fprintf(stderr, "hit boundary!\n");
398 if (part_start != NULL) {
399 if (strlen(partnum) > 0) {
400 sprintf(nested_partnum, "%s.%d",
401 partnum, ++part_seq);
404 sprintf(nested_partnum, "%d",
407 the_mime_parser(nested_partnum,
408 part_start, part_end,
410 PreMultiPartCallBack,
411 PostMultiPartCallBack,
415 ptr = memreadline(ptr, buf, SIZ);
422 } while ( (strcasecmp(ptr, endary)) && (ptr <= content_end) );
423 if (PostMultiPartCallBack != NULL) {
424 PostMultiPartCallBack("", "", partnum, "", NULL,
425 content_type, 0, encoding, userdata);
430 /* If it's not a multipart message, then do something with it */
432 fprintf(stderr, "doing non-multipart thing\n");
435 while (ptr < content_end) {
439 part_end = content_end;
441 /* Truncate if the header told us to */
442 if ( (content_length > 0) && (length > content_length) ) {
443 length = content_length;
444 fprintf(stderr, "truncated to %ld\n", (long)content_length);
449 content_type, encoding, disposition,
451 CallBack, NULL, NULL,
452 userdata, dont_decode);
455 end_parser: /* free the buffers! end the oppression!! */
470 * Entry point for the MIME parser.
471 * (This function expects to be fed HEADERS + CONTENT)
472 * Note: NULL can be supplied as content_end; in this case, the message is
473 * considered to have ended when the parser encounters a 0x00 byte.
475 void mime_parser(char *content_start,
489 void (*PreMultiPartCallBack)
500 void (*PostMultiPartCallBack)
516 fprintf(stderr, "mime_parser() called\n");
517 the_mime_parser("", content_start, content_end,
519 PreMultiPartCallBack,
520 PostMultiPartCallBack,
521 userdata, dont_decode);