Added GPL declaration to source files in libcitadel
[citadel.git] / libcitadel / lib / mime_parser.c
1 /*
2  * This is the MIME parser for Citadel.
3  *
4  * Copyright (c) 1998-2010 by the citadel.org development team.
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <signal.h>
25 #include <sys/types.h>
26 #include <ctype.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <dirent.h>
31 #include <errno.h>
32
33 #include "xdgmime/xdgmime.h"
34 #include "libcitadel.h"
35 #include "libcitadellocal.h"
36
37 const unsigned char FromHexTable [256] = {
38         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //  0
39         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 10
40         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 20
41         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 30
42         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x01, // 40
43         0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0xFF, // 50
44         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, // 60
45         0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 70
46         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 80
47         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0A, 0x0B, 0x0C, // 90
48         0x0D, 0x0E, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //100
49         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //110
50         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //120
51         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //130
52         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //140
53         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //150
54         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //160
55         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //170
56         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //180
57         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //190
58         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //200
59         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //210
60         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //220
61         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //230
62         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //240
63         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF                          //250
64 };
65
66
67 long extract_key(char *target, char *source, long sourcelen, char *key, long keylen, char KeyEnd)
68 {
69         char *sptr, *ptr = NULL;
70         int double_quotes = 0;
71         long RealKeyLen = keylen;
72
73         sptr = source;
74
75         while (sptr != NULL)
76         {
77                 ptr = bmstrcasestr_len(sptr, sourcelen - (sptr - source), 
78                                        key, keylen);
79                 if(ptr != NULL)
80                 {
81                         while (isspace(*(ptr + RealKeyLen)))
82                                 RealKeyLen ++;
83                         if (*(ptr + RealKeyLen) == KeyEnd)
84                         {
85                                 sptr = NULL;
86                                 RealKeyLen ++;                          
87                         }
88                         else
89                         {
90                                 sptr = ptr + RealKeyLen + 1;
91                         }
92                 }
93                 else 
94                         sptr = ptr;
95         }
96         if (ptr == NULL) {
97                 *target = '\0';
98                 return 0;
99         }
100         strcpy(target, (ptr + RealKeyLen));
101
102         for (ptr=target; (*ptr != 0); ptr++) {
103
104                 /* A semicolon means we've hit the end of the key, unless we're inside double quotes */
105                 if ( (double_quotes != 1) && (*ptr == ';')) {
106                         *ptr = 0;
107                 }
108
109                 /* if we find double quotes, we've got a great set of string boundaries */
110                 if (*ptr == '\"') {
111                         ++double_quotes;
112                         if (double_quotes == 1) {
113                                 strcpy(ptr, ptr+1);
114                         }
115                         else {
116                                 *ptr = 0;
117                         }
118                 }
119         }
120         *ptr = '\0';
121         return ptr - target;
122 }
123
124
125 /*
126  * For non-multipart messages, we need to generate a quickie partnum of "1"
127  * to return to callback functions.  Some callbacks demand it.
128  */
129 char *fixed_partnum(char *supplied_partnum) {
130         if (supplied_partnum == NULL) return "1";
131         if (strlen(supplied_partnum)==0) return "1";
132         return supplied_partnum;
133 }
134
135
136 static inline unsigned int _decode_hex(const char *Source)
137 {
138         int ret = '?';
139         unsigned char LO_NIBBLE;
140         unsigned char HI_NIBBLE;
141
142         HI_NIBBLE = FromHexTable[(unsigned char) *Source];
143         LO_NIBBLE = FromHexTable[(unsigned char) *(Source+1)];
144         
145         if ((LO_NIBBLE == 0xFF) || (LO_NIBBLE == 0xFF))
146                 return ret;
147         ret = HI_NIBBLE;
148         ret = ret << 4;
149         ret = ret | LO_NIBBLE;
150         return ret;
151 }
152
153 unsigned int decode_hex(char *Source) {return _decode_hex(Source);}
154
155 /*
156  * Convert "quoted-printable" to binary.  Returns number of bytes decoded.
157  * according to RFC2045 section 6.7
158  */
159 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
160         unsigned int ch;
161         int decoded_length = 0;
162         int pos = 0;
163
164         while (pos < sourcelen)
165         {
166                 if (*(encoded + pos) == '=')
167                 {
168                         pos ++;
169                         if (*(encoded + pos) == '\n')
170                         {
171                                 pos ++;
172                         }
173                         else if (*(encoded + pos) == '\r')
174                         {
175                                 pos ++;
176                                 if (*(encoded + pos) == '\n')
177                                         pos++;
178                         }
179                         else
180                         {
181                                 ch = 0;
182                                 ch = _decode_hex(&encoded[pos]);
183                                 pos += 2;
184                                 decoded[decoded_length++] = ch;
185                         }
186                 }
187                 else
188                 {
189                         decoded[decoded_length++] = encoded[pos];
190                         pos += 1;
191                 }
192         }
193         decoded[decoded_length] = 0;
194         return(decoded_length);
195 }
196
197
198 /*
199  * Given a message or message-part body and a length, handle any necessary
200  * decoding and pass the request up the stack.
201  */
202 void mime_decode(char *partnum,
203                  char *part_start, size_t length,
204                  char *content_type, char *charset, char *encoding,
205                  char *disposition,
206                  char *id,
207                  char *name, char *filename,
208                  MimeParserCallBackType CallBack,
209                  MimeParserCallBackType PreMultiPartCallBack,
210                  MimeParserCallBackType PostMultiPartCallBack,
211                  void *userdata,
212                  int dont_decode)
213 {
214
215         char *decoded;
216         size_t bytes_decoded = 0;
217
218         /* Some encodings aren't really encodings */
219         if (!strcasecmp(encoding, "7bit"))
220                 strcpy(encoding, "");
221         if (!strcasecmp(encoding, "8bit"))
222                 strcpy(encoding, "");
223         if (!strcasecmp(encoding, "binary"))
224                 strcpy(encoding, "");
225         if (!strcasecmp(encoding, "ISO-8859-1"))
226                 strcpy(encoding, "");
227
228         /* If this part is not encoded, send as-is */
229         if ( (strlen(encoding) == 0) || (dont_decode)) {
230                 if (CallBack != NULL) {
231                         CallBack(name, 
232                                  filename, 
233                                  fixed_partnum(partnum),
234                                  disposition, 
235                                  part_start,
236                                  content_type, 
237                                  charset, 
238                                  length, 
239                                  encoding, 
240                                  id,
241                                  userdata);
242                         }
243                 return;
244         }
245         
246         /* Fail silently if we hit an unknown encoding. */
247         if ((strcasecmp(encoding, "base64"))
248             && (strcasecmp(encoding, "quoted-printable"))) {
249                 return;
250         }
251
252         /*
253          * Allocate a buffer for the decoded data.  The output buffer is slightly
254          * larger than the input buffer; this assumes that the decoded data
255          * will never be significantly larger than the encoded data.  This is a
256          * safe assumption with base64, uuencode, and quoted-printable.
257          */
258         decoded = malloc(length + 32768);
259         if (decoded == NULL) {
260                 return;
261         }
262
263         if (!strcasecmp(encoding, "base64")) {
264                 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
265         }
266         else if (!strcasecmp(encoding, "quoted-printable")) {
267                 bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
268         }
269
270         if (bytes_decoded > 0) if (CallBack != NULL) {
271                         char encoding_buf[SIZ];
272
273                         strcpy(encoding_buf, "binary");
274                         CallBack(name, 
275                                  filename, 
276                                  fixed_partnum(partnum),
277                                  disposition, 
278                                  decoded,
279                                  content_type, 
280                                  charset, 
281                                  bytes_decoded, 
282                                  encoding_buf, 
283                                  id, 
284                                  userdata);
285         }
286
287         free(decoded);
288 }
289
290 /*
291  * this is the extract of mime_decode which can be called if 'dont_decode' was set; 
292  * to save the cpu intense process of decoding to the time when it realy wants the content. 
293  * returns: 
294  *   - > 0 we decoded something, its on *decoded, you need to free it.
295  *   - = 0 no need to decode stuff. *decoded will be NULL.
296  *   - < 0 an error occured, either an unknown encoding, or alloc failed. no need to free.
297  */
298 int mime_decode_now (char *part_start, 
299                      size_t length,
300                      char *encoding,
301                      char **decoded,
302                      size_t *bytes_decoded)
303 {
304         *bytes_decoded = 0;
305         *decoded = NULL;
306         /* Some encodings aren't really encodings */
307         if (!strcasecmp(encoding, "7bit"))
308                 strcpy(encoding, "");
309         if (!strcasecmp(encoding, "8bit"))
310                 strcpy(encoding, "");
311         if (!strcasecmp(encoding, "binary"))
312                 strcpy(encoding, "");
313
314         /* If this part is not encoded, send as-is */
315         if (strlen(encoding) == 0) {
316                 return 0;
317         }
318         
319
320         /* Fail if we hit an unknown encoding. */
321         if ((strcasecmp(encoding, "base64"))
322             && (strcasecmp(encoding, "quoted-printable"))) {
323                 return -1;
324         }
325
326         /*
327          * Allocate a buffer for the decoded data.  The output buffer is slightly
328          * larger than the input buffer; this assumes that the decoded data
329          * will never be significantly larger than the encoded data.  This is a
330          * safe assumption with base64, uuencode, and quoted-printable.
331          */
332         *decoded = malloc(length + 32768);
333         if (decoded == NULL) {
334                 return -1;
335         }
336
337         if (!strcasecmp(encoding, "base64")) {
338                 *bytes_decoded = CtdlDecodeBase64(*decoded, part_start, length);
339                 return 1;
340         }
341         else if (!strcasecmp(encoding, "quoted-printable")) {
342                 *bytes_decoded = CtdlDecodeQuotedPrintable(*decoded, part_start, length);
343                 return 1;
344         }
345         return -1;
346 }
347
348 typedef enum _eIntMimeHdrs {
349         boundary,
350         startary,
351         endary,
352         content_type,
353         charset,
354         encoding,
355         content_type_name,
356         content_disposition_name,
357         filename,
358         disposition,
359         id,
360         eMax /* don't move ! */
361 } eIntMimeHdrs;
362
363 typedef struct _CBufStr {
364         char Key[SIZ];
365         long len;
366 }CBufStr;
367
368 typedef struct _interesting_mime_headers {
369         CBufStr b[eMax];
370         long content_length;
371         long is_multipart;
372 } interesting_mime_headers;
373
374
375 static void FlushInterestingMimes(interesting_mime_headers *m)
376 {
377         int i;
378         
379         for (i = 0; i < eMax; i++) {
380              m->b[i].Key[0] = '\0';
381              m->b[i].len = 0;
382         }
383         m->content_length = -1;
384 }
385 static interesting_mime_headers *InitInterestingMimes(void)
386 {
387         interesting_mime_headers *m;
388         m = (interesting_mime_headers*) malloc( sizeof(interesting_mime_headers));
389
390         FlushInterestingMimes(m);
391
392         return m;
393 }
394
395
396 static long parse_MimeHeaders(interesting_mime_headers *m, 
397                               char** pcontent_start, 
398                               char *content_end)
399 {
400         char buf[SIZ];
401         char header[SIZ];
402         long headerlen;
403         char *ptr, *pch;
404         int buflen = 0;
405         int i;
406
407         /* Learn interesting things from the headers */
408         ptr = *pcontent_start;
409         *header = '\0';
410         headerlen = 0;
411         do {
412                 ptr = memreadlinelen(ptr, buf, SIZ, &buflen);
413
414                 for (i = 0; i < buflen; ++i) {
415                         if (isspace(buf[i])) {
416                                 buf[i] = ' ';
417                         }
418                 }
419
420                 if (!isspace(buf[0]) && (headerlen > 0)) {
421                         if (!strncasecmp(header, "Content-type:", 13)) {
422                                 memcpy (m->b[content_type].Key, &header[13], headerlen - 12);
423                                 m->b[content_type].Key[headerlen - 12] = '\0';
424                                 m->b[content_type].len = striplt (m->b[content_type].Key);
425
426                                 m->b[content_type_name].len = extract_key(m->b[content_type_name].Key, CKEY(m->b[content_type]), HKEY("name"), '=');
427                                 m->b[charset].len           = extract_key(m->b[charset].Key,           CKEY(m->b[content_type]), HKEY("charset"), '=');
428                                 m->b[boundary].len          = extract_key(m->b[boundary].Key,          header,       headerlen,  HKEY("boundary"), '=');
429
430                                 /* Deal with weird headers */
431                                 pch = strchr(m->b[content_type].Key, ' ');
432                                 if (pch != NULL) {
433                                         *pch = '\0';
434                                         m->b[content_type].len = m->b[content_type].Key - pch;
435                                 }
436                                 pch = strchr(m->b[content_type].Key, ';');
437                                 if (pch != NULL) {
438                                         *pch = '\0';
439                                         m->b[content_type].len = m->b[content_type].Key - pch;
440                                 }
441                         }
442                         else if (!strncasecmp(header, "Content-Disposition:", 20)) {
443                                 memcpy (m->b[disposition].Key, &header[20], headerlen - 19);
444                                 m->b[disposition].Key[headerlen - 19] = '\0';
445                                 m->b[disposition].len = striplt(m->b[disposition].Key);
446
447                                 m->b[content_disposition_name].len = extract_key(m->b[content_disposition_name].Key, CKEY(m->b[disposition]), HKEY("name"), '=');
448                                 m->b[filename].len                 = extract_key(m->b[filename].Key,                 CKEY(m->b[disposition]), HKEY("filename"), '=');
449                                 pch = strchr(m->b[disposition].Key, ';');
450                                 if (pch != NULL) *pch = '\0';
451                                 m->b[disposition].len = striplt(m->b[disposition].Key);
452                         }
453                         else if (!strncasecmp(header, "Content-ID:", 11)) {
454                                 memcpy(m->b[id].Key, &header[11], headerlen - 11);
455                                 m->b[id].Key[headerlen - 11] = '\0';
456                                 striplt(m->b[id].Key);
457                                 m->b[id].len = stripallbut(m->b[id].Key, '<', '>');
458                         }
459                         else if (!strncasecmp(header, "Content-length: ", 15)) {
460                                 char *clbuf;
461                                 clbuf = &header[15];
462                                 while (isspace(*clbuf))
463                                         clbuf ++;
464                                 m->content_length = (size_t) atol(clbuf);
465                         }
466                         else if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
467                                 memcpy(m->b[encoding].Key, &header[26], headerlen - 26);
468                                 m->b[encoding].Key[headerlen - 26] = '\0';
469                                 m->b[encoding].len = striplt(m->b[encoding].Key);
470                         }
471                         *header = '\0';
472                         headerlen = 0;
473                 }
474                 if ((headerlen + buflen + 2) < SIZ) {
475                         memcpy(&header[headerlen], buf, buflen);
476                         headerlen += buflen;
477                         header[headerlen] = '\0';
478                 }
479                 if (ptr >= content_end) {
480                         return -1;
481                 }
482         } while ((!IsEmptyStr(buf)) && (*ptr != 0));
483
484         m->is_multipart = m->b[boundary].len != 0;
485         *pcontent_start = ptr;
486
487         return 0;
488 }
489
490
491 static int IsAsciiEncoding(interesting_mime_headers *m)
492 {
493
494         if ((m->b[encoding].len != 0) &&
495             (strcasecmp(m->b[encoding].Key, "base64") == 0))
496                 return 1;
497         if ((m->b[encoding].len != 0) &&
498             (strcmp(m->b[encoding].Key, "quoted-printable") == 0))
499                 return 1;
500
501         return 0;
502 }
503
504 static char *FindNextContent(char *ptr,
505                              char *content_end,
506                              interesting_mime_headers *SubMimeHeaders,
507                              interesting_mime_headers *m)
508 {
509         char *next_boundary;
510         char  tmp;
511
512         if (IsAsciiEncoding(SubMimeHeaders)) {
513                 tmp = *content_end;
514                 *content_end = '\0';
515
516                 /** 
517                  * ok, if we have a content length of the mime part, 
518                  * try skipping the content on the search for the next
519                  * boundary. since we don't trust the content_length
520                  * to be all accurate, and suspect it to lose one digit 
521                  * per line with a line length of 80 chars, we need 
522                  * to start searching a little before..
523                  */
524                                    
525                 if ((SubMimeHeaders->content_length != -1) &&
526                     (SubMimeHeaders->content_length > 10))
527                 {
528                         char *pptr;
529                         long lines;
530                                         
531                         lines = SubMimeHeaders->content_length / 80;
532                         pptr = ptr + SubMimeHeaders->content_length - lines - 10;
533                         if (pptr < content_end)
534                                 ptr = pptr;
535                 }
536                         
537                 next_boundary = strstr(ptr, m->b[startary].Key);
538                 *content_end = tmp;
539         }
540         else {
541                 char *srch;
542                 /** 
543                  * ok, if we have a content length of the mime part, 
544                  * try skipping the content on the search for the next
545                  * boundary. since we don't trust the content_length
546                  * to be all accurate, start searching a little before..
547                  */
548                                    
549                 if ((SubMimeHeaders->content_length != -1) &&
550                     (SubMimeHeaders->content_length > 10))
551                 {
552                         char *pptr;
553                         pptr = ptr + SubMimeHeaders->content_length - 10;
554                         if (pptr < content_end)
555                                 ptr = pptr;
556                 }
557                 
558
559                 srch = next_boundary = NULL;
560                 for (srch = memchr(ptr, '-',  content_end - ptr);
561                      (srch != NULL) && (srch < content_end); 
562                      srch = memchr(srch, '-',  content_end - srch)) 
563                 {
564                         if (!memcmp(srch, 
565                                     m->b[startary].Key, 
566                                     m->b[startary].len)) 
567                         {
568                                 next_boundary = srch;
569                                 srch = content_end;
570                         }
571                         else srch ++;
572
573                 }
574
575         }
576         return next_boundary;
577 }
578
579 /*
580  * Break out the components of a multipart message
581  * (This function expects to be fed HEADERS + CONTENT)
582  * Note: NULL can be supplied as content_end; in this case, the message is
583  * considered to have ended when the parser encounters a 0x00 byte.
584  */
585 static void recurseable_mime_parser(char *partnum,
586                                     char *content_start, char *content_end,
587                                     MimeParserCallBackType CallBack,
588                                     MimeParserCallBackType PreMultiPartCallBack,
589                                     MimeParserCallBackType PostMultiPartCallBack,
590                                     void *userdata,
591                                     int dont_decode, 
592                                     interesting_mime_headers *m)
593 {
594         interesting_mime_headers *SubMimeHeaders;
595         char     *ptr;
596         char     *part_start;
597         char     *part_end = NULL;
598         char     *evaluate_crlf_ptr = NULL;
599         char     *next_boundary;
600         char      nested_partnum[256];
601         int       crlf_in_use = 0;
602         int       part_seq = 0;
603         CBufStr  *chosen_name;
604
605
606         /* If this is a multipart message, then recursively process it */
607         ptr = content_start;
608         part_start = NULL;
609         if (m->is_multipart) {
610
611                 /* Tell the client about this message's multipartedness */
612                 if (PreMultiPartCallBack != NULL) {
613                         PreMultiPartCallBack("", 
614                                              "", 
615                                              partnum, 
616                                              "",
617                                              NULL, 
618                                              m->b[content_type].Key, 
619                                              m->b[charset].Key,
620                                              0, 
621                                              m->b[encoding].Key, 
622                                              m->b[id].Key, 
623                                              userdata);
624                 }
625
626                 /* Figure out where the boundaries are */
627                 m->b[startary].len = snprintf(m->b[startary].Key, SIZ, "--%s", m->b[boundary].Key);
628                 SubMimeHeaders = InitInterestingMimes ();
629                 if (*ptr == '\r')
630                         ptr ++;
631                 if (*ptr == '\n')
632                         ptr ++;
633                 if (strncmp(ptr, m->b[startary].Key, m->b[startary].len) == 0)
634                         ptr += m->b[startary].len;
635                 if (*ptr == '\r')
636                         ptr ++;
637                 if (*ptr == '\n')
638                         ptr ++;
639                 part_start = NULL;
640                 do {
641                         char *optr;
642
643                         optr = ptr;
644                         if (parse_MimeHeaders(SubMimeHeaders, &ptr, content_end) != 0)
645                                 break;
646                         if ((ptr - optr > 2) && 
647                             (*(ptr - 2) == '\r'))
648                                 crlf_in_use = 1;
649                         
650                         part_start = ptr;
651                         
652                         next_boundary = FindNextContent(ptr,
653                                                         content_end,
654                                                         SubMimeHeaders,
655                                                         m);
656                         if ((next_boundary != NULL) && 
657                             (next_boundary - part_start < 3)) {
658                                 FlushInterestingMimes(SubMimeHeaders);
659
660                                 continue;
661                         }
662
663                         if ( (part_start != NULL) && (next_boundary != NULL) ) {
664                                 part_end = next_boundary;
665                                 --part_end;             /* omit the trailing LF */
666                                 if (crlf_in_use) {
667                                         --part_end;     /* omit the trailing CR */
668                                 }
669
670                                 if (!IsEmptyStr(partnum)) {
671                                         snprintf(nested_partnum,
672                                                  sizeof nested_partnum,
673                                                  "%s.%d", partnum,
674                                                  ++part_seq);
675                                 }
676                                 else {
677                                         snprintf(nested_partnum,
678                                                  sizeof nested_partnum,
679                                                  "%d", ++part_seq);
680                                 }
681                                 recurseable_mime_parser(nested_partnum,
682                                                         part_start, 
683                                                         part_end,
684                                                         CallBack,
685                                                         PreMultiPartCallBack,
686                                                         PostMultiPartCallBack,
687                                                         userdata,
688                                                         dont_decode, 
689                                                         SubMimeHeaders);
690                         }
691
692                         if (next_boundary != NULL) {
693                                 /* If we pass out of scope, don't attempt to
694                                  * read past the end boundary. */
695                                 if ((*(next_boundary + m->b[startary].len) == '-') && 
696                                     (*(next_boundary + m->b[startary].len + 1) == '-') ){
697                                         ptr = content_end;
698                                 }
699                                 else {
700                                         /* Set up for the next part. */
701                                         part_start = strstr(next_boundary, "\n");
702                                         
703                                         /* Determine whether newlines are LF or CRLF */
704                                         evaluate_crlf_ptr = part_start;
705                                         --evaluate_crlf_ptr;
706                                         if ((*evaluate_crlf_ptr == '\r') && 
707                                             (*(evaluate_crlf_ptr + 1) == '\n'))
708                                         {
709                                                 crlf_in_use = 1;
710                                         }
711                                         else {
712                                                 crlf_in_use = 0;
713                                         }
714
715                                         /* Advance past the LF ... now we're in the next part */
716                                         ++part_start;
717                                         ptr = part_start;
718                                 }
719                         }
720                         else {
721                                 /* Invalid end of multipart.  Bail out! */
722                                 ptr = content_end;
723                         }
724                         FlushInterestingMimes(SubMimeHeaders);
725                 } while ( (ptr < content_end) && (next_boundary != NULL) );
726
727                 free(SubMimeHeaders);
728
729                 if (PostMultiPartCallBack != NULL) {
730                         PostMultiPartCallBack("", 
731                                               "", 
732                                               partnum, 
733                                               "", 
734                                               NULL,
735                                               m->b[content_type].Key, 
736                                               m->b[charset].Key,
737                                               0, 
738                                               m->b[encoding].Key, 
739                                               m->b[id].Key, 
740                                               userdata);
741                 }
742         } /* If it's not a multipart message, then do something with it */
743         else {
744                 size_t length;
745                 part_start = ptr;
746                 length = content_end - part_start;
747                 ptr = part_end = content_end;
748
749
750                 /* The following code will truncate the MIME part to the size
751                  * specified by the Content-length: header.   We have commented it
752                  * out because these headers have a tendency to be wrong.
753                  *
754                  *      if ( (content_length > 0) && (length > content_length) ) {
755                  *              length = content_length;
756                  *      }
757                  */
758
759                 /* Sometimes the "name" field is tacked on to Content-type,
760                  * and sometimes it's tacked on to Content-disposition.  Use
761                  * whichever one we have.
762                  */
763                 if (m->b[content_disposition_name].len > m->b[content_type_name].len) {
764                         chosen_name = &m->b[content_disposition_name];
765                 }
766                 else {
767                         chosen_name = &m->b[content_type_name];
768                 }
769         
770                 /* Ok, we've got a non-multipart part here, so do something with it.
771                  */
772                 mime_decode(partnum,
773                             part_start, 
774                             length,
775                             m->b[content_type].Key, 
776                             m->b[charset].Key,
777                             m->b[encoding].Key, 
778                             m->b[disposition].Key, 
779                             m->b[id].Key, 
780                             chosen_name->Key, 
781                             m->b[filename].Key,
782                             CallBack, 
783                             NULL, NULL,
784                             userdata, 
785                             dont_decode
786                         );
787
788                 /*
789                  * Now if it's an encapsulated message/rfc822 then we have to recurse into it
790                  */
791                 if (!strcasecmp(&m->b[content_type].Key[0], "message/rfc822")) {
792
793                         if (PreMultiPartCallBack != NULL) {
794                                 PreMultiPartCallBack("", 
795                                                      "", 
796                                                      partnum, 
797                                                      "",
798                                                      NULL, 
799                                                      m->b[content_type].Key, 
800                                                      m->b[charset].Key,
801                                                      0, 
802                                                      m->b[encoding].Key, 
803                                                      m->b[id].Key, 
804                                                      userdata);
805                         }
806                         if (CallBack != NULL) {
807                                 if (strlen(partnum) > 0) {
808                                         snprintf(nested_partnum,
809                                                  sizeof nested_partnum,
810                                                  "%s.%d", partnum,
811                                                  ++part_seq);
812                                 }
813                                 else {
814                                         snprintf(nested_partnum,
815                                                  sizeof nested_partnum,
816                                                  "%d", ++part_seq);
817                                 }
818                                 the_mime_parser(nested_partnum,
819                                                 part_start, 
820                                                 part_end,
821                                                 CallBack,
822                                                 PreMultiPartCallBack,
823                                                 PostMultiPartCallBack,
824                                                 userdata,
825                                                 dont_decode
826                                         );
827                         }
828                         if (PostMultiPartCallBack != NULL) {
829                                 PostMultiPartCallBack("", 
830                                                       "", 
831                                                       partnum, 
832                                                       "", 
833                                                       NULL,
834                                                       m->b[content_type].Key, 
835                                                       m->b[charset].Key,
836                                                       0, 
837                                                       m->b[encoding].Key, 
838                                                       m->b[id].Key, 
839                                                       userdata);
840                         }
841
842
843                 }
844
845         }
846
847 }
848
849 /*
850  * Break out the components of a multipart message
851  * (This function expects to be fed HEADERS + CONTENT)
852  * Note: NULL can be supplied as content_end; in this case, the message is
853  * considered to have ended when the parser encounters a 0x00 byte.
854  */
855 void the_mime_parser(char *partnum,
856                      char *content_start, char *content_end,
857                      MimeParserCallBackType CallBack,
858                      MimeParserCallBackType PreMultiPartCallBack,
859                      MimeParserCallBackType PostMultiPartCallBack,
860                      void *userdata,
861                      int dont_decode)
862 {
863         interesting_mime_headers *m;
864
865         /* If the caller didn't supply an endpointer, generate one by measure */
866         if (content_end == NULL) {
867                 content_end = &content_start[strlen(content_start)];
868         }
869
870         m = InitInterestingMimes();
871
872         if (!parse_MimeHeaders(m, &content_start, content_end))
873         {
874
875                 recurseable_mime_parser(partnum,
876                                         content_start, content_end,
877                                         CallBack,
878                                         PreMultiPartCallBack,
879                                         PostMultiPartCallBack,
880                                         userdata,
881                                         dont_decode,
882                                         m);
883         }
884         free(m);
885 }
886
887 /*
888  * Entry point for the MIME parser.
889  * (This function expects to be fed HEADERS + CONTENT)
890  * Note: NULL can be supplied as content_end; in this case, the message is
891  * considered to have ended when the parser encounters a 0x00 byte.
892  */
893 void mime_parser(char *content_start,
894                  char *content_end,
895                  MimeParserCallBackType CallBack,
896                  MimeParserCallBackType PreMultiPartCallBack,
897                  MimeParserCallBackType PostMultiPartCallBack,
898                  void *userdata,
899                  int dont_decode)
900 {
901
902         the_mime_parser("", content_start, content_end,
903                         CallBack,
904                         PreMultiPartCallBack,
905                         PostMultiPartCallBack,
906                         userdata, dont_decode);
907 }
908
909
910
911
912
913
914 typedef struct _MimeGuess {
915         const char *Pattern;
916         size_t PatternLen;
917         long PatternOffset;
918         const char *MimeString;
919 } MimeGuess;
920
921 MimeGuess MyMimes [] = {
922         {
923                 "GIF",
924                 3,
925                 0,
926                 "image/gif"
927         },
928         {
929                 "\xff\xd8",
930                 2,
931                 0,
932                 "image/jpeg"
933         },
934         {
935                 "\x89PNG",
936                 4,
937                 0,
938                 "image/png"
939         },
940         { // last...
941                 "",
942                 0,
943                 0,
944                 ""
945         }
946 };
947
948
949 const char *GuessMimeType(const char *data, size_t dlen)
950 {
951         int MimeIndex = 0;
952
953         while (MyMimes[MimeIndex].PatternLen != 0)
954         {
955                 if ((MyMimes[MimeIndex].PatternLen + 
956                      MyMimes[MimeIndex].PatternOffset < dlen) &&
957                     strncmp(MyMimes[MimeIndex].Pattern, 
958                             &data[MyMimes[MimeIndex].PatternOffset], 
959                             MyMimes[MimeIndex].PatternLen) == 0)
960                 {
961                         return MyMimes[MimeIndex].MimeString;
962                 }
963                 MimeIndex ++;
964         }
965         /* 
966          * ok, our simple minded algorythm didn't find anything, 
967          * let the big chegger try it, he wil default to application/octet-stream
968          */
969         return (xdg_mime_get_mime_type_for_data(data, dlen));
970 }
971
972
973 const char* GuessMimeByFilename(const char *what, size_t len)
974 {
975         /* we know some hardcoded on our own, try them... */
976         if ((len > 3) && !strncasecmp(&what[len - 4], ".gif", 4))
977                 return "image/gif";
978         else if ((len > 2) && !strncasecmp(&what[len - 3], ".js", 3))
979                 return  "text/javascript";
980         else if ((len > 3) && !strncasecmp(&what[len - 4], ".txt", 4))
981                 return "text/plain";
982         else if ((len > 3) && !strncasecmp(&what[len - 4], ".css", 4))
983                 return "text/css";
984         else if ((len > 3) && !strncasecmp(&what[len - 4], ".htc", 4))
985                 return "text/x-component";
986         else if ((len > 3) && !strncasecmp(&what[len - 4], ".jpg", 4))
987                 return "image/jpeg";
988         else if ((len > 3) && !strncasecmp(&what[len - 4], ".png", 4))
989                 return "image/png";
990         else if ((len > 3) && !strncasecmp(&what[len - 4], ".ico", 4))
991                 return "image/x-icon";
992         else if ((len > 3) && !strncasecmp(&what[len - 4], ".vcf", 4))
993                 return "text/x-vcard";
994         else if ((len > 4) && !strncasecmp(&what[len - 5], ".html", 5))
995                 return "text/html";
996         else if ((len > 3) && !strncasecmp(&what[len - 4], ".htm", 4))
997                 return "text/html";
998         else if ((len > 3) && !strncasecmp(&what[len - 4], ".wml", 4))
999                 return "text/vnd.wap.wml";
1000         else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmls", 5))
1001                 return "text/vnd.wap.wmlscript";
1002         else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmlc", 5))
1003                 return "application/vnd.wap.wmlc";
1004         else if ((len > 5) && !strncasecmp(&what[len - 6], ".wmlsc", 6))
1005                 return "application/vnd.wap.wmlscriptc";
1006         else if ((len > 4) && !strncasecmp(&what[len - 5], ".wbmp", 5))
1007                 return "image/vnd.wap.wbmp";
1008         else
1009                 /* and let xdgmime do the fallback. */
1010                 return xdg_mime_get_mime_type_from_file_name(what);
1011 }
1012
1013 static HashList *IconHash = NULL;
1014
1015 typedef struct IconName IconName;
1016
1017 struct IconName {
1018         char *FlatName;
1019         char *FileName;
1020 };
1021
1022 static void DeleteIcon(void *IconNamePtr)
1023 {
1024         IconName *Icon = (IconName*) IconNamePtr;
1025         free(Icon->FlatName);
1026         free(Icon->FileName);
1027         free(Icon);
1028 }
1029
1030 /*
1031 static const char *PrintFlat(void *IconNamePtr)
1032 {
1033         IconName *Icon = (IconName*) IconNamePtr;
1034         return Icon->FlatName;
1035 }
1036 static const char *PrintFile(void *IconNamePtr)
1037 {
1038         IconName *Icon = (IconName*) IconNamePtr;
1039         return Icon->FileName;
1040 }
1041 */
1042
1043 #define GENSTR "x-generic"
1044 #define IGNORE_PREFIX_1 "gnome-mime"
1045 int LoadIconDir(const char *DirName)
1046 {
1047         DIR *filedir = NULL;
1048         struct dirent *filedir_entry;
1049         int d_namelen;
1050         int d_without_ext;
1051         IconName *Icon;
1052
1053         filedir = opendir (DirName);
1054         IconHash = NewHash(1, NULL);
1055         if (filedir == NULL) {
1056                 return 0;
1057         }
1058
1059         while ((filedir_entry = readdir(filedir)))
1060         {
1061                 char *MinorPtr;
1062                 char *PStart;
1063 #ifdef _DIRENT_HAVE_D_NAMELEN
1064                 d_namelen = filedir_entry->d_namelen;
1065 #else
1066                 d_namelen = strlen(filedir_entry->d_name);
1067 #endif
1068                 d_without_ext = d_namelen;
1069                 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1070                         d_without_ext --;
1071                 if ((d_without_ext == 0) || (d_namelen < 3))
1072                         continue;
1073
1074                 if ((sizeof(IGNORE_PREFIX_1) < d_namelen) &&
1075                     (strncmp(IGNORE_PREFIX_1, 
1076                              filedir_entry->d_name, 
1077                              sizeof(IGNORE_PREFIX_1) - 1) == 0)) {
1078                         PStart = filedir_entry->d_name + sizeof(IGNORE_PREFIX_1);
1079                         d_without_ext -= sizeof(IGNORE_PREFIX_1);
1080                 }
1081                 else {
1082                         PStart = filedir_entry->d_name;
1083                 }
1084                 Icon = malloc(sizeof(IconName));
1085
1086                 Icon->FileName = malloc(d_namelen + 1);
1087                 memcpy(Icon->FileName, filedir_entry->d_name, d_namelen + 1);
1088
1089                 Icon->FlatName = malloc(d_without_ext + 1);
1090                 memcpy(Icon->FlatName, PStart, d_without_ext);
1091                 Icon->FlatName[d_without_ext] = '\0';
1092                 /* Try to find Minor type in image-jpeg */
1093                 MinorPtr = strchr(Icon->FlatName, '-');
1094                 if (MinorPtr != NULL) {
1095                         size_t MinorLen;
1096                         MinorLen = 1 + d_without_ext - (MinorPtr - Icon->FlatName + 1);
1097                         if ((MinorLen == sizeof(GENSTR)) && 
1098                             (strncmp(MinorPtr + 1, GENSTR, sizeof(GENSTR)) == 0)) {
1099                                 /* ok, we found a generic filename. cut the generic. */
1100                                 *MinorPtr = '\0';
1101                                 d_without_ext = d_without_ext - (MinorPtr - Icon->FlatName);
1102                         }
1103                         else { /* Map the major / minor separator to / */
1104                                 *MinorPtr = '/';
1105                         }
1106                 }
1107
1108 //              PrintHash(IconHash, PrintFlat, PrintFile);
1109 //              printf("%s - %s\n", Icon->FlatName, Icon->FileName);
1110                 Put(IconHash, Icon->FlatName, d_without_ext, Icon, DeleteIcon);
1111 //              PrintHash(IconHash, PrintFlat, PrintFile);
1112         }
1113         closedir(filedir);
1114         return 1;
1115 }
1116
1117 const char *GetIconFilename(char *MimeType, size_t len)
1118 {
1119         void *vIcon;
1120         IconName *Icon;
1121         
1122         if(IconHash == NULL)
1123                 return NULL;
1124
1125         GetHash(IconHash, MimeType, len, &vIcon), Icon = (IconName*) vIcon;
1126         /* didn't find the exact mimetype? try major only. */
1127         if (Icon == NULL) {
1128                 char * pMinor;
1129                 pMinor = strchr(MimeType, '/');
1130                 if (pMinor != NULL) {
1131                         *pMinor = '\0';
1132                         GetHash(IconHash, MimeType, pMinor - MimeType, &vIcon),
1133                                 Icon = (IconName*) vIcon;
1134                 }
1135         }
1136         if (Icon == NULL) {
1137                 return NULL;
1138         }
1139
1140         /*printf("Getting: [%s] == [%s] -> [%s]\n", MimeType, Icon->FlatName, Icon->FileName);*/
1141         return Icon->FileName;
1142 }
1143
1144 void ShutDownLibCitadelMime(void)
1145 {
1146         DeleteHash(&IconHash);
1147 }