Updated update-po to use the new GtkBuilder .ui file
[rox-filer.git] / ROX-Filer / src / xdgmimemagic.c
blob82278a16a3694313421d58022f2667c8a9daa8ee
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* xdgmimemagic.: Private file. Datastructure for storing magic files.
4 * More info can be found at http://www.freedesktop.org/standards/
6 * Copyright (C) 2003 Red Hat, Inc.
7 * Copyright (C) 2003 Jonathan Blandford <jrb@alum.mit.edu>
9 * Licensed under the Academic Free License version 2.0
10 * Or under the following terms:
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.
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.
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.
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
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>
42 /* ROX: */
43 #include <glib.h>
45 #ifndef FALSE
46 #define FALSE (0)
47 #endif
49 #ifndef TRUE
50 #define TRUE (!FALSE)
51 #endif
53 #if !defined getc_unlocked && !defined HAVE_GETC_UNLOCKED
54 #define getc_unlocked(fp) getc (fp)
55 #endif
57 typedef struct XdgMimeMagicMatch XdgMimeMagicMatch;
58 typedef struct XdgMimeMagicMatchlet XdgMimeMagicMatchlet;
60 typedef enum
62 XDG_MIME_MAGIC_SECTION,
63 XDG_MIME_MAGIC_MAGIC,
64 XDG_MIME_MAGIC_ERROR,
65 XDG_MIME_MAGIC_EOF
66 } XdgMimeMagicState;
68 struct XdgMimeMagicMatch
70 const char *mime_type;
71 int priority;
72 XdgMimeMagicMatchlet *matchlet;
73 XdgMimeMagicMatch *next;
77 struct XdgMimeMagicMatchlet
79 int indent;
80 int offset;
81 unsigned int value_length;
82 unsigned char *value;
83 unsigned char *mask;
84 unsigned int range_length;
85 unsigned int word_size;
86 XdgMimeMagicMatchlet *next;
90 struct XdgMimeMagic
92 XdgMimeMagicMatch *match_list;
93 int max_extent;
96 static XdgMimeMagicMatch *
97 _xdg_mime_magic_match_new (void)
99 return calloc (1, sizeof (XdgMimeMagicMatch));
103 static XdgMimeMagicMatchlet *
104 _xdg_mime_magic_matchlet_new (void)
106 XdgMimeMagicMatchlet *matchlet;
108 matchlet = malloc (sizeof (XdgMimeMagicMatchlet));
110 matchlet->indent = 0;
111 matchlet->offset = 0;
112 matchlet->value_length = 0;
113 matchlet->value = NULL;
114 matchlet->mask = NULL;
115 matchlet->range_length = 1;
116 matchlet->word_size = 1;
117 matchlet->next = NULL;
119 return matchlet;
123 static void
124 _xdg_mime_magic_matchlet_free (XdgMimeMagicMatchlet *mime_magic_matchlet)
126 if (mime_magic_matchlet)
128 if (mime_magic_matchlet->next)
129 _xdg_mime_magic_matchlet_free (mime_magic_matchlet->next);
130 if (mime_magic_matchlet->value)
131 free (mime_magic_matchlet->value);
132 if (mime_magic_matchlet->mask)
133 free (mime_magic_matchlet->mask);
134 free (mime_magic_matchlet);
139 /* Frees mime_magic_match and the remainder of its list
141 static void
142 _xdg_mime_magic_match_free (XdgMimeMagicMatch *mime_magic_match)
144 XdgMimeMagicMatch *ptr, *next;
146 ptr = mime_magic_match;
147 while (ptr)
149 next = ptr->next;
151 if (ptr->mime_type)
152 free ((void *) ptr->mime_type);
153 if (ptr->matchlet)
154 _xdg_mime_magic_matchlet_free (ptr->matchlet);
155 free (ptr);
157 ptr = next;
161 /* Reads in a hunk of data until a newline character or a '\000' is hit. The
162 * returned string is null terminated, and doesn't include the newline.
164 static unsigned char *
165 _xdg_mime_magic_read_to_newline (FILE *magic_file,
166 int *end_of_file)
168 unsigned char *retval;
169 int c;
170 int len, pos;
172 len = 128;
173 pos = 0;
174 retval = malloc (len);
175 *end_of_file = FALSE;
177 while (TRUE)
179 c = getc_unlocked (magic_file);
180 if (c == EOF)
182 *end_of_file = TRUE;
183 break;
185 if (c == '\n' || c == '\000')
186 break;
187 retval[pos++] = (unsigned char) c;
188 if (pos % 128 == 127)
190 len = len + 128;
191 retval = realloc (retval, len);
195 retval[pos] = '\000';
196 return retval;
199 /* Returns the number read from the file, or -1 if no number could be read.
201 static int
202 _xdg_mime_magic_read_a_number (FILE *magic_file,
203 int *end_of_file)
205 /* LONG_MAX is about 20 characters on my system */
206 #define MAX_NUMBER_SIZE 30
207 char number_string[MAX_NUMBER_SIZE + 1];
208 int pos = 0;
209 int c;
210 long retval = -1;
212 while (TRUE)
214 c = getc_unlocked (magic_file);
216 if (c == EOF)
218 *end_of_file = TRUE;
219 break;
221 if (! isdigit (c))
223 ungetc (c, magic_file);
224 break;
226 number_string[pos] = (char) c;
227 pos++;
228 if (pos == MAX_NUMBER_SIZE)
229 break;
231 if (pos > 0)
233 number_string[pos] = '\000';
234 errno = 0;
235 retval = strtol (number_string, NULL, 10);
237 if ((retval < INT_MIN) || (retval > INT_MAX) || (errno != 0))
238 return -1;
241 return retval;
244 /* Headers are of the format:
245 * [<priority>:<mime-type>]
247 static XdgMimeMagicState
248 _xdg_mime_magic_parse_header (FILE *magic_file, XdgMimeMagicMatch *match)
250 int c;
251 char *buffer;
252 char *end_ptr;
253 int end_of_file = 0;
255 assert (magic_file != NULL);
256 assert (match != NULL);
258 c = getc_unlocked (magic_file);
259 if (c == EOF)
260 return XDG_MIME_MAGIC_EOF;
261 if (c != '[')
262 return XDG_MIME_MAGIC_ERROR;
264 match->priority = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
265 if (end_of_file)
266 return XDG_MIME_MAGIC_EOF;
267 if (match->priority == -1)
268 return XDG_MIME_MAGIC_ERROR;
270 c = getc_unlocked (magic_file);
271 if (c == EOF)
272 return XDG_MIME_MAGIC_EOF;
273 if (c != ':')
274 return XDG_MIME_MAGIC_ERROR;
276 buffer = (char *)_xdg_mime_magic_read_to_newline (magic_file, &end_of_file);
277 if (end_of_file)
278 return XDG_MIME_MAGIC_EOF;
280 end_ptr = buffer;
281 while (*end_ptr != ']' && *end_ptr != '\000' && *end_ptr != '\n')
282 end_ptr++;
283 if (*end_ptr != ']')
285 free (buffer);
286 return XDG_MIME_MAGIC_ERROR;
288 *end_ptr = '\000';
290 match->mime_type = strdup (buffer);
291 free (buffer);
293 return XDG_MIME_MAGIC_MAGIC;
296 static XdgMimeMagicState
297 _xdg_mime_magic_parse_error (FILE *magic_file)
299 int c;
301 while (1)
303 c = getc_unlocked (magic_file);
304 if (c == EOF)
305 return XDG_MIME_MAGIC_EOF;
306 if (c == '\n')
307 return XDG_MIME_MAGIC_SECTION;
311 /* Headers are of the format:
312 * [ indent ] ">" start-offset "=" value
313 * [ "&" mask ] [ "~" word-size ] [ "+" range-length ] "\n"
315 static XdgMimeMagicState
316 _xdg_mime_magic_parse_magic_line (FILE *magic_file,
317 XdgMimeMagicMatch *match)
319 XdgMimeMagicMatchlet *matchlet;
320 int c;
321 int end_of_file;
322 int indent = 0;
323 int bytes_read;
325 assert (magic_file != NULL);
327 /* Sniff the buffer to make sure it's a valid line */
328 c = getc_unlocked (magic_file);
329 if (c == EOF)
330 return XDG_MIME_MAGIC_EOF;
331 else if (c == '[')
333 ungetc (c, magic_file);
334 return XDG_MIME_MAGIC_SECTION;
336 else if (c == '\n')
337 return XDG_MIME_MAGIC_MAGIC;
339 /* At this point, it must be a digit or a '>' */
340 end_of_file = FALSE;
341 if (isdigit (c))
343 ungetc (c, magic_file);
344 indent = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
345 if (end_of_file)
346 return XDG_MIME_MAGIC_EOF;
347 if (indent == -1)
348 return XDG_MIME_MAGIC_ERROR;
349 c = getc_unlocked (magic_file);
350 if (c == EOF)
351 return XDG_MIME_MAGIC_EOF;
354 if (c != '>')
355 return XDG_MIME_MAGIC_ERROR;
357 matchlet = _xdg_mime_magic_matchlet_new ();
358 matchlet->indent = indent;
359 matchlet->offset = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
360 if (end_of_file)
362 _xdg_mime_magic_matchlet_free (matchlet);
363 return XDG_MIME_MAGIC_EOF;
365 if (matchlet->offset == -1)
367 _xdg_mime_magic_matchlet_free (matchlet);
368 return XDG_MIME_MAGIC_ERROR;
370 c = getc_unlocked (magic_file);
371 if (c == EOF)
373 _xdg_mime_magic_matchlet_free (matchlet);
374 return XDG_MIME_MAGIC_EOF;
376 else if (c != '=')
378 _xdg_mime_magic_matchlet_free (matchlet);
379 return XDG_MIME_MAGIC_ERROR;
382 /* Next two bytes determine how long the value is */
383 matchlet->value_length = 0;
384 c = getc_unlocked (magic_file);
385 if (c == EOF)
387 _xdg_mime_magic_matchlet_free (matchlet);
388 return XDG_MIME_MAGIC_EOF;
390 matchlet->value_length = c & 0xFF;
391 matchlet->value_length = matchlet->value_length << 8;
393 c = getc_unlocked (magic_file);
394 if (c == EOF)
396 _xdg_mime_magic_matchlet_free (matchlet);
397 return XDG_MIME_MAGIC_EOF;
399 matchlet->value_length = matchlet->value_length + (c & 0xFF);
401 matchlet->value = malloc (matchlet->value_length);
403 /* OOM */
404 if (matchlet->value == NULL)
406 _xdg_mime_magic_matchlet_free (matchlet);
407 return XDG_MIME_MAGIC_ERROR;
409 bytes_read = fread (matchlet->value, 1, matchlet->value_length, magic_file);
410 if (bytes_read != matchlet->value_length)
412 _xdg_mime_magic_matchlet_free (matchlet);
413 if (feof (magic_file))
414 return XDG_MIME_MAGIC_EOF;
415 else
416 return XDG_MIME_MAGIC_ERROR;
419 c = getc_unlocked (magic_file);
420 if (c == '&')
422 matchlet->mask = malloc (matchlet->value_length);
423 /* OOM */
424 if (matchlet->mask == NULL)
426 _xdg_mime_magic_matchlet_free (matchlet);
427 return XDG_MIME_MAGIC_ERROR;
429 bytes_read = fread (matchlet->mask, 1, matchlet->value_length, magic_file);
430 if (bytes_read != matchlet->value_length)
432 _xdg_mime_magic_matchlet_free (matchlet);
433 if (feof (magic_file))
434 return XDG_MIME_MAGIC_EOF;
435 else
436 return XDG_MIME_MAGIC_ERROR;
438 c = getc_unlocked (magic_file);
441 if (c == '~')
443 matchlet->word_size = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
444 if (end_of_file)
446 _xdg_mime_magic_matchlet_free (matchlet);
447 return XDG_MIME_MAGIC_EOF;
449 if (matchlet->word_size != 0 &&
450 matchlet->word_size != 1 &&
451 matchlet->word_size != 2 &&
452 matchlet->word_size != 4)
454 _xdg_mime_magic_matchlet_free (matchlet);
455 return XDG_MIME_MAGIC_ERROR;
457 c = getc_unlocked (magic_file);
460 if (c == '+')
462 matchlet->range_length = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
463 if (end_of_file)
465 _xdg_mime_magic_matchlet_free (matchlet);
466 return XDG_MIME_MAGIC_EOF;
468 if (matchlet->range_length == -1)
470 _xdg_mime_magic_matchlet_free (matchlet);
471 return XDG_MIME_MAGIC_ERROR;
473 c = getc_unlocked (magic_file);
477 if (c == '\n')
479 /* We clean up the matchlet, byte swapping if needed */
480 if (matchlet->word_size > 1)
482 #if LITTLE_ENDIAN
483 int i;
484 #endif
485 if (matchlet->value_length % matchlet->word_size != 0)
487 _xdg_mime_magic_matchlet_free (matchlet);
488 return XDG_MIME_MAGIC_ERROR;
490 /* FIXME: need to get this defined in a <config.h> style file */
491 #if LITTLE_ENDIAN
492 for (i = 0; i < matchlet->value_length; i = i + matchlet->word_size)
494 if (matchlet->word_size == 2)
495 *((xdg_uint16_t *) matchlet->value + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->value + i)));
496 else if (matchlet->word_size == 4)
497 *((xdg_uint32_t *) matchlet->value + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->value + i)));
498 if (matchlet->mask)
500 if (matchlet->word_size == 2)
501 *((xdg_uint16_t *) matchlet->mask + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->mask + i)));
502 else if (matchlet->word_size == 4)
503 *((xdg_uint32_t *) matchlet->mask + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->mask + i)));
507 #endif
510 matchlet->next = match->matchlet;
511 match->matchlet = matchlet;
514 return XDG_MIME_MAGIC_MAGIC;
517 _xdg_mime_magic_matchlet_free (matchlet);
518 if (c == EOF)
519 return XDG_MIME_MAGIC_EOF;
521 return XDG_MIME_MAGIC_ERROR;
524 static int
525 _xdg_mime_magic_matchlet_compare_to_data (XdgMimeMagicMatchlet *matchlet,
526 const void *data,
527 size_t len)
529 int i, j;
530 for (i = matchlet->offset; i < matchlet->offset + matchlet->range_length; i++)
532 int valid_matchlet = TRUE;
534 if (i + matchlet->value_length > len)
535 return FALSE;
537 if (matchlet->mask)
539 for (j = 0; j < matchlet->value_length; j++)
541 if ((matchlet->value[j] & matchlet->mask[j]) !=
542 ((((unsigned char *) data)[j + i]) & matchlet->mask[j]))
544 valid_matchlet = FALSE;
545 break;
549 else
551 for (j = 0; j < matchlet->value_length; j++)
553 if (matchlet->value[j] != ((unsigned char *) data)[j + i])
555 valid_matchlet = FALSE;
556 break;
560 if (valid_matchlet)
561 return TRUE;
563 return FALSE;
566 static int
567 _xdg_mime_magic_matchlet_compare_level (XdgMimeMagicMatchlet *matchlet,
568 const void *data,
569 size_t len,
570 int indent)
572 while ((matchlet != NULL) && (matchlet->indent == indent))
574 if (_xdg_mime_magic_matchlet_compare_to_data (matchlet, data, len))
576 if ((matchlet->next == NULL) ||
577 (matchlet->next->indent <= indent))
578 return TRUE;
580 if (_xdg_mime_magic_matchlet_compare_level (matchlet->next,
581 data,
582 len,
583 indent + 1))
584 return TRUE;
589 matchlet = matchlet->next;
591 while (matchlet && matchlet->indent > indent);
594 return FALSE;
597 static int
598 _xdg_mime_magic_match_compare_to_data (XdgMimeMagicMatch *match,
599 const void *data,
600 size_t len)
602 return _xdg_mime_magic_matchlet_compare_level (match->matchlet, data, len, 0);
605 static void
606 _xdg_mime_magic_insert_match (XdgMimeMagic *mime_magic,
607 XdgMimeMagicMatch *match)
609 XdgMimeMagicMatch *list;
611 if (mime_magic->match_list == NULL)
613 mime_magic->match_list = match;
614 return;
617 if (match->priority > mime_magic->match_list->priority)
619 match->next = mime_magic->match_list;
620 mime_magic->match_list = match;
621 return;
624 list = mime_magic->match_list;
625 while (list->next != NULL)
627 if (list->next->priority < match->priority)
629 match->next = list->next;
630 list->next = match;
631 return;
633 list = list->next;
635 list->next = match;
636 match->next = NULL;
639 XdgMimeMagic *
640 _xdg_mime_magic_new (void)
642 return calloc (1, sizeof (XdgMimeMagic));
645 void
646 _xdg_mime_magic_free (XdgMimeMagic *mime_magic)
648 if (mime_magic) {
649 _xdg_mime_magic_match_free (mime_magic->match_list);
650 free (mime_magic);
655 _xdg_mime_magic_get_buffer_extents (XdgMimeMagic *mime_magic)
657 return mime_magic->max_extent;
660 static gboolean _rox_buffer_looks_like_text (const void *data,
661 const size_t len)
663 gchar *end;
665 if (g_utf8_validate (data, len, (const gchar**)&end))
667 /* g_utf8_validate allows control characters */
668 int i;
669 for (i = 0; i < len; i++)
671 unsigned char c = ((const guchar *) data)[i];
672 if (c < 32 && c != '\r' && c != '\n' && c != '\t')
673 return FALSE;
675 return TRUE;
677 } else {
678 /* Check whether the string was truncated in the middle of
679 * a valid UTF8 char, or if we really have an invalid
680 * UTF8 string
682 gint remaining_bytes = len;
684 remaining_bytes -= (end-((gchar*)data));
686 if (g_utf8_get_char_validated(end, remaining_bytes) == -2)
687 return TRUE;
688 #if defined(HAVE_WCTYPE_H) && defined (HAVE_MBRTOWC)
689 else {
690 size_t wlen;
691 wchar_t wc;
692 gchar *src, *end;
693 mbstate_t state;
695 src = data;
696 end = data+len;
698 memset (&state, 0, sizeof (state));
699 while (src < end) {
700 /* Don't allow embedded zeros in textfiles */
701 if (*src == 0)
702 return FALSE;
704 wlen = mbrtowc(&wc, src, end - src, &state);
706 if (wlen == (size_t)(-1)) {
707 /* Illegal mb sequence */
708 return FALSE;
711 if (wlen == (size_t)(-2)) {
712 /* No complete mb char before end
713 * Probably a cut off char which is ok */
714 return TRUE;
717 if (wlen == 0) {
718 /* Don't allow embedded zeros in textfiles */
719 return FALSE;
722 if (!iswspace (wc) && !iswprint(wc)) {
723 /* Not a printable or whitspace
724 * Probably not a text file */
725 return FALSE;
728 src += wlen;
730 return TRUE;
732 #endif /* defined(HAVE_WCTYPE_H) && defined (HAVE_MBRTOWC) */
734 return FALSE;
737 const char *
738 _xdg_mime_magic_lookup_data (XdgMimeMagic *mime_magic,
739 const void *data,
740 size_t len,
741 const char *mime_types[],
742 int n_mime_types)
744 XdgMimeMagicMatch *match;
745 const char *mime_type;
746 int n;
747 int priority;
748 int had_match;
750 mime_type = NULL;
751 priority = 0;
752 had_match = 0;
753 for (match = mime_magic->match_list; match; match = match->next)
755 if (_xdg_mime_magic_match_compare_to_data (match, data, len))
757 if (!had_match || match->priority > priority ||
758 (mime_type != NULL && _xdg_mime_mime_type_subclass (match->mime_type, mime_type)))
760 mime_type = match->mime_type;
761 priority = match->priority;
763 /* Is this another match at same priority which is not the same
764 * type again, or a sub-type */
765 else if (had_match && match->priority == priority && mime_type &&
766 strcmp(mime_type, match->mime_type)!=0 &&
767 !_xdg_mime_mime_type_subclass (mime_type, match->mime_type))
768 /* multiple unrelated patterns with the same priority matched,
769 * so we can't tell what type this is. */
770 mime_type = NULL;
772 had_match = 1;
774 else
776 for (n = 0; n < n_mime_types; n++)
778 if (mime_types[n] &&
779 _xdg_mime_mime_type_equal (mime_types[n], match->mime_type))
780 mime_types[n] = NULL;
785 if (mime_type == NULL)
787 for (n = 0; n < n_mime_types; n++)
789 if (mime_types[n])
790 mime_type = mime_types[n];
794 if (mime_type == NULL)
796 if(_rox_buffer_looks_like_text(data, len))
797 mime_type = XDG_MIME_TYPE_UNKNOWN_TEXT;
800 return mime_type;
803 static void
804 _xdg_mime_update_mime_magic_extents (XdgMimeMagic *mime_magic)
806 XdgMimeMagicMatch *match;
807 int max_extent = 0;
809 for (match = mime_magic->match_list; match; match = match->next)
811 XdgMimeMagicMatchlet *matchlet;
813 for (matchlet = match->matchlet; matchlet; matchlet = matchlet->next)
815 int extent;
817 extent = matchlet->value_length + matchlet->offset + matchlet->range_length;
818 if (max_extent < extent)
819 max_extent = extent;
823 mime_magic->max_extent = max_extent;
826 static XdgMimeMagicMatchlet *
827 _xdg_mime_magic_matchlet_mirror (XdgMimeMagicMatchlet *matchlets)
829 XdgMimeMagicMatchlet *new_list;
830 XdgMimeMagicMatchlet *tmp;
832 if ((matchlets == NULL) || (matchlets->next == NULL))
833 return matchlets;
835 new_list = NULL;
836 tmp = matchlets;
837 while (tmp != NULL)
839 XdgMimeMagicMatchlet *matchlet;
841 matchlet = tmp;
842 tmp = tmp->next;
843 matchlet->next = new_list;
844 new_list = matchlet;
847 return new_list;
851 static void
852 _xdg_mime_magic_read_magic_file (XdgMimeMagic *mime_magic,
853 FILE *magic_file)
855 XdgMimeMagicState state;
856 XdgMimeMagicMatch *match = NULL; /* Quiet compiler */
858 state = XDG_MIME_MAGIC_SECTION;
860 while (state != XDG_MIME_MAGIC_EOF)
862 switch (state)
864 case XDG_MIME_MAGIC_SECTION:
865 match = _xdg_mime_magic_match_new ();
866 state = _xdg_mime_magic_parse_header (magic_file, match);
867 if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
868 _xdg_mime_magic_match_free (match);
869 break;
870 case XDG_MIME_MAGIC_MAGIC:
871 state = _xdg_mime_magic_parse_magic_line (magic_file, match);
872 if (state == XDG_MIME_MAGIC_SECTION ||
873 (state == XDG_MIME_MAGIC_EOF && match->mime_type))
875 match->matchlet = _xdg_mime_magic_matchlet_mirror (match->matchlet);
876 _xdg_mime_magic_insert_match (mime_magic, match);
878 else if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
879 _xdg_mime_magic_match_free (match);
880 break;
881 case XDG_MIME_MAGIC_ERROR:
882 state = _xdg_mime_magic_parse_error (magic_file);
883 break;
884 case XDG_MIME_MAGIC_EOF:
885 default:
886 /* Make the compiler happy */
887 assert (0);
890 _xdg_mime_update_mime_magic_extents (mime_magic);
893 void
894 _xdg_mime_magic_read_from_file (XdgMimeMagic *mime_magic,
895 const char *file_name)
897 FILE *magic_file;
898 char header[12];
900 magic_file = fopen (file_name, "r");
902 if (magic_file == NULL)
903 return;
905 if (fread (header, 1, 12, magic_file) == 12)
907 if (memcmp ("MIME-Magic\0\n", header, 12) == 0)
908 _xdg_mime_magic_read_magic_file (mime_magic, magic_file);
911 fclose (magic_file);