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);
172 *end_of_file = FALSE;
176 c = getc_unlocked (magic_file);
182 if (c == '\n' || c == '\000')
184 retval[pos++] = (unsigned char) c;
185 if (pos % 128 == 127)
188 retval = realloc (retval, len);
192 retval[pos] = '\000';
196 /* Returns the number read from the file, or -1 if no number could be read.
199 _xdg_mime_magic_read_a_number (FILE *magic_file,
202 /* LONG_MAX is about 20 characters on my system */
203 #define MAX_NUMBER_SIZE 30
204 char number_string[MAX_NUMBER_SIZE + 1];
211 c = getc_unlocked (magic_file);
220 ungetc (c, magic_file);
223 number_string[pos] = (char) c;
225 if (pos == MAX_NUMBER_SIZE)
230 number_string[pos] = '\000';
232 retval = strtol (number_string, NULL, 10);
234 if ((retval < INT_MIN) || (retval > INT_MAX) || (errno != 0))
241 /* Headers are of the format:
242 * [<priority>:<mime-type>]
244 static XdgMimeMagicState
245 _xdg_mime_magic_parse_header (FILE *magic_file, XdgMimeMagicMatch *match)
252 assert (magic_file != NULL);
253 assert (match != NULL);
255 c = getc_unlocked (magic_file);
257 return XDG_MIME_MAGIC_EOF;
259 return XDG_MIME_MAGIC_ERROR;
261 match->priority = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
263 return XDG_MIME_MAGIC_EOF;
264 if (match->priority == -1)
265 return XDG_MIME_MAGIC_ERROR;
267 c = getc_unlocked (magic_file);
269 return XDG_MIME_MAGIC_EOF;
271 return XDG_MIME_MAGIC_ERROR;
273 buffer = (char *)_xdg_mime_magic_read_to_newline (magic_file, &end_of_file);
275 return XDG_MIME_MAGIC_EOF;
278 while (*end_ptr != ']' && *end_ptr != '\000' && *end_ptr != '\n')
283 return XDG_MIME_MAGIC_ERROR;
287 match->mime_type = strdup (buffer);
290 return XDG_MIME_MAGIC_MAGIC;
293 static XdgMimeMagicState
294 _xdg_mime_magic_parse_error (FILE *magic_file)
300 c = getc_unlocked (magic_file);
302 return XDG_MIME_MAGIC_EOF;
304 return XDG_MIME_MAGIC_SECTION;
308 /* Headers are of the format:
309 * [ indent ] ">" start-offset "=" value
310 * [ "&" mask ] [ "~" word-size ] [ "+" range-length ] "\n"
312 static XdgMimeMagicState
313 _xdg_mime_magic_parse_magic_line (FILE *magic_file,
314 XdgMimeMagicMatch *match)
316 XdgMimeMagicMatchlet *matchlet;
322 assert (magic_file != NULL);
324 /* Sniff the buffer to make sure it's a valid line */
325 c = getc_unlocked (magic_file);
327 return XDG_MIME_MAGIC_EOF;
330 ungetc (c, magic_file);
331 return XDG_MIME_MAGIC_SECTION;
334 return XDG_MIME_MAGIC_MAGIC;
336 /* At this point, it must be a digit or a '>' */
340 ungetc (c, magic_file);
341 indent = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
343 return XDG_MIME_MAGIC_EOF;
345 return XDG_MIME_MAGIC_ERROR;
346 c = getc_unlocked (magic_file);
348 return XDG_MIME_MAGIC_EOF;
352 return XDG_MIME_MAGIC_ERROR;
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);
359 _xdg_mime_magic_matchlet_free (matchlet);
360 return XDG_MIME_MAGIC_EOF;
362 if (matchlet->offset == -1)
364 _xdg_mime_magic_matchlet_free (matchlet);
365 return XDG_MIME_MAGIC_ERROR;
367 c = getc_unlocked (magic_file);
370 _xdg_mime_magic_matchlet_free (matchlet);
371 return XDG_MIME_MAGIC_EOF;
375 _xdg_mime_magic_matchlet_free (matchlet);
376 return XDG_MIME_MAGIC_ERROR;
379 /* Next two bytes determine how long the value is */
380 matchlet->value_length = 0;
381 c = getc_unlocked (magic_file);
384 _xdg_mime_magic_matchlet_free (matchlet);
385 return XDG_MIME_MAGIC_EOF;
387 matchlet->value_length = c & 0xFF;
388 matchlet->value_length = matchlet->value_length << 8;
390 c = getc_unlocked (magic_file);
393 _xdg_mime_magic_matchlet_free (matchlet);
394 return XDG_MIME_MAGIC_EOF;
396 matchlet->value_length = matchlet->value_length + (c & 0xFF);
398 matchlet->value = malloc (matchlet->value_length);
401 if (matchlet->value == NULL)
403 _xdg_mime_magic_matchlet_free (matchlet);
404 return XDG_MIME_MAGIC_ERROR;
406 bytes_read = fread (matchlet->value, 1, matchlet->value_length, magic_file);
407 if (bytes_read != matchlet->value_length)
409 _xdg_mime_magic_matchlet_free (matchlet);
410 if (feof (magic_file))
411 return XDG_MIME_MAGIC_EOF;
413 return XDG_MIME_MAGIC_ERROR;
416 c = getc_unlocked (magic_file);
419 matchlet->mask = malloc (matchlet->value_length);
421 if (matchlet->mask == NULL)
423 _xdg_mime_magic_matchlet_free (matchlet);
424 return XDG_MIME_MAGIC_ERROR;
426 bytes_read = fread (matchlet->mask, 1, matchlet->value_length, magic_file);
427 if (bytes_read != matchlet->value_length)
429 _xdg_mime_magic_matchlet_free (matchlet);
430 if (feof (magic_file))
431 return XDG_MIME_MAGIC_EOF;
433 return XDG_MIME_MAGIC_ERROR;
435 c = getc_unlocked (magic_file);
440 matchlet->word_size = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
443 _xdg_mime_magic_matchlet_free (matchlet);
444 return XDG_MIME_MAGIC_EOF;
446 if (matchlet->word_size != 0 &&
447 matchlet->word_size != 1 &&
448 matchlet->word_size != 2 &&
449 matchlet->word_size != 4)
451 _xdg_mime_magic_matchlet_free (matchlet);
452 return XDG_MIME_MAGIC_ERROR;
454 c = getc_unlocked (magic_file);
459 matchlet->range_length = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
462 _xdg_mime_magic_matchlet_free (matchlet);
463 return XDG_MIME_MAGIC_EOF;
465 if (matchlet->range_length == -1)
467 _xdg_mime_magic_matchlet_free (matchlet);
468 return XDG_MIME_MAGIC_ERROR;
470 c = getc_unlocked (magic_file);
476 /* We clean up the matchlet, byte swapping if needed */
477 if (matchlet->word_size > 1)
480 if (matchlet->value_length % matchlet->word_size != 0)
482 _xdg_mime_magic_matchlet_free (matchlet);
483 return XDG_MIME_MAGIC_ERROR;
485 /* FIXME: need to get this defined in a <config.h> style file */
487 for (i = 0; i < matchlet->value_length; i = i + matchlet->word_size)
489 if (matchlet->word_size == 2)
490 *((xdg_uint16_t *) matchlet->value + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->value + i)));
491 else if (matchlet->word_size == 4)
492 *((xdg_uint32_t *) matchlet->value + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->value + i)));
495 if (matchlet->word_size == 2)
496 *((xdg_uint16_t *) matchlet->mask + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->mask + i)));
497 else if (matchlet->word_size == 4)
498 *((xdg_uint32_t *) matchlet->mask + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->mask + i)));
505 matchlet->next = match->matchlet;
506 match->matchlet = matchlet;
509 return XDG_MIME_MAGIC_MAGIC;
512 _xdg_mime_magic_matchlet_free (matchlet);
514 return XDG_MIME_MAGIC_EOF;
516 return XDG_MIME_MAGIC_ERROR;
520 _xdg_mime_magic_matchlet_compare_to_data (XdgMimeMagicMatchlet *matchlet,
525 for (i = matchlet->offset; i < matchlet->offset + matchlet->range_length; i++)
527 int valid_matchlet = TRUE;
529 if (i + matchlet->value_length > len)
534 for (j = 0; j < matchlet->value_length; j++)
536 if ((matchlet->value[j] & matchlet->mask[j]) !=
537 ((((unsigned char *) data)[j + i]) & matchlet->mask[j]))
539 valid_matchlet = FALSE;
546 for (j = 0; j < matchlet->value_length; j++)
548 if (matchlet->value[j] != ((unsigned char *) data)[j + i])
550 valid_matchlet = FALSE;
562 _xdg_mime_magic_matchlet_compare_level (XdgMimeMagicMatchlet *matchlet,
567 while ((matchlet != NULL) && (matchlet->indent == indent))
569 if (_xdg_mime_magic_matchlet_compare_to_data (matchlet, data, len))
571 if ((matchlet->next == NULL) ||
572 (matchlet->next->indent <= indent))
575 if (_xdg_mime_magic_matchlet_compare_level (matchlet->next,
584 matchlet = matchlet->next;
586 while (matchlet && matchlet->indent > indent);
593 _xdg_mime_magic_match_compare_to_data (XdgMimeMagicMatch *match,
597 return _xdg_mime_magic_matchlet_compare_level (match->matchlet, data, len, 0);
601 _xdg_mime_magic_insert_match (XdgMimeMagic *mime_magic,
602 XdgMimeMagicMatch *match)
604 XdgMimeMagicMatch *list;
606 if (mime_magic->match_list == NULL)
608 mime_magic->match_list = match;
612 if (match->priority > mime_magic->match_list->priority)
614 match->next = mime_magic->match_list;
615 mime_magic->match_list = match;
619 list = mime_magic->match_list;
620 while (list->next != NULL)
622 if (list->next->priority < match->priority)
624 match->next = list->next;
635 _xdg_mime_magic_new (void)
637 return calloc (1, sizeof (XdgMimeMagic));
641 _xdg_mime_magic_free (XdgMimeMagic *mime_magic)
644 _xdg_mime_magic_match_free (mime_magic->match_list);
650 _xdg_mime_magic_get_buffer_extents (XdgMimeMagic *mime_magic)
652 return mime_magic->max_extent;
656 _xdg_mime_magic_lookup_data (XdgMimeMagic *mime_magic,
660 const char *mime_types[],
663 XdgMimeMagicMatch *match;
664 const char *mime_type;
670 for (match = mime_magic->match_list; match; match = match->next)
672 if (_xdg_mime_magic_match_compare_to_data (match, data, len))
674 prio = match->priority;
675 mime_type = match->mime_type;
680 for (n = 0; n < n_mime_types; n++)
683 _xdg_mime_mime_type_equal (mime_types[n], match->mime_type))
684 mime_types[n] = NULL;
689 if (mime_type == NULL)
691 for (n = 0; n < n_mime_types; n++)
694 mime_type = mime_types[n];
705 _xdg_mime_update_mime_magic_extents (XdgMimeMagic *mime_magic)
707 XdgMimeMagicMatch *match;
710 for (match = mime_magic->match_list; match; match = match->next)
712 XdgMimeMagicMatchlet *matchlet;
714 for (matchlet = match->matchlet; matchlet; matchlet = matchlet->next)
718 extent = matchlet->value_length + matchlet->offset + matchlet->range_length;
719 if (max_extent < extent)
724 mime_magic->max_extent = max_extent;
727 static XdgMimeMagicMatchlet *
728 _xdg_mime_magic_matchlet_mirror (XdgMimeMagicMatchlet *matchlets)
730 XdgMimeMagicMatchlet *new_list;
731 XdgMimeMagicMatchlet *tmp;
733 if ((matchlets == NULL) || (matchlets->next == NULL))
740 XdgMimeMagicMatchlet *matchlet;
744 matchlet->next = new_list;
753 _xdg_mime_magic_read_magic_file (XdgMimeMagic *mime_magic,
756 XdgMimeMagicState state;
757 XdgMimeMagicMatch *match = NULL; /* Quiet compiler */
759 state = XDG_MIME_MAGIC_SECTION;
761 while (state != XDG_MIME_MAGIC_EOF)
765 case XDG_MIME_MAGIC_SECTION:
766 match = _xdg_mime_magic_match_new ();
767 state = _xdg_mime_magic_parse_header (magic_file, match);
768 if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
769 _xdg_mime_magic_match_free (match);
771 case XDG_MIME_MAGIC_MAGIC:
772 state = _xdg_mime_magic_parse_magic_line (magic_file, match);
773 if (state == XDG_MIME_MAGIC_SECTION ||
774 (state == XDG_MIME_MAGIC_EOF && match->mime_type))
776 match->matchlet = _xdg_mime_magic_matchlet_mirror (match->matchlet);
777 _xdg_mime_magic_insert_match (mime_magic, match);
779 else if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
780 _xdg_mime_magic_match_free (match);
782 case XDG_MIME_MAGIC_ERROR:
783 state = _xdg_mime_magic_parse_error (magic_file);
785 case XDG_MIME_MAGIC_EOF:
787 /* Make the compiler happy */
791 _xdg_mime_update_mime_magic_extents (mime_magic);
795 _xdg_mime_magic_read_from_file (XdgMimeMagic *mime_magic,
796 const char *file_name)
801 magic_file = fopen (file_name, "r");
803 if (magic_file == NULL)
806 if (fread (header, 1, 12, magic_file) == 12)
808 if (memcmp ("MIME-Magic\0\n", header, 12) == 0)
809 _xdg_mime_magic_read_magic_file (mime_magic, magic_file);