~ubuntu-branches/ubuntu/trusty/pcmanfm/trusty-proposed

« back to all changes in this revision

Viewing changes to src/xdgmime/xdgmimemagic.c

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Lee
  • Date: 2008-09-26 10:19:20 UTC
  • mfrom: (4.1.5 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080926101920-cfldybkmwgwrtv9u
Tags: 0.5-3
* Correct spellings,  03_correct_spelling.dpatch (Closes:498794) 
* Code in some files are taken from other projects, added these
  informations into copyright file. (Closes:499678)
* Applied 04_defaut_terminal.dpatch to support x-terminal-emulator
  alternative. (Closes:497494) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: C; c-file-style: "gnu" -*- */
2
 
/* xdgmimemagic.: Private file.  Datastructure for storing magic files.
3
 
 *
4
 
 * More info can be found at http://www.freedesktop.org/standards/
5
 
 *
6
 
 * Copyright (C) 2003  Red Hat, Inc.
7
 
 * Copyright (C) 2003  Jonathan Blandford <jrb@alum.mit.edu>
8
 
 *
9
 
 * Licensed under the Academic Free License version 2.0
10
 
 * Or under the following terms:
11
 
 *
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.
16
 
 *
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.
21
 
 *
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.
26
 
 */
27
 
 
28
 
#ifdef HAVE_CONFIG_H
29
 
#include <config.h>
30
 
#endif
31
 
 
32
 
#include <assert.h>
33
 
#include "xdgmimemagic.h"
34
 
#include "xdgmimeint.h"
35
 
#include <stdio.h>
36
 
#include <stdlib.h>
37
 
#include <string.h>
38
 
#include <ctype.h>
39
 
#include <errno.h>
40
 
#include <limits.h>
41
 
 
42
 
#ifndef FALSE
43
 
#define FALSE   (0)
44
 
#endif
45
 
 
46
 
#ifndef TRUE
47
 
#define TRUE    (!FALSE)
48
 
#endif
49
 
 
50
 
extern int errno;
51
 
 
52
 
typedef struct XdgMimeMagicMatch XdgMimeMagicMatch;
53
 
typedef struct XdgMimeMagicMatchlet XdgMimeMagicMatchlet;
54
 
 
55
 
typedef enum
56
 
{
57
 
  XDG_MIME_MAGIC_SECTION,
58
 
  XDG_MIME_MAGIC_MAGIC,
59
 
  XDG_MIME_MAGIC_ERROR,
60
 
  XDG_MIME_MAGIC_EOF
61
 
} XdgMimeMagicState;
62
 
 
63
 
struct XdgMimeMagicMatch
64
 
{
65
 
  const char *mime_type;
66
 
  int priority;
67
 
  XdgMimeMagicMatchlet *matchlet;
68
 
  XdgMimeMagicMatch *next;
69
 
};
70
 
 
71
 
 
72
 
struct XdgMimeMagicMatchlet
73
 
{
74
 
  int indent;
75
 
  int offset;
76
 
  unsigned int value_length;
77
 
  unsigned char *value;
78
 
  unsigned char *mask;
79
 
  unsigned int range_length;
80
 
  unsigned int word_size;
81
 
  XdgMimeMagicMatchlet *next;
82
 
};
83
 
 
84
 
 
85
 
struct XdgMimeMagic
86
 
{
87
 
  XdgMimeMagicMatch *match_list;
88
 
  int max_extent;
89
 
};
90
 
 
91
 
static XdgMimeMagicMatch *
92
 
_xdg_mime_magic_match_new (void)
93
 
{
94
 
  return calloc (1, sizeof (XdgMimeMagicMatch));
95
 
}
96
 
 
97
 
 
98
 
static XdgMimeMagicMatchlet *
99
 
_xdg_mime_magic_matchlet_new (void)
100
 
{
101
 
  XdgMimeMagicMatchlet *matchlet;
102
 
 
103
 
  matchlet = malloc (sizeof (XdgMimeMagicMatchlet));
104
 
 
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;
113
 
 
114
 
  return matchlet;
115
 
}
116
 
 
117
 
 
118
 
static void
119
 
_xdg_mime_magic_matchlet_free (XdgMimeMagicMatchlet *mime_magic_matchlet)
120
 
{
121
 
  if (mime_magic_matchlet)
122
 
    {
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);
130
 
    }
131
 
}
132
 
 
133
 
 
134
 
