41#include <netinet/in.h>
46#warning Building xdgmime without MMAP support. Binary "mime.cache" files will not be used.
56#define MAX(a,b) ((a) > (b) ? (a) : (b))
72#define MAP_FAILED ((void *) -1)
75#define MAJOR_VERSION 1
76#define MINOR_VERSION_MIN 1
77#define MINOR_VERSION_MAX 2
88#define GET_UINT16(cache,offset) (ntohs(*(xdg_uint16_t*)((cache) + (offset))))
89#define GET_UINT32(cache,offset) (ntohl(*(xdg_uint32_t*)((cache) + (offset))))
125 fd = open (file_name, O_RDONLY|_O_BINARY, 0);
126 }
while (fd == -1 && errno == EINTR);
131 if (fstat (fd, &st) < 0 || st.st_size < 4)
134 buffer = (
char *) mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
136 if (buffer == MAP_FAILED)
139 minor = GET_UINT16 (buffer, 2);
141 if (GET_UINT16 (buffer, 0) != MAJOR_VERSION ||
142 (minor < MINOR_VERSION_MIN ||
143 minor > MINOR_VERSION_MAX))
145 munmap (buffer, st.st_size);
151 cache->
minor = minor;
154 cache->
size = st.st_size;
166cache_magic_matchlet_compare_to_data (
XdgMimeCache *cache,
179 for (i = range_start; i < range_start + range_length; i++)
181 int valid_matchlet = TRUE;
183 if (i + data_length > len)
188 for (j = 0; j < data_length; j++)
190 if ((((
unsigned char *)cache->
buffer)[data_offset + j] & ((
unsigned char *)cache->
buffer)[mask_offset + j]) !=
191 ((((
unsigned char *) data)[j + i]) & ((
unsigned char *)cache->
buffer)[mask_offset + j]))
193 valid_matchlet = FALSE;
200 valid_matchlet = memcmp(cache->
buffer + data_offset, (
unsigned char *)data + i, data_length) == 0;
221 if (cache_magic_matchlet_compare_to_data (cache, offset, data, len))
226 for (i = 0; i < n_children; i++)
228 if (cache_magic_matchlet_compare (cache, child_offset + 32 * i,
251 for (i = 0; i < n_matchlets; i++)
253 if (cache_magic_matchlet_compare (cache, matchlet_offset + i * 32,
258 return cache->
buffer + mimetype_offset;
270 const char *mime_types[],
281 list_offset = GET_UINT32 (cache->
buffer, 24);
282 n_entries = GET_UINT32 (cache->
buffer, list_offset);
283 offset = GET_UINT32 (cache->
buffer, list_offset + 8);
285 for (j = 0; j < n_entries; j++)
289 match = cache_magic_compare_to_data (cache, offset + 16 * j,
296 const char *non_match;
298 mimetype_offset = GET_UINT32 (cache->
buffer, offset + 16 * j + 4);
299 non_match = cache->
buffer + mimetype_offset;
301 for (n = 0; n < n_mime_types; n++)
305 mime_types[n] = NULL;
314cache_alias_lookup (
const char *alias)
317 int i, min, max, mid, cmp;
330 mid = (min + max) / 2;
332 offset = GET_UINT32 (cache->
buffer, list_offset + 4 + 8 * mid);
333 ptr = cache->
buffer + offset;
334 cmp = strcmp (ptr, alias);
342 offset = GET_UINT32 (cache->
buffer, list_offset + 4 + 8 * mid + 4);
343 return cache->
buffer + offset;
357cache_glob_lookup_literal (
const char *file_name,
358 const char *mime_types[],
360 int case_sensitive_check)
363 int i, min, max, mid, cmp;
376 mid = (min + max) / 2;
378 offset = GET_UINT32 (cache->
buffer, list_offset + 4 + 12 * mid);
379 ptr = cache->
buffer + offset;
380 cmp = strcmp (ptr, file_name);
388 int weight = GET_UINT32 (cache->
buffer, list_offset + 4 + 12 * mid + 8);
389 int case_sensitive = weight & 0x100;
390 weight = weight & 0xff;
392 if (case_sensitive_check || !case_sensitive)
394 offset = GET_UINT32 (cache->
buffer, list_offset + 4 + 12 * mid + 4);
395 mime_types[0] = (
const char *)(cache->
buffer + offset);
408cache_glob_lookup_fnmatch (
const char *file_name,
411 int case_sensitive_check)
413 const char *mime_type;
426 for (j = 0; j < n_entries && n < n_mime_types; j++)
429 xdg_uint32_t mimetype_offset = GET_UINT32 (cache->
buffer, list_offset + 4 + 12 * j + 4);
430 int weight = GET_UINT32 (cache->
buffer, list_offset + 4 + 12 * j + 8);
431 int case_sensitive = weight & 0x100;
432 weight = weight & 0xff;
433 ptr = cache->
buffer + offset;
434 mime_type = cache->
buffer + mimetype_offset;
435 if (case_sensitive_check || !case_sensitive)
438 if (fnmatch (ptr, file_name, 0) == 0)
440 mime_types[n].
mime = mime_type;
441 mime_types[n].
weight = weight;
458 const char *file_name,
460 int case_sensitive_check,
472 int min, max, mid, n, i;
474 character = file_name[len - 1];
476 assert (character != 0);
482 mid = (min + max) / 2;
483 match_char = GET_UINT32 (cache->
buffer, offset + 12 * mid);
484 if (match_char < character)
486 else if (match_char > character)
492 n_children = GET_UINT32 (cache->
buffer, offset + 12 * mid + 4);
493 child_offset = GET_UINT32 (cache->
buffer, offset + 12 * mid + 8);
497 n = cache_glob_node_lookup_suffix (cache,
498 n_children, child_offset,
500 case_sensitive_check,
507 while (n < n_mime_types && i < n_children)
509 match_char = GET_UINT32 (cache->
buffer, child_offset + 12 * i);
513 mimetype_offset = GET_UINT32 (cache->
buffer, child_offset + 12 * i + 4);
514 weight = GET_UINT32 (cache->
buffer, child_offset + 12 * i + 8);
515 case_sensitive = weight & 0x100;
516 weight = weight & 0xff;
518 if (case_sensitive_check || !case_sensitive)
520 mime_types[n].
mime = cache->
buffer + mimetype_offset;
521 mime_types[n].
weight = weight;
534cache_glob_lookup_suffix (
const char *file_name,
550 n = cache_glob_node_lookup_suffix (cache,
563static int compare_mime_weight (
const void *a,
const void *b)
571#define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
573ascii_tolower (
const char *str)
577 lower = strdup (str);
582 *p++ = ISUPPER (c) ? c -
'A' +
'a' : c;
588cache_glob_lookup_file_name (
const char *file_name,
589 const char *mime_types[],
599 assert (file_name != NULL && n_mime_types > 0);
603 lower_case = ascii_tolower (file_name);
605 n = cache_glob_lookup_literal (lower_case, mime_types, n_mime_types, FALSE);
612 n = cache_glob_lookup_literal (file_name, mime_types, n_mime_types, TRUE);
619 len = strlen (file_name);
620 n = cache_glob_lookup_suffix (lower_case, len, FALSE, mimes, n_mimes);
622 n = cache_glob_lookup_suffix (file_name, len, TRUE, mimes, n_mimes);
626 n = cache_glob_lookup_fnmatch (lower_case, mimes, n_mimes, FALSE);
628 n = cache_glob_lookup_fnmatch (file_name, mimes, n_mimes, TRUE);
632 qsort (mimes, n,
sizeof (
MimeWeight), compare_mime_weight);
634 if (n_mime_types < n)
637 for (i = 0; i < n; i++)
638 mime_types[i] = mimes[i].mime;
655 offset = GET_UINT32 (cache->
buffer, 24);
656 max_extent = MAX (max_extent, GET_UINT32 (cache->
buffer, offset + 4));
663cache_get_mime_type_for_data (
const void *data,
666 const char *mime_types[],
669 const char *mime_type;
681 match = cache_magic_lookup_data (cache, data, len, &prio,
682 mime_types, n_mime_types);
691 *result_prio = priority;
696 for (n = 0; n < n_mime_types; n++)
699 return mime_types[n];
707 for (n = 0; n < n_mime_types; n++)
710 return mime_types[n];
721 return cache_get_mime_type_for_data (data, len, result_prio, NULL, 0);
726 struct stat *statbuf)
728 const char *mime_type;
729 const char *mime_types[10];
735 const char *base_name;
738 if (file_name == NULL)
745 n = cache_glob_lookup_file_name (base_name, mime_types, 10);
748 return mime_types[0];
752 if (stat (file_name, &buf) != 0)
753 return XDG_MIME_TYPE_UNKNOWN;
758 if (statbuf->st_size == 0)
759 return XDG_MIME_TYPE_EMPTY;
761 if (!S_ISREG (statbuf->st_mode))
762 return XDG_MIME_TYPE_UNKNOWN;
768 data = malloc (max_extent);
770 return XDG_MIME_TYPE_UNKNOWN;
772 file = fopen (file_name,
"r");
776 return XDG_MIME_TYPE_UNKNOWN;
779 bytes_read = fread (data, 1, max_extent, file);
784 return XDG_MIME_TYPE_UNKNOWN;
787 mime_type = cache_get_mime_type_for_data (data, bytes_read, NULL,
802 const char *mime_type;
804 if (cache_glob_lookup_file_name (file_name, &mime_type, 1))
807 return XDG_MIME_TYPE_UNKNOWN;
812 const char *mime_types[],
815 return cache_glob_lookup_file_name (file_name, mime_types, n_mime_types);
820ends_with (
const char *str,
826 length = strlen (str);
827 suffix_length = strlen (suffix);
828 if (length < suffix_length)
831 if (strcmp (str + length - suffix_length, suffix) == 0)
838is_super_type (
const char *mime)
840 return ends_with (mime,
"/*");
848 const char *umime, *ubase;
850 int i, j, min, max, med, cmp;
855 if (strcmp (umime, ubase) == 0)
863 if (is_super_type (ubase) &&
869 if (strcmp (ubase,
"text/plain") == 0 &&
870 strncmp (umime,
"text/", 5) == 0)
873 if (strcmp (ubase,
"application/octet-stream") == 0 &&
874 strncmp (umime,
"inode/", 6) != 0)
891 offset = GET_UINT32 (cache->
buffer, list_offset + 4 + 8 * med);
892 cmp = strcmp (cache->
buffer + offset, umime);
899 offset = GET_UINT32 (cache->
buffer, list_offset + 4 + 8 * med + 4);
900 n_parents = GET_UINT32 (cache->
buffer, offset);
902 for (j = 0; j < n_parents; j++)
904 parent_offset = GET_UINT32 (cache->
buffer, offset + 4 + 4 * j);
922 lookup = cache_alias_lookup (mime);
934 char *all_parents[128];
947 for (j = 0; j < n_entries; j++)
950 xdg_uint32_t parents_offset = GET_UINT32 (cache->
buffer, list_offset + 4 + 8 * j + 4);
952 if (strcmp (cache->
buffer + mimetype_offset, mime) == 0)
957 for (k = 0; k < n_parents && p < 127; k++)
959 parent_mime_offset = GET_UINT32 (cache->
buffer, parents_offset + 4 + 4 * k);
964 for (l = 0; l < p; l++)
966 if (strcmp (all_parents[l], cache->
buffer + parent_mime_offset) == 0)
971 all_parents[p++] = cache->
buffer + parent_mime_offset;
978 all_parents[p++] = NULL;
980 result = (
char **) malloc (p *
sizeof (
char *));
981 memcpy (result, all_parents, p *
sizeof (
char *));
987cache_lookup_icon (
const char *mime,
int header)
990 int i, min, max, mid, cmp;
1000 max = n_entries - 1;
1003 mid = (min + max) / 2;
1005 offset = GET_UINT32 (cache->
buffer, list_offset + 4 + 8 * mid);
1006 ptr = cache->
buffer + offset;
1007 cmp = strcmp (ptr, mime);
1015 offset = GET_UINT32 (cache->
buffer, list_offset + 4 + 8 * mid + 4);
1016 return cache->
buffer + offset;
1027 return cache_lookup_icon (mime, 36);
1033 return cache_lookup_icon (mime, 32);
1047 character = GET_UINT32 (cache->
buffer, offset);
1048 mime_offset = GET_UINT32 (cache->
buffer, offset + 4);
1049 n_children = GET_UINT32 (cache->
buffer, offset + 8);
1050 child_offset = GET_UINT32 (cache->
buffer, offset + 12);
1051 for (i = 0; i < depth; i++)
1053 printf (
"%c", character);
1055 printf (
" - %s", cache->
buffer + mime_offset);
1059 for (i = 0; i < n_children; i++)
1060 dump_glob_node (cache, child_offset + 20 * i, depth + 1);
1074 list_offset = GET_UINT32 (cache->
buffer, 16);
1075 n_entries = GET_UINT32 (cache->
buffer, list_offset);
1076 offset = GET_UINT32 (cache->
buffer, list_offset + 4);
1077 for (j = 0; j < n_entries; j++)
1078 dump_glob_node (cache, offset + 20 * j, 0);
int _xdg_mime_mime_type_equal(const char *mime_a, const char *mime_b)
int xdg_mime_media_type_equal(const char *mime_a, const char *mime_b)
const char * xdg_mime_unalias_mime_type(const char *mime_type)
const char * _xdg_mime_cache_get_icon(const char *mime)
int _xdg_mime_cache_get_mime_types_from_file_name(const char *file_name, const char *mime_types[], int n_mime_types)
XdgMimeCache * _xdg_mime_cache_new_from_file(const char *file_name)
int _xdg_mime_cache_get_max_buffer_extents(void)
const char * _xdg_mime_cache_get_mime_type_from_file_name(const char *file_name)
XdgMimeCache * _xdg_mime_cache_ref(XdgMimeCache *cache)
void _xdg_mime_cache_glob_dump(void)
const char * _xdg_mime_cache_get_mime_type_for_file(const char *file_name, struct stat *statbuf)
const char * _xdg_mime_cache_unalias_mime_type(const char *mime)
const char * _xdg_mime_cache_get_mime_type_for_data(const void *data, size_t len, int *result_prio)
int _xdg_mime_cache_mime_type_subclass(const char *mime, const char *base)
const char * _xdg_mime_cache_get_generic_icon(const char *mime)
void _xdg_mime_cache_unref(XdgMimeCache *cache)
char ** _xdg_mime_cache_list_mime_parents(const char *mime)
const char * _xdg_binary_or_text_fallback(const void *data, size_t len)
const char * _xdg_get_base_name(const char *file_name)
int _xdg_utf8_validate(const char *source)
unsigned int xdg_uint32_t
unsigned int xdg_unichar_t