configure.in, AssemblyInfo.cs: For those unfortunate earthlings without libchm, libwv...
[beagle.git] / glue / xdgmime / xdgmimemagic.c
blob8258b2bb1bc814ff7f9dd632ea38d178b8a381b6
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 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)
271 return XDG_MIME_MAGIC_EOF;
273 end_ptr = buffer;
274 while (*end_ptr != ']' && *end_ptr != '\000' && *end_ptr != '\n')
275 end_ptr++;
276 if (*end_ptr != ']')
278 free (buffer);
279 return XDG_MIME_MAGIC_ERROR;
281 *end_ptr = '\000';
283 match->mime_type = strdup (buffer);
284 free (buffer);
286 return XDG_MIME_MAGIC_MAGIC;
289 static XdgMimeMagicState
290 _xdg_mime_magic_parse_error (FILE *magic_file)
292 int c;
294 while (1)
296 c = getc_unlocked (magic_file);
297 if (c == EOF)
298 return XDG_MIME_MAGIC_EOF;
299 if (c == '\n')
300 return XDG_MIME_MAGIC_SECTION;
304 /* Headers are of the format:
305 * [ indent ] ">" start-offset "=" value
306 * [ "&" mask ] [ "~" word-size ] [ "+" range-length ] "\n"
308 static XdgMimeMagicState
309 _xdg_mime_magic_parse_magic_line (FILE *magic_file,
310 XdgMimeMagicMatch *match)
312 XdgMimeMagicMatchlet *matchlet;
313 int c;
314 int end_of_file;
315 int indent = 0;
316 int bytes_read;
318 assert (magic_file != NULL);
320 /* Sniff the buffer to make sure it's a valid line */
321 c = getc_unlocked (magic_file);
322 if (c == EOF)
323 return XDG_MIME_MAGIC_EOF;
324 else if (c == '[')
326 ungetc (c, magic_file);
327 return XDG_MIME_MAGIC_SECTION;
329 else if (c == '\n')
330 return XDG_MIME_MAGIC_MAGIC;
332 /* At this point, it must be a digit or a '>' */
333 end_of_file = FALSE;
334 if (isdigit (c))
336 ungetc (c, magic_file);
337 indent = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
338 if (end_of_file)
339 return XDG_MIME_MAGIC_EOF;
340 if (indent == -1)
341 return XDG_MIME_MAGIC_ERROR;
342 c = getc_unlocked (magic_file);
343 if (c == EOF)
344 return XDG_MIME_MAGIC_EOF;
347 if (c != '>')
348 return XDG_MIME_MAGIC_ERROR;
350 matchlet = _xdg_mime_magic_matchlet_new ();
351 matchlet->indent = indent;
352 matchlet->offset = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
353 if (end_of_file)
355 _xdg_mime_magic_matchlet_free (matchlet);
356 return XDG_MIME_MAGIC_EOF;
358 if (matchlet->offset == -1)
360 _xdg_mime_magic_matchlet_free (matchlet);
361 return XDG_MIME_MAGIC_ERROR;
363 c = getc_unlocked (magic_file);
364 if (c == EOF)
366 _xdg_mime_magic_matchlet_free (matchlet);
367 return XDG_MIME_MAGIC_EOF;
369 else if (c != '=')
371 _xdg_mime_magic_matchlet_free (matchlet);
372 return XDG_MIME_MAGIC_ERROR;
375 /* Next two bytes determine how long the value is */
376 matchlet->value_length = 0;
377 c = getc_unlocked (magic_file);
378 if (c == EOF)
380 _xdg_mime_magic_matchlet_free (matchlet);
381 return XDG_MIME_MAGIC_EOF;
383 matchlet->value_length = c & 0xFF;
384 matchlet->value_length = matchlet->value_length << 8;
386 c = getc_unlocked (magic_file);
387 if (c == EOF)
389 _xdg_mime_magic_matchlet_free (matchlet);
390 return XDG_MIME_MAGIC_EOF;
392 matchlet->value_length = matchlet->value_length + (c & 0xFF);
394 matchlet->value = malloc (matchlet->value_length);
396 /* OOM */
397 if (matchlet->value == NULL)
399 _xdg_mime_magic_matchlet_free (matchlet);
400 return XDG_MIME_MAGIC_ERROR;
402 bytes_read = fread (matchlet->value, 1, matchlet->value_length, magic_file);
403 if (bytes_read != matchlet->value_length)
405 _xdg_mime_magic_matchlet_free (matchlet);
406 if (feof (magic_file))
407 return XDG_MIME_MAGIC_EOF;
408 else
409 return XDG_MIME_MAGIC_ERROR;
412 c = getc_unlocked (magic_file);
413 if (c == '&')
415 matchlet->mask = malloc (matchlet->value_length);
416 /* OOM */
417 if (matchlet->mask == NULL)
419 _xdg_mime_magic_matchlet_free (matchlet);
420 return XDG_MIME_MAGIC_ERROR;
422 bytes_read = fread (matchlet->mask, 1, matchlet->value_length, magic_file);
423 if (bytes_read != matchlet->value_length)
425 _xdg_mime_magic_matchlet_free (matchlet);
426 if (feof (magic_file))
427 return XDG_MIME_MAGIC_EOF;
428 else
429 return XDG_MIME_MAGIC_ERROR;
431 c = getc_unlocked (magic_file);
434 if (c == '~')
436 matchlet->word_size = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
437 if (end_of_file)
439 _xdg_mime_magic_matchlet_free (matchlet);
440 return XDG_MIME_MAGIC_EOF;
442 if (matchlet->word_size != 0 &&
443 matchlet->word_size != 1 &&
444 matchlet->word_size != 2 &&
445 matchlet->word_size != 4)
447 _xdg_mime_magic_matchlet_free (matchlet);
448 return XDG_MIME_MAGIC_ERROR;
450 c = getc_unlocked (magic_file);
453 if (c == '+')
455 matchlet->range_length = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
456 if (end_of_file)
458 _xdg_mime_magic_matchlet_free (matchlet);
459 return XDG_MIME_MAGIC_EOF;
461 if (matchlet->range_length == -1)
463 _xdg_mime_magic_matchlet_free (matchlet);
464 return XDG_MIME_MAGIC_ERROR;
466 c = getc_unlocked (magic_file);
470 if (c == '\n')
472 /* We clean up the matchlet, byte swapping if needed */
473 if (matchlet->word_size > 1)
475 int i;
476 if (matchlet->value_length % matchlet->word_size != 0)
478 _xdg_mime_magic_matchlet_free (matchlet);
479 return XDG_MIME_MAGIC_ERROR;
481 /* FIXME: need to get this defined in a <config.h> style file */
482 #if LITTLE_ENDIAN
483 for (i = 0; i < matchlet->value_length; i = i + matchlet->word_size)
485 if (matchlet->word_size == 2)
486 *((xdg_uint16_t *) matchlet->value + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->value + i)));
487 else if (matchlet->word_size == 4)
488 *((xdg_uint32_t *) matchlet->value + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->value + i)));
489 if (matchlet->mask)
491 if (matchlet->word_size == 2)
492 *((xdg_uint16_t *) matchlet->mask + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->mask + i)));
493 else if (matchlet->word_size == 4)
494 *((xdg_uint32_t *) matchlet->mask + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->mask + i)));
498 #endif
501 matchlet->next = match->matchlet;
502 match->matchlet = matchlet;
505 return XDG_MIME_MAGIC_MAGIC;
508 _xdg_mime_magic_matchlet_free (matchlet);
509 if (c == EOF)
510 return XDG_MIME_MAGIC_EOF;
512 return XDG_MIME_MAGIC_ERROR;
515 static int
516 _xdg_mime_magic_matchlet_compare_to_data (XdgMimeMagicMatchlet *matchlet,
517 const void *data,
518 size_t len)
520 int i, j;
521 for (i = matchlet->offset; i < matchlet->offset + matchlet->range_length; i++)
523 int valid_matchlet = TRUE;
525 if (i + matchlet->value_length > len)
526 return FALSE;
528 if (matchlet->mask)
530 for (j = 0; j < matchlet->value_length; j++)
532 if ((matchlet->value[j] & matchlet->mask[j]) !=
533 ((((unsigned char *) data)[j + i]) & matchlet->mask[j]))
535 valid_matchlet = FALSE;
536 break;
540 else
542 for (j = 0; j < matchlet->value_length; j++)
544 if (matchlet->value[j] != ((unsigned char *) data)[j + i])
546 valid_matchlet = FALSE;
547 break;
551 if (valid_matchlet)
552 return TRUE;
554 return FALSE;
557 static int
558 _xdg_mime_magic_matchlet_compare_level (XdgMimeMagicMatchlet *matchlet,
559 const void *data,
560 size_t len,
561 int indent)
563 while ((matchlet != NULL) && (matchlet->indent == indent))
565 if (_xdg_mime_magic_matchlet_compare_to_data (matchlet, data, len))
567 if ((matchlet->next == NULL) ||
568 (matchlet->next->indent <= indent))
569 return TRUE;
571 if (_xdg_mime_magic_matchlet_compare_level (matchlet->next,
572 data,
573 len,
574 indent + 1))
575 return TRUE;
580 matchlet = matchlet->next;
582 while (matchlet && matchlet->indent > indent);
585 return FALSE;
588 static int
589 _xdg_mime_magic_match_compare_to_data (XdgMimeMagicMatch *match,
590 const void *data,
591 size_t len)
593 return _xdg_mime_magic_matchlet_compare_level (match->matchlet, data, len, 0);
596 static void
597 _xdg_mime_magic_insert_match (XdgMimeMagic *mime_magic,
598 XdgMimeMagicMatch *match)
600 XdgMimeMagicMatch *list;
602 if (mime_magic->match_list == NULL)
604 mime_magic->match_list = match;
605 return;
608 if (match->priority > mime_magic->match_list->priority)
610 match->next = mime_magic->match_list;
611 mime_magic->match_list = match;
612 return;
615 list = mime_magic->match_list;
616 while (list->next != NULL)
618 if (list->next->priority < match->priority)
620 match->next = list->next;
621 list->next = match;
622 return;
624 list = list->next;
626 list->next = match;
627 match->next = NULL;
630 XdgMimeMagic *
631 _xdg_mime_magic_new (void)
633 return calloc (1, sizeof (XdgMimeMagic));
636 void
637 _xdg_mime_magic_free (XdgMimeMagic *mime_magic)
639 if (mime_magic) {
640 _xdg_mime_magic_match_free (mime_magic->match_list);
641 free (mime_magic);
646 _xdg_mime_magic_get_buffer_extents (XdgMimeMagic *mime_magic)
648 return mime_magic->max_extent;
651 const char *
652 _xdg_mime_magic_lookup_data (XdgMimeMagic *mime_magic,
653 const void *data,
654 size_t len,
655 const char *mime_types[],
656 int n_mime_types)
658 XdgMimeMagicMatch *match;
659 const char *mime_type;
660 int n;
662 mime_type = NULL;
663 for (match = mime_magic->match_list; match; match = match->next)
665 if (_xdg_mime_magic_match_compare_to_data (match, data, len))
667 if ((mime_type == NULL) || (_xdg_mime_mime_type_subclass (match->mime_type, mime_type))) {
668 mime_type = match->mime_type;
671 else
673 for (n = 0; n < n_mime_types; n++)
675 if (mime_types[n] &&
676 _xdg_mime_mime_type_equal (mime_types[n], match->mime_type))
677 mime_types[n] = NULL;
682 if (mime_type == NULL)
684 for (n = 0; n < n_mime_types; n++)
686 if (mime_types[n])
687 mime_type = mime_types[n];
691 return mime_type;
694 static void
695 _xdg_mime_update_mime_magic_extents (XdgMimeMagic *mime_magic)
697 XdgMimeMagicMatch *match;
698 int max_extent = 0;
700 for (match = mime_magic->match_list; match; match = match->next)
702 XdgMimeMagicMatchlet *matchlet;
704 for (matchlet = match->matchlet; matchlet; matchlet = matchlet->next)
706 int extent;
708 extent = matchlet->value_length + matchlet->offset + matchlet->range_length;
709 if (max_extent < extent)
710 max_extent = extent;
714 mime_magic->max_extent = max_extent;
717 static XdgMimeMagicMatchlet *
718 _xdg_mime_magic_matchlet_mirror (XdgMimeMagicMatchlet *matchlets)
720 XdgMimeMagicMatchlet *new_list;
721 XdgMimeMagicMatchlet *tmp;
723 if ((matchlets == NULL) || (matchlets->next == NULL))
724 return matchlets;
726 new_list = NULL;
727 tmp = matchlets;
728 while (tmp != NULL)
730 XdgMimeMagicMatchlet *matchlet;
732 matchlet = tmp;
733 tmp = tmp->next;
734 matchlet->next = new_list;
735 new_list = matchlet;
738 return new_list;
742 static void
743 _xdg_mime_magic_read_magic_file (XdgMimeMagic *mime_magic,
744 FILE *magic_file)
746 XdgMimeMagicState state;
747 XdgMimeMagicMatch *match = NULL; /* Quiet compiler */
749 state = XDG_MIME_MAGIC_SECTION;
751 while (state != XDG_MIME_MAGIC_EOF)
753 switch (state)
755 case XDG_MIME_MAGIC_SECTION:
756 match = _xdg_mime_magic_match_new ();
757 state = _xdg_mime_magic_parse_header (magic_file, match);
758 if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
759 _xdg_mime_magic_match_free (match);
760 break;
761 case XDG_MIME_MAGIC_MAGIC:
762 state = _xdg_mime_magic_parse_magic_line (magic_file, match);
763 if (state == XDG_MIME_MAGIC_SECTION ||
764 (state == XDG_MIME_MAGIC_EOF && match->mime_type))
766 match->matchlet = _xdg_mime_magic_matchlet_mirror (match->matchlet);
767 _xdg_mime_magic_insert_match (mime_magic, match);
769 else 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_ERROR:
773 state = _xdg_mime_magic_parse_error (magic_file);
774 break;
775 case XDG_MIME_MAGIC_EOF:
776 default:
777 /* Make the compiler happy */
778 assert (0);
781 _xdg_mime_update_mime_magic_extents (mime_magic);
784 void
785 _xdg_mime_magic_read_from_file (XdgMimeMagic *mime_magic,
786 const char *file_name)
788 FILE *magic_file;
789 char header[12];
791 magic_file = fopen (file_name, "r");
793 if (magic_file == NULL)
794 return;
796 if (fread (header, 1, 12, magic_file) == 12)
798 if (memcmp ("MIME-Magic\0\n", header, 12) == 0)
799 _xdg_mime_magic_read_magic_file (mime_magic, magic_file);
802 fclose (magic_file);