QueryResponses.cs, DumpIndex.cs, IQueryResult.cs, QueryExecutor.cs, QueryResult.cs...
[beagle.git] / chooser-fu / xdgmimemagic.c
blob1b1d3bfc7c529673bce06f5d8a6955303e9d9806
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 CONFIG_H
29 #include <config.h>
30 #endif
31 #include <assert.h>
32 #include "xdgmimemagic.h"
33 #include "xdgmimeint.h"
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <limits.h>
41 #ifndef FALSE
42 #define FALSE (0)
43 #endif
45 #ifndef TRUE
46 #define TRUE (!FALSE)
47 #endif
49 extern int errno;
51 typedef struct XdgMimeMagicMatch XdgMimeMagicMatch;
52 typedef struct XdgMimeMagicMatchlet XdgMimeMagicMatchlet;
54 typedef enum
56 XDG_MIME_MAGIC_SECTION,
57 XDG_MIME_MAGIC_MAGIC,
58 XDG_MIME_MAGIC_ERROR,
59 XDG_MIME_MAGIC_EOF
60 } XdgMimeMagicState;
62 struct XdgMimeMagicMatch
64 const char *mime_type;
65 int priority;
66 XdgMimeMagicMatchlet *matchlet;
67 XdgMimeMagicMatch *next;
71 struct XdgMimeMagicMatchlet
73 int indent;
74 int offset;
75 unsigned int value_length;
76 unsigned char *value;
77 unsigned char *mask;
78 unsigned int range_length;
79 unsigned int word_size;
80 XdgMimeMagicMatchlet *next;
84 struct XdgMimeMagic
86 XdgMimeMagicMatch *match_list;
87 int max_extent;
90 static XdgMimeMagicMatch *
91 _xdg_mime_magic_match_new (void)
93 return calloc (1, sizeof (XdgMimeMagicMatch));
97 static XdgMimeMagicMatchlet *
98 _xdg_mime_magic_matchlet_new (void)
100 XdgMimeMagicMatchlet *matchlet;
102 matchlet = malloc (sizeof (XdgMimeMagicMatchlet));
104 matchlet->indent = 0;
105 matchlet->offset = 0;
106 matchlet->value_length = 0;
107 matchlet->value = NULL;
108 matchlet->mask = NULL;
109 matchlet->range_length = 1;
110 matchlet->word_size = 1;
111 matchlet->next = NULL;
113 return matchlet;
117 static void
118 _xdg_mime_magic_matchlet_free (XdgMimeMagicMatchlet *mime_magic_matchlet)
120 if (mime_magic_matchlet)
122 if (mime_magic_matchlet->next)
123 _xdg_mime_magic_matchlet_free (mime_magic_matchlet->next);
124 if (mime_magic_matchlet->value)
125 free (mime_magic_matchlet->value);
126 if (mime_magic_matchlet->mask)
127 free (mime_magic_matchlet->mask);
128 free (mime_magic_matchlet);
133 /* Frees mime_magic_match and the remainder of its list
135 static void
136 _xdg_mime_magic_match_free (XdgMimeMagicMatch *mime_magic_match)
138 XdgMimeMagicMatch *ptr, *next;
140 ptr = mime_magic_match;
141 while (ptr)
143 next = ptr->next;
145 if (ptr->mime_type)
146 free ((void *) ptr->mime_type);
147 if (ptr->matchlet)
148 _xdg_mime_magic_matchlet_free (ptr->matchlet);
149 free (ptr);
151 ptr = next;
155 /* Reads in a hunk of data until a newline character or a '\000' is hit. The
156 * returned string is null terminated, and doesn't include the newline.
158 static unsigned char *
159 _xdg_mime_magic_read_to_newline (FILE *magic_file,
160 int *end_of_file)
162 unsigned char *retval;
163 int c;
164 int len, pos;
166 len = 128;
167 pos = 0;
168 retval = malloc (len);
169 *end_of_file = FALSE;
171 while (TRUE)
173 c = getc_unlocked (magic_file);
174 if (c == EOF)
176 *end_of_file = TRUE;
177 break;
179 if (c == '\n' || c == '\000')
180 break;
181 retval[pos++] = (unsigned char) c;
182 if (pos % 128 == 127)
184 len = len + 128;
185 retval = realloc (retval, len);
189 retval[pos] = '\000';
190 return retval;
193 /* Returns the number read from the file, or -1 if no number could be read.
195 static int
196 _xdg_mime_magic_read_a_number (FILE *magic_file,
197 int *end_of_file)
199 /* LONG_MAX is about 20 characters on my system */
200 #define MAX_NUMBER_SIZE 30
201 char number_string[MAX_NUMBER_SIZE + 1];
202 int pos = 0;
203 int c;
204 long retval = -1;
206 while (TRUE)
208 c = getc_unlocked (magic_file);
210 if (c == EOF)
212 *end_of_file = TRUE;
213 break;
215 if (! isdigit (c))
217 ungetc (c, magic_file);
218 break;
220 number_string[pos] = (char) c;
221 pos++;
222 if (pos == MAX_NUMBER_SIZE)
223 break;
225 if (pos > 0)
227 number_string[pos] = '\000';
228 errno = 0;
229 retval = strtol (number_string, NULL, 10);
231 if ((retval < INT_MIN) || (retval > INT_MAX) || (errno != 0))
232 return -1;
235 return retval;
238 /* Headers are of the format:
239 * [<priority>:<mime-type>]
241 static XdgMimeMagicState
242 _xdg_mime_magic_parse_header (FILE *magic_file, XdgMimeMagicMatch *match)
244 int c;
245 char *buffer;
246 char *end_ptr;
247 int end_of_file = 0;
249 assert (magic_file != NULL);
250 assert (match != NULL);
252 c = getc_unlocked (magic_file);
253 if (c == EOF)
254 return XDG_MIME_MAGIC_EOF;
255 if (c != '[')
256 return XDG_MIME_MAGIC_ERROR;
258 match->priority = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
259 if (end_of_file)
260 return XDG_MIME_MAGIC_EOF;
261 if (match->priority == -1)
262 return XDG_MIME_MAGIC_ERROR;
264 c = getc_unlocked (magic_file);
265 if (c == EOF)
266 return XDG_MIME_MAGIC_EOF;
267 if (c != ':')
268 return XDG_MIME_MAGIC_ERROR;
270 buffer = _xdg_mime_magic_read_to_newline (magic_file, &end_of_file);
271 if (end_of_file)
272 return XDG_MIME_MAGIC_EOF;
274 end_ptr = buffer;
275 while (*end_ptr != ']' && *end_ptr != '\000' && *end_ptr != '\n')
276 end_ptr++;
277 if (*end_ptr != ']')
279 free (buffer);
280 return XDG_MIME_MAGIC_ERROR;
282 *end_ptr = '\000';
284 match->mime_type = strdup (buffer);
285 free (buffer);
287 return XDG_MIME_MAGIC_MAGIC;
290 static XdgMimeMagicState
291 _xdg_mime_magic_parse_error (FILE *magic_file)
293 int c;
295 while (1)
297 c = getc_unlocked (magic_file);
298 if (c == EOF)
299 return XDG_MIME_MAGIC_EOF;
300 if (c == '\n')
301 return XDG_MIME_MAGIC_SECTION;
305 /* Headers are of the format:
306 * [ indent ] ">" start-offset "=" value
307 * [ "&" mask ] [ "~" word-size ] [ "+" range-length ] "\n"
309 static XdgMimeMagicState
310 _xdg_mime_magic_parse_magic_line (FILE *magic_file,
311 XdgMimeMagicMatch *match)
313 XdgMimeMagicMatchlet *matchlet;
314 int c;
315 int end_of_file;
316 int indent = 0;
317 int bytes_read;
319 assert (magic_file != NULL);
321 /* Sniff the buffer to make sure it's a valid line */
322 c = getc_unlocked (magic_file);
323 if (c == EOF)
324 return XDG_MIME_MAGIC_EOF;
325 else if (c == '[')
327 ungetc (c, magic_file);
328 return XDG_MIME_MAGIC_SECTION;
330 else if (c == '\n')
331 return XDG_MIME_MAGIC_MAGIC;
333 /* At this point, it must be a digit or a '>' */
334 end_of_file = FALSE;
335 if (isdigit (c))
337 ungetc (c, magic_file);
338 indent = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
339 if (end_of_file)
340 return XDG_MIME_MAGIC_EOF;
341 if (indent == -1)
342 return XDG_MIME_MAGIC_ERROR;
343 c = getc_unlocked (magic_file);
344 if (c == EOF)
345 return XDG_MIME_MAGIC_EOF;
348 if (c != '>')
349 return XDG_MIME_MAGIC_ERROR;
351 matchlet = _xdg_mime_magic_matchlet_new ();
352 matchlet->indent = indent;
353 matchlet->offset = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
354 if (end_of_file)
356 _xdg_mime_magic_matchlet_free (matchlet);
357 return XDG_MIME_MAGIC_EOF;
359 if (matchlet->offset == -1)
361 _xdg_mime_magic_matchlet_free (matchlet);
362 return XDG_MIME_MAGIC_ERROR;
364 c = getc_unlocked (magic_file);
365 if (c == EOF)
367 _xdg_mime_magic_matchlet_free (matchlet);
368 return XDG_MIME_MAGIC_EOF;
370 else if (c != '=')
372 _xdg_mime_magic_matchlet_free (matchlet);
373 return XDG_MIME_MAGIC_ERROR;
376 /* Next two bytes determine how long the value is */
377 matchlet->value_length = 0;
378 c = getc_unlocked (magic_file);
379 if (c == EOF)
381 _xdg_mime_magic_matchlet_free (matchlet);
382 return XDG_MIME_MAGIC_EOF;
384 matchlet->value_length = c & 0xFF;
385 matchlet->value_length = matchlet->value_length << 8;
387 c = getc_unlocked (magic_file);
388 if (c == EOF)
390 _xdg_mime_magic_matchlet_free (matchlet);
391 return XDG_MIME_MAGIC_EOF;
393 matchlet->value_length = matchlet->value_length + (c & 0xFF);
395 matchlet->value = malloc (matchlet->value_length);
397 /* OOM */
398 if (matchlet->value == NULL)
400 _xdg_mime_magic_matchlet_free (matchlet);
401 return XDG_MIME_MAGIC_ERROR;
403 bytes_read = fread (matchlet->value, 1, matchlet->value_length, magic_file);
404 if (bytes_read != matchlet->value_length)
406 _xdg_mime_magic_matchlet_free (matchlet);
407 if (feof (magic_file))
408 return XDG_MIME_MAGIC_EOF;
409 else
410 return XDG_MIME_MAGIC_ERROR;
413 c = getc_unlocked (magic_file);
414 if (c == '&')
416 matchlet->mask = malloc (matchlet->value_length);
417 /* OOM */
418 if (matchlet->mask == NULL)
420 _xdg_mime_magic_matchlet_free (matchlet);
421 return XDG_MIME_MAGIC_ERROR;
423 bytes_read = fread (matchlet->mask, 1, matchlet->value_length, magic_file);
424 if (bytes_read != matchlet->value_length)
426 _xdg_mime_magic_matchlet_free (matchlet);
427 if (feof (magic_file))
428 return XDG_MIME_MAGIC_EOF;
429 else
430 return XDG_MIME_MAGIC_ERROR;
432 c = getc_unlocked (magic_file);
435 if (c == '~')
437 matchlet->word_size = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
438 if (end_of_file)
440 _xdg_mime_magic_matchlet_free (matchlet);
441 return XDG_MIME_MAGIC_EOF;
443 if (matchlet->word_size != 0 &&
444 matchlet->word_size != 1 &&
445 matchlet->word_size != 2 &&
446 matchlet->word_size != 4)
448 _xdg_mime_magic_matchlet_free (matchlet);
449 return XDG_MIME_MAGIC_ERROR;
451 c = getc_unlocked (magic_file);
454 if (c == '+')
456 matchlet->range_length = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
457 if (end_of_file)
459 _xdg_mime_magic_matchlet_free (matchlet);
460 return XDG_MIME_MAGIC_EOF;
462 if (matchlet->range_length == -1)
464 _xdg_mime_magic_matchlet_free (matchlet);
465 return XDG_MIME_MAGIC_ERROR;
467 c = getc_unlocked (magic_file);
471 if (c == '\n')
473 /* We clean up the matchlet, byte swapping if needed */
474 if (matchlet->word_size > 1)
476 int i;
477 if (matchlet->value_length % matchlet->word_size != 0)
479 _xdg_mime_magic_matchlet_free (matchlet);
480 return XDG_MIME_MAGIC_ERROR;
482 /* FIXME: need to get this defined in a <config.h> style file */
483 #if LITTLE_ENDIAN
484 for (i = 0; i < matchlet->value_length; i = i + matchlet->word_size)
486 if (matchlet->word_size == 2)
487 *((xdg_uint16_t *) matchlet->value + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->value + i)));
488 else if (matchlet->word_size == 4)
489 *((xdg_uint32_t *) matchlet->value + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->value + i)));
490 if (matchlet->mask)
492 if (matchlet->word_size == 2)
493 *((xdg_uint16_t *) matchlet->mask + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->mask + i)));
494 else if (matchlet->word_size == 4)
495 *((xdg_uint32_t *) matchlet->mask + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->mask + i)));
499 #endif
502 matchlet->next = match->matchlet;
503 match->matchlet = matchlet;
506 return XDG_MIME_MAGIC_MAGIC;
509 _xdg_mime_magic_matchlet_free (matchlet);
510 if (c == EOF)
511 return XDG_MIME_MAGIC_EOF;
513 return XDG_MIME_MAGIC_ERROR;
516 static int
517 _xdg_mime_magic_matchlet_compare_to_data (XdgMimeMagicMatchlet *matchlet,
518 const void *data,
519 size_t len)
521 int i, j;
523 for (i = matchlet->offset; i <= matchlet->offset + matchlet->range_length; i++)
525 int valid_matchlet = TRUE;
527 if (i + matchlet->value_length > len)
528 return FALSE;
530 if (matchlet->mask)
532 for (j = 0; j < matchlet->value_length; j++)
534 if ((matchlet->value[j] & matchlet->mask[j]) !=
535 ((((unsigned char *) data)[j + i]) & matchlet->mask[j]))
537 valid_matchlet = FALSE;
538 break;
542 else
544 for (j = 0; j < matchlet->value_length; j++)
546 if (matchlet->value[j] != ((unsigned char *) data)[j + i])
548 valid_matchlet = FALSE;
549 break;
553 if (valid_matchlet)
554 return TRUE;
556 return FALSE;
559 static int
560 _xdg_mime_magic_matchlet_compare_level (XdgMimeMagicMatchlet *matchlet,
561 const void *data,
562 size_t len,
563 int indent)
565 while ((matchlet != NULL) && (matchlet->indent == indent))
567 if (_xdg_mime_magic_matchlet_compare_to_data (matchlet, data, len))
569 if ((matchlet->next == NULL) ||
570 (matchlet->next->indent <= indent))
571 return TRUE;
573 if (_xdg_mime_magic_matchlet_compare_level (matchlet->next,
574 data,
575 len,
576 indent + 1))
577 return TRUE;
582 matchlet = matchlet->next;
584 while (matchlet && matchlet->indent > indent);
587 return FALSE;
590 static int
591 _xdg_mime_magic_match_compare_to_data (XdgMimeMagicMatch *match,
592 const void *data,
593 size_t len)
595 return _xdg_mime_magic_matchlet_compare_level (match->matchlet, data, len, 0);
598 static void
599 _xdg_mime_magic_insert_match (XdgMimeMagic *mime_magic,
600 XdgMimeMagicMatch *match)
602 XdgMimeMagicMatch *list;
604 if (mime_magic->match_list == NULL)
606 mime_magic->match_list = match;
607 return;
610 if (match->priority > mime_magic->match_list->priority)
612 match->next = mime_magic->match_list;
613 mime_magic->match_list = match;
614 return;
617 list = mime_magic->match_list;
618 while (list->next != NULL)
620 if (list->next->priority < match->priority)
622 match->next = list->next;
623 list->next = match;
624 return;
626 list = list->next;
628 list->next = match;
629 match->next = NULL;
632 XdgMimeMagic *
633 _xdg_mime_magic_new (void)
635 return calloc (1, sizeof (XdgMimeMagic));
638 void
639 _xdg_mime_magic_free (XdgMimeMagic *mime_magic)
641 if (mime_magic)
642 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)
656 XdgMimeMagicMatch *match;
658 for (match = mime_magic->match_list; match; match = match->next)
660 if (_xdg_mime_magic_match_compare_to_data (match, data, len))
662 return match->mime_type;
666 return NULL;
669 static void
670 _xdg_mime_update_mime_magic_extents (XdgMimeMagic *mime_magic)
672 XdgMimeMagicMatch *match;
673 int max_extent = 0;
675 for (match = mime_magic->match_list; match; match = match->next)
677 XdgMimeMagicMatchlet *matchlet;
679 for (matchlet = match->matchlet; matchlet; matchlet = matchlet->next)
681 int extent;
683 extent = matchlet->value_length + matchlet->offset + matchlet->range_length;
684 if (max_extent < extent)
685 max_extent = extent;
689 mime_magic->max_extent = max_extent;
692 static XdgMimeMagicMatchlet *
693 _xdg_mime_magic_matchlet_mirror (XdgMimeMagicMatchlet *matchlets)
695 XdgMimeMagicMatchlet *new_list;
696 XdgMimeMagicMatchlet *tmp;
698 if ((matchlets == NULL) || (matchlets->next == NULL))
699 return matchlets;
701 new_list = NULL;
702 tmp = matchlets;
703 while (tmp != NULL)
705 XdgMimeMagicMatchlet *matchlet;
707 matchlet = tmp;
708 tmp = tmp->next;
709 matchlet->next = new_list;
710 new_list = matchlet;
713 return new_list;
717 static void
718 _xdg_mime_magic_read_magic_file (XdgMimeMagic *mime_magic,
719 FILE *magic_file)
721 XdgMimeMagicState state;
722 XdgMimeMagicMatch *match = NULL; /* Quiet compiler */
724 state = XDG_MIME_MAGIC_SECTION;
726 while (state != XDG_MIME_MAGIC_EOF)
728 switch (state)
730 case XDG_MIME_MAGIC_SECTION:
731 match = _xdg_mime_magic_match_new ();
732 state = _xdg_mime_magic_parse_header (magic_file, match);
733 if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
734 _xdg_mime_magic_match_free (match);
735 break;
736 case XDG_MIME_MAGIC_MAGIC:
737 state = _xdg_mime_magic_parse_magic_line (magic_file, match);
738 if (state == XDG_MIME_MAGIC_SECTION ||
739 (state == XDG_MIME_MAGIC_EOF && match->mime_type))
741 match->matchlet = _xdg_mime_magic_matchlet_mirror (match->matchlet);
742 _xdg_mime_magic_insert_match (mime_magic, match);
744 else if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
745 _xdg_mime_magic_match_free (match);
746 break;
747 case XDG_MIME_MAGIC_ERROR:
748 state = _xdg_mime_magic_parse_error (magic_file);
749 break;
750 case XDG_MIME_MAGIC_EOF:
751 default:
752 /* Make the compiler happy */
753 assert (0);
756 _xdg_mime_update_mime_magic_extents (mime_magic);
759 void
760 _xdg_mime_magic_read_from_file (XdgMimeMagic *mime_magic,
761 const char *file_name)
763 FILE *magic_file;
764 char header[12];
766 magic_file = fopen (file_name, "r");
768 if (magic_file == NULL)
769 return;
771 fread (header, 1, 12, magic_file);
773 if (memcmp ("MIME-Magic\0\n", header, 12) == 0)
774 _xdg_mime_magic_read_magic_file (mime_magic, magic_file);
775 fclose (magic_file);