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