]> 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 2eac3a3cd7846f016df435b0474a9a33667a8b82..7814dba996f820e584a7e518e06b1fd9b80dafc7 100644 (file)
 /*
- * "$Id: file.c 6311 2007-02-27 14:43:39Z mike $"
+ * "$Id$"
  *
- *   File functions for the Common UNIX Printing System (CUPS).
+ * File functions for CUPS.
  *
- *   Since stdio files max out at 256 files on many systems, we have to
- *   write similar functions without this limit.  At the same time, using
- *   our own file functions allows us to provide transparent support of
- *   gzip'd print files, PPD files, etc.
+ * Since stdio files max out at 256 files on many systems, we have to
+ * write similar functions without this limit.  At the same time, using
+ * our own file functions allows us to provide transparent support of
+ * gzip'd print files, PPD files, etc.
  *
- *   Copyright 1997-2007 by Easy Software Products, all rights reserved.
+ * 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
- *   property of Easy Software Products 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 missing or damaged please contact Easy Software Products
- *   at:
+ * 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/".
  *
- *       Attn: CUPS Licensing Information
- *       Easy Software Products
- *       44141 Airport View Drive, Suite 204
- *       Hollywood, Maryland 20636 USA
- *
- *       Voice: (301) 373-9600
- *       EMail: cups-info@cups.org
- *         WWW: http://www.cups.org
- *
- * Contents:
- *
- *   cupsFileClose()       - Close a CUPS file.
- *   cupsFileCompression() - Return whether a file is compressed.
- *   cupsFileEOF()         - Return the end-of-file status.
- *   cupsFileFind()        - Find a file using the specified path.
- *   cupsFileFlush()       - Flush pending output.
- *   cupsFileGetChar()     - Get a single character from a file.
- *   cupsFileGetConf()     - Get a line from a configuration file...
- *   cupsFileGetLine()     - Get a CR and/or LF-terminated line that may
- *                           contain binary data.
- *   cupsFileGets()        - Get a CR and/or LF-terminated line.
- *   cupsFileLock()        - Temporarily lock access to a file.
- *   cupsFileNumber()      - Return the file descriptor associated with a CUPS
- *                           file.
- *   cupsFileOpen()        - Open a CUPS file.
- *   cupsFileOpenFd()      - Open a CUPS file using a file descriptor.
- *   cupsFilePeekChar()    - Peek at the next character from a file.
- *   cupsFilePrintf()      - Write a formatted string.
- *   cupsFilePutChar()     - Write a character.
- *   cupsFilePuts()        - Write a string.
- *   cupsFileRead()        - Read from a file.
- *   cupsFileRewind()      - Rewind a file.
- *   cupsFileSeek()        - Seek in a file.
- *   cupsFileStderr()      - Return a CUPS file associated with stderr.
- *   cupsFileStdin()       - Return a CUPS file associated with stdin.
- *   cupsFileStdout()      - Return a CUPS file associated with stdout.
- *   cupsFileTell()        - Return the current file position.
- *   cupsFileUnlock()      - Unlock access to a file.
- *   cupsFileWrite()       - Write to a file.
- *   cups_compress()       - Compress a buffer of data...
- *   cups_fill()           - Fill the input buffer...
- *   cups_read()           - Read from a file descriptor.
- *   cups_write()          - Write to a file descriptor.
+ * This file is subject to the Apple OS-Developed Software exception.
  */
 
 /*
  * Include necessary headers...
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdarg.h>
-#include <errno.h>
+#include "file-private.h"
+#include <sys/stat.h>
 #include <sys/types.h>
-#include <fcntl.h>
-#include "http-private.h"
-#include "globals.h"
-#include "debug.h"
+
+
+/*
+ * Local functions...
+ */
 
 #ifdef HAVE_LIBZ
-#  include <zlib.h>
+static ssize_t cups_compress(cups_file_t *fp, const char *buf, size_t bytes);
 #endif /* HAVE_LIBZ */
-#ifdef WIN32
-#  include <io.h>
-#  include <sys/locking.h>
-#endif /* WIN32 */
+static ssize_t cups_fill(cups_file_t *fp);
+static int     cups_open(const char *filename, int mode);
+static ssize_t cups_read(cups_file_t *fp, char *buf, size_t bytes);
+static ssize_t cups_write(cups_file_t *fp, const char *buf, size_t bytes);
 
 
+#ifndef WIN32
 /*
- * Some operating systems support large files via open flag O_LARGEFILE...
+ * '_cupsFileCheck()' - Check the permissions of the given filename.
  */
 
-#ifndef O_LARGEFILE
-#  define O_LARGEFILE 0
-#endif /* !O_LARGEFILE */
+_cups_fc_result_t                      /* O - Check result */
+_cupsFileCheck(
+    const char          *filename,     /* I - Filename to check */
+    _cups_fc_filetype_t filetype,      /* I - Type of file checks? */
+    int                 dorootchecks,  /* I - Check for root permissions? */
+    _cups_fc_func_t     cb,            /* I - Callback function */
+    void                *context)      /* I - Context pointer for callback */
 
