Merge branch 'test-ip_mreq_source-android-only' into 'master'
[glib.git] / gio / xdgmime / xdgmimemagic.c
blob51be9722bdaf8e02b8a2ac381f182e089560140e
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.1 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, see <http://www.gnu.org/licenses/>.
26 #include "config.h"
28 #include <assert.h>
29 #include "xdgmimemagic.h"
30 #include "xdgmimeint.h"
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <errno.h>
36 #include <limits.h>
38 #ifndef FALSE
39 #define FALSE (0)
40 #endif
42 #ifndef TRUE
43 #define TRUE (!FALSE)
44 #endif
46 #if !defined getc_unlocked && !defined HAVE_GETC_UNLOCKED
47 # define getc_unlocked(fp) getc (fp)
48 #endif
50 typedef struct XdgMimeMagicMatch XdgMimeMagicMatch;
51 typedef struct XdgMimeMagicMatchlet XdgMimeMagicMatchlet;
53 typedef enum
55 XDG_MIME_MAGIC_SECTION,
56 XDG_MIME_MAGIC_MAGIC,
57 XDG_MIME_MAGIC_ERROR,
58 XDG_MIME_MAGIC_EOF
59 } XdgMimeMagicState;
61 struct XdgMimeMagicMatch
63 const char *mime_type;
64 int priority;
65 XdgMimeMagicMatchlet *matchlet;
66 XdgMimeMagicMatch *next;
70 struct XdgMimeMagicMatchlet
72 int indent;
73 int offset;
74 unsigned int value_length;
75 unsigned char *value;
76 unsigned char *mask;
77 unsigned int range_length;
78 unsigned int word_size;
79 XdgMimeMagicMatchlet *next;
83 struct XdgMimeMagic
85 XdgMimeMagicMatch *match_list;
86 int max_extent;
89 static XdgMimeMagicMatch *
90 _xdg_mime_magic_match_new (void)
92 return calloc (1, sizeof (XdgMimeMagicMatch));
96 static XdgMimeMagicMatchlet *
97 _xdg_mime_magic_matchlet_new (void)
99 XdgMimeMagicMatchlet *matchlet;
101 matchlet = malloc (sizeof (XdgMimeMagicMatchlet));
103 matchlet->indent = 0;
104 matchlet->offset = 0;
105 matchlet->value_length = 0;
106 matchlet->value = NULL;
107 matchlet->mask = NULL;
108 matchlet->range_length = 1;
109 matchlet->word_size = 1;
110 matchlet->next = NULL;
112 return matchlet;
116 static void
117 _xdg_mime_magic_matchlet_free (XdgMimeMagicMatchlet *mime_magic_matchlet)
119 if (mime_magic_matchlet)
121 if (mime_magic_matchlet->next)
122 _xdg_mime_magic_matchlet_free (mime_magic_matchlet->next);
123 if (mime_magic_matchlet->value)
124 free (mime_magic_matchlet->value);
125 if (mime_magic_matchlet->mask)
126 free (mime_magic_matchlet->mask);
127 free (mime_magic_matchlet);
132 /* Frees mime_magic_match and the remainder of its list
134 static void
135 _xdg_mime_magic_match_free (XdgMimeMagicMatch *mime_magic_match)
137 XdgMimeMagicMatch *ptr, *next;
139 ptr = mime_magic_match;
140 while (ptr)
142 next = ptr->next;
144 if (ptr->mime_type)
145 free ((void *) ptr->mime_type);
146 if (ptr->matchlet)
147 _xdg_mime_magic_matchlet_free (ptr->matchlet);
148 free (ptr);
150 ptr = next;
154 /* Reads in a hunk of data until a newline character or a '\000' is hit. The
155 * returned string is null terminated, and doesn't include the newline.
157 static unsigned char *
158 _xdg_mime_magic_read_to_newline (FILE *magic_file,
159 int *end_of_file)
161 unsigned char *retval;
162 int c;
163 int len, pos;
165 len = 128;
166 pos = 0;
167 retval = malloc (len);
168 *end_of_file = FALSE;
170 while (TRUE)
172 c = getc_unlocked (magic_file);
173 if (c == EOF)
175 *end_of_file = TRUE;
176 break;
178 if (c == '\n' || c == '\000')
179 break;
180 retval[pos++] = (unsigned char) c;
181 if (pos % 128 == 127)
183 len = len + 128;
184 retval = realloc (retval, len);
188 retval[pos] = '\000';
189 return retval;
192 /* Returns the number read from the file, or -1 if no number could be read.
194 static int
195 _xdg_mime_magic_read_a_number (FILE *magic_file,
196 int *end_of_file)
198 /* LONG_MAX is about 20 characters on my system */
199 #define MAX_NUMBER_SIZE 30
200 char number_string[MAX_NUMBER_SIZE + 1];
201 int pos = 0;
202 int c;
203 long retval = -1;
205 while (TRUE)
207 c = getc_unlocked (magic_file);
209 if (c == EOF)
211 *end_of_file = TRUE;
212 break;
214 if (! isdigit (c))
216 ungetc (c, magic_file);
217 break;
219 number_string[pos] = (char) c;
220 pos++;
221 if (pos == MAX_NUMBER_SIZE)
222 break;
224 if (pos > 0)
226 number_string[pos] = '\000';
227 errno = 0;
228 retval = strtol (number_string, NULL, 10);
230 if ((retval < INT_MIN) || (retval > INT_MAX) || (errno != 0))
231 return -1;
234 return retval;
237 /* Headers are of the format:
238 * [<priority>:<mime-type>]
240 static XdgMimeMagicState
241 _xdg_mime_magic_parse_header (FILE *magic_file, XdgMimeMagicMatch *match)
243 int c;
244 char *buffer;
245 char *end_ptr;
246 int end_of_file = 0;
248 assert (magic_file != NULL);
249 assert (match != NULL);
251 c = getc_unlocked (magic_file);
252 if (c == EOF)
253 return XDG_MIME_MAGIC_EOF;
254 if (c != '[')
255 return XDG_MIME_MAGIC_ERROR;
257 match->priority = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
258 if (end_of_file)
259 return XDG_MIME_MAGIC_EOF;
260 if (match->priority == -1)
261 return XDG_MIME_MAGIC_ERROR;
263 c = getc_unlocked (magic_file);
264 if (c == EOF)
265 return XDG_MIME_MAGIC_EOF;
266 if (c != ':')
267 return XDG_MIME_MAGIC_ERROR;
269 buffer = (char *)_xdg_mime_magic_read_to_newline (magic_file, &end_of_file);
270 if (end_of_file)
272 free (buffer);
273 return XDG_MIME_MAGIC_EOF;
276 end_ptr = buffer;
277 while (*end_ptr != ']' && *end_ptr != '\000' && *end_ptr != '\n')
278 end_ptr++;
279 if (*end_ptr != ']')
281 free (buffer);
282 return XDG_MIME_MAGIC_ERROR;
284 *end_ptr = '\000';
286 match->mime_type = strdup (buffer);
287 free (buffer);
289 return XDG_MIME_MAGIC_MAGIC;
292 static XdgMimeMagicState
293 _xdg_mime_magic_parse_error (FILE *magic_file)
295 int c;
297 while (1)
299 c = getc_unlocked (magic_file);
300 if (c == EOF)
301 return XDG_MIME_MAGIC_EOF;
302 if (c == '\n')
303 return XDG_MIME_MAGIC_SECTION;
307 /* Headers are of the format:
308 * [ indent ] ">" start-offset "=" value
309 * [ "&" mask ] [ "~" word-size ] [ "+" range-length ] "\n"
311 static XdgMimeMagicState
312 _xdg_mime_magic_parse_magic_line (FILE *magic_file,
313 XdgMimeMagicMatch *match)
315 XdgMimeMagicMatchlet *matchlet;
316 int c;
317 int end_of_file;
318 int indent = 0;
319 int bytes_read;
321 assert (magic_file != NULL);
323 /* Sniff the buffer to make sure it's a valid line */
324 c = getc_unlocked (magic_file);
325 if (c == EOF)
326 return XDG_MIME_MAGIC_EOF;
327 else if (c == '[')
329 ungetc (c, magic_file);
330 return XDG_MIME_MAGIC_SECTION;
332 else if (c == '\n')
333 return XDG_MIME_MAGIC_MAGIC;
335 /* At this point, it must be a digit or a '>' */
336 end_of_file = FALSE;
337 if (isdigit (c))
339 ungetc (c, magic_file);
340 indent = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
341 if (end_of_file)
342 return XDG_MIME_MAGIC_EOF;
343 if (indent == -1)
344 return XDG_MIME_MAGIC_ERROR;
345 c = getc_unlocked (magic_file);
346 if (c == EOF)
347 return XDG_MIME_MAGIC_EOF;
350 if (c != '>')
351 return XDG_MIME_MAGIC_ERROR;
353 matchlet = _xdg_mime_magic_matchlet_new ();
354 matchlet->indent = indent;
355 matchlet->offset = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
356 if (end_of_file)
358 _xdg_mime_magic_matchlet_free (matchlet);
359 return XDG_MIME_MAGIC_EOF;
361 if (matchlet->offset == -1)
363 _xdg_mime_magic_matchlet_free (matchlet);
364 return XDG_MIME_MAGIC_ERROR;
366 c = getc_unlocked (magic_file);
367 if (c == EOF)
369 _xdg_mime_magic_matchlet_free (matchlet);
370 return XDG_MIME_MAGIC_EOF;
372 else if (c != '=')
374 _xdg_mime_magic_matchlet_free (matchlet);
375 return XDG_MIME_MAGIC_ERROR;
378 /* Next two bytes determine how long the value is */
379 matchlet->value_length = 0;
380 c = getc_unlocked (magic_file);
381 if (c == EOF)
383 _xdg_mime_magic_matchlet_free (matchlet);
384 return XDG_MIME_MAGIC_EOF;
386 matchlet->value_length = c & 0xFF;
387 matchlet->value_length = matchlet->value_length << 8;
389 c = getc_unlocked (magic_file);
390 if (c == EOF)
392 _xdg_mime_magic_matchlet_free (matchlet);
393 return XDG_MIME_MAGIC_EOF;
395 matchlet->value_length = matchlet->value_length + (c & 0xFF);
397 matchlet->value = malloc (matchlet->value_length);
399 /* OOM */
400 if (matchlet->value == NULL)
402 _xdg_mime_magic_matchlet_free (matchlet);
403 return XDG_MIME_MAGIC_ERROR;
405 bytes_read = fread (matchlet->value, 1, matchlet->value_length, magic_file);
406 if (bytes_read != matchlet->value_length)
408 _xdg_mime_magic_matchlet_free (matchlet);
409 if (feof (magic_file))
410 return XDG_MIME_MAGIC_EOF;
411 else
412 return XDG_MIME_MAGIC_ERROR;
415 c = getc_unlocked (magic_file);
416 if (c == '&')
418 matchlet->mask = malloc (matchlet->value_length);
419 /* OOM */
420 if (matchlet->mask == NULL)
422 _xdg_mime_magic_matchlet_free (matchlet);
423 return XDG_MIME_MAGIC_ERROR;
425 bytes_read = fread (matchlet->mask, 1, matchlet->value_length, magic_file);
426 if (bytes_read != matchlet->value_length)
428 _xdg_mime_magic_matchlet_free (matchlet);
429 if (feof (magic_file))
430 return XDG_MIME_MAGIC_EOF;
431 else
432 return XDG_MIME_MAGIC_ERROR;
434 c = getc_unlocked (magic_file);
437 if (c == '~')
439 matchlet->word_size = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
440 if (end_of_file)
442 _xdg_mime_magic_matchlet_free (matchlet);
443 return XDG_MIME_MAGIC_EOF;
445 if (matchlet->word_size != 0 &&
446 matchlet->word_size != 1 &&
447 matchlet->word_size != 2 &&
448 matchlet->word_size != 4)
450 _xdg_mime_magic_matchlet_free (matchlet);
451 return XDG_MIME_MAGIC_ERROR;
453 c = getc_unlocked (magic_file);
456 if (c == '+')
458 matchlet->range_length = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
459 if (end_of_file)
461 _xdg_mime_magic_matchlet_free (matchlet);
462 return XDG_MIME_MAGIC_EOF;
464 if (matchlet->range_length == -1)
466 _xdg_mime_magic_matchlet_free (matchlet);
467 return XDG_MIME_MAGIC_ERROR;
469 c = getc_unlocked (magic_file);
473 if (c == '\n')
475 /* We clean up the matchlet, byte swapping if needed */
476 if (matchlet->word_size > 1)
478 #if LITTLE_ENDIAN
479 int i;
480 #endif
481 if (matchlet->value_length % matchlet->word_size != 0)
483 _xdg_mime_magic_matchlet_free (matchlet);
484 return XDG_MIME_MAGIC_ERROR;
486 /* FIXME: need to get this defined in a <config.h> style file */
487 #if LITTLE_ENDIAN
488 for (i = 0; i < matchlet->value_length; i = i + matchlet->word_size)
490 if (matchlet->word_size == 2)
491 *((xdg_uint16_t *) matchlet->value + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->value + i)));
492 else if (matchlet->word_size == 4)
493 *((xdg_uint32_t *) matchlet->value + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->value + i)));
494 if (matchlet->mask)
496 if (matchlet->word_size == 2)
497 *((xdg_uint16_t *) matchlet->mask + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->mask + i)));
498 else if (matchlet->word_size == 4)
499 *((xdg_uint32_t *) matchlet->mask + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->mask + i)));
503 #endif
506 matchlet->next = match->matchlet;
507 match->matchlet = matchlet;
510 return XDG_MIME_MAGIC_MAGIC;
513 _xdg_mime_magic_matchlet_free (matchlet);
514 if (c == EOF)
515 return XDG_MIME_MAGIC_EOF;
517 return XDG_MIME_MAGIC_ERROR;
520 static int
521 _xdg_mime_magic_matchlet_compare_to_data (XdgMimeMagicMatchlet *matchlet,
522 const void *data,
523 size_t len)
525 int i, j;
526 for (i = matchlet->offset; i < matchlet->offset + matchlet->range_length; i++)
528 int valid_matchlet = TRUE;
530 if (i + matchlet->value_length > len)
531 return FALSE;
533 if (matchlet->mask)
535 for (j = 0; j < matchlet->value_length; j++)
537 if ((matchlet->value[j] & matchlet->mask[j]) !=
538 ((((unsigned char *) data)[j + i]) & matchlet->mask[j]))
540 valid_matchlet = FALSE;
541 break;
545 else
547 for (j = 0; j < matchlet->value_length; j++)
549 if (matchlet->value[j] != ((unsigned char *) data)[j + i])
551 valid_matchlet = FALSE;
552 break;
556 if (valid_matchlet)
557 return TRUE;
559 return FALSE;
562 static int
563 _xdg_mime_magic_matchlet_compare_level (XdgMimeMagicMatchlet *matchlet,
564 const void *data,
565 size_t len,
566 int indent)
568 while ((matchlet != NULL) && (matchlet->indent == indent))
570 if (_xdg_mime_magic_matchlet_compare_to_data (matchlet, data, len))
572 if ((matchlet->next == NULL) ||
573 (matchlet->next->indent <= indent))
574 return TRUE;
576 if (_xdg_mime_magic_matchlet_compare_level (matchlet->next,
577 data,
578 len,
579 indent + 1))
580 return TRUE;
585 matchlet = matchlet->next;
587 while (matchlet && matchlet->indent > indent);
590 return FALSE;
593 static int
594 _xdg_mime_magic_match_compare_to_data (XdgMimeMagicMatch *match,
595 const void *data,
596 size_t len)
598 return _xdg_mime_magic_matchlet_compare_level (match->matchlet, data, len, 0);
601 static void
602 _xdg_mime_magic_insert_match (XdgMimeMagic *mime_magic,
603 XdgMimeMagicMatch *match)
605 XdgMimeMagicMatch *list;
607 if (mime_magic->match_list == NULL)
609 mime_magic->match_list = match;
610 return;
613 if (match->priority > mime_magic->match_list->priority)
615 match->next = mime_magic->match_list;
616 mime_magic->match_list = match;
617 return;
620 list = mime_magic->match_list;
621 while (list->next != NULL)
623 if (list->next->priority < match->priority)
625 match->next = list->next;
626 list->next = match;
627 return;
629 list = list->next;
631 list->next = match;
632 match->next = NULL;
635 XdgMimeMagic *
636 _xdg_mime_magic_new (void)
638 return calloc (1, sizeof (XdgMimeMagic));
641 void
642 _xdg_mime_magic_free (XdgMimeMagic *mime_magic)
644 if (mime_magic) {
645 _xdg_mime_magic_match_free (mime_magic->match_list);
646 free (mime_magic);
651 _xdg_mime_magic_get_buffer_extents (XdgMimeMagic *mime_magic)
653 return mime_magic->max_extent;
656 const char *
657 _xdg_mime_magic_lookup_data (XdgMimeMagic *mime_magic,
658 const void *data,
659 size_t len,
660 int *result_prio,
661 const char *mime_types[],
662 int n_mime_types)
664 XdgMimeMagicMatch *match;
665 const char *mime_type;
666 int n;
667 int prio;
669 prio = 0;
670 mime_type = NULL;
671 for (match = mime_magic->match_list; match; match = match->next)
673 if (_xdg_mime_magic_match_compare_to_data (match, data, len))
675 prio = match->priority;
676 mime_type = match->mime_type;
677 break;
679 else
681 for (n = 0; n < n_mime_types; n++)
683 if (mime_types[n] &&
684 _xdg_mime_mime_type_equal (mime_types[n], match->mime_type))
685 mime_types[n] = NULL;
690 if (mime_type == NULL)
692 for (n = 0; n < n_mime_types; n++)
694 if (mime_types[n])
695 mime_type = mime_types[n];
699 if (result_prio)
700 *result_prio = prio;
702 return mime_type;
705 static void
706 _xdg_mime_update_mime_magic_extents (XdgMimeMagic *mime_magic)
708 XdgMimeMagicMatch *match;
709 int max_extent = 0;
711 for (match = mime_magic->match_list; match; match = match->next)
713 XdgMimeMagicMatchlet *matchlet;
715 for (matchlet = match->matchlet; matchlet; matchlet = matchlet->next)
717 int extent;
719 extent = matchlet->value_length + matchlet->offset + matchlet->range_length;
720 if (max_extent < extent)
721 max_extent = extent;
725 mime_magic->max_extent = max_extent;
728 static XdgMimeMagicMatchlet *
729 _xdg_mime_magic_matchlet_mirror (XdgMimeMagicMatchlet *matchlets)
731 XdgMimeMagicMatchlet *new_list;
732 XdgMimeMagicMatchlet *tmp;
734 if ((matchlets == NULL) || (matchlets->next == NULL))
735 return matchlets;
737 new_list = NULL;
738 tmp = matchlets;
739 while (tmp != NULL)
741 XdgMimeMagicMatchlet *matchlet;
743 matchlet = tmp;
744 tmp = tmp->next;
745 matchlet->next = new_list;
746 new_list = matchlet;
749 return new_list;
753 static void
754 _xdg_mime_magic_read_magic_file (XdgMimeMagic *mime_magic,
755 FILE *magic_file)
757 XdgMimeMagicState state;
758 XdgMimeMagicMatch *match = NULL; /* Quiet compiler */
760 state = XDG_MIME_MAGIC_SECTION;
762 while (state != XDG_MIME_MAGIC_EOF)
764 switch (state)
766 case XDG_MIME_MAGIC_SECTION:
767 match = _xdg_mime_magic_match_new ();
768 state = _xdg_mime_magic_parse_header (magic_file, match);
769 if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
770 _xdg_mime_magic_match_free (match);
771 break;
772 case XDG_MIME_MAGIC_MAGIC:
773 state = _xdg_mime_magic_parse_magic_line (magic_file, match);
774 if (state == XDG_MIME_MAGIC_SECTION ||
775 (state == XDG_MIME_MAGIC_EOF && match->mime_type))
777 match->matchlet = _xdg_mime_magic_matchlet_mirror (match->matchlet);
778 _xdg_mime_magic_insert_match (mime_magic, match);
780 else if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
781 _xdg_mime_magic_match_free (match);
782 break;
783 case XDG_MIME_MAGIC_ERROR:
784 state = _xdg_mime_magic_parse_error (magic_file);
785 break;
786 case XDG_MIME_MAGIC_EOF:
787 default:
788 /* Make the compiler happy */
789 assert (0);
792 _xdg_mime_update_mime_magic_extents (mime_magic);
795 void
796 _xdg_mime_magic_read_from_file (XdgMimeMagic *mime_magic,
797 const char *file_name)
799 FILE *magic_file;
800 char header[12];
802 magic_file = fopen (file_name, "r");
804 if (magic_file == NULL)
805 return;
807 if (fread (header, 1, 12, magic_file) == 12)
809 if (memcmp ("MIME-Magic\0\n", header, 12) == 0)
810 _xdg_mime_magic_read_magic_file (mime_magic, magic_file);
813 fclose (magic_file);