/* Frees mime_magic_match and the remainder of its list
135
 
 */
136
 
static void
137
 
_xdg_mime_magic_match_free (XdgMimeMagicMatch *mime_magic_match)
138
 
{
139
 
  XdgMimeMagicMatch *ptr, *next;
140
 
 
141
 
  ptr = mime_magic_match;
142
 
  while (ptr)
143
 
    {
144
 
      next = ptr->next;
145
 
 
146
 
      if (ptr->mime_type)
147
 
        free ((void *) ptr->mime_type);
148
 
      if (ptr->matchlet)
149
 
        _xdg_mime_magic_matchlet_free (ptr->matchlet);
150
 
      free (ptr);
151
 
 
152
 
      ptr = next;
153
 
    }
154
 
}
155
 
 
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.
158
 
 */
159
 
static unsigned char *
160
 
_xdg_mime_magic_read_to_newline (FILE *magic_file,
161
 
                                 int  *end_of_file)
162
 
{
163
 
  unsigned char *retval;
164
 
  int c;
165
 
  int len, pos;
166
 
 
167
 
  len = 128;
168
 
  pos = 0;
169
 
  retval = malloc (len);
170
 
  *end_of_file = FALSE;
171
 
 
172
 
  while (TRUE)
173
 
    {
174
 
      c = getc_unlocked (magic_file);
175
 
      if (c == EOF)
176
 
        {
177
 
          *end_of_file = TRUE;
178
 
          break;
179
 
        }
180
 
      if (c == '\n' || c == '\000')
181
 
        break;
182
 
      retval[pos++] = (unsigned char) c;
183
 
      if (pos % 128 == 127)
184
 
        {
185
 
          len = len + 128;
186
 
          retval = realloc (retval, len);
187
 
        }
188
 
    }
189
 
 
190
 
  retval[pos] = '\000';
191
 
  return retval;
192
 
}
193
 
 
194
 
/* Returns the number read from the file, or -1 if no number could be read.
195
 
 */
196
 
static int
197
 
_xdg_mime_magic_read_a_number (FILE *magic_file,
198
 
                               int  *end_of_file)
199
 
{
200
 
  /* LONG_MAX is about 20 characters on my system */
201
 
#define MAX_NUMBER_SIZE 30
202
 
  char number_string[MAX_NUMBER_SIZE + 1];
203
 
  int pos = 0;
204
 
  int c;
205
 
  long retval = -1;
206
 
 
207
 
  while (TRUE)
208
 
    {
209
 
      c = getc_unlocked (magic_file);
210
 
 
211
 
      if (c == EOF)
212
 
        {
213
 
          *end_of_file = TRUE;
214
 
          break;
215
 
        }
216
 
      if (! isdigit (c))
217
 
        {
218
 
          ungetc (c, magic_file);
219
 
          break;
220
 
        }
221
 
      number_string[pos] = (char) c;
222
 
      pos++;
223
 
      if (pos == MAX_NUMBER_SIZE)
224
 
        break;
225
 
    }
226
 
  if (pos > 0)
227
 
    {
228
 
      number_string[pos] = '\000';
229
 
      errno = 0;
230
 
      retval = strtol (number_string, NULL, 10);
231
 
 
232
 
      if ((retval < INT_MIN) || (retval > INT_MAX) || (errno != 0))
233
 
        return -1;
234
 
    }
235
 
 
236
 
  return retval;
237
 
}
238
 
 
239
 
/* Headers are of the format:
240
 
 * [<priority>:<mime-type>]
241
 
 */
242
 
static XdgMimeMagicState
243
 
_xdg_mime_magic_parse_header (FILE *magic_file, XdgMimeMagicMatch *match)
244
 
