Converted README to markdown
[rox-filer.git] / ROX-Filer / src / xdgmimecache.c
blob0ecaa4fc1eb085f013f54a86474b726df6c8d3a3
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.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 valid_matchlet = memcmp(cache->buffer + data_offset, data + i, data_length) == 0;
200 if (valid_matchlet)
201 return TRUE;
204 return FALSE;
207 static int
208 cache_magic_matchlet_compare (XdgMimeCache *cache,
209 xdg_uint32_t offset,
210 const void *data,
211 size_t len)
213 xdg_uint32_t n_children = GET_UINT32 (cache->buffer, offset + 24);
214 xdg_uint32_t child_offset = GET_UINT32 (cache->buffer, offset + 28);
216 int i;
218 if (cache_magic_matchlet_compare_to_data (cache, offset, data, len))
220 if (n_children == 0)
221 return TRUE;
223 for (i = 0; i < n_children; i++)
225 if (cache_magic_matchlet_compare (cache, child_offset + 32 * i,
226 data, len))
227 return TRUE;
231 return FALSE;
234 static const char *
235 cache_magic_compare_to_data (XdgMimeCache *cache,
236 xdg_uint32_t offset,
237 const void *data,
238 size_t len,
239 int *prio)
241 xdg_uint32_t priority = GET_UINT32 (cache->buffer, offset);
242 xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, offset + 4);
243 xdg_uint32_t n_matchlets = GET_UINT32 (cache->buffer, offset + 8);
244 xdg_uint32_t matchlet_offset = GET_UINT32 (cache->buffer, offset + 12);
246 int i;
248 for (i = 0; i < n_matchlets; i++)
250 if (cache_magic_matchlet_compare (cache, matchlet_offset + i * 32,
251 data, len))
253 *prio = priority;
255 return cache->buffer + mimetype_offset;
259 return NULL;
262 static const char *
263 cache_magic_lookup_data (XdgMimeCache *cache,
264 const void *data,
265 size_t len,
266 int *prio,
267 const char *mime_types[],
268 int n_mime_types)
270 xdg_uint32_t list_offset;
271 xdg_uint32_t n_entries;
272 xdg_uint32_t offset;
274 int j, n;
276 *prio = 0;
278 list_offset = GET_UINT32 (cache->buffer, 24);
279 n_entries = GET_UINT32 (cache->buffer, list_offset);
280 offset = GET_UINT32 (cache->buffer, list_offset + 8);
282 for (j = 0; j < n_entries; j++)
284 const char *match;
286 match = cache_magic_compare_to_data (cache, offset + 16 * j,
287 data, len, prio);
288 if (match)
289 return match;
290 else
292 xdg_uint32_t mimetype_offset;
293 const char *non_match;
295 mimetype_offset = GET_UINT32 (cache->buffer, offset + 16 * j + 4);
296 non_match = cache->buffer + mimetype_offset;
298 for (n = 0; n < n_mime_types; n++)
300 if (mime_types[n] &&
301 _xdg_mime_mime_type_equal (mime_types[n], non_match))
302 mime_types[n] = NULL;
307 return NULL;
310 static const char *
311 cache_alias_lookup (const char *alias)
313 const char *ptr;
314 int i, min, max, mid, cmp;
316 for (i = 0; _caches[i]; i++)
318 XdgMimeCache *cache = _caches[i];
319 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 4);
320 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
321 xdg_uint32_t offset;
323 min = 0;
324 max = n_entries - 1;
325 while (max >= min)
327 mid = (min + max) / 2;
329 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid);
330 ptr = cache->buffer + offset;
331 cmp = strcmp (ptr, alias);
333 if (cmp < 0)
334 min = mid + 1;
335 else if (cmp > 0)
336 max = mid - 1;
337 else
339 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid + 4);
340 return cache->buffer + offset;
345 return NULL;
348 typedef struct {
349 const char *mime;
350 int weight;
351 } MimeWeight;
353 static int
354 cache_glob_lookup_literal (const char *file_name,
355 const char *mime_types[],
356 int n_mime_types,
357 int case_sensitive_check)
359 const char *ptr;
360 int i, min, max, mid, cmp;
362 for (i = 0; _caches[i]; i++)
364 XdgMimeCache *cache = _caches[i];
365 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 12);
366 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
367 xdg_uint32_t offset;
369 min = 0;
370 max = n_entries - 1;
371 while (max >= min)
373 mid = (min + max) / 2;
375 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * mid);
376 ptr = cache->buffer + offset;
377 cmp = strcmp (ptr, file_name);
379 if (cmp < 0)
380 min = mid + 1;
381 else if (cmp > 0)
382 max = mid - 1;
383 else
385 int weight = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * mid + 8);
386 int case_sensitive = weight & 0x100;
387 weight = weight & 0xff;
389 if (case_sensitive_check || !case_sensitive)
391 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * mid + 4);
392 mime_types[0] = (const char *)(cache->buffer + offset);
394 return 1;
396 return 0;
401 return 0;
404 static int
405 cache_glob_lookup_fnmatch (const char *file_name,
406 MimeWeight mime_types[],
407 int n_mime_types,
408 int case_sensitive_check)
410 const char *mime_type;
411 const char *ptr;
413 int i, j, n;
415 n = 0;
416 for (i = 0; _caches[i]; i++)
418 XdgMimeCache *cache = _caches[i];
420 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 20);
421 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
423 for (j = 0; j < n_entries && n < n_mime_types; j++)
425 xdg_uint32_t offset = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * j);
426 xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * j + 4);
427 int weight = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * j + 8);
428 int case_sensitive = weight & 0x100;
429 weight = weight & 0xff;
430 ptr = cache->buffer + offset;
431 mime_type = cache->buffer + mimetype_offset;
432 if (case_sensitive_check || !case_sensitive)
434 /* FIXME: Not UTF-8 safe */
435 if (fnmatch (ptr, file_name, 0) == 0)
437 mime_types[n].mime = mime_type;
438 mime_types[n].weight = weight;
439 n++;
444 if (n > 0)
445 return n;
448 return 0;
451 static int
452 cache_glob_node_lookup_suffix (XdgMimeCache *cache,
453 xdg_uint32_t n_entries,
454 xdg_uint32_t offset,
455 const char *file_name,
456 int len,
457 int case_sensitive_check,
458 MimeWeight mime_types[],
459 int n_mime_types)
461 xdg_unichar_t character;
462 xdg_unichar_t match_char;
463 xdg_uint32_t mimetype_offset;
464 xdg_uint32_t n_children;
465 xdg_uint32_t child_offset;
466 int weight;
467 int case_sensitive;
469 int min, max, mid, n, i;
471 character = file_name[len - 1];
473 assert (character != 0);
475 min = 0;
476 max = n_entries - 1;
477 while (max >= min)
479 mid = (min + max) / 2;
480 match_char = GET_UINT32 (cache->buffer, offset + 12 * mid);
481 if (match_char < character)
482 min = mid + 1;
483 else if (match_char > character)
484 max = mid - 1;
485 else
487 len--;
488 n = 0;
489 n_children = GET_UINT32 (cache->buffer, offset + 12 * mid + 4);
490 child_offset = GET_UINT32 (cache->buffer, offset + 12 * mid + 8);
492 if (len > 0)
494 n = cache_glob_node_lookup_suffix (cache,
495 n_children, child_offset,
496 file_name, len,
497 case_sensitive_check,
498 mime_types,
499 n_mime_types);
501 if (n == 0)
503 i = 0;
504 while (n < n_mime_types && i < n_children)
506 match_char = GET_UINT32 (cache->buffer, child_offset + 12 * i);
507 if (match_char != 0)
508 break;
510 mimetype_offset = GET_UINT32 (cache->buffer, child_offset + 12 * i + 4);
511 weight = GET_UINT32 (cache->buffer, child_offset + 12 * i + 8);
512 case_sensitive = weight & 0x100;
513 weight = weight & 0xff;
515 if (case_sensitive_check || !case_sensitive)
517 mime_types[n].mime = cache->buffer + mimetype_offset;
518 mime_types[n].weight = weight;
519 n++;
521 i++;
524 return n;
527 return 0;
530 static int
531 cache_glob_lookup_suffix (const char *file_name,
532 int len,
533 int ignore_case,
534 MimeWeight mime_types[],
535 int n_mime_types)
537 int i, n;
539 for (i = 0; _caches[i]; i++)
541 XdgMimeCache *cache = _caches[i];
543 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 16);
544 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
545 xdg_uint32_t offset = GET_UINT32 (cache->buffer, list_offset + 4);
547 n = cache_glob_node_lookup_suffix (cache,
548 n_entries, offset,
549 file_name, len,
550 ignore_case,
551 mime_types,
552 n_mime_types);
553 if (n > 0)
554 return n;
557 return 0;
560 static int compare_mime_weight (const void *a, const void *b)
562 const MimeWeight *aa = (const MimeWeight *)a;
563 const MimeWeight *bb = (const MimeWeight *)b;
565 return bb->weight - aa->weight;
568 #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
569 static char *
570 ascii_tolower (const char *str)
572 char *p, *lower;
574 lower = strdup (str);
575 p = lower;
576 while (*p != 0)
578 char c = *p;
579 *p++ = ISUPPER (c) ? c - 'A' + 'a' : c;
581 return lower;
584 static int
585 cache_glob_lookup_file_name (const char *file_name,
586 const char *mime_types[],
587 int n_mime_types)
589 int n;
590 MimeWeight mimes[10];
591 int n_mimes = 10;
592 int i;
593 int len;
594 char *lower_case;
596 assert (file_name != NULL && n_mime_types > 0);
598 /* First, check the literals */
600 lower_case = ascii_tolower (file_name);
602 n = cache_glob_lookup_literal (lower_case, mime_types, n_mime_types, FALSE);
603 if (n > 0)
605 free (lower_case);
606 return n;
609 n = cache_glob_lookup_literal (file_name, mime_types, n_mime_types, TRUE);
610 if (n > 0)
612 free (lower_case);
613 return n;
616 len = strlen (file_name);
617 n = cache_glob_lookup_suffix (lower_case, len, FALSE, mimes, n_mimes);
618 if (n == 0)
619 n = cache_glob_lookup_suffix (file_name, len, TRUE, mimes, n_mimes);
621 /* Last, try fnmatch */
622 if (n == 0)
623 n = cache_glob_lookup_fnmatch (lower_case, mimes, n_mimes, FALSE);
624 if (n == 0)
625 n = cache_glob_lookup_fnmatch (file_name, mimes, n_mimes, TRUE);
627 free (lower_case);
629 qsort (mimes, n, sizeof (MimeWeight), compare_mime_weight);
631 if (n_mime_types < n)
632 n = n_mime_types;
634 for (i = 0; i < n; i++)
635 mime_types[i] = mimes[i].mime;
637 return n;
641 _xdg_mime_cache_get_max_buffer_extents (void)
643 xdg_uint32_t offset;
644 xdg_uint32_t max_extent;
645 int i;
647 max_extent = 0;
648 for (i = 0; _caches[i]; i++)
650 XdgMimeCache *cache = _caches[i];
652 offset = GET_UINT32 (cache->buffer, 24);
653 max_extent = MAX (max_extent, GET_UINT32 (cache->buffer, offset + 4));
656 return max_extent;
659 static const char *
660 cache_get_mime_type_for_data (const void *data,
661 size_t len,
662 int *result_prio,
663 const char *mime_types[],
664 int n_mime_types)
666 const char *mime_type;
667 int i, n, priority;
669 priority = 0;
670 mime_type = NULL;
671 for (i = 0; _caches[i]; i++)
673 XdgMimeCache *cache = _caches[i];
675 int prio;
676 const char *match;
678 match = cache_magic_lookup_data (cache, data, len, &prio,
679 mime_types, n_mime_types);
680 if (prio > priority)
682 priority = prio;
683 mime_type = match;
687 if (result_prio)
688 *result_prio = priority;
690 if (priority > 0)
692 /* Pick glob-result R where mime_type inherits from R */
693 for (n = 0; n < n_mime_types; n++)
695 if (mime_types[n] && _xdg_mime_cache_mime_type_subclass(mime_types[n], mime_type))
696 return mime_types[n];
699 /* Return magic match */
700 return mime_type;
703 /* Pick first glob result, as fallback */
704 for (n = 0; n < n_mime_types; n++)
706 if (mime_types[n])
707 return mime_types[n];
710 return NULL;
713 const char *
714 _xdg_mime_cache_get_mime_type_for_data (const void *data,
715 size_t len,
716 int *result_prio)
718 return cache_get_mime_type_for_data (data, len, result_prio, NULL, 0);
721 const char *
722 _xdg_mime_cache_get_mime_type_for_file (const char *file_name,
723 struct stat *statbuf)
725 const char *mime_type;
726 const char *mime_types[10];
727 FILE *file;
728 unsigned char *data;
729 int max_extent;
730 int bytes_read;
731 struct stat buf;
732 const char *base_name;
733 int n;
735 if (file_name == NULL)
736 return NULL;
738 if (! _xdg_utf8_validate (file_name))
739 return NULL;
741 base_name = _xdg_get_base_name (file_name);
742 n = cache_glob_lookup_file_name (base_name, mime_types, 10);
744 if (n == 1)
745 return mime_types[0];
747 if (!statbuf)
749 if (stat (file_name, &buf) != 0)
750 return XDG_MIME_TYPE_UNKNOWN;
752 statbuf = &buf;
755 if (statbuf->st_size == 0)
756 return XDG_MIME_TYPE_EMPTY;
758 if (!S_ISREG (statbuf->st_mode))
759 return XDG_MIME_TYPE_UNKNOWN;
761 /* FIXME: Need to make sure that max_extent isn't totally broken. This could
762 * be large and need getting from a stream instead of just reading it all
763 * in. */
764 max_extent = _xdg_mime_cache_get_max_buffer_extents ();
765 data = malloc (max_extent);
766 if (data == NULL)
767 return XDG_MIME_TYPE_UNKNOWN;
769 file = fopen (file_name, "r");
770 if (file == NULL)
772 free (data);
773 return XDG_MIME_TYPE_UNKNOWN;
776 bytes_read = fread (data, 1, max_extent, file);
777 if (ferror (file))
779 free (data);
780 fclose (file);
781 return XDG_MIME_TYPE_UNKNOWN;
784 mime_type = cache_get_mime_type_for_data (data, bytes_read, NULL,
785 mime_types, n);
787 if (!mime_type)
788 mime_type = _xdg_binary_or_text_fallback(data, bytes_read);
790 free (data);
791 fclose (file);
793 return mime_type;
796 const char *
797 _xdg_mime_cache_get_mime_type_from_file_name (const char *file_name)
799 const char *mime_type;
801 if (cache_glob_lookup_file_name (file_name, &mime_type, 1))
802 return mime_type;
803 else
804 return XDG_MIME_TYPE_UNKNOWN;
808 _xdg_mime_cache_get_mime_types_from_file_name (const char *file_name,
809 const char *mime_types[],
810 int n_mime_types)
812 return cache_glob_lookup_file_name (file_name, mime_types, n_mime_types);
815 #if 1
816 static int
817 is_super_type (const char *mime)
819 int length;
820 const char *type;
822 length = strlen (mime);
823 type = &(mime[length - 2]);
825 if (strcmp (type, "/*") == 0)
826 return 1;
828 return 0;
830 #endif
833 _xdg_mime_cache_mime_type_subclass (const char *mime,
834 const char *base)
836 const char *umime, *ubase;
838 int i, j, min, max, med, cmp;
840 umime = _xdg_mime_cache_unalias_mime_type (mime);
841 ubase = _xdg_mime_cache_unalias_mime_type (base);
843 if (strcmp (umime, ubase) == 0)
844 return 1;
846 /* We really want to handle text/ * in GtkFileFilter, so we just
847 * turn on the supertype matching
849 #if 1
850 /* Handle supertypes */
851 if (is_super_type (ubase) &&
852 xdg_mime_media_type_equal (umime, ubase))
853 return 1;
854 #endif
856 /* Handle special cases text/plain and application/octet-stream */
857 if (strcmp (ubase, "text/plain") == 0 &&
858 strncmp (umime, "text/", 5) == 0)
859 return 1;
861 if (strcmp (ubase, "application/octet-stream") == 0)
862 return 1;
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;
872 min = 0;
873 max = n_entries - 1;
874 while (max >= min)
876 med = (min + max)/2;
878 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * med);
879 cmp = strcmp (cache->buffer + offset, umime);
880 if (cmp < 0)
881 min = med + 1;
882 else if (cmp > 0)
883 max = med - 1;
884 else
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))
893 return 1;
896 break;
901 return 0;
904 const char *
905 _xdg_mime_cache_unalias_mime_type (const char *mime)
907 const char *lookup;
909 lookup = cache_alias_lookup (mime);
911 if (lookup)
912 return lookup;
914 return mime;
917 char **
918 _xdg_mime_cache_list_mime_parents (const char *mime)
920 int i, j, k, l, p;
921 char *all_parents[128]; /* we'll stop at 128 */
922 char **result;
924 mime = xdg_mime_unalias_mime_type (mime);
926 p = 0;
927 for (i = 0; _caches[i]; i++)
929 XdgMimeCache *cache = _caches[i];
931 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 8);
932 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
934 for (j = 0; j < n_entries; j++)
936 xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * j);
937 xdg_uint32_t parents_offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * j + 4);
939 if (strcmp (cache->buffer + mimetype_offset, mime) == 0)
941 xdg_uint32_t parent_mime_offset;
942 xdg_uint32_t n_parents = GET_UINT32 (cache->buffer, parents_offset);
944 for (k = 0; k < n_parents && p < 127; k++)
946 parent_mime_offset = GET_UINT32 (cache->buffer, parents_offset + 4 + 4 * k);
948 /* Don't add same parent multiple times.
949 * This can happen for instance if the same type is listed in multiple directories
951 for (l = 0; l < p; l++)
953 if (strcmp (all_parents[l], cache->buffer + parent_mime_offset) == 0)
954 break;
957 if (l == p)
958 all_parents[p++] = cache->buffer + parent_mime_offset;
961 break;
965 all_parents[p++] = NULL;
967 result = (char **) malloc (p * sizeof (char *));
968 memcpy (result, all_parents, p * sizeof (char *));
970 return result;
973 static const char *
974 cache_lookup_icon (const char *mime, int header)
976 const char *ptr;
977 int i, min, max, mid, cmp;
979 for (i = 0; _caches[i]; i++)
981 XdgMimeCache *cache = _caches[i];
982 xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, header);
983 xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
984 xdg_uint32_t offset;
986 min = 0;
987 max = n_entries - 1;
988 while (max >= min)
990 mid = (min + max) / 2;
992 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid);
993 ptr = cache->buffer + offset;
994 cmp = strcmp (ptr, mime);
996 if (cmp < 0)
997 min = mid + 1;
998 else if (cmp > 0)
999 max = mid - 1;
1000 else
1002 offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid + 4);
1003 return cache->buffer + offset;
1008 return NULL;
1011 const char *
1012 _xdg_mime_cache_get_generic_icon (const char *mime)
1014 return cache_lookup_icon (mime, 36);
1017 const char *
1018 _xdg_mime_cache_get_icon (const char *mime)
1020 return cache_lookup_icon (mime, 32);
1023 static void
1024 dump_glob_node (XdgMimeCache *cache,
1025 xdg_uint32_t offset,
1026 int depth)
1028 xdg_unichar_t character;
1029 xdg_uint32_t mime_offset;
1030 xdg_uint32_t n_children;
1031 xdg_uint32_t child_offset;
1032 int i;
1034 character = GET_UINT32 (cache->buffer, offset);
1035 mime_offset = GET_UINT32 (cache->buffer, offset + 4);
1036 n_children = GET_UINT32 (cache->buffer, offset + 8);
1037 child_offset = GET_UINT32 (cache->buffer, offset + 12);
1038 for (i = 0; i < depth; i++)
1039 printf (" ");
1040 printf ("%c", character);
1041 if (mime_offset)
1042 printf (" - %s", cache->buffer + mime_offset);
1043 printf ("\n");
1044 if (child_offset)
1046 for (i = 0; i < n_children; i++)
1047 dump_glob_node (cache, child_offset + 20 * i, depth + 1);
1051 void
1052 _xdg_mime_cache_glob_dump (void)
1054 int i, j;
1055 for (i = 0; _caches[i]; i++)
1057 XdgMimeCache *cache = _caches[i];
1058 xdg_uint32_t list_offset;
1059 xdg_uint32_t n_entries;
1060 xdg_uint32_t offset;
1061 list_offset = GET_UINT32 (cache->buffer, 16);
1062 n_entries = GET_UINT32 (cache->buffer, list_offset);
1063 offset = GET_UINT32 (cache->buffer, list_offset + 4);
1064 for (j = 0; j < n_entries; j++)
1065 dump_glob_node (cache, offset + 20 * j, 0);