Italian translation update.
[rox-filer.git] / ROX-Filer / src / xdgmimemagic.c
blob892688bbfcf679571844475658d397dd860ef9a5
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 #if !defined getc_unlocked && !defined HAVE_GETC_UNLOCKED
51 #define getc_unlocked(fp) getc (fp)
52 #endif
54 typedef struct XdgMimeMagicMatch XdgMimeMagicMatch;
55 typedef struct XdgMimeMagicMatchlet XdgMimeMagicMatchlet;
57 typedef enum
59 XDG_MIME_MAGIC_SECTION,
60 XDG_MIME_MAGIC_MAGIC,
61 XDG_MIME_MAGIC_ERROR,
62 XDG_MIME_MAGIC_EOF
63 } XdgMimeMagicState;
65 struct XdgMimeMagicMatch
67 const char *mime_type;
68 int priority;
69 XdgMimeMagicMatchlet *matchlet;
70 XdgMimeMagicMatch *next;
74 struct XdgMimeMagicMatchlet
76 int indent;
77 int offset;
78 unsigned int value_length;
79 unsigned char *value;
80 unsigned char *mask;
81 unsigned int range_length;
82 unsigned int word_size;
83 XdgMimeMagicMatchlet *next;
87 struct XdgMimeMagic
89 XdgMimeMagicMatch *match_list;
90 int max_extent;
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;
116 return matchlet;
120 static void
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
138 static void
139 _xdg_mime_magic_match_free (XdgMimeMagicMatch *mime_magic_match)
141 XdgMimeMagicMatch *ptr, *next;
143 ptr = mime_magic_match;
144 while (ptr)
146 next = ptr->next;
148 if (ptr->mime_type)
149 free ((void *) ptr->mime_type);
150 if (ptr->matchlet)
151 _xdg_mime_magic_matchlet_free (ptr->matchlet);
152 free (ptr);
154 ptr = next;
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,
163 int *end_of_file)
165 unsigned char *retval;
166 int c;
167 int len, pos;
169 len = 128;
170 pos = 0;
171 retval = malloc (len);
172 *end_of_file = FALSE;
174 while (TRUE)
176 c = getc_unlocked (magic_file);
177 if (c == EOF)
179 *end_of_file = TRUE;
180 break;
182 if (c == '\n' || c == '\000')
183 break;
184 retval[pos++] = (unsigned char) c;
185 if (pos % 128 == 127)
187 len = len + 128;
188 retval = realloc (retval, len);
192 retval[pos] = '\000';
193 return retval;
196 /* Returns the number read from the file, or -1 if no number could be read.
198 static int
199 _xdg_mime_magic_read_a_number (FILE *magic_file,
200 int *end_of_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];
205 int pos = 0;
206 int c;
207 long retval = -1;
209 while (TRUE)
211 c = getc_unlocked (magic_file);
213 if (c == EOF)
215 *end_of_file = TRUE;
216 break;
218 if (! isdigit (c))
220 ungetc (c, magic_file);
221 break;
223 number_string[pos] = (char) c;
224 pos++;
225 if (pos == MAX_NUMBER_SIZE)
226 break;
228 if (pos > 0)
230 number_string[pos] = '\000';
231 errno = 0;
232 retval = strtol (number_string, NULL, 10);
234 if ((retval < INT_MIN) || (retval > INT_MAX) || (errno != 0))
235 return -1;
238 return retval;
241 /* Headers are of the format:
242 * [<priority>:<mime-type>]
244 static XdgMimeMagicState
245 _xdg_mime_magic_parse_header (FILE *magic_file, XdgMimeMagicMatch *match)
247 int c;
248 char *buffer;
249 char *end_ptr;
250 int end_of_file = 0;
252 assert (magic_file != NULL);
253 assert (match != NULL);
255 c = getc_unlocked (magic_file);
256 if (c == EOF)
257 return XDG_MIME_MAGIC_EOF;
258 if (c != '[')
259 return XDG_MIME_MAGIC_ERROR;
261 match->priority = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
262 if (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);
268 if (c == EOF)
269 return XDG_MIME_MAGIC_EOF;
270 if (c != ':')
271 return XDG_MIME_MAGIC_ERROR;
273 buffer = (char *)_xdg_mime_magic_read_to_newline (magic_file, &end_of_file);
274 if (end_of_file)
275 return XDG_MIME_MAGIC_EOF;
277 end_ptr = buffer;
278 while (*end_ptr != ']' && *end_ptr != '\000' && *end_ptr != '\n')
279 end_ptr++;
280 if (*end_ptr != ']')
282 free (buffer);
283 return XDG_MIME_MAGIC_ERROR;
285 *end_ptr = '\000';
287 match->mime_type = strdup (buffer);
288 free (buffer);
290 return XDG_MIME_MAGIC_MAGIC;
293 static XdgMimeMagicState
294 _xdg_mime_magic_parse_error (FILE *magic_file)
296 int c;
298 while (1)
300 c = getc_unlocked (magic_file);
301 if (c == EOF)
302 return XDG_MIME_MAGIC_EOF;
303 if (c == '\n')
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;
317 int c;
318 int end_of_file;
319 int indent = 0;
320 int bytes_read;
322 assert (magic_file != NULL);
324 /* Sniff the buffer to make sure it's a valid line */
325 c = getc_unlocked (magic_file);
326 if (c == EOF)
327 return XDG_MIME_MAGIC_EOF;
328 else if (c == '[')
330 ungetc (c, magic_file);
331 return XDG_MIME_MAGIC_SECTION;
333 else if (c == '\n')
334 return XDG_MIME_MAGIC_MAGIC;
336 /* At this point, it must be a digit or a '>' */
337 end_of_file = FALSE;
338 if (isdigit (c))
340 ungetc (c, magic_file);
341 indent = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
342 if (end_of_file)
343 return XDG_MIME_MAGIC_EOF;
344 if (indent == -1)
345 return XDG_MIME_MAGIC_ERROR;
346 c = getc_unlocked (magic_file);
347 if (c == EOF)
348 return XDG_MIME_MAGIC_EOF;
351 if (c != '>')
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);
357 if (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);
368 if (c == EOF)
370 _xdg_mime_magic_matchlet_free (matchlet);
371 return XDG_MIME_MAGIC_EOF;
373 else if (c != '=')
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);
382 if (c == EOF)
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);
391 if (c == EOF)
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);
400 /* OOM */
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;
412 else
413 return XDG_MIME_MAGIC_ERROR;
416 c = getc_unlocked (magic_file);
417 if (c == '&')
419 matchlet->mask = malloc (matchlet->value_length);
420 /* OOM */
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;
432 else
433 return XDG_MIME_MAGIC_ERROR;
435 c = getc_unlocked (magic_file);
438 if (c == '~')
440 matchlet->word_size = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
441 if (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);
457 if (c == '+')
459 matchlet->range_length = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
460 if (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);
474 if (c == '\n')
476 /* We clean up the matchlet, byte swapping if needed */
477 if (matchlet->word_size > 1)
479 #if LITTLE_ENDIAN
480 int i;
481 #endif
482 if (matchlet->value_length % matchlet->word_size != 0)
484 _xdg_mime_magic_matchlet_free (matchlet);
485 return XDG_MIME_MAGIC_ERROR;
487 /* FIXME: need to get this defined in a <config.h> style file */
488 #if LITTLE_ENDIAN
489 for (i = 0; i < matchlet->value_length; i = i + matchlet->word_size)
491 if (matchlet->word_size == 2)
492 *((xdg_uint16_t *) matchlet->value + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->value + i)));
493 else if (matchlet->word_size == 4)
494 *((xdg_uint32_t *) matchlet->value + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->value + i)));
495 if (matchlet->mask)
497 if (matchlet->word_size == 2)
498 *((xdg_uint16_t *) matchlet->mask + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->mask + i)));
499 else if (matchlet->word_size == 4)
500 *((xdg_uint32_t *) matchlet->mask + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->mask + i)));
504 #endif
507 matchlet->next = match->matchlet;
508 match->matchlet = matchlet;
511 return XDG_MIME_MAGIC_MAGIC;
514 _xdg_mime_magic_matchlet_free (matchlet);
515 if (c == EOF)
516 return XDG_MIME_MAGIC_EOF;
518 return XDG_MIME_MAGIC_ERROR;
521 static int
522 _xdg_mime_magic_matchlet_compare_to_data (XdgMimeMagicMatchlet *matchlet,
523 const void *data,
524 size_t len)
526 int i, j;
527 for (i = matchlet->offset; i < matchlet->offset + matchlet->range_length; i++)
529 int valid_matchlet = TRUE;
531 if (i + matchlet->value_length > len)
532 return FALSE;
534 if (matchlet->mask)
536 for (j = 0; j < matchlet->value_length; j++)
538 if ((matchlet->value[j] & matchlet->mask[j]) !=
539 ((((unsigned char *) data)[j + i]) & matchlet->mask[j]))
541 valid_matchlet = FALSE;
542 break;
546 else
548 for (j = 0; j < matchlet->value_length; j++)
550 if (matchlet->value[j] != ((unsigned char *) data)[j + i])
552 valid_matchlet = FALSE;
553 break;
557 if (valid_matchlet)
558 return TRUE;
560 return FALSE;
563 static int
564 _xdg_mime_magic_matchlet_compare_level (XdgMimeMagicMatchlet *matchlet,
565 const void *data,
566 size_t len,
567 int indent)
569 while ((matchlet != NULL) && (matchlet->indent == indent))
571 if (_xdg_mime_magic_matchlet_compare_to_data (matchlet, data, len))
573 if ((matchlet->next == NULL) ||
574 (matchlet->next->indent <= indent))
575 return TRUE;
577 if (_xdg_mime_magic_matchlet_compare_level (matchlet->next,
578 data,
579 len,
580 indent + 1))
581 return TRUE;
586 matchlet = matchlet->next;
588 while (matchlet && matchlet->indent > indent);
591 return FALSE;
594 static int
595 _xdg_mime_magic_match_compare_to_data (XdgMimeMagicMatch *match,
596 const void *data,
597 size_t len)
599 return _xdg_mime_magic_matchlet_compare_level (match->matchlet, data, len, 0);
602 static void
603 _xdg_mime_magic_insert_match (XdgMimeMagic *mime_magic,
604 XdgMimeMagicMatch *match)
606 XdgMimeMagicMatch *list;
608 if (mime_magic->match_list == NULL)
610 mime_magic->match_list = match;
611 return;
614 if (match->priority > mime_magic->match_list->priority)
616 match->next = mime_magic->match_list;
617 mime_magic->match_list = match;
618 return;
621 list = mime_magic->match_list;
622 while (list->next != NULL)
624 if (list->next->priority < match->priority)
626 match->next = list->next;
627 list->next = match;
628 return;
630 list = list->next;
632 list->next = match;
633 match->next = NULL;
636 XdgMimeMagic *
637 _xdg_mime_magic_new (void)
639 return calloc (1, sizeof (XdgMimeMagic));
642 void
643 _xdg_mime_magic_free (XdgMimeMagic *mime_magic)
645 if (mime_magic) {
646 _xdg_mime_magic_match_free (mime_magic->match_list);
647 free (mime_magic);
652 _xdg_mime_magic_get_buffer_extents (XdgMimeMagic *mime_magic)
654 return mime_magic->max_extent;
657 const char *
658 _xdg_mime_magic_lookup_data (XdgMimeMagic *mime_magic,
659 const void *data,
660 size_t len,
661 const char *mime_types[],
662 int n_mime_types)
664 XdgMimeMagicMatch *match;
665 const char *mime_type;
666 int n;
667 int priority;
668 int had_match;
670 mime_type = NULL;
671 priority = 0;
672 had_match = 0;
673 for (match = mime_magic->match_list; match; match = match->next)
675 if (_xdg_mime_magic_match_compare_to_data (match, data, len))
677 if (!had_match || match->priority > priority ||
678 (mime_type != NULL && _xdg_mime_mime_type_subclass (match->mime_type, mime_type)))
680 mime_type = match->mime_type;
681 priority = match->priority;
683 else if (had_match && match->priority == priority && mime_type &&
684 !_xdg_mime_mime_type_subclass (mime_type, match->mime_type))
685 /* multiple unrelated patterns with the same priority matched,
686 * so we can't tell what type this is. */
687 mime_type = NULL;
689 had_match = 1;
691 else
693 for (n = 0; n < n_mime_types; n++)
695 if (mime_types[n] &&
696 _xdg_mime_mime_type_equal (mime_types[n], match->mime_type))
697 mime_types[n] = NULL;
702 if (mime_type == NULL)
704 for (n = 0; n < n_mime_types; n++)
706 if (mime_types[n])
707 mime_type = mime_types[n];
711 return mime_type;
714 static void
715 _xdg_mime_update_mime_magic_extents (XdgMimeMagic *mime_magic)
717 XdgMimeMagicMatch *match;
718 int max_extent = 0;
720 for (match = mime_magic->match_list; match; match = match->next)
722 XdgMimeMagicMatchlet *matchlet;
724 for (matchlet = match->matchlet; matchlet; matchlet = matchlet->next)
726 int extent;
728 extent = matchlet->value_length + matchlet->offset + matchlet->range_length;
729 if (max_extent < extent)
730 max_extent = extent;
734 mime_magic->max_extent = max_extent;
737 static XdgMimeMagicMatchlet *
738 _xdg_mime_magic_matchlet_mirror (XdgMimeMagicMatchlet *matchlets)
740 XdgMimeMagicMatchlet *new_list;
741 XdgMimeMagicMatchlet *tmp;
743 if ((matchlets == NULL) || (matchlets->next == NULL))
744 return matchlets;
746 new_list = NULL;
747 tmp = matchlets;
748 while (tmp != NULL)
750 XdgMimeMagicMatchlet *matchlet;
752 matchlet = tmp;
753 tmp = tmp->next;
754 matchlet->next = new_list;
755 new_list = matchlet;
758 return new_list;
762 static void
763 _xdg_mime_magic_read_magic_file (XdgMimeMagic *mime_magic,
764 FILE *magic_file)
766 XdgMimeMagicState state;
767 XdgMimeMagicMatch *match = NULL; /* Quiet compiler */
769 state = XDG_MIME_MAGIC_SECTION;
771 while (state != XDG_MIME_MAGIC_EOF)
773 switch (state)
775 case XDG_MIME_MAGIC_SECTION:
776 match = _xdg_mime_magic_match_new ();
777 state = _xdg_mime_magic_parse_header (magic_file, match);
778 if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
779 _xdg_mime_magic_match_free (match);
780 break;
781 case XDG_MIME_MAGIC_MAGIC:
782 state = _xdg_mime_magic_parse_magic_line (magic_file, match);
783 if (state == XDG_MIME_MAGIC_SECTION ||
784 (state == XDG_MIME_MAGIC_EOF && match->mime_type))
786 match->matchlet = _xdg_mime_magic_matchlet_mirror (match->matchlet);
787 _xdg_mime_magic_insert_match (mime_magic, match);
789 else if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
790 _xdg_mime_magic_match_free (match);
791 break;
792 case XDG_MIME_MAGIC_ERROR:
793 state = _xdg_mime_magic_parse_error (magic_file);
794 break;
795 case XDG_MIME_MAGIC_EOF:
796 default:
797 /* Make the compiler happy */
798 assert (0);
801 _xdg_mime_update_mime_magic_extents (mime_magic);
804 void
805 _xdg_mime_magic_read_from_file (XdgMimeMagic *mime_magic,
806 const char *file_name)
808 FILE *magic_file;
809 char header[12];
811 magic_file = fopen (file_name, "r");
813 if (magic_file == NULL)
814 return;
816 if (fread (header, 1, 12, magic_file) == 12)
818 if (memcmp ("MIME-Magic\0\n", header, 12) == 0)
819 _xdg_mime_magic_read_magic_file (mime_magic, magic_file);
822 fclose (magic_file);