diff options
author | msweet <msweet@a1ca3aef-8c08-0410-bb20-df032aa958be> | 2013-09-10 19:15:36 +0000 |
---|---|---|
committer | msweet <msweet@a1ca3aef-8c08-0410-bb20-df032aa958be> | 2013-09-10 19:15:36 +0000 |
commit | 6ee54c072996fd4cde3d5a53e80918e90f8e29c5 (patch) | |
tree | 32be8fbd077075bd27074d4ff68d29a0077fbe1f /scheduler/type.c | |
parent | 2c3f586529d576638b4975a5b38e626bcc46303e (diff) | |
download | cups-6ee54c072996fd4cde3d5a53e80918e90f8e29c5.tar.gz |
<rdar://problem/11131245> PDF detection in cups needs to be tightened up
Add regex() rule support.
git-svn-id: svn+ssh://src.apple.com/svn/cups/cups.org/trunk@11272 a1ca3aef-8c08-0410-bb20-df032aa958be
Diffstat (limited to 'scheduler/type.c')
-rw-r--r-- | scheduler/type.c | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/scheduler/type.c b/scheduler/type.c index 7b8743882..c466a257f 100644 --- a/scheduler/type.c +++ b/scheduler/type.c @@ -3,7 +3,7 @@ * * MIME typing routines for CUPS. * - * Copyright 2007-2012 by Apple Inc. + * Copyright 2007-2013 by Apple Inc. * Copyright 1997-2006 by Easy Software Products, all rights reserved. * * These coded instructions, statements, and computer programs are the @@ -425,6 +425,8 @@ mimeAddTypeRule(mime_type_t *mt, /* I - Type to add to */ op = MIME_MAGIC_ASCII; else if (!strcmp(name, "printable")) op = MIME_MAGIC_PRINTABLE; + else if (!strcmp(name, "regex")) + op = MIME_MAGIC_REGEX; else if (!strcmp(name, "string")) op = MIME_MAGIC_STRING; else if (!strcmp(name, "istring")) @@ -524,6 +526,12 @@ mimeAddTypeRule(mime_type_t *mt, /* I - Type to add to */ if (temp->length > MIME_MAX_BUFFER) temp->length = MIME_MAX_BUFFER; break; + case MIME_MAGIC_REGEX : + temp->offset = strtol(value[0], NULL, 0); + temp->length = MIME_MAX_BUFFER; + if (regcomp(&(temp->value.rev), value[1], REG_NOSUB | REG_EXTENDED)) + return (-1); + break; case MIME_MAGIC_STRING : case MIME_MAGIC_ISTRING : temp->offset = strtol(value[0], NULL, 0); @@ -852,6 +860,49 @@ mime_check_rules( result = (n == 0); break; + case MIME_MAGIC_REGEX : + DEBUG_printf(("5mime_check_rules: regex(%d, \"%s\")", rules->offset, + rules->value.stringv)); + + /* + * Load the buffer if necessary... + */ + + if (fb->offset < 0 || rules->offset < fb->offset || + (rules->offset + rules->length) > (fb->offset + fb->length)) + { + /* + * Reload file buffer... + */ + + cupsFileSeek(fb->fp, rules->offset); + fb->length = cupsFileRead(fb->fp, (char *)fb->buffer, + sizeof(fb->buffer)); + fb->offset = rules->offset; + + DEBUG_printf(("5mime_check_rules: loaded %d byte fb->buffer at %d, starts " + "with \"%c%c%c%c\".", + fb->length, fb->offset, fb->buffer[0], fb->buffer[1], + fb->buffer[2], fb->buffer[3])); + } + + /* + * Compare the buffer against the string. If the file is too + * short then don't compare - it can't match... + */ + + { + char temp[MIME_MAX_BUFFER + 1]; + /* Temporary buffer */ + + memcpy(temp, fb->buffer, fb->length); + temp[fb->length] = '\0'; + result = !regexec(&(rules->value.rev), temp, 0, NULL, 0); + } + + DEBUG_printf(("5mime_check_rules: result=%d", result)); + break; + case MIME_MAGIC_STRING : DEBUG_printf(("5mime_check_rules: string(%d, \"%s\")", rules->offset, rules->value.stringv)); |