1 // This is the MIME parser for Citadel.
3 // Copyright (c) 1998-2022 by the citadel.org development team.
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.
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.
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
23 #include <sys/types.h>
27 #include <sys/types.h>
31 #include "xdgmime/xdgmime.h"
32 #include "libcitadel.h"
33 #include "libcitadellocal.h"
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
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;
72 while (sptr != NULL) {
73 ptr = bmstrcasestr_len(sptr, sourcelen - (sptr - source), key, keylen);
75 while (isspace(*(ptr + RealKeyLen)))
77 if (*(ptr + RealKeyLen) == KeyEnd) {
82 sptr = ptr + RealKeyLen + 1;
92 strcpy(target, (ptr + RealKeyLen));
94 for (ptr=target; (*ptr != 0); ptr++) {
96 /* A semicolon means we've hit the end of the key, unless we're inside double quotes */
97 if ( (double_quotes != 1) && (*ptr == ';')) {
101 /* if we find double quotes, we've got a great set of string boundaries */
104 if (double_quotes == 1) {
118 * For non-multipart messages, we need to generate a quickie partnum of "1"
119 * to return to callback functions. Some callbacks demand it.
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;
128 static inline unsigned int _decode_hex(const char *Source) {
129 unsigned int ret = '?';
130 unsigned char LO_NIBBLE;
131 unsigned char HI_NIBBLE;
133 HI_NIBBLE = FromHexTable[(unsigned char) *Source];
134 LO_NIBBLE = FromHexTable[(unsigned char) *(Source+1)];
136 if ((LO_NIBBLE == 0xFF) || (LO_NIBBLE == 0xFF))
140 ret = ret | LO_NIBBLE;
144 unsigned int decode_hex(char *Source) {return _decode_hex(Source);}
147 * Convert "quoted-printable" to binary. Returns number of bytes decoded.
148 * according to RFC2045 section 6.7
150 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
152 int decoded_length = 0;
155 while (pos < sourcelen) {
156 if (*(encoded + pos) == '=') {
158 if (*(encoded + pos) == '\n') {
161 else if (*(encoded + pos) == '\r') {
163 if (*(encoded + pos) == '\n')
167 ch = _decode_hex(&encoded[pos]);
169 decoded[decoded_length++] = ch;
173 decoded[decoded_length++] = encoded[pos];
177 decoded[decoded_length] = 0;
178 return(decoded_length);
183 * Given a message or message-part body and a length, handle any necessary
184 * decoding and pass the request up the stack.
186 void mime_decode(char *partnum,
187 char *part_start, size_t length,
188 char *content_type, char *charset, char *encoding,
191 char *name, char *filename,
192 MimeParserCallBackType CallBack,
193 MimeParserCallBackType PreMultiPartCallBack,
194 MimeParserCallBackType PostMultiPartCallBack,
199 size_t bytes_decoded = 0;
201 // Some encodings aren't really encodings
202 if (!strcasecmp(encoding, "7bit"))
204 if (!strcasecmp(encoding, "8bit"))
206 if (!strcasecmp(encoding, "binary"))
208 if (!strcasecmp(encoding, "ISO-8859-1"))
211 // If this part is not encoded, send as-is
212 if ( (strlen(encoding) == 0) || (dont_decode)) {
213 if (CallBack != NULL) {
216 fixed_partnum(partnum),
229 // Fail silently if we hit an unknown encoding.
230 if ((strcasecmp(encoding, "base64")) && (strcasecmp(encoding, "quoted-printable"))) {
233 fprintf(stderr, "\033[33mSource encoded length: %d\033[0m\n", length);
235 // Allocate a buffer for the decoded data. The output buffer is slightly
236 // larger than the input buffer; this assumes that the decoded data
237 // will never be significantly larger than the encoded data. This is a
238 // safe assumption with base64, uuencode, and quoted-printable.
239 decoded = malloc(length + 32768);
240 if (decoded == NULL) {
244 if (!strcasecmp(encoding, "base64")) {
245 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
247 else if (!strcasecmp(encoding, "quoted-printable")) {
248 bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
250 fprintf(stderr, "\033[33mTarget decoded length: %d\033[0m\n", bytes_decoded);
252 if (bytes_decoded > 0) if (CallBack != NULL) {
253 char encoding_buf[SIZ];
255 strcpy(encoding_buf, "binary");
258 fixed_partnum(partnum),
273 * this is the extract of mime_decode which can be called if 'dont_decode' was set;
274 * to save the cpu intense process of decoding to the time when it realy wants the content.
276 * - > 0 we decoded something, its on *decoded, you need to free it.
277 * - = 0 no need to decode stuff. *decoded will be NULL.
278 * - < 0 an error occured, either an unknown encoding, or alloc failed. no need to free.
280 int mime_decode_now (char *part_start,
284 size_t *bytes_decoded)
288 /* Some encodings aren't really encodings */
289 if (!strcasecmp(encoding, "7bit"))
291 if (!strcasecmp(encoding, "8bit"))
293 if (!strcasecmp(encoding, "binary"))
296 /* If this part is not encoded, send as-is */
297 if (strlen(encoding) == 0) {
302 /* Fail if we hit an unknown encoding. */
303 if ((strcasecmp(encoding, "base64"))
304 && (strcasecmp(encoding, "quoted-printable"))) {
309 * Allocate a buffer for the decoded data. The output buffer is slightly
310 * larger than the input buffer; this assumes that the decoded data
311 * will never be significantly larger than the encoded data. This is a
312 * safe assumption with base64, uuencode, and quoted-printable.
314 *decoded = malloc(length + 32768);
315 if (decoded == NULL) {
319 if (!strcasecmp(encoding, "base64")) {
320 *bytes_decoded = CtdlDecodeBase64(*decoded, part_start, length);
323 else if (!strcasecmp(encoding, "quoted-printable")) {
324 *bytes_decoded = CtdlDecodeQuotedPrintable(*decoded, part_start, length);
330 typedef enum _eIntMimeHdrs {
338 content_disposition_name,
342 eMax /* don't move ! */
345 typedef struct _CBufStr {
350 typedef struct _interesting_mime_headers {
354 } interesting_mime_headers;
357 static void FlushInterestingMimes(interesting_mime_headers *m)
361 for (i = 0; i < eMax; i++) {
362 m->b[i].Key[0] = '\0';
365 m->content_length = -1;
367 static interesting_mime_headers *InitInterestingMimes(void)
369 interesting_mime_headers *m;
370 m = (interesting_mime_headers*) malloc( sizeof(interesting_mime_headers));
372 FlushInterestingMimes(m);
378 static long parse_MimeHeaders(interesting_mime_headers *m,
379 char** pcontent_start,
389 /* Learn interesting things from the headers */
390 ptr = *pcontent_start;
394 ptr = memreadlinelen(ptr, buf, SIZ, &buflen);
396 for (i = 0; i < buflen; ++i) {
397 if (isspace(buf[i])) {
402 if (!isspace(buf[0]) && (headerlen > 0)) {
403 if (!strncasecmp(header, "Content-type:", 13)) {
404 memcpy (m->b[content_type].Key, &header[13], headerlen - 12);
405 m->b[content_type].Key[headerlen - 12] = '\0';
406 m->b[content_type].len = striplt (m->b[content_type].Key);
408 m->b[content_type_name].len = extract_key(m->b[content_type_name].Key, CKEY(m->b[content_type]), HKEY("name"), '=');
409 m->b[charset].len = extract_key(m->b[charset].Key, CKEY(m->b[content_type]), HKEY("charset"), '=');
410 m->b[boundary].len = extract_key(m->b[boundary].Key, header, headerlen, HKEY("boundary"), '=');
412 /* Deal with weird headers */
413 pch = strchr(m->b[content_type].Key, ' ');
416 m->b[content_type].len = m->b[content_type].Key - pch;
418 pch = strchr(m->b[content_type].Key, ';');
421 m->b[content_type].len = m->b[content_type].Key - pch;
424 else if (!strncasecmp(header, "Content-Disposition:", 20)) {
425 memcpy (m->b[disposition].Key, &header[20], headerlen - 19);
426 m->b[disposition].Key[headerlen - 19] = '\0';
427 m->b[disposition].len = striplt(m->b[disposition].Key);
429 m->b[content_disposition_name].len = extract_key(m->b[content_disposition_name].Key, CKEY(m->b[disposition]), HKEY("name"), '=');
430 m->b[filename].len = extract_key(m->b[filename].Key, CKEY(m->b[disposition]), HKEY("filename"), '=');
431 pch = strchr(m->b[disposition].Key, ';');
432 if (pch != NULL) *pch = '\0';
433 m->b[disposition].len = striplt(m->b[disposition].Key);
435 else if (!strncasecmp(header, "Content-ID:", 11)) {
436 memcpy(m->b[id].Key, &header[11], headerlen - 11);
437 m->b[id].Key[headerlen - 11] = '\0';
438 striplt(m->b[id].Key);
439 m->b[id].len = stripallbut(m->b[id].Key, '<', '>');
441 else if (!strncasecmp(header, "Content-length: ", 15)) {
444 while (isspace(*clbuf))
446 m->content_length = (size_t) atol(clbuf);
448 else if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
449 memcpy(m->b[encoding].Key, &header[26], headerlen - 26);
450 m->b[encoding].Key[headerlen - 26] = '\0';
451 m->b[encoding].len = striplt(m->b[encoding].Key);
456 if ((headerlen + buflen + 2) < SIZ) {
457 memcpy(&header[headerlen], buf, buflen);
459 header[headerlen] = '\0';
461 if (ptr >= content_end) {
464 } while ((!IsEmptyStr(buf)) && (*ptr != 0));
466 m->is_multipart = m->b[boundary].len != 0;
467 *pcontent_start = ptr;
473 static int IsAsciiEncoding(interesting_mime_headers *m)
476 if ((m->b[encoding].len != 0) &&
477 (strcasecmp(m->b[encoding].Key, "base64") == 0))
479 if ((m->b[encoding].len != 0) &&
480 (strcmp(m->b[encoding].Key, "quoted-printable") == 0))
486 static char *FindNextContent(char *ptr,
488 interesting_mime_headers *SubMimeHeaders,
489 interesting_mime_headers *m)
494 if (IsAsciiEncoding(SubMimeHeaders)) {
499 * ok, if we have a content length of the mime part,
500 * try skipping the content on the search for the next
501 * boundary. since we don't trust the content_length
502 * to be all accurate, and suspect it to lose one digit
503 * per line with a line length of 80 chars, we need
504 * to start searching a little before..
507 if ((SubMimeHeaders->content_length != -1) &&
508 (SubMimeHeaders->content_length > 10))
513 lines = SubMimeHeaders->content_length / 80;
514 pptr = ptr + SubMimeHeaders->content_length - lines - 10;
515 if (pptr < content_end)
519 next_boundary = strstr(ptr, m->b[startary].Key);
525 * ok, if we have a content length of the mime part,
526 * try skipping the content on the search for the next
527 * boundary. since we don't trust the content_length
528 * to be all accurate, start searching a little before..
531 if ((SubMimeHeaders->content_length != -1) &&
532 (SubMimeHeaders->content_length > 10))
535 pptr = ptr + SubMimeHeaders->content_length - 10;
536 if (pptr < content_end)
541 srch = next_boundary = NULL;
542 for (srch = memchr(ptr, '-', content_end - ptr);
543 (srch != NULL) && (srch < content_end);
544 srch = memchr(srch, '-', content_end - srch))
550 next_boundary = srch;
558 return next_boundary;
562 * Break out the components of a multipart message
563 * (This function expects to be fed HEADERS + CONTENT)
564 * Note: NULL can be supplied as content_end; in this case, the message is
565 * considered to have ended when the parser encounters a 0x00 byte.
567 static void recurseable_mime_parser(char *partnum,
568 char *content_start, char *content_end,
569 MimeParserCallBackType CallBack,
570 MimeParserCallBackType PreMultiPartCallBack,
571 MimeParserCallBackType PostMultiPartCallBack,
574 interesting_mime_headers *m)
576 interesting_mime_headers *SubMimeHeaders;
579 char *part_end = NULL;
580 char *evaluate_crlf_ptr = NULL;
582 char nested_partnum[256];
585 CBufStr *chosen_name;
588 /* If this is a multipart message, then recursively process it */
591 if (m->is_multipart) {
593 /* Tell the client about this message's multipartedness */
594 if (PreMultiPartCallBack != NULL) {
595 PreMultiPartCallBack("",
600 m->b[content_type].Key,
608 /* Figure out where the boundaries are */
609 m->b[startary].len = snprintf(m->b[startary].Key, SIZ, "--%s", m->b[boundary].Key);
610 SubMimeHeaders = InitInterestingMimes ();
612 while ((*ptr == '\r') || (*ptr == '\n')) ptr ++;
614 if (strncmp(ptr, m->b[startary].Key, m->b[startary].len) == 0)
615 ptr += m->b[startary].len;
617 while ((*ptr == '\r') || (*ptr == '\n')) ptr ++;
624 if (parse_MimeHeaders(SubMimeHeaders, &ptr, content_end) != 0)
626 if ((ptr - optr > 2) && (*(ptr - 2) == '\r')) {
632 next_boundary = FindNextContent(ptr, content_end, SubMimeHeaders, m);
633 if ((next_boundary != NULL) && (next_boundary - part_start < 3)) {
634 FlushInterestingMimes(SubMimeHeaders);
638 if ( (part_start != NULL) && (next_boundary != NULL) ) {
639 part_end = next_boundary;
640 --part_end; /* omit the trailing LF */
642 --part_end; /* omit the trailing CR */
645 if (!IsEmptyStr(partnum)) {
646 snprintf(nested_partnum,
647 sizeof nested_partnum,
652 snprintf(nested_partnum,
653 sizeof nested_partnum,
656 recurseable_mime_parser(nested_partnum,
660 PreMultiPartCallBack,
661 PostMultiPartCallBack,
667 if (next_boundary != NULL) {
668 /* If we pass out of scope, don't attempt to
669 * read past the end boundary. */
670 if ((*(next_boundary + m->b[startary].len) == '-') &&
671 (*(next_boundary + m->b[startary].len + 1) == '-') ){
675 /* Set up for the next part. */
676 part_start = strstr(next_boundary, "\n");
678 /* Determine whether newlines are LF or CRLF */
679 evaluate_crlf_ptr = part_start;
681 if ((*evaluate_crlf_ptr == '\r') && (*(evaluate_crlf_ptr + 1) == '\n')) {
688 /* Advance past the LF ... now we're in the next part */
694 /* Invalid end of multipart. Bail out! */
697 FlushInterestingMimes(SubMimeHeaders);
698 } while ( (ptr < content_end) && (next_boundary != NULL) );
700 free(SubMimeHeaders);
702 if (PostMultiPartCallBack != NULL) {
703 PostMultiPartCallBack("",
708 m->b[content_type].Key,
715 } /* If it's not a multipart message, then do something with it */
719 length = content_end - part_start;
720 ptr = part_end = content_end;
722 /* The following code will truncate the MIME part to the size
723 * specified by the Content-length: header. We have commented it
724 * out because these headers have a tendency to be wrong.
726 * if ( (content_length > 0) && (length > content_length) ) {
727 * length = content_length;
731 /* Sometimes the "name" field is tacked on to Content-type,
732 * and sometimes it's tacked on to Content-disposition. Use
733 * whichever one we have.
735 if (m->b[content_disposition_name].len > m->b[content_type_name].len) {
736 chosen_name = &m->b[content_disposition_name];
739 chosen_name = &m->b[content_type_name];
742 // Ok, we've got a non-multipart part here, so do something with it.
746 m->b[content_type].Key,
749 m->b[disposition].Key,
760 * Now if it's an encapsulated message/rfc822 then we have to recurse into it
762 if (!strcasecmp(&m->b[content_type].Key[0], "message/rfc822")) {
764 if (PreMultiPartCallBack != NULL) {
765 PreMultiPartCallBack("",
770 m->b[content_type].Key,
777 if (CallBack != NULL) {
778 if (strlen(partnum) > 0) {
779 snprintf(nested_partnum,
780 sizeof nested_partnum,
785 snprintf(nested_partnum,
786 sizeof nested_partnum,
789 the_mime_parser(nested_partnum,
793 PreMultiPartCallBack,
794 PostMultiPartCallBack,
799 if (PostMultiPartCallBack != NULL) {
800 PostMultiPartCallBack("",
805 m->b[content_type].Key,
821 * Break out the components of a multipart message
822 * (This function expects to be fed HEADERS + CONTENT)
823 * Note: NULL can be supplied as content_end; in this case, the message is
824 * considered to have ended when the parser encounters a 0x00 byte.
826 void the_mime_parser(char *partnum,
827 char *content_start, char *content_end,
828 MimeParserCallBackType CallBack,
829 MimeParserCallBackType PreMultiPartCallBack,
830 MimeParserCallBackType PostMultiPartCallBack,
834 interesting_mime_headers *m;
836 /* If the caller didn't supply an endpointer, generate one by measure */
837 if (content_end == NULL) {
838 content_end = &content_start[strlen(content_start)];
841 m = InitInterestingMimes();
843 if (!parse_MimeHeaders(m, &content_start, content_end))
846 recurseable_mime_parser(partnum,
847 content_start, content_end,
849 PreMultiPartCallBack,
850 PostMultiPartCallBack,
860 * Entry point for the MIME parser.
861 * (This function expects to be fed HEADERS + CONTENT)
862 * Note: NULL can be supplied as content_end; in this case, the message is
863 * considered to have ended when the parser encounters a 0x00 byte.
865 void mime_parser(char *content_start,
867 MimeParserCallBackType CallBack,
868 MimeParserCallBackType PreMultiPartCallBack,
869 MimeParserCallBackType PostMultiPartCallBack,
874 the_mime_parser("", content_start, content_end,
876 PreMultiPartCallBack,
877 PostMultiPartCallBack,
878 userdata, dont_decode);
886 typedef struct _MimeGuess {
890 const char *MimeString;
893 MimeGuess MyMimes [] = {
921 const char *GuessMimeType(const char *data, size_t dlen)
925 while (MyMimes[MimeIndex].PatternLen != 0)
927 if ((MyMimes[MimeIndex].PatternLen +
928 MyMimes[MimeIndex].PatternOffset < dlen) &&
929 strncmp(MyMimes[MimeIndex].Pattern,
930 &data[MyMimes[MimeIndex].PatternOffset],
931 MyMimes[MimeIndex].PatternLen) == 0)
933 return MyMimes[MimeIndex].MimeString;
938 * ok, our simple minded algorythm didn't find anything,
939 * let the big chegger try it, he wil default to application/octet-stream
941 return (xdg_mime_get_mime_type_for_data(data, dlen));
945 const char* GuessMimeByFilename(const char *what, size_t len)
947 /* we know some hardcoded on our own, try them... */
948 if ((len > 3) && !strncasecmp(&what[len - 4], ".gif", 4))
950 else if ((len > 2) && !strncasecmp(&what[len - 3], ".js", 3))
951 return "text/javascript";
952 else if ((len > 3) && !strncasecmp(&what[len - 4], ".txt", 4))
954 else if ((len > 3) && !strncasecmp(&what[len - 4], ".css", 4))
956 else if ((len > 3) && !strncasecmp(&what[len - 4], ".htc", 4))
957 return "text/x-component";
958 else if ((len > 3) && !strncasecmp(&what[len - 4], ".jpg", 4))
960 else if ((len > 4) && !strncasecmp(&what[len - 5], ".jpeg", 5))
962 else if ((len > 3) && !strncasecmp(&what[len - 4], ".png", 4))
964 else if ((len > 3) && !strncasecmp(&what[len - 4], ".ico", 4))
965 return "image/x-icon";
966 else if ((len > 3) && !strncasecmp(&what[len - 4], ".vcf", 4))
967 return "text/x-vcard";
968 else if ((len > 4) && !strncasecmp(&what[len - 5], ".html", 5))
970 else if ((len > 3) && !strncasecmp(&what[len - 4], ".htm", 4))
972 else if ((len > 3) && !strncasecmp(&what[len - 4], ".wml", 4))
973 return "text/vnd.wap.wml";
974 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmls", 5))
975 return "text/vnd.wap.wmlscript";
976 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmlc", 5))
977 return "application/vnd.wap.wmlc";
978 else if ((len > 5) && !strncasecmp(&what[len - 6], ".wmlsc", 6))
979 return "application/vnd.wap.wmlscriptc";
980 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wbmp", 5))
981 return "image/vnd.wap.wbmp";
983 /* and let xdgmime do the fallback. */
984 return xdg_mime_get_mime_type_from_file_name(what);
987 static HashList *IconHash = NULL;
989 typedef struct IconName IconName;
996 static void DeleteIcon(void *IconNamePtr)
998 IconName *Icon = (IconName*) IconNamePtr;
999 free(Icon->FlatName);
1000 free(Icon->FileName);
1005 static const char *PrintFlat(void *IconNamePtr)
1007 IconName *Icon = (IconName*) IconNamePtr;
1008 return Icon->FlatName;
1010 static const char *PrintFile(void *IconNamePtr)
1012 IconName *Icon = (IconName*) IconNamePtr;
1013 return Icon->FileName;
1017 #define GENSTR "x-generic"
1018 #define IGNORE_PREFIX_1 "gnome-mime"
1019 int LoadIconDir(const char *DirName)
1021 DIR *filedir = NULL;
1022 struct dirent *filedir_entry;
1027 filedir = opendir (DirName);
1028 IconHash = NewHash(1, NULL);
1029 if (filedir == NULL) {
1033 while ((filedir_entry = readdir(filedir)))
1037 #ifdef _DIRENT_HAVE_D_NAMLEN
1038 d_namelen = filedir_entry->d_namlen;
1040 d_namelen = strlen(filedir_entry->d_name);
1042 d_without_ext = d_namelen;
1043 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1045 if ((d_without_ext == 0) || (d_namelen < 3))
1048 if ((sizeof(IGNORE_PREFIX_1) < d_namelen) &&
1049 (strncmp(IGNORE_PREFIX_1,
1050 filedir_entry->d_name,
1051 sizeof(IGNORE_PREFIX_1) - 1) == 0)) {
1052 PStart = filedir_entry->d_name + sizeof(IGNORE_PREFIX_1);
1053 d_without_ext -= sizeof(IGNORE_PREFIX_1);
1056 PStart = filedir_entry->d_name;
1058 Icon = malloc(sizeof(IconName));
1060 Icon->FileName = malloc(d_namelen + 1);
1061 memcpy(Icon->FileName, filedir_entry->d_name, d_namelen + 1);
1063 Icon->FlatName = malloc(d_without_ext + 1);
1064 memcpy(Icon->FlatName, PStart, d_without_ext);
1065 Icon->FlatName[d_without_ext] = '\0';
1066 /* Try to find Minor type in image-jpeg */
1067 MinorPtr = strchr(Icon->FlatName, '-');
1068 if (MinorPtr != NULL) {
1070 MinorLen = 1 + d_without_ext - (MinorPtr - Icon->FlatName + 1);
1071 if ((MinorLen == sizeof(GENSTR)) &&
1072 (strncmp(MinorPtr + 1, GENSTR, sizeof(GENSTR)) == 0)) {
1073 /* ok, we found a generic filename. cut the generic. */
1075 d_without_ext = d_without_ext - (MinorPtr - Icon->FlatName);
1077 else { /* Map the major / minor separator to / */
1082 // PrintHash(IconHash, PrintFlat, PrintFile);
1083 // printf("%s - %s\n", Icon->FlatName, Icon->FileName);
1084 Put(IconHash, Icon->FlatName, d_without_ext, Icon, DeleteIcon);
1085 // PrintHash(IconHash, PrintFlat, PrintFile);
1091 const char *GetIconFilename(char *MimeType, size_t len)
1096 if(IconHash == NULL)
1099 GetHash(IconHash, MimeType, len, &vIcon), Icon = (IconName*) vIcon;
1100 /* didn't find the exact mimetype? try major only. */
1103 pMinor = strchr(MimeType, '/');
1104 if (pMinor != NULL) {
1106 GetHash(IconHash, MimeType, pMinor - MimeType, &vIcon),
1107 Icon = (IconName*) vIcon;
1114 /*printf("Getting: [%s] == [%s] -> [%s]\n", MimeType, Icon->FlatName, Icon->FileName);*/
1115 return Icon->FileName;
1118 void ShutDownLibCitadelMime(void)
1120 DeleteHash(&IconHash);