{
245
 
  int c;
246
 
  char *buffer;
247
 
  char *end_ptr;
248
 
  int end_of_file = 0;
249
 
 
250
 
  assert (magic_file != NULL);
251
 
  assert (match != NULL);
252
 
 
253
 
  c = getc_unlocked (magic_file);
254
 
  if (c == EOF)
255
 
    return XDG_MIME_MAGIC_EOF;
256
 
  if (c != '[')
257
 
    return XDG_MIME_MAGIC_ERROR;
258
 
 
259
 
  match->priority = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
260
 
  if (end_of_file)
261
 
    return XDG_MIME_MAGIC_EOF;
262
 
  if (match->priority == -1)
263
 
    return XDG_MIME_MAGIC_ERROR;
264
 
 
265
 
  c = getc_unlocked (magic_file);
266
 
  if (c == EOF)
267
 
    return XDG_MIME_MAGIC_EOF;
268
 
  if (c != ':')
269
 
    return XDG_MIME_MAGIC_ERROR;
270
 
 
271
 
  buffer = (char *)_xdg_mime_magic_read_to_newline (magic_file, &end_of_file);
272
 
  if (end_of_file)
273
 
    return XDG_MIME_MAGIC_EOF;
274
 
 
275
 
  end_ptr = buffer;
276
 
  while (*end_ptr != ']' && *end_ptr != '\000' && *end_ptr != '\n')
277
 
    end_ptr++;
278
 
  if (*end_ptr != ']')
279
 
    {
280
 
      free (buffer);
281
 
      return XDG_MIME_MAGIC_ERROR;
282
 
    }
283
 
  *end_ptr = '\000';
284
 
 
285
 
  match->mime_type = strdup (buffer);
286
 
  free (buffer);
287
 
 
288
 
  return XDG_MIME_MAGIC_MAGIC;
289
 
}
290
 
 
291
 
static XdgMimeMagicState
292
 
_xdg_mime_magic_parse_error (FILE *magic_file)
293
 
{
294
 
  int c;
295
 
 
296
 
  while (1)
297
 
    {
298
 
      c = getc_unlocked (magic_file);
299
 
      if (c == EOF)
300
 
        return XDG_MIME_MAGIC_EOF;
301
 
      if (c == '\n')
302
 
        return XDG_MIME_MAGIC_SECTION;
303
 
    }
304
 
}
305
 
 
306
 
/* Headers are of the format:
307
 
 * [ indent ] ">" start-offset "=" value
308
 
 * [ "&" mask ] [ "~" word-size ] [ "+" range-length ] "\n"
309
 
 */
310
 
static XdgMimeMagicState
311
 
_xdg_mime_magic_parse_magic_line (FILE              *magic_file,
312
 
                                  XdgMimeMagicMatch *match)
313
 
{
314
 
  XdgMimeMagicMatchlet *matchlet;
315
 
  int c;
316
 
  int end_of_file;
317
 
  int indent = 0;
318
 
  int bytes_read;
319
 
 
320
 
  assert (magic_file != NULL);
321
 
 
322
 
  /* Sniff the buffer to make sure it's a valid line */
323
 
  c = getc_unlocked (magic_file);
324
 
  if (c == EOF)
325
 
    return XDG_MIME_MAGIC_EOF;
326
 
  else if (c == '[')
327
 
    {
328
 
      ungetc (c, magic_file);
329
 
      return XDG_MIME_MAGIC_SECTION;
330
 
    }
331
 
  else if (c == '\n')
332
 
    return XDG_MIME_MAGIC_MAGIC;
333
 
 
334
 
  /* At this point, it must be a digit or a '>' */
335
 
  end_of_file = FALSE;
336
 
  if (isdigit (c))
337
 
    {
338
 
      ungetc (c, magic_file);
339
 
      indent = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
340
 
      if (end_of_file)
341
 
        return XDG_MIME_MAGIC_EOF;
342
 
      if (indent == -1)
343
 
        return XDG_MIME_MAGIC_ERROR;
344
 
      c = getc_unlocked (magic_file);
345
 
      if (c == EOF)
346
 
        return XDG_MIME_MAGIC_EOF;
347
 
    }
348
 
 
349
 
  if (c != '>')
350
 
    return XDG_MIME_MAGIC_ERROR;
351
 
 
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);
355
 
  if (end_of_file)
356
 
    {
357
 
      _xdg_mime_magic_matchlet_free (matchlet);
358
 
      return XDG_MIME_MAGIC_EOF;
359
 
    }
360
 
  if (matchlet->offset == -1)
361
 
    {
362
 
      _xdg_mime_magic_matchlet_free (matchlet);
363
 
      return XDG_MIME_MAGIC_ERROR;
364
 
    }
365
 
  c = getc_unlocked (magic_file);
366
 
  if (c == EOF)
