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.
33 #include "xdgmimemagic.h"
34 #include "xdgmimeint.h"
50 #if !defined getc_unlocked && !defined HAVE_GETC_UNLOCKED
51 # define getc_unlocked(fp) getc (fp)
54 typedef struct XdgMimeMagicMatch XdgMimeMagicMatch;
55 typedef struct XdgMimeMagicMatchlet XdgMimeMagicMatchlet;
59 XDG_MIME_MAGIC_SECTION,
65 struct XdgMimeMagicMatch
67 const char *mime_type;
69 XdgMimeMagicMatchlet *matchlet;
70 XdgMimeMagicMatch *next;
74 struct XdgMimeMagicMatchlet
78 unsigned int value_length;
81 unsigned int range_length;
82 unsigned int word_size;
83 XdgMimeMagicMatchlet *next;
89 XdgMimeMagicMatch *match_list;
93 static XdgMimeMagicMatch *
94 _xdg_mime_magic_match_new (void)
96 return calloc (1, sizeof (XdgMimeMagicMatch));
100 static XdgMimeMagicMatchlet *
101 _xdg_mime_magic_matchlet_new (void)
103 XdgMimeMagicMatchlet *matchlet;
105 matchlet = malloc (sizeof (XdgMimeMagicMatchlet));
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;
121 _xdg_mime_magic_matchlet_free (XdgMimeMagicMatchlet *mime_magic_matchlet)
123 if (mime_magic_matchlet)
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);
136 /* Frees mime_magic_match and the remainder of its list
139 _xdg_mime_magic_match_free (XdgMimeMagicMatch *mime_magic_match)
141 XdgMimeMagicMatch *ptr, *next;
143 ptr = mime_magic_match;
149 free ((void *) ptr->mime_type);
151 _xdg_mime_magic_matchlet_free (ptr->matchlet);
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.
161 static unsigned char *
162 _xdg_mime_magic_read_to_newline (FILE *magic_file,
165 unsigned char *retval;
171 retval = malloc (len);
175 *end_of_file = FALSE;
179 c = getc_unlocked (magic_file);
185 if (c == '\n' || c == '\000')
187 retval[pos++] = (unsigned char) c;
188 if (pos % 128 == 127)
191 unsigned char *tmp = realloc (retval, len);
201 retval[pos] = '\000';
205 /* Returns the number read from the file, or -1 if no number could be read.
208 _xdg_mime_magic_read_a_number (FILE *magic_file,
211 /* LONG_MAX is about 20 characters on my system */
212 #define MAX_NUMBER_SIZE 30
213 char number_string[MAX_NUMBER_SIZE + 1];
220 c = getc_unlocked (magic_file);
229 ungetc (c, magic_file);
232 number_string[pos] = (char) c;
234 if (pos == MAX_NUMBER_SIZE)
239 number_string[pos] = '\000';
241 retval = strtol (number_string, NULL, 10);
243 if ((retval == LONG_MIN) || (retval == LONG_MAX) || (errno != 0))
250 /* Headers are of the format:
251 * [<priority>:<mime-type>]
253 static XdgMimeMagicState
254 _xdg_mime_magic_parse_header (FILE *magic_file, XdgMimeMagicMatch *match)
261 assert (magic_file != NULL);
262 assert (match != NULL);
264 c = getc_unlocked (magic_file);
266 return XDG_MIME_MAGIC_EOF;
268 return XDG_MIME_MAGIC_ERROR;
270 match->priority = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
272 return XDG_MIME_MAGIC_EOF;
273 if (match->priority == -1)
274 return XDG_MIME_MAGIC_ERROR;
276 c = getc_unlocked (magic_file);
278 return XDG_MIME_MAGIC_EOF;
280 return XDG_MIME_MAGIC_ERROR;
282 buffer = (char *)_xdg_mime_magic_read_to_newline (magic_file, &end_of_file);
287 return XDG_MIME_MAGIC_EOF;
291 while (*end_ptr != ']' && *end_ptr != '\000' && *end_ptr != '\n')
296 return XDG_MIME_MAGIC_ERROR;
300 match->mime_type = strdup (buffer);
303 return XDG_MIME_MAGIC_MAGIC;
306 static XdgMimeMagicState
307 _xdg_mime_magic_parse_error (FILE *magic_file)
313 c = getc_unlocked (magic_file);
315 return XDG_MIME_MAGIC_EOF;
317 return XDG_MIME_MAGIC_SECTION;
321 /* Headers are of the format:
322 * [ indent ] ">" start-offset "=" value
323 * [ "&" mask ] [ "~" word-size ] [ "+" range-length ] "\n"
325 static XdgMimeMagicState
326 _xdg_mime_magic_parse_magic_line (FILE *magic_file,
327 XdgMimeMagicMatch *match)
329 XdgMimeMagicMatchlet *matchlet;
335 assert (magic_file != NULL);
337 /* Sniff the buffer to make sure it's a valid line */
338 c = getc_unlocked (magic_file);
340 return XDG_MIME_MAGIC_EOF;
343 ungetc (c, magic_file);
344 return XDG_MIME_MAGIC_SECTION;
347 return XDG_MIME_MAGIC_MAGIC;
349 /* At this point, it must be a digit or a '>' */
353 ungetc (c, magic_file);
354 indent = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
356 return XDG_MIME_MAGIC_EOF;
358 return XDG_MIME_MAGIC_ERROR;
359 c = getc_unlocked (magic_file);
361 return XDG_MIME_MAGIC_EOF;
365 return XDG_MIME_MAGIC_ERROR;
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);
372 _xdg_mime_magic_matchlet_free (matchlet);
373 return XDG_MIME_MAGIC_EOF;
375 if (matchlet->offset == -1)
377 _xdg_mime_magic_matchlet_free (matchlet);
378 return XDG_MIME_MAGIC_ERROR;
380 c = getc_unlocked (magic_file);
383 _xdg_mime_magic_matchlet_free (matchlet);
384 return XDG_MIME_MAGIC_EOF;
388 _xdg_mime_magic_matchlet_free (matchlet);
389 return XDG_MIME_MAGIC_ERROR;
392 /* Next two bytes determine how long the value is */
393 matchlet->value_length = 0;
394 c = getc_unlocked (magic_file);
397 _xdg_mime_magic_matchlet_free (matchlet);
398 return XDG_MIME_MAGIC_EOF;
400 matchlet->value_length = c & 0xFF;
401 matchlet->value_length = matchlet->value_length << 8;
403 c = getc_unlocked (magic_file);
406 _xdg_mime_magic_matchlet_free (matchlet);
407 return XDG_MIME_MAGIC_EOF;
409 matchlet->value_length = matchlet->value_length + (c & 0xFF);
411 matchlet->value = malloc (matchlet->value_length);
414 if (matchlet->value == NULL)
416 _xdg_mime_magic_matchlet_free (matchlet);
417 return XDG_MIME_MAGIC_ERROR;
419 bytes_read = fread (matchlet->value, 1, matchlet->value_length, magic_file);
420 if (bytes_read != matchlet->value_length)
422 _xdg_mime_magic_matchlet_free (matchlet);
423 if (feof (magic_file))
424 return XDG_MIME_MAGIC_EOF;
426 return XDG_MIME_MAGIC_ERROR;
429 c = getc_unlocked (magic_file);
432 matchlet->mask = malloc (matchlet->value_length);
434 if (matchlet->mask == NULL)
436 _xdg_mime_magic_matchlet_free (matchlet);
437 return XDG_MIME_MAGIC_ERROR;
439 bytes_read = fread (matchlet->mask, 1, matchlet->value_length, magic_file);
440 if (bytes_read != matchlet->value_length)
442 _xdg_mime_magic_matchlet_free (matchlet);
443 if (feof (magic_file))
444 return XDG_MIME_MAGIC_EOF;
446 return XDG_MIME_MAGIC_ERROR;
448 c = getc_unlocked (magic_file);
453 matchlet->word_size = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
456 _xdg_mime_magic_matchlet_free (matchlet);
457 return XDG_MIME_MAGIC_EOF;
459 if (matchlet->word_size != 0 &&
460 matchlet->word_size != 1 &&
461 matchlet->word_size != 2 &&
462 matchlet->word_size != 4)
464 _xdg_mime_magic_matchlet_free (matchlet);
465 return XDG_MIME_MAGIC_ERROR;
467 c = getc_unlocked (magic_file);
472 matchlet->range_length = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
475 _xdg_mime_magic_matchlet_free (matchlet);
476 return XDG_MIME_MAGIC_EOF;
478 if (matchlet->range_length == -1)
480 _xdg_mime_magic_matchlet_free (matchlet);
481 return XDG_MIME_MAGIC_ERROR;
483 c = getc_unlocked (magic_file);
489 /* We clean up the matchlet, byte swapping if needed */
490 if (matchlet->word_size > 1)
493 if (matchlet->value_length % matchlet->word_size != 0)
495 _xdg_mime_magic_matchlet_free (matchlet);
496 return XDG_MIME_MAGIC_ERROR;
498 /* FIXME: need to get this defined in a <config.h> style file */
500 for (i = 0; i < matchlet->value_length; i = i + matchlet->word_size)
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)));
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)));
518 matchlet->next = match->matchlet;
519 match->matchlet = matchlet;
522 return XDG_MIME_MAGIC_MAGIC;
525 _xdg_mime_magic_matchlet_free (matchlet);
527 return XDG_MIME_MAGIC_EOF;
529 return XDG_MIME_MAGIC_ERROR;
533 _xdg_mime_magic_matchlet_compare_to_data (XdgMimeMagicMatchlet *matchlet,
538 for (i = matchlet->offset; i < matchlet->offset + matchlet->range_length; i++)
540 int valid_matchlet = TRUE;
542 if (i + matchlet->value_length > len)
547 for (j = 0; j < matchlet->value_length; j++)
549 if ((matchlet->value[j] & matchlet->mask[j]) !=
550 ((((unsigned char *) data)[j + i]) & matchlet->mask[j]))
552 valid_matchlet = FALSE;
559 for (j = 0; j < matchlet->value_length; j++)
561 if (matchlet->value[j] != ((unsigned char *) data)[j + i])
563 valid_matchlet = FALSE;
575 _xdg_mime_magic_matchlet_compare_level (XdgMimeMagicMatchlet *matchlet,
580 while ((matchlet != NULL) && (matchlet->indent == indent))
582 if (_xdg_mime_magic_matchlet_compare_to_data (matchlet, data, len))
584 if ((matchlet->next == NULL) ||
585 (matchlet->next->indent <= indent))
588 if (_xdg_mime_magic_matchlet_compare_level (matchlet->next,
597 matchlet = matchlet->next;
599 while (matchlet && matchlet->indent > indent);
606 _xdg_mime_magic_match_compare_to_data (XdgMimeMagicMatch *match,
610 return _xdg_mime_magic_matchlet_compare_level (match->matchlet, data, len, 0);
614 _xdg_mime_magic_insert_match (XdgMimeMagic *mime_magic,
615 XdgMimeMagicMatch *match)
617 XdgMimeMagicMatch *list;
619 if (mime_magic->match_list == NULL)
621 mime_magic->match_list = match;
625 if (match->priority > mime_magic->match_list->priority)
627 match->next = mime_magic->match_list;
628 mime_magic->match_list = match;
632 list = mime_magic->match_list;
633 while (list->next != NULL)
635 if (list->next->priority < match->priority)
637 match->next = list->next;
648 _xdg_mime_magic_new (void)
650 return calloc (1, sizeof (XdgMimeMagic));
654 _xdg_mime_magic_free (XdgMimeMagic *mime_magic)
657 _xdg_mime_magic_match_free (mime_magic->match_list);
663 _xdg_mime_magic_get_buffer_extents (XdgMimeMagic *mime_magic)
665 return mime_magic->max_extent;
669 _xdg_mime_magic_lookup_data (XdgMimeMagic *mime_magic,
673 const char *mime_types[],
676 XdgMimeMagicMatch *match;
677 const char *mime_type;
683 for (match = mime_magic->match_list; match; match = match->next)
685 if (_xdg_mime_magic_match_compare_to_data (match, data, len))
687 prio = match->priority;
688 mime_type = match->mime_type;
693 for (n = 0; n < n_mime_types; n++)
696 _xdg_mime_mime_type_equal (mime_types[n], match->mime_type))
697 mime_types[n] = NULL;
702 if (mime_type == NULL)
704 for (n = 0; n < n_mime_types; n++)
707 mime_type = mime_types[n];
718 _xdg_mime_update_mime_magic_extents (XdgMimeMagic *mime_magic)
720 XdgMimeMagicMatch *match;
723 for (match = mime_magic->match_list; match; match = match->next)
725 XdgMimeMagicMatchlet *matchlet;
727 for (matchlet = match->matchlet; matchlet; matchlet = matchlet->next)
731 extent = matchlet->value_length + matchlet->offset + matchlet->range_length;
732 if (max_extent < extent)
737 mime_magic->max_extent = max_extent;
740 static XdgMimeMagicMatchlet *
741 _xdg_mime_magic_matchlet_mirror (XdgMimeMagicMatchlet *matchlets)
743 XdgMimeMagicMatchlet *new_list;
744 XdgMimeMagicMatchlet *tmp;
746 if ((matchlets == NULL) || (matchlets->next == NULL))
753 XdgMimeMagicMatchlet *matchlet;
757 matchlet->next = new_list;
766 _xdg_mime_magic_read_magic_file (XdgMimeMagic *mime_magic,
769 XdgMimeMagicState state;
770 XdgMimeMagicMatch *match = NULL; /* Quiet compiler */
772 state = XDG_MIME_MAGIC_SECTION;
774 while (state != XDG_MIME_MAGIC_EOF)
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);
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))
789 match->matchlet = _xdg_mime_magic_matchlet_mirror (match->matchlet);
790 _xdg_mime_magic_insert_match (mime_magic, match);
792 else if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
793 _xdg_mime_magic_match_free (match);
795 case XDG_MIME_MAGIC_ERROR:
796 state = _xdg_mime_magic_parse_error (magic_file);
799 /* Make the compiler happy */
803 _xdg_mime_update_mime_magic_extents (mime_magic);
807 _xdg_mime_magic_read_from_file (XdgMimeMagic *mime_magic,
808 const char *file_name)
813 magic_file = fopen (file_name, "r");
815 if (magic_file == NULL)
818 if (fread (header, 1, 12, magic_file) == 12)
820 if (memcmp ("MIME-Magic\0\n", header, 12) == 0)
821 _xdg_mime_magic_read_magic_file (mime_magic, magic_file);