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