Merge branch 'master' of ssh://git.citadel.org/appl/gitroot/citadel
[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].len = striplt (m->b[content_type].Key);
410
411                                 m->b[content_type_name].len = extract_key(m->b[content_type_name].Key, CKEY(m->b[content_type]), HKEY("name"), '=');
412                                 m->b[charset].len           = extract_key(m->b[charset].Key,           CKEY(m->b[content_type]), HKEY("charset"), '=');
413                                 m->b[boundary].len          = extract_key(m->b[boundary].Key,          header,       headerlen,  HKEY("boundary"), '=');
414
415                                 /* Deal with weird headers */
416                                 pch = strchr(m->b[content_type].Key, ' ');
417                                 if (pch != NULL) {
418                                         *pch = '\0';
419                                         m->b[content_type].len = m->b[content_type].Key - pch;
420                                 }
421                                 pch = strchr(m->b[content_type].Key, ';');
422                                 if (pch != NULL) {
423                                         *pch = '\0';
424                                         m->b[content_type].len = m->b[content_type].Key - pch;
425                                 }
426                         }
427                         else if (!strncasecmp(header, "Content-Disposition:", 20)) {
428                                 memcpy (m->b[disposition].Key, &header[20], headerlen - 19);
429                                 m->b[disposition].len = striplt(m->b[disposition].Key);
430
431                                 m->b[content_disposition_name].len = extract_key(m->b[content_disposition_name].Key, CKEY(m->b[disposition]), HKEY("name"), '=');
432                                 m->b[filename].len                 = extract_key(m->b[filename].Key,                 CKEY(m->b[disposition]), HKEY("filename"), '=');
433                                 pch = strchr(m->b[disposition].Key, ';');
434                                 if (pch != NULL) *pch = '\0';
435                                 m->b[disposition].len = striplt(m->b[disposition].Key);
436                         }
437                         else if (!strncasecmp(header, "Content-ID:", 11)) {
438                                 memcpy(m->b[id].Key, &header[11], headerlen);
439                                 striplt(m->b[id].Key);
440                                 m->b[id].len = stripallbut(m->b[id].Key, '<', '>');
441                         }
442                         else if (!strncasecmp(header, "Content-length: ", 15)) {
443                                 char *clbuf;
444                                 clbuf = &header[15];
445                                 while (isspace(*clbuf))
446                                         clbuf ++;
447                                 m->content_length = (size_t) atol(clbuf);
448                         }
449                         else if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
450                                 memcpy(m->b[encoding].Key, &header[26], headerlen - 26);
451                                 m->b[encoding].len = striplt(m->b[encoding].Key);
452                         }
453                         *header = '\0';
454                         headerlen = 0;
455                 }
456                 if ((headerlen + buflen + 2) < SIZ) {
457                         memcpy(&header[headerlen], buf, buflen);
458                         headerlen += buflen;
459                         header[headerlen] = '\0';
460                 }
461                 if (ptr >= content_end) {
462                         return -1;
463                 }
464         } while ((!IsEmptyStr(buf)) && (*ptr != 0));
465
466         m->is_multipart = m->b[boundary].len != 0;
467         *pcontent_start = ptr;
468
469         return 0;
470 }
471
472
473 static int IsAsciiEncoding(interesting_mime_headers *m)
474 {
475
476         if ((m->b[encoding].len != 0) &&
477             (strcasecmp(m->b[encoding].Key, "base64") == 0))
478                 return 1;
479         if ((m->b[encoding].len != 0) &&
480             (strcmp(m->b[encoding].Key, "quoted-printable") == 0))
481                 return 1;
482
483         return 0;
484 }
485
486 static char *FindNextContent(char *ptr,
487                              char *content_end,
488                              interesting_mime_headers *SubMimeHeaders,
489                              interesting_mime_headers *m)
490 {
491         char *next_boundary;
492         char  tmp;
493
494         if (IsAsciiEncoding(SubMimeHeaders)) {
495                 tmp = *content_end;
496                 *content_end = '\0';
497
498                 /** 
499                  * ok, if we have a content length of the mime part, 
500                  * try skipping the content on the search for the next
501                  * boundary. since we don't trust the content_length
502                  * to be all accurate, and suspect it to lose one digit 
503                  * per line with a line length of 80 chars, we need 
504                  * to start searching a little before..
505                  */
506                                    
507                 if ((SubMimeHeaders->content_length != -1) &&
508                     (SubMimeHeaders->content_length > 10))
509                 {
510                         char *pptr;
511                         long lines;
512                                         
513                         lines = SubMimeHeaders->content_length / 80;
514                         pptr = ptr + SubMimeHeaders->content_length - lines - 10;
515                         if (pptr < content_end)
516                                 ptr = pptr;
517                 }
518                         
519                 next_boundary = strstr(ptr, m->b[startary].Key);
520                 *content_end = tmp;
521         }
522         else {
523                 char *srch;
524                 /** 
525                  * ok, if we have a content length of the mime part, 
526                  * try skipping the content on the search for the next
527                  * boundary. since we don't trust the content_length
528                  * to be all accurate, start searching a little before..
529                  */
530                                    
531                 if ((SubMimeHeaders->content_length != -1) &&
532                     (SubMimeHeaders->content_length > 10))
533                 {
534                         char *pptr;
535                         pptr = ptr + SubMimeHeaders->content_length - 10;
536                         if (pptr < content_end)
537                                 ptr = pptr;
538                 }
539                 
540
541                 srch = next_boundary = NULL;
542                 for (srch = memchr(ptr, '-',  content_end - srch);
543                      (srch != NULL) && (srch < content_end); 
544                      srch = memchr(srch, '-',  content_end - srch)) 
545                 {
546                         if (!memcmp(srch, 
547                                     m->b[startary].Key, 
548                                     m->b[startary].len)) 
549                         {
550                                 next_boundary = srch;
551                                 srch = content_end;
552                         }
553                         else srch ++;
554
555                 }
556
557         }
558         return next_boundary;
559 }
560
561 /*
562  * Break out the components of a multipart message
563  * (This function expects to be fed HEADERS + CONTENT)
564  * Note: NULL can be supplied as content_end; in this case, the message is
565  * considered to have ended when the parser encounters a 0x00 byte.
566  */
567 static void recurseable_mime_parser(char *partnum,
568                                     char *content_start, char *content_end,
569                                     MimeParserCallBackType CallBack,
570                                     MimeParserCallBackType PreMultiPartCallBack,
571                                     MimeParserCallBackType PostMultiPartCallBack,
572                                     void *userdata,
573                                     int dont_decode, 
574                                     interesting_mime_headers *m)
575 {
576         interesting_mime_headers *SubMimeHeaders;
577         char     *ptr;
578         char     *part_start;
579         char     *part_end = NULL;
580         char     *evaluate_crlf_ptr = NULL;
581         char     *next_boundary;
582         char      nested_partnum[256];
583         int       crlf_in_use = 0;
584         int       part_seq = 0;
585         CBufStr  *chosen_name;
586
587
588         /* If this is a multipart message, then recursively process it */
589         ptr = content_start;
590         part_start = NULL;
591         if (m->is_multipart) {
592
593                 /* Tell the client about this message's multipartedness */
594                 if (PreMultiPartCallBack != NULL) {
595                         PreMultiPartCallBack("", 
596                                              "", 
597                                              partnum, 
598                                              "",
599                                              NULL, 
600                                              m->b[content_type].Key, 
601                                              m->b[charset].Key,
602                                              0, 
603                                              m->b[encoding].Key, 
604                                              m->b[id].Key, 
605                                              userdata);
606                 }
607
608                 /* Figure out where the boundaries are */
609                 m->b[startary].len = snprintf(m->b[startary].Key, SIZ, "--%s", m->b[boundary].Key);
610                 SubMimeHeaders = InitInterestingMimes ();
611                 if (*ptr == '\r')
612                         ptr ++;
613                 if (*ptr == '\n')
614                         ptr ++;
615                 if (strncmp(ptr, m->b[startary].Key, m->b[startary].len) == 0)
616                         ptr += m->b[startary].len;
617                 if (*ptr == '\r')
618                         ptr ++;
619                 if (*ptr == '\n')
620                         ptr ++;
621                 part_start = NULL;
622                 do {
623                         char *optr;
624
625                         optr = ptr;
626                         if (parse_MimeHeaders(SubMimeHeaders, &ptr, content_end) != 0)
627                                 break;
628                         if ((ptr - optr > 2) && 
629                             (*(ptr - 2) == '\r'))
630                                 crlf_in_use = 1;
631                         
632                         part_start = ptr;
633                         
634                         next_boundary = FindNextContent(ptr,
635                                                         content_end,
636                                                         SubMimeHeaders,
637                                                         m);
638                         if ((next_boundary != NULL) && 
639                             (next_boundary - part_start < 3))
640                                 continue;
641
642                         if ( (part_start != NULL) && (next_boundary != NULL) ) {
643                                 part_end = next_boundary;
644                                 --part_end;             /* omit the trailing LF */
645                                 if (crlf_in_use) {
646                                         --part_end;     /* omit the trailing CR */
647                                 }
648
649                                 if (!IsEmptyStr(partnum)) {
650                                         snprintf(nested_partnum,
651                                                  sizeof nested_partnum,
652                                                  "%s.%d", partnum,
653                                                  ++part_seq);
654                                 }
655                                 else {
656                                         snprintf(nested_partnum,
657                                                  sizeof nested_partnum,
658                                                  "%d", ++part_seq);
659                                 }
660                                 recurseable_mime_parser(nested_partnum,
661                                                         part_start, 
662                                                         part_end,
663                                                         CallBack,
664                                                         PreMultiPartCallBack,
665                                                         PostMultiPartCallBack,
666                                                         userdata,
667                                                         dont_decode, 
668                                                         SubMimeHeaders);
669                         }
670
671                         if (next_boundary != NULL) {
672                                 /* If we pass out of scope, don't attempt to
673                                  * read past the end boundary. */
674                                 if ((*(next_boundary + m->b[startary].len + 1) == '-') && 
675                                     (*(next_boundary + m->b[startary].len + 2) == '-') ){
676                                         ptr = content_end;
677                                 }
678                                 else {
679                                         /* Set up for the next part. */
680                                         part_start = strstr(next_boundary, "\n");
681                                         
682                                         /* Determine whether newlines are LF or CRLF */
683                                         evaluate_crlf_ptr = part_start;
684                                         --evaluate_crlf_ptr;
685                                         if ((*evaluate_crlf_ptr == '\r') && 
686                                             (*(evaluate_crlf_ptr + 1) == '\n'))
687                                         {
688                                                 crlf_in_use = 1;
689                                         }
690                                         else {
691                                                 crlf_in_use = 0;
692                                         }
693
694                                         /* Advance past the LF ... now we're in the next part */
695                                         ++part_start;
696                                         ptr = part_start;
697                                 }
698                         }
699                         else {
700                                 /* Invalid end of multipart.  Bail out! */
701                                 ptr = content_end;
702                         }
703                         FlushInterestingMimes(SubMimeHeaders);
704                 } while ( (ptr < content_end) && (next_boundary != NULL) );
705
706                 free(SubMimeHeaders);
707
708                 if (PostMultiPartCallBack != NULL) {
709                         PostMultiPartCallBack("", 
710                                               "", 
711                                               partnum, 
712                                               "", 
713                                               NULL,
714                                               m->b[content_type].Key, 
715                                               m->b[charset].Key,
716                                               0, 
717                                               m->b[encoding].Key, 
718                                               m->b[id].Key, 
719                                               userdata);
720                 }
721         } /* If it's not a multipart message, then do something with it */
722         else {
723                 size_t length;
724                 part_start = ptr;
725                 length = content_end - part_start;
726                 ptr = part_end = content_end;
727
728
729                 /* The following code will truncate the MIME part to the size
730                  * specified by the Content-length: header.   We have commented it
731                  * out because these headers have a tendency to be wrong.
732                  *
733                  *      if ( (content_length > 0) && (length > content_length) ) {
734                  *              length = content_length;
735                  *      }
736                  */
737
738                 /* Sometimes the "name" field is tacked on to Content-type,
739                  * and sometimes it's tacked on to Content-disposition.  Use
740                  * whichever one we have.
741                  */
742                 if (m->b[content_disposition_name].len > m->b[content_type_name].len) {
743                         chosen_name = &m->b[content_disposition_name];
744                 }
745                 else {
746                         chosen_name = &m->b[content_type_name];
747                 }
748         
749                 /* Ok, we've got a non-multipart part here, so do something with it.
750                  */
751                 mime_decode(partnum,
752                             part_start, 
753                             length,
754                             m->b[content_type].Key, 
755                             m->b[charset].Key,
756                             m->b[encoding].Key, 
757                             m->b[disposition].Key, 
758                             m->b[id].Key, 
759                             chosen_name->Key, 
760                             m->b[filename].Key,
761                             CallBack, 
762                             NULL, NULL,
763                             userdata, 
764                             dont_decode
765                         );
766
767                 /*
768                  * Now if it's an encapsulated message/rfc822 then we have to recurse into it
769                  */
770                 if (!strcasecmp(&m->b[content_type].Key[0], "message/rfc822")) {
771
772                         if (PreMultiPartCallBack != NULL) {
773                                 PreMultiPartCallBack("", 
774                                                      "", 
775                                                      partnum, 
776                                                      "",
777                                                      NULL, 
778                                                      m->b[content_type].Key, 
779                                                      m->b[charset].Key,
780                                                      0, 
781                                                      m->b[encoding].Key, 
782                                                      m->b[id].Key, 
783                                                      userdata);
784                         }
785                         if (CallBack != NULL) {
786                                 if (strlen(partnum) > 0) {
787                                         snprintf(nested_partnum,
788                                                  sizeof nested_partnum,
789                                                  "%s.%d", partnum,
790                                                  ++part_seq);
791                                 }
792                                 else {
793                                         snprintf(nested_partnum,
794                                                  sizeof nested_partnum,
795                                                  "%d", ++part_seq);
796                                 }
797                                 the_mime_parser(nested_partnum,
798                                                 part_start, 
799                                                 part_end,
800                                                 CallBack,
801                                                 PreMultiPartCallBack,
802                                                 PostMultiPartCallBack,
803                                                 userdata,
804                                                 dont_decode
805                                         );
806                         }
807                         if (PostMultiPartCallBack != NULL) {
808                                 PostMultiPartCallBack("", 
809                                                       "", 
810                                                       partnum, 
811                                                       "", 
812                                                       NULL,
813                                                       m->b[content_type].Key, 
814                                                       m->b[charset].Key,
815                                                       0, 
816                                                       m->b[encoding].Key, 
817                                                       m->b[id].Key, 
818                                                       userdata);
819                         }
820
821
822                 }
823
824         }
825
826 }
827
828 /*
829  * Break out the components of a multipart message
830  * (This function expects to be fed HEADERS + CONTENT)
831  * Note: NULL can be supplied as content_end; in this case, the message is
832  * considered to have ended when the parser encounters a 0x00 byte.
833  */
834 void the_mime_parser(char *partnum,
835                      char *content_start, char *content_end,
836                      MimeParserCallBackType CallBack,
837                      MimeParserCallBackType PreMultiPartCallBack,
838                      MimeParserCallBackType PostMultiPartCallBack,
839                      void *userdata,
840                      int dont_decode)
841 {
842         interesting_mime_headers *m;
843
844         /* If the caller didn't supply an endpointer, generate one by measure */
845         if (content_end == NULL) {
846                 content_end = &content_start[strlen(content_start)];
847         }
848
849         m = InitInterestingMimes();
850
851         if (!parse_MimeHeaders(m, &content_start, content_end))
852         {
853
854                 recurseable_mime_parser(partnum,
855                                         content_start, content_end,
856                                         CallBack,
857                                         PreMultiPartCallBack,
858                                         PostMultiPartCallBack,
859                                         userdata,
860                                         dont_decode,
861                                         m);
862         }
863         free(m);
864 }
865
866 /*
867  * Entry point for the MIME parser.
868  * (This function expects to be fed HEADERS + CONTENT)
869  * Note: NULL can be supplied as content_end; in this case, the message is
870  * considered to have ended when the parser encounters a 0x00 byte.
871  */
872 void mime_parser(char *content_start,
873                  char *content_end,
874                  MimeParserCallBackType CallBack,
875                  MimeParserCallBackType PreMultiPartCallBack,
876                  MimeParserCallBackType PostMultiPartCallBack,
877                  void *userdata,
878                  int dont_decode)
879 {
880
881         the_mime_parser("", content_start, content_end,
882                         CallBack,
883                         PreMultiPartCallBack,
884                         PostMultiPartCallBack,
885                         userdata, dont_decode);
886 }
887
888
889
890
891
892
893 typedef struct _MimeGuess {
894         const char *Pattern;
895         size_t PatternLen;
896         long PatternOffset;
897         const char *MimeString;
898 } MimeGuess;
899
900 MimeGuess MyMimes [] = {
901         {
902                 "GIF",
903                 3,
904                 0,
905                 "image/gif"
906         },
907         {
908                 "\xff\xd8",
909                 2,
910                 0,
911                 "image/jpeg"
912         },
913         {
914                 "\x89PNG",
915                 4,
916                 0,
917                 "image/png"
918         },
919         { // last...
920                 "",
921                 0,
922                 0,
923                 ""
924         }
925 };
926
927
928 const char *GuessMimeType(const char *data, size_t dlen)
929 {
930         int MimeIndex = 0;
931
932         while (MyMimes[MimeIndex].PatternLen != 0)
933         {
934                 if ((MyMimes[MimeIndex].PatternLen + 
935                      MyMimes[MimeIndex].PatternOffset < dlen) &&
936                     strncmp(MyMimes[MimeIndex].Pattern, 
937                             &data[MyMimes[MimeIndex].PatternOffset], 
938                             MyMimes[MimeIndex].PatternLen) == 0)
939                 {
940                         return MyMimes[MimeIndex].MimeString;
941                 }
942                 MimeIndex ++;
943         }
944         /* 
945          * ok, our simple minded algorythm didn't find anything, 
946          * let the big chegger try it, he wil default to application/octet-stream
947          */
948         return (xdg_mime_get_mime_type_for_data(data, dlen));
949 }
950
951
952 const char* GuessMimeByFilename(const char *what, size_t len)
953 {
954         /* we know some hardcoded on our own, try them... */
955         if ((len > 3) && !strncasecmp(&what[len - 4], ".gif", 4))
956                 return "image/gif";
957         else if ((len > 2) && !strncasecmp(&what[len - 3], ".js", 3))
958                 return  "text/javascript";
959         else if ((len > 3) && !strncasecmp(&what[len - 4], ".txt", 4))
960                 return "text/plain";
961         else if ((len > 3) && !strncasecmp(&what[len - 4], ".css", 4))
962                 return "text/css";
963         else if ((len > 3) && !strncasecmp(&what[len - 4], ".htc", 4))
964                 return "text/x-component";
965         else if ((len > 3) && !strncasecmp(&what[len - 4], ".jpg", 4))
966                 return "image/jpeg";
967         else if ((len > 3) && !strncasecmp(&what[len - 4], ".png", 4))
968                 return "image/png";
969         else if ((len > 3) && !strncasecmp(&what[len - 4], ".ico", 4))
970                 return "image/x-icon";
971         else if ((len > 3) && !strncasecmp(&what[len - 4], ".vcf", 4))
972                 return "text/x-vcard";
973         else if ((len > 4) && !strncasecmp(&what[len - 5], ".html", 5))
974                 return "text/html";
975         else if ((len > 3) && !strncasecmp(&what[len - 4], ".htm", 4))
976                 return "text/html";
977         else if ((len > 3) && !strncasecmp(&what[len - 4], ".wml", 4))
978                 return "text/vnd.wap.wml";
979         else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmls", 5))
980                 return "text/vnd.wap.wmlscript";
981         else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmlc", 5))
982                 return "application/vnd.wap.wmlc";
983         else if ((len > 5) && !strncasecmp(&what[len - 6], ".wmlsc", 6))
984                 return "application/vnd.wap.wmlscriptc";
985         else if ((len > 4) && !strncasecmp(&what[len - 5], ".wbmp", 5))
986                 return "image/vnd.wap.wbmp";
987         else
988                 /* and let xdgmime do the fallback. */
989                 return xdg_mime_get_mime_type_from_file_name(what);
990 }
991
992 static HashList *IconHash = NULL;
993
994 typedef struct IconName IconName;
995
996 struct IconName {
997         char *FlatName;
998         char *FileName;
999 };
1000
1001 static void DeleteIcon(void *IconNamePtr)
1002 {
1003         IconName *Icon = (IconName*) IconNamePtr;
1004         free(Icon->FlatName);
1005         free(Icon->FileName);
1006         free(Icon);
1007 }
1008
1009 /*
1010 static const char *PrintFlat(void *IconNamePtr)
1011 {
1012         IconName *Icon = (IconName*) IconNamePtr;
1013         return Icon->FlatName;
1014 }
1015 static const char *PrintFile(void *IconNamePtr)
1016 {
1017         IconName *Icon = (IconName*) IconNamePtr;
1018         return Icon->FileName;
1019 }
1020 */
1021
1022 #define GENSTR "x-generic"
1023 #define IGNORE_PREFIX_1 "gnome-mime"
1024 int LoadIconDir(const char *DirName)
1025 {
1026         DIR *filedir = NULL;
1027         struct dirent *filedir_entry;
1028         int d_namelen;
1029         int d_without_ext;
1030         IconName *Icon;
1031
1032         filedir = opendir (DirName);
1033         IconHash = NewHash(1, NULL);
1034         if (filedir == NULL) {
1035                 return 0;
1036         }
1037
1038         while ((filedir_entry = readdir(filedir)))
1039         {
1040                 char *MinorPtr;
1041                 char *PStart;
1042 #ifdef _DIRENT_HAVE_D_NAMELEN
1043                 d_namelen = filedir_entry->d_namelen;
1044 #else
1045                 d_namelen = strlen(filedir_entry->d_name);
1046 #endif
1047                 d_without_ext = d_namelen;
1048                 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1049                         d_without_ext --;
1050                 if ((d_without_ext == 0) || (d_namelen < 3))
1051                         continue;
1052
1053                 if ((sizeof(IGNORE_PREFIX_1) < d_namelen) &&
1054                     (strncmp(IGNORE_PREFIX_1, 
1055                              filedir_entry->d_name, 
1056                              sizeof(IGNORE_PREFIX_1) - 1) == 0)) {
1057                         PStart = filedir_entry->d_name + sizeof(IGNORE_PREFIX_1);
1058                         d_without_ext -= sizeof(IGNORE_PREFIX_1);
1059                 }
1060                 else {
1061                         PStart = filedir_entry->d_name;
1062                 }
1063                 Icon = malloc(sizeof(IconName));
1064
1065                 Icon->FileName = malloc(d_namelen + 1);
1066                 memcpy(Icon->FileName, filedir_entry->d_name, d_namelen + 1);
1067
1068                 Icon->FlatName = malloc(d_without_ext + 1);
1069                 memcpy(Icon->FlatName, PStart, d_without_ext);
1070                 Icon->FlatName[d_without_ext] = '\0';
1071                 /* Try to find Minor type in image-jpeg */
1072                 MinorPtr = strchr(Icon->FlatName, '-');
1073                 if (MinorPtr != NULL) {
1074                         size_t MinorLen;
1075                         MinorLen = 1 + d_without_ext - (MinorPtr - Icon->FlatName + 1);
1076                         if ((MinorLen == sizeof(GENSTR)) && 
1077                             (strncmp(MinorPtr + 1, GENSTR, sizeof(GENSTR)) == 0)) {
1078                                 /* ok, we found a generic filename. cut the generic. */
1079                                 *MinorPtr = '\0';
1080                                 d_without_ext = d_without_ext - (MinorPtr - Icon->FlatName);
1081                         }
1082                         else { /* Map the major / minor separator to / */
1083                                 *MinorPtr = '/';
1084                         }
1085                 }
1086
1087 //              PrintHash(IconHash, PrintFlat, PrintFile);
1088 //              printf("%s - %s\n", Icon->FlatName, Icon->FileName);
1089                 Put(IconHash, Icon->FlatName, d_without_ext, Icon, DeleteIcon);
1090 //              PrintHash(IconHash, PrintFlat, PrintFile);
1091         }
1092         closedir(filedir);
1093         return 1;
1094 }
1095
1096 const char *GetIconFilename(char *MimeType, size_t len)
1097 {
1098         void *vIcon;
1099         IconName *Icon;
1100         
1101         if(IconHash == NULL)
1102                 return NULL;
1103
1104         GetHash(IconHash, MimeType, len, &vIcon), Icon = (IconName*) vIcon;
1105         /* didn't find the exact mimetype? try major only. */
1106         if (Icon == NULL) {
1107                 char * pMinor;
1108                 pMinor = strchr(MimeType, '/');
1109                 if (pMinor != NULL) {
1110                         *pMinor = '\0';
1111                         GetHash(IconHash, MimeType, pMinor - MimeType, &vIcon),
1112                                 Icon = (IconName*) vIcon;
1113                 }
1114         }
1115         if (Icon == NULL) {
1116                 return NULL;
1117         }
1118
1119         /*printf("Getting: [%s] == [%s] -> [%s]\n", MimeType, Icon->FlatName, Icon->FileName);*/
1120         return Icon->FileName;
1121 }
1122
1123 void ShutDownLibCitadelMime(void)
1124 {
1125         DeleteHash(&IconHash);
1126 }