4 * File functions for CUPS.
6 * Since stdio files max out at 256 files on many systems, we have to
7 * write similar functions without this limit. At the same time, using
8 * our own file functions allows us to provide transparent support of
9 * gzip'd print files, PPD files, etc.
11 * Copyright 2007-2015 by Apple Inc.
12 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
14 * These coded instructions, statements, and computer programs are the
15 * property of Apple Inc. and are protected by Federal copyright
16 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
17 * which should have been included with this file. If this file is
18 * file is missing or damaged, see the license at "http://www.cups.org/".
20 * This file is subject to the Apple OS-Developed Software exception.
24 * Include necessary headers...
27 #include "file-private.h"
29 #include <sys/types.h>
37 static ssize_t
cups_compress(cups_file_t
*fp
, const char *buf
, size_t bytes
);
38 #endif /* HAVE_LIBZ */
39 static ssize_t
cups_fill(cups_file_t
*fp
);
40 static int cups_open(const char *filename
, int mode
);
41 static ssize_t
cups_read(cups_file_t
*fp
, char *buf
, size_t bytes
);
42 static ssize_t
cups_write(cups_file_t
*fp
, const char *buf
, size_t bytes
);
47 * '_cupsFileCheck()' - Check the permissions of the given filename.
50 _cups_fc_result_t
/* O - Check result */
52 const char *filename
, /* I - Filename to check */
53 _cups_fc_filetype_t filetype
, /* I - Type of file checks? */
54 int dorootchecks
, /* I - Check for root permissions? */
55 _cups_fc_func_t cb
, /* I - Callback function */
56 void *context
) /* I - Context pointer for callback */
59 struct stat fileinfo
; /* File information */
60 char message
[1024], /* Message string */
61 temp
[1024], /* Parent directory filename */
62 *ptr
; /* Pointer into parent directory */
63 _cups_fc_result_t result
; /* Check result */
67 * Does the filename contain a relative path ("../")?
70 if (strstr(filename
, "../"))
76 result
= _CUPS_FILE_CHECK_RELATIVE_PATH
;
81 * Does the program even exist and is it accessible?
84 if (stat(filename
, &fileinfo
))
90 result
= _CUPS_FILE_CHECK_MISSING
;
95 * Check the execute bit...
98 result
= _CUPS_FILE_CHECK_OK
;
102 case _CUPS_FILE_CHECK_DIRECTORY
:
103 if (!S_ISDIR(fileinfo
.st_mode
))
104 result
= _CUPS_FILE_CHECK_WRONG_TYPE
;
108 if (!S_ISREG(fileinfo
.st_mode
))
109 result
= _CUPS_FILE_CHECK_WRONG_TYPE
;
117 * Are we doing root checks?
123 * Nope, so anything (else) goes...
130 * Verify permission of the file itself:
132 * 1. Must be owned by root
133 * 2. Must not be writable by group
134 * 3. Must not be setuid
135 * 4. Must not be writable by others
138 if (fileinfo
.st_uid
|| /* 1. Must be owned by root */
139 (fileinfo
.st_mode
& S_IWGRP
) || /* 2. Must not be writable by group */
140 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
141 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
143 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
147 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
||
148 filetype
== _CUPS_FILE_CHECK_FILE_ONLY
)
152 * Now check the containing directory...
155 strlcpy(temp
, filename
, sizeof(temp
));
156 if ((ptr
= strrchr(temp
, '/')) != NULL
)
164 if (stat(temp
, &fileinfo
))
170 result
= _CUPS_FILE_CHECK_MISSING
;
171 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
177 if (fileinfo
.st_uid
|| /* 1. Must be owned by root */
178 (fileinfo
.st_mode
& S_IWGRP
) || /* 2. Must not be writable by group */
179 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
180 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
182 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
183 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
188 * Common return point...
195 cups_lang_t
*lang
= cupsLangDefault();
196 /* Localization information */
200 case _CUPS_FILE_CHECK_OK
:
201 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
202 snprintf(message
, sizeof(message
),
203 _cupsLangString(lang
, _("Directory \"%s\" permissions OK "
204 "(0%o/uid=%d/gid=%d).")),
205 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
206 (int)fileinfo
.st_gid
);
208 snprintf(message
, sizeof(message
),
209 _cupsLangString(lang
, _("File \"%s\" permissions OK "
210 "(0%o/uid=%d/gid=%d).")),
211 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
212 (int)fileinfo
.st_gid
);
215 case _CUPS_FILE_CHECK_MISSING
:
216 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
217 snprintf(message
, sizeof(message
),
218 _cupsLangString(lang
, _("Directory \"%s\" not available: "
220 filename
, strerror(errno
));
222 snprintf(message
, sizeof(message
),
223 _cupsLangString(lang
, _("File \"%s\" not available: %s")),
224 filename
, strerror(errno
));
227 case _CUPS_FILE_CHECK_PERMISSIONS
:
228 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
229 snprintf(message
, sizeof(message
),
230 _cupsLangString(lang
, _("Directory \"%s\" has insecure "
232 "(0%o/uid=%d/gid=%d).")),
233 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
234 (int)fileinfo
.st_gid
);
236 snprintf(message
, sizeof(message
),
237 _cupsLangString(lang
, _("File \"%s\" has insecure "
239 "(0%o/uid=%d/gid=%d).")),
240 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
241 (int)fileinfo
.st_gid
);
244 case _CUPS_FILE_CHECK_WRONG_TYPE
:
245 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
246 snprintf(message
, sizeof(message
),
247 _cupsLangString(lang
, _("Directory \"%s\" is a file.")),
250 snprintf(message
, sizeof(message
),
251 _cupsLangString(lang
, _("File \"%s\" is a directory.")),
255 case _CUPS_FILE_CHECK_RELATIVE_PATH
:
256 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
257 snprintf(message
, sizeof(message
),
258 _cupsLangString(lang
, _("Directory \"%s\" contains a "
259 "relative path.")), filename
);
261 snprintf(message
, sizeof(message
),
262 _cupsLangString(lang
, _("File \"%s\" contains a relative "
263 "path.")), filename
);
267 (*cb
)(context
, result
, message
);
275 * '_cupsFileCheckFilter()' - Report file check results as CUPS filter messages.
279 _cupsFileCheckFilter(
280 void *context
, /* I - Context pointer (unused) */
281 _cups_fc_result_t result
, /* I - Result code */
282 const char *message
) /* I - Message text */
284 const char *prefix
; /* Messaging prefix */
292 case _CUPS_FILE_CHECK_OK
:
296 case _CUPS_FILE_CHECK_MISSING
:
297 case _CUPS_FILE_CHECK_WRONG_TYPE
:
299 fputs("STATE: +cups-missing-filter-warning\n", stderr
);
302 case _CUPS_FILE_CHECK_PERMISSIONS
:
303 case _CUPS_FILE_CHECK_RELATIVE_PATH
:
305 fputs("STATE: +cups-insecure-filter-warning\n", stderr
);
309 fprintf(stderr
, "%s: %s\n", prefix
, message
);
315 * 'cupsFileClose()' - Close a CUPS file.
317 * @since CUPS 1.2/OS X 10.5@
320 int /* O - 0 on success, -1 on error */
321 cupsFileClose(cups_file_t
*fp
) /* I - CUPS file */
323 int fd
; /* File descriptor */
324 char mode
; /* Open mode */
325 int status
; /* Return status */
326 int is_stdio
; /* Is a stdio file? */
329 DEBUG_printf(("cupsFileClose(fp=%p)", fp
));
339 * Flush pending write data...
343 status
= cupsFileFlush(fp
);
348 if (fp
->compressed
&& status
>= 0)
353 * Free decompression data...
356 inflateEnd(&fp
->stream
);
361 * Flush any remaining compressed data...
364 unsigned char trailer
[8]; /* Trailer CRC and length */
365 int done
; /* Done writing... */
368 fp
->stream
.avail_in
= 0;
372 if (fp
->stream
.next_out
> fp
->cbuf
)
374 if (cups_write(fp
, (char *)fp
->cbuf
,
375 (size_t)(fp
->stream
.next_out
- fp
->cbuf
)) < 0)
378 fp
->stream
.next_out
= fp
->cbuf
;
379 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
382 if (done
|| status
< 0)
385 done
= deflate(&fp
->stream
, Z_FINISH
) == Z_STREAM_END
&&
386 fp
->stream
.next_out
== fp
->cbuf
;
390 * Write the CRC and length...
393 trailer
[0] = (unsigned char)fp
->crc
;
394 trailer
[1] = (unsigned char)(fp
->crc
>> 8);
395 trailer
[2] = (unsigned char)(fp
->crc
>> 16);
396 trailer
[3] = (unsigned char)(fp
->crc
>> 24);
397 trailer
[4] = (unsigned char)fp
->pos
;
398 trailer
[5] = (unsigned char)(fp
->pos
>> 8);
399 trailer
[6] = (unsigned char)(fp
->pos
>> 16);
400 trailer
[7] = (unsigned char)(fp
->pos
>> 24);
402 if (cups_write(fp
, (char *)trailer
, 8) < 0)
406 * Free all memory used by the compression stream...
409 deflateEnd(&(fp
->stream
));
412 #endif /* HAVE_LIBZ */
415 * Save the file descriptor we used and free memory...
420 is_stdio
= fp
->is_stdio
;
422 if (fp
->printf_buffer
)
423 free(fp
->printf_buffer
);
428 * Close the file, returning the close status...
433 if (httpAddrClose(NULL
, fd
) < 0)
447 * 'cupsFileCompression()' - Return whether a file is compressed.
449 * @since CUPS 1.2/OS X 10.5@
452 int /* O - @code CUPS_FILE_NONE@ or @code CUPS_FILE_GZIP@ */
453 cupsFileCompression(cups_file_t
*fp
) /* I - CUPS file */
455 return (fp
? fp
->compressed
: CUPS_FILE_NONE
);
460 * 'cupsFileEOF()' - Return the end-of-file status.
462 * @since CUPS 1.2/OS X 10.5@
465 int /* O - 1 on end of file, 0 otherwise */
466 cupsFileEOF(cups_file_t
*fp
) /* I - CUPS file */
468 return (fp
? fp
->eof
: 1);
473 * 'cupsFileFind()' - Find a file using the specified path.
475 * This function allows the paths in the path string to be separated by
476 * colons (UNIX standard) or semicolons (Windows standard) and stores the
477 * result in the buffer supplied. If the file cannot be found in any of
478 * the supplied paths, @code NULL@ is returned. A @code NULL@ path only
479 * matches the current directory.
481 * @since CUPS 1.2/OS X 10.5@
484 const char * /* O - Full path to file or @code NULL@ if not found */
485 cupsFileFind(const char *filename
, /* I - File to find */
486 const char *path
, /* I - Colon/semicolon-separated path */
487 int executable
, /* I - 1 = executable files, 0 = any file/dir */
488 char *buffer
, /* I - Filename buffer */
489 int bufsize
) /* I - Size of filename buffer */
491 char *bufptr
, /* Current position in buffer */
492 *bufend
; /* End of buffer */
496 * Range check input...
499 DEBUG_printf(("cupsFileFind(filename=\"%s\", path=\"%s\", executable=%d, "
500 "buffer=%p, bufsize=%d)", filename
, path
, executable
, buffer
,
503 if (!filename
|| !buffer
|| bufsize
< 2)
509 * No path, so check current directory...
512 if (!access(filename
, 0))
514 strlcpy(buffer
, filename
, (size_t)bufsize
);
522 * Now check each path and return the first match...
525 bufend
= buffer
+ bufsize
- 1;
531 if (*path
== ';' || (*path
== ':' && ((bufptr
- buffer
) > 1 || !isalpha(buffer
[0] & 255))))
533 if (*path
== ';' || *path
== ':')
536 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
539 strlcpy(bufptr
, filename
, (size_t)(bufend
- bufptr
));
542 if (!access(buffer
, 0))
544 if (!access(buffer
, executable
? X_OK
: 0))
547 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
553 else if (bufptr
< bufend
)
560 * Check the last path...
563 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
566 strlcpy(bufptr
, filename
, (size_t)(bufend
- bufptr
));
568 if (!access(buffer
, 0))
570 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
575 DEBUG_puts("1cupsFileFind: Returning NULL");
582 * 'cupsFileFlush()' - Flush pending output.
584 * @since CUPS 1.2/OS X 10.5@
587 int /* O - 0 on success, -1 on error */
588 cupsFileFlush(cups_file_t
*fp
) /* I - CUPS file */
590 ssize_t bytes
; /* Bytes to write */
593 DEBUG_printf(("cupsFileFlush(fp=%p)", fp
));
596 * Range check input...
599 if (!fp
|| fp
->mode
!= 'w')
601 DEBUG_puts("1cupsFileFlush: Attempt to flush a read-only file...");
605 bytes
= (ssize_t
)(fp
->ptr
- fp
->buf
);
607 DEBUG_printf(("2cupsFileFlush: Flushing " CUPS_LLFMT
" bytes...",
614 bytes
= cups_compress(fp
, fp
->buf
, (size_t)bytes
);
616 #endif /* HAVE_LIBZ */
617 bytes
= cups_write(fp
, fp
->buf
, (size_t)bytes
);
630 * 'cupsFileGetChar()' - Get a single character from a file.
632 * @since CUPS 1.2/OS X 10.5@
635 int /* O - Character or -1 on end of file */
636 cupsFileGetChar(cups_file_t
*fp
) /* I - CUPS file */
639 * Range check input...
642 DEBUG_printf(("4cupsFileGetChar(fp=%p)", fp
));
644 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
646 DEBUG_puts("5cupsFileGetChar: Bad arguments!");
651 * If the input buffer is empty, try to read more data...
654 DEBUG_printf(("5cupsFileGetChar: fp->eof=%d, fp->ptr=%p, fp->end=%p", fp
->eof
, fp
->ptr
, fp
->end
));
656 if (fp
->ptr
>= fp
->end
)
657 if (cups_fill(fp
) <= 0)
659 DEBUG_puts("5cupsFileGetChar: Unable to fill buffer!");
664 * Return the next character in the buffer...
667 DEBUG_printf(("5cupsFileGetChar: Returning %d...", *(fp
->ptr
) & 255));
671 DEBUG_printf(("6cupsFileGetChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
673 return (*(fp
->ptr
)++ & 255);
678 * 'cupsFileGetConf()' - Get a line from a configuration file.
680 * @since CUPS 1.2/OS X 10.5@
683 char * /* O - Line read or @code NULL@ on end of file or error */
684 cupsFileGetConf(cups_file_t
*fp
, /* I - CUPS file */
685 char *buf
, /* O - String buffer */
686 size_t buflen
, /* I - Size of string buffer */
687 char **value
, /* O - Pointer to value */
688 int *linenum
) /* IO - Current line number */
690 char *ptr
; /* Pointer into line */
694 * Range check input...
697 DEBUG_printf(("2cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT
698 ", value=%p, linenum=%p)", fp
, buf
, CUPS_LLCAST buflen
,
701 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') ||
702 !buf
|| buflen
< 2 || !value
)
711 * Read the next non-comment line...
716 while (cupsFileGets(fp
, buf
, buflen
))
721 * Strip any comments...
724 if ((ptr
= strchr(buf
, '#')) != NULL
)
726 if (ptr
> buf
&& ptr
[-1] == '\\')
729 _cups_strcpy(ptr
- 1, ptr
);
733 // Strip the comment and any trailing whitespace...
736 if (!_cups_isspace(ptr
[-1]))
747 * Strip leading whitespace...
750 for (ptr
= buf
; _cups_isspace(*ptr
); ptr
++);
753 _cups_strcpy(buf
, ptr
);
756 * See if there is anything left...
762 * Yes, grab any value and return...
765 for (ptr
= buf
; *ptr
; ptr
++)
766 if (_cups_isspace(*ptr
))
772 * Have a value, skip any other spaces...
775 while (_cups_isspace(*ptr
))
782 * Strip trailing whitespace and > for lines that begin with <...
785 ptr
+= strlen(ptr
) - 1;
787 if (buf
[0] == '<' && *ptr
== '>')
789 else if (buf
[0] == '<' && *ptr
!= '>')
799 while (ptr
> *value
&& _cups_isspace(*ptr
))
816 * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may
817 * contain binary data.
819 * This function differs from @link cupsFileGets@ in that the trailing CR
820 * and LF are preserved, as is any binary data on the line. The buffer is
821 * nul-terminated, however you should use the returned length to determine
822 * the number of bytes on the line.
824 * @since CUPS 1.2/OS X 10.5@
827 size_t /* O - Number of bytes on line or 0 on end of file */
828 cupsFileGetLine(cups_file_t
*fp
, /* I - File to read from */
829 char *buf
, /* I - Buffer */
830 size_t buflen
) /* I - Size of buffer */
832 int ch
; /* Character from file */
833 char *ptr
, /* Current position in line buffer */
834 *end
; /* End of line buffer */
838 * Range check input...
841 DEBUG_printf(("2cupsFileGetLine(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")",
842 fp
, buf
, CUPS_LLCAST buflen
));
844 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 3)
848 * Now loop until we have a valid line...
851 for (ptr
= buf
, end
= buf
+ buflen
- 2; ptr
< end
;)
853 if (fp
->ptr
>= fp
->end
)
854 if (cups_fill(fp
) <= 0)
857 *ptr
++ = ch
= *(fp
->ptr
)++;
866 if (fp
->ptr
>= fp
->end
)
867 if (cups_fill(fp
) <= 0)
870 if (*(fp
->ptr
) == '\n')
872 *ptr
++ = *(fp
->ptr
)++;
881 * Line feed ends a line...
890 DEBUG_printf(("4cupsFileGetLine: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
892 return ((size_t)(ptr
- buf
));
897 * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
899 * @since CUPS 1.2/OS X 10.5@
902 char * /* O - Line read or @code NULL@ on end of file or error */
903 cupsFileGets(cups_file_t
*fp
, /* I - CUPS file */
904 char *buf
, /* O - String buffer */
905 size_t buflen
) /* I - Size of string buffer */
907 int ch
; /* Character from file */
908 char *ptr
, /* Current position in line buffer */
909 *end
; /* End of line buffer */
913 * Range check input...
916 DEBUG_printf(("2cupsFileGets(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")", fp
, buf
,
917 CUPS_LLCAST buflen
));
919 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 2)
923 * Now loop until we have a valid line...
926 for (ptr
= buf
, end
= buf
+ buflen
- 1; ptr
< end
;)
928 if (fp
->ptr
>= fp
->end
)
929 if (cups_fill(fp
) <= 0)
946 if (fp
->ptr
>= fp
->end
)
947 if (cups_fill(fp
) <= 0)
950 if (*(fp
->ptr
) == '\n')
961 * Line feed ends a line...
972 DEBUG_printf(("4cupsFileGets: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
979 * 'cupsFileLock()' - Temporarily lock access to a file.
981 * @since CUPS 1.2/OS X 10.5@
984 int /* O - 0 on success, -1 on error */
985 cupsFileLock(cups_file_t
*fp
, /* I - CUPS file */
986 int block
) /* I - 1 to wait for the lock, 0 to fail right away */
992 if (!fp
|| fp
->mode
== 's')
1000 return (_locking(fp
->fd
, block
? _LK_LOCK
: _LK_NBLCK
, 0));
1002 return (lockf(fp
->fd
, block
? F_LOCK
: F_TLOCK
, 0));
1008 * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
1010 * @since CUPS 1.2/OS X 10.5@
1013 int /* O - File descriptor */
1014 cupsFileNumber(cups_file_t
*fp
) /* I - CUPS file */
1024 * 'cupsFileOpen()' - Open a CUPS file.
1026 * The "mode" parameter can be "r" to read, "w" to write, overwriting any
1027 * existing file, "a" to append to an existing file or create a new file,
1028 * or "s" to open a socket connection.
1030 * When opening for writing ("w"), an optional number from 1 to 9 can be
1031 * supplied which enables Flate compression of the file. Compression is
1032 * not supported for the "a" (append) mode.
1034 * When opening a socket connection, the filename is a string of the form
1035 * "address:port" or "hostname:port". The socket will make an IPv4 or IPv6
1036 * connection as needed, generally preferring IPv6 connections when there is
1039 * @since CUPS 1.2/OS X 10.5@
1042 cups_file_t
* /* O - CUPS file or @code NULL@ if the file or socket cannot be opened */
1043 cupsFileOpen(const char *filename
, /* I - Name of file */
1044 const char *mode
) /* I - Open mode */
1046 cups_file_t
*fp
; /* New CUPS file */
1047 int fd
; /* File descriptor */
1048 char hostname
[1024], /* Hostname */
1049 *portname
; /* Port "name" (number or service) */
1050 http_addrlist_t
*addrlist
; /* Host address list */
1053 DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")", filename
,
1057 * Range check input...
1060 if (!filename
|| !mode
||
1061 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1062 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1071 case 'a' : /* Append file */
1072 fd
= cups_open(filename
,
1073 O_RDWR
| O_CREAT
| O_APPEND
| O_LARGEFILE
| O_BINARY
);
1076 case 'r' : /* Read file */
1077 fd
= open(filename
, O_RDONLY
| O_LARGEFILE
| O_BINARY
, 0);
1080 case 'w' : /* Write file */
1081 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1082 if (fd
< 0 && errno
== ENOENT
)
1084 fd
= cups_open(filename
,
1085 O_WRONLY
| O_CREAT
| O_EXCL
| O_LARGEFILE
| O_BINARY
);
1086 if (fd
< 0 && errno
== EEXIST
)
1087 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1098 case 's' : /* Read/write socket */
1099 strlcpy(hostname
, filename
, sizeof(hostname
));
1100 if ((portname
= strrchr(hostname
, ':')) != NULL
)
1106 * Lookup the hostname and service...
1109 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
1113 * Connect to the server...
1116 if (!httpAddrConnect(addrlist
, &fd
))
1118 httpAddrFreeList(addrlist
);
1122 httpAddrFreeList(addrlist
);
1125 default : /* Remove bogus compiler warning... */
1133 * Create the CUPS file structure...
1136 if ((fp
= cupsFileOpenFd(fd
, mode
)) == NULL
)
1139 httpAddrClose(NULL
, fd
);
1152 * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
1154 * The "mode" parameter can be "r" to read, "w" to write, "a" to append,
1155 * or "s" to treat the file descriptor as a bidirectional socket connection.
1157 * When opening for writing ("w"), an optional number from 1 to 9 can be
1158 * supplied which enables Flate compression of the file. Compression is
1159 * not supported for the "a" (append) mode.
1161 * @since CUPS 1.2/OS X 10.5@
1164 cups_file_t
* /* O - CUPS file or @code NULL@ if the file could not be opened */
1165 cupsFileOpenFd(int fd
, /* I - File descriptor */
1166 const char *mode
) /* I - Open mode */
1168 cups_file_t
*fp
; /* New CUPS file */
1171 DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")", fd
, mode
));
1174 * Range check input...
1177 if (fd
< 0 || !mode
||
1178 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1179 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1183 * Allocate memory...
1186 if ((fp
= calloc(1, sizeof(cups_file_t
))) == NULL
)
1198 fp
->pos
= lseek(fd
, 0, SEEK_END
);
1203 fp
->end
= fp
->buf
+ sizeof(fp
->buf
);
1206 if (mode
[1] >= '1' && mode
[1] <= '9')
1209 * Open a compressed stream, so write the standard gzip file
1213 unsigned char header
[10]; /* gzip file header */
1214 time_t curtime
; /* Current time */
1217 curtime
= time(NULL
);
1220 header
[2] = Z_DEFLATED
;
1222 header
[4] = (unsigned char)curtime
;
1223 header
[5] = (unsigned char)(curtime
>> 8);
1224 header
[6] = (unsigned char)(curtime
>> 16);
1225 header
[7] = (unsigned char)(curtime
>> 24);
1229 cups_write(fp
, (char *)header
, 10);
1232 * Initialize the compressor...
1235 deflateInit2(&(fp
->stream
), mode
[1] - '0', Z_DEFLATED
, -15, 8,
1236 Z_DEFAULT_STRATEGY
);
1238 fp
->stream
.next_out
= fp
->cbuf
;
1239 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
1241 fp
->crc
= crc32(0L, Z_NULL
, 0);
1243 #endif /* HAVE_LIBZ */
1254 default : /* Remove bogus compiler warning... */
1259 * Don't pass this file to child processes...
1263 fcntl(fp
->fd
, F_SETFD
, fcntl(fp
->fd
, F_GETFD
) | FD_CLOEXEC
);
1271 * 'cupsFilePeekChar()' - Peek at the next character from a file.
1273 * @since CUPS 1.2/OS X 10.5@
1276 int /* O - Character or -1 on end of file */
1277 cupsFilePeekChar(cups_file_t
*fp
) /* I - CUPS file */
1280 * Range check input...
1283 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1287 * If the input buffer is empty, try to read more data...
1290 if (fp
->ptr
>= fp
->end
)
1291 if (cups_fill(fp
) <= 0)
1295 * Return the next character in the buffer...
1298 return (*(fp
->ptr
) & 255);
1303 * 'cupsFilePrintf()' - Write a formatted string.
1305 * @since CUPS 1.2/OS X 10.5@
1308 int /* O - Number of bytes written or -1 on error */
1309 cupsFilePrintf(cups_file_t
*fp
, /* I - CUPS file */
1310 const char *format
, /* I - Printf-style format string */
1311 ...) /* I - Additional args as necessary */
1313 va_list ap
; /* Argument list */
1314 ssize_t bytes
; /* Formatted size */
1317 DEBUG_printf(("2cupsFilePrintf(fp=%p, format=\"%s\", ...)", fp
, format
));
1319 if (!fp
|| !format
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1322 if (!fp
->printf_buffer
)
1325 * Start with an 1k printf buffer...
1328 if ((fp
->printf_buffer
= malloc(1024)) == NULL
)
1331 fp
->printf_size
= 1024;
1334 va_start(ap
, format
);
1335 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1338 if (bytes
>= (ssize_t
)fp
->printf_size
)
1341 * Expand the printf buffer...
1344 char *temp
; /* Temporary buffer pointer */
1350 if ((temp
= realloc(fp
->printf_buffer
, (size_t)(bytes
+ 1))) == NULL
)
1353 fp
->printf_buffer
= temp
;
1354 fp
->printf_size
= (size_t)(bytes
+ 1);
1356 va_start(ap
, format
);
1357 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1361 if (fp
->mode
== 's')
1363 if (cups_write(fp
, fp
->printf_buffer
, (size_t)bytes
) < 0)
1368 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1370 return ((int)bytes
);
1373 if ((fp
->ptr
+ bytes
) > fp
->end
)
1374 if (cupsFileFlush(fp
))
1379 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1381 if ((size_t)bytes
> sizeof(fp
->buf
))
1385 return ((int)cups_compress(fp
, fp
->printf_buffer
, (size_t)bytes
));
1387 #endif /* HAVE_LIBZ */
1388 return ((int)cups_write(fp
, fp
->printf_buffer
, (size_t)bytes
));
1392 memcpy(fp
->ptr
, fp
->printf_buffer
, (size_t)bytes
);
1394 return ((int)bytes
);
1400 * 'cupsFilePutChar()' - Write a character.
1402 * @since CUPS 1.2/OS X 10.5@
1405 int /* O - 0 on success, -1 on error */
1406 cupsFilePutChar(cups_file_t
*fp
, /* I - CUPS file */
1407 int c
) /* I - Character to write */
1410 * Range check input...
1413 if (!fp
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1416 if (fp
->mode
== 's')
1419 * Send character immediately over socket...
1422 char ch
; /* Output character */
1427 if (send(fp
->fd
, &ch
, 1, 0) < 1)
1436 if (fp
->ptr
>= fp
->end
)
1437 if (cupsFileFlush(fp
))
1440 *(fp
->ptr
) ++ = (char)c
;
1445 DEBUG_printf(("4cupsFilePutChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1452 * 'cupsFilePutConf()' - Write a configuration line.
1454 * This function handles any comment escaping of the value.
1456 * @since CUPS 1.4/OS X 10.6@
1459 ssize_t
/* O - Number of bytes written or -1 on error */
1460 cupsFilePutConf(cups_file_t
*fp
, /* I - CUPS file */
1461 const char *directive
, /* I - Directive */
1462 const char *value
) /* I - Value */
1464 ssize_t bytes
, /* Number of bytes written */
1465 temp
; /* Temporary byte count */
1466 const char *ptr
; /* Pointer into value */
1469 if (!fp
|| !directive
|| !*directive
)
1472 if ((bytes
= cupsFilePuts(fp
, directive
)) < 0)
1475 if (cupsFilePutChar(fp
, ' ') < 0)
1479 if (value
&& *value
)
1481 if ((ptr
= strchr(value
, '#')) != NULL
)
1484 * Need to quote the first # in the info string...
1487 if ((temp
= cupsFileWrite(fp
, value
, (size_t)(ptr
- value
))) < 0)
1491 if (cupsFilePutChar(fp
, '\\') < 0)
1495 if ((temp
= cupsFilePuts(fp
, ptr
)) < 0)
1499 else if ((temp
= cupsFilePuts(fp
, value
)) < 0)
1505 if (cupsFilePutChar(fp
, '\n') < 0)
1513 * 'cupsFilePuts()' - Write a string.
1515 * Like the @code fputs@ function, no newline is appended to the string.
1517 * @since CUPS 1.2/OS X 10.5@
1520 int /* O - Number of bytes written or -1 on error */
1521 cupsFilePuts(cups_file_t
*fp
, /* I - CUPS file */
1522 const char *s
) /* I - String to write */
1524 ssize_t bytes
; /* Bytes to write */
1528 * Range check input...
1531 if (!fp
|| !s
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1535 * Write the string...
1538 bytes
= (ssize_t
)strlen(s
);
1540 if (fp
->mode
== 's')
1542 if (cups_write(fp
, s
, (size_t)bytes
) < 0)
1547 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1549 return ((int)bytes
);
1552 if ((fp
->ptr
+ bytes
) > fp
->end
)
1553 if (cupsFileFlush(fp
))
1558 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1560 if ((size_t)bytes
> sizeof(fp
->buf
))
1564 return ((int)cups_compress(fp
, s
, (size_t)bytes
));
1566 #endif /* HAVE_LIBZ */
1567 return ((int)cups_write(fp
, s
, (size_t)bytes
));
1571 memcpy(fp
->ptr
, s
, (size_t)bytes
);
1573 return ((int)bytes
);
1579 * 'cupsFileRead()' - Read from a file.
1581 * @since CUPS 1.2/OS X 10.5@
1584 ssize_t
/* O - Number of bytes read or -1 on error */
1585 cupsFileRead(cups_file_t
*fp
, /* I - CUPS file */
1586 char *buf
, /* O - Buffer */
1587 size_t bytes
) /* I - Number of bytes to read */
1589 size_t total
; /* Total bytes read */
1590 ssize_t count
; /* Bytes read */
1593 DEBUG_printf(("2cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
1594 CUPS_LLCAST bytes
));
1597 * Range check input...
1600 if (!fp
|| !buf
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1607 * Loop until all bytes are read...
1613 if (fp
->ptr
>= fp
->end
)
1614 if (cups_fill(fp
) <= 0)
1616 DEBUG_printf(("4cupsFileRead: cups_fill() returned -1, total="
1617 CUPS_LLFMT
, CUPS_LLCAST total
));
1620 return ((ssize_t
)total
);
1625 count
= (ssize_t
)(fp
->end
- fp
->ptr
);
1626 if (count
> (ssize_t
)bytes
)
1627 count
= (ssize_t
)bytes
;
1629 memcpy(buf
, fp
->ptr
,(size_t) count
);
1633 DEBUG_printf(("4cupsFileRead: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1636 * Update the counts for the last read...
1639 bytes
-= (size_t)count
;
1640 total
+= (size_t)count
;
1645 * Return the total number of bytes read...
1648 DEBUG_printf(("3cupsFileRead: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
1650 return ((ssize_t
)total
);
1655 * 'cupsFileRewind()' - Set the current file position to the beginning of the
1658 * @since CUPS 1.2/OS X 10.5@
1661 off_t
/* O - New file position or -1 on error */
1662 cupsFileRewind(cups_file_t
*fp
) /* I - CUPS file */
1665 * Range check input...
1668 DEBUG_printf(("cupsFileRewind(fp=%p)", fp
));
1669 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1671 if (!fp
|| fp
->mode
!= 'r')
1675 * Handle special cases...
1678 if (fp
->bufpos
== 0)
1681 * No seeking necessary...
1692 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1698 * Otherwise, seek in the file and cleanup any compression buffers...
1704 inflateEnd(&fp
->stream
);
1707 #endif /* HAVE_LIBZ */
1709 if (lseek(fp
->fd
, 0, SEEK_SET
))
1711 DEBUG_printf(("1cupsFileRewind: lseek failed: %s", strerror(errno
)));
1721 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1728 * 'cupsFileSeek()' - Seek in a file.
1730 * @since CUPS 1.2/OS X 10.5@
1733 off_t
/* O - New file position or -1 on error */
1734 cupsFileSeek(cups_file_t
*fp
, /* I - CUPS file */
1735 off_t pos
) /* I - Position in file */
1737 ssize_t bytes
; /* Number bytes in buffer */
1740 DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT
")", fp
,
1742 DEBUG_printf(("2cupsFileSeek: fp->pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1743 DEBUG_printf(("2cupsFileSeek: fp->ptr=%p, fp->end=%p", fp
->ptr
, fp
->end
));
1746 * Range check input...
1749 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1753 * Handle special cases...
1757 return (cupsFileRewind(fp
));
1761 bytes
= (ssize_t
)(fp
->end
- fp
->buf
);
1763 DEBUG_printf(("2cupsFileSeek: bytes=" CUPS_LLFMT
, CUPS_LLCAST bytes
));
1765 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1768 * No seeking necessary...
1772 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1780 if (!fp
->compressed
&& !fp
->ptr
)
1783 * Preload a buffer to determine whether the file is compressed...
1786 if (cups_fill(fp
) <= 0)
1789 #endif /* HAVE_LIBZ */
1792 * Seek forwards or backwards...
1797 if (pos
< fp
->bufpos
)
1800 * Need to seek backwards...
1803 DEBUG_puts("2cupsFileSeek: SEEK BACKWARDS");
1808 inflateEnd(&fp
->stream
);
1810 lseek(fp
->fd
, 0, SEEK_SET
);
1816 while ((bytes
= cups_fill(fp
)) > 0)
1817 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1823 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1827 #endif /* HAVE_LIBZ */
1829 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1830 fp
->pos
= fp
->bufpos
;
1834 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1835 CUPS_LLCAST fp
->pos
));
1841 * Need to seek forwards...
1844 DEBUG_puts("2cupsFileSeek: SEEK FORWARDS");
1849 while ((bytes
= cups_fill(fp
)) > 0)
1851 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1858 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1862 #endif /* HAVE_LIBZ */
1864 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1865 fp
->pos
= fp
->bufpos
;
1869 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1870 CUPS_LLCAST fp
->pos
));
1874 DEBUG_printf(("2cupsFileSeek: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1881 * 'cupsFileStderr()' - Return a CUPS file associated with stderr.
1883 * @since CUPS 1.2/OS X 10.5@
1886 cups_file_t
* /* O - CUPS file */
1887 cupsFileStderr(void)
1889 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1893 * Open file descriptor 2 as needed...
1896 if (!cg
->stdio_files
[2])
1899 * Flush any pending output on the stdio file...
1905 * Open file descriptor 2...
1908 if ((cg
->stdio_files
[2] = cupsFileOpenFd(2, "w")) != NULL
)
1909 cg
->stdio_files
[2]->is_stdio
= 1;
1912 return (cg
->stdio_files
[2]);
1917 * 'cupsFileStdin()' - Return a CUPS file associated with stdin.
1919 * @since CUPS 1.2/OS X 10.5@
1922 cups_file_t
* /* O - CUPS file */
1925 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1929 * Open file descriptor 0 as needed...
1932 if (!cg
->stdio_files
[0])
1935 * Open file descriptor 0...
1938 if ((cg
->stdio_files
[0] = cupsFileOpenFd(0, "r")) != NULL
)
1939 cg
->stdio_files
[0]->is_stdio
= 1;
1942 return (cg
->stdio_files
[0]);
1947 * 'cupsFileStdout()' - Return a CUPS file associated with stdout.
1949 * @since CUPS 1.2/OS X 10.5@
1952 cups_file_t
* /* O - CUPS file */
1953 cupsFileStdout(void)
1955 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1959 * Open file descriptor 1 as needed...
1962 if (!cg
->stdio_files
[1])
1965 * Flush any pending output on the stdio file...
1971 * Open file descriptor 1...
1974 if ((cg
->stdio_files
[1] = cupsFileOpenFd(1, "w")) != NULL
)
1975 cg
->stdio_files
[1]->is_stdio
= 1;
1978 return (cg
->stdio_files
[1]);
1983 * 'cupsFileTell()' - Return the current file position.
1985 * @since CUPS 1.2/OS X 10.5@
1988 off_t
/* O - File position */
1989 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
1991 DEBUG_printf(("2cupsFileTell(fp=%p)", fp
));
1992 DEBUG_printf(("3cupsFileTell: pos=" CUPS_LLFMT
,
1993 CUPS_LLCAST (fp
? fp
->pos
: -1)));
1995 return (fp
? fp
->pos
: 0);
2000 * 'cupsFileUnlock()' - Unlock access to a file.
2002 * @since CUPS 1.2/OS X 10.5@
2005 int /* O - 0 on success, -1 on error */
2006 cupsFileUnlock(cups_file_t
*fp
) /* I - CUPS file */
2012 DEBUG_printf(("cupsFileUnlock(fp=%p)", fp
));
2014 if (!fp
|| fp
->mode
== 's')
2022 return (_locking(fp
->fd
, _LK_UNLCK
, 0));
2024 return (lockf(fp
->fd
, F_ULOCK
, 0));
2030 * 'cupsFileWrite()' - Write to a file.
2032 * @since CUPS 1.2/OS X 10.5@
2035 ssize_t
/* O - Number of bytes written or -1 on error */
2036 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
2037 const char *buf
, /* I - Buffer */
2038 size_t bytes
) /* I - Number of bytes to write */
2041 * Range check input...
2044 DEBUG_printf(("2cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")",
2045 fp
, buf
, CUPS_LLCAST bytes
));
2047 if (!fp
|| !buf
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
2054 * Write the buffer...
2057 if (fp
->mode
== 's')
2059 if (cups_write(fp
, buf
, bytes
) < 0)
2062 fp
->pos
+= (off_t
)bytes
;
2064 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2066 return ((ssize_t
)bytes
);
2069 if ((fp
->ptr
+ bytes
) > fp
->end
)
2070 if (cupsFileFlush(fp
))
2073 fp
->pos
+= (off_t
)bytes
;
2075 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2077 if (bytes
> sizeof(fp
->buf
))
2081 return (cups_compress(fp
, buf
, bytes
));
2083 #endif /* HAVE_LIBZ */
2084 return (cups_write(fp
, buf
, bytes
));
2088 memcpy(fp
->ptr
, buf
, bytes
);
2090 return ((ssize_t
)bytes
);
2097 * 'cups_compress()' - Compress a buffer of data.
2100 static ssize_t
/* O - Number of bytes written or -1 */
2101 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
2102 const char *buf
, /* I - Buffer */
2103 size_t bytes
) /* I - Number bytes */
2105 DEBUG_printf(("7cups_compress(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2106 CUPS_LLCAST bytes
));
2112 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, (uInt
)bytes
);
2115 * Deflate the bytes...
2118 fp
->stream
.next_in
= (Bytef
*)buf
;
2119 fp
->stream
.avail_in
= (uInt
)bytes
;
2121 while (fp
->stream
.avail_in
> 0)
2124 * Flush the current buffer...
2127 DEBUG_printf(("9cups_compress: avail_in=%d, avail_out=%d",
2128 fp
->stream
.avail_in
, fp
->stream
.avail_out
));
2130 if (fp
->stream
.avail_out
< (uInt
)(sizeof(fp
->cbuf
) / 8))
2132 if (cups_write(fp
, (char *)fp
->cbuf
, (size_t)(fp
->stream
.next_out
- fp
->cbuf
)) < 0)
2135 fp
->stream
.next_out
= fp
->cbuf
;
2136 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
2139 deflate(&(fp
->stream
), Z_NO_FLUSH
);
2142 return ((ssize_t
)bytes
);
2144 #endif /* HAVE_LIBZ */
2148 * 'cups_fill()' - Fill the input buffer.
2151 static ssize_t
/* O - Number of bytes or -1 */
2152 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
2154 ssize_t bytes
; /* Number of bytes read */
2156 int status
; /* Decompression status */
2157 const unsigned char *ptr
, /* Pointer into buffer */
2158 *end
; /* End of buffer */
2159 #endif /* HAVE_LIBZ */
2162 DEBUG_printf(("7cups_fill(fp=%p)", fp
));
2163 DEBUG_printf(("9cups_fill: fp->ptr=%p, fp->end=%p, fp->buf=%p, "
2164 "fp->bufpos=" CUPS_LLFMT
", fp->eof=%d",
2165 fp
->ptr
, fp
->end
, fp
->buf
, CUPS_LLCAST fp
->bufpos
, fp
->eof
));
2167 if (fp
->ptr
&& fp
->end
)
2168 fp
->bufpos
+= fp
->end
- fp
->buf
;
2171 DEBUG_printf(("9cups_fill: fp->compressed=%d", fp
->compressed
));
2173 while (!fp
->ptr
|| fp
->compressed
)
2176 * Check to see if we have read any data yet; if not, see if we have a
2177 * compressed file...
2183 * Reset the file position in case we are seeking...
2189 * Read the first bytes in the file to determine if we have a gzip'd
2193 if ((bytes
= cups_read(fp
, (char *)fp
->buf
, sizeof(fp
->buf
))) < 0)
2196 * Can't read from file!
2199 DEBUG_printf(("9cups_fill: cups_read() returned " CUPS_LLFMT
,
2200 CUPS_LLCAST bytes
));
2207 if (bytes
< 10 || fp
->buf
[0] != 0x1f ||
2208 (fp
->buf
[1] & 255) != 0x8b ||
2209 fp
->buf
[2] != 8 || (fp
->buf
[3] & 0xe0) != 0)
2212 * Not a gzip'd file!
2216 fp
->end
= fp
->buf
+ bytes
;
2218 DEBUG_printf(("9cups_fill: Returning " CUPS_LLFMT
,
2219 CUPS_LLCAST bytes
));
2225 * Parse header junk: extra data, original name, and comment...
2228 ptr
= (unsigned char *)fp
->buf
+ 10;
2229 end
= (unsigned char *)fp
->buf
+ bytes
;
2231 if (fp
->buf
[3] & 0x04)
2234 * Skip extra data...
2237 if ((ptr
+ 2) > end
)
2240 * Can't read from file!
2243 DEBUG_puts("9cups_fill: Extra gzip header data missing, returning -1.");
2251 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
2257 * Can't read from file!
2260 DEBUG_puts("9cups_fill: Extra gzip header data does not fit in initial buffer, returning -1.");
2269 if (fp
->buf
[3] & 0x08)
2272 * Skip original name data...
2275 while (ptr
< end
&& *ptr
)
2283 * Can't read from file!
2286 DEBUG_puts("9cups_fill: Original filename in gzip header data does not fit in initial buffer, returning -1.");
2295 if (fp
->buf
[3] & 0x10)
2298 * Skip comment data...
2301 while (ptr
< end
&& *ptr
)
2309 * Can't read from file!
2312 DEBUG_puts("9cups_fill: Comment in gzip header data does not fit in initial buffer, returning -1.");
2321 if (fp
->buf
[3] & 0x02)
2324 * Skip header CRC data...
2332 * Can't read from file!
2335 DEBUG_puts("9cups_fill: Header CRC in gzip header data does not fit in initial buffer, returning -1.");
2345 * Copy the flate-compressed data to the compression buffer...
2348 if ((bytes
= end
- ptr
) > 0)
2349 memcpy(fp
->cbuf
, ptr
, (size_t)bytes
);
2352 * Setup the decompressor data...
2355 fp
->stream
.zalloc
= (alloc_func
)0;
2356 fp
->stream
.zfree
= (free_func
)0;
2357 fp
->stream
.opaque
= (voidpf
)0;
2358 fp
->stream
.next_in
= (Bytef
*)fp
->cbuf
;
2359 fp
->stream
.next_out
= NULL
;
2360 fp
->stream
.avail_in
= (uInt
)bytes
;
2361 fp
->stream
.avail_out
= 0;
2362 fp
->crc
= crc32(0L, Z_NULL
, 0);
2364 if ((status
= inflateInit2(&(fp
->stream
), -15)) != Z_OK
)
2366 DEBUG_printf(("9cups_fill: inflateInit2 returned %d, returning -1.", status
));
2380 * If we have reached end-of-file, return immediately...
2385 DEBUG_puts("9cups_fill: EOF, returning 0.");
2391 * Fill the decompression buffer as needed...
2394 if (fp
->stream
.avail_in
== 0)
2396 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
2398 DEBUG_printf(("9cups_fill: cups_read error, returning %d.", (int)bytes
));
2405 fp
->stream
.next_in
= fp
->cbuf
;
2406 fp
->stream
.avail_in
= (uInt
)bytes
;
2410 * Decompress data from the buffer...
2413 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
2414 fp
->stream
.avail_out
= sizeof(fp
->buf
);
2416 status
= inflate(&(fp
->stream
), Z_NO_FLUSH
);
2418 if (fp
->stream
.next_out
> (Bytef
*)fp
->buf
)
2419 fp
->crc
= crc32(fp
->crc
, (Bytef
*)fp
->buf
,
2420 (uInt
)(fp
->stream
.next_out
- (Bytef
*)fp
->buf
));
2422 if (status
== Z_STREAM_END
)
2425 * Read the CRC and length...
2428 unsigned char trailer
[8]; /* Trailer bytes */
2429 uLong tcrc
; /* Trailer CRC */
2430 ssize_t tbytes
= 0; /* Number of bytes */
2432 if (fp
->stream
.avail_in
> 0)
2434 if (fp
->stream
.avail_in
> sizeof(trailer
))
2435 tbytes
= (ssize_t
)sizeof(trailer
);
2437 tbytes
= (ssize_t
)fp
->stream
.avail_in
;
2439 memcpy(trailer
, fp
->stream
.next_in
, (size_t)tbytes
);
2440 fp
->stream
.next_in
+= tbytes
;
2441 fp
->stream
.avail_in
-= (size_t)tbytes
;
2444 if (tbytes
< (ssize_t
)sizeof(trailer
))
2446 if (read(fp
->fd
, trailer
+ tbytes
, sizeof(trailer
) - (size_t)tbytes
) < ((ssize_t
)sizeof(trailer
) - tbytes
))
2449 * Can't get it, so mark end-of-file...
2452 DEBUG_puts("9cups_fill: Unable to read gzip CRC trailer, returning -1.");
2461 tcrc
= ((((((uLong
)trailer
[3] << 8) | (uLong
)trailer
[2]) << 8) |
2462 (uLong
)trailer
[1]) << 8) | (uLong
)trailer
[0];
2464 if (tcrc
!= fp
->crc
)
2467 * Bad CRC, mark end-of-file...
2470 DEBUG_printf(("9cups_fill: tcrc=%08x != fp->crc=%08x, returning -1.", (unsigned int)tcrc
, (unsigned int)fp
->crc
));
2479 * Otherwise, reset the compressed flag so that we re-read the
2485 else if (status
< Z_OK
)
2487 DEBUG_printf(("9cups_fill: inflate returned %d, returning -1.", status
));
2495 bytes
= (ssize_t
)sizeof(fp
->buf
) - (ssize_t
)fp
->stream
.avail_out
;
2498 * Return the decompressed data...
2502 fp
->end
= fp
->buf
+ bytes
;
2506 DEBUG_printf(("9cups_fill: Returning %d.", (int)bytes
));
2511 #endif /* HAVE_LIBZ */
2514 * Read a buffer's full of data...
2517 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
2520 * Can't read from file!
2530 * Return the bytes we read...
2535 fp
->end
= fp
->buf
+ bytes
;
2538 DEBUG_printf(("9cups_fill: Not gzip, returning %d.", (int)bytes
));
2545 * 'cups_open()' - Safely open a file for writing.
2547 * We don't allow appending to directories or files that are hard-linked or
2551 static int /* O - File descriptor or -1 otherwise */
2552 cups_open(const char *filename
, /* I - Filename */
2553 int mode
) /* I - Open mode */
2555 int fd
; /* File descriptor */
2556 struct stat fileinfo
; /* File information */
2558 struct stat linkinfo
; /* Link information */
2566 if ((fd
= open(filename
, mode
, 0666)) < 0)
2570 * Then verify that the file descriptor doesn't point to a directory or hard-
2574 if (fstat(fd
, &fileinfo
))
2580 if (fileinfo
.st_nlink
!= 1)
2588 if (fileinfo
.st_mode
& _S_IFDIR
)
2590 if (S_ISDIR(fileinfo
.st_mode
))
2600 * Then use lstat to determine whether the filename is a symlink...
2603 if (lstat(filename
, &linkinfo
))
2609 if (S_ISLNK(linkinfo
.st_mode
) ||
2610 fileinfo
.st_dev
!= linkinfo
.st_dev
||
2611 fileinfo
.st_ino
!= linkinfo
.st_ino
||
2613 fileinfo
.st_gen
!= linkinfo
.st_gen
||
2614 #endif /* HAVE_ST_GEN */
2615 fileinfo
.st_nlink
!= linkinfo
.st_nlink
||
2616 fileinfo
.st_mode
!= linkinfo
.st_mode
)
2633 * 'cups_read()' - Read from a file descriptor.
2636 static ssize_t
/* O - Number of bytes read or -1 */
2637 cups_read(cups_file_t
*fp
, /* I - CUPS file */
2638 char *buf
, /* I - Buffer */
2639 size_t bytes
) /* I - Number bytes */
2641 ssize_t total
; /* Total bytes read */
2644 DEBUG_printf(("7cups_read(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2645 CUPS_LLCAST bytes
));
2648 * Loop until we read at least 0 bytes...
2654 if (fp
->mode
== 's')
2655 total
= (ssize_t
)recv(fp
->fd
, buf
, (unsigned)bytes
, 0);
2657 total
= (ssize_t
)read(fp
->fd
, buf
, (unsigned)bytes
);
2659 if (fp
->mode
== 's')
2660 total
= recv(fp
->fd
, buf
, bytes
, 0);
2662 total
= read(fp
->fd
, buf
, bytes
);
2665 DEBUG_printf(("9cups_read: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
2671 * Reads can be interrupted by signals and unavailable resources...
2674 if (errno
== EAGAIN
|| errno
== EINTR
)
2681 * Return the total number of bytes read...
2689 * 'cups_write()' - Write to a file descriptor.
2692 static ssize_t
/* O - Number of bytes written or -1 */
2693 cups_write(cups_file_t
*fp
, /* I - CUPS file */
2694 const char *buf
, /* I - Buffer */
2695 size_t bytes
) /* I - Number bytes */
2697 size_t total
; /* Total bytes written */
2698 ssize_t count
; /* Count this time */
2701 DEBUG_printf(("7cups_write(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", fp
, buf
,
2702 CUPS_LLCAST bytes
));
2705 * Loop until all bytes are written...
2712 if (fp
->mode
== 's')
2713 count
= (ssize_t
)send(fp
->fd
, buf
, (unsigned)bytes
, 0);
2715 count
= (ssize_t
)write(fp
->fd
, buf
, (unsigned)bytes
);
2717 if (fp
->mode
== 's')
2718 count
= send(fp
->fd
, buf
, bytes
, 0);
2720 count
= write(fp
->fd
, buf
, bytes
);
2723 DEBUG_printf(("9cups_write: count=" CUPS_LLFMT
, CUPS_LLCAST count
));
2728 * Writes can be interrupted by signals and unavailable resources...
2731 if (errno
== EAGAIN
|| errno
== EINTR
)
2738 * Update the counts for the last write call...
2741 bytes
-= (size_t)count
;
2742 total
+= (size_t)count
;
2747 * Return the total number of bytes written...
2750 return ((ssize_t
)total
);