+{
+  struct stat          fileinfo;       /* File information */
+  char                 message[1024],  /* Message string */
+                       temp[1024],     /* Parent directory filename */
+                       *ptr;           /* Pointer into parent directory */
+  _cups_fc_result_t    result;         /* Check result */
 
-/*
- * Some operating systems don't define O_BINARY, which is used by Microsoft
- * and IBM to flag binary files...
- */
 
-#ifndef O_BINARY
-#  define O_BINARY 0
-#endif /* !O_BINARY */
+ /*
+  * Does the filename contain a relative path ("../")?
+  */
 
+  if (strstr(filename, "../"))
+  {
+   /*
+    * Yes, fail it!
+    */
 
-/*
- * Types and structures...
- */
+    result = _CUPS_FILE_CHECK_RELATIVE_PATH;
+    goto finishup;
+  }
 
-struct _cups_file_s                    /**** CUPS file structure... ****/
+ /*
+  * Does the program even exist and is it accessible?
+  */
 
-{
-  int          fd;                     /* File descriptor */
-  char         mode,                   /* Mode ('r' or 'w') */
-               compressed,             /* Compression used? */
-               is_stdio,               /* stdin/out/err? */
-               eof,                    /* End of file? */
-               buf[4096],              /* Buffer */
-               *ptr,                   /* Pointer into buffer */
-               *end;                   /* End of buffer data */
-  off_t                pos;                    /* File position for start of buffer */
+  if (stat(filename, &fileinfo))
+  {
+   /*
+    * Nope...
+    */
 
-#ifdef HAVE_LIBZ
-  z_stream     stream;                 /* (De)compression stream */
-  Bytef                cbuf[4096];             /* (De)compression buffer */
-  uLong                crc;                    /* (De)compression CRC */
-#endif /* HAVE_LIBZ */
-};
+    result = _CUPS_FILE_CHECK_MISSING;
+    goto finishup;
+  }
+
+ /*
+  * Check the execute bit...
+  */
+
+  result = _CUPS_FILE_CHECK_OK;
+
+  switch (filetype)
+  {
+    case _CUPS_FILE_CHECK_DIRECTORY :
+        if (!S_ISDIR(fileinfo.st_mode))
+         result = _CUPS_FILE_CHECK_WRONG_TYPE;
+        break;
+
+    default :
+        if (!S_ISREG(fileinfo.st_mode))
+         result = _CUPS_FILE_CHECK_WRONG_TYPE;
+        break;
+  }
+
+  if (result)
+    goto finishup;
+
+ /*
+  * Are we doing root checks?
+  */
+
+  if (!dorootchecks)
+  {
+   /*
+    * Nope, so anything (else) goes...
+    */
+
+    goto finishup;
+  }
+
+ /*
+  * Verify permission of the file itself:
+  *
+  * 1. Must be owned by root
+  * 2. Must not be writable by group
+  * 3. Must not be setuid
+  * 4. Must not be writable by others
+  */
+
+  if (fileinfo.st_uid ||               /* 1. Must be owned by root */
+      (fileinfo.st_mode & S_IWGRP)  || /* 2. Must not be writable by group */
+      (fileinfo.st_mode & S_ISUID) ||  /* 3. Must not be setuid */
+      (fileinfo.st_mode & S_IWOTH))    /* 4. Must not be writable by others */
+  {
+    result = _CUPS_FILE_CHECK_PERMISSIONS;
+    goto finishup;
+  }
+
+  if (filetype == _CUPS_FILE_CHECK_DIRECTORY ||
+      filetype == _CUPS_FILE_CHECK_FILE_ONLY)
+    goto finishup;
+
+ /*
+  * Now check the containing directory...
+  */
+
+  strlcpy(temp, filename, sizeof(temp));
+  if ((ptr = strrchr(temp, '/')) != NULL)
+  {
+    if (ptr == temp)
+      ptr[1] = '\0';
+    else
+      *ptr = '\0';
+  }
+
+  if (stat(temp, &fileinfo))
+  {
+   /*
+    * Doesn't exist?!?
+    */
+
+    result   = _CUPS_FILE_CHECK_MISSING;
+    filetype = _CUPS_FILE_CHECK_DIRECTORY;
+    filename = temp;
+
+    goto finishup;
+  }
+
+  if (fileinfo.st_uid ||               /* 1. Must be owned by root */
+      (fileinfo.st_mode & S_IWGRP) ||  /* 2. Must not be writable by group */
+      (fileinfo.st_mode & S_ISUID) ||  /* 3. Must not be setuid */
+      (fileinfo.st_mode & S_IWOTH))    /* 4. Must not be writable by others */
+  {
+    result   = _CUPS_FILE_CHECK_PERMISSIONS;
+    filetype = _CUPS_FILE_CHECK_DIRECTORY;
+    filename = temp;
+  }
+
+ /*
+  * Common return point...
+  */
+
+  finishup:
+
+  if (cb)
+  {
+    cups_lang_t *lang = cupsLangDefault();
+                                       /* Localization information */
+
+    switch (result)
+    {
+      case _CUPS_FILE_CHECK_OK :
+         if (filetype == _CUPS_FILE_CHECK_DIRECTORY)
+           snprintf(message, sizeof(message),
+                    _cupsLangString(lang, _("Directory \"%s\" permissions OK "
+                                            "(0%o/uid=%d/gid=%d).")),
+                    filename, fileinfo.st_mode, (int)fileinfo.st_uid,
+                    (int)fileinfo.st_gid);
+         else
+           snprintf(message, sizeof(message),
+                    _cupsLangString(lang, _("File \"%s\" permissions OK "
+                                            "(0%o/uid=%d/gid=%d).")),
+                    filename, fileinfo.st_mode, (int)fileinfo.st_uid,
+                    (int)fileinfo.st_gid);
+          break;
+
+      case _CUPS_FILE_CHECK_MISSING :
+         if (filetype == _CUPS_FILE_CHECK_DIRECTORY)
+           snprintf(message, sizeof(message),
+                    _cupsLangString(lang, _("Directory \"%s\" not available: "
+                                            "%s")),
+                    filename, strerror(errno));
+         else
+           snprintf(message, sizeof(message),
+                    _cupsLangString(lang, _("File \"%s\" not available: %s")),
+                    filename, strerror(errno));
+          break;
+
+      case _CUPS_FILE_CHECK_PERMISSIONS :
+         if (filetype == _CUPS_FILE_CHECK_DIRECTORY)
+           snprintf(message, sizeof(message),
+                    _cupsLangString(lang, _("Directory \"%s\" has insecure "
+                                            "permissions "
+                                            "(0%o/uid=%d/gid=%d).")),
+                    filename, fileinfo.st_mode, (int)fileinfo.st_uid,
+                    (int)fileinfo.st_gid);
+         else
+           snprintf(message, sizeof(message),
+                    _cupsLangString(lang, _("File \"%s\" has insecure "
+                                            "permissions "
+                                            "(0%o/uid=%d/gid=%d).")),
+                    filename, fileinfo.st_mode, (int)fileinfo.st_uid,
+                    (int)fileinfo.st_gid);
+          break;
+
+      case _CUPS_FILE_CHECK_WRONG_TYPE :
+         if (filetype == _CUPS_FILE_CHECK_DIRECTORY)
+           snprintf(message, sizeof(message),
+                    _cupsLangString(lang, _("Directory \"%s\" is a file.")),
+                    filename);
+         else
+           snprintf(message, sizeof(message),
+                    _cupsLangString(lang, _("File \"%s\" is a directory.")),
+                    filename);
+          break;
+
+      case _CUPS_FILE_CHECK_RELATIVE_PATH :
+         if (filetype == _CUPS_FILE_CHECK_DIRECTORY)
+           snprintf(message, sizeof(message),
+                    _cupsLangString(lang, _("Directory \"%s\" contains a "
+                                            "relative path.")), filename);
+         else
+           snprintf(message, sizeof(message),
+                    _cupsLangString(lang, _("File \"%s\" contains a relative "
+                                            "path.")), filename);
+          break;
+    }
+
+    (*cb)(context, result, message);
+  }
+
+  return (result);
+}
 
 
 /*
- * Local functions...
+ * '_cupsFileCheckFilter()' - Report file check results as CUPS filter messages.
  */
 
-#ifdef HAVE_LIBZ
-static ssize_t cups_compress(cups_file_t *fp, const char *buf, size_t bytes);
-#endif /* HAVE_LIBZ */
-static ssize_t cups_fill(cups_file_t *fp);
-static ssize_t cups_read(cups_file_t *fp, char *buf, size_t bytes);
-static ssize_t cups_write(cups_file_t *fp, const char *buf, size_t bytes);
+void
+_cupsFileCheckFilter(
+    void              *context,                /* I - Context pointer (unused) */
+    _cups_fc_result_t result,          /* I - Result code */
+    const char        *message)                /* I - Message text */
+{
+  const char   *prefix;                /* Messaging prefix */
+
+
+  (void)context;
+
+  switch (result)
+  {
+    default :
+    case _CUPS_FILE_CHECK_OK :
+        prefix = "DEBUG2";
+       break;
+
+    case _CUPS_FILE_CHECK_MISSING :
+    case _CUPS_FILE_CHECK_WRONG_TYPE :
+        prefix = "ERROR";
+       fputs("STATE: +cups-missing-filter-warning\n", stderr);
+       break;
+
+    case _CUPS_FILE_CHECK_PERMISSIONS :
+    case _CUPS_FILE_CHECK_RELATIVE_PATH :
+        prefix = "ERROR";
+       fputs("STATE: +cups-insecure-filter-warning\n", stderr);
+       break;
+  }
+
+  fprintf(stderr, "%s: %s\n", prefix, message);
+}
+#endif /* !WIN32 */
 
 
 /*
  * 'cupsFileClose()' - Close a CUPS file.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
 int                                    /* O - 0 on success, -1 on error */
@@ -154,7 +326,7 @@ cupsFileClose(cups_file_t *fp)              /* I - CUPS file */
   int  is_stdio;                       /* Is a stdio file? */
 
 
-  DEBUG_printf(("cupsFileClose(fp=%p)\n", fp));
+  DEBUG_printf(("cupsFileClose(fp=%p)", (void *)fp));
 
  /*
   * Range check...
@@ -200,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;
@@ -218,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;
@@ -247,6 +419,9 @@ cupsFileClose(cups_file_t *fp)              /* I - CUPS file */
   mode     = fp->mode;
   is_stdio = fp->is_stdio;
 
+  if (fp->printf_buffer)
+    free(fp->printf_buffer);
+
   free(fp);
 
  /*
@@ -255,7 +430,7 @@ cupsFileClose(cups_file_t *fp)              /* I - CUPS file */
 
   if (mode == 's')
   {
-    if (closesocket(fd) < 0)
+    if (httpAddrClose(NULL, fd) < 0)
       status = -1;
   }
   else if (!is_stdio)
