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/>.
29 #include "xdgmimemagic.h"
30 #include "xdgmimeint.h"
46 #if !defined getc_unlocked && !defined HAVE_GETC_UNLOCKED
47 # define getc_unlocked(fp) getc (fp)
50 typedef struct XdgMimeMagicMatch XdgMimeMagicMatch
;
51 typedef struct XdgMimeMagicMatchlet XdgMimeMagicMatchlet
;
55 XDG_MIME_MAGIC_SECTION
,
61 struct XdgMimeMagicMatch
63 const char *mime_type
;
65 XdgMimeMagicMatchlet
*matchlet
;
66 XdgMimeMagicMatch
*next
;
70 struct XdgMimeMagicMatchlet
74 unsigned int value_length
;
77 unsigned int range_length
;
78 unsigned int word_size
;
79 XdgMimeMagicMatchlet
*next
;
85 XdgMimeMagicMatch
*match_list
;
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
;
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
135 _xdg_mime_magic_match_free (XdgMimeMagicMatch
*mime_magic_match
)
137 XdgMimeMagicMatch
*ptr
, *next
;
139 ptr
= mime_magic_match
;
145 free ((void *) ptr
->mime_type
);
147 _xdg_mime_magic_matchlet_free (ptr
->matchlet
);
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
,
161 unsigned char *retval
;
167 retval
= malloc (len
);
168 *end_of_file
= FALSE
;
172 c
= getc_unlocked (magic_file
);
178 if (c
== '\n' || c
== '\000')
180 retval
[pos
++] = (unsigned char) c
;
181 if (pos
% 128 == 127)
184 retval
= realloc (retval
, len
);
188 retval
[pos
] = '\000';
192 /* Returns the number read from the file, or -1 if no number could be read.
195 _xdg_mime_magic_read_a_number (FILE *magic_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];
207 c
= getc_unlocked (magic_file
);
216 ungetc (c
, magic_file
);
219 number_string
[pos
] = (char) c
;
221 if (pos
== MAX_NUMBER_SIZE
)
226 number_string
[pos
] = '\000';
228 retval
= strtol (number_string
, NULL
, 10);
230 if ((retval
< INT_MIN
) || (retval
> INT_MAX
) || (errno
!= 0))
237 /* Headers are of the format:
238 * [<priority>:<mime-type>]
240 static XdgMimeMagicState
241 _xdg_mime_magic_parse_header (FILE *magic_file
, XdgMimeMagicMatch
*match
)
248 assert (magic_file
!= NULL
);
249 assert (match
!= NULL
);
251 c
= getc_unlocked (magic_file
);
253 return XDG_MIME_MAGIC_EOF
;
255 return XDG_MIME_MAGIC_ERROR
;
257 match
->priority
= _xdg_mime_magic_read_a_number (magic_file
, &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
);
265 return XDG_MIME_MAGIC_EOF
;
267 return XDG_MIME_MAGIC_ERROR
;
269 buffer
= (char *)_xdg_mime_magic_read_to_newline (magic_file
, &end_of_file
);
273 return XDG_MIME_MAGIC_EOF
;
277 while (*end_ptr
!= ']' && *end_ptr
!= '\000' && *end_ptr
!= '\n')
282 return XDG_MIME_MAGIC_ERROR
;
286 match
->mime_type
= strdup (buffer
);
289 return XDG_MIME_MAGIC_MAGIC
;
292 static XdgMimeMagicState
293 _xdg_mime_magic_parse_error (FILE *magic_file
)
299 c
= getc_unlocked (magic_file
);
301 return XDG_MIME_MAGIC_EOF
;
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
;
321 assert (magic_file
!= NULL
);
323 /* Sniff the buffer to make sure it's a valid line */
324 c
= getc_unlocked (magic_file
);
326 return XDG_MIME_MAGIC_EOF
;
329 ungetc (c
, magic_file
);
330 return XDG_MIME_MAGIC_SECTION
;
333 return XDG_MIME_MAGIC_MAGIC
;
335 /* At this point, it must be a digit or a '>' */
339 ungetc (c
, magic_file
);
340 indent
= _xdg_mime_magic_read_a_number (magic_file
, &end_of_file
);
342 return XDG_MIME_MAGIC_EOF
;
344 return XDG_MIME_MAGIC_ERROR
;
345 c
= getc_unlocked (magic_file
);
347 return XDG_MIME_MAGIC_EOF
;
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
);
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
);
369 _xdg_mime_magic_matchlet_free (matchlet
);
370 return XDG_MIME_MAGIC_EOF
;
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
);
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
);
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
);
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
;
412 return XDG_MIME_MAGIC_ERROR
;
415 c
= getc_unlocked (magic_file
);
418 matchlet
->mask
= malloc (matchlet
->value_length
);
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
;
432 return XDG_MIME_MAGIC_ERROR
;
434 c
= getc_unlocked (magic_file
);
439 matchlet
->word_size
= _xdg_mime_magic_read_a_number (magic_file
, &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
);
458 matchlet
->range_length
= _xdg_mime_magic_read_a_number (magic_file
, &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
);
475 /* We clean up the matchlet, byte swapping if needed */
476 if (matchlet
->word_size
> 1)
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 */
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
)));
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
)));
506 matchlet
->next
= match
->matchlet
;
507 match
->matchlet
= matchlet
;
510 return XDG_MIME_MAGIC_MAGIC
;
513 _xdg_mime_magic_matchlet_free (matchlet
);
515 return XDG_MIME_MAGIC_EOF
;
517 return XDG_MIME_MAGIC_ERROR
;
521 _xdg_mime_magic_matchlet_compare_to_data (XdgMimeMagicMatchlet
*matchlet
,
526 for (i
= matchlet
->offset
; i
< matchlet
->offset
+ matchlet
->range_length
; i
++)
528 int valid_matchlet
= TRUE
;
530 if (i
+ matchlet
->value_length
> len
)
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
;
547 for (j
= 0; j
< matchlet
->value_length
; j
++)
549 if (matchlet
->value
[j
] != ((unsigned char *) data
)[j
+ i
])
551 valid_matchlet
= FALSE
;
563 _xdg_mime_magic_matchlet_compare_level (XdgMimeMagicMatchlet
*matchlet
,
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
))
576 if (_xdg_mime_magic_matchlet_compare_level (matchlet
->next
,
585 matchlet
= matchlet
->next
;
587 while (matchlet
&& matchlet
->indent
> indent
);
594 _xdg_mime_magic_match_compare_to_data (XdgMimeMagicMatch
*match
,
598 return _xdg_mime_magic_matchlet_compare_level (match
->matchlet
, data
, len
, 0);
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
;
613 if (match
->priority
> mime_magic
->match_list
->priority
)
615 match
->next
= mime_magic
->match_list
;
616 mime_magic
->match_list
= match
;
620 list
= mime_magic
->match_list
;
621 while (list
->next
!= NULL
)
623 if (list
->next
->priority
< match
->priority
)
625 match
->next
= list
->next
;
636 _xdg_mime_magic_new (void)
638 return calloc (1, sizeof (XdgMimeMagic
));
642 _xdg_mime_magic_free (XdgMimeMagic
*mime_magic
)
645 _xdg_mime_magic_match_free (mime_magic
->match_list
);
651 _xdg_mime_magic_get_buffer_extents (XdgMimeMagic
*mime_magic
)
653 return mime_magic
->max_extent
;
657 _xdg_mime_magic_lookup_data (XdgMimeMagic
*mime_magic
,
661 const char *mime_types
[],
664 XdgMimeMagicMatch
*match
;
665 const char *mime_type
;
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
;
681 for (n
= 0; n
< n_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
++)
695 mime_type
= mime_types
[n
];
706 _xdg_mime_update_mime_magic_extents (XdgMimeMagic
*mime_magic
)
708 XdgMimeMagicMatch
*match
;
711 for (match
= mime_magic
->match_list
; match
; match
= match
->next
)
713 XdgMimeMagicMatchlet
*matchlet
;
715 for (matchlet
= match
->matchlet
; matchlet
; matchlet
= matchlet
->next
)
719 extent
= matchlet
->value_length
+ matchlet
->offset
+ matchlet
->range_length
;
720 if (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
))
741 XdgMimeMagicMatchlet
*matchlet
;
745 matchlet
->next
= new_list
;
754 _xdg_mime_magic_read_magic_file (XdgMimeMagic
*mime_magic
,
757 XdgMimeMagicState state
;
758 XdgMimeMagicMatch
*match
= NULL
; /* Quiet compiler */
760 state
= XDG_MIME_MAGIC_SECTION
;
762 while (state
!= XDG_MIME_MAGIC_EOF
)
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
);
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
);
783 case XDG_MIME_MAGIC_ERROR
:
784 state
= _xdg_mime_magic_parse_error (magic_file
);
786 case XDG_MIME_MAGIC_EOF
:
788 /* Make the compiler happy */
792 _xdg_mime_update_mime_magic_extents (mime_magic
);
796 _xdg_mime_magic_read_from_file (XdgMimeMagic
*mime_magic
,
797 const char *file_name
)
802 magic_file
= fopen (file_name
, "r");
804 if (magic_file
== NULL
)
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
);