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