]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/type.c
2 * MIME typing routines for CUPS.
4 * Copyright 2007-2016 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include <cups/string-private.h>
15 #include <cups/debug-private.h>
24 typedef struct _mime_filebuf_s
/**** File buffer for MIME typing ****/
26 cups_file_t
*fp
; /* File pointer */
27 int offset
, /* Offset in file */
28 length
; /* Length of buffered data */
29 unsigned char buffer
[MIME_MAX_BUFFER
];/* Buffered data */
37 static int mime_compare_types(mime_type_t
*t0
, mime_type_t
*t1
);
38 static int mime_check_rules(const char *filename
, _mime_filebuf_t
*fb
,
40 static int mime_patmatch(const char *s
, const char *pat
);
48 static const char * const debug_ops
[] =
50 "NOP", /* No operation */
51 "AND", /* Logical AND of all children */
52 "OR", /* Logical OR of all children */
53 "MATCH", /* Filename match */
54 "ASCII", /* ASCII characters in range */
55 "PRINTABLE", /* Printable characters (32-255) */
56 "STRING", /* String matches */
57 "CHAR", /* Character/byte matches */
58 "SHORT", /* Short/16-bit word matches */
59 "INT", /* Integer/32-bit word matches */
60 "LOCALE", /* Current locale matches string */
61 "CONTAINS", /* File contains a string */
62 "ISTRING", /* Case-insensitive string matches */
63 "REGEX" /* Regular expression matches */
69 * 'mimeAddType()' - Add a MIME type to a database.
72 mime_type_t
* /* O - New (or existing) MIME type */
73 mimeAddType(mime_t
*mime
, /* I - MIME database */
74 const char *super
, /* I - Super-type name */
75 const char *type
) /* I - Type name */
77 mime_type_t
*temp
; /* New MIME type */
78 size_t typelen
; /* Length of type name */
81 DEBUG_printf(("mimeAddType(mime=%p, super=\"%s\", type=\"%s\")", mime
, super
,
85 * Range check input...
88 if (!mime
|| !super
|| !type
)
90 DEBUG_puts("1mimeAddType: Returning NULL (bad arguments).");
95 * See if the type already exists; if so, return the existing type...
98 if ((temp
= mimeType(mime
, super
, type
)) != NULL
)
100 DEBUG_printf(("1mimeAddType: Returning %p (existing).", temp
));
105 * The type doesn't exist; add it...
109 mime
->types
= cupsArrayNew((cups_array_func_t
)mime_compare_types
, NULL
);
113 DEBUG_puts("1mimeAddType: Returning NULL (no types).");
117 typelen
= strlen(type
) + 1;
119 if ((temp
= calloc(1, sizeof(mime_type_t
) - MIME_MAX_TYPE
+ typelen
)) == NULL
)
121 DEBUG_puts("1mimeAddType: Returning NULL (out of memory).");
125 strlcpy(temp
->super
, super
, sizeof(temp
->super
));
126 memcpy(temp
->type
, type
, typelen
);
127 temp
->priority
= 100;
129 cupsArrayAdd(mime
->types
, temp
);
131 DEBUG_printf(("1mimeAddType: Returning %p (new).", temp
));
137 * 'mimeAddTypeRule()' - Add a detection rule for a file type.
140 int /* O - 0 on success, -1 on failure */
141 mimeAddTypeRule(mime_type_t
*mt
, /* I - Type to add to */
142 const char *rule
) /* I - Rule to add */
144 int num_values
, /* Number of values seen */
145 op
, /* Operation code */
146 logic
, /* Logic for next rule */
147 invert
; /* Invert following rule? */
148 char name
[255], /* Name in rule string */
149 value
[3][255], /* Value in rule string */
150 *ptr
, /* Position in name or value */
151 quote
; /* Quote character */
152 int length
[3]; /* Length of each parameter */
153 mime_magic_t
*temp
, /* New rule */
154 *current
; /* Current rule */
157 DEBUG_printf(("mimeAddTypeRule(mt=%p(%s/%s), rule=\"%s\")", mt
,
158 mt
? mt
->super
: "???", mt
? mt
->type
: "???", rule
));
161 * Range check input...
168 * Find the last rule in the top-level of the rules tree.
171 for (current
= mt
->rules
; current
; current
= current
->next
)
176 * Parse the rules string. Most rules are either a file extension or a
177 * comparison function:
180 * function(parameters)
183 logic
= MIME_MAGIC_NOP
;
186 while (*rule
!= '\0')
188 while (isspace(*rule
& 255))
193 DEBUG_puts("1mimeAddTypeRule: New parenthesis group");
194 logic
= MIME_MAGIC_NOP
;
197 else if (*rule
== ')')
199 DEBUG_puts("1mimeAddTypeRule: Close paren...");
200 if (current
== NULL
|| current
->parent
== NULL
)
203 current
= current
->parent
;
205 if (current
->parent
== NULL
)
206 logic
= MIME_MAGIC_OR
;
208 logic
= current
->parent
->op
;
212 else if (*rule
== '+' && current
!= NULL
)
214 if (logic
!= MIME_MAGIC_AND
&&
215 current
!= NULL
&& current
->prev
!= NULL
)
218 * OK, we have more than 1 rule in the current tree level... Make a
219 * new group tree and move the previous rule to it...
222 if ((temp
= calloc(1, sizeof(mime_magic_t
))) == NULL
)
225 temp
->op
= MIME_MAGIC_AND
;
226 temp
->child
= current
;
227 temp
->parent
= current
->parent
;
228 current
->prev
->next
= temp
;
229 temp
->prev
= current
->prev
;
231 current
->prev
= NULL
;
232 current
->parent
= temp
;
234 DEBUG_printf(("1mimeAddTypeRule: Creating new AND group %p.", temp
));
236 else if (current
->parent
)
238 DEBUG_printf(("1mimeAddTypeRule: Setting group %p op to AND.",
240 current
->parent
->op
= MIME_MAGIC_AND
;
243 logic
= MIME_MAGIC_AND
;
246 else if (*rule
== ',')
248 if (logic
!= MIME_MAGIC_OR
&& current
!= NULL
)
251 * OK, we have two possibilities; either this is the top-level rule or
252 * we have a bunch of AND rules at this level.
255 if (current
->parent
== NULL
)
258 * This is the top-level rule; we have to move *all* of the AND rules
259 * down a level, as AND has precedence over OR.
262 if ((temp
= calloc(1, sizeof(mime_magic_t
))) == NULL
)
265 DEBUG_printf(("1mimeAddTypeRule: Creating new AND group %p inside OR "
268 while (current
->prev
!= NULL
)
270 current
->parent
= temp
;
271 current
= current
->prev
;
274 current
->parent
= temp
;
275 temp
->op
= MIME_MAGIC_AND
;
276 temp
->child
= current
;
278 mt
->rules
= current
= temp
;
283 * This isn't the top rule, so go up one level...
286 DEBUG_puts("1mimeAddTypeRule: Going up one level.");
287 current
= current
->parent
;
291 logic
= MIME_MAGIC_OR
;
294 else if (*rule
== '!')
296 DEBUG_puts("1mimeAddTypeRule: NOT");
300 else if (isalnum(*rule
& 255))
303 * Read an extension name or a function...
307 while (isalnum(*rule
& 255) && (size_t)(ptr
- name
) < (sizeof(name
) - 1))
315 * Read function parameters...
320 num_values
< (int)(sizeof(value
) / sizeof(value
[0]));
323 ptr
= value
[num_values
];
325 while ((size_t)(ptr
- value
[num_values
]) < (sizeof(value
[0]) - 1) &&
326 *rule
!= '\0' && *rule
!= ',' && *rule
!= ')')
328 if (isspace(*rule
& 255))
331 * Ignore whitespace...
337 else if (*rule
== '\"' || *rule
== '\'')
340 * Copy quoted strings literally...
345 while (*rule
!= '\0' && *rule
!= quote
&&
346 (size_t)(ptr
- value
[num_values
]) < (sizeof(value
[0]) - 1))
354 else if (*rule
== '<')
358 while (*rule
!= '>' && *rule
!= '\0' &&
359 (size_t)(ptr
- value
[num_values
]) < (sizeof(value
[0]) - 1))
361 if (isxdigit(rule
[0] & 255) && isxdigit(rule
[1] & 255))
364 *ptr
= (char)((*rule
++ - '0') << 4);
366 *ptr
= (char)((tolower(*rule
++) - 'a' + 10) << 4);
369 *ptr
++ |= *rule
++ - '0';
371 *ptr
++ |= tolower(*rule
++) - 'a' + 10;
387 length
[num_values
] = ptr
- value
[num_values
];
404 * Figure out the function...
407 if (!strcmp(name
, "match"))
408 op
= MIME_MAGIC_MATCH
;
409 else if (!strcmp(name
, "ascii"))
410 op
= MIME_MAGIC_ASCII
;
411 else if (!strcmp(name
, "printable"))
412 op
= MIME_MAGIC_PRINTABLE
;
413 else if (!strcmp(name
, "regex"))
414 op
= MIME_MAGIC_REGEX
;
415 else if (!strcmp(name
, "string"))
416 op
= MIME_MAGIC_STRING
;
417 else if (!strcmp(name
, "istring"))
418 op
= MIME_MAGIC_ISTRING
;
419 else if (!strcmp(name
, "char"))
420 op
= MIME_MAGIC_CHAR
;
421 else if (!strcmp(name
, "short"))
422 op
= MIME_MAGIC_SHORT
;
423 else if (!strcmp(name
, "int"))
425 else if (!strcmp(name
, "locale"))
426 op
= MIME_MAGIC_LOCALE
;
427 else if (!strcmp(name
, "contains"))
428 op
= MIME_MAGIC_CONTAINS
;
429 else if (!strcmp(name
, "priority") && num_values
== 1)
431 mt
->priority
= atoi(value
[0]);
440 * This is just a filename match on the extension...
443 snprintf(value
[0], sizeof(value
[0]), "*.%s", name
);
444 length
[0] = (int)strlen(value
[0]);
445 op
= MIME_MAGIC_MATCH
;
449 * Add a rule for this operation.
452 if ((temp
= calloc(1, sizeof(mime_magic_t
))) == NULL
)
455 temp
->invert
= (short)invert
;
458 temp
->parent
= current
->parent
;
459 current
->next
= temp
;
464 temp
->prev
= current
;
466 if (logic
== MIME_MAGIC_NOP
)
469 * Add parenthetical grouping...
472 DEBUG_printf(("1mimeAddTypeRule: Making new OR group %p for "
473 "parenthesis.", temp
));
475 temp
->op
= MIME_MAGIC_OR
;
477 if ((temp
->child
= calloc(1, sizeof(mime_magic_t
))) == NULL
)
480 temp
->child
->parent
= temp
;
481 temp
->child
->invert
= temp
->invert
;
485 logic
= MIME_MAGIC_OR
;
488 DEBUG_printf(("1mimeAddTypeRule: Adding %p: %s, op=MIME_MAGIC_%s(%d), "
489 "logic=MIME_MAGIC_%s, invert=%d.", temp
, name
,
490 debug_ops
[op
], op
, debug_ops
[logic
], invert
));
493 * Fill in data for the rule...
497 temp
->op
= (short)op
;
502 case MIME_MAGIC_MATCH
:
503 if ((size_t)length
[0] > (sizeof(temp
->value
.matchv
) - 1))
505 strlcpy(temp
->value
.matchv
, value
[0], sizeof(temp
->value
.matchv
));
507 case MIME_MAGIC_ASCII
:
508 case MIME_MAGIC_PRINTABLE
:
509 temp
->offset
= strtol(value
[0], NULL
, 0);
510 temp
->length
= strtol(value
[1], NULL
, 0);
511 if (temp
->length
> MIME_MAX_BUFFER
)
512 temp
->length
= MIME_MAX_BUFFER
;
514 case MIME_MAGIC_REGEX
:
515 temp
->offset
= strtol(value
[0], NULL
, 0);
516 temp
->length
= MIME_MAX_BUFFER
;
517 if (regcomp(&(temp
->value
.rev
), value
[1], REG_NOSUB
| REG_EXTENDED
))
520 case MIME_MAGIC_STRING
:
521 case MIME_MAGIC_ISTRING
:
522 temp
->offset
= strtol(value
[0], NULL
, 0);
523 if ((size_t)length
[1] > sizeof(temp
->value
.stringv
))
525 temp
->length
= length
[1];
526 memcpy(temp
->value
.stringv
, value
[1], (size_t)length
[1]);
528 case MIME_MAGIC_CHAR
:
529 temp
->offset
= strtol(value
[0], NULL
, 0);
531 temp
->value
.charv
= (unsigned char)value
[1][0];
533 temp
->value
.charv
= (unsigned char)strtol(value
[1], NULL
, 0);
535 DEBUG_printf(("1mimeAddTypeRule: CHAR(%d,0x%02x)", temp
->offset
,
538 case MIME_MAGIC_SHORT
:
539 temp
->offset
= strtol(value
[0], NULL
, 0);
540 temp
->value
.shortv
= (unsigned short)strtol(value
[1], NULL
, 0);
542 case MIME_MAGIC_INT
:
543 temp
->offset
= strtol(value
[0], NULL
, 0);
544 temp
->value
.intv
= (unsigned)strtol(value
[1], NULL
, 0);
546 case MIME_MAGIC_LOCALE
:
547 if ((size_t)length
[0] > (sizeof(temp
->value
.localev
) - 1))
550 strlcpy(temp
->value
.localev
, value
[0], sizeof(temp
->value
.localev
));
552 case MIME_MAGIC_CONTAINS
:
553 temp
->offset
= strtol(value
[0], NULL
, 0);
554 temp
->region
= strtol(value
[1], NULL
, 0);
555 if ((size_t)length
[2] > sizeof(temp
->value
.stringv
))
557 temp
->length
= length
[2];
558 memcpy(temp
->value
.stringv
, value
[2], (size_t)length
[2]);
571 * 'mimeFileType()' - Determine the type of a file.
574 mime_type_t
* /* O - Type of file */
575 mimeFileType(mime_t
*mime
, /* I - MIME database */
576 const char *pathname
, /* I - Name of file to check on disk */
577 const char *filename
, /* I - Original filename or NULL */
578 int *compression
) /* O - Is the file compressed? */
580 _mime_filebuf_t fb
; /* File buffer */
581 const char *base
; /* Base filename of file */
582 mime_type_t
*type
, /* File type */
583 *best
; /* Best match */
586 DEBUG_printf(("mimeFileType(mime=%p, pathname=\"%s\", filename=\"%s\", "
587 "compression=%p)", mime
, pathname
, filename
, compression
));
590 * Range check input parameters...
593 if (!mime
|| !pathname
)
595 DEBUG_puts("1mimeFileType: Returning NULL.");
600 * Try to open the file...
603 if ((fb
.fp
= cupsFileOpen(pathname
, "r")) == NULL
)
605 DEBUG_printf(("1mimeFileType: Unable to open \"%s\": %s", pathname
,
607 DEBUG_puts("1mimeFileType: Returning NULL.");
612 * Then preload the first MIME_MAX_BUFFER bytes of the file into the file
613 * buffer, returning an error if we can't read anything...
617 fb
.length
= (int)cupsFileRead(fb
.fp
, (char *)fb
.buffer
, MIME_MAX_BUFFER
);
621 DEBUG_printf(("1mimeFileType: Unable to read from \"%s\": %s", pathname
, strerror(errno
)));
622 DEBUG_puts("1mimeFileType: Returning NULL.");
624 cupsFileClose(fb
.fp
);
630 * Figure out the base filename (without directory portion)...
635 if ((base
= strrchr(filename
, '/')) != NULL
)
640 else if ((base
= strrchr(pathname
, '/')) != NULL
)
646 * Then check it against all known types...
649 for (type
= (mime_type_t
*)cupsArrayFirst(mime
->types
), best
= NULL
;
651 type
= (mime_type_t
*)cupsArrayNext(mime
->types
))
652 if (mime_check_rules(base
, &fb
, type
->rules
))
654 if (!best
|| type
->priority
> best
->priority
)
659 * Finally, close the file and return a match (if any)...
664 *compression
= cupsFileCompression(fb
.fp
);
665 DEBUG_printf(("1mimeFileType: *compression=%d", *compression
));
668 cupsFileClose(fb
.fp
);
670 DEBUG_printf(("1mimeFileType: Returning %p(%s/%s).", best
,
671 best
? best
->super
: "???", best
? best
->type
: "???"));
677 * 'mimeType()' - Lookup a file type.
680 mime_type_t
* /* O - Matching file type definition */
681 mimeType(mime_t
*mime
, /* I - MIME database */
682 const char *super
, /* I - Super-type name */
683 const char *type
) /* I - Type name */
685 mime_type_t key
, /* MIME type search key */
686 *mt
; /* Matching type */
689 DEBUG_printf(("mimeType(mime=%p, super=\"%s\", type=\"%s\")", mime
, super
,
693 * Range check input...
696 if (!mime
|| !super
|| !type
)
698 DEBUG_puts("1mimeType: Returning NULL.");
703 * Lookup the type in the array...
706 strlcpy(key
.super
, super
, sizeof(key
.super
));
707 strlcpy(key
.type
, type
, sizeof(key
.type
));
709 mt
= (mime_type_t
*)cupsArrayFind(mime
->types
, &key
);
710 DEBUG_printf(("1mimeType: Returning %p.", mt
));
716 * 'mime_compare_types()' - Compare two MIME super/type names.
719 static int /* O - Result of comparison */
720 mime_compare_types(mime_type_t
*t0
, /* I - First type */
721 mime_type_t
*t1
) /* I - Second type */
723 int i
; /* Result of comparison */
726 if ((i
= _cups_strcasecmp(t0
->super
, t1
->super
)) == 0)
727 i
= _cups_strcasecmp(t0
->type
, t1
->type
);
734 * 'mime_check_rules()' - Check each rule in a list.
737 static int /* O - 1 if match, 0 if no match */
739 const char *filename
, /* I - Filename */
740 _mime_filebuf_t
*fb
, /* I - File to check */
741 mime_magic_t
*rules
) /* I - Rules to check */
743 int n
; /* Looping var */
744 int region
; /* Region to look at */
745 int logic
, /* Logic to apply */
746 result
; /* Result of test */
747 unsigned intv
; /* Integer value */
748 short shortv
; /* Short value */
749 unsigned char *bufptr
; /* Pointer into buffer */
752 DEBUG_printf(("4mime_check_rules(filename=\"%s\", fb=%p, rules=%p)", filename
,
758 if (rules
->parent
== NULL
)
759 logic
= MIME_MAGIC_OR
;
761 logic
= rules
->parent
->op
;
765 while (rules
!= NULL
)
768 * Compute the result of this rule...
773 case MIME_MAGIC_MATCH
:
774 result
= mime_patmatch(filename
, rules
->value
.matchv
);
777 case MIME_MAGIC_ASCII
:
779 * Load the buffer if necessary...
782 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
783 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
786 * Reload file buffer...
789 cupsFileSeek(fb
->fp
, rules
->offset
);
790 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
792 fb
->offset
= rules
->offset
;
794 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_ASCII fb->length=%d", fb
->length
));
798 * Test for ASCII printable characters plus standard control chars.
801 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
802 n
= fb
->offset
+ fb
->length
- rules
->offset
;
806 bufptr
= fb
->buffer
+ rules
->offset
- fb
->offset
;
808 if ((*bufptr
>= 32 && *bufptr
<= 126) ||
809 (*bufptr
>= 8 && *bufptr
<= 13) ||
810 *bufptr
== 26 || *bufptr
== 27)
821 case MIME_MAGIC_PRINTABLE
:
823 * Load the buffer if necessary...
826 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
827 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
830 * Reload file buffer...
833 cupsFileSeek(fb
->fp
, rules
->offset
);
834 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
836 fb
->offset
= rules
->offset
;
838 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_PRINTABLE fb->length=%d", fb
->length
));
842 * Test for 8-bit printable characters plus standard control chars.
845 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
846 n
= fb
->offset
+ fb
->length
- rules
->offset
;
850 bufptr
= fb
->buffer
+ rules
->offset
- fb
->offset
;
853 if (*bufptr
>= 128 ||
854 (*bufptr
>= 32 && *bufptr
<= 126) ||
855 (*bufptr
>= 8 && *bufptr
<= 13) ||
856 *bufptr
== 26 || *bufptr
== 27)
867 case MIME_MAGIC_REGEX
:
868 DEBUG_printf(("5mime_check_rules: regex(%d, \"%s\")", rules
->offset
,
869 rules
->value
.stringv
));
872 * Load the buffer if necessary...
875 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
876 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
879 * Reload file buffer...
882 cupsFileSeek(fb
->fp
, rules
->offset
);
883 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
885 fb
->offset
= rules
->offset
;
887 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_REGEX fb->length=%d", fb
->length
));
889 DEBUG_printf(("5mime_check_rules: loaded %d byte fb->buffer at %d, starts "
890 "with \"%c%c%c%c\".",
891 fb
->length
, fb
->offset
, fb
->buffer
[0], fb
->buffer
[1],
892 fb
->buffer
[2], fb
->buffer
[3]));
896 * Compare the buffer against the string. If the file is too
897 * short then don't compare - it can't match...
902 char temp
[MIME_MAX_BUFFER
+ 1];
903 /* Temporary buffer */
905 memcpy(temp
, fb
->buffer
, (size_t)fb
->length
);
906 temp
[fb
->length
] = '\0';
907 result
= !regexec(&(rules
->value
.rev
), temp
, 0, NULL
, 0);
910 DEBUG_printf(("5mime_check_rules: result=%d", result
));
913 case MIME_MAGIC_STRING
:
914 DEBUG_printf(("5mime_check_rules: string(%d, \"%s\")", rules
->offset
,
915 rules
->value
.stringv
));
918 * Load the buffer if necessary...
921 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
922 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
925 * Reload file buffer...
928 cupsFileSeek(fb
->fp
, rules
->offset
);
929 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
931 fb
->offset
= rules
->offset
;
933 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_STRING fb->length=%d", fb
->length
));
935 DEBUG_printf(("5mime_check_rules: loaded %d byte fb->buffer at %d, starts "
936 "with \"%c%c%c%c\".",
937 fb
->length
, fb
->offset
, fb
->buffer
[0], fb
->buffer
[1],
938 fb
->buffer
[2], fb
->buffer
[3]));
942 * Compare the buffer against the string. If the file is too
943 * short then don't compare - it can't match...
946 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
949 result
= !memcmp(fb
->buffer
+ rules
->offset
- fb
->offset
, rules
->value
.stringv
, (size_t)rules
->length
);
950 DEBUG_printf(("5mime_check_rules: result=%d", result
));
953 case MIME_MAGIC_ISTRING
:
955 * Load the buffer if necessary...
958 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
959 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
962 * Reload file buffer...
965 cupsFileSeek(fb
->fp
, rules
->offset
);
966 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
968 fb
->offset
= rules
->offset
;
970 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_ISTRING fb->length=%d", fb
->length
));
974 * Compare the buffer against the string. If the file is too
975 * short then don't compare - it can't match...
978 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
981 result
= !_cups_strncasecmp((char *)fb
->buffer
+ rules
->offset
- fb
->offset
, rules
->value
.stringv
, (size_t)rules
->length
);
984 case MIME_MAGIC_CHAR
:
986 * Load the buffer if necessary...
989 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
)
992 * Reload file buffer...
995 cupsFileSeek(fb
->fp
, rules
->offset
);
996 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
998 fb
->offset
= rules
->offset
;
1000 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_CHAR fb->length=%d", fb
->length
));
1004 * Compare the character values; if the file is too short, it
1011 result
= (fb
->buffer
[rules
->offset
- fb
->offset
] ==
1012 rules
->value
.charv
);
1015 case MIME_MAGIC_SHORT
:
1017 * Load the buffer if necessary...
1020 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
1021 (rules
->offset
+ 2) > (fb
->offset
+ fb
->length
))
1024 * Reload file buffer...
1027 cupsFileSeek(fb
->fp
, rules
->offset
);
1028 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
1029 sizeof(fb
->buffer
));
1030 fb
->offset
= rules
->offset
;
1032 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_SHORT fb->length=%d", fb
->length
));
1036 * Compare the short values; if the file is too short, it
1044 bufptr
= fb
->buffer
+ rules
->offset
- fb
->offset
;
1045 shortv
= (short)((bufptr
[0] << 8) | bufptr
[1]);
1046 result
= (shortv
== rules
->value
.shortv
);
1050 case MIME_MAGIC_INT
:
1052 * Load the buffer if necessary...
1055 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
1056 (rules
->offset
+ 4) > (fb
->offset
+ fb
->length
))
1059 * Reload file buffer...
1062 cupsFileSeek(fb
->fp
, rules
->offset
);
1063 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
1064 sizeof(fb
->buffer
));
1065 fb
->offset
= rules
->offset
;
1067 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_INT fb->length=%d", fb
->length
));
1071 * Compare the int values; if the file is too short, it
1079 bufptr
= fb
->buffer
+ rules
->offset
- fb
->offset
;
1080 intv
= (unsigned)((((((bufptr
[0] << 8) | bufptr
[1]) << 8) | bufptr
[2]) << 8) | bufptr
[3]);
1081 result
= (intv
== rules
->value
.intv
);
1085 case MIME_MAGIC_LOCALE
:
1086 #if defined(WIN32) || defined(__EMX__) || defined(__APPLE__)
1087 result
= !strcmp(rules
->value
.localev
, setlocale(LC_ALL
, ""));
1089 result
= !strcmp(rules
->value
.localev
, setlocale(LC_MESSAGES
, ""));
1090 #endif /* __APPLE__ */
1093 case MIME_MAGIC_CONTAINS
:
1095 * Load the buffer if necessary...
1098 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
1099 (rules
->offset
+ rules
->region
) > (fb
->offset
+ fb
->length
))
1102 * Reload file buffer...
1105 cupsFileSeek(fb
->fp
, rules
->offset
);
1106 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
1107 sizeof(fb
->buffer
));
1108 fb
->offset
= rules
->offset
;
1110 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_CONTAINS fb->length=%d", fb
->length
));
1114 * Compare the buffer against the string. If the file is too
1115 * short then don't compare - it can't match...
1118 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
1122 if (fb
->length
> rules
->region
)
1123 region
= rules
->region
- rules
->length
;
1125 region
= fb
->length
- rules
->length
;
1127 for (n
= 0; n
< region
; n
++)
1128 if ((result
= (memcmp(fb
->buffer
+ rules
->offset
- fb
->offset
+ n
, rules
->value
.stringv
, (size_t)rules
->length
) == 0)) != 0)
1134 if (rules
->child
!= NULL
)
1135 result
= mime_check_rules(filename
, fb
, rules
->child
);
1142 * If the logic is inverted, invert the result...
1149 * OK, now if the current logic is OR and this result is true, the this
1150 * rule set is true. If the current logic is AND and this result is false,
1151 * the the rule set is false...
1154 DEBUG_printf(("5mime_check_rules: result of test %p (MIME_MAGIC_%s) is %d",
1155 rules
, debug_ops
[rules
->op
], result
));
1157 if ((result
&& logic
== MIME_MAGIC_OR
) ||
1158 (!result
&& logic
== MIME_MAGIC_AND
))
1162 * Otherwise the jury is still out on this one, so move to the next rule.
1165 rules
= rules
->next
;
1173 * 'mime_patmatch()' - Pattern matching.
1176 static int /* O - 1 if match, 0 if no match */
1177 mime_patmatch(const char *s
, /* I - String to match against */
1178 const char *pat
) /* I - Pattern to match against */
1181 * Range check the input...
1184 if (s
== NULL
|| pat
== NULL
)
1188 * Loop through the pattern and match strings, and stop if we come to a
1189 * point where the strings don't match or we find a complete match.
1192 while (*s
!= '\0' && *pat
!= '\0')
1197 * Wildcard - 0 or more characters...
1202 return (1); /* Last pattern char is *, so everything matches... */
1205 * Test all remaining combinations until we get to the end of the string.
1210 if (mime_patmatch(s
, pat
))
1216 else if (*pat
== '?')
1219 * Wildcard - 1 character...
1226 else if (*pat
== '[')
1229 * Match a character from the input set [chars]...
1233 while (*pat
!= ']' && *pat
!= '\0')
1239 if (*pat
== ']' || *pat
== '\0')
1242 while (*pat
!= ']' && *pat
!= '\0')
1250 else if (*pat
== '\\')
1253 * Handle quoted characters...
1260 * Stop if the pattern and string don't match...
1268 * Done parsing the pattern and string; return 1 if the last character
1269 * matches and 0 otherwise...
1272 return (*s
== *pat
);