2009-10-02 Alexander Larsson <alexl@redhat.com>
[rox-filer.git] / src / xdgmimecache.c
blob4c7f7268e45bd1c4e56f8bdd0f83b3c348cfd2e2
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.
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <fnmatch.h>
38 #include <assert.h>
40 #include <netinet/in.h> /* for ntohl/ntohs */
42 #ifdef HAVE_MMAP
43 #include <sys/mman.h>
44 #else
45 #warning Building xdgmime without MMAP support. Binary "mime.info" cache files will not be used.
46 #endif
48 #include <sys/stat.h>
49 #include <sys/types.h>
51 #include "xdgmimecache.h"
52 #include "xdgmimeint.h"
54 #ifndef MAX
55 #define MAX(a,b) ((a) > (b) ? (a) : (b))
56 #endif
58 #ifndef FALSE
59 #define FALSE (0)
60 #endif
62 #ifndef TRUE
63 #define TRUE (!FALSE)
64 #endif
66 #ifndef _O_BINARY
67 #define _O_BINARY 0
68 #endif
70 #ifndef MAP_FAILED
71 #define MAP_FAILED ((void *) -1)
72 #endif
74 #define MAJOR_VERSION 1
75 #define MINOR_VERSION_MIN 1
76 #define MINOR_VERSION_MAX 2
78 struct _XdgMimeCache
80 int ref_count;
81 int minor;
83 size_t size;
84 char *buffer;
87 #define GET_UINT16(cache,offset) (ntohs(*(xdg_uint16_t*)((cache) + (offset))))
88 #define GET_UINT32(cache,offset) (ntohl(*(xdg_uint32_t*)((cache) + (offset))))
90 XdgMimeCache *
91 _xdg_mime_cache_ref (XdgMimeCache *cache)
93 cache->ref_count++;
94 return cache;
97 void
98 _xdg_mime_cache_unref (XdgMimeCache *cache)
100 cache->ref_count--;
102 if (cache->ref_count == 0)
104 #ifdef HAVE_MMAP
105 munmap (cache->buffer, cache->size);
106 #endif
107 free (cache);
111 XdgMimeCache *
112 _xdg_mime_cache_new_from_file (const char *file_name)
114 XdgMimeCache *cache = NULL;
116 #ifdef HAVE_MMAP
117 int fd = -1;
118 struct stat st;
119 char *buffer = NULL;
120 int minor;
122 /* Open the file and map it into memory */
123 fd = open (file_name, O_RDONLY|_O_BINARY, 0);
125 if (fd < 0)
126 return NULL;
128 if (fstat (fd, &st) < 0 || st.st_size < 4)
129 goto done;
131 buffer = (char *) mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
133 if (buffer == MAP_FAILED)
134 goto done;
136 minor = GET_UINT16 (buffer, 2);
137 /* Verify version */
138 if (GET_UINT16 (buffer, 0) != MAJOR_VERSION ||
139 (minor < MINOR_VERSION_MIN ||
140 minor > MINOR_VERSION_MAX))
142 munmap (buffer, st.st_size);
144 goto done;
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;
153 done:
154 if (fd != -1)
155 close (fd);
157 #endif /* HAVE_MMAP */
159 return cache;
162 static int
163 cache_magic_matchlet_compare_to_data (XdgMimeCache *cache,
164 xdg_uint32_t offset,
165 const void *data,
166 size_t len)
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);
174 int i, j;
176 for (i = range_start; i <= range_start + range_length; i++)
178 int valid_matchlet = TRUE;
180 if (i + data_length > len)
181 return FALSE;
183 if (mask_offset)
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;
191 break;
195 else
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;
202 break;
207 if (valid_matchlet)
208 return TRUE;
211 return FALSE;
214 static int
215 cache_magic_matchlet_compare (XdgMimeCache *cache,
216 xdg_uint32_t offset,
217 const void *data,
218 size_t len)
220 xdg_uint32_t n_children = GET_UINT32 (cache->buffer, offset + 24);
221 xdg_uint32_t child_offset = GET_UINT32 (cache->buffer, offset + 28);
223 int i;
225 if (cache_magic_matchlet_compare_to_data (cache, offset, data, len))
227 if (n_children == 0)
228 return TRUE;
230 for (i = 0; i < n_children; i++)
232 if (cache_magic_matchlet_compare (cache, child_offset + 32 * i,
233 data, len))
234 return TRUE;
238 return FALSE;
241 static const char *
242 cache_magic_compare_to_data (XdgMimeCache *cache,
243 xdg_uint32_t offset,
244 const void *data,
245 size_t len,
246 int *prio)
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);
253 int i;
255 for (i = 0; i < n_matchlets; i++)
257 if (cache_magic_matchlet_compare (cache, matchlet_offset + i * 32,
258 data, len))
260 *prio = priority;
262 return cache->buffer + mimetype_offset;
266 return NULL;
269 static const char *
270 cache_magic_lookup_data (XdgMimeCache *cache,
271 const void *data,
272 size_t len,
273 int *prio,
274 const char *mime_types[],
275 int n_mime_types)
277 xdg_uint32_t list_offset;
278 xdg_uint32_t n_entries;
279 xdg_uint32_t offset;
281 int j, n;
283 *prio = 0;
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++)
291 const char *match;
293 match = cache_magic_compare_to_data (cache, offset + 16 * j,
294 data, len, prio);
295 if (match)
296 return match;
297 else
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++)
307 if (mime_types[n] &&
308 _xdg_mime_mime_type_equal (mime_types[n], non_match))
309 mime_types[n] = NULL;
314 return NULL;
317 static const char *
318 cache_alias_lookup (const char *alias)
320 const char *ptr;
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);
328 xdg_uint32_t offset;
330 min = 0;
331 max = n_entries - 1;
332 while (max >= min)
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);
340 if (cmp < 0)
341 min = mid + 1;
342 else if (cmp > 0)
343 max = mid - 1;
344 else
346 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid + 4);
347 return cache->buffer + offset;
352 return NULL;
355 typedef struct {
356 const char *mime;
357 int weight;
358 } MimeWeight;
360 static int
361 cache_glob_lookup_literal (const char *file_name,
362 const char *mime_types[],
363 int n_mime_types,
364 int case_sensitive_check)
366 const char *ptr;
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);
374 xdg_uint32_t offset;
376 min = 0;
377 max = n_entries - 1;
378 while (max >= min)
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);
386 if (cmp < 0)
387 min = mid + 1;
388 else if (cmp > 0)
389 max = mid - 1;
390 else
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);
401 return 1;
403 return 0;
408 return 0;
411 static int
412 cache_glob_lookup_fnmatch (const char *file_name,
413 MimeWeight mime_types[],
414 int n_mime_types)
416 const char *mime_type;
417 const char *ptr;
419 int i, j, n;
421 n = 0;
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;
443 n++;
447 if (n > 0)
448 return n;
451 return 0;
454 static int
455 cache_glob_node_lookup_suffix (XdgMimeCache *cache,
456 xdg_uint32_t n_entries,
457 xdg_uint32_t offset,
458 const char *file_name,
459 int len,
460 int case_sensitive_check,
461 MimeWeight mime_types[],
462 int n_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;
469 int weight;
470 int case_sensitive;
472 int min, max, mid, n, i;
474 character = file_name[len - 1];
476 assert (character != 0);
478 min = 0;
479 max = n_entries - 1;
480 while (max >= min)
482 mid = (min + max) / 2;
483 match_char = GET_UINT32 (cache->buffer, offset + 12 * mid);
484 if (match_char < character)
485 min = mid + 1;
486 else if (match_char > character)
487 max = mid - 1;
488 else
490 len--;
491 n = 0;
492 n_children = GET_UINT32 (cache->buffer, offset + 12 * mid + 4);
493 child_offset = GET_UINT32 (cache->buffer, offset + 12 * mid + 8);
495 if (len > 0)
497 n = cache_glob_node_lookup_suffix (cache,
498 n_children, child_offset,
499 file_name, len,
500 case_sensitive_check,
501 mime_types,
502 n_mime_types);
504 if (n == 0)
506 i = 0;
507 while (n < n_mime_types && i < n_children)
509 match_char = GET_UINT32 (cache->buffer, child_offset + 12 * i);
510 if (match_char != 0)
511 break;
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;
522 n++;
524 i++;
527 return n;
530 return 0;
533 static int
534 cache_glob_lookup_suffix (const char *file_name,
535 int len,
536 int ignore_case,
537 MimeWeight mime_types[],
538 int n_mime_types)
540 int i, n;
542 for (i = 0; _caches[i]; i++)
544 XdgMimeCache *cache = _caches[i];
546 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 16);
547 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
548 xdg_uint32_t offset = GET_UINT32 (cache->buffer, list_offset + 4);
550 n = cache_glob_node_lookup_suffix (cache,
551 n_entries, offset,
552 file_name, len,
553 ignore_case,
554 mime_types,
555 n_mime_types);
556 if (n > 0)
557 return n;
560 return 0;
563 static int compare_mime_weight (const void *a, const void *b)
565 const MimeWeight *aa = (const MimeWeight *)a;
566 const MimeWeight *bb = (const MimeWeight *)b;
568 return aa->weight - bb->weight;
571 #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
572 static char *
573 ascii_tolower (const char *str)
575 char *p, *lower;
577 lower = strdup (str);
578 p = lower;
579 while (*p != 0)
581 char c = *p;
582 *p++ = ISUPPER (c) ? c - 'A' + 'a' : c;
584 return lower;
587 static int
588 cache_glob_lookup_file_name (const char *file_name,
589 const char *mime_types[],
590 int n_mime_types)
592 int n;
593 MimeWeight mimes[10];
594 int n_mimes = 10;
595 int i;
596 int len;
597 char *lower_case;
598 int try_lower_case;
600 assert (file_name != NULL && n_mime_types > 0);
602 /* First, check the literals */
604 lower_case = ascii_tolower (file_name);
606 n = cache_glob_lookup_literal (lower_case, mime_types, n_mime_types, FALSE);
607 if (n > 0)
609 free (lower_case);
610 return n;
613 n = cache_glob_lookup_literal (file_name, mime_types, n_mime_types, TRUE);
614 if (n > 0)
616 free (lower_case);
617 return n;
620 len = strlen (file_name);
621 n = cache_glob_lookup_suffix (lower_case, len, FALSE, mimes, n_mimes);
622 if (n == 0)
623 n = cache_glob_lookup_suffix (file_name, len, TRUE, mimes, n_mimes);
625 free (lower_case);
627 /* Last, try fnmatch */
628 if (n == 0)
629 n = cache_glob_lookup_fnmatch (file_name, mimes, n_mimes);
631 qsort (mimes, n, sizeof (MimeWeight), compare_mime_weight);
633 if (n_mime_types < n)
634 n = n_mime_types;
636 for (i = 0; i < n; i++)
637 mime_types[i] = mimes[i].mime;
639 return n;
643 _xdg_mime_cache_get_max_buffer_extents (void)
645 xdg_uint32_t offset;
646 xdg_uint32_t max_extent;
647 int i;
649 max_extent = 0;
650 for (i = 0; _caches[i]; i++)
652 XdgMimeCache *cache = _caches[i];
654 offset = GET_UINT32 (cache->buffer, 24);
655 max_extent = MAX (max_extent, GET_UINT32 (cache->buffer, offset + 4));
658 return max_extent;
661 static const char *
662 cache_get_mime_type_for_data (const void *data,
663 size_t len,
664 int *result_prio,
665 const char *mime_types[],
666 int n_mime_types)
668 const char *mime_type;
669 int i, n, priority;
671 priority = 0;
672 mime_type = NULL;
673 for (i = 0; _caches[i]; i++)
675 XdgMimeCache *cache = _caches[i];
677 int prio;
678 const char *match;
680 match = cache_magic_lookup_data (cache, data, len, &prio,
681 mime_types, n_mime_types);
682 if (prio > priority)
684 priority = prio;
685 mime_type = match;
689 if (result_prio)
690 *result_prio = priority;
692 if (priority > 0)
693 return mime_type;
695 for (n = 0; n < n_mime_types; n++)
698 if (mime_types[n])
699 return mime_types[n];
702 return XDG_MIME_TYPE_UNKNOWN;
705 const char *
706 _xdg_mime_cache_get_mime_type_for_data (const void *data,
707 size_t len,
708 int *result_prio)
710 return cache_get_mime_type_for_data (data, len, result_prio, NULL, 0);
713 const char *
714 _xdg_mime_cache_get_mime_type_for_file (const char *file_name,
715 struct stat *statbuf)
717 const char *mime_type;
718 const char *mime_types[10];
719 FILE *file;
720 unsigned char *data;
721 int max_extent;
722 int bytes_read;
723 struct stat buf;
724 const char *base_name;
725 int n;
727 if (file_name == NULL)
728 return NULL;
730 if (! _xdg_utf8_validate (file_name))
731 return NULL;
733 base_name = _xdg_get_base_name (file_name);
734 n = cache_glob_lookup_file_name (base_name, mime_types, 10);
736 if (n == 1)
737 return mime_types[0];
739 if (!statbuf)
741 if (stat (file_name, &buf) != 0)
742 return XDG_MIME_TYPE_UNKNOWN;
744 statbuf = &buf;
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
752 * in. */
753 max_extent = _xdg_mime_cache_get_max_buffer_extents ();
754 data = malloc (max_extent);
755 if (data == NULL)
756 return XDG_MIME_TYPE_UNKNOWN;
758 file = fopen (file_name, "r");
759 if (file == NULL)
761 free (data);
762 return XDG_MIME_TYPE_UNKNOWN;
765 bytes_read = fread (data, 1, max_extent, file);
766 if (ferror (file))
768 free (data);
769 fclose (file);
770 return XDG_MIME_TYPE_UNKNOWN;
773 mime_type = cache_get_mime_type_for_data (data, bytes_read, NULL,
774 mime_types, n);
776 free (data);
777 fclose (file);
779 return mime_type;
782 const char *
783 _xdg_mime_cache_get_mime_type_from_file_name (const char *file_name)
785 const char *mime_type;
787 if (cache_glob_lookup_file_name (file_name, &mime_type, 1))
788 return mime_type;
789 else
790 return XDG_MIME_TYPE_UNKNOWN;
794 _xdg_mime_cache_get_mime_types_from_file_name (const char *file_name,
795 const char *mime_types[],
796 int n_mime_types)
798 return cache_glob_lookup_file_name (file_name, mime_types, n_mime_types);
801 #if 1
802 static int
803 is_super_type (const char *mime)
805 int length;
806 const char *type;
808 length = strlen (mime);
809 type = &(mime[length - 2]);
811 if (strcmp (type, "/*") == 0)
812 return 1;
814 return 0;
816 #endif
819 _xdg_mime_cache_mime_type_subclass (const char *mime,
820 const char *base)
822 const char *umime, *ubase;
824 int i, j, min, max, med, cmp;
826 umime = _xdg_mime_cache_unalias_mime_type (mime);
827 ubase = _xdg_mime_cache_unalias_mime_type (base);
829 if (strcmp (umime, ubase) == 0)
830 return 1;
832 /* We really want to handle text/ * in GtkFileFilter, so we just
833 * turn on the supertype matching
835 #if 1
836 /* Handle supertypes */
837 if (is_super_type (ubase) &&
838 xdg_mime_media_type_equal (umime, ubase))
839 return 1;
840 #endif
842 /* Handle special cases text/plain and application/octet-stream */
843 if (strcmp (ubase, "text/plain") == 0 &&
844 strncmp (umime, "text/", 5) == 0)
845 return 1;
847 if (strcmp (ubase, "application/octet-stream") == 0)
848 return 1;
850 for (i = 0; _caches[i]; i++)
852 XdgMimeCache *cache = _caches[i];
854 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 8);
855 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
856 xdg_uint32_t offset, n_parents, parent_offset;
858 min = 0;
859 max = n_entries - 1;
860 while (max >= min)
862 med = (min + max)/2;
864 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * med);
865 cmp = strcmp (cache->buffer + offset, umime);
866 if (cmp < 0)
867 min = med + 1;
868 else if (cmp > 0)
869 max = med - 1;
870 else
872 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * med + 4);
873 n_parents = GET_UINT32 (cache->buffer, offset);
875 for (j = 0; j < n_parents; j++)
877 parent_offset = GET_UINT32 (cache->buffer, offset + 4 + 4 * j);
878 if (_xdg_mime_cache_mime_type_subclass (cache->buffer + parent_offset, ubase))
879 return 1;
882 break;
887 return 0;
890 const char *
891 _xdg_mime_cache_unalias_mime_type (const char *mime)
893 const char *lookup;
895 lookup = cache_alias_lookup (mime);
897 if (lookup)
898 return lookup;
900 return mime;
903 char **
904 _xdg_mime_cache_list_mime_parents (const char *mime)
906 int i, j, k, l, p;
907 char *all_parents[128]; /* we'll stop at 128 */
908 char **result;
910 mime = xdg_mime_unalias_mime_type (mime);
912 p = 0;
913 for (i = 0; _caches[i]; i++)
915 XdgMimeCache *cache = _caches[i];
917 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 8);
918 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
920 for (j = 0; j < n_entries; j++)
922 xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * j);
923 xdg_uint32_t parents_offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * j + 4);
925 if (strcmp (cache->buffer + mimetype_offset, mime) == 0)
927 xdg_uint32_t parent_mime_offset;
928 xdg_uint32_t n_parents = GET_UINT32 (cache->buffer, parents_offset);
930 for (k = 0; k < n_parents && p < 127; k++)
932 parent_mime_offset = GET_UINT32 (cache->buffer, parents_offset + 4 + 4 * k);
934 /* Don't add same parent multiple times.
935 * This can happen for instance if the same type is listed in multiple directories
937 for (l = 0; l < p; l++)
939 if (strcmp (all_parents[l], cache->buffer + parent_mime_offset) == 0)
940 break;
943 if (l == p)
944 all_parents[p++] = cache->buffer + parent_mime_offset;
947 break;
951 all_parents[p++] = NULL;
953 result = (char **) malloc (p * sizeof (char *));
954 memcpy (result, all_parents, p * sizeof (char *));
956 return result;
959 static const char *
960 cache_lookup_icon (const char *mime, int header)
962 const char *ptr;
963 int i, min, max, mid, cmp;
965 for (i = 0; _caches[i]; i++)
967 XdgMimeCache *cache = _caches[i];
968 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, header);
969 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
970 xdg_uint32_t offset;
972 min = 0;
973 max = n_entries - 1;
974 while (max >= min)
976 mid = (min + max) / 2;
978 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid);
979 ptr = cache->buffer + offset;
980 cmp = strcmp (ptr, mime);
982 if (cmp < 0)
983 min = mid + 1;
984 else if (cmp > 0)
985 max = mid - 1;
986 else
988 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid + 4);
989 return cache->buffer + offset;
994 return NULL;
997 const char *
998 _xdg_mime_cache_get_generic_icon (const char *mime)
1000 return cache_lookup_icon (mime, 36);
1003 const char *
1004 _xdg_mime_cache_get_icon (const char *mime)
1006 const char *icon;
1008 icon = cache_lookup_icon (mime, 32);
1010 if (icon == NULL)
1011 icon = _xdg_mime_cache_get_generic_icon (mime);
1013 return icon;
1016 static void
1017 dump_glob_node (XdgMimeCache *cache,
1018 xdg_uint32_t offset,
1019 int depth)
1021 xdg_unichar_t character;
1022 xdg_uint32_t mime_offset;
1023 xdg_uint32_t n_children;
1024 xdg_uint32_t child_offset;
1025 int i;
1027 character = GET_UINT32 (cache->buffer, offset);
1028 mime_offset = GET_UINT32 (cache->buffer, offset + 4);
1029 n_children = GET_UINT32 (cache->buffer, offset + 8);
1030 child_offset = GET_UINT32 (cache->buffer, offset + 12);
1031 for (i = 0; i < depth; i++)
1032 printf (" ");
1033 printf ("%c", character);
1034 if (mime_offset)
1035 printf (" - %s", cache->buffer + mime_offset);
1036 printf ("\n");
1037 if (child_offset)
1039 for (i = 0; i < n_children; i++)
1040 dump_glob_node (cache, child_offset + 20 * i, depth + 1);
1044 void
1045 _xdg_mime_cache_glob_dump (void)
1047 int i, j;
1048 for (i = 0; _caches[i]; i++)
1050 XdgMimeCache *cache = _caches[i];
1051 xdg_uint32_t list_offset;
1052 xdg_uint32_t n_entries;
1053 xdg_uint32_t offset;
1054 list_offset = GET_UINT32 (cache->buffer, 16);
1055 n_entries = GET_UINT32 (cache->buffer, list_offset);
1056 offset = GET_UINT32 (cache->buffer, list_offset + 4);
1057 for (j = 0; j < n_entries; j++)
1058 dump_glob_node (cache, offset + 20 * j, 0);