Include "config.h" instead of <config.h> Command used: find -name
[platform/upstream/glib.git] / gio / xdgmime / xdgmimecache.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* xdgmimealias.c: Private file.  mmappable caches for mime data
3  *
4  * More info can be found at http://www.freedesktop.org/standards/
5  *
6  * Copyright (C) 2005  Matthias Clasen <mclasen@redhat.com>
7  *
8  * Licensed under the Academic Free License version 2.0
9  * Or under the following terms:
10  *
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.
15  *
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.
20  *
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.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <fnmatch.h>
38 #include <assert.h>
39
40 #include <netinet/in.h> /* for ntohl/ntohs */
41
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
47
48 #include <sys/stat.h>
49 #include <sys/types.h>
50
51 #include "xdgmimecache.h"
52 #include "xdgmimeint.h"
53
54 #ifndef MAX
55 #define MAX(a,b) ((a) > (b) ? (a) : (b))
56 #endif
57
58 #ifndef FALSE
59 #define FALSE   (0)
60 #endif
61
62 #ifndef TRUE
63 #define TRUE    (!FALSE)
64 #endif
65
66 #ifndef _O_BINARY
67 #define _O_BINARY 0
68 #endif
69
70 #ifndef MAP_FAILED
71 #define MAP_FAILED ((void *) -1)
72 #endif
73
74 #define MAJOR_VERSION 1
75 #define MINOR_VERSION 1
76
77 struct _XdgMimeCache
78 {
79   int ref_count;
80
81   size_t  size;
82   char   *buffer;
83 };
84
85 #define GET_UINT16(cache,offset) (ntohs(*(xdg_uint16_t*)((cache) + (offset))))
86 #define GET_UINT32(cache,offset) (ntohl(*(xdg_uint32_t*)((cache) + (offset))))
87
88 XdgMimeCache *
89 _xdg_mime_cache_ref (XdgMimeCache *cache)
90 {
91   cache->ref_count++;
92   return cache;
93 }
94
95 void
96 _xdg_mime_cache_unref (XdgMimeCache *cache)
97 {
98   cache->ref_count--;
99
100   if (cache->ref_count == 0)
101     {
102 #ifdef HAVE_MMAP
103       munmap (cache->buffer, cache->size);
104 #endif
105       free (cache);
106     }
107 }
108
109 XdgMimeCache *
110 _xdg_mime_cache_new_from_file (const char *file_name)
111 {
112   XdgMimeCache *cache = NULL;
113
114 #ifdef HAVE_MMAP
115   int fd = -1;
116   struct stat st;
117   char *buffer = NULL;
118
119   /* Open the file and map it into memory */
120   fd = open (file_name, O_RDONLY|_O_BINARY, 0);
121
122   if (fd < 0)
123     return NULL;
124   
125   if (fstat (fd, &st) < 0 || st.st_size < 4)
126     goto done;
127
128   buffer = (char *) mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
129
130   if (buffer == MAP_FAILED)
131     goto done;
132
133   /* Verify version */
134   if (GET_UINT16 (buffer, 0) != MAJOR_VERSION ||
135       GET_UINT16 (buffer, 2) != MINOR_VERSION)
136     {
137       munmap (buffer, st.st_size);
138
139       goto done;
140     }
141   
142   cache = (XdgMimeCache *) malloc (sizeof (XdgMimeCache));
143   cache->ref_count = 1;
144   cache->buffer = buffer;
145   cache->size = st.st_size;
146
147  done:
148   if (fd != -1)
149     close (fd);
150
151 #endif  /* HAVE_MMAP */
152
153   return cache;
154 }
155
156 static int
157 cache_magic_matchlet_compare_to_data (XdgMimeCache *cache, 
158                                       xdg_uint32_t  offset,
159                                       const void   *data,
160                                       size_t        len)
161 {
162   xdg_uint32_t range_start = GET_UINT32 (cache->buffer, offset);
163   xdg_uint32_t range_length = GET_UINT32 (cache->buffer, offset + 4);
164   xdg_uint32_t data_length = GET_UINT32 (cache->buffer, offset + 12);
165   xdg_uint32_t data_offset = GET_UINT32 (cache->buffer, offset + 16);
166   xdg_uint32_t mask_offset = GET_UINT32 (cache->buffer, offset + 20);
167   
168   int i, j;
169
170   for (i = range_start; i <= range_start + range_length; i++)
171     {
172       int valid_matchlet = TRUE;
173       
174       if (i + data_length > len)
175         return FALSE;
176
177       if (mask_offset)
178         {
179           for (j = 0; j < data_length; j++)
180             {
181               if ((((unsigned char *)cache->buffer)[data_offset + j] & ((unsigned char *)cache->buffer)[mask_offset + j]) !=
182                   ((((unsigned char *) data)[j + i]) & ((unsigned char *)cache->buffer)[mask_offset + j]))
183                 {
184                   valid_matchlet = FALSE;
185                   break;
186                 }
187             }
188         }
189       else
190         {
191           for (j = 0; j < data_length; j++)
192             {
193               if (((unsigned char *)cache->buffer)[data_offset + j] != ((unsigned char *) data)[j + i])
194                 {
195                   valid_matchlet = FALSE;
196                   break;
197                 }
198             }
199         }
200       
201       if (valid_matchlet)
202         return TRUE;
203     }
204   
205   return FALSE;  
206 }
207
208 static int
209 cache_magic_matchlet_compare (XdgMimeCache *cache, 
210                               xdg_uint32_t  offset,
211                               const void   *data,
212                               size_t        len)
213 {
214   xdg_uint32_t n_children = GET_UINT32 (cache->buffer, offset + 24);
215   xdg_uint32_t child_offset = GET_UINT32 (cache->buffer, offset + 28);
216
217   int i;
218   
219   if (cache_magic_matchlet_compare_to_data (cache, offset, data, len))
220     {
221       if (n_children == 0)
222         return TRUE;
223       
224       for (i = 0; i < n_children; i++)
225         {
226           if (cache_magic_matchlet_compare (cache, child_offset + 32 * i,
227                                             data, len))
228             return TRUE;
229         }
230     }
231   
232   return FALSE;  
233 }
234
235 static const char *
236 cache_magic_compare_to_data (XdgMimeCache *cache, 
237                              xdg_uint32_t  offset,
238                              const void   *data, 
239                              size_t        len, 
240                              int          *prio)
241 {
242   xdg_uint32_t priority = GET_UINT32 (cache->buffer, offset);
243   xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, offset + 4);
244   xdg_uint32_t n_matchlets = GET_UINT32 (cache->buffer, offset + 8);
245   xdg_uint32_t matchlet_offset = GET_UINT32 (cache->buffer, offset + 12);
246
247   int i;
248
249   for (i = 0; i < n_matchlets; i++)
250     {
251       if (cache_magic_matchlet_compare (cache, matchlet_offset + i * 32, 
252                                         data, len))
253         {
254           *prio = priority;
255           
256           return cache->buffer + mimetype_offset;
257         }
258     }
259
260   return NULL;
261 }
262
263 static const char *
264 cache_magic_lookup_data (XdgMimeCache *cache, 
265                          const void   *data, 
266                          size_t        len, 
267                          int          *prio,
268                          const char   *mime_types[],
269                          int           n_mime_types)
270 {
271   xdg_uint32_t list_offset;
272   xdg_uint32_t n_entries;
273   xdg_uint32_t offset;
274
275   int j, n;
276
277   *prio = 0;
278
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);
282   
283   for (j = 0; j < n_entries; j++)
284     {
285       const char *match;
286
287       match = cache_magic_compare_to_data (cache, offset + 16 * j, 
288                                            data, len, prio);
289       if (match)
290         return match;
291       else
292         {
293           xdg_uint32_t mimetype_offset;
294           const char *non_match;
295           
296           mimetype_offset = GET_UINT32 (cache->buffer, offset + 16 * j + 4);
297           non_match = cache->buffer + mimetype_offset;
298
299           for (n = 0; n < n_mime_types; n++)
300             {
301               if (mime_types[n] && 
302                   _xdg_mime_mime_type_equal (mime_types[n], non_match))
303                 mime_types[n] = NULL;
304             }
305         }
306     }
307
308   return NULL;
309 }
310
311 static const char *
312 cache_alias_lookup (const char *alias)
313 {
314   const char *ptr;
315   int i, min, max, mid, cmp;
316
317   for (i = 0; _caches[i]; i++)
318     {
319       XdgMimeCache *cache = _caches[i];
320       xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 4);
321       xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
322       xdg_uint32_t offset;
323
324       min = 0; 
325       max = n_entries - 1;
326       while (max >= min) 
327         {
328           mid = (min + max) / 2;
329
330           offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid);
331           ptr = cache->buffer + offset;
332           cmp = strcmp (ptr, alias);
333           
334           if (cmp < 0)
335             min = mid + 1;
336           else if (cmp > 0)
337             max = mid - 1;
338           else
339             {
340               offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid + 4);
341               return cache->buffer + offset;
342             }
343         }
344     }
345
346   return NULL;
347 }
348
349 typedef struct {
350   const char *mime;
351   int weight;
352 } MimeWeight;
353
354 static int
355 cache_glob_lookup_literal (const char *file_name,
356                            const char *mime_types[],
357                            int         n_mime_types)
358 {
359   const char *ptr;
360   int i, min, max, mid, cmp;
361
362   for (i = 0; _caches[i]; i++)
363     {
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;
368
369       min = 0; 
370       max = n_entries - 1;
371       while (max >= min) 
372         {
373           mid = (min + max) / 2;
374
375           offset = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * mid);
376           ptr = cache->buffer + offset;
377           cmp = strcmp (ptr, file_name);
378           
379           if (cmp < 0)
380             min = mid + 1;
381           else if (cmp > 0)
382             max = mid - 1;
383           else
384             {
385               offset = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * mid + 4);
386               mime_types[0] = (const char *)(cache->buffer + offset);
387               
388               return 1;
389             }
390         }
391     }
392
393   return 0;
394 }
395
396 static int
397 cache_glob_lookup_fnmatch (const char *file_name,
398                            MimeWeight  mime_types[],
399                            int         n_mime_types)
400 {
401   const char *mime_type;
402   const char *ptr;
403
404   int i, j, n;
405
406   n = 0;
407   for (i = 0; _caches[i]; i++)
408     {
409       XdgMimeCache *cache = _caches[i];
410
411       xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 20);
412       xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
413
414       for (j = 0; j < n_entries && n < n_mime_types; j++)
415         {
416           xdg_uint32_t offset = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * j);
417           xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * j + 4);
418           int weight = GET_UINT32 (cache->buffer, list_offset + 4 + 12 * j + 8);
419           ptr = cache->buffer + offset;
420           mime_type = cache->buffer + mimetype_offset;
421
422           /* FIXME: Not UTF-8 safe */
423           if (fnmatch (ptr, file_name, 0) == 0)
424             {
425               mime_types[n].mime = mime_type;
426               mime_types[n].weight = weight;
427               n++;
428             }
429         }
430
431       if (n > 0)
432         return n;
433     }
434   
435   return 0;
436 }
437
438 static int
439 cache_glob_node_lookup_suffix (XdgMimeCache  *cache,
440                                xdg_uint32_t   n_entries,
441                                xdg_uint32_t   offset,
442                                xdg_unichar_t *file_name,
443                                int            len,
444                                int            ignore_case,
445                                MimeWeight     mime_types[],
446                                int            n_mime_types)
447 {
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; 
453   int weight;
454
455   int min, max, mid, n, i;
456
457   character = file_name[len - 1];
458   if (ignore_case)
459     character = _xdg_ucs4_to_lower (character);
460
461   assert (character != 0);
462
463   min = 0;
464   max = n_entries - 1;
465   while (max >= min)
466     {
467       mid = (min + max) /  2;
468       match_char = GET_UINT32 (cache->buffer, offset + 12 * mid);
469       if (match_char < character)
470         min = mid + 1;
471       else if (match_char > character)
472         max = mid - 1;
473       else 
474         {
475           len--;
476           n = 0;
477           n_children = GET_UINT32 (cache->buffer, offset + 12 * mid + 4);
478           child_offset = GET_UINT32 (cache->buffer, offset + 12 * mid + 8);
479       
480           if (len > 0)
481             {
482               n = cache_glob_node_lookup_suffix (cache, 
483                                                  n_children, child_offset,
484                                                  file_name, len, 
485                                                  ignore_case,
486                                                  mime_types,
487                                                  n_mime_types);
488             }
489           if (n == 0)
490             {
491               i = 0;
492               while (n < n_mime_types && i < n_children)
493                 {
494                   match_char = GET_UINT32 (cache->buffer, child_offset + 12 * i);
495                   if (match_char != 0)
496                     break;
497
498                   mimetype_offset = GET_UINT32 (cache->buffer, child_offset + 12 * i + 4);
499                   weight = GET_UINT32 (cache->buffer, child_offset + 12 * i + 8);
500
501                   mime_types[n].mime = cache->buffer + mimetype_offset;
502                   mime_types[n].weight = weight;
503                   n++;
504                   i++;
505                 }
506             }
507           return n;
508         }
509     }
510   return 0;
511 }
512
513 static int
514 cache_glob_lookup_suffix (xdg_unichar_t *file_name,
515                           int            len,
516                           int            ignore_case,
517                           MimeWeight     mime_types[],
518                           int            n_mime_types)
519 {
520   int i, n;
521
522   for (i = 0; _caches[i]; i++)
523     {
524       XdgMimeCache *cache = _caches[i];
525
526       xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 16);
527       xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
528       xdg_uint32_t offset = GET_UINT32 (cache->buffer, list_offset + 4);
529
530       n = cache_glob_node_lookup_suffix (cache, 
531                                          n_entries, offset, 
532                                          file_name, len,
533                                          ignore_case,
534                                          mime_types,
535                                          n_mime_types);
536       if (n > 0)
537         return n;
538     }
539
540   return 0;
541 }
542
543 static int compare_mime_weight (const void *a, const void *b)
544 {
545   const MimeWeight *aa = (const MimeWeight *)a;
546   const MimeWeight *bb = (const MimeWeight *)b;
547
548   return aa->weight - bb->weight;
549 }
550
551 static int
552 cache_glob_lookup_file_name (const char *file_name, 
553                              const char *mime_types[],
554                              int         n_mime_types)
555 {
556   int n;
557   MimeWeight mimes[10];
558   int n_mimes = 10;
559   int i;
560   xdg_unichar_t *ucs4;
561   int len;
562   
563   assert (file_name != NULL && n_mime_types > 0);
564
565   /* First, check the literals */
566   n = cache_glob_lookup_literal (file_name, mime_types, n_mime_types);
567   if (n > 0)
568     return n;
569
570   ucs4 = _xdg_convert_to_ucs4 (file_name, &len);
571   n = cache_glob_lookup_suffix (ucs4, len, FALSE, mimes, n_mimes);
572
573   if (n == 0)
574     n = cache_glob_lookup_suffix (ucs4, len, TRUE, mimes, n_mimes);
575   free(ucs4);
576   
577   /* Last, try fnmatch */
578   if (n == 0)
579     n = cache_glob_lookup_fnmatch (file_name, mimes, n_mimes);
580
581   qsort (mimes, n, sizeof (MimeWeight), compare_mime_weight);
582
583   if (n_mime_types < n)
584     n = n_mime_types;
585
586   for (i = 0; i < n; i++)
587     mime_types[i] = mimes[i].mime;
588
589   return n;
590 }
591
592 int
593 _xdg_mime_cache_get_max_buffer_extents (void)
594 {
595   xdg_uint32_t offset;
596   xdg_uint32_t max_extent;
597   int i;
598
599   max_extent = 0;
600   for (i = 0; _caches[i]; i++)
601     {
602       XdgMimeCache *cache = _caches[i];
603
604       offset = GET_UINT32 (cache->buffer, 24);
605       max_extent = MAX (max_extent, GET_UINT32 (cache->buffer, offset + 4));
606     }
607
608   return max_extent;
609 }
610
611 static const char *
612 cache_get_mime_type_for_data (const void *data,
613                               size_t      len,
614                               int        *result_prio,
615                               const char *mime_types[],
616                               int         n_mime_types)
617 {
618   const char *mime_type;
619   int i, n, priority;
620
621   priority = 0;
622   mime_type = NULL;
623   for (i = 0; _caches[i]; i++)
624     {
625       XdgMimeCache *cache = _caches[i];
626
627       int prio;
628       const char *match;
629
630       match = cache_magic_lookup_data (cache, data, len, &prio, 
631                                        mime_types, n_mime_types);
632       if (prio > priority)
633         {
634           priority = prio;
635           mime_type = match;
636         }
637     }
638
639   if (result_prio)
640     *result_prio = priority;
641   
642   if (priority > 0)
643     return mime_type;
644
645   for (n = 0; n < n_mime_types; n++)
646     {
647       
648       if (mime_types[n])
649         return mime_types[n];
650     }
651
652   return XDG_MIME_TYPE_UNKNOWN;
653 }
654
655 const char *
656 _xdg_mime_cache_get_mime_type_for_data (const void *data,
657                                         size_t      len,
658                                         int        *result_prio)
659 {
660   return cache_get_mime_type_for_data (data, len, result_prio, NULL, 0);
661 }
662
663 const char *
664 _xdg_mime_cache_get_mime_type_for_file (const char  *file_name,
665                                         struct stat *statbuf)
666 {
667   const char *mime_type;
668   const char *mime_types[10];
669   FILE *file;
670   unsigned char *data;
671   int max_extent;
672   int bytes_read;
673   struct stat buf;
674   const char *base_name;
675   int n;
676
677   if (file_name == NULL)
678     return NULL;
679
680   if (! _xdg_utf8_validate (file_name))
681     return NULL;
682
683   base_name = _xdg_get_base_name (file_name);
684   n = cache_glob_lookup_file_name (base_name, mime_types, 10);
685
686   if (n == 1)
687     return mime_types[0];
688
689   if (!statbuf)
690     {
691       if (stat (file_name, &buf) != 0)
692         return XDG_MIME_TYPE_UNKNOWN;
693
694       statbuf = &buf;
695     }
696
697   if (!S_ISREG (statbuf->st_mode))
698     return XDG_MIME_TYPE_UNKNOWN;
699
700   /* FIXME: Need to make sure that max_extent isn't totally broken.  This could
701    * be large and need getting from a stream instead of just reading it all
702    * in. */
703   max_extent = _xdg_mime_cache_get_max_buffer_extents ();
704   data = malloc (max_extent);
705   if (data == NULL)
706     return XDG_MIME_TYPE_UNKNOWN;
707         
708   file = fopen (file_name, "r");
709   if (file == NULL)
710     {
711       free (data);
712       return XDG_MIME_TYPE_UNKNOWN;
713     }
714
715   bytes_read = fread (data, 1, max_extent, file);
716   if (ferror (file))
717     {
718       free (data);
719       fclose (file);
720       return XDG_MIME_TYPE_UNKNOWN;
721     }
722
723   mime_type = cache_get_mime_type_for_data (data, bytes_read, NULL,
724                                             mime_types, n);
725
726   free (data);
727   fclose (file);
728
729   return mime_type;
730 }
731
732 const char *
733 _xdg_mime_cache_get_mime_type_from_file_name (const char *file_name)
734 {
735   const char *mime_type;
736
737   if (cache_glob_lookup_file_name (file_name, &mime_type, 1))
738     return mime_type;
739   else
740     return XDG_MIME_TYPE_UNKNOWN;
741 }
742
743 int
744 _xdg_mime_cache_get_mime_types_from_file_name (const char *file_name,
745                                                const char  *mime_types[],
746                                                int          n_mime_types)
747 {
748   return cache_glob_lookup_file_name (file_name, mime_types, n_mime_types);
749 }
750
751 #if 1
752 static int
753 is_super_type (const char *mime)
754 {
755   int length;
756   const char *type;
757
758   length = strlen (mime);
759   type = &(mime[length - 2]);
760
761   if (strcmp (type, "/*") == 0)
762     return 1;
763
764   return 0;
765 }
766 #endif
767
768 int
769 _xdg_mime_cache_mime_type_subclass (const char *mime,
770                                     const char *base)
771 {
772   const char *umime, *ubase;
773
774   int i, j, min, max, med, cmp;
775   
776   umime = _xdg_mime_cache_unalias_mime_type (mime);
777   ubase = _xdg_mime_cache_unalias_mime_type (base);
778
779   if (strcmp (umime, ubase) == 0)
780     return 1;
781
782   /* We really want to handle text/ * in GtkFileFilter, so we just
783    * turn on the supertype matching
784    */
785 #if 1
786   /* Handle supertypes */
787   if (is_super_type (ubase) &&
788       xdg_mime_media_type_equal (umime, ubase))
789     return 1;
790 #endif
791
792   /*  Handle special cases text/plain and application/octet-stream */
793   if (strcmp (ubase, "text/plain") == 0 && 
794       strncmp (umime, "text/", 5) == 0)
795     return 1;
796
797   if (strcmp (ubase, "application/octet-stream") == 0)
798     return 1;
799  
800   for (i = 0; _caches[i]; i++)
801     {
802       XdgMimeCache *cache = _caches[i];
803       
804       xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 8);
805       xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
806       xdg_uint32_t offset, n_parents, parent_offset;
807
808       min = 0; 
809       max = n_entries - 1;
810       while (max >= min)
811         {
812           med = (min + max)/2;
813           
814           offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * med);
815           cmp = strcmp (cache->buffer + offset, umime);
816           if (cmp < 0)
817             min = med + 1;
818           else if (cmp > 0)
819             max = med - 1;
820           else
821             {
822               offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * med + 4);
823               n_parents = GET_UINT32 (cache->buffer, offset);
824               
825               for (j = 0; j < n_parents; j++)
826                 {
827                   parent_offset = GET_UINT32 (cache->buffer, offset + 4 + 4 * j);
828                   if (_xdg_mime_cache_mime_type_subclass (cache->buffer + parent_offset, ubase))
829                     return 1;
830                 }
831
832               break;
833             }
834         }
835     }
836
837   return 0;
838 }
839
840 const char *
841 _xdg_mime_cache_unalias_mime_type (const char *mime)
842 {
843   const char *lookup;
844   
845   lookup = cache_alias_lookup (mime);
846   
847   if (lookup)
848     return lookup;
849   
850   return mime;  
851 }
852
853 char **
854 _xdg_mime_cache_list_mime_parents (const char *mime)
855 {
856   int i, j, k, l, p;
857   char *all_parents[128]; /* we'll stop at 128 */ 
858   char **result;
859
860   mime = xdg_mime_unalias_mime_type (mime);
861
862   p = 0;
863   for (i = 0; _caches[i]; i++)
864     {
865       XdgMimeCache *cache = _caches[i];
866   
867       xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, 8);
868       xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
869
870       for (j = 0; j < n_entries; j++)
871         {
872           xdg_uint32_t mimetype_offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * j);
873           xdg_uint32_t parents_offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * j + 4);
874
875           if (strcmp (cache->buffer + mimetype_offset, mime) == 0)
876             {
877               xdg_uint32_t parent_mime_offset;
878               xdg_uint32_t n_parents = GET_UINT32 (cache->buffer, parents_offset);
879
880               for (k = 0; k < n_parents && p < 127; k++)
881                 {
882                   parent_mime_offset = GET_UINT32 (cache->buffer, parents_offset + 4 + 4 * k);
883
884                   /* Don't add same parent multiple times.
885                    * This can happen for instance if the same type is listed in multiple directories
886                    */
887                   for (l = 0; l < p; l++)
888                     {
889                       if (strcmp (all_parents[l], cache->buffer + parent_mime_offset) == 0)
890                         break;
891                     }
892
893                   if (l == p)
894                     all_parents[p++] = cache->buffer + parent_mime_offset;
895                 }
896
897               break;
898             }
899         }
900     }
901   all_parents[p++] = NULL;
902   
903   result = (char **) malloc (p * sizeof (char *));
904   memcpy (result, all_parents, p * sizeof (char *));
905
906   return result;
907 }
908
909 static const char *
910 cache_lookup_icon (const char *mime, int header)
911 {
912   const char *ptr;
913   int i, min, max, mid, cmp;
914
915   for (i = 0; _caches[i]; i++)
916     {
917       XdgMimeCache *cache = _caches[i];
918       xdg_uint32_t list_offset = GET_UINT32 (cache->buffer, header);
919       xdg_uint32_t n_entries = GET_UINT32 (cache->buffer, list_offset);
920       xdg_uint32_t offset;
921
922       min = 0; 
923       max = n_entries - 1;
924       while (max >= min) 
925         {
926           mid = (min + max) / 2;
927
928           offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid);
929           ptr = cache->buffer + offset;
930           cmp = strcmp (ptr, mime);
931          
932           if (cmp < 0)
933             min = mid + 1;
934           else if (cmp > 0)
935             max = mid - 1;
936           else
937             {
938               offset = GET_UINT32 (cache->buffer, list_offset + 4 + 8 * mid + 4);
939               return cache->buffer + offset;
940             }
941         }
942     }
943
944   return NULL;
945 }
946
947 const char *
948 _xdg_mime_cache_get_generic_icon (const char *mime)
949 {
950   return cache_lookup_icon (mime, 36);
951 }
952
953 const char *
954 _xdg_mime_cache_get_icon (const char *mime)
955 {
956   const char *icon;
957  
958   icon = cache_lookup_icon (mime, 32);
959  
960   if (icon == NULL)
961     icon = _xdg_mime_cache_get_generic_icon (mime);
962
963   return icon;
964 }
965
966 static void
967 dump_glob_node (XdgMimeCache *cache,
968                 xdg_uint32_t  offset,
969                 int           depth)
970 {
971   xdg_unichar_t character;
972   xdg_uint32_t mime_offset;
973   xdg_uint32_t n_children;
974   xdg_uint32_t child_offset;
975   int i;
976
977   character = GET_UINT32 (cache->buffer, offset);
978   mime_offset = GET_UINT32 (cache->buffer, offset + 4);
979   n_children = GET_UINT32 (cache->buffer, offset + 8);
980   child_offset = GET_UINT32 (cache->buffer, offset + 12);
981   for (i = 0; i < depth; i++)
982     printf (" ");
983   printf ("%c", character);
984   if (mime_offset)
985     printf (" - %s", cache->buffer + mime_offset);
986   printf ("\n");
987   if (child_offset)
988   {
989     for (i = 0; i < n_children; i++)
990       dump_glob_node (cache, child_offset + 20 * i, depth + 1);
991   }
992 }
993
994 void
995 _xdg_mime_cache_glob_dump (void)
996 {
997   int i, j;
998   for (i = 0; _caches[i]; i++)
999   {
1000     XdgMimeCache *cache = _caches[i];
1001     xdg_uint32_t list_offset;
1002     xdg_uint32_t n_entries;
1003     xdg_uint32_t offset;
1004     list_offset = GET_UINT32 (cache->buffer, 16);
1005     n_entries = GET_UINT32 (cache->buffer, list_offset);
1006     offset = GET_UINT32 (cache->buffer, list_offset + 4);
1007     for (j = 0; j < n_entries; j++)
1008             dump_glob_node (cache, offset + 20 * j, 0);
1009   }
1010 }
1011
1012