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),
254 // this is the extract of mime_decode which can be called if 'dont_decode' was set;
255 // to save the cpu intense process of decoding to the time when it realy wants the content.
257 // - > 0 we decoded something, its on *decoded, you need to free it.
258 // - = 0 no need to decode stuff. *decoded will be NULL.
259 // - < 0 an error occured, either an unknown encoding, or alloc failed. no need to free.
260 int mime_decode_now (char *part_start,
264 size_t *bytes_decoded)
268 // Some encodings aren't really encodings
269 if (!strcasecmp(encoding, "7bit"))
271 if (!strcasecmp(encoding, "8bit"))
273 if (!strcasecmp(encoding, "binary"))
276 // If this part is not encoded, send as-is
277 if (strlen(encoding) == 0) {
282 // Fail if we hit an unknown encoding.
283 if ((strcasecmp(encoding, "base64"))
284 && (strcasecmp(encoding, "quoted-printable"))) {
288 // Allocate a buffer for the decoded data. The output buffer is slightly
289 // larger than the input buffer; this assumes that the decoded data
290 // will never be significantly larger than the encoded data. This is a
291 // safe assumption with base64, uuencode, and quoted-printable.
292 *decoded = malloc(length + 32768);
293 if (decoded == NULL) {
297 if (!strcasecmp(encoding, "base64")) {
298 *bytes_decoded = CtdlDecodeBase64(*decoded, part_start, length);
301 else if (!strcasecmp(encoding, "quoted-printable")) {
302 *bytes_decoded = CtdlDecodeQuotedPrintable(*decoded, part_start, length);
308 typedef enum _eIntMimeHdrs {
316 content_disposition_name,
320 eMax /* don't move ! */
323 typedef struct _CBufStr {
328 typedef struct _interesting_mime_headers {
332 } interesting_mime_headers;
335 static void FlushInterestingMimes(interesting_mime_headers *m)
339 for (i = 0; i < eMax; i++) {
340 m->b[i].Key[0] = '\0';
343 m->content_length = -1;
345 static interesting_mime_headers *InitInterestingMimes(void)
347 interesting_mime_headers *m;
348 m = (interesting_mime_headers*) malloc( sizeof(interesting_mime_headers));
350 FlushInterestingMimes(m);
356 static long parse_MimeHeaders(interesting_mime_headers *m,
357 char** pcontent_start,
367 // Learn interesting things from the headers
368 ptr = *pcontent_start;
372 ptr = memreadlinelen(ptr, buf, SIZ, &buflen);
374 for (i = 0; i < buflen; ++i) {
375 if (isspace(buf[i])) {
380 if (!isspace(buf[0]) && (headerlen > 0)) {
381 if (!strncasecmp(header, "Content-type:", 13)) {
382 memcpy (m->b[content_type].Key, &header[13], headerlen - 12);
383 m->b[content_type].Key[headerlen - 12] = '\0';
384 m->b[content_type].len = string_trim (m->b[content_type].Key);
386 m->b[content_type_name].len = extract_key(m->b[content_type_name].Key, CKEY(m->b[content_type]), HKEY("name"), '=');
387 m->b[charset].len = extract_key(m->b[charset].Key, CKEY(m->b[content_type]), HKEY("charset"), '=');
388 m->b[boundary].len = extract_key(m->b[boundary].Key, header, headerlen, HKEY("boundary"), '=');
390 /* Deal with weird headers */
391 pch = strchr(m->b[content_type].Key, ' ');
394 m->b[content_type].len = m->b[content_type].Key - pch;
396 pch = strchr(m->b[content_type].Key, ';');
399 m->b[content_type].len = m->b[content_type].Key - pch;
402 else if (!strncasecmp(header, "Content-Disposition:", 20)) {
403 memcpy (m->b[disposition].Key, &header[20], headerlen - 19);
404 m->b[disposition].Key[headerlen - 19] = '\0';
405 m->b[disposition].len = string_trim(m->b[disposition].Key);
407 m->b[content_disposition_name].len = extract_key(m->b[content_disposition_name].Key, CKEY(m->b[disposition]), HKEY("name"), '=');
408 m->b[filename].len = extract_key(m->b[filename].Key, CKEY(m->b[disposition]), HKEY("filename"), '=');
409 pch = strchr(m->b[disposition].Key, ';');
410 if (pch != NULL) *pch = '\0';
411 m->b[disposition].len = string_trim(m->b[disposition].Key);
413 else if (!strncasecmp(header, "Content-ID:", 11)) {
414 memcpy(m->b[id].Key, &header[11], headerlen - 11);
415 m->b[id].Key[headerlen - 11] = '\0';
416 string_trim(m->b[id].Key);
417 m->b[id].len = stripallbut(m->b[id].Key, '<', '>');
419 else if (!strncasecmp(header, "Content-length: ", 15)) {
422 while (isspace(*clbuf))
424 m->content_length = (size_t) atol(clbuf);
426 else if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
427 memcpy(m->b[encoding].Key, &header[26], headerlen - 26);
428 m->b[encoding].Key[headerlen - 26] = '\0';
429 m->b[encoding].len = string_trim(m->b[encoding].Key);
434 if ((headerlen + buflen + 2) < SIZ) {
435 memcpy(&header[headerlen], buf, buflen);
437 header[headerlen] = '\0';
439 if (ptr >= content_end) {
442 } while ((!IsEmptyStr(buf)) && (*ptr != 0));
444 m->is_multipart = m->b[boundary].len != 0;
445 *pcontent_start = ptr;
451 static int IsAsciiEncoding(interesting_mime_headers *m) {
453 if ((m->b[encoding].len != 0) &&
454 (strcasecmp(m->b[encoding].Key, "base64") == 0))
456 if ((m->b[encoding].len != 0) &&
457 (strcmp(m->b[encoding].Key, "quoted-printable") == 0))
463 static char *FindNextContent(char *ptr,
465 interesting_mime_headers *SubMimeHeaders,
466 interesting_mime_headers *m)
471 if (IsAsciiEncoding(SubMimeHeaders)) {
475 // ok, if we have a content length of the mime part,
476 // try skipping the content on the search for the next
477 // boundary. since we don't trust the content_length
478 // to be all accurate, and suspect it to lose one digit
479 // per line with a line length of 80 chars, we need
480 // to start searching a little before..
482 if ((SubMimeHeaders->content_length != -1) && (SubMimeHeaders->content_length > 10)) {
486 lines = SubMimeHeaders->content_length / 80;
487 pptr = ptr + SubMimeHeaders->content_length - lines - 10;
488 if (pptr < content_end)
492 next_boundary = strstr(ptr, m->b[startary].Key);
497 // ok, if we have a content length of the mime part,
498 // try skipping the content on the search for the next
499 // boundary. since we don't trust the content_length
500 // to be all accurate, start searching a little before..
502 if ((SubMimeHeaders->content_length != -1) && (SubMimeHeaders->content_length > 10)) {
504 pptr = ptr + SubMimeHeaders->content_length - 10;
505 if (pptr < content_end)
509 srch = next_boundary = NULL;
510 for (srch = memchr(ptr, '-', content_end - ptr);
511 (srch != NULL) && (srch < content_end);
512 srch = memchr(srch, '-', content_end - srch))
514 if (!memcmp(srch, m->b[startary].Key, m->b[startary].len)) {
515 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;
552 // If this is a multipart message, then recursively process it
555 if (m->is_multipart) {
557 // Tell the client about this message's multipartedness
558 if (PreMultiPartCallBack != NULL) {
559 PreMultiPartCallBack("",
564 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,
684 // If it's not a multipart message, then do something with it
688 length = content_end - part_start;
689 ptr = part_end = content_end;
691 /* The following code will truncate the MIME part to the size
692 * specified by the Content-length: header. We have commented it
693 * out because these headers have a tendency to be wrong.
695 * if ( (content_length > 0) && (length > content_length) ) {
696 * length = content_length;
700 /* Sometimes the "name" field is tacked on to Content-type,
701 * and sometimes it's tacked on to Content-disposition. Use
702 * 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
731 if (!strcasecmp(&m->b[content_type].Key[0], "message/rfc822")) {
733 if (PreMultiPartCallBack != NULL) {
734 PreMultiPartCallBack("",
739 m->b[content_type].Key,
746 if (CallBack != NULL) {
747 if (strlen(partnum) > 0) {
748 snprintf(nested_partnum,
749 sizeof nested_partnum,
754 snprintf(nested_partnum,
755 sizeof nested_partnum,
758 the_mime_parser(nested_partnum,
762 PreMultiPartCallBack,
763 PostMultiPartCallBack,
768 if (PostMultiPartCallBack != NULL) {
769 PostMultiPartCallBack("",
774 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();
811 if (!parse_MimeHeaders(m, &content_start, content_end)) {
813 recurseable_mime_parser(partnum,
814 content_start, content_end,
816 PreMultiPartCallBack,
817 PostMultiPartCallBack,
826 // Entry point for the MIME parser.
827 // (This function expects to be fed HEADERS + CONTENT)
828 // Note: NULL can be supplied as content_end; in this case, the message is
829 // considered to have ended when the parser encounters a 0x00 byte.
830 void mime_parser(char *content_start,
832 MimeParserCallBackType CallBack,
833 MimeParserCallBackType PreMultiPartCallBack,
834 MimeParserCallBackType PostMultiPartCallBack,
838 the_mime_parser("", content_start, content_end,
840 PreMultiPartCallBack,
841 PostMultiPartCallBack,
842 userdata, dont_decode);
846 typedef struct _MimeGuess {
850 const char *MimeString;
853 MimeGuess MyMimes [] = {
881 const char *GuessMimeType(const char *data, size_t dlen) {
884 while (MyMimes[MimeIndex].PatternLen != 0) {
885 if ((MyMimes[MimeIndex].PatternLen +
886 MyMimes[MimeIndex].PatternOffset < dlen) &&
887 strncmp(MyMimes[MimeIndex].Pattern,
888 &data[MyMimes[MimeIndex].PatternOffset],
889 MyMimes[MimeIndex].PatternLen) == 0)
891 return MyMimes[MimeIndex].MimeString;
896 * ok, our simple minded algorythm didn't find anything,
897 * let the big chegger try it, he wil default to application/octet-stream
899 return (xdg_mime_get_mime_type_for_data(data, dlen));
903 const char* GuessMimeByFilename(const char *what, size_t len) {
904 // we know some hardcoded on our own, try them...
905 if ((len > 3) && !strncasecmp(&what[len - 4], ".gif", 4))
907 else if ((len > 2) && !strncasecmp(&what[len - 3], ".js", 3))
908 return "text/javascript";
909 else if ((len > 3) && !strncasecmp(&what[len - 4], ".txt", 4))
911 else if ((len > 3) && !strncasecmp(&what[len - 4], ".css", 4))
913 else if ((len > 3) && !strncasecmp(&what[len - 4], ".htc", 4))
914 return "text/x-component";
915 else if ((len > 3) && !strncasecmp(&what[len - 4], ".jpg", 4))
917 else if ((len > 4) && !strncasecmp(&what[len - 5], ".jpeg", 5))
919 else if ((len > 3) && !strncasecmp(&what[len - 4], ".png", 4))
921 else if ((len > 3) && !strncasecmp(&what[len - 4], ".ico", 4))
922 return "image/x-icon";
923 else if ((len > 3) && !strncasecmp(&what[len - 4], ".vcf", 4))
924 return "text/x-vcard";
925 else if ((len > 4) && !strncasecmp(&what[len - 5], ".html", 5))
927 else if ((len > 3) && !strncasecmp(&what[len - 4], ".htm", 4))
929 else if ((len > 3) && !strncasecmp(&what[len - 4], ".wml", 4))
930 return "text/vnd.wap.wml";
931 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmls", 5))
932 return "text/vnd.wap.wmlscript";
933 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmlc", 5))
934 return "application/vnd.wap.wmlc";
935 else if ((len > 5) && !strncasecmp(&what[len - 6], ".wmlsc", 6))
936 return "application/vnd.wap.wmlscriptc";
937 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wbmp", 5))
938 return "image/vnd.wap.wbmp";
940 // and let xdgmime do the fallback.
941 return xdg_mime_get_mime_type_from_file_name(what);
944 static HashList *IconHash = NULL;
946 typedef struct IconName IconName;
954 static void DeleteIcon(void *IconNamePtr) {
955 IconName *Icon = (IconName*) IconNamePtr;
956 free(Icon->FlatName);
957 free(Icon->FileName);
962 #define GENSTR "x-generic"
963 #define IGNORE_PREFIX_1 "gnome-mime"
964 int LoadIconDir(const char *DirName) {
966 struct dirent *filedir_entry;
971 filedir = opendir (DirName);
972 IconHash = NewHash(1, NULL);
973 if (filedir == NULL) {
977 while ((filedir_entry = readdir(filedir))) {
980 #ifdef _DIRENT_HAVE_D_NAMLEN
981 d_namelen = filedir_entry->d_namlen;
983 d_namelen = strlen(filedir_entry->d_name);
985 d_without_ext = d_namelen;
986 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
988 if ((d_without_ext == 0) || (d_namelen < 3))
991 if ((sizeof(IGNORE_PREFIX_1) < d_namelen) &&
992 (strncmp(IGNORE_PREFIX_1,
993 filedir_entry->d_name,
994 sizeof(IGNORE_PREFIX_1) - 1) == 0)) {
995 PStart = filedir_entry->d_name + sizeof(IGNORE_PREFIX_1);
996 d_without_ext -= sizeof(IGNORE_PREFIX_1);
999 PStart = filedir_entry->d_name;
1001 Icon = malloc(sizeof(IconName));
1003 Icon->FileName = malloc(d_namelen + 1);
1004 memcpy(Icon->FileName, filedir_entry->d_name, d_namelen + 1);
1006 Icon->FlatName = malloc(d_without_ext + 1);
1007 memcpy(Icon->FlatName, PStart, d_without_ext);
1008 Icon->FlatName[d_without_ext] = '\0';
1009 // Try to find Minor type in image-jpeg
1010 MinorPtr = strchr(Icon->FlatName, '-');
1011 if (MinorPtr != NULL) {
1013 MinorLen = 1 + d_without_ext - (MinorPtr - Icon->FlatName + 1);
1014 if ((MinorLen == sizeof(GENSTR)) &&
1015 (strncmp(MinorPtr + 1, GENSTR, sizeof(GENSTR)) == 0)) {
1016 // ok, we found a generic filename. cut the generic.
1018 d_without_ext = d_without_ext - (MinorPtr - Icon->FlatName);
1020 else { // Map the major / minor separator to /
1025 Put(IconHash, Icon->FlatName, d_without_ext, Icon, DeleteIcon);
1032 const char *GetIconFilename(char *MimeType, size_t len) {
1036 if (IconHash == NULL) {
1040 GetHash(IconHash, MimeType, len, &vIcon), Icon = (IconName*) vIcon;
1041 // didn't find the exact mimetype? try major only.
1044 pMinor = strchr(MimeType, '/');
1045 if (pMinor != NULL) {
1047 GetHash(IconHash, MimeType, pMinor - MimeType, &vIcon),
1048 Icon = (IconName*) vIcon;
1055 return Icon->FileName;
1059 void ShutDownLibCitadelMime(void) {
1060 DeleteHash(&IconHash);