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