1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* xdgmimealias.c: Private file. mmappable caches for mime data
4 * More info can be found at http://www.freedesktop.org/standards/
6 * Copyright (C) 2005 Matthias Clasen <mclasen@redhat.com>
8 * Licensed under the Academic Free License version 2.0
9 * Or under the following terms:
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 02111-1307, USA.
40 #include <netinet/in.h> /* for ntohl/ntohs */
45 #warning Building xdgmime without MMAP support. Binary "mime.info" cache files will not be used.
49 #include <sys/types.h>
51 #include "xdgmimecache.h"
52 #include "xdgmimeint.h"
55 #define MAX(a,b) ((a) > (b) ? (a) : (b))
71 #define MAP_FAILED ((void *) -1)
74 #define MAJOR_VERSION 1
75 #define MINOR_VERSION_MIN 1
76 #define MINOR_VERSION_MAX 2
87 #define GET_UINT16(cache,offset) (ntohs(*(xdg_uint16_t*)((cache) + (offset))))
88 #define GET_UINT32(cache,offset) (ntohl(*(xdg_uint32_t*)((cache) + (offset))))
91 _xdg_mime_cache_ref (XdgMimeCache *cache)
98 _xdg_mime_cache_unref (XdgMimeCache *cache)
102 if (cache->ref_count == 0)
105 munmap (cache->buffer, cache->size);
112 _xdg_mime_cache_new_from_file (const char *file_name)
114 XdgMimeCache *cache = NULL;
122 /* Open the file and map it into memory */
123 fd = open (file_name, O_RDONLY|_O_BINARY, 0);
128 if (fstat (fd, &st) < 0 || st.st_size < 4)
131 buffer = (char *) mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
133 if (buffer == MAP_FAILED)
136 minor = GET_UINT16 (buffer, 2);
138 if (GET_UINT16 (buffer, 0) != MAJOR_VERSION ||
139 (minor < MINOR_VERSION_MIN ||
140 minor > MINOR_VERSION_MAX))
142 munmap (buffer, st.st_size);
147 cache = (XdgMimeCache *) malloc (sizeof (XdgMimeCache));
150 munmap (buffer, st.st_size);
155 cache->minor = minor;
156 cache->ref_count = 1;
157 cache->buffer = buffer;
158 cache->size = st.st_size;
164 #endif /* HAVE_MMAP */
170 cache_magic_matchlet_compare_to_data (XdgMimeCache *cache,
175 xdg_uint32_t range_start = GET_UINT32 (cache->buffer, offset);
176 xdg_uint32_t range_length = GET_UINT32 (cache->buffer, offset + 4);
177 xdg_uint32_t data_length = GET_UINT32 (cache->buffer, offset + 12);
178 xdg_uint32_t data_offset = GET_UINT32 (cache->buffer, offset + 16);
179 xdg_uint32_t mask_offset = GET_UINT32 (cache->buffer, offset + 20);
183 for (i = range_start; i < range_start + range_length; i++)
185 int valid_matchlet = TRUE;
187 if (i + data_length > len)
192 for (j = 0; j < data_length; j++)
194 if ((((unsigned char *)cache->buffer)[data_offset + j] & ((unsigned char *)cache->buffer)[mask_offset + j]) !=
195 ((((unsigned char *) data)[j + i]) & ((unsigned char *)cache->buffer)[mask_offset + j]))
197 valid_matchlet = FALSE;
204 for (j = 0; j < data_length; j++)
206 if (((unsigned char *)cache->buffer)[data_offset + j] != ((unsigned char *) data)[j + i])
208 valid_matchlet = FALSE;
222 cache_magic_matchlet_compare (XdgMimeCache *cache,
227 xdg_uint32_t n_children = GET_UINT32 (cache->buffer, offset + 24);
228 xdg_uint32_t child_offset = GET_UINT32 (cache->buffer, offset + 28);
232 if (cache_magic_matchlet_compare_to_data (cache, offset, data, len))
237 for (i = 0; i < n_children; i++)
239 if (cache_magic_matchlet_compare (cache, child_offset + 32 * i,
249 cache_magic_compare_to_data (XdgMimeCache *cache,
255 xdg_uint32_t priority = GET_UINT32 (cache->buffer, offset);
256 xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, offset + 4);
257 xdg_uint32_t n_matchlets = GET_UINT32 (cache->buffer, offset + 8);
258 xdg_uint32_t matchlet_offset = GET_UINT32 (cache->buffer, offset + 12);
262 for (i = 0; i < n_matchlets; i++)
264 if (cache_magic_matchlet_compare (cache, matchlet_offset + i * 32,
269 return cache->buffer + mimetype_offset;
277 cache_magic_lookup_data (XdgMimeCache *cache,
281 const char *mime_types[],
284 xdg_uint32_t list_offset;
285 xdg_uint32_t n_entries;
292 list_offset = GET_UINT32 (cache->buffer, 24);
293 n_entries = GET_UINT32 (cache->buffer, list_offset);
294 offset = GET_UINT32 (cache->buffer, list_offset + 8);
296 for (j = 0; j < n_entries; j++)
300 match = cache_magic_compare_to_data (cache, offset + 16 * j,
306 xdg_uint32_t mimetype_offset;
307 const char *non_match;
309 mimetype_offset = GET_UINT32 (cache->buffer, offset + 16 * j + 4);
310 non_match = cache->buffer + mimetype_offset;
312 for (n = 0; n < n_mime_types; n++)
315 _xdg_mime_mime_type_equal (mime_types[n], non_match))
316 mime_types[n] = NULL;
325 cache_alias_lookup (const char *alias)
328 int i, min, max, mid, cmp;
330 for (i = 0; _caches[i]; i++)
332 XdgMimeCache *cache = _caches[i];
333 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 4);
334 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
341 mid = (min + max) / 2;
343 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid);
344 ptr = cache->buffer + offset;
345 cmp = strcmp (ptr, alias);
353 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid + 4);
354 return cache->buffer + offset;
368 cache_glob_lookup_literal (const char *file_name,
369 const char *mime_types[],
371 int case_sensitive_check)
374 int i, min, max, mid, cmp;
376 for (i = 0; _caches[i]; i++)
378 XdgMimeCache *cache = _caches[i];
379 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 12);
380 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
387 mid = (min + max) / 2;
389 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * mid);
390 ptr = cache->buffer + offset;
391 cmp = strcmp (ptr, file_name);
399 int weight = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * mid + 8);
400 int case_sensitive = weight & 0x100;
401 weight = weight & 0xff;
403 if (case_sensitive_check || !case_sensitive)
405 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * mid + 4);
406 mime_types[0] = (const char *)(cache->buffer + offset);
419 cache_glob_lookup_fnmatch (const char *file_name,
420 MimeWeight mime_types[],
423 const char *mime_type;
429 for (i = 0; _caches[i]; i++)
431 XdgMimeCache *cache = _caches[i];
433 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 20);
434 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
436 for (j = 0; j < n_entries && n < n_mime_types; j++)
438 xdg_uint32_t offset = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * j);
439 xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * j + 4);
440 int weight = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * j + 8);
441 weight = weight & 0xff;
442 ptr = cache->buffer + offset;
443 mime_type = cache->buffer + mimetype_offset;
445 /* FIXME: Not UTF-8 safe */
446 if (fnmatch (ptr, file_name, 0) == 0)
448 mime_types[n].mime = mime_type;
449 mime_types[n].weight = weight;
462 cache_glob_node_lookup_suffix (XdgMimeCache *cache,
463 xdg_uint32_t n_entries,
465 const char *file_name,
467 int case_sensitive_check,
468 MimeWeight mime_types[],
471 xdg_unichar_t character;
472 xdg_unichar_t match_char;
473 xdg_uint32_t mimetype_offset;
474 xdg_uint32_t n_children;
475 xdg_uint32_t child_offset;
479 int min, max, mid, n, i;
481 character = file_name[len - 1];
483 assert (character != 0);
489 mid = (min + max) / 2;
490 match_char = GET_UINT32 (cache->buffer, offset + 12 * mid);
491 if (match_char < character)
493 else if (match_char > character)
499 n_children = GET_UINT32 (cache->buffer, offset + 12 * mid + 4);
500 child_offset = GET_UINT32 (cache->buffer, offset + 12 * mid + 8);
504 n = cache_glob_node_lookup_suffix (cache,
505 n_children, child_offset,
507 case_sensitive_check,
514 while (n < n_mime_types && i < n_children)
516 match_char = GET_UINT32 (cache->buffer, child_offset + 12 * i);
520 mimetype_offset = GET_UINT32 (cache->buffer, child_offset + 12 * i + 4);
521 weight = GET_UINT32 (cache->buffer, child_offset + 12 * i + 8);
522 case_sensitive = weight & 0x100;
523 weight = weight & 0xff;
525 if (case_sensitive_check || !case_sensitive)
527 mime_types[n].mime = cache->buffer + mimetype_offset;
528 mime_types[n].weight = weight;
541 cache_glob_lookup_suffix (const char *file_name,
544 MimeWeight mime_types[],
549 for (i = 0; _caches[i]; i++)
551 XdgMimeCache *cache = _caches[i];
553 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 16);
554 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
555 xdg_uint32_t offset = GET_UINT32 (cache->buffer, list_offset + 4);
557 n = cache_glob_node_lookup_suffix (cache,
570 static int compare_mime_weight (const void *a, const void *b)
572 const MimeWeight *aa = (const MimeWeight *)a;
573 const MimeWeight *bb = (const MimeWeight *)b;
575 return bb->weight - aa->weight;
578 #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
580 ascii_tolower (const char *str)
584 lower = strdup (str);
589 *p++ = ISUPPER (c) ? c - 'A' + 'a' : c;
595 cache_glob_lookup_file_name (const char *file_name,
596 const char *mime_types[],
600 MimeWeight mimes[10];
606 assert (file_name != NULL && n_mime_types > 0);
608 /* First, check the literals */
610 lower_case = ascii_tolower (file_name);
612 n = cache_glob_lookup_literal (lower_case, mime_types, n_mime_types, FALSE);
619 n = cache_glob_lookup_literal (file_name, mime_types, n_mime_types, TRUE);
626 len = strlen (file_name);
627 n = cache_glob_lookup_suffix (lower_case, len, FALSE, mimes, n_mimes);
629 n = cache_glob_lookup_suffix (file_name, len, TRUE, mimes, n_mimes);
633 /* Last, try fnmatch */
635 n = cache_glob_lookup_fnmatch (file_name, mimes, n_mimes);
637 qsort (mimes, n, sizeof (MimeWeight), compare_mime_weight);
639 if (n_mime_types < n)
642 for (i = 0; i < n; i++)
643 mime_types[i] = mimes[i].mime;
649 _xdg_mime_cache_get_max_buffer_extents (void)
652 xdg_uint32_t max_extent;
656 for (i = 0; _caches[i]; i++)
658 XdgMimeCache *cache = _caches[i];
660 offset = GET_UINT32 (cache->buffer, 24);
661 max_extent = MAX (max_extent, GET_UINT32 (cache->buffer, offset + 4));
668 cache_get_mime_type_for_data (const void *data,
671 const char *mime_types[],
674 const char *mime_type;
679 for (i = 0; _caches[i]; i++)
681 XdgMimeCache *cache = _caches[i];
686 match = cache_magic_lookup_data (cache, data, len, &prio,
687 mime_types, n_mime_types);
696 *result_prio = priority;
701 for (n = 0; n < n_mime_types; n++)
705 return mime_types[n];
708 return XDG_MIME_TYPE_UNKNOWN;
712 _xdg_mime_cache_get_mime_type_for_data (const void *data,
716 return cache_get_mime_type_for_data (data, len, result_prio, NULL, 0);
720 _xdg_mime_cache_get_mime_type_for_file (const char *file_name,
721 struct stat *statbuf)
723 const char *mime_type;
724 const char *mime_types[10];
730 const char *base_name;
733 if (file_name == NULL)
736 if (! _xdg_utf8_validate (file_name))
739 base_name = _xdg_get_base_name (file_name);
740 n = cache_glob_lookup_file_name (base_name, mime_types, 10);
743 return mime_types[0];
747 if (stat (file_name, &buf) != 0)
748 return XDG_MIME_TYPE_UNKNOWN;
753 if (!S_ISREG (statbuf->st_mode))
754 return XDG_MIME_TYPE_UNKNOWN;
756 /* FIXME: Need to make sure that max_extent isn't totally broken. This could
757 * be large and need getting from a stream instead of just reading it all
759 max_extent = _xdg_mime_cache_get_max_buffer_extents ();
760 data = malloc (max_extent);
762 return XDG_MIME_TYPE_UNKNOWN;
764 file = fopen (file_name, "r");
768 return XDG_MIME_TYPE_UNKNOWN;
771 bytes_read = fread (data, 1, max_extent, file);
776 return XDG_MIME_TYPE_UNKNOWN;
779 mime_type = cache_get_mime_type_for_data (data, bytes_read, NULL,
789 _xdg_mime_cache_get_mime_type_from_file_name (const char *file_name)
791 const char *mime_type;
793 if (cache_glob_lookup_file_name (file_name, &mime_type, 1))
796 return XDG_MIME_TYPE_UNKNOWN;
800 _xdg_mime_cache_get_mime_types_from_file_name (const char *file_name,
801 const char *mime_types[],
804 return cache_glob_lookup_file_name (file_name, mime_types, n_mime_types);
809 is_super_type (const char *mime)
814 length = strlen (mime);
815 type = &(mime[length - 2]);
817 if (strcmp (type, "/*") == 0)
825 _xdg_mime_cache_mime_type_subclass (const char *mime,
828 const char *umime, *ubase;
830 int i, j, min, max, med, cmp;
832 umime = _xdg_mime_cache_unalias_mime_type (mime);
833 ubase = _xdg_mime_cache_unalias_mime_type (base);
835 if (strcmp (umime, ubase) == 0)
838 /* We really want to handle text/ * in GtkFileFilter, so we just
839 * turn on the supertype matching
842 /* Handle supertypes */
843 if (is_super_type (ubase) &&
844 xdg_mime_media_type_equal (umime, ubase))
848 /* Handle special cases text/plain and application/octet-stream */
849 if (strcmp (ubase, "text/plain") == 0 &&
850 strncmp (umime, "text/", 5) == 0)
853 if (strcmp (ubase, "application/octet-stream") == 0)
856 for (i = 0; _caches[i]; i++)
858 XdgMimeCache *cache = _caches[i];
860 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 8);
861 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
862 xdg_uint32_t offset, n_parents, parent_offset;
870 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * med);
871 cmp = strcmp (cache->buffer + offset, umime);
878 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * med + 4);
879 n_parents = GET_UINT32 (cache->buffer, offset);
881 for (j = 0; j < n_parents; j++)
883 parent_offset = GET_UINT32 (cache->buffer, offset + 4 + 4 * j);
884 if (_xdg_mime_cache_mime_type_subclass (cache->buffer + parent_offset, ubase))
897 _xdg_mime_cache_unalias_mime_type (const char *mime)
901 lookup = cache_alias_lookup (mime);
910 _xdg_mime_cache_list_mime_parents (const char *mime)
913 char *all_parents[128]; /* we'll stop at 128 */
916 mime = xdg_mime_unalias_mime_type (mime);
919 for (i = 0; _caches[i]; i++)
921 XdgMimeCache *cache = _caches[i];
923 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 8);
924 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
926 for (j = 0; j < n_entries; j++)
928 xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * j);
929 xdg_uint32_t parents_offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * j + 4);
931 if (strcmp (cache->buffer + mimetype_offset, mime) == 0)
933 xdg_uint32_t parent_mime_offset;
934 xdg_uint32_t n_parents = GET_UINT32 (cache->buffer, parents_offset);
936 for (k = 0; k < n_parents && p < 127; k++)
938 parent_mime_offset = GET_UINT32 (cache->buffer, parents_offset + 4 + 4 * k);
940 /* Don't add same parent multiple times.
941 * This can happen for instance if the same type is listed in multiple directories
943 for (l = 0; l < p; l++)
945 if (strcmp (all_parents[l], cache->buffer + parent_mime_offset) == 0)
950 all_parents[p++] = cache->buffer + parent_mime_offset;
957 all_parents[p++] = NULL;
959 result = (char **) malloc (p * sizeof (char *));
960 memcpy (result, all_parents, p * sizeof (char *));
966 cache_lookup_icon (const char *mime, int header)
969 int i, min, max, mid, cmp;
971 for (i = 0; _caches[i]; i++)
973 XdgMimeCache *cache = _caches[i];
974 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, header);
975 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
982 mid = (min + max) / 2;
984 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid);
985 ptr = cache->buffer + offset;
986 cmp = strcmp (ptr, mime);
994 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid + 4);
995 return cache->buffer + offset;
1004 _xdg_mime_cache_get_generic_icon (const char *mime)
1006 return cache_lookup_icon (mime, 36);
1010 _xdg_mime_cache_get_icon (const char *mime)
1012 return cache_lookup_icon (mime, 32);
1016 dump_glob_node (XdgMimeCache *cache,
1017 xdg_uint32_t offset,
1020 xdg_unichar_t character;
1021 xdg_uint32_t mime_offset;
1022 xdg_uint32_t n_children;
1023 xdg_uint32_t child_offset;
1026 character = GET_UINT32 (cache->buffer, offset);
1027 mime_offset = GET_UINT32 (cache->buffer, offset + 4);
1028 n_children = GET_UINT32 (cache->buffer, offset + 8);
1029 child_offset = GET_UINT32 (cache->buffer, offset + 12);
1030 for (i = 0; i < depth; i++)
1032 printf ("%c", character);
1034 printf (" - %s", cache->buffer + mime_offset);
1038 for (i = 0; i < n_children; i++)
1039 dump_glob_node (cache, child_offset + 20 * i, depth + 1);
1044 _xdg_mime_cache_glob_dump (void)
1047 for (i = 0; _caches[i]; i++)
1049 XdgMimeCache *cache = _caches[i];
1050 xdg_uint32_t list_offset;
1051 xdg_uint32_t n_entries;
1052 xdg_uint32_t offset;
1053 list_offset = GET_UINT32 (cache->buffer, 16);
1054 n_entries = GET_UINT32 (cache->buffer, list_offset);
1055 offset = GET_UINT32 (cache->buffer, list_offset + 4);
1056 for (j = 0; j < n_entries; j++)
1057 dump_glob_node (cache, offset + 20 * j, 0);