1 // This is the MIME parser for Citadel.
3 // Copyright (c) 1998-2022 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) {
107 * For non-multipart messages, we need to generate a quickie partnum of "1"
108 * to return to callback functions. Some callbacks demand it.
110 char *fixed_partnum(char *supplied_partnum) {
111 if (supplied_partnum == NULL) return "1";
112 if (strlen(supplied_partnum)==0) return "1";
113 return supplied_partnum;
117 static inline unsigned int _decode_hex(const char *Source) {
118 unsigned int ret = '?';
119 unsigned char LO_NIBBLE;
120 unsigned char HI_NIBBLE;
122 HI_NIBBLE = FromHexTable[(unsigned char) *Source];
123 LO_NIBBLE = FromHexTable[(unsigned char) *(Source+1)];
125 if ((LO_NIBBLE == 0xFF) || (LO_NIBBLE == 0xFF))
129 ret = ret | LO_NIBBLE;
133 unsigned int decode_hex(char *Source) {return _decode_hex(Source);}
136 * Convert "quoted-printable" to binary. Returns number of bytes decoded.
137 * according to RFC2045 section 6.7
139 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
141 int decoded_length = 0;
144 while (pos < sourcelen) {
145 if (*(encoded + pos) == '=') {
147 if (*(encoded + pos) == '\n') {
150 else if (*(encoded + pos) == '\r') {
152 if (*(encoded + pos) == '\n')
156 ch = _decode_hex(&encoded[pos]);
158 decoded[decoded_length++] = ch;
162 decoded[decoded_length++] = encoded[pos];
166 decoded[decoded_length] = 0;
167 return(decoded_length);
172 * Given a message or message-part body and a length, handle any necessary
173 * decoding and pass the request up the stack.
175 void mime_decode(char *partnum,
176 char *part_start, size_t length,
177 char *content_type, char *charset, char *encoding,
180 char *name, char *filename,
181 MimeParserCallBackType CallBack,
182 MimeParserCallBackType PreMultiPartCallBack,
183 MimeParserCallBackType PostMultiPartCallBack,
188 size_t bytes_decoded = 0;
190 // Some encodings aren't really encodings
191 if (!strcasecmp(encoding, "7bit"))
193 if (!strcasecmp(encoding, "8bit"))
195 if (!strcasecmp(encoding, "binary"))
197 if (!strcasecmp(encoding, "ISO-8859-1"))
200 // If this part is not encoded, send as-is
201 if ( (strlen(encoding) == 0) || (dont_decode)) {
202 if (CallBack != NULL) {
205 fixed_partnum(partnum),
218 // Fail silently if we hit an unknown encoding.
219 if ((strcasecmp(encoding, "base64")) && (strcasecmp(encoding, "quoted-printable"))) {
223 // Allocate a buffer for the decoded data. The output buffer is slightly
224 // larger than the input buffer; this assumes that the decoded data
225 // will never be significantly larger than the encoded data. This is a
226 // safe assumption with base64, uuencode, and quoted-printable.
227 decoded = malloc(length + 32768);
228 if (decoded == NULL) {
232 if (!strcasecmp(encoding, "base64")) {
233 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
235 else if (!strcasecmp(encoding, "quoted-printable")) {
236 bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
239 if (bytes_decoded > 0) if (CallBack != NULL) {
240 char encoding_buf[SIZ];
242 strcpy(encoding_buf, "binary");
245 fixed_partnum(partnum),
260 * this is the extract of mime_decode which can be called if 'dont_decode' was set;
261 * to save the cpu intense process of decoding to the time when it realy wants the content.
263 * - > 0 we decoded something, its on *decoded, you need to free it.
264 * - = 0 no need to decode stuff. *decoded will be NULL.
265 * - < 0 an error occured, either an unknown encoding, or alloc failed. no need to free.
267 int mime_decode_now (char *part_start,
271 size_t *bytes_decoded)
275 /* Some encodings aren't really encodings */
276 if (!strcasecmp(encoding, "7bit"))
278 if (!strcasecmp(encoding, "8bit"))
280 if (!strcasecmp(encoding, "binary"))
283 /* If this part is not encoded, send as-is */
284 if (strlen(encoding) == 0) {
289 /* Fail if we hit an unknown encoding. */
290 if ((strcasecmp(encoding, "base64"))
291 && (strcasecmp(encoding, "quoted-printable"))) {
296 * Allocate a buffer for the decoded data. The output buffer is slightly
297 * larger than the input buffer; this assumes that the decoded data
298 * will never be significantly larger than the encoded data. This is a
299 * safe assumption with base64, uuencode, and quoted-printable.
301 *decoded = malloc(length + 32768);
302 if (decoded == NULL) {
306 if (!strcasecmp(encoding, "base64")) {
307 *bytes_decoded = CtdlDecodeBase64(*decoded, part_start, length);
310 else if (!strcasecmp(encoding, "quoted-printable")) {
311 *bytes_decoded = CtdlDecodeQuotedPrintable(*decoded, part_start, length);
317 typedef enum _eIntMimeHdrs {
325 content_disposition_name,
329 eMax /* don't move ! */
332 typedef struct _CBufStr {
337 typedef struct _interesting_mime_headers {
341 } interesting_mime_headers;
344 static void FlushInterestingMimes(interesting_mime_headers *m)
348 for (i = 0; i < eMax; i++) {
349 m->b[i].Key[0] = '\0';
352 m->content_length = -1;
354 static interesting_mime_headers *InitInterestingMimes(void)
356 interesting_mime_headers *m;
357 m = (interesting_mime_headers*) malloc( sizeof(interesting_mime_headers));
359 FlushInterestingMimes(m);
365 static long parse_MimeHeaders(interesting_mime_headers *m,
366 char** pcontent_start,
376 /* Learn interesting things from the headers */
377 ptr = *pcontent_start;
381 ptr = memreadlinelen(ptr, buf, SIZ, &buflen);
383 for (i = 0; i < buflen; ++i) {
384 if (isspace(buf[i])) {
389 if (!isspace(buf[0]) && (headerlen > 0)) {
390 if (!strncasecmp(header, "Content-type:", 13)) {
391 memcpy (m->b[content_type].Key, &header[13], headerlen - 12);
392 m->b[content_type].Key[headerlen - 12] = '\0';
393 m->b[content_type].len = string_trim (m->b[content_type].Key);
395 m->b[content_type_name].len = extract_key(m->b[content_type_name].Key, CKEY(m->b[content_type]), HKEY("name"), '=');
396 m->b[charset].len = extract_key(m->b[charset].Key, CKEY(m->b[content_type]), HKEY("charset"), '=');
397 m->b[boundary].len = extract_key(m->b[boundary].Key, header, headerlen, HKEY("boundary"), '=');
399 /* Deal with weird headers */
400 pch = strchr(m->b[content_type].Key, ' ');
403 m->b[content_type].len = m->b[content_type].Key - pch;
405 pch = strchr(m->b[content_type].Key, ';');
408 m->b[content_type].len = m->b[content_type].Key - pch;
411 else if (!strncasecmp(header, "Content-Disposition:", 20)) {
412 memcpy (m->b[disposition].Key, &header[20], headerlen - 19);
413 m->b[disposition].Key[headerlen - 19] = '\0';
414 m->b[disposition].len = string_trim(m->b[disposition].Key);
416 m->b[content_disposition_name].len = extract_key(m->b[content_disposition_name].Key, CKEY(m->b[disposition]), HKEY("name"), '=');
417 m->b[filename].len = extract_key(m->b[filename].Key, CKEY(m->b[disposition]), HKEY("filename"), '=');
418 pch = strchr(m->b[disposition].Key, ';');
419 if (pch != NULL) *pch = '\0';
420 m->b[disposition].len = string_trim(m->b[disposition].Key);
422 else if (!strncasecmp(header, "Content-ID:", 11)) {
423 memcpy(m->b[id].Key, &header[11], headerlen - 11);
424 m->b[id].Key[headerlen - 11] = '\0';
425 string_trim(m->b[id].Key);
426 m->b[id].len = stripallbut(m->b[id].Key, '<', '>');
428 else if (!strncasecmp(header, "Content-length: ", 15)) {
431 while (isspace(*clbuf))
433 m->content_length = (size_t) atol(clbuf);
435 else if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
436 memcpy(m->b[encoding].Key, &header[26], headerlen - 26);
437 m->b[encoding].Key[headerlen - 26] = '\0';
438 m->b[encoding].len = string_trim(m->b[encoding].Key);
443 if ((headerlen + buflen + 2) < SIZ) {
444 memcpy(&header[headerlen], buf, buflen);
446 header[headerlen] = '\0';
448 if (ptr >= content_end) {
451 } while ((!IsEmptyStr(buf)) && (*ptr != 0));
453 m->is_multipart = m->b[boundary].len != 0;
454 *pcontent_start = ptr;
460 static int IsAsciiEncoding(interesting_mime_headers *m)
463 if ((m->b[encoding].len != 0) &&
464 (strcasecmp(m->b[encoding].Key, "base64") == 0))
466 if ((m->b[encoding].len != 0) &&
467 (strcmp(m->b[encoding].Key, "quoted-printable") == 0))
473 static char *FindNextContent(char *ptr,
475 interesting_mime_headers *SubMimeHeaders,
476 interesting_mime_headers *m)
481 if (IsAsciiEncoding(SubMimeHeaders)) {
486 * ok, if we have a content length of the mime part,
487 * try skipping the content on the search for the next
488 * boundary. since we don't trust the content_length
489 * to be all accurate, and suspect it to lose one digit
490 * per line with a line length of 80 chars, we need
491 * to start searching a little before..
494 if ((SubMimeHeaders->content_length != -1) &&
495 (SubMimeHeaders->content_length > 10))
500 lines = SubMimeHeaders->content_length / 80;
501 pptr = ptr + SubMimeHeaders->content_length - lines - 10;
502 if (pptr < content_end)
506 next_boundary = strstr(ptr, m->b[startary].Key);
512 * ok, if we have a content length of the mime part,
513 * try skipping the content on the search for the next
514 * boundary. since we don't trust the content_length
515 * to be all accurate, start searching a little before..
518 if ((SubMimeHeaders->content_length != -1) &&
519 (SubMimeHeaders->content_length > 10))
522 pptr = ptr + SubMimeHeaders->content_length - 10;
523 if (pptr < content_end)
528 srch = next_boundary = NULL;
529 for (srch = memchr(ptr, '-', content_end - ptr);
530 (srch != NULL) && (srch < content_end);
531 srch = memchr(srch, '-', content_end - srch))
537 next_boundary = srch;
545 return next_boundary;
549 * Break out the components of a multipart message
550 * (This function expects to be fed HEADERS + CONTENT)
551 * Note: NULL can be supplied as content_end; in this case, the message is
552 * considered to have ended when the parser encounters a 0x00 byte.
554 static void recurseable_mime_parser(char *partnum,
555 char *content_start, char *content_end,
556 MimeParserCallBackType CallBack,
557 MimeParserCallBackType PreMultiPartCallBack,
558 MimeParserCallBackType PostMultiPartCallBack,
561 interesting_mime_headers *m)
563 interesting_mime_headers *SubMimeHeaders;
566 char *part_end = NULL;
567 char *evaluate_crlf_ptr = NULL;
569 char nested_partnum[256];
572 CBufStr *chosen_name;
575 /* If this is a multipart message, then recursively process it */
578 if (m->is_multipart) {
580 /* Tell the client about this message's multipartedness */
581 if (PreMultiPartCallBack != NULL) {
582 PreMultiPartCallBack("",
587 m->b[content_type].Key,
595 /* Figure out where the boundaries are */
596 m->b[startary].len = snprintf(m->b[startary].Key, SIZ, "--%s", m->b[boundary].Key);
597 SubMimeHeaders = InitInterestingMimes ();
599 while ((*ptr == '\r') || (*ptr == '\n')) ptr ++;
601 if (strncmp(ptr, m->b[startary].Key, m->b[startary].len) == 0)
602 ptr += m->b[startary].len;
604 while ((*ptr == '\r') || (*ptr == '\n')) ptr ++;
611 if (parse_MimeHeaders(SubMimeHeaders, &ptr, content_end) != 0)
613 if ((ptr - optr > 2) && (*(ptr - 2) == '\r')) {
619 next_boundary = FindNextContent(ptr, content_end, SubMimeHeaders, m);
620 if ((next_boundary != NULL) && (next_boundary - part_start < 3)) {
621 FlushInterestingMimes(SubMimeHeaders);
625 if ( (part_start != NULL) && (next_boundary != NULL) ) {
626 part_end = next_boundary;
627 --part_end; /* omit the trailing LF */
629 --part_end; /* omit the trailing CR */
632 if (!IsEmptyStr(partnum)) {
633 snprintf(nested_partnum,
634 sizeof nested_partnum,
639 snprintf(nested_partnum,
640 sizeof nested_partnum,
643 recurseable_mime_parser(nested_partnum,
647 PreMultiPartCallBack,
648 PostMultiPartCallBack,
654 if (next_boundary != NULL) {
655 /* If we pass out of scope, don't attempt to
656 * read past the end boundary. */
657 if ((*(next_boundary + m->b[startary].len) == '-') &&
658 (*(next_boundary + m->b[startary].len + 1) == '-') ){
662 /* Set up for the next part. */
663 part_start = strstr(next_boundary, "\n");
665 /* Determine whether newlines are LF or CRLF */
666 evaluate_crlf_ptr = part_start;
668 if ((*evaluate_crlf_ptr == '\r') && (*(evaluate_crlf_ptr + 1) == '\n')) {
675 /* Advance past the LF ... now we're in the next part */
681 /* Invalid end of multipart. Bail out! */
684 FlushInterestingMimes(SubMimeHeaders);
685 } while ( (ptr < content_end) && (next_boundary != NULL) );
687 free(SubMimeHeaders);
689 if (PostMultiPartCallBack != NULL) {
690 PostMultiPartCallBack("",
695 m->b[content_type].Key,
702 } /* If it's not a multipart message, then do something with it */
706 length = content_end - part_start;
707 ptr = part_end = content_end;
709 /* The following code will truncate the MIME part to the size
710 * specified by the Content-length: header. We have commented it
711 * out because these headers have a tendency to be wrong.
713 * if ( (content_length > 0) && (length > content_length) ) {
714 * length = content_length;
718 /* Sometimes the "name" field is tacked on to Content-type,
719 * and sometimes it's tacked on to Content-disposition. Use
720 * whichever one we have.
722 if (m->b[content_disposition_name].len > m->b[content_type_name].len) {
723 chosen_name = &m->b[content_disposition_name];
726 chosen_name = &m->b[content_type_name];
729 // Ok, we've got a non-multipart part here, so do something with it.
733 m->b[content_type].Key,
736 m->b[disposition].Key,
747 * Now if it's an encapsulated message/rfc822 then we have to recurse into it
749 if (!strcasecmp(&m->b[content_type].Key[0], "message/rfc822")) {
751 if (PreMultiPartCallBack != NULL) {
752 PreMultiPartCallBack("",
757 m->b[content_type].Key,
764 if (CallBack != NULL) {
765 if (strlen(partnum) > 0) {
766 snprintf(nested_partnum,
767 sizeof nested_partnum,
772 snprintf(nested_partnum,
773 sizeof nested_partnum,
776 the_mime_parser(nested_partnum,
780 PreMultiPartCallBack,
781 PostMultiPartCallBack,
786 if (PostMultiPartCallBack != NULL) {
787 PostMultiPartCallBack("",
792 m->b[content_type].Key,
808 * Break out the components of a multipart message
809 * (This function expects to be fed HEADERS + CONTENT)
810 * Note: NULL can be supplied as content_end; in this case, the message is
811 * considered to have ended when the parser encounters a 0x00 byte.
813 void the_mime_parser(char *partnum,
814 char *content_start, char *content_end,
815 MimeParserCallBackType CallBack,
816 MimeParserCallBackType PreMultiPartCallBack,
817 MimeParserCallBackType PostMultiPartCallBack,
821 interesting_mime_headers *m;
823 /* If the caller didn't supply an endpointer, generate one by measure */
824 if (content_end == NULL) {
825 content_end = &content_start[strlen(content_start)];
828 m = InitInterestingMimes();
830 if (!parse_MimeHeaders(m, &content_start, content_end))
833 recurseable_mime_parser(partnum,
834 content_start, content_end,
836 PreMultiPartCallBack,
837 PostMultiPartCallBack,
847 * Entry point for the MIME parser.
848 * (This function expects to be fed HEADERS + CONTENT)
849 * Note: NULL can be supplied as content_end; in this case, the message is
850 * considered to have ended when the parser encounters a 0x00 byte.
852 void mime_parser(char *content_start,
854 MimeParserCallBackType CallBack,
855 MimeParserCallBackType PreMultiPartCallBack,
856 MimeParserCallBackType PostMultiPartCallBack,
861 the_mime_parser("", content_start, content_end,
863 PreMultiPartCallBack,
864 PostMultiPartCallBack,
865 userdata, dont_decode);
873 typedef struct _MimeGuess {
877 const char *MimeString;
880 MimeGuess MyMimes [] = {
908 const char *GuessMimeType(const char *data, size_t dlen)
912 while (MyMimes[MimeIndex].PatternLen != 0)
914 if ((MyMimes[MimeIndex].PatternLen +
915 MyMimes[MimeIndex].PatternOffset < dlen) &&
916 strncmp(MyMimes[MimeIndex].Pattern,
917 &data[MyMimes[MimeIndex].PatternOffset],
918 MyMimes[MimeIndex].PatternLen) == 0)
920 return MyMimes[MimeIndex].MimeString;
925 * ok, our simple minded algorythm didn't find anything,
926 * let the big chegger try it, he wil default to application/octet-stream
928 return (xdg_mime_get_mime_type_for_data(data, dlen));
932 const char* GuessMimeByFilename(const char *what, size_t len)
934 /* we know some hardcoded on our own, try them... */
935 if ((len > 3) && !strncasecmp(&what[len - 4], ".gif", 4))
937 else if ((len > 2) && !strncasecmp(&what[len - 3], ".js", 3))
938 return "text/javascript";
939 else if ((len > 3) && !strncasecmp(&what[len - 4], ".txt", 4))
941 else if ((len > 3) && !strncasecmp(&what[len - 4], ".css", 4))
943 else if ((len > 3) && !strncasecmp(&what[len - 4], ".htc", 4))
944 return "text/x-component";
945 else if ((len > 3) && !strncasecmp(&what[len - 4], ".jpg", 4))
947 else if ((len > 4) && !strncasecmp(&what[len - 5], ".jpeg", 5))
949 else if ((len > 3) && !strncasecmp(&what[len - 4], ".png", 4))
951 else if ((len > 3) && !strncasecmp(&what[len - 4], ".ico", 4))
952 return "image/x-icon";
953 else if ((len > 3) && !strncasecmp(&what[len - 4], ".vcf", 4))
954 return "text/x-vcard";
955 else if ((len > 4) && !strncasecmp(&what[len - 5], ".html", 5))
957 else if ((len > 3) && !strncasecmp(&what[len - 4], ".htm", 4))
959 else if ((len > 3) && !strncasecmp(&what[len - 4], ".wml", 4))
960 return "text/vnd.wap.wml";
961 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmls", 5))
962 return "text/vnd.wap.wmlscript";
963 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmlc", 5))
964 return "application/vnd.wap.wmlc";
965 else if ((len > 5) && !strncasecmp(&what[len - 6], ".wmlsc", 6))
966 return "application/vnd.wap.wmlscriptc";
967 else if ((len > 4) && !strncasecmp(&what[len - 5], ".wbmp", 5))
968 return "image/vnd.wap.wbmp";
970 /* and let xdgmime do the fallback. */
971 return xdg_mime_get_mime_type_from_file_name(what);
974 static HashList *IconHash = NULL;
976 typedef struct IconName IconName;
983 static void DeleteIcon(void *IconNamePtr)
985 IconName *Icon = (IconName*) IconNamePtr;
986 free(Icon->FlatName);
987 free(Icon->FileName);
992 static const char *PrintFlat(void *IconNamePtr)
994 IconName *Icon = (IconName*) IconNamePtr;
995 return Icon->FlatName;
997 static const char *PrintFile(void *IconNamePtr)
999 IconName *Icon = (IconName*) IconNamePtr;
1000 return Icon->FileName;
1004 #define GENSTR "x-generic"
1005 #define IGNORE_PREFIX_1 "gnome-mime"
1006 int LoadIconDir(const char *DirName)
1008 DIR *filedir = NULL;
1009 struct dirent *filedir_entry;
1014 filedir = opendir (DirName);
1015 IconHash = NewHash(1, NULL);
1016 if (filedir == NULL) {
1020 while ((filedir_entry = readdir(filedir)))
1024 #ifdef _DIRENT_HAVE_D_NAMLEN
1025 d_namelen = filedir_entry->d_namlen;
1027 d_namelen = strlen(filedir_entry->d_name);
1029 d_without_ext = d_namelen;
1030 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
1032 if ((d_without_ext == 0) || (d_namelen < 3))
1035 if ((sizeof(IGNORE_PREFIX_1) < d_namelen) &&
1036 (strncmp(IGNORE_PREFIX_1,
1037 filedir_entry->d_name,
1038 sizeof(IGNORE_PREFIX_1) - 1) == 0)) {
1039 PStart = filedir_entry->d_name + sizeof(IGNORE_PREFIX_1);
1040 d_without_ext -= sizeof(IGNORE_PREFIX_1);
1043 PStart = filedir_entry->d_name;
1045 Icon = malloc(sizeof(IconName));
1047 Icon->FileName = malloc(d_namelen + 1);
1048 memcpy(Icon->FileName, filedir_entry->d_name, d_namelen + 1);
1050 Icon->FlatName = malloc(d_without_ext + 1);
1051 memcpy(Icon->FlatName, PStart, d_without_ext);
1052 Icon->FlatName[d_without_ext] = '\0';
1053 /* Try to find Minor type in image-jpeg */
1054 MinorPtr = strchr(Icon->FlatName, '-');
1055 if (MinorPtr != NULL) {
1057 MinorLen = 1 + d_without_ext - (MinorPtr - Icon->FlatName + 1);
1058 if ((MinorLen == sizeof(GENSTR)) &&
1059 (strncmp(MinorPtr + 1, GENSTR, sizeof(GENSTR)) == 0)) {
1060 /* ok, we found a generic filename. cut the generic. */
1062 d_without_ext = d_without_ext - (MinorPtr - Icon->FlatName);
1064 else { /* Map the major / minor separator to / */
1069 // PrintHash(IconHash, PrintFlat, PrintFile);
1070 // printf("%s - %s\n", Icon->FlatName, Icon->FileName);
1071 Put(IconHash, Icon->FlatName, d_without_ext, Icon, DeleteIcon);
1072 // PrintHash(IconHash, PrintFlat, PrintFile);
1078 const char *GetIconFilename(char *MimeType, size_t len)
1083 if(IconHash == NULL)
1086 GetHash(IconHash, MimeType, len, &vIcon), Icon = (IconName*) vIcon;
1087 /* didn't find the exact mimetype? try major only. */
1090 pMinor = strchr(MimeType, '/');
1091 if (pMinor != NULL) {
1093 GetHash(IconHash, MimeType, pMinor - MimeType, &vIcon),
1094 Icon = (IconName*) vIcon;
1101 /*printf("Getting: [%s] == [%s] -> [%s]\n", MimeType, Icon->FlatName, Icon->FileName);*/
1102 return Icon->FileName;
1105 void ShutDownLibCitadelMime(void)
1107 DeleteHash(&IconHash);