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
));
148 cache
->minor
= minor
;
149 cache
->ref_count
= 1;
150 cache
->buffer
= buffer
;
151 cache
->size
= st
.st_size
;
157 #endif /* HAVE_MMAP */
163 cache_magic_matchlet_compare_to_data (XdgMimeCache
*cache
,
168 xdg_uint32_t range_start
= GET_UINT32 (cache
->buffer
, offset
);
169 xdg_uint32_t range_length
= GET_UINT32 (cache
->buffer
, offset
+ 4);
170 xdg_uint32_t data_length
= GET_UINT32 (cache
->buffer
, offset
+ 12);
171 xdg_uint32_t data_offset
= GET_UINT32 (cache
->buffer
, offset
+ 16);
172 xdg_uint32_t mask_offset
= GET_UINT32 (cache
->buffer
, offset
+ 20);
176 for (i
= range_start
; i
< range_start
+ range_length
; i
++)
178 int valid_matchlet
= TRUE
;
180 if (i
+ data_length
> len
)
185 for (j
= 0; j
< data_length
; j
++)
187 if ((((unsigned char *)cache
->buffer
)[data_offset
+ j
] & ((unsigned char *)cache
->buffer
)[mask_offset
+ j
]) !=
188 ((((unsigned char *) data
)[j
+ i
]) & ((unsigned char *)cache
->buffer
)[mask_offset
+ j
]))
190 valid_matchlet
= FALSE
;
197 for (j
= 0; j
< data_length
; j
++)
199 if (((unsigned char *)cache
->buffer
)[data_offset
+ j
] != ((unsigned char *) data
)[j
+ i
])
201 valid_matchlet
= FALSE
;
215 cache_magic_matchlet_compare (XdgMimeCache
*cache
,
220 xdg_uint32_t n_children
= GET_UINT32 (cache
->buffer
, offset
+ 24);
221 xdg_uint32_t child_offset
= GET_UINT32 (cache
->buffer
, offset
+ 28);
225 if (cache_magic_matchlet_compare_to_data (cache
, offset
, data
, len
))
230 for (i
= 0; i
< n_children
; i
++)
232 if (cache_magic_matchlet_compare (cache
, child_offset
+ 32 * i
,
242 cache_magic_compare_to_data (XdgMimeCache
*cache
,
248 xdg_uint32_t priority
= GET_UINT32 (cache
->buffer
, offset
);
249 xdg_uint32_t mimetype_offset
= GET_UINT32 (cache
->buffer
, offset
+ 4);
250 xdg_uint32_t n_matchlets
= GET_UINT32 (cache
->buffer
, offset
+ 8);
251 xdg_uint32_t matchlet_offset
= GET_UINT32 (cache
->buffer
, offset
+ 12);
255 for (i
= 0; i
< n_matchlets
; i
++)
257 if (cache_magic_matchlet_compare (cache
, matchlet_offset
+ i
* 32,
262 return cache
->buffer
+ mimetype_offset
;
270 cache_magic_lookup_data (XdgMimeCache
*cache
,
274 const char *mime_types
[],
277 xdg_uint32_t list_offset
;
278 xdg_uint32_t n_entries
;
285 list_offset
= GET_UINT32 (cache
->buffer
, 24);
286 n_entries
= GET_UINT32 (cache
->buffer
, list_offset
);
287 offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 8);
289 for (j
= 0; j
< n_entries
; j
++)
293 match
= cache_magic_compare_to_data (cache
, offset
+ 16 * j
,
299 xdg_uint32_t mimetype_offset
;
300 const char *non_match
;
302 mimetype_offset
= GET_UINT32 (cache
->buffer
, offset
+ 16 * j
+ 4);
303 non_match
= cache
->buffer
+ mimetype_offset
;
305 for (n
= 0; n
< n_mime_types
; n
++)
308 _xdg_mime_mime_type_equal (mime_types
[n
], non_match
))
309 mime_types
[n
] = NULL
;
318 cache_alias_lookup (const char *alias
)
321 int i
, min
, max
, mid
, cmp
;
323 for (i
= 0; _caches
[i
]; i
++)
325 XdgMimeCache
*cache
= _caches
[i
];
326 xdg_uint32_t list_offset
= GET_UINT32 (cache
->buffer
, 4);
327 xdg_uint32_t n_entries
= GET_UINT32 (cache
->buffer
, list_offset
);
334 mid
= (min
+ max
) / 2;
336 offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 8 * mid
);
337 ptr
= cache
->buffer
+ offset
;
338 cmp
= strcmp (ptr
, alias
);
346 offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 8 * mid
+ 4);
347 return cache
->buffer
+ offset
;
361 cache_glob_lookup_literal (const char *file_name
,
362 const char *mime_types
[],
364 int case_sensitive_check
)
367 int i
, min
, max
, mid
, cmp
;
369 for (i
= 0; _caches
[i
]; i
++)
371 XdgMimeCache
*cache
= _caches
[i
];
372 xdg_uint32_t list_offset
= GET_UINT32 (cache
->buffer
, 12);
373 xdg_uint32_t n_entries
= GET_UINT32 (cache
->buffer
, list_offset
);
380 mid
= (min
+ max
) / 2;
382 offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 12 * mid
);
383 ptr
= cache
->buffer
+ offset
;
384 cmp
= strcmp (ptr
, file_name
);
392 int weight
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 12 * mid
+ 8);
393 int case_sensitive
= weight
& 0x100;
394 weight
= weight
& 0xff;
396 if (case_sensitive_check
|| !case_sensitive
)
398 offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 12 * mid
+ 4);
399 mime_types
[0] = (const char *)(cache
->buffer
+ offset
);
412 cache_glob_lookup_fnmatch (const char *file_name
,
413 MimeWeight mime_types
[],
416 const char *mime_type
;
422 for (i
= 0; _caches
[i
]; i
++)
424 XdgMimeCache
*cache
= _caches
[i
];
426 xdg_uint32_t list_offset
= GET_UINT32 (cache
->buffer
, 20);
427 xdg_uint32_t n_entries
= GET_UINT32 (cache
->buffer
, list_offset
);
429 for (j
= 0; j
< n_entries
&& n
< n_mime_types
; j
++)
431 xdg_uint32_t offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 12 * j
);
432 xdg_uint32_t mimetype_offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 12 * j
+ 4);
433 int weight
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 12 * j
+ 8);
434 weight
= weight
& 0xff;
435 ptr
= cache
->buffer
+ offset
;
436 mime_type
= cache
->buffer
+ mimetype_offset
;
438 /* FIXME: Not UTF-8 safe */
439 if (fnmatch (ptr
, file_name
, 0) == 0)
441 mime_types
[n
].mime
= mime_type
;
442 mime_types
[n
].weight
= weight
;
447 if (n
== n_mime_types
)
455 cache_glob_node_lookup_suffix (XdgMimeCache
*cache
,
456 xdg_uint32_t n_entries
,
458 const char *file_name
,
460 int case_sensitive_check
,
461 MimeWeight mime_types
[],
464 xdg_unichar_t character
;
465 xdg_unichar_t match_char
;
466 xdg_uint32_t mimetype_offset
;
467 xdg_uint32_t n_children
;
468 xdg_uint32_t child_offset
;
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
;
534 cache_glob_lookup_suffix (const char *file_name
,
537 MimeWeight mime_types
[],
543 for (i
= 0; _caches
[i
]; i
++)
545 XdgMimeCache
*cache
= _caches
[i
];
547 xdg_uint32_t list_offset
= GET_UINT32 (cache
->buffer
, 16);
548 xdg_uint32_t n_entries
= GET_UINT32 (cache
->buffer
, list_offset
);
549 xdg_uint32_t offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4);
551 n
+= cache_glob_node_lookup_suffix (cache
,
557 if (n
== n_mime_types
)
564 static int compare_mime_weight (const void *a
, const void *b
)
566 const MimeWeight
*aa
= (const MimeWeight
*)a
;
567 const MimeWeight
*bb
= (const MimeWeight
*)b
;
569 return bb
->weight
- aa
->weight
;
572 #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
574 ascii_tolower (const char *str
)
578 lower
= strdup (str
);
583 *p
++ = ISUPPER (c
) ? c
- 'A' + 'a' : c
;
589 filter_out_dupes (MimeWeight mimes
[], int n_mimes
)
596 for (i
= 0; i
< last
; i
++)
601 if (strcmp (mimes
[i
].mime
, mimes
[j
].mime
) == 0)
603 mimes
[i
].weight
= MAX (mimes
[i
].weight
, mimes
[j
].weight
);
605 mimes
[j
].mime
= mimes
[last
].mime
;
606 mimes
[j
].weight
= mimes
[last
].weight
;
617 cache_glob_lookup_file_name (const char *file_name
,
618 const char *mime_types
[],
622 MimeWeight mimes
[10];
628 assert (file_name
!= NULL
&& n_mime_types
> 0);
630 /* First, check the literals */
632 lower_case
= ascii_tolower (file_name
);
634 n
= cache_glob_lookup_literal (lower_case
, mime_types
, n_mime_types
, FALSE
);
641 n
= cache_glob_lookup_literal (file_name
, mime_types
, n_mime_types
, TRUE
);
648 len
= strlen (file_name
);
649 n
= cache_glob_lookup_suffix (lower_case
, len
, FALSE
, mimes
, n_mimes
);
651 n
+= cache_glob_lookup_suffix (file_name
, len
, TRUE
, mimes
+ n
, n_mimes
- n
);
655 /* Last, try fnmatch */
657 n
+= cache_glob_lookup_fnmatch (file_name
, mimes
+ n
, n_mimes
- n
);
659 n
= filter_out_dupes (mimes
, n
);
661 qsort (mimes
, n
, sizeof (MimeWeight
), compare_mime_weight
);
663 if (n_mime_types
< n
)
666 for (i
= 0; i
< n
; i
++)
667 mime_types
[i
] = mimes
[i
].mime
;
673 _xdg_mime_cache_get_max_buffer_extents (void)
676 xdg_uint32_t max_extent
;
680 for (i
= 0; _caches
[i
]; i
++)
682 XdgMimeCache
*cache
= _caches
[i
];
684 offset
= GET_UINT32 (cache
->buffer
, 24);
685 max_extent
= MAX (max_extent
, GET_UINT32 (cache
->buffer
, offset
+ 4));
692 cache_get_mime_type_for_data (const void *data
,
695 const char *mime_types
[],
698 const char *mime_type
;
703 for (i
= 0; _caches
[i
]; i
++)
705 XdgMimeCache
*cache
= _caches
[i
];
710 match
= cache_magic_lookup_data (cache
, data
, len
, &prio
,
711 mime_types
, n_mime_types
);
720 *result_prio
= priority
;
725 for (n
= 0; n
< n_mime_types
; n
++)
729 return mime_types
[n
];
732 return XDG_MIME_TYPE_UNKNOWN
;
736 _xdg_mime_cache_get_mime_type_for_data (const void *data
,
740 return cache_get_mime_type_for_data (data
, len
, result_prio
, NULL
, 0);
743 #ifdef NOT_USED_IN_GIO
746 _xdg_mime_cache_get_mime_type_for_file (const char *file_name
,
747 struct stat
*statbuf
)
749 const char *mime_type
;
750 const char *mime_types
[10];
756 const char *base_name
;
759 if (file_name
== NULL
)
762 if (! _xdg_utf8_validate (file_name
))
765 base_name
= _xdg_get_base_name (file_name
);
766 n
= cache_glob_lookup_file_name (base_name
, mime_types
, 10);
769 return mime_types
[0];
773 if (stat (file_name
, &buf
) != 0)
774 return XDG_MIME_TYPE_UNKNOWN
;
779 if (!S_ISREG (statbuf
->st_mode
))
780 return XDG_MIME_TYPE_UNKNOWN
;
782 /* FIXME: Need to make sure that max_extent isn't totally broken. This could
783 * be large and need getting from a stream instead of just reading it all
785 max_extent
= _xdg_mime_cache_get_max_buffer_extents ();
786 data
= malloc (max_extent
);
788 return XDG_MIME_TYPE_UNKNOWN
;
790 file
= fopen (file_name
, "r");
794 return XDG_MIME_TYPE_UNKNOWN
;
797 bytes_read
= fread (data
, 1, max_extent
, file
);
802 return XDG_MIME_TYPE_UNKNOWN
;
805 mime_type
= cache_get_mime_type_for_data (data
, bytes_read
, NULL
,
815 _xdg_mime_cache_get_mime_type_from_file_name (const char *file_name
)
817 const char *mime_type
;
819 if (cache_glob_lookup_file_name (file_name
, &mime_type
, 1))
822 return XDG_MIME_TYPE_UNKNOWN
;
828 _xdg_mime_cache_get_mime_types_from_file_name (const char *file_name
,
829 const char *mime_types
[],
832 return cache_glob_lookup_file_name (file_name
, mime_types
, n_mime_types
);
837 is_super_type (const char *mime
)
842 length
= strlen (mime
);
843 type
= &(mime
[length
- 2]);
845 if (strcmp (type
, "/*") == 0)
853 _xdg_mime_cache_mime_type_subclass (const char *mime
,
856 const char *umime
, *ubase
;
858 int i
, j
, min
, max
, med
, cmp
;
860 umime
= _xdg_mime_cache_unalias_mime_type (mime
);
861 ubase
= _xdg_mime_cache_unalias_mime_type (base
);
863 if (strcmp (umime
, ubase
) == 0)
866 /* We really want to handle text/ * in GtkFileFilter, so we just
867 * turn on the supertype matching
870 /* Handle supertypes */
871 if (is_super_type (ubase
) &&
872 xdg_mime_media_type_equal (umime
, ubase
))
876 /* Handle special cases text/plain and application/octet-stream */
877 if (strcmp (ubase
, "text/plain") == 0 &&
878 strncmp (umime
, "text/", 5) == 0)
881 if (strcmp (ubase
, "application/octet-stream") == 0)
884 for (i
= 0; _caches
[i
]; i
++)
886 XdgMimeCache
*cache
= _caches
[i
];
888 xdg_uint32_t list_offset
= GET_UINT32 (cache
->buffer
, 8);
889 xdg_uint32_t n_entries
= GET_UINT32 (cache
->buffer
, list_offset
);
890 xdg_uint32_t offset
, n_parents
, parent_offset
;
898 offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 8 * med
);
899 cmp
= strcmp (cache
->buffer
+ offset
, umime
);
906 offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 8 * med
+ 4);
907 n_parents
= GET_UINT32 (cache
->buffer
, offset
);
909 for (j
= 0; j
< n_parents
; j
++)
911 parent_offset
= GET_UINT32 (cache
->buffer
, offset
+ 4 + 4 * j
);
912 if (_xdg_mime_cache_mime_type_subclass (cache
->buffer
+ parent_offset
, ubase
))
925 _xdg_mime_cache_unalias_mime_type (const char *mime
)
929 lookup
= cache_alias_lookup (mime
);
938 _xdg_mime_cache_list_mime_parents (const char *mime
)
941 char *all_parents
[128]; /* we'll stop at 128 */
944 mime
= xdg_mime_unalias_mime_type (mime
);
947 for (i
= 0; _caches
[i
]; i
++)
949 XdgMimeCache
*cache
= _caches
[i
];
951 xdg_uint32_t list_offset
= GET_UINT32 (cache
->buffer
, 8);
952 xdg_uint32_t n_entries
= GET_UINT32 (cache
->buffer
, list_offset
);
954 for (j
= 0; j
< n_entries
; j
++)
956 xdg_uint32_t mimetype_offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 8 * j
);
957 xdg_uint32_t parents_offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 8 * j
+ 4);
959 if (strcmp (cache
->buffer
+ mimetype_offset
, mime
) == 0)
961 xdg_uint32_t parent_mime_offset
;
962 xdg_uint32_t n_parents
= GET_UINT32 (cache
->buffer
, parents_offset
);
964 for (k
= 0; k
< n_parents
&& p
< 127; k
++)
966 parent_mime_offset
= GET_UINT32 (cache
->buffer
, parents_offset
+ 4 + 4 * k
);
968 /* Don't add same parent multiple times.
969 * This can happen for instance if the same type is listed in multiple directories
971 for (l
= 0; l
< p
; l
++)
973 if (strcmp (all_parents
[l
], cache
->buffer
+ parent_mime_offset
) == 0)
978 all_parents
[p
++] = cache
->buffer
+ parent_mime_offset
;
985 all_parents
[p
++] = NULL
;
987 result
= (char **) malloc (p
* sizeof (char *));
988 memcpy (result
, all_parents
, p
* sizeof (char *));
994 cache_lookup_icon (const char *mime
, int header
)
997 int i
, min
, max
, mid
, cmp
;
999 for (i
= 0; _caches
[i
]; i
++)
1001 XdgMimeCache
*cache
= _caches
[i
];
1002 xdg_uint32_t list_offset
= GET_UINT32 (cache
->buffer
, header
);
1003 xdg_uint32_t n_entries
= GET_UINT32 (cache
->buffer
, list_offset
);
1004 xdg_uint32_t offset
;
1007 max
= n_entries
- 1;
1010 mid
= (min
+ max
) / 2;
1012 offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 8 * mid
);
1013 ptr
= cache
->buffer
+ offset
;
1014 cmp
= strcmp (ptr
, mime
);
1022 offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4 + 8 * mid
+ 4);
1023 return cache
->buffer
+ offset
;
1032 _xdg_mime_cache_get_generic_icon (const char *mime
)
1034 return cache_lookup_icon (mime
, 36);
1038 _xdg_mime_cache_get_icon (const char *mime
)
1040 return cache_lookup_icon (mime
, 32);
1043 #ifdef NOT_USED_IN_GIO
1046 dump_glob_node (XdgMimeCache
*cache
,
1047 xdg_uint32_t offset
,
1050 xdg_unichar_t character
;
1051 xdg_uint32_t mime_offset
;
1052 xdg_uint32_t n_children
;
1053 xdg_uint32_t child_offset
;
1056 character
= GET_UINT32 (cache
->buffer
, offset
);
1057 mime_offset
= GET_UINT32 (cache
->buffer
, offset
+ 4);
1058 n_children
= GET_UINT32 (cache
->buffer
, offset
+ 8);
1059 child_offset
= GET_UINT32 (cache
->buffer
, offset
+ 12);
1060 for (i
= 0; i
< depth
; i
++)
1062 printf ("%c", character
);
1064 printf (" - %s", cache
->buffer
+ mime_offset
);
1068 for (i
= 0; i
< n_children
; i
++)
1069 dump_glob_node (cache
, child_offset
+ 20 * i
, depth
+ 1);
1074 _xdg_mime_cache_glob_dump (void)
1077 for (i
= 0; _caches
[i
]; i
++)
1079 XdgMimeCache
*cache
= _caches
[i
];
1080 xdg_uint32_t list_offset
;
1081 xdg_uint32_t n_entries
;
1082 xdg_uint32_t offset
;
1083 list_offset
= GET_UINT32 (cache
->buffer
, 16);
1084 n_entries
= GET_UINT32 (cache
->buffer
, list_offset
);
1085 offset
= GET_UINT32 (cache
->buffer
, list_offset
+ 4);
1086 for (j
= 0; j
< n_entries
; j
++)
1087 dump_glob_node (cache
, offset
+ 20 * j
, 0);