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