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"
50 #if !defined getc_unlocked && !defined HAVE_GETC_UNLOCKED
51 # define getc_unlocked(fp) getc (fp)
54 typedef struct XdgMimeMagicMatch XdgMimeMagicMatch
;
55 typedef struct XdgMimeMagicMatchlet XdgMimeMagicMatchlet
;
59 XDG_MIME_MAGIC_SECTION
,
65 struct XdgMimeMagicMatch
67 const char *mime_type
;
69 XdgMimeMagicMatchlet
*matchlet
;
70 XdgMimeMagicMatch
*next
;
74 struct XdgMimeMagicMatchlet
78 unsigned int value_length
;
81 unsigned int range_length
;
82 unsigned int word_size
;
83 XdgMimeMagicMatchlet
*next
;
89 XdgMimeMagicMatch
*match_list
;
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
;
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
139 _xdg_mime_magic_match_free (XdgMimeMagicMatch
*mime_magic_match
)
141 XdgMimeMagicMatch
*ptr
, *next
;
143 ptr
= mime_magic_match
;
149 free ((void *) ptr
->mime_type
);
151 _xdg_mime_magic_matchlet_free (ptr
->matchlet
);
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
,
165 unsigned char *retval
;
171 retval
= malloc (len
);
172 *end_of_file
= FALSE
;
176 c
= getc_unlocked (magic_file
);
182 if (c
== '\n' || c
== '\000')
184 retval
[pos
++] = (unsigned char) c
;
185 if (pos
% 128 == 127)
188 retval
= realloc (retval
, len
);
192 retval
[pos
] = '\000';
196 /* Returns the number read from the file, or -1 if no number could be read.
199 _xdg_mime_magic_read_a_number (FILE *magic_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];
211 c
= getc_unlocked (magic_file
);
220 ungetc (c
, magic_file
);
223 number_string
[pos
] = (char) c
;
225 if (pos
== MAX_NUMBER_SIZE
)
230 number_string
[pos
] = '\000';
232 retval
= strtol (number_string
, NULL
, 10);
234 if ((retval
< INT_MIN
) || (retval
> INT_MAX
) || (errno
!= 0))
241 /* Headers are of the format:
242 * [<priority>:<mime-type>]
244 static XdgMimeMagicState
245 _xdg_mime_magic_parse_header (FILE *magic_file
, XdgMimeMagicMatch
*match
)
252 assert (magic_file
!= NULL
);
253 assert (match
!= NULL
);
255 c
= getc_unlocked (magic_file
);
257 return XDG_MIME_MAGIC_EOF
;
259 return XDG_MIME_MAGIC_ERROR
;
261 match
->priority
= _xdg_mime_magic_read_a_number (magic_file
, &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
);
269 return XDG_MIME_MAGIC_EOF
;
271 return XDG_MIME_MAGIC_ERROR
;
273 buffer
= (char *)_xdg_mime_magic_read_to_newline (magic_file
, &end_of_file
);
277 return XDG_MIME_MAGIC_EOF
;
281 while (*end_ptr
!= ']' && *end_ptr
!= '\000' && *end_ptr
!= '\n')
286 return XDG_MIME_MAGIC_ERROR
;
290 match
->mime_type
= strdup (buffer
);
293 return XDG_MIME_MAGIC_MAGIC
;
296 static XdgMimeMagicState
297 _xdg_mime_magic_parse_error (FILE *magic_file
)
303 c
= getc_unlocked (magic_file
);
305 return XDG_MIME_MAGIC_EOF
;
307 return XDG_MIME_MAGIC_SECTION
;
311 /* Headers are of the format:
312 * [ indent ] ">" start-offset "=" value
313 * [ "&" mask ] [ "~" word-size ] [ "+" range-length ] "\n"
315 static XdgMimeMagicState
316 _xdg_mime_magic_parse_magic_line (FILE *magic_file
,
317 XdgMimeMagicMatch
*match
)
319 XdgMimeMagicMatchlet
*matchlet
;
325 assert (magic_file
!= NULL
);
327 /* Sniff the buffer to make sure it's a valid line */
328 c
= getc_unlocked (magic_file
);
330 return XDG_MIME_MAGIC_EOF
;
333 ungetc (c
, magic_file
);
334 return XDG_MIME_MAGIC_SECTION
;
337 return XDG_MIME_MAGIC_MAGIC
;
339 /* At this point, it must be a digit or a '>' */
343 ungetc (c
, magic_file
);
344 indent
= _xdg_mime_magic_read_a_number (magic_file
, &end_of_file
);
346 return XDG_MIME_MAGIC_EOF
;
348 return XDG_MIME_MAGIC_ERROR
;
349 c
= getc_unlocked (magic_file
);
351 return XDG_MIME_MAGIC_EOF
;
355 return XDG_MIME_MAGIC_ERROR
;
357 matchlet
= _xdg_mime_magic_matchlet_new ();
358 matchlet
->indent
= indent
;
359 matchlet
->offset
= _xdg_mime_magic_read_a_number (magic_file
, &end_of_file
);
362 _xdg_mime_magic_matchlet_free (matchlet
);
363 return XDG_MIME_MAGIC_EOF
;
365 if (matchlet
->offset
== -1)
367 _xdg_mime_magic_matchlet_free (matchlet
);
368 return XDG_MIME_MAGIC_ERROR
;
370 c
= getc_unlocked (magic_file
);
373 _xdg_mime_magic_matchlet_free (matchlet
);
374 return XDG_MIME_MAGIC_EOF
;
378 _xdg_mime_magic_matchlet_free (matchlet
);
379 return XDG_MIME_MAGIC_ERROR
;
382 /* Next two bytes determine how long the value is */
383 matchlet
->value_length
= 0;
384 c
= getc_unlocked (magic_file
);
387 _xdg_mime_magic_matchlet_free (matchlet
);
388 return XDG_MIME_MAGIC_EOF
;
390 matchlet
->value_length
= c
& 0xFF;
391 matchlet
->value_length
= matchlet
->value_length
<< 8;
393 c
= getc_unlocked (magic_file
);
396 _xdg_mime_magic_matchlet_free (matchlet
);
397 return XDG_MIME_MAGIC_EOF
;
399 matchlet
->value_length
= matchlet
->value_length
+ (c
& 0xFF);
401 matchlet
->value
= malloc (matchlet
->value_length
);
404 if (matchlet
->value
== NULL
)
406 _xdg_mime_magic_matchlet_free (matchlet
);
407 return XDG_MIME_MAGIC_ERROR
;
409 bytes_read
= fread (matchlet
->value
, 1, matchlet
->value_length
, magic_file
);
410 if (bytes_read
!= (size_t) matchlet
->value_length
)
412 _xdg_mime_magic_matchlet_free (matchlet
);
413 if (feof (magic_file
))
414 return XDG_MIME_MAGIC_EOF
;
416 return XDG_MIME_MAGIC_ERROR
;
419 c
= getc_unlocked (magic_file
);
422 matchlet
->mask
= malloc (matchlet
->value_length
);
424 if (matchlet
->mask
== NULL
)
426 _xdg_mime_magic_matchlet_free (matchlet
);
427 return XDG_MIME_MAGIC_ERROR
;
429 bytes_read
= fread (matchlet
->mask
, 1, matchlet
->value_length
, magic_file
);
430 if (bytes_read
!= (size_t) matchlet
->value_length
)
432 _xdg_mime_magic_matchlet_free (matchlet
);
433 if (feof (magic_file
))
434 return XDG_MIME_MAGIC_EOF
;
436 return XDG_MIME_MAGIC_ERROR
;
438 c
= getc_unlocked (magic_file
);
443 matchlet
->word_size
= _xdg_mime_magic_read_a_number (magic_file
, &end_of_file
);
446 _xdg_mime_magic_matchlet_free (matchlet
);
447 return XDG_MIME_MAGIC_EOF
;
449 if (matchlet
->word_size
!= 0 &&
450 matchlet
->word_size
!= 1 &&
451 matchlet
->word_size
!= 2 &&
452 matchlet
->word_size
!= 4)
454 _xdg_mime_magic_matchlet_free (matchlet
);
455 return XDG_MIME_MAGIC_ERROR
;
457 c
= getc_unlocked (magic_file
);
462 matchlet
->range_length
= _xdg_mime_magic_read_a_number (magic_file
, &end_of_file
);
465 _xdg_mime_magic_matchlet_free (matchlet
);
466 return XDG_MIME_MAGIC_EOF
;
468 if (matchlet
->range_length
== (unsigned int) -1)
470 _xdg_mime_magic_matchlet_free (matchlet
);
471 return XDG_MIME_MAGIC_ERROR
;
473 c
= getc_unlocked (magic_file
);
479 /* We clean up the matchlet, byte swapping if needed */
480 if (matchlet
->word_size
> 1)
483 if (matchlet
->value_length
% matchlet
->word_size
!= 0)
485 _xdg_mime_magic_matchlet_free (matchlet
);
486 return XDG_MIME_MAGIC_ERROR
;
488 /* FIXME: need to get this defined in a <config.h> style file */
490 for (i
= 0; i
< matchlet
->value_length
; i
= i
+ matchlet
->word_size
)
492 if (matchlet
->word_size
== 2)
493 *((xdg_uint16_t
*) matchlet
->value
+ i
) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t
*) (matchlet
->value
+ i
)));
494 else if (matchlet
->word_size
== 4)
495 *((xdg_uint32_t
*) matchlet
->value
+ i
) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t
*) (matchlet
->value
+ i
)));
498 if (matchlet
->word_size
== 2)
499 *((xdg_uint16_t
*) matchlet
->mask
+ i
) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t
*) (matchlet
->mask
+ i
)));
500 else if (matchlet
->word_size
== 4)
501 *((xdg_uint32_t
*) matchlet
->mask
+ i
) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t
*) (matchlet
->mask
+ i
)));
508 matchlet
->next
= match
->matchlet
;
509 match
->matchlet
= matchlet
;
512 return XDG_MIME_MAGIC_MAGIC
;
515 _xdg_mime_magic_matchlet_free (matchlet
);
517 return XDG_MIME_MAGIC_EOF
;
519 return XDG_MIME_MAGIC_ERROR
;
523 _xdg_mime_magic_matchlet_compare_to_data (XdgMimeMagicMatchlet
*matchlet
,
528 for (i
= matchlet
->offset
; i
< matchlet
->offset
+ matchlet
->range_length
; i
++)
530 int valid_matchlet
= TRUE
;
532 if (i
+ matchlet
->value_length
> len
)
537 for (j
= 0; j
< matchlet
->value_length
; j
++)
539 if ((matchlet
->value
[j
] & matchlet
->mask
[j
]) !=
540 ((((unsigned char *) data
)[j
+ i
]) & matchlet
->mask
[j
]))
542 valid_matchlet
= FALSE
;
549 for (j
= 0; j
< matchlet
->value_length
; j
++)
551 if (matchlet
->value
[j
] != ((unsigned char *) data
)[j
+ i
])
553 valid_matchlet
= FALSE
;
565 _xdg_mime_magic_matchlet_compare_level (XdgMimeMagicMatchlet
*matchlet
,
570 while ((matchlet
!= NULL
) && (matchlet
->indent
== indent
))
572 if (_xdg_mime_magic_matchlet_compare_to_data (matchlet
, data
, len
))
574 if ((matchlet
->next
== NULL
) ||
575 (matchlet
->next
->indent
<= indent
))
578 if (_xdg_mime_magic_matchlet_compare_level (matchlet
->next
,
587 matchlet
= matchlet
->next
;
589 while (matchlet
&& matchlet
->indent
> indent
);
596 _xdg_mime_magic_match_compare_to_data (XdgMimeMagicMatch
*match
,
600 return _xdg_mime_magic_matchlet_compare_level (match
->matchlet
, data
, len
, 0);
604 _xdg_mime_magic_insert_match (XdgMimeMagic
*mime_magic
,
605 XdgMimeMagicMatch
*match
)
607 XdgMimeMagicMatch
*list
;
609 if (mime_magic
->match_list
== NULL
)
611 mime_magic
->match_list
= match
;
615 if (match
->priority
> mime_magic
->match_list
->priority
)
617 match
->next
= mime_magic
->match_list
;
618 mime_magic
->match_list
= match
;
622 list
= mime_magic
->match_list
;
623 while (list
->next
!= NULL
)
625 if (list
->next
->priority
< match
->priority
)
627 match
->next
= list
->next
;
638 _xdg_mime_magic_new (void)
640 return calloc (1, sizeof (XdgMimeMagic
));
644 _xdg_mime_magic_free (XdgMimeMagic
*mime_magic
)
647 _xdg_mime_magic_match_free (mime_magic
->match_list
);
653 _xdg_mime_magic_get_buffer_extents (XdgMimeMagic
*mime_magic
)
655 return mime_magic
->max_extent
;
659 _xdg_mime_magic_lookup_data (XdgMimeMagic
*mime_magic
,
663 const char *mime_types
[],
666 XdgMimeMagicMatch
*match
;
667 const char *mime_type
;
673 for (match
= mime_magic
->match_list
; match
; match
= match
->next
)
675 if (_xdg_mime_magic_match_compare_to_data (match
, data
, len
))
677 prio
= match
->priority
;
678 mime_type
= match
->mime_type
;
683 for (n
= 0; n
< n_mime_types
; n
++)
686 _xdg_mime_mime_type_equal (mime_types
[n
], match
->mime_type
))
687 mime_types
[n
] = NULL
;
692 if (mime_type
== NULL
)
694 for (n
= 0; n
< n_mime_types
; n
++)
697 mime_type
= mime_types
[n
];
708 _xdg_mime_update_mime_magic_extents (XdgMimeMagic
*mime_magic
)
710 XdgMimeMagicMatch
*match
;
713 for (match
= mime_magic
->match_list
; match
; match
= match
->next
)
715 XdgMimeMagicMatchlet
*matchlet
;
717 for (matchlet
= match
->matchlet
; matchlet
; matchlet
= matchlet
->next
)
721 extent
= matchlet
->value_length
+ matchlet
->offset
+ matchlet
->range_length
;
722 if (max_extent
< extent
)
727 mime_magic
->max_extent
= max_extent
;
730 static XdgMimeMagicMatchlet
*
731 _xdg_mime_magic_matchlet_mirror (XdgMimeMagicMatchlet
*matchlets
)
733 XdgMimeMagicMatchlet
*new_list
;
734 XdgMimeMagicMatchlet
*tmp
;
736 if ((matchlets
== NULL
) || (matchlets
->next
== NULL
))
743 XdgMimeMagicMatchlet
*matchlet
;
747 matchlet
->next
= new_list
;
756 _xdg_mime_magic_read_magic_file (XdgMimeMagic
*mime_magic
,
759 XdgMimeMagicState state
;
760 XdgMimeMagicMatch
*match
= NULL
; /* Quiet compiler */
762 state
= XDG_MIME_MAGIC_SECTION
;
764 while (state
!= XDG_MIME_MAGIC_EOF
)
768 case XDG_MIME_MAGIC_SECTION
:
769 match
= _xdg_mime_magic_match_new ();
770 state
= _xdg_mime_magic_parse_header (magic_file
, match
);
771 if (state
== XDG_MIME_MAGIC_EOF
|| state
== XDG_MIME_MAGIC_ERROR
)
772 _xdg_mime_magic_match_free (match
);
774 case XDG_MIME_MAGIC_MAGIC
:
775 state
= _xdg_mime_magic_parse_magic_line (magic_file
, match
);
776 if (state
== XDG_MIME_MAGIC_SECTION
||
777 (state
== XDG_MIME_MAGIC_EOF
&& match
->mime_type
))
779 match
->matchlet
= _xdg_mime_magic_matchlet_mirror (match
->matchlet
);
780 _xdg_mime_magic_insert_match (mime_magic
, match
);
782 else if (state
== XDG_MIME_MAGIC_EOF
|| state
== XDG_MIME_MAGIC_ERROR
)
783 _xdg_mime_magic_match_free (match
);
785 case XDG_MIME_MAGIC_ERROR
:
786 state
= _xdg_mime_magic_parse_error (magic_file
);
788 case XDG_MIME_MAGIC_EOF
:
790 /* Make the compiler happy */
794 _xdg_mime_update_mime_magic_extents (mime_magic
);
798 _xdg_mime_magic_read_from_file (XdgMimeMagic
*mime_magic
,
799 const char *file_name
)
804 magic_file
= fopen (file_name
, "r");
806 if (magic_file
== NULL
)
809 if (fread (header
, 1, 12, magic_file
) == 12)
811 if (memcmp ("MIME-Magic\0\n", header
, 12) == 0)
812 _xdg_mime_magic_read_magic_file (mime_magic
, magic_file
);