@@ -270,9 +445,11 @@ cupsFileClose(cups_file_t *fp)             /* I - CUPS file */
 
 /*
  * 'cupsFileCompression()' - Return whether a file is compressed.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
-int                                    /* O - CUPS_FILE_NONE or CUPS_FILE_GZIP */
+int                                    /* O - @code CUPS_FILE_NONE@ or @code CUPS_FILE_GZIP@ */
 cupsFileCompression(cups_file_t *fp)   /* I - CUPS file */
 {
   return (fp ? fp->compressed : CUPS_FILE_NONE);
@@ -281,9 +458,11 @@ cupsFileCompression(cups_file_t *fp)       /* I - CUPS file */
 
 /*
  * 'cupsFileEOF()' - Return the end-of-file status.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
-int                                    /* O - 1 on EOF, 0 otherwise */
+int                                    /* O - 1 on end of file, 0 otherwise */
 cupsFileEOF(cups_file_t *fp)           /* I - CUPS file */
 {
   return (fp ? fp->eof : 1);
@@ -296,11 +475,13 @@ cupsFileEOF(cups_file_t *fp)              /* I - CUPS file */
  * This function allows the paths in the path string to be separated by
  * colons (UNIX standard) or semicolons (Windows standard) and stores the
  * result in the buffer supplied.  If the file cannot be found in any of
- * the supplied paths, NULL is returned. A NULL path only matches the
- * current directory.
+ * the supplied paths, @code NULL@ is returned. A @code NULL@ path only
+ * matches the current directory.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
-const char *                           /* O - Full path to file or NULL */
+const char *                           /* O - Full path to file or @code NULL@ if not found */
 cupsFileFind(const char *filename,     /* I - File to find */
              const char *path,         /* I - Colon/semicolon-separated path */
              int        executable,    /* I - 1 = executable files, 0 = any file/dir */
@@ -315,6 +496,8 @@ 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, (void *)buffer, bufsize));
+
   if (!filename || !buffer || bufsize < 2)
     return (NULL);
 
@@ -326,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
@@ -351,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))
@@ -359,7 +542,7 @@ cupsFileFind(const char *filename,  /* I - File to find */
       if (!access(buffer, executable ? X_OK : 0))
 #endif /* WIN32 */
       {
-        DEBUG_printf(("cupsFileFind: Returning \"%s\"\n", buffer));
+        DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer));
         return (buffer);
       }
 
@@ -378,16 +561,16 @@ 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))
   {
-    DEBUG_printf(("cupsFileFind: Returning \"%s\"\n", buffer));
+    DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer));
     return (buffer);
   }
   else
   {
-    DEBUG_puts("cupsFileFind: Returning NULL");
+    DEBUG_puts("1cupsFileFind: Returning NULL");
     return (NULL);
   }
 }
@@ -395,6 +578,8 @@ cupsFileFind(const char *filename,  /* I - File to find */
 
 /*
  * 'cupsFileFlush()' - Flush pending output.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
 int                                    /* O - 0 on success, -1 on error */
@@ -403,7 +588,7 @@ cupsFileFlush(cups_file_t *fp)              /* I - CUPS file */
   ssize_t      bytes;                  /* Bytes to write */
 
 
-  DEBUG_printf(("cupsFileFlush(fp=%p)\n", fp));
+  DEBUG_printf(("cupsFileFlush(fp=%p)", (void *)fp));
 
  /*
   * Range check input...
@@ -411,47 +596,52 @@ cupsFileFlush(cups_file_t *fp)            /* I - CUPS file */
 
   if (!fp || fp->mode != 'w')
   {
-    DEBUG_puts("    Attempt to flush a read-only file...");
+    DEBUG_puts("1cupsFileFlush: Attempt to flush a read-only file...");
     return (-1);
   }
 
   bytes = (ssize_t)(fp->ptr - fp->buf);
 
-  DEBUG_printf(("    Flushing %ld bytes...\n", (long)bytes));
+  DEBUG_printf(("2cupsFileFlush: Flushing " CUPS_LLFMT " bytes...",
+                CUPS_LLCAST bytes));
 
   if (bytes > 0)
   {
 #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);
 
     fp->ptr = fp->buf;
   }
-   
+
   return (0);
 }
 
 
 /*
  * 'cupsFileGetChar()' - Get a single character from a file.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
-int                                    /* O - Character or -1 on EOF */
+int                                    /* O - Character or -1 on end of file */
 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("cupsFileGetChar: Bad arguments!");
+    DEBUG_puts("5cupsFileGetChar: Bad arguments!");
     return (-1);
   }
 
@@ -459,10 +649,12 @@ 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("cupsFileGetChar: Unable to fill buffer!");
+      DEBUG_puts("5cupsFileGetChar: Unable to fill buffer!");
       return (-1);
     }
 
@@ -470,17 +662,23 @@ cupsFileGetChar(cups_file_t *fp)  /* I - CUPS file */
   * Return the next character in the buffer...
   */
 
