]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/type.c
2 * "$Id: type.c 7694 2008-06-26 00:23:20Z mike $"
4 * MIME typing routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
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/".
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...
27 * Include necessary headers...
35 #include <cups/string.h>
37 #include <cups/debug.h>
44 typedef struct _mime_filebuf_s
/**** File buffer for MIME typing ****/
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 */
57 static int compare_types(mime_type_t
*t0
, mime_type_t
*t1
);
58 static int checkrules(const char *filename
, _mime_filebuf_t
*fb
,
60 static int patmatch(const char *s
, const char *pat
);
64 * 'mimeAddType()' - Add a MIME type to a database.
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 */
72 mime_type_t
*temp
; /* New MIME type */
76 * Range check input...
79 if (!mime
|| !super
|| !type
)
83 * See if the type already exists; if so, return the existing type...
86 if ((temp
= mimeType(mime
, super
, type
)) != NULL
)
90 * The type doesn't exist; add it...
94 mime
->types
= cupsArrayNew((cups_array_func_t
)compare_types
, NULL
);
99 if ((temp
= calloc(1, sizeof(mime_type_t
) - MIME_MAX_TYPE
+
100 strlen(type
) + 1)) == NULL
)
103 strlcpy(temp
->super
, super
, sizeof(temp
->super
));
104 strcpy(temp
->type
, type
); /* Safe: temp->type is allocated */
105 temp
->priority
= 100;
107 cupsArrayAdd(mime
->types
, temp
);
114 * 'mimeAddTypeRule()' - Add a detection rule for a file type.
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 */
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 */
135 * Range check input...
142 * Find the last rule in the top-level of the rules tree.
145 for (current
= mt
->rules
; current
; current
= current
->next
)
150 * Parse the rules string. Most rules are either a file extension or a
151 * comparison function:
154 * function(parameters)
157 logic
= MIME_MAGIC_NOP
;
160 DEBUG_printf(("mimeAddTypeRule: %s/%s: %s\n", mt
->super
, mt
->type
, rule
));
162 while (*rule
!= '\0')
164 while (isspace(*rule
& 255))
169 DEBUG_puts("mimeAddTypeRule: New parenthesis group");
170 logic
= MIME_MAGIC_NOP
;
173 else if (*rule
== ')')
175 DEBUG_puts("mimeAddTypeRule: Close paren...");
176 if (current
== NULL
|| current
->parent
== NULL
)
179 current
= current
->parent
;
181 if (current
->parent
== NULL
)
182 logic
= MIME_MAGIC_OR
;
184 logic
= current
->parent
->op
;
188 else if (*rule
== '+' && current
!= NULL
)
190 if (logic
!= MIME_MAGIC_AND
&&
191 current
!= NULL
&& current
->prev
!= NULL
&&
192 current
->prev
->prev
!= NULL
)
195 * OK, we have more than 1 rule in the current tree level... Make a
196 * new group tree and move the previous rule to it...
199 if ((temp
= calloc(1, sizeof(mime_magic_t
))) == NULL
)
202 temp
->op
= MIME_MAGIC_AND
;
203 temp
->child
= current
;
204 temp
->parent
= current
->parent
;
205 current
->prev
->next
= temp
;
206 temp
->prev
= current
->prev
;
208 current
->prev
= NULL
;
209 current
->parent
= temp
;
211 DEBUG_printf(("mimeAddTypeRule: Creating new AND group %p...\n", temp
));
215 DEBUG_printf(("mimeAddTypeRule: Setting group %p op to AND...\n",
217 current
->parent
->op
= MIME_MAGIC_AND
;
220 logic
= MIME_MAGIC_AND
;
223 else if (*rule
== ',')
225 if (logic
!= MIME_MAGIC_OR
&& current
!= NULL
)
228 * OK, we have two possibilities; either this is the top-level rule or
229 * we have a bunch of AND rules at this level.
232 if (current
->parent
== NULL
)
235 * This is the top-level rule; we have to move *all* of the AND rules
236 * down a level, as AND has precedence over OR.
239 if ((temp
= calloc(1, sizeof(mime_magic_t
))) == NULL
)
242 DEBUG_printf(("mimeAddTypeRule: Creating new AND group %p inside OR "
245 while (current
->prev
!= NULL
)
247 current
->parent
= temp
;
248 current
= current
->prev
;
251 current
->parent
= temp
;
252 temp
->op
= MIME_MAGIC_AND
;
253 temp
->child
= current
;
255 mt
->rules
= current
= temp
;
260 * This isn't the top rule, so go up one level...
263 DEBUG_puts("mimeAddTypeRule: Going up one level");
264 current
= current
->parent
;
268 logic
= MIME_MAGIC_OR
;
271 else if (*rule
== '!')
273 DEBUG_puts("mimeAddTypeRule: NOT");
277 else if (isalnum(*rule
& 255))
280 * Read an extension name or a function...
284 while (isalnum(*rule
& 255) && (ptr
- name
) < (sizeof(name
) - 1))
292 * Read function parameters...
297 num_values
< (sizeof(value
) / sizeof(value
[0]));
300 ptr
= value
[num_values
];
302 while ((ptr
- value
[num_values
]) < (sizeof(value
[0]) - 1) &&
303 *rule
!= '\0' && *rule
!= ',' && *rule
!= ')')
305 if (isspace(*rule
& 255))
308 * Ignore whitespace...
314 else if (*rule
== '\"' || *rule
== '\'')
317 * Copy quoted strings literally...
322 while (*rule
!= '\0' && *rule
!= quote
&&
323 (ptr
- value
[num_values
]) < (sizeof(value
[0]) - 1))
331 else if (*rule
== '<')
335 while (*rule
!= '>' && *rule
!= '\0' &&
336 (ptr
- value
[num_values
]) < (sizeof(value
[0]) - 1))
338 if (isxdigit(rule
[0] & 255) && isxdigit(rule
[1] & 255))
341 *ptr
= (*rule
++ - '0') << 4;
343 *ptr
= (tolower(*rule
++) - 'a' + 10) << 4;
346 *ptr
++ |= *rule
++ - '0';
348 *ptr
++ |= tolower(*rule
++) - 'a' + 10;
364 length
[num_values
] = ptr
- value
[num_values
];
378 * Figure out the function...
381 if (!strcmp(name
, "match"))
382 op
= MIME_MAGIC_MATCH
;
383 else if (!strcmp(name
, "ascii"))
384 op
= MIME_MAGIC_ASCII
;
385 else if (!strcmp(name
, "printable"))
386 op
= MIME_MAGIC_PRINTABLE
;
387 else if (!strcmp(name
, "string"))
388 op
= MIME_MAGIC_STRING
;
389 else if (!strcmp(name
, "istring"))
390 op
= MIME_MAGIC_ISTRING
;
391 else if (!strcmp(name
, "char"))
392 op
= MIME_MAGIC_CHAR
;
393 else if (!strcmp(name
, "short"))
394 op
= MIME_MAGIC_SHORT
;
395 else if (!strcmp(name
, "int"))
397 else if (!strcmp(name
, "locale"))
398 op
= MIME_MAGIC_LOCALE
;
399 else if (!strcmp(name
, "contains"))
400 op
= MIME_MAGIC_CONTAINS
;
401 else if (!strcmp(name
, "priority") && num_values
== 1)
403 mt
->priority
= atoi(value
[0]);
412 * This is just a filename match on the extension...
415 snprintf(value
[0], sizeof(value
[0]), "*.%s", name
);
416 length
[0] = strlen(value
[0]);
417 op
= MIME_MAGIC_MATCH
;
421 * Add a rule for this operation.
424 if ((temp
= calloc(1, sizeof(mime_magic_t
))) == NULL
)
427 temp
->invert
= invert
;
430 temp
->parent
= current
->parent
;
431 current
->next
= temp
;
436 temp
->prev
= current
;
438 if (logic
== MIME_MAGIC_NOP
)
441 * Add parenthetical grouping...
444 DEBUG_printf(("mimeAddTypeRule: Making new OR group %p for "
445 "parenthesis...\n", temp
));
447 temp
->op
= MIME_MAGIC_OR
;
449 if ((temp
->child
= calloc(1, sizeof(mime_magic_t
))) == NULL
)
452 temp
->child
->parent
= temp
;
455 logic
= MIME_MAGIC_OR
;
458 DEBUG_printf(("mimeAddTypeRule: adding %p: %s, op=%d, logic=%d, "
459 "invert=%d\n", temp
, name
, op
, logic
, invert
));
462 * Fill in data for the rule...
471 case MIME_MAGIC_MATCH
:
472 if (length
[0] > (sizeof(temp
->value
.matchv
) - 1))
474 strcpy(temp
->value
.matchv
, value
[0]);
476 case MIME_MAGIC_ASCII
:
477 case MIME_MAGIC_PRINTABLE
:
478 temp
->offset
= strtol(value
[0], NULL
, 0);
479 temp
->length
= strtol(value
[1], NULL
, 0);
480 if (temp
->length
> MIME_MAX_BUFFER
)
481 temp
->length
= MIME_MAX_BUFFER
;
483 case MIME_MAGIC_STRING
:
484 case MIME_MAGIC_ISTRING
:
485 temp
->offset
= strtol(value
[0], NULL
, 0);
486 if (length
[1] > sizeof(temp
->value
.stringv
))
488 temp
->length
= length
[1];
489 memcpy(temp
->value
.stringv
, value
[1], length
[1]);
491 case MIME_MAGIC_CHAR
:
492 temp
->offset
= strtol(value
[0], NULL
, 0);
494 temp
->value
.charv
= value
[1][0];
496 temp
->value
.charv
= (char)strtol(value
[1], NULL
, 0);
498 case MIME_MAGIC_SHORT
:
499 temp
->offset
= strtol(value
[0], NULL
, 0);
500 temp
->value
.shortv
= (short)strtol(value
[1], NULL
, 0);
502 case MIME_MAGIC_INT
:
503 temp
->offset
= strtol(value
[0], NULL
, 0);
504 temp
->value
.intv
= (int)strtol(value
[1], NULL
, 0);
506 case MIME_MAGIC_LOCALE
:
507 if (length
[0] > (sizeof(temp
->value
.localev
) - 1))
510 strcpy(temp
->value
.localev
, value
[0]);
512 case MIME_MAGIC_CONTAINS
:
513 temp
->offset
= strtol(value
[0], NULL
, 0);
514 temp
->region
= strtol(value
[1], NULL
, 0);
515 if (length
[2] > sizeof(temp
->value
.stringv
))
517 temp
->length
= length
[2];
518 memcpy(temp
->value
.stringv
, value
[2], length
[2]);
531 * 'mimeFileType()' - Determine the type of a file.
534 mime_type_t
* /* O - Type of file */
535 mimeFileType(mime_t
*mime
, /* I - MIME database */
536 const char *pathname
, /* I - Name of file to check on disk */
537 const char *filename
, /* I - Original filename or NULL */
538 int *compression
) /* O - Is the file compressed? */
540 _mime_filebuf_t fb
; /* File buffer */
541 const char *base
; /* Base filename of file */
542 mime_type_t
*type
, /* File type */
543 *best
; /* Best match */
546 DEBUG_printf(("mimeFileType(mime=%p, pathname=\"%s\", filename=\"%s\", "
547 "compression=%p)\n", mime
, pathname
, filename
, compression
));
550 * Range check input parameters...
553 if (!mime
|| !pathname
)
557 * Try to open the file...
560 if ((fb
.fp
= cupsFileOpen(pathname
, "r")) == NULL
)
567 * Figure out the base filename (without directory portion)...
572 if ((base
= strrchr(filename
, '/')) != NULL
)
577 else if ((base
= strrchr(pathname
, '/')) != NULL
)
583 * Then check it against all known types...
586 for (type
= (mime_type_t
*)cupsArrayFirst(mime
->types
), best
= NULL
;
588 type
= (mime_type_t
*)cupsArrayNext(mime
->types
))
589 if (checkrules(base
, &fb
, type
->rules
))
591 if (!best
|| type
->priority
> best
->priority
)
596 * Finally, close the file and return a match (if any)...
600 *compression
= cupsFileCompression(fb
.fp
);
602 cupsFileClose(fb
.fp
);
609 * 'mimeType()' - Lookup a file type.
612 mime_type_t
* /* O - Matching file type definition */
613 mimeType(mime_t
*mime
, /* I - MIME database */
614 const char *super
, /* I - Super-type name */
615 const char *type
) /* I - Type name */
617 mime_type_t key
; /* MIME type search key*/
621 * Range check input...
624 if (!mime
|| !super
|| !type
)
628 * Lookup the type in the array...
631 strlcpy(key
.super
, super
, sizeof(key
.super
));
632 strlcpy(key
.type
, type
, sizeof(key
.type
));
634 return ((mime_type_t
*)cupsArrayFind(mime
->types
, &key
));
639 * 'compare_types()' - Compare two MIME super/type names.
642 static int /* O - Result of comparison */
643 compare_types(mime_type_t
*t0
, /* I - First type */
644 mime_type_t
*t1
) /* I - Second type */
646 int i
; /* Result of comparison */
649 if ((i
= strcasecmp(t0
->super
, t1
->super
)) == 0)
650 i
= strcasecmp(t0
->type
, t1
->type
);
657 * 'checkrules()' - Check each rule in a list.
660 static int /* O - 1 if match, 0 if no match */
661 checkrules(const char *filename
, /* I - Filename */
662 _mime_filebuf_t
*fb
, /* I - File to check */
663 mime_magic_t
*rules
) /* I - Rules to check */
665 int n
; /* Looping var */
666 int region
; /* Region to look at */
667 int logic
, /* Logic to apply */
668 result
, /* Result of test */
669 intv
; /* Integer value */
670 short shortv
; /* Short value */
671 unsigned char *bufptr
; /* Pointer into buffer */
673 const char * const debug_tests
[] = /* Test names... */
675 "NOP", /* No operation */
676 "AND", /* Logical AND of all children */
677 "OR", /* Logical OR of all children */
678 "MATCH", /* Filename match */
679 "ASCII", /* ASCII characters in range */
680 "PRINTABLE", /* Printable characters (32-255) */
681 "STRING", /* String matches */
682 "CHAR", /* Character/byte matches */
683 "SHORT", /* Short/16-bit word matches */
684 "INT", /* Integer/32-bit word matches */
685 "LOCALE" /* Current locale matches string */
686 "CONTAINS" /* File contains a string */
687 "ISTRING" /* Case-insensitive string matches */
692 DEBUG_printf(("checkrules(filename=\"%s\", fb=%p, rules=%p)\n", filename
,
698 if (rules
->parent
== NULL
)
699 logic
= MIME_MAGIC_OR
;
701 logic
= rules
->parent
->op
;
705 while (rules
!= NULL
)
708 * Compute the result of this rule...
713 case MIME_MAGIC_MATCH
:
714 result
= patmatch(filename
, rules
->value
.matchv
);
717 case MIME_MAGIC_ASCII
:
719 * Load the buffer if necessary...
722 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
723 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
726 * Reload file buffer...
729 cupsFileSeek(fb
->fp
, rules
->offset
);
730 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
732 fb
->offset
= rules
->offset
;
736 * Test for ASCII printable characters plus standard control chars.
739 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
740 n
= fb
->offset
+ fb
->length
- rules
->offset
;
744 bufptr
= fb
->buffer
+ rules
->offset
- fb
->offset
;
746 if ((*bufptr
>= 32 && *bufptr
<= 126) ||
747 (*bufptr
>= 8 && *bufptr
<= 13) ||
748 *bufptr
== 26 || *bufptr
== 27)
759 case MIME_MAGIC_PRINTABLE
:
761 * Load the buffer if necessary...
764 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
765 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
768 * Reload file buffer...
771 cupsFileSeek(fb
->fp
, rules
->offset
);
772 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
774 fb
->offset
= rules
->offset
;
778 * Test for 8-bit printable characters plus standard control chars.
781 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
782 n
= fb
->offset
+ fb
->length
- rules
->offset
;
786 bufptr
= fb
->buffer
+ rules
->offset
- fb
->offset
;
789 if (*bufptr
>= 128 ||
790 (*bufptr
>= 32 && *bufptr
<= 126) ||
791 (*bufptr
>= 8 && *bufptr
<= 13) ||
792 *bufptr
== 26 || *bufptr
== 27)
803 case MIME_MAGIC_STRING
:
804 DEBUG_printf(("checkrules: string(%d, \"%s\")\n", rules
->offset
,
805 rules
->value
.stringv
));
808 * Load the buffer if necessary...
811 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
812 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
815 * Reload file buffer...
818 cupsFileSeek(fb
->fp
, rules
->offset
);
819 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
821 fb
->offset
= rules
->offset
;
823 DEBUG_printf(("checkrules: loaded %d byte fb->buffer at %d, starts "
824 "with \"%c%c%c%c\"...\n",
825 fb
->length
, fb
->offset
, fb
->buffer
[0], fb
->buffer
[1],
826 fb
->buffer
[2], fb
->buffer
[3]));
830 * Compare the buffer against the string. If the file is too
831 * short then don't compare - it can't match...
834 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
837 result
= (memcmp(fb
->buffer
+ rules
->offset
- fb
->offset
,
838 rules
->value
.stringv
, rules
->length
) == 0);
839 DEBUG_printf(("checkrules: result=%d\n", result
));
842 case MIME_MAGIC_ISTRING
:
844 * Load the buffer if necessary...
847 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
848 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
851 * Reload file buffer...
854 cupsFileSeek(fb
->fp
, rules
->offset
);
855 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
857 fb
->offset
= rules
->offset
;
861 * Compare the buffer against the string. If the file is too
862 * short then don't compare - it can't match...
865 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
868 result
= (strncasecmp((char *)fb
->buffer
+ rules
->offset
-
870 rules
->value
.stringv
, rules
->length
) == 0);
873 case MIME_MAGIC_CHAR
:
875 * Load the buffer if necessary...
878 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
)
881 * Reload file buffer...
884 cupsFileSeek(fb
->fp
, rules
->offset
);
885 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
887 fb
->offset
= rules
->offset
;
891 * Compare the character values; if the file is too short, it
898 result
= (fb
->buffer
[rules
->offset
- fb
->offset
] ==
902 case MIME_MAGIC_SHORT
:
904 * Load the buffer if necessary...
907 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
908 (rules
->offset
+ 2) > (fb
->offset
+ fb
->length
))
911 * Reload file buffer...
914 cupsFileSeek(fb
->fp
, rules
->offset
);
915 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
917 fb
->offset
= rules
->offset
;
921 * Compare the short values; if the file is too short, it
929 bufptr
= fb
->buffer
+ rules
->offset
- fb
->offset
;
930 shortv
= (bufptr
[0] << 8) | bufptr
[1];
931 result
= (shortv
== rules
->value
.shortv
);
935 case MIME_MAGIC_INT
:
937 * Load the buffer if necessary...
940 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
941 (rules
->offset
+ 4) > (fb
->offset
+ fb
->length
))
944 * Reload file buffer...
947 cupsFileSeek(fb
->fp
, rules
->offset
);
948 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
950 fb
->offset
= rules
->offset
;
954 * Compare the int values; if the file is too short, it
962 bufptr
= fb
->buffer
+ rules
->offset
- fb
->offset
;
963 intv
= (((((bufptr
[0] << 8) | bufptr
[1]) << 8) |
964 bufptr
[2]) << 8) | bufptr
[3];
965 result
= (intv
== rules
->value
.intv
);
969 case MIME_MAGIC_LOCALE
:
970 #if defined(WIN32) || defined(__EMX__) || defined(__APPLE__)
971 result
= (strcmp(rules
->value
.localev
,
972 setlocale(LC_ALL
, "")) == 0);
974 result
= (strcmp(rules
->value
.localev
,
975 setlocale(LC_MESSAGES
, "")) == 0);
976 #endif /* __APPLE__ */
979 case MIME_MAGIC_CONTAINS
:
981 * Load the buffer if necessary...
984 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
985 (rules
->offset
+ rules
->region
) > (fb
->offset
+ fb
->length
))
988 * Reload file buffer...
991 cupsFileSeek(fb
->fp
, rules
->offset
);
992 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
994 fb
->offset
= rules
->offset
;
998 * Compare the buffer against the string. If the file is too
999 * short then don't compare - it can't match...
1002 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
1006 if (fb
->length
> rules
->region
)
1007 region
= rules
->region
- rules
->length
;
1009 region
= fb
->length
- rules
->length
;
1011 for (n
= 0; n
< region
; n
++)
1012 if ((result
= (memcmp(fb
->buffer
+ rules
->offset
- fb
->offset
+ n
,
1013 rules
->value
.stringv
,
1014 rules
->length
) == 0)) != 0)
1020 if (rules
->child
!= NULL
)
1021 result
= checkrules(filename
, fb
, rules
->child
);
1028 * If the logic is inverted, invert the result...
1035 * OK, now if the current logic is OR and this result is true, the this
1036 * rule set is true. If the current logic is AND and this result is false,
1037 * the the rule set is false...
1040 DEBUG_printf(("checkrules: result of test %p (MIME_MAGIC_%s) is %d\n",
1041 rules
, debug_tests
[rules
->op
], result
));
1043 if ((result
&& logic
== MIME_MAGIC_OR
) ||
1044 (!result
&& logic
== MIME_MAGIC_AND
))
1048 * Otherwise the jury is still out on this one, so move to the next rule.
1051 rules
= rules
->next
;
1059 * 'patmatch()' - Pattern matching...
1062 static int /* O - 1 if match, 0 if no match */
1063 patmatch(const char *s
, /* I - String to match against */
1064 const char *pat
) /* I - Pattern to match against */
1067 * Range check the input...
1070 if (s
== NULL
|| pat
== NULL
)
1074 * Loop through the pattern and match strings, and stop if we come to a
1075 * point where the strings don't match or we find a complete match.
1078 while (*s
!= '\0' && *pat
!= '\0')
1083 * Wildcard - 0 or more characters...
1088 return (1); /* Last pattern char is *, so everything matches... */
1091 * Test all remaining combinations until we get to the end of the string.
1096 if (patmatch(s
, pat
))
1102 else if (*pat
== '?')
1105 * Wildcard - 1 character...
1112 else if (*pat
== '[')
1115 * Match a character from the input set [chars]...
1119 while (*pat
!= ']' && *pat
!= '\0')
1125 if (*pat
== ']' || *pat
== '\0')
1128 while (*pat
!= ']' && *pat
!= '\0')
1136 else if (*pat
== '\\')
1139 * Handle quoted characters...
1146 * Stop if the pattern and string don't match...
1154 * Done parsing the pattern and string; return 1 if the last character
1155 * matches and 0 otherwise...
1158 return (*s
== *pat
);
1163 * End of "$Id: type.c 7694 2008-06-26 00:23:20Z mike $".