* add xdgmime from freedesktop.org
[citadel.git] / libcitadel / lib / 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, 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   *end_of_file = FALSE;
173
174   while (TRUE)
175     {
176       c = getc_unlocked (magic_file);
177       if (c == EOF)
178         {
179           *end_of_file = TRUE;
180           break;
181         }
182       if (c == '\n' || c == '\000')
183         break;
184       retval[pos++] = (unsigned char) c;
185       if (pos % 128 == 127)
186         {
187           len = len + 128;
188           retval = realloc (retval, len);
189         }
190     }
191
192   retval[pos] = '\000';
193   return retval;
194 }
195
196 /* Returns the number read from the file, or -1 if no number could be read.
197  */
198 static int
199 _xdg_mime_magic_read_a_number (FILE *magic_file,
200                                int  *end_of_file)
201 {
202   /* LONG_MAX is about 20 characters on my system */
203 #define MAX_NUMBER_SIZE 30
204   char number_string[MAX_NUMBER_SIZE + 1];
205   int pos = 0;
206   int c;
207   long retval = -1;
208
209   while (TRUE)
210     {
211       c = getc_unlocked (magic_file);
212
213       if (c == EOF)
214         {
215           *end_of_file = TRUE;
216           break;
217         }
218       if (! isdigit (c))
219         {
220           ungetc (c, magic_file);
221           break;
222         }
223       number_string[pos] = (char) c;
224       pos++;
225       if (pos == MAX_NUMBER_SIZE)
226         break;
227     }
228   if (pos > 0)
229     {
230       number_string[pos] = '\000';
231       errno = 0;
232       retval = strtol (number_string, NULL, 10);
233
234       if ((retval < INT_MIN) || (retval > INT_MAX) || (errno != 0))
235         return -1;
236     }
237
238   return retval;
239 }
240
241 /* Headers are of the format:
242  * [<priority>:<mime-type>]
243  */
244 static XdgMimeMagicState
245 _xdg_mime_magic_parse_header (FILE *magic_file, XdgMimeMagicMatch *match)
246 {
247   int c;
248   char *buffer;
249   char *end_ptr;
250   int end_of_file = 0;
251
252   assert (magic_file != NULL);
253   assert (match != NULL);
254
255   c = getc_unlocked (magic_file);
256   if (c == EOF)
257     return XDG_MIME_MAGIC_EOF;
258   if (c != '[')
259     return XDG_MIME_MAGIC_ERROR;
260
261   match->priority = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
262   if (end_of_file)
263     return XDG_MIME_MAGIC_EOF;
264   if (match->priority == -1)
265     return XDG_MIME_MAGIC_ERROR;
266
267   c = getc_unlocked (magic_file);
268   if (c == EOF)
269     return XDG_MIME_MAGIC_EOF;
270   if (c != ':')
271     return XDG_MIME_MAGIC_ERROR;
272
273   buffer = (char *)_xdg_mime_magic_read_to_newline (magic_file, &end_of_file);
274   if (end_of_file)
275     return XDG_MIME_MAGIC_EOF;
276
277   end_ptr = buffer;
278   while (*end_ptr != ']' && *end_ptr != '\000' && *end_ptr != '\n')
279     end_ptr++;
280   if (*end_ptr != ']')
281     {
282       free (buffer);
283       return XDG_MIME_MAGIC_ERROR;
284     }
285   *end_ptr = '\000';
286
287   match->mime_type = strdup (buffer);
288   free (buffer);
289
290   return XDG_MIME_MAGIC_MAGIC;
291 }
292
293 static XdgMimeMagicState
294 _xdg_mime_magic_parse_error (FILE *magic_file)
295 {
296   int c;
297
298   while (1)
299     {
300       c = getc_unlocked (magic_file);
301       if (c == EOF)
302         return XDG_MIME_MAGIC_EOF;
303       if (c == '\n')
304         return XDG_MIME_MAGIC_SECTION;
305     }
306 }
307
308 /* Headers are of the format:
309  * [ indent ] ">" start-offset "=" value
310  * [ "&" mask ] [ "~" word-size ] [ "+" range-length ] "\n"
311  */
312 static XdgMimeMagicState
313 _xdg_mime_magic_parse_magic_line (FILE              *magic_file,
314                                   XdgMimeMagicMatch *match)
315 {
316   XdgMimeMagicMatchlet *matchlet;
317   int c;
318   int end_of_file;
319   int indent = 0;
320   int bytes_read;
321
322   assert (magic_file != NULL);
323
324   /* Sniff the buffer to make sure it's a valid line */
325   c = getc_unlocked (magic_file);
326   if (c == EOF)
327     return XDG_MIME_MAGIC_EOF;
328   else if (c == '[')
329     {
330       ungetc (c, magic_file);
331       return XDG_MIME_MAGIC_SECTION;
332     }
333   else if (c == '\n')
334     return XDG_MIME_MAGIC_MAGIC;
335
336   /* At this point, it must be a digit or a '>' */
337   end_of_file = FALSE;
338   if (isdigit (c))
339     {
340       ungetc (c, magic_file);
341       indent = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
342       if (end_of_file)
343         return XDG_MIME_MAGIC_EOF;
344       if (indent == -1)
345         return XDG_MIME_MAGIC_ERROR;
346       c = getc_unlocked (magic_file);
347       if (c == EOF)
348         return XDG_MIME_MAGIC_EOF;
349     }
350
351   if (c != '>')
352     return XDG_MIME_MAGIC_ERROR;
353
354   matchlet = _xdg_mime_magic_matchlet_new ();
355   matchlet->indent = indent;
356   matchlet->offset = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
357   if (end_of_file)
358     {
359       _xdg_mime_magic_matchlet_free (matchlet);
360       return XDG_MIME_MAGIC_EOF;
361     }
362   if (matchlet->offset == -1)
363     {
364       _xdg_mime_magic_matchlet_free (matchlet);
365       return XDG_MIME_MAGIC_ERROR;
366     }
367   c = getc_unlocked (magic_file);
368   if (c == EOF)
369     {
370       _xdg_mime_magic_matchlet_free (matchlet);
371       return XDG_MIME_MAGIC_EOF;
372     }
373   else if (c != '=')
374     {
375       _xdg_mime_magic_matchlet_free (matchlet);
376       return XDG_MIME_MAGIC_ERROR;
377     }
378
379   /* Next two bytes determine how long the value is */
380   matchlet->value_length = 0;
381   c = getc_unlocked (magic_file);
382   if (c == EOF)
383     {
384       _xdg_mime_magic_matchlet_free (matchlet);
385       return XDG_MIME_MAGIC_EOF;
386     }
387   matchlet->value_length = c & 0xFF;
388   matchlet->value_length = matchlet->value_length << 8;
389
390   c = getc_unlocked (magic_file);
391   if (c == EOF)
392     {
393       _xdg_mime_magic_matchlet_free (matchlet);
394       return XDG_MIME_MAGIC_EOF;
395     }
396   matchlet->value_length = matchlet->value_length + (c & 0xFF);
397
398   matchlet->value = malloc (matchlet->value_length);
399
400   /* OOM */
401   if (matchlet->value == NULL)
402     {
403       _xdg_mime_magic_matchlet_free (matchlet);
404       return XDG_MIME_MAGIC_ERROR;
405     }
406   bytes_read = fread (matchlet->value, 1, matchlet->value_length, magic_file);
407   if (bytes_read != matchlet->value_length)
408     {
409       _xdg_mime_magic_matchlet_free (matchlet);
410       if (feof (magic_file))
411         return XDG_MIME_MAGIC_EOF;
412       else
413         return XDG_MIME_MAGIC_ERROR;
414     }
415
416   c = getc_unlocked (magic_file);
417   if (c == '&')
418     {
419       matchlet->mask = malloc (matchlet->value_length);
420       /* OOM */
421       if (matchlet->mask == NULL)
422         {
423           _xdg_mime_magic_matchlet_free (matchlet);
424           return XDG_MIME_MAGIC_ERROR;
425         }
426       bytes_read = fread (matchlet->mask, 1, matchlet->value_length, magic_file);
427       if (bytes_read != matchlet->value_length)
428         {
429           _xdg_mime_magic_matchlet_free (matchlet);
430           if (feof (magic_file))
431             return XDG_MIME_MAGIC_EOF;
432           else
433             return XDG_MIME_MAGIC_ERROR;
434         }
435       c = getc_unlocked (magic_file);
436     }
437
438   if (c == '~')
439     {
440       matchlet->word_size = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
441       if (end_of_file)
442         {
443           _xdg_mime_magic_matchlet_free (matchlet);
444           return XDG_MIME_MAGIC_EOF;
445         }
446       if (matchlet->word_size != 0 &&
447           matchlet->word_size != 1 &&
448           matchlet->word_size != 2 &&
449           matchlet->word_size != 4)
450         {
451           _xdg_mime_magic_matchlet_free (matchlet);
452           return XDG_MIME_MAGIC_ERROR;
453         }
454       c = getc_unlocked (magic_file);
455     }
456
457   if (c == '+')
458     {
459       matchlet->range_length = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
460       if (end_of_file)
461         {
462           _xdg_mime_magic_matchlet_free (matchlet);
463           return XDG_MIME_MAGIC_EOF;
464         }
465       if (matchlet->range_length == -1)
466         {
467           _xdg_mime_magic_matchlet_free (matchlet);
468           return XDG_MIME_MAGIC_ERROR;
469         }
470       c = getc_unlocked (magic_file);
471     }
472
473
474   if (c == '\n')
475     {
476       /* We clean up the matchlet, byte swapping if needed */
477       if (matchlet->word_size > 1)
478         {
479 #if LITTLE_ENDIAN
480           int i;
481 #endif
482           if (matchlet->value_length % matchlet->word_size != 0)
483             {
484               _xdg_mime_magic_matchlet_free (matchlet);
485               return XDG_MIME_MAGIC_ERROR;
486             }
487           /* FIXME: need to get this defined in a <config.h> style file */
488 #if LITTLE_ENDIAN
489           for (i = 0; i < matchlet->value_length; i = i + matchlet->word_size)
490             {
491               if (matchlet->word_size == 2)
492                 *((xdg_uint16_t *) matchlet->value + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->value + i)));
493               else if (matchlet->word_size == 4)
494                 *((xdg_uint32_t *) matchlet->value + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->value + i)));
495               if (matchlet->mask)
496                 {
497                   if (matchlet->word_size == 2)
498                     *((xdg_uint16_t *) matchlet->mask + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->mask + i)));
499                   else if (matchlet->word_size == 4)
500                     *((xdg_uint32_t *) matchlet->mask + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->mask + i)));
501
502                 }
503             }
504 #endif
505         }
506
507       matchlet->next = match->matchlet;
508       match->matchlet = matchlet;
509
510
511       return XDG_MIME_MAGIC_MAGIC;
512     }
513
514   _xdg_mime_magic_matchlet_free (matchlet);
515   if (c == EOF)
516     return XDG_MIME_MAGIC_EOF;
517
518   return XDG_MIME_MAGIC_ERROR;
519 }
520
521 static int
522 _xdg_mime_magic_matchlet_compare_to_data (XdgMimeMagicMatchlet *matchlet,
523                                           const void           *data,
524                                           size_t                len)
525 {
526   int i, j;
527   for (i = matchlet->offset; i < matchlet->offset + matchlet->range_length; i++)
528     {
529       int valid_matchlet = TRUE;
530
531       if (i + matchlet->value_length > len)
532         return FALSE;
533
534       if (matchlet->mask)
535         {
536           for (j = 0; j < matchlet->value_length; j++)
537             {
538               if ((matchlet->value[j] & matchlet->mask[j]) !=
539                   ((((unsigned char *) data)[j + i]) & matchlet->mask[j]))
540                 {
541                   valid_matchlet = FALSE;
542                   break;
543                 }
544             }
545         }
546       else
547         {
548           for (j = 0; j <  matchlet->value_length; j++)
549             {
550               if (matchlet->value[j] != ((unsigned char *) data)[j + i])
551                 {
552                   valid_matchlet = FALSE;
553                   break;
554                 }
555             }
556         }
557       if (valid_matchlet)
558         return TRUE;
559     }
560   return FALSE;
561 }
562
563 static int
564 _xdg_mime_magic_matchlet_compare_level (XdgMimeMagicMatchlet *matchlet,
565                                         const void           *data,
566                                         size_t                len,
567                                         int                   indent)
568 {
569   while ((matchlet != NULL) && (matchlet->indent == indent))
570     {
571       if (_xdg_mime_magic_matchlet_compare_to_data (matchlet, data, len))
572         {
573           if ((matchlet->next == NULL) ||
574               (matchlet->next->indent <= indent))
575             return TRUE;
576
577           if (_xdg_mime_magic_matchlet_compare_level (matchlet->next,
578                                                       data,
579                                                       len,
580                                                       indent + 1))
581             return TRUE;
582         }
583
584       do
585         {
586           matchlet = matchlet->next;
587         }
588       while (matchlet && matchlet->indent > indent);
589     }
590
591   return FALSE;
592 }
593
594 static int
595 _xdg_mime_magic_match_compare_to_data (XdgMimeMagicMatch *match,
596                                        const void        *data,
597                                        size_t             len)
598 {
599   return _xdg_mime_magic_matchlet_compare_level (match->matchlet, data, len, 0);
600 }
601
602 static void
603 _xdg_mime_magic_insert_match (XdgMimeMagic      *mime_magic,
604                               XdgMimeMagicMatch *match)
605 {
606   XdgMimeMagicMatch *list;
607
608   if (mime_magic->match_list == NULL)
609     {
610       mime_magic->match_list = match;
611       return;
612     }
613
614   if (match->priority > mime_magic->match_list->priority)
615     {
616       match->next = mime_magic->match_list;
617       mime_magic->match_list = match;
618       return;
619     }
620
621   list = mime_magic->match_list;
622   while (list->next != NULL)
623     {
624       if (list->next->priority < match->priority)
625         {
626           match->next = list->next;
627           list->next = match;
628           return;
629         }
630       list = list->next;
631     }
632   list->next = match;
633   match->next = NULL;
634 }
635
636 XdgMimeMagic *
637 _xdg_mime_magic_new (void)
638 {
639   return calloc (1, sizeof (XdgMimeMagic));
640 }
641
642 void
643 _xdg_mime_magic_free (XdgMimeMagic *mime_magic)
644 {
645   if (mime_magic) {
646     _xdg_mime_magic_match_free (mime_magic->match_list);
647     free (mime_magic);
648   }
649 }
650
651 int
652 _xdg_mime_magic_get_buffer_extents (XdgMimeMagic *mime_magic)
653 {
654   return mime_magic->max_extent;
655 }
656
657 const char *
658 _xdg_mime_magic_lookup_data (XdgMimeMagic *mime_magic,
659                              const void   *data,
660                              size_t        len,
661                              const char   *mime_types[],
662                              int           n_mime_types)
663 {
664   XdgMimeMagicMatch *match;
665   const char *mime_type;
666   int n;
667   int priority;
668   int had_match;
669
670   mime_type = NULL;
671   priority = 0;
672   had_match = 0;
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           if (!had_match || match->priority > priority ||
678               (mime_type != NULL && _xdg_mime_mime_type_subclass (match->mime_type, mime_type)))
679             {
680               mime_type = match->mime_type;
681               priority = match->priority;
682             }
683           else if (had_match && match->priority == priority &&
684                    !_xdg_mime_mime_type_subclass (mime_type, match->mime_type))
685             /* multiple unrelated patterns with the same priority matched,
686              * so we can't tell what type this is. */
687             mime_type = NULL;
688
689           had_match = 1;
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   return mime_type;
712 }
713
714 static void
715 _xdg_mime_update_mime_magic_extents (XdgMimeMagic *mime_magic)
716 {
717   XdgMimeMagicMatch *match;
718   int max_extent = 0;
719
720   for (match = mime_magic->match_list; match; match = match->next)
721     {
722       XdgMimeMagicMatchlet *matchlet;
723
724       for (matchlet = match->matchlet; matchlet; matchlet = matchlet->next)
725         {
726           int extent;
727
728           extent = matchlet->value_length + matchlet->offset + matchlet->range_length;
729           if (max_extent < extent)
730             max_extent = extent;
731         }
732     }
733
734   mime_magic->max_extent = max_extent;
735 }
736
737 static XdgMimeMagicMatchlet *
738 _xdg_mime_magic_matchlet_mirror (XdgMimeMagicMatchlet *matchlets)
739 {
740   XdgMimeMagicMatchlet *new_list;
741   XdgMimeMagicMatchlet *tmp;
742
743   if ((matchlets == NULL) || (matchlets->next == NULL))
744     return matchlets;
745
746   new_list = NULL;
747   tmp = matchlets;
748   while (tmp != NULL)
749     {
750       XdgMimeMagicMatchlet *matchlet;
751
752       matchlet = tmp;
753       tmp = tmp->next;
754       matchlet->next = new_list;
755       new_list = matchlet;
756     }
757
758   return new_list;
759
760 }
761
762 static void
763 _xdg_mime_magic_read_magic_file (XdgMimeMagic *mime_magic,
764                                  FILE         *magic_file)
765 {
766   XdgMimeMagicState state;
767   XdgMimeMagicMatch *match = NULL; /* Quiet compiler */
768
769   state = XDG_MIME_MAGIC_SECTION;
770
771   while (state != XDG_MIME_MAGIC_EOF)
772     {
773       switch (state)
774         {
775         case XDG_MIME_MAGIC_SECTION:
776           match = _xdg_mime_magic_match_new ();
777           state = _xdg_mime_magic_parse_header (magic_file, match);
778           if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
779             _xdg_mime_magic_match_free (match);
780           break;
781         case XDG_MIME_MAGIC_MAGIC:
782           state = _xdg_mime_magic_parse_magic_line (magic_file, match);
783           if (state == XDG_MIME_MAGIC_SECTION ||
784               (state == XDG_MIME_MAGIC_EOF && match->mime_type))
785             {
786               match->matchlet = _xdg_mime_magic_matchlet_mirror (match->matchlet);
787               _xdg_mime_magic_insert_match (mime_magic, match);
788             }
789           else if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
790             _xdg_mime_magic_match_free (match);
791           break;
792         case XDG_MIME_MAGIC_ERROR:
793           state = _xdg_mime_magic_parse_error (magic_file);
794           break;
795         case XDG_MIME_MAGIC_EOF:
796         default:
797           /* Make the compiler happy */
798           assert (0);
799         }
800     }
801   _xdg_mime_update_mime_magic_extents (mime_magic);
802 }
803
804 void
805 _xdg_mime_magic_read_from_file (XdgMimeMagic *mime_magic,
806                                 const char   *file_name)
807 {
808   FILE *magic_file;
809   char header[12];
810
811   magic_file = fopen (file_name, "r");
812
813   if (magic_file == NULL)
814     return;
815
816   if (fread (header, 1, 12, magic_file) == 12)
817     {
818       if (memcmp ("MIME-Magic\0\n", header, 12) == 0)
819         _xdg_mime_magic_read_magic_file (mime_magic, magic_file);
820     }
821
822   fclose (magic_file);
823 }