Dont add null mimetypes. Fixes bgo# 337431. The patch hasnt been officially accepted...
[beagle.git] / glue / xdgmime / xdgmimemagic.c
blob53630b05f756c52e86ba76e871d7d306c1e80721
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 #ifndef FALSE
43 #define FALSE (0)
44 #endif
46 #ifndef TRUE
47 #define TRUE (!FALSE)
48 #endif
50 extern int errno;
52 typedef struct XdgMimeMagicMatch XdgMimeMagicMatch;
53 typedef struct XdgMimeMagicMatchlet XdgMimeMagicMatchlet;
55 typedef enum
57 XDG_MIME_MAGIC_SECTION,
58 XDG_MIME_MAGIC_MAGIC,
59 XDG_MIME_MAGIC_ERROR,
60 XDG_MIME_MAGIC_EOF
61 } XdgMimeMagicState;
63 struct XdgMimeMagicMatch
65 const char *mime_type;
66 int priority;
67 XdgMimeMagicMatchlet *matchlet;
68 XdgMimeMagicMatch *next;
72 struct XdgMimeMagicMatchlet
74 int indent;
75 int offset;
76 unsigned int value_length;
77 unsigned char *value;
78 unsigned char *mask;
79 unsigned int range_length;
80 unsigned int word_size;
81 XdgMimeMagicMatchlet *next;
85 struct XdgMimeMagic
87 XdgMimeMagicMatch *match_list;
88 int max_extent;
91 static XdgMimeMagicMatch *
92 _xdg_mime_magic_match_new (void)
94 return calloc (1, sizeof (XdgMimeMagicMatch));
98 static XdgMimeMagicMatchlet *
99 _xdg_mime_magic_matchlet_new (void)
101 XdgMimeMagicMatchlet *matchlet;
103 matchlet = malloc (sizeof (XdgMimeMagicMatchlet));
105 matchlet->indent = 0;
106 matchlet->offset = 0;
107 matchlet->value_length = 0;
108 matchlet->value = NULL;
109 matchlet->mask = NULL;
110 matchlet->range_length = 1;
111 matchlet->word_size = 1;
112 matchlet->next = NULL;
114 return matchlet;
118 static void
119 _xdg_mime_magic_matchlet_free (XdgMimeMagicMatchlet *mime_magic_matchlet)
121 if (mime_magic_matchlet)
123 if (mime_magic_matchlet->next)
124 _xdg_mime_magic_matchlet_free (mime_magic_matchlet->next);
125 if (mime_magic_matchlet->value)
126 free (mime_magic_matchlet->value);
127 if (mime_magic_matchlet->mask)
128 free (mime_magic_matchlet->mask);
129 free (mime_magic_matchlet);
134 /* Frees mime_magic_match and the remainder of its list
136 static void
137 _xdg_mime_magic_match_free (XdgMimeMagicMatch *mime_magic_match)
139 XdgMimeMagicMatch *ptr, *next;
141 ptr = mime_magic_match;
142 while (ptr)
144 next = ptr->next;
146 if (ptr->mime_type)
147 free ((void *) ptr->mime_type);
148 if (ptr->matchlet)
149 _xdg_mime_magic_matchlet_free (ptr->matchlet);
150 free (ptr);
152 ptr = next;
156 /* Reads in a hunk of data until a newline character or a '\000' is hit. The
157 * returned string is null terminated, and doesn't include the newline.
159 static unsigned char *
160 _xdg_mime_magic_read_to_newline (FILE *magic_file,
161 int *end_of_file)
163 unsigned char *retval;
164 int c;
165 int len, pos;
167 len = 128;
168 pos = 0;
169 retval = malloc (len);
170 *end_of_file = FALSE;
172 while (TRUE)
174 c = getc_unlocked (magic_file);
175 if (c == EOF)
177 *end_of_file = TRUE;
178 break;
180 if (c == '\n' || c == '\000')
181 break;
182 retval[pos++] = (unsigned char) c;
183 if (pos % 128 == 127)
185 len = len + 128;
186 retval = realloc (retval, len);
190 retval[pos] = '\000';
191 return retval;
194 /* Returns the number read from the file, or -1 if no number could be read.
196 static int
197 _xdg_mime_magic_read_a_number (FILE *magic_file,
198 int *end_of_file)
200 /* LONG_MAX is about 20 characters on my system */
201 #define MAX_NUMBER_SIZE 30
202 char number_string[MAX_NUMBER_SIZE + 1];
203 int pos = 0;
204 int c;
205 long retval = -1;
207 while (TRUE)
209 c = getc_unlocked (magic_file);
211 if (c == EOF)
213 *end_of_file = TRUE;
214 break;
216 if (! isdigit (c))
218 ungetc (c, magic_file);
219 break;
221 number_string[pos] = (char) c;
222 pos++;
223 if (pos == MAX_NUMBER_SIZE)
224 break;
226 if (pos > 0)
228 number_string[pos] = '\000';
229 errno = 0;
230 retval = strtol (number_string, NULL, 10);
232 if ((retval < INT_MIN) || (retval > INT_MAX) || (errno != 0))
233 return -1;
236 return retval;
239 /* Headers are of the format:
240 * [<priority>:<mime-type>]
242 static XdgMimeMagicState
243 _xdg_mime_magic_parse_header (FILE *magic_file, XdgMimeMagicMatch *match)
245 int c;
246 char *buffer;
247 char *end_ptr;
248 int end_of_file = 0;
250 assert (magic_file != NULL);
251 assert (match != NULL);
253 c = getc_unlocked (magic_file);
254 if (c == EOF)
255 return XDG_MIME_MAGIC_EOF;
256 if (c != '[')
257 return XDG_MIME_MAGIC_ERROR;
259 match->priority = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
260 if (end_of_file)
261 return XDG_MIME_MAGIC_EOF;
262 if (match->priority == -1)
263 return XDG_MIME_MAGIC_ERROR;
265 c = getc_unlocked (magic_file);
266 if (c == EOF)
267 return XDG_MIME_MAGIC_EOF;
268 if (c != ':')
269 return XDG_MIME_MAGIC_ERROR;
271 buffer = (char *)_xdg_mime_magic_read_to_newline (magic_file, &end_of_file);
272 if (end_of_file)
273 return XDG_MIME_MAGIC_EOF;
275 end_ptr = buffer;
276 while (*end_ptr != ']' && *end_ptr != '\000' && *end_ptr != '\n')
277 end_ptr++;
278 if (*end_ptr != ']')
280 free (buffer);
281 return XDG_MIME_MAGIC_ERROR;
283 *end_ptr = '\000';
285 match->mime_type = strdup (buffer);
286 free (buffer);
288 return XDG_MIME_MAGIC_MAGIC;
291 static XdgMimeMagicState
292 _xdg_mime_magic_parse_error (FILE *magic_file)
294 int c;
296 while (1)
298 c = getc_unlocked (magic_file);
299 if (c == EOF)
300 return XDG_MIME_MAGIC_EOF;
301 if (c == '\n')
302 return XDG_MIME_MAGIC_SECTION;
306 /* Headers are of the format:
307 * [ indent ] ">" start-offset "=" value
308 * [ "&" mask ] [ "~" word-size ] [ "+" range-length ] "\n"
310 static XdgMimeMagicState
311 _xdg_mime_magic_parse_magic_line (FILE *magic_file,
312 XdgMimeMagicMatch *match)
314 XdgMimeMagicMatchlet *matchlet;
315 int c;
316 int end_of_file;
317 int indent = 0;
318 int bytes_read;
320 assert (magic_file != NULL);
322 /* Sniff the buffer to make sure it's a valid line */
323 c = getc_unlocked (magic_file);
324 if (c == EOF)
325 return XDG_MIME_MAGIC_EOF;
326 else if (c == '[')
328 ungetc (c, magic_file);
329 return XDG_MIME_MAGIC_SECTION;
331 else if (c == '\n')
332 return XDG_MIME_MAGIC_MAGIC;
334 /* At this point, it must be a digit or a '>' */
335 end_of_file = FALSE;
336 if (isdigit (c))
338 ungetc (c, magic_file);
339 indent = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
340 if (end_of_file)
341 return XDG_MIME_MAGIC_EOF;
342 if (indent == -1)
343 return XDG_MIME_MAGIC_ERROR;
344 c = getc_unlocked (magic_file);
345 if (c == EOF)
346 return XDG_MIME_MAGIC_EOF;
349 if (c != '>')
350 return XDG_MIME_MAGIC_ERROR;
352 matchlet = _xdg_mime_magic_matchlet_new ();
353 matchlet->indent = indent;
354 matchlet->offset = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
355 if (end_of_file)
357 _xdg_mime_magic_matchlet_free (matchlet);
358 return XDG_MIME_MAGIC_EOF;
360 if (matchlet->offset == -1)
362 _xdg_mime_magic_matchlet_free (matchlet);
363 return XDG_MIME_MAGIC_ERROR;
365 c = getc_unlocked (magic_file);
366 if (c == EOF)
368 _xdg_mime_magic_matchlet_free (matchlet);
369 return XDG_MIME_MAGIC_EOF;
371 else if (c != '=')
373 _xdg_mime_magic_matchlet_free (matchlet);
374 return XDG_MIME_MAGIC_ERROR;
377 /* Next two bytes determine how long the value is */
378 matchlet->value_length = 0;
379 c = getc_unlocked (magic_file);
380 if (c == EOF)
382 _xdg_mime_magic_matchlet_free (matchlet);
383 return XDG_MIME_MAGIC_EOF;
385 matchlet->value_length = c & 0xFF;
386 matchlet->value_length = matchlet->value_length << 8;
388 c = getc_unlocked (magic_file);
389 if (c == EOF)
391 _xdg_mime_magic_matchlet_free (matchlet);
392 return XDG_MIME_MAGIC_EOF;
394 matchlet->value_length = matchlet->value_length + (c & 0xFF);
396 matchlet->value = malloc (matchlet->value_length);
398 /* OOM */
399 if (matchlet->value == NULL)
401 _xdg_mime_magic_matchlet_free (matchlet);
402 return XDG_MIME_MAGIC_ERROR;
404 bytes_read = fread (matchlet->value, 1, matchlet->value_length, magic_file);
405 if (bytes_read != matchlet->value_length)
407 _xdg_mime_magic_matchlet_free (matchlet);
408 if (feof (magic_file))
409 return XDG_MIME_MAGIC_EOF;
410 else
411 return XDG_MIME_MAGIC_ERROR;
414 c = getc_unlocked (magic_file);
415 if (c == '&')
417 matchlet->mask = malloc (matchlet->value_length);
418 /* OOM */
419 if (matchlet->mask == NULL)
421 _xdg_mime_magic_matchlet_free (matchlet);
422 return XDG_MIME_MAGIC_ERROR;
424 bytes_read = fread (matchlet->mask, 1, matchlet->value_length, magic_file);
425 if (bytes_read != matchlet->value_length)
427 _xdg_mime_magic_matchlet_free (matchlet);
428 if (feof (magic_file))
429 return XDG_MIME_MAGIC_EOF;
430 else
431 return XDG_MIME_MAGIC_ERROR;
433 c = getc_unlocked (magic_file);
436 if (c == '~')
438 matchlet->word_size = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
439 if (end_of_file)
441 _xdg_mime_magic_matchlet_free (matchlet);
442 return XDG_MIME_MAGIC_EOF;
444 if (matchlet->word_size != 0 &&
445 matchlet->word_size != 1 &&
446 matchlet->word_size != 2 &&
447 matchlet->word_size != 4)
449 _xdg_mime_magic_matchlet_free (matchlet);
450 return XDG_MIME_MAGIC_ERROR;
452 c = getc_unlocked (magic_file);
455 if (c == '+')
457 matchlet->range_length = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
458 if (end_of_file)
460 _xdg_mime_magic_matchlet_free (matchlet);
461 return XDG_MIME_MAGIC_EOF;
463 if (matchlet->range_length == -1)
465 _xdg_mime_magic_matchlet_free (matchlet);
466 return XDG_MIME_MAGIC_ERROR;
468 c = getc_unlocked (magic_file);
472 if (c == '\n')
474 /* We clean up the matchlet, byte swapping if needed */
475 if (matchlet->word_size > 1)
477 #if LITTLE_ENDIAN
478 int i;
479 #endif
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 */
486 #if LITTLE_ENDIAN
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)));
493 if (matchlet->mask)
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)));
502 #endif
505 matchlet->next = match->matchlet;
506 match->matchlet = matchlet;
509 return XDG_MIME_MAGIC_MAGIC;
512 _xdg_mime_magic_matchlet_free (matchlet);
513 if (c == EOF)
514 return XDG_MIME_MAGIC_EOF;
516 return XDG_MIME_MAGIC_ERROR;
519 static int
520 _xdg_mime_magic_matchlet_compare_to_data (XdgMimeMagicMatchlet *matchlet,
521 const void *data,
522 size_t len)
524 int i, j;
525 for (i = matchlet->offset; i < matchlet->offset + matchlet->range_length; i++)
527 int valid_matchlet = TRUE;
529 if (i + matchlet->value_length > len)
530 return FALSE;
532 if (matchlet->mask)
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;
540 break;
544 else
546 for (j = 0; j < matchlet->value_length; j++)
548 if (matchlet->value[j] != ((unsigned char *) data)[j + i])
550 valid_matchlet = FALSE;
551 break;
555 if (valid_matchlet)
556 return TRUE;
558 return FALSE;
561 static int
562 _xdg_mime_magic_matchlet_compare_level (XdgMimeMagicMatchlet *matchlet,
563 const void *data,
564 size_t len,
565 int indent)
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))
573 return TRUE;
575 if (_xdg_mime_magic_matchlet_compare_level (matchlet->next,
576 data,
577 len,
578 indent + 1))
579 return TRUE;
584 matchlet = matchlet->next;
586 while (matchlet && matchlet->indent > indent);
589 return FALSE;
592 static int
593 _xdg_mime_magic_match_compare_to_data (XdgMimeMagicMatch *match,
594 const void *data,
595 size_t len)
597 return _xdg_mime_magic_matchlet_compare_level (match->matchlet, data, len, 0);
600 static void
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;
609 return;
612 if (match->priority > mime_magic->match_list->priority)
614 match->next = mime_magic->match_list;
615 mime_magic->match_list = match;
616 return;
619 list = mime_magic->match_list;
620 while (list->next != NULL)
622 if (list->next->priority < match->priority)
624 match->next = list->next;
625 list->next = match;
626 return;
628 list = list->next;
630 list->next = match;
631 match->next = NULL;
634 XdgMimeMagic *
635 _xdg_mime_magic_new (void)
637 return calloc (1, sizeof (XdgMimeMagic));
640 void
641 _xdg_mime_magic_free (XdgMimeMagic *mime_magic)
643 if (mime_magic) {
644 _xdg_mime_magic_match_free (mime_magic->match_list);
645 free (mime_magic);
650 _xdg_mime_magic_get_buffer_extents (XdgMimeMagic *mime_magic)
652 return mime_magic->max_extent;
655 const char *
656 _xdg_mime_magic_lookup_data (XdgMimeMagic *mime_magic,
657 const void *data,
658 size_t len,
659 const char *mime_types[],
660 int n_mime_types)
662 XdgMimeMagicMatch *match;
663 const char *mime_type;
664 int n;
665 int priority;
666 int had_match;
668 mime_type = NULL;
669 priority = 0;
670 had_match = 0;
671 for (match = mime_magic->match_list; match; match = match->next)
673 if (_xdg_mime_magic_match_compare_to_data (match, data, len))
675 if (!had_match || match->priority > priority ||
676 (mime_type != NULL && _xdg_mime_mime_type_subclass (match->mime_type, mime_type)))
678 mime_type = match->mime_type;
679 priority = match->priority;
681 else if (had_match && match->priority == priority)
682 /* multiple unrelated patterns with the same priority matched,
683 * so we can't tell what type this is. */
684 mime_type = NULL;
686 had_match = 1;
688 else
690 for (n = 0; n < n_mime_types; n++)
692 if (mime_types[n] &&
693 _xdg_mime_mime_type_equal (mime_types[n], match->mime_type))
694 mime_types[n] = NULL;
699 if (mime_type == NULL)
701 for (n = 0; n < n_mime_types; n++)
703 if (mime_types[n])
704 mime_type = mime_types[n];
708 return mime_type;
711 static void
712 _xdg_mime_update_mime_magic_extents (XdgMimeMagic *mime_magic)
714 XdgMimeMagicMatch *match;
715 int max_extent = 0;
717 for (match = mime_magic->match_list; match; match = match->next)
719 XdgMimeMagicMatchlet *matchlet;
721 for (matchlet = match->matchlet; matchlet; matchlet = matchlet->next)
723 int extent;
725 extent = matchlet->value_length + matchlet->offset + matchlet->range_length;
726 if (max_extent < extent)
727 max_extent = extent;
731 mime_magic->max_extent = max_extent;
734 static XdgMimeMagicMatchlet *
735 _xdg_mime_magic_matchlet_mirror (XdgMimeMagicMatchlet *matchlets)
737 XdgMimeMagicMatchlet *new_list;
738 XdgMimeMagicMatchlet *tmp;
740 if ((matchlets == NULL) || (matchlets->next == NULL))
741 return matchlets;
743 new_list = NULL;
744 tmp = matchlets;
745 while (tmp != NULL)
747 XdgMimeMagicMatchlet *matchlet;
749 matchlet = tmp;
750 tmp = tmp->next;
751 matchlet->next = new_list;
752 new_list = matchlet;
755 return new_list;
759 static void
760 _xdg_mime_magic_read_magic_file (XdgMimeMagic *mime_magic,
761 FILE *magic_file)
763 XdgMimeMagicState state;
764 XdgMimeMagicMatch *match = NULL; /* Quiet compiler */
766 state = XDG_MIME_MAGIC_SECTION;
768 while (state != XDG_MIME_MAGIC_EOF)
770 switch (state)
772 case XDG_MIME_MAGIC_SECTION:
773 match = _xdg_mime_magic_match_new ();
774 state = _xdg_mime_magic_parse_header (magic_file, match);
775 if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
776 _xdg_mime_magic_match_free (match);
777 break;
778 case XDG_MIME_MAGIC_MAGIC:
779 state = _xdg_mime_magic_parse_magic_line (magic_file, match);
780 if (state == XDG_MIME_MAGIC_SECTION ||
781 (state == XDG_MIME_MAGIC_EOF && match->mime_type))
783 match->matchlet = _xdg_mime_magic_matchlet_mirror (match->matchlet);
784 _xdg_mime_magic_insert_match (mime_magic, match);
786 else if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
787 _xdg_mime_magic_match_free (match);
788 break;
789 case XDG_MIME_MAGIC_ERROR:
790 state = _xdg_mime_magic_parse_error (magic_file);
791 break;
792 case XDG_MIME_MAGIC_EOF:
793 default:
794 /* Make the compiler happy */
795 assert (0);
798 _xdg_mime_update_mime_magic_extents (mime_magic);
801 void
802 _xdg_mime_magic_read_from_file (XdgMimeMagic *mime_magic,
803 const char *file_name)
805 FILE *magic_file;
806 char header[12];
808 magic_file = fopen (file_name, "r");
810 if (magic_file == NULL)
811 return;
813 if (fread (header, 1, 12, magic_file) == 12)
815 if (memcmp ("MIME-Magic\0\n", header, 12) == 0)
816 _xdg_mime_magic_read_magic_file (mime_magic, magic_file);
819 fclose (magic_file);