]>
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 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
15 * Include necessary headers...
18 #include <cups/string-private.h>
19 #include <cups/debug-private.h>
28 typedef struct _mime_filebuf_s
/**** File buffer for MIME typing ****/
30 cups_file_t
*fp
; /* File pointer */
31 int offset
, /* Offset in file */
32 length
; /* Length of buffered data */
33 unsigned char buffer
[MIME_MAX_BUFFER
];/* Buffered data */
41 static int mime_compare_types(mime_type_t
*t0
, mime_type_t
*t1
);
42 static int mime_check_rules(const char *filename
, _mime_filebuf_t
*fb
,
44 static int mime_patmatch(const char *s
, const char *pat
);
52 static const char * const debug_ops
[] =
54 "NOP", /* No operation */
55 "AND", /* Logical AND of all children */
56 "OR", /* Logical OR of all children */
57 "MATCH", /* Filename match */
58 "ASCII", /* ASCII characters in range */
59 "PRINTABLE", /* Printable characters (32-255) */
60 "STRING", /* String matches */
61 "CHAR", /* Character/byte matches */
62 "SHORT", /* Short/16-bit word matches */
63 "INT", /* Integer/32-bit word matches */
64 "LOCALE", /* Current locale matches string */
65 "CONTAINS", /* File contains a string */
66 "ISTRING", /* Case-insensitive string matches */
67 "REGEX" /* Regular expression matches */
73 * 'mimeAddType()' - Add a MIME type to a database.
76 mime_type_t
* /* O - New (or existing) MIME type */
77 mimeAddType(mime_t
*mime
, /* I - MIME database */
78 const char *super
, /* I - Super-type name */
79 const char *type
) /* I - Type name */
81 mime_type_t
*temp
; /* New MIME type */
82 size_t typelen
; /* Length of type name */
85 DEBUG_printf(("mimeAddType(mime=%p, super=\"%s\", type=\"%s\")", mime
, super
,
89 * Range check input...
92 if (!mime
|| !super
|| !type
)
94 DEBUG_puts("1mimeAddType: Returning NULL (bad arguments).");
99 * See if the type already exists; if so, return the existing type...
102 if ((temp
= mimeType(mime
, super
, type
)) != NULL
)
104 DEBUG_printf(("1mimeAddType: Returning %p (existing).", temp
));
109 * The type doesn't exist; add it...
113 mime
->types
= cupsArrayNew((cups_array_func_t
)mime_compare_types
, NULL
);
117 DEBUG_puts("1mimeAddType: Returning NULL (no types).");
121 typelen
= strlen(type
) + 1;
123 if ((temp
= calloc(1, sizeof(mime_type_t
) - MIME_MAX_TYPE
+ typelen
)) == NULL
)
125 DEBUG_puts("1mimeAddType: Returning NULL (out of memory).");
129 strlcpy(temp
->super
, super
, sizeof(temp
->super
));
130 memcpy(temp
->type
, type
, typelen
);
131 temp
->priority
= 100;
133 cupsArrayAdd(mime
->types
, temp
);
135 DEBUG_printf(("1mimeAddType: Returning %p (new).", temp
));
141 * 'mimeAddTypeRule()' - Add a detection rule for a file type.
144 int /* O - 0 on success, -1 on failure */
145 mimeAddTypeRule(mime_type_t
*mt
, /* I - Type to add to */
146 const char *rule
) /* I - Rule to add */
148 int num_values
, /* Number of values seen */
149 op
, /* Operation code */
150 logic
, /* Logic for next rule */
151 invert
; /* Invert following rule? */
152 char name
[255], /* Name in rule string */
153 value
[3][255], /* Value in rule string */
154 *ptr
, /* Position in name or value */
155 quote
; /* Quote character */
156 int length
[3]; /* Length of each parameter */
157 mime_magic_t
*temp
, /* New rule */
158 *current
; /* Current rule */
161 DEBUG_printf(("mimeAddTypeRule(mt=%p(%s/%s), rule=\"%s\")", mt
,
162 mt
? mt
->super
: "???", mt
? mt
->type
: "???", rule
));
165 * Range check input...
172 * Find the last rule in the top-level of the rules tree.
175 for (current
= mt
->rules
; current
; current
= current
->next
)
180 * Parse the rules string. Most rules are either a file extension or a
181 * comparison function:
184 * function(parameters)
187 logic
= MIME_MAGIC_NOP
;
190 while (*rule
!= '\0')
192 while (isspace(*rule
& 255))
197 DEBUG_puts("1mimeAddTypeRule: New parenthesis group");
198 logic
= MIME_MAGIC_NOP
;
201 else if (*rule
== ')')
203 DEBUG_puts("1mimeAddTypeRule: Close paren...");
204 if (current
== NULL
|| current
->parent
== NULL
)
207 current
= current
->parent
;
209 if (current
->parent
== NULL
)
210 logic
= MIME_MAGIC_OR
;
212 logic
= current
->parent
->op
;
216 else if (*rule
== '+' && current
!= NULL
)
218 if (logic
!= MIME_MAGIC_AND
&&
219 current
!= NULL
&& current
->prev
!= NULL
)
222 * OK, we have more than 1 rule in the current tree level... Make a
223 * new group tree and move the previous rule to it...
226 if ((temp
= calloc(1, sizeof(mime_magic_t
))) == NULL
)
229 temp
->op
= MIME_MAGIC_AND
;
230 temp
->child
= current
;
231 temp
->parent
= current
->parent
;
232 current
->prev
->next
= temp
;
233 temp
->prev
= current
->prev
;
235 current
->prev
= NULL
;
236 current
->parent
= temp
;
238 DEBUG_printf(("1mimeAddTypeRule: Creating new AND group %p.", temp
));
240 else if (current
->parent
)
242 DEBUG_printf(("1mimeAddTypeRule: Setting group %p op to AND.",
244 current
->parent
->op
= MIME_MAGIC_AND
;
247 logic
= MIME_MAGIC_AND
;
250 else if (*rule
== ',')
252 if (logic
!= MIME_MAGIC_OR
&& current
!= NULL
)
255 * OK, we have two possibilities; either this is the top-level rule or
256 * we have a bunch of AND rules at this level.
259 if (current
->parent
== NULL
)
262 * This is the top-level rule; we have to move *all* of the AND rules
263 * down a level, as AND has precedence over OR.
266 if ((temp
= calloc(1, sizeof(mime_magic_t
))) == NULL
)
269 DEBUG_printf(("1mimeAddTypeRule: Creating new AND group %p inside OR "
272 while (current
->prev
!= NULL
)
274 current
->parent
= temp
;
275 current
= current
->prev
;
278 current
->parent
= temp
;
279 temp
->op
= MIME_MAGIC_AND
;
280 temp
->child
= current
;
282 mt
->rules
= current
= temp
;
287 * This isn't the top rule, so go up one level...
290 DEBUG_puts("1mimeAddTypeRule: Going up one level.");
291 current
= current
->parent
;
295 logic
= MIME_MAGIC_OR
;
298 else if (*rule
== '!')
300 DEBUG_puts("1mimeAddTypeRule: NOT");
304 else if (isalnum(*rule
& 255))
307 * Read an extension name or a function...
311 while (isalnum(*rule
& 255) && (size_t)(ptr
- name
) < (sizeof(name
) - 1))
319 * Read function parameters...
324 num_values
< (int)(sizeof(value
) / sizeof(value
[0]));
327 ptr
= value
[num_values
];
329 while ((size_t)(ptr
- value
[num_values
]) < (sizeof(value
[0]) - 1) &&
330 *rule
!= '\0' && *rule
!= ',' && *rule
!= ')')
332 if (isspace(*rule
& 255))
335 * Ignore whitespace...
341 else if (*rule
== '\"' || *rule
== '\'')
344 * Copy quoted strings literally...
349 while (*rule
!= '\0' && *rule
!= quote
&&
350 (size_t)(ptr
- value
[num_values
]) < (sizeof(value
[0]) - 1))
358 else if (*rule
== '<')
362 while (*rule
!= '>' && *rule
!= '\0' &&
363 (size_t)(ptr
- value
[num_values
]) < (sizeof(value
[0]) - 1))
365 if (isxdigit(rule
[0] & 255) && isxdigit(rule
[1] & 255))
368 *ptr
= (char)((*rule
++ - '0') << 4);
370 *ptr
= (char)((tolower(*rule
++) - 'a' + 10) << 4);
373 *ptr
++ |= *rule
++ - '0';
375 *ptr
++ |= tolower(*rule
++) - 'a' + 10;
391 length
[num_values
] = ptr
- value
[num_values
];
408 * Figure out the function...
411 if (!strcmp(name
, "match"))
412 op
= MIME_MAGIC_MATCH
;
413 else if (!strcmp(name
, "ascii"))
414 op
= MIME_MAGIC_ASCII
;
415 else if (!strcmp(name
, "printable"))
416 op
= MIME_MAGIC_PRINTABLE
;
417 else if (!strcmp(name
, "regex"))
418 op
= MIME_MAGIC_REGEX
;
419 else if (!strcmp(name
, "string"))
420 op
= MIME_MAGIC_STRING
;
421 else if (!strcmp(name
, "istring"))
422 op
= MIME_MAGIC_ISTRING
;
423 else if (!strcmp(name
, "char"))
424 op
= MIME_MAGIC_CHAR
;
425 else if (!strcmp(name
, "short"))
426 op
= MIME_MAGIC_SHORT
;
427 else if (!strcmp(name
, "int"))
429 else if (!strcmp(name
, "locale"))
430 op
= MIME_MAGIC_LOCALE
;
431 else if (!strcmp(name
, "contains"))
432 op
= MIME_MAGIC_CONTAINS
;
433 else if (!strcmp(name
, "priority") && num_values
== 1)
435 mt
->priority
= atoi(value
[0]);
444 * This is just a filename match on the extension...
447 snprintf(value
[0], sizeof(value
[0]), "*.%s", name
);
448 length
[0] = (int)strlen(value
[0]);
449 op
= MIME_MAGIC_MATCH
;
453 * Add a rule for this operation.
456 if ((temp
= calloc(1, sizeof(mime_magic_t
))) == NULL
)
459 temp
->invert
= (short)invert
;
462 temp
->parent
= current
->parent
;
463 current
->next
= temp
;
468 temp
->prev
= current
;
470 if (logic
== MIME_MAGIC_NOP
)
473 * Add parenthetical grouping...
476 DEBUG_printf(("1mimeAddTypeRule: Making new OR group %p for "
477 "parenthesis.", temp
));
479 temp
->op
= MIME_MAGIC_OR
;
481 if ((temp
->child
= calloc(1, sizeof(mime_magic_t
))) == NULL
)
484 temp
->child
->parent
= temp
;
485 temp
->child
->invert
= temp
->invert
;
489 logic
= MIME_MAGIC_OR
;
492 DEBUG_printf(("1mimeAddTypeRule: Adding %p: %s, op=MIME_MAGIC_%s(%d), "
493 "logic=MIME_MAGIC_%s, invert=%d.", temp
, name
,
494 debug_ops
[op
], op
, debug_ops
[logic
], invert
));
497 * Fill in data for the rule...
501 temp
->op
= (short)op
;
506 case MIME_MAGIC_MATCH
:
507 if ((size_t)length
[0] > (sizeof(temp
->value
.matchv
) - 1))
509 strlcpy(temp
->value
.matchv
, value
[0], sizeof(temp
->value
.matchv
));
511 case MIME_MAGIC_ASCII
:
512 case MIME_MAGIC_PRINTABLE
:
513 temp
->offset
= strtol(value
[0], NULL
, 0);
514 temp
->length
= strtol(value
[1], NULL
, 0);
515 if (temp
->length
> MIME_MAX_BUFFER
)
516 temp
->length
= MIME_MAX_BUFFER
;
518 case MIME_MAGIC_REGEX
:
519 temp
->offset
= strtol(value
[0], NULL
, 0);
520 temp
->length
= MIME_MAX_BUFFER
;
521 if (regcomp(&(temp
->value
.rev
), value
[1], REG_NOSUB
| REG_EXTENDED
))
524 case MIME_MAGIC_STRING
:
525 case MIME_MAGIC_ISTRING
:
526 temp
->offset
= strtol(value
[0], NULL
, 0);
527 if ((size_t)length
[1] > sizeof(temp
->value
.stringv
))
529 temp
->length
= length
[1];
530 memcpy(temp
->value
.stringv
, value
[1], (size_t)length
[1]);
532 case MIME_MAGIC_CHAR
:
533 temp
->offset
= strtol(value
[0], NULL
, 0);
535 temp
->value
.charv
= (unsigned char)value
[1][0];
537 temp
->value
.charv
= (unsigned char)strtol(value
[1], NULL
, 0);
539 DEBUG_printf(("1mimeAddTypeRule: CHAR(%d,0x%02x)", temp
->offset
,
542 case MIME_MAGIC_SHORT
:
543 temp
->offset
= strtol(value
[0], NULL
, 0);
544 temp
->value
.shortv
= (unsigned short)strtol(value
[1], NULL
, 0);
546 case MIME_MAGIC_INT
:
547 temp
->offset
= strtol(value
[0], NULL
, 0);
548 temp
->value
.intv
= (unsigned)strtol(value
[1], NULL
, 0);
550 case MIME_MAGIC_LOCALE
:
551 if ((size_t)length
[0] > (sizeof(temp
->value
.localev
) - 1))
554 strlcpy(temp
->value
.localev
, value
[0], sizeof(temp
->value
.localev
));
556 case MIME_MAGIC_CONTAINS
:
557 temp
->offset
= strtol(value
[0], NULL
, 0);
558 temp
->region
= strtol(value
[1], NULL
, 0);
559 if ((size_t)length
[2] > sizeof(temp
->value
.stringv
))
561 temp
->length
= length
[2];
562 memcpy(temp
->value
.stringv
, value
[2], (size_t)length
[2]);
575 * 'mimeFileType()' - Determine the type of a file.
578 mime_type_t
* /* O - Type of file */
579 mimeFileType(mime_t
*mime
, /* I - MIME database */
580 const char *pathname
, /* I - Name of file to check on disk */
581 const char *filename
, /* I - Original filename or NULL */
582 int *compression
) /* O - Is the file compressed? */
584 _mime_filebuf_t fb
; /* File buffer */
585 const char *base
; /* Base filename of file */
586 mime_type_t
*type
, /* File type */
587 *best
; /* Best match */
590 DEBUG_printf(("mimeFileType(mime=%p, pathname=\"%s\", filename=\"%s\", "
591 "compression=%p)", mime
, pathname
, filename
, compression
));
594 * Range check input parameters...
597 if (!mime
|| !pathname
)
599 DEBUG_puts("1mimeFileType: Returning NULL.");
604 * Try to open the file...
607 if ((fb
.fp
= cupsFileOpen(pathname
, "r")) == NULL
)
609 DEBUG_printf(("1mimeFileType: Unable to open \"%s\": %s", pathname
,
611 DEBUG_puts("1mimeFileType: Returning NULL.");
616 * Then preload the first MIME_MAX_BUFFER bytes of the file into the file
617 * buffer, returning an error if we can't read anything...
621 fb
.length
= (int)cupsFileRead(fb
.fp
, (char *)fb
.buffer
, MIME_MAX_BUFFER
);
625 DEBUG_printf(("1mimeFileType: Unable to read from \"%s\": %s", pathname
, strerror(errno
)));
626 DEBUG_puts("1mimeFileType: Returning NULL.");
628 cupsFileClose(fb
.fp
);
634 * Figure out the base filename (without directory portion)...
639 if ((base
= strrchr(filename
, '/')) != NULL
)
644 else if ((base
= strrchr(pathname
, '/')) != NULL
)
650 * Then check it against all known types...
653 for (type
= (mime_type_t
*)cupsArrayFirst(mime
->types
), best
= NULL
;
655 type
= (mime_type_t
*)cupsArrayNext(mime
->types
))
656 if (mime_check_rules(base
, &fb
, type
->rules
))
658 if (!best
|| type
->priority
> best
->priority
)
663 * Finally, close the file and return a match (if any)...
668 *compression
= cupsFileCompression(fb
.fp
);
669 DEBUG_printf(("1mimeFileType: *compression=%d", *compression
));
672 cupsFileClose(fb
.fp
);
674 DEBUG_printf(("1mimeFileType: Returning %p(%s/%s).", best
,
675 best
? best
->super
: "???", best
? best
->type
: "???"));
681 * 'mimeType()' - Lookup a file type.
684 mime_type_t
* /* O - Matching file type definition */
685 mimeType(mime_t
*mime
, /* I - MIME database */
686 const char *super
, /* I - Super-type name */
687 const char *type
) /* I - Type name */
689 mime_type_t key
, /* MIME type search key */
690 *mt
; /* Matching type */
693 DEBUG_printf(("mimeType(mime=%p, super=\"%s\", type=\"%s\")", mime
, super
,
697 * Range check input...
700 if (!mime
|| !super
|| !type
)
702 DEBUG_puts("1mimeType: Returning NULL.");
707 * Lookup the type in the array...
710 strlcpy(key
.super
, super
, sizeof(key
.super
));
711 strlcpy(key
.type
, type
, sizeof(key
.type
));
713 mt
= (mime_type_t
*)cupsArrayFind(mime
->types
, &key
);
714 DEBUG_printf(("1mimeType: Returning %p.", mt
));
720 * 'mime_compare_types()' - Compare two MIME super/type names.
723 static int /* O - Result of comparison */
724 mime_compare_types(mime_type_t
*t0
, /* I - First type */
725 mime_type_t
*t1
) /* I - Second type */
727 int i
; /* Result of comparison */
730 if ((i
= _cups_strcasecmp(t0
->super
, t1
->super
)) == 0)
731 i
= _cups_strcasecmp(t0
->type
, t1
->type
);
738 * 'mime_check_rules()' - Check each rule in a list.
741 static int /* O - 1 if match, 0 if no match */
743 const char *filename
, /* I - Filename */
744 _mime_filebuf_t
*fb
, /* I - File to check */
745 mime_magic_t
*rules
) /* I - Rules to check */
747 int n
; /* Looping var */
748 int region
; /* Region to look at */
749 int logic
, /* Logic to apply */
750 result
; /* Result of test */
751 unsigned intv
; /* Integer value */
752 short shortv
; /* Short value */
753 unsigned char *bufptr
; /* Pointer into buffer */
756 DEBUG_printf(("4mime_check_rules(filename=\"%s\", fb=%p, rules=%p)", filename
,
762 if (rules
->parent
== NULL
)
763 logic
= MIME_MAGIC_OR
;
765 logic
= rules
->parent
->op
;
769 while (rules
!= NULL
)
772 * Compute the result of this rule...
777 case MIME_MAGIC_MATCH
:
778 result
= mime_patmatch(filename
, rules
->value
.matchv
);
781 case MIME_MAGIC_ASCII
:
783 * Load the buffer if necessary...
786 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
787 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
790 * Reload file buffer...
793 cupsFileSeek(fb
->fp
, rules
->offset
);
794 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
796 fb
->offset
= rules
->offset
;
798 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_ASCII fb->length=%d", fb
->length
));
802 * Test for ASCII printable characters plus standard control chars.
805 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
806 n
= fb
->offset
+ fb
->length
- rules
->offset
;
810 bufptr
= fb
->buffer
+ rules
->offset
- fb
->offset
;
812 if ((*bufptr
>= 32 && *bufptr
<= 126) ||
813 (*bufptr
>= 8 && *bufptr
<= 13) ||
814 *bufptr
== 26 || *bufptr
== 27)
825 case MIME_MAGIC_PRINTABLE
:
827 * Load the buffer if necessary...
830 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
831 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
834 * Reload file buffer...
837 cupsFileSeek(fb
->fp
, rules
->offset
);
838 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
840 fb
->offset
= rules
->offset
;
842 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_PRINTABLE fb->length=%d", fb
->length
));
846 * Test for 8-bit printable characters plus standard control chars.
849 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
850 n
= fb
->offset
+ fb
->length
- rules
->offset
;
854 bufptr
= fb
->buffer
+ rules
->offset
- fb
->offset
;
857 if (*bufptr
>= 128 ||
858 (*bufptr
>= 32 && *bufptr
<= 126) ||
859 (*bufptr
>= 8 && *bufptr
<= 13) ||
860 *bufptr
== 26 || *bufptr
== 27)
871 case MIME_MAGIC_REGEX
:
872 DEBUG_printf(("5mime_check_rules: regex(%d, \"%s\")", rules
->offset
,
873 rules
->value
.stringv
));
876 * Load the buffer if necessary...
879 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
880 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
883 * Reload file buffer...
886 cupsFileSeek(fb
->fp
, rules
->offset
);
887 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
889 fb
->offset
= rules
->offset
;
891 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_REGEX fb->length=%d", fb
->length
));
893 DEBUG_printf(("5mime_check_rules: loaded %d byte fb->buffer at %d, starts "
894 "with \"%c%c%c%c\".",
895 fb
->length
, fb
->offset
, fb
->buffer
[0], fb
->buffer
[1],
896 fb
->buffer
[2], fb
->buffer
[3]));
900 * Compare the buffer against the string. If the file is too
901 * short then don't compare - it can't match...
906 char temp
[MIME_MAX_BUFFER
+ 1];
907 /* Temporary buffer */
909 memcpy(temp
, fb
->buffer
, (size_t)fb
->length
);
910 temp
[fb
->length
] = '\0';
911 result
= !regexec(&(rules
->value
.rev
), temp
, 0, NULL
, 0);
914 DEBUG_printf(("5mime_check_rules: result=%d", result
));
917 case MIME_MAGIC_STRING
:
918 DEBUG_printf(("5mime_check_rules: string(%d, \"%s\")", rules
->offset
,
919 rules
->value
.stringv
));
922 * Load the buffer if necessary...
925 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
926 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
929 * Reload file buffer...
932 cupsFileSeek(fb
->fp
, rules
->offset
);
933 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
935 fb
->offset
= rules
->offset
;
937 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_STRING fb->length=%d", fb
->length
));
939 DEBUG_printf(("5mime_check_rules: loaded %d byte fb->buffer at %d, starts "
940 "with \"%c%c%c%c\".",
941 fb
->length
, fb
->offset
, fb
->buffer
[0], fb
->buffer
[1],
942 fb
->buffer
[2], fb
->buffer
[3]));
946 * Compare the buffer against the string. If the file is too
947 * short then don't compare - it can't match...
950 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
953 result
= !memcmp(fb
->buffer
+ rules
->offset
- fb
->offset
, rules
->value
.stringv
, (size_t)rules
->length
);
954 DEBUG_printf(("5mime_check_rules: result=%d", result
));
957 case MIME_MAGIC_ISTRING
:
959 * Load the buffer if necessary...
962 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
963 (rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
966 * Reload file buffer...
969 cupsFileSeek(fb
->fp
, rules
->offset
);
970 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
972 fb
->offset
= rules
->offset
;
974 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_ISTRING fb->length=%d", fb
->length
));
978 * Compare the buffer against the string. If the file is too
979 * short then don't compare - it can't match...
982 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
985 result
= !_cups_strncasecmp((char *)fb
->buffer
+ rules
->offset
- fb
->offset
, rules
->value
.stringv
, (size_t)rules
->length
);
988 case MIME_MAGIC_CHAR
:
990 * Load the buffer if necessary...
993 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
)
996 * Reload file buffer...
999 cupsFileSeek(fb
->fp
, rules
->offset
);
1000 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
1001 sizeof(fb
->buffer
));
1002 fb
->offset
= rules
->offset
;
1004 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_CHAR fb->length=%d", fb
->length
));
1008 * Compare the character values; if the file is too short, it
1015 result
= (fb
->buffer
[rules
->offset
- fb
->offset
] ==
1016 rules
->value
.charv
);
1019 case MIME_MAGIC_SHORT
:
1021 * Load the buffer if necessary...
1024 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
1025 (rules
->offset
+ 2) > (fb
->offset
+ fb
->length
))
1028 * Reload file buffer...
1031 cupsFileSeek(fb
->fp
, rules
->offset
);
1032 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
1033 sizeof(fb
->buffer
));
1034 fb
->offset
= rules
->offset
;
1036 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_SHORT fb->length=%d", fb
->length
));
1040 * Compare the short values; if the file is too short, it
1048 bufptr
= fb
->buffer
+ rules
->offset
- fb
->offset
;
1049 shortv
= (short)((bufptr
[0] << 8) | bufptr
[1]);
1050 result
= (shortv
== rules
->value
.shortv
);
1054 case MIME_MAGIC_INT
:
1056 * Load the buffer if necessary...
1059 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
1060 (rules
->offset
+ 4) > (fb
->offset
+ fb
->length
))
1063 * Reload file buffer...
1066 cupsFileSeek(fb
->fp
, rules
->offset
);
1067 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
1068 sizeof(fb
->buffer
));
1069 fb
->offset
= rules
->offset
;
1071 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_INT fb->length=%d", fb
->length
));
1075 * Compare the int values; if the file is too short, it
1083 bufptr
= fb
->buffer
+ rules
->offset
- fb
->offset
;
1084 intv
= (unsigned)((((((bufptr
[0] << 8) | bufptr
[1]) << 8) | bufptr
[2]) << 8) | bufptr
[3]);
1085 result
= (intv
== rules
->value
.intv
);
1089 case MIME_MAGIC_LOCALE
:
1090 #if defined(WIN32) || defined(__EMX__) || defined(__APPLE__)
1091 result
= !strcmp(rules
->value
.localev
, setlocale(LC_ALL
, ""));
1093 result
= !strcmp(rules
->value
.localev
, setlocale(LC_MESSAGES
, ""));
1094 #endif /* __APPLE__ */
1097 case MIME_MAGIC_CONTAINS
:
1099 * Load the buffer if necessary...
1102 if (fb
->offset
< 0 || rules
->offset
< fb
->offset
||
1103 (rules
->offset
+ rules
->region
) > (fb
->offset
+ fb
->length
))
1106 * Reload file buffer...
1109 cupsFileSeek(fb
->fp
, rules
->offset
);
1110 fb
->length
= cupsFileRead(fb
->fp
, (char *)fb
->buffer
,
1111 sizeof(fb
->buffer
));
1112 fb
->offset
= rules
->offset
;
1114 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_CONTAINS fb->length=%d", fb
->length
));
1118 * Compare the buffer against the string. If the file is too
1119 * short then don't compare - it can't match...
1122 if ((rules
->offset
+ rules
->length
) > (fb
->offset
+ fb
->length
))
1126 if (fb
->length
> rules
->region
)
1127 region
= rules
->region
- rules
->length
;
1129 region
= fb
->length
- rules
->length
;
1131 for (n
= 0; n
< region
; n
++)
1132 if ((result
= (memcmp(fb
->buffer
+ rules
->offset
- fb
->offset
+ n
, rules
->value
.stringv
, (size_t)rules
->length
) == 0)) != 0)
1138 if (rules
->child
!= NULL
)
1139 result
= mime_check_rules(filename
, fb
, rules
->child
);
1146 * If the logic is inverted, invert the result...
1153 * OK, now if the current logic is OR and this result is true, the this
1154 * rule set is true. If the current logic is AND and this result is false,
1155 * the the rule set is false...
1158 DEBUG_printf(("5mime_check_rules: result of test %p (MIME_MAGIC_%s) is %d",
1159 rules
, debug_ops
[rules
->op
], result
));
1161 if ((result
&& logic
== MIME_MAGIC_OR
) ||
1162 (!result
&& logic
== MIME_MAGIC_AND
))
1166 * Otherwise the jury is still out on this one, so move to the next rule.
1169 rules
= rules
->next
;
1177 * 'mime_patmatch()' - Pattern matching.
1180 static int /* O - 1 if match, 0 if no match */
1181 mime_patmatch(const char *s
, /* I - String to match against */
1182 const char *pat
) /* I - Pattern to match against */
1185 * Range check the input...
1188 if (s
== NULL
|| pat
== NULL
)
1192 * Loop through the pattern and match strings, and stop if we come to a
1193 * point where the strings don't match or we find a complete match.
1196 while (*s
!= '\0' && *pat
!= '\0')
1201 * Wildcard - 0 or more characters...
1206 return (1); /* Last pattern char is *, so everything matches... */
1209 * Test all remaining combinations until we get to the end of the string.
1214 if (mime_patmatch(s
, pat
))
1220 else if (*pat
== '?')
1223 * Wildcard - 1 character...
1230 else if (*pat
== '[')
1233 * Match a character from the input set [chars]...
1237 while (*pat
!= ']' && *pat
!= '\0')
1243 if (*pat
== ']' || *pat
== '\0')
1246 while (*pat
!= ']' && *pat
!= '\0')
1254 else if (*pat
== '\\')
1257 * Handle quoted characters...
1264 * Stop if the pattern and string don't match...
1272 * Done parsing the pattern and string; return 1 if the last character
1273 * matches and 0 otherwise...
1276 return (*s
== *pat
);