Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / base / third_party / xdg_mime / xdgmimecache.c
blobddb875462a2d493dfa24f30d2c95242e948bcdc9
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 #define HAVE_MMAP 1
44 #ifdef HAVE_MMAP
45 #include <sys/mman.h>
46 #else
47 #warning Building xdgmime without MMAP support. Binary "mime.cache" files will not be used.
48 #endif
50 #include <sys/stat.h>
51 #include <sys/types.h>
53 #include "xdgmimecache.h"
54 #include "xdgmimeint.h"
56 #ifndef MAX
57 #define MAX(a,b) ((a) > (b) ? (a) : (b))
58 #endif
60 #ifndef FALSE
61 #define FALSE (0)
62 #endif
64 #ifndef TRUE
65 #define TRUE (!FALSE)
66 #endif
68 #ifndef _O_BINARY
69 #define _O_BINARY 0
70 #endif
72 #ifndef MAP_FAILED
73 #define MAP_FAILED ((void *) -1)
74 #endif
76 #define MAJOR_VERSION 1
77 #define MINOR_VERSION_MIN 1
78 #define MINOR_VERSION_MAX 2
80 struct _XdgMimeCache
82 int ref_count;
83 int minor;
85 size_t size;
86 char *buffer;
89 #define GET_UINT16(cache,offset) (ntohs(*(xdg_uint16_t*)((cache) + (offset))))
90 #define GET_UINT32(cache,offset) (ntohl(*(xdg_uint32_t*)((cache) + (offset))))
92 XdgMimeCache *
93 _xdg_mime_cache_ref (XdgMimeCache *cache)
95 cache->ref_count++;
96 return cache;
99 void
100 _xdg_mime_cache_unref (XdgMimeCache *cache)
102 cache->ref_count--;
104 if (cache->ref_count == 0)
106 #ifdef HAVE_MMAP
107 munmap (cache->buffer, cache->size);
108 #endif
109 free (cache);
113 XdgMimeCache *
114 _xdg_mime_cache_new_from_file (const char *file_name)
116 XdgMimeCache *cache = NULL;
118 #ifdef HAVE_MMAP
119 int fd = -1;
120 struct stat st;
121 char *buffer = NULL;
122 int minor;
124 /* Open the file and map it into memory */
125 fd = open (file_name, O_RDONLY|_O_BINARY, 0);
127 if (fd < 0)
128 return NULL;
130 if (fstat (fd, &st) < 0 || st.st_size < 4)
131 goto done;
133 buffer = (char *) mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
135 if (buffer == MAP_FAILED)
136 goto done;
138 minor = GET_UINT16 (buffer, 2);
139 /* Verify version */
140 if (GET_UINT16 (buffer, 0) != MAJOR_VERSION ||
141 (minor < MINOR_VERSION_MIN ||
142 minor > MINOR_VERSION_MAX))
144 munmap (buffer, st.st_size);
146 goto done;
149 cache = (XdgMimeCache *) malloc (sizeof (XdgMimeCache));
150 cache->minor = minor;
151 cache->ref_count = 1;
152 cache->buffer = buffer;
153 cache->size = st.st_size;
155 done:
156 if (fd != -1)
157 close (fd);
159 #endif /* HAVE_MMAP */
161 return cache;
164 static int
165 cache_magic_matchlet_compare_to_data (XdgMimeCache *cache,
166 xdg_uint32_t offset,
167 const void *data,
168 size_t len)
170 xdg_uint32_t range_start = GET_UINT32 (cache->buffer, offset);
171 xdg_uint32_t range_length = GET_UINT32 (cache->buffer, offset + 4);
172 xdg_uint32_t data_length = GET_UINT32 (cache->buffer, offset + 12);
173 xdg_uint32_t data_offset = GET_UINT32 (cache->buffer, offset + 16);
174 xdg_uint32_t mask_offset = GET_UINT32 (cache->buffer, offset + 20);
176 int i, j;
178 for (i = range_start; i < range_start + range_length; i++)
180 int valid_matchlet = TRUE;
182 if (i + data_length > len)
183 return FALSE;
185 if (mask_offset)
187 for (j = 0; j < data_length; j++)
189 if ((((unsigned char *)cache->buffer)[data_offset + j] & ((unsigned char *)cache->buffer)[mask_offset + j]) !=
190 ((((unsigned char *) data)[j + i]) & ((unsigned char *)cache->buffer)[mask_offset + j]))
192 valid_matchlet = FALSE;
193 break;
197 else
199 valid_matchlet = memcmp(cache->buffer + data_offset, data + i, data_length) == 0;
202 if (valid_matchlet)
203 return TRUE;
206 return FALSE;
209 static int
210 cache_magic_matchlet_compare (XdgMimeCache *cache,
211 xdg_uint32_t offset,
212 const void *data,
213 size_t len)
215 xdg_uint32_t n_children = GET_UINT32 (cache->buffer, offset + 24);
216 xdg_uint32_t child_offset = GET_UINT32 (cache->buffer, offset + 28);
218 int i;
220 if (cache_magic_matchlet_compare_to_data (cache, offset, data, len))
222 if (n_children == 0)
223 return TRUE;
225 for (i = 0; i < n_children; i++)
227 if (cache_magic_matchlet_compare (cache, child_offset + 32 * i,
228 data, len))
229 return TRUE;
233 return FALSE;
236 static const char *
237 cache_magic_compare_to_data (XdgMimeCache *cache,
238 xdg_uint32_t offset,
239 const void *data,
240 size_t len,
241 int *prio)
243 xdg_uint32_t priority = GET_UINT32 (cache->buffer, offset);
244 xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, offset + 4);
245 xdg_uint32_t n_matchlets = GET_UINT32 (cache->buffer, offset + 8);
246 xdg_uint32_t matchlet_offset = GET_UINT32 (cache->buffer, offset + 12);
248 int i;
250 for (i = 0; i < n_matchlets; i++)
252 if (cache_magic_matchlet_compare (cache, matchlet_offset + i * 32,
253 data, len))
255 *prio = priority;
257 return cache->buffer + mimetype_offset;
261 return NULL;
264 static const char *
265 cache_magic_lookup_data (XdgMimeCache *cache,
266 const void *data,
267 size_t len,
268 int *prio,
269 const char *mime_types[],
270 int n_mime_types)
272 xdg_uint32_t list_offset;
273 xdg_uint32_t n_entries;
274 xdg_uint32_t offset;
276 int j, n;
278 *prio = 0;
280 list_offset = GET_UINT32 (cache->buffer, 24);
281 n_entries = GET_UINT32 (cache->buffer, list_offset);
282 offset = GET_UINT32 (cache->buffer, list_offset + 8);
284 for (j = 0; j < n_entries; j++)
286 const char *match;
288 match = cache_magic_compare_to_data (cache, offset + 16 * j,
289 data, len, prio);
290 if (match)
291 return match;
292 else
294 xdg_uint32_t mimetype_offset;
295 const char *non_match;
297 mimetype_offset = GET_UINT32 (cache->buffer, offset + 16 * j + 4);
298 non_match = cache->buffer + mimetype_offset;
300 for (n = 0; n < n_mime_types; n++)
302 if (mime_types[n] &&
303 _xdg_mime_mime_type_equal (mime_types[n], non_match))
304 mime_types[n] = NULL;
309 return NULL;
312 static const char *
313 cache_alias_lookup (const char *alias)
315 const char *ptr;
316 int i, min, max, mid, cmp;
318 for (i = 0; _caches[i]; i++)
320 XdgMimeCache *cache = _caches[i];
321 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 4);
322 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
323 xdg_uint32_t offset;
325 min = 0;
326 max = n_entries - 1;
327 while (max >= min)
329 mid = (min + max) / 2;
331 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid);
332 ptr = cache->buffer + offset;
333 cmp = strcmp (ptr, alias);
335 if (cmp < 0)
336 min = mid + 1;
337 else if (cmp > 0)
338 max = mid - 1;
339 else
341 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid + 4);
342 return cache->buffer + offset;
347 return NULL;
350 typedef struct {
351 const char *mime;
352 int weight;
353 } MimeWeight;
355 static int
356 cache_glob_lookup_literal (const char *file_name,
357 const char *mime_types[],
358 int n_mime_types,
359 int case_sensitive_check)
361 const char *ptr;
362 int i, min, max, mid, cmp;
364 for (i = 0; _caches[i]; i++)
366 XdgMimeCache *cache = _caches[i];
367 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 12);
368 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
369 xdg_uint32_t offset;
371 min = 0;
372 max = n_entries - 1;
373 while (max >= min)
375 mid = (min + max) / 2;
377 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * mid);
378 ptr = cache->buffer + offset;
379 cmp = strcmp (ptr, file_name);
381 if (cmp < 0)
382 min = mid + 1;
383 else if (cmp > 0)
384 max = mid - 1;
385 else
387 int weight = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * mid + 8);
388 int case_sensitive = weight & 0x100;
389 weight = weight & 0xff;
391 if (case_sensitive_check || !case_sensitive)
393 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * mid + 4);
394 mime_types[0] = (const char *)(cache->buffer + offset);
396 return 1;
398 return 0;
403 return 0;
406 static int
407 cache_glob_lookup_fnmatch (const char *file_name,
408 MimeWeight mime_types[],
409 int n_mime_types,
410 int case_sensitive_check)
412 const char *mime_type;
413 const char *ptr;
415 int i, j, n;
417 n = 0;
418 for (i = 0; _caches[i]; i++)
420 XdgMimeCache *cache = _caches[i];
422 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 20);
423 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
425 for (j = 0; j < n_entries && n < n_mime_types; j++)
427 xdg_uint32_t offset = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * j);
428 xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * j + 4);
429 int weight = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * j + 8);
430 int case_sensitive = weight & 0x100;
431 weight = weight & 0xff;
432 ptr = cache->buffer + offset;
433 mime_type = cache->buffer + mimetype_offset;
434 if (case_sensitive_check || !case_sensitive)
436 /* FIXME: Not UTF-8 safe */
437 if (fnmatch (ptr, file_name, 0) == 0)
439 mime_types[n].mime = mime_type;
440 mime_types[n].weight = weight;
441 n++;
446 if (n > 0)
447 return n;
450 return 0;
453 static int
454 cache_glob_node_lookup_suffix (XdgMimeCache *cache,
455 xdg_uint32_t n_entries,
456 xdg_uint32_t offset,
457 const char *file_name,
458 int len,
459 int case_sensitive_check,
460 MimeWeight mime_types[],
461 int n_mime_types)
463 xdg_unichar_t character;
464 xdg_unichar_t match_char;
465 xdg_uint32_t mimetype_offset;
466 xdg_uint32_t n_children;
467 xdg_uint32_t child_offset;
468 int weight;
469 int case_sensitive;
471 int min, max, mid, n, i;
473 character = file_name[len - 1];
475 assert (character != 0);
477 min = 0;
478 max = n_entries - 1;
479 while (max >= min)
481 mid = (min + max) / 2;
482 match_char = GET_UINT32 (cache->buffer, offset + 12 * mid);
483 if (match_char < character)
484 min = mid + 1;
485 else if (match_char > character)
486 max = mid - 1;
487 else
489 len--;
490 n = 0;
491 n_children = GET_UINT32 (cache->buffer, offset + 12 * mid + 4);
492 child_offset = GET_UINT32 (cache->buffer, offset + 12 * mid + 8);
494 if (len > 0)
496 n = cache_glob_node_lookup_suffix (cache,
497 n_children, child_offset,
498 file_name, len,
499 case_sensitive_check,
500 mime_types,
501 n_mime_types);
503 if (n == 0)
505 i = 0;
506 while (n < n_mime_types && i < n_children)
508 match_char = GET_UINT32 (cache->buffer, child_offset + 12 * i);
509 if (match_char != 0)
510 break;
512 mimetype_offset = GET_UINT32 (cache->buffer, child_offset + 12 * i + 4);
513 weight = GET_UINT32 (cache->buffer, child_offset + 12 * i + 8);
514 case_sensitive = weight & 0x100;
515 weight = weight & 0xff;
517 if (case_sensitive_check || !case_sensitive)
519 mime_types[n].mime = cache->buffer + mimetype_offset;
520 mime_types[n].weight = weight;
521 n++;
523 i++;
526 return n;
529 return 0;
532 static int
533 cache_glob_lookup_suffix (const char *file_name,
534 int len,
535 int ignore_case,
536 MimeWeight mime_types[],
537 int n_mime_types)
539 int i, n;
541 for (i = 0; _caches[i]; i++)
543 XdgMimeCache *cache = _caches[i];
545 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 16);
546 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
547 xdg_uint32_t offset = GET_UINT32 (cache->buffer, list_offset + 4);
549 n = cache_glob_node_lookup_suffix (cache,
550 n_entries, offset,
551 file_name, len,
552 ignore_case,
553 mime_types,
554 n_mime_types);
555 if (n > 0)
556 return n;
559 return 0;
562 static int compare_mime_weight (const void *a, const void *b)
564 const MimeWeight *aa = (const MimeWeight *)a;
565 const MimeWeight *bb = (const MimeWeight *)b;
567 return bb->weight - aa->weight;
570 #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
571 static char *
572 ascii_tolower (const char *str)
574 char *p, *lower;
576 lower = strdup (str);
577 p = lower;
578 while (*p != 0)
580 char c = *p;
581 *p++ = ISUPPER (c) ? c - 'A' + 'a' : c;
583 return lower;
586 static int
587 cache_glob_lookup_file_name (const char *file_name,
588 const char *mime_types[],
589 int n_mime_types)
591 int n;
592 MimeWeight mimes[10];
593 int n_mimes = 10;
594 int i;
595 int len;
596 char *lower_case;
598 assert (file_name != NULL && n_mime_types > 0);
600 /* First, check the literals */
602 lower_case = ascii_tolower (file_name);
604 n = cache_glob_lookup_literal (lower_case, mime_types, n_mime_types, FALSE);
605 if (n > 0)
607 free (lower_case);
608 return n;
611 n = cache_glob_lookup_literal (file_name, mime_types, n_mime_types, TRUE);
612 if (n > 0)
614 free (lower_case);
615 return n;
618 len = strlen (file_name);
619 n = cache_glob_lookup_suffix (lower_case, len, FALSE, mimes, n_mimes);
620 if (n == 0)
621 n = cache_glob_lookup_suffix (file_name, len, TRUE, mimes, n_mimes);
623 /* Last, try fnmatch */
624 if (n == 0)
625 n = cache_glob_lookup_fnmatch (lower_case, mimes, n_mimes, FALSE);
626 if (n == 0)
627 n = cache_glob_lookup_fnmatch (file_name, mimes, n_mimes, TRUE);
629 free (lower_case);
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)
694 /* Pick glob-result R where mime_type inherits from R */
695 for (n = 0; n < n_mime_types; n++)
697 if (mime_types[n] && _xdg_mime_cache_mime_type_subclass(mime_types[n], mime_type))
698 return mime_types[n];
701 /* Return magic match */
702 return mime_type;
705 /* Pick first glob result, as fallback */
706 for (n = 0; n < n_mime_types; n++)
708 if (mime_types[n])
709 return mime_types[n];
712 return NULL;
715 const char *
716 _xdg_mime_cache_get_mime_type_for_data (const void *data,
717 size_t len,
718 int *result_prio)
720 return cache_get_mime_type_for_data (data, len, result_prio, NULL, 0);
723 const char *
724 _xdg_mime_cache_get_mime_type_for_file (const char *file_name,
725 struct stat *statbuf)
727 const char *mime_type;
728 const char *mime_types[10];
729 FILE *file;
730 unsigned char *data;
731 int max_extent;
732 int bytes_read;
733 struct stat buf;
734 const char *base_name;
735 int n;
737 if (file_name == NULL)
738 return NULL;
740 if (! _xdg_utf8_validate (file_name))
741 return NULL;
743 base_name = _xdg_get_base_name (file_name);
744 n = cache_glob_lookup_file_name (base_name, mime_types, 10);
746 if (n == 1)
747 return mime_types[0];
749 if (!statbuf)
751 if (stat (file_name, &buf) != 0)
752 return XDG_MIME_TYPE_UNKNOWN;
754 statbuf = &buf;
757 if (statbuf->st_size == 0)
758 return XDG_MIME_TYPE_EMPTY;
760 if (!S_ISREG (statbuf->st_mode))
761 return XDG_MIME_TYPE_UNKNOWN;
763 /* FIXME: Need to make sure that max_extent isn't totally broken. This could
764 * be large and need getting from a stream instead of just reading it all
765 * in. */
766 max_extent = _xdg_mime_cache_get_max_buffer_extents ();
767 data = malloc (max_extent);
768 if (data == NULL)
769 return XDG_MIME_TYPE_UNKNOWN;
771 file = fopen (file_name, "r");
772 if (file == NULL)
774 free (data);
775 return XDG_MIME_TYPE_UNKNOWN;
778 bytes_read = fread (data, 1, max_extent, file);
779 if (ferror (file))
781 free (data);
782 fclose (file);
783 return XDG_MIME_TYPE_UNKNOWN;
786 mime_type = cache_get_mime_type_for_data (data, bytes_read, NULL,
787 mime_types, n);
789 if (!mime_type)
790 mime_type = _xdg_binary_or_text_fallback(data, bytes_read);
792 free (data);
793 fclose (file);
795 return mime_type;
798 const char *
799 _xdg_mime_cache_get_mime_type_from_file_name (const char *file_name)
801 const char *mime_type;
803 if (cache_glob_lookup_file_name (file_name, &mime_type, 1))
804 return mime_type;
805 else
806 return XDG_MIME_TYPE_UNKNOWN;
810 _xdg_mime_cache_get_mime_types_from_file_name (const char *file_name,
811 const char *mime_types[],
812 int n_mime_types)
814 return cache_glob_lookup_file_name (file_name, mime_types, n_mime_types);
817 #if 1
818 static int
819 is_super_type (const char *mime)
821 int length;
822 const char *type;
824 length = strlen (mime);
825 type = &(mime[length - 2]);
827 if (strcmp (type, "/*") == 0)
828 return 1;
830 return 0;
832 #endif
835 _xdg_mime_cache_mime_type_subclass (const char *mime,
836 const char *base)
838 const char *umime, *ubase;
840 int i, j, min, max, med, cmp;
842 umime = _xdg_mime_cache_unalias_mime_type (mime);
843 ubase = _xdg_mime_cache_unalias_mime_type (base);
845 if (strcmp (umime, ubase) == 0)
846 return 1;
848 /* We really want to handle text/ * in GtkFileFilter, so we just
849 * turn on the supertype matching
851 #if 1
852 /* Handle supertypes */
853 if (is_super_type (ubase) &&
854 xdg_mime_media_type_equal (umime, ubase))
855 return 1;
856 #endif
858 /* Handle special cases text/plain and application/octet-stream */
859 if (strcmp (ubase, "text/plain") == 0 &&
860 strncmp (umime, "text/", 5) == 0)
861 return 1;
863 if (strcmp (ubase, "application/octet-stream") == 0)
864 return 1;
866 for (i = 0; _caches[i]; i++)
868 XdgMimeCache *cache = _caches[i];
870 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 8);
871 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
872 xdg_uint32_t offset, n_parents, parent_offset;
874 min = 0;
875 max = n_entries - 1;
876 while (max >= min)
878 med = (min + max)/2;
880 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * med);
881 cmp = strcmp (cache->buffer + offset, umime);
882 if (cmp < 0)
883 min = med + 1;
884 else if (cmp > 0)
885 max = med - 1;
886 else
888 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * med + 4);
889 n_parents = GET_UINT32 (cache->buffer, offset);
891 for (j = 0; j < n_parents; j++)
893 parent_offset = GET_UINT32 (cache->buffer, offset + 4 + 4 * j);
894 if (_xdg_mime_cache_mime_type_subclass (cache->buffer + parent_offset, ubase))
895 return 1;
898 break;
903 return 0;
906 const char *
907 _xdg_mime_cache_unalias_mime_type (const char *mime)
909 const char *lookup;
911 lookup = cache_alias_lookup (mime);
913 if (lookup)
914 return lookup;
916 return mime;
919 char **
920 _xdg_mime_cache_list_mime_parents (const char *mime)
922 int i, j, k, l, p;
923 char *all_parents[128]; /* we'll stop at 128 */
924 char **result;
926 mime = xdg_mime_unalias_mime_type (mime);
928 p = 0;
929 for (i = 0; _caches[i]; i++)
931 XdgMimeCache *cache = _caches[i];
933 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 8);
934 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
936 for (j = 0; j < n_entries; j++)
938 xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * j);
939 xdg_uint32_t parents_offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * j + 4);
941 if (strcmp (cache->buffer + mimetype_offset, mime) == 0)
943 xdg_uint32_t parent_mime_offset;
944 xdg_uint32_t n_parents = GET_UINT32 (cache->buffer, parents_offset);
946 for (k = 0; k < n_parents && p < 127; k++)
948 parent_mime_offset = GET_UINT32 (cache->buffer, parents_offset + 4 + 4 * k);
950 /* Don't add same parent multiple times.
951 * This can happen for instance if the same type is listed in multiple directories
953 for (l = 0; l < p; l++)
955 if (strcmp (all_parents[l], cache->buffer + parent_mime_offset) == 0)
956 break;
959 if (l == p)
960 all_parents[p++] = cache->buffer + parent_mime_offset;
963 break;
967 all_parents[p++] = NULL;
969 result = (char **) malloc (p * sizeof (char *));
970 memcpy (result, all_parents, p * sizeof (char *));
972 return result;
975 static const char *
976 cache_lookup_icon (const char *mime, int header)
978 const char *ptr;
979 int i, min, max, mid, cmp;
981 for (i = 0; _caches[i]; i++)
983 XdgMimeCache *cache = _caches[i];
984 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, header);
985 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
986 xdg_uint32_t offset;
988 min = 0;
989 max = n_entries - 1;
990 while (max >= min)
992 mid = (min + max) / 2;
994 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid);
995 ptr = cache->buffer + offset;
996 cmp = strcmp (ptr, mime);
998 if (cmp < 0)
999 min = mid + 1;
1000 else if (cmp > 0)
1001 max = mid - 1;
1002 else
1004 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid + 4);
1005 return cache->buffer + offset;
1010 return NULL;
1013 const char *
1014 _xdg_mime_cache_get_generic_icon (const char *mime)
1016 return cache_lookup_icon (mime, 36);
1019 const char *
1020 _xdg_mime_cache_get_icon (const char *mime)
1022 return cache_lookup_icon (mime, 32);
1025 static void
1026 dump_glob_node (XdgMimeCache *cache,
1027 xdg_uint32_t offset,
1028 int depth)
1030 xdg_unichar_t character;
1031 xdg_uint32_t mime_offset;
1032 xdg_uint32_t n_children;
1033 xdg_uint32_t child_offset;
1034 int i;
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++)
1041 printf (" ");
1042 printf ("%c", character);
1043 if (mime_offset)
1044 printf (" - %s", cache->buffer + mime_offset);
1045 printf ("\n");
1046 if (child_offset)
1048 for (i = 0; i < n_children; i++)
1049 dump_glob_node (cache, child_offset + 20 * i, depth + 1);
1053 void
1054 _xdg_mime_cache_glob_dump (void)
1056 int i, j;
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);