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"))) {
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) {
243 if (!strcasecmp(encoding, "base64")) {
244 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
246 else if (!strcasecmp(encoding, "quoted-printable")) {
247 bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
250 if (bytes_decoded > 0) if (CallBack != NULL) {
251 char encoding_buf[SIZ];
253 strcpy(encoding_buf, "binary");
256 fixed_partnum(partnum),
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.
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.
278 int mime_decode_now (char *part_start,
282 size_t *bytes_decoded)
286 /* Some encodings aren't really encodings */
287 if (!strcasecmp(encoding, "7bit"))
289 if (!strcasecmp(encoding, "8bit"))
291 if (!strcasecmp(encoding, "binary"))
294 /* If this part is not encoded, send as-is */
295 if (strlen(encoding) == 0) {
300 /* Fail if we hit an unknown encoding. */
301 if ((strcasecmp(encoding, "base64"))
302 && (strcasecmp(encoding, "quoted-printable"))) {
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.
312 *decoded = malloc(length + 32768);
313 if (decoded == NULL) {
317 if (!strcasecmp(encoding, "base64")) {
318 *bytes_decoded = CtdlDecodeBase64(*decoded, part_start, length);
321 else if (!strcasecmp(encoding, "quoted-printable")) {
322 *bytes_decoded = CtdlDecodeQuotedPrintable(*decoded, part_start, length);
328 typedef enum _eIntMimeHdrs {
336 content_disposition_name,
340 eMax /* don't move ! */
343 typedef struct _CBufStr {
348 typedef struct _interesting_mime_headers {
352 } interesting_mime_headers;
355 static void FlushInterestingMimes(interesting_mime_headers *m)
359 for (i = 0; i < eMax; i++) {
360 m->b[i].Key[0] = '\0';
363 m->content_length = -1;
365 static interesting_mime_headers *InitInterestingMimes(void)
367 interesting_mime_headers *m;
368 m = (interesting_mime_headers*) malloc( sizeof(interesting_mime_headers));
370 FlushInterestingMimes(m);
376 static long parse_MimeHeaders(interesting_mime_headers *m,
377 char** pcontent_start,
387 /* Learn interesting things from the headers */
388 ptr = *pcontent_start;
392 ptr = memreadlinelen(ptr, buf, SIZ, &buflen);
394 for (i = 0; i < buflen; ++i) {
395 if (isspace(buf[i])) {
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);
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"), '=');
410 /* Deal with weird headers */
411 pch = strchr(m->b[content_type].Key, ' ');
414 m->b[content_type].len = m->b[content_type].Key - pch;
416 pch = strchr(m->b[content_type].Key, ';');
419 m->b[content_type].len = m->b[content_type].Key - pch;
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);
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);
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, '<', '>');
439 else if (!strncasecmp(header, "Content-length: ", 15)) {
442 while (isspace(*clbuf))
444 m->content_length = (size_t) atol(clbuf);
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);
454 if ((headerlen + buflen + 2) < SIZ) {
455 memcpy(&header[headerlen], buf, buflen);
457 header[headerlen] = '\0';
459 if (ptr >= content_end) {
462 } while ((!IsEmptyStr(buf)) && (*ptr != 0));
464 m->is_multipart = m->b[boundary].len != 0;
465 *pcontent_start = ptr;
471 static int IsAsciiEncoding(interesting_mime_headers *m)
474 if ((m->b[encoding].len != 0) &&
475 (strcasecmp(m->b[encoding].Key, "base64") == 0))
477 if ((m->b[encoding].len != 0) &&
478 (strcmp(m->b[encoding].Key, "quoted-printable") == 0))
484 static char *FindNextContent(char *ptr,
486 interesting_mime_headers *SubMimeHeaders,
487 interesting_mime_headers *m)
492 if (IsAsciiEncoding(SubMimeHeaders)) {
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..
505 if ((SubMimeHeaders->content_length != -1) &&
506 (SubMimeHeaders->content_length > 10))
511 lines = SubMimeHeaders->content_length / 80;
512 pptr = ptr + SubMimeHeaders->content_length - lines - 10;
513 if (pptr < content_end)
517 next_boundary = strstr(ptr, m->b[startary].Key);
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..
529 if ((SubMimeHeaders->content_length != -1) &&
530 (SubMimeHeaders->content_length > 10))
533 pptr = ptr + SubMimeHeaders->content_length - 10;
534 if (pptr < content_end)
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))
548 next_boundary = srch;
556 return next_boundary;
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.
565 static void recurseable_mime_parser(char *partnum,
566 char *content_start, char *content_end,
567 MimeParserCallBackType CallBack,
568 MimeParserCallBackType PreMultiPartCallBack,
569 MimeParserCallBackType PostMultiPartCallBack,
572 interesting_mime_headers *m)
574 interesting_mime_headers *SubMimeHeaders;
577 char *part_end = NULL;
578 char *evaluate_crlf_ptr = NULL;
580 char nested_partnum[256];
583 CBufStr *chosen_name;
586 /* If this is a multipart message, then recursively process it */
589 if (m->is_multipart) {
591 /* Tell the client about this message's multipartedness */
592 if (PreMultiPartCallBack != NULL) {
593 PreMultiPartCallBack("",
598 m->b[content_type].Key,
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 ();
610 while ((*ptr == '\r') || (*ptr == '\n')) ptr ++;
612 if (strncmp(ptr, m->b[startary].Key, m->b[startary].len) == 0)
613 ptr += m->b[startary].len;
615 while ((*ptr == '\r') || (*ptr == '\n')) ptr ++;
622 if (parse_MimeHeaders(SubMimeHeaders, &ptr, content_end) != 0)
624 if ((ptr - optr > 2) && (*(ptr - 2) == '\r')) {
630 next_boundary = FindNextContent(ptr, content_end, SubMimeHeaders, m);
631 if ((next_boundary != NULL) && (next_boundary - part_start < 3)) {
632 FlushInterestingMimes(SubMimeHeaders);
636 if ( (part_start != NULL) && (next_boundary != NULL) ) {
637 part_end = next_boundary;
638 --part_end; /* omit the trailing LF */
640 --part_end; /* omit the trailing CR */
643 if (!IsEmptyStr(partnum)) {
644 snprintf(nested_partnum,
645 sizeof nested_partnum,
650 snprintf(nested_partnum,
651 sizeof nested_partnum,
654 recurseable_mime_parser(nested_partnum,
658 PreMultiPartCallBack,
659 PostMultiPartCallBack,
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) == '-') ){
673 /* Set up for the next part. */
674 part_start = strstr(next_boundary, "\n");
676 /* Determine whether newlines are LF or CRLF */
677 evaluate_crlf_ptr = part_start;
679 if ((*evaluate_crlf_ptr == '\r') && (*(evaluate_crlf_ptr + 1) == '\n')) {
686 /* Advance past the LF ... now we're in the next part */
692 /* Invalid end of multipart. Bail out! */
695 FlushInterestingMimes(SubMimeHeaders);
696 } while ( (ptr < content_end) && (next_boundary != NULL) );
698 free(SubMimeHeaders);
700 if (PostMultiPartCallBack != NULL) {
701 PostMultiPartCallBack("",
706 m->b[content_type].Key,
713 } /* If it's not a multipart message, then do something with it */
717 length = content_end - part_start;
718 ptr = part_end = content_end;
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.
724 * if ( (content_length > 0) && (length > content_length) ) {
725 * length = content_length;
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.
733 if (m->b[content_disposition_name].len > m->b[content_type_name].len) {
734 chosen_name = &m->b[content_disposition_name];
737 chosen_name = &m->b[content_type_name];
740 // Ok, we've got a non-multipart part here, so do something with it.
744 m->b[content_type].Key,
747 m->b[disposition].Key,
758 * Now if it's an encapsulated message/rfc822 then we have to recurse into it
760 if (!strcasecmp(&m->b[content_type].Key[0], "message/rfc822")) {
762 if (PreMultiPartCallBack != NULL) {
763 PreMultiPartCallBack("",
768 m->b[content_type].Key,
775 if (CallBack != NULL) {
776 if (strlen(partnum) > 0) {
777 snprintf(nested_partnum,
778 sizeof nested_partnum,
783 snprintf(nested_partnum,
784 sizeof nested_partnum,
787 the_mime_parser(nested_partnum,
791 PreMultiPartCallBack,
792 PostMultiPartCallBack,
797 if (PostMultiPartCallBack != NULL) {
798 PostMultiPartCallBack("",
803 m->b[content_type].Key,
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.
824 void the_mime_parser(char *partnum,
825 char *content_start, char *content_end,
826 MimeParserCallBackType CallBack,
827 MimeParserCallBackType PreMultiPartCallBack,
828 MimeParserCallBackType PostMultiPartCallBack,
832 interesting_mime_headers *m;
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)];
839 m = InitInterestingMimes();
841 if (!parse_MimeHeaders(m, &content_start, content_end))
844 recurseable_mime_parser(partnum,
845 content_start, content_end,
847 PreMultiPartCallBack,
848 PostMultiPartCallBack,
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.
863 void mime_parser(char *content_start,
865 MimeParserCallBackType CallBack,
866 MimeParserCallBackType PreMultiPartCallBack,
867 MimeParserCallBackType PostMultiPartCallBack,
872 the_mime_parser("", content_start, content_end,
874 PreMultiPartCallBack,
875 PostMultiPartCallBack,
876 userdata, dont_decode);
884 typedef struct _MimeGuess {
888 const char *MimeString;
891 MimeGuess MyMimes [] = {
919 const char *GuessMimeType(const char *data, size_t dlen)
923 while (MyMimes[MimeIndex].PatternLen != 0)
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)
931 return MyMimes[MimeIndex].MimeString;
936 * ok, our simple minded algorythm didn't find anything,
937 * let the big chegger try it, he wil default to application/octet-stream
939 return (xdg_mime_get_mime_type_for_data(data, dlen));
943 const char* GuessMimeByFilename(const char *what, size_t len)
945 /* we know some hardcoded on our own, try them... */
946 if ((len > 3) && !strncasecmp(&what[len - 4], ".gif", 4))
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))
952 else if ((len > 3) && !strncasecmp(&what[len - 4], ".css", 4))
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))
958 else if ((len > 4) && !strncasecmp(&what[len - 5], ".jpeg", 5))
960 else if ((len > 3) && !strncasecmp(&what[len - 4], ".png", 4))
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))
968 else if ((len > 3) && !strncasecmp(&what[len - 4], ".htm", 4))
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";
981 /* and let xdgmime do the fallback. */
982 return xdg_mime_get_mime_type_from_file_name(what);
985 static HashList *IconHash = NULL;
987 typedef struct IconName IconName;
994 static void DeleteIcon(void *IconNamePtr)
996 IconName *Icon = (IconName*) IconNamePtr;
997 free(Icon->FlatName);
998 free(Icon->FileName);
1003 static const char *PrintFlat(void *IconNamePtr)
1005 IconName *Icon = (IconName*) IconNamePtr;
1006 return Icon->FlatName;
1008 static const char *PrintFile(void *IconNamePtr)
1010 IconName *Icon = (IconName*) IconNamePtr;
1011 return Icon->FileName;
1015 #define GENSTR "x-generic"
1016 #define IGNORE_PREFIX_1 "gnome-mime"
1017 int LoadIconDir(const char *DirName)
1019 DIR *filedir = NULL;
1020 struct dirent *filedir_entry;
1025 filedir = opendir (DirName);
1026 IconHash = NewHash(1, NULL);
1027 if (filedir == NULL) {
1031 while ((filedir_entry = readdir(filedir)))
1035 #ifdef _DIRENT_HAVE_D_NAMLEN
1036 d_namelen = filedir_entry->d_namlen;
1038 d_namelen = strlen(filedir_entry->d_name);
1040 d_without_ext = d_namelen;
1041 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1043 if ((d_without_ext == 0) || (d_namelen < 3))
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);
1054 PStart = filedir_entry->d_name;
1056 Icon = malloc(sizeof(IconName));
1058 Icon->FileName = malloc(d_namelen + 1);
1059 memcpy(Icon->FileName, filedir_entry->d_name, d_namelen + 1);
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) {
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. */
1073 d_without_ext = d_without_ext - (MinorPtr - Icon->FlatName);
1075 else { /* Map the major / minor separator to / */
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);
1089 const char *GetIconFilename(char *MimeType, size_t len)
1094 if(IconHash == NULL)
1097 GetHash(IconHash, MimeType, len, &vIcon), Icon = (IconName*) vIcon;
1098 /* didn't find the exact mimetype? try major only. */
1101 pMinor = strchr(MimeType, '/');
1102 if (pMinor != NULL) {
1104 GetHash(IconHash, MimeType, pMinor - MimeType, &vIcon),
1105 Icon = (IconName*) vIcon;
1112 /*printf("Getting: [%s] == [%s] -> [%s]\n", MimeType, Icon->FlatName, Icon->FileName);*/
1113 return Icon->FileName;
1116 void ShutDownLibCitadelMime(void)
1118 DeleteHash(&IconHash);