]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/type.c
Merge pull request #5410 from AOSC-Dev/master
[thirdparty/cups.git] / scheduler / type.c
index 8357197f3702cb8bb007088edab63e0e336816d0..2cceeda58c8ed43dd6abc6c337aa0dc457aaf2e2 100644 (file)
@@ -1,16 +1,10 @@
 /*
- * "$Id$"
- *
  * MIME typing routines for CUPS.
  *
- * Copyright 2007-2014 by Apple Inc.
+ * Copyright 2007-2016 by Apple Inc.
  * Copyright 1997-2006 by Easy Software Products, all rights reserved.
  *
- * These coded instructions, statements, and computer programs are the
- * property of Apple Inc. and are protected by Federal copyright
- * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- * which should have been included with this file.  If this file is
- * file is missing or damaged, see the license at "http://www.cups.org/".
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
  */
 
 /*
  */
 
 #include <cups/string-private.h>
-#include <cups/debug-private.h>
 #include <locale.h>
 #include "mime.h"
 
 
+/*
+ * Debug macros that used to be private API...
+ */
+
+#define DEBUG_puts(x)
+#define DEBUG_printf(...)
+
+
 /*
  * Local types...
  */
@@ -50,7 +51,7 @@ static int    mime_patmatch(const char *s, const char *pat);
  * Local globals...
  */
 
-#ifdef DEBUG
+#ifdef MIME_DEBUG
 static const char * const debug_ops[] =
                {                       /* Test names... */
                  "NOP",                /* No operation */
@@ -65,7 +66,8 @@ static const char * const debug_ops[] =
                  "INT",                /* Integer/32-bit word matches */
                  "LOCALE",             /* Current locale matches string */
                  "CONTAINS",           /* File contains a string */
-                 "ISTRING"             /* Case-insensitive string matches */
+                 "ISTRING",            /* Case-insensitive string matches */
+                 "REGEX"               /* Regular expression matches */
                };
 #endif /* DEBUG */
 
@@ -528,7 +530,7 @@ mimeAddTypeRule(mime_type_t *mt,    /* I - Type to add to */
            if ((size_t)length[1] > sizeof(temp->value.stringv))
              return (-1);
            temp->length = length[1];
-           memcpy(temp->value.stringv, value[1], length[1]);
+           memcpy(temp->value.stringv, value[1], (size_t)length[1]);
            break;
        case MIME_MAGIC_CHAR :
            temp->offset = strtol(value[0], NULL, 0);
@@ -560,7 +562,7 @@ mimeAddTypeRule(mime_type_t *mt,    /* I - Type to add to */
            if ((size_t)length[2] > sizeof(temp->value.stringv))
              return (-1);
            temp->length = length[2];
-           memcpy(temp->value.stringv, value[2], length[2]);
+           memcpy(temp->value.stringv, value[2], (size_t)length[2]);
            break;
       }
     }
@@ -613,8 +615,23 @@ mimeFileType(mime_t     *mime,             /* I - MIME database */
     return (NULL);
   }
 
-  fb.offset = -1;
-  fb.length = 0;
+ /*
+  * Then preload the first MIME_MAX_BUFFER bytes of the file into the file
+  * buffer, returning an error if we can't read anything...
+  */
+
+  fb.offset = 0;
+  fb.length = (int)cupsFileRead(fb.fp, (char *)fb.buffer, MIME_MAX_BUFFER);
+
+  if (fb.length <= 0)
+  {
+    DEBUG_printf(("1mimeFileType: Unable to read from \"%s\": %s", pathname, strerror(errno)));
+    DEBUG_puts("1mimeFileType: Returning NULL.");
+
+    cupsFileClose(fb.fp);
+
+    return (NULL);
+  }
 
  /*
   * Figure out the base filename (without directory portion)...
@@ -780,6 +797,8 @@ mime_check_rules(
            fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
                                      sizeof(fb->buffer));
            fb->offset = rules->offset;
+
+           DEBUG_printf(("4mime_check_rules: MIME_MAGIC_ASCII fb->length=%d", fb->length));
          }
 
          /*
@@ -822,6 +841,8 @@ mime_check_rules(
            fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
                                      sizeof(fb->buffer));
            fb->offset = rules->offset;
+
+           DEBUG_printf(("4mime_check_rules: MIME_MAGIC_PRINTABLE fb->length=%d", fb->length));
          }
 
          /*
@@ -870,6 +891,8 @@ mime_check_rules(
                                      sizeof(fb->buffer));
            fb->offset = rules->offset;
 
+           DEBUG_printf(("4mime_check_rules: MIME_MAGIC_REGEX fb->length=%d", fb->length));
+
             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],
@@ -886,7 +909,7 @@ mime_check_rules(
             char temp[MIME_MAX_BUFFER + 1];
                                        /* Temporary buffer */
 
-            memcpy(temp, fb->buffer, fb->length);
+            memcpy(temp, fb->buffer, (size_t)fb->length);
             temp[fb->length] = '\0';
             result = !regexec(&(rules->value.rev), temp, 0, NULL, 0);
           }
@@ -914,6 +937,8 @@ mime_check_rules(
                                      sizeof(fb->buffer));
            fb->offset = rules->offset;
 
+           DEBUG_printf(("4mime_check_rules: MIME_MAGIC_STRING fb->length=%d", fb->length));
+
             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],
@@ -948,6 +973,8 @@ mime_check_rules(
            fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
                                      sizeof(fb->buffer));
            fb->offset = rules->offset;
+
+           DEBUG_printf(("4mime_check_rules: MIME_MAGIC_ISTRING fb->length=%d", fb->length));
          }
 
          /*
@@ -976,6 +1003,8 @@ mime_check_rules(
            fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
                                      sizeof(fb->buffer));
            fb->offset = rules->offset;
+
+           DEBUG_printf(("4mime_check_rules: MIME_MAGIC_CHAR fb->length=%d", fb->length));
          }
 
         /*
@@ -1006,6 +1035,8 @@ mime_check_rules(
            fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
                                      sizeof(fb->buffer));
            fb->offset = rules->offset;
+
+           DEBUG_printf(("4mime_check_rules: MIME_MAGIC_SHORT fb->length=%d", fb->length));
          }
 
         /*
@@ -1039,6 +1070,8 @@ mime_check_rules(
            fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
                                      sizeof(fb->buffer));
            fb->offset = rules->offset;
+
+           DEBUG_printf(("4mime_check_rules: MIME_MAGIC_INT fb->length=%d", fb->length));
          }
 
         /*
@@ -1057,7 +1090,7 @@ mime_check_rules(
          break;
 
       case MIME_MAGIC_LOCALE :
-#if defined(WIN32) || defined(__EMX__) || defined(__APPLE__)
+#if defined(_WIN32) || defined(__EMX__) || defined(__APPLE__)
           result = !strcmp(rules->value.localev, setlocale(LC_ALL, ""));
 #else
           result = !strcmp(rules->value.localev, setlocale(LC_MESSAGES, ""));
@@ -1080,6 +1113,8 @@ mime_check_rules(
            fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
                                      sizeof(fb->buffer));
            fb->offset = rules->offset;
+
+           DEBUG_printf(("4mime_check_rules: MIME_MAGIC_CONTAINS fb->length=%d", fb->length));
          }
 
          /*
@@ -1243,8 +1278,3 @@ mime_patmatch(const char *s,              /* I - String to match against */
 
   return (*s == *pat);
 }
-
-
-/*
- * End of "$Id$".
- */