2 * File functions for CUPS.
4 * Since stdio files max out at 256 files on many systems, we have to
5 * write similar functions without this limit. At the same time, using
6 * our own file functions allows us to provide transparent support of
7 * different line endings, gzip'd print files, PPD files, etc.
9 * Copyright © 2007-2018 by Apple Inc.
10 * Copyright © 1997-2007 by Easy Software Products, all rights reserved.
12 * Licensed under Apache License v2.0. See the file "LICENSE" for more
17 * Include necessary headers...
20 #include "file-private.h"
22 #include <sys/types.h>
26 # endif /* HAVE_LIBZ */
30 * Internal structures...
33 struct _cups_file_s
/**** CUPS file structure... ****/
36 int fd
; /* File descriptor */
37 char mode
, /* Mode ('r' or 'w') */
38 compressed
, /* Compression used? */
39 is_stdio
, /* stdin/out/err? */
40 eof
, /* End of file? */
41 buf
[4096], /* Buffer */
42 *ptr
, /* Pointer into buffer */
43 *end
; /* End of buffer data */
44 off_t pos
, /* Position in file */
45 bufpos
; /* File position for start of buffer */
48 z_stream stream
; /* (De)compression stream */
49 Bytef cbuf
[4096]; /* (De)compression buffer */
50 uLong crc
; /* (De)compression CRC */
51 #endif /* HAVE_LIBZ */
53 char *printf_buffer
; /* cupsFilePrintf buffer */
54 size_t printf_size
; /* Size of cupsFilePrintf buffer */
63 static ssize_t
cups_compress(cups_file_t
*fp
, const char *buf
, size_t bytes
);
64 #endif /* HAVE_LIBZ */
65 static ssize_t
cups_fill(cups_file_t
*fp
);
66 static int cups_open(const char *filename
, int mode
);
67 static ssize_t
cups_read(cups_file_t
*fp
, char *buf
, size_t bytes
);
68 static ssize_t
cups_write(cups_file_t
*fp
, const char *buf
, size_t bytes
);
73 * '_cupsFileCheck()' - Check the permissions of the given filename.
76 _cups_fc_result_t
/* O - Check result */
78 const char *filename
, /* I - Filename to check */
79 _cups_fc_filetype_t filetype
, /* I - Type of file checks? */
80 int dorootchecks
, /* I - Check for root permissions? */
81 _cups_fc_func_t cb
, /* I - Callback function */
82 void *context
) /* I - Context pointer for callback */
85 struct stat fileinfo
; /* File information */
86 char message
[1024], /* Message string */
87 temp
[1024], /* Parent directory filename */
88 *ptr
; /* Pointer into parent directory */
89 _cups_fc_result_t result
; /* Check result */
93 * Does the filename contain a relative path ("../")?
96 if (strstr(filename
, "../"))
102 result
= _CUPS_FILE_CHECK_RELATIVE_PATH
;
107 * Does the program even exist and is it accessible?
110 if (stat(filename
, &fileinfo
))
116 result
= _CUPS_FILE_CHECK_MISSING
;
121 * Check the execute bit...
124 result
= _CUPS_FILE_CHECK_OK
;
128 case _CUPS_FILE_CHECK_DIRECTORY
:
129 if (!S_ISDIR(fileinfo
.st_mode
))
130 result
= _CUPS_FILE_CHECK_WRONG_TYPE
;
134 if (!S_ISREG(fileinfo
.st_mode
))
135 result
= _CUPS_FILE_CHECK_WRONG_TYPE
;
143 * Are we doing root checks?
149 * Nope, so anything (else) goes...
156 * Verify permission of the file itself:
158 * 1. Must be owned by root
159 * 2. Must not be writable by group
160 * 3. Must not be setuid
161 * 4. Must not be writable by others
164 if (fileinfo
.st_uid
|| /* 1. Must be owned by root */
165 (fileinfo
.st_mode
& S_IWGRP
) || /* 2. Must not be writable by group */
166 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
167 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
169 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
173 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
||
174 filetype
== _CUPS_FILE_CHECK_FILE_ONLY
)
178 * Now check the containing directory...
181 strlcpy(temp
, filename
, sizeof(temp
));
182 if ((ptr
= strrchr(temp
, '/')) != NULL
)
190 if (stat(temp
, &fileinfo
))
196 result
= _CUPS_FILE_CHECK_MISSING
;
197 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
203 if (fileinfo
.st_uid
|| /* 1. Must be owned by root */
204 (fileinfo
.st_mode
& S_IWGRP
) || /* 2. Must not be writable by group */
205 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
206 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
208 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
209 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
214 * Common return point...
221 cups_lang_t
*lang
= cupsLangDefault();
222 /* Localization information */
226 case _CUPS_FILE_CHECK_OK
:
227 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
228 snprintf(message
, sizeof(message
),
229 _cupsLangString(lang
, _("Directory \"%s\" permissions OK "
230 "(0%o/uid=%d/gid=%d).")),
231 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
232 (int)fileinfo
.st_gid
);
234 snprintf(message
, sizeof(message
),
235 _cupsLangString(lang
, _("File \"%s\" permissions OK "
236 "(0%o/uid=%d/gid=%d).")),
237 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
238 (int)fileinfo
.st_gid
);
241 case _CUPS_FILE_CHECK_MISSING
:
242 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
243 snprintf(message
, sizeof(message
),
244 _cupsLangString(lang
, _("Directory \"%s\" not available: "
246 filename
, strerror(errno
));
248 snprintf(message
, sizeof(message
),
249 _cupsLangString(lang
, _("File \"%s\" not available: %s")),
250 filename
, strerror(errno
));
253 case _CUPS_FILE_CHECK_PERMISSIONS
:
254 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
255 snprintf(message
, sizeof(message
),
256 _cupsLangString(lang
, _("Directory \"%s\" has insecure "
258 "(0%o/uid=%d/gid=%d).")),
259 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
260 (int)fileinfo
.st_gid
);
262 snprintf(message
, sizeof(message
),
263 _cupsLangString(lang
, _("File \"%s\" has insecure "
265 "(0%o/uid=%d/gid=%d).")),
266 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
267 (int)fileinfo
.st_gid
);
270 case _CUPS_FILE_CHECK_WRONG_TYPE
:
271 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
272 snprintf(message
, sizeof(message
),
273 _cupsLangString(lang
, _("Directory \"%s\" is a file.")),
276 snprintf(message
, sizeof(message
),
277 _cupsLangString(lang
, _("File \"%s\" is a directory.")),
281 case _CUPS_FILE_CHECK_RELATIVE_PATH
:
282 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
283 snprintf(message
, sizeof(message
),
284 _cupsLangString(lang
, _("Directory \"%s\" contains a "
285 "relative path.")), filename
);
287 snprintf(message
, sizeof(message
),
288 _cupsLangString(lang
, _("File \"%s\" contains a relative "
289 "path.")), filename
);
293 (*cb
)(context
, result
, message
);
301 * '_cupsFileCheckFilter()' - Report file check results as CUPS filter messages.
305 _cupsFileCheckFilter(
306 void *context
, /* I - Context pointer (unused) */
307 _cups_fc_result_t result
, /* I - Result code */
308 const char *message
) /* I - Message text */
310 const char *prefix
; /* Messaging prefix */
318 case _CUPS_FILE_CHECK_OK
:
322 case _CUPS_FILE_CHECK_MISSING
:
323 case _CUPS_FILE_CHECK_WRONG_TYPE
:
325 fputs("STATE: +cups-missing-filter-warning\n", stderr
);
328 case _CUPS_FILE_CHECK_PERMISSIONS
:
329 case _CUPS_FILE_CHECK_RELATIVE_PATH
:
331 fputs("STATE: +cups-insecure-filter-warning\n", stderr
);
335 fprintf(stderr
, "%s: %s\n", prefix
, message
);
341 * 'cupsFileClose()' - Close a CUPS file.
343 * @since CUPS 1.2/macOS 10.5@
346 int /* O - 0 on success, -1 on error */
347 cupsFileClose(cups_file_t
*fp
) /* I - CUPS file */
349 int fd
; /* File descriptor */
350 char mode
; /* Open mode */
351 int status
; /* Return status */
354 DEBUG_printf(("cupsFileClose(fp=%p)", (void *)fp
));
364 * Flush pending write data...
368 status
= cupsFileFlush(fp
);
373 if (fp
->compressed
&& status
>= 0)
378 * Free decompression data...
381 inflateEnd(&fp
->stream
);
386 * Flush any remaining compressed data...
389 unsigned char trailer
[8]; /* Trailer CRC and length */
390 int done
; /* Done writing... */
393 fp
->stream
.avail_in
= 0;
397 if (fp
->stream
.next_out
> fp
->cbuf
)
399 if (cups_write(fp
, (char *)fp
->cbuf
,
400 (size_t)(fp
->stream
.next_out
- fp
->cbuf
)) < 0)
403 fp
->stream
.next_out
= fp
->cbuf
;
404 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
407 if (done
|| status
< 0)
410 done
= deflate(&fp
->stream
, Z_FINISH
) == Z_STREAM_END
&&
411 fp
->stream
.next_out
== fp
->cbuf
;
415 * Write the CRC and length...
418 trailer
[0] = (unsigned char)fp
->crc
;
419 trailer
[1] = (unsigned char)(fp
->crc
>> 8);
420 trailer
[2] = (unsigned char)(fp
->crc
>> 16);
421 trailer
[3] = (unsigned char)(fp
->crc
>> 24);
422 trailer
[4] = (unsigned char)fp
->pos
;
423 trailer
[5] = (unsigned char)(fp
->pos
>> 8);
424 trailer
[6] = (unsigned char)(fp
->pos
>> 16);
425 trailer
[7] = (unsigned char)(fp
->pos
>> 24);
427 if (cups_write(fp
, (char *)trailer
, 8) < 0)
431 * Free all memory used by the compression stream...
434 deflateEnd(&(fp
->stream
));
437 #endif /* HAVE_LIBZ */
440 * If this is one of the cupsFileStdin/out/err files, return now and don't
441 * actually free memory or close (these last the life of the process...)
448 * Save the file descriptor we used and free memory...
454 if (fp
->printf_buffer
)
455 free(fp
->printf_buffer
);
460 * Close the file, returning the close status...
465 if (httpAddrClose(NULL
, fd
) < 0)
468 else if (close(fd
) < 0)
476 * 'cupsFileCompression()' - Return whether a file is compressed.
478 * @since CUPS 1.2/macOS 10.5@
481 int /* O - @code CUPS_FILE_NONE@ or @code CUPS_FILE_GZIP@ */
482 cupsFileCompression(cups_file_t
*fp
) /* I - CUPS file */
484 return (fp
? fp
->compressed
: CUPS_FILE_NONE
);
489 * 'cupsFileEOF()' - Return the end-of-file status.
491 * @since CUPS 1.2/macOS 10.5@
494 int /* O - 1 on end of file, 0 otherwise */
495 cupsFileEOF(cups_file_t
*fp
) /* I - CUPS file */
497 return (fp
? fp
->eof
: 1);
502 * 'cupsFileFind()' - Find a file using the specified path.
504 * This function allows the paths in the path string to be separated by
505 * colons (UNIX standard) or semicolons (Windows standard) and stores the
506 * result in the buffer supplied. If the file cannot be found in any of
507 * the supplied paths, @code NULL@ is returned. A @code NULL@ path only
508 * matches the current directory.
510 * @since CUPS 1.2/macOS 10.5@
513 const char * /* O - Full path to file or @code NULL@ if not found */
514 cupsFileFind(const char *filename
, /* I - File to find */
515 const char *path
, /* I - Colon/semicolon-separated path */
516 int executable
, /* I - 1 = executable files, 0 = any file/dir */
517 char *buffer
, /* I - Filename buffer */
518 int bufsize
) /* I - Size of filename buffer */
520 char *bufptr
, /* Current position in buffer */
521 *bufend
; /* End of buffer */
525 * Range check input...
528 DEBUG_printf(("cupsFileFind(filename=\"%s\", path=\"%s\", executable=%d, buffer=%p, bufsize=%d)", filename
, path
, executable
, (void *)buffer
, bufsize
));
530 if (!filename
|| !buffer
|| bufsize
< 2)
536 * No path, so check current directory...
539 if (!access(filename
, 0))
541 strlcpy(buffer
, filename
, (size_t)bufsize
);
549 * Now check each path and return the first match...
552 bufend
= buffer
+ bufsize
- 1;
558 if (*path
== ';' || (*path
== ':' && ((bufptr
- buffer
) > 1 || !isalpha(buffer
[0] & 255))))
560 if (*path
== ';' || *path
== ':')
563 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
566 strlcpy(bufptr
, filename
, (size_t)(bufend
- bufptr
));
569 if (!access(buffer
, 0))
571 if (!access(buffer
, executable
? X_OK
: 0))
574 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
580 else if (bufptr
< bufend
)
587 * Check the last path...
590 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
593 strlcpy(bufptr
, filename
, (size_t)(bufend
- bufptr
));
595 if (!access(buffer
, 0))
597 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
602 DEBUG_puts("1cupsFileFind: Returning NULL");
609 * 'cupsFileFlush()' - Flush pending output.
611 * @since CUPS 1.2/macOS 10.5@
614 int /* O - 0 on success, -1 on error */
615 cupsFileFlush(cups_file_t
*fp
) /* I - CUPS file */
617 ssize_t bytes
; /* Bytes to write */
620 DEBUG_printf(("cupsFileFlush(fp=%p)", (void *)fp
));
623 * Range check input...
626 if (!fp
|| fp
->mode
!= 'w')
628 DEBUG_puts("1cupsFileFlush: Attempt to flush a read-only file...");
632 bytes
= (ssize_t
)(fp
->ptr
- fp
->buf
);
634 DEBUG_printf(("2cupsFileFlush: Flushing " CUPS_LLFMT
" bytes...",
641 bytes
= cups_compress(fp
, fp
->buf
, (size_t)bytes
);
643 #endif /* HAVE_LIBZ */
644 bytes
= cups_write(fp
, fp
->buf
, (size_t)bytes
);
657 * 'cupsFileGetChar()' - Get a single character from a file.
659 * @since CUPS 1.2/macOS 10.5@
662 int /* O - Character or -1 on end of file */
663 cupsFileGetChar(cups_file_t
*fp
) /* I - CUPS file */
666 * Range check input...
669 DEBUG_printf(("4cupsFileGetChar(fp=%p)", (void *)fp
));
671 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
673 DEBUG_puts("5cupsFileGetChar: Bad arguments!");
678 * If the input buffer is empty, try to read more data...
681 DEBUG_printf(("5cupsFileGetChar: fp->eof=%d, fp->ptr=%p, fp->end=%p", fp
->eof
, (void *)fp
->ptr
, (void *)fp
->end
));
683 if (fp
->ptr
>= fp
->end
)
684 if (cups_fill(fp
) <= 0)
686 DEBUG_puts("5cupsFileGetChar: Unable to fill buffer!");
691 * Return the next character in the buffer...
694 DEBUG_printf(("5cupsFileGetChar: Returning %d...", *(fp
->ptr
) & 255));
698 DEBUG_printf(("6cupsFileGetChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
700 return (*(fp
->ptr
)++ & 255);
705 * 'cupsFileGetConf()' - Get a line from a configuration file.
707 * @since CUPS 1.2/macOS 10.5@
710 char * /* O - Line read or @code NULL@ on end of file or error */
711 cupsFileGetConf(cups_file_t
*fp
, /* I - CUPS file */
712 char *buf
, /* O - String buffer */
713 size_t buflen
, /* I - Size of string buffer */
714 char **value
, /* O - Pointer to value */
715 int *linenum
) /* IO - Current line number */
717 char *ptr
; /* Pointer into line */
721 * Range check input...
724 DEBUG_printf(("2cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT
725 ", value=%p, linenum=%p)", (void *)fp
, (void *)buf
, CUPS_LLCAST buflen
, (void *)value
, (void *)linenum
));
727 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') ||
728 !buf
|| buflen
< 2 || !value
)
737 * Read the next non-comment line...
742 while (cupsFileGets(fp
, buf
, buflen
))
747 * Strip any comments...
750 if ((ptr
= strchr(buf
, '#')) != NULL
)
752 if (ptr
> buf
&& ptr
[-1] == '\\')
755 _cups_strcpy(ptr
- 1, ptr
);
759 // Strip the comment and any trailing whitespace...
762 if (!_cups_isspace(ptr
[-1]))
773 * Strip leading whitespace...
776 for (ptr
= buf
; _cups_isspace(*ptr
); ptr
++);
779 _cups_strcpy(buf
, ptr
);
782 * See if there is anything left...
788 * Yes, grab any value and return...
791 for (ptr
= buf
; *ptr
; ptr
++)
792 if (_cups_isspace(*ptr
))
798 * Have a value, skip any other spaces...
801 while (_cups_isspace(*ptr
))
808 * Strip trailing whitespace and > for lines that begin with <...
811 ptr
+= strlen(ptr
) - 1;
813 if (buf
[0] == '<' && *ptr
== '>')
815 else if (buf
[0] == '<' && *ptr
!= '>')
825 while (ptr
> *value
&& _cups_isspace(*ptr
))
842 * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may
843 * contain binary data.
845 * This function differs from @link cupsFileGets@ in that the trailing CR
846 * and LF are preserved, as is any binary data on the line. The buffer is
847 * nul-terminated, however you should use the returned length to determine
848 * the number of bytes on the line.
850 * @since CUPS 1.2/macOS 10.5@
853 size_t /* O - Number of bytes on line or 0 on end of file */
854 cupsFileGetLine(cups_file_t
*fp
, /* I - File to read from */
855 char *buf
, /* I - Buffer */
856 size_t buflen
) /* I - Size of buffer */
858 int ch
; /* Character from file */
859 char *ptr
, /* Current position in line buffer */
860 *end
; /* End of line buffer */
864 * Range check input...
867 DEBUG_printf(("2cupsFileGetLine(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST buflen
));
869 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 3)
873 * Now loop until we have a valid line...
876 for (ptr
= buf
, end
= buf
+ buflen
- 2; ptr
< end
;)
878 if (fp
->ptr
>= fp
->end
)
879 if (cups_fill(fp
) <= 0)
882 *ptr
++ = ch
= *(fp
->ptr
)++;
891 if (fp
->ptr
>= fp
->end
)
892 if (cups_fill(fp
) <= 0)
895 if (*(fp
->ptr
) == '\n')
897 *ptr
++ = *(fp
->ptr
)++;
906 * Line feed ends a line...
915 DEBUG_printf(("4cupsFileGetLine: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
917 return ((size_t)(ptr
- buf
));
922 * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
924 * @since CUPS 1.2/macOS 10.5@
927 char * /* O - Line read or @code NULL@ on end of file or error */
928 cupsFileGets(cups_file_t
*fp
, /* I - CUPS file */
929 char *buf
, /* O - String buffer */
930 size_t buflen
) /* I - Size of string buffer */
932 int ch
; /* Character from file */
933 char *ptr
, /* Current position in line buffer */
934 *end
; /* End of line buffer */
938 * Range check input...
941 DEBUG_printf(("2cupsFileGets(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST buflen
));
943 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 2)
947 * Now loop until we have a valid line...
950 for (ptr
= buf
, end
= buf
+ buflen
- 1; ptr
< end
;)
952 if (fp
->ptr
>= fp
->end
)
953 if (cups_fill(fp
) <= 0)
970 if (fp
->ptr
>= fp
->end
)
971 if (cups_fill(fp
) <= 0)
974 if (*(fp
->ptr
) == '\n')
985 * Line feed ends a line...
996 DEBUG_printf(("4cupsFileGets: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1003 * 'cupsFileLock()' - Temporarily lock access to a file.
1005 * @since CUPS 1.2/macOS 10.5@
1008 int /* O - 0 on success, -1 on error */
1009 cupsFileLock(cups_file_t
*fp
, /* I - CUPS file */
1010 int block
) /* I - 1 to wait for the lock, 0 to fail right away */
1016 if (!fp
|| fp
->mode
== 's')
1024 return (_locking(fp
->fd
, block
? _LK_LOCK
: _LK_NBLCK
, 0));
1026 return (lockf(fp
->fd
, block
? F_LOCK
: F_TLOCK
, 0));
1032 * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
1034 * @since CUPS 1.2/macOS 10.5@
1037 int /* O - File descriptor */
1038 cupsFileNumber(cups_file_t
*fp
) /* I - CUPS file */
1048 * 'cupsFileOpen()' - Open a CUPS file.
1050 * The "mode" parameter can be "r" to read, "w" to write, overwriting any
1051 * existing file, "a" to append to an existing file or create a new file,
1052 * or "s" to open a socket connection.
1054 * When opening for writing ("w"), an optional number from 1 to 9 can be
1055 * supplied which enables Flate compression of the file. Compression is
1056 * not supported for the "a" (append) mode.
1058 * When opening a socket connection, the filename is a string of the form
1059 * "address:port" or "hostname:port". The socket will make an IPv4 or IPv6
1060 * connection as needed, generally preferring IPv6 connections when there is
1063 * @since CUPS 1.2/macOS 10.5@
1066 cups_file_t
* /* O - CUPS file or @code NULL@ if the file or socket cannot be opened */
1067 cupsFileOpen(const char *filename
, /* I - Name of file */
1068 const char *mode
) /* I - Open mode */
1070 cups_file_t
*fp
; /* New CUPS file */
1071 int fd
; /* File descriptor */
1072 char hostname
[1024], /* Hostname */
1073 *portname
; /* Port "name" (number or service) */
1074 http_addrlist_t
*addrlist
; /* Host address list */
1077 DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")", filename
,
1081 * Range check input...
1084 if (!filename
|| !mode
||
1085 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1086 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1095 case 'a' : /* Append file */
1096 fd
= cups_open(filename
,
1097 O_RDWR
| O_CREAT
| O_APPEND
| O_LARGEFILE
| O_BINARY
);
1100 case 'r' : /* Read file */
1101 fd
= open(filename
, O_RDONLY
| O_LARGEFILE
| O_BINARY
, 0);
1104 case 'w' : /* Write file */
1105 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1106 if (fd
< 0 && errno
== ENOENT
)
1108 fd
= cups_open(filename
,
1109 O_WRONLY
| O_CREAT
| O_EXCL
| O_LARGEFILE
| O_BINARY
);
1110 if (fd
< 0 && errno
== EEXIST
)
1111 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1122 case 's' : /* Read/write socket */
1123 strlcpy(hostname
, filename
, sizeof(hostname
));
1124 if ((portname
= strrchr(hostname
, ':')) != NULL
)
1130 * Lookup the hostname and service...
1133 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
1137 * Connect to the server...
1140 if (!httpAddrConnect(addrlist
, &fd
))
1142 httpAddrFreeList(addrlist
);
1146 httpAddrFreeList(addrlist
);
1149 default : /* Remove bogus compiler warning... */
1157 * Create the CUPS file structure...
1160 if ((fp
= cupsFileOpenFd(fd
, mode
)) == NULL
)
1163 httpAddrClose(NULL
, fd
);
1176 * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
1178 * The "mode" parameter can be "r" to read, "w" to write, "a" to append,
1179 * or "s" to treat the file descriptor as a bidirectional socket connection.
1181 * When opening for writing ("w"), an optional number from 1 to 9 can be
1182 * supplied which enables Flate compression of the file. Compression is
1183 * not supported for the "a" (append) mode.
1185 * @since CUPS 1.2/macOS 10.5@
1188 cups_file_t
* /* O - CUPS file or @code NULL@ if the file could not be opened */
1189 cupsFileOpenFd(int fd
, /* I - File descriptor */
1190 const char *mode
) /* I - Open mode */
1192 cups_file_t
*fp
; /* New CUPS file */
1195 DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")", fd
, mode
));
1198 * Range check input...
1201 if (fd
< 0 || !mode
||
1202 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1203 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1207 * Allocate memory...
1210 if ((fp
= calloc(1, sizeof(cups_file_t
))) == NULL
)
1222 fp
->pos
= lseek(fd
, 0, SEEK_END
);
1227 fp
->end
= fp
->buf
+ sizeof(fp
->buf
);
1230 if (mode
[1] >= '1' && mode
[1] <= '9')
1233 * Open a compressed stream, so write the standard gzip file
1237 unsigned char header
[10]; /* gzip file header */
1238 time_t curtime
; /* Current time */
1241 curtime
= time(NULL
);
1244 header
[2] = Z_DEFLATED
;
1246 header
[4] = (unsigned char)curtime
;
1247 header
[5] = (unsigned char)(curtime
>> 8);
1248 header
[6] = (unsigned char)(curtime
>> 16);
1249 header
[7] = (unsigned char)(curtime
>> 24);
1253 cups_write(fp
, (char *)header
, 10);
1256 * Initialize the compressor...
1259 deflateInit2(&(fp
->stream
), mode
[1] - '0', Z_DEFLATED
, -15, 8,
1260 Z_DEFAULT_STRATEGY
);
1262 fp
->stream
.next_out
= fp
->cbuf
;
1263 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
1265 fp
->crc
= crc32(0L, Z_NULL
, 0);
1267 #endif /* HAVE_LIBZ */
1278 default : /* Remove bogus compiler warning... */
1283 * Don't pass this file to child processes...
1287 fcntl(fp
->fd
, F_SETFD
, fcntl(fp
->fd
, F_GETFD
) | FD_CLOEXEC
);
1288 #endif /* !_WIN32 */
1295 * '_cupsFilePeekAhead()' - See if the requested character is buffered up.
1298 int /* O - 1 if present, 0 otherwise */
1299 _cupsFilePeekAhead(cups_file_t
*fp
, /* I - CUPS file */
1300 int ch
) /* I - Character */
1302 return (fp
&& fp
->ptr
&& memchr(fp
->ptr
, ch
, (size_t)(fp
->end
- fp
->ptr
)));
1307 * 'cupsFilePeekChar()' - Peek at the next character from a file.
1309 * @since CUPS 1.2/macOS 10.5@
1312 int /* O - Character or -1 on end of file */
1313 cupsFilePeekChar(cups_file_t
*fp
) /* I - CUPS file */
1316 * Range check input...
1319 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1323 * If the input buffer is empty, try to read more data...
1326 if (fp
->ptr
>= fp
->end
)
1327 if (cups_fill(fp
) <= 0)
1331 * Return the next character in the buffer...
1334 return (*(fp
->ptr
) & 255);
1339 * 'cupsFilePrintf()' - Write a formatted string.
1341 * @since CUPS 1.2/macOS 10.5@
1344 int /* O - Number of bytes written or -1 on error */
1345 cupsFilePrintf(cups_file_t
*fp
, /* I - CUPS file */
1346 const char *format
, /* I - Printf-style format string */
1347 ...) /* I - Additional args as necessary */
1349 va_list ap
; /* Argument list */
1350 ssize_t bytes
; /* Formatted size */
1353 DEBUG_printf(("2cupsFilePrintf(fp=%p, format=\"%s\", ...)", (void *)fp
, format
));
1355 if (!fp
|| !format
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1358 if (!fp
->printf_buffer
)
1361 * Start with an 1k printf buffer...
1364 if ((fp
->printf_buffer
= malloc(1024)) == NULL
)
1367 fp
->printf_size
= 1024;
1370 va_start(ap
, format
);
1371 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1374 if (bytes
>= (ssize_t
)fp
->printf_size
)
1377 * Expand the printf buffer...
1380 char *temp
; /* Temporary buffer pointer */
1386 if ((temp
= realloc(fp
->printf_buffer
, (size_t)(bytes
+ 1))) == NULL
)
1389 fp
->printf_buffer
= temp
;
1390 fp
->printf_size
= (size_t)(bytes
+ 1);
1392 va_start(ap
, format
);
1393 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1397 if (fp
->mode
== 's')
1399 if (cups_write(fp
, fp
->printf_buffer
, (size_t)bytes
) < 0)
1404 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1406 return ((int)bytes
);
1409 if ((fp
->ptr
+ bytes
) > fp
->end
)
1410 if (cupsFileFlush(fp
))
1415 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1417 if ((size_t)bytes
> sizeof(fp
->buf
))
1421 return ((int)cups_compress(fp
, fp
->printf_buffer
, (size_t)bytes
));
1423 #endif /* HAVE_LIBZ */
1424 return ((int)cups_write(fp
, fp
->printf_buffer
, (size_t)bytes
));
1428 memcpy(fp
->ptr
, fp
->printf_buffer
, (size_t)bytes
);
1431 if (fp
->is_stdio
&& cupsFileFlush(fp
))
1434 return ((int)bytes
);
1440 * 'cupsFilePutChar()' - Write a character.
1442 * @since CUPS 1.2/macOS 10.5@
1445 int /* O - 0 on success, -1 on error */
1446 cupsFilePutChar(cups_file_t
*fp
, /* I - CUPS file */
1447 int c
) /* I - Character to write */
1450 * Range check input...
1453 if (!fp
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1456 if (fp
->mode
== 's')
1459 * Send character immediately over socket...
1462 char ch
; /* Output character */
1467 if (send(fp
->fd
, &ch
, 1, 0) < 1)
1476 if (fp
->ptr
>= fp
->end
)
1477 if (cupsFileFlush(fp
))
1480 *(fp
->ptr
) ++ = (char)c
;
1485 DEBUG_printf(("4cupsFilePutChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1492 * 'cupsFilePutConf()' - Write a configuration line.
1494 * This function handles any comment escaping of the value.
1496 * @since CUPS 1.4/macOS 10.6@
1499 ssize_t
/* O - Number of bytes written or -1 on error */
1500 cupsFilePutConf(cups_file_t
*fp
, /* I - CUPS file */
1501 const char *directive
, /* I - Directive */
1502 const char *value
) /* I - Value */
1504 ssize_t bytes
, /* Number of bytes written */
1505 temp
; /* Temporary byte count */
1506 const char *ptr
; /* Pointer into value */
1509 if (!fp
|| !directive
|| !*directive
)
1512 if ((bytes
= cupsFilePuts(fp
, directive
)) < 0)
1515 if (cupsFilePutChar(fp
, ' ') < 0)
1519 if (value
&& *value
)
1521 if ((ptr
= strchr(value
, '#')) != NULL
)
1524 * Need to quote the first # in the info string...
1527 if ((temp
= cupsFileWrite(fp
, value
, (size_t)(ptr
- value
))) < 0)
1531 if (cupsFilePutChar(fp
, '\\') < 0)
1535 if ((temp
= cupsFilePuts(fp
, ptr
)) < 0)
1539 else if ((temp
= cupsFilePuts(fp
, value
)) < 0)
1545 if (cupsFilePutChar(fp
, '\n') < 0)
1553 * 'cupsFilePuts()' - Write a string.
1555 * Like the @code fputs@ function, no newline is appended to the string.
1557 * @since CUPS 1.2/macOS 10.5@
1560 int /* O - Number of bytes written or -1 on error */
1561 cupsFilePuts(cups_file_t
*fp
, /* I - CUPS file */
1562 const char *s
) /* I - String to write */
1564 ssize_t bytes
; /* Bytes to write */
1568 * Range check input...
1571 if (!fp
|| !s
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1575 * Write the string...
1578 bytes
= (ssize_t
)strlen(s
);
1580 if (fp
->mode
== 's')
1582 if (cups_write(fp
, s
, (size_t)bytes
) < 0)
1587 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1589 return ((int)bytes
);
1592 if ((fp
->ptr
+ bytes
) > fp
->end
)
1593 if (cupsFileFlush(fp
))
1598 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1600 if ((size_t)bytes
> sizeof(fp
->buf
))
1604 return ((int)cups_compress(fp
, s
, (size_t)bytes
));
1606 #endif /* HAVE_LIBZ */
1607 return ((int)cups_write(fp
, s
, (size_t)bytes
));
1611 memcpy(fp
->ptr
, s
, (size_t)bytes
);
1614 if (fp
->is_stdio
&& cupsFileFlush(fp
))
1617 return ((int)bytes
);
1623 * 'cupsFileRead()' - Read from a file.
1625 * @since CUPS 1.2/macOS 10.5@
1628 ssize_t
/* O - Number of bytes read or -1 on error */
1629 cupsFileRead(cups_file_t
*fp
, /* I - CUPS file */
1630 char *buf
, /* O - Buffer */
1631 size_t bytes
) /* I - Number of bytes to read */
1633 size_t total
; /* Total bytes read */
1634 ssize_t count
; /* Bytes read */
1637 DEBUG_printf(("2cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
1640 * Range check input...
1643 if (!fp
|| !buf
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1650 * Loop until all bytes are read...
1656 if (fp
->ptr
>= fp
->end
)
1657 if (cups_fill(fp
) <= 0)
1659 DEBUG_printf(("4cupsFileRead: cups_fill() returned -1, total="
1660 CUPS_LLFMT
, CUPS_LLCAST total
));
1663 return ((ssize_t
)total
);
1668 count
= (ssize_t
)(fp
->end
- fp
->ptr
);
1669 if (count
> (ssize_t
)bytes
)
1670 count
= (ssize_t
)bytes
;
1672 memcpy(buf
, fp
->ptr
,(size_t) count
);
1676 DEBUG_printf(("4cupsFileRead: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1679 * Update the counts for the last read...
1682 bytes
-= (size_t)count
;
1683 total
+= (size_t)count
;
1688 * Return the total number of bytes read...
1691 DEBUG_printf(("3cupsFileRead: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
1693 return ((ssize_t
)total
);
1698 * 'cupsFileRewind()' - Set the current file position to the beginning of the
1701 * @since CUPS 1.2/macOS 10.5@
1704 off_t
/* O - New file position or -1 on error */
1705 cupsFileRewind(cups_file_t
*fp
) /* I - CUPS file */
1708 * Range check input...
1711 DEBUG_printf(("cupsFileRewind(fp=%p)", (void *)fp
));
1712 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1714 if (!fp
|| fp
->mode
!= 'r')
1718 * Handle special cases...
1721 if (fp
->bufpos
== 0)
1724 * No seeking necessary...
1735 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1741 * Otherwise, seek in the file and cleanup any compression buffers...
1747 inflateEnd(&fp
->stream
);
1750 #endif /* HAVE_LIBZ */
1752 if (lseek(fp
->fd
, 0, SEEK_SET
))
1754 DEBUG_printf(("1cupsFileRewind: lseek failed: %s", strerror(errno
)));
1764 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1771 * 'cupsFileSeek()' - Seek in a file.
1773 * @since CUPS 1.2/macOS 10.5@
1776 off_t
/* O - New file position or -1 on error */
1777 cupsFileSeek(cups_file_t
*fp
, /* I - CUPS file */
1778 off_t pos
) /* I - Position in file */
1780 ssize_t bytes
; /* Number bytes in buffer */
1783 DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT
")", (void *)fp
, CUPS_LLCAST pos
));
1784 DEBUG_printf(("2cupsFileSeek: fp->pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1785 DEBUG_printf(("2cupsFileSeek: fp->ptr=%p, fp->end=%p", (void *)fp
->ptr
, (void *)fp
->end
));
1788 * Range check input...
1791 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1795 * Handle special cases...
1799 return (cupsFileRewind(fp
));
1803 bytes
= (ssize_t
)(fp
->end
- fp
->buf
);
1805 DEBUG_printf(("2cupsFileSeek: bytes=" CUPS_LLFMT
, CUPS_LLCAST bytes
));
1807 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1810 * No seeking necessary...
1814 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1822 if (!fp
->compressed
&& !fp
->ptr
)
1825 * Preload a buffer to determine whether the file is compressed...
1828 if (cups_fill(fp
) <= 0)
1831 #endif /* HAVE_LIBZ */
1834 * Seek forwards or backwards...
1839 if (pos
< fp
->bufpos
)
1842 * Need to seek backwards...
1845 DEBUG_puts("2cupsFileSeek: SEEK BACKWARDS");
1850 inflateEnd(&fp
->stream
);
1852 lseek(fp
->fd
, 0, SEEK_SET
);
1858 while ((bytes
= cups_fill(fp
)) > 0)
1859 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1865 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1869 #endif /* HAVE_LIBZ */
1871 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1872 fp
->pos
= fp
->bufpos
;
1876 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1877 CUPS_LLCAST fp
->pos
));
1883 * Need to seek forwards...
1886 DEBUG_puts("2cupsFileSeek: SEEK FORWARDS");
1891 while ((bytes
= cups_fill(fp
)) > 0)
1893 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1900 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1904 #endif /* HAVE_LIBZ */
1906 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1907 fp
->pos
= fp
->bufpos
;
1911 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1912 CUPS_LLCAST fp
->pos
));
1916 DEBUG_printf(("2cupsFileSeek: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1923 * 'cupsFileStderr()' - Return a CUPS file associated with stderr.
1925 * @since CUPS 1.2/macOS 10.5@
1928 cups_file_t
* /* O - CUPS file */
1929 cupsFileStderr(void)
1931 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1935 * Open file descriptor 2 as needed...
1938 if (!cg
->stdio_files
[2])
1941 * Flush any pending output on the stdio file...
1947 * Open file descriptor 2...
1950 if ((cg
->stdio_files
[2] = cupsFileOpenFd(2, "w")) != NULL
)
1951 cg
->stdio_files
[2]->is_stdio
= 1;
1954 return (cg
->stdio_files
[2]);
1959 * 'cupsFileStdin()' - Return a CUPS file associated with stdin.
1961 * @since CUPS 1.2/macOS 10.5@
1964 cups_file_t
* /* O - CUPS file */
1967 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1971 * Open file descriptor 0 as needed...
1974 if (!cg
->stdio_files
[0])
1977 * Open file descriptor 0...
1980 if ((cg
->stdio_files
[0] = cupsFileOpenFd(0, "r")) != NULL
)
1981 cg
->stdio_files
[0]->is_stdio
= 1;
1984 return (cg
->stdio_files
[0]);
1989 * 'cupsFileStdout()' - Return a CUPS file associated with stdout.
1991 * @since CUPS 1.2/macOS 10.5@
1994 cups_file_t
* /* O - CUPS file */
1995 cupsFileStdout(void)
1997 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
2001 * Open file descriptor 1 as needed...
2004 if (!cg
->stdio_files
[1])
2007 * Flush any pending output on the stdio file...
2013 * Open file descriptor 1...
2016 if ((cg
->stdio_files
[1] = cupsFileOpenFd(1, "w")) != NULL
)
2017 cg
->stdio_files
[1]->is_stdio
= 1;
2020 return (cg
->stdio_files
[1]);
2025 * 'cupsFileTell()' - Return the current file position.
2027 * @since CUPS 1.2/macOS 10.5@
2030 off_t
/* O - File position */
2031 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
2033 DEBUG_printf(("2cupsFileTell(fp=%p)", (void *)fp
));
2034 DEBUG_printf(("3cupsFileTell: pos=" CUPS_LLFMT
, CUPS_LLCAST (fp
? fp
->pos
: -1)));
2036 return (fp
? fp
->pos
: 0);
2041 * 'cupsFileUnlock()' - Unlock access to a file.
2043 * @since CUPS 1.2/macOS 10.5@
2046 int /* O - 0 on success, -1 on error */
2047 cupsFileUnlock(cups_file_t
*fp
) /* I - CUPS file */
2053 DEBUG_printf(("cupsFileUnlock(fp=%p)", (void *)fp
));
2055 if (!fp
|| fp
->mode
== 's')
2063 return (_locking(fp
->fd
, _LK_UNLCK
, 0));
2065 return (lockf(fp
->fd
, F_ULOCK
, 0));
2071 * 'cupsFileWrite()' - Write to a file.
2073 * @since CUPS 1.2/macOS 10.5@
2076 ssize_t
/* O - Number of bytes written or -1 on error */
2077 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
2078 const char *buf
, /* I - Buffer */
2079 size_t bytes
) /* I - Number of bytes to write */
2082 * Range check input...
2085 DEBUG_printf(("2cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2087 if (!fp
|| !buf
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
2094 * Write the buffer...
2097 if (fp
->mode
== 's')
2099 if (cups_write(fp
, buf
, bytes
) < 0)
2102 fp
->pos
+= (off_t
)bytes
;
2104 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2106 return ((ssize_t
)bytes
);
2109 if ((fp
->ptr
+ bytes
) > fp
->end
)
2110 if (cupsFileFlush(fp
))
2113 fp
->pos
+= (off_t
)bytes
;
2115 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2117 if (bytes
> sizeof(fp
->buf
))
2121 return (cups_compress(fp
, buf
, bytes
));
2123 #endif /* HAVE_LIBZ */
2124 return (cups_write(fp
, buf
, bytes
));
2128 memcpy(fp
->ptr
, buf
, bytes
);
2130 return ((ssize_t
)bytes
);
2137 * 'cups_compress()' - Compress a buffer of data.
2140 static ssize_t
/* O - Number of bytes written or -1 */
2141 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
2142 const char *buf
, /* I - Buffer */
2143 size_t bytes
) /* I - Number bytes */
2145 DEBUG_printf(("7cups_compress(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2151 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, (uInt
)bytes
);
2154 * Deflate the bytes...
2157 fp
->stream
.next_in
= (Bytef
*)buf
;
2158 fp
->stream
.avail_in
= (uInt
)bytes
;
2160 while (fp
->stream
.avail_in
> 0)
2163 * Flush the current buffer...
2166 DEBUG_printf(("9cups_compress: avail_in=%d, avail_out=%d",
2167 fp
->stream
.avail_in
, fp
->stream
.avail_out
));
2169 if (fp
->stream
.avail_out
< (uInt
)(sizeof(fp
->cbuf
) / 8))
2171 if (cups_write(fp
, (char *)fp
->cbuf
, (size_t)(fp
->stream
.next_out
- fp
->cbuf
)) < 0)
2174 fp
->stream
.next_out
= fp
->cbuf
;
2175 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
2178 deflate(&(fp
->stream
), Z_NO_FLUSH
);
2181 return ((ssize_t
)bytes
);
2183 #endif /* HAVE_LIBZ */
2187 * 'cups_fill()' - Fill the input buffer.
2190 static ssize_t
/* O - Number of bytes or -1 */
2191 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
2193 ssize_t bytes
; /* Number of bytes read */
2195 int status
; /* Decompression status */
2196 const unsigned char *ptr
, /* Pointer into buffer */
2197 *end
; /* End of buffer */
2198 #endif /* HAVE_LIBZ */
2201 DEBUG_printf(("7cups_fill(fp=%p)", (void *)fp
));
2202 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
));
2204 if (fp
->ptr
&& fp
->end
)
2205 fp
->bufpos
+= fp
->end
- fp
->buf
;
2208 DEBUG_printf(("9cups_fill: fp->compressed=%d", fp
->compressed
));
2210 while (!fp
->ptr
|| fp
->compressed
)
2213 * Check to see if we have read any data yet; if not, see if we have a
2214 * compressed file...
2220 * Reset the file position in case we are seeking...
2226 * Read the first bytes in the file to determine if we have a gzip'd
2230 if ((bytes
= cups_read(fp
, (char *)fp
->buf
, sizeof(fp
->buf
))) < 0)
2233 * Can't read from file!
2236 DEBUG_printf(("9cups_fill: cups_read() returned " CUPS_LLFMT
,
2237 CUPS_LLCAST bytes
));
2244 if (bytes
< 10 || fp
->buf
[0] != 0x1f ||
2245 (fp
->buf
[1] & 255) != 0x8b ||
2246 fp
->buf
[2] != 8 || (fp
->buf
[3] & 0xe0) != 0)
2249 * Not a gzip'd file!
2253 fp
->end
= fp
->buf
+ bytes
;
2255 DEBUG_printf(("9cups_fill: Returning " CUPS_LLFMT
,
2256 CUPS_LLCAST bytes
));
2262 * Parse header junk: extra data, original name, and comment...
2265 ptr
= (unsigned char *)fp
->buf
+ 10;
2266 end
= (unsigned char *)fp
->buf
+ bytes
;
2268 if (fp
->buf
[3] & 0x04)
2271 * Skip extra data...
2274 if ((ptr
+ 2) > end
)
2277 * Can't read from file!
2280 DEBUG_puts("9cups_fill: Extra gzip header data missing, returning -1.");
2288 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
2294 * Can't read from file!
2297 DEBUG_puts("9cups_fill: Extra gzip header data does not fit in initial buffer, returning -1.");
2306 if (fp
->buf
[3] & 0x08)
2309 * Skip original name data...
2312 while (ptr
< end
&& *ptr
)
2320 * Can't read from file!
2323 DEBUG_puts("9cups_fill: Original filename in gzip header data does not fit in initial buffer, returning -1.");
2332 if (fp
->buf
[3] & 0x10)
2335 * Skip comment data...
2338 while (ptr
< end
&& *ptr
)
2346 * Can't read from file!
2349 DEBUG_puts("9cups_fill: Comment in gzip header data does not fit in initial buffer, returning -1.");
2358 if (fp
->buf
[3] & 0x02)
2361 * Skip header CRC data...
2369 * Can't read from file!
2372 DEBUG_puts("9cups_fill: Header CRC in gzip header data does not fit in initial buffer, returning -1.");
2382 * Copy the flate-compressed data to the compression buffer...
2385 if ((bytes
= end
- ptr
) > 0)
2386 memcpy(fp
->cbuf
, ptr
, (size_t)bytes
);
2389 * Setup the decompressor data...
2392 fp
->stream
.zalloc
= (alloc_func
)0;
2393 fp
->stream
.zfree
= (free_func
)0;
2394 fp
->stream
.opaque
= (voidpf
)0;
2395 fp
->stream
.next_in
= (Bytef
*)fp
->cbuf
;
2396 fp
->stream
.next_out
= NULL
;
2397 fp
->stream
.avail_in
= (uInt
)bytes
;
2398 fp
->stream
.avail_out
= 0;
2399 fp
->crc
= crc32(0L, Z_NULL
, 0);
2401 if ((status
= inflateInit2(&(fp
->stream
), -15)) != Z_OK
)
2403 DEBUG_printf(("9cups_fill: inflateInit2 returned %d, returning -1.", status
));
2417 * If we have reached end-of-file, return immediately...
2422 DEBUG_puts("9cups_fill: EOF, returning 0.");
2428 * Fill the decompression buffer as needed...
2431 if (fp
->stream
.avail_in
== 0)
2433 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
2435 DEBUG_printf(("9cups_fill: cups_read error, returning %d.", (int)bytes
));
2442 fp
->stream
.next_in
= fp
->cbuf
;
2443 fp
->stream
.avail_in
= (uInt
)bytes
;
2447 * Decompress data from the buffer...
2450 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
2451 fp
->stream
.avail_out
= sizeof(fp
->buf
);
2453 status
= inflate(&(fp
->stream
), Z_NO_FLUSH
);
2455 if (fp
->stream
.next_out
> (Bytef
*)fp
->buf
)
2456 fp
->crc
= crc32(fp
->crc
, (Bytef
*)fp
->buf
,
2457 (uInt
)(fp
->stream
.next_out
- (Bytef
*)fp
->buf
));
2459 if (status
== Z_STREAM_END
)
2462 * Read the CRC and length...
2465 unsigned char trailer
[8]; /* Trailer bytes */
2466 uLong tcrc
; /* Trailer CRC */
2467 ssize_t tbytes
= 0; /* Number of bytes */
2469 if (fp
->stream
.avail_in
> 0)
2471 if (fp
->stream
.avail_in
> sizeof(trailer
))
2472 tbytes
= (ssize_t
)sizeof(trailer
);
2474 tbytes
= (ssize_t
)fp
->stream
.avail_in
;
2476 memcpy(trailer
, fp
->stream
.next_in
, (size_t)tbytes
);
2477 fp
->stream
.next_in
+= tbytes
;
2478 fp
->stream
.avail_in
-= (size_t)tbytes
;
2481 if (tbytes
< (ssize_t
)sizeof(trailer
))
2483 if (read(fp
->fd
, trailer
+ tbytes
, sizeof(trailer
) - (size_t)tbytes
) < ((ssize_t
)sizeof(trailer
) - tbytes
))
2486 * Can't get it, so mark end-of-file...
2489 DEBUG_puts("9cups_fill: Unable to read gzip CRC trailer, returning -1.");
2498 tcrc
= ((((((uLong
)trailer
[3] << 8) | (uLong
)trailer
[2]) << 8) |
2499 (uLong
)trailer
[1]) << 8) | (uLong
)trailer
[0];
2501 if (tcrc
!= fp
->crc
)
2504 * Bad CRC, mark end-of-file...
2507 DEBUG_printf(("9cups_fill: tcrc=%08x != fp->crc=%08x, returning -1.", (unsigned int)tcrc
, (unsigned int)fp
->crc
));
2516 * Otherwise, reset the compressed flag so that we re-read the
2520 inflateEnd(&fp
->stream
);
2524 else if (status
< Z_OK
)
2526 DEBUG_printf(("9cups_fill: inflate returned %d, returning -1.", status
));
2534 bytes
= (ssize_t
)sizeof(fp
->buf
) - (ssize_t
)fp
->stream
.avail_out
;
2537 * Return the decompressed data...
2541 fp
->end
= fp
->buf
+ bytes
;
2545 DEBUG_printf(("9cups_fill: Returning %d.", (int)bytes
));
2550 #endif /* HAVE_LIBZ */
2553 * Read a buffer's full of data...
2556 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
2559 * Can't read from file!
2569 * Return the bytes we read...
2574 fp
->end
= fp
->buf
+ bytes
;
2577 DEBUG_printf(("9cups_fill: Not gzip, returning %d.", (int)bytes
));
2584 * 'cups_open()' - Safely open a file for writing.
2586 * We don't allow appending to directories or files that are hard-linked or
2590 static int /* O - File descriptor or -1 otherwise */
2591 cups_open(const char *filename
, /* I - Filename */
2592 int mode
) /* I - Open mode */
2594 int fd
; /* File descriptor */
2595 struct stat fileinfo
; /* File information */
2597 struct stat linkinfo
; /* Link information */
2598 #endif /* !_WIN32 */
2605 if ((fd
= open(filename
, mode
, 0666)) < 0)
2609 * Then verify that the file descriptor doesn't point to a directory or hard-
2613 if (fstat(fd
, &fileinfo
))
2619 if (fileinfo
.st_nlink
!= 1)
2627 if (fileinfo
.st_mode
& _S_IFDIR
)
2629 if (S_ISDIR(fileinfo
.st_mode
))
2639 * Then use lstat to determine whether the filename is a symlink...
2642 if (lstat(filename
, &linkinfo
))
2648 if (S_ISLNK(linkinfo
.st_mode
) ||
2649 fileinfo
.st_dev
!= linkinfo
.st_dev
||
2650 fileinfo
.st_ino
!= linkinfo
.st_ino
||
2652 fileinfo
.st_gen
!= linkinfo
.st_gen
||
2653 #endif /* HAVE_ST_GEN */
2654 fileinfo
.st_nlink
!= linkinfo
.st_nlink
||
2655 fileinfo
.st_mode
!= linkinfo
.st_mode
)
2665 #endif /* !_WIN32 */
2672 * 'cups_read()' - Read from a file descriptor.
2675 static ssize_t
/* O - Number of bytes read or -1 */
2676 cups_read(cups_file_t
*fp
, /* I - CUPS file */
2677 char *buf
, /* I - Buffer */
2678 size_t bytes
) /* I - Number bytes */
2680 ssize_t total
; /* Total bytes read */
2683 DEBUG_printf(("7cups_read(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2686 * Loop until we read at least 0 bytes...
2692 if (fp
->mode
== 's')
2693 total
= (ssize_t
)recv(fp
->fd
, buf
, (unsigned)bytes
, 0);
2695 total
= (ssize_t
)read(fp
->fd
, buf
, (unsigned)bytes
);
2697 if (fp
->mode
== 's')
2698 total
= recv(fp
->fd
, buf
, bytes
, 0);
2700 total
= read(fp
->fd
, buf
, bytes
);
2703 DEBUG_printf(("9cups_read: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
2709 * Reads can be interrupted by signals and unavailable resources...
2712 if (errno
== EAGAIN
|| errno
== EINTR
)
2719 * Return the total number of bytes read...
2727 * 'cups_write()' - Write to a file descriptor.
2730 static ssize_t
/* O - Number of bytes written or -1 */
2731 cups_write(cups_file_t
*fp
, /* I - CUPS file */
2732 const char *buf
, /* I - Buffer */
2733 size_t bytes
) /* I - Number bytes */
2735 size_t total
; /* Total bytes written */
2736 ssize_t count
; /* Count this time */
2739 DEBUG_printf(("7cups_write(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2742 * Loop until all bytes are written...
2749 if (fp
->mode
== 's')
2750 count
= (ssize_t
)send(fp
->fd
, buf
, (unsigned)bytes
, 0);
2752 count
= (ssize_t
)write(fp
->fd
, buf
, (unsigned)bytes
);
2754 if (fp
->mode
== 's')
2755 count
= send(fp
->fd
, buf
, bytes
, 0);
2757 count
= write(fp
->fd
, buf
, bytes
);
2760 DEBUG_printf(("9cups_write: count=" CUPS_LLFMT
, CUPS_LLCAST count
));
2765 * Writes can be interrupted by signals and unavailable resources...
2768 if (errno
== EAGAIN
|| errno
== EINTR
)
2775 * Update the counts for the last write call...
2778 bytes
-= (size_t)count
;
2779 total
+= (size_t)count
;
2784 * Return the total number of bytes written...
2787 return ((ssize_t
)total
);