]>
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))
293 * Read function parameters...
298 num_values
< (sizeof(value
) / sizeof(value
[0]));
301 ptr
= value
[num_values
];
303 while ((ptr
- value
[num_values
]) < (sizeof(value
[0]) - 1) &&
304 *rule
!= '\0' && *rule
!= ',' && *rule
!= ')')
306 if (isspace(*rule
& 255))
309 * Ignore whitespace...
315 else if (*rule
== '\"' || *rule
== '\'')
318 * Copy quoted strings literally...
323 while (*rule
!= '\0' && *rule
!= quote
&&
324 (ptr
- value
[num_values
]) < (sizeof(value
[0]) - 1))
332 else if (*rule
== '<')
336 while (*rule
!= '>' && *rule
!= '\0' &&
337 (ptr
- value
[num_values
]) < (sizeof(value
[0]) - 1))
339 if (isxdigit(rule
[0] & 255) && isxdigit(rule
[1] & 255))
342 *ptr
= (*rule
++ - '0') << 4;
344 *ptr
= (tolower(*rule
++) - 'a' + 10) << 4;
347 *ptr
++ |= *rule
++ - '0';
349 *ptr
++ |= tolower(*rule
++) - 'a' + 10;
365 length
[num_values
] = ptr
- value
[num_values
];
379 * Figure out the function...
382 if (!strcmp(name
, "match"))
383 op
= MIME_MAGIC_MATCH
;
384 else if (!strcmp(name
, "ascii"))
385 op
= MIME_MAGIC_ASCII
;
386 else if (!strcmp(name
, "printable"))
387 op
= MIME_MAGIC_PRINTABLE
;
388 else if (!strcmp(name
, "string"))
389 op
= MIME_MAGIC_STRING
;
390 else if (!strcmp(name
, "istring"))
391 op
= MIME_MAGIC_ISTRING
;
392 else if (!strcmp(name
, "char"))
393 op
= MIME_MAGIC_CHAR
;
394 else if (!strcmp(name
, "short"))
395 op
= MIME_MAGIC_SHORT
;
396 else if (!strcmp(name
, "int"))
398 else if (!strcmp(name
, "locale"))
399 op
= MIME_MAGIC_LOCALE
;
400 else if (!strcmp(name
, "contains"))
401 op
= MIME_MAGIC_CONTAINS
;
402 else if (!strcmp(name
, "priority") && num_values
== 1)
404 mt
->priority
= atoi(value
[0]);
413 * This is just a filename match on the extension...
416 snprintf(value
[0], sizeof(value
[0]), "*.%s", name
);
417 length
[0] = strlen(value
[0]);
419 op
= MIME_MAGIC_MATCH
;
423 * Add a rule for this operation.
426 if ((temp
= calloc(1, sizeof(mime_magic_t
))) == NULL
)
429 temp
->invert
= invert
;
432 temp
->parent
= current
->parent
;
433 current
->next
= temp
;
438 temp
->prev
= current
;
440 if (logic
== MIME_MAGIC_NOP
)
443 * Add parenthetical grouping...
446 DEBUG_printf(("mimeAddTypeRule: Making new OR group %p for "
447 "parenthesis...\n", temp
));
449 temp
->op
= MIME_MAGIC_OR
;
451 if ((temp
->child
= calloc(1, sizeof(mime_magic_t
))) == NULL
)
454 temp
->child
->parent
= temp
;
457 logic
= MIME_MAGIC_OR
;
460 DEBUG_printf(("mimeAddTypeRule: adding %p: %s, op=%d, logic=%d, "
461 "invert=%d\n", temp
, name
, op
, logic
, invert
));
464 * Fill in data for the rule...
473 case MIME_MAGIC_MATCH
:
474 if (length
[0] > (sizeof(temp
->value
.matchv
) - 1))
476 strcpy(temp
->value
.matchv
, value
[0]);
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
;
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
))
490 temp
->length
= length
[1];
491 memcpy(temp
->value
.stringv
, value
[1], length
[1]);
493 case MIME_MAGIC_CHAR
:
494 temp
->offset
= strtol(value
[0], NULL
, 0);
496 temp
->value
.charv
= value
[1][0];
498 temp
->value
.charv
= (char)strtol(value
[1], NULL
, 0);
500 case MIME_MAGIC_SHORT
:
501 temp
->offset
= strtol(value
[0], NULL
, 0);
502 temp
->value
.shortv
= (short)strtol(value
[1], NULL
, 0);
504 case MIME_MAGIC_INT
:
505 temp
->offset
= strtol(value
[0], NULL
, 0);
506 temp
->value
.intv
= (int)strtol(value
[1], NULL
, 0);
508 case MIME_MAGIC_LOCALE
:
509 if (length
[0] > (sizeof(temp
->value
.localev
) - 1))
512 strcpy(temp
->value
.localev
, value
[0]);
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
))
519 temp
->length
= length
[2];
520 memcpy(temp
->value
.stringv
, value
[2], length
[2]);
533 * 'mimeFileType()' - Determine the type of a file.
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? */
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 */
548 DEBUG_printf(("mimeFileType(mime=%p, pathname=\"%s\", filename=\"%s\", "
549 "compression=%p)\n", mime
, pathname
, filename
, compression
));
552 * Range check input parameters...
555 if (!mime
|| !pathname
)
559 * Try to open the file...
562 if ((fb
.fp
= cupsFileOpen(pathname
, "r")) == NULL
)
569 * Figure out the base filename (without directory portion)...
574 if ((base
= strrchr(filename
, '/')) != NULL
)
579 else if ((base
= strrchr(pathname
, '/')) != NULL
)
585 * Then check it against all known types...
588 for (type
= (mime_type_t
*)cupsArrayFirst(mime
->types
), best
= NULL
;
590 type
= (mime_type_t
*)cupsArrayNext(mime
->types
))
591 if (checkrules(base
, &fb
, type
->rules
))
593 if (!best
|| type
->priority
> best
->priority
)
598 * Finally, close the file and return a match (if any)...
602 *compression
= cupsFileCompression(fb
.fp
);
604 cupsFileClose(fb
.fp
);
611 * 'mimeType()' - Lookup a file type.
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 */
619 mime_type_t key
; /* MIME type search key*/
623 * Range check input...
626 if (!mime
|| !super
|| !type
)
630 * Lookup the type in the array...
633 strlcpy(key
.super
, super
, sizeof(key
.super
));
634 strlcpy(key
.type
, type
, sizeof(key
.type
));
636 return ((mime_type_t
*)cupsArrayFind(mime
->types
, &key
));
641 * 'compare_types()' - Compare two MIME super/type names.
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 */
648 int i
; /* Result of comparison */
651 if ((i
= strcasecmp(t0
->super
, t1
->super
)) == 0)
652 i
= strcasecmp(t0
->type
, t1
->type
);
659 * 'checkrules()' - Check each rule in a list.
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 */
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 */
675 const char * const debug_tests
[] = /* Test names... */
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 */
694 DEBUG_printf(("checkrules(filename=\"%s\", fb=%p, rules=%p)\n", filename
,
700 if (rules
->parent
== NULL
)
701 logic
= MIME_MAGIC_OR
;
703 logic
= rules
->parent
->op
;
707 while (rules
!= NULL
)
710 * Compute the result of this rule...
715 case MIME_MAGIC_MATCH
:
716 result
= patmatch(filename
, rules
->value
.matchv
);
719 case MIME_MAGIC_ASCII
:
721 * Load the buffer if necessary...
724 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
725 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
728 * Reload file buffer...
731 cupsFileSeek(fb
->fp
, rules
->offset
);
732 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
734 fb
->offset
= rules
->offset
;
738 * Test for ASCII printable characters plus standard control chars.
741 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
742 n
= fb
->offset
+ fb
->length
- rules
->offset
;
746 bufptr
= fb
->buffer
+ rules
->offset
- fb
->offset
;
748 if ((*bufptr
>= 32 && *bufptr
<= 126) ||
749 (*bufptr
>= 8 && *bufptr
<= 13) ||
750 *bufptr
== 26 || *bufptr
== 27)
761 case MIME_MAGIC_PRINTABLE
:
763 * Load the buffer if necessary...
766 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
767 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
770 * Reload file buffer...
773 cupsFileSeek(fb
->fp
, rules
->offset
);
774 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
776 fb
->offset
= rules
->offset
;
780 * Test for 8-bit printable characters plus standard control chars.
783 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
784 n
= fb
->offset
+ fb
->length
- rules
->offset
;
788 bufptr
= fb
->buffer
+ rules
->offset
- fb
->offset
;
791 if (*bufptr
>= 128 ||
792 (*bufptr
>= 32 && *bufptr
<= 126) ||
793 (*bufptr
>= 8 && *bufptr
<= 13) ||
794 *bufptr
== 26 || *bufptr
== 27)
805 case MIME_MAGIC_STRING
:
806 DEBUG_printf(("checkrules: string(%d, \"%s\")\n", rules
->offset
,
807 rules
->value
.stringv
));
810 * Load the buffer if necessary...
813 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
814 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
817 * Reload file buffer...
820 cupsFileSeek(fb
->fp
, rules
->offset
);
821 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
823 fb
->offset
= rules
->offset
;
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]));
832 * Compare the buffer against the string. If the file is too
833 * short then don't compare - it can't match...
836 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
839 result
= (memcmp(fb
->buffer
+ rules
->offset
- fb
->offset
,
840 rules
->value
.stringv
, rules
->length
) == 0);
841 DEBUG_printf(("checkrules: result=%d\n", result
));
844 case MIME_MAGIC_ISTRING
:
846 * Load the buffer if necessary...
849 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
850 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
853 * Reload file buffer...
856 cupsFileSeek(fb
->fp
, rules
->offset
);
857 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
859 fb
->offset
= rules
->offset
;
863 * Compare the buffer against the string. If the file is too
864 * short then don't compare - it can't match...
867 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
870 result
= (strncasecmp((char *)fb
->buffer
+ rules
->offset
-
872 rules
->value
.stringv
, rules
->length
) == 0);
875 case MIME_MAGIC_CHAR
:
877 * Load the buffer if necessary...
880 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
)
883 * Reload file buffer...
886 cupsFileSeek(fb
->fp
, rules
->offset
);
887 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
889 fb
->offset
= rules
->offset
;
893 * Compare the character values; if the file is too short, it
900 result
= (fb
->buffer
[rules
->offset
- fb
->offset
] ==
904 case MIME_MAGIC_SHORT
:
906 * Load the buffer if necessary...
909 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
910 (rules
->offset
+ 2) > (fb
->offset
+ fb
->length
))
913 * Reload file buffer...
916 cupsFileSeek(fb
->fp
, rules
->offset
);
917 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
919 fb
->offset
= rules
->offset
;
923 * Compare the short values; if the file is too short, it
931 bufptr
= fb
->buffer
+ rules
->offset
- fb
->offset
;
932 shortv
= (bufptr
[0] << 8) | bufptr
[1];
933 result
= (shortv
== rules
->value
.shortv
);
937 case MIME_MAGIC_INT
:
939 * Load the buffer if necessary...
942 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
943 (rules
->offset
+ 4) > (fb
->offset
+ fb
->length
))
946 * Reload file buffer...
949 cupsFileSeek(fb
->fp
, rules
->offset
);
950 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
952 fb
->offset
= rules
->offset
;
956 * Compare the int values; if the file is too short, it
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
);
971 case MIME_MAGIC_LOCALE
:
972 #if defined(WIN32) || defined(__EMX__) || defined(__APPLE__)
973 result
= (strcmp(rules
->value
.localev
,
974 setlocale(LC_ALL
, "")) == 0);
976 result
= (strcmp(rules
->value
.localev
,
977 setlocale(LC_MESSAGES
, "")) == 0);
978 #endif /* __APPLE__ */
981 case MIME_MAGIC_CONTAINS
:
983 * Load the buffer if necessary...
986 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
987 (rules
->offset
+ rules
->region
) > (fb
->offset
+ fb
->length
))
990 * Reload file buffer...
993 cupsFileSeek(fb
->fp
, rules
->offset
);
994 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
996 fb
->offset
= rules
->offset
;
1000 * Compare the buffer against the string. If the file is too
1001 * short then don't compare - it can't match...
1004 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
1008 if (fb
->length
> rules
->region
)
1009 region
= rules
->region
- rules
->length
;
1011 region
= fb
->length
- rules
->length
;
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)
1022 if (rules
->child
!= NULL
)
1023 result
= checkrules(filename
, fb
, rules
->child
);
1030 * If the logic is inverted, invert the result...
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...
1042 DEBUG_printf(("checkrules: result of test %p (MIME_MAGIC_%s) is %d\n",
1043 rules
, debug_tests
[rules
->op
], result
));
1045 if ((result
&& logic
== MIME_MAGIC_OR
) ||
1046 (!result
&& logic
== MIME_MAGIC_AND
))
1050 * Otherwise the jury is still out on this one, so move to the next rule.
1053 rules
= rules
->next
;
1061 * 'patmatch()' - Pattern matching...
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 */
1069 * Range check the input...
1072 if (s
== NULL
|| pat
== NULL
)
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.
1080 while (*s
!= '\0' && *pat
!= '\0')
1085 * Wildcard - 0 or more characters...
1090 return (1); /* Last pattern char is *, so everything matches... */
1093 * Test all remaining combinations until we get to the end of the string.
1098 if (patmatch(s
, pat
))
1104 else if (*pat
== '?')
1107 * Wildcard - 1 character...
1114 else if (*pat
== '[')
1117 * Match a character from the input set [chars]...
1121 while (*pat
!= ']' && *pat
!= '\0')
1127 if (*pat
== ']' || *pat
== '\0')
1130 while (*pat
!= ']' && *pat
!= '\0')
1138 else if (*pat
== '\\')
1141 * Handle quoted characters...
1148 * Stop if the pattern and string don't match...
1156 * Done parsing the pattern and string; return 1 if the last character
1157 * matches and 0 otherwise...
1160 return (*s
== *pat
);
1165 * End of "$Id: type.c 7694 2008-06-26 00:23:20Z mike $".