367
 
    {
368
 
      _xdg_mime_magic_matchlet_free (matchlet);
369
 
      return XDG_MIME_MAGIC_EOF;
370
 
    }
371
 
  else if (c != '=')
372
 
    {
373
 
      _xdg_mime_magic_matchlet_free (matchlet);
374
 
      return XDG_MIME_MAGIC_ERROR;
375
 
    }
376
 
 
377
 
  /* Next two bytes determine how long the value is */
378
 
  matchlet->value_length = 0;
379
 
  c = getc_unlocked (magic_file);
380
 
  if (c == EOF)
381
 
    {
382
 
      _xdg_mime_magic_matchlet_free (matchlet);
383
 
      return XDG_MIME_MAGIC_EOF;
384
 
    }
385
 
  matchlet->value_length = c & 0xFF;
386
 
  matchlet->value_length = matchlet->value_length << 8;
387
 
 
388
 
  c = getc_unlocked (magic_file);
389
 
  if (c == EOF)
390
 
    {
391
 
      _xdg_mime_magic_matchlet_free (matchlet);
392
 
      return XDG_MIME_MAGIC_EOF;
393
 
    }
394
 
  matchlet->value_length = matchlet->value_length + (c & 0xFF);
395
 
 
396
 
  matchlet->value = malloc (matchlet->value_length);
397
 
 
398
 
  /* OOM */
399
 
  if (matchlet->value == NULL)
400
 
    {
401
 
      _xdg_mime_magic_matchlet_free (matchlet);
402
 
      return XDG_MIME_MAGIC_ERROR;
403
 
    }
404
 
  bytes_read = fread (matchlet->value, 1, matchlet->value_length, magic_file);
405
 
  if (bytes_read != matchlet->value_length)
406
 
    {
407
 
      _xdg_mime_magic_matchlet_free (matchlet);
408
 
      if (feof (magic_file))
409
 
        return XDG_MIME_MAGIC_EOF;
410
 
      else
411
 
        return XDG_MIME_MAGIC_ERROR;
412
 
    }
413
 
 
414
 
  c = getc_unlocked (magic_file);
415
 
  if (c == '&')
416
 
    {
417
 
      matchlet->mask = malloc (matchlet->value_length);
418
 
      /* OOM */
419
 
      if (matchlet->mask == NULL)
420
 
        {
421
 
          _xdg_mime_magic_matchlet_free (matchlet);
422
 
          return XDG_MIME_MAGIC_ERROR;
423
 
        }
424
 
      bytes_read = fread (matchlet->mask, 1, matchlet->value_length, magic_file);
425
 
      if (bytes_read != matchlet->value_length)
426
 
        {
427
 
          _xdg_mime_magic_matchlet_free (matchlet);
428
 
          if (feof (magic_file))
429
 
            return XDG_MIME_MAGIC_EOF;
430
 
          else
431
 
            return XDG_MIME_MAGIC_ERROR;
432
 
        }
433
 
      c = getc_unlocked (magic_file);
434
 
    }
435
 
 
436
 
  if (c == '~')
437
 
    {
438
 
      matchlet->word_size = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
439
 
      if (end_of_file)
440
 
        {
441
 
          _xdg_mime_magic_matchlet_free (matchlet);
442
 
          return XDG_MIME_MAGIC_EOF;
443
 
        }
444
 
      if (matchlet->word_size != 0 &&
445
 
          matchlet->word_size != 1 &&
446
 
          matchlet->word_size != 2 &&
447
 
          matchlet->word_size != 4)
448
 
        {
449
 
          _xdg_mime_magic_matchlet_free (matchlet);
450
 
          return XDG_MIME_MAGIC_ERROR;
451
 
        }
452
 
      c = getc_unlocked (magic_file);
453
 
    }
454
 
 
455
 
  if (c == '+')
456
 
    {
457
 
      matchlet->range_length = _xdg_mime_magic_read_a_number (magic_file, &end_of_file);
458
 
      if (end_of_file)
459
 
        {
460
 
          _xdg_mime_magic_matchlet_free (matchlet);
461
 
          return XDG_MIME_MAGIC_EOF;
462
 
        }
463
 
      if (matchlet->range_length == -1)
464
 
        {
465
 
          _xdg_mime_magic_matchlet_free (matchlet);
466
 
          return XDG_MIME_MAGIC_ERROR;
467
 
        }
468
 
      c = getc_unlocked (magic_file);
469
 
    }
