2 * This is the MIME parser for Citadel.
4 * Copyright (c) 1998-2010 by the citadel.org development team.
6 * This program is open source software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/types.h>
29 #include <sys/types.h>
33 #include "xdgmime/xdgmime.h"
34 #include "libcitadel.h"
35 #include "libcitadellocal.h"
37 const unsigned char FromHexTable [256] = {
38 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0
39 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 10
40 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 20
41 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 30
42 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x01, // 40
43 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0xFF, // 50
44 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, // 60
45 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 70
46 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 80
47 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0A, 0x0B, 0x0C, // 90
48 0x0D, 0x0E, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //100
49 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //110
50 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //120
51 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //130
52 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //140
53 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //150
54 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //160
55 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //170
56 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //180
57 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //190
58 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //200
59 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //210
60 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //220
61 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //230
62 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //240
63 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF //250
67 long extract_key(char *target, char *source, long sourcelen, char *key, long keylen, char KeyEnd)
69 char *sptr, *ptr = NULL;
70 int double_quotes = 0;
71 long RealKeyLen = keylen;
77 ptr = bmstrcasestr_len(sptr, sourcelen - (sptr - source),
81 while (isspace(*(ptr + RealKeyLen)))
83 if (*(ptr + RealKeyLen) == KeyEnd)
90 sptr = ptr + RealKeyLen + 1;
100 strcpy(target, (ptr + RealKeyLen));
102 for (ptr=target; (*ptr != 0); ptr++) {
104 /* A semicolon means we've hit the end of the key, unless we're inside double quotes */
105 if ( (double_quotes != 1) && (*ptr == ';')) {
109 /* if we find double quotes, we've got a great set of string boundaries */
112 if (double_quotes == 1) {
126 * For non-multipart messages, we need to generate a quickie partnum of "1"
127 * to return to callback functions. Some callbacks demand it.
129 char *fixed_partnum(char *supplied_partnum) {
130 if (supplied_partnum == NULL) return "1";
131 if (strlen(supplied_partnum)==0) return "1";
132 return supplied_partnum;
136 static inline unsigned int _decode_hex(const char *Source)
139 unsigned char LO_NIBBLE;
140 unsigned char HI_NIBBLE;
142 HI_NIBBLE = FromHexTable[(unsigned char) *Source];
143 LO_NIBBLE = FromHexTable[(unsigned char) *(Source+1)];
145 if ((LO_NIBBLE == 0xFF) || (LO_NIBBLE == 0xFF))
149 ret = ret | LO_NIBBLE;
153 unsigned int decode_hex(char *Source) {return _decode_hex(Source);}
156 * Convert "quoted-printable" to binary. Returns number of bytes decoded.
157 * according to RFC2045 section 6.7
159 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
161 int decoded_length = 0;
164 while (pos < sourcelen)
166 if (*(encoded + pos) == '=')
169 if (*(encoded + pos) == '\n')
173 else if (*(encoded + pos) == '\r')
176 if (*(encoded + pos) == '\n')
182 ch = _decode_hex(&encoded[pos]);
184 decoded[decoded_length++] = ch;
189 decoded[decoded_length++] = encoded[pos];
193 decoded[decoded_length] = 0;
194 return(decoded_length);
199 * Given a message or message-part body and a length, handle any necessary
200 * decoding and pass the request up the stack.
202 void mime_decode(char *partnum,
203 char *part_start, size_t length,
204 char *content_type, char *charset, char *encoding,
207 char *name, char *filename,
208 MimeParserCallBackType CallBack,
209 MimeParserCallBackType PreMultiPartCallBack,
210 MimeParserCallBackType PostMultiPartCallBack,
216 size_t bytes_decoded = 0;
218 /* Some encodings aren't really encodings */
219 if (!strcasecmp(encoding, "7bit"))
220 strcpy(encoding, "");
221 if (!strcasecmp(encoding, "8bit"))
222 strcpy(encoding, "");
223 if (!strcasecmp(encoding, "binary"))
224 strcpy(encoding, "");
225 if (!strcasecmp(encoding, "ISO-8859-1"))
226 strcpy(encoding, "");
228 /* If this part is not encoded, send as-is */
229 if ( (strlen(encoding) == 0) || (dont_decode)) {
230 if (CallBack != NULL) {
233 fixed_partnum(partnum),
246 /* Fail silently if we hit an unknown encoding. */
247 if ((strcasecmp(encoding, "base64"))
248 && (strcasecmp(encoding, "quoted-printable"))) {
253 * Allocate a buffer for the decoded data. The output buffer is slightly
254 * larger than the input buffer; this assumes that the decoded data
255 * will never be significantly larger than the encoded data. This is a
256 * safe assumption with base64, uuencode, and quoted-printable.
258 decoded = malloc(length + 32768);
259 if (decoded == NULL) {
263 if (!strcasecmp(encoding, "base64")) {
264 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
266 else if (!strcasecmp(encoding, "quoted-printable")) {
267 bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
270 if (bytes_decoded > 0) if (CallBack != NULL) {
271 char encoding_buf[SIZ];
273 strcpy(encoding_buf, "binary");
276 fixed_partnum(partnum),
291 * this is the extract of mime_decode which can be called if 'dont_decode' was set;
292 * to save the cpu intense process of decoding to the time when it realy wants the content.
294 * - > 0 we decoded something, its on *decoded, you need to free it.
295 * - = 0 no need to decode stuff. *decoded will be NULL.
296 * - < 0 an error occured, either an unknown encoding, or alloc failed. no need to free.
298 int mime_decode_now (char *part_start,
302 size_t *bytes_decoded)
306 /* Some encodings aren't really encodings */
307 if (!strcasecmp(encoding, "7bit"))
308 strcpy(encoding, "");
309 if (!strcasecmp(encoding, "8bit"))
310 strcpy(encoding, "");
311 if (!strcasecmp(encoding, "binary"))
312 strcpy(encoding, "");
314 /* If this part is not encoded, send as-is */
315 if (strlen(encoding) == 0) {
320 /* Fail if we hit an unknown encoding. */
321 if ((strcasecmp(encoding, "base64"))
322 && (strcasecmp(encoding, "quoted-printable"))) {
327 * Allocate a buffer for the decoded data. The output buffer is slightly
328 * larger than the input buffer; this assumes that the decoded data
329 * will never be significantly larger than the encoded data. This is a
330 * safe assumption with base64, uuencode, and quoted-printable.
332 *decoded = malloc(length + 32768);
333 if (decoded == NULL) {
337 if (!strcasecmp(encoding, "base64")) {
338 *bytes_decoded = CtdlDecodeBase64(*decoded, part_start, length);
341 else if (!strcasecmp(encoding, "quoted-printable")) {
342 *bytes_decoded = CtdlDecodeQuotedPrintable(*decoded, part_start, length);
348 typedef enum _eIntMimeHdrs {
356 content_disposition_name,
360 eMax /* don't move ! */
363 typedef struct _CBufStr {
368 typedef struct _interesting_mime_headers {
372 } interesting_mime_headers;
375 static void FlushInterestingMimes(interesting_mime_headers *m)
379 for (i = 0; i < eMax; i++) {
380 m->b[i].Key[0] = '\0';
383 m->content_length = -1;
385 static interesting_mime_headers *InitInterestingMimes(void)
387 interesting_mime_headers *m;
388 m = (interesting_mime_headers*) malloc( sizeof(interesting_mime_headers));
390 FlushInterestingMimes(m);
396 static long parse_MimeHeaders(interesting_mime_headers *m,
397 char** pcontent_start,
407 /* Learn interesting things from the headers */
408 ptr = *pcontent_start;
412 ptr = memreadlinelen(ptr, buf, SIZ, &buflen);
414 for (i = 0; i < buflen; ++i) {
415 if (isspace(buf[i])) {
420 if (!isspace(buf[0]) && (headerlen > 0)) {
421 if (!strncasecmp(header, "Content-type:", 13)) {
422 memcpy (m->b[content_type].Key, &header[13], headerlen - 12);
423 m->b[content_type].Key[headerlen - 12] = '\0';
424 m->b[content_type].len = striplt (m->b[content_type].Key);
426 m->b[content_type_name].len = extract_key(m->b[content_type_name].Key, CKEY(m->b[content_type]), HKEY("name"), '=');
427 m->b[charset].len = extract_key(m->b[charset].Key, CKEY(m->b[content_type]), HKEY("charset"), '=');
428 m->b[boundary].len = extract_key(m->b[boundary].Key, header, headerlen, HKEY("boundary"), '=');
430 /* Deal with weird headers */
431 pch = strchr(m->b[content_type].Key, ' ');
434 m->b[content_type].len = m->b[content_type].Key - pch;
436 pch = strchr(m->b[content_type].Key, ';');
439 m->b[content_type].len = m->b[content_type].Key - pch;
442 else if (!strncasecmp(header, "Content-Disposition:", 20)) {
443 memcpy (m->b[disposition].Key, &header[20], headerlen - 19);
444 m->b[disposition].Key[headerlen - 19] = '\0';
445 m->b[disposition].len = striplt(m->b[disposition].Key);
447 m->b[content_disposition_name].len = extract_key(m->b[content_disposition_name].Key, CKEY(m->b[disposition]), HKEY("name"), '=');
448 m->b[filename].len = extract_key(m->b[filename].Key, CKEY(m->b[disposition]), HKEY("filename"), '=');
449 pch = strchr(m->b[disposition].Key, ';');
450 if (pch != NULL) *pch = '\0';
451 m->b[disposition].len = striplt(m->b[disposition].Key);
453 else if (!strncasecmp(header, "Content-ID:", 11)) {
454 memcpy(m->b[id].Key, &header[11], headerlen - 11);
455 m->b[id].Key[headerlen - 11] = '\0';
456 striplt(m->b[id].Key);
457 m->b[id].len = stripallbut(m->b[id].Key, '<', '>');
459 else if (!strncasecmp(header, "Content-length: ", 15)) {
462 while (isspace(*clbuf))
464 m->content_length = (size_t) atol(clbuf);
466 else if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
467 memcpy(m->b[encoding].Key, &header[26], headerlen - 26);
468 m->b[encoding].Key[headerlen - 26] = '\0';
469 m->b[encoding].len = striplt(m->b[encoding].Key);
474 if ((headerlen + buflen + 2) < SIZ) {
475 memcpy(&header[headerlen], buf, buflen);
477 header[headerlen] = '\0';
479 if (ptr >= content_end) {
482 } while ((!IsEmptyStr(buf)) && (*ptr != 0));
484 m->is_multipart = m->b[boundary].len != 0;
485 *pcontent_start = ptr;
491 static int IsAsciiEncoding(interesting_mime_headers *m)
494 if ((m->b[encoding].len != 0) &&
495 (strcasecmp(m->b[encoding].Key, "base64") == 0))
497 if ((m->b[encoding].len != 0) &&
498 (strcmp(m->b[encoding].Key, "quoted-printable") == 0))
504 static char *FindNextContent(char *ptr,
506 interesting_mime_headers *SubMimeHeaders,
507 interesting_mime_headers *m)
512 if (IsAsciiEncoding(SubMimeHeaders)) {
517 * ok, if we have a content length of the mime part,
518 * try skipping the content on the search for the next
519 * boundary. since we don't trust the content_length
520 * to be all accurate, and suspect it to lose one digit
521 * per line with a line length of 80 chars, we need
522 * to start searching a little before..
525 if ((SubMimeHeaders->content_length != -1) &&
526 (SubMimeHeaders->content_length > 10))
531 lines = SubMimeHeaders->content_length / 80;
532 pptr = ptr + SubMimeHeaders->content_length - lines - 10;
533 if (pptr < content_end)
537 next_boundary = strstr(ptr, m->b[startary].Key);
543 * ok, if we have a content length of the mime part,
544 * try skipping the content on the search for the next
545 * boundary. since we don't trust the content_length
546 * to be all accurate, start searching a little before..
549 if ((SubMimeHeaders->content_length != -1) &&
550 (SubMimeHeaders->content_length > 10))
553 pptr = ptr + SubMimeHeaders->content_length - 10;
554 if (pptr < content_end)
559 srch = next_boundary = NULL;
560 for (srch = memchr(ptr, '-', content_end - ptr);
561 (srch != NULL) && (srch < content_end);
562 srch = memchr(srch, '-', content_end - srch))
568 next_boundary = srch;
576 return next_boundary;
580 * Break out the components of a multipart message
581 * (This function expects to be fed HEADERS + CONTENT)
582 * Note: NULL can be supplied as content_end; in this case, the message is
583 * considered to have ended when the parser encounters a 0x00 byte.
585 static void recurseable_mime_parser(char *partnum,
586 char *content_start, char *content_end,
587 MimeParserCallBackType CallBack,
588 MimeParserCallBackType PreMultiPartCallBack,
589 MimeParserCallBackType PostMultiPartCallBack,
592 interesting_mime_headers *m)
594 interesting_mime_headers *SubMimeHeaders;
597 char *part_end = NULL;
598 char *evaluate_crlf_ptr = NULL;
600 char nested_partnum[256];
603 CBufStr *chosen_name;
606 /* If this is a multipart message, then recursively process it */
609 if (m->is_multipart) {
611 /* Tell the client about this message's multipartedness */
612 if (PreMultiPartCallBack != NULL) {
613 PreMultiPartCallBack("",
618 m->b[content_type].Key,
626 /* Figure out where the boundaries are */
627 m->b[startary].len = snprintf(m->b[startary].Key, SIZ, "--%s", m->b[boundary].Key);
628 SubMimeHeaders = InitInterestingMimes ();
633 if (strncmp(ptr, m->b[startary].Key, m->b[startary].len) == 0)
634 ptr += m->b[startary].len;
644 if (parse_MimeHeaders(SubMimeHeaders, &ptr, content_end) != 0)
646 if ((ptr - optr > 2) &&
647 (*(ptr - 2) == '\r'))
652 next_boundary = FindNextContent(ptr,
656 if ((next_boundary != NULL) &&
657 (next_boundary - part_start < 3)) {
658 FlushInterestingMimes(SubMimeHeaders);
663 if ( (part_start != NULL) && (next_boundary != NULL) ) {
664 part_end = next_boundary;
665 --part_end; /* omit the trailing LF */
667 --part_end; /* omit the trailing CR */
670 if (!IsEmptyStr(partnum)) {
671 snprintf(nested_partnum,
672 sizeof nested_partnum,
677 snprintf(nested_partnum,
678 sizeof nested_partnum,
681 recurseable_mime_parser(nested_partnum,
685 PreMultiPartCallBack,
686 PostMultiPartCallBack,
692 if (next_boundary != NULL) {
693 /* If we pass out of scope, don't attempt to
694 * read past the end boundary. */
695 if ((*(next_boundary + m->b[startary].len) == '-') &&
696 (*(next_boundary + m->b[startary].len + 1) == '-') ){
700 /* Set up for the next part. */
701 part_start = strstr(next_boundary, "\n");
703 /* Determine whether newlines are LF or CRLF */
704 evaluate_crlf_ptr = part_start;
706 if ((*evaluate_crlf_ptr == '\r') &&
707 (*(evaluate_crlf_ptr + 1) == '\n'))
715 /* Advance past the LF ... now we're in the next part */
721 /* Invalid end of multipart. Bail out! */
724 FlushInterestingMimes(SubMimeHeaders);
725 } while ( (ptr < content_end) && (next_boundary != NULL) );
727 free(SubMimeHeaders);
729 if (PostMultiPartCallBack != NULL) {
730 PostMultiPartCallBack("",
735 m->b[content_type].Key,
742 } /* If it's not a multipart message, then do something with it */
746 length = content_end - part_start;
747 ptr = part_end = content_end;
750 /* The following code will truncate the MIME part to the size
751 * specified by the Content-length: header. We have commented it
752 * out because these headers have a tendency to be wrong.
754 * if ( (content_length > 0) && (length > content_length) ) {
755 * length = content_length;
759 /* Sometimes the "name" field is tacked on to Content-type,
760 * and sometimes it's tacked on to Content-disposition. Use
761 * whichever one we have.
763 if (m->b[content_disposition_name].len > m->b[content_type_name].len) {
764 chosen_name = &m->b[content_disposition_name];
767 chosen_name = &m->b[content_type_name];
770 /* Ok, we've got a non-multipart part here, so do something with it.
775 m->b[content_type].Key,
778 m->b[disposition].Key,
789 * Now if it's an encapsulated message/rfc822 then we have to recurse into it
791 if (!strcasecmp(&m->b[content_type].Key[0], "message/rfc822")) {
793 if (PreMultiPartCallBack != NULL) {
794 PreMultiPartCallBack("",
799 m->b[content_type].Key,
806 if (CallBack != NULL) {
807 if (strlen(partnum) > 0) {
808 snprintf(nested_partnum,
809 sizeof nested_partnum,
814 snprintf(nested_partnum,
815 sizeof nested_partnum,
818 the_mime_parser(nested_partnum,
822 PreMultiPartCallBack,
823 PostMultiPartCallBack,
828 if (PostMultiPartCallBack != NULL) {
829 PostMultiPartCallBack("",
834 m->b[content_type].Key,
850 * Break out the components of a multipart message
851 * (This function expects to be fed HEADERS + CONTENT)
852 * Note: NULL can be supplied as content_end; in this case, the message is
853 * considered to have ended when the parser encounters a 0x00 byte.
855 void the_mime_parser(char *partnum,
856 char *content_start, char *content_end,
857 MimeParserCallBackType CallBack,
858 MimeParserCallBackType PreMultiPartCallBack,
859 MimeParserCallBackType PostMultiPartCallBack,
863 interesting_mime_headers *m;
865 /* If the caller didn't supply an endpointer, generate one by measure */
866 if (content_end == NULL) {
867 content_end = &content_start[strlen(content_start)];
870 m = InitInterestingMimes();
872 if (!parse_MimeHeaders(m, &content_start, content_end))
875 recurseable_mime_parser(partnum,
876 content_start, content_end,
878 PreMultiPartCallBack,
879 PostMultiPartCallBack,
888 * Entry point for the MIME parser.
889 * (This function expects to be fed HEADERS + CONTENT)
890 * Note: NULL can be supplied as content_end; in this case, the message is
891 * considered to have ended when the parser encounters a 0x00 byte.
893 void mime_parser(char *content_start,
895 MimeParserCallBackType CallBack,
896 MimeParserCallBackType PreMultiPartCallBack,
897 MimeParserCallBackType PostMultiPartCallBack,
902 the_mime_parser("", content_start, content_end,
904 PreMultiPartCallBack,
905 PostMultiPartCallBack,
906 userdata, dont_decode);
914 typedef struct _MimeGuess {
918 const char *MimeString;
921 MimeGuess MyMimes [] = {
949 const char *GuessMimeType(const char *data, size_t dlen)
953 while (MyMimes[MimeIndex].PatternLen != 0)
955 if ((MyMimes[MimeIndex].PatternLen +
956 MyMimes[MimeIndex].PatternOffset < dlen) &&
957 strncmp(MyMimes[MimeIndex].Pattern,
958 &data[MyMimes[MimeIndex].PatternOffset],
959 MyMimes[MimeIndex].PatternLen) == 0)
961 return MyMimes[MimeIndex].MimeString;
966 * ok, our simple minded algorythm didn't find anything,
967 * let the big chegger try it, he wil default to application/octet-stream
969 return (xdg_mime_get_mime_type_for_data(data, dlen));
973 const char* GuessMimeByFilename(const char *what, size_t len)
975 /* we know some hardcoded on our own, try them... */
976 if ((len > 3) && !strncasecmp(&what[len - 4], ".gif", 4))
978 else if ((len > 2) && !strncasecmp(&what[len - 3], ".js", 3))
979 return "text/javascript";
980 else if ((len > 3) && !strncasecmp(&what[len - 4], ".txt", 4))
982 else if ((len > 3) && !strncasecmp(&what[len - 4], ".css", 4))
984 else if ((len > 3) && !strncasecmp(&what[len - 4], ".htc", 4))
985 return "text/x-component";
986 else if ((len > 3) && !strncasecmp(&what[len - 4], ".jpg", 4))
988 else if ((len > 3) && !strncasecmp(&what[len - 4], ".png", 4))
990 else if ((len > 3) && !strncasecmp(&what[len - 4], ".ico", 4))
991 return "image/x-icon";
992 else if ((len > 3) && !strncasecmp(&what[len - 4], ".vcf", 4))
993 return "text/x-vcard";
994 else if ((len > 4) && !strncasecmp(&what[len - 5], ".html", 5))
996 else if ((len > 3) && !strncasecmp(&what[len - 4], ".htm", 4))
998 else if ((len > 3) && !strncasecmp(&what[len - 4], ".wml", 4))
999 return "text/vnd.wap.wml";
1000 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmls", 5))
1001 return "text/vnd.wap.wmlscript";
1002 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmlc", 5))
1003 return "application/vnd.wap.wmlc";
1004 else if ((len > 5) && !strncasecmp(&what[len - 6], ".wmlsc", 6))
1005 return "application/vnd.wap.wmlscriptc";
1006 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wbmp", 5))
1007 return "image/vnd.wap.wbmp";
1009 /* and let xdgmime do the fallback. */
1010 return xdg_mime_get_mime_type_from_file_name(what);
1013 static HashList *IconHash = NULL;
1015 typedef struct IconName IconName;
1022 static void DeleteIcon(void *IconNamePtr)
1024 IconName *Icon = (IconName*) IconNamePtr;
1025 free(Icon->FlatName);
1026 free(Icon->FileName);
1031 static const char *PrintFlat(void *IconNamePtr)
1033 IconName *Icon = (IconName*) IconNamePtr;
1034 return Icon->FlatName;
1036 static const char *PrintFile(void *IconNamePtr)
1038 IconName *Icon = (IconName*) IconNamePtr;
1039 return Icon->FileName;
1043 #define GENSTR "x-generic"
1044 #define IGNORE_PREFIX_1 "gnome-mime"
1045 int LoadIconDir(const char *DirName)
1047 DIR *filedir = NULL;
1048 struct dirent *filedir_entry;
1053 filedir = opendir (DirName);
1054 IconHash = NewHash(1, NULL);
1055 if (filedir == NULL) {
1059 while ((filedir_entry = readdir(filedir)))
1063 #ifdef _DIRENT_HAVE_D_NAMELEN
1064 d_namelen = filedir_entry->d_namelen;
1066 d_namelen = strlen(filedir_entry->d_name);
1068 d_without_ext = d_namelen;
1069 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1071 if ((d_without_ext == 0) || (d_namelen < 3))
1074 if ((sizeof(IGNORE_PREFIX_1) < d_namelen) &&
1075 (strncmp(IGNORE_PREFIX_1,
1076 filedir_entry->d_name,
1077 sizeof(IGNORE_PREFIX_1) - 1) == 0)) {
1078 PStart = filedir_entry->d_name + sizeof(IGNORE_PREFIX_1);
1079 d_without_ext -= sizeof(IGNORE_PREFIX_1);
1082 PStart = filedir_entry->d_name;
1084 Icon = malloc(sizeof(IconName));
1086 Icon->FileName = malloc(d_namelen + 1);
1087 memcpy(Icon->FileName, filedir_entry->d_name, d_namelen + 1);
1089 Icon->FlatName = malloc(d_without_ext + 1);
1090 memcpy(Icon->FlatName, PStart, d_without_ext);
1091 Icon->FlatName[d_without_ext] = '\0';
1092 /* Try to find Minor type in image-jpeg */
1093 MinorPtr = strchr(Icon->FlatName, '-');
1094 if (MinorPtr != NULL) {
1096 MinorLen = 1 + d_without_ext - (MinorPtr - Icon->FlatName + 1);
1097 if ((MinorLen == sizeof(GENSTR)) &&
1098 (strncmp(MinorPtr + 1, GENSTR, sizeof(GENSTR)) == 0)) {
1099 /* ok, we found a generic filename. cut the generic. */
1101 d_without_ext = d_without_ext - (MinorPtr - Icon->FlatName);
1103 else { /* Map the major / minor separator to / */
1108 // PrintHash(IconHash, PrintFlat, PrintFile);
1109 // printf("%s - %s\n", Icon->FlatName, Icon->FileName);
1110 Put(IconHash, Icon->FlatName, d_without_ext, Icon, DeleteIcon);
1111 // PrintHash(IconHash, PrintFlat, PrintFile);
1117 const char *GetIconFilename(char *MimeType, size_t len)
1122 if(IconHash == NULL)
1125 GetHash(IconHash, MimeType, len, &vIcon), Icon = (IconName*) vIcon;
1126 /* didn't find the exact mimetype? try major only. */
1129 pMinor = strchr(MimeType, '/');
1130 if (pMinor != NULL) {
1132 GetHash(IconHash, MimeType, pMinor - MimeType, &vIcon),
1133 Icon = (IconName*) vIcon;
1140 /*printf("Getting: [%s] == [%s] -> [%s]\n", MimeType, Icon->FlatName, Icon->FileName);*/
1141 return Icon->FileName;
1144 void ShutDownLibCitadelMime(void)
1146 DeleteHash(&IconHash);