]>
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>
20 * Debug macros that used to be private API...
24 #define DEBUG_printf(...)
31 typedef struct _mime_filebuf_s
/**** File buffer for MIME typing ****/
33 cups_file_t
*fp
; /* File pointer */
34 int offset
, /* Offset in file */
35 length
; /* Length of buffered data */
36 unsigned char buffer
[MIME_MAX_BUFFER
];/* Buffered data */
44 static int mime_compare_types(mime_type_t
*t0
, mime_type_t
*t1
);
45 static int mime_check_rules(const char *filename
, _mime_filebuf_t
*fb
,
47 static int mime_patmatch(const char *s
, const char *pat
);
55 static const char * const debug_ops
[] =
57 "NOP", /* No operation */
58 "AND", /* Logical AND of all children */
59 "OR", /* Logical OR of all children */
60 "MATCH", /* Filename match */
61 "ASCII", /* ASCII characters in range */
62 "PRINTABLE", /* Printable characters (32-255) */
63 "STRING", /* String matches */
64 "CHAR", /* Character/byte matches */
65 "SHORT", /* Short/16-bit word matches */
66 "INT", /* Integer/32-bit word matches */
67 "LOCALE", /* Current locale matches string */
68 "CONTAINS", /* File contains a string */
69 "ISTRING", /* Case-insensitive string matches */
70 "REGEX" /* Regular expression matches */
76 * 'mimeAddType()' - Add a MIME type to a database.
79 mime_type_t
* /* O - New (or existing) MIME type */
80 mimeAddType(mime_t
*mime
, /* I - MIME database */
81 const char *super
, /* I - Super-type name */
82 const char *type
) /* I - Type name */
84 mime_type_t
*temp
; /* New MIME type */
85 size_t typelen
; /* Length of type name */
88 DEBUG_printf(("mimeAddType(mime=%p, super=\"%s\", type=\"%s\")", mime
, super
,
92 * Range check input...
95 if (!mime
|| !super
|| !type
)
97 DEBUG_puts("1mimeAddType: Returning NULL (bad arguments).");
102 * See if the type already exists; if so, return the existing type...
105 if ((temp
= mimeType(mime
, super
, type
)) != NULL
)
107 DEBUG_printf(("1mimeAddType: Returning %p (existing).", temp
));
112 * The type doesn't exist; add it...
116 mime
->types
= cupsArrayNew((cups_array_func_t
)mime_compare_types
, NULL
);
120 DEBUG_puts("1mimeAddType: Returning NULL (no types).");
124 typelen
= strlen(type
) + 1;
126 if ((temp
= calloc(1, sizeof(mime_type_t
) - MIME_MAX_TYPE
+ typelen
)) == NULL
)
128 DEBUG_puts("1mimeAddType: Returning NULL (out of memory).");
132 strlcpy(temp
->super
, super
, sizeof(temp
->super
));
133 memcpy(temp
->type
, type
, typelen
);
134 temp
->priority
= 100;
136 cupsArrayAdd(mime
->types
, temp
);
138 DEBUG_printf(("1mimeAddType: Returning %p (new).", temp
));
144 * 'mimeAddTypeRule()' - Add a detection rule for a file type.
147 int /* O - 0 on success, -1 on failure */
148 mimeAddTypeRule(mime_type_t
*mt
, /* I - Type to add to */
149 const char *rule
) /* I - Rule to add */
151 int num_values
, /* Number of values seen */
152 op
, /* Operation code */
153 logic
, /* Logic for next rule */
154 invert
; /* Invert following rule? */
155 char name
[255], /* Name in rule string */
156 value
[3][255], /* Value in rule string */
157 *ptr
, /* Position in name or value */
158 quote
; /* Quote character */
159 int length
[3]; /* Length of each parameter */
160 mime_magic_t
*temp
, /* New rule */
161 *current
; /* Current rule */
164 DEBUG_printf(("mimeAddTypeRule(mt=%p(%s/%s), rule=\"%s\")", mt
,
165 mt
? mt
->super
: "???", mt
? mt
->type
: "???", rule
));
168 * Range check input...
175 * Find the last rule in the top-level of the rules tree.
178 for (current
= mt
->rules
; current
; current
= current
->next
)
183 * Parse the rules string. Most rules are either a file extension or a
184 * comparison function:
187 * function(parameters)
190 logic
= MIME_MAGIC_NOP
;
193 while (*rule
!= '\0')
195 while (isspace(*rule
& 255))
200 DEBUG_puts("1mimeAddTypeRule: New parenthesis group");
201 logic
= MIME_MAGIC_NOP
;
204 else if (*rule
== ')')
206 DEBUG_puts("1mimeAddTypeRule: Close paren...");
207 if (current
== NULL
|| current
->parent
== NULL
)
210 current
= current
->parent
;
212 if (current
->parent
== NULL
)
213 logic
= MIME_MAGIC_OR
;
215 logic
= current
->parent
->op
;
219 else if (*rule
== '+' && current
!= NULL
)
221 if (logic
!= MIME_MAGIC_AND
&&
222 current
!= NULL
&& current
->prev
!= NULL
)
225 * OK, we have more than 1 rule in the current tree level... Make a
226 * new group tree and move the previous rule to it...
229 if ((temp
= calloc(1, sizeof(mime_magic_t
))) == NULL
)
232 temp
->op
= MIME_MAGIC_AND
;
233 temp
->child
= current
;
234 temp
->parent
= current
->parent
;
235 current
->prev
->next
= temp
;
236 temp
->prev
= current
->prev
;
238 current
->prev
= NULL
;
239 current
->parent
= temp
;
241 DEBUG_printf(("1mimeAddTypeRule: Creating new AND group %p.", temp
));
243 else if (current
->parent
)
245 DEBUG_printf(("1mimeAddTypeRule: Setting group %p op to AND.",
247 current
->parent
->op
= MIME_MAGIC_AND
;
250 logic
= MIME_MAGIC_AND
;
253 else if (*rule
== ',')
255 if (logic
!= MIME_MAGIC_OR
&& current
!= NULL
)
258 * OK, we have two possibilities; either this is the top-level rule or
259 * we have a bunch of AND rules at this level.
262 if (current
->parent
== NULL
)
265 * This is the top-level rule; we have to move *all* of the AND rules
266 * down a level, as AND has precedence over OR.
269 if ((temp
= calloc(1, sizeof(mime_magic_t
))) == NULL
)
272 DEBUG_printf(("1mimeAddTypeRule: Creating new AND group %p inside OR "
275 while (current
->prev
!= NULL
)
277 current
->parent
= temp
;
278 current
= current
->prev
;
281 current
->parent
= temp
;
282 temp
->op
= MIME_MAGIC_AND
;
283 temp
->child
= current
;
285 mt
->rules
= current
= temp
;
290 * This isn't the top rule, so go up one level...
293 DEBUG_puts("1mimeAddTypeRule: Going up one level.");
294 current
= current
->parent
;
298 logic
= MIME_MAGIC_OR
;
301 else if (*rule
== '!')
303 DEBUG_puts("1mimeAddTypeRule: NOT");
307 else if (isalnum(*rule
& 255))
310 * Read an extension name or a function...
314 while (isalnum(*rule
& 255) && (size_t)(ptr
- name
) < (sizeof(name
) - 1))
322 * Read function parameters...
327 num_values
< (int)(sizeof(value
) / sizeof(value
[0]));
330 ptr
= value
[num_values
];
332 while ((size_t)(ptr
- value
[num_values
]) < (sizeof(value
[0]) - 1) &&
333 *rule
!= '\0' && *rule
!= ',' && *rule
!= ')')
335 if (isspace(*rule
& 255))
338 * Ignore whitespace...
344 else if (*rule
== '\"' || *rule
== '\'')
347 * Copy quoted strings literally...
352 while (*rule
!= '\0' && *rule
!= quote
&&
353 (size_t)(ptr
- value
[num_values
]) < (sizeof(value
[0]) - 1))
361 else if (*rule
== '<')
365 while (*rule
!= '>' && *rule
!= '\0' &&
366 (size_t)(ptr
- value
[num_values
]) < (sizeof(value
[0]) - 1))
368 if (isxdigit(rule
[0] & 255) && isxdigit(rule
[1] & 255))
371 *ptr
= (char)((*rule
++ - '0') << 4);
373 *ptr
= (char)((tolower(*rule
++) - 'a' + 10) << 4);
376 *ptr
++ |= *rule
++ - '0';
378 *ptr
++ |= tolower(*rule
++) - 'a' + 10;
394 length
[num_values
] = ptr
- value
[num_values
];
411 * Figure out the function...
414 if (!strcmp(name
, "match"))
415 op
= MIME_MAGIC_MATCH
;
416 else if (!strcmp(name
, "ascii"))
417 op
= MIME_MAGIC_ASCII
;
418 else if (!strcmp(name
, "printable"))
419 op
= MIME_MAGIC_PRINTABLE
;
420 else if (!strcmp(name
, "regex"))
421 op
= MIME_MAGIC_REGEX
;
422 else if (!strcmp(name
, "string"))
423 op
= MIME_MAGIC_STRING
;
424 else if (!strcmp(name
, "istring"))
425 op
= MIME_MAGIC_ISTRING
;
426 else if (!strcmp(name
, "char"))
427 op
= MIME_MAGIC_CHAR
;
428 else if (!strcmp(name
, "short"))
429 op
= MIME_MAGIC_SHORT
;
430 else if (!strcmp(name
, "int"))
432 else if (!strcmp(name
, "locale"))
433 op
= MIME_MAGIC_LOCALE
;
434 else if (!strcmp(name
, "contains"))
435 op
= MIME_MAGIC_CONTAINS
;
436 else if (!strcmp(name
, "priority") && num_values
== 1)
438 mt
->priority
= atoi(value
[0]);
447 * This is just a filename match on the extension...
450 snprintf(value
[0], sizeof(value
[0]), "*.%s", name
);
451 length
[0] = (int)strlen(value
[0]);
452 op
= MIME_MAGIC_MATCH
;
456 * Add a rule for this operation.
459 if ((temp
= calloc(1, sizeof(mime_magic_t
))) == NULL
)
462 temp
->invert
= (short)invert
;
465 temp
->parent
= current
->parent
;
466 current
->next
= temp
;
471 temp
->prev
= current
;
473 if (logic
== MIME_MAGIC_NOP
)
476 * Add parenthetical grouping...
479 DEBUG_printf(("1mimeAddTypeRule: Making new OR group %p for "
480 "parenthesis.", temp
));
482 temp
->op
= MIME_MAGIC_OR
;
484 if ((temp
->child
= calloc(1, sizeof(mime_magic_t
))) == NULL
)
487 temp
->child
->parent
= temp
;
488 temp
->child
->invert
= temp
->invert
;
492 logic
= MIME_MAGIC_OR
;
495 DEBUG_printf(("1mimeAddTypeRule: Adding %p: %s, op=MIME_MAGIC_%s(%d), "
496 "logic=MIME_MAGIC_%s, invert=%d.", temp
, name
,
497 debug_ops
[op
], op
, debug_ops
[logic
], invert
));
500 * Fill in data for the rule...
504 temp
->op
= (short)op
;
509 case MIME_MAGIC_MATCH
:
510 if ((size_t)length
[0] > (sizeof(temp
->value
.matchv
) - 1))
512 strlcpy(temp
->value
.matchv
, value
[0], sizeof(temp
->value
.matchv
));
514 case MIME_MAGIC_ASCII
:
515 case MIME_MAGIC_PRINTABLE
:
516 temp
->offset
= strtol(value
[0], NULL
, 0);
517 temp
->length
= strtol(value
[1], NULL
, 0);
518 if (temp
->length
> MIME_MAX_BUFFER
)
519 temp
->length
= MIME_MAX_BUFFER
;
521 case MIME_MAGIC_REGEX
:
522 temp
->offset
= strtol(value
[0], NULL
, 0);
523 temp
->length
= MIME_MAX_BUFFER
;
524 if (regcomp(&(temp
->value
.rev
), value
[1], REG_NOSUB
| REG_EXTENDED
))
527 case MIME_MAGIC_STRING
:
528 case MIME_MAGIC_ISTRING
:
529 temp
->offset
= strtol(value
[0], NULL
, 0);
530 if ((size_t)length
[1] > sizeof(temp
->value
.stringv
))
532 temp
->length
= length
[1];
533 memcpy(temp
->value
.stringv
, value
[1], (size_t)length
[1]);
535 case MIME_MAGIC_CHAR
:
536 temp
->offset
= strtol(value
[0], NULL
, 0);
538 temp
->value
.charv
= (unsigned char)value
[1][0];
540 temp
->value
.charv
= (unsigned char)strtol(value
[1], NULL
, 0);
542 DEBUG_printf(("1mimeAddTypeRule: CHAR(%d,0x%02x)", temp
->offset
,
545 case MIME_MAGIC_SHORT
:
546 temp
->offset
= strtol(value
[0], NULL
, 0);
547 temp
->value
.shortv
= (unsigned short)strtol(value
[1], NULL
, 0);
549 case MIME_MAGIC_INT
:
550 temp
->offset
= strtol(value
[0], NULL
, 0);
551 temp
->value
.intv
= (unsigned)strtol(value
[1], NULL
, 0);
553 case MIME_MAGIC_LOCALE
:
554 if ((size_t)length
[0] > (sizeof(temp
->value
.localev
) - 1))
557 strlcpy(temp
->value
.localev
, value
[0], sizeof(temp
->value
.localev
));
559 case MIME_MAGIC_CONTAINS
:
560 temp
->offset
= strtol(value
[0], NULL
, 0);
561 temp
->region
= strtol(value
[1], NULL
, 0);
562 if ((size_t)length
[2] > sizeof(temp
->value
.stringv
))
564 temp
->length
= length
[2];
565 memcpy(temp
->value
.stringv
, value
[2], (size_t)length
[2]);
578 * 'mimeFileType()' - Determine the type of a file.
581 mime_type_t
* /* O - Type of file */
582 mimeFileType(mime_t
*mime
, /* I - MIME database */
583 const char *pathname
, /* I - Name of file to check on disk */
584 const char *filename
, /* I - Original filename or NULL */
585 int *compression
) /* O - Is the file compressed? */
587 _mime_filebuf_t fb
; /* File buffer */
588 const char *base
; /* Base filename of file */
589 mime_type_t
*type
, /* File type */
590 *best
; /* Best match */
593 DEBUG_printf(("mimeFileType(mime=%p, pathname=\"%s\", filename=\"%s\", "
594 "compression=%p)", mime
, pathname
, filename
, compression
));
597 * Range check input parameters...
600 if (!mime
|| !pathname
)
602 DEBUG_puts("1mimeFileType: Returning NULL.");
607 * Try to open the file...
610 if ((fb
.fp
= cupsFileOpen(pathname
, "r")) == NULL
)
612 DEBUG_printf(("1mimeFileType: Unable to open \"%s\": %s", pathname
,
614 DEBUG_puts("1mimeFileType: Returning NULL.");
619 * Then preload the first MIME_MAX_BUFFER bytes of the file into the file
620 * buffer, returning an error if we can't read anything...
624 fb
.length
= (int)cupsFileRead(fb
.fp
, (char *)fb
.buffer
, MIME_MAX_BUFFER
);
628 DEBUG_printf(("1mimeFileType: Unable to read from \"%s\": %s", pathname
, strerror(errno
)));
629 DEBUG_puts("1mimeFileType: Returning NULL.");
631 cupsFileClose(fb
.fp
);
637 * Figure out the base filename (without directory portion)...
642 if ((base
= strrchr(filename
, '/')) != NULL
)
647 else if ((base
= strrchr(pathname
, '/')) != NULL
)
653 * Then check it against all known types...
656 for (type
= (mime_type_t
*)cupsArrayFirst(mime
->types
), best
= NULL
;
658 type
= (mime_type_t
*)cupsArrayNext(mime
->types
))
659 if (mime_check_rules(base
, &fb
, type
->rules
))
661 if (!best
|| type
->priority
> best
->priority
)
666 * Finally, close the file and return a match (if any)...
671 *compression
= cupsFileCompression(fb
.fp
);
672 DEBUG_printf(("1mimeFileType: *compression=%d", *compression
));
675 cupsFileClose(fb
.fp
);
677 DEBUG_printf(("1mimeFileType: Returning %p(%s/%s).", best
,
678 best
? best
->super
: "???", best
? best
->type
: "???"));
684 * 'mimeType()' - Lookup a file type.
687 mime_type_t
* /* O - Matching file type definition */
688 mimeType(mime_t
*mime
, /* I - MIME database */
689 const char *super
, /* I - Super-type name */
690 const char *type
) /* I - Type name */
692 mime_type_t key
, /* MIME type search key */
693 *mt
; /* Matching type */
696 DEBUG_printf(("mimeType(mime=%p, super=\"%s\", type=\"%s\")", mime
, super
,
700 * Range check input...
703 if (!mime
|| !super
|| !type
)
705 DEBUG_puts("1mimeType: Returning NULL.");
710 * Lookup the type in the array...
713 strlcpy(key
.super
, super
, sizeof(key
.super
));
714 strlcpy(key
.type
, type
, sizeof(key
.type
));
716 mt
= (mime_type_t
*)cupsArrayFind(mime
->types
, &key
);
717 DEBUG_printf(("1mimeType: Returning %p.", mt
));
723 * 'mime_compare_types()' - Compare two MIME super/type names.
726 static int /* O - Result of comparison */
727 mime_compare_types(mime_type_t
*t0
, /* I - First type */
728 mime_type_t
*t1
) /* I - Second type */
730 int i
; /* Result of comparison */
733 if ((i
= _cups_strcasecmp(t0
->super
, t1
->super
)) == 0)
734 i
= _cups_strcasecmp(t0
->type
, t1
->type
);
741 * 'mime_check_rules()' - Check each rule in a list.
744 static int /* O - 1 if match, 0 if no match */
746 const char *filename
, /* I - Filename */
747 _mime_filebuf_t
*fb
, /* I - File to check */
748 mime_magic_t
*rules
) /* I - Rules to check */
750 int n
; /* Looping var */
751 int region
; /* Region to look at */
752 int logic
, /* Logic to apply */
753 result
; /* Result of test */
754 unsigned intv
; /* Integer value */
755 short shortv
; /* Short value */
756 unsigned char *bufptr
; /* Pointer into buffer */
759 DEBUG_printf(("4mime_check_rules(filename=\"%s\", fb=%p, rules=%p)", filename
,
765 if (rules
->parent
== NULL
)
766 logic
= MIME_MAGIC_OR
;
768 logic
= rules
->parent
->op
;
772 while (rules
!= NULL
)
775 * Compute the result of this rule...
780 case MIME_MAGIC_MATCH
:
781 result
= mime_patmatch(filename
, rules
->value
.matchv
);
784 case MIME_MAGIC_ASCII
:
786 * Load the buffer if necessary...
789 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
790 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
793 * Reload file buffer...
796 cupsFileSeek(fb
->fp
, rules
->offset
);
797 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
799 fb
->offset
= rules
->offset
;
801 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_ASCII fb->length=%d", fb
->length
));
805 * Test for ASCII printable characters plus standard control chars.
808 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
809 n
= fb
->offset
+ fb
->length
- rules
->offset
;
813 bufptr
= fb
->buffer
+ rules
->offset
- fb
->offset
;
815 if ((*bufptr
>= 32 && *bufptr
<= 126) ||
816 (*bufptr
>= 8 && *bufptr
<= 13) ||
817 *bufptr
== 26 || *bufptr
== 27)
828 case MIME_MAGIC_PRINTABLE
:
830 * Load the buffer if necessary...
833 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
834 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
837 * Reload file buffer...
840 cupsFileSeek(fb
->fp
, rules
->offset
);
841 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
843 fb
->offset
= rules
->offset
;
845 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_PRINTABLE fb->length=%d", fb
->length
));
849 * Test for 8-bit printable characters plus standard control chars.
852 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
853 n
= fb
->offset
+ fb
->length
- rules
->offset
;
857 bufptr
= fb
->buffer
+ rules
->offset
- fb
->offset
;
860 if (*bufptr
>= 128 ||
861 (*bufptr
>= 32 && *bufptr
<= 126) ||
862 (*bufptr
>= 8 && *bufptr
<= 13) ||
863 *bufptr
== 26 || *bufptr
== 27)
874 case MIME_MAGIC_REGEX
:
875 DEBUG_printf(("5mime_check_rules: regex(%d, \"%s\")", rules
->offset
,
876 rules
->value
.stringv
));
879 * Load the buffer if necessary...
882 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
883 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
886 * Reload file buffer...
889 cupsFileSeek(fb
->fp
, rules
->offset
);
890 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
892 fb
->offset
= rules
->offset
;
894 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_REGEX fb->length=%d", fb
->length
));
896 DEBUG_printf(("5mime_check_rules: loaded %d byte fb->buffer at %d, starts "
897 "with \"%c%c%c%c\".",
898 fb
->length
, fb
->offset
, fb
->buffer
[0], fb
->buffer
[1],
899 fb
->buffer
[2], fb
->buffer
[3]));
903 * Compare the buffer against the string. If the file is too
904 * short then don't compare - it can't match...
909 char temp
[MIME_MAX_BUFFER
+ 1];
910 /* Temporary buffer */
912 memcpy(temp
, fb
->buffer
, (size_t)fb
->length
);
913 temp
[fb
->length
] = '\0';
914 result
= !regexec(&(rules
->value
.rev
), temp
, 0, NULL
, 0);
917 DEBUG_printf(("5mime_check_rules: result=%d", result
));
920 case MIME_MAGIC_STRING
:
921 DEBUG_printf(("5mime_check_rules: string(%d, \"%s\")", rules
->offset
,
922 rules
->value
.stringv
));
925 * Load the buffer if necessary...
928 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
929 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
932 * Reload file buffer...
935 cupsFileSeek(fb
->fp
, rules
->offset
);
936 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
938 fb
->offset
= rules
->offset
;
940 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_STRING fb->length=%d", fb
->length
));
942 DEBUG_printf(("5mime_check_rules: loaded %d byte fb->buffer at %d, starts "
943 "with \"%c%c%c%c\".",
944 fb
->length
, fb
->offset
, fb
->buffer
[0], fb
->buffer
[1],
945 fb
->buffer
[2], fb
->buffer
[3]));
949 * Compare the buffer against the string. If the file is too
950 * short then don't compare - it can't match...
953 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
956 result
= !memcmp(fb
->buffer
+ rules
->offset
- fb
->offset
, rules
->value
.stringv
, (size_t)rules
->length
);
957 DEBUG_printf(("5mime_check_rules: result=%d", result
));
960 case MIME_MAGIC_ISTRING
:
962 * Load the buffer if necessary...
965 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
966 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
969 * Reload file buffer...
972 cupsFileSeek(fb
->fp
, rules
->offset
);
973 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
975 fb
->offset
= rules
->offset
;
977 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_ISTRING fb->length=%d", fb
->length
));
981 * Compare the buffer against the string. If the file is too
982 * short then don't compare - it can't match...
985 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
988 result
= !_cups_strncasecmp((char *)fb
->buffer
+ rules
->offset
- fb
->offset
, rules
->value
.stringv
, (size_t)rules
->length
);
991 case MIME_MAGIC_CHAR
:
993 * Load the buffer if necessary...
996 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
)
999 * Reload file buffer...
1002 cupsFileSeek(fb
->fp
, rules
->offset
);
1003 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
1004 sizeof(fb
->buffer
));
1005 fb
->offset
= rules
->offset
;
1007 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_CHAR fb->length=%d", fb
->length
));
1011 * Compare the character values; if the file is too short, it
1018 result
= (fb
->buffer
[rules
->offset
- fb
->offset
] ==
1019 rules
->value
.charv
);
1022 case MIME_MAGIC_SHORT
:
1024 * Load the buffer if necessary...
1027 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
1028 (rules
->offset
+ 2) > (fb
->offset
+ fb
->length
))
1031 * Reload file buffer...
1034 cupsFileSeek(fb
->fp
, rules
->offset
);
1035 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
1036 sizeof(fb
->buffer
));
1037 fb
->offset
= rules
->offset
;
1039 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_SHORT fb->length=%d", fb
->length
));
1043 * Compare the short values; if the file is too short, it
1051 bufptr
= fb
->buffer
+ rules
->offset
- fb
->offset
;
1052 shortv
= (short)((bufptr
[0] << 8) | bufptr
[1]);
1053 result
= (shortv
== rules
->value
.shortv
);
1057 case MIME_MAGIC_INT
:
1059 * Load the buffer if necessary...
1062 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
1063 (rules
->offset
+ 4) > (fb
->offset
+ fb
->length
))
1066 * Reload file buffer...
1069 cupsFileSeek(fb
->fp
, rules
->offset
);
1070 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
1071 sizeof(fb
->buffer
));
1072 fb
->offset
= rules
->offset
;
1074 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_INT fb->length=%d", fb
->length
));
1078 * Compare the int values; if the file is too short, it
1086 bufptr
= fb
->buffer
+ rules
->offset
- fb
->offset
;
1087 intv
= (unsigned)((((((bufptr
[0] << 8) | bufptr
[1]) << 8) | bufptr
[2]) << 8) | bufptr
[3]);
1088 result
= (intv
== rules
->value
.intv
);
1092 case MIME_MAGIC_LOCALE
:
1093 #if defined(_WIN32) || defined(__EMX__) || defined(__APPLE__)
1094 result
= !strcmp(rules
->value
.localev
, setlocale(LC_ALL
, ""));
1096 result
= !strcmp(rules
->value
.localev
, setlocale(LC_MESSAGES
, ""));
1097 #endif /* __APPLE__ */
1100 case MIME_MAGIC_CONTAINS
:
1102 * Load the buffer if necessary...
1105 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
1106 (rules
->offset
+ rules
->region
) > (fb
->offset
+ fb
->length
))
1109 * Reload file buffer...
1112 cupsFileSeek(fb
->fp
, rules
->offset
);
1113 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
1114 sizeof(fb
->buffer
));
1115 fb
->offset
= rules
->offset
;
1117 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_CONTAINS fb->length=%d", fb
->length
));
1121 * Compare the buffer against the string. If the file is too
1122 * short then don't compare - it can't match...
1125 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
1129 if (fb
->length
> rules
->region
)
1130 region
= rules
->region
- rules
->length
;
1132 region
= fb
->length
- rules
->length
;
1134 for (n
= 0; n
< region
; n
++)
1135 if ((result
= (memcmp(fb
->buffer
+ rules
->offset
- fb
->offset
+ n
, rules
->value
.stringv
, (size_t)rules
->length
) == 0)) != 0)
1141 if (rules
->child
!= NULL
)
1142 result
= mime_check_rules(filename
, fb
, rules
->child
);
1149 * If the logic is inverted, invert the result...
1156 * OK, now if the current logic is OR and this result is true, the this
1157 * rule set is true. If the current logic is AND and this result is false,
1158 * the the rule set is false...
1161 DEBUG_printf(("5mime_check_rules: result of test %p (MIME_MAGIC_%s) is %d",
1162 rules
, debug_ops
[rules
->op
], result
));
1164 if ((result
&& logic
== MIME_MAGIC_OR
) ||
1165 (!result
&& logic
== MIME_MAGIC_AND
))
1169 * Otherwise the jury is still out on this one, so move to the next rule.
1172 rules
= rules
->next
;
1180 * 'mime_patmatch()' - Pattern matching.
1183 static int /* O - 1 if match, 0 if no match */
1184 mime_patmatch(const char *s
, /* I - String to match against */
1185 const char *pat
) /* I - Pattern to match against */
1188 * Range check the input...
1191 if (s
== NULL
|| pat
== NULL
)
1195 * Loop through the pattern and match strings, and stop if we come to a
1196 * point where the strings don't match or we find a complete match.
1199 while (*s
!= '\0' && *pat
!= '\0')
1204 * Wildcard - 0 or more characters...
1209 return (1); /* Last pattern char is *, so everything matches... */
1212 * Test all remaining combinations until we get to the end of the string.
1217 if (mime_patmatch(s
, pat
))
1223 else if (*pat
== '?')
1226 * Wildcard - 1 character...
1233 else if (*pat
== '[')
1236 * Match a character from the input set [chars]...
1240 while (*pat
!= ']' && *pat
!= '\0')
1246 if (*pat
== ']' || *pat
== '\0')
1249 while (*pat
!= ']' && *pat
!= '\0')
1257 else if (*pat
== '\\')
1260 * Handle quoted characters...
1267 * Stop if the pattern and string don't match...
1275 * Done parsing the pattern and string; return 1 if the last character
1276 * matches and 0 otherwise...
1279 return (*s
== *pat
);