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, "");
214 /* If this part is not encoded, send as-is */
215 if ( (strlen(encoding) == 0) || (dont_decode)) {
216 if (CallBack != NULL) {
219 fixed_partnum(partnum),
232 /* Fail silently if we hit an unknown encoding. */
233 if ((strcasecmp(encoding, "base64"))
234 && (strcasecmp(encoding, "quoted-printable"))) {
239 * Allocate a buffer for the decoded data. The output buffer is slightly
240 * larger than the input buffer; this assumes that the decoded data
241 * will never be significantly larger than the encoded data. This is a
242 * safe assumption with base64, uuencode, and quoted-printable.
244 decoded = malloc(length + 32768);
245 if (decoded == NULL) {
249 if (!strcasecmp(encoding, "base64")) {
250 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
252 else if (!strcasecmp(encoding, "quoted-printable")) {
253 bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
256 if (bytes_decoded > 0) if (CallBack != NULL) {
257 char encoding_buf[SIZ];
259 strcpy(encoding_buf, "binary");
262 fixed_partnum(partnum),
277 * this is the extract of mime_decode which can be called if 'dont_decode' was set;
278 * to save the cpu intense process of decoding to the time when it realy wants the content.
280 * - > 0 we decoded something, its on *decoded, you need to free it.
281 * - = 0 no need to decode stuff. *decoded will be NULL.
282 * - < 0 an error occured, either an unknown encoding, or alloc failed. no need to free.
284 int mime_decode_now (char *part_start,
288 size_t *bytes_decoded)
292 /* Some encodings aren't really encodings */
293 if (!strcasecmp(encoding, "7bit"))
294 strcpy(encoding, "");
295 if (!strcasecmp(encoding, "8bit"))
296 strcpy(encoding, "");
297 if (!strcasecmp(encoding, "binary"))
298 strcpy(encoding, "");
300 /* If this part is not encoded, send as-is */
301 if (strlen(encoding) == 0) {
306 /* Fail if we hit an unknown encoding. */
307 if ((strcasecmp(encoding, "base64"))
308 && (strcasecmp(encoding, "quoted-printable"))) {
313 * Allocate a buffer for the decoded data. The output buffer is slightly
314 * larger than the input buffer; this assumes that the decoded data
315 * will never be significantly larger than the encoded data. This is a
316 * safe assumption with base64, uuencode, and quoted-printable.
318 *decoded = malloc(length + 32768);
319 if (decoded == NULL) {
323 if (!strcasecmp(encoding, "base64")) {
324 *bytes_decoded = CtdlDecodeBase64(*decoded, part_start, length);
327 else if (!strcasecmp(encoding, "quoted-printable")) {
328 *bytes_decoded = CtdlDecodeQuotedPrintable(*decoded, part_start, length);
334 typedef enum _eIntMimeHdrs {
342 content_disposition_name,
346 eMax /* don't move ! */
349 typedef struct _CBufStr {
354 typedef struct _interesting_mime_headers {
358 } interesting_mime_headers;
361 static void FlushInterestingMimes(interesting_mime_headers *m)
365 for (i = 0; i < eMax; i++) {
366 m->b[i].Key[0] = '\0';
369 m->content_length = -1;
371 static interesting_mime_headers *InitInterestingMimes(void)
373 interesting_mime_headers *m;
374 m = (interesting_mime_headers*) malloc( sizeof(interesting_mime_headers));
376 FlushInterestingMimes(m);
382 static long parse_MimeHeaders(interesting_mime_headers *m,
383 char** pcontent_start,
393 /* Learn interesting things from the headers */
394 ptr = *pcontent_start;
398 ptr = memreadlinelen(ptr, buf, SIZ, &buflen);
400 for (i = 0; i < buflen; ++i) {
401 if (isspace(buf[i])) {
406 if (!isspace(buf[0]) && (headerlen > 0)) {
407 if (!strncasecmp(header, "Content-type:", 13)) {
408 memcpy (m->b[content_type].Key, &header[13], headerlen - 12);
409 m->b[content_type].Key[headerlen - 12] = '\0';
410 m->b[content_type].len = striplt (m->b[content_type].Key);
412 m->b[content_type_name].len = extract_key(m->b[content_type_name].Key, CKEY(m->b[content_type]), HKEY("name"), '=');
413 m->b[charset].len = extract_key(m->b[charset].Key, CKEY(m->b[content_type]), HKEY("charset"), '=');
414 m->b[boundary].len = extract_key(m->b[boundary].Key, header, headerlen, HKEY("boundary"), '=');
416 /* Deal with weird headers */
417 pch = strchr(m->b[content_type].Key, ' ');
420 m->b[content_type].len = m->b[content_type].Key - pch;
422 pch = strchr(m->b[content_type].Key, ';');
425 m->b[content_type].len = m->b[content_type].Key - pch;
428 else if (!strncasecmp(header, "Content-Disposition:", 20)) {
429 memcpy (m->b[disposition].Key, &header[20], headerlen - 19);
430 m->b[disposition].Key[headerlen - 19] = '\0';
431 m->b[disposition].len = striplt(m->b[disposition].Key);
433 m->b[content_disposition_name].len = extract_key(m->b[content_disposition_name].Key, CKEY(m->b[disposition]), HKEY("name"), '=');
434 m->b[filename].len = extract_key(m->b[filename].Key, CKEY(m->b[disposition]), HKEY("filename"), '=');
435 pch = strchr(m->b[disposition].Key, ';');
436 if (pch != NULL) *pch = '\0';
437 m->b[disposition].len = striplt(m->b[disposition].Key);
439 else if (!strncasecmp(header, "Content-ID:", 11)) {
440 memcpy(m->b[id].Key, &header[11], headerlen - 11);
441 m->b[id].Key[headerlen - 11] = '\0';
442 striplt(m->b[id].Key);
443 m->b[id].len = stripallbut(m->b[id].Key, '<', '>');
445 else if (!strncasecmp(header, "Content-length: ", 15)) {
448 while (isspace(*clbuf))
450 m->content_length = (size_t) atol(clbuf);
452 else if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
453 memcpy(m->b[encoding].Key, &header[26], headerlen - 26);
454 m->b[encoding].Key[headerlen - 26] = '\0';
455 m->b[encoding].len = striplt(m->b[encoding].Key);
460 if ((headerlen + buflen + 2) < SIZ) {
461 memcpy(&header[headerlen], buf, buflen);
463 header[headerlen] = '\0';
465 if (ptr >= content_end) {
468 } while ((!IsEmptyStr(buf)) && (*ptr != 0));
470 m->is_multipart = m->b[boundary].len != 0;
471 *pcontent_start = ptr;
477 static int IsAsciiEncoding(interesting_mime_headers *m)
480 if ((m->b[encoding].len != 0) &&
481 (strcasecmp(m->b[encoding].Key, "base64") == 0))
483 if ((m->b[encoding].len != 0) &&
484 (strcmp(m->b[encoding].Key, "quoted-printable") == 0))
490 static char *FindNextContent(char *ptr,
492 interesting_mime_headers *SubMimeHeaders,
493 interesting_mime_headers *m)
498 if (IsAsciiEncoding(SubMimeHeaders)) {
503 * ok, if we have a content length of the mime part,
504 * try skipping the content on the search for the next
505 * boundary. since we don't trust the content_length
506 * to be all accurate, and suspect it to lose one digit
507 * per line with a line length of 80 chars, we need
508 * to start searching a little before..
511 if ((SubMimeHeaders->content_length != -1) &&
512 (SubMimeHeaders->content_length > 10))
517 lines = SubMimeHeaders->content_length / 80;
518 pptr = ptr + SubMimeHeaders->content_length - lines - 10;
519 if (pptr < content_end)
523 next_boundary = strstr(ptr, m->b[startary].Key);
529 * ok, if we have a content length of the mime part,
530 * try skipping the content on the search for the next
531 * boundary. since we don't trust the content_length
532 * to be all accurate, start searching a little before..
535 if ((SubMimeHeaders->content_length != -1) &&
536 (SubMimeHeaders->content_length > 10))
539 pptr = ptr + SubMimeHeaders->content_length - 10;
540 if (pptr < content_end)
545 srch = next_boundary = NULL;
546 for (srch = memchr(ptr, '-', content_end - srch);
547 (srch != NULL) && (srch < content_end);
548 srch = memchr(srch, '-', content_end - srch))
554 next_boundary = srch;
562 return next_boundary;
566 * Break out the components of a multipart message
567 * (This function expects to be fed HEADERS + CONTENT)
568 * Note: NULL can be supplied as content_end; in this case, the message is
569 * considered to have ended when the parser encounters a 0x00 byte.
571 static void recurseable_mime_parser(char *partnum,
572 char *content_start, char *content_end,
573 MimeParserCallBackType CallBack,
574 MimeParserCallBackType PreMultiPartCallBack,
575 MimeParserCallBackType PostMultiPartCallBack,
578 interesting_mime_headers *m)
580 interesting_mime_headers *SubMimeHeaders;
583 char *part_end = NULL;
584 char *evaluate_crlf_ptr = NULL;
586 char nested_partnum[256];
589 CBufStr *chosen_name;
592 /* If this is a multipart message, then recursively process it */
595 if (m->is_multipart) {
597 /* Tell the client about this message's multipartedness */
598 if (PreMultiPartCallBack != NULL) {
599 PreMultiPartCallBack("",
604 m->b[content_type].Key,
612 /* Figure out where the boundaries are */
613 m->b[startary].len = snprintf(m->b[startary].Key, SIZ, "--%s", m->b[boundary].Key);
614 SubMimeHeaders = InitInterestingMimes ();
619 if (strncmp(ptr, m->b[startary].Key, m->b[startary].len) == 0)
620 ptr += m->b[startary].len;
630 if (parse_MimeHeaders(SubMimeHeaders, &ptr, content_end) != 0)
632 if ((ptr - optr > 2) &&
633 (*(ptr - 2) == '\r'))
638 next_boundary = FindNextContent(ptr,
642 if ((next_boundary != NULL) &&
643 (next_boundary - part_start < 3)) {
644 FlushInterestingMimes(SubMimeHeaders);
649 if ( (part_start != NULL) && (next_boundary != NULL) ) {
650 part_end = next_boundary;
651 --part_end; /* omit the trailing LF */
653 --part_end; /* omit the trailing CR */
656 if (!IsEmptyStr(partnum)) {
657 snprintf(nested_partnum,
658 sizeof nested_partnum,
663 snprintf(nested_partnum,
664 sizeof nested_partnum,
667 recurseable_mime_parser(nested_partnum,
671 PreMultiPartCallBack,
672 PostMultiPartCallBack,
678 if (next_boundary != NULL) {
679 /* If we pass out of scope, don't attempt to
680 * read past the end boundary. */
681 if ((*(next_boundary + m->b[startary].len + 1) == '-') &&
682 (*(next_boundary + m->b[startary].len + 2) == '-') ){
686 /* Set up for the next part. */
687 part_start = strstr(next_boundary, "\n");
689 /* Determine whether newlines are LF or CRLF */
690 evaluate_crlf_ptr = part_start;
692 if ((*evaluate_crlf_ptr == '\r') &&
693 (*(evaluate_crlf_ptr + 1) == '\n'))
701 /* Advance past the LF ... now we're in the next part */
707 /* Invalid end of multipart. Bail out! */
710 FlushInterestingMimes(SubMimeHeaders);
711 } while ( (ptr < content_end) && (next_boundary != NULL) );
713 free(SubMimeHeaders);
715 if (PostMultiPartCallBack != NULL) {
716 PostMultiPartCallBack("",
721 m->b[content_type].Key,
728 } /* If it's not a multipart message, then do something with it */
732 length = content_end - part_start;
733 ptr = part_end = content_end;
736 /* The following code will truncate the MIME part to the size
737 * specified by the Content-length: header. We have commented it
738 * out because these headers have a tendency to be wrong.
740 * if ( (content_length > 0) && (length > content_length) ) {
741 * length = content_length;
745 /* Sometimes the "name" field is tacked on to Content-type,
746 * and sometimes it's tacked on to Content-disposition. Use
747 * whichever one we have.
749 if (m->b[content_disposition_name].len > m->b[content_type_name].len) {
750 chosen_name = &m->b[content_disposition_name];
753 chosen_name = &m->b[content_type_name];
756 /* Ok, we've got a non-multipart part here, so do something with it.
761 m->b[content_type].Key,
764 m->b[disposition].Key,
775 * Now if it's an encapsulated message/rfc822 then we have to recurse into it
777 if (!strcasecmp(&m->b[content_type].Key[0], "message/rfc822")) {
779 if (PreMultiPartCallBack != NULL) {
780 PreMultiPartCallBack("",
785 m->b[content_type].Key,
792 if (CallBack != NULL) {
793 if (strlen(partnum) > 0) {
794 snprintf(nested_partnum,
795 sizeof nested_partnum,
800 snprintf(nested_partnum,
801 sizeof nested_partnum,
804 the_mime_parser(nested_partnum,
808 PreMultiPartCallBack,
809 PostMultiPartCallBack,
814 if (PostMultiPartCallBack != NULL) {
815 PostMultiPartCallBack("",
820 m->b[content_type].Key,
836 * Break out the components of a multipart message
837 * (This function expects to be fed HEADERS + CONTENT)
838 * Note: NULL can be supplied as content_end; in this case, the message is
839 * considered to have ended when the parser encounters a 0x00 byte.
841 void the_mime_parser(char *partnum,
842 char *content_start, char *content_end,
843 MimeParserCallBackType CallBack,
844 MimeParserCallBackType PreMultiPartCallBack,
845 MimeParserCallBackType PostMultiPartCallBack,
849 interesting_mime_headers *m;
851 /* If the caller didn't supply an endpointer, generate one by measure */
852 if (content_end == NULL) {
853 content_end = &content_start[strlen(content_start)];
856 m = InitInterestingMimes();
858 if (!parse_MimeHeaders(m, &content_start, content_end))
861 recurseable_mime_parser(partnum,
862 content_start, content_end,
864 PreMultiPartCallBack,
865 PostMultiPartCallBack,
874 * Entry point for the MIME parser.
875 * (This function expects to be fed HEADERS + CONTENT)
876 * Note: NULL can be supplied as content_end; in this case, the message is
877 * considered to have ended when the parser encounters a 0x00 byte.
879 void mime_parser(char *content_start,
881 MimeParserCallBackType CallBack,
882 MimeParserCallBackType PreMultiPartCallBack,
883 MimeParserCallBackType PostMultiPartCallBack,
888 the_mime_parser("", content_start, content_end,
890 PreMultiPartCallBack,
891 PostMultiPartCallBack,
892 userdata, dont_decode);
900 typedef struct _MimeGuess {
904 const char *MimeString;
907 MimeGuess MyMimes [] = {
935 const char *GuessMimeType(const char *data, size_t dlen)
939 while (MyMimes[MimeIndex].PatternLen != 0)
941 if ((MyMimes[MimeIndex].PatternLen +
942 MyMimes[MimeIndex].PatternOffset < dlen) &&
943 strncmp(MyMimes[MimeIndex].Pattern,
944 &data[MyMimes[MimeIndex].PatternOffset],
945 MyMimes[MimeIndex].PatternLen) == 0)
947 return MyMimes[MimeIndex].MimeString;
952 * ok, our simple minded algorythm didn't find anything,
953 * let the big chegger try it, he wil default to application/octet-stream
955 return (xdg_mime_get_mime_type_for_data(data, dlen));
959 const char* GuessMimeByFilename(const char *what, size_t len)
961 /* we know some hardcoded on our own, try them... */
962 if ((len > 3) && !strncasecmp(&what[len - 4], ".gif", 4))
964 else if ((len > 2) && !strncasecmp(&what[len - 3], ".js", 3))
965 return "text/javascript";
966 else if ((len > 3) && !strncasecmp(&what[len - 4], ".txt", 4))
968 else if ((len > 3) && !strncasecmp(&what[len - 4], ".css", 4))
970 else if ((len > 3) && !strncasecmp(&what[len - 4], ".htc", 4))
971 return "text/x-component";
972 else if ((len > 3) && !strncasecmp(&what[len - 4], ".jpg", 4))
974 else if ((len > 3) && !strncasecmp(&what[len - 4], ".png", 4))
976 else if ((len > 3) && !strncasecmp(&what[len - 4], ".ico", 4))
977 return "image/x-icon";
978 else if ((len > 3) && !strncasecmp(&what[len - 4], ".vcf", 4))
979 return "text/x-vcard";
980 else if ((len > 4) && !strncasecmp(&what[len - 5], ".html", 5))
982 else if ((len > 3) && !strncasecmp(&what[len - 4], ".htm", 4))
984 else if ((len > 3) && !strncasecmp(&what[len - 4], ".wml", 4))
985 return "text/vnd.wap.wml";
986 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmls", 5))
987 return "text/vnd.wap.wmlscript";
988 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmlc", 5))
989 return "application/vnd.wap.wmlc";
990 else if ((len > 5) && !strncasecmp(&what[len - 6], ".wmlsc", 6))
991 return "application/vnd.wap.wmlscriptc";
992 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wbmp", 5))
993 return "image/vnd.wap.wbmp";
995 /* and let xdgmime do the fallback. */
996 return xdg_mime_get_mime_type_from_file_name(what);
999 static HashList *IconHash = NULL;
1001 typedef struct IconName IconName;
1008 static void DeleteIcon(void *IconNamePtr)
1010 IconName *Icon = (IconName*) IconNamePtr;
1011 free(Icon->FlatName);
1012 free(Icon->FileName);
1017 static const char *PrintFlat(void *IconNamePtr)
1019 IconName *Icon = (IconName*) IconNamePtr;
1020 return Icon->FlatName;
1022 static const char *PrintFile(void *IconNamePtr)
1024 IconName *Icon = (IconName*) IconNamePtr;
1025 return Icon->FileName;
1029 #define GENSTR "x-generic"
1030 #define IGNORE_PREFIX_1 "gnome-mime"
1031 int LoadIconDir(const char *DirName)
1033 DIR *filedir = NULL;
1034 struct dirent *filedir_entry;
1039 filedir = opendir (DirName);
1040 IconHash = NewHash(1, NULL);
1041 if (filedir == NULL) {
1045 while ((filedir_entry = readdir(filedir)))
1049 #ifdef _DIRENT_HAVE_D_NAMELEN
1050 d_namelen = filedir_entry->d_namelen;
1052 d_namelen = strlen(filedir_entry->d_name);
1054 d_without_ext = d_namelen;
1055 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1057 if ((d_without_ext == 0) || (d_namelen < 3))
1060 if ((sizeof(IGNORE_PREFIX_1) < d_namelen) &&
1061 (strncmp(IGNORE_PREFIX_1,
1062 filedir_entry->d_name,
1063 sizeof(IGNORE_PREFIX_1) - 1) == 0)) {
1064 PStart = filedir_entry->d_name + sizeof(IGNORE_PREFIX_1);
1065 d_without_ext -= sizeof(IGNORE_PREFIX_1);
1068 PStart = filedir_entry->d_name;
1070 Icon = malloc(sizeof(IconName));
1072 Icon->FileName = malloc(d_namelen + 1);
1073 memcpy(Icon->FileName, filedir_entry->d_name, d_namelen + 1);
1075 Icon->FlatName = malloc(d_without_ext + 1);
1076 memcpy(Icon->FlatName, PStart, d_without_ext);
1077 Icon->FlatName[d_without_ext] = '\0';
1078 /* Try to find Minor type in image-jpeg */
1079 MinorPtr = strchr(Icon->FlatName, '-');
1080 if (MinorPtr != NULL) {
1082 MinorLen = 1 + d_without_ext - (MinorPtr - Icon->FlatName + 1);
1083 if ((MinorLen == sizeof(GENSTR)) &&
1084 (strncmp(MinorPtr + 1, GENSTR, sizeof(GENSTR)) == 0)) {
1085 /* ok, we found a generic filename. cut the generic. */
1087 d_without_ext = d_without_ext - (MinorPtr - Icon->FlatName);
1089 else { /* Map the major / minor separator to / */
1094 // PrintHash(IconHash, PrintFlat, PrintFile);
1095 // printf("%s - %s\n", Icon->FlatName, Icon->FileName);
1096 Put(IconHash, Icon->FlatName, d_without_ext, Icon, DeleteIcon);
1097 // PrintHash(IconHash, PrintFlat, PrintFile);
1103 const char *GetIconFilename(char *MimeType, size_t len)
1108 if(IconHash == NULL)
1111 GetHash(IconHash, MimeType, len, &vIcon), Icon = (IconName*) vIcon;
1112 /* didn't find the exact mimetype? try major only. */
1115 pMinor = strchr(MimeType, '/');
1116 if (pMinor != NULL) {
1118 GetHash(IconHash, MimeType, pMinor - MimeType, &vIcon),
1119 Icon = (IconName*) vIcon;
1126 /*printf("Getting: [%s] == [%s] -> [%s]\n", MimeType, Icon->FlatName, Icon->FileName);*/
1127 return Icon->FileName;
1130 void ShutDownLibCitadelMime(void)
1132 DeleteHash(&IconHash);