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.
32 #include "xdgmimemagic.h"
33 #include "xdgmimeint.h"
51 typedef struct XdgMimeMagicMatch XdgMimeMagicMatch
;
52 typedef struct XdgMimeMagicMatchlet XdgMimeMagicMatchlet
;
56 XDG_MIME_MAGIC_SECTION
,
62 struct XdgMimeMagicMatch
64 const char *mime_type
;
66 XdgMimeMagicMatchlet
*matchlet
;
67 XdgMimeMagicMatch
*next
;
71 struct XdgMimeMagicMatchlet
75 unsigned int value_length
;
78 unsigned int range_length
;
79 unsigned int word_size
;
80 XdgMimeMagicMatchlet
*next
;
86 XdgMimeMagicMatch
*match_list
;
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
;
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
136 _xdg_mime_magic_match_free (XdgMimeMagicMatch
*mime_magic_match
)
138 XdgMimeMagicMatch
*ptr
, *next
;
140 ptr
= mime_magic_match
;
146 free ((void *) ptr
->mime_type
);
148 _xdg_mime_magic_matchlet_free (ptr
->matchlet
);
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
,
162 unsigned char *retval
;
168 retval
= malloc (len
);
169 *end_of_file
= FALSE
;
173 c
= getc_unlocked (magic_file
);
179 if (c
== '\n' || c
== '\000')
181 retval
[pos
++] = (unsigned char) c
;
182 if (pos
% 128 == 127)
185 retval
= realloc (retval
, len
);
189 retval
[pos
] = '\000';
193 /* Returns the number read from the file, or -1 if no number could be read.
196 _xdg_mime_magic_read_a_number (FILE *magic_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];
208 c
= getc_unlocked (magic_file
);
217 ungetc (c
, magic_file
);
220 number_string
[pos
] = (char) c
;
222 if (pos
== MAX_NUMBER_SIZE
)
227 number_string
[pos
] = '\000';
229 retval
= strtol (number_string
, NULL
, 10);
231 if ((retval
< INT_MIN
) || (retval
> INT_MAX
) || (errno
!= 0))
238 /* Headers are of the format:
239 * [<priority>:<mime-type>]
241 static XdgMimeMagicState
242 _xdg_mime_magic_parse_header (FILE *magic_file
, XdgMimeMagicMatch
*match
)
249 assert (magic_file
!= NULL
);
250 assert (match
!= NULL
);
252 c
= getc_unlocked (magic_file
);
254 return XDG_MIME_MAGIC_EOF
;
256 return XDG_MIME_MAGIC_ERROR
;
258 match
->priority
= _xdg_mime_magic_read_a_number (magic_file
, &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
);
266 return XDG_MIME_MAGIC_EOF
;
268 return XDG_MIME_MAGIC_ERROR
;
270 buffer
= _xdg_mime_magic_read_to_newline (magic_file
, &end_of_file
);
272 return XDG_MIME_MAGIC_EOF
;
275 while (*end_ptr
!= ']' && *end_ptr
!= '\000' && *end_ptr
!= '\n')
280 return XDG_MIME_MAGIC_ERROR
;
284 match
->mime_type
= strdup (buffer
);
287 return XDG_MIME_MAGIC_MAGIC
;
290 static XdgMimeMagicState
291 _xdg_mime_magic_parse_error (FILE *magic_file
)
297 c
= getc_unlocked (magic_file
);
299 return XDG_MIME_MAGIC_EOF
;
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
;
319 assert (magic_file
!= NULL
);
321 /* Sniff the buffer to make sure it's a valid line */
322 c
= getc_unlocked (magic_file
);
324 return XDG_MIME_MAGIC_EOF
;
327 ungetc (c
, magic_file
);
328 return XDG_MIME_MAGIC_SECTION
;
331 return XDG_MIME_MAGIC_MAGIC
;
333 /* At this point, it must be a digit or a '>' */
337 ungetc (c
, magic_file
);
338 indent
= _xdg_mime_magic_read_a_number (magic_file
, &end_of_file
);
340 return XDG_MIME_MAGIC_EOF
;
342 return XDG_MIME_MAGIC_ERROR
;
343 c
= getc_unlocked (magic_file
);
345 return XDG_MIME_MAGIC_EOF
;
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
);
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
);
367 _xdg_mime_magic_matchlet_free (matchlet
);
368 return XDG_MIME_MAGIC_EOF
;
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
);
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
);
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
);
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
;
410 return XDG_MIME_MAGIC_ERROR
;
413 c
= getc_unlocked (magic_file
);
416 matchlet
->mask
= malloc (matchlet
->value_length
);
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
;
430 return XDG_MIME_MAGIC_ERROR
;
432 c
= getc_unlocked (magic_file
);
437 matchlet
->word_size
= _xdg_mime_magic_read_a_number (magic_file
, &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
);
456 matchlet
->range_length
= _xdg_mime_magic_read_a_number (magic_file
, &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
);
473 /* We clean up the matchlet, byte swapping if needed */
474 if (matchlet
->word_size
> 1)
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 */
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
)));
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
)));
502 matchlet
->next
= match
->matchlet
;
503 match
->matchlet
= matchlet
;
506 return XDG_MIME_MAGIC_MAGIC
;
509 _xdg_mime_magic_matchlet_free (matchlet
);
511 return XDG_MIME_MAGIC_EOF
;
513 return XDG_MIME_MAGIC_ERROR
;
517 _xdg_mime_magic_matchlet_compare_to_data (XdgMimeMagicMatchlet
*matchlet
,
523 for (i
= matchlet
->offset
; i
<= matchlet
->offset
+ matchlet
->range_length
; i
++)
525 int valid_matchlet
= TRUE
;
527 if (i
+ matchlet
->value_length
> len
)
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
;
544 for (j
= 0; j
< matchlet
->value_length
; j
++)
546 if (matchlet
->value
[j
] != ((unsigned char *) data
)[j
+ i
])
548 valid_matchlet
= FALSE
;
560 _xdg_mime_magic_matchlet_compare_level (XdgMimeMagicMatchlet
*matchlet
,
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
))
573 if (_xdg_mime_magic_matchlet_compare_level (matchlet
->next
,
582 matchlet
= matchlet
->next
;
584 while (matchlet
&& matchlet
->indent
> indent
);
591 _xdg_mime_magic_match_compare_to_data (XdgMimeMagicMatch
*match
,
595 return _xdg_mime_magic_matchlet_compare_level (match
->matchlet
, data
, len
, 0);
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
;
610 if (match
->priority
> mime_magic
->match_list
->priority
)
612 match
->next
= mime_magic
->match_list
;
613 mime_magic
->match_list
= match
;
617 list
= mime_magic
->match_list
;
618 while (list
->next
!= NULL
)
620 if (list
->next
->priority
< match
->priority
)
622 match
->next
= list
->next
;
633 _xdg_mime_magic_new (void)
635 return calloc (1, sizeof (XdgMimeMagic
));
639 _xdg_mime_magic_free (XdgMimeMagic
*mime_magic
)
646 _xdg_mime_magic_get_buffer_extents (XdgMimeMagic
*mime_magic
)
648 return mime_magic
->max_extent
;
652 _xdg_mime_magic_lookup_data (XdgMimeMagic
*mime_magic
,
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
;
670 _xdg_mime_update_mime_magic_extents (XdgMimeMagic
*mime_magic
)
672 XdgMimeMagicMatch
*match
;
675 for (match
= mime_magic
->match_list
; match
; match
= match
->next
)
677 XdgMimeMagicMatchlet
*matchlet
;
679 for (matchlet
= match
->matchlet
; matchlet
; matchlet
= matchlet
->next
)
683 extent
= matchlet
->value_length
+ matchlet
->offset
+ matchlet
->range_length
;
684 if (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
))
705 XdgMimeMagicMatchlet
*matchlet
;
709 matchlet
->next
= new_list
;
718 _xdg_mime_magic_read_magic_file (XdgMimeMagic
*mime_magic
,
721 XdgMimeMagicState state
;
722 XdgMimeMagicMatch
*match
= NULL
; /* Quiet compiler */
724 state
= XDG_MIME_MAGIC_SECTION
;
726 while (state
!= XDG_MIME_MAGIC_EOF
)
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
);
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
);
747 case XDG_MIME_MAGIC_ERROR
:
748 state
= _xdg_mime_magic_parse_error (magic_file
);
750 case XDG_MIME_MAGIC_EOF
:
752 /* Make the compiler happy */
756 _xdg_mime_update_mime_magic_extents (mime_magic
);
760 _xdg_mime_magic_read_from_file (XdgMimeMagic
*mime_magic
,
761 const char *file_name
)
766 magic_file
= fopen (file_name
, "r");
768 if (magic_file
== NULL
)
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
);