apps test: add new "monitor" subcommand
[platform/upstream/glib.git] / gio / xdgmime / xdgmimemagic.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* xdgmimemagic.: Private file.  Datastructure for storing magic files.
3  *
4  * More info can be found at http://www.freedesktop.org/standards/
5  *
6  * Copyright (C) 2003  Red Hat, Inc.
7  * Copyright (C) 2003  Jonathan Blandford <jrb@alum.mit.edu>
8  *
9  * Licensed under the Academic Free License version 2.0
10  * Or under the following terms:
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <assert.h>
31 #include "xdgmimemagic.h"
32 #include "xdgmimeint.h"
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <limits.h>
39
40 #ifndef FALSE
41 #define FALSE   (0)
42 #endif
43
44 #ifndef TRUE
45 #define TRUE    (!FALSE)
46 #endif
47
48 #if !defined getc_unlocked && !defined HAVE_GETC_UNLOCKED
49 # define getc_unlocked(fp) getc (fp)
50 #endif
51
52 typedef struct XdgMimeMagicMatch XdgMimeMagicMatch;
53 typedef struct XdgMimeMagicMatchlet XdgMimeMagicMatchlet;
54
55 typedef enum
56 {
57   XDG_MIME_MAGIC_SECTION,
58   XDG_MIME_MAGIC_MAGIC,
59   XDG_MIME_MAGIC_ERROR,
60   XDG_MIME_MAGIC_EOF
61 } XdgMimeMagicState;
62
63 struct XdgMimeMagicMatch
64 {
65   const char *mime_type;
66   int priority;
67   XdgMimeMagicMatchlet *matchlet;
68   XdgMimeMagicMatch *next;
69 };
70
71
72 struct XdgMimeMagicMatchlet
73 {
74   int indent;
75   int offset;
76   unsigned int value_length;
77   unsigned char *value;
78   unsigned char *mask;
79   unsigned int range_length;
80   unsigned int word_size;
81   XdgMimeMagicMatchlet *next;
82 };
83
84
85 struct XdgMimeMagic
86 {
87   XdgMimeMagicMatch *match_list;
88   int max_extent;
89 };
90
91 static XdgMimeMagicMatch *
92 _xdg_mime_magic_match_new (void)
93 {
94   return calloc (1, sizeof (XdgMimeMagicMatch));
95 }
96
97
98 static XdgMimeMagicMatchlet *
99 _xdg_mime_magic_matchlet_new (void)
100 {
101   XdgMimeMagicMatchlet *matchlet;
102
103   matchlet = malloc (sizeof (XdgMimeMagicMatchlet));
104
105   matchlet->indent = 0;
106   matchlet->offset = 0;
107   matchlet->value_length = 0;
108   matchlet->value = NULL;
109   matchlet->mask = NULL;
110   matchlet->range_length = 1;
111   matchlet->word_size = 1;
112   matchlet->next = NULL;
113
114   return matchlet;
115 }
116
117
118 static void
119 _xdg_mime_magic_matchlet_free (XdgMimeMagicMatchlet *mime_magic_matchlet)
120 {
121   if (mime_magic_matchlet)
122     {
123       if (mime_magic_matchlet->next)
124         _xdg_mime_magic_matchlet_free (mime_magic_matchlet->next);
125       if (mime_magic_matchlet->value)
126         free (mime_magic_matchlet->value);
127       if (mime_magic_matchlet->mask)
128         free (mime_magic_matchlet->mask);
129       free (mime_magic_matchlet);
130     }
131 }
132
133
134 /* Frees mime_magic_match and the remainder of its list
135  */
136 static void
137 _xdg_mime_magic_match_free (XdgMimeMagicMatch *mime_magic_match)
138 {
139   XdgMimeMagicMatch *ptr, *next;
140
141   ptr = mime_magic_match;
142   while (ptr)
143     {
144       next = ptr->next;
145
146       if (ptr->mime_type)
147         free ((void *) ptr->mime_type);
148       if (ptr->matchlet)
149         _xdg_mime_magic_matchlet_free (ptr->matchlet);
150       free (ptr);
151
152       ptr = next;
153     }
154 }
155
156 /* Reads in a hunk of data until a newline character or a '\000' is hit.  The
157  * returned string is null terminated, and doesn't include the newline.
158  */
159 static unsigned char *
160 _xdg_mime_magic_read_to_newline (FILE *magic_file,
161                                  int  *end_of_file)
162 {
163   unsigned char *retval;
164   int c;
165   int len, pos;
166
167   len = 128;
168   pos = 0;
169   retval = malloc (len);
170   *end_of_file = FALSE;
171
172   while (TRUE)
173     {
174       c = getc_unlocked (magic_file);
175       if (c == EOF)
176         {
177           *end_of_file = TRUE;
178           break;
179         }
180       if (c == '\n' || c == '\000')
181         break;
182       retval[pos++] = (unsigned char) c;
183       if (pos % 128 == 127)
184         {
185           len = len + 128;
186           retval = realloc (retval, len);
187         }
188     }
189
190   retval[pos] = '\000';
191   return retval;
192 }
193
194 /* Returns the number read from the file, or -1 if no number could be read.
195  */
196 static int
197 _xdg_mime_magic_read_a_number (FILE *magic_file,
198                                int  *end_of_file)
199 {
200   /* LONG_MAX is about 20 characters on my system */
201 #define MAX_NUMBER_SIZE 30
202   char number_string[MAX_NUMBER_SIZE + 1];
203   int pos = 0;
204   int c;
205   long retval = -1;
206
207   while (TRUE)
208     {
209       c = getc_unlocked (magic_file);
210
211       if (c == EOF)
212         {
213           *end_of_file = TRUE;
214           break;
215         }
216       if (! isdigit (c))
217         {
218           ungetc (c, magic_file);
219           break;
220         }
221       number_string[pos] = (char) c;
222       pos++;
223       if (pos == MAX_NUMBER_SIZE)
224         break;
225     }
226   if (pos > 0)
227     {
228       number_string[pos] = '\000';
229       errno = 0;
230       retval = strtol (number_string, NULL, 10);
231
232       if ((retval < INT_MIN) || (retval > INT_MAX) || (errno != 0))
233         return -1;
234     }
235
236   return retval;
237 }
238
239 /* Headers are of the format:
240  * [<priority>:<mime-type>]
241  */
242 static XdgMimeMagicState
243 _xdg_mime_magic_parse_header (FILE *magic_file, XdgMimeMagicMatch *match)
244 {
245   int c;
246   char *buffer;
247   char *end_ptr;
248   int end_of_file = 0;
249
250   assert (magic_file != NULL);
251   assert (match != NULL);
252
253   c = getc_unlocked (magic_file);
254   if (c == EOF)
255     return XDG_MIME_MAGIC_EOF;
256   if (c != '[')
257     return XDG_MIME_MAGIC_ERROR;
258
259   match->priority = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
260   if (end_of_file)
261     return XDG_MIME_MAGIC_EOF;
262   if (match->priority == -1)
263     return XDG_MIME_MAGIC_ERROR;
264
265   c = getc_unlocked (magic_file);
266   if (c == EOF)
267     return XDG_MIME_MAGIC_EOF;
268   if (c != ':')
269     return XDG_MIME_MAGIC_ERROR;
270
271   buffer = (char *)_xdg_mime_magic_read_to_newline (magic_file, &end_of_file);
272   if (end_of_file)
273     {
274       free (buffer);
275       return XDG_MIME_MAGIC_EOF;
276     }
277
278   end_ptr = buffer;
279   while (*end_ptr != ']' && *end_ptr != '\000' && *end_ptr != '\n')
280     end_ptr++;
281   if (*end_ptr != ']')
282     {
283       free (buffer);
284       return XDG_MIME_MAGIC_ERROR;
285     }
286   *end_ptr = '\000';
287
288   match->mime_type = strdup (buffer);
289   free (buffer);
290
291   return XDG_MIME_MAGIC_MAGIC;
292 }
293
294 static XdgMimeMagicState
295 _xdg_mime_magic_parse_error (FILE *magic_file)
296 {
297   int c;
298
299   while (1)
300     {
301       c = getc_unlocked (magic_file);
302       if (c == EOF)
303         return XDG_MIME_MAGIC_EOF;
304       if (c == '\n')
305         return XDG_MIME_MAGIC_SECTION;
306     }
307 }
308
309 /* Headers are of the format:
310  * [ indent ] ">" start-offset "=" value
311  * [ "&" mask ] [ "~" word-size ] [ "+" range-length ] "\n"
312  */
313 static XdgMimeMagicState
314 _xdg_mime_magic_parse_magic_line (FILE              *magic_file,
315                                   XdgMimeMagicMatch *match)
316 {
317   XdgMimeMagicMatchlet *matchlet;
318   int c;
319   int end_of_file;
320   int indent = 0;
321   int bytes_read;
322
323   assert (magic_file != NULL);
324
325   /* Sniff the buffer to make sure it's a valid line */
326   c = getc_unlocked (magic_file);
327   if (c == EOF)
328     return XDG_MIME_MAGIC_EOF;
329   else if (c == '[')
330     {
331       ungetc (c, magic_file);
332       return XDG_MIME_MAGIC_SECTION;
333     }
334   else if (c == '\n')
335     return XDG_MIME_MAGIC_MAGIC;
336
337   /* At this point, it must be a digit or a '>' */
338   end_of_file = FALSE;
339   if (isdigit (c))
340     {
341       ungetc (c, magic_file);
342       indent = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
343       if (end_of_file)
344         return XDG_MIME_MAGIC_EOF;
345       if (indent == -1)
346         return XDG_MIME_MAGIC_ERROR;
347       c = getc_unlocked (magic_file);
348       if (c == EOF)
349         return XDG_MIME_MAGIC_EOF;
350     }
351
352   if (c != '>')
353     return XDG_MIME_MAGIC_ERROR;
354
355   matchlet = _xdg_mime_magic_matchlet_new ();
356   matchlet->indent = indent;
357   matchlet->offset = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
358   if (end_of_file)
359     {
360       _xdg_mime_magic_matchlet_free (matchlet);
361       return XDG_MIME_MAGIC_EOF;
362     }
363   if (matchlet->offset == -1)
364     {
365       _xdg_mime_magic_matchlet_free (matchlet);
366       return XDG_MIME_MAGIC_ERROR;
367     }
368   c = getc_unlocked (magic_file);
369   if (c == EOF)
370     {
371       _xdg_mime_magic_matchlet_free (matchlet);
372       return XDG_MIME_MAGIC_EOF;
373     }
374   else if (c != '=')
375     {
376       _xdg_mime_magic_matchlet_free (matchlet);
377       return XDG_MIME_MAGIC_ERROR;
378     }
379
380   /* Next two bytes determine how long the value is */
381   matchlet->value_length = 0;
382   c = getc_unlocked (magic_file);
383   if (c == EOF)
384     {
385       _xdg_mime_magic_matchlet_free (matchlet);
386       return XDG_MIME_MAGIC_EOF;
387     }
388   matchlet->value_length = c & 0xFF;
389   matchlet->value_length = matchlet->value_length << 8;
390
391   c = getc_unlocked (magic_file);
392   if (c == EOF)
393     {
394       _xdg_mime_magic_matchlet_free (matchlet);
395       return XDG_MIME_MAGIC_EOF;
396     }
397   matchlet->value_length = matchlet->value_length + (c & 0xFF);
398
399   matchlet->value = malloc (matchlet->value_length);
400
401   /* OOM */
402   if (matchlet->value == NULL)
403     {
404       _xdg_mime_magic_matchlet_free (matchlet);
405       return XDG_MIME_MAGIC_ERROR;
406     }
407   bytes_read = fread (matchlet->value, 1, matchlet->value_length, magic_file);
408   if (bytes_read != matchlet->value_length)
409     {
410       _xdg_mime_magic_matchlet_free (matchlet);
411       if (feof (magic_file))
412         return XDG_MIME_MAGIC_EOF;
413       else
414         return XDG_MIME_MAGIC_ERROR;
415     }
416
417   c = getc_unlocked (magic_file);
418   if (c == '&')
419     {
420       matchlet->mask = malloc (matchlet->value_length);
421       /* OOM */
422       if (matchlet->mask == NULL)
423         {
424           _xdg_mime_magic_matchlet_free (matchlet);
425           return XDG_MIME_MAGIC_ERROR;
426         }
427       bytes_read = fread (matchlet->mask, 1, matchlet->value_length, magic_file);
428       if (bytes_read != matchlet->value_length)
429         {
430           _xdg_mime_magic_matchlet_free (matchlet);
431           if (feof (magic_file))
432             return XDG_MIME_MAGIC_EOF;
433           else
434             return XDG_MIME_MAGIC_ERROR;
435         }
436       c = getc_unlocked (magic_file);
437     }
438
439   if (c == '~')
440     {
441       matchlet->word_size = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
442       if (end_of_file)
443         {
444           _xdg_mime_magic_matchlet_free (matchlet);
445           return XDG_MIME_MAGIC_EOF;
446         }
447       if (matchlet->word_size != 0 &&
448           matchlet->word_size != 1 &&
449           matchlet->word_size != 2 &&
450           matchlet->word_size != 4)
451         {
452           _xdg_mime_magic_matchlet_free (matchlet);
453           return XDG_MIME_MAGIC_ERROR;
454         }
455       c = getc_unlocked (magic_file);
456     }
457
458   if (c == '+')
459     {
460       matchlet->range_length = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
461       if (end_of_file)
462         {
463           _xdg_mime_magic_matchlet_free (matchlet);
464           return XDG_MIME_MAGIC_EOF;
465         }
466       if (matchlet->range_length == -1)
467         {
468           _xdg_mime_magic_matchlet_free (matchlet);
469           return XDG_MIME_MAGIC_ERROR;
470         }
471       c = getc_unlocked (magic_file);
472     }
473
474
475   if (c == '\n')
476     {
477       /* We clean up the matchlet, byte swapping if needed */
478       if (matchlet->word_size > 1)
479         {
480 #if LITTLE_ENDIAN
481           int i;
482 #endif
483           if (matchlet->value_length % matchlet->word_size != 0)
484             {
485               _xdg_mime_magic_matchlet_free (matchlet);
486               return XDG_MIME_MAGIC_ERROR;
487             }
488           /* FIXME: need to get this defined in a <config.h> style file */
489 #if LITTLE_ENDIAN
490           for (i = 0; i < matchlet->value_length; i = i + matchlet->word_size)
491             {
492               if (matchlet->word_size == 2)
493                 *((xdg_uint16_t *) matchlet->value + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->value + i)));
494               else if (matchlet->word_size == 4)
495                 *((xdg_uint32_t *) matchlet->value + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->value + i)));
496               if (matchlet->mask)
497                 {
498                   if (matchlet->word_size == 2)
499                     *((xdg_uint16_t *) matchlet->mask + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->mask + i)));
500                   else if (matchlet->word_size == 4)
501                     *((xdg_uint32_t *) matchlet->mask + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->mask + i)));
502
503                 }
504             }
505 #endif
506         }
507
508       matchlet->next = match->matchlet;
509       match->matchlet = matchlet;
510
511
512       return XDG_MIME_MAGIC_MAGIC;
513     }
514
515   _xdg_mime_magic_matchlet_free (matchlet);
516   if (c == EOF)
517     return XDG_MIME_MAGIC_EOF;
518
519   return XDG_MIME_MAGIC_ERROR;
520 }
521
522 static int
523 _xdg_mime_magic_matchlet_compare_to_data (XdgMimeMagicMatchlet *matchlet,
524                                           const void           *data,
525                                           size_t                len)
526 {
527   int i, j;
528   for (i = matchlet->offset; i < matchlet->offset + matchlet->range_length; i++)
529     {
530       int valid_matchlet = TRUE;
531
532       if (i + matchlet->value_length > len)
533         return FALSE;
534
535       if (matchlet->mask)
536         {
537           for (j = 0; j < matchlet->value_length; j++)
538             {
539               if ((matchlet->value[j] & matchlet->mask[j]) !=
540                   ((((unsigned char *) data)[j + i]) & matchlet->mask[j]))
541                 {
542                   valid_matchlet = FALSE;
543                   break;
544                 }
545             }
546         }
547       else
548         {
549           for (j = 0; j <  matchlet->value_length; j++)
550             {
551               if (matchlet->value[j] != ((unsigned char *) data)[j + i])
552                 {
553                   valid_matchlet = FALSE;
554                   break;
555                 }
556             }
557         }
558       if (valid_matchlet)
559         return TRUE;
560     }
561   return FALSE;
562 }
563
564 static int
565 _xdg_mime_magic_matchlet_compare_level (XdgMimeMagicMatchlet *matchlet,
566                                         const void           *data,
567                                         size_t                len,
568                                         int                   indent)
569 {
570   while ((matchlet != NULL) && (matchlet->indent == indent))
571     {
572       if (_xdg_mime_magic_matchlet_compare_to_data (matchlet, data, len))
573         {
574           if ((matchlet->next == NULL) ||
575               (matchlet->next->indent <= indent))
576             return TRUE;
577
578           if (_xdg_mime_magic_matchlet_compare_level (matchlet->next,
579                                                       data,
580                                                       len,
581                                                       indent + 1))
582             return TRUE;
583         }
584
585       do
586         {
587           matchlet = matchlet->next;
588         }
589       while (matchlet && matchlet->indent > indent);
590     }
591
592   return FALSE;
593 }
594
595 static int
596 _xdg_mime_magic_match_compare_to_data (XdgMimeMagicMatch *match,
597                                        const void        *data,
598                                        size_t             len)
599 {
600   return _xdg_mime_magic_matchlet_compare_level (match->matchlet, data, len, 0);
601 }
602
603 static void
604 _xdg_mime_magic_insert_match (XdgMimeMagic      *mime_magic,
605                               XdgMimeMagicMatch *match)
606 {
607   XdgMimeMagicMatch *list;
608
609   if (mime_magic->match_list == NULL)
610     {
611       mime_magic->match_list = match;
612       return;
613     }
614
615   if (match->priority > mime_magic->match_list->priority)
616     {
617       match->next = mime_magic->match_list;
618       mime_magic->match_list = match;
619       return;
620     }
621
622   list = mime_magic->match_list;
623   while (list->next != NULL)
624     {
625       if (list->next->priority < match->priority)
626         {
627           match->next = list->next;
628           list->next = match;
629           return;
630         }
631       list = list->next;
632     }
633   list->next = match;
634   match->next = NULL;
635 }
636
637 XdgMimeMagic *
638 _xdg_mime_magic_new (void)
639 {
640   return calloc (1, sizeof (XdgMimeMagic));
641 }
642
643 void
644 _xdg_mime_magic_free (XdgMimeMagic *mime_magic)
645 {
646   if (mime_magic) {
647     _xdg_mime_magic_match_free (mime_magic->match_list);
648     free (mime_magic);
649   }
650 }
651
652 int
653 _xdg_mime_magic_get_buffer_extents (XdgMimeMagic *mime_magic)
654 {
655   return mime_magic->max_extent;
656 }
657
658 const char *
659 _xdg_mime_magic_lookup_data (XdgMimeMagic *mime_magic,
660                              const void   *data,
661                              size_t        len,
662                              int           *result_prio,
663                              const char   *mime_types[],
664                              int           n_mime_types)
665 {
666   XdgMimeMagicMatch *match;
667   const char *mime_type;
668   int n;
669   int prio;
670
671   prio = 0;
672   mime_type = NULL;
673   for (match = mime_magic->match_list; match; match = match->next)
674     {
675       if (_xdg_mime_magic_match_compare_to_data (match, data, len))
676         {
677           prio = match->priority;
678           mime_type = match->mime_type;
679           break;
680         }
681       else 
682         {
683           for (n = 0; n < n_mime_types; n++)
684             {
685               if (mime_types[n] && 
686                   _xdg_mime_mime_type_equal (mime_types[n], match->mime_type))
687                 mime_types[n] = NULL;
688             }
689         }
690     }
691
692   if (mime_type == NULL)
693     {
694       for (n = 0; n < n_mime_types; n++)
695         {
696           if (mime_types[n])
697             mime_type = mime_types[n];
698         }
699     }
700   
701   if (result_prio)
702     *result_prio = prio;
703
704   return mime_type;
705 }
706
707 static void
708 _xdg_mime_update_mime_magic_extents (XdgMimeMagic *mime_magic)
709 {
710   XdgMimeMagicMatch *match;
711   int max_extent = 0;
712
713   for (match = mime_magic->match_list; match; match = match->next)
714     {
715       XdgMimeMagicMatchlet *matchlet;
716
717       for (matchlet = match->matchlet; matchlet; matchlet = matchlet->next)
718         {
719           int extent;
720
721           extent = matchlet->value_length + matchlet->offset + matchlet->range_length;
722           if (max_extent < extent)
723             max_extent = extent;
724         }
725     }
726
727   mime_magic->max_extent = max_extent;
728 }
729
730 static XdgMimeMagicMatchlet *
731 _xdg_mime_magic_matchlet_mirror (XdgMimeMagicMatchlet *matchlets)
732 {
733   XdgMimeMagicMatchlet *new_list;
734   XdgMimeMagicMatchlet *tmp;
735
736   if ((matchlets == NULL) || (matchlets->next == NULL))
737     return matchlets;
738
739   new_list = NULL;
740   tmp = matchlets;
741   while (tmp != NULL)
742     {
743       XdgMimeMagicMatchlet *matchlet;
744
745       matchlet = tmp;
746       tmp = tmp->next;
747       matchlet->next = new_list;
748       new_list = matchlet;
749     }
750
751   return new_list;
752
753 }
754
755 static void
756 _xdg_mime_magic_read_magic_file (XdgMimeMagic *mime_magic,
757                                  FILE         *magic_file)
758 {
759   XdgMimeMagicState state;
760   XdgMimeMagicMatch *match = NULL; /* Quiet compiler */
761
762   state = XDG_MIME_MAGIC_SECTION;
763
764   while (state != XDG_MIME_MAGIC_EOF)
765     {
766       switch (state)
767         {
768         case XDG_MIME_MAGIC_SECTION:
769           match = _xdg_mime_magic_match_new ();
770           state = _xdg_mime_magic_parse_header (magic_file, match);
771           if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
772             _xdg_mime_magic_match_free (match);
773           break;
774         case XDG_MIME_MAGIC_MAGIC:
775           state = _xdg_mime_magic_parse_magic_line (magic_file, match);
776           if (state == XDG_MIME_MAGIC_SECTION ||
777               (state == XDG_MIME_MAGIC_EOF && match->mime_type))
778             {
779               match->matchlet = _xdg_mime_magic_matchlet_mirror (match->matchlet);
780               _xdg_mime_magic_insert_match (mime_magic, match);
781             }
782           else if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
783             _xdg_mime_magic_match_free (match);
784           break;
785         case XDG_MIME_MAGIC_ERROR:
786           state = _xdg_mime_magic_parse_error (magic_file);
787           break;
788         case XDG_MIME_MAGIC_EOF:
789         default:
790           /* Make the compiler happy */
791           assert (0);
792         }
793     }
794   _xdg_mime_update_mime_magic_extents (mime_magic);
795 }
796
797 void
798 _xdg_mime_magic_read_from_file (XdgMimeMagic *mime_magic,
799                                 const char   *file_name)
800 {
801   FILE *magic_file;
802   char header[12];
803
804   magic_file = fopen (file_name, "r");
805
806   if (magic_file == NULL)
807     return;
808
809   if (fread (header, 1, 12, magic_file) == 12)
810     {
811       if (memcmp ("MIME-Magic\0\n", header, 12) == 0)
812         _xdg_mime_magic_read_magic_file (mime_magic, magic_file);
813     }
814
815   fclose (magic_file);
816 }