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(char *ptr,
462 interesting_mime_headers *SubMimeHeaders,
463 interesting_mime_headers *m)
468 if (IsAsciiEncoding(SubMimeHeaders)) {
472 // ok, if we have a content length of the mime part,
473 // try skipping the content on the search for the next
474 // boundary. since we don't trust the content_length
475 // to be all accurate, and suspect it to lose one digit
476 // per line with a line length of 80 chars, we need
477 // to start searching a little before..
479 if ((SubMimeHeaders->content_length != -1) && (SubMimeHeaders->content_length > 10)) {
483 lines = SubMimeHeaders->content_length / 80;
484 pptr = ptr + SubMimeHeaders->content_length - lines - 10;
485 if (pptr < content_end)
489 next_boundary = strstr(ptr, m->b[startary].Key);
494 // ok, if we have a content length of the mime part,
495 // try skipping the content on the search for the next
496 // boundary. since we don't trust the content_length
497 // to be all accurate, start searching a little before..
499 if ((SubMimeHeaders->content_length != -1) && (SubMimeHeaders->content_length > 10)) {
501 pptr = ptr + SubMimeHeaders->content_length - 10;
502 if (pptr < content_end)
506 srch = next_boundary = NULL;
507 for (srch = memchr(ptr, '-', content_end - ptr);
508 (srch != NULL) && (srch < content_end);
509 srch = memchr(srch, '-', content_end - srch))
511 if (!memcmp(srch, m->b[startary].Key, m->b[startary].len)) {
512 next_boundary = srch;
520 return next_boundary;
524 // Break out the components of a multipart message
525 // (This function expects to be fed HEADERS + CONTENT)
526 // Note: NULL can be supplied as content_end; in this case, the message is
527 // considered to have ended when the parser encounters a 0x00 byte.
528 static void recurseable_mime_parser(char *partnum,
529 char *content_start, char *content_end,
530 MimeParserCallBackType CallBack,
531 MimeParserCallBackType PreMultiPartCallBack,
532 MimeParserCallBackType PostMultiPartCallBack,
535 interesting_mime_headers *m
537 interesting_mime_headers *SubMimeHeaders;
540 char *part_end = NULL;
541 char *evaluate_crlf_ptr = NULL;
543 char nested_partnum[256];
546 CBufStr *chosen_name;
548 // If this is a multipart message, then recursively process it
551 if (m->is_multipart) {
553 // Tell the client about this message's multipartedness
554 if (PreMultiPartCallBack != NULL) {
555 PreMultiPartCallBack("",
560 m->b[content_type].Key,
569 // Figure out where the boundaries are
570 m->b[startary].len = snprintf(m->b[startary].Key, SIZ, "--%s", m->b[boundary].Key);
571 SubMimeHeaders = InitInterestingMimes ();
573 while ((*ptr == '\r') || (*ptr == '\n')) {
577 if (strncmp(ptr, m->b[startary].Key, m->b[startary].len) == 0) {
578 ptr += m->b[startary].len;
581 while ((*ptr == '\r') || (*ptr == '\n')) {
590 if (parse_MimeHeaders(SubMimeHeaders, &ptr, content_end) != 0)
592 if ((ptr - optr > 2) && (*(ptr - 2) == '\r')) {
598 next_boundary = FindNextContent(ptr, content_end, SubMimeHeaders, m);
599 if ((next_boundary != NULL) && (next_boundary - part_start < 3)) {
600 FlushInterestingMimes(SubMimeHeaders);
604 if ( (part_start != NULL) && (next_boundary != NULL) ) {
605 part_end = next_boundary;
606 --part_end; // omit the trailing LF
608 --part_end; // omit the trailing CR
611 if (!IsEmptyStr(partnum)) {
612 snprintf(nested_partnum,
613 sizeof nested_partnum,
618 snprintf(nested_partnum,
619 sizeof nested_partnum,
622 recurseable_mime_parser(nested_partnum,
626 PreMultiPartCallBack,
627 PostMultiPartCallBack,
633 if (next_boundary != NULL) {
634 // If we pass out of scope, don't attempt to read past the end boundary.
635 if ((*(next_boundary + m->b[startary].len) == '-') &&
636 (*(next_boundary + m->b[startary].len + 1) == '-') ){
640 // Set up for the next part.
641 part_start = strstr(next_boundary, "\n");
643 // Determine whether newlines are LF or CRLF
644 evaluate_crlf_ptr = part_start;
646 if ((*evaluate_crlf_ptr == '\r') && (*(evaluate_crlf_ptr + 1) == '\n')) {
653 // Advance past the LF ... now we're in the next part
659 // Invalid end of multipart. Bail out!
662 FlushInterestingMimes(SubMimeHeaders);
663 } while ( (ptr < content_end) && (next_boundary != NULL) );
665 free(SubMimeHeaders);
667 if (PostMultiPartCallBack != NULL) {
668 PostMultiPartCallBack("",
673 m->b[content_type].Key,
682 // If it's not a multipart message, then do something with it
686 length = content_end - part_start;
687 ptr = part_end = content_end;
689 // The following code will truncate the MIME part to the size
690 // specified by the Content-length: header. We have commented it
691 // out because these headers have a tendency to be wrong.
693 // if ( (content_length > 0) && (length > content_length) ) {
694 // length = content_length;
697 // Sometimes the "name" field is tacked on to Content-type,
698 // and sometimes it's tacked on to Content-disposition. Use
699 // whichever one we have.
700 if (m->b[content_disposition_name].len > m->b[content_type_name].len) {
701 chosen_name = &m->b[content_disposition_name];
704 chosen_name = &m->b[content_type_name];
707 // Ok, we've got a non-multipart part here, so do something with it.
711 m->b[content_type].Key,
714 m->b[disposition].Key,
725 // Now if it's an encapsulated message/rfc822 then we have to recurse into it
726 if (!strcasecmp(&m->b[content_type].Key[0], "message/rfc822")) {
728 if (PreMultiPartCallBack != NULL) {
729 PreMultiPartCallBack("",
734 m->b[content_type].Key,
741 if (CallBack != NULL) {
742 if (strlen(partnum) > 0) {
743 snprintf(nested_partnum,
744 sizeof nested_partnum,
749 snprintf(nested_partnum,
750 sizeof nested_partnum,
753 the_mime_parser(nested_partnum,
757 PreMultiPartCallBack,
758 PostMultiPartCallBack,
763 if (PostMultiPartCallBack != NULL) {
764 PostMultiPartCallBack("",
769 m->b[content_type].Key,
786 // Break out the components of a multipart message
787 // (This function expects to be fed HEADERS + CONTENT)
788 // Note: NULL can be supplied as content_end; in this case, the message is
789 // considered to have ended when the parser encounters a 0x00 byte.
790 void the_mime_parser(char *partnum,
791 char *content_start, char *content_end,
792 MimeParserCallBackType CallBack,
793 MimeParserCallBackType PreMultiPartCallBack,
794 MimeParserCallBackType PostMultiPartCallBack,
798 interesting_mime_headers *m;
800 // If the caller didn't supply an endpointer, generate one by measure
801 if (content_end == NULL) {
802 content_end = &content_start[strlen(content_start)];
805 m = InitInterestingMimes();
807 if (!parse_MimeHeaders(m, &content_start, content_end)) {
809 recurseable_mime_parser(partnum,
810 content_start, content_end,
812 PreMultiPartCallBack,
813 PostMultiPartCallBack,
822 // Entry point for the MIME parser.
823 // (This function expects to be fed HEADERS + CONTENT)
824 // Note: NULL can be supplied as content_end; in this case, the message is
825 // considered to have ended when the parser encounters a 0x00 byte.
826 void mime_parser(char *content_start,
828 MimeParserCallBackType CallBack,
829 MimeParserCallBackType PreMultiPartCallBack,
830 MimeParserCallBackType PostMultiPartCallBack,
834 the_mime_parser("", content_start, content_end,
836 PreMultiPartCallBack,
837 PostMultiPartCallBack,
838 userdata, dont_decode);
842 typedef struct _MimeGuess {
846 const char *MimeString;
849 MimeGuess MyMimes [] = {
877 const char *GuessMimeType(const char *data, size_t dlen) {
880 while (MyMimes[MimeIndex].PatternLen != 0) {
881 if ((MyMimes[MimeIndex].PatternLen +
882 MyMimes[MimeIndex].PatternOffset < dlen) &&
883 strncmp(MyMimes[MimeIndex].Pattern,
884 &data[MyMimes[MimeIndex].PatternOffset],
885 MyMimes[MimeIndex].PatternLen) == 0)
887 return MyMimes[MimeIndex].MimeString;
891 // ok, our simple minded algorythm didn't find anything,
892 // let the big chegger try it, he wil default to application/octet-stream
893 return (xdg_mime_get_mime_type_for_data(data, dlen));
897 const char* GuessMimeByFilename(const char *what, size_t len) {
898 // we know some hardcoded on our own, try them...
899 if ((len > 3) && !strncasecmp(&what[len - 4], ".gif", 4))
901 else if ((len > 2) && !strncasecmp(&what[len - 3], ".js", 3))
902 return "text/javascript";
903 else if ((len > 3) && !strncasecmp(&what[len - 4], ".txt", 4))
905 else if ((len > 3) && !strncasecmp(&what[len - 4], ".css", 4))
907 else if ((len > 3) && !strncasecmp(&what[len - 4], ".htc", 4))
908 return "text/x-component";
909 else if ((len > 3) && !strncasecmp(&what[len - 4], ".jpg", 4))
911 else if ((len > 4) && !strncasecmp(&what[len - 5], ".jpeg", 5))
913 else if ((len > 3) && !strncasecmp(&what[len - 4], ".png", 4))
915 else if ((len > 3) && !strncasecmp(&what[len - 4], ".ico", 4))
916 return "image/x-icon";
917 else if ((len > 3) && !strncasecmp(&what[len - 4], ".vcf", 4))
918 return "text/x-vcard";
919 else if ((len > 4) && !strncasecmp(&what[len - 5], ".html", 5))
921 else if ((len > 3) && !strncasecmp(&what[len - 4], ".htm", 4))
923 else if ((len > 3) && !strncasecmp(&what[len - 4], ".wml", 4))
924 return "text/vnd.wap.wml";
925 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmls", 5))
926 return "text/vnd.wap.wmlscript";
927 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmlc", 5))
928 return "application/vnd.wap.wmlc";
929 else if ((len > 5) && !strncasecmp(&what[len - 6], ".wmlsc", 6))
930 return "application/vnd.wap.wmlscriptc";
931 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wbmp", 5))
932 return "image/vnd.wap.wbmp";
934 // and let xdgmime do the fallback.
935 return xdg_mime_get_mime_type_from_file_name(what);
938 static HashList *IconHash = NULL;
940 typedef struct IconName IconName;
948 static void DeleteIcon(void *IconNamePtr) {
949 IconName *Icon = (IconName*) IconNamePtr;
950 free(Icon->FlatName);
951 free(Icon->FileName);
956 #define GENSTR "x-generic"
957 #define IGNORE_PREFIX_1 "gnome-mime"
958 int LoadIconDir(const char *DirName) {
960 struct dirent *filedir_entry;
965 filedir = opendir (DirName);
966 IconHash = NewHash(1, NULL);
967 if (filedir == NULL) {
971 while ((filedir_entry = readdir(filedir))) {
974 #ifdef _DIRENT_HAVE_D_NAMLEN
975 d_namelen = filedir_entry->d_namlen;
977 d_namelen = strlen(filedir_entry->d_name);
979 d_without_ext = d_namelen;
980 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
982 if ((d_without_ext == 0) || (d_namelen < 3))
985 if ((sizeof(IGNORE_PREFIX_1) < d_namelen) &&
986 (strncmp(IGNORE_PREFIX_1,
987 filedir_entry->d_name,
988 sizeof(IGNORE_PREFIX_1) - 1) == 0)) {
989 PStart = filedir_entry->d_name + sizeof(IGNORE_PREFIX_1);
990 d_without_ext -= sizeof(IGNORE_PREFIX_1);
993 PStart = filedir_entry->d_name;
995 Icon = malloc(sizeof(IconName));
997 Icon->FileName = malloc(d_namelen + 1);
998 memcpy(Icon->FileName, filedir_entry->d_name, d_namelen + 1);
1000 Icon->FlatName = malloc(d_without_ext + 1);
1001 memcpy(Icon->FlatName, PStart, d_without_ext);
1002 Icon->FlatName[d_without_ext] = '\0';
1003 // Try to find Minor type in image-jpeg
1004 MinorPtr = strchr(Icon->FlatName, '-');
1005 if (MinorPtr != NULL) {
1007 MinorLen = 1 + d_without_ext - (MinorPtr - Icon->FlatName + 1);
1008 if ((MinorLen == sizeof(GENSTR)) &&
1009 (strncmp(MinorPtr + 1, GENSTR, sizeof(GENSTR)) == 0)) {
1010 // ok, we found a generic filename. cut the generic.
1012 d_without_ext = d_without_ext - (MinorPtr - Icon->FlatName);
1014 else { // Map the major / minor separator to /
1019 Put(IconHash, Icon->FlatName, d_without_ext, Icon, DeleteIcon);
1026 const char *GetIconFilename(char *MimeType, size_t len) {
1030 if (IconHash == NULL) {
1034 GetHash(IconHash, MimeType, len, &vIcon), Icon = (IconName*) vIcon;
1035 // didn't find the exact mimetype? try major only.
1038 pMinor = strchr(MimeType, '/');
1039 if (pMinor != NULL) {
1041 GetHash(IconHash, MimeType, pMinor - MimeType, &vIcon),
1042 Icon = (IconName*) vIcon;
1049 return Icon->FileName;
1053 void ShutDownLibCitadelMime(void) {
1054 DeleteHash(&IconHash);