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