]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/file.c
Changes to eliminate warnings from new Clang.
[thirdparty/cups.git] / cups / file.c
index 9b1f592a9da4c966e75b25f0d17cfd6d24e3a0eb..7814dba996f820e584a7e518e06b1fd9b80dafc7 100644 (file)
@@ -8,7 +8,7 @@
  * our own file functions allows us to provide transparent support of
  * gzip'd print files, PPD files, etc.
  *
- * Copyright 2007-2013 by Apple Inc.
+ * Copyright 2007-2015 by Apple Inc.
  * Copyright 1997-2007 by Easy Software Products, all rights reserved.
  *
  * These coded instructions, statements, and computer programs are the
@@ -16,6 +16,8 @@
  * 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/".
+ *
+ * This file is subject to the Apple OS-Developed Software exception.
  */
 
 /*
@@ -324,7 +326,7 @@ cupsFileClose(cups_file_t *fp)              /* I - CUPS file */
   int  is_stdio;                       /* Is a stdio file? */
 
 
-  DEBUG_printf(("cupsFileClose(fp=%p)", fp));
+  DEBUG_printf(("cupsFileClose(fp=%p)", (void *)fp));
 
  /*
   * Range check...
@@ -370,7 +372,7 @@ cupsFileClose(cups_file_t *fp)              /* I - CUPS file */
         if (fp->stream.next_out > fp->cbuf)
        {
          if (cups_write(fp, (char *)fp->cbuf,
-                        fp->stream.next_out - fp->cbuf) < 0)
+                        (size_t)(fp->stream.next_out - fp->cbuf)) < 0)
            status = -1;
 
          fp->stream.next_out  = fp->cbuf;
@@ -388,14 +390,14 @@ cupsFileClose(cups_file_t *fp)            /* I - CUPS file */
       * Write the CRC and length...
       */
 
-      trailer[0] = fp->crc;
-      trailer[1] = fp->crc >> 8;
-      trailer[2] = fp->crc >> 16;
-      trailer[3] = fp->crc >> 24;
-      trailer[4] = fp->pos;
-      trailer[5] = fp->pos >> 8;
-      trailer[6] = fp->pos >> 16;
-      trailer[7] = fp->pos >> 24;
+      trailer[0] = (unsigned char)fp->crc;
+      trailer[1] = (unsigned char)(fp->crc >> 8);
+      trailer[2] = (unsigned char)(fp->crc >> 16);
+      trailer[3] = (unsigned char)(fp->crc >> 24);
+      trailer[4] = (unsigned char)fp->pos;
+      trailer[5] = (unsigned char)(fp->pos >> 8);
+      trailer[6] = (unsigned char)(fp->pos >> 16);
+      trailer[7] = (unsigned char)(fp->pos >> 24);
 
       if (cups_write(fp, (char *)trailer, 8) < 0)
         status = -1;
