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)
138 unsigned int ret = '?';
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')
181 ch = _decode_hex(&encoded[pos]);
183 decoded[decoded_length++] = ch;
188 decoded[decoded_length++] = encoded[pos];
192 decoded[decoded_length] = 0;
193 return(decoded_length);
198 * Given a message or message-part body and a length, handle any necessary
199 * decoding and pass the request up the stack.
201 void mime_decode(char *partnum,
202 char *part_start, size_t length,
203 char *content_type, char *charset, char *encoding,
206 char *name, char *filename,
207 MimeParserCallBackType CallBack,
208 MimeParserCallBackType PreMultiPartCallBack,
209 MimeParserCallBackType PostMultiPartCallBack,
215 size_t bytes_decoded = 0;
217 /* Some encodings aren't really encodings */
218 if (!strcasecmp(encoding, "7bit"))
220 if (!strcasecmp(encoding, "8bit"))
222 if (!strcasecmp(encoding, "binary"))
224 if (!strcasecmp(encoding, "ISO-8859-1"))
227 /* If this part is not encoded, send as-is */
228 if ( (strlen(encoding) == 0) || (dont_decode)) {
229 if (CallBack != NULL) {
232 fixed_partnum(partnum),
245 /* Fail silently if we hit an unknown encoding. */
246 if ((strcasecmp(encoding, "base64"))
247 && (strcasecmp(encoding, "quoted-printable"))) {
252 * Allocate a buffer for the decoded data. The output buffer is slightly
253 * larger than the input buffer; this assumes that the decoded data
254 * will never be significantly larger than the encoded data. This is a
255 * safe assumption with base64, uuencode, and quoted-printable.
257 decoded = malloc(length + 32768);
258 if (decoded == NULL) {
262 if (!strcasecmp(encoding, "base64")) {
263 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
265 else if (!strcasecmp(encoding, "quoted-printable")) {
266 bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
269 if (bytes_decoded > 0) if (CallBack != NULL) {
270 char encoding_buf[SIZ];
272 strcpy(encoding_buf, "binary");
275 fixed_partnum(partnum),
290 * this is the extract of mime_decode which can be called if 'dont_decode' was set;
291 * to save the cpu intense process of decoding to the time when it realy wants the content.
293 * - > 0 we decoded something, its on *decoded, you need to free it.
294 * - = 0 no need to decode stuff. *decoded will be NULL.
295 * - < 0 an error occured, either an unknown encoding, or alloc failed. no need to free.
297 int mime_decode_now (char *part_start,
301 size_t *bytes_decoded)
305 /* Some encodings aren't really encodings */
306 if (!strcasecmp(encoding, "7bit"))
308 if (!strcasecmp(encoding, "8bit"))
310 if (!strcasecmp(encoding, "binary"))
313 /* If this part is not encoded, send as-is */
314 if (strlen(encoding) == 0) {
319 /* Fail if we hit an unknown encoding. */
320 if ((strcasecmp(encoding, "base64"))
321 && (strcasecmp(encoding, "quoted-printable"))) {
326 * Allocate a buffer for the decoded data. The output buffer is slightly
327 * larger than the input buffer; this assumes that the decoded data
328 * will never be significantly larger than the encoded data. This is a
329 * safe assumption with base64, uuencode, and quoted-printable.
331 *decoded = malloc(length + 32768);
332 if (decoded == NULL) {
336 if (!strcasecmp(encoding, "base64")) {
337 *bytes_decoded = CtdlDecodeBase64(*decoded, part_start, length);
340 else if (!strcasecmp(encoding, "quoted-printable")) {
341 *bytes_decoded = CtdlDecodeQuotedPrintable(*decoded, part_start, length);
347 typedef enum _eIntMimeHdrs {
355 content_disposition_name,
359 eMax /* don't move ! */
362 typedef struct _CBufStr {
367 typedef struct _interesting_mime_headers {
371 } interesting_mime_headers;
374 static void FlushInterestingMimes(interesting_mime_headers *m)
378 for (i = 0; i < eMax; i++) {
379 m->b[i].Key[0] = '\0';
382 m->content_length = -1;
384 static interesting_mime_headers *InitInterestingMimes(void)
386 interesting_mime_headers *m;
387 m = (interesting_mime_headers*) malloc( sizeof(interesting_mime_headers));
389 FlushInterestingMimes(m);
395 static long parse_MimeHeaders(interesting_mime_headers *m,
396 char** pcontent_start,
406 /* Learn interesting things from the headers */
407 ptr = *pcontent_start;
411 ptr = memreadlinelen(ptr, buf, SIZ, &buflen);
413 for (i = 0; i < buflen; ++i) {
414 if (isspace(buf[i])) {
419 if (!isspace(buf[0]) && (headerlen > 0)) {
420 if (!strncasecmp(header, "Content-type:", 13)) {
421 memcpy (m->b[content_type].Key, &header[13], headerlen - 12);
422 m->b[content_type].Key[headerlen - 12] = '\0';
423 m->b[content_type].len = striplt (m->b[content_type].Key);
425 m->b[content_type_name].len = extract_key(m->b[content_type_name].Key, CKEY(m->b[content_type]), HKEY("name"), '=');
426 m->b[charset].len = extract_key(m->b[charset].Key, CKEY(m->b[content_type]), HKEY("charset"), '=');
427 m->b[boundary].len = extract_key(m->b[boundary].Key, header, headerlen, HKEY("boundary"), '=');
429 /* Deal with weird headers */
430 pch = strchr(m->b[content_type].Key, ' ');
433 m->b[content_type].len = m->b[content_type].Key - pch;
435 pch = strchr(m->b[content_type].Key, ';');
438 m->b[content_type].len = m->b[content_type].Key - pch;
441 else if (!strncasecmp(header, "Content-Disposition:", 20)) {
442 memcpy (m->b[disposition].Key, &header[20], headerlen - 19);
443 m->b[disposition].Key[headerlen - 19] = '\0';
444 m->b[disposition].len = striplt(m->b[disposition].Key);
446 m->b[content_disposition_name].len = extract_key(m->b[content_disposition_name].Key, CKEY(m->b[disposition]), HKEY("name"), '=');
447 m->b[filename].len = extract_key(m->b[filename].Key, CKEY(m->b[disposition]), HKEY("filename"), '=');
448 pch = strchr(m->b[disposition].Key, ';');
449 if (pch != NULL) *pch = '\0';
450 m->b[disposition].len = striplt(m->b[disposition].Key);
452 else if (!strncasecmp(header, "Content-ID:", 11)) {
453 memcpy(m->b[id].Key, &header[11], headerlen - 11);
454 m->b[id].Key[headerlen - 11] = '\0';
455 striplt(m->b[id].Key);
456 m->b[id].len = stripallbut(m->b[id].Key, '<', '>');
458 else if (!strncasecmp(header, "Content-length: ", 15)) {
461 while (isspace(*clbuf))
463 m->content_length = (size_t) atol(clbuf);
465 else if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
466 memcpy(m->b[encoding].Key, &header[26], headerlen - 26);
467 m->b[encoding].Key[headerlen - 26] = '\0';
468 m->b[encoding].len = striplt(m->b[encoding].Key);
473 if ((headerlen + buflen + 2) < SIZ) {
474 memcpy(&header[headerlen], buf, buflen);
476 header[headerlen] = '\0';
478 if (ptr >= content_end) {
481 } while ((!IsEmptyStr(buf)) && (*ptr != 0));
483 m->is_multipart = m->b[boundary].len != 0;
484 *pcontent_start = ptr;
490 static int IsAsciiEncoding(interesting_mime_headers *m)
493 if ((m->b[encoding].len != 0) &&
494 (strcasecmp(m->b[encoding].Key, "base64") == 0))
496 if ((m->b[encoding].len != 0) &&
497 (strcmp(m->b[encoding].Key, "quoted-printable") == 0))
503 static char *FindNextContent(char *ptr,
505 interesting_mime_headers *SubMimeHeaders,
506 interesting_mime_headers *m)
511 if (IsAsciiEncoding(SubMimeHeaders)) {
516 * ok, if we have a content length of the mime part,
517 * try skipping the content on the search for the next
518 * boundary. since we don't trust the content_length
519 * to be all accurate, and suspect it to lose one digit
520 * per line with a line length of 80 chars, we need
521 * to start searching a little before..
524 if ((SubMimeHeaders->content_length != -1) &&
525 (SubMimeHeaders->content_length > 10))
530 lines = SubMimeHeaders->content_length / 80;
531 pptr = ptr + SubMimeHeaders->content_length - lines - 10;
532 if (pptr < content_end)
536 next_boundary = strstr(ptr, m->b[startary].Key);
542 * ok, if we have a content length of the mime part,
543 * try skipping the content on the search for the next
544 * boundary. since we don't trust the content_length
545 * to be all accurate, start searching a little before..
548 if ((SubMimeHeaders->content_length != -1) &&
549 (SubMimeHeaders->content_length > 10))
552 pptr = ptr + SubMimeHeaders->content_length - 10;
553 if (pptr < content_end)
558 srch = next_boundary = NULL;
559 for (srch = memchr(ptr, '-', content_end - ptr);
560 (srch != NULL) && (srch < content_end);
561 srch = memchr(srch, '-', content_end - srch))
567 next_boundary = srch;
575 return next_boundary;
579 * Break out the components of a multipart message
580 * (This function expects to be fed HEADERS + CONTENT)
581 * Note: NULL can be supplied as content_end; in this case, the message is
582 * considered to have ended when the parser encounters a 0x00 byte.
584 static void recurseable_mime_parser(char *partnum,
585 char *content_start, char *content_end,
586 MimeParserCallBackType CallBack,
587 MimeParserCallBackType PreMultiPartCallBack,
588 MimeParserCallBackType PostMultiPartCallBack,
591 interesting_mime_headers *m)
593 interesting_mime_headers *SubMimeHeaders;
596 char *part_end = NULL;
597 char *evaluate_crlf_ptr = NULL;
599 char nested_partnum[256];
602 CBufStr *chosen_name;
605 /* If this is a multipart message, then recursively process it */
608 if (m->is_multipart) {
610 /* Tell the client about this message's multipartedness */
611 if (PreMultiPartCallBack != NULL) {
612 PreMultiPartCallBack("",
617 m->b[content_type].Key,
625 /* Figure out where the boundaries are */
626 m->b[startary].len = snprintf(m->b[startary].Key, SIZ, "--%s", m->b[boundary].Key);
627 SubMimeHeaders = InitInterestingMimes ();
629 while ((*ptr == '\r') || (*ptr == '\n')) ptr ++;
631 if (strncmp(ptr, m->b[startary].Key, m->b[startary].len) == 0)
632 ptr += m->b[startary].len;
634 while ((*ptr == '\r') || (*ptr == '\n')) ptr ++;
641 if (parse_MimeHeaders(SubMimeHeaders, &ptr, content_end) != 0)
643 if ((ptr - optr > 2) &&
644 (*(ptr - 2) == '\r'))
649 next_boundary = FindNextContent(ptr,
653 if ((next_boundary != NULL) &&
654 (next_boundary - part_start < 3)) {
655 FlushInterestingMimes(SubMimeHeaders);
660 if ( (part_start != NULL) && (next_boundary != NULL) ) {
661 part_end = next_boundary;
662 --part_end; /* omit the trailing LF */
664 --part_end; /* omit the trailing CR */
667 if (!IsEmptyStr(partnum)) {
668 snprintf(nested_partnum,
669 sizeof nested_partnum,
674 snprintf(nested_partnum,
675 sizeof nested_partnum,
678 recurseable_mime_parser(nested_partnum,
682 PreMultiPartCallBack,
683 PostMultiPartCallBack,
689 if (next_boundary != NULL) {
690 /* If we pass out of scope, don't attempt to
691 * read past the end boundary. */
692 if ((*(next_boundary + m->b[startary].len) == '-') &&
693 (*(next_boundary + m->b[startary].len + 1) == '-') ){
697 /* Set up for the next part. */
698 part_start = strstr(next_boundary, "\n");
700 /* Determine whether newlines are LF or CRLF */
701 evaluate_crlf_ptr = part_start;
703 if ((*evaluate_crlf_ptr == '\r') &&
704 (*(evaluate_crlf_ptr + 1) == '\n'))
712 /* Advance past the LF ... now we're in the next part */
718 /* Invalid end of multipart. Bail out! */
721 FlushInterestingMimes(SubMimeHeaders);
722 } while ( (ptr < content_end) && (next_boundary != NULL) );
724 free(SubMimeHeaders);
726 if (PostMultiPartCallBack != NULL) {
727 PostMultiPartCallBack("",
732 m->b[content_type].Key,
739 } /* If it's not a multipart message, then do something with it */
743 length = content_end - part_start;
744 ptr = part_end = content_end;
747 /* The following code will truncate the MIME part to the size
748 * specified by the Content-length: header. We have commented it
749 * out because these headers have a tendency to be wrong.
751 * if ( (content_length > 0) && (length > content_length) ) {
752 * length = content_length;
756 /* Sometimes the "name" field is tacked on to Content-type,
757 * and sometimes it's tacked on to Content-disposition. Use
758 * whichever one we have.
760 if (m->b[content_disposition_name].len > m->b[content_type_name].len) {
761 chosen_name = &m->b[content_disposition_name];
764 chosen_name = &m->b[content_type_name];
767 /* Ok, we've got a non-multipart part here, so do something with it.
772 m->b[content_type].Key,
775 m->b[disposition].Key,
786 * Now if it's an encapsulated message/rfc822 then we have to recurse into it
788 if (!strcasecmp(&m->b[content_type].Key[0], "message/rfc822")) {
790 if (PreMultiPartCallBack != NULL) {
791 PreMultiPartCallBack("",
796 m->b[content_type].Key,
803 if (CallBack != NULL) {
804 if (strlen(partnum) > 0) {
805 snprintf(nested_partnum,
806 sizeof nested_partnum,
811 snprintf(nested_partnum,
812 sizeof nested_partnum,
815 the_mime_parser(nested_partnum,
819 PreMultiPartCallBack,
820 PostMultiPartCallBack,
825 if (PostMultiPartCallBack != NULL) {
826 PostMultiPartCallBack("",
831 m->b[content_type].Key,
847 * Break out the components of a multipart message
848 * (This function expects to be fed HEADERS + CONTENT)
849 * Note: NULL can be supplied as content_end; in this case, the message is
850 * considered to have ended when the parser encounters a 0x00 byte.
852 void the_mime_parser(char *partnum,
853 char *content_start, char *content_end,
854 MimeParserCallBackType CallBack,
855 MimeParserCallBackType PreMultiPartCallBack,
856 MimeParserCallBackType PostMultiPartCallBack,
860 interesting_mime_headers *m;
862 /* If the caller didn't supply an endpointer, generate one by measure */
863 if (content_end == NULL) {
864 content_end = &content_start[strlen(content_start)];
867 m = InitInterestingMimes();
869 if (!parse_MimeHeaders(m, &content_start, content_end))
872 recurseable_mime_parser(partnum,
873 content_start, content_end,
875 PreMultiPartCallBack,
876 PostMultiPartCallBack,
885 * Entry point for the MIME parser.
886 * (This function expects to be fed HEADERS + CONTENT)
887 * Note: NULL can be supplied as content_end; in this case, the message is
888 * considered to have ended when the parser encounters a 0x00 byte.
890 void mime_parser(char *content_start,
892 MimeParserCallBackType CallBack,
893 MimeParserCallBackType PreMultiPartCallBack,
894 MimeParserCallBackType PostMultiPartCallBack,
899 the_mime_parser("", content_start, content_end,
901 PreMultiPartCallBack,
902 PostMultiPartCallBack,
903 userdata, dont_decode);
911 typedef struct _MimeGuess {
915 const char *MimeString;
918 MimeGuess MyMimes [] = {
946 const char *GuessMimeType(const char *data, size_t dlen)
950 while (MyMimes[MimeIndex].PatternLen != 0)
952 if ((MyMimes[MimeIndex].PatternLen +
953 MyMimes[MimeIndex].PatternOffset < dlen) &&
954 strncmp(MyMimes[MimeIndex].Pattern,
955 &data[MyMimes[MimeIndex].PatternOffset],
956 MyMimes[MimeIndex].PatternLen) == 0)
958 return MyMimes[MimeIndex].MimeString;
963 * ok, our simple minded algorythm didn't find anything,
964 * let the big chegger try it, he wil default to application/octet-stream
966 return (xdg_mime_get_mime_type_for_data(data, dlen));
970 const char* GuessMimeByFilename(const char *what, size_t len)
972 /* we know some hardcoded on our own, try them... */
973 if ((len > 3) && !strncasecmp(&what[len - 4], ".gif", 4))
975 else if ((len > 2) && !strncasecmp(&what[len - 3], ".js", 3))
976 return "text/javascript";
977 else if ((len > 3) && !strncasecmp(&what[len - 4], ".txt", 4))
979 else if ((len > 3) && !strncasecmp(&what[len - 4], ".css", 4))
981 else if ((len > 3) && !strncasecmp(&what[len - 4], ".htc", 4))
982 return "text/x-component";
983 else if ((len > 3) && !strncasecmp(&what[len - 4], ".jpg", 4))
985 else if ((len > 4) && !strncasecmp(&what[len - 5], ".jpeg", 5))
987 else if ((len > 3) && !strncasecmp(&what[len - 4], ".png", 4))
989 else if ((len > 3) && !strncasecmp(&what[len - 4], ".ico", 4))
990 return "image/x-icon";
991 else if ((len > 3) && !strncasecmp(&what[len - 4], ".vcf", 4))
992 return "text/x-vcard";
993 else if ((len > 4) && !strncasecmp(&what[len - 5], ".html", 5))
995 else if ((len > 3) && !strncasecmp(&what[len - 4], ".htm", 4))
997 else if ((len > 3) && !strncasecmp(&what[len - 4], ".wml", 4))
998 return "text/vnd.wap.wml";
999 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmls", 5))
1000 return "text/vnd.wap.wmlscript";
1001 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmlc", 5))
1002 return "application/vnd.wap.wmlc";
1003 else if ((len > 5) && !strncasecmp(&what[len - 6], ".wmlsc", 6))
1004 return "application/vnd.wap.wmlscriptc";
1005 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wbmp", 5))
1006 return "image/vnd.wap.wbmp";
1008 /* and let xdgmime do the fallback. */
1009 return xdg_mime_get_mime_type_from_file_name(what);
1012 static HashList *IconHash = NULL;
1014 typedef struct IconName IconName;
1021 static void DeleteIcon(void *IconNamePtr)
1023 IconName *Icon = (IconName*) IconNamePtr;
1024 free(Icon->FlatName);
1025 free(Icon->FileName);
1030 static const char *PrintFlat(void *IconNamePtr)
1032 IconName *Icon = (IconName*) IconNamePtr;
1033 return Icon->FlatName;
1035 static const char *PrintFile(void *IconNamePtr)
1037 IconName *Icon = (IconName*) IconNamePtr;
1038 return Icon->FileName;
1042 #define GENSTR "x-generic"
1043 #define IGNORE_PREFIX_1 "gnome-mime"
1044 int LoadIconDir(const char *DirName)
1046 DIR *filedir = NULL;
1047 struct dirent *filedir_entry;
1052 filedir = opendir (DirName);
1053 IconHash = NewHash(1, NULL);
1054 if (filedir == NULL) {
1058 while ((filedir_entry = readdir(filedir)))
1062 #ifdef _DIRENT_HAVE_D_NAMLEN
1063 d_namelen = filedir_entry->d_namlen;
1065 d_namelen = strlen(filedir_entry->d_name);
1067 d_without_ext = d_namelen;
1068 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1070 if ((d_without_ext == 0) || (d_namelen < 3))
1073 if ((sizeof(IGNORE_PREFIX_1) < d_namelen) &&
1074 (strncmp(IGNORE_PREFIX_1,
1075 filedir_entry->d_name,
1076 sizeof(IGNORE_PREFIX_1) - 1) == 0)) {
1077 PStart = filedir_entry->d_name + sizeof(IGNORE_PREFIX_1);
1078 d_without_ext -= sizeof(IGNORE_PREFIX_1);
1081 PStart = filedir_entry->d_name;
1083 Icon = malloc(sizeof(IconName));
1085 Icon->FileName = malloc(d_namelen + 1);
1086 memcpy(Icon->FileName, filedir_entry->d_name, d_namelen + 1);
1088 Icon->FlatName = malloc(d_without_ext + 1);
1089 memcpy(Icon->FlatName, PStart, d_without_ext);
1090 Icon->FlatName[d_without_ext] = '\0';
1091 /* Try to find Minor type in image-jpeg */
1092 MinorPtr = strchr(Icon->FlatName, '-');
1093 if (MinorPtr != NULL) {
1095 MinorLen = 1 + d_without_ext - (MinorPtr - Icon->FlatName + 1);
1096 if ((MinorLen == sizeof(GENSTR)) &&
1097 (strncmp(MinorPtr + 1, GENSTR, sizeof(GENSTR)) == 0)) {
1098 /* ok, we found a generic filename. cut the generic. */
1100 d_without_ext = d_without_ext - (MinorPtr - Icon->FlatName);
1102 else { /* Map the major / minor separator to / */
1107 // PrintHash(IconHash, PrintFlat, PrintFile);
1108 // printf("%s - %s\n", Icon->FlatName, Icon->FileName);
1109 Put(IconHash, Icon->FlatName, d_without_ext, Icon, DeleteIcon);
1110 // PrintHash(IconHash, PrintFlat, PrintFile);
1116 const char *GetIconFilename(char *MimeType, size_t len)
1121 if(IconHash == NULL)
1124 GetHash(IconHash, MimeType, len, &vIcon), Icon = (IconName*) vIcon;
1125 /* didn't find the exact mimetype? try major only. */
1128 pMinor = strchr(MimeType, '/');
1129 if (pMinor != NULL) {
1131 GetHash(IconHash, MimeType, pMinor - MimeType, &vIcon),
1132 Icon = (IconName*) vIcon;
1139 /*printf("Getting: [%s] == [%s] -> [%s]\n", MimeType, Icon->FlatName, Icon->FileName);*/
1140 return Icon->FileName;
1143 void ShutDownLibCitadelMime(void)
1145 DeleteHash(&IconHash);