50#if !defined getc_unlocked && !defined HAVE_GETC_UNLOCKED
51# define getc_unlocked(fp) getc (fp)
94_xdg_mime_magic_match_new (
void)
101_xdg_mime_magic_matchlet_new (
void)
110 matchlet->
value = NULL;
111 matchlet->
mask = NULL;
114 matchlet->
next = NULL;
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);
143 ptr = mime_magic_match;
151 _xdg_mime_magic_matchlet_free (ptr->
matchlet);
161static 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';
199_xdg_mime_magic_read_a_number (FILE *magic_file,
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))
252 assert (magic_file != NULL);
253 assert (match != NULL);
255 c = getc_unlocked (magic_file);
261 match->
priority = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
267 c = getc_unlocked (magic_file);
273 buffer = (
char *)_xdg_mime_magic_read_to_newline (magic_file, &end_of_file);
281 while (*end_ptr !=
']' && *end_ptr !=
'\000' && *end_ptr !=
'\n')
297_xdg_mime_magic_parse_error (FILE *magic_file)
303 c = getc_unlocked (magic_file);
316_xdg_mime_magic_parse_magic_line (FILE *magic_file,
325 assert (magic_file != NULL);
328 c = getc_unlocked (magic_file);
333 ungetc (c, magic_file);
343 ungetc (c, magic_file);
344 indent = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
349 c = getc_unlocked (magic_file);
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);
365 if (matchlet->
offset == -1)
367 _xdg_mime_magic_matchlet_free (matchlet);
370 c = getc_unlocked (magic_file);
373 _xdg_mime_magic_matchlet_free (matchlet);
378 _xdg_mime_magic_matchlet_free (matchlet);
384 c = getc_unlocked (magic_file);
387 _xdg_mime_magic_matchlet_free (matchlet);
393 c = getc_unlocked (magic_file);
396 _xdg_mime_magic_matchlet_free (matchlet);
404 if (matchlet->
value == NULL)
406 _xdg_mime_magic_matchlet_free (matchlet);
412 _xdg_mime_magic_matchlet_free (matchlet);
413 if (feof (magic_file))
419 c = getc_unlocked (magic_file);
424 if (matchlet->
mask == NULL)
426 _xdg_mime_magic_matchlet_free (matchlet);
432 _xdg_mime_magic_matchlet_free (matchlet);
433 if (feof (magic_file))
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);
454 _xdg_mime_magic_matchlet_free (matchlet);
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);
470 _xdg_mime_magic_matchlet_free (matchlet);
473 c = getc_unlocked (magic_file);
485 _xdg_mime_magic_matchlet_free (matchlet);
515 _xdg_mime_magic_matchlet_free (matchlet);
530 int valid_matchlet = TRUE;
539 if ((matchlet->
value[j] & matchlet->
mask[j]) !=
540 ((((
unsigned char *) data)[j + i]) & matchlet->
mask[j]))
542 valid_matchlet = FALSE;
551 if (matchlet->
value[j] != ((
unsigned char *) data)[j + i])
553 valid_matchlet = FALSE;
570 while ((matchlet != NULL) && (matchlet->
indent == indent))
572 if (_xdg_mime_magic_matchlet_compare_to_data (matchlet, data, len))
574 if ((matchlet->
next == NULL) ||
578 if (_xdg_mime_magic_matchlet_compare_level (matchlet->
next,
587 matchlet = matchlet->
next;
589 while (matchlet && matchlet->
indent > indent);
600 return _xdg_mime_magic_matchlet_compare_level (match->
matchlet, data, len, 0);
623 while (list->
next != NULL)
647 _xdg_mime_magic_match_free (mime_magic->
match_list);
663 const char *mime_types[],
667 const char *mime_type;
675 if (_xdg_mime_magic_match_compare_to_data (match, data, len))
683 for (n = 0; n < n_mime_types; n++)
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)
717 for (matchlet = match->
matchlet; matchlet; matchlet = matchlet->
next)
722 if (max_extent < extent)
736 if ((matchlets == NULL) || (matchlets->
next == NULL))
747 matchlet->
next = new_list;
756_xdg_mime_magic_read_magic_file (
XdgMimeMagic *mime_magic,
769 match = _xdg_mime_magic_match_new ();
770 state = _xdg_mime_magic_parse_header (magic_file, match);
772 _xdg_mime_magic_match_free (match);
775 state = _xdg_mime_magic_parse_magic_line (magic_file, match);
780 _xdg_mime_magic_insert_match (mime_magic, match);
783 _xdg_mime_magic_match_free (match);
786 state = _xdg_mime_magic_parse_error (magic_file);
794 _xdg_mime_update_mime_magic_extents (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);
XdgMimeMagicMatchlet * matchlet
XdgMimeMagicMatchlet * next
unsigned int value_length
unsigned int range_length
XdgMimeMagicMatch * match_list
int _xdg_mime_mime_type_equal(const char *mime_a, const char *mime_b)
unsigned int xdg_uint32_t
unsigned short xdg_uint16_t
void _xdg_mime_magic_read_from_file(XdgMimeMagic *mime_magic, const char *file_name)
XdgMimeMagic * _xdg_mime_magic_new(void)
int _xdg_mime_magic_get_buffer_extents(XdgMimeMagic *mime_magic)
void _xdg_mime_magic_free(XdgMimeMagic *mime_magic)
const char * _xdg_mime_magic_lookup_data(XdgMimeMagic *mime_magic, const void *data, size_t len, int *result_prio, const char *mime_types[], int n_mime_types)