]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/type.c
2 * MIME typing routines for CUPS.
4 * Copyright © 2007-2019 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
12 * Include necessary headers...
15 #include <cups/string-private.h>
21 * Debug macros that used to be private API...
25 #define DEBUG_printf(...)
32 typedef struct _mime_filebuf_s
/**** File buffer for MIME typing ****/
34 cups_file_t
*fp
; /* File pointer */
35 int offset
, /* Offset in file */
36 length
; /* Length of buffered data */
37 unsigned char buffer
[MIME_MAX_BUFFER
];/* Buffered data */
45 static int mime_compare_types(mime_type_t
*t0
, mime_type_t
*t1
);
46 static int mime_check_rules(const char *filename
, _mime_filebuf_t
*fb
,
48 static int mime_patmatch(const char *s
, const char *pat
);
56 static const char * const debug_ops
[] =
58 "NOP", /* No operation */
59 "AND", /* Logical AND of all children */
60 "OR", /* Logical OR of all children */
61 "MATCH", /* Filename match */
62 "ASCII", /* ASCII characters in range */
63 "PRINTABLE", /* Printable characters (32-255) */
64 "STRING", /* String matches */
65 "CHAR", /* Character/byte matches */
66 "SHORT", /* Short/16-bit word matches */
67 "INT", /* Integer/32-bit word matches */
68 "LOCALE", /* Current locale matches string */
69 "CONTAINS", /* File contains a string */
70 "ISTRING", /* Case-insensitive string matches */
71 "REGEX" /* Regular expression matches */
77 * 'mimeAddType()' - Add a MIME type to a database.
80 mime_type_t
* /* O - New (or existing) MIME type */
81 mimeAddType(mime_t
*mime
, /* I - MIME database */
82 const char *super
, /* I - Super-type name */
83 const char *type
) /* I - Type name */
85 mime_type_t
*temp
; /* New MIME type */
86 size_t typelen
; /* Length of type name */
89 DEBUG_printf(("mimeAddType(mime=%p, super=\"%s\", type=\"%s\")", mime
, super
,
93 * Range check input...
96 if (!mime
|| !super
|| !type
)
98 DEBUG_puts("1mimeAddType: Returning NULL (bad arguments).");
103 * See if the type already exists; if so, return the existing type...
106 if ((temp
= mimeType(mime
, super
, type
)) != NULL
)
108 DEBUG_printf(("1mimeAddType: Returning %p (existing).", temp
));
113 * The type doesn't exist; add it...
117 mime
->types
= cupsArrayNew((cups_array_func_t
)mime_compare_types
, NULL
);
121 DEBUG_puts("1mimeAddType: Returning NULL (no types).");
125 typelen
= strlen(type
) + 1;
127 if ((temp
= calloc(1, sizeof(mime_type_t
) - MIME_MAX_TYPE
+ typelen
)) == NULL
)
129 DEBUG_puts("1mimeAddType: Returning NULL (out of memory).");
133 strlcpy(temp
->super
, super
, sizeof(temp
->super
));
134 memcpy(temp
->type
, type
, typelen
);
135 temp
->priority
= 100;
137 cupsArrayAdd(mime
->types
, temp
);
139 DEBUG_printf(("1mimeAddType: Returning %p (new).", temp
));
145 * 'mimeAddTypeRule()' - Add a detection rule for a file type.
148 int /* O - 0 on success, -1 on failure */
149 mimeAddTypeRule(mime_type_t
*mt
, /* I - Type to add to */
150 const char *rule
) /* I - Rule to add */
152 int num_values
, /* Number of values seen */
153 op
, /* Operation code */
154 logic
, /* Logic for next rule */
155 invert
; /* Invert following rule? */
156 char name
[255], /* Name in rule string */
157 value
[3][255], /* Value in rule string */
158 *ptr
, /* Position in name or value */
159 quote
; /* Quote character */
160 int length
[3]; /* Length of each parameter */
161 mime_magic_t
*temp
, /* New rule */
162 *current
; /* Current rule */
165 DEBUG_printf(("mimeAddTypeRule(mt=%p(%s/%s), rule=\"%s\")", mt
,
166 mt
? mt
->super
: "???", mt
? mt
->type
: "???", rule
));
169 * Range check input...
176 * Find the last rule in the top-level of the rules tree.
179 for (current
= mt
->rules
; current
; current
= current
->next
)
184 * Parse the rules string. Most rules are either a file extension or a
185 * comparison function:
188 * function(parameters)
191 logic
= MIME_MAGIC_NOP
;
194 while (*rule
!= '\0')
196 while (isspace(*rule
& 255))
201 DEBUG_puts("1mimeAddTypeRule: New parenthesis group");
202 logic
= MIME_MAGIC_NOP
;
205 else if (*rule
== ')')
207 DEBUG_puts("1mimeAddTypeRule: Close paren...");
208 if (current
== NULL
|| current
->parent
== NULL
)
211 current
= current
->parent
;
213 if (current
->parent
== NULL
)
214 logic
= MIME_MAGIC_OR
;
216 logic
= current
->parent
->op
;
220 else if (*rule
== '+' && current
!= NULL
)
222 if (logic
!= MIME_MAGIC_AND
&&
223 current
!= NULL
&& current
->prev
!= NULL
)
226 * OK, we have more than 1 rule in the current tree level... Make a
227 * new group tree and move the previous rule to it...
230 if ((temp
= calloc(1, sizeof(mime_magic_t
))) == NULL
)
233 temp
->op
= MIME_MAGIC_AND
;
234 temp
->child
= current
;
235 temp
->parent
= current
->parent
;
236 current
->prev
->next
= temp
;
237 temp
->prev
= current
->prev
;
239 current
->prev
= NULL
;
240 current
->parent
= temp
;
242 DEBUG_printf(("1mimeAddTypeRule: Creating new AND group %p.", temp
));
244 else if (current
->parent
)
246 DEBUG_printf(("1mimeAddTypeRule: Setting group %p op to AND.",
248 current
->parent
->op
= MIME_MAGIC_AND
;
251 logic
= MIME_MAGIC_AND
;
254 else if (*rule
== ',')
256 if (logic
!= MIME_MAGIC_OR
&& current
!= NULL
)
259 * OK, we have two possibilities; either this is the top-level rule or
260 * we have a bunch of AND rules at this level.
263 if (current
->parent
== NULL
)
266 * This is the top-level rule; we have to move *all* of the AND rules
267 * down a level, as AND has precedence over OR.
270 if ((temp
= calloc(1, sizeof(mime_magic_t
))) == NULL
)
273 DEBUG_printf(("1mimeAddTypeRule: Creating new AND group %p inside OR "
276 while (current
->prev
!= NULL
)
278 current
->parent
= temp
;
279 current
= current
->prev
;
282 current
->parent
= temp
;
283 temp
->op
= MIME_MAGIC_AND
;
284 temp
->child
= current
;
286 mt
->rules
= current
= temp
;
291 * This isn't the top rule, so go up one level...
294 DEBUG_puts("1mimeAddTypeRule: Going up one level.");
295 current
= current
->parent
;
299 logic
= MIME_MAGIC_OR
;
302 else if (*rule
== '!')
304 DEBUG_puts("1mimeAddTypeRule: NOT");
308 else if (isalnum(*rule
& 255))
311 * Read an extension name or a function...
315 while (isalnum(*rule
& 255) && (size_t)(ptr
- name
) < (sizeof(name
) - 1))
323 * Read function parameters...
328 num_values
< (int)(sizeof(value
) / sizeof(value
[0]));
331 ptr
= value
[num_values
];
333 while ((size_t)(ptr
- value
[num_values
]) < (sizeof(value
[0]) - 1) &&
334 *rule
!= '\0' && *rule
!= ',' && *rule
!= ')')
336 if (isspace(*rule
& 255))
339 * Ignore whitespace...
345 else if (*rule
== '\"' || *rule
== '\'')
348 * Copy quoted strings literally...
353 while (*rule
!= '\0' && *rule
!= quote
&&
354 (size_t)(ptr
- value
[num_values
]) < (sizeof(value
[0]) - 1))
362 else if (*rule
== '<')
366 while (*rule
!= '>' && *rule
!= '\0' &&
367 (size_t)(ptr
- value
[num_values
]) < (sizeof(value
[0]) - 1))
369 if (isxdigit(rule
[0] & 255) && isxdigit(rule
[1] & 255))
372 *ptr
= (char)((*rule
++ - '0') << 4);
374 *ptr
= (char)((tolower(*rule
++) - 'a' + 10) << 4);
377 *ptr
++ |= *rule
++ - '0';
379 *ptr
++ |= tolower(*rule
++) - 'a' + 10;
395 length
[num_values
] = ptr
- value
[num_values
];
412 * Figure out the function...
415 if (!strcmp(name
, "match"))
416 op
= MIME_MAGIC_MATCH
;
417 else if (!strcmp(name
, "ascii"))
418 op
= MIME_MAGIC_ASCII
;
419 else if (!strcmp(name
, "printable"))
420 op
= MIME_MAGIC_PRINTABLE
;
421 else if (!strcmp(name
, "regex"))
422 op
= MIME_MAGIC_REGEX
;
423 else if (!strcmp(name
, "string"))
424 op
= MIME_MAGIC_STRING
;
425 else if (!strcmp(name
, "istring"))
426 op
= MIME_MAGIC_ISTRING
;
427 else if (!strcmp(name
, "char"))
428 op
= MIME_MAGIC_CHAR
;
429 else if (!strcmp(name
, "short"))
430 op
= MIME_MAGIC_SHORT
;
431 else if (!strcmp(name
, "int"))
433 else if (!strcmp(name
, "locale"))
434 op
= MIME_MAGIC_LOCALE
;
435 else if (!strcmp(name
, "contains"))
436 op
= MIME_MAGIC_CONTAINS
;
437 else if (!strcmp(name
, "priority") && num_values
== 1)
439 mt
->priority
= atoi(value
[0]);
448 * This is just a filename match on the extension...
451 snprintf(value
[0], sizeof(value
[0]), "*.%s", name
);
452 length
[0] = (int)strlen(value
[0]);
453 op
= MIME_MAGIC_MATCH
;
458 * Add a rule for this operation.
461 if ((temp
= calloc(1, sizeof(mime_magic_t
))) == NULL
)
464 temp
->invert
= (short)invert
;
467 temp
->parent
= current
->parent
;
468 current
->next
= temp
;
473 temp
->prev
= current
;
475 if (logic
== MIME_MAGIC_NOP
)
478 * Add parenthetical grouping...
481 DEBUG_printf(("1mimeAddTypeRule: Making new OR group %p for "
482 "parenthesis.", temp
));
484 temp
->op
= MIME_MAGIC_OR
;
486 if ((temp
->child
= calloc(1, sizeof(mime_magic_t
))) == NULL
)
489 temp
->child
->parent
= temp
;
490 temp
->child
->invert
= temp
->invert
;
494 logic
= MIME_MAGIC_OR
;
497 DEBUG_printf(("1mimeAddTypeRule: Adding %p: %s, op=MIME_MAGIC_%s(%d), "
498 "logic=MIME_MAGIC_%s, invert=%d.", temp
, name
,
499 debug_ops
[op
], op
, debug_ops
[logic
], invert
));
502 * Fill in data for the rule...
506 temp
->op
= (short)op
;
511 case MIME_MAGIC_MATCH
:
512 if ((size_t)length
[0] > (sizeof(temp
->value
.matchv
) - 1))
514 strlcpy(temp
->value
.matchv
, value
[0], sizeof(temp
->value
.matchv
));
516 case MIME_MAGIC_ASCII
:
517 case MIME_MAGIC_PRINTABLE
:
518 temp
->offset
= strtol(value
[0], NULL
, 0);
519 temp
->length
= strtol(value
[1], NULL
, 0);
520 if (temp
->length
> MIME_MAX_BUFFER
)
521 temp
->length
= MIME_MAX_BUFFER
;
523 case MIME_MAGIC_REGEX
:
524 temp
->offset
= strtol(value
[0], NULL
, 0);
525 temp
->length
= MIME_MAX_BUFFER
;
526 if (regcomp(&(temp
->value
.rev
), value
[1], REG_NOSUB
| REG_EXTENDED
))
529 case MIME_MAGIC_STRING
:
530 case MIME_MAGIC_ISTRING
:
531 temp
->offset
= strtol(value
[0], NULL
, 0);
532 if (num_values
< 2 || (size_t)length
[1] > sizeof(temp
->value
.stringv
))
534 temp
->length
= length
[1];
535 memcpy(temp
->value
.stringv
, value
[1], (size_t)length
[1]);
537 case MIME_MAGIC_CHAR
:
538 temp
->offset
= strtol(value
[0], NULL
, 0);
541 else if (length
[1] == 1)
542 temp
->value
.charv
= (unsigned char)value
[1][0];
544 temp
->value
.charv
= (unsigned char)strtol(value
[1], NULL
, 0);
546 DEBUG_printf(("1mimeAddTypeRule: CHAR(%d,0x%02x)", temp
->offset
,
549 case MIME_MAGIC_SHORT
:
550 temp
->offset
= strtol(value
[0], NULL
, 0);
551 temp
->value
.shortv
= (unsigned short)strtol(value
[1], NULL
, 0);
553 case MIME_MAGIC_INT
:
554 temp
->offset
= strtol(value
[0], NULL
, 0);
555 temp
->value
.intv
= (unsigned)strtol(value
[1], NULL
, 0);
557 case MIME_MAGIC_LOCALE
:
558 if ((size_t)length
[0] > (sizeof(temp
->value
.localev
) - 1))
561 strlcpy(temp
->value
.localev
, value
[0], sizeof(temp
->value
.localev
));
563 case MIME_MAGIC_CONTAINS
:
564 temp
->offset
= strtol(value
[0], NULL
, 0);
565 temp
->region
= strtol(value
[1], NULL
, 0);
566 if (num_values
< 3 || (size_t)length
[2] > sizeof(temp
->value
.stringv
))
568 temp
->length
= length
[2];
569 memcpy(temp
->value
.stringv
, value
[2], (size_t)length
[2]);
582 * 'mimeFileType()' - Determine the type of a file.
585 mime_type_t
* /* O - Type of file */
586 mimeFileType(mime_t
*mime
, /* I - MIME database */
587 const char *pathname
, /* I - Name of file to check on disk */
588 const char *filename
, /* I - Original filename or NULL */
589 int *compression
) /* O - Is the file compressed? */
591 _mime_filebuf_t fb
; /* File buffer */
592 const char *base
; /* Base filename of file */
593 mime_type_t
*type
, /* File type */
594 *best
; /* Best match */
597 DEBUG_printf(("mimeFileType(mime=%p, pathname=\"%s\", filename=\"%s\", "
598 "compression=%p)", mime
, pathname
, filename
, compression
));
601 * Range check input parameters...
604 if (!mime
|| !pathname
)
606 DEBUG_puts("1mimeFileType: Returning NULL.");
611 * Try to open the file...
614 if ((fb
.fp
= cupsFileOpen(pathname
, "r")) == NULL
)
616 DEBUG_printf(("1mimeFileType: Unable to open \"%s\": %s", pathname
,
618 DEBUG_puts("1mimeFileType: Returning NULL.");
623 * Then preload the first MIME_MAX_BUFFER bytes of the file into the file
624 * buffer, returning an error if we can't read anything...
628 fb
.length
= (int)cupsFileRead(fb
.fp
, (char *)fb
.buffer
, MIME_MAX_BUFFER
);
632 DEBUG_printf(("1mimeFileType: Unable to read from \"%s\": %s", pathname
, strerror(errno
)));
633 DEBUG_puts("1mimeFileType: Returning NULL.");
635 cupsFileClose(fb
.fp
);
641 * Figure out the base filename (without directory portion)...
646 if ((base
= strrchr(filename
, '/')) != NULL
)
651 else if ((base
= strrchr(pathname
, '/')) != NULL
)
657 * Then check it against all known types...
660 for (type
= (mime_type_t
*)cupsArrayFirst(mime
->types
), best
= NULL
;
662 type
= (mime_type_t
*)cupsArrayNext(mime
->types
))
663 if (mime_check_rules(base
, &fb
, type
->rules
))
665 if (!best
|| type
->priority
> best
->priority
)
670 * Finally, close the file and return a match (if any)...
675 *compression
= cupsFileCompression(fb
.fp
);
676 DEBUG_printf(("1mimeFileType: *compression=%d", *compression
));
679 cupsFileClose(fb
.fp
);
681 DEBUG_printf(("1mimeFileType: Returning %p(%s/%s).", best
,
682 best
? best
->super
: "???", best
? best
->type
: "???"));
688 * 'mimeType()' - Lookup a file type.
691 mime_type_t
* /* O - Matching file type definition */
692 mimeType(mime_t
*mime
, /* I - MIME database */
693 const char *super
, /* I - Super-type name */
694 const char *type
) /* I - Type name */
696 mime_type_t key
, /* MIME type search key */
697 *mt
; /* Matching type */
700 DEBUG_printf(("mimeType(mime=%p, super=\"%s\", type=\"%s\")", mime
, super
,
704 * Range check input...
707 if (!mime
|| !super
|| !type
)
709 DEBUG_puts("1mimeType: Returning NULL.");
714 * Lookup the type in the array...
717 strlcpy(key
.super
, super
, sizeof(key
.super
));
718 strlcpy(key
.type
, type
, sizeof(key
.type
));
720 mt
= (mime_type_t
*)cupsArrayFind(mime
->types
, &key
);
721 DEBUG_printf(("1mimeType: Returning %p.", mt
));
727 * 'mime_compare_types()' - Compare two MIME super/type names.
730 static int /* O - Result of comparison */
731 mime_compare_types(mime_type_t
*t0
, /* I - First type */
732 mime_type_t
*t1
) /* I - Second type */
734 int i
; /* Result of comparison */
737 if ((i
= _cups_strcasecmp(t0
->super
, t1
->super
)) == 0)
738 i
= _cups_strcasecmp(t0
->type
, t1
->type
);
745 * 'mime_check_rules()' - Check each rule in a list.
748 static int /* O - 1 if match, 0 if no match */
750 const char *filename
, /* I - Filename */
751 _mime_filebuf_t
*fb
, /* I - File to check */
752 mime_magic_t
*rules
) /* I - Rules to check */
754 int n
; /* Looping var */
755 int region
; /* Region to look at */
756 int logic
, /* Logic to apply */
757 result
; /* Result of test */
758 unsigned intv
; /* Integer value */
759 short shortv
; /* Short value */
760 unsigned char *bufptr
; /* Pointer into buffer */
763 DEBUG_printf(("4mime_check_rules(filename=\"%s\", fb=%p, rules=%p)", filename
,
769 if (rules
->parent
== NULL
)
770 logic
= MIME_MAGIC_OR
;
772 logic
= rules
->parent
->op
;
776 while (rules
!= NULL
)
779 * Compute the result of this rule...
784 case MIME_MAGIC_MATCH
:
785 result
= mime_patmatch(filename
, rules
->value
.matchv
);
788 case MIME_MAGIC_ASCII
:
790 * Load the buffer if necessary...
793 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
794 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
797 * Reload file buffer...
800 cupsFileSeek(fb
->fp
, rules
->offset
);
801 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
803 fb
->offset
= rules
->offset
;
805 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_ASCII fb->length=%d", fb
->length
));
809 * Test for ASCII printable characters plus standard control chars.
812 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
813 n
= fb
->offset
+ fb
->length
- rules
->offset
;
817 bufptr
= fb
->buffer
+ rules
->offset
- fb
->offset
;
819 if ((*bufptr
>= 32 && *bufptr
<= 126) ||
820 (*bufptr
>= 8 && *bufptr
<= 13) ||
821 *bufptr
== 26 || *bufptr
== 27)
832 case MIME_MAGIC_PRINTABLE
:
834 * Load the buffer if necessary...
837 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
838 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
841 * Reload file buffer...
844 cupsFileSeek(fb
->fp
, rules
->offset
);
845 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
847 fb
->offset
= rules
->offset
;
849 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_PRINTABLE fb->length=%d", fb
->length
));
853 * Test for 8-bit printable characters plus standard control chars.
856 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
857 n
= fb
->offset
+ fb
->length
- rules
->offset
;
861 bufptr
= fb
->buffer
+ rules
->offset
- fb
->offset
;
864 if (*bufptr
>= 128 ||
865 (*bufptr
>= 32 && *bufptr
<= 126) ||
866 (*bufptr
>= 8 && *bufptr
<= 13) ||
867 *bufptr
== 26 || *bufptr
== 27)
878 case MIME_MAGIC_REGEX
:
879 DEBUG_printf(("5mime_check_rules: regex(%d, \"%s\")", rules
->offset
,
880 rules
->value
.stringv
));
883 * Load the buffer if necessary...
886 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
887 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
890 * Reload file buffer...
893 cupsFileSeek(fb
->fp
, rules
->offset
);
894 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
896 fb
->offset
= rules
->offset
;
898 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_REGEX fb->length=%d", fb
->length
));
900 DEBUG_printf(("5mime_check_rules: loaded %d byte fb->buffer at %d, starts "
901 "with \"%c%c%c%c\".",
902 fb
->length
, fb
->offset
, fb
->buffer
[0], fb
->buffer
[1],
903 fb
->buffer
[2], fb
->buffer
[3]));
907 * Compare the buffer against the string. If the file is too
908 * short then don't compare - it can't match...
913 char temp
[MIME_MAX_BUFFER
+ 1];
914 /* Temporary buffer */
916 memcpy(temp
, fb
->buffer
, (size_t)fb
->length
);
917 temp
[fb
->length
] = '\0';
918 result
= !regexec(&(rules
->value
.rev
), temp
, 0, NULL
, 0);
921 DEBUG_printf(("5mime_check_rules: result=%d", result
));
924 case MIME_MAGIC_STRING
:
925 DEBUG_printf(("5mime_check_rules: string(%d, \"%s\")", rules
->offset
,
926 rules
->value
.stringv
));
929 * Load the buffer if necessary...
932 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
933 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
936 * Reload file buffer...
939 cupsFileSeek(fb
->fp
, rules
->offset
);
940 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
942 fb
->offset
= rules
->offset
;
944 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_STRING fb->length=%d", fb
->length
));
946 DEBUG_printf(("5mime_check_rules: loaded %d byte fb->buffer at %d, starts "
947 "with \"%c%c%c%c\".",
948 fb
->length
, fb
->offset
, fb
->buffer
[0], fb
->buffer
[1],
949 fb
->buffer
[2], fb
->buffer
[3]));
953 * Compare the buffer against the string. If the file is too
954 * short then don't compare - it can't match...
957 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
960 result
= !memcmp(fb
->buffer
+ rules
->offset
- fb
->offset
, rules
->value
.stringv
, (size_t)rules
->length
);
961 DEBUG_printf(("5mime_check_rules: result=%d", result
));
964 case MIME_MAGIC_ISTRING
:
966 * Load the buffer if necessary...
969 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
970 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
973 * Reload file buffer...
976 cupsFileSeek(fb
->fp
, rules
->offset
);
977 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
979 fb
->offset
= rules
->offset
;
981 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_ISTRING fb->length=%d", fb
->length
));
985 * Compare the buffer against the string. If the file is too
986 * short then don't compare - it can't match...
989 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
992 result
= !_cups_strncasecmp((char *)fb
->buffer
+ rules
->offset
- fb
->offset
, rules
->value
.stringv
, (size_t)rules
->length
);
995 case MIME_MAGIC_CHAR
:
997 * Load the buffer if necessary...
1000 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
)
1003 * Reload file buffer...
1006 cupsFileSeek(fb
->fp
, rules
->offset
);
1007 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
1008 sizeof(fb
->buffer
));
1009 fb
->offset
= rules
->offset
;
1011 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_CHAR fb->length=%d", fb
->length
));
1015 * Compare the character values; if the file is too short, it
1022 result
= (fb
->buffer
[rules
->offset
- fb
->offset
] ==
1023 rules
->value
.charv
);
1026 case MIME_MAGIC_SHORT
:
1028 * Load the buffer if necessary...
1031 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
1032 (rules
->offset
+ 2) > (fb
->offset
+ fb
->length
))
1035 * Reload file buffer...
1038 cupsFileSeek(fb
->fp
, rules
->offset
);
1039 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
1040 sizeof(fb
->buffer
));
1041 fb
->offset
= rules
->offset
;
1043 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_SHORT fb->length=%d", fb
->length
));
1047 * Compare the short values; if the file is too short, it
1055 bufptr
= fb
->buffer
+ rules
->offset
- fb
->offset
;
1056 shortv
= (short)((bufptr
[0] << 8) | bufptr
[1]);
1057 result
= (shortv
== rules
->value
.shortv
);
1061 case MIME_MAGIC_INT
:
1063 * Load the buffer if necessary...
1066 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
1067 (rules
->offset
+ 4) > (fb
->offset
+ fb
->length
))
1070 * Reload file buffer...
1073 cupsFileSeek(fb
->fp
, rules
->offset
);
1074 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
1075 sizeof(fb
->buffer
));
1076 fb
->offset
= rules
->offset
;
1078 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_INT fb->length=%d", fb
->length
));
1082 * Compare the int values; if the file is too short, it
1090 bufptr
= fb
->buffer
+ rules
->offset
- fb
->offset
;
1091 intv
= (unsigned)((((((bufptr
[0] << 8) | bufptr
[1]) << 8) | bufptr
[2]) << 8) | bufptr
[3]);
1092 result
= (intv
== rules
->value
.intv
);
1096 case MIME_MAGIC_LOCALE
:
1097 #if defined(_WIN32) || defined(__EMX__) || defined(__APPLE__)
1098 result
= !strcmp(rules
->value
.localev
, setlocale(LC_ALL
, ""));
1100 result
= !strcmp(rules
->value
.localev
, setlocale(LC_MESSAGES
, ""));
1101 #endif /* __APPLE__ */
1104 case MIME_MAGIC_CONTAINS
:
1106 * Load the buffer if necessary...
1109 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
1110 (rules
->offset
+ rules
->region
) > (fb
->offset
+ fb
->length
))
1113 * Reload file buffer...
1116 cupsFileSeek(fb
->fp
, rules
->offset
);
1117 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
1118 sizeof(fb
->buffer
));
1119 fb
->offset
= rules
->offset
;
1121 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_CONTAINS fb->length=%d", fb
->length
));
1125 * Compare the buffer against the string. If the file is too
1126 * short then don't compare - it can't match...
1129 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
1133 if (fb
->length
> rules
->region
)
1134 region
= rules
->region
- rules
->length
;
1136 region
= fb
->length
- rules
->length
;
1138 for (n
= 0; n
< region
; n
++)
1139 if ((result
= (memcmp(fb
->buffer
+ rules
->offset
- fb
->offset
+ n
, rules
->value
.stringv
, (size_t)rules
->length
) == 0)) != 0)
1145 if (rules
->child
!= NULL
)
1146 result
= mime_check_rules(filename
, fb
, rules
->child
);
1153 * If the logic is inverted, invert the result...
1160 * OK, now if the current logic is OR and this result is true, the this
1161 * rule set is true. If the current logic is AND and this result is false,
1162 * the the rule set is false...
1165 DEBUG_printf(("5mime_check_rules: result of test %p (MIME_MAGIC_%s) is %d",
1166 rules
, debug_ops
[rules
->op
], result
));
1168 if ((result
&& logic
== MIME_MAGIC_OR
) ||
1169 (!result
&& logic
== MIME_MAGIC_AND
))
1173 * Otherwise the jury is still out on this one, so move to the next rule.
1176 rules
= rules
->next
;
1184 * 'mime_patmatch()' - Pattern matching.
1187 static int /* O - 1 if match, 0 if no match */
1188 mime_patmatch(const char *s
, /* I - String to match against */
1189 const char *pat
) /* I - Pattern to match against */
1192 * Range check the input...
1195 if (s
== NULL
|| pat
== NULL
)
1199 * Loop through the pattern and match strings, and stop if we come to a
1200 * point where the strings don't match or we find a complete match.
1203 while (*s
!= '\0' && *pat
!= '\0')
1208 * Wildcard - 0 or more characters...
1213 return (1); /* Last pattern char is *, so everything matches... */
1216 * Test all remaining combinations until we get to the end of the string.
1221 if (mime_patmatch(s
, pat
))
1227 else if (*pat
== '?')
1230 * Wildcard - 1 character...
1237 else if (*pat
== '[')
1240 * Match a character from the input set [chars]...
1244 while (*pat
!= ']' && *pat
!= '\0')
1250 if (*pat
== ']' || *pat
== '\0')
1253 while (*pat
!= ']' && *pat
!= '\0')
1261 else if (*pat
== '\\')
1264 * Handle quoted characters...
1271 * Stop if the pattern and string don't match...
1279 * Done parsing the pattern and string; return 1 if the last character
1280 * matches and 0 otherwise...
1283 return (*s
== *pat
);