/*
- * "$Id$"
- *
* File functions for CUPS.
*
* Since stdio files max out at 256 files on many systems, we have to
* our own file functions allows us to provide transparent support of
* gzip'd print files, PPD files, etc.
*
- * Copyright 2007-2014 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
* 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/".
+ * missing or damaged, see the license at "http://www.cups.org/".
+ *
+ * This file is subject to the Apple OS-Developed Software exception.
*/
/*
switch (result)
{
+ default :
case _CUPS_FILE_CHECK_OK :
prefix = "DEBUG2";
break;
/*
* 'cupsFileClose()' - Close a CUPS file.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
int /* O - 0 on success, -1 on error */
int is_stdio; /* Is a stdio file? */
- DEBUG_printf(("cupsFileClose(fp=%p)", fp));
+ DEBUG_printf(("cupsFileClose(fp=%p)", (void *)fp));
/*
* Range check...
/*
* 'cupsFileCompression()' - Return whether a file is compressed.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
int /* O - @code CUPS_FILE_NONE@ or @code CUPS_FILE_GZIP@ */
/*
* 'cupsFileEOF()' - Return the end-of-file status.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
int /* O - 1 on end of file, 0 otherwise */
* the supplied paths, @code NULL@ is returned. A @code NULL@ path only
* matches the current directory.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
const char * /* O - Full path to file or @code NULL@ if not found */
* 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);
if (!access(filename, 0))
{
- strlcpy(buffer, filename, bufsize);
+ strlcpy(buffer, filename, (size_t)bufsize);
return (buffer);
}
else
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))
if (bufptr > buffer && bufptr[-1] != '/' && bufptr < bufend)
*bufptr++ = '/';
- strlcpy(bufptr, filename, bufend - bufptr);
+ strlcpy(bufptr, filename, (size_t)(bufend - bufptr));
if (!access(buffer, 0))
{
/*
* 'cupsFileFlush()' - Flush pending output.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
int /* O - 0 on success, -1 on error */
ssize_t bytes; /* Bytes to write */
- DEBUG_printf(("cupsFileFlush(fp=%p)", fp));
+ DEBUG_printf(("cupsFileFlush(fp=%p)", (void *)fp));
/*
* Range check input...
/*
* 'cupsFileGetChar()' - Get a single character from a file.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
int /* O - Character or -1 on end of file */
* Range check input...
*/
+ DEBUG_printf(("4cupsFileGetChar(fp=%p)", (void *)fp));
+
if (!fp || (fp->mode != 'r' && fp->mode != 's'))
{
DEBUG_puts("5cupsFileGetChar: Bad arguments!");
* 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);
/*
* 'cupsFileGetConf()' - Get a line from a configuration file.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
char * /* O - Line read or @code NULL@ on end of file or error */
*/
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)
* 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@
+ * @since CUPS 1.2/macOS 10.5@
*/
size_t /* O - Number of bytes on line or 0 on end of file */
* 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);
/*
* 'cupsFileGets()' - Get a CR and/or LF-terminated line.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
char * /* O - Line read or @code NULL@ on end of file or error */
* 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);
/*
* 'cupsFileLock()' - Temporarily lock access to a file.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
int /* O - 0 on success, -1 on error */
/*
* 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
int /* O - File descriptor */
* connection as needed, generally preferring IPv6 connections when there is
* a choice.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
cups_file_t * /* O - CUPS file or @code NULL@ if the file or socket cannot be opened */
* 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@
+ * @since CUPS 1.2/macOS 10.5@
*/
cups_file_t * /* O - CUPS file or @code NULL@ if the file could not be opened */
/*
* 'cupsFilePeekChar()' - Peek at the next character from a file.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
int /* O - Character or -1 on end of file */
*/
if (fp->ptr >= fp->end)
- if (cups_fill(fp) < 0)
+ if (cups_fill(fp) <= 0)
return (-1);
/*
/*
* 'cupsFilePrintf()' - Write a formatted string.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
int /* O - Number of bytes written or -1 on error */
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);
}
else
{
- memcpy(fp->ptr, fp->printf_buffer, bytes);
+ memcpy(fp->ptr, fp->printf_buffer, (size_t)bytes);
fp->ptr += bytes;
return ((int)bytes);
}
/*
* 'cupsFilePutChar()' - Write a character.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
int /* O - 0 on success, -1 on error */
*
* This function handles any comment escaping of the value.
*
- * @since CUPS 1.4/OS X 10.6@
+ * @since CUPS 1.4/macOS 10.6@
*/
ssize_t /* O - Number of bytes written or -1 on error */
*
* Like the @code fputs@ function, no newline is appended to the string.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
int /* O - Number of bytes written or -1 on error */
}
else
{
- memcpy(fp->ptr, s, bytes);
+ memcpy(fp->ptr, s, (size_t)bytes);
fp->ptr += bytes;
return ((int)bytes);
}
/*
* 'cupsFileRead()' - Read from a file.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
ssize_t /* O - Number of bytes read or -1 on error */
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...
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;
* 'cupsFileRewind()' - Set the current file position to the beginning of the
* file.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
off_t /* O - New file position or -1 on error */
* 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')
/*
* 'cupsFileSeek()' - Seek in a file.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
off_t /* O - New file position or -1 on error */
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...
* 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 */
/*
* 'cupsFileStderr()' - Return a CUPS file associated with stderr.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
cups_file_t * /* O - CUPS file */
/*
* 'cupsFileStdin()' - Return a CUPS file associated with stdin.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
cups_file_t * /* O - CUPS file */
/*
* 'cupsFileStdout()' - Return a CUPS file associated with stdout.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
cups_file_t * /* O - CUPS file */
/*
* 'cupsFileTell()' - Return the current file position.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
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);
}
/*
* 'cupsFileUnlock()' - Unlock access to a file.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
int /* O - 0 on success, -1 on error */
* Range check...
*/
- DEBUG_printf(("cupsFileUnlock(fp=%p)", fp));
+ DEBUG_printf(("cupsFileUnlock(fp=%p)", (void *)fp));
if (!fp || fp->mode == 's')
return (-1);
/*
* 'cupsFileWrite()' - Write to a file.
*
- * @since CUPS 1.2/OS X 10.5@
+ * @since CUPS 1.2/macOS 10.5@
*/
ssize_t /* O - Number of bytes written or -1 on error */
* 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);
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...
#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;
DEBUG_printf(("9cups_fill: cups_read() returned " CUPS_LLFMT,
CUPS_LLCAST bytes));
+ fp->eof = 1;
+
return (-1);
}
* Can't read from file!
*/
+ DEBUG_puts("9cups_fill: Extra gzip header data missing, returning -1.");
+
+ fp->eof = 1;
+ errno = EIO;
+
return (-1);
}
* 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);
}
}
* 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);
}
}
* 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);
}
}
* 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);
}
}
*/
if ((bytes = end - ptr) > 0)
- memcpy(fp->cbuf, ptr, bytes);
+ memcpy(fp->cbuf, ptr, (size_t)bytes);
/*
* Setup the decompressor data...
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;
}
*/
if (fp->eof)
- return (-1);
+ {
+ DEBUG_puts("9cups_fill: EOF, returning 0.");
+
+ return (0);
+ }
/*
* Fill the decompression buffer as needed...
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 = (uInt)bytes;
unsigned char trailer[8]; /* Trailer bytes */
uLong tcrc; /* Trailer CRC */
+ ssize_t tbytes = 0; /* Number of bytes */
-
- if (read(fp->fd, trailer, sizeof(trailer)) < (ssize_t)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));
+
+ fp->eof = 1;
+ errno = EIO;
+
+ return (-1);
}
bytes = (ssize_t)sizeof(fp->buf) - (ssize_t)fp->stream.avail_out;
fp->end = fp->buf + bytes;
if (bytes)
+ {
+ DEBUG_printf(("9cups_fill: Returning %d.", (int)bytes));
return (bytes);
+ }
}
}
#endif /* HAVE_LIBZ */
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);
}
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...
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...
return ((ssize_t)total);
}
-
-
-/*
- * End of "$Id$".
- */