]> git.ipfire.org Git - thirdparty/cups.git/commitdiff
<rdar://problem/11131245> PDF detection in cups needs to be tightened up
authormsweet <msweet@a1ca3aef-8c08-0410-bb20-df032aa958be>
Tue, 10 Sep 2013 19:15:36 +0000 (19:15 +0000)
committermsweet <msweet@a1ca3aef-8c08-0410-bb20-df032aa958be>
Tue, 10 Sep 2013 19:15:36 +0000 (19:15 +0000)
Add regex() rule support.

git-svn-id: svn+ssh://src.apple.com/svn/cups/cups.org/trunk@11272 a1ca3aef-8c08-0410-bb20-df032aa958be

CHANGES.txt
conf/mime.types
scheduler/mime.c
scheduler/mime.h
scheduler/type.c

index cde9d92a4b60599d4294a034d3124eea2f82b9a6..e3c8a1b22e9d3d6c1c5ca2b897f32055b24caa3a 100644 (file)
@@ -1,5 +1,7 @@
-CHANGES.txt - 2.0b1 - 2013-08-06
+CHANGES.txt - 2.0b1 - 2013-09-10
 --------------------------------
 
 CHANGES IN CUPS V2.0b1
 
+       - Added support for regular expression matching in the MIME type rules
+         (<rdar://problem/11131245>)
index 217d75bee95af42a3b4ca45fa2c86d2bb0994653..3e07408b9cb28d43df71317a32f02b759b47a809 100644 (file)
@@ -7,7 +7,7 @@
 #   VERSIONS OF CUPS.  Instead, create a "local.types" file that
 #   reflects your local configuration changes.
 #
-#   Copyright 2007-2011 by Apple Inc.
+#   Copyright 2007-2013 by Apple Inc.
 #   Copyright 1997-2007 by Easy Software Products.
 #
 #   These coded instructions, statements, and computer programs are the
@@ -37,6 +37,7 @@
 #                                       100=default, 200=highest)
 #   printable(offset,length)           True if bytes are printable 8-bit chars
 #                                      (CR, NL, TAB, BS, 32-126, 128-254)
+#   regex(offset,"regex")              True if bytes match regular expression
 #   string(offset,"string")            True if bytes are identical to string
 #   istring(offset,"string")           True if bytes are identical to
 #                                       case-insensitive string
@@ -70,7 +71,7 @@
 #
 
 #application/msword            doc string(0,<D0CF11E0A1B11AE1>)
-application/pdf                        pdf string(0,%PDF)
+application/pdf                        pdf regex(0,^[\\n\\r]*%PDF)
 application/postscript         ai eps ps string(0,%!) string(0,<04>%!) \
                                contains(0,128,<1B>%-12345X) + \
                                (contains(0,4096,"LANGUAGE=POSTSCRIPT") \
index de3821c961154c7d0b2716aa0b5667d086fa97a1..60272cf8add1b8541bbe07d3c30b46b391e0f3ad 100644 (file)
@@ -3,7 +3,7 @@
  *
  *   MIME database file 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
index 6feceecc4ad3d455bca0e9aa6f37b091c54dba4a..3d61d2e1e2a6ee4e181d5f560f8326ab84ad6000 100644 (file)
@@ -3,7 +3,7 @@
  *
  *   MIME type/conversion database definitions for CUPS.
  *
- *   Copyright 2007-2011 by Apple Inc.
+ *   Copyright 2007-2013 by Apple Inc.
  *   Copyright 1997-2007 by Easy Software Products, all rights reserved.
  *
  *   These coded instructions, statements, and computer programs are the
@@ -19,6 +19,7 @@
 #  include <cups/array.h>
 #  include <cups/ipp.h>
 #  include <cups/file.h>
+#  include <regex.h>
 
 
 /*
@@ -58,7 +59,8 @@ typedef enum
   MIME_MAGIC_INT,                      /* Integer/32-bit word matches */
   MIME_MAGIC_LOCALE,                   /* Current locale matches string */
   MIME_MAGIC_CONTAINS,                 /* File contains a string */
-  MIME_MAGIC_ISTRING                   /* Case-insensitive string matches */
+  MIME_MAGIC_ISTRING,                  /* Case-insensitive string matches */
+  MIME_MAGIC_REGEX                     /* Regular expression matches */
 } mime_op_t;
 
 typedef struct _mime_magic_s           /**** MIME Magic Data ****/
@@ -80,6 +82,7 @@ typedef struct _mime_magic_s          /**** MIME Magic Data ****/
     unsigned char charv;               /* Byte value */
     unsigned short shortv;             /* Short value */
     unsigned   intv;                   /* Integer value */
+    regex_t    rev;                    /* Regular expression value */
   }            value;
 } mime_magic_t;
 
index 7b8743882f782f7029ccf83a66efb93e0170f0e4..c466a257fa8f5be06042ae93311b52b4aa3170fa 100644 (file)
@@ -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));