-  DEBUG_printf(("cupsFileGetChar: Returning %d...\n", *(fp->ptr) & 255));
+  DEBUG_printf(("5cupsFileGetChar: Returning %d...", *(fp->ptr) & 255));
+
+  fp->pos ++;
+
+  DEBUG_printf(("6cupsFileGetChar: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
 
   return (*(fp->ptr)++ & 255);
 }
 
 
 /*
- * 'cupsFileGetConf()' - Get a line from a configuration file...
+ * 'cupsFileGetConf()' - Get a line from a configuration file.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
-char *                                 /* O  - Line read or NULL on eof/error */
+char *                                 /* O  - Line read or @code NULL@ on end of file or error */
 cupsFileGetConf(cups_file_t *fp,       /* I  - CUPS file */
                 char        *buf,      /* O  - String buffer */
                size_t      buflen,     /* I  - Size of string buffer */
@@ -494,6 +692,9 @@ cupsFileGetConf(cups_file_t *fp,    /* I  - CUPS file */
   * Range check input...
   */
 
+  DEBUG_printf(("2cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT
+                ", 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)
   {
@@ -529,7 +730,7 @@ cupsFileGetConf(cups_file_t *fp,    /* I  - CUPS file */
         // Strip the comment and any trailing whitespace...
        while (ptr > buf)
        {
-         if (!isspace(ptr[-1] & 255))
+         if (!_cups_isspace(ptr[-1]))
            break;
 
          ptr --;
@@ -543,7 +744,7 @@ cupsFileGetConf(cups_file_t *fp,    /* I  - CUPS file */
     * Strip leading whitespace...
     */
 
-    for (ptr = buf; isspace(*ptr & 255); ptr ++);
+    for (ptr = buf; _cups_isspace(*ptr); ptr ++);
 
     if (ptr > buf)
       _cups_strcpy(buf, ptr);
@@ -559,7 +760,7 @@ cupsFileGetConf(cups_file_t *fp,    /* I  - CUPS file */
       */
 
       for (ptr = buf; *ptr; ptr ++)
-        if (isspace(*ptr & 255))
+        if (_cups_isspace(*ptr))
          break;
 
       if (*ptr)
@@ -568,7 +769,7 @@ cupsFileGetConf(cups_file_t *fp,    /* I  - CUPS file */
         * Have a value, skip any other spaces...
        */
 
-        while (isspace(*ptr & 255))
+        while (_cups_isspace(*ptr))
          *ptr++ = '\0';
 
         if (*ptr)
@@ -592,7 +793,7 @@ cupsFileGetConf(cups_file_t *fp,    /* I  - CUPS file */
          return (buf);
        }
 
-        while (ptr > *value && isspace(*ptr & 255))
+        while (ptr > *value && _cups_isspace(*ptr))
          *ptr-- = '\0';
       }
 
@@ -612,13 +813,15 @@ cupsFileGetConf(cups_file_t *fp,  /* I  - CUPS file */
  * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may
  *                       contain binary data.
  *
- * This function differs from cupsFileGets() in that the trailing CR and LF
- * are preserved, as is any binary data on the line. The buffer is nul-
- * terminated, however you should use the returned length to determine
+ * This function differs from @link cupsFileGets@ in that the trailing CR
+ * and LF are preserved, as is any binary data on the line. The buffer is
+ * nul-terminated, however you should use the returned length to determine
  * the number of bytes on the line.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
-size_t                                 /* O - Number of bytes on line or 0 on EOF */
+size_t                                 /* O - Number of bytes on line or 0 on end of file */
 cupsFileGetLine(cups_file_t *fp,       /* I - File to read from */
                 char        *buf,      /* I - Buffer */
                 size_t      buflen)    /* I - Size of buffer */
@@ -632,6 +835,8 @@ cupsFileGetLine(cups_file_t *fp,    /* I - File to read from */
   * Range check input...
   */
 
+  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);
 
@@ -646,6 +851,7 @@ cupsFileGetLine(cups_file_t *fp,    /* I - File to read from */
         break;
 
     *ptr++ = ch = *(fp->ptr)++;
+    fp->pos ++;
 
     if (ch == '\r')
     {
@@ -658,7 +864,10 @@ cupsFileGetLine(cups_file_t *fp,   /* I - File to read from */
           break;
 
       if (*(fp->ptr) == '\n')
+      {
         *ptr++ = *(fp->ptr)++;
+       fp->pos ++;
+      }
 
       break;
     }
@@ -674,15 +883,19 @@ cupsFileGetLine(cups_file_t *fp,  /* I - File to read from */
 
   *ptr = '\0';
 
-  return (ptr - buf);
+  DEBUG_printf(("4cupsFileGetLine: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
+
+  return ((size_t)(ptr - buf));
 }
 
 
 /*
  * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
-char *                                 /* O - Line read or NULL on eof/error */
+char *                                 /* O - Line read or @code NULL@ on end of file or error */
 cupsFileGets(cups_file_t *fp,          /* I - CUPS file */
              char        *buf,         /* O - String buffer */
             size_t      buflen)        /* I - Size of string buffer */
@@ -696,6 +909,8 @@ cupsFileGets(cups_file_t *fp,               /* I - CUPS file */
   * Range check input...
   */
 
+  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);
 
@@ -715,6 +930,7 @@ cupsFileGets(cups_file_t *fp,               /* I - CUPS file */
       }
 
     ch = *(fp->ptr)++;
+    fp->pos ++;
 
     if (ch == '\r')
     {
@@ -727,7 +943,10 @@ cupsFileGets(cups_file_t *fp,              /* I - CUPS file */
           break;
 
       if (*(fp->ptr) == '\n')
-        fp->ptr ++;      
+      {
+        fp->ptr ++;
+       fp->pos ++;
+      }
 
       break;
     }
@@ -740,21 +959,25 @@ cupsFileGets(cups_file_t *fp,             /* I - CUPS file */
       break;
     }
     else
-      *ptr++ = ch;
+      *ptr++ = (char)ch;
   }
 
   *ptr = '\0';
 
+  DEBUG_printf(("4cupsFileGets: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
+
   return (buf);
 }
 
 
 /*
  * 'cupsFileLock()' - Temporarily lock access to a file.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
 int                                    /* O - 0 on success, -1 on error */
-cupsFileLock(cups_file_t *fp,          /* I - File to lock */
+cupsFileLock(cups_file_t *fp,          /* I - CUPS file */
              int         block)                /* I - 1 to wait for the lock, 0 to fail right away */
 {
  /*
@@ -769,7 +992,7 @@ cupsFileLock(cups_file_t *fp,               /* I - File to lock */
   */
 
 #ifdef WIN32
-  return (locking(fp->fd, block ? _LK_LOCK : _LK_NBLCK, 0));
+  return (_locking(fp->fd, block ? _LK_LOCK : _LK_NBLCK, 0));
 #else
   return (lockf(fp->fd, block ? F_LOCK : F_TLOCK, 0));
 #endif /* WIN32 */
@@ -778,20 +1001,40 @@ cupsFileLock(cups_file_t *fp,            /* I - File to lock */
 
 /*
  * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
 int                                    /* O - File descriptor */
 cupsFileNumber(cups_file_t *fp)                /* I - CUPS file */
 {
-  return (fp->fd);
+  if (fp)
+    return (fp->fd);
+  else
+    return (-1);
 }
 
 
 /*
  * 'cupsFileOpen()' - Open a CUPS file.
+ *
+ * The "mode" parameter can be "r" to read, "w" to write, overwriting any
+ * existing file, "a" to append to an existing file or create a new file,
+ * or "s" to open a socket connection.
+ *
+ * When opening for writing ("w"), an optional number from 1 to 9 can be
+ * supplied which enables Flate compression of the file.  Compression is
+ * not supported for the "a" (append) mode.
+ *
+ * When opening a socket connection, the filename is a string of the form
+ * "address:port" or "hostname:port". The socket will make an IPv4 or IPv6
+ * connection as needed, generally preferring IPv6 connections when there is
+ * a choice.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
-cups_file_t *                          /* O - CUPS file or NULL */
+cups_file_t *                          /* O - CUPS file or @code NULL@ if the file or socket cannot be opened */
 cupsFileOpen(const char *filename,     /* I - Name of file */
              const char *mode)         /* I - Open mode */
 {
@@ -802,7 +1045,7 @@ cupsFileOpen(const char *filename, /* I - Name of file */
   http_addrlist_t *addrlist;           /* Host address list */
 
 
-  DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")\n", filename,
+  DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")", filename,
                 mode));
 
  /*
@@ -810,7 +1053,8 @@ cupsFileOpen(const char *filename, /* I - Name of file */
   */
 
   if (!filename || !mode ||
-      (*mode != 'r' && *mode != 'w' && *mode != 'a' && *mode != 's'))
+      (*mode != 'r' && *mode != 'w' && *mode != 'a' && *mode != 's') ||
+      (*mode == 'a' && isdigit(mode[1] & 255)))
     return (NULL);
 
  /*
@@ -820,7 +1064,8 @@ cupsFileOpen(const char *filename, /* I - Name of file */
   switch (*mode)
   {
     case 'a' : /* Append file */
-        fd = open(filename, O_RDWR | O_CREAT | O_APPEND | O_LARGEFILE | O_BINARY, 0666);
+        fd = cups_open(filename,
+                      O_RDWR | O_CREAT | O_APPEND | O_LARGEFILE | O_BINARY);
         break;
 
     case 'r' : /* Read file */
@@ -828,7 +1073,21 @@ cupsFileOpen(const char *filename,        /* I - Name of file */
        break;
 
     case 'w' : /* Write file */
-        fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT | O_LARGEFILE | O_BINARY, 0666);
+        fd = cups_open(filename, O_WRONLY | O_LARGEFILE | O_BINARY);
+       if (fd < 0 && errno == ENOENT)
+       {
+         fd = cups_open(filename,
+                        O_WRONLY | O_CREAT | O_EXCL | O_LARGEFILE | O_BINARY);
+         if (fd < 0 && errno == EEXIST)
+           fd = cups_open(filename, O_WRONLY | O_LARGEFILE | O_BINARY);
+       }
+
+       if (fd >= 0)
+#ifdef WIN32
+         _chsize(fd, 0);
+#else
+         ftruncate(fd, 0);
+#endif /* WIN32 */
         break;
 
     case 's' : /* Read/write socket */
@@ -872,7 +1131,7 @@ cupsFileOpen(const char *filename, /* I - Name of file */
   if ((fp = cupsFileOpenFd(fd, mode)) == NULL)
   {
     if (*mode == 's')
-      closesocket(fd);
+      httpAddrClose(NULL, fd);
     else
       close(fd);
   }
@@ -886,23 +1145,33 @@ cupsFileOpen(const char *filename,       /* I - Name of file */
 
 /*
  * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
+ *
+ * The "mode" parameter can be "r" to read, "w" to write, "a" to append,
+ * or "s" to treat the file descriptor as a bidirectional socket connection.
+ *
+ * When opening for writing ("w"), an optional number from 1 to 9 can be
+ * supplied which enables Flate compression of the file.  Compression is
+ * not supported for the "a" (append) mode.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
-cups_file_t *                          /* O - CUPS file or NULL */
+cups_file_t *                          /* O - CUPS file or @code NULL@ if the file could not be opened */
 cupsFileOpenFd(int        fd,          /* I - File descriptor */
               const char *mode)        /* I - Open mode */
 {
   cups_file_t  *fp;                    /* New CUPS file */
 
 
-  DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")\n", fd, mode));
+  DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")", fd, mode));
 
  /*
   * Range check input...
   */
 
   if (fd < 0 || !mode ||
-      (*mode != 'r' && *mode != 'w' && *mode != 'a' && *mode != 's'))
+      (*mode != 'r' && *mode != 'w' && *mode != 'a' && *mode != 's') ||
+      (*mode == 'a' && isdigit(mode[1] & 255)))
     return (NULL);
 
  /*
@@ -920,8 +1189,10 @@ cupsFileOpenFd(int        fd,             /* I - File descriptor */
 
   switch (*mode)
   {
-    case 'w' :
     case 'a' :
+        fp->pos = lseek(fd, 0, SEEK_END);
+
+    case 'w' :
        fp->mode = 'w';
        fp->ptr  = fp->buf;
        fp->end  = fp->buf + sizeof(fp->buf);
@@ -943,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;
 
@@ -993,9 +1264,11 @@ cupsFileOpenFd(int        fd,             /* I - File descriptor */
 
 /*
  * 'cupsFilePeekChar()' - Peek at the next character from a file.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
-int                                    /* O - Character or -1 on EOF */
+int                                    /* O - Character or -1 on end of file */
 cupsFilePeekChar(cups_file_t *fp)      /* I - CUPS file */
 {
  /*
@@ -1010,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);
 
  /*
@@ -1023,32 +1296,74 @@ cupsFilePeekChar(cups_file_t *fp)       /* I - CUPS file */
 
 /*
  * 'cupsFilePrintf()' - Write a formatted string.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
-int                                    /* O - Number of bytes written or -1 */
+int                                    /* O - Number of bytes written or -1 on error */
 cupsFilePrintf(cups_file_t *fp,                /* I - CUPS file */
                const char  *format,    /* I - Printf-style format string */
               ...)                     /* I - Additional args as necessary */
 {
   va_list      ap;                     /* Argument list */
   ssize_t      bytes;                  /* Formatted size */
-  char         buf[8192];              /* Formatted text */
 
 
-  DEBUG_printf(("cupsFilePrintf(fp=%p, format=\"%s\", ...)\n", fp, format));
+  DEBUG_printf(("2cupsFilePrintf(fp=%p, format=\"%s\", ...)", (void *)fp, format));
 
   if (!fp || !format || (fp->mode != 'w' && fp->mode != 's'))
     return (-1);
 
+  if (!fp->printf_buffer)
+  {
+   /*
+    * Start with an 1k printf buffer...
+    */
+
+    if ((fp->printf_buffer = malloc(1024)) == NULL)
+      return (-1);
+
+    fp->printf_size = 1024;
+  }
+
   va_start(ap, format);
-  bytes = vsnprintf(buf, sizeof(buf), format, ap);
+  bytes = vsnprintf(fp->printf_buffer, fp->printf_size, format, ap);
   va_end(ap);
 
-  if (bytes >= sizeof(buf))
-    return (-1);
+  if (bytes >= (ssize_t)fp->printf_size)
+  {
+   /*
+    * Expand the printf buffer...
+    */
+
+    char       *temp;                  /* Temporary buffer pointer */
+
+
+    if (bytes > 65535)
+      return (-1);
+
+    if ((temp = realloc(fp->printf_buffer, (size_t)(bytes + 1))) == NULL)
+      return (-1);
+
+    fp->printf_buffer = temp;
+    fp->printf_size   = (size_t)(bytes + 1);
+
+    va_start(ap, format);
+    bytes = vsnprintf(fp->printf_buffer, fp->printf_size, format, ap);
+    va_end(ap);
+  }
 
   if (fp->mode == 's')
-    return (cups_write(fp, buf, bytes));
+  {
+    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 ((int)bytes);
+  }
 
   if ((fp->ptr + bytes) > fp->end)
     if (cupsFileFlush(fp))
@@ -1056,26 +1371,30 @@ cupsFilePrintf(cups_file_t *fp,         /* I - CUPS file */
 
   fp->pos += bytes;
 
-  if (bytes > sizeof(fp->buf))
+  DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
+
+  if ((size_t)bytes > sizeof(fp->buf))
   {
 #ifdef HAVE_LIBZ
     if (fp->compressed)
-      return (cups_compress(fp, buf, bytes));
+      return ((int)cups_compress(fp, fp->printf_buffer, (size_t)bytes));
     else
 #endif /* HAVE_LIBZ */
-      return (cups_write(fp, buf, bytes));
+      return ((int)cups_write(fp, fp->printf_buffer, (size_t)bytes));
   }
   else
   {
-    memcpy(fp->ptr, buf, bytes);
+    memcpy(fp->ptr, fp->printf_buffer, (size_t)bytes);
     fp->ptr += bytes;
-    return (bytes);
+    return ((int)bytes);
   }
 }
 
 
 /*
  * 'cupsFilePutChar()' - Write a character.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
 int                                    /* O - 0 on success, -1 on error */
@@ -1098,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);
@@ -1113,20 +1432,87 @@ cupsFilePutChar(cups_file_t *fp,        /* I - CUPS file */
       if (cupsFileFlush(fp))
        return (-1);
 
-    *(fp->ptr) ++ = c;
+    *(fp->ptr) ++ = (char)c;
   }
 
   fp->pos ++;
 
+  DEBUG_printf(("4cupsFilePutChar: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
+
   return (0);
 }
 
 
+/*
+ * 'cupsFilePutConf()' - Write a configuration line.
+ *
+ * This function handles any comment escaping of the value.
+ *
+ * @since CUPS 1.4/OS X 10.6@
+ */
+
+ssize_t                                        /* O - Number of bytes written or -1 on error */
+cupsFilePutConf(cups_file_t *fp,       /* I - CUPS file */
+                const char *directive, /* I - Directive */
+               const char *value)      /* I - Value */
+{
+  ssize_t      bytes,                  /* Number of bytes written */
+               temp;                   /* Temporary byte count */
+  const char   *ptr;                   /* Pointer into value */
+
+
+  if (!fp || !directive || !*directive)
+    return (-1);
+
+  if ((bytes = cupsFilePuts(fp, directive)) < 0)
+    return (-1);
+
+  if (cupsFilePutChar(fp, ' ') < 0)
+    return (-1);
+  bytes ++;
+
+  if (value && *value)
+  {
+    if ((ptr = strchr(value, '#')) != NULL)
+    {
+     /*
+      * Need to quote the first # in the info string...
+      */
+
+      if ((temp = cupsFileWrite(fp, value, (size_t)(ptr - value))) < 0)
+        return (-1);
+      bytes += temp;
+
+      if (cupsFilePutChar(fp, '\\') < 0)
+        return (-1);
+      bytes ++;
+
+      if ((temp = cupsFilePuts(fp, ptr)) < 0)
+        return (-1);
+      bytes += temp;
+    }
+    else if ((temp = cupsFilePuts(fp, value)) < 0)
+      return (-1);
+    else
+      bytes += temp;
+  }
+
+  if (cupsFilePutChar(fp, '\n') < 0)
+    return (-1);
+  else
+    return (bytes + 1);
+}
+
+
 /*
  * 'cupsFilePuts()' - Write a string.
+ *
+ * Like the @code fputs@ function, no newline is appended to the string.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
-int                                    /* O - Number of bytes written or -1 */
+int                                    /* O - Number of bytes written or -1 on error */
 cupsFilePuts(cups_file_t *fp,          /* I - CUPS file */
              const char  *s)           /* I - String to write */
 {
@@ -1144,16 +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;
 
-    return (bytes);
+    DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
+
+    return ((int)bytes);
   }
 
   if ((fp->ptr + bytes) > fp->end)
@@ -1162,29 +1550,33 @@ cupsFilePuts(cups_file_t *fp,           /* I - CUPS file */
 
   fp->pos += bytes;
 
-  if (bytes > sizeof(fp->buf))
+  DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
+
+  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);
   }
 }
 
 
 /*
  * 'cupsFileRead()' - Read from a file.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
-ssize_t                                        /* O - Number of bytes read or -1 */
+ssize_t                                        /* O - Number of bytes read or -1 on error */
 cupsFileRead(cups_file_t *fp,          /* I - CUPS file */
              char        *buf,         /* O - Buffer */
             size_t      bytes)         /* I - Number of bytes to read */
@@ -1193,14 +1585,13 @@ cupsFileRead(cups_file_t *fp,           /* I - CUPS file */
   ssize_t      count;                  /* Bytes read */
 
 
-  DEBUG_printf(("cupsFileRead(fp=%p, buf=%p, bytes=%ld)\n", fp, buf,
-                (long)bytes));
+  DEBUG_printf(("2cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")", (void *)fp, (void *)buf, CUPS_LLCAST bytes));
 
  /*
   * Range check input...
   */
 
-  if (!fp || !buf || bytes < 0 || (fp->mode != 'r' && fp->mode != 's'))
+  if (!fp || !buf || (fp->mode != 'r' && fp->mode != 's'))
     return (-1);
 
   if (bytes == 0)
@@ -1216,7 +1607,8 @@ cupsFileRead(cups_file_t *fp,             /* I - CUPS file */
     if (fp->ptr >= fp->end)
       if (cups_fill(fp) <= 0)
       {
-        DEBUG_printf(("    cups_fill() returned -1, total=%d\n", total));
+        DEBUG_printf(("4cupsFileRead: cups_fill() returned -1, total="
+                     CUPS_LLFMT, CUPS_LLCAST total));
 
         if (total > 0)
           return ((ssize_t)total);
@@ -1228,15 +1620,18 @@ 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;
+
+    DEBUG_printf(("4cupsFileRead: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
 
    /*
     * Update the counts for the last read...
     */
 
-    bytes -= count;
-    total += count;
+    bytes -= (size_t)count;
+    total += (size_t)count;
     buf   += count;
   }
 
@@ -1244,23 +1639,29 @@ cupsFileRead(cups_file_t *fp,           /* I - CUPS file */
   * Return the total number of bytes read...
   */
 
-  DEBUG_printf(("    total=%d\n", total));
+  DEBUG_printf(("3cupsFileRead: total=" CUPS_LLFMT, CUPS_LLCAST total));
 
   return ((ssize_t)total);
 }
 
 
 /*
- * 'cupsFileRewind()' - Rewind a file.
+ * 'cupsFileRewind()' - Set the current file position to the beginning of the
+ *                      file.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
-off_t                                  /* O - New file position or -1 */
+off_t                                  /* O - New file position or -1 on error */
 cupsFileRewind(cups_file_t *fp)                /* I - CUPS file */
 {
  /*
   * Range check input...
   */
 
+  DEBUG_printf(("cupsFileRewind(fp=%p)", (void *)fp));
+  DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
+
   if (!fp || fp->mode != 'r')
     return (-1);
 
@@ -1268,18 +1669,22 @@ cupsFileRewind(cups_file_t *fp)         /* I - CUPS file */
   * Handle special cases...
   */
 
-  if (fp->pos == 0)
+  if (fp->bufpos == 0)
   {
    /*
     * No seeking necessary...
     */
 
+    fp->pos = 0;
+
     if (fp->ptr)
     {
       fp->ptr = fp->buf;
       fp->eof = 0;
     }
 
+    DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
+
     return (0);
   }
 
@@ -1295,12 +1700,19 @@ cupsFileRewind(cups_file_t *fp)         /* I - CUPS file */
   }
 #endif /* HAVE_LIBZ */
 
-  lseek(fp->fd, 0, SEEK_SET);
+  if (lseek(fp->fd, 0, SEEK_SET))
+  {
+    DEBUG_printf(("1cupsFileRewind: lseek failed: %s", strerror(errno)));
+    return (-1);
+  }
 
-  fp->pos = 0;
-  fp->ptr = NULL;
-  fp->end = NULL;
-  fp->eof = 0;
+  fp->bufpos = 0;
+  fp->pos    = 0;
+  fp->ptr    = NULL;
+  fp->end    = NULL;
+  fp->eof    = 0;
+
+  DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
 
   return (0);
 }
@@ -1308,18 +1720,20 @@ cupsFileRewind(cups_file_t *fp)         /* I - CUPS file */
 
 /*
  * 'cupsFileSeek()' - Seek in a file.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
-off_t                                  /* O - New file position or -1 */
+off_t                                  /* O - New file position or -1 on error */
 cupsFileSeek(cups_file_t *fp,          /* I - CUPS file */
              off_t       pos)          /* I - Position in file */
 {
   ssize_t      bytes;                  /* Number bytes in buffer */
 
 
-  DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT ")\n", fp, pos));
-  DEBUG_printf(("    fp->pos=" CUPS_LLFMT "\n", fp->pos));
-  DEBUG_printf(("    fp->ptr=%p, fp->end=%p\n", fp->ptr, fp->end));
+  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", (void *)fp->ptr, (void *)fp->end));
 
  /*
   * Range check input...
@@ -1335,19 +1749,24 @@ cupsFileSeek(cups_file_t *fp,           /* I - CUPS file */
   if (pos == 0)
     return (cupsFileRewind(fp));
 
-  if (fp->pos == pos)
+  if (fp->ptr)
   {
-   /*
-    * No seeking necessary...
-    */
+    bytes = (ssize_t)(fp->end - fp->buf);
 
-    if (fp->ptr)
+    DEBUG_printf(("2cupsFileSeek: bytes=" CUPS_LLFMT, CUPS_LLCAST bytes));
+
+    if (pos >= fp->bufpos && pos < (fp->bufpos + bytes))
     {
-      fp->ptr = fp->buf;
+     /*
+      * No seeking necessary...
+      */
+
+      fp->pos = pos;
+      fp->ptr = fp->buf + pos - fp->bufpos;
       fp->eof = 0;
-    }
 
-    return (pos);
+      return (pos);
+    }
   }
 
 #ifdef HAVE_LIBZ
@@ -1357,32 +1776,24 @@ 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 */
 
  /*
-  * Figure out the number of bytes in the current buffer, and then
-  * see if we are outside of it...
+  * Seek forwards or backwards...
   */
 
-  if (fp->ptr)
-    bytes = (ssize_t)(fp->end - fp->buf);
-  else
-    bytes = 0;
-
   fp->eof = 0;
 
-  DEBUG_printf(("    bytes=" CUPS_LLFMT "\n", CUPS_LLCAST bytes));
-
-  if (pos < fp->pos)
+  if (pos < fp->bufpos)
   {
    /*
     * Need to seek backwards...
     */
 
-    DEBUG_puts("    SEEK BACKWARDS");
+    DEBUG_puts("2cupsFileSeek: SEEK BACKWARDS");
 
 #ifdef HAVE_LIBZ
     if (fp->compressed)
@@ -1390,72 +1801,70 @@ cupsFileSeek(cups_file_t *fp,           /* I - CUPS file */
       inflateEnd(&fp->stream);
 
       lseek(fp->fd, 0, SEEK_SET);
-      fp->pos = 0;
-      fp->ptr = NULL;
-      fp->end = NULL;
+      fp->bufpos = 0;
+      fp->pos    = 0;
+      fp->ptr    = NULL;
+      fp->end    = NULL;
 
       while ((bytes = cups_fill(fp)) > 0)
-        if (pos >= fp->pos && pos < (fp->pos + bytes))
+        if (pos >= fp->bufpos && pos < (fp->bufpos + bytes))
          break;
 
       if (bytes <= 0)
         return (-1);
 
-      fp->ptr = fp->buf + pos - fp->pos;
+      fp->ptr = fp->buf + pos - fp->bufpos;
+      fp->pos = pos;
     }
     else
 #endif /* HAVE_LIBZ */
     {
-      fp->pos = lseek(fp->fd, pos, SEEK_SET);
-      fp->ptr = NULL;
-      fp->end = NULL;
+      fp->bufpos = lseek(fp->fd, pos, SEEK_SET);
+      fp->pos    = fp->bufpos;
+      fp->ptr    = NULL;
+      fp->end    = NULL;
 
-      DEBUG_printf(("    lseek() returned %ld...\n", (long)fp->pos));
+      DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT,
+                    CUPS_LLCAST fp->pos));
     }
   }
-  else if (pos >= (fp->pos + bytes))
+  else
   {
    /*
     * Need to seek forwards...
     */
 
-    DEBUG_puts("    SEEK FORWARDS");
+    DEBUG_puts("2cupsFileSeek: SEEK FORWARDS");
 
 #ifdef HAVE_LIBZ
     if (fp->compressed)
     {
       while ((bytes = cups_fill(fp)) > 0)
       {
-        if (pos >= fp->pos && pos < (fp->pos + bytes))
+        if (pos >= fp->bufpos && pos < (fp->bufpos + bytes))
          break;
       }
 
       if (bytes <= 0)
         return (-1);
 
-      fp->ptr = fp->buf + pos - fp->pos;
+      fp->ptr = fp->buf + pos - fp->bufpos;
+      fp->pos = pos;
     }
     else
 #endif /* HAVE_LIBZ */
     {
-      fp->pos = lseek(fp->fd, pos, SEEK_SET);
-      fp->ptr = NULL;
-      fp->end = NULL;
+      fp->bufpos = lseek(fp->fd, pos, SEEK_SET);
+      fp->pos    = fp->bufpos;
+      fp->ptr    = NULL;
+      fp->end    = NULL;
 
-      DEBUG_printf(("    lseek() returned " CUPS_LLFMT "...\n", fp->pos));
+      DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT,
+                    CUPS_LLCAST fp->pos));
     }
   }
-  else
-  {
-   /*
-    * Just reposition the current pointer, since we have the right
-    * range...
-    */
 
-    DEBUG_puts("    SEEK INSIDE BUFFER");
-
-    fp->ptr = fp->buf + pos - fp->pos;
-  }
+  DEBUG_printf(("2cupsFileSeek: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
 
   return (fp->pos);
 }
@@ -1463,9 +1872,11 @@ cupsFileSeek(cups_file_t *fp,            /* I - CUPS file */
 
 /*
  * 'cupsFileStderr()' - Return a CUPS file associated with stderr.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
-cups_file_t *
+cups_file_t *                          /* O - CUPS file */
 cupsFileStderr(void)
 {
   _cups_globals_t *cg = _cupsGlobals();        /* Pointer to library globals... */
@@ -1497,9 +1908,11 @@ cupsFileStderr(void)
 
 /*
  * 'cupsFileStdin()' - Return a CUPS file associated with stdin.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
-cups_file_t *
+cups_file_t *                          /* O - CUPS file */
 cupsFileStdin(void)
 {
   _cups_globals_t *cg = _cupsGlobals();        /* Pointer to library globals... */
@@ -1525,9 +1938,11 @@ cupsFileStdin(void)
 
 /*
  * 'cupsFileStdout()' - Return a CUPS file associated with stdout.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
-cups_file_t *
+cups_file_t *                          /* O - CUPS file */
 cupsFileStdout(void)
 {
   _cups_globals_t *cg = _cupsGlobals();        /* Pointer to library globals... */
@@ -1559,26 +1974,35 @@ cupsFileStdout(void)
 
 /*
  * 'cupsFileTell()' - Return the current file position.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
 off_t                                  /* O - File position */
 cupsFileTell(cups_file_t *fp)          /* I - CUPS file */
 {
+  DEBUG_printf(("2cupsFileTell(fp=%p)", (void *)fp));
+  DEBUG_printf(("3cupsFileTell: pos=" CUPS_LLFMT, CUPS_LLCAST (fp ? fp->pos : -1)));
+
   return (fp ? fp->pos : 0);
 }
 
 
 /*
  * 'cupsFileUnlock()' - Unlock access to a file.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
 int                                    /* O - 0 on success, -1 on error */
-cupsFileUnlock(cups_file_t *fp)                /* I - File to lock */
+cupsFileUnlock(cups_file_t *fp)                /* I - CUPS file */
 {
  /*
   * Range check...
   */
 
+  DEBUG_printf(("cupsFileUnlock(fp=%p)", (void *)fp));
+
   if (!fp || fp->mode == 's')
     return (-1);
 
@@ -1587,7 +2011,7 @@ cupsFileUnlock(cups_file_t *fp)           /* I - File to lock */
   */
 
 #ifdef WIN32
-  return (locking(fp->fd, _LK_UNLCK, 0));
+  return (_locking(fp->fd, _LK_UNLCK, 0));
 #else
   return (lockf(fp->fd, F_ULOCK, 0));
 #endif /* WIN32 */
@@ -1596,9 +2020,11 @@ cupsFileUnlock(cups_file_t *fp)          /* I - File to lock */
 
 /*
  * 'cupsFileWrite()' - Write to a file.
+ *
+ * @since CUPS 1.2/OS X 10.5@
  */
 
-ssize_t                                        /* O - Number of bytes written */
+ssize_t                                        /* O - Number of bytes written or -1 on error */
 cupsFileWrite(cups_file_t *fp,         /* I - CUPS file */
               const char  *buf,                /* I - Buffer */
              size_t      bytes)        /* I - Number of bytes to write */
@@ -1607,7 +2033,9 @@ cupsFileWrite(cups_file_t *fp,            /* I - CUPS file */
   * Range check input...
   */
 
-  if (!fp || !buf || bytes < 0 || (fp->mode != 'w' && fp->mode != 's'))
+  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);
 
   if (bytes == 0)
@@ -1624,6 +2052,8 @@ cupsFileWrite(cups_file_t *fp,            /* I - CUPS file */
 
     fp->pos += (off_t)bytes;
 
+    DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
+
     return ((ssize_t)bytes);
   }
 
@@ -1633,6 +2063,8 @@ cupsFileWrite(cups_file_t *fp,            /* I - CUPS file */
 
   fp->pos += (off_t)bytes;
 
+  DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
+
   if (bytes > sizeof(fp->buf))
   {
 #ifdef HAVE_LIBZ
@@ -1653,7 +2085,7 @@ cupsFileWrite(cups_file_t *fp,            /* I - CUPS file */
 
 #ifdef HAVE_LIBZ
 /*
- * 'cups_compress()' - Compress a buffer of data...
+ * 'cups_compress()' - Compress a buffer of data.
  */
 
 static ssize_t                         /* O - Number of bytes written or -1 */
@@ -1661,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(("cups_compress(fp=%p, buf=%p, bytes=%ld)\n", fp, buf,
-                (long)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)
   {
@@ -1683,12 +2114,12 @@ cups_compress(cups_file_t *fp,          /* I - CUPS file */
     * Flush the current buffer...
     */
 
-    DEBUG_printf(("    avail_in=%d, avail_out=%d\n", fp->stream.avail_in,
-                  fp->stream.avail_out));
+    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;
@@ -1698,13 +2129,13 @@ cups_compress(cups_file_t *fp,          /* I - CUPS file */
     deflate(&(fp->stream), Z_NO_FLUSH);
   }
 
-  return (bytes);
+  return ((ssize_t)bytes);
 }
 #endif /* HAVE_LIBZ */
 
 
 /*
- * 'cups_fill()' - Fill the input buffer...
+ * 'cups_fill()' - Fill the input buffer.
  */
 
 static ssize_t                         /* O - Number of bytes or -1 */
@@ -1712,25 +2143,20 @@ cups_fill(cups_file_t *fp)              /* I - CUPS file */
 {
   ssize_t              bytes;          /* Number of bytes read */
 #ifdef HAVE_LIBZ
+  int                  status;         /* Decompression status */
   const unsigned char  *ptr,           /* Pointer into buffer */
                        *end;           /* End of buffer */
 #endif /* HAVE_LIBZ */
 
 
-  DEBUG_printf(("cups_fill(fp=%p)\n", fp));
-  DEBUG_printf(("    fp->ptr=%p, fp->end=%p, fp->buf=%p, "
-                "fp->pos=" CUPS_LLFMT ", fp->eof=%d\n",
-                fp->ptr, fp->end, fp->buf, fp->pos, fp->eof));
-
- /*
-  * Update the "pos" element as needed...
-  */
+  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->pos += (off_t)(fp->end - fp->buf);
+    fp->bufpos += fp->end - fp->buf;
 
 #ifdef HAVE_LIBZ
-  DEBUG_printf(("    fp->compressed=%d\n", fp->compressed));
+  DEBUG_printf(("9cups_fill: fp->compressed=%d", fp->compressed));
 
   while (!fp->ptr || fp->compressed)
   {
@@ -1758,9 +2184,11 @@ cups_fill(cups_file_t *fp)               /* I - CUPS file */
        * Can't read from file!
        */
 
-        DEBUG_printf(("    cups_read() returned " CUPS_LLFMT "!\n",
+        DEBUG_printf(("9cups_fill: cups_read() returned " CUPS_LLFMT,
                      CUPS_LLCAST bytes));
 
+        fp->eof = 1;
+
        return (-1);
       }
 
@@ -1775,7 +2203,8 @@ cups_fill(cups_file_t *fp)                /* I - CUPS file */
        fp->ptr = fp->buf;
        fp->end = fp->buf + bytes;
 
-        DEBUG_printf(("    returning " CUPS_LLFMT "!\n", CUPS_LLCAST bytes));
+        DEBUG_printf(("9cups_fill: Returning " CUPS_LLFMT,
+                     CUPS_LLCAST bytes));
 
        return (bytes);
       }
@@ -1799,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);
        }
 
@@ -1811,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);
        }
       }
@@ -1832,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);
        }
       }
@@ -1853,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);
        }
       }
@@ -1871,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);
        }
       }
@@ -1880,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...
@@ -1891,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;
     }
@@ -1908,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...
@@ -1917,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;
       }
 
      /*
@@ -1930,7 +2401,13 @@ cups_fill(cups_file_t *fp)               /* I - CUPS file */
       fp->stream.next_out  = (Bytef *)fp->buf;
       fp->stream.avail_out = sizeof(fp->buf);
 
-      if (inflate(&(fp->stream), Z_NO_FLUSH) == Z_STREAM_END)
+      status = inflate(&(fp->stream), Z_NO_FLUSH);
+
+      if (fp->stream.next_out > (Bytef *)fp->buf)
+        fp->crc = crc32(fp->crc, (Bytef *)fp->buf,
+                       (uInt)(fp->stream.next_out - (Bytef *)fp->buf));
+
+      if (status == Z_STREAM_END)
       {
        /*
        * Read the CRC and length...
@@ -1938,42 +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...
-         */
-
-          fp->eof = 1;
+         if (fp->stream.avail_in > sizeof(trailer))
+           tbytes = (ssize_t)sizeof(trailer);
+         else
+           tbytes = (ssize_t)fp->stream.avail_in;
+
+         memcpy(trailer, fp->stream.next_in, (size_t)tbytes);
+         fp->stream.next_in  += tbytes;
+         fp->stream.avail_in -= (size_t)tbytes;
        }
-       else
-       {
-         tcrc = (((((trailer[3] << 8) | trailer[2]) << 8) | trailer[1]) << 8) |
-                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_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));
+
+        fp->eof = 1;
+        errno   = EIO;
+
+       return (-1);
       }
 
-      bytes = sizeof(fp->buf) - fp->stream.avail_out;
+      bytes = (ssize_t)sizeof(fp->buf) - (ssize_t)fp->stream.avail_out;
 
      /*
       * Return the decompressed data...
@@ -1983,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 */
@@ -2001,19 +2511,109 @@ cups_fill(cups_file_t *fp)             /* I - CUPS file */
     fp->eof = 1;
     fp->ptr = fp->buf;
     fp->end = fp->buf;
+  }
+  else
+  {
+   /*
+    * Return the bytes we read...
+    */
+
+    fp->eof = 0;
+    fp->ptr = fp->buf;
+    fp->end = fp->buf + bytes;
+  }
+
+  DEBUG_printf(("9cups_fill: Not gzip, returning %d.", (int)bytes));
+
+  return (bytes);
+}
+
+
+/*
+ * 'cups_open()' - Safely open a file for writing.
+ *
+ * We don't allow appending to directories or files that are hard-linked or
+ * symlinked.
+ */
+
+static int                             /* O - File descriptor or -1 otherwise */
+cups_open(const char *filename,                /* I - Filename */
+         int        mode)              /* I - Open mode */
+{
+  int          fd;                     /* File descriptor */
+  struct stat  fileinfo;               /* File information */
+#ifndef WIN32
+  struct stat  linkinfo;               /* Link information */
+#endif /* !WIN32 */
+
+
+ /*
+  * Open the file...
+  */
+
+  if ((fd = open(filename, mode, 0666)) < 0)
+    return (-1);
+
+ /*
+  * Then verify that the file descriptor doesn't point to a directory or hard-
+  * linked file.
+  */
+
+  if (fstat(fd, &fileinfo))
+  {
+    close(fd);
+    return (-1);
+  }
 
+  if (fileinfo.st_nlink != 1)
+  {
+    close(fd);
+    errno = EPERM;
     return (-1);
   }
 
+#ifdef WIN32
+  if (fileinfo.st_mode & _S_IFDIR)
+#else
+  if (S_ISDIR(fileinfo.st_mode))
+#endif /* WIN32 */
+  {
+    close(fd);
+    errno = EISDIR;
+    return (-1);
+  }
+
+#ifndef WIN32
  /*
-  * Return the bytes we read...
+  * Then use lstat to determine whether the filename is a symlink...
   */
 
-  fp->eof = 0;
-  fp->ptr = fp->buf;
-  fp->end = fp->buf + bytes;
+  if (lstat(filename, &linkinfo))
+  {
+    close(fd);
+    return (-1);
+  }
 
-  return (bytes);
+  if (S_ISLNK(linkinfo.st_mode) ||
+      fileinfo.st_dev != linkinfo.st_dev ||
+      fileinfo.st_ino != linkinfo.st_ino ||
+#ifdef HAVE_ST_GEN
+      fileinfo.st_gen != linkinfo.st_gen ||
+#endif /* HAVE_ST_GEN */
+      fileinfo.st_nlink != linkinfo.st_nlink ||
+      fileinfo.st_mode != linkinfo.st_mode)
+  {
+   /*
+    * Yes, don't allow!
+    */
+
+    close(fd);
+    errno = EPERM;
+    return (-1);
+  }
+#endif /* !WIN32 */
+
+  return (fd);
 }
 
 
@@ -2029,6 +2629,8 @@ 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 ")", (void *)fp, (void *)buf, CUPS_LLCAST bytes));
+
  /*
   * Loop until we read at least 0 bytes...
   */
@@ -2047,6 +2649,8 @@ cups_read(cups_file_t *fp,                /* I - CUPS file */
       total = read(fp->fd, buf, bytes);
 #endif /* WIN32 */
 
+    DEBUG_printf(("9cups_read: total=" CUPS_LLFMT, CUPS_LLCAST total));
+
     if (total >= 0)
       break;
 
@@ -2081,8 +2685,7 @@ cups_write(cups_file_t *fp,               /* I - CUPS file */
   ssize_t      count;                  /* Count this time */
 
 
-  DEBUG_printf(("cups_write(fp=%p, buf=%p, bytes=%ld)\n", fp, buf,
-                (long)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...
@@ -2103,6 +2706,8 @@ cups_write(cups_file_t *fp,               /* I - CUPS file */
       count = write(fp->fd, buf, bytes);
 #endif /* WIN32 */
 
+    DEBUG_printf(("9cups_write: count=" CUPS_LLFMT, CUPS_LLCAST count));
+
     if (count < 0)
     {
      /*
@@ -2115,14 +2720,12 @@ cups_write(cups_file_t *fp,             /* I - CUPS file */
         return (-1);
     }
 
-    DEBUG_printf(("    count=%ld\n", (long)count));
-
    /*
     * Update the counts for the last write call...
     */
 
-    bytes -= count;
-    total += count;
+    bytes -= (size_t)count;
+    total += (size_t)count;
     buf   += count;
   }
 
@@ -2135,5 +2738,5 @@ cups_write(cups_file_t *fp,               /* I - CUPS file */
 
 
 /*
- * End of "$Id: file.c 6311 2007-02-27 14:43:39Z mike $".
+ * End of "$Id$".
  */