]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/file.c
Import CUPS 1.4svn r7023 into easysw/current.
[thirdparty/cups.git] / cups / file.c
index 31fd2c233472339e110ddb98094f23b0a33911e7..e7c7b0b15a85319b1e71fe2a7c309c845d6da99a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * "$Id: file.c 177 2006-06-21 00:20:03Z jlovell $"
+ * "$Id: file.c 6962 2007-09-17 20:35:47Z mike $"
  *
  *   File functions for the Common UNIX Printing System (CUPS).
  *
@@ -8,23 +8,14 @@
  *   our own file functions allows us to provide transparent support of
  *   gzip'd print files, PPD files, etc.
  *
- *   Copyright 1997-2006 by Easy Software Products, all rights reserved.
+ *   Copyright 2007 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:
- *
- *       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
+ *   property of Apple Inc. and are protected by Federal copyright
+ *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ *   which should have been included with this file.  If this file is
+ *   file is missing or damaged, see the license at "http://www.cups.org/".
  *
  * Contents:
  *
 #endif /* !O_LARGEFILE */
 
 
+/*
+ * 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 */
+
+
 /*
  * Types and structures...
  */
@@ -332,7 +333,11 @@ cupsFileFind(const char *filename, /* I - File to find */
 
   while (*path)
   {
+#ifdef WIN32
+    if (*path == ';' || (*path == ':' && ((bufptr - buffer) > 1 || !isalpha(buffer[0] & 255))))
+#else
     if (*path == ';' || *path == ':')
+#endif /* WIN32 */
     {
       if (bufptr > buffer && bufptr[-1] != '/' && bufptr < bufend)
         *bufptr++ = '/';
@@ -344,7 +349,10 @@ cupsFileFind(const char *filename, /* I - File to find */
 #else
       if (!access(buffer, executable ? X_OK : 0))
 #endif /* WIN32 */
+      {
+        DEBUG_printf(("cupsFileFind: Returning \"%s\"\n", buffer));
         return (buffer);
+      }
 
       bufptr = buffer;
     }
@@ -364,9 +372,15 @@ cupsFileFind(const char *filename, /* I - File to find */
   strlcpy(bufptr, filename, bufend - bufptr);
 
   if (!access(buffer, 0))
+  {
+    DEBUG_printf(("cupsFileFind: Returning \"%s\"\n", buffer));
     return (buffer);
+  }
   else
+  {
+    DEBUG_puts("cupsFileFind: Returning NULL");
     return (NULL);
+  }
 }
 
 
@@ -377,7 +391,7 @@ cupsFileFind(const char *filename,  /* I - File to find */
 int                                    /* O - 0 on success, -1 on error */
 cupsFileFlush(cups_file_t *fp)         /* I - CUPS file */
 {
-  size_t       bytes;                  /* Bytes to write */
+  ssize_t      bytes;                  /* Bytes to write */
 
 
   DEBUG_printf(("cupsFileFlush(fp=%p)\n", fp));
@@ -392,7 +406,7 @@ cupsFileFlush(cups_file_t *fp)              /* I - CUPS file */
     return (-1);
   }
 
-  bytes = fp->ptr - fp->buf;
+  bytes = (ssize_t)(fp->ptr - fp->buf);
 
   DEBUG_printf(("    Flushing %ld bytes...\n", (long)bytes));
 
@@ -427,7 +441,10 @@ cupsFileGetChar(cups_file_t *fp)   /* I - CUPS file */
   */
 
   if (!fp || (fp->mode != 'r' && fp->mode != 's'))
+  {
+    DEBUG_puts("cupsFileGetChar: Bad arguments!");
     return (-1);
+  }
 
  /*
   * If the input buffer is empty, try to read more data...
@@ -435,12 +452,17 @@ cupsFileGetChar(cups_file_t *fp)  /* I - CUPS file */
 
   if (fp->ptr >= fp->end)
     if (cups_fill(fp) < 0)
+    {
+      DEBUG_puts("cupsFileGetChar: Unable to fill buffer!");
       return (-1);
+    }
 
  /*
   * Return the next character in the buffer...
   */
 
+  DEBUG_printf(("cupsFileGetChar: Returning %d...\n", *(fp->ptr) & 255));
+
   return (*(fp->ptr)++ & 255);
 }
 
@@ -477,7 +499,7 @@ cupsFileGetConf(cups_file_t *fp,    /* I  - CUPS file */
   */
 
   *value = NULL;
-  
+
   while (cupsFileGets(fp, buf, buflen))
   {
     (*linenum) ++;
@@ -488,15 +510,24 @@ cupsFileGetConf(cups_file_t *fp,  /* I  - CUPS file */
 
     if ((ptr = strchr(buf, '#')) != NULL)
     {
-      while (ptr > buf)
+      if (ptr > buf && ptr[-1] == '\\')
       {
-       if (!isspace(ptr[-1] & 255))
-         break;
-
-        ptr --;
+        // Unquote the #...
+       _cups_strcpy(ptr - 1, ptr);
       }
+      else
+      {
+        // Strip the comment and any trailing whitespace...
+       while (ptr > buf)
+       {
+         if (!isspace(ptr[-1] & 255))
+           break;
+
+         ptr --;
+       }
 
-      *ptr = '\0';
+       *ptr = '\0';
+      }
     }
 
    /*
@@ -780,15 +811,15 @@ 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, 0666);
+        fd = open(filename, O_RDWR | O_CREAT | O_APPEND | O_LARGEFILE | O_BINARY, 0666);
         break;
 
     case 'r' : /* Read file */
-       fd = open(filename, O_RDONLY | O_LARGEFILE, 0);
+       fd = open(filename, O_RDONLY | O_LARGEFILE | O_BINARY, 0);
        break;
 
     case 'w' : /* Write file */
-        fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT | O_LARGEFILE, 0666);
+        fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT | O_LARGEFILE | O_BINARY, 0666);
         break;
 
     case 's' : /* Read/write socket */
@@ -991,7 +1022,7 @@ cupsFilePrintf(cups_file_t *fp,            /* I - CUPS file */
               ...)                     /* I - Additional args as necessary */
 {
   va_list      ap;                     /* Argument list */
-  size_t       bytes;                  /* Formatted size */
+  ssize_t      bytes;                  /* Formatted size */
   char         buf[8192];              /* Formatted text */
 
 
@@ -1090,7 +1121,7 @@ int                                       /* O - Number of bytes written or -1 */
 cupsFilePuts(cups_file_t *fp,          /* I - CUPS file */
              const char  *s)           /* I - String to write */
 {
-  size_t       bytes;                  /* Bytes to write */
+  ssize_t      bytes;                  /* Bytes to write */
 
 
  /*
@@ -1104,7 +1135,7 @@ cupsFilePuts(cups_file_t *fp,             /* I - CUPS file */
   * Write the string...
   */
 
-  bytes = strlen(s);
+  bytes = (int)strlen(s);
 
   if (fp->mode == 's')
   {
@@ -1149,8 +1180,8 @@ cupsFileRead(cups_file_t *fp,             /* I - CUPS file */
              char        *buf,         /* O - Buffer */
             size_t      bytes)         /* I - Number of bytes to read */
 {
-  size_t       total,                  /* Total bytes read */
-               count;                  /* Bytes read */
+  size_t       total;                  /* Total bytes read */
+  ssize_t      count;                  /* Bytes read */
 
 
   DEBUG_printf(("cupsFileRead(fp=%p, buf=%p, bytes=%ld)\n", fp, buf,
@@ -1176,17 +1207,17 @@ 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(("    cups_fill() returned -1, total=%d\n", (int)total));
 
         if (total > 0)
-          return (total);
+          return ((ssize_t)total);
        else
          return (-1);
       }
 
-    count = fp->end - fp->ptr;
-    if (count > bytes)
-      count = bytes;
+    count = (ssize_t)(fp->end - fp->ptr);
+    if (count > (ssize_t)bytes)
+      count = (ssize_t)bytes;
 
     memcpy(buf, fp->ptr, count);
     fp->ptr += count;
@@ -1204,9 +1235,9 @@ cupsFileRead(cups_file_t *fp,             /* I - CUPS file */
   * Return the total number of bytes read...
   */
 
-  DEBUG_printf(("    total=%d\n", total));
+  DEBUG_printf(("    total=%d\n", (int)total));
 
-  return (total);
+  return ((ssize_t)total);
 }
 
 
@@ -1260,6 +1291,7 @@ cupsFileRewind(cups_file_t *fp)           /* I - CUPS file */
   fp->pos = 0;
   fp->ptr = NULL;
   fp->end = NULL;
+  fp->eof = 0;
 
   return (0);
 }
@@ -1273,7 +1305,7 @@ off_t                                     /* O - New file position or -1 */
 cupsFileSeek(cups_file_t *fp,          /* I - CUPS file */
              off_t       pos)          /* I - Position in file */
 {
-  size_t       bytes;                  /* Number bytes in buffer */
+  ssize_t      bytes;                  /* Number bytes in buffer */
 
 
   DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT ")\n", fp, pos));
@@ -1327,7 +1359,7 @@ cupsFileSeek(cups_file_t *fp,             /* I - CUPS file */
   */
 
   if (fp->ptr)
-    bytes = fp->end - fp->buf;
+    bytes = (ssize_t)(fp->end - fp->buf);
   else
     bytes = 0;
 
@@ -1581,16 +1613,16 @@ cupsFileWrite(cups_file_t *fp,          /* I - CUPS file */
     if (cups_write(fp, buf, bytes) < 0)
       return (-1);
 
-    fp->pos += bytes;
+    fp->pos += (off_t)bytes;
 
-    return (bytes);
+    return ((ssize_t)bytes);
   }
 
   if ((fp->ptr + bytes) > fp->end)
     if (cupsFileFlush(fp))
       return (-1);
 
-  fp->pos += bytes;
+  fp->pos += (off_t)bytes;
 
   if (bytes > sizeof(fp->buf))
   {
@@ -1605,7 +1637,7 @@ cupsFileWrite(cups_file_t *fp,            /* I - CUPS file */
   {
     memcpy(fp->ptr, buf, bytes);
     fp->ptr += bytes;
-    return (bytes);
+    return ((ssize_t)bytes);
   }
 }
 
@@ -1671,6 +1703,7 @@ 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 */
@@ -1686,7 +1719,7 @@ cups_fill(cups_file_t *fp)                /* I - CUPS file */
   */
 
   if (fp->ptr && fp->end)
-    fp->pos += fp->end - fp->buf;
+    fp->pos += (off_t)(fp->end - fp->buf);
 
 #ifdef HAVE_LIBZ
   DEBUG_printf(("    fp->compressed=%d\n", fp->compressed));
@@ -1889,7 +1922,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,
+                       fp->stream.next_out - (Bytef *)fp->buf);
+
+      if (status == Z_STREAM_END)
       {
        /*
        * Read the CRC and length...
@@ -1918,6 +1957,9 @@ cups_fill(cups_file_t *fp)                /* I - CUPS file */
             * Bad CRC, mark end-of-file...
            */
 
+            DEBUG_printf(("cups_fill: tcrc=%08x, fp->crc=%08x\n",
+                         (unsigned int)tcrc, (unsigned int)fp->crc));
+
            fp->eof = 1;
 
            return (-1);
@@ -1994,10 +2036,17 @@ cups_read(cups_file_t *fp,              /* I - CUPS file */
 
   for (;;)
   {
+#ifdef WIN32
+    if (fp->mode == 's')
+      total = (ssize_t)recv(fp->fd, buf, (unsigned)bytes, 0);
+    else
+      total = (ssize_t)read(fp->fd, buf, (unsigned)bytes);
+#else
     if (fp->mode == 's')
       total = recv(fp->fd, buf, bytes, 0);
     else
       total = read(fp->fd, buf, bytes);
+#endif /* WIN32 */
 
     if (total >= 0)
       break;
@@ -2029,8 +2078,8 @@ cups_write(cups_file_t *fp,               /* I - CUPS file */
            const char  *buf,           /* I - Buffer */
           size_t      bytes)           /* I - Number bytes */
 {
-  size_t       total,                  /* Total bytes written */
-               count;                  /* Count this time */
+  size_t       total;                  /* Total bytes written */
+  ssize_t      count;                  /* Count this time */
 
 
   DEBUG_printf(("cups_write(fp=%p, buf=%p, bytes=%ld)\n", fp, buf,
@@ -2043,10 +2092,17 @@ cups_write(cups_file_t *fp,             /* I - CUPS file */
   total = 0;
   while (bytes > 0)
   {
+#ifdef WIN32
+    if (fp->mode == 's')
+      count = (ssize_t)send(fp->fd, buf, (unsigned)bytes, 0);
+    else
+      count = (ssize_t)write(fp->fd, buf, (unsigned)bytes);
+#else
     if (fp->mode == 's')
       count = send(fp->fd, buf, bytes, 0);
     else
       count = write(fp->fd, buf, bytes);
+#endif /* WIN32 */
 
     if (count < 0)
     {
@@ -2075,10 +2131,10 @@ cups_write(cups_file_t *fp,             /* I - CUPS file */
   * Return the total number of bytes written...
   */
 
-  return (total);
+  return ((ssize_t)total);
 }
 
 
 /*
- * End of "$Id: file.c 177 2006-06-21 00:20:03Z jlovell $".
+ * End of "$Id: file.c 6962 2007-09-17 20:35:47Z mike $".
  */