]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
c07d5b2d | 2 | * "$Id: type.c 177 2006-06-21 00:20:03Z jlovell $" |
ef416fc2 | 3 | * |
4 | * MIME typing routines for the Common UNIX Printing System (CUPS). | |
5 | * | |
fa73b229 | 6 | * Copyright 1997-2006 by Easy Software Products, all rights reserved. |
ef416fc2 | 7 | * |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
26 | * mimeAddType() - Add a MIME type to a database. | |
27 | * mimeAddTypeRule() - Add a detection rule for a file type. | |
28 | * mimeFileType() - Determine the type of a file. | |
29 | * mimeType() - Lookup a file type. | |
fa73b229 | 30 | * compare_types() - Compare two MIME super/type names. |
ef416fc2 | 31 | * checkrules() - Check each rule in a list. |
32 | * patmatch() - Pattern matching... | |
33 | */ | |
34 | ||
35 | /* | |
36 | * Include necessary headers... | |
37 | */ | |
38 | ||
39 | #include <stdio.h> | |
40 | #include <stdlib.h> | |
41 | #include <ctype.h> | |
42 | #include <locale.h> | |
43 | ||
44 | #include <cups/string.h> | |
45 | #include "mime.h" | |
46 | #include <cups/debug.h> | |
47 | ||
48 | ||
4400e98d | 49 | /* |
50 | * Local types... | |
51 | */ | |
52 | ||
53 | typedef struct _mime_filebuf_s /**** File buffer for MIME typing ****/ | |
54 | { | |
55 | cups_file_t *fp; /* File pointer */ | |
56 | int offset, /* Offset in file */ | |
57 | length; /* Length of buffered data */ | |
58 | unsigned char buffer[MIME_MAX_BUFFER];/* Buffered data */ | |
59 | } _mime_filebuf_t; | |
60 | ||
61 | ||
ef416fc2 | 62 | /* |
63 | * Local functions... | |
64 | */ | |
65 | ||
fa73b229 | 66 | static int compare_types(mime_type_t *t0, mime_type_t *t1); |
4400e98d | 67 | static int checkrules(const char *filename, _mime_filebuf_t *fb, |
68 | mime_magic_t *rules); | |
69 | static int patmatch(const char *s, const char *pat); | |
ef416fc2 | 70 | |
71 | ||
72 | /* | |
73 | * 'mimeAddType()' - Add a MIME type to a database. | |
74 | */ | |
75 | ||
fa73b229 | 76 | mime_type_t * /* O - New (or existing) MIME type */ |
77 | mimeAddType(mime_t *mime, /* I - MIME database */ | |
78 | const char *super, /* I - Super-type name */ | |
79 | const char *type) /* I - Type name */ | |
ef416fc2 | 80 | { |
fa73b229 | 81 | mime_type_t *temp; /* New MIME type */ |
ef416fc2 | 82 | |
83 | ||
84 | /* | |
85 | * Range check input... | |
86 | */ | |
87 | ||
fa73b229 | 88 | if (!mime || !super || !type) |
ef416fc2 | 89 | return (NULL); |
90 | ||
91 | /* | |
92 | * See if the type already exists; if so, return the existing type... | |
93 | */ | |
94 | ||
95 | if ((temp = mimeType(mime, super, type)) != NULL) | |
96 | return (temp); | |
97 | ||
98 | /* | |
99 | * The type doesn't exist; add it... | |
100 | */ | |
101 | ||
fa73b229 | 102 | if (!mime->types) |
103 | mime->types = cupsArrayNew((cups_array_func_t)compare_types, NULL); | |
ef416fc2 | 104 | |
fa73b229 | 105 | if (!mime->types) |
ef416fc2 | 106 | return (NULL); |
ef416fc2 | 107 | |
fa73b229 | 108 | if ((temp = calloc(1, sizeof(mime_type_t) - MIME_MAX_TYPE + |
109 | strlen(type) + 1)) == NULL) | |
110 | return (NULL); | |
ef416fc2 | 111 | |
ef416fc2 | 112 | strlcpy(temp->super, super, sizeof(temp->super)); |
fa73b229 | 113 | strcpy(temp->type, type); /* Safe: temp->type is allocated */ |
ef416fc2 | 114 | |
fa73b229 | 115 | cupsArrayAdd(mime->types, temp); |
ef416fc2 | 116 | |
117 | return (temp); | |
118 | } | |
119 | ||
120 | ||
121 | /* | |
122 | * 'mimeAddTypeRule()' - Add a detection rule for a file type. | |
123 | */ | |
124 | ||
125 | int /* O - 0 on success, -1 on failure */ | |
126 | mimeAddTypeRule(mime_type_t *mt, /* I - Type to add to */ | |
127 | const char *rule) /* I - Rule to add */ | |
128 | { | |
129 | int num_values, /* Number of values seen */ | |
130 | op, /* Operation code */ | |
131 | logic, /* Logic for next rule */ | |
132 | invert; /* Invert following rule? */ | |
133 | char name[255], /* Name in rule string */ | |
134 | value[3][255], /* Value in rule string */ | |
135 | *ptr, /* Position in name or value */ | |
136 | quote; /* Quote character */ | |
137 | int length[3]; /* Length of each parameter */ | |
138 | mime_magic_t *temp, /* New rule */ | |
139 | *current; /* Current rule */ | |
140 | ||
141 | ||
142 | /* | |
143 | * Range check input... | |
144 | */ | |
145 | ||
fa73b229 | 146 | if (!mt || !rule) |
ef416fc2 | 147 | return (-1); |
148 | ||
149 | /* | |
150 | * Find the last rule in the top-level of the rules tree. | |
151 | */ | |
152 | ||
fa73b229 | 153 | for (current = mt->rules; current; current = current->next) |
154 | if (!current->next) | |
ef416fc2 | 155 | break; |
156 | ||
157 | /* | |
158 | * Parse the rules string. Most rules are either a file extension or a | |
159 | * comparison function: | |
160 | * | |
161 | * extension | |
162 | * function(parameters) | |
163 | */ | |
164 | ||
165 | logic = MIME_MAGIC_NOP; | |
166 | invert = 0; | |
167 | ||
168 | DEBUG_printf(("%s/%s: %s\n", mt->super, mt->type, rule)); | |
169 | ||
170 | while (*rule != '\0') | |
171 | { | |
172 | while (isspace(*rule & 255)) | |
173 | rule ++; | |
174 | ||
175 | if (*rule == '(') | |
176 | { | |
177 | DEBUG_puts("new parenthesis group"); | |
178 | logic = MIME_MAGIC_NOP; | |
179 | rule ++; | |
180 | } | |
181 | else if (*rule == ')') | |
182 | { | |
183 | DEBUG_puts("close paren..."); | |
184 | if (current == NULL || current->parent == NULL) | |
185 | return (-1); | |
186 | ||
187 | current = current->parent; | |
188 | ||
189 | if (current->parent == NULL) | |
190 | logic = MIME_MAGIC_OR; | |
191 | else | |
192 | logic = current->parent->op; | |
193 | ||
194 | rule ++; | |
195 | } | |
196 | else if (*rule == '+' && current != NULL) | |
197 | { | |
198 | if (logic != MIME_MAGIC_AND && | |
4400e98d | 199 | current != NULL && current->prev != NULL && |
200 | current->prev->prev != NULL) | |
ef416fc2 | 201 | { |
202 | /* | |
203 | * OK, we have more than 1 rule in the current tree level... Make a | |
204 | * new group tree and move the previous rule to it... | |
205 | */ | |
206 | ||
207 | if ((temp = calloc(1, sizeof(mime_magic_t))) == NULL) | |
208 | return (-1); | |
209 | ||
210 | temp->op = MIME_MAGIC_AND; | |
211 | temp->child = current; | |
212 | temp->parent = current->parent; | |
213 | current->prev->next = temp; | |
214 | temp->prev = current->prev; | |
215 | ||
216 | current->prev = NULL; | |
217 | current->parent = temp; | |
218 | ||
219 | DEBUG_printf(("creating new AND group %p...\n", temp)); | |
220 | } | |
221 | else | |
222 | { | |
223 | DEBUG_printf(("setting group %p op to AND...\n", current->parent)); | |
224 | current->parent->op = MIME_MAGIC_AND; | |
225 | } | |
226 | ||
227 | logic = MIME_MAGIC_AND; | |
228 | rule ++; | |
229 | } | |
230 | else if (*rule == ',') | |
231 | { | |
232 | if (logic != MIME_MAGIC_OR && current != NULL) | |
233 | { | |
234 | /* | |
235 | * OK, we have two possibilities; either this is the top-level rule or | |
236 | * we have a bunch of AND rules at this level. | |
237 | */ | |
238 | ||
239 | if (current->parent == NULL) | |
240 | { | |
241 | /* | |
242 | * This is the top-level rule; we have to move *all* of the AND rules | |
243 | * down a level, as AND has precedence over OR. | |
244 | */ | |
245 | ||
246 | if ((temp = calloc(1, sizeof(mime_magic_t))) == NULL) | |
247 | return (-1); | |
248 | ||
249 | DEBUG_printf(("creating new AND group %p inside OR group\n", temp)); | |
250 | ||
251 | while (current->prev != NULL) | |
252 | { | |
253 | current->parent = temp; | |
254 | current = current->prev; | |
255 | } | |
256 | ||
257 | current->parent = temp; | |
258 | temp->op = MIME_MAGIC_AND; | |
259 | temp->child = current; | |
260 | ||
261 | mt->rules = current = temp; | |
262 | } | |
263 | else | |
264 | { | |
265 | /* | |
266 | * This isn't the top rule, so go up one level... | |
267 | */ | |
268 | ||
269 | DEBUG_puts("going up one level"); | |
270 | current = current->parent; | |
271 | } | |
272 | } | |
273 | ||
274 | logic = MIME_MAGIC_OR; | |
275 | rule ++; | |
276 | } | |
277 | else if (*rule == '!') | |
278 | { | |
279 | DEBUG_puts("NOT"); | |
280 | invert = 1; | |
281 | rule ++; | |
282 | } | |
283 | else if (isalnum(*rule & 255)) | |
284 | { | |
285 | /* | |
286 | * Read an extension name or a function... | |
287 | */ | |
288 | ||
4400e98d | 289 | ptr = name; |
290 | while (isalnum(*rule & 255) && (ptr - name) < (sizeof(name) - 1)) | |
ef416fc2 | 291 | *ptr++ = *rule++; |
292 | ||
293 | *ptr = '\0'; | |
294 | num_values = 0; | |
295 | ||
296 | if (*rule == '(') | |
297 | { | |
298 | /* | |
299 | * Read function parameters... | |
300 | */ | |
301 | ||
302 | rule ++; | |
303 | for (num_values = 0; | |
304 | num_values < (sizeof(value) / sizeof(value[0])); | |
305 | num_values ++) | |
306 | { | |
307 | ptr = value[num_values]; | |
308 | ||
309 | while ((ptr - value[num_values]) < (sizeof(value[0]) - 1) && | |
310 | *rule != '\0' && *rule != ',' && *rule != ')') | |
311 | { | |
312 | if (isspace(*rule & 255)) | |
313 | { | |
314 | /* | |
315 | * Ignore whitespace... | |
316 | */ | |
317 | ||
318 | rule ++; | |
319 | continue; | |
320 | } | |
321 | else if (*rule == '\"' || *rule == '\'') | |
322 | { | |
323 | /* | |
324 | * Copy quoted strings literally... | |
325 | */ | |
326 | ||
327 | quote = *rule++; | |
328 | ||
329 | while (*rule != '\0' && *rule != quote && | |
330 | (ptr - value[num_values]) < (sizeof(value[0]) - 1)) | |
331 | *ptr++ = *rule++; | |
332 | ||
333 | if (*rule == quote) | |
334 | rule ++; | |
335 | else | |
336 | return (-1); | |
337 | } | |
338 | else if (*rule == '<') | |
339 | { | |
340 | rule ++; | |
341 | ||
342 | while (*rule != '>' && *rule != '\0' && | |
343 | (ptr - value[num_values]) < (sizeof(value[0]) - 1)) | |
344 | { | |
345 | if (isxdigit(rule[0] & 255) && isxdigit(rule[1] & 255)) | |
346 | { | |
347 | if (isdigit(*rule)) | |
348 | *ptr = (*rule++ - '0') << 4; | |
349 | else | |
350 | *ptr = (tolower(*rule++) - 'a' + 10) << 4; | |
351 | ||
352 | if (isdigit(*rule)) | |
353 | *ptr++ |= *rule++ - '0'; | |
354 | else | |
355 | *ptr++ |= tolower(*rule++) - 'a' + 10; | |
356 | } | |
357 | else | |
358 | return (-1); | |
359 | } | |
360 | ||
361 | if (*rule == '>') | |
362 | rule ++; | |
363 | else | |
364 | return (-1); | |
365 | } | |
366 | else | |
367 | *ptr++ = *rule++; | |
368 | } | |
369 | ||
370 | *ptr = '\0'; | |
371 | length[num_values] = ptr - value[num_values]; | |
372 | ||
373 | if (*rule != ',') | |
374 | break; | |
375 | ||
376 | rule ++; | |
377 | } | |
378 | ||
379 | if (*rule != ')') | |
380 | return (-1); | |
381 | ||
382 | rule ++; | |
383 | ||
384 | /* | |
385 | * Figure out the function... | |
386 | */ | |
387 | ||
fa73b229 | 388 | if (!strcmp(name, "match")) |
ef416fc2 | 389 | op = MIME_MAGIC_MATCH; |
fa73b229 | 390 | else if (!strcmp(name, "ascii")) |
ef416fc2 | 391 | op = MIME_MAGIC_ASCII; |
fa73b229 | 392 | else if (!strcmp(name, "printable")) |
ef416fc2 | 393 | op = MIME_MAGIC_PRINTABLE; |
fa73b229 | 394 | else if (!strcmp(name, "string")) |
ef416fc2 | 395 | op = MIME_MAGIC_STRING; |
fa73b229 | 396 | else if (!strcmp(name, "istring")) |
ef416fc2 | 397 | op = MIME_MAGIC_ISTRING; |
fa73b229 | 398 | else if (!strcmp(name, "char")) |
ef416fc2 | 399 | op = MIME_MAGIC_CHAR; |
fa73b229 | 400 | else if (!strcmp(name, "short")) |
ef416fc2 | 401 | op = MIME_MAGIC_SHORT; |
fa73b229 | 402 | else if (!strcmp(name, "int")) |
ef416fc2 | 403 | op = MIME_MAGIC_INT; |
fa73b229 | 404 | else if (!strcmp(name, "locale")) |
ef416fc2 | 405 | op = MIME_MAGIC_LOCALE; |
fa73b229 | 406 | else if (!strcmp(name, "contains")) |
ef416fc2 | 407 | op = MIME_MAGIC_CONTAINS; |
408 | else | |
409 | return (-1); | |
410 | } | |
411 | else | |
412 | { | |
413 | /* | |
414 | * This is just a filename match on the extension... | |
415 | */ | |
416 | ||
417 | snprintf(value[0], sizeof(value[0]), "*.%s", name); | |
418 | length[0] = strlen(value[0]); | |
419 | num_values = 1; | |
420 | op = MIME_MAGIC_MATCH; | |
421 | } | |
422 | ||
423 | /* | |
424 | * Add a rule for this operation. | |
425 | */ | |
426 | ||
427 | if ((temp = calloc(1, sizeof(mime_magic_t))) == NULL) | |
428 | return (-1); | |
429 | ||
430 | temp->invert = invert; | |
431 | if (current != NULL) | |
432 | { | |
433 | temp->parent = current->parent; | |
434 | current->next = temp; | |
435 | } | |
436 | else | |
437 | mt->rules = temp; | |
438 | ||
439 | temp->prev = current; | |
440 | ||
441 | if (logic == MIME_MAGIC_NOP) | |
442 | { | |
443 | /* | |
444 | * Add parenthetical grouping... | |
445 | */ | |
446 | ||
447 | DEBUG_printf(("making new OR group %p for parenthesis...\n", temp)); | |
448 | ||
449 | temp->op = MIME_MAGIC_OR; | |
450 | ||
451 | if ((temp->child = calloc(1, sizeof(mime_magic_t))) == NULL) | |
452 | return (-1); | |
453 | ||
454 | temp->child->parent = temp; | |
455 | ||
456 | temp = temp->child; | |
457 | logic = MIME_MAGIC_OR; | |
458 | } | |
459 | ||
460 | DEBUG_printf(("adding %p: %s, op = %d, logic = %d, invert = %d\n", | |
461 | temp, name, op, logic, invert)); | |
462 | ||
463 | /* | |
464 | * Fill in data for the rule... | |
465 | */ | |
466 | ||
467 | current = temp; | |
468 | temp->op = op; | |
469 | invert = 0; | |
470 | ||
471 | switch (op) | |
472 | { | |
473 | case MIME_MAGIC_MATCH : | |
474 | if (length[0] > (sizeof(temp->value.matchv) - 1)) | |
475 | return (-1); | |
476 | strcpy(temp->value.matchv, value[0]); | |
477 | break; | |
478 | case MIME_MAGIC_ASCII : | |
479 | case MIME_MAGIC_PRINTABLE : | |
480 | temp->offset = strtol(value[0], NULL, 0); | |
481 | temp->length = strtol(value[1], NULL, 0); | |
482 | if (temp->length > MIME_MAX_BUFFER) | |
483 | temp->length = MIME_MAX_BUFFER; | |
484 | break; | |
485 | case MIME_MAGIC_STRING : | |
486 | case MIME_MAGIC_ISTRING : | |
487 | temp->offset = strtol(value[0], NULL, 0); | |
488 | if (length[1] > sizeof(temp->value.stringv)) | |
489 | return (-1); | |
490 | temp->length = length[1]; | |
491 | memcpy(temp->value.stringv, value[1], length[1]); | |
492 | break; | |
493 | case MIME_MAGIC_CHAR : | |
494 | temp->offset = strtol(value[0], NULL, 0); | |
495 | if (length[1] == 1) | |
496 | temp->value.charv = value[1][0]; | |
497 | else | |
498 | temp->value.charv = (char)strtol(value[1], NULL, 0); | |
499 | break; | |
500 | case MIME_MAGIC_SHORT : | |
501 | temp->offset = strtol(value[0], NULL, 0); | |
502 | temp->value.shortv = (short)strtol(value[1], NULL, 0); | |
503 | break; | |
504 | case MIME_MAGIC_INT : | |
505 | temp->offset = strtol(value[0], NULL, 0); | |
506 | temp->value.intv = (int)strtol(value[1], NULL, 0); | |
507 | break; | |
508 | case MIME_MAGIC_LOCALE : | |
509 | if (length[0] > (sizeof(temp->value.localev) - 1)) | |
510 | return (-1); | |
511 | ||
512 | strcpy(temp->value.localev, value[0]); | |
513 | break; | |
514 | case MIME_MAGIC_CONTAINS : | |
515 | temp->offset = strtol(value[0], NULL, 0); | |
516 | temp->region = strtol(value[1], NULL, 0); | |
517 | if (length[2] > sizeof(temp->value.stringv)) | |
518 | return (-1); | |
519 | temp->length = length[2]; | |
520 | memcpy(temp->value.stringv, value[2], length[2]); | |
521 | break; | |
522 | } | |
523 | } | |
524 | else | |
525 | break; | |
526 | } | |
527 | ||
528 | return (0); | |
529 | } | |
530 | ||
531 | ||
532 | /* | |
533 | * 'mimeFileType()' - Determine the type of a file. | |
534 | */ | |
535 | ||
536 | mime_type_t * /* O - Type of file */ | |
537 | mimeFileType(mime_t *mime, /* I - MIME database */ | |
4400e98d | 538 | const char *pathname, /* I - Name of file to check on disk */ |
539 | const char *filename, /* I - Original filename or NULL */ | |
ef416fc2 | 540 | int *compression) /* O - Is the file compressed? */ |
541 | { | |
4400e98d | 542 | _mime_filebuf_t fb; /* File buffer */ |
543 | const char *base; /* Base filename of file */ | |
544 | mime_type_t *type; /* File type */ | |
ef416fc2 | 545 | |
546 | ||
b423cd4c | 547 | DEBUG_printf(("mimeFileType(mime=%p, pathname=\"%s\", filename=\"%s\", " |
4400e98d | 548 | "compression=%p)\n", |
549 | mime, pathname ? pathname : "(nil)", | |
550 | filename ? filename : "(nil)", | |
b423cd4c | 551 | compression)); |
ef416fc2 | 552 | |
553 | /* | |
554 | * Range check input parameters... | |
555 | */ | |
556 | ||
fa73b229 | 557 | if (!mime || !pathname) |
ef416fc2 | 558 | return (NULL); |
559 | ||
560 | /* | |
561 | * Try to open the file... | |
562 | */ | |
563 | ||
4400e98d | 564 | if ((fb.fp = cupsFileOpen(pathname, "r")) == NULL) |
ef416fc2 | 565 | return (NULL); |
566 | ||
4400e98d | 567 | fb.offset = -1; |
568 | fb.length = 0; | |
569 | ||
ef416fc2 | 570 | /* |
4400e98d | 571 | * Figure out the base filename (without directory portion)... |
ef416fc2 | 572 | */ |
573 | ||
4400e98d | 574 | if (filename) |
575 | { | |
576 | if ((base = strrchr(filename, '/')) != NULL) | |
577 | base ++; | |
578 | else | |
579 | filename = filename; | |
580 | } | |
581 | else if ((base = strrchr(pathname, '/')) != NULL) | |
582 | base ++; | |
ef416fc2 | 583 | else |
584 | filename = pathname; | |
585 | ||
586 | /* | |
587 | * Then check it against all known types... | |
588 | */ | |
589 | ||
fa73b229 | 590 | for (type = (mime_type_t *)cupsArrayFirst(mime->types); |
591 | type; | |
592 | type = (mime_type_t *)cupsArrayNext(mime->types)) | |
4400e98d | 593 | if (checkrules(base, &fb, type->rules)) |
ef416fc2 | 594 | break; |
595 | ||
596 | /* | |
597 | * Finally, close the file and return a match (if any)... | |
598 | */ | |
599 | ||
600 | if (compression) | |
4400e98d | 601 | *compression = cupsFileCompression(fb.fp); |
ef416fc2 | 602 | |
4400e98d | 603 | cupsFileClose(fb.fp); |
ef416fc2 | 604 | |
fa73b229 | 605 | return (type); |
ef416fc2 | 606 | } |
607 | ||
608 | ||
609 | /* | |
610 | * 'mimeType()' - Lookup a file type. | |
611 | */ | |
612 | ||
fa73b229 | 613 | mime_type_t * /* O - Matching file type definition */ |
614 | mimeType(mime_t *mime, /* I - MIME database */ | |
615 | const char *super, /* I - Super-type name */ | |
616 | const char *type) /* I - Type name */ | |
ef416fc2 | 617 | { |
fa73b229 | 618 | mime_type_t key; /* MIME type search key*/ |
619 | ||
ef416fc2 | 620 | |
621 | /* | |
622 | * Range check input... | |
623 | */ | |
624 | ||
fa73b229 | 625 | if (!mime || !super || !type) |
ef416fc2 | 626 | return (NULL); |
627 | ||
628 | /* | |
629 | * Lookup the type in the array... | |
630 | */ | |
631 | ||
632 | strlcpy(key.super, super, sizeof(key.super)); | |
fa73b229 | 633 | strlcpy(key.type, type, sizeof(key.type)); |
ef416fc2 | 634 | |
fa73b229 | 635 | return ((mime_type_t *)cupsArrayFind(mime->types, &key)); |
ef416fc2 | 636 | } |
637 | ||
638 | ||
639 | /* | |
fa73b229 | 640 | * 'compare_types()' - Compare two MIME super/type names. |
ef416fc2 | 641 | */ |
642 | ||
fa73b229 | 643 | static int /* O - Result of comparison */ |
644 | compare_types(mime_type_t *t0, /* I - First type */ | |
645 | mime_type_t *t1) /* I - Second type */ | |
ef416fc2 | 646 | { |
fa73b229 | 647 | int i; /* Result of comparison */ |
ef416fc2 | 648 | |
649 | ||
fa73b229 | 650 | if ((i = strcmp(t0->super, t1->super)) == 0) |
651 | i = strcmp(t0->type, t1->type); | |
ef416fc2 | 652 | |
653 | return (i); | |
654 | } | |
655 | ||
656 | ||
657 | /* | |
658 | * 'checkrules()' - Check each rule in a list. | |
659 | */ | |
660 | ||
661 | static int /* O - 1 if match, 0 if no match */ | |
4400e98d | 662 | checkrules(const char *filename, /* I - Filename */ |
663 | _mime_filebuf_t *fb, /* I - File to check */ | |
664 | mime_magic_t *rules) /* I - Rules to check */ | |
ef416fc2 | 665 | { |
666 | int n; /* Looping var */ | |
667 | int region; /* Region to look at */ | |
668 | int logic, /* Logic to apply */ | |
669 | result, /* Result of test */ | |
670 | intv; /* Integer value */ | |
671 | short shortv; /* Short value */ | |
4400e98d | 672 | unsigned char *bufptr; /* Pointer into buffer */ |
ef416fc2 | 673 | #ifdef DEBUG |
674 | const char * const debug_tests[] = /* Test names... */ | |
675 | { | |
676 | "NOP", /* No operation */ | |
677 | "AND", /* Logical AND of all children */ | |
678 | "OR", /* Logical OR of all children */ | |
679 | "MATCH", /* Filename match */ | |
680 | "ASCII", /* ASCII characters in range */ | |
4400e98d | 681 | "PRINTABLE", /* Printable characters (32-255) */ |
ef416fc2 | 682 | "STRING", /* String matches */ |
683 | "CHAR", /* Character/byte matches */ | |
684 | "SHORT", /* Short/16-bit word matches */ | |
685 | "INT", /* Integer/32-bit word matches */ | |
686 | "LOCALE" /* Current locale matches string */ | |
687 | "CONTAINS" /* File contains a string */ | |
688 | "ISTRING" /* Case-insensitive string matches */ | |
689 | }; | |
690 | #endif /* DEBUG */ | |
691 | ||
692 | ||
b423cd4c | 693 | DEBUG_printf(("checkrules(filename=\"%s\", fb=%p, rules=%p)\n", filename, |
694 | fb, rules)); | |
ef416fc2 | 695 | |
696 | if (rules == NULL) | |
697 | return (0); | |
698 | ||
699 | if (rules->parent == NULL) | |
700 | logic = MIME_MAGIC_OR; | |
701 | else | |
702 | logic = rules->parent->op; | |
703 | ||
4400e98d | 704 | result = 0; |
ef416fc2 | 705 | |
706 | while (rules != NULL) | |
707 | { | |
708 | /* | |
709 | * Compute the result of this rule... | |
710 | */ | |
711 | ||
712 | switch (rules->op) | |
713 | { | |
714 | case MIME_MAGIC_MATCH : | |
715 | result = patmatch(filename, rules->value.matchv); | |
716 | break; | |
717 | ||
718 | case MIME_MAGIC_ASCII : | |
719 | /* | |
720 | * Load the buffer if necessary... | |
721 | */ | |
722 | ||
4400e98d | 723 | if (fb->offset < 0 || rules->offset < fb->offset || |
724 | (rules->offset + rules->length) > (fb->offset + fb->length)) | |
ef416fc2 | 725 | { |
726 | /* | |
727 | * Reload file buffer... | |
728 | */ | |
729 | ||
4400e98d | 730 | cupsFileSeek(fb->fp, rules->offset); |
731 | fb->length = cupsFileRead(fb->fp, (char *)fb->buffer, | |
732 | sizeof(fb->buffer)); | |
733 | fb->offset = rules->offset; | |
ef416fc2 | 734 | } |
735 | ||
736 | /* | |
737 | * Test for ASCII printable characters plus standard control chars. | |
738 | */ | |
739 | ||
4400e98d | 740 | if ((rules->offset + rules->length) > (fb->offset + fb->length)) |
741 | n = fb->offset + fb->length - rules->offset; | |
ef416fc2 | 742 | else |
743 | n = rules->length; | |
744 | ||
4400e98d | 745 | bufptr = fb->buffer + rules->offset - fb->offset; |
ef416fc2 | 746 | while (n > 0) |
747 | if ((*bufptr >= 32 && *bufptr <= 126) || | |
748 | (*bufptr >= 8 && *bufptr <= 13) || | |
749 | *bufptr == 26 || *bufptr == 27) | |
750 | { | |
751 | n --; | |
752 | bufptr ++; | |
753 | } | |
754 | else | |
755 | break; | |
756 | ||
757 | result = (n == 0); | |
758 | break; | |
759 | ||
760 | case MIME_MAGIC_PRINTABLE : | |
761 | /* | |
762 | * Load the buffer if necessary... | |
763 | */ | |
764 | ||
4400e98d | 765 | if (fb->offset < 0 || rules->offset < fb->offset || |
766 | (rules->offset + rules->length) > (fb->offset + fb->length)) | |
ef416fc2 | 767 | { |
768 | /* | |
769 | * Reload file buffer... | |
770 | */ | |
771 | ||
4400e98d | 772 | cupsFileSeek(fb->fp, rules->offset); |
773 | fb->length = cupsFileRead(fb->fp, (char *)fb->buffer, | |
774 | sizeof(fb->buffer)); | |
775 | fb->offset = rules->offset; | |
ef416fc2 | 776 | } |
777 | ||
778 | /* | |
779 | * Test for 8-bit printable characters plus standard control chars. | |
780 | */ | |
781 | ||
4400e98d | 782 | if ((rules->offset + rules->length) > (fb->offset + fb->length)) |
783 | n = fb->offset + fb->length - rules->offset; | |
ef416fc2 | 784 | else |
785 | n = rules->length; | |
786 | ||
4400e98d | 787 | bufptr = fb->buffer + rules->offset - fb->offset; |
ef416fc2 | 788 | |
789 | while (n > 0) | |
790 | if (*bufptr >= 128 || | |
791 | (*bufptr >= 32 && *bufptr <= 126) || | |
792 | (*bufptr >= 8 && *bufptr <= 13) || | |
793 | *bufptr == 26 || *bufptr == 27) | |
794 | { | |
795 | n --; | |
796 | bufptr ++; | |
797 | } | |
798 | else | |
799 | break; | |
800 | ||
801 | result = (n == 0); | |
802 | break; | |
803 | ||
804 | case MIME_MAGIC_STRING : | |
805 | DEBUG_printf((" string(%d, \"%s\")\n", rules->offset, | |
806 | rules->value.stringv)); | |
807 | ||
808 | /* | |
809 | * Load the buffer if necessary... | |
810 | */ | |
811 | ||
4400e98d | 812 | if (fb->offset < 0 || rules->offset < fb->offset || |
813 | (rules->offset + rules->length) > (fb->offset + fb->length)) | |
ef416fc2 | 814 | { |
815 | /* | |
816 | * Reload file buffer... | |
817 | */ | |
818 | ||
4400e98d | 819 | cupsFileSeek(fb->fp, rules->offset); |
820 | fb->length = cupsFileRead(fb->fp, (char *)fb->buffer, | |
821 | sizeof(fb->buffer)); | |
822 | fb->offset = rules->offset; | |
ef416fc2 | 823 | |
4400e98d | 824 | DEBUG_printf((" loaded %d byte fb->buffer at %d, starts " |
825 | "with \"%c%c%c%c\"...\n", | |
826 | fb->length, fb->offset, fb->buffer[0], fb->buffer[1], | |
827 | fb->buffer[2], fb->buffer[3])); | |
ef416fc2 | 828 | } |
829 | ||
830 | /* | |
831 | * Compare the buffer against the string. If the file is too | |
832 | * short then don't compare - it can't match... | |
833 | */ | |
834 | ||
4400e98d | 835 | if ((rules->offset + rules->length) > (fb->offset + fb->length)) |
ef416fc2 | 836 | result = 0; |
837 | else | |
4400e98d | 838 | result = (memcmp(fb->buffer + rules->offset - fb->offset, |
ef416fc2 | 839 | rules->value.stringv, rules->length) == 0); |
840 | DEBUG_printf((" result=%d\n", result)); | |
841 | break; | |
842 | ||
843 | case MIME_MAGIC_ISTRING : | |
844 | /* | |
845 | * Load the buffer if necessary... | |
846 | */ | |
847 | ||
4400e98d | 848 | if (fb->offset < 0 || rules->offset < fb->offset || |
849 | (rules->offset + rules->length) > (fb->offset + fb->length)) | |
ef416fc2 | 850 | { |
851 | /* | |
852 | * Reload file buffer... | |
853 | */ | |
854 | ||
4400e98d | 855 | cupsFileSeek(fb->fp, rules->offset); |
856 | fb->length = cupsFileRead(fb->fp, (char *)fb->buffer, | |
857 | sizeof(fb->buffer)); | |
858 | fb->offset = rules->offset; | |
ef416fc2 | 859 | } |
860 | ||
861 | /* | |
862 | * Compare the buffer against the string. If the file is too | |
863 | * short then don't compare - it can't match... | |
864 | */ | |
865 | ||
4400e98d | 866 | if ((rules->offset + rules->length) > (fb->offset + fb->length)) |
ef416fc2 | 867 | result = 0; |
868 | else | |
4400e98d | 869 | result = (strncasecmp((char *)fb->buffer + rules->offset - |
870 | fb->offset, | |
ef416fc2 | 871 | rules->value.stringv, rules->length) == 0); |
872 | break; | |
873 | ||
874 | case MIME_MAGIC_CHAR : | |
875 | /* | |
876 | * Load the buffer if necessary... | |
877 | */ | |
878 | ||
4400e98d | 879 | if (fb->offset < 0 || rules->offset < fb->offset) |
ef416fc2 | 880 | { |
881 | /* | |
882 | * Reload file buffer... | |
883 | */ | |
884 | ||
4400e98d | 885 | cupsFileSeek(fb->fp, rules->offset); |
886 | fb->length = cupsFileRead(fb->fp, (char *)fb->buffer, | |
887 | sizeof(fb->buffer)); | |
888 | fb->offset = rules->offset; | |
ef416fc2 | 889 | } |
890 | ||
891 | /* | |
892 | * Compare the character values; if the file is too short, it | |
893 | * can't match... | |
894 | */ | |
895 | ||
4400e98d | 896 | if (fb->length < 1) |
ef416fc2 | 897 | result = 0; |
898 | else | |
4400e98d | 899 | result = (fb->buffer[rules->offset - fb->offset] == |
900 | rules->value.charv); | |
ef416fc2 | 901 | break; |
902 | ||
903 | case MIME_MAGIC_SHORT : | |
904 | /* | |
905 | * Load the buffer if necessary... | |
906 | */ | |
907 | ||
4400e98d | 908 | if (fb->offset < 0 || rules->offset < fb->offset || |
909 | (rules->offset + 2) > (fb->offset + fb->length)) | |
ef416fc2 | 910 | { |
911 | /* | |
912 | * Reload file buffer... | |
913 | */ | |
914 | ||
4400e98d | 915 | cupsFileSeek(fb->fp, rules->offset); |
916 | fb->length = cupsFileRead(fb->fp, (char *)fb->buffer, | |
917 | sizeof(fb->buffer)); | |
918 | fb->offset = rules->offset; | |
ef416fc2 | 919 | } |
920 | ||
921 | /* | |
922 | * Compare the short values; if the file is too short, it | |
923 | * can't match... | |
924 | */ | |
925 | ||
4400e98d | 926 | if (fb->length < 2) |
ef416fc2 | 927 | result = 0; |
928 | else | |
929 | { | |
4400e98d | 930 | bufptr = fb->buffer + rules->offset - fb->offset; |
ef416fc2 | 931 | shortv = (bufptr[0] << 8) | bufptr[1]; |
932 | result = (shortv == rules->value.shortv); | |
933 | } | |
934 | break; | |
935 | ||
936 | case MIME_MAGIC_INT : | |
937 | /* | |
938 | * Load the buffer if necessary... | |
939 | */ | |
940 | ||
4400e98d | 941 | if (fb->offset < 0 || rules->offset < fb->offset || |
942 | (rules->offset + 4) > (fb->offset + fb->length)) | |
ef416fc2 | 943 | { |
944 | /* | |
945 | * Reload file buffer... | |
946 | */ | |
947 | ||
4400e98d | 948 | cupsFileSeek(fb->fp, rules->offset); |
949 | fb->length = cupsFileRead(fb->fp, (char *)fb->buffer, | |
950 | sizeof(fb->buffer)); | |
951 | fb->offset = rules->offset; | |
ef416fc2 | 952 | } |
953 | ||
954 | /* | |
955 | * Compare the int values; if the file is too short, it | |
956 | * can't match... | |
957 | */ | |
958 | ||
4400e98d | 959 | if (fb->length < 4) |
ef416fc2 | 960 | result = 0; |
961 | else | |
962 | { | |
4400e98d | 963 | bufptr = fb->buffer + rules->offset - fb->offset; |
964 | intv = (((((bufptr[0] << 8) | bufptr[1]) << 8) | | |
965 | bufptr[2]) << 8) | bufptr[3]; | |
ef416fc2 | 966 | result = (intv == rules->value.intv); |
967 | } | |
968 | break; | |
969 | ||
970 | case MIME_MAGIC_LOCALE : | |
971 | #if defined(WIN32) || defined(__EMX__) || defined(__APPLE__) | |
4400e98d | 972 | result = (strcmp(rules->value.localev, |
973 | setlocale(LC_ALL, "")) == 0); | |
ef416fc2 | 974 | #else |
4400e98d | 975 | result = (strcmp(rules->value.localev, |
976 | setlocale(LC_MESSAGES, "")) == 0); | |
ef416fc2 | 977 | #endif /* __APPLE__ */ |
978 | break; | |
979 | ||
980 | case MIME_MAGIC_CONTAINS : | |
981 | /* | |
982 | * Load the buffer if necessary... | |
983 | */ | |
984 | ||
4400e98d | 985 | if (fb->offset < 0 || rules->offset < fb->offset || |
986 | (rules->offset + rules->region) > (fb->offset + fb->length)) | |
ef416fc2 | 987 | { |
988 | /* | |
989 | * Reload file buffer... | |
990 | */ | |
991 | ||
4400e98d | 992 | cupsFileSeek(fb->fp, rules->offset); |
993 | fb->length = cupsFileRead(fb->fp, (char *)fb->buffer, | |
994 | sizeof(fb->buffer)); | |
995 | fb->offset = rules->offset; | |
ef416fc2 | 996 | } |
997 | ||
998 | /* | |
999 | * Compare the buffer against the string. If the file is too | |
1000 | * short then don't compare - it can't match... | |
1001 | */ | |
1002 | ||
4400e98d | 1003 | if ((rules->offset + rules->length) > (fb->offset + fb->length)) |
ef416fc2 | 1004 | result = 0; |
1005 | else | |
1006 | { | |
4400e98d | 1007 | if (fb->length > rules->region) |
ef416fc2 | 1008 | region = rules->region - rules->length; |
1009 | else | |
4400e98d | 1010 | region = fb->length - rules->length; |
ef416fc2 | 1011 | |
1012 | for (n = 0; n < region; n ++) | |
4400e98d | 1013 | if ((result = (memcmp(fb->buffer + rules->offset - fb->offset + n, |
1014 | rules->value.stringv, | |
1015 | rules->length) == 0)) != 0) | |
ef416fc2 | 1016 | break; |
1017 | } | |
1018 | break; | |
1019 | ||
1020 | default : | |
1021 | if (rules->child != NULL) | |
4400e98d | 1022 | result = checkrules(filename, fb, rules->child); |
ef416fc2 | 1023 | else |
1024 | result = 0; | |
1025 | break; | |
1026 | } | |
1027 | ||
1028 | /* | |
1029 | * If the logic is inverted, invert the result... | |
1030 | */ | |
1031 | ||
1032 | if (rules->invert) | |
1033 | result = !result; | |
1034 | ||
1035 | /* | |
1036 | * OK, now if the current logic is OR and this result is true, the this | |
1037 | * rule set is true. If the current logic is AND and this result is false, | |
1038 | * the the rule set is false... | |
1039 | */ | |
1040 | ||
1041 | DEBUG_printf((" result of test %p (MIME_MAGIC_%s) is %d\n", rules, | |
1042 | debug_tests[rules->op], result)); | |
1043 | ||
1044 | if ((result && logic == MIME_MAGIC_OR) || | |
1045 | (!result && logic == MIME_MAGIC_AND)) | |
1046 | return (result); | |
1047 | ||
1048 | /* | |
1049 | * Otherwise the jury is still out on this one, so move to the next rule. | |
1050 | */ | |
1051 | ||
1052 | rules = rules->next; | |
1053 | } | |
1054 | ||
1055 | return (result); | |
1056 | } | |
1057 | ||
1058 | ||
1059 | /* | |
1060 | * 'patmatch()' - Pattern matching... | |
1061 | */ | |
1062 | ||
1063 | static int /* O - 1 if match, 0 if no match */ | |
1064 | patmatch(const char *s, /* I - String to match against */ | |
1065 | const char *pat) /* I - Pattern to match against */ | |
1066 | { | |
1067 | /* | |
1068 | * Range check the input... | |
1069 | */ | |
1070 | ||
1071 | if (s == NULL || pat == NULL) | |
1072 | return (0); | |
1073 | ||
1074 | /* | |
1075 | * Loop through the pattern and match strings, and stop if we come to a | |
1076 | * point where the strings don't match or we find a complete match. | |
1077 | */ | |
1078 | ||
1079 | while (*s != '\0' && *pat != '\0') | |
1080 | { | |
1081 | if (*pat == '*') | |
1082 | { | |
1083 | /* | |
1084 | * Wildcard - 0 or more characters... | |
1085 | */ | |
1086 | ||
1087 | pat ++; | |
1088 | if (*pat == '\0') | |
4400e98d | 1089 | return (1); /* Last pattern char is *, so everything matches... */ |
ef416fc2 | 1090 | |
1091 | /* | |
1092 | * Test all remaining combinations until we get to the end of the string. | |
1093 | */ | |
1094 | ||
1095 | while (*s != '\0') | |
1096 | { | |
1097 | if (patmatch(s, pat)) | |
1098 | return (1); | |
1099 | ||
1100 | s ++; | |
1101 | } | |
1102 | } | |
1103 | else if (*pat == '?') | |
1104 | { | |
1105 | /* | |
1106 | * Wildcard - 1 character... | |
1107 | */ | |
1108 | ||
1109 | pat ++; | |
1110 | s ++; | |
1111 | continue; | |
1112 | } | |
1113 | else if (*pat == '[') | |
1114 | { | |
1115 | /* | |
1116 | * Match a character from the input set [chars]... | |
1117 | */ | |
1118 | ||
1119 | pat ++; | |
1120 | while (*pat != ']' && *pat != '\0') | |
1121 | if (*s == *pat) | |
1122 | break; | |
1123 | else | |
1124 | pat ++; | |
1125 | ||
1126 | if (*pat == ']' || *pat == '\0') | |
1127 | return (0); | |
1128 | ||
1129 | while (*pat != ']' && *pat != '\0') | |
1130 | pat ++; | |
1131 | ||
1132 | if (*pat == ']') | |
1133 | pat ++; | |
1134 | ||
1135 | continue; | |
1136 | } | |
1137 | else if (*pat == '\\') | |
1138 | { | |
1139 | /* | |
1140 | * Handle quoted characters... | |
1141 | */ | |
1142 | ||
1143 | pat ++; | |
1144 | } | |
1145 | ||
1146 | /* | |
1147 | * Stop if the pattern and string don't match... | |
1148 | */ | |
1149 | ||
1150 | if (*pat++ != *s++) | |
1151 | return (0); | |
1152 | } | |
1153 | ||
1154 | /* | |
4400e98d | 1155 | * Done parsing the pattern and string; return 1 if the last character |
1156 | * matches and 0 otherwise... | |
ef416fc2 | 1157 | */ |
1158 | ||
1159 | return (*s == *pat); | |
1160 | } | |
1161 | ||
1162 | ||
1163 | /* | |
c07d5b2d | 1164 | * End of "$Id: type.c 177 2006-06-21 00:20:03Z jlovell $". |
ef416fc2 | 1165 | */ |