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