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