2 * This is the MIME parser for Citadel.
4 * Copyright (c) 1998-2010 by the citadel.org development team.
5 * This code is distributed under the GNU General Public License v3.
13 #include <sys/types.h>
17 #include <sys/types.h>
21 #include "xdgmime/xdgmime.h"
22 #include "libcitadel.h"
23 #include "libcitadellocal.h"
25 const unsigned char FromHexTable [256] = {
26 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0
27 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 10
28 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 20
29 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 30
30 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x01, // 40
31 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0xFF, // 50
32 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, // 60
33 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 70
34 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 80
35 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0A, 0x0B, 0x0C, // 90
36 0x0D, 0x0E, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //100
37 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //110
38 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //120
39 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //130
40 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //140
41 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //150
42 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //160
43 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //170
44 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //180
45 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //190
46 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //200
47 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //210
48 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //220
49 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //230
50 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //240
51 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF //250
55 long extract_key(char *target, char *source, long sourcelen, char *key, long keylen, char KeyEnd)
57 char *sptr, *ptr = NULL;
58 int double_quotes = 0;
59 long RealKeyLen = keylen;
65 ptr = bmstrcasestr_len(sptr, sourcelen - (sptr - source),
69 while (isspace(*(ptr + RealKeyLen)))
71 if (*(ptr + RealKeyLen) == KeyEnd)
78 sptr = ptr + RealKeyLen + 1;
88 strcpy(target, (ptr + RealKeyLen));
90 for (ptr=target; (*ptr != 0); ptr++) {
92 /* A semicolon means we've hit the end of the key, unless we're inside double quotes */
93 if ( (double_quotes != 1) && (*ptr == ';')) {
97 /* if we find double quotes, we've got a great set of string boundaries */
100 if (double_quotes == 1) {
114 * For non-multipart messages, we need to generate a quickie partnum of "1"
115 * to return to callback functions. Some callbacks demand it.
117 char *fixed_partnum(char *supplied_partnum) {
118 if (supplied_partnum == NULL) return "1";
119 if (strlen(supplied_partnum)==0) return "1";
120 return supplied_partnum;
124 static inline unsigned int _decode_hex(const char *Source)
127 unsigned char LO_NIBBLE;
128 unsigned char HI_NIBBLE;
130 HI_NIBBLE = FromHexTable[(unsigned char) *Source];
131 LO_NIBBLE = FromHexTable[(unsigned char) *(Source+1)];
133 if ((LO_NIBBLE == 0xFF) || (LO_NIBBLE == 0xFF))
137 ret = ret | LO_NIBBLE;
141 unsigned int decode_hex(char *Source) {return _decode_hex(Source);}
144 * Convert "quoted-printable" to binary. Returns number of bytes decoded.
145 * according to RFC2045 section 6.7
147 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
149 int decoded_length = 0;
152 while (pos < sourcelen)
154 if (*(encoded + pos) == '=')
157 if (*(encoded + pos) == '\n')
161 else if (*(encoded + pos) == '\r')
164 if (*(encoded + pos) == '\n')
170 ch = _decode_hex(&encoded[pos]);
172 decoded[decoded_length++] = ch;
177 decoded[decoded_length++] = encoded[pos];
181 decoded[decoded_length] = 0;
182 return(decoded_length);
187 * Given a message or message-part body and a length, handle any necessary
188 * decoding and pass the request up the stack.
190 void mime_decode(char *partnum,
191 char *part_start, size_t length,
192 char *content_type, char *charset, char *encoding,
195 char *name, char *filename,
196 MimeParserCallBackType CallBack,
197 MimeParserCallBackType PreMultiPartCallBack,
198 MimeParserCallBackType PostMultiPartCallBack,
204 size_t bytes_decoded = 0;
206 /* Some encodings aren't really encodings */
207 if (!strcasecmp(encoding, "7bit"))
208 strcpy(encoding, "");
209 if (!strcasecmp(encoding, "8bit"))
210 strcpy(encoding, "");
211 if (!strcasecmp(encoding, "binary"))
212 strcpy(encoding, "");
213 if (!strcasecmp(encoding, "ISO-8859-1"))
214 strcpy(encoding, "");
216 /* If this part is not encoded, send as-is */
217 if ( (strlen(encoding) == 0) || (dont_decode)) {
218 if (CallBack != NULL) {
221 fixed_partnum(partnum),
234 /* Fail silently if we hit an unknown encoding. */
235 if ((strcasecmp(encoding, "base64"))
236 && (strcasecmp(encoding, "quoted-printable"))) {
241 * Allocate a buffer for the decoded data. The output buffer is slightly
242 * larger than the input buffer; this assumes that the decoded data
243 * will never be significantly larger than the encoded data. This is a
244 * safe assumption with base64, uuencode, and quoted-printable.
246 decoded = malloc(length + 32768);
247 if (decoded == NULL) {
251 if (!strcasecmp(encoding, "base64")) {
252 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
254 else if (!strcasecmp(encoding, "quoted-printable")) {
255 bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
258 if (bytes_decoded > 0) if (CallBack != NULL) {
259 char encoding_buf[SIZ];
261 strcpy(encoding_buf, "binary");
264 fixed_partnum(partnum),
279 * this is the extract of mime_decode which can be called if 'dont_decode' was set;
280 * to save the cpu intense process of decoding to the time when it realy wants the content.
282 * - > 0 we decoded something, its on *decoded, you need to free it.
283 * - = 0 no need to decode stuff. *decoded will be NULL.
284 * - < 0 an error occured, either an unknown encoding, or alloc failed. no need to free.
286 int mime_decode_now (char *part_start,
290 size_t *bytes_decoded)
294 /* Some encodings aren't really encodings */
295 if (!strcasecmp(encoding, "7bit"))
296 strcpy(encoding, "");
297 if (!strcasecmp(encoding, "8bit"))
298 strcpy(encoding, "");
299 if (!strcasecmp(encoding, "binary"))
300 strcpy(encoding, "");
302 /* If this part is not encoded, send as-is */
303 if (strlen(encoding) == 0) {
308 /* Fail if we hit an unknown encoding. */
309 if ((strcasecmp(encoding, "base64"))
310 && (strcasecmp(encoding, "quoted-printable"))) {
315 * Allocate a buffer for the decoded data. The output buffer is slightly
316 * larger than the input buffer; this assumes that the decoded data
317 * will never be significantly larger than the encoded data. This is a
318 * safe assumption with base64, uuencode, and quoted-printable.
320 *decoded = malloc(length + 32768);
321 if (decoded == NULL) {
325 if (!strcasecmp(encoding, "base64")) {
326 *bytes_decoded = CtdlDecodeBase64(*decoded, part_start, length);
329 else if (!strcasecmp(encoding, "quoted-printable")) {
330 *bytes_decoded = CtdlDecodeQuotedPrintable(*decoded, part_start, length);
336 typedef enum _eIntMimeHdrs {
344 content_disposition_name,
348 eMax /* don't move ! */
351 typedef struct _CBufStr {
356 typedef struct _interesting_mime_headers {
360 } interesting_mime_headers;
363 static void FlushInterestingMimes(interesting_mime_headers *m)
367 for (i = 0; i < eMax; i++) {
368 m->b[i].Key[0] = '\0';
371 m->content_length = -1;
373 static interesting_mime_headers *InitInterestingMimes(void)
375 interesting_mime_headers *m;
376 m = (interesting_mime_headers*) malloc( sizeof(interesting_mime_headers));
378 FlushInterestingMimes(m);
384 static long parse_MimeHeaders(interesting_mime_headers *m,
385 char** pcontent_start,
395 /* Learn interesting things from the headers */
396 ptr = *pcontent_start;
400 ptr = memreadlinelen(ptr, buf, SIZ, &buflen);
402 for (i = 0; i < buflen; ++i) {
403 if (isspace(buf[i])) {
408 if (!isspace(buf[0]) && (headerlen > 0)) {
409 if (!strncasecmp(header, "Content-type:", 13)) {
410 memcpy (m->b[content_type].Key, &header[13], headerlen - 12);
411 m->b[content_type].Key[headerlen - 12] = '\0';
412 m->b[content_type].len = striplt (m->b[content_type].Key);
414 m->b[content_type_name].len = extract_key(m->b[content_type_name].Key, CKEY(m->b[content_type]), HKEY("name"), '=');
415 m->b[charset].len = extract_key(m->b[charset].Key, CKEY(m->b[content_type]), HKEY("charset"), '=');
416 m->b[boundary].len = extract_key(m->b[boundary].Key, header, headerlen, HKEY("boundary"), '=');
418 /* Deal with weird headers */
419 pch = strchr(m->b[content_type].Key, ' ');
422 m->b[content_type].len = m->b[content_type].Key - pch;
424 pch = strchr(m->b[content_type].Key, ';');
427 m->b[content_type].len = m->b[content_type].Key - pch;
430 else if (!strncasecmp(header, "Content-Disposition:", 20)) {
431 memcpy (m->b[disposition].Key, &header[20], headerlen - 19);
432 m->b[disposition].Key[headerlen - 19] = '\0';
433 m->b[disposition].len = striplt(m->b[disposition].Key);
435 m->b[content_disposition_name].len = extract_key(m->b[content_disposition_name].Key, CKEY(m->b[disposition]), HKEY("name"), '=');
436 m->b[filename].len = extract_key(m->b[filename].Key, CKEY(m->b[disposition]), HKEY("filename"), '=');
437 pch = strchr(m->b[disposition].Key, ';');
438 if (pch != NULL) *pch = '\0';
439 m->b[disposition].len = striplt(m->b[disposition].Key);
441 else if (!strncasecmp(header, "Content-ID:", 11)) {
442 memcpy(m->b[id].Key, &header[11], headerlen - 11);
443 m->b[id].Key[headerlen - 11] = '\0';
444 striplt(m->b[id].Key);
445 m->b[id].len = stripallbut(m->b[id].Key, '<', '>');
447 else if (!strncasecmp(header, "Content-length: ", 15)) {
450 while (isspace(*clbuf))
452 m->content_length = (size_t) atol(clbuf);
454 else if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
455 memcpy(m->b[encoding].Key, &header[26], headerlen - 26);
456 m->b[encoding].Key[headerlen - 26] = '\0';
457 m->b[encoding].len = striplt(m->b[encoding].Key);
462 if ((headerlen + buflen + 2) < SIZ) {
463 memcpy(&header[headerlen], buf, buflen);
465 header[headerlen] = '\0';
467 if (ptr >= content_end) {
470 } while ((!IsEmptyStr(buf)) && (*ptr != 0));
472 m->is_multipart = m->b[boundary].len != 0;
473 *pcontent_start = ptr;
479 static int IsAsciiEncoding(interesting_mime_headers *m)
482 if ((m->b[encoding].len != 0) &&
483 (strcasecmp(m->b[encoding].Key, "base64") == 0))
485 if ((m->b[encoding].len != 0) &&
486 (strcmp(m->b[encoding].Key, "quoted-printable") == 0))
492 static char *FindNextContent(char *ptr,
494 interesting_mime_headers *SubMimeHeaders,
495 interesting_mime_headers *m)
500 if (IsAsciiEncoding(SubMimeHeaders)) {
505 * ok, if we have a content length of the mime part,
506 * try skipping the content on the search for the next
507 * boundary. since we don't trust the content_length
508 * to be all accurate, and suspect it to lose one digit
509 * per line with a line length of 80 chars, we need
510 * to start searching a little before..
513 if ((SubMimeHeaders->content_length != -1) &&
514 (SubMimeHeaders->content_length > 10))
519 lines = SubMimeHeaders->content_length / 80;
520 pptr = ptr + SubMimeHeaders->content_length - lines - 10;
521 if (pptr < content_end)
525 next_boundary = strstr(ptr, m->b[startary].Key);
531 * ok, if we have a content length of the mime part,
532 * try skipping the content on the search for the next
533 * boundary. since we don't trust the content_length
534 * to be all accurate, start searching a little before..
537 if ((SubMimeHeaders->content_length != -1) &&
538 (SubMimeHeaders->content_length > 10))
541 pptr = ptr + SubMimeHeaders->content_length - 10;
542 if (pptr < content_end)
547 srch = next_boundary = NULL;
548 for (srch = memchr(ptr, '-', content_end - ptr);
549 (srch != NULL) && (srch < content_end);
550 srch = memchr(srch, '-', content_end - srch))
556 next_boundary = srch;
564 return next_boundary;
568 * Break out the components of a multipart message
569 * (This function expects to be fed HEADERS + CONTENT)
570 * Note: NULL can be supplied as content_end; in this case, the message is
571 * considered to have ended when the parser encounters a 0x00 byte.
573 static void recurseable_mime_parser(char *partnum,
574 char *content_start, char *content_end,
575 MimeParserCallBackType CallBack,
576 MimeParserCallBackType PreMultiPartCallBack,
577 MimeParserCallBackType PostMultiPartCallBack,
580 interesting_mime_headers *m)
582 interesting_mime_headers *SubMimeHeaders;
585 char *part_end = NULL;
586 char *evaluate_crlf_ptr = NULL;
588 char nested_partnum[256];
591 CBufStr *chosen_name;
594 /* If this is a multipart message, then recursively process it */
597 if (m->is_multipart) {
599 /* Tell the client about this message's multipartedness */
600 if (PreMultiPartCallBack != NULL) {
601 PreMultiPartCallBack("",
606 m->b[content_type].Key,
614 /* Figure out where the boundaries are */
615 m->b[startary].len = snprintf(m->b[startary].Key, SIZ, "--%s", m->b[boundary].Key);
616 SubMimeHeaders = InitInterestingMimes ();
621 if (strncmp(ptr, m->b[startary].Key, m->b[startary].len) == 0)
622 ptr += m->b[startary].len;
632 if (parse_MimeHeaders(SubMimeHeaders, &ptr, content_end) != 0)
634 if ((ptr - optr > 2) &&
635 (*(ptr - 2) == '\r'))
640 next_boundary = FindNextContent(ptr,
644 if ((next_boundary != NULL) &&
645 (next_boundary - part_start < 3)) {
646 FlushInterestingMimes(SubMimeHeaders);
651 if ( (part_start != NULL) && (next_boundary != NULL) ) {
652 part_end = next_boundary;
653 --part_end; /* omit the trailing LF */
655 --part_end; /* omit the trailing CR */
658 if (!IsEmptyStr(partnum)) {
659 snprintf(nested_partnum,
660 sizeof nested_partnum,
665 snprintf(nested_partnum,
666 sizeof nested_partnum,
669 recurseable_mime_parser(nested_partnum,
673 PreMultiPartCallBack,
674 PostMultiPartCallBack,
680 if (next_boundary != NULL) {
681 /* If we pass out of scope, don't attempt to
682 * read past the end boundary. */
683 if ((*(next_boundary + m->b[startary].len) == '-') &&
684 (*(next_boundary + m->b[startary].len + 1) == '-') ){
688 /* Set up for the next part. */
689 part_start = strstr(next_boundary, "\n");
691 /* Determine whether newlines are LF or CRLF */
692 evaluate_crlf_ptr = part_start;
694 if ((*evaluate_crlf_ptr == '\r') &&
695 (*(evaluate_crlf_ptr + 1) == '\n'))
703 /* Advance past the LF ... now we're in the next part */
709 /* Invalid end of multipart. Bail out! */
712 FlushInterestingMimes(SubMimeHeaders);
713 } while ( (ptr < content_end) && (next_boundary != NULL) );
715 free(SubMimeHeaders);
717 if (PostMultiPartCallBack != NULL) {
718 PostMultiPartCallBack("",
723 m->b[content_type].Key,
730 } /* If it's not a multipart message, then do something with it */
734 length = content_end - part_start;
735 ptr = part_end = content_end;
738 /* The following code will truncate the MIME part to the size
739 * specified by the Content-length: header. We have commented it
740 * out because these headers have a tendency to be wrong.
742 * if ( (content_length > 0) && (length > content_length) ) {
743 * length = content_length;
747 /* Sometimes the "name" field is tacked on to Content-type,
748 * and sometimes it's tacked on to Content-disposition. Use
749 * whichever one we have.
751 if (m->b[content_disposition_name].len > m->b[content_type_name].len) {
752 chosen_name = &m->b[content_disposition_name];
755 chosen_name = &m->b[content_type_name];
758 /* Ok, we've got a non-multipart part here, so do something with it.
763 m->b[content_type].Key,
766 m->b[disposition].Key,
777 * Now if it's an encapsulated message/rfc822 then we have to recurse into it
779 if (!strcasecmp(&m->b[content_type].Key[0], "message/rfc822")) {
781 if (PreMultiPartCallBack != NULL) {
782 PreMultiPartCallBack("",
787 m->b[content_type].Key,
794 if (CallBack != NULL) {
795 if (strlen(partnum) > 0) {
796 snprintf(nested_partnum,
797 sizeof nested_partnum,
802 snprintf(nested_partnum,
803 sizeof nested_partnum,
806 the_mime_parser(nested_partnum,
810 PreMultiPartCallBack,
811 PostMultiPartCallBack,
816 if (PostMultiPartCallBack != NULL) {
817 PostMultiPartCallBack("",
822 m->b[content_type].Key,
838 * Break out the components of a multipart message
839 * (This function expects to be fed HEADERS + CONTENT)
840 * Note: NULL can be supplied as content_end; in this case, the message is
841 * considered to have ended when the parser encounters a 0x00 byte.
843 void the_mime_parser(char *partnum,
844 char *content_start, char *content_end,
845 MimeParserCallBackType CallBack,
846 MimeParserCallBackType PreMultiPartCallBack,
847 MimeParserCallBackType PostMultiPartCallBack,
851 interesting_mime_headers *m;
853 /* If the caller didn't supply an endpointer, generate one by measure */
854 if (content_end == NULL) {
855 content_end = &content_start[strlen(content_start)];
858 m = InitInterestingMimes();
860 if (!parse_MimeHeaders(m, &content_start, content_end))
863 recurseable_mime_parser(partnum,
864 content_start, content_end,
866 PreMultiPartCallBack,
867 PostMultiPartCallBack,
876 * Entry point for the MIME parser.
877 * (This function expects to be fed HEADERS + CONTENT)
878 * Note: NULL can be supplied as content_end; in this case, the message is
879 * considered to have ended when the parser encounters a 0x00 byte.
881 void mime_parser(char *content_start,
883 MimeParserCallBackType CallBack,
884 MimeParserCallBackType PreMultiPartCallBack,
885 MimeParserCallBackType PostMultiPartCallBack,
890 the_mime_parser("", content_start, content_end,
892 PreMultiPartCallBack,
893 PostMultiPartCallBack,
894 userdata, dont_decode);
902 typedef struct _MimeGuess {
906 const char *MimeString;
909 MimeGuess MyMimes [] = {
937 const char *GuessMimeType(const char *data, size_t dlen)
941 while (MyMimes[MimeIndex].PatternLen != 0)
943 if ((MyMimes[MimeIndex].PatternLen +
944 MyMimes[MimeIndex].PatternOffset < dlen) &&
945 strncmp(MyMimes[MimeIndex].Pattern,
946 &data[MyMimes[MimeIndex].PatternOffset],
947 MyMimes[MimeIndex].PatternLen) == 0)
949 return MyMimes[MimeIndex].MimeString;
954 * ok, our simple minded algorythm didn't find anything,
955 * let the big chegger try it, he wil default to application/octet-stream
957 return (xdg_mime_get_mime_type_for_data(data, dlen));
961 const char* GuessMimeByFilename(const char *what, size_t len)
963 /* we know some hardcoded on our own, try them... */
964 if ((len > 3) && !strncasecmp(&what[len - 4], ".gif", 4))
966 else if ((len > 2) && !strncasecmp(&what[len - 3], ".js", 3))
967 return "text/javascript";
968 else if ((len > 3) && !strncasecmp(&what[len - 4], ".txt", 4))
970 else if ((len > 3) && !strncasecmp(&what[len - 4], ".css", 4))
972 else if ((len > 3) && !strncasecmp(&what[len - 4], ".htc", 4))
973 return "text/x-component";
974 else if ((len > 3) && !strncasecmp(&what[len - 4], ".jpg", 4))
976 else if ((len > 3) && !strncasecmp(&what[len - 4], ".png", 4))
978 else if ((len > 3) && !strncasecmp(&what[len - 4], ".ico", 4))
979 return "image/x-icon";
980 else if ((len > 3) && !strncasecmp(&what[len - 4], ".vcf", 4))
981 return "text/x-vcard";
982 else if ((len > 4) && !strncasecmp(&what[len - 5], ".html", 5))
984 else if ((len > 3) && !strncasecmp(&what[len - 4], ".htm", 4))
986 else if ((len > 3) && !strncasecmp(&what[len - 4], ".wml", 4))
987 return "text/vnd.wap.wml";
988 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmls", 5))
989 return "text/vnd.wap.wmlscript";
990 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmlc", 5))
991 return "application/vnd.wap.wmlc";
992 else if ((len > 5) && !strncasecmp(&what[len - 6], ".wmlsc", 6))
993 return "application/vnd.wap.wmlscriptc";
994 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wbmp", 5))
995 return "image/vnd.wap.wbmp";
997 /* and let xdgmime do the fallback. */
998 return xdg_mime_get_mime_type_from_file_name(what);
1001 static HashList *IconHash = NULL;
1003 typedef struct IconName IconName;
1010 static void DeleteIcon(void *IconNamePtr)
1012 IconName *Icon = (IconName*) IconNamePtr;
1013 free(Icon->FlatName);
1014 free(Icon->FileName);
1019 static const char *PrintFlat(void *IconNamePtr)
1021 IconName *Icon = (IconName*) IconNamePtr;
1022 return Icon->FlatName;
1024 static const char *PrintFile(void *IconNamePtr)
1026 IconName *Icon = (IconName*) IconNamePtr;
1027 return Icon->FileName;
1031 #define GENSTR "x-generic"
1032 #define IGNORE_PREFIX_1 "gnome-mime"
1033 int LoadIconDir(const char *DirName)
1035 DIR *filedir = NULL;
1036 struct dirent *filedir_entry;
1041 filedir = opendir (DirName);
1042 IconHash = NewHash(1, NULL);
1043 if (filedir == NULL) {
1047 while ((filedir_entry = readdir(filedir)))
1051 #ifdef _DIRENT_HAVE_D_NAMELEN
1052 d_namelen = filedir_entry->d_namelen;
1054 d_namelen = strlen(filedir_entry->d_name);
1056 d_without_ext = d_namelen;
1057 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1059 if ((d_without_ext == 0) || (d_namelen < 3))
1062 if ((sizeof(IGNORE_PREFIX_1) < d_namelen) &&
1063 (strncmp(IGNORE_PREFIX_1,
1064 filedir_entry->d_name,
1065 sizeof(IGNORE_PREFIX_1) - 1) == 0)) {
1066 PStart = filedir_entry->d_name + sizeof(IGNORE_PREFIX_1);
1067 d_without_ext -= sizeof(IGNORE_PREFIX_1);
1070 PStart = filedir_entry->d_name;
1072 Icon = malloc(sizeof(IconName));
1074 Icon->FileName = malloc(d_namelen + 1);
1075 memcpy(Icon->FileName, filedir_entry->d_name, d_namelen + 1);
1077 Icon->FlatName = malloc(d_without_ext + 1);
1078 memcpy(Icon->FlatName, PStart, d_without_ext);
1079 Icon->FlatName[d_without_ext] = '\0';
1080 /* Try to find Minor type in image-jpeg */
1081 MinorPtr = strchr(Icon->FlatName, '-');
1082 if (MinorPtr != NULL) {
1084 MinorLen = 1 + d_without_ext - (MinorPtr - Icon->FlatName + 1);
1085 if ((MinorLen == sizeof(GENSTR)) &&
1086 (strncmp(MinorPtr + 1, GENSTR, sizeof(GENSTR)) == 0)) {
1087 /* ok, we found a generic filename. cut the generic. */
1089 d_without_ext = d_without_ext - (MinorPtr - Icon->FlatName);
1091 else { /* Map the major / minor separator to / */
1096 // PrintHash(IconHash, PrintFlat, PrintFile);
1097 // printf("%s - %s\n", Icon->FlatName, Icon->FileName);
1098 Put(IconHash, Icon->FlatName, d_without_ext, Icon, DeleteIcon);
1099 // PrintHash(IconHash, PrintFlat, PrintFile);
1105 const char *GetIconFilename(char *MimeType, size_t len)
1110 if(IconHash == NULL)
1113 GetHash(IconHash, MimeType, len, &vIcon), Icon = (IconName*) vIcon;
1114 /* didn't find the exact mimetype? try major only. */
1117 pMinor = strchr(MimeType, '/');
1118 if (pMinor != NULL) {
1120 GetHash(IconHash, MimeType, pMinor - MimeType, &vIcon),
1121 Icon = (IconName*) vIcon;
1128 /*printf("Getting: [%s] == [%s] -> [%s]\n", MimeType, Icon->FlatName, Icon->FileName);*/
1129 return Icon->FileName;
1132 void ShutDownLibCitadelMime(void)
1134 DeleteHash(&IconHash);