@@ -494,9 +496,7 @@ cupsFileFind(const char *filename,  /* I - File to find */
   * Range check input...
   */
 
-  DEBUG_printf(("cupsFileFind(filename=\"%s\", path=\"%s\", executable=%d, "
-                "buffer=%p, bufsize=%d)", filename, path, executable, buffer,
-               bufsize));
+  DEBUG_printf(("cupsFileFind(filename=\"%s\", path=\"%s\", executable=%d, buffer=%p, bufsize=%d)", filename, path, executable, (void *)buffer, bufsize));
 
   if (!filename || !buffer || bufsize < 2)
     return (NULL);
@@ -509,7 +509,7 @@ cupsFileFind(const char *filename,  /* I - File to find */
 
     if (!access(filename, 0))
     {
-      strlcpy(buffer, filename, bufsize);
+      strlcpy(buffer, filename, (size_t)bufsize);
       return (buffer);
     }
     else
@@ -534,7 +534,7 @@ cupsFileFind(const char *filename,  /* I - File to find */
       if (bufptr > buffer && bufptr[-1] != '/' && bufptr < bufend)
         *bufptr++ = '/';
 
-      strlcpy(bufptr, filename, bufend - bufptr);
+      strlcpy(bufptr, filename, (size_t)(bufend - bufptr));
 
 #ifdef WIN32
       if (!access(buffer, 0))
@@ -561,7 +561,7 @@ cupsFileFind(const char *filename,  /* I - File to find */
   if (bufptr > buffer && bufptr[-1] != '/' && bufptr < bufend)
     *bufptr++ = '/';
 
-  strlcpy(bufptr, filename, bufend - bufptr);
+  strlcpy(bufptr, filename, (size_t)(bufend - bufptr));
 
   if (!access(buffer, 0))
   {
@@ -588,7 +588,7 @@ cupsFileFlush(cups_file_t *fp)              /* I - CUPS file */
   ssize_t      bytes;                  /* Bytes to write */
 
 
-  DEBUG_printf(("cupsFileFlush(fp=%p)", fp));
+  DEBUG_printf(("cupsFileFlush(fp=%p)", (void *)fp));
 
  /*
   * Range check input...
@@ -609,10 +609,10 @@ cupsFileFlush(cups_file_t *fp)            /* I - CUPS file */
   {
 #ifdef HAVE_LIBZ
     if (fp->compressed)
-      bytes = cups_compress(fp, fp->buf, bytes);
+      bytes = cups_compress(fp, fp->buf, (size_t)bytes);
     else
 #endif /* HAVE_LIBZ */
-      bytes = cups_write(fp, fp->buf, bytes);
+      bytes = cups_write(fp, fp->buf, (size_t)bytes);
 
     if (bytes < 0)
       return (-1);
@@ -637,6 +637,8 @@ cupsFileGetChar(cups_file_t *fp)    /* I - CUPS file */
   * Range check input...
   */
 
+  DEBUG_printf(("4cupsFileGetChar(fp=%p)", (void *)fp));
+
   if (!fp || (fp->mode != 'r' && fp->mode != 's'))
   {
     DEBUG_puts("5cupsFileGetChar: Bad arguments!");
@@ -647,8 +649,10 @@ cupsFileGetChar(cups_file_t *fp)   /* I - CUPS file */
   * If the input buffer is empty, try to read more data...
   */
 
+  DEBUG_printf(("5cupsFileGetChar: fp->eof=%d, fp->ptr=%p, fp->end=%p", fp->eof, (void *)fp->ptr, (void *)fp->end));
+
   if (fp->ptr >= fp->end)
-    if (cups_fill(fp) < 0)
+    if (cups_fill(fp) <= 0)
     {
       DEBUG_puts("5cupsFileGetChar: Unable to fill buffer!");
       return (-1);
@@ -689,8 +693,7 @@ cupsFileGetConf(cups_file_t *fp,    /* I  - CUPS file */
   */
 
   DEBUG_printf(("2cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT
-                ", value=%p, linenum=%p)", fp, buf, CUPS_LLCAST buflen,
-               value, linenum));
+                ", value=%p, linenum=%p)", (void *)fp, (void *)buf, CUPS_LLCAST buflen, (void *)value, (void *)linenum));
 
   if (!fp || (fp->mode != 'r' && fp->mode != 's') ||
       !buf || buflen < 2 || !value)
@@ -832,8 +835,7 @@ cupsFileGetLine(cups_file_t *fp,    /* I - File to read from */
   * Range check input...
   */
 
-  DEBUG_printf(("2cupsFileGetLine(fp=%p, buf=%p, buflen=" CUPS_LLFMT ")",
-                fp, buf, CUPS_LLCAST buflen));
+  DEBUG_printf(("2cupsFileGetLine(fp=%p, buf=%p, buflen=" CUPS_LLFMT ")", (void *)fp, (void *)buf, CUPS_LLCAST buflen));
 
   if (!fp || (fp->mode != 'r' && fp->mode != 's') || !buf || buflen < 3)
     return (0);
@@ -883,7 +885,7 @@ cupsFileGetLine(cups_file_t *fp,    /* I - File to read from */
 
   DEBUG_printf(("4cupsFileGetLine: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
 
-  return (ptr - buf);
+  return ((size_t)(ptr - buf));
 }
 
 
@@ -907,8 +909,7 @@ cupsFileGets(cups_file_t *fp,               /* I - CUPS file */
   * Range check input...
   */
 
-  DEBUG_printf(("2cupsFileGets(fp=%p, buf=%p, buflen=" CUPS_LLFMT ")", fp, buf,
-                CUPS_LLCAST buflen));
+  DEBUG_printf(("2cupsFileGets(fp=%p, buf=%p, buflen=" CUPS_LLFMT ")", (void *)fp, (void *)buf, CUPS_LLCAST buflen));
 
   if (!fp || (fp->mode != 'r' && fp->mode != 's') || !buf || buflen < 2)
     return (NULL);
@@ -958,7 +959,7 @@ cupsFileGets(cups_file_t *fp,               /* I - CUPS file */
       break;
     }
     else
-      *ptr++ = ch;
+      *ptr++ = (char)ch;
   }
 
   *ptr = '\0';
@@ -1213,10 +1214,10 @@ cupsFileOpenFd(int        fd,           /* I - File descriptor */
          header[1] = 0x8b;
          header[2] = Z_DEFLATED;
          header[3] = 0;
-         header[4] = curtime;
-         header[5] = curtime >> 8;
-         header[6] = curtime >> 16;
-         header[7] = curtime >> 24;
+         header[4] = (unsigned char)curtime;
+         header[5] = (unsigned char)(curtime >> 8);
+         header[6] = (unsigned char)(curtime >> 16);
+         header[7] = (unsigned char)(curtime >> 24);
          header[8] = 0;
          header[9] = 0x03;
 
@@ -1282,7 +1283,7 @@ cupsFilePeekChar(cups_file_t *fp) /* I - CUPS file */
   */
 
   if (fp->ptr >= fp->end)
-    if (cups_fill(fp) < 0)
+    if (cups_fill(fp) <= 0)
       return (-1);
 
  /*
@@ -1308,7 +1309,7 @@ cupsFilePrintf(cups_file_t *fp,           /* I - CUPS file */
   ssize_t      bytes;                  /* Formatted size */
 
 
-  DEBUG_printf(("2cupsFilePrintf(fp=%p, format=\"%s\", ...)", fp, format));
+  DEBUG_printf(("2cupsFilePrintf(fp=%p, format=\"%s\", ...)", (void *)fp, format));
 
   if (!fp || !format || (fp->mode != 'w' && fp->mode != 's'))
     return (-1);
@@ -1341,11 +1342,11 @@ cupsFilePrintf(cups_file_t *fp,         /* I - CUPS file */
     if (bytes > 65535)
       return (-1);
 
-    if ((temp = realloc(fp->printf_buffer, bytes + 1)) == NULL)
+    if ((temp = realloc(fp->printf_buffer, (size_t)(bytes + 1))) == NULL)
       return (-1);
 
     fp->printf_buffer = temp;
-    fp->printf_size   = bytes + 1;
+    fp->printf_size   = (size_t)(bytes + 1);
 
     va_start(ap, format);
     bytes = vsnprintf(fp->printf_buffer, fp->printf_size, format, ap);
@@ -1354,14 +1355,14 @@ cupsFilePrintf(cups_file_t *fp,         /* I - CUPS file */
 
   if (fp->mode == 's')
   {
-    if (cups_write(fp, fp->printf_buffer, bytes) < 0)
+    if (cups_write(fp, fp->printf_buffer, (size_t)bytes) < 0)
       return (-1);
 
     fp->pos += bytes;
 
     DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
 
-    return (bytes);
+    return ((int)bytes);
   }
 
   if ((fp->ptr + bytes) > fp->end)
@@ -1372,20 +1373,20 @@ cupsFilePrintf(cups_file_t *fp,         /* I - CUPS file */
 
   DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
 
-  if (bytes > sizeof(fp->buf))
+  if ((size_t)bytes > sizeof(fp->buf))
   {
 #ifdef HAVE_LIBZ
     if (fp->compressed)
-      return (cups_compress(fp, fp->printf_buffer, bytes));
+      return ((int)cups_compress(fp, fp->printf_buffer, (size_t)bytes));
     else
 #endif /* HAVE_LIBZ */
-      return (cups_write(fp, fp->printf_buffer, bytes));
+      return ((int)cups_write(fp, fp->printf_buffer, (size_t)bytes));
   }
   else
   {
-    memcpy(fp->ptr, fp->printf_buffer, bytes);
+    memcpy(fp->ptr, fp->printf_buffer, (size_t)bytes);
     fp->ptr += bytes;
-    return (bytes);
+    return ((int)bytes);
   }
 }
 
@@ -1416,7 +1417,7 @@ cupsFilePutChar(cups_file_t *fp,  /* I - CUPS file */
     char ch;                           /* Output character */
 
 
-    ch = c;
+    ch = (char)c;
 
     if (send(fp->fd, &ch, 1, 0) < 1)
       return (-1);
@@ -1431,7 +1432,7 @@ cupsFilePutChar(cups_file_t *fp,  /* I - CUPS file */
       if (cupsFileFlush(fp))
        return (-1);
 
-    *(fp->ptr) ++ = c;
+    *(fp->ptr) ++ = (char)c;
   }
 
   fp->pos ++;
@@ -1478,7 +1479,7 @@ cupsFilePutConf(cups_file_t *fp,  /* I - CUPS file */
       * Need to quote the first # in the info string...
       */
 
-      if ((temp = cupsFileWrite(fp, value, ptr - value)) < 0)
+      if ((temp = cupsFileWrite(fp, value, (size_t)(ptr - value))) < 0)
         return (-1);
       bytes += temp;
 
@@ -1529,18 +1530,18 @@ cupsFilePuts(cups_file_t *fp,           /* I - CUPS file */
   * Write the string...
   */
 
-  bytes = (int)strlen(s);
+  bytes = (ssize_t)strlen(s);
 
   if (fp->mode == 's')
   {
-    if (cups_write(fp, s, bytes) < 0)
+    if (cups_write(fp, s, (size_t)bytes) < 0)
       return (-1);
 
     fp->pos += bytes;
 
     DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
 
-    return (bytes);
+    return ((int)bytes);
   }
 
   if ((fp->ptr + bytes) > fp->end)
@@ -1551,20 +1552,20 @@ cupsFilePuts(cups_file_t *fp,           /* I - CUPS file */
 
   DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
 
-  if (bytes > sizeof(fp->buf))
+  if ((size_t)bytes > sizeof(fp->buf))
   {
 #ifdef HAVE_LIBZ
     if (fp->compressed)
-      return (cups_compress(fp, s, bytes));
+      return ((int)cups_compress(fp, s, (size_t)bytes));
     else
 #endif /* HAVE_LIBZ */
-      return (cups_write(fp, s, bytes));
+      return ((int)cups_write(fp, s, (size_t)bytes));
   }
   else
   {
-    memcpy(fp->ptr, s, bytes);
+    memcpy(fp->ptr, s, (size_t)bytes);
     fp->ptr += bytes;
-    return (bytes);
+    return ((int)bytes);
   }
 }
 
@@ -1584,8 +1585,7 @@ cupsFileRead(cups_file_t *fp,             /* I - CUPS file */
   ssize_t      count;                  /* Bytes read */
 
 
-  DEBUG_printf(("2cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")", fp, buf,
-                CUPS_LLCAST bytes));
+  DEBUG_printf(("2cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")", (void *)fp, (void *)buf, CUPS_LLCAST bytes));
 
  /*
   * Range check input...
@@ -1620,7 +1620,7 @@ cupsFileRead(cups_file_t *fp,             /* I - CUPS file */
     if (count > (ssize_t)bytes)
       count = (ssize_t)bytes;
 
-    memcpy(buf, fp->ptr, count);
+    memcpy(buf, fp->ptr,(size_t) count);
     fp->ptr += count;
     fp->pos += count;
 
@@ -1630,8 +1630,8 @@ cupsFileRead(cups_file_t *fp,             /* I - CUPS file */
     * Update the counts for the last read...
     */
 
-    bytes -= count;
-    total += count;
+    bytes -= (size_t)count;
+    total += (size_t)count;
     buf   += count;
   }
 
@@ -1659,7 +1659,7 @@ cupsFileRewind(cups_file_t *fp)           /* I - CUPS file */
   * Range check input...
   */
 
-  DEBUG_printf(("cupsFileRewind(fp=%p)", fp));
+  DEBUG_printf(("cupsFileRewind(fp=%p)", (void *)fp));
   DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
 
   if (!fp || fp->mode != 'r')
@@ -1731,10 +1731,9 @@ cupsFileSeek(cups_file_t *fp,            /* I - CUPS file */
   ssize_t      bytes;                  /* Number bytes in buffer */
 
 
-  DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT ")", fp,
-                CUPS_LLCAST pos));
+  DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT ")", (void *)fp, CUPS_LLCAST pos));
   DEBUG_printf(("2cupsFileSeek: fp->pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
-  DEBUG_printf(("2cupsFileSeek: fp->ptr=%p, fp->end=%p", fp->ptr, fp->end));
+  DEBUG_printf(("2cupsFileSeek: fp->ptr=%p, fp->end=%p", (void *)fp->ptr, (void *)fp->end));
 
  /*
   * Range check input...
@@ -1777,7 +1776,7 @@ cupsFileSeek(cups_file_t *fp,             /* I - CUPS file */
     * Preload a buffer to determine whether the file is compressed...
     */
 
-    if (cups_fill(fp) < 0)
+    if (cups_fill(fp) <= 0)
       return (-1);
   }
 #endif /* HAVE_LIBZ */
@@ -1982,9 +1981,8 @@ cupsFileStdout(void)
 off_t                                  /* O - File position */
 cupsFileTell(cups_file_t *fp)          /* I - CUPS file */
 {
-  DEBUG_printf(("2cupsFileTell(fp=%p)", fp));
-  DEBUG_printf(("3cupsFileTell: pos=" CUPS_LLFMT,
-               CUPS_LLCAST (fp ? fp->pos : -1)));
+  DEBUG_printf(("2cupsFileTell(fp=%p)", (void *)fp));
+  DEBUG_printf(("3cupsFileTell: pos=" CUPS_LLFMT, CUPS_LLCAST (fp ? fp->pos : -1)));
 
   return (fp ? fp->pos : 0);
 }
@@ -2003,7 +2001,7 @@ cupsFileUnlock(cups_file_t *fp)           /* I - CUPS file */
   * Range check...
   */
 
-  DEBUG_printf(("cupsFileUnlock(fp=%p)", fp));
+  DEBUG_printf(("cupsFileUnlock(fp=%p)", (void *)fp));
 
   if (!fp || fp->mode == 's')
     return (-1);
@@ -2035,8 +2033,7 @@ cupsFileWrite(cups_file_t *fp,            /* I - CUPS file */
   * Range check input...
   */
 
-  DEBUG_printf(("2cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")",
-                fp, buf, CUPS_LLCAST bytes));
+  DEBUG_printf(("2cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")", (void *)fp, (void *)buf, CUPS_LLCAST bytes));
 
   if (!fp || !buf || (fp->mode != 'w' && fp->mode != 's'))
     return (-1);
@@ -2096,21 +2093,20 @@ cups_compress(cups_file_t *fp,          /* I - CUPS file */
               const char  *buf,                /* I - Buffer */
              size_t      bytes)        /* I - Number bytes */
 {
-  DEBUG_printf(("7cups_compress(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")", fp, buf,
-                CUPS_LLCAST bytes));
+  DEBUG_printf(("7cups_compress(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")", (void *)fp, (void *)buf, CUPS_LLCAST bytes));
 
  /*
   * Update the CRC...
   */
 
-  fp->crc = crc32(fp->crc, (const Bytef *)buf, bytes);
+  fp->crc = crc32(fp->crc, (const Bytef *)buf, (uInt)bytes);
 
  /*
   * Deflate the bytes...
   */
 
   fp->stream.next_in  = (Bytef *)buf;
-  fp->stream.avail_in = bytes;
+  fp->stream.avail_in = (uInt)bytes;
 
   while (fp->stream.avail_in > 0)
   {
@@ -2121,9 +2117,9 @@ cups_compress(cups_file_t *fp,            /* I - CUPS file */
     DEBUG_printf(("9cups_compress: avail_in=%d, avail_out=%d",
                   fp->stream.avail_in, fp->stream.avail_out));
 
-    if (fp->stream.avail_out < (int)(sizeof(fp->cbuf) / 8))
+    if (fp->stream.avail_out < (uInt)(sizeof(fp->cbuf) / 8))
     {
-      if (cups_write(fp, (char *)fp->cbuf, fp->stream.next_out - fp->cbuf) < 0)
+      if (cups_write(fp, (char *)fp->cbuf, (size_t)(fp->stream.next_out - fp->cbuf)) < 0)
         return (-1);
 
       fp->stream.next_out  = fp->cbuf;
@@ -2133,7 +2129,7 @@ cups_compress(cups_file_t *fp,            /* I - CUPS file */
     deflate(&(fp->stream), Z_NO_FLUSH);
   }
 
-  return (bytes);
+  return ((ssize_t)bytes);
 }
 #endif /* HAVE_LIBZ */
 
@@ -2153,10 +2149,8 @@ cups_fill(cups_file_t *fp)               /* I - CUPS file */
 #endif /* HAVE_LIBZ */
 
 
-  DEBUG_printf(("7cups_fill(fp=%p)", fp));
-  DEBUG_printf(("9cups_fill: fp->ptr=%p, fp->end=%p, fp->buf=%p, "
-                "fp->bufpos=" CUPS_LLFMT ", fp->eof=%d",
-                fp->ptr, fp->end, fp->buf, CUPS_LLCAST fp->bufpos, fp->eof));
+  DEBUG_printf(("7cups_fill(fp=%p)", (void *)fp));
+  DEBUG_printf(("9cups_fill: fp->ptr=%p, fp->end=%p, fp->buf=%p, fp->bufpos=" CUPS_LLFMT ", fp->eof=%d", (void *)fp->ptr, (void *)fp->end, (void *)fp->buf, CUPS_LLCAST fp->bufpos, fp->eof));
 
   if (fp->ptr && fp->end)
     fp->bufpos += fp->end - fp->buf;
@@ -2193,6 +2187,8 @@ cups_fill(cups_file_t *fp)                /* I - CUPS file */
         DEBUG_printf(("9cups_fill: cups_read() returned " CUPS_LLFMT,
                      CUPS_LLCAST bytes));
 
+        fp->eof = 1;
+
        return (-1);
       }
 
@@ -2232,6 +2228,11 @@ cups_fill(cups_file_t *fp)               /* I - CUPS file */
          * Can't read from file!
          */
 
+         DEBUG_puts("9cups_fill: Extra gzip header data missing, returning -1.");
+
+          fp->eof = 1;
+         errno   = EIO;
+
          return (-1);
        }
 
@@ -2244,6 +2245,11 @@ cups_fill(cups_file_t *fp)               /* I - CUPS file */
          * Can't read from file!
          */
 
+         DEBUG_puts("9cups_fill: Extra gzip header data does not fit in initial buffer, returning -1.");
+
+          fp->eof = 1;
+         errno   = EIO;
+
          return (-1);
        }
       }
@@ -2265,6 +2271,11 @@ cups_fill(cups_file_t *fp)               /* I - CUPS file */
          * Can't read from file!
          */
 
+         DEBUG_puts("9cups_fill: Original filename in gzip header data does not fit in initial buffer, returning -1.");
+
+          fp->eof = 1;
+         errno   = EIO;
+
          return (-1);
        }
       }
@@ -2286,6 +2297,11 @@ cups_fill(cups_file_t *fp)               /* I - CUPS file */
          * Can't read from file!
          */
 
+         DEBUG_puts("9cups_fill: Comment in gzip header data does not fit in initial buffer, returning -1.");
+
+          fp->eof = 1;
+         errno   = EIO;
+
          return (-1);
        }
       }
@@ -2304,6 +2320,11 @@ cups_fill(cups_file_t *fp)               /* I - CUPS file */
          * Can't read from file!
          */
 
+         DEBUG_puts("9cups_fill: Header CRC in gzip header data does not fit in initial buffer, returning -1.");
+
+          fp->eof = 1;
+         errno   = EIO;
+
          return (-1);
        }
       }
@@ -2313,7 +2334,7 @@ cups_fill(cups_file_t *fp)                /* I - CUPS file */
       */
 
       if ((bytes = end - ptr) > 0)
-        memcpy(fp->cbuf, ptr, bytes);
+        memcpy(fp->cbuf, ptr, (size_t)bytes);
 
      /*
       * Setup the decompressor data...
@@ -2324,12 +2345,19 @@ cups_fill(cups_file_t *fp)              /* I - CUPS file */
       fp->stream.opaque    = (voidpf)0;
       fp->stream.next_in   = (Bytef *)fp->cbuf;
       fp->stream.next_out  = NULL;
-      fp->stream.avail_in  = bytes;
+      fp->stream.avail_in  = (uInt)bytes;
       fp->stream.avail_out = 0;
       fp->crc              = crc32(0L, Z_NULL, 0);
 
-      if (inflateInit2(&(fp->stream), -15) != Z_OK)
+      if ((status = inflateInit2(&(fp->stream), -15)) != Z_OK)
+      {
+       DEBUG_printf(("9cups_fill: inflateInit2 returned %d, returning -1.", status));
+
+        fp->eof = 1;
+        errno   = EIO;
+
        return (-1);
+      }
 
       fp->compressed = 1;
     }
@@ -2341,7 +2369,11 @@ cups_fill(cups_file_t *fp)               /* I - CUPS file */
       */
 
       if (fp->eof)
-       return (-1);
+      {
+        DEBUG_puts("9cups_fill: EOF, returning 0.");
+
+       return (0);
+      }
 
      /*
       * Fill the decompression buffer as needed...
@@ -2350,10 +2382,16 @@ cups_fill(cups_file_t *fp)              /* I - CUPS file */
       if (fp->stream.avail_in == 0)
       {
        if ((bytes = cups_read(fp, (char *)fp->cbuf, sizeof(fp->cbuf))) <= 0)
-          return (-1);
+       {
+         DEBUG_printf(("9cups_fill: cups_read error, returning %d.", (int)bytes));
+
+         fp->eof = 1;
+
+          return (bytes);
+       }
 
        fp->stream.next_in  = fp->cbuf;
-       fp->stream.avail_in = bytes;
+       fp->stream.avail_in = (uInt)bytes;
       }
 
      /*
@@ -2367,7 +2405,7 @@ cups_fill(cups_file_t *fp)                /* I - CUPS file */
 
       if (fp->stream.next_out > (Bytef *)fp->buf)
         fp->crc = crc32(fp->crc, (Bytef *)fp->buf,
-                       fp->stream.next_out - (Bytef *)fp->buf);
+                       (uInt)(fp->stream.next_out - (Bytef *)fp->buf));
 
       if (status == Z_STREAM_END)
       {
@@ -2377,45 +2415,72 @@ cups_fill(cups_file_t *fp)              /* I - CUPS file */
 
        unsigned char   trailer[8];     /* Trailer bytes */
        uLong           tcrc;           /* Trailer CRC */
+       ssize_t         tbytes = 0;     /* Number of bytes */
 
-
-       if (read(fp->fd, trailer, sizeof(trailer)) < sizeof(trailer))
+       if (fp->stream.avail_in > 0)
        {
-        /*
-          * Can't get it, so mark end-of-file...
-         */
+         if (fp->stream.avail_in > sizeof(trailer))
+           tbytes = (ssize_t)sizeof(trailer);
+         else
+           tbytes = (ssize_t)fp->stream.avail_in;
 
-          fp->eof = 1;
+         memcpy(trailer, fp->stream.next_in, (size_t)tbytes);
+         fp->stream.next_in  += tbytes;
+         fp->stream.avail_in -= (size_t)tbytes;
        }
-       else
-       {
-         tcrc = ((((((uLong)trailer[3] << 8) | (uLong)trailer[2]) << 8) |
-                 (uLong)trailer[1]) << 8) | (uLong)trailer[0];
 
-         if (tcrc != fp->crc)
+        if (tbytes < (ssize_t)sizeof(trailer))
+       {
+         if (read(fp->fd, trailer + tbytes, sizeof(trailer) - (size_t)tbytes) < ((ssize_t)sizeof(trailer) - tbytes))
          {
           /*
-            * Bad CRC, mark end-of-file...
+           * Can't get it, so mark end-of-file...
            */
 
-            DEBUG_printf(("9cups_fill: tcrc=%08x != fp->crc=%08x",
-                         (unsigned int)tcrc, (unsigned int)fp->crc));
+           DEBUG_puts("9cups_fill: Unable to read gzip CRC trailer, returning -1.");
 
            fp->eof = 1;
+           errno   = EIO;
 
            return (-1);
          }
+       }
 
+       tcrc = ((((((uLong)trailer[3] << 8) | (uLong)trailer[2]) << 8) |
+               (uLong)trailer[1]) << 8) | (uLong)trailer[0];
+
+       if (tcrc != fp->crc)
+       {
         /*
-         * Otherwise, reset the compressed flag so that we re-read the
-         * file header...
+         * Bad CRC, mark end-of-file...
          */
 
-         fp->compressed = 0;
+         DEBUG_printf(("9cups_fill: tcrc=%08x != fp->crc=%08x, returning -1.", (unsigned int)tcrc, (unsigned int)fp->crc));
+
+         fp->eof = 1;
+         errno   = EIO;
+
+         return (-1);
        }
+
+       /*
+       * Otherwise, reset the compressed flag so that we re-read the
+       * file header...
+       */
+
+       fp->compressed = 0;
       }
+      else if (status < Z_OK)
+      {
+       DEBUG_printf(("9cups_fill: inflate returned %d, returning -1.", status));
 
-      bytes = sizeof(fp->buf) - fp->stream.avail_out;
+        fp->eof = 1;
+        errno   = EIO;
+
+       return (-1);
+      }
+
+      bytes = (ssize_t)sizeof(fp->buf) - (ssize_t)fp->stream.avail_out;
 
      /*
       * Return the decompressed data...
@@ -2425,7 +2490,10 @@ cups_fill(cups_file_t *fp)               /* I - CUPS file */
       fp->end = fp->buf + bytes;
 
       if (bytes)
+      {
+        DEBUG_printf(("9cups_fill: Returning %d.", (int)bytes));
        return (bytes);
+      }
     }
   }
 #endif /* HAVE_LIBZ */
@@ -2443,17 +2511,19 @@ cups_fill(cups_file_t *fp)              /* I - CUPS file */
     fp->eof = 1;
     fp->ptr = fp->buf;
     fp->end = fp->buf;
-
-    return (-1);
   }
+  else
+  {
+   /*
+    * Return the bytes we read...
+    */
 
- /*
-  * Return the bytes we read...
-  */
+    fp->eof = 0;
+    fp->ptr = fp->buf;
+    fp->end = fp->buf + bytes;
+  }
 
-  fp->eof = 0;
-  fp->ptr = fp->buf;
-  fp->end = fp->buf + bytes;
+  DEBUG_printf(("9cups_fill: Not gzip, returning %d.", (int)bytes));
 
   return (bytes);
 }
@@ -2559,8 +2629,7 @@ cups_read(cups_file_t *fp,                /* I - CUPS file */
   ssize_t      total;                  /* Total bytes read */
 
 
-  DEBUG_printf(("7cups_read(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")", fp, buf,
-                CUPS_LLCAST bytes));
+  DEBUG_printf(("7cups_read(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")", (void *)fp, (void *)buf, CUPS_LLCAST bytes));
 
  /*
   * Loop until we read at least 0 bytes...
@@ -2616,8 +2685,7 @@ cups_write(cups_file_t *fp,               /* I - CUPS file */
   ssize_t      count;                  /* Count this time */
 
 
-  DEBUG_printf(("7cups_write(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")", fp, buf,
-                CUPS_LLCAST bytes));
+  DEBUG_printf(("7cups_write(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")", (void *)fp, (void *)buf, CUPS_LLCAST bytes));
 
  /*
   * Loop until all bytes are written...
@@ -2656,8 +2724,8 @@ cups_write(cups_file_t *fp,               /* I - CUPS file */
     * Update the counts for the last write call...
     */
 
-    bytes -= count;
-    total += count;
+    bytes -= (size_t)count;
+    total += (size_t)count;
     buf   += count;
   }