xdgmimemagic.c 18 KB

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