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 CallBack(name, filename, fixed_partnum(partnum),
258 disposition, decoded,
259 content_type, charset, bytes_decoded, "binary", id, userdata);
266 * this is the extract of mime_decode which can be called if 'dont_decode' was set;
267 * to save the cpu intense process of decoding to the time when it realy wants the content.
269 * - > 0 we decoded something, its on *decoded, you need to free it.
270 * - = 0 no need to decode stuff. *decoded will be NULL.
271 * - < 0 an error occured, either an unknown encoding, or alloc failed. no need to free.
273 int mime_decode_now (char *part_start,
277 size_t *bytes_decoded)
281 /* Some encodings aren't really encodings */
282 if (!strcasecmp(encoding, "7bit"))
283 strcpy(encoding, "");
284 if (!strcasecmp(encoding, "8bit"))
285 strcpy(encoding, "");
286 if (!strcasecmp(encoding, "binary"))
287 strcpy(encoding, "");
289 /* If this part is not encoded, send as-is */
290 if (strlen(encoding) == 0) {
295 /* Fail if we hit an unknown encoding. */
296 if ((strcasecmp(encoding, "base64"))
297 && (strcasecmp(encoding, "quoted-printable"))) {
302 * Allocate a buffer for the decoded data. The output buffer is slightly
303 * larger than the input buffer; this assumes that the decoded data
304 * will never be significantly larger than the encoded data. This is a
305 * safe assumption with base64, uuencode, and quoted-printable.
307 *decoded = malloc(length + 32768);
308 if (decoded == NULL) {
312 if (!strcasecmp(encoding, "base64")) {
313 *bytes_decoded = CtdlDecodeBase64(*decoded, part_start, length);
316 else if (!strcasecmp(encoding, "quoted-printable")) {
317 *bytes_decoded = CtdlDecodeQuotedPrintable(*decoded, part_start, length);
323 typedef enum _eIntMimeHdrs {
331 content_disposition_name,
335 eMax /* don't move ! */
338 typedef struct _CBufStr {
343 typedef struct _interesting_mime_headers {
347 } interesting_mime_headers;
350 static void FlushInterestingMimes(interesting_mime_headers *m)
354 for (i = 0; i < eMax; i++) {
355 m->b[i].Key[0] = '\0';
358 m->content_length = -1;
360 static interesting_mime_headers *InitInterestingMimes(void)
362 interesting_mime_headers *m;
363 m = (interesting_mime_headers*) malloc( sizeof(interesting_mime_headers));
365 FlushInterestingMimes(m);
371 static long parse_MimeHeaders(interesting_mime_headers *m,
372 char** pcontent_start,
382 /* Learn interesting things from the headers */
383 ptr = *pcontent_start;
387 ptr = memreadlinelen(ptr, buf, SIZ, &buflen);
389 for (i = 0; i < buflen; ++i) {
390 if (isspace(buf[i])) {
395 if (!isspace(buf[0]) && (headerlen > 0)) {
396 if (!strncasecmp(header, "Content-type:", 13)) {
397 memcpy (m->b[content_type].Key, &header[13], headerlen - 12);
398 m->b[content_type].len = striplt (m->b[content_type].Key);
400 m->b[content_type_name].len = extract_key(m->b[content_type_name].Key, CKEY(m->b[content_type]), HKEY("name"), '=');
401 m->b[charset].len = extract_key(m->b[charset].Key, CKEY(m->b[content_type]), HKEY("charset"), '=');
402 m->b[boundary].len = extract_key(m->b[boundary].Key, header, headerlen, HKEY("boundary"), '=');
404 /* Deal with weird headers */
405 pch = strchr(m->b[content_type].Key, ' ');
408 m->b[content_type].len = m->b[content_type].Key - pch;
410 pch = strchr(m->b[content_type].Key, ';');
413 m->b[content_type].len = m->b[content_type].Key - pch;
416 else if (!strncasecmp(header, "Content-Disposition:", 20)) {
417 memcpy (m->b[disposition].Key, &header[20], headerlen - 19);
418 m->b[disposition].len = striplt(m->b[disposition].Key);
420 m->b[content_disposition_name].len = extract_key(m->b[content_disposition_name].Key, CKEY(m->b[disposition]), HKEY("name"), '=');
421 m->b[filename].len = extract_key(m->b[filename].Key, CKEY(m->b[disposition]), HKEY("filename"), '=');
422 pch = strchr(m->b[disposition].Key, ';');
423 if (pch != NULL) *pch = '\0';
424 m->b[disposition].len = striplt(m->b[disposition].Key);
426 else if (!strncasecmp(header, "Content-ID:", 11)) {
427 memcpy(m->b[id].Key, &header[11], headerlen);
428 striplt(m->b[id].Key);
429 m->b[id].len = stripallbut(m->b[id].Key, '<', '>');
431 else if (!strncasecmp(header, "Content-length: ", 15)) {
434 while (isspace(*clbuf))
436 m->content_length = (size_t) atol(clbuf);
438 else if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
439 memcpy(m->b[encoding].Key, &header[26], headerlen - 26);
440 m->b[encoding].len = striplt(m->b[encoding].Key);
445 if ((headerlen + buflen + 2) < SIZ) {
446 memcpy(&header[headerlen], buf, buflen);
448 header[headerlen] = '\0';
450 if (ptr >= content_end) {
453 } while ((!IsEmptyStr(buf)) && (*ptr != 0));
455 m->is_multipart = m->b[boundary].len != 0;
456 *pcontent_start = ptr;
462 static int IsAsciiEncoding(interesting_mime_headers *m)
464 if ((m->b[encoding].len != 0) &&
465 (strcmp(m->b[encoding].Key, "binary") == 0))
471 static char *FindNextContent(char *ptr,
473 interesting_mime_headers *SubMimeHeaders,
474 interesting_mime_headers *m)
479 if (IsAsciiEncoding(SubMimeHeaders)) {
484 * ok, if we have a content length of the mime part,
485 * try skipping the content on the search for the next
486 * boundary. since we don't trust the content_length
487 * to be all accurate, and suspect it to lose one digit
488 * per line with a line length of 80 chars, we need
489 * to start searching a little before..
492 if ((SubMimeHeaders->content_length != -1) &&
493 (SubMimeHeaders->content_length > 10))
498 lines = SubMimeHeaders->content_length / 80;
499 pptr = ptr + SubMimeHeaders->content_length - lines - 10;
500 if (pptr < content_end)
504 next_boundary = strstr(ptr, m->b[startary].Key);
510 * ok, if we have a content length of the mime part,
511 * try skipping the content on the search for the next
512 * boundary. since we don't trust the content_length
513 * to be all accurate, start searching a little before..
516 if ((SubMimeHeaders->content_length != -1) &&
517 (SubMimeHeaders->content_length > 10))
520 pptr = ptr + SubMimeHeaders->content_length - 10;
521 if (pptr < content_end)
524 next_boundary = NULL;
525 for (srch=ptr; srch<content_end; ++srch) {
530 next_boundary = srch;
536 return next_boundary;
540 * Break out the components of a multipart message
541 * (This function expects to be fed HEADERS + CONTENT)
542 * Note: NULL can be supplied as content_end; in this case, the message is
543 * considered to have ended when the parser encounters a 0x00 byte.
545 static void recurseable_mime_parser(char *partnum,
546 char *content_start, char *content_end,
547 MimeParserCallBackType CallBack,
548 MimeParserCallBackType PreMultiPartCallBack,
549 MimeParserCallBackType PostMultiPartCallBack,
552 interesting_mime_headers *m)
554 interesting_mime_headers *SubMimeHeaders;
557 char *part_end = NULL;
558 char *evaluate_crlf_ptr = NULL;
560 char nested_partnum[256];
563 CBufStr *chosen_name;
566 /* If this is a multipart message, then recursively process it */
569 if (m->is_multipart) {
571 /* Tell the client about this message's multipartedness */
572 if (PreMultiPartCallBack != NULL) {
573 PreMultiPartCallBack("",
578 m->b[content_type].Key,
586 /* Figure out where the boundaries are */
587 m->b[startary].len = snprintf(m->b[startary].Key, SIZ, "--%s", m->b[boundary].Key);
588 SubMimeHeaders = InitInterestingMimes ();
593 if (strncmp(ptr, m->b[startary].Key, m->b[startary].len) == 0)
594 ptr += m->b[startary].len;
602 if (parse_MimeHeaders(SubMimeHeaders, &ptr, content_end) != 0)
606 next_boundary = FindNextContent(ptr,
610 if ((next_boundary != NULL) &&
611 (next_boundary - part_start < 3))
614 if ( (part_start != NULL) && (next_boundary != NULL) ) {
615 part_end = next_boundary;
616 --part_end; /* omit the trailing LF */
618 --part_end; /* omit the trailing CR */
621 if (!IsEmptyStr(partnum)) {
622 snprintf(nested_partnum,
623 sizeof nested_partnum,
628 snprintf(nested_partnum,
629 sizeof nested_partnum,
632 recurseable_mime_parser(nested_partnum,
636 PreMultiPartCallBack,
637 PostMultiPartCallBack,
643 if (next_boundary != NULL) {
644 /* If we pass out of scope, don't attempt to
645 * read past the end boundary. */
646 if ((*(next_boundary + m->b[startary].len + 1) == '-') &&
647 (*(next_boundary + m->b[startary].len + 2) == '-') ){
651 /* Set up for the next part. */
652 part_start = strstr(next_boundary, "\n");
654 /* Determine whether newlines are LF or CRLF */
655 evaluate_crlf_ptr = part_start;
657 if ((*evaluate_crlf_ptr == '\r') &&
658 (*(evaluate_crlf_ptr + 1) == '\n'))
666 /* Advance past the LF ... now we're in the next part */
672 /* Invalid end of multipart. Bail out! */
675 FlushInterestingMimes(SubMimeHeaders);
676 } while ( (ptr < content_end) && (next_boundary != NULL) );
678 free(SubMimeHeaders);
680 if (PostMultiPartCallBack != NULL) {
681 PostMultiPartCallBack("",
686 m->b[content_type].Key,
693 } /* If it's not a multipart message, then do something with it */
697 length = content_end - part_start;
698 ptr = part_end = content_end;
701 /* The following code will truncate the MIME part to the size
702 * specified by the Content-length: header. We have commented it
703 * out because these headers have a tendency to be wrong.
705 * if ( (content_length > 0) && (length > content_length) ) {
706 * length = content_length;
710 /* Sometimes the "name" field is tacked on to Content-type,
711 * and sometimes it's tacked on to Content-disposition. Use
712 * whichever one we have.
714 if (m->b[content_disposition_name].len > m->b[content_type_name].len) {
715 chosen_name = &m->b[content_disposition_name];
718 chosen_name = &m->b[content_type_name];
721 /* Ok, we've got a non-multipart part here, so do something with it.
726 m->b[content_type].Key,
729 m->b[disposition].Key,
740 * Now if it's an encapsulated message/rfc822 then we have to recurse into it
742 if (!strcasecmp(&m->b[content_type].Key[0], "message/rfc822")) {
744 if (PreMultiPartCallBack != NULL) {
745 PreMultiPartCallBack("",
750 m->b[content_type].Key,
757 if (CallBack != NULL) {
758 if (strlen(partnum) > 0) {
759 snprintf(nested_partnum,
760 sizeof nested_partnum,
765 snprintf(nested_partnum,
766 sizeof nested_partnum,
769 the_mime_parser(nested_partnum,
773 PreMultiPartCallBack,
774 PostMultiPartCallBack,
779 if (PostMultiPartCallBack != NULL) {
780 PostMultiPartCallBack("",
785 m->b[content_type].Key,
801 * Break out the components of a multipart message
802 * (This function expects to be fed HEADERS + CONTENT)
803 * Note: NULL can be supplied as content_end; in this case, the message is
804 * considered to have ended when the parser encounters a 0x00 byte.
806 void the_mime_parser(char *partnum,
807 char *content_start, char *content_end,
808 MimeParserCallBackType CallBack,
809 MimeParserCallBackType PreMultiPartCallBack,
810 MimeParserCallBackType PostMultiPartCallBack,
814 interesting_mime_headers *m;
816 /* If the caller didn't supply an endpointer, generate one by measure */
817 if (content_end == NULL) {
818 content_end = &content_start[strlen(content_start)];
821 m = InitInterestingMimes();
823 if (!parse_MimeHeaders(m, &content_start, content_end))
826 recurseable_mime_parser(partnum,
827 content_start, content_end,
829 PreMultiPartCallBack,
830 PostMultiPartCallBack,
839 * Entry point for the MIME parser.
840 * (This function expects to be fed HEADERS + CONTENT)
841 * Note: NULL can be supplied as content_end; in this case, the message is
842 * considered to have ended when the parser encounters a 0x00 byte.
844 void mime_parser(char *content_start,
846 MimeParserCallBackType CallBack,
847 MimeParserCallBackType PreMultiPartCallBack,
848 MimeParserCallBackType PostMultiPartCallBack,
853 the_mime_parser("", content_start, content_end,
855 PreMultiPartCallBack,
856 PostMultiPartCallBack,
857 userdata, dont_decode);
865 typedef struct _MimeGuess {
869 const char *MimeString;
872 MimeGuess MyMimes [] = {
900 const char *GuessMimeType(const char *data, size_t dlen)
904 while (MyMimes[MimeIndex].PatternLen != 0)
906 if ((MyMimes[MimeIndex].PatternLen +
907 MyMimes[MimeIndex].PatternOffset < dlen) &&
908 strncmp(MyMimes[MimeIndex].Pattern,
909 &data[MyMimes[MimeIndex].PatternOffset],
910 MyMimes[MimeIndex].PatternLen) == 0)
912 return MyMimes[MimeIndex].MimeString;
917 * ok, our simple minded algorythm didn't find anything,
918 * let the big chegger try it, he wil default to application/octet-stream
920 return (xdg_mime_get_mime_type_for_data(data, dlen));
924 const char* GuessMimeByFilename(const char *what, size_t len)
926 /* we know some hardcoded on our own, try them... */
927 if ((len > 3) && !strncasecmp(&what[len - 4], ".gif", 4))
929 else if ((len > 2) && !strncasecmp(&what[len - 3], ".js", 3))
930 return "text/javascript";
931 else if ((len > 3) && !strncasecmp(&what[len - 4], ".txt", 4))
933 else if ((len > 3) && !strncasecmp(&what[len - 4], ".css", 4))
935 else if ((len > 3) && !strncasecmp(&what[len - 4], ".htc", 4))
936 return "text/x-component";
937 else if ((len > 3) && !strncasecmp(&what[len - 4], ".jpg", 4))
939 else if ((len > 3) && !strncasecmp(&what[len - 4], ".png", 4))
941 else if ((len > 3) && !strncasecmp(&what[len - 4], ".ico", 4))
942 return "image/x-icon";
943 else if ((len > 3) && !strncasecmp(&what[len - 4], ".vcf", 4))
944 return "text/x-vcard";
945 else if ((len > 4) && !strncasecmp(&what[len - 5], ".html", 5))
947 else if ((len > 3) && !strncasecmp(&what[len - 4], ".htm", 4))
949 else if ((len > 3) && !strncasecmp(&what[len - 4], ".wml", 4))
950 return "text/vnd.wap.wml";
951 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmls", 5))
952 return "text/vnd.wap.wmlscript";
953 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmlc", 5))
954 return "application/vnd.wap.wmlc";
955 else if ((len > 5) && !strncasecmp(&what[len - 6], ".wmlsc", 6))
956 return "application/vnd.wap.wmlscriptc";
957 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wbmp", 5))
958 return "image/vnd.wap.wbmp";
960 /* and let xdgmime do the fallback. */
961 return xdg_mime_get_mime_type_from_file_name(what);
964 static HashList *IconHash = NULL;
966 typedef struct IconName IconName;
973 static void DeleteIcon(void *IconNamePtr)
975 IconName *Icon = (IconName*) IconNamePtr;
976 free(Icon->FlatName);
977 free(Icon->FileName);
982 static const char *PrintFlat(void *IconNamePtr)
984 IconName *Icon = (IconName*) IconNamePtr;
985 return Icon->FlatName;
987 static const char *PrintFile(void *IconNamePtr)
989 IconName *Icon = (IconName*) IconNamePtr;
990 return Icon->FileName;
994 #define GENSTR "x-generic"
995 #define IGNORE_PREFIX_1 "gnome-mime"
996 int LoadIconDir(const char *DirName)
999 struct dirent *filedir_entry;
1004 filedir = opendir (DirName);
1005 IconHash = NewHash(1, NULL);
1006 if (filedir == NULL) {
1010 while ((filedir_entry = readdir(filedir)))
1014 #ifdef _DIRENT_HAVE_D_NAMELEN
1015 d_namelen = filedir_entry->d_namelen;
1017 d_namelen = strlen(filedir_entry->d_name);
1019 d_without_ext = d_namelen;
1020 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1022 if ((d_without_ext == 0) || (d_namelen < 3))
1025 if ((sizeof(IGNORE_PREFIX_1) < d_namelen) &&
1026 (strncmp(IGNORE_PREFIX_1,
1027 filedir_entry->d_name,
1028 sizeof(IGNORE_PREFIX_1) - 1) == 0)) {
1029 PStart = filedir_entry->d_name + sizeof(IGNORE_PREFIX_1);
1030 d_without_ext -= sizeof(IGNORE_PREFIX_1);
1033 PStart = filedir_entry->d_name;
1035 Icon = malloc(sizeof(IconName));
1037 Icon->FileName = malloc(d_namelen + 1);
1038 memcpy(Icon->FileName, filedir_entry->d_name, d_namelen + 1);
1040 Icon->FlatName = malloc(d_without_ext + 1);
1041 memcpy(Icon->FlatName, PStart, d_without_ext);
1042 Icon->FlatName[d_without_ext] = '\0';
1043 /* Try to find Minor type in image-jpeg */
1044 MinorPtr = strchr(Icon->FlatName, '-');
1045 if (MinorPtr != NULL) {
1047 MinorLen = 1 + d_without_ext - (MinorPtr - Icon->FlatName + 1);
1048 if ((MinorLen == sizeof(GENSTR)) &&
1049 (strncmp(MinorPtr + 1, GENSTR, sizeof(GENSTR)) == 0)) {
1050 /* ok, we found a generic filename. cut the generic. */
1052 d_without_ext = d_without_ext - (MinorPtr - Icon->FlatName);
1054 else { /* Map the major / minor separator to / */
1059 // PrintHash(IconHash, PrintFlat, PrintFile);
1060 // printf("%s - %s\n", Icon->FlatName, Icon->FileName);
1061 Put(IconHash, Icon->FlatName, d_without_ext, Icon, DeleteIcon);
1062 // PrintHash(IconHash, PrintFlat, PrintFile);
1068 const char *GetIconFilename(char *MimeType, size_t len)
1073 if(IconHash == NULL)
1076 GetHash(IconHash, MimeType, len, &vIcon), Icon = (IconName*) vIcon;
1077 /* didn't find the exact mimetype? try major only. */
1080 pMinor = strchr(MimeType, '/');
1081 if (pMinor != NULL) {
1083 GetHash(IconHash, MimeType, pMinor - MimeType, &vIcon),
1084 Icon = (IconName*) vIcon;
1091 /*printf("Getting: [%s] == [%s] -> [%s]\n", MimeType, Icon->FlatName, Icon->FileName);*/
1092 return Icon->FileName;
1095 void ShutDownLibCitadelMime(void)
1097 DeleteHash(&IconHash);