/*
- * "$Id: file.c 7672 2008-06-18 22:03:02Z mike $"
+ * "$Id$"
*
- * File functions for 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 2007-2011 by Apple Inc.
- * Copyright 1997-2007 by Easy Software Products, all rights reserved.
+ * Copyright 2007-2014 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/".
+ * These coded instructions, statements, and computer programs are the
+ * property of Apple Inc. and are protected by Federal copyright
+ * law. Distribution and use rights are outlined in the file "LICENSE.txt"
+ * which should have been included with this file. If this file is
+ * file is missing or damaged, see the license at "http://www.cups.org/".
*
- * Contents:
- *
- * _cupsFileCheck() - Check the permissions of the given filename.
- * _cupsFileCheckFilter() - Report file check results as CUPS filter messages.
- * 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.
- * cupsFilePutConf() - Write a configuration line.
- * cupsFilePuts() - Write a string.
- * cupsFileRead() - Read from a file.
- * cupsFileRewind() - Set the current file position to the beginning of
- * the 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_open() - Safely open a file for writing.
- * 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.
*/
/*
switch (result)
{
- default :
case _CUPS_FILE_CHECK_OK :
prefix = "DEBUG2";
break;
/*
* 'cupsFileClose()' - Close a CUPS file.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
int /* O - 0 on success, -1 on error */
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;
* 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;
if (mode == 's')
{
- if (closesocket(fd) < 0)
+ if (httpAddrClose(NULL, fd) < 0)
status = -1;
}
else if (!is_stdio)
/*
* 'cupsFileCompression()' - Return whether a file is compressed.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
int /* O - @code CUPS_FILE_NONE@ or @code CUPS_FILE_GZIP@ */
/*
* 'cupsFileEOF()' - Return the end-of-file status.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 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/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
const char * /* O - Full path to file or @code NULL@ if not found */
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/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
int /* O - 0 on success, -1 on error */
{
#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);
/*
* 'cupsFileGetChar()' - Get a single character from a file.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
int /* O - Character or -1 on end of file */
/*
* 'cupsFileGetConf()' - Get a line from a configuration file.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
char * /* O - Line read or @code NULL@ on end of file or error */
* nul-terminated, however you should use the returned length to determine
* the number of bytes on the line.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
size_t /* O - Number of bytes on line or 0 on end of file */
DEBUG_printf(("4cupsFileGetLine: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
- return (ptr - buf);
+ return ((size_t)(ptr - buf));
}
/*
* 'cupsFileGets()' - Get a CR and/or LF-terminated line.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
char * /* O - Line read or @code NULL@ on end of file or error */
break;
}
else
- *ptr++ = ch;
+ *ptr++ = (char)ch;
}
*ptr = '\0';
/*
* 'cupsFileLock()' - Temporarily lock access to a file.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
int /* O - 0 on success, -1 on error */
/*
* 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
int /* O - File descriptor */
* connection as needed, generally preferring IPv6 connections when there is
* a choice.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
cups_file_t * /* O - CUPS file or @code NULL@ if the file or socket cannot be opened */
if ((fp = cupsFileOpenFd(fd, mode)) == NULL)
{
if (*mode == 's')
- closesocket(fd);
+ httpAddrClose(NULL, fd);
else
close(fd);
}
* supplied which enables Flate compression of the file. Compression is
* not supported for the "a" (append) mode.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
cups_file_t * /* O - CUPS file or @code NULL@ if the file could not be opened */
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;
/*
* 'cupsFilePeekChar()' - Peek at the next character from a file.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
int /* O - Character or -1 on end of file */
/*
* 'cupsFilePrintf()' - Write a formatted string.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
int /* O - Number of bytes written or -1 on error */
if (bytes > 65535)
return (-1);
- if ((temp = realloc(fp->printf_buffer, bytes + 1)) == NULL)
+ if ((temp = realloc(fp->printf_buffer, (size_t)(bytes + 1))) == NULL)
return (-1);
fp->printf_buffer = temp;
- fp->printf_size = bytes + 1;
+ fp->printf_size = (size_t)(bytes + 1);
va_start(ap, format);
bytes = vsnprintf(fp->printf_buffer, fp->printf_size, format, ap);
if (fp->mode == 's')
{
- if (cups_write(fp, fp->printf_buffer, bytes) < 0)
+ if (cups_write(fp, fp->printf_buffer, (size_t)bytes) < 0)
return (-1);
fp->pos += bytes;
DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
- return (bytes);
+ return ((int)bytes);
}
if ((fp->ptr + bytes) > fp->end)
DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
- if (bytes > sizeof(fp->buf))
+ if ((size_t)bytes > sizeof(fp->buf))
{
#ifdef HAVE_LIBZ
if (fp->compressed)
- return (cups_compress(fp, fp->printf_buffer, bytes));
+ return ((int)cups_compress(fp, fp->printf_buffer, (size_t)bytes));
else
#endif /* HAVE_LIBZ */
- return (cups_write(fp, fp->printf_buffer, bytes));
+ return ((int)cups_write(fp, fp->printf_buffer, (size_t)bytes));
}
else
{
- memcpy(fp->ptr, fp->printf_buffer, bytes);
+ memcpy(fp->ptr, fp->printf_buffer, (size_t)bytes);
fp->ptr += bytes;
- return (bytes);
+ return ((int)bytes);
}
}
/*
* 'cupsFilePutChar()' - Write a character.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
int /* O - 0 on success, -1 on error */
char ch; /* Output character */
- ch = c;
+ ch = (char)c;
if (send(fp->fd, &ch, 1, 0) < 1)
return (-1);
if (cupsFileFlush(fp))
return (-1);
- *(fp->ptr) ++ = c;
+ *(fp->ptr) ++ = (char)c;
}
fp->pos ++;
*
* This function handles any comment escaping of the value.
*
- * @since CUPS 1.4/Mac OS X 10.6@
+ * @since CUPS 1.4/OS X 10.6@
*/
ssize_t /* O - Number of bytes written or -1 on error */
* Need to quote the first # in the info string...
*/
- if ((temp = cupsFileWrite(fp, value, ptr - value)) < 0)
+ if ((temp = cupsFileWrite(fp, value, (size_t)(ptr - value))) < 0)
return (-1);
bytes += temp;
*
* Like the @code fputs@ function, no newline is appended to the string.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
int /* O - Number of bytes written or -1 on error */
* Write the string...
*/
- bytes = (int)strlen(s);
+ bytes = (ssize_t)strlen(s);
if (fp->mode == 's')
{
- if (cups_write(fp, s, bytes) < 0)
+ if (cups_write(fp, s, (size_t)bytes) < 0)
return (-1);
fp->pos += bytes;
DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
- return (bytes);
+ return ((int)bytes);
}
if ((fp->ptr + bytes) > fp->end)
DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos));
- if (bytes > sizeof(fp->buf))
+ if ((size_t)bytes > sizeof(fp->buf))
{
#ifdef HAVE_LIBZ
if (fp->compressed)
- return (cups_compress(fp, s, bytes));
+ return ((int)cups_compress(fp, s, (size_t)bytes));
else
#endif /* HAVE_LIBZ */
- return (cups_write(fp, s, bytes));
+ return ((int)cups_write(fp, s, (size_t)bytes));
}
else
{
- memcpy(fp->ptr, s, bytes);
+ memcpy(fp->ptr, s, (size_t)bytes);
fp->ptr += bytes;
- return (bytes);
+ return ((int)bytes);
}
}
/*
* 'cupsFileRead()' - Read from a file.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
ssize_t /* O - Number of bytes read or -1 on error */
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;
* Update the counts for the last read...
*/
- bytes -= count;
- total += count;
+ bytes -= (size_t)count;
+ total += (size_t)count;
buf += count;
}
* 'cupsFileRewind()' - Set the current file position to the beginning of the
* file.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
off_t /* O - New file position or -1 on error */
/*
* 'cupsFileSeek()' - Seek in a file.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
off_t /* O - New file position or -1 on error */
/*
* 'cupsFileStderr()' - Return a CUPS file associated with stderr.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
cups_file_t * /* O - CUPS file */
/*
* 'cupsFileStdin()' - Return a CUPS file associated with stdin.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
cups_file_t * /* O - CUPS file */
/*
* 'cupsFileStdout()' - Return a CUPS file associated with stdout.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
cups_file_t * /* O - CUPS file */
/*
* 'cupsFileTell()' - Return the current file position.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
off_t /* O - File position */
/*
* 'cupsFileUnlock()' - Unlock access to a file.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
int /* O - 0 on success, -1 on error */
/*
* 'cupsFileWrite()' - Write to a file.
*
- * @since CUPS 1.2/Mac OS X 10.5@
+ * @since CUPS 1.2/OS X 10.5@
*/
ssize_t /* O - Number of bytes written or -1 on error */
* 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)
{
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;
deflate(&(fp->stream), Z_NO_FLUSH);
}
- return (bytes);
+ return ((ssize_t)bytes);
}
#endif /* HAVE_LIBZ */
*/
if ((bytes = end - ptr) > 0)
- memcpy(fp->cbuf, ptr, bytes);
+ memcpy(fp->cbuf, ptr, (size_t)bytes);
/*
* Setup the decompressor data...
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);
return (-1);
fp->stream.next_in = fp->cbuf;
- fp->stream.avail_in = bytes;
+ fp->stream.avail_in = (uInt)bytes;
}
/*
if (fp->stream.next_out > (Bytef *)fp->buf)
fp->crc = crc32(fp->crc, (Bytef *)fp->buf,
- fp->stream.next_out - (Bytef *)fp->buf);
+ (uInt)(fp->stream.next_out - (Bytef *)fp->buf));
if (status == Z_STREAM_END)
{
uLong tcrc; /* Trailer CRC */
- if (read(fp->fd, trailer, sizeof(trailer)) < sizeof(trailer))
+ if (read(fp->fd, trailer, sizeof(trailer)) < (ssize_t)sizeof(trailer))
{
/*
* Can't get it, so mark end-of-file...
}
else
{
- tcrc = (((((trailer[3] << 8) | trailer[2]) << 8) | trailer[1]) << 8) |
- trailer[0];
+ tcrc = ((((((uLong)trailer[3] << 8) | (uLong)trailer[2]) << 8) |
+ (uLong)trailer[1]) << 8) | (uLong)trailer[0];
if (tcrc != fp->crc)
{
* Bad CRC, mark end-of-file...
*/
- DEBUG_printf(("9cups_fill: tcrc=%08x, fp->crc=%08x",
+ DEBUG_printf(("9cups_fill: tcrc=%08x != fp->crc=%08x",
(unsigned int)tcrc, (unsigned int)fp->crc));
fp->eof = 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...
* Update the counts for the last write call...
*/
- bytes -= count;
- total += count;
+ bytes -= (size_t)count;
+ total += (size_t)count;
buf += count;
}
/*
- * End of "$Id: file.c 7672 2008-06-18 22:03:02Z mike $".
+ * End of "$Id$".
*/