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