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.
33 #include "xdgmimemagic.h"
34 #include "xdgmimeint.h"
52 typedef struct XdgMimeMagicMatch XdgMimeMagicMatch
;
53 typedef struct XdgMimeMagicMatchlet XdgMimeMagicMatchlet
;
57 XDG_MIME_MAGIC_SECTION
,
63 struct XdgMimeMagicMatch
65 const char *mime_type
;
67 XdgMimeMagicMatchlet
*matchlet
;
68 XdgMimeMagicMatch
*next
;
72 struct XdgMimeMagicMatchlet
76 unsigned int value_length
;
79 unsigned int range_length
;
80 unsigned int word_size
;
81 XdgMimeMagicMatchlet
*next
;
87 XdgMimeMagicMatch
*match_list
;
91 static XdgMimeMagicMatch
*
92 _xdg_mime_magic_match_new (void)
94 return calloc (1, sizeof (XdgMimeMagicMatch
));
98 static XdgMimeMagicMatchlet
*
99 _xdg_mime_magic_matchlet_new (void)
101 XdgMimeMagicMatchlet
*matchlet
;
103 matchlet
= malloc (sizeof (XdgMimeMagicMatchlet
));
105 matchlet
->indent
= 0;
106 matchlet
->offset
= 0;
107 matchlet
->value_length
= 0;
108 matchlet
->value
= NULL
;
109 matchlet
->mask
= NULL
;
110 matchlet
->range_length
= 1;
111 matchlet
->word_size
= 1;
112 matchlet
->next
= NULL
;
119 _xdg_mime_magic_matchlet_free (XdgMimeMagicMatchlet
*mime_magic_matchlet
)
121 if (mime_magic_matchlet
)
123 if (mime_magic_matchlet
->next
)
124 _xdg_mime_magic_matchlet_free (mime_magic_matchlet
->next
);
125 if (mime_magic_matchlet
->value
)
126 free (mime_magic_matchlet
->value
);
127 if (mime_magic_matchlet
->mask
)
128 free (mime_magic_matchlet
->mask
);
129 free (mime_magic_matchlet
);
134 /* Frees mime_magic_match and the remainder of its list
137 _xdg_mime_magic_match_free (XdgMimeMagicMatch
*mime_magic_match
)
139 XdgMimeMagicMatch
*ptr
, *next
;
141 ptr
= mime_magic_match
;
147 free ((void *) ptr
->mime_type
);
149 _xdg_mime_magic_matchlet_free (ptr
->matchlet
);
156 /* Reads in a hunk of data until a newline character or a '\000' is hit. The
157 * returned string is null terminated, and doesn't include the newline.
159 static unsigned char *
160 _xdg_mime_magic_read_to_newline (FILE *magic_file
,
163 unsigned char *retval
;
169 retval
= malloc (len
);
170 *end_of_file
= FALSE
;
174 c
= getc_unlocked (magic_file
);
180 if (c
== '\n' || c
== '\000')
182 retval
[pos
++] = (unsigned char) c
;
183 if (pos
% 128 == 127)
186 retval
= realloc (retval
, len
);
190 retval
[pos
] = '\000';
194 /* Returns the number read from the file, or -1 if no number could be read.
197 _xdg_mime_magic_read_a_number (FILE *magic_file
,
200 /* LONG_MAX is about 20 characters on my system */
201 #define MAX_NUMBER_SIZE 30
202 char number_string
[MAX_NUMBER_SIZE
+ 1];
209 c
= getc_unlocked (magic_file
);
218 ungetc (c
, magic_file
);
221 number_string
[pos
] = (char) c
;
223 if (pos
== MAX_NUMBER_SIZE
)
228 number_string
[pos
] = '\000';
230 retval
= strtol (number_string
, NULL
, 10);
232 if ((retval
< INT_MIN
) || (retval
> INT_MAX
) || (errno
!= 0))
239 /* Headers are of the format:
240 * [<priority>:<mime-type>]
242 static XdgMimeMagicState
243 _xdg_mime_magic_parse_header (FILE *magic_file
, XdgMimeMagicMatch
*match
)
250 assert (magic_file
!= NULL
);
251 assert (match
!= NULL
);
253 c
= getc_unlocked (magic_file
);
255 return XDG_MIME_MAGIC_EOF
;
257 return XDG_MIME_MAGIC_ERROR
;
259 match
->priority
= _xdg_mime_magic_read_a_number (magic_file
, &end_of_file
);
261 return XDG_MIME_MAGIC_EOF
;
262 if (match
->priority
== -1)
263 return XDG_MIME_MAGIC_ERROR
;
265 c
= getc_unlocked (magic_file
);
267 return XDG_MIME_MAGIC_EOF
;
269 return XDG_MIME_MAGIC_ERROR
;
271 buffer
= (char *)_xdg_mime_magic_read_to_newline (magic_file
, &end_of_file
);
273 return XDG_MIME_MAGIC_EOF
;
276 while (*end_ptr
!= ']' && *end_ptr
!= '\000' && *end_ptr
!= '\n')
281 return XDG_MIME_MAGIC_ERROR
;
285 match
->mime_type
= strdup (buffer
);
288 return XDG_MIME_MAGIC_MAGIC
;
291 static XdgMimeMagicState
292 _xdg_mime_magic_parse_error (FILE *magic_file
)
298 c
= getc_unlocked (magic_file
);
300 return XDG_MIME_MAGIC_EOF
;
302 return XDG_MIME_MAGIC_SECTION
;
306 /* Headers are of the format:
307 * [ indent ] ">" start-offset "=" value
308 * [ "&" mask ] [ "~" word-size ] [ "+" range-length ] "\n"
310 static XdgMimeMagicState
311 _xdg_mime_magic_parse_magic_line (FILE *magic_file
,
312 XdgMimeMagicMatch
*match
)
314 XdgMimeMagicMatchlet
*matchlet
;
320 assert (magic_file
!= NULL
);
322 /* Sniff the buffer to make sure it's a valid line */
323 c
= getc_unlocked (magic_file
);
325 return XDG_MIME_MAGIC_EOF
;
328 ungetc (c
, magic_file
);
329 return XDG_MIME_MAGIC_SECTION
;
332 return XDG_MIME_MAGIC_MAGIC
;
334 /* At this point, it must be a digit or a '>' */
338 ungetc (c
, magic_file
);
339 indent
= _xdg_mime_magic_read_a_number (magic_file
, &end_of_file
);
341 return XDG_MIME_MAGIC_EOF
;
343 return XDG_MIME_MAGIC_ERROR
;
344 c
= getc_unlocked (magic_file
);
346 return XDG_MIME_MAGIC_EOF
;
350 return XDG_MIME_MAGIC_ERROR
;
352 matchlet
= _xdg_mime_magic_matchlet_new ();
353 matchlet
->indent
= indent
;
354 matchlet
->offset
= _xdg_mime_magic_read_a_number (magic_file
, &end_of_file
);
357 _xdg_mime_magic_matchlet_free (matchlet
);
358 return XDG_MIME_MAGIC_EOF
;
360 if (matchlet
->offset
== -1)
362 _xdg_mime_magic_matchlet_free (matchlet
);
363 return XDG_MIME_MAGIC_ERROR
;
365 c
= getc_unlocked (magic_file
);
368 _xdg_mime_magic_matchlet_free (matchlet
);
369 return XDG_MIME_MAGIC_EOF
;
373 _xdg_mime_magic_matchlet_free (matchlet
);
374 return XDG_MIME_MAGIC_ERROR
;
377 /* Next two bytes determine how long the value is */
378 matchlet
->value_length
= 0;
379 c
= getc_unlocked (magic_file
);
382 _xdg_mime_magic_matchlet_free (matchlet
);
383 return XDG_MIME_MAGIC_EOF
;
385 matchlet
->value_length
= c
& 0xFF;
386 matchlet
->value_length
= matchlet
->value_length
<< 8;
388 c
= getc_unlocked (magic_file
);
391 _xdg_mime_magic_matchlet_free (matchlet
);
392 return XDG_MIME_MAGIC_EOF
;
394 matchlet
->value_length
= matchlet
->value_length
+ (c
& 0xFF);
396 matchlet
->value
= malloc (matchlet
->value_length
);
399 if (matchlet
->value
== NULL
)
401 _xdg_mime_magic_matchlet_free (matchlet
);
402 return XDG_MIME_MAGIC_ERROR
;
404 bytes_read
= fread (matchlet
->value
, 1, matchlet
->value_length
, magic_file
);
405 if (bytes_read
!= matchlet
->value_length
)
407 _xdg_mime_magic_matchlet_free (matchlet
);
408 if (feof (magic_file
))
409 return XDG_MIME_MAGIC_EOF
;
411 return XDG_MIME_MAGIC_ERROR
;
414 c
= getc_unlocked (magic_file
);
417 matchlet
->mask
= malloc (matchlet
->value_length
);
419 if (matchlet
->mask
== NULL
)
421 _xdg_mime_magic_matchlet_free (matchlet
);
422 return XDG_MIME_MAGIC_ERROR
;
424 bytes_read
= fread (matchlet
->mask
, 1, matchlet
->value_length
, magic_file
);
425 if (bytes_read
!= matchlet
->value_length
)
427 _xdg_mime_magic_matchlet_free (matchlet
);
428 if (feof (magic_file
))
429 return XDG_MIME_MAGIC_EOF
;
431 return XDG_MIME_MAGIC_ERROR
;
433 c
= getc_unlocked (magic_file
);
438 matchlet
->word_size
= _xdg_mime_magic_read_a_number (magic_file
, &end_of_file
);
441 _xdg_mime_magic_matchlet_free (matchlet
);
442 return XDG_MIME_MAGIC_EOF
;
444 if (matchlet
->word_size
!= 0 &&
445 matchlet
->word_size
!= 1 &&
446 matchlet
->word_size
!= 2 &&
447 matchlet
->word_size
!= 4)
449 _xdg_mime_magic_matchlet_free (matchlet
);
450 return XDG_MIME_MAGIC_ERROR
;
452 c
= getc_unlocked (magic_file
);
457 matchlet
->range_length
= _xdg_mime_magic_read_a_number (magic_file
, &end_of_file
);
460 _xdg_mime_magic_matchlet_free (matchlet
);
461 return XDG_MIME_MAGIC_EOF
;
463 if (matchlet
->range_length
== -1)
465 _xdg_mime_magic_matchlet_free (matchlet
);
466 return XDG_MIME_MAGIC_ERROR
;
468 c
= getc_unlocked (magic_file
);
474 /* We clean up the matchlet, byte swapping if needed */
475 if (matchlet
->word_size
> 1)
480 if (matchlet
->value_length
% matchlet
->word_size
!= 0)
482 _xdg_mime_magic_matchlet_free (matchlet
);
483 return XDG_MIME_MAGIC_ERROR
;
485 /* FIXME: need to get this defined in a <config.h> style file */
487 for (i
= 0; i
< matchlet
->value_length
; i
= i
+ matchlet
->word_size
)
489 if (matchlet
->word_size
== 2)
490 *((xdg_uint16_t
*) matchlet
->value
+ i
) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t
*) (matchlet
->value
+ i
)));
491 else if (matchlet
->word_size
== 4)
492 *((xdg_uint32_t
*) matchlet
->value
+ i
) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t
*) (matchlet
->value
+ i
)));
495 if (matchlet
->word_size
== 2)
496 *((xdg_uint16_t
*) matchlet
->mask
+ i
) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t
*) (matchlet
->mask
+ i
)));
497 else if (matchlet
->word_size
== 4)
498 *((xdg_uint32_t
*) matchlet
->mask
+ i
) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t
*) (matchlet
->mask
+ i
)));
505 matchlet
->next
= match
->matchlet
;
506 match
->matchlet
= matchlet
;
509 return XDG_MIME_MAGIC_MAGIC
;
512 _xdg_mime_magic_matchlet_free (matchlet
);
514 return XDG_MIME_MAGIC_EOF
;
516 return XDG_MIME_MAGIC_ERROR
;
520 _xdg_mime_magic_matchlet_compare_to_data (XdgMimeMagicMatchlet
*matchlet
,
525 for (i
= matchlet
->offset
; i
< matchlet
->offset
+ matchlet
->range_length
; i
++)
527 int valid_matchlet
= TRUE
;
529 if (i
+ matchlet
->value_length
> len
)
534 for (j
= 0; j
< matchlet
->value_length
; j
++)
536 if ((matchlet
->value
[j
] & matchlet
->mask
[j
]) !=
537 ((((unsigned char *) data
)[j
+ i
]) & matchlet
->mask
[j
]))
539 valid_matchlet
= FALSE
;
546 for (j
= 0; j
< matchlet
->value_length
; j
++)
548 if (matchlet
->value
[j
] != ((unsigned char *) data
)[j
+ i
])
550 valid_matchlet
= FALSE
;
562 _xdg_mime_magic_matchlet_compare_level (XdgMimeMagicMatchlet
*matchlet
,
567 while ((matchlet
!= NULL
) && (matchlet
->indent
== indent
))
569 if (_xdg_mime_magic_matchlet_compare_to_data (matchlet
, data
, len
))
571 if ((matchlet
->next
== NULL
) ||
572 (matchlet
->next
->indent
<= indent
))
575 if (_xdg_mime_magic_matchlet_compare_level (matchlet
->next
,
584 matchlet
= matchlet
->next
;
586 while (matchlet
&& matchlet
->indent
> indent
);
593 _xdg_mime_magic_match_compare_to_data (XdgMimeMagicMatch
*match
,
597 return _xdg_mime_magic_matchlet_compare_level (match
->matchlet
, data
, len
, 0);
601 _xdg_mime_magic_insert_match (XdgMimeMagic
*mime_magic
,
602 XdgMimeMagicMatch
*match
)
604 XdgMimeMagicMatch
*list
;
606 if (mime_magic
->match_list
== NULL
)
608 mime_magic
->match_list
= match
;
612 if (match
->priority
> mime_magic
->match_list
->priority
)
614 match
->next
= mime_magic
->match_list
;
615 mime_magic
->match_list
= match
;
619 list
= mime_magic
->match_list
;
620 while (list
->next
!= NULL
)
622 if (list
->next
->priority
< match
->priority
)
624 match
->next
= list
->next
;
635 _xdg_mime_magic_new (void)
637 return calloc (1, sizeof (XdgMimeMagic
));
641 _xdg_mime_magic_free (XdgMimeMagic
*mime_magic
)
644 _xdg_mime_magic_match_free (mime_magic
->match_list
);
650 _xdg_mime_magic_get_buffer_extents (XdgMimeMagic
*mime_magic
)
652 return mime_magic
->max_extent
;
656 _xdg_mime_magic_lookup_data (XdgMimeMagic
*mime_magic
,
659 const char *mime_types
[],
662 XdgMimeMagicMatch
*match
;
663 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 if (!had_match
|| match
->priority
> priority
||
676 (mime_type
!= NULL
&& _xdg_mime_mime_type_subclass (match
->mime_type
, mime_type
)))
678 mime_type
= match
->mime_type
;
679 priority
= match
->priority
;
681 else if (had_match
&& match
->priority
== priority
)
682 /* multiple unrelated patterns with the same priority matched,
683 * so we can't tell what type this is. */
690 for (n
= 0; n
< n_mime_types
; n
++)
693 _xdg_mime_mime_type_equal (mime_types
[n
], match
->mime_type
))
694 mime_types
[n
] = NULL
;
699 if (mime_type
== NULL
)
701 for (n
= 0; n
< n_mime_types
; n
++)
704 mime_type
= mime_types
[n
];
712 _xdg_mime_update_mime_magic_extents (XdgMimeMagic
*mime_magic
)
714 XdgMimeMagicMatch
*match
;
717 for (match
= mime_magic
->match_list
; match
; match
= match
->next
)
719 XdgMimeMagicMatchlet
*matchlet
;
721 for (matchlet
= match
->matchlet
; matchlet
; matchlet
= matchlet
->next
)
725 extent
= matchlet
->value_length
+ matchlet
->offset
+ matchlet
->range_length
;
726 if (max_extent
< extent
)
731 mime_magic
->max_extent
= max_extent
;
734 static XdgMimeMagicMatchlet
*
735 _xdg_mime_magic_matchlet_mirror (XdgMimeMagicMatchlet
*matchlets
)
737 XdgMimeMagicMatchlet
*new_list
;
738 XdgMimeMagicMatchlet
*tmp
;
740 if ((matchlets
== NULL
) || (matchlets
->next
== NULL
))
747 XdgMimeMagicMatchlet
*matchlet
;
751 matchlet
->next
= new_list
;
760 _xdg_mime_magic_read_magic_file (XdgMimeMagic
*mime_magic
,
763 XdgMimeMagicState state
;
764 XdgMimeMagicMatch
*match
= NULL
; /* Quiet compiler */
766 state
= XDG_MIME_MAGIC_SECTION
;
768 while (state
!= XDG_MIME_MAGIC_EOF
)
772 case XDG_MIME_MAGIC_SECTION
:
773 match
= _xdg_mime_magic_match_new ();
774 state
= _xdg_mime_magic_parse_header (magic_file
, match
);
775 if (state
== XDG_MIME_MAGIC_EOF
|| state
== XDG_MIME_MAGIC_ERROR
)
776 _xdg_mime_magic_match_free (match
);
778 case XDG_MIME_MAGIC_MAGIC
:
779 state
= _xdg_mime_magic_parse_magic_line (magic_file
, match
);
780 if (state
== XDG_MIME_MAGIC_SECTION
||
781 (state
== XDG_MIME_MAGIC_EOF
&& match
->mime_type
))
783 match
->matchlet
= _xdg_mime_magic_matchlet_mirror (match
->matchlet
);
784 _xdg_mime_magic_insert_match (mime_magic
, match
);
786 else if (state
== XDG_MIME_MAGIC_EOF
|| state
== XDG_MIME_MAGIC_ERROR
)
787 _xdg_mime_magic_match_free (match
);
789 case XDG_MIME_MAGIC_ERROR
:
790 state
= _xdg_mime_magic_parse_error (magic_file
);
792 case XDG_MIME_MAGIC_EOF
:
794 /* Make the compiler happy */
798 _xdg_mime_update_mime_magic_extents (mime_magic
);
802 _xdg_mime_magic_read_from_file (XdgMimeMagic
*mime_magic
,
803 const char *file_name
)
808 magic_file
= fopen (file_name
, "r");
810 if (magic_file
== NULL
)
813 if (fread (header
, 1, 12, magic_file
) == 12)
815 if (memcmp ("MIME-Magic\0\n", header
, 12) == 0)
816 _xdg_mime_magic_read_magic_file (mime_magic
, magic_file
);