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);
226 * Break out the components of a multipart message
227 * (This function expects to be fed HEADERS + CONTENT)
228 * Note: NULL can be supplied as content_end; in this case, the message is
229 * considered to have ended when the parser encounters a 0x00 byte.
231 void the_mime_parser(char *partnum,
232 char *content_start, char *content_end,
243 void (*PreMultiPartCallBack)
253 void (*PostMultiPartCallBack)
269 char *part_start, *part_end = NULL;
275 char content_type[SIZ];
276 size_t content_length;
278 char disposition[SIZ];
285 char nested_partnum[SIZ];
287 fprintf(stderr, "the_mime_parser() called\n");
289 memset(boundary, 0, sizeof boundary);
290 memset(content_type, 0, sizeof content_type);
291 memset(encoding, 0, sizeof encoding);
292 memset(name, 0, sizeof name);
293 memset(filename, 0, sizeof filename);
294 memset(disposition, 0, sizeof disposition);
297 /* If the caller didn't supply an endpointer, generate one by measure */
298 if (content_end == NULL) {
299 content_end = &content_start[strlen(content_start)];
302 /* Learn interesting things from the headers */
305 ptr = memreadline(ptr, buf, sizeof buf);
306 if (ptr >= content_end)
309 for (i = 0; i < strlen(buf); ++i)
312 if (!isspace(buf[0])) {
313 if (!strncasecmp(header, "Content-type: ", 14)) {
314 strcpy(content_type, &header[14]);
315 extract_key(name, content_type, "name");
317 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
318 strcpy(disposition, &header[21]);
319 extract_key(filename, disposition, "filename");
321 if (!strncasecmp(header, "Content-length: ", 16)) {
322 content_length = (size_t) atol(&header[16]);
324 if (!strncasecmp(header,
325 "Content-transfer-encoding: ", 27))
326 strcpy(encoding, &header[27]);
327 if (strlen(boundary) == 0)
328 extract_key(boundary, header, "boundary");
331 if ((strlen(header) + strlen(buf) + 2) < sizeof(header))
333 } while ((strlen(buf) > 0) && (*ptr != 0));
335 for (i = 0; i < strlen(disposition); ++i)
336 if (disposition[i] == ';')
338 while (isspace(disposition[0]))
339 strcpy(disposition, &disposition[1]);
340 for (i = 0; i < strlen(content_type); ++i)
341 if (content_type[i] == ';')
343 while (isspace(content_type[0]))
344 strcpy(content_type, &content_type[1]);
346 if (strlen(boundary) > 0) {
352 fprintf(stderr, "is_multipart=%d, boundary=<%s>\n",
353 is_multipart, boundary);
355 /* If this is a multipart message, then recursively process it */
359 /* Tell the client about this message's multipartedness */
360 if (PreMultiPartCallBack != NULL) {
361 PreMultiPartCallBack("", "", partnum, "",
363 0, encoding, userdata);
366 /* Figure out where the boundaries are */
367 sprintf(startary, "--%s", boundary);
368 sprintf(endary, "--%s--", boundary);
370 /* if (ptr >= content_end) goto END_MULTI; */
372 if ( (!strncasecmp(ptr, startary, strlen(startary)))
373 || (!strncasecmp(ptr, endary, strlen(endary))) ) {
374 fprintf(stderr, "hit boundary!\n");
375 if (part_start != NULL) {
376 if (strlen(partnum) > 0) {
377 sprintf(nested_partnum, "%s.%d",
378 partnum, ++part_seq);
381 sprintf(nested_partnum, "%d",
384 the_mime_parser(nested_partnum,
385 part_start, part_end,
387 PreMultiPartCallBack,
388 PostMultiPartCallBack,
392 ptr = memreadline(ptr, buf, sizeof(buf));
399 } while ( (strcasecmp(ptr, endary)) && (ptr <= content_end) );
400 if (PostMultiPartCallBack != NULL) {
401 PostMultiPartCallBack("", "", partnum, "", NULL,
402 content_type, 0, encoding, userdata);
407 /* If it's not a multipart message, then do something with it */
409 fprintf(stderr, "doing non-multipart thing\n");
412 while (ptr < content_end) {
416 part_end = content_end;
418 /* Truncate if the header told us to */
419 if ( (content_length > 0) && (length > content_length) ) {
420 length = content_length;
421 fprintf(stderr, "truncated to %d\n", content_length);
426 content_type, encoding, disposition,
428 CallBack, NULL, NULL,
429 userdata, dont_decode);
436 * Entry point for the MIME parser.
437 * (This function expects to be fed HEADERS + CONTENT)
438 * Note: NULL can be supplied as content_end; in this case, the message is
439 * considered to have ended when the parser encounters a 0x00 byte.
441 void mime_parser(char *content_start,
455 void (*PreMultiPartCallBack)
466 void (*PostMultiPartCallBack)
482 fprintf(stderr, "mime_parser() called\n");
483 the_mime_parser("", content_start, content_end,
485 PreMultiPartCallBack,
486 PostMultiPartCallBack,
487 userdata, dont_decode);