Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Side by Side Diff: base/third_party/xdg_mime/xdgmimecache.c

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

Powered by Google App Engine
This is Rietveld 408576698