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