1 // This is the MIME parser for Citadel.
3 // Copyright (c) 1998-2023 by the citadel.org development team.
5 // This program is open source software. Use, duplication, or disclosure
6 // is subject to the terms of the GNU General Public License, version 3.
12 #include <sys/types.h>
16 #include <sys/types.h>
20 #include "xdgmime/xdgmime.h"
21 #include "libcitadel.h"
22 #include "libcitadellocal.h"
24 const unsigned char FromHexTable[256] = {
25 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0
26 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 10
27 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 20
28 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 30
29 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x01, // 40
30 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0xFF, // 50
31 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, // 60
32 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 70
33 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 80
34 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0A, 0x0B, 0x0C, // 90
35 0x0D, 0x0E, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //100
36 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //110
37 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //120
38 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //130
39 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //140
40 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //150
41 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //160
42 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //170
43 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //180
44 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //190
45 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //200
46 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //210
47 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //220
48 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //230
49 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //240
50 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF //250
54 long extract_key(char *target, char *source, long sourcelen, char *key, long keylen, char KeyEnd) {
55 char *sptr, *ptr = NULL;
56 int double_quotes = 0;
57 long RealKeyLen = keylen;
61 while (sptr != NULL) {
62 ptr = bmstrcasestr_len(sptr, sourcelen - (sptr - source), key, keylen);
64 while (isspace(*(ptr + RealKeyLen)))
66 if (*(ptr + RealKeyLen) == KeyEnd) {
71 sptr = ptr + RealKeyLen + 1;
81 strcpy(target, (ptr + RealKeyLen));
83 for (ptr=target; (*ptr != 0); ptr++) {
85 // A semicolon means we've hit the end of the key, unless we're inside double quotes
86 if ( (double_quotes != 1) && (*ptr == ';')) {
90 // if we find double quotes, we've got a great set of string boundaries
93 if (double_quotes == 1) {
106 // For non-multipart messages, we need to generate a quickie partnum of "1"
107 // to return to callback functions. Some callbacks demand it.
108 char *fixed_partnum(char *supplied_partnum) {
109 if (supplied_partnum == NULL) return "1";
110 if (strlen(supplied_partnum)==0) return "1";
111 return supplied_partnum;
115 static inline unsigned int _decode_hex(const char *Source) {
116 unsigned int ret = '?';
117 unsigned char LO_NIBBLE;
118 unsigned char HI_NIBBLE;
120 HI_NIBBLE = FromHexTable[(unsigned char) *Source];
121 LO_NIBBLE = FromHexTable[(unsigned char) *(Source+1)];
123 if ((LO_NIBBLE == 0xFF) || (LO_NIBBLE == 0xFF))
127 ret = ret | LO_NIBBLE;
131 unsigned int decode_hex(char *Source) {return _decode_hex(Source);}
134 // Convert "quoted-printable" to binary. Returns number of bytes decoded.
135 // according to RFC2045 section 6.7
136 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
138 int decoded_length = 0;
141 while (pos < sourcelen) {
142 if (*(encoded + pos) == '=') {
144 if (*(encoded + pos) == '\n') {
147 else if (*(encoded + pos) == '\r') {
149 if (*(encoded + pos) == '\n')
153 ch = _decode_hex(&encoded[pos]);
155 decoded[decoded_length++] = ch;
159 decoded[decoded_length++] = encoded[pos];
163 decoded[decoded_length] = 0;
164 return(decoded_length);
168 // Given a message or message-part body and a length, handle any necessary
169 // decoding and pass the request up the stack.
170 void mime_decode(char *partnum,
171 char *part_start, size_t length,
172 char *content_type, char *charset, char *encoding,
175 char *name, char *filename,
176 MimeParserCallBackType CallBack,
177 MimeParserCallBackType PreMultiPartCallBack,
178 MimeParserCallBackType PostMultiPartCallBack,
183 size_t bytes_decoded = 0;
185 // Some encodings aren't really encodings
186 if (!strcasecmp(encoding, "7bit"))
188 if (!strcasecmp(encoding, "8bit"))
190 if (!strcasecmp(encoding, "binary"))
192 if (!strcasecmp(encoding, "ISO-8859-1"))
195 // If this part is not encoded, send as-is
196 if ( (strlen(encoding) == 0) || (dont_decode)) {
197 if (CallBack != NULL) {
200 fixed_partnum(partnum),
213 // Fail silently if we hit an unknown encoding.
214 if ((strcasecmp(encoding, "base64")) && (strcasecmp(encoding, "quoted-printable"))) {
218 // Allocate a buffer for the decoded data. The output buffer is slightly
219 // larger than the input buffer; this assumes that the decoded data
220 // will never be significantly larger than the encoded data. This is a
221 // safe assumption with base64, uuencode, and quoted-printable.
222 decoded = malloc(length + 32768);
223 if (decoded == NULL) {
227 if (!strcasecmp(encoding, "base64")) {
228 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
230 else if (!strcasecmp(encoding, "quoted-printable")) {
231 bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
234 if (bytes_decoded > 0) if (CallBack != NULL) {
235 char encoding_buf[SIZ];
237 strcpy(encoding_buf, "binary");
240 fixed_partnum(partnum),
255 // this is a bastardization of mime_decode() which can be called if 'dont_decode' was set;
256 // to postpone the cpu intense process of decoding until the time when it realy wants the content.
258 // - > 0 we decoded something, its on *decoded, you need to free it.
259 // - = 0 no need to decode stuff. *decoded will be NULL.
260 // - < 0 an error occured, either an unknown encoding, or alloc failed. no need to free.
261 int mime_decode_now (char *part_start,
265 size_t *bytes_decoded)
269 // Some encodings aren't really encodings
270 if (!strcasecmp(encoding, "7bit"))
272 if (!strcasecmp(encoding, "8bit"))
274 if (!strcasecmp(encoding, "binary"))
277 // If this part is not encoded, send as-is
278 if (strlen(encoding) == 0) {
282 // Fail if we hit an unknown encoding.
283 if ((strcasecmp(encoding, "base64")) && (strcasecmp(encoding, "quoted-printable"))) {
287 // Allocate a buffer for the decoded data. The output buffer is slightly
288 // larger than the input buffer; this assumes that the decoded data
289 // will never be significantly larger than the encoded data. This is a
290 // safe assumption with base64, uuencode, and quoted-printable.
291 *decoded = malloc(length + 32768);
292 if (decoded == NULL) {
296 if (!strcasecmp(encoding, "base64")) {
297 *bytes_decoded = CtdlDecodeBase64(*decoded, part_start, length);
300 else if (!strcasecmp(encoding, "quoted-printable")) {
301 *bytes_decoded = CtdlDecodeQuotedPrintable(*decoded, part_start, length);
307 typedef enum _eIntMimeHdrs {
315 content_disposition_name,
322 typedef struct _CBufStr {
327 typedef struct _interesting_mime_headers {
331 } interesting_mime_headers;
334 static void FlushInterestingMimes(interesting_mime_headers *m) {
337 for (i = 0; i < eMax; i++) {
338 m->b[i].Key[0] = '\0';
341 m->content_length = -1;
345 static interesting_mime_headers *InitInterestingMimes(void) {
346 interesting_mime_headers *m;
347 m = (interesting_mime_headers*) malloc( sizeof(interesting_mime_headers));
349 FlushInterestingMimes(m);
355 static long parse_MimeHeaders(interesting_mime_headers *m, char** pcontent_start, char *content_end) {
363 // Learn interesting things from the headers
364 ptr = *pcontent_start;
368 ptr = memreadlinelen(ptr, buf, SIZ, &buflen);
370 for (i = 0; i < buflen; ++i) {
371 if (isspace(buf[i])) {
376 if (!isspace(buf[0]) && (headerlen > 0)) {
377 if (!strncasecmp(header, "Content-type:", 13)) {
378 memcpy (m->b[content_type].Key, &header[13], headerlen - 12);
379 m->b[content_type].Key[headerlen - 12] = '\0';
380 m->b[content_type].len = string_trim (m->b[content_type].Key);
382 m->b[content_type_name].len = extract_key(m->b[content_type_name].Key, CKEY(m->b[content_type]), HKEY("name"), '=');
383 m->b[charset].len = extract_key(m->b[charset].Key, CKEY(m->b[content_type]), HKEY("charset"), '=');
384 m->b[boundary].len = extract_key(m->b[boundary].Key, header, headerlen, HKEY("boundary"), '=');
386 // Deal with weird headers
387 pch = strchr(m->b[content_type].Key, ' ');
390 m->b[content_type].len = m->b[content_type].Key - pch;
392 pch = strchr(m->b[content_type].Key, ';');
395 m->b[content_type].len = m->b[content_type].Key - pch;
398 else if (!strncasecmp(header, "Content-Disposition:", 20)) {
399 memcpy (m->b[disposition].Key, &header[20], headerlen - 19);
400 m->b[disposition].Key[headerlen - 19] = '\0';
401 m->b[disposition].len = string_trim(m->b[disposition].Key);
403 m->b[content_disposition_name].len = extract_key(m->b[content_disposition_name].Key, CKEY(m->b[disposition]), HKEY("name"), '=');
404 m->b[filename].len = extract_key(m->b[filename].Key, CKEY(m->b[disposition]), HKEY("filename"), '=');
405 pch = strchr(m->b[disposition].Key, ';');
406 if (pch != NULL) *pch = '\0';
407 m->b[disposition].len = string_trim(m->b[disposition].Key);
409 else if (!strncasecmp(header, "Content-ID:", 11)) {
410 memcpy(m->b[id].Key, &header[11], headerlen - 11);
411 m->b[id].Key[headerlen - 11] = '\0';
412 string_trim(m->b[id].Key);
413 m->b[id].len = stripallbut(m->b[id].Key, '<', '>');
415 else if (!strncasecmp(header, "Content-length: ", 15)) {
418 while (isspace(*clbuf))
420 m->content_length = (size_t) atol(clbuf);
422 else if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
423 memcpy(m->b[encoding].Key, &header[26], headerlen - 26);
424 m->b[encoding].Key[headerlen - 26] = '\0';
425 m->b[encoding].len = string_trim(m->b[encoding].Key);
430 if ((headerlen + buflen + 2) < SIZ) {
431 memcpy(&header[headerlen], buf, buflen);
433 header[headerlen] = '\0';
435 if (ptr >= content_end) {
438 } while ((!IsEmptyStr(buf)) && (*ptr != 0));
440 m->is_multipart = m->b[boundary].len != 0;
441 *pcontent_start = ptr;
447 static int IsAsciiEncoding(interesting_mime_headers *m) {
449 if ((m->b[encoding].len != 0) && (strcasecmp(m->b[encoding].Key, "base64") == 0)) {
452 if ((m->b[encoding].len != 0) && (strcmp(m->b[encoding].Key, "quoted-printable") == 0)) {
460 static char *FindNextContent(
463 interesting_mime_headers *SubMimeHeaders,
464 interesting_mime_headers *m
469 if (IsAsciiEncoding(SubMimeHeaders)) {
473 // ok, if we have a content length of the mime part,
474 // try skipping the content on the search for the next
475 // boundary. since we don't trust the content_length
476 // to be all accurate, and suspect it to lose one digit
477 // per line with a line length of 80 chars, we need
478 // to start searching a little before..
480 if ((SubMimeHeaders->content_length != -1) && (SubMimeHeaders->content_length > 10)) {
484 lines = SubMimeHeaders->content_length / 80;
485 pptr = ptr + SubMimeHeaders->content_length - lines - 10;
486 if (pptr < content_end) {
491 next_boundary = strstr(ptr, m->b[startary].Key);
496 // ok, if we have a content length of the mime part,
497 // try skipping the content on the search for the next
498 // boundary. since we don't trust the content_length
499 // to be all accurate, start searching a little before..
501 if ((SubMimeHeaders->content_length != -1) && (SubMimeHeaders->content_length > 10)) {
503 pptr = ptr + SubMimeHeaders->content_length - 10;
504 if (pptr < content_end)
508 srch = next_boundary = NULL;
509 for (srch = memchr(ptr, '-', content_end - ptr);
510 (srch != NULL) && (srch < content_end);
511 srch = memchr(srch, '-', content_end - srch))
513 if (!memcmp(srch, m->b[startary].Key, m->b[startary].len)) {
514 next_boundary = srch;
523 return next_boundary;
527 // Break out the components of a multipart message
528 // (This function expects to be fed HEADERS + CONTENT)
529 // Note: NULL can be supplied as content_end; in this case, the message is
530 // considered to have ended when the parser encounters a 0x00 byte.
531 static void recurseable_mime_parser(char *partnum,
532 char *content_start, char *content_end,
533 MimeParserCallBackType CallBack,
534 MimeParserCallBackType PreMultiPartCallBack,
535 MimeParserCallBackType PostMultiPartCallBack,
538 interesting_mime_headers *m
540 interesting_mime_headers *SubMimeHeaders;
543 char *part_end = NULL;
544 char *evaluate_crlf_ptr = NULL;
546 char nested_partnum[256];
549 CBufStr *chosen_name;
551 // If this is a multipart message, then recursively process it
554 if (m->is_multipart) {
556 // Tell the client about this message's multipartedness
557 if (PreMultiPartCallBack != NULL) {
558 PreMultiPartCallBack("",
563 m->b[content_type].Key,
572 // Figure out where the boundaries are
573 m->b[startary].len = snprintf(m->b[startary].Key, SIZ, "--%s", m->b[boundary].Key);
574 SubMimeHeaders = InitInterestingMimes();
576 while ((*ptr == '\r') || (*ptr == '\n')) {
580 if (strncmp(ptr, m->b[startary].Key, m->b[startary].len) == 0) {
581 ptr += m->b[startary].len;
584 while ((*ptr == '\r') || (*ptr == '\n')) {
593 if (parse_MimeHeaders(SubMimeHeaders, &ptr, content_end) != 0)
595 if ((ptr - optr > 2) && (*(ptr - 2) == '\r')) {
601 next_boundary = FindNextContent(ptr, content_end, SubMimeHeaders, m);
602 if ((next_boundary != NULL) && (next_boundary - part_start < 3)) {
603 FlushInterestingMimes(SubMimeHeaders);
607 if ( (part_start != NULL) && (next_boundary != NULL) ) {
608 part_end = next_boundary;
609 --part_end; // omit the trailing LF
611 --part_end; // omit the trailing CR
614 if (!IsEmptyStr(partnum)) {
615 snprintf(nested_partnum,
616 sizeof nested_partnum,
621 snprintf(nested_partnum,
622 sizeof nested_partnum,
625 recurseable_mime_parser(nested_partnum,
629 PreMultiPartCallBack,
630 PostMultiPartCallBack,
636 if (next_boundary != NULL) {
637 // If we pass out of scope, don't attempt to read past the end boundary.
638 if ((*(next_boundary + m->b[startary].len) == '-') &&
639 (*(next_boundary + m->b[startary].len + 1) == '-') ){
643 // Set up for the next part.
644 part_start = strstr(next_boundary, "\n");
646 // Determine whether newlines are LF or CRLF
647 evaluate_crlf_ptr = part_start;
649 if ((*evaluate_crlf_ptr == '\r') && (*(evaluate_crlf_ptr + 1) == '\n')) {
656 // Advance past the LF ... now we're in the next part
662 // Invalid end of multipart. Bail out!
665 FlushInterestingMimes(SubMimeHeaders);
666 } while ( (ptr < content_end) && (next_boundary != NULL) );
668 free(SubMimeHeaders);
670 if (PostMultiPartCallBack != NULL) {
671 PostMultiPartCallBack("",
676 m->b[content_type].Key,
686 // If it's not a multipart message, then do something with it
690 length = content_end - part_start;
691 ptr = part_end = content_end;
693 // The following code will truncate the MIME part to the size
694 // specified by the Content-length: header. We have commented it
695 // out because these headers have a tendency to be wrong.
697 // if ( (content_length > 0) && (length > content_length) ) {
698 // length = content_length;
701 // Sometimes the "name" field is tacked on to Content-type,
702 // and sometimes it's tacked on to Content-disposition. Use
703 // whichever one we have.
704 if (m->b[content_disposition_name].len > m->b[content_type_name].len) {
705 chosen_name = &m->b[content_disposition_name];
708 chosen_name = &m->b[content_type_name];
711 // Ok, we've got a non-multipart part here, so do something with it.
715 m->b[content_type].Key,
718 m->b[disposition].Key,
729 // Now if it's an encapsulated message/rfc822 then we have to recurse into it
730 if (!strcasecmp(&m->b[content_type].Key[0], "message/rfc822")) {
732 if (PreMultiPartCallBack != NULL) {
733 PreMultiPartCallBack("",
738 m->b[content_type].Key,
745 if (CallBack != NULL) {
746 if (strlen(partnum) > 0) {
747 snprintf(nested_partnum,
748 sizeof nested_partnum,
753 snprintf(nested_partnum,
754 sizeof nested_partnum,
757 the_mime_parser(nested_partnum,
761 PreMultiPartCallBack,
762 PostMultiPartCallBack,
767 if (PostMultiPartCallBack != NULL) {
768 PostMultiPartCallBack("",
773 m->b[content_type].Key,
790 // Break out the components of a multipart message
791 // (This function expects to be fed HEADERS + CONTENT)
792 // Note: NULL can be supplied as content_end; in this case, the message is
793 // considered to have ended when the parser encounters a 0x00 byte.
794 void the_mime_parser(char *partnum,
795 char *content_start, char *content_end,
796 MimeParserCallBackType CallBack,
797 MimeParserCallBackType PreMultiPartCallBack,
798 MimeParserCallBackType PostMultiPartCallBack,
802 interesting_mime_headers *m;
804 // If the caller didn't supply an endpointer, generate one by measure
805 if (content_end == NULL) {
806 content_end = &content_start[strlen(content_start)];
809 m = InitInterestingMimes();
810 if (!parse_MimeHeaders(m, &content_start, content_end)) {
811 recurseable_mime_parser(partnum,
812 content_start, content_end,
814 PreMultiPartCallBack,
815 PostMultiPartCallBack,
824 // Entry point for the MIME parser.
825 // (This function expects to be fed HEADERS + CONTENT)
826 // Note: NULL can be supplied as content_end; in this case, the message is
827 // considered to have ended when the parser encounters a 0x00 byte.
828 void mime_parser(char *content_start,
830 MimeParserCallBackType CallBack,
831 MimeParserCallBackType PreMultiPartCallBack,
832 MimeParserCallBackType PostMultiPartCallBack,
836 the_mime_parser("", content_start, content_end,
838 PreMultiPartCallBack,
839 PostMultiPartCallBack,
840 userdata, dont_decode);
844 typedef struct _MimeGuess {
848 const char *MimeString;
851 MimeGuess MyMimes [] = {
879 const char *GuessMimeType(const char *data, size_t dlen) {
882 while (MyMimes[MimeIndex].PatternLen != 0) {
883 if ((MyMimes[MimeIndex].PatternLen +
884 MyMimes[MimeIndex].PatternOffset < dlen) &&
885 strncmp(MyMimes[MimeIndex].Pattern,
886 &data[MyMimes[MimeIndex].PatternOffset],
887 MyMimes[MimeIndex].PatternLen) == 0)
889 return MyMimes[MimeIndex].MimeString;
893 // ok, our simple minded algorythm didn't find anything,
894 // let the big chegger try it, he wil default to application/octet-stream
895 return (xdg_mime_get_mime_type_for_data(data, dlen));
899 const char* GuessMimeByFilename(const char *what, size_t len) {
900 // we know some hardcoded on our own, try them...
901 if ((len > 3) && !strncasecmp(&what[len - 4], ".gif", 4))
903 else if ((len > 2) && !strncasecmp(&what[len - 3], ".js", 3))
904 return "text/javascript";
905 else if ((len > 3) && !strncasecmp(&what[len - 4], ".txt", 4))
907 else if ((len > 3) && !strncasecmp(&what[len - 4], ".css", 4))
909 else if ((len > 3) && !strncasecmp(&what[len - 4], ".htc", 4))
910 return "text/x-component";
911 else if ((len > 3) && !strncasecmp(&what[len - 4], ".jpg", 4))
913 else if ((len > 4) && !strncasecmp(&what[len - 5], ".jpeg", 5))
915 else if ((len > 3) && !strncasecmp(&what[len - 4], ".png", 4))
917 else if ((len > 3) && !strncasecmp(&what[len - 4], ".ico", 4))
918 return "image/x-icon";
919 else if ((len > 3) && !strncasecmp(&what[len - 4], ".vcf", 4))
920 return "text/x-vcard";
921 else if ((len > 4) && !strncasecmp(&what[len - 5], ".html", 5))
923 else if ((len > 3) && !strncasecmp(&what[len - 4], ".htm", 4))
925 else if ((len > 3) && !strncasecmp(&what[len - 4], ".wml", 4))
926 return "text/vnd.wap.wml";
927 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmls", 5))
928 return "text/vnd.wap.wmlscript";
929 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmlc", 5))
930 return "application/vnd.wap.wmlc";
931 else if ((len > 5) && !strncasecmp(&what[len - 6], ".wmlsc", 6))
932 return "application/vnd.wap.wmlscriptc";
933 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wbmp", 5))
934 return "image/vnd.wap.wbmp";
936 // and let xdgmime do the fallback.
937 return xdg_mime_get_mime_type_from_file_name(what);
940 static HashList *IconHash = NULL;
942 typedef struct IconName IconName;
950 static void DeleteIcon(void *IconNamePtr) {
951 IconName *Icon = (IconName*) IconNamePtr;
952 free(Icon->FlatName);
953 free(Icon->FileName);
958 #define GENSTR "x-generic"
959 #define IGNORE_PREFIX_1 "gnome-mime"
960 int LoadIconDir(const char *DirName) {
962 struct dirent *filedir_entry;
967 filedir = opendir (DirName);
968 IconHash = NewHash(1, NULL);
969 if (filedir == NULL) {
973 while ((filedir_entry = readdir(filedir))) {
976 #ifdef _DIRENT_HAVE_D_NAMLEN
977 d_namelen = filedir_entry->d_namlen;
979 d_namelen = strlen(filedir_entry->d_name);
981 d_without_ext = d_namelen;
982 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
984 if ((d_without_ext == 0) || (d_namelen < 3))
987 if ((sizeof(IGNORE_PREFIX_1) < d_namelen) &&
988 (strncmp(IGNORE_PREFIX_1,
989 filedir_entry->d_name,
990 sizeof(IGNORE_PREFIX_1) - 1) == 0)) {
991 PStart = filedir_entry->d_name + sizeof(IGNORE_PREFIX_1);
992 d_without_ext -= sizeof(IGNORE_PREFIX_1);
995 PStart = filedir_entry->d_name;
997 Icon = malloc(sizeof(IconName));
999 Icon->FileName = malloc(d_namelen + 1);
1000 memcpy(Icon->FileName, filedir_entry->d_name, d_namelen + 1);
1002 Icon->FlatName = malloc(d_without_ext + 1);
1003 memcpy(Icon->FlatName, PStart, d_without_ext);
1004 Icon->FlatName[d_without_ext] = '\0';
1005 // Try to find Minor type in image-jpeg
1006 MinorPtr = strchr(Icon->FlatName, '-');
1007 if (MinorPtr != NULL) {
1009 MinorLen = 1 + d_without_ext - (MinorPtr - Icon->FlatName + 1);
1010 if ((MinorLen == sizeof(GENSTR)) &&
1011 (strncmp(MinorPtr + 1, GENSTR, sizeof(GENSTR)) == 0)) {
1012 // ok, we found a generic filename. cut the generic.
1014 d_without_ext = d_without_ext - (MinorPtr - Icon->FlatName);
1016 else { // Map the major / minor separator to /
1021 Put(IconHash, Icon->FlatName, d_without_ext, Icon, DeleteIcon);
1028 const char *GetIconFilename(char *MimeType, size_t len) {
1032 if (IconHash == NULL) {
1036 GetHash(IconHash, MimeType, len, &vIcon), Icon = (IconName*) vIcon;
1037 // didn't find the exact mimetype? try major only.
1040 pMinor = strchr(MimeType, '/');
1041 if (pMinor != NULL) {
1043 GetHash(IconHash, MimeType, pMinor - MimeType, &vIcon),
1044 Icon = (IconName*) vIcon;
1051 return Icon->FileName;
1055 void ShutDownLibCitadelMime(void) {
1056 DeleteHash(&IconHash);