470
 
 
471
 
 
472
 
  if (c == '\n')
473
 
    {
474
 
      /* We clean up the matchlet, byte swapping if needed */
475
 
      if (matchlet->word_size > 1)
476
 
        {
477
 
          int i;
478
 
          if (matchlet->value_length % matchlet->word_size != 0)
479
 
            {
480
 
              _xdg_mime_magic_matchlet_free (matchlet);
481
 
              return XDG_MIME_MAGIC_ERROR;
482
 
            }
483
 
          /* FIXME: need to get this defined in a <config.h> style file */
484
 
#if LITTLE_ENDIAN
485
 
          for (i = 0; i < matchlet->value_length; i = i + matchlet->word_size)
486
 
            {
487
 
              if (matchlet->word_size == 2)
488
 
                *((xdg_uint16_t *) matchlet->value + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->value + i)));
489
 
              else if (matchlet->word_size == 4)
490
 
                *((xdg_uint32_t *) matchlet->value + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->value + i)));
491
 
              if (matchlet->mask)
492
 
                {
493
 
                  if (matchlet->word_size == 2)
494
 
                    *((xdg_uint16_t *) matchlet->mask + i) = SWAP_BE16_TO_LE16 (*((xdg_uint16_t *) (matchlet->mask + i)));
495
 
                  else if (matchlet->word_size == 4)
496
 
                    *((xdg_uint32_t *) matchlet->mask + i) = SWAP_BE32_TO_LE32 (*((xdg_uint32_t *) (matchlet->mask + i)));
497
 
 
498
 
                }
499
 
            }
500
 
#endif
501
 
        }
502
 
 
503
 
      matchlet->next = match->matchlet;
504
 
      match->matchlet = matchlet;
505
 
 
506
 
 
507
 
      return XDG_MIME_MAGIC_MAGIC;
508
 
    }
509
 
 
510
 
  _xdg_mime_magic_matchlet_free (matchlet);
511
 
  if (c == EOF)
512
 
    return XDG_MIME_MAGIC_EOF;
513
 
 
514
 
  return XDG_MIME_MAGIC_ERROR;
515
 
}
516
 
 
517
 
static int
518
 
_xdg_mime_magic_matchlet_compare_to_data (XdgMimeMagicMatchlet *matchlet,
519
 
                                          const void           *data,
520
 
                                          size_t                len)
521
 
{
522
 
  int i, j;
523
 
  for (i = matchlet->offset; i < matchlet->offset + matchlet->range_length; i++)
524
 
    {
525
 
      int valid_matchlet = TRUE;
526
 
 
527
 
      if (i + matchlet->value_length > len)
528
 
        return FALSE;
529
 
 
530
 
      if (matchlet->mask)
531
 
        {
532
 
          for (j = 0; j < matchlet->value_length; j++)
533
 
            {
534
 
              if ((matchlet->value[j] & matchlet->mask[j]) !=
535
 
                  ((((unsigned char *) data)[j + i]) & matchlet->mask[j]))
536
 
                {
537
 
                  valid_matchlet = FALSE;
538
 
                  break;
539
 
                }
540
 
            }
541
 
        }
542
 
      else
543
 
        {
544
 
          for (j = 0; j <  matchlet->value_length; j++)
545
 
            {
546
 
              if (matchlet->value[j] != ((unsigned char *) data)[j + i])
547
 
                {
548
 
                  valid_matchlet = FALSE;
549
 
                  break;
550
 
                }
551
 
            }
552
 
        }
553
 
      if (valid_matchlet)
554
 
        return TRUE;
555
 
    }
556
 
  return FALSE;
557
 
}
558
 
 
559
 
static int
560
 
_xdg_mime_magic_matchlet_compare_level (XdgMimeMagicMatchlet *matchlet,
561
 
                                        const void           *data,
562
 
                                        size_t                len,
563
 
                                        int                   indent)
564
 
