]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/type.c
Fix whitespace issues.
[thirdparty/cups.git] / scheduler / type.c
1 /*
2 * MIME typing routines for CUPS.
3 *
4 * Copyright 2007-2016 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
6 *
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
8 */
9
10 /*
11 * Include necessary headers...
12 */
13
14 #include <cups/string-private.h>
15 #include <cups/debug-private.h>
16 #include <locale.h>
17 #include "mime.h"
18
19
20 /*
21 * Local types...
22 */
23
24 typedef struct _mime_filebuf_s /**** File buffer for MIME typing ****/
25 {
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 */
30 } _mime_filebuf_t;
31
32
33 /*
34 * Local functions...
35 */
36
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,
39 mime_magic_t *rules);
40 static int mime_patmatch(const char *s, const char *pat);
41
42
43 /*
44 * Local globals...
45 */
46
47 #ifdef DEBUG
48 static const char * const debug_ops[] =
49 { /* Test names... */
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 */
64 };
65 #endif /* DEBUG */
66
67
68 /*
69 * 'mimeAddType()' - Add a MIME type to a database.
70 */
71
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 */
76 {
77 mime_type_t *temp; /* New MIME type */
78 size_t typelen; /* Length of type name */
79
80
81 DEBUG_printf(("mimeAddType(mime=%p, super=\"%s\", type=\"%s\")", mime, super,
82 type));
83
84 /*
85 * Range check input...
86 */
87
88 if (!mime || !super || !type)
89 {
90 DEBUG_puts("1mimeAddType: Returning NULL (bad arguments).");
91 return (NULL);
92 }
93
94 /*
95 * See if the type already exists; if so, return the existing type...
96 */
97
98 if ((temp = mimeType(mime, super, type)) != NULL)
99 {
100 DEBUG_printf(("1mimeAddType: Returning %p (existing).", temp));
101 return (temp);
102 }
103
104 /*
105 * The type doesn't exist; add it...
106 */
107
108 if (!mime->types)
109 mime->types = cupsArrayNew((cups_array_func_t)mime_compare_types, NULL);
110
111 if (!mime->types)
112 {
113 DEBUG_puts("1mimeAddType: Returning NULL (no types).");
114 return (NULL);
115 }
116
117 typelen = strlen(type) + 1;
118
119 if ((temp = calloc(1, sizeof(mime_type_t) - MIME_MAX_TYPE + typelen)) == NULL)
120 {
121 DEBUG_puts("1mimeAddType: Returning NULL (out of memory).");
122 return (NULL);
123 }
124
125 strlcpy(temp->super, super, sizeof(temp->super));
126 memcpy(temp->type, type, typelen);
127 temp->priority = 100;
128
129 cupsArrayAdd(mime->types, temp);
130
131 DEBUG_printf(("1mimeAddType: Returning %p (new).", temp));
132 return (temp);
133 }
134
135
136 /*
137 * 'mimeAddTypeRule()' - Add a detection rule for a file type.
138 */
139
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 */
143 {
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 */
155
156
157 DEBUG_printf(("mimeAddTypeRule(mt=%p(%s/%s), rule=\"%s\")", mt,
158 mt ? mt->super : "???", mt ? mt->type : "???", rule));
159
160 /*
161 * Range check input...
162 */
163
164 if (!mt || !rule)
165 return (-1);
166
167 /*
168 * Find the last rule in the top-level of the rules tree.
169 */
170
171 for (current = mt->rules; current; current = current->next)
172 if (!current->next)
173 break;
174
175 /*
176 * Parse the rules string. Most rules are either a file extension or a
177 * comparison function:
178 *
179 * extension
180 * function(parameters)
181 */
182
183 logic = MIME_MAGIC_NOP;
184 invert = 0;
185
186 while (*rule != '\0')
187 {
188 while (isspace(*rule & 255))
189 rule ++;
190
191 if (*rule == '(')
192 {
193 DEBUG_puts("1mimeAddTypeRule: New parenthesis group");
194 logic = MIME_MAGIC_NOP;
195 rule ++;
196 }
197 else if (*rule == ')')
198 {
199 DEBUG_puts("1mimeAddTypeRule: Close paren...");
200 if (current == NULL || current->parent == NULL)
201 return (-1);
202
203 current = current->parent;
204
205 if (current->parent == NULL)
206 logic = MIME_MAGIC_OR;
207 else
208 logic = current->parent->op;
209
210 rule ++;
211 }
212 else if (*rule == '+' && current != NULL)
213 {
214 if (logic != MIME_MAGIC_AND &&
215 current != NULL && current->prev != NULL)
216 {
217 /*
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...
220 */
221
222 if ((temp = calloc(1, sizeof(mime_magic_t))) == NULL)
223 return (-1);
224
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;
230
231 current->prev = NULL;
232 current->parent = temp;
233
234 DEBUG_printf(("1mimeAddTypeRule: Creating new AND group %p.", temp));
235 }
236 else if (current->parent)
237 {
238 DEBUG_printf(("1mimeAddTypeRule: Setting group %p op to AND.",
239 current->parent));
240 current->parent->op = MIME_MAGIC_AND;
241 }
242
243 logic = MIME_MAGIC_AND;
244 rule ++;
245 }
246 else if (*rule == ',')
247 {
248 if (logic != MIME_MAGIC_OR && current != NULL)
249 {
250 /*
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.
253 */
254
255 if (current->parent == NULL)
256 {
257 /*
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.
260 */
261
262 if ((temp = calloc(1, sizeof(mime_magic_t))) == NULL)
263 return (-1);
264
265 DEBUG_printf(("1mimeAddTypeRule: Creating new AND group %p inside OR "
266 "group.", temp));
267
268 while (current->prev != NULL)
269 {
270 current->parent = temp;
271 current = current->prev;
272 }
273
274 current->parent = temp;
275 temp->op = MIME_MAGIC_AND;
276 temp->child = current;
277
278 mt->rules = current = temp;
279 }
280 else
281 {
282 /*
283 * This isn't the top rule, so go up one level...
284 */
285
286 DEBUG_puts("1mimeAddTypeRule: Going up one level.");
287 current = current->parent;
288 }
289 }
290
291 logic = MIME_MAGIC_OR;
292 rule ++;
293 }
294 else if (*rule == '!')
295 {
296 DEBUG_puts("1mimeAddTypeRule: NOT");
297 invert = 1;
298 rule ++;
299 }
300 else if (isalnum(*rule & 255))
301 {
302 /*
303 * Read an extension name or a function...
304 */
305
306 ptr = name;
307 while (isalnum(*rule & 255) && (size_t)(ptr - name) < (sizeof(name) - 1))
308 *ptr++ = *rule++;
309
310 *ptr = '\0';
311
312 if (*rule == '(')
313 {
314 /*
315 * Read function parameters...
316 */
317
318 rule ++;
319 for (num_values = 0;
320 num_values < (int)(sizeof(value) / sizeof(value[0]));
321 num_values ++)
322 {
323 ptr = value[num_values];
324
325 while ((size_t)(ptr - value[num_values]) < (sizeof(value[0]) - 1) &&
326 *rule != '\0' && *rule != ',' && *rule != ')')
327 {
328 if (isspace(*rule & 255))
329 {
330 /*
331 * Ignore whitespace...
332 */
333
334 rule ++;
335 continue;
336 }
337 else if (*rule == '\"' || *rule == '\'')
338 {
339 /*
340 * Copy quoted strings literally...
341 */
342
343 quote = *rule++;
344
345 while (*rule != '\0' && *rule != quote &&
346 (size_t)(ptr - value[num_values]) < (sizeof(value[0]) - 1))
347 *ptr++ = *rule++;
348
349 if (*rule == quote)
350 rule ++;
351 else
352 return (-1);
353 }
354 else if (*rule == '<')
355 {
356 rule ++;
357
358 while (*rule != '>' && *rule != '\0' &&
359 (size_t)(ptr - value[num_values]) < (sizeof(value[0]) - 1))
360 {
361 if (isxdigit(rule[0] & 255) && isxdigit(rule[1] & 255))
362 {
363 if (isdigit(*rule))
364 *ptr = (char)((*rule++ - '0') << 4);
365 else
366 *ptr = (char)((tolower(*rule++) - 'a' + 10) << 4);
367
368 if (isdigit(*rule))
369 *ptr++ |= *rule++ - '0';
370 else
371 *ptr++ |= tolower(*rule++) - 'a' + 10;
372 }
373 else
374 return (-1);
375 }
376
377 if (*rule == '>')
378 rule ++;
379 else
380 return (-1);
381 }
382 else
383 *ptr++ = *rule++;
384 }
385
386 *ptr = '\0';
387 length[num_values] = ptr - value[num_values];
388
389 if (*rule != ',')
390 {
391 num_values ++;
392 break;
393 }
394
395 rule ++;
396 }
397
398 if (*rule != ')')
399 return (-1);
400
401 rule ++;
402
403 /*
404 * Figure out the function...
405 */
406
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"))
424 op = MIME_MAGIC_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)
430 {
431 mt->priority = atoi(value[0]);
432 continue;
433 }
434 else
435 return (-1);
436 }
437 else
438 {
439 /*
440 * This is just a filename match on the extension...
441 */
442
443 snprintf(value[0], sizeof(value[0]), "*.%s", name);
444 length[0] = (int)strlen(value[0]);
445 op = MIME_MAGIC_MATCH;
446 }
447
448 /*
449 * Add a rule for this operation.
450 */
451
452 if ((temp = calloc(1, sizeof(mime_magic_t))) == NULL)
453 return (-1);
454
455 temp->invert = (short)invert;
456 if (current != NULL)
457 {
458 temp->parent = current->parent;
459 current->next = temp;
460 }
461 else
462 mt->rules = temp;
463
464 temp->prev = current;
465
466 if (logic == MIME_MAGIC_NOP)
467 {
468 /*
469 * Add parenthetical grouping...
470 */
471
472 DEBUG_printf(("1mimeAddTypeRule: Making new OR group %p for "
473 "parenthesis.", temp));
474
475 temp->op = MIME_MAGIC_OR;
476
477 if ((temp->child = calloc(1, sizeof(mime_magic_t))) == NULL)
478 return (-1);
479
480 temp->child->parent = temp;
481 temp->child->invert = temp->invert;
482 temp->invert = 0;
483
484 temp = temp->child;
485 logic = MIME_MAGIC_OR;
486 }
487
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));
491
492 /*
493 * Fill in data for the rule...
494 */
495
496 current = temp;
497 temp->op = (short)op;
498 invert = 0;
499
500 switch (op)
501 {
502 case MIME_MAGIC_MATCH :
503 if ((size_t)length[0] > (sizeof(temp->value.matchv) - 1))
504 return (-1);
505 strlcpy(temp->value.matchv, value[0], sizeof(temp->value.matchv));
506 break;
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;
513 break;
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))
518 return (-1);
519 break;
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))
524 return (-1);
525 temp->length = length[1];
526 memcpy(temp->value.stringv, value[1], (size_t)length[1]);
527 break;
528 case MIME_MAGIC_CHAR :
529 temp->offset = strtol(value[0], NULL, 0);
530 if (length[1] == 1)
531 temp->value.charv = (unsigned char)value[1][0];
532 else
533 temp->value.charv = (unsigned char)strtol(value[1], NULL, 0);
534
535 DEBUG_printf(("1mimeAddTypeRule: CHAR(%d,0x%02x)", temp->offset,
536 temp->value.charv));
537 break;
538 case MIME_MAGIC_SHORT :
539 temp->offset = strtol(value[0], NULL, 0);
540 temp->value.shortv = (unsigned short)strtol(value[1], NULL, 0);
541 break;
542 case MIME_MAGIC_INT :
543 temp->offset = strtol(value[0], NULL, 0);
544 temp->value.intv = (unsigned)strtol(value[1], NULL, 0);
545 break;
546 case MIME_MAGIC_LOCALE :
547 if ((size_t)length[0] > (sizeof(temp->value.localev) - 1))
548 return (-1);
549
550 strlcpy(temp->value.localev, value[0], sizeof(temp->value.localev));
551 break;
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))
556 return (-1);
557 temp->length = length[2];
558 memcpy(temp->value.stringv, value[2], (size_t)length[2]);
559 break;
560 }
561 }
562 else
563 break;
564 }
565
566 return (0);
567 }
568
569
570 /*
571 * 'mimeFileType()' - Determine the type of a file.
572 */
573
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? */
579 {
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 */
584
585
586 DEBUG_printf(("mimeFileType(mime=%p, pathname=\"%s\", filename=\"%s\", "
587 "compression=%p)", mime, pathname, filename, compression));
588
589 /*
590 * Range check input parameters...
591 */
592
593 if (!mime || !pathname)
594 {
595 DEBUG_puts("1mimeFileType: Returning NULL.");
596 return (NULL);
597 }
598
599 /*
600 * Try to open the file...
601 */
602
603 if ((fb.fp = cupsFileOpen(pathname, "r")) == NULL)
604 {
605 DEBUG_printf(("1mimeFileType: Unable to open \"%s\": %s", pathname,
606 strerror(errno)));
607 DEBUG_puts("1mimeFileType: Returning NULL.");
608 return (NULL);
609 }
610
611 /*
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...
614 */
615
616 fb.offset = 0;
617 fb.length = (int)cupsFileRead(fb.fp, (char *)fb.buffer, MIME_MAX_BUFFER);
618
619 if (fb.length <= 0)
620 {
621 DEBUG_printf(("1mimeFileType: Unable to read from \"%s\": %s", pathname, strerror(errno)));
622 DEBUG_puts("1mimeFileType: Returning NULL.");
623
624 cupsFileClose(fb.fp);
625
626 return (NULL);
627 }
628
629 /*
630 * Figure out the base filename (without directory portion)...
631 */
632
633 if (filename)
634 {
635 if ((base = strrchr(filename, '/')) != NULL)
636 base ++;
637 else
638 base = filename;
639 }
640 else if ((base = strrchr(pathname, '/')) != NULL)
641 base ++;
642 else
643 base = pathname;
644
645 /*
646 * Then check it against all known types...
647 */
648
649 for (type = (mime_type_t *)cupsArrayFirst(mime->types), best = NULL;
650 type;
651 type = (mime_type_t *)cupsArrayNext(mime->types))
652 if (mime_check_rules(base, &fb, type->rules))
653 {
654 if (!best || type->priority > best->priority)
655 best = type;
656 }
657
658 /*
659 * Finally, close the file and return a match (if any)...
660 */
661
662 if (compression)
663 {
664 *compression = cupsFileCompression(fb.fp);
665 DEBUG_printf(("1mimeFileType: *compression=%d", *compression));
666 }
667
668 cupsFileClose(fb.fp);
669
670 DEBUG_printf(("1mimeFileType: Returning %p(%s/%s).", best,
671 best ? best->super : "???", best ? best->type : "???"));
672 return (best);
673 }
674
675
676 /*
677 * 'mimeType()' - Lookup a file type.
678 */
679
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 */
684 {
685 mime_type_t key, /* MIME type search key */
686 *mt; /* Matching type */
687
688
689 DEBUG_printf(("mimeType(mime=%p, super=\"%s\", type=\"%s\")", mime, super,
690 type));
691
692 /*
693 * Range check input...
694 */
695
696 if (!mime || !super || !type)
697 {
698 DEBUG_puts("1mimeType: Returning NULL.");
699 return (NULL);
700 }
701
702 /*
703 * Lookup the type in the array...
704 */
705
706 strlcpy(key.super, super, sizeof(key.super));
707 strlcpy(key.type, type, sizeof(key.type));
708
709 mt = (mime_type_t *)cupsArrayFind(mime->types, &key);
710 DEBUG_printf(("1mimeType: Returning %p.", mt));
711 return (mt);
712 }
713
714
715 /*
716 * 'mime_compare_types()' - Compare two MIME super/type names.
717 */
718
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 */
722 {
723 int i; /* Result of comparison */
724
725
726 if ((i = _cups_strcasecmp(t0->super, t1->super)) == 0)
727 i = _cups_strcasecmp(t0->type, t1->type);
728
729 return (i);
730 }
731
732
733 /*
734 * 'mime_check_rules()' - Check each rule in a list.
735 */
736
737 static int /* O - 1 if match, 0 if no match */
738 mime_check_rules(
739 const char *filename, /* I - Filename */
740 _mime_filebuf_t *fb, /* I - File to check */
741 mime_magic_t *rules) /* I - Rules to check */
742 {
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 */
750
751
752 DEBUG_printf(("4mime_check_rules(filename=\"%s\", fb=%p, rules=%p)", filename,
753 fb, rules));
754
755 if (rules == NULL)
756 return (0);
757
758 if (rules->parent == NULL)
759 logic = MIME_MAGIC_OR;
760 else
761 logic = rules->parent->op;
762
763 result = 0;
764
765 while (rules != NULL)
766 {
767 /*
768 * Compute the result of this rule...
769 */
770
771 switch (rules->op)
772 {
773 case MIME_MAGIC_MATCH :
774 result = mime_patmatch(filename, rules->value.matchv);
775 break;
776
777 case MIME_MAGIC_ASCII :
778 /*
779 * Load the buffer if necessary...
780 */
781
782 if (fb->offset < 0 || rules->offset < fb->offset ||
783 (rules->offset + rules->length) > (fb->offset + fb->length))
784 {
785 /*
786 * Reload file buffer...
787 */
788
789 cupsFileSeek(fb->fp, rules->offset);
790 fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
791 sizeof(fb->buffer));
792 fb->offset = rules->offset;
793
794 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_ASCII fb->length=%d", fb->length));
795 }
796
797 /*
798 * Test for ASCII printable characters plus standard control chars.
799 */
800
801 if ((rules->offset + rules->length) > (fb->offset + fb->length))
802 n = fb->offset + fb->length - rules->offset;
803 else
804 n = rules->length;
805
806 bufptr = fb->buffer + rules->offset - fb->offset;
807 while (n > 0)
808 if ((*bufptr >= 32 && *bufptr <= 126) ||
809 (*bufptr >= 8 && *bufptr <= 13) ||
810 *bufptr == 26 || *bufptr == 27)
811 {
812 n --;
813 bufptr ++;
814 }
815 else
816 break;
817
818 result = (n == 0);
819 break;
820
821 case MIME_MAGIC_PRINTABLE :
822 /*
823 * Load the buffer if necessary...
824 */
825
826 if (fb->offset < 0 || rules->offset < fb->offset ||
827 (rules->offset + rules->length) > (fb->offset + fb->length))
828 {
829 /*
830 * Reload file buffer...
831 */
832
833 cupsFileSeek(fb->fp, rules->offset);
834 fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
835 sizeof(fb->buffer));
836 fb->offset = rules->offset;
837
838 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_PRINTABLE fb->length=%d", fb->length));
839 }
840
841 /*
842 * Test for 8-bit printable characters plus standard control chars.
843 */
844
845 if ((rules->offset + rules->length) > (fb->offset + fb->length))
846 n = fb->offset + fb->length - rules->offset;
847 else
848 n = rules->length;
849
850 bufptr = fb->buffer + rules->offset - fb->offset;
851
852 while (n > 0)
853 if (*bufptr >= 128 ||
854 (*bufptr >= 32 && *bufptr <= 126) ||
855 (*bufptr >= 8 && *bufptr <= 13) ||
856 *bufptr == 26 || *bufptr == 27)
857 {
858 n --;
859 bufptr ++;
860 }
861 else
862 break;
863
864 result = (n == 0);
865 break;
866
867 case MIME_MAGIC_REGEX :
868 DEBUG_printf(("5mime_check_rules: regex(%d, \"%s\")", rules->offset,
869 rules->value.stringv));
870
871 /*
872 * Load the buffer if necessary...
873 */
874
875 if (fb->offset < 0 || rules->offset < fb->offset ||
876 (rules->offset + rules->length) > (fb->offset + fb->length))
877 {
878 /*
879 * Reload file buffer...
880 */
881
882 cupsFileSeek(fb->fp, rules->offset);
883 fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
884 sizeof(fb->buffer));
885 fb->offset = rules->offset;
886
887 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_REGEX fb->length=%d", fb->length));
888
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]));
893 }
894
895 /*
896 * Compare the buffer against the string. If the file is too
897 * short then don't compare - it can't match...
898 */
899
900 if (fb->length > 0)
901 {
902 char temp[MIME_MAX_BUFFER + 1];
903 /* Temporary buffer */
904
905 memcpy(temp, fb->buffer, (size_t)fb->length);
906 temp[fb->length] = '\0';
907 result = !regexec(&(rules->value.rev), temp, 0, NULL, 0);
908 }
909
910 DEBUG_printf(("5mime_check_rules: result=%d", result));
911 break;
912
913 case MIME_MAGIC_STRING :
914 DEBUG_printf(("5mime_check_rules: string(%d, \"%s\")", rules->offset,
915 rules->value.stringv));
916
917 /*
918 * Load the buffer if necessary...
919 */
920
921 if (fb->offset < 0 || rules->offset < fb->offset ||
922 (rules->offset + rules->length) > (fb->offset + fb->length))
923 {
924 /*
925 * Reload file buffer...
926 */
927
928 cupsFileSeek(fb->fp, rules->offset);
929 fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
930 sizeof(fb->buffer));
931 fb->offset = rules->offset;
932
933 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_STRING fb->length=%d", fb->length));
934
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]));
939 }
940
941 /*
942 * Compare the buffer against the string. If the file is too
943 * short then don't compare - it can't match...
944 */
945
946 if ((rules->offset + rules->length) > (fb->offset + fb->length))
947 result = 0;
948 else
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));
951 break;
952
953 case MIME_MAGIC_ISTRING :
954 /*
955 * Load the buffer if necessary...
956 */
957
958 if (fb->offset < 0 || rules->offset < fb->offset ||
959 (rules->offset + rules->length) > (fb->offset + fb->length))
960 {
961 /*
962 * Reload file buffer...
963 */
964
965 cupsFileSeek(fb->fp, rules->offset);
966 fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
967 sizeof(fb->buffer));
968 fb->offset = rules->offset;
969
970 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_ISTRING fb->length=%d", fb->length));
971 }
972
973 /*
974 * Compare the buffer against the string. If the file is too
975 * short then don't compare - it can't match...
976 */
977
978 if ((rules->offset + rules->length) > (fb->offset + fb->length))
979 result = 0;
980 else
981 result = !_cups_strncasecmp((char *)fb->buffer + rules->offset - fb->offset, rules->value.stringv, (size_t)rules->length);
982 break;
983
984 case MIME_MAGIC_CHAR :
985 /*
986 * Load the buffer if necessary...
987 */
988
989 if (fb->offset < 0 || rules->offset < fb->offset)
990 {
991 /*
992 * Reload file buffer...
993 */
994
995 cupsFileSeek(fb->fp, rules->offset);
996 fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
997 sizeof(fb->buffer));
998 fb->offset = rules->offset;
999
1000 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_CHAR fb->length=%d", fb->length));
1001 }
1002
1003 /*
1004 * Compare the character values; if the file is too short, it
1005 * can't match...
1006 */
1007
1008 if (fb->length < 1)
1009 result = 0;
1010 else
1011 result = (fb->buffer[rules->offset - fb->offset] ==
1012 rules->value.charv);
1013 break;
1014
1015 case MIME_MAGIC_SHORT :
1016 /*
1017 * Load the buffer if necessary...
1018 */
1019
1020 if (fb->offset < 0 || rules->offset < fb->offset ||
1021 (rules->offset + 2) > (fb->offset + fb->length))
1022 {
1023 /*
1024 * Reload file buffer...
1025 */
1026
1027 cupsFileSeek(fb->fp, rules->offset);
1028 fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
1029 sizeof(fb->buffer));
1030 fb->offset = rules->offset;
1031
1032 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_SHORT fb->length=%d", fb->length));
1033 }
1034
1035 /*
1036 * Compare the short values; if the file is too short, it
1037 * can't match...
1038 */
1039
1040 if (fb->length < 2)
1041 result = 0;
1042 else
1043 {
1044 bufptr = fb->buffer + rules->offset - fb->offset;
1045 shortv = (short)((bufptr[0] << 8) | bufptr[1]);
1046 result = (shortv == rules->value.shortv);
1047 }
1048 break;
1049
1050 case MIME_MAGIC_INT :
1051 /*
1052 * Load the buffer if necessary...
1053 */
1054
1055 if (fb->offset < 0 || rules->offset < fb->offset ||
1056 (rules->offset + 4) > (fb->offset + fb->length))
1057 {
1058 /*
1059 * Reload file buffer...
1060 */
1061
1062 cupsFileSeek(fb->fp, rules->offset);
1063 fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
1064 sizeof(fb->buffer));
1065 fb->offset = rules->offset;
1066
1067 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_INT fb->length=%d", fb->length));
1068 }
1069
1070 /*
1071 * Compare the int values; if the file is too short, it
1072 * can't match...
1073 */
1074
1075 if (fb->length < 4)
1076 result = 0;
1077 else
1078 {
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);
1082 }
1083 break;
1084
1085 case MIME_MAGIC_LOCALE :
1086 #if defined(WIN32) || defined(__EMX__) || defined(__APPLE__)
1087 result = !strcmp(rules->value.localev, setlocale(LC_ALL, ""));
1088 #else
1089 result = !strcmp(rules->value.localev, setlocale(LC_MESSAGES, ""));
1090 #endif /* __APPLE__ */
1091 break;
1092
1093 case MIME_MAGIC_CONTAINS :
1094 /*
1095 * Load the buffer if necessary...
1096 */
1097
1098 if (fb->offset < 0 || rules->offset < fb->offset ||
1099 (rules->offset + rules->region) > (fb->offset + fb->length))
1100 {
1101 /*
1102 * Reload file buffer...
1103 */
1104
1105 cupsFileSeek(fb->fp, rules->offset);
1106 fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
1107 sizeof(fb->buffer));
1108 fb->offset = rules->offset;
1109
1110 DEBUG_printf(("4mime_check_rules: MIME_MAGIC_CONTAINS fb->length=%d", fb->length));
1111 }
1112
1113 /*
1114 * Compare the buffer against the string. If the file is too
1115 * short then don't compare - it can't match...
1116 */
1117
1118 if ((rules->offset + rules->length) > (fb->offset + fb->length))
1119 result = 0;
1120 else
1121 {
1122 if (fb->length > rules->region)
1123 region = rules->region - rules->length;
1124 else
1125 region = fb->length - rules->length;
1126
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)
1129 break;
1130 }
1131 break;
1132
1133 default :
1134 if (rules->child != NULL)
1135 result = mime_check_rules(filename, fb, rules->child);
1136 else
1137 result = 0;
1138 break;
1139 }
1140
1141 /*
1142 * If the logic is inverted, invert the result...
1143 */
1144
1145 if (rules->invert)
1146 result = !result;
1147
1148 /*
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...
1152 */
1153
1154 DEBUG_printf(("5mime_check_rules: result of test %p (MIME_MAGIC_%s) is %d",
1155 rules, debug_ops[rules->op], result));
1156
1157 if ((result && logic == MIME_MAGIC_OR) ||
1158 (!result && logic == MIME_MAGIC_AND))
1159 return (result);
1160
1161 /*
1162 * Otherwise the jury is still out on this one, so move to the next rule.
1163 */
1164
1165 rules = rules->next;
1166 }
1167
1168 return (result);
1169 }
1170
1171
1172 /*
1173 * 'mime_patmatch()' - Pattern matching.
1174 */
1175
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 */
1179 {
1180 /*
1181 * Range check the input...
1182 */
1183
1184 if (s == NULL || pat == NULL)
1185 return (0);
1186
1187 /*
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.
1190 */
1191
1192 while (*s != '\0' && *pat != '\0')
1193 {
1194 if (*pat == '*')
1195 {
1196 /*
1197 * Wildcard - 0 or more characters...
1198 */
1199
1200 pat ++;
1201 if (*pat == '\0')
1202 return (1); /* Last pattern char is *, so everything matches... */
1203
1204 /*
1205 * Test all remaining combinations until we get to the end of the string.
1206 */
1207
1208 while (*s != '\0')
1209 {
1210 if (mime_patmatch(s, pat))
1211 return (1);
1212
1213 s ++;
1214 }
1215 }
1216 else if (*pat == '?')
1217 {
1218 /*
1219 * Wildcard - 1 character...
1220 */
1221
1222 pat ++;
1223 s ++;
1224 continue;
1225 }
1226 else if (*pat == '[')
1227 {
1228 /*
1229 * Match a character from the input set [chars]...
1230 */
1231
1232 pat ++;
1233 while (*pat != ']' && *pat != '\0')
1234 if (*s == *pat)
1235 break;
1236 else
1237 pat ++;
1238
1239 if (*pat == ']' || *pat == '\0')
1240 return (0);
1241
1242 while (*pat != ']' && *pat != '\0')
1243 pat ++;
1244
1245 if (*pat == ']')
1246 pat ++;
1247
1248 continue;
1249 }
1250 else if (*pat == '\\')
1251 {
1252 /*
1253 * Handle quoted characters...
1254 */
1255
1256 pat ++;
1257 }
1258
1259 /*
1260 * Stop if the pattern and string don't match...
1261 */
1262
1263 if (*pat++ != *s++)
1264 return (0);
1265 }
1266
1267 /*
1268 * Done parsing the pattern and string; return 1 if the last character
1269 * matches and 0 otherwise...
1270 */
1271
1272 return (*s == *pat);
1273 }