nexmon – Blame information for rev 1

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