]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/type.c
Merge changes from CUPS 1.4svn-r7696.
[thirdparty/cups.git] / scheduler / type.c
CommitLineData
ef416fc2 1/*
75bd9771 2 * "$Id: type.c 7694 2008-06-26 00:23:20Z mike $"
ef416fc2 3 *
4 * MIME typing routines for the Common UNIX Printing System (CUPS).
5 *
dd1abb6b 6 * Copyright 2007-2008 by Apple Inc.
fa73b229 7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 14 *
15 * Contents:
16 *
17 * mimeAddType() - Add a MIME type to a database.
18 * mimeAddTypeRule() - Add a detection rule for a file type.
19 * mimeFileType() - Determine the type of a file.
20 * mimeType() - Lookup a file type.
fa73b229 21 * compare_types() - Compare two MIME super/type names.
ef416fc2 22 * checkrules() - Check each rule in a list.
23 * patmatch() - Pattern matching...
24 */
25
26/*
27 * Include necessary headers...
28 */
29
30#include <stdio.h>
31#include <stdlib.h>
32#include <ctype.h>
33#include <locale.h>
34
35#include <cups/string.h>
36#include "mime.h"
37#include <cups/debug.h>
38
39
4400e98d 40/*
41 * Local types...
42 */
43
44typedef struct _mime_filebuf_s /**** File buffer for MIME typing ****/
45{
46 cups_file_t *fp; /* File pointer */
47 int offset, /* Offset in file */
48 length; /* Length of buffered data */
49 unsigned char buffer[MIME_MAX_BUFFER];/* Buffered data */
50} _mime_filebuf_t;
51
52
ef416fc2 53/*
54 * Local functions...
55 */
56
fa73b229 57static int compare_types(mime_type_t *t0, mime_type_t *t1);
4400e98d 58static int checkrules(const char *filename, _mime_filebuf_t *fb,
59 mime_magic_t *rules);
60static int patmatch(const char *s, const char *pat);
ef416fc2 61
62
63/*
64 * 'mimeAddType()' - Add a MIME type to a database.
65 */
66
fa73b229 67mime_type_t * /* O - New (or existing) MIME type */
68mimeAddType(mime_t *mime, /* I - MIME database */
69 const char *super, /* I - Super-type name */
70 const char *type) /* I - Type name */
ef416fc2 71{
fa73b229 72 mime_type_t *temp; /* New MIME type */
ef416fc2 73
74
75 /*
76 * Range check input...
77 */
78
fa73b229 79 if (!mime || !super || !type)
ef416fc2 80 return (NULL);
81
82 /*
83 * See if the type already exists; if so, return the existing type...
84 */
85
86 if ((temp = mimeType(mime, super, type)) != NULL)
87 return (temp);
88
89 /*
90 * The type doesn't exist; add it...
91 */
92
fa73b229 93 if (!mime->types)
94 mime->types = cupsArrayNew((cups_array_func_t)compare_types, NULL);
ef416fc2 95
fa73b229 96 if (!mime->types)
ef416fc2 97 return (NULL);
ef416fc2 98
fa73b229 99 if ((temp = calloc(1, sizeof(mime_type_t) - MIME_MAX_TYPE +
100 strlen(type) + 1)) == NULL)
101 return (NULL);
ef416fc2 102
ef416fc2 103 strlcpy(temp->super, super, sizeof(temp->super));
fa73b229 104 strcpy(temp->type, type); /* Safe: temp->type is allocated */
dd1abb6b 105 temp->priority = 100;
ef416fc2 106
fa73b229 107 cupsArrayAdd(mime->types, temp);
ef416fc2 108
109 return (temp);
110}
111
112
113/*
114 * 'mimeAddTypeRule()' - Add a detection rule for a file type.
115 */
116
117int /* O - 0 on success, -1 on failure */
118mimeAddTypeRule(mime_type_t *mt, /* I - Type to add to */
119 const char *rule) /* I - Rule to add */
120{
121 int num_values, /* Number of values seen */
122 op, /* Operation code */
123 logic, /* Logic for next rule */
124 invert; /* Invert following rule? */
125 char name[255], /* Name in rule string */
126 value[3][255], /* Value in rule string */
127 *ptr, /* Position in name or value */
128 quote; /* Quote character */
129 int length[3]; /* Length of each parameter */
130 mime_magic_t *temp, /* New rule */
131 *current; /* Current rule */
132
133
134 /*
135 * Range check input...
136 */
137
fa73b229 138 if (!mt || !rule)
ef416fc2 139 return (-1);
140
141 /*
142 * Find the last rule in the top-level of the rules tree.
143 */
144
fa73b229 145 for (current = mt->rules; current; current = current->next)
146 if (!current->next)
ef416fc2 147 break;
148
149 /*
150 * Parse the rules string. Most rules are either a file extension or a
151 * comparison function:
152 *
153 * extension
154 * function(parameters)
155 */
156
157 logic = MIME_MAGIC_NOP;
158 invert = 0;
159
75bd9771 160 DEBUG_printf(("mimeAddTypeRule: %s/%s: %s\n", mt->super, mt->type, rule));
ef416fc2 161
162 while (*rule != '\0')
163 {
164 while (isspace(*rule & 255))
165 rule ++;
166
167 if (*rule == '(')
168 {
75bd9771 169 DEBUG_puts("mimeAddTypeRule: New parenthesis group");
ef416fc2 170 logic = MIME_MAGIC_NOP;
171 rule ++;
172 }
173 else if (*rule == ')')
174 {
75bd9771 175 DEBUG_puts("mimeAddTypeRule: Close paren...");
ef416fc2 176 if (current == NULL || current->parent == NULL)
177 return (-1);
178
179 current = current->parent;
180
181 if (current->parent == NULL)
182 logic = MIME_MAGIC_OR;
183 else
184 logic = current->parent->op;
185
186 rule ++;
187 }
188 else if (*rule == '+' && current != NULL)
189 {
190 if (logic != MIME_MAGIC_AND &&
4400e98d 191 current != NULL && current->prev != NULL &&
192 current->prev->prev != NULL)
ef416fc2 193 {
194 /*
195 * OK, we have more than 1 rule in the current tree level... Make a
196 * new group tree and move the previous rule to it...
197 */
198
199 if ((temp = calloc(1, sizeof(mime_magic_t))) == NULL)
200 return (-1);
201
202 temp->op = MIME_MAGIC_AND;
203 temp->child = current;
204 temp->parent = current->parent;
205 current->prev->next = temp;
206 temp->prev = current->prev;
207
208 current->prev = NULL;
209 current->parent = temp;
210
75bd9771 211 DEBUG_printf(("mimeAddTypeRule: Creating new AND group %p...\n", temp));
ef416fc2 212 }
213 else
214 {
75bd9771
MS
215 DEBUG_printf(("mimeAddTypeRule: Setting group %p op to AND...\n",
216 current->parent));
ef416fc2 217 current->parent->op = MIME_MAGIC_AND;
218 }
219
220 logic = MIME_MAGIC_AND;
221 rule ++;
222 }
223 else if (*rule == ',')
224 {
225 if (logic != MIME_MAGIC_OR && current != NULL)
226 {
227 /*
228 * OK, we have two possibilities; either this is the top-level rule or
229 * we have a bunch of AND rules at this level.
230 */
231
232 if (current->parent == NULL)
233 {
234 /*
235 * This is the top-level rule; we have to move *all* of the AND rules
236 * down a level, as AND has precedence over OR.
237 */
238
239 if ((temp = calloc(1, sizeof(mime_magic_t))) == NULL)
240 return (-1);
241
75bd9771
MS
242 DEBUG_printf(("mimeAddTypeRule: Creating new AND group %p inside OR "
243 "group\n", temp));
ef416fc2 244
245 while (current->prev != NULL)
246 {
247 current->parent = temp;
248 current = current->prev;
249 }
250
251 current->parent = temp;
252 temp->op = MIME_MAGIC_AND;
253 temp->child = current;
254
255 mt->rules = current = temp;
256 }
257 else
258 {
259 /*
260 * This isn't the top rule, so go up one level...
261 */
262
75bd9771 263 DEBUG_puts("mimeAddTypeRule: Going up one level");
ef416fc2 264 current = current->parent;
265 }
266 }
267
268 logic = MIME_MAGIC_OR;
269 rule ++;
270 }
271 else if (*rule == '!')
272 {
75bd9771 273 DEBUG_puts("mimeAddTypeRule: NOT");
ef416fc2 274 invert = 1;
275 rule ++;
276 }
277 else if (isalnum(*rule & 255))
278 {
279 /*
280 * Read an extension name or a function...
281 */
282
4400e98d 283 ptr = name;
284 while (isalnum(*rule & 255) && (ptr - name) < (sizeof(name) - 1))
ef416fc2 285 *ptr++ = *rule++;
286
287 *ptr = '\0';
288 num_values = 0;
289
290 if (*rule == '(')
291 {
292 /*
293 * Read function parameters...
294 */
295
296 rule ++;
297 for (num_values = 0;
298 num_values < (sizeof(value) / sizeof(value[0]));
299 num_values ++)
300 {
301 ptr = value[num_values];
302
303 while ((ptr - value[num_values]) < (sizeof(value[0]) - 1) &&
304 *rule != '\0' && *rule != ',' && *rule != ')')
305 {
306 if (isspace(*rule & 255))
307 {
308 /*
309 * Ignore whitespace...
310 */
311
312 rule ++;
313 continue;
314 }
315 else if (*rule == '\"' || *rule == '\'')
316 {
317 /*
318 * Copy quoted strings literally...
319 */
320
321 quote = *rule++;
322
323 while (*rule != '\0' && *rule != quote &&
324 (ptr - value[num_values]) < (sizeof(value[0]) - 1))
325 *ptr++ = *rule++;
326
327 if (*rule == quote)
328 rule ++;
329 else
330 return (-1);
331 }
332 else if (*rule == '<')
333 {
334 rule ++;
335
336 while (*rule != '>' && *rule != '\0' &&
337 (ptr - value[num_values]) < (sizeof(value[0]) - 1))
338 {
339 if (isxdigit(rule[0] & 255) && isxdigit(rule[1] & 255))
340 {
341 if (isdigit(*rule))
342 *ptr = (*rule++ - '0') << 4;
343 else
344 *ptr = (tolower(*rule++) - 'a' + 10) << 4;
345
346 if (isdigit(*rule))
347 *ptr++ |= *rule++ - '0';
348 else
349 *ptr++ |= tolower(*rule++) - 'a' + 10;
350 }
351 else
352 return (-1);
353 }
354
355 if (*rule == '>')
356 rule ++;
357 else
358 return (-1);
359 }
360 else
361 *ptr++ = *rule++;
362 }
363
364 *ptr = '\0';
365 length[num_values] = ptr - value[num_values];
366
367 if (*rule != ',')
368 break;
369
370 rule ++;
371 }
372
373 if (*rule != ')')
374 return (-1);
375
376 rule ++;
377
378 /*
379 * Figure out the function...
380 */
381
fa73b229 382 if (!strcmp(name, "match"))
ef416fc2 383 op = MIME_MAGIC_MATCH;
fa73b229 384 else if (!strcmp(name, "ascii"))
ef416fc2 385 op = MIME_MAGIC_ASCII;
fa73b229 386 else if (!strcmp(name, "printable"))
ef416fc2 387 op = MIME_MAGIC_PRINTABLE;
fa73b229 388 else if (!strcmp(name, "string"))
ef416fc2 389 op = MIME_MAGIC_STRING;
fa73b229 390 else if (!strcmp(name, "istring"))
ef416fc2 391 op = MIME_MAGIC_ISTRING;
fa73b229 392 else if (!strcmp(name, "char"))
ef416fc2 393 op = MIME_MAGIC_CHAR;
fa73b229 394 else if (!strcmp(name, "short"))
ef416fc2 395 op = MIME_MAGIC_SHORT;
fa73b229 396 else if (!strcmp(name, "int"))
ef416fc2 397 op = MIME_MAGIC_INT;
fa73b229 398 else if (!strcmp(name, "locale"))
ef416fc2 399 op = MIME_MAGIC_LOCALE;
fa73b229 400 else if (!strcmp(name, "contains"))
ef416fc2 401 op = MIME_MAGIC_CONTAINS;
dd1abb6b
MS
402 else if (!strcmp(name, "priority") && num_values == 1)
403 {
404 mt->priority = atoi(value[0]);
405 continue;
406 }
ef416fc2 407 else
408 return (-1);
409 }
410 else
411 {
412 /*
413 * This is just a filename match on the extension...
414 */
415
416 snprintf(value[0], sizeof(value[0]), "*.%s", name);
417 length[0] = strlen(value[0]);
418 num_values = 1;
419 op = MIME_MAGIC_MATCH;
420 }
421
422 /*
423 * Add a rule for this operation.
424 */
425
426 if ((temp = calloc(1, sizeof(mime_magic_t))) == NULL)
427 return (-1);
428
429 temp->invert = invert;
430 if (current != NULL)
431 {
432 temp->parent = current->parent;
433 current->next = temp;
434 }
435 else
436 mt->rules = temp;
437
438 temp->prev = current;
439
440 if (logic == MIME_MAGIC_NOP)
441 {
442 /*
443 * Add parenthetical grouping...
444 */
445
75bd9771
MS
446 DEBUG_printf(("mimeAddTypeRule: Making new OR group %p for "
447 "parenthesis...\n", temp));
ef416fc2 448
449 temp->op = MIME_MAGIC_OR;
450
451 if ((temp->child = calloc(1, sizeof(mime_magic_t))) == NULL)
452 return (-1);
453
454 temp->child->parent = temp;
455
456 temp = temp->child;
457 logic = MIME_MAGIC_OR;
458 }
459
75bd9771
MS
460 DEBUG_printf(("mimeAddTypeRule: adding %p: %s, op=%d, logic=%d, "
461 "invert=%d\n", temp, name, op, logic, invert));
ef416fc2 462
463 /*
464 * Fill in data for the rule...
465 */
466
467 current = temp;
468 temp->op = op;
469 invert = 0;
470
471 switch (op)
472 {
473 case MIME_MAGIC_MATCH :
474 if (length[0] > (sizeof(temp->value.matchv) - 1))
475 return (-1);
476 strcpy(temp->value.matchv, value[0]);
477 break;
478 case MIME_MAGIC_ASCII :
479 case MIME_MAGIC_PRINTABLE :
480 temp->offset = strtol(value[0], NULL, 0);
481 temp->length = strtol(value[1], NULL, 0);
482 if (temp->length > MIME_MAX_BUFFER)
483 temp->length = MIME_MAX_BUFFER;
484 break;
485 case MIME_MAGIC_STRING :
486 case MIME_MAGIC_ISTRING :
487 temp->offset = strtol(value[0], NULL, 0);
488 if (length[1] > sizeof(temp->value.stringv))
489 return (-1);
490 temp->length = length[1];
491 memcpy(temp->value.stringv, value[1], length[1]);
492 break;
493 case MIME_MAGIC_CHAR :
494 temp->offset = strtol(value[0], NULL, 0);
495 if (length[1] == 1)
496 temp->value.charv = value[1][0];
497 else
498 temp->value.charv = (char)strtol(value[1], NULL, 0);
499 break;
500 case MIME_MAGIC_SHORT :
501 temp->offset = strtol(value[0], NULL, 0);
502 temp->value.shortv = (short)strtol(value[1], NULL, 0);
503 break;
504 case MIME_MAGIC_INT :
505 temp->offset = strtol(value[0], NULL, 0);
506 temp->value.intv = (int)strtol(value[1], NULL, 0);
507 break;
508 case MIME_MAGIC_LOCALE :
509 if (length[0] > (sizeof(temp->value.localev) - 1))
510 return (-1);
511
512 strcpy(temp->value.localev, value[0]);
513 break;
514 case MIME_MAGIC_CONTAINS :
515 temp->offset = strtol(value[0], NULL, 0);
516 temp->region = strtol(value[1], NULL, 0);
517 if (length[2] > sizeof(temp->value.stringv))
518 return (-1);
519 temp->length = length[2];
520 memcpy(temp->value.stringv, value[2], length[2]);
521 break;
522 }
523 }
524 else
525 break;
526 }
527
528 return (0);
529}
530
531
532/*
533 * 'mimeFileType()' - Determine the type of a file.
534 */
535
536mime_type_t * /* O - Type of file */
537mimeFileType(mime_t *mime, /* I - MIME database */
4400e98d 538 const char *pathname, /* I - Name of file to check on disk */
539 const char *filename, /* I - Original filename or NULL */
ef416fc2 540 int *compression) /* O - Is the file compressed? */
541{
4400e98d 542 _mime_filebuf_t fb; /* File buffer */
543 const char *base; /* Base filename of file */
dd1abb6b
MS
544 mime_type_t *type, /* File type */
545 *best; /* Best match */
ef416fc2 546
547
b423cd4c 548 DEBUG_printf(("mimeFileType(mime=%p, pathname=\"%s\", filename=\"%s\", "
75bd9771 549 "compression=%p)\n", mime, pathname, filename, compression));
ef416fc2 550
551 /*
552 * Range check input parameters...
553 */
554
fa73b229 555 if (!mime || !pathname)
ef416fc2 556 return (NULL);
557
558 /*
559 * Try to open the file...
560 */
561
4400e98d 562 if ((fb.fp = cupsFileOpen(pathname, "r")) == NULL)
ef416fc2 563 return (NULL);
564
4400e98d 565 fb.offset = -1;
566 fb.length = 0;
567
ef416fc2 568 /*
4400e98d 569 * Figure out the base filename (without directory portion)...
ef416fc2 570 */
571
4400e98d 572 if (filename)
573 {
574 if ((base = strrchr(filename, '/')) != NULL)
575 base ++;
576 else
91c84a35 577 base = filename;
4400e98d 578 }
579 else if ((base = strrchr(pathname, '/')) != NULL)
580 base ++;
ef416fc2 581 else
91c84a35 582 base = pathname;
ef416fc2 583
584 /*
585 * Then check it against all known types...
586 */
587
dd1abb6b 588 for (type = (mime_type_t *)cupsArrayFirst(mime->types), best = NULL;
fa73b229 589 type;
590 type = (mime_type_t *)cupsArrayNext(mime->types))
4400e98d 591 if (checkrules(base, &fb, type->rules))
dd1abb6b
MS
592 {
593 if (!best || type->priority > best->priority)
594 best = type;
595 }
ef416fc2 596
597 /*
598 * Finally, close the file and return a match (if any)...
599 */
600
601 if (compression)
4400e98d 602 *compression = cupsFileCompression(fb.fp);
ef416fc2 603
4400e98d 604 cupsFileClose(fb.fp);
ef416fc2 605
dd1abb6b 606 return (best);
ef416fc2 607}
608
609
610/*
611 * 'mimeType()' - Lookup a file type.
612 */
613
fa73b229 614mime_type_t * /* O - Matching file type definition */
615mimeType(mime_t *mime, /* I - MIME database */
616 const char *super, /* I - Super-type name */
617 const char *type) /* I - Type name */
ef416fc2 618{
fa73b229 619 mime_type_t key; /* MIME type search key*/
620
ef416fc2 621
622 /*
623 * Range check input...
624 */
625
fa73b229 626 if (!mime || !super || !type)
ef416fc2 627 return (NULL);
628
629 /*
630 * Lookup the type in the array...
631 */
632
633 strlcpy(key.super, super, sizeof(key.super));
fa73b229 634 strlcpy(key.type, type, sizeof(key.type));
ef416fc2 635
fa73b229 636 return ((mime_type_t *)cupsArrayFind(mime->types, &key));
ef416fc2 637}
638
639
640/*
fa73b229 641 * 'compare_types()' - Compare two MIME super/type names.
ef416fc2 642 */
643
fa73b229 644static int /* O - Result of comparison */
645compare_types(mime_type_t *t0, /* I - First type */
646 mime_type_t *t1) /* I - Second type */
ef416fc2 647{
fa73b229 648 int i; /* Result of comparison */
ef416fc2 649
650
080811b1
MS
651 if ((i = strcasecmp(t0->super, t1->super)) == 0)
652 i = strcasecmp(t0->type, t1->type);
ef416fc2 653
654 return (i);
655}
656
657
658/*
659 * 'checkrules()' - Check each rule in a list.
660 */
661
662static int /* O - 1 if match, 0 if no match */
4400e98d 663checkrules(const char *filename, /* I - Filename */
664 _mime_filebuf_t *fb, /* I - File to check */
665 mime_magic_t *rules) /* I - Rules to check */
ef416fc2 666{
667 int n; /* Looping var */
668 int region; /* Region to look at */
669 int logic, /* Logic to apply */
670 result, /* Result of test */
671 intv; /* Integer value */
672 short shortv; /* Short value */
4400e98d 673 unsigned char *bufptr; /* Pointer into buffer */
ef416fc2 674#ifdef DEBUG
675 const char * const debug_tests[] = /* Test names... */
676 {
677 "NOP", /* No operation */
678 "AND", /* Logical AND of all children */
679 "OR", /* Logical OR of all children */
680 "MATCH", /* Filename match */
681 "ASCII", /* ASCII characters in range */
4400e98d 682 "PRINTABLE", /* Printable characters (32-255) */
ef416fc2 683 "STRING", /* String matches */
684 "CHAR", /* Character/byte matches */
685 "SHORT", /* Short/16-bit word matches */
686 "INT", /* Integer/32-bit word matches */
687 "LOCALE" /* Current locale matches string */
688 "CONTAINS" /* File contains a string */
689 "ISTRING" /* Case-insensitive string matches */
690 };
691#endif /* DEBUG */
692
693
b423cd4c 694 DEBUG_printf(("checkrules(filename=\"%s\", fb=%p, rules=%p)\n", filename,
695 fb, rules));
ef416fc2 696
697 if (rules == NULL)
698 return (0);
699
700 if (rules->parent == NULL)
701 logic = MIME_MAGIC_OR;
702 else
703 logic = rules->parent->op;
704
4400e98d 705 result = 0;
ef416fc2 706
707 while (rules != NULL)
708 {
709 /*
710 * Compute the result of this rule...
711 */
712
713 switch (rules->op)
714 {
715 case MIME_MAGIC_MATCH :
716 result = patmatch(filename, rules->value.matchv);
717 break;
718
719 case MIME_MAGIC_ASCII :
720 /*
721 * Load the buffer if necessary...
722 */
723
4400e98d 724 if (fb->offset < 0 || rules->offset < fb->offset ||
725 (rules->offset + rules->length) > (fb->offset + fb->length))
ef416fc2 726 {
727 /*
728 * Reload file buffer...
729 */
730
4400e98d 731 cupsFileSeek(fb->fp, rules->offset);
732 fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
733 sizeof(fb->buffer));
734 fb->offset = rules->offset;
ef416fc2 735 }
736
737 /*
738 * Test for ASCII printable characters plus standard control chars.
739 */
740
4400e98d 741 if ((rules->offset + rules->length) > (fb->offset + fb->length))
742 n = fb->offset + fb->length - rules->offset;
ef416fc2 743 else
744 n = rules->length;
745
4400e98d 746 bufptr = fb->buffer + rules->offset - fb->offset;
ef416fc2 747 while (n > 0)
748 if ((*bufptr >= 32 && *bufptr <= 126) ||
749 (*bufptr >= 8 && *bufptr <= 13) ||
750 *bufptr == 26 || *bufptr == 27)
751 {
752 n --;
753 bufptr ++;
754 }
755 else
756 break;
757
758 result = (n == 0);
759 break;
760
761 case MIME_MAGIC_PRINTABLE :
762 /*
763 * Load the buffer if necessary...
764 */
765
4400e98d 766 if (fb->offset < 0 || rules->offset < fb->offset ||
767 (rules->offset + rules->length) > (fb->offset + fb->length))
ef416fc2 768 {
769 /*
770 * Reload file buffer...
771 */
772
4400e98d 773 cupsFileSeek(fb->fp, rules->offset);
774 fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
775 sizeof(fb->buffer));
776 fb->offset = rules->offset;
ef416fc2 777 }
778
779 /*
780 * Test for 8-bit printable characters plus standard control chars.
781 */
782
4400e98d 783 if ((rules->offset + rules->length) > (fb->offset + fb->length))
784 n = fb->offset + fb->length - rules->offset;
ef416fc2 785 else
786 n = rules->length;
787
4400e98d 788 bufptr = fb->buffer + rules->offset - fb->offset;
ef416fc2 789
790 while (n > 0)
791 if (*bufptr >= 128 ||
792 (*bufptr >= 32 && *bufptr <= 126) ||
793 (*bufptr >= 8 && *bufptr <= 13) ||
794 *bufptr == 26 || *bufptr == 27)
795 {
796 n --;
797 bufptr ++;
798 }
799 else
800 break;
801
802 result = (n == 0);
803 break;
804
805 case MIME_MAGIC_STRING :
75bd9771 806 DEBUG_printf(("checkrules: string(%d, \"%s\")\n", rules->offset,
ef416fc2 807 rules->value.stringv));
808
809 /*
810 * Load the buffer if necessary...
811 */
812
4400e98d 813 if (fb->offset < 0 || rules->offset < fb->offset ||
814 (rules->offset + rules->length) > (fb->offset + fb->length))
ef416fc2 815 {
816 /*
817 * Reload file buffer...
818 */
819
4400e98d 820 cupsFileSeek(fb->fp, rules->offset);
821 fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
822 sizeof(fb->buffer));
823 fb->offset = rules->offset;
ef416fc2 824
75bd9771 825 DEBUG_printf(("checkrules: loaded %d byte fb->buffer at %d, starts "
4400e98d 826 "with \"%c%c%c%c\"...\n",
827 fb->length, fb->offset, fb->buffer[0], fb->buffer[1],
828 fb->buffer[2], fb->buffer[3]));
ef416fc2 829 }
830
831 /*
832 * Compare the buffer against the string. If the file is too
833 * short then don't compare - it can't match...
834 */
835
4400e98d 836 if ((rules->offset + rules->length) > (fb->offset + fb->length))
ef416fc2 837 result = 0;
838 else
4400e98d 839 result = (memcmp(fb->buffer + rules->offset - fb->offset,
ef416fc2 840 rules->value.stringv, rules->length) == 0);
75bd9771 841 DEBUG_printf(("checkrules: result=%d\n", result));
ef416fc2 842 break;
843
844 case MIME_MAGIC_ISTRING :
845 /*
846 * Load the buffer if necessary...
847 */
848
4400e98d 849 if (fb->offset < 0 || rules->offset < fb->offset ||
850 (rules->offset + rules->length) > (fb->offset + fb->length))
ef416fc2 851 {
852 /*
853 * Reload file buffer...
854 */
855
4400e98d 856 cupsFileSeek(fb->fp, rules->offset);
857 fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
858 sizeof(fb->buffer));
859 fb->offset = rules->offset;
ef416fc2 860 }
861
862 /*
863 * Compare the buffer against the string. If the file is too
864 * short then don't compare - it can't match...
865 */
866
4400e98d 867 if ((rules->offset + rules->length) > (fb->offset + fb->length))
ef416fc2 868 result = 0;
869 else
4400e98d 870 result = (strncasecmp((char *)fb->buffer + rules->offset -
871 fb->offset,
ef416fc2 872 rules->value.stringv, rules->length) == 0);
873 break;
874
875 case MIME_MAGIC_CHAR :
876 /*
877 * Load the buffer if necessary...
878 */
879
4400e98d 880 if (fb->offset < 0 || rules->offset < fb->offset)
ef416fc2 881 {
882 /*
883 * Reload file buffer...
884 */
885
4400e98d 886 cupsFileSeek(fb->fp, rules->offset);
887 fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
888 sizeof(fb->buffer));
889 fb->offset = rules->offset;
ef416fc2 890 }
891
892 /*
893 * Compare the character values; if the file is too short, it
894 * can't match...
895 */
896
4400e98d 897 if (fb->length < 1)
ef416fc2 898 result = 0;
899 else
4400e98d 900 result = (fb->buffer[rules->offset - fb->offset] ==
901 rules->value.charv);
ef416fc2 902 break;
903
904 case MIME_MAGIC_SHORT :
905 /*
906 * Load the buffer if necessary...
907 */
908
4400e98d 909 if (fb->offset < 0 || rules->offset < fb->offset ||
910 (rules->offset + 2) > (fb->offset + fb->length))
ef416fc2 911 {
912 /*
913 * Reload file buffer...
914 */
915
4400e98d 916 cupsFileSeek(fb->fp, rules->offset);
917 fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
918 sizeof(fb->buffer));
919 fb->offset = rules->offset;
ef416fc2 920 }
921
922 /*
923 * Compare the short values; if the file is too short, it
924 * can't match...
925 */
926
4400e98d 927 if (fb->length < 2)
ef416fc2 928 result = 0;
929 else
930 {
4400e98d 931 bufptr = fb->buffer + rules->offset - fb->offset;
ef416fc2 932 shortv = (bufptr[0] << 8) | bufptr[1];
933 result = (shortv == rules->value.shortv);
934 }
935 break;
936
937 case MIME_MAGIC_INT :
938 /*
939 * Load the buffer if necessary...
940 */
941
4400e98d 942 if (fb->offset < 0 || rules->offset < fb->offset ||
943 (rules->offset + 4) > (fb->offset + fb->length))
ef416fc2 944 {
945 /*
946 * Reload file buffer...
947 */
948
4400e98d 949 cupsFileSeek(fb->fp, rules->offset);
950 fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
951 sizeof(fb->buffer));
952 fb->offset = rules->offset;
ef416fc2 953 }
954
955 /*
956 * Compare the int values; if the file is too short, it
957 * can't match...
958 */
959
4400e98d 960 if (fb->length < 4)
ef416fc2 961 result = 0;
962 else
963 {
4400e98d 964 bufptr = fb->buffer + rules->offset - fb->offset;
965 intv = (((((bufptr[0] << 8) | bufptr[1]) << 8) |
966 bufptr[2]) << 8) | bufptr[3];
ef416fc2 967 result = (intv == rules->value.intv);
968 }
969 break;
970
971 case MIME_MAGIC_LOCALE :
972#if defined(WIN32) || defined(__EMX__) || defined(__APPLE__)
4400e98d 973 result = (strcmp(rules->value.localev,
974 setlocale(LC_ALL, "")) == 0);
ef416fc2 975#else
4400e98d 976 result = (strcmp(rules->value.localev,
977 setlocale(LC_MESSAGES, "")) == 0);
ef416fc2 978#endif /* __APPLE__ */
979 break;
980
981 case MIME_MAGIC_CONTAINS :
982 /*
983 * Load the buffer if necessary...
984 */
985
4400e98d 986 if (fb->offset < 0 || rules->offset < fb->offset ||
987 (rules->offset + rules->region) > (fb->offset + fb->length))
ef416fc2 988 {
989 /*
990 * Reload file buffer...
991 */
992
4400e98d 993 cupsFileSeek(fb->fp, rules->offset);
994 fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
995 sizeof(fb->buffer));
996 fb->offset = rules->offset;
ef416fc2 997 }
998
999 /*
1000 * Compare the buffer against the string. If the file is too
1001 * short then don't compare - it can't match...
1002 */
1003
4400e98d 1004 if ((rules->offset + rules->length) > (fb->offset + fb->length))
ef416fc2 1005 result = 0;
1006 else
1007 {
4400e98d 1008 if (fb->length > rules->region)
ef416fc2 1009 region = rules->region - rules->length;
1010 else
4400e98d 1011 region = fb->length - rules->length;
ef416fc2 1012
1013 for (n = 0; n < region; n ++)
4400e98d 1014 if ((result = (memcmp(fb->buffer + rules->offset - fb->offset + n,
1015 rules->value.stringv,
1016 rules->length) == 0)) != 0)
ef416fc2 1017 break;
1018 }
1019 break;
1020
1021 default :
1022 if (rules->child != NULL)
4400e98d 1023 result = checkrules(filename, fb, rules->child);
ef416fc2 1024 else
1025 result = 0;
1026 break;
1027 }
1028
1029 /*
1030 * If the logic is inverted, invert the result...
1031 */
1032
1033 if (rules->invert)
1034 result = !result;
1035
1036 /*
1037 * OK, now if the current logic is OR and this result is true, the this
1038 * rule set is true. If the current logic is AND and this result is false,
1039 * the the rule set is false...
1040 */
1041
75bd9771
MS
1042 DEBUG_printf(("checkrules: result of test %p (MIME_MAGIC_%s) is %d\n",
1043 rules, debug_tests[rules->op], result));
ef416fc2 1044
1045 if ((result && logic == MIME_MAGIC_OR) ||
1046 (!result && logic == MIME_MAGIC_AND))
1047 return (result);
1048
1049 /*
1050 * Otherwise the jury is still out on this one, so move to the next rule.
1051 */
1052
1053 rules = rules->next;
1054 }
1055
1056 return (result);
1057}
1058
1059
1060/*
1061 * 'patmatch()' - Pattern matching...
1062 */
1063
1064static int /* O - 1 if match, 0 if no match */
1065patmatch(const char *s, /* I - String to match against */
1066 const char *pat) /* I - Pattern to match against */
1067{
1068 /*
1069 * Range check the input...
1070 */
1071
1072 if (s == NULL || pat == NULL)
1073 return (0);
1074
1075 /*
1076 * Loop through the pattern and match strings, and stop if we come to a
1077 * point where the strings don't match or we find a complete match.
1078 */
1079
1080 while (*s != '\0' && *pat != '\0')
1081 {
1082 if (*pat == '*')
1083 {
1084 /*
1085 * Wildcard - 0 or more characters...
1086 */
1087
1088 pat ++;
1089 if (*pat == '\0')
4400e98d 1090 return (1); /* Last pattern char is *, so everything matches... */
ef416fc2 1091
1092 /*
1093 * Test all remaining combinations until we get to the end of the string.
1094 */
1095
1096 while (*s != '\0')
1097 {
1098 if (patmatch(s, pat))
1099 return (1);
1100
1101 s ++;
1102 }
1103 }
1104 else if (*pat == '?')
1105 {
1106 /*
1107 * Wildcard - 1 character...
1108 */
1109
1110 pat ++;
1111 s ++;
1112 continue;
1113 }
1114 else if (*pat == '[')
1115 {
1116 /*
1117 * Match a character from the input set [chars]...
1118 */
1119
1120 pat ++;
1121 while (*pat != ']' && *pat != '\0')
1122 if (*s == *pat)
1123 break;
1124 else
1125 pat ++;
1126
1127 if (*pat == ']' || *pat == '\0')
1128 return (0);
1129
1130 while (*pat != ']' && *pat != '\0')
1131 pat ++;
1132
1133 if (*pat == ']')
1134 pat ++;
1135
1136 continue;
1137 }
1138 else if (*pat == '\\')
1139 {
1140 /*
1141 * Handle quoted characters...
1142 */
1143
1144 pat ++;
1145 }
1146
1147 /*
1148 * Stop if the pattern and string don't match...
1149 */
1150
1151 if (*pat++ != *s++)
1152 return (0);
1153 }
1154
1155 /*
4400e98d 1156 * Done parsing the pattern and string; return 1 if the last character
1157 * matches and 0 otherwise...
ef416fc2 1158 */
1159
1160 return (*s == *pat);
1161}
1162
1163
1164/*
75bd9771 1165 * End of "$Id: type.c 7694 2008-06-26 00:23:20Z mike $".
ef416fc2 1166 */