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