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 < INT_MIN) || (retval > INT_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);
284 return XDG_MIME_MAGIC_EOF;
287 while (*end_ptr != ']' && *end_ptr != '\000' && *end_ptr != '\n')
292 return XDG_MIME_MAGIC_ERROR;
296 match->mime_type = strdup (buffer);
299 return XDG_MIME_MAGIC_MAGIC;
302 static XdgMimeMagicState
303 _xdg_mime_magic_parse_error (FILE *magic_file)
309 c = getc_unlocked (magic_file);
311 return XDG_MIME_MAGIC_EOF;
313 return XDG_MIME_MAGIC_SECTION;
317 /* Headers are of the format:
318 * [ indent ] ">" start-offset "=" value
319 * [ "&" mask ] [ "~" word-size ] [ "+" range-length ] "\n"
321 static XdgMimeMagicState
322 _xdg_mime_magic_parse_magic_line (FILE *magic_file,
323 XdgMimeMagicMatch *match)
325 XdgMimeMagicMatchlet *matchlet;
331 assert (magic_file != NULL);
333 /* Sniff the buffer to make sure it's a valid line */
334 c = getc_unlocked (magic_file);
336 return XDG_MIME_MAGIC_EOF;
339 ungetc (c, magic_file);
340 return XDG_MIME_MAGIC_SECTION;
343 return XDG_MIME_MAGIC_MAGIC;
345 /* At this point, it must be a digit or a '>' */
349 ungetc (c, magic_file);
350 indent = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
352 return XDG_MIME_MAGIC_EOF;
354 return XDG_MIME_MAGIC_ERROR;
355 c = getc_unlocked (magic_file);
357 return XDG_MIME_MAGIC_EOF;
361 return XDG_MIME_MAGIC_ERROR;
363 matchlet = _xdg_mime_magic_matchlet_new ();
364 matchlet->indent = indent;
365 matchlet->offset = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
368 _xdg_mime_magic_matchlet_free (matchlet);
369 return XDG_MIME_MAGIC_EOF;
371 if (matchlet->offset == -1)
373 _xdg_mime_magic_matchlet_free (matchlet);
374 return XDG_MIME_MAGIC_ERROR;
376 c = getc_unlocked (magic_file);
379 _xdg_mime_magic_matchlet_free (matchlet);
380 return XDG_MIME_MAGIC_EOF;
384 _xdg_mime_magic_matchlet_free (matchlet);
385 return XDG_MIME_MAGIC_ERROR;
388 /* Next two bytes determine how long the value is */
389 matchlet->value_length = 0;
390 c = getc_unlocked (magic_file);
393 _xdg_mime_magic_matchlet_free (matchlet);
394 return XDG_MIME_MAGIC_EOF;
396 matchlet->value_length = c & 0xFF;
397 matchlet->value_length = matchlet->value_length << 8;
399 c = getc_unlocked (magic_file);
402 _xdg_mime_magic_matchlet_free (matchlet);
403 return XDG_MIME_MAGIC_EOF;
405 matchlet->value_length = matchlet->value_length + (c & 0xFF);
407 matchlet->value = malloc (matchlet->value_length);
410 if (matchlet->value == NULL)
412 _xdg_mime_magic_matchlet_free (matchlet);
413 return XDG_MIME_MAGIC_ERROR;
415 bytes_read = fread (matchlet->value, 1, matchlet->value_length, magic_file);
416 if (bytes_read != matchlet->value_length)
418 _xdg_mime_magic_matchlet_free (matchlet);
419 if (feof (magic_file))
420 return XDG_MIME_MAGIC_EOF;
422 return XDG_MIME_MAGIC_ERROR;
425 c = getc_unlocked (magic_file);
428 matchlet->mask = malloc (matchlet->value_length);
430 if (matchlet->mask == NULL)
432 _xdg_mime_magic_matchlet_free (matchlet);
433 return XDG_MIME_MAGIC_ERROR;
435 bytes_read = fread (matchlet->mask, 1, matchlet->value_length, magic_file);
436 if (bytes_read != matchlet->value_length)
438 _xdg_mime_magic_matchlet_free (matchlet);
439 if (feof (magic_file))
440 return XDG_MIME_MAGIC_EOF;
442 return XDG_MIME_MAGIC_ERROR;
444 c = getc_unlocked (magic_file);
449 matchlet->word_size = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
452 _xdg_mime_magic_matchlet_free (matchlet);
453 return XDG_MIME_MAGIC_EOF;
455 if (matchlet->word_size != 0 &&
456 matchlet->word_size != 1 &&
457 matchlet->word_size != 2 &&
458 matchlet->word_size != 4)
460 _xdg_mime_magic_matchlet_free (matchlet);
461 return XDG_MIME_MAGIC_ERROR;
463 c = getc_unlocked (magic_file);
468 matchlet->range_length = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
471 _xdg_mime_magic_matchlet_free (matchlet);
472 return XDG_MIME_MAGIC_EOF;
474 if (matchlet->range_length == -1)
476 _xdg_mime_magic_matchlet_free (matchlet);
477 return XDG_MIME_MAGIC_ERROR;
479 c = getc_unlocked (magic_file);
485 /* We clean up the matchlet, byte swapping if needed */
486 if (matchlet->word_size > 1)
489 if (matchlet->value_length % matchlet->word_size != 0)
491 _xdg_mime_magic_matchlet_free (matchlet);
492 return XDG_MIME_MAGIC_ERROR;
494 /* FIXME: need to get this defined in a <config.h> style file */
496 for (i = 0; i < matchlet->value_length; i = i + matchlet->word_size)
498 if (matchlet->word_size == 2)
499 *((xdg_uint16_t *) matchlet->value + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->value + i)));
500 else if (matchlet->word_size == 4)
501 *((xdg_uint32_t *) matchlet->value + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->value + i)));
504 if (matchlet->word_size == 2)
505 *((xdg_uint16_t *) matchlet->mask + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->mask + i)));
506 else if (matchlet->word_size == 4)
507 *((xdg_uint32_t *) matchlet->mask + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->mask + i)));
514 matchlet->next = match->matchlet;
515 match->matchlet = matchlet;
518 return XDG_MIME_MAGIC_MAGIC;
521 _xdg_mime_magic_matchlet_free (matchlet);
523 return XDG_MIME_MAGIC_EOF;
525 return XDG_MIME_MAGIC_ERROR;
529 _xdg_mime_magic_matchlet_compare_to_data (XdgMimeMagicMatchlet *matchlet,
534 for (i = matchlet->offset; i < matchlet->offset + matchlet->range_length; i++)
536 int valid_matchlet = TRUE;
538 if (i + matchlet->value_length > len)
543 for (j = 0; j < matchlet->value_length; j++)
545 if ((matchlet->value[j] & matchlet->mask[j]) !=
546 ((((unsigned char *) data)[j + i]) & matchlet->mask[j]))
548 valid_matchlet = FALSE;
555 for (j = 0; j < matchlet->value_length; j++)
557 if (matchlet->value[j] != ((unsigned char *) data)[j + i])
559 valid_matchlet = FALSE;
571 _xdg_mime_magic_matchlet_compare_level (XdgMimeMagicMatchlet *matchlet,
576 while ((matchlet != NULL) && (matchlet->indent == indent))
578 if (_xdg_mime_magic_matchlet_compare_to_data (matchlet, data, len))
580 if ((matchlet->next == NULL) ||
581 (matchlet->next->indent <= indent))
584 if (_xdg_mime_magic_matchlet_compare_level (matchlet->next,
593 matchlet = matchlet->next;
595 while (matchlet && matchlet->indent > indent);
602 _xdg_mime_magic_match_compare_to_data (XdgMimeMagicMatch *match,
606 return _xdg_mime_magic_matchlet_compare_level (match->matchlet, data, len, 0);
610 _xdg_mime_magic_insert_match (XdgMimeMagic *mime_magic,
611 XdgMimeMagicMatch *match)
613 XdgMimeMagicMatch *list;
615 if (mime_magic->match_list == NULL)
617 mime_magic->match_list = match;
621 if (match->priority > mime_magic->match_list->priority)
623 match->next = mime_magic->match_list;
624 mime_magic->match_list = match;
628 list = mime_magic->match_list;
629 while (list->next != NULL)
631 if (list->next->priority < match->priority)
633 match->next = list->next;
644 _xdg_mime_magic_new (void)
646 return calloc (1, sizeof (XdgMimeMagic));
650 _xdg_mime_magic_free (XdgMimeMagic *mime_magic)
653 _xdg_mime_magic_match_free (mime_magic->match_list);
659 _xdg_mime_magic_get_buffer_extents (XdgMimeMagic *mime_magic)
661 return mime_magic->max_extent;
665 _xdg_mime_magic_lookup_data (XdgMimeMagic *mime_magic,
669 const char *mime_types[],
672 XdgMimeMagicMatch *match;
673 const char *mime_type;
679 for (match = mime_magic->match_list; match; match = match->next)
681 if (_xdg_mime_magic_match_compare_to_data (match, data, len))
683 prio = match->priority;
684 mime_type = match->mime_type;
689 for (n = 0; n < n_mime_types; n++)
692 _xdg_mime_mime_type_equal (mime_types[n], match->mime_type))
693 mime_types[n] = NULL;
698 if (mime_type == NULL)
700 for (n = 0; n < n_mime_types; n++)
703 mime_type = mime_types[n];
714 _xdg_mime_update_mime_magic_extents (XdgMimeMagic *mime_magic)
716 XdgMimeMagicMatch *match;
719 for (match = mime_magic->match_list; match; match = match->next)
721 XdgMimeMagicMatchlet *matchlet;
723 for (matchlet = match->matchlet; matchlet; matchlet = matchlet->next)
727 extent = matchlet->value_length + matchlet->offset + matchlet->range_length;
728 if (max_extent < extent)
733 mime_magic->max_extent = max_extent;
736 static XdgMimeMagicMatchlet *
737 _xdg_mime_magic_matchlet_mirror (XdgMimeMagicMatchlet *matchlets)
739 XdgMimeMagicMatchlet *new_list;
740 XdgMimeMagicMatchlet *tmp;
742 if ((matchlets == NULL) || (matchlets->next == NULL))
749 XdgMimeMagicMatchlet *matchlet;
753 matchlet->next = new_list;
762 _xdg_mime_magic_read_magic_file (XdgMimeMagic *mime_magic,
765 XdgMimeMagicState state;
766 XdgMimeMagicMatch *match = NULL; /* Quiet compiler */
768 state = XDG_MIME_MAGIC_SECTION;
770 while (state != XDG_MIME_MAGIC_EOF)
774 case XDG_MIME_MAGIC_SECTION:
775 match = _xdg_mime_magic_match_new ();
776 state = _xdg_mime_magic_parse_header (magic_file, match);
777 if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
778 _xdg_mime_magic_match_free (match);
780 case XDG_MIME_MAGIC_MAGIC:
781 state = _xdg_mime_magic_parse_magic_line (magic_file, match);
782 if (state == XDG_MIME_MAGIC_SECTION ||
783 (state == XDG_MIME_MAGIC_EOF && match->mime_type))
785 match->matchlet = _xdg_mime_magic_matchlet_mirror (match->matchlet);
786 _xdg_mime_magic_insert_match (mime_magic, match);
788 else if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
789 _xdg_mime_magic_match_free (match);
791 case XDG_MIME_MAGIC_ERROR:
792 state = _xdg_mime_magic_parse_error (magic_file);
794 case XDG_MIME_MAGIC_EOF:
796 /* Make the compiler happy */
800 _xdg_mime_update_mime_magic_extents (mime_magic);
804 _xdg_mime_magic_read_from_file (XdgMimeMagic *mime_magic,
805 const char *file_name)
810 magic_file = fopen (file_name, "r");
812 if (magic_file == NULL)
815 if (fread (header, 1, 12, magic_file) == 12)
817 if (memcmp ("MIME-Magic\0\n", header, 12) == 0)
818 _xdg_mime_magic_read_magic_file (mime_magic, magic_file);