{
565
 
  while ((matchlet != NULL) && (matchlet->indent == indent))
566
 
    {
567
 
      if (_xdg_mime_magic_matchlet_compare_to_data (matchlet, data, len))
568
 
        {
569
 
          if ((matchlet->next == NULL) ||
570
 
              (matchlet->next->indent <= indent))
571
 
            return TRUE;
572
 
 
573
 
          if (_xdg_mime_magic_matchlet_compare_level (matchlet->next,
574
 
                                                      data,
575
 
                                                      len,
576
 
                                                      indent + 1))
577
 
            return TRUE;
578
 
        }
579
 
 
580
 
      do
581
 
        {
582
 
          matchlet = matchlet->next;
583
 
        }
584
 
      while (matchlet && matchlet->indent > indent);
585
 
    }
586
 
 
587
 
  return FALSE;
588
 
}
589
 
 
590
 
static int
591
 
_xdg_mime_magic_match_compare_to_data (XdgMimeMagicMatch *match,
592
 
                                       const void        *data,
593
 
                                       size_t             len)
594
 
{
595
 
  return _xdg_mime_magic_matchlet_compare_level (match->matchlet, data, len, 0);
596
 
}
597
 
 
598
 
static void
599
 
_xdg_mime_magic_insert_match (XdgMimeMagic      *mime_magic,
600
 
                              XdgMimeMagicMatch *match)
601
 
{
602
 
  XdgMimeMagicMatch *list;
603
 
 
604
 
  if (mime_magic->match_list == NULL)
605
 
    {
606
 
      mime_magic->match_list = match;
607
 
      return;
608
 
    }
609
 
 
610
 
  if (match->priority > mime_magic->match_list->priority)
611
 
    {
612
 
      match->next = mime_magic->match_list;
613
 
      mime_magic->match_list = match;
614
 
      return;
615
 
    }
616
 
 
617
 
  list = mime_magic->match_list;
618
 
  while (list->next != NULL)
619
 
    {
620
 
      if (list->next->priority < match->priority)
621
 
        {
622
 
          match->next = list->next;
623
 
          list->next = match;
624
 
          return;
625
 
        }
626
 
      list = list->next;
627
 
    }
628
 
  list->next = match;
629
 
  match->next = NULL;
630
 
}
631
 
 
632
 
XdgMimeMagic *
633
 
_xdg_mime_magic_new (void)
634
 
{
635
 
  return calloc (1, sizeof (XdgMimeMagic));
636
 
}
637
 
 
638
 
void
639
 
_xdg_mime_magic_free (XdgMimeMagic *mime_magic)
640
 
{
641
 
  if (mime_magic) {
642
 
    _xdg_mime_magic_match_free (mime_magic->match_list);
643
 
    free (mime_magic);
644
 
  }
645
 
}
646
 
 
647
 
int
648
 
_xdg_mime_magic_get_buffer_extents (XdgMimeMagic *mime_magic)
649
 
{
650
 
  return mime_magic->max_extent;
651
 
}
652
 
 
653
 
const char *
654
 
_xdg_mime_magic_lookup_data (XdgMimeMagic *mime_magic,
655
 
                             const void   *data,
656
 
                             size_t        len,
657
 
                             const char   *mime_types[],
658
 
                             int           n_mime_types)
659
 
{
660
 
  XdgMimeMagicMatch *match;
661
 
  const char *mime_type;
662
 
  int n;
663
 
 
664
 
  mime_type = NULL;
665
 
  for (match = mime_magic->match_list; match; match = match->next)
666
 
    {
667
 
      if (_xdg_mime_magic_match_compare_to_data (match, data, len))
668
 
        {
669
 
          if ((mime_type == NULL) || (xdg_mime_mime_type_subclass (match->mime_type, mime_type))) {
670
 
            mime_type = match->mime_type;
671
 
          }
672
 
        }
673
 
      else 
674
 
        {
675
 
          for (n = 0; n < n_mime_types; n++)
676
 
            {
677
 
              if (mime_types[n] && 
678
 
                  xdg_mime_mime_type_equal (mime_types[n], match->mime_type))
679
 
                mime_types[n] = NULL;
680
 
            }
681
 
        }
682
 
    }
683
 
 
684
 
  if (mime_type == NULL)
685
 
    {
686
 
      for (n = 0; n < n_mime_types; n++)
687
 
        {
688
 
          if (mime_types[n])
689
 
            mime_type = mime_types[n];
690
 
        }
691
 
    }
692
 
 
693
 
  return mime_type;
694
 
}
695
 
 
696
 
static void
697
 
_xdg_mime_update_mime_magic_extents (XdgMimeMagic *mime_magic)
698
 
