]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - scheduler/type.c
Fix another bug in the new logging code (STR #4687)
[thirdparty/cups.git] / scheduler / type.c
index ce7fbdaa994ff1942b74eb0ba0b5376c1b43fb74..5300d7e7d6981316fbb84946e05b6698047c2ea6 100644 (file)
@@ -1,26 +1,16 @@
 /*
  * "$Id$"
  *
- *   MIME typing routines for CUPS.
+ * MIME typing routines for CUPS.
  *
- *   Copyright 2007-2013 by Apple Inc.
- *   Copyright 1997-2006 by Easy Software Products, all rights reserved.
+ * Copyright 2007-2015 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/".
- *
- * Contents:
- *
- *   mimeAddType()        - Add a MIME type to a database.
- *   mimeAddTypeRule()    - Add a detection rule for a file type.
- *   mimeFileType()       - Determine the type of a file.
- *   mimeType()           - Lookup a file type.
- *   mime_compare_types() - Compare two MIME super/type names.
- *   mime_check_rules()   - Check each rule in a list.
- *   mime_patmatch()      - Pattern matching.
+ * 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/".
  */
 
 /*
@@ -319,7 +309,7 @@ mimeAddTypeRule(mime_type_t *mt,    /* I - Type to add to */
       */
 
       ptr = name;
-      while (isalnum(*rule & 255) && (ptr - name) < (sizeof(name) - 1))
+      while (isalnum(*rule & 255) && (size_t)(ptr - name) < (sizeof(name) - 1))
         *ptr++ = *rule++;
 
       *ptr = '\0';
@@ -332,12 +322,12 @@ mimeAddTypeRule(mime_type_t *mt,  /* I - Type to add to */
 
        rule ++;
        for (num_values = 0;
-            num_values < (sizeof(value) / sizeof(value[0]));
+            num_values < (int)(sizeof(value) / sizeof(value[0]));
             num_values ++)
        {
          ptr = value[num_values];
 
-         while ((ptr - value[num_values]) < (sizeof(value[0]) - 1) &&
+         while ((size_t)(ptr - value[num_values]) < (sizeof(value[0]) - 1) &&
                 *rule != '\0' && *rule != ',' && *rule != ')')
          {
            if (isspace(*rule & 255))
@@ -358,7 +348,7 @@ mimeAddTypeRule(mime_type_t *mt,    /* I - Type to add to */
              quote = *rule++;
 
              while (*rule != '\0' && *rule != quote &&
-                    (ptr - value[num_values]) < (sizeof(value[0]) - 1))
+                    (size_t)(ptr - value[num_values]) < (sizeof(value[0]) - 1))
                *ptr++ = *rule++;
 
               if (*rule == quote)
@@ -371,14 +361,14 @@ mimeAddTypeRule(mime_type_t *mt,  /* I - Type to add to */
              rule ++;
 
              while (*rule != '>' && *rule != '\0' &&
-                    (ptr - value[num_values]) < (sizeof(value[0]) - 1))
+                    (size_t)(ptr - value[num_values]) < (sizeof(value[0]) - 1))
              {
                if (isxdigit(rule[0] & 255) && isxdigit(rule[1] & 255))
                {
                  if (isdigit(*rule))
-                   *ptr = (*rule++ - '0') << 4;
+                   *ptr = (char)((*rule++ - '0') << 4);
                  else
-                   *ptr = (tolower(*rule++) - 'a' + 10) << 4;
+                   *ptr = (char)((tolower(*rule++) - 'a' + 10) << 4);
 
                  if (isdigit(*rule))
                    *ptr++ |= *rule++ - '0';
@@ -456,7 +446,7 @@ mimeAddTypeRule(mime_type_t *mt,    /* I - Type to add to */
        */
 
        snprintf(value[0], sizeof(value[0]), "*.%s", name);
-       length[0]  = strlen(value[0]);
+       length[0]  = (int)strlen(value[0]);
        op         = MIME_MAGIC_MATCH;
       }
 
@@ -467,7 +457,7 @@ mimeAddTypeRule(mime_type_t *mt,    /* I - Type to add to */
       if ((temp = calloc(1, sizeof(mime_magic_t))) == NULL)
        return (-1);
 
-      temp->invert = invert;
+      temp->invert = (short)invert;
       if (current != NULL)
       {
        temp->parent  = current->parent;
@@ -509,13 +499,13 @@ mimeAddTypeRule(mime_type_t *mt,  /* I - Type to add to */
       */
 
       current  = temp;
-      temp->op = op;
+      temp->op = (short)op;
       invert   = 0;
 
       switch (op)
       {
         case MIME_MAGIC_MATCH :
-           if (length[0] > (sizeof(temp->value.matchv) - 1))
+           if ((size_t)length[0] > (sizeof(temp->value.matchv) - 1))
              return (-1);
            strlcpy(temp->value.matchv, value[0], sizeof(temp->value.matchv));
            break;
@@ -535,15 +525,15 @@ mimeAddTypeRule(mime_type_t *mt,  /* I - Type to add to */
        case MIME_MAGIC_STRING :
        case MIME_MAGIC_ISTRING :
            temp->offset = strtol(value[0], NULL, 0);
-           if (length[1] > sizeof(temp->value.stringv))
+           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);
            if (length[1] == 1)
-             temp->value.charv = value[1][0];
+             temp->value.charv = (unsigned char)value[1][0];
            else
              temp->value.charv = (unsigned char)strtol(value[1], NULL, 0);
 
@@ -559,7 +549,7 @@ mimeAddTypeRule(mime_type_t *mt,    /* I - Type to add to */
            temp->value.intv = (unsigned)strtol(value[1], NULL, 0);
            break;
        case MIME_MAGIC_LOCALE :
-           if (length[0] > (sizeof(temp->value.localev) - 1))
+           if ((size_t)length[0] > (sizeof(temp->value.localev) - 1))
              return (-1);
 
            strlcpy(temp->value.localev, value[0], sizeof(temp->value.localev));
@@ -567,10 +557,10 @@ mimeAddTypeRule(mime_type_t *mt,  /* I - Type to add to */
        case MIME_MAGIC_CONTAINS :
            temp->offset = strtol(value[0], NULL, 0);
            temp->region = strtol(value[1], NULL, 0);
-           if (length[2] > sizeof(temp->value.stringv))
+           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;
       }
     }
@@ -623,8 +613,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)...
@@ -743,8 +748,8 @@ mime_check_rules(
   int          n;                      /* Looping var */
   int          region;                 /* Region to look at */
   int          logic,                  /* Logic to apply */
-               result,                 /* Result of test */
-               intv;                   /* Integer value */
+               result;                 /* Result of test */
+  unsigned     intv;                   /* Integer value */
   short                shortv;                 /* Short value */
   unsigned char        *bufptr;                /* Pointer into buffer */
 
@@ -790,6 +795,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));
          }
 
          /*
@@ -832,6 +839,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));
          }
 
          /*
@@ -880,6 +889,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],
@@ -896,7 +907,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);
           }
@@ -924,6 +935,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],
@@ -938,8 +951,7 @@ mime_check_rules(
          if ((rules->offset + rules->length) > (fb->offset + fb->length))
            result = 0;
          else
-            result = (memcmp(fb->buffer + rules->offset - fb->offset,
-                            rules->value.stringv, rules->length) == 0);
+            result = !memcmp(fb->buffer + rules->offset - fb->offset, rules->value.stringv, (size_t)rules->length);
           DEBUG_printf(("5mime_check_rules: result=%d", result));
          break;
 
@@ -959,6 +971,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));
          }
 
          /*
@@ -969,9 +983,7 @@ mime_check_rules(
          if ((rules->offset + rules->length) > (fb->offset + fb->length))
            result = 0;
          else
-            result = (_cups_strncasecmp((char *)fb->buffer + rules->offset -
-                                     fb->offset,
-                                 rules->value.stringv, rules->length) == 0);
+            result = !_cups_strncasecmp((char *)fb->buffer + rules->offset - fb->offset, rules->value.stringv, (size_t)rules->length);
          break;
 
       case MIME_MAGIC_CHAR :
@@ -989,6 +1001,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));
          }
 
         /*
@@ -1019,6 +1033,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));
          }
 
         /*
@@ -1031,7 +1047,7 @@ mime_check_rules(
          else
          {
            bufptr = fb->buffer + rules->offset - fb->offset;
-           shortv = (bufptr[0] << 8) | bufptr[1];
+           shortv = (short)((bufptr[0] << 8) | bufptr[1]);
            result = (shortv == rules->value.shortv);
          }
          break;
@@ -1052,6 +1068,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));
          }
 
         /*
@@ -1064,19 +1082,16 @@ mime_check_rules(
          else
          {
            bufptr = fb->buffer + rules->offset - fb->offset;
-           intv   = (((((bufptr[0] << 8) | bufptr[1]) << 8) |
-                      bufptr[2]) << 8) | bufptr[3];
+           intv   = (unsigned)((((((bufptr[0] << 8) | bufptr[1]) << 8) | bufptr[2]) << 8) | bufptr[3]);
            result = (intv == rules->value.intv);
          }
          break;
 
       case MIME_MAGIC_LOCALE :
 #if defined(WIN32) || defined(__EMX__) || defined(__APPLE__)
-          result = (strcmp(rules->value.localev,
-                          setlocale(LC_ALL, "")) == 0);
+          result = !strcmp(rules->value.localev, setlocale(LC_ALL, ""));
 #else
-          result = (strcmp(rules->value.localev,
-                          setlocale(LC_MESSAGES, "")) == 0);
+          result = !strcmp(rules->value.localev, setlocale(LC_MESSAGES, ""));
 #endif /* __APPLE__ */
          break;
 
@@ -1096,6 +1111,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));
          }
 
          /*
@@ -1113,9 +1130,7 @@ mime_check_rules(
              region = fb->length - rules->length;
 
            for (n = 0; n < region; n ++)
-             if ((result = (memcmp(fb->buffer + rules->offset - fb->offset + n,
-                                   rules->value.stringv,
-                                   rules->length) == 0)) != 0)
+             if ((result = (memcmp(fb->buffer + rules->offset - fb->offset + n, rules->value.stringv, (size_t)rules->length) == 0)) != 0)
                break;
           }
          break;