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.
41 #include <netinet/in.h> /* for ntohl/ntohs */
46 #warning Building xdgmime without MMAP support. Binary "mime.cache" files will not be used.
50 #include <sys/types.h>
52 #include "xdgmimecache.h"
53 #include "xdgmimeint.h"
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))))
92 _xdg_mime_cache_ref (XdgMimeCache
*cache
)
99 _xdg_mime_cache_unref (XdgMimeCache
*cache
)
103 if (cache
->ref_count
== 0)
106 munmap (cache
->buffer
, cache
->size
);
113 _xdg_mime_cache_new_from_file (const char *file_name
)
115 XdgMimeCache
*cache
= NULL
;
123 /* Open the file and map it into memory */
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
);
150 cache
= (XdgMimeCache
*) malloc (sizeof (XdgMimeCache
));
151 cache
->minor
= minor
;
152 cache
->ref_count
= 1;
153 cache
->buffer
= buffer
;
154 cache
->size
= st
.st_size
;
160 #endif /* HAVE_MMAP */
166 cache_magic_matchlet_compare_to_data (XdgMimeCache
*cache
,
171 xdg_uint32_t range_start
= GET_UINT32 (cache
->buffer
, offset
);
172 xdg_uint32_t range_length
= GET_UINT32 (cache
->buffer
, offset
+ 4);
173 xdg_uint32_t data_length
= GET_UINT32 (cache
->buffer
, offset
+ 12);
174 xdg_uint32_t data_offset
= GET_UINT32 (cache
->buffer
, offset
+ 16);
175 xdg_uint32_t mask_offset
= GET_UINT32 (cache
->buffer
, offset
+ 20);
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;
211 cache_magic_matchlet_compare (XdgMimeCache
*cache
,
216 xdg_uint32_t n_children
= GET_UINT32 (cache
->buffer
, offset
+ 24);
217 xdg_uint32_t child_offset
= GET_UINT32 (cache
->buffer
, offset
+ 28);
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
,
238 cache_magic_compare_to_data (XdgMimeCache
*cache
,
244 xdg_uint32_t priority
= GET_UINT32 (cache
->buffer
, offset
);
245 xdg_uint32_t mimetype_offset
= GET_UINT32 (cache
->buffer
, offset
+ 4);
246 xdg_uint32_t n_matchlets
= GET_UINT32 (cache
->buffer
, offset
+ 8);
247 xdg_uint32_t matchlet_offset
= GET_UINT32 (cache
->buffer
, offset
+ 12);
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
;
266 cache_magic_lookup_data (XdgMimeCache
*cache
,
271 xdg_uint32_t list_offset
;
272 xdg_uint32_t n_entries
;
279 list_offset
= GET_UINT32 (cache
->buffer
, 24);
280 n_entries
= GET_UINT32 (cache
->buffer
, list_offset
);
281 offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 8);
283 for (j
= 0; j
< n_entries
; j
++)
287 match
= cache_magic_compare_to_data (cache
, offset
+ 16 * j
,
297 cache_alias_lookup (const char *alias
)
300 int i
, min
, max
, mid
, cmp
;
302 for (i
= 0; _caches
[i
]; i
++)
304 XdgMimeCache
*cache
= _caches
[i
];
305 xdg_uint32_t list_offset
= GET_UINT32 (cache
->buffer
, 4);
306 xdg_uint32_t n_entries
= GET_UINT32 (cache
->buffer
, list_offset
);
313 mid
= (min
+ max
) / 2;
315 offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 8 * mid
);
316 ptr
= cache
->buffer
+ offset
;
317 cmp
= strcmp (ptr
, alias
);
325 offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 8 * mid
+ 4);
326 return cache
->buffer
+ offset
;
340 cache_glob_lookup_literal (const char *file_name
,
341 const char *mime_types
[],
343 int case_sensitive_check
)
346 int i
, min
, max
, mid
, cmp
;
348 for (i
= 0; _caches
[i
]; i
++)
350 XdgMimeCache
*cache
= _caches
[i
];
351 xdg_uint32_t list_offset
= GET_UINT32 (cache
->buffer
, 12);
352 xdg_uint32_t n_entries
= GET_UINT32 (cache
->buffer
, list_offset
);
359 mid
= (min
+ max
) / 2;
361 offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 12 * mid
);
362 ptr
= cache
->buffer
+ offset
;
363 cmp
= strcmp (ptr
, file_name
);
371 int weight
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 12 * mid
+ 8);
372 int case_sensitive
= weight
& 0x100;
373 weight
= weight
& 0xff;
375 if (case_sensitive_check
|| !case_sensitive
)
377 offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 12 * mid
+ 4);
378 mime_types
[0] = (const char *)(cache
->buffer
+ offset
);
391 cache_glob_lookup_fnmatch (const char *file_name
,
392 MimeWeight mime_types
[],
394 int case_sensitive_check
)
396 const char *mime_type
;
403 for (i
= 0; _caches
[i
]; i
++)
405 XdgMimeCache
*cache
= _caches
[i
];
407 xdg_uint32_t list_offset
= GET_UINT32 (cache
->buffer
, 20);
408 xdg_uint32_t n_entries
= GET_UINT32 (cache
->buffer
, list_offset
);
410 for (j
= 0; j
< n_entries
&& n
< n_mime_types
; j
++)
412 xdg_uint32_t offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 12 * j
);
413 xdg_uint32_t mimetype_offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 12 * j
+ 4);
414 int weight
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 12 * j
+ 8);
415 int case_sensitive
= weight
& 0x100;
416 weight
= weight
& 0xff;
417 ptr
= cache
->buffer
+ offset
;
418 mime_type
= cache
->buffer
+ mimetype_offset
;
419 if (case_sensitive_check
|| !case_sensitive
)
421 /* FIXME: Not UTF-8 safe */
422 if (fnmatch (ptr
, file_name
, 0) == 0)
424 mime_types
[n
].mime
= mime_type
;
425 mime_types
[n
].weight
= weight
;
439 cache_glob_node_lookup_suffix (XdgMimeCache
*cache
,
440 xdg_uint32_t n_entries
,
442 const char *file_name
,
444 int case_sensitive_check
,
445 MimeWeight mime_types
[],
448 xdg_unichar_t character
;
449 xdg_unichar_t match_char
;
450 xdg_uint32_t mimetype_offset
;
451 xdg_uint32_t n_children
;
452 xdg_uint32_t child_offset
;
457 int min
, max
, mid
, n
;
459 character
= file_name
[len
- 1];
461 assert (character
!= 0);
467 mid
= (min
+ max
) / 2;
468 match_char
= GET_UINT32 (cache
->buffer
, offset
+ 12 * mid
);
469 if (match_char
< character
)
471 else if (match_char
> character
)
477 n_children
= GET_UINT32 (cache
->buffer
, offset
+ 12 * mid
+ 4);
478 child_offset
= GET_UINT32 (cache
->buffer
, offset
+ 12 * mid
+ 8);
482 n
= cache_glob_node_lookup_suffix (cache
,
483 n_children
, child_offset
,
485 case_sensitive_check
,
492 while (n
< n_mime_types
&& i
< n_children
)
494 match_char
= GET_UINT32 (cache
->buffer
, child_offset
+ 12 * i
);
498 mimetype_offset
= GET_UINT32 (cache
->buffer
, child_offset
+ 12 * i
+ 4);
499 weight
= GET_UINT32 (cache
->buffer
, child_offset
+ 12 * i
+ 8);
500 case_sensitive
= weight
& 0x100;
501 weight
= weight
& 0xff;
503 if (case_sensitive_check
|| !case_sensitive
)
505 mime_types
[n
].mime
= cache
->buffer
+ mimetype_offset
;
506 mime_types
[n
].weight
= weight
;
519 cache_glob_lookup_suffix (const char *file_name
,
522 MimeWeight mime_types
[],
527 for (i
= 0; _caches
[i
]; i
++)
529 XdgMimeCache
*cache
= _caches
[i
];
531 xdg_uint32_t list_offset
= GET_UINT32 (cache
->buffer
, 16);
532 xdg_uint32_t n_entries
= GET_UINT32 (cache
->buffer
, list_offset
);
533 xdg_uint32_t offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4);
535 n
= cache_glob_node_lookup_suffix (cache
,
548 static int compare_mime_weight (const void *a
, const void *b
)
550 const MimeWeight
*aa
= (const MimeWeight
*)a
;
551 const MimeWeight
*bb
= (const MimeWeight
*)b
;
553 return bb
->weight
- aa
->weight
;
556 #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
558 ascii_tolower (const char *str
)
562 lower
= strdup (str
);
567 *p
++ = ISUPPER (c
) ? c
- 'A' + 'a' : c
;
573 cache_glob_lookup_file_name (const char *file_name
,
574 const char *mime_types
[],
578 MimeWeight mimes
[10];
584 assert (file_name
!= NULL
&& n_mime_types
> 0);
586 /* First, check the literals */
588 lower_case
= ascii_tolower (file_name
);
590 n
= cache_glob_lookup_literal (lower_case
, mime_types
, n_mime_types
, FALSE
);
597 n
= cache_glob_lookup_literal (file_name
, mime_types
, n_mime_types
, TRUE
);
604 len
= strlen (file_name
);
605 n
= cache_glob_lookup_suffix (lower_case
, len
, FALSE
, mimes
, n_mimes
);
607 n
= cache_glob_lookup_suffix (file_name
, len
, TRUE
, mimes
, n_mimes
);
609 /* Last, try fnmatch */
611 n
= cache_glob_lookup_fnmatch (lower_case
, mimes
, n_mimes
, FALSE
);
613 n
= cache_glob_lookup_fnmatch (file_name
, mimes
, n_mimes
, TRUE
);
617 qsort (mimes
, n
, sizeof (MimeWeight
), compare_mime_weight
);
619 if (n_mime_types
< n
)
622 for (i
= 0; i
< n
; i
++)
623 mime_types
[i
] = mimes
[i
].mime
;
629 _xdg_mime_cache_get_max_buffer_extents (void)
632 xdg_uint32_t max_extent
;
636 for (i
= 0; _caches
[i
]; i
++)
638 XdgMimeCache
*cache
= _caches
[i
];
640 offset
= GET_UINT32 (cache
->buffer
, 24);
641 max_extent
= MAX (max_extent
, GET_UINT32 (cache
->buffer
, offset
+ 4));
648 cache_get_mime_type_for_data (const void *data
,
651 const char *mime_types
[],
654 const char *mime_type
;
659 for (i
= 0; _caches
[i
]; i
++)
661 XdgMimeCache
*cache
= _caches
[i
];
666 match
= cache_magic_lookup_data (cache
, data
, len
, &prio
);
675 *result_prio
= priority
;
679 /* Pick glob-result R where mime_type inherits from R */
680 for (n
= 0; n
< n_mime_types
; n
++)
682 if (mime_types
[n
] && _xdg_mime_cache_mime_type_subclass(mime_types
[n
], mime_type
))
683 return mime_types
[n
];
687 /* No globs: return magic match */
692 /* Pick first glob result, as fallback */
693 for (n
= 0; n
< n_mime_types
; n
++)
696 return mime_types
[n
];
703 _xdg_mime_cache_get_mime_type_for_data (const void *data
,
707 return cache_get_mime_type_for_data (data
, len
, result_prio
, NULL
, 0);
711 _xdg_mime_cache_get_mime_type_for_file (const char *file_name
,
712 struct stat
*statbuf
)
714 const char *mime_type
;
715 const char *mime_types
[10];
721 const char *base_name
;
724 if (file_name
== NULL
)
727 if (! _xdg_utf8_validate (file_name
))
730 base_name
= _xdg_get_base_name (file_name
);
731 n
= cache_glob_lookup_file_name (base_name
, mime_types
, 10);
734 return mime_types
[0];
738 if (stat (file_name
, &buf
) != 0)
739 return XDG_MIME_TYPE_UNKNOWN
;
744 if (statbuf
->st_size
== 0)
745 return XDG_MIME_TYPE_EMPTY
;
747 if (!S_ISREG (statbuf
->st_mode
))
748 return XDG_MIME_TYPE_UNKNOWN
;
750 /* FIXME: Need to make sure that max_extent isn't totally broken. This could
751 * be large and need getting from a stream instead of just reading it all
753 max_extent
= _xdg_mime_cache_get_max_buffer_extents ();
754 data
= malloc (max_extent
);
756 return XDG_MIME_TYPE_UNKNOWN
;
758 file
= fopen (file_name
, "r");
762 return XDG_MIME_TYPE_UNKNOWN
;
765 bytes_read
= fread (data
, 1, max_extent
, file
);
770 return XDG_MIME_TYPE_UNKNOWN
;
773 mime_type
= cache_get_mime_type_for_data (data
, bytes_read
, NULL
,
777 mime_type
= _xdg_binary_or_text_fallback (data
, bytes_read
);
786 _xdg_mime_cache_get_mime_type_from_file_name (const char *file_name
)
788 const char *mime_type
;
790 if (cache_glob_lookup_file_name (file_name
, &mime_type
, 1))
793 return XDG_MIME_TYPE_UNKNOWN
;
797 _xdg_mime_cache_get_mime_types_from_file_name (const char *file_name
,
798 const char *mime_types
[],
801 return cache_glob_lookup_file_name (file_name
, mime_types
, n_mime_types
);
806 ends_with (const char *str
,
812 length
= strlen (str
);
813 suffix_length
= strlen (suffix
);
814 if (length
< suffix_length
)
817 if (strcmp (str
+ length
- suffix_length
, suffix
) == 0)
824 is_super_type (const char *mime
)
826 return ends_with (mime
, "/*");
831 _xdg_mime_cache_mime_type_subclass (const char *mime
,
834 const char *umime
, *ubase
;
837 int i
, min
, max
, med
, cmp
;
839 umime
= _xdg_mime_cache_unalias_mime_type (mime
);
840 ubase
= _xdg_mime_cache_unalias_mime_type (base
);
842 if (strcmp (umime
, ubase
) == 0)
845 /* We really want to handle text/ * in GtkFileFilter, so we just
846 * turn on the supertype matching
849 /* Handle supertypes */
850 if (is_super_type (ubase
) &&
851 xdg_mime_media_type_equal (umime
, ubase
))
855 /* Handle special cases text/plain and application/octet-stream */
856 if (strcmp (ubase
, "text/plain") == 0 &&
857 strncmp (umime
, "text/", 5) == 0)
860 if (strcmp (ubase
, "application/octet-stream") == 0 &&
861 strncmp (umime
, "inode/", 6) != 0)
864 for (i
= 0; _caches
[i
]; i
++)
866 XdgMimeCache
*cache
= _caches
[i
];
868 xdg_uint32_t list_offset
= GET_UINT32 (cache
->buffer
, 8);
869 xdg_uint32_t n_entries
= GET_UINT32 (cache
->buffer
, list_offset
);
870 xdg_uint32_t offset
, n_parents
, parent_offset
;
878 offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 8 * med
);
879 cmp
= strcmp (cache
->buffer
+ offset
, umime
);
886 offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 8 * med
+ 4);
887 n_parents
= GET_UINT32 (cache
->buffer
, offset
);
889 for (j
= 0; j
< n_parents
; j
++)
891 parent_offset
= GET_UINT32 (cache
->buffer
, offset
+ 4 + 4 * j
);
892 if (_xdg_mime_cache_mime_type_subclass (cache
->buffer
+ parent_offset
, ubase
))
905 _xdg_mime_cache_unalias_mime_type (const char *mime
)
909 lookup
= cache_alias_lookup (mime
);
918 _xdg_mime_cache_list_mime_parents (const char *mime
)
922 char *all_parents
[128]; /* we'll stop at 128 */
925 mime
= xdg_mime_unalias_mime_type (mime
);
928 for (i
= 0; _caches
[i
]; i
++)
930 XdgMimeCache
*cache
= _caches
[i
];
932 xdg_uint32_t list_offset
= GET_UINT32 (cache
->buffer
, 8);
933 xdg_uint32_t n_entries
= GET_UINT32 (cache
->buffer
, list_offset
);
935 for (j
= 0; j
< n_entries
; j
++)
937 xdg_uint32_t mimetype_offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 8 * j
);
938 xdg_uint32_t parents_offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 8 * j
+ 4);
940 if (strcmp (cache
->buffer
+ mimetype_offset
, mime
) == 0)
942 xdg_uint32_t parent_mime_offset
;
943 xdg_uint32_t n_parents
= GET_UINT32 (cache
->buffer
, parents_offset
);
945 for (k
= 0; k
< n_parents
&& p
< 127; k
++)
947 parent_mime_offset
= GET_UINT32 (cache
->buffer
, parents_offset
+ 4 + 4 * k
);
949 /* Don't add same parent multiple times.
950 * This can happen for instance if the same type is listed in multiple directories
952 for (l
= 0; l
< p
; l
++)
954 if (strcmp (all_parents
[l
], cache
->buffer
+ parent_mime_offset
) == 0)
959 all_parents
[p
++] = cache
->buffer
+ parent_mime_offset
;
966 all_parents
[p
++] = NULL
;
968 result
= (char **) malloc (p
* sizeof (char *));
969 memcpy (result
, all_parents
, p
* sizeof (char *));
975 cache_lookup_icon (const char *mime
, int header
)
978 int i
, min
, max
, mid
, cmp
;
980 for (i
= 0; _caches
[i
]; i
++)
982 XdgMimeCache
*cache
= _caches
[i
];
983 xdg_uint32_t list_offset
= GET_UINT32 (cache
->buffer
, header
);
984 xdg_uint32_t n_entries
= GET_UINT32 (cache
->buffer
, list_offset
);
991 mid
= (min
+ max
) / 2;
993 offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 8 * mid
);
994 ptr
= cache
->buffer
+ offset
;
995 cmp
= strcmp (ptr
, mime
);
1003 offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 8 * mid
+ 4);
1004 return cache
->buffer
+ offset
;
1013 _xdg_mime_cache_get_generic_icon (const char *mime
)
1015 return cache_lookup_icon (mime
, 36);
1019 _xdg_mime_cache_get_icon (const char *mime
)
1021 return cache_lookup_icon (mime
, 32);
1025 dump_glob_node (XdgMimeCache
*cache
,
1026 xdg_uint32_t offset
,
1029 xdg_unichar_t character
;
1030 xdg_uint32_t mime_offset
;
1031 xdg_uint32_t n_children
;
1032 xdg_uint32_t child_offset
;
1036 character
= GET_UINT32 (cache
->buffer
, offset
);
1037 mime_offset
= GET_UINT32 (cache
->buffer
, offset
+ 4);
1038 n_children
= GET_UINT32 (cache
->buffer
, offset
+ 8);
1039 child_offset
= GET_UINT32 (cache
->buffer
, offset
+ 12);
1040 for (i
= 0; i
< depth
; i
++)
1042 printf ("%c", character
);
1044 printf (" - %s", cache
->buffer
+ mime_offset
);
1048 for (k
= 0; k
< n_children
; k
++)
1049 dump_glob_node (cache
, child_offset
+ 20 * k
, depth
+ 1);
1054 _xdg_mime_cache_glob_dump (void)
1057 for (i
= 0; _caches
[i
]; i
++)
1059 XdgMimeCache
*cache
= _caches
[i
];
1060 xdg_uint32_t list_offset
;
1061 xdg_uint32_t n_entries
;
1062 xdg_uint32_t offset
;
1063 list_offset
= GET_UINT32 (cache
->buffer
, 16);
1064 n_entries
= GET_UINT32 (cache
->buffer
, list_offset
);
1065 offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4);
1066 for (j
= 0; j
< n_entries
; j
++)
1067 dump_glob_node (cache
, offset
+ 20 * j
, 0);