{
699
 
  XdgMimeMagicMatch *match;
700
 
  int max_extent = 0;
701
 
 
702
 
  for (match = mime_magic->match_list; match; match = match->next)
703
 
    {
704
 
      XdgMimeMagicMatchlet *matchlet;
705
 
 
706
 
      for (matchlet = match->matchlet; matchlet; matchlet = matchlet->next)
707
 
        {
708
 
          int extent;
709
 
 
710
 
          extent = matchlet->value_length + matchlet->offset + matchlet->range_length;
711
 
          if (max_extent < extent)
712
 
            max_extent = extent;
713
 
        }
714
 
    }
715
 
 
716
 
  mime_magic->max_extent = max_extent;
717
 
}
718
 
 
719
 
static XdgMimeMagicMatchlet *
720
 
_xdg_mime_magic_matchlet_mirror (XdgMimeMagicMatchlet *matchlets)
721
 
{
722
 
  XdgMimeMagicMatchlet *new_list;
723
 
  XdgMimeMagicMatchlet *tmp;
724
 
 
725
 
  if ((matchlets == NULL) || (matchlets->next == NULL))
726
 
    return matchlets;
727
 
 
728
 
  new_list = NULL;
729
 
  tmp = matchlets;
730
 
  while (tmp != NULL)
731
 
    {
732
 
      XdgMimeMagicMatchlet *matchlet;
733
 
 
734
 
      matchlet = tmp;
735
 
      tmp = tmp->next;
736
 
      matchlet->next = new_list;
737
 
      new_list = matchlet;
738
 
    }
739
 
 
740
 
  return new_list;
741
 
 
742
 
}
743
 
 
744
 
static void
745
 
_xdg_mime_magic_read_magic_file (XdgMimeMagic *mime_magic,
746
 
                                 FILE         *magic_file)
747
 
{
748
 
  XdgMimeMagicState state;
749
 
  XdgMimeMagicMatch *match = NULL; /* Quiet compiler */
750
 
 
751
 
  state = XDG_MIME_MAGIC_SECTION;
752
 
 
753
 
  while (state != XDG_MIME_MAGIC_EOF)
754
 
    {
755
 
      switch (state)
756
 
        {
757
 
        case XDG_MIME_MAGIC_SECTION:
758
 
          match = _xdg_mime_magic_match_new ();
759
 
          state = _xdg_mime_magic_parse_header (magic_file, match);
760
 
          if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
761
 
            _xdg_mime_magic_match_free (match);
762
 
          break;
763
 
        case XDG_MIME_MAGIC_MAGIC:
764
 
          state = _xdg_mime_magic_parse_magic_line (magic_file, match);
765
 
          if (state == XDG_MIME_MAGIC_SECTION ||
766
 
              (state == XDG_MIME_MAGIC_EOF && match->mime_type))
767
 
            {
768
 
              match->matchlet = _xdg_mime_magic_matchlet_mirror (match->matchlet);
769
 
              _xdg_mime_magic_insert_match (mime_magic, match);
770
 
            }
771
 
          else if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR)
772
 
            _xdg_mime_magic_match_free (match);
773
 
          break;
774
 
        case XDG_MIME_MAGIC_ERROR:
775
 
          state = _xdg_mime_magic_parse_error (magic_file);
776
 
          break;
777
 
        case XDG_MIME_MAGIC_EOF:
778
 
        default:
779
 
          /* Make the compiler happy */
780
 
          assert (0);
781
 
        }
782
 
    }
783
 
  _xdg_mime_update_mime_magic_extents (mime_magic);
784
 
}
785
 
 
786
 
void
787
 
_xdg_mime_magic_read_from_file (XdgMimeMagic *mime_magic,
788
 
                                const char   *file_name)
789
 
{
790
 
  FILE *magic_file;
791
 
  char header[12];
792
 
 
793
 
  magic_file = fopen (file_name, "r");
794
 
 
795
 
  if (magic_file == NULL)
796
 
    return;
797
 
 
798
 
  if (fread (header, 1, 12, magic_file) == 12)
799
 
    {
800
 
      if (memcmp ("MIME-Magic\0\n", header, 12) == 0)
801
 
        _xdg_mime_magic_read_magic_file (mime_magic, magic_file);
802
 
    }
803
 
 
804
 
  fclose (magic_file);
805
 
}