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-2019 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"
21 #include "debug-internal.h"
23 #include <sys/types.h>
27 # endif /* HAVE_LIBZ */
31 * Internal structures...
34 struct _cups_file_s
/**** CUPS file structure... ****/
37 int fd
; /* File descriptor */
38 char mode
, /* Mode ('r' or 'w') */
39 compressed
, /* Compression used? */
40 is_stdio
, /* stdin/out/err? */
41 eof
, /* End of file? */
42 buf
[4096], /* Buffer */
43 *ptr
, /* Pointer into buffer */
44 *end
; /* End of buffer data */
45 off_t pos
, /* Position in file */
46 bufpos
; /* File position for start of buffer */
49 z_stream stream
; /* (De)compression stream */
50 Bytef cbuf
[4096]; /* (De)compression buffer */
51 uLong crc
; /* (De)compression CRC */
52 #endif /* HAVE_LIBZ */
54 char *printf_buffer
; /* cupsFilePrintf buffer */
55 size_t printf_size
; /* Size of cupsFilePrintf buffer */
64 static ssize_t
cups_compress(cups_file_t
*fp
, const char *buf
, size_t bytes
);
65 #endif /* HAVE_LIBZ */
66 static ssize_t
cups_fill(cups_file_t
*fp
);
67 static int cups_open(const char *filename
, int mode
);
68 static ssize_t
cups_read(cups_file_t
*fp
, char *buf
, size_t bytes
);
69 static ssize_t
cups_write(cups_file_t
*fp
, const char *buf
, size_t bytes
);
74 * '_cupsFileCheck()' - Check the permissions of the given filename.
77 _cups_fc_result_t
/* O - Check result */
79 const char *filename
, /* I - Filename to check */
80 _cups_fc_filetype_t filetype
, /* I - Type of file checks? */
81 int dorootchecks
, /* I - Check for root permissions? */
82 _cups_fc_func_t cb
, /* I - Callback function */
83 void *context
) /* I - Context pointer for callback */
86 struct stat fileinfo
; /* File information */
87 char message
[1024], /* Message string */
88 temp
[1024], /* Parent directory filename */
89 *ptr
; /* Pointer into parent directory */
90 _cups_fc_result_t result
; /* Check result */
94 * Does the filename contain a relative path ("../")?
97 if (strstr(filename
, "../"))
103 result
= _CUPS_FILE_CHECK_RELATIVE_PATH
;
108 * Does the program even exist and is it accessible?
111 if (stat(filename
, &fileinfo
))
117 result
= _CUPS_FILE_CHECK_MISSING
;
122 * Check the execute bit...
125 result
= _CUPS_FILE_CHECK_OK
;
129 case _CUPS_FILE_CHECK_DIRECTORY
:
130 if (!S_ISDIR(fileinfo
.st_mode
))
131 result
= _CUPS_FILE_CHECK_WRONG_TYPE
;
135 if (!S_ISREG(fileinfo
.st_mode
))
136 result
= _CUPS_FILE_CHECK_WRONG_TYPE
;
144 * Are we doing root checks?
150 * Nope, so anything (else) goes...
157 * Verify permission of the file itself:
159 * 1. Must be owned by root
160 * 2. Must not be writable by group
161 * 3. Must not be setuid
162 * 4. Must not be writable by others
165 if (fileinfo
.st_uid
|| /* 1. Must be owned by root */
166 (fileinfo
.st_mode
& S_IWGRP
) || /* 2. Must not be writable by group */
167 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
168 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
170 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
174 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
||
175 filetype
== _CUPS_FILE_CHECK_FILE_ONLY
)
179 * Now check the containing directory...
182 strlcpy(temp
, filename
, sizeof(temp
));
183 if ((ptr
= strrchr(temp
, '/')) != NULL
)
191 if (stat(temp
, &fileinfo
))
197 result
= _CUPS_FILE_CHECK_MISSING
;
198 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
204 if (fileinfo
.st_uid
|| /* 1. Must be owned by root */
205 (fileinfo
.st_mode
& S_IWGRP
) || /* 2. Must not be writable by group */
206 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
207 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
209 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
210 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
215 * Common return point...
222 cups_lang_t
*lang
= cupsLangDefault();
223 /* Localization information */
227 case _CUPS_FILE_CHECK_OK
:
228 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
229 snprintf(message
, sizeof(message
),
230 _cupsLangString(lang
, _("Directory \"%s\" permissions OK "
231 "(0%o/uid=%d/gid=%d).")),
232 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
233 (int)fileinfo
.st_gid
);
235 snprintf(message
, sizeof(message
),
236 _cupsLangString(lang
, _("File \"%s\" permissions OK "
237 "(0%o/uid=%d/gid=%d).")),
238 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
239 (int)fileinfo
.st_gid
);
242 case _CUPS_FILE_CHECK_MISSING
:
243 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
244 snprintf(message
, sizeof(message
),
245 _cupsLangString(lang
, _("Directory \"%s\" not available: "
247 filename
, strerror(errno
));
249 snprintf(message
, sizeof(message
),
250 _cupsLangString(lang
, _("File \"%s\" not available: %s")),
251 filename
, strerror(errno
));
254 case _CUPS_FILE_CHECK_PERMISSIONS
:
255 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
256 snprintf(message
, sizeof(message
),
257 _cupsLangString(lang
, _("Directory \"%s\" has insecure "
259 "(0%o/uid=%d/gid=%d).")),
260 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
261 (int)fileinfo
.st_gid
);
263 snprintf(message
, sizeof(message
),
264 _cupsLangString(lang
, _("File \"%s\" has insecure "
266 "(0%o/uid=%d/gid=%d).")),
267 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
268 (int)fileinfo
.st_gid
);
271 case _CUPS_FILE_CHECK_WRONG_TYPE
:
272 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
273 snprintf(message
, sizeof(message
),
274 _cupsLangString(lang
, _("Directory \"%s\" is a file.")),
277 snprintf(message
, sizeof(message
),
278 _cupsLangString(lang
, _("File \"%s\" is a directory.")),
282 case _CUPS_FILE_CHECK_RELATIVE_PATH
:
283 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
284 snprintf(message
, sizeof(message
),
285 _cupsLangString(lang
, _("Directory \"%s\" contains a "
286 "relative path.")), filename
);
288 snprintf(message
, sizeof(message
),
289 _cupsLangString(lang
, _("File \"%s\" contains a relative "
290 "path.")), filename
);
294 (*cb
)(context
, result
, message
);
302 * '_cupsFileCheckFilter()' - Report file check results as CUPS filter messages.
306 _cupsFileCheckFilter(
307 void *context
, /* I - Context pointer (unused) */
308 _cups_fc_result_t result
, /* I - Result code */
309 const char *message
) /* I - Message text */
311 const char *prefix
; /* Messaging prefix */
319 case _CUPS_FILE_CHECK_OK
:
323 case _CUPS_FILE_CHECK_MISSING
:
324 case _CUPS_FILE_CHECK_WRONG_TYPE
:
326 fputs("STATE: +cups-missing-filter-warning\n", stderr
);
329 case _CUPS_FILE_CHECK_PERMISSIONS
:
330 case _CUPS_FILE_CHECK_RELATIVE_PATH
:
332 fputs("STATE: +cups-insecure-filter-warning\n", stderr
);
336 fprintf(stderr
, "%s: %s\n", prefix
, message
);
342 * 'cupsFileClose()' - Close a CUPS file.
344 * @since CUPS 1.2/macOS 10.5@
347 int /* O - 0 on success, -1 on error */
348 cupsFileClose(cups_file_t
*fp
) /* I - CUPS file */
350 int fd
; /* File descriptor */
351 char mode
; /* Open mode */
352 int status
; /* Return status */
355 DEBUG_printf(("cupsFileClose(fp=%p)", (void *)fp
));
365 * Flush pending write data...
369 status
= cupsFileFlush(fp
);
374 if (fp
->compressed
&& status
>= 0)
379 * Free decompression data...
382 inflateEnd(&fp
->stream
);
387 * Flush any remaining compressed data...
390 unsigned char trailer
[8]; /* Trailer CRC and length */
391 int done
; /* Done writing... */
394 fp
->stream
.avail_in
= 0;
398 if (fp
->stream
.next_out
> fp
->cbuf
)
400 if (cups_write(fp
, (char *)fp
->cbuf
,
401 (size_t)(fp
->stream
.next_out
- fp
->cbuf
)) < 0)
404 fp
->stream
.next_out
= fp
->cbuf
;
405 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
408 if (done
|| status
< 0)
411 done
= deflate(&fp
->stream
, Z_FINISH
) == Z_STREAM_END
&&
412 fp
->stream
.next_out
== fp
->cbuf
;
416 * Write the CRC and length...
419 trailer
[0] = (unsigned char)fp
->crc
;
420 trailer
[1] = (unsigned char)(fp
->crc
>> 8);
421 trailer
[2] = (unsigned char)(fp
->crc
>> 16);
422 trailer
[3] = (unsigned char)(fp
->crc
>> 24);
423 trailer
[4] = (unsigned char)fp
->pos
;
424 trailer
[5] = (unsigned char)(fp
->pos
>> 8);
425 trailer
[6] = (unsigned char)(fp
->pos
>> 16);
426 trailer
[7] = (unsigned char)(fp
->pos
>> 24);
428 if (cups_write(fp
, (char *)trailer
, 8) < 0)
432 * Free all memory used by the compression stream...
435 deflateEnd(&(fp
->stream
));
438 #endif /* HAVE_LIBZ */
441 * If this is one of the cupsFileStdin/out/err files, return now and don't
442 * actually free memory or close (these last the life of the process...)
449 * Save the file descriptor we used and free memory...
455 if (fp
->printf_buffer
)
456 free(fp
->printf_buffer
);
461 * Close the file, returning the close status...
466 if (httpAddrClose(NULL
, fd
) < 0)
469 else if (close(fd
) < 0)
477 * 'cupsFileCompression()' - Return whether a file is compressed.
479 * @since CUPS 1.2/macOS 10.5@
482 int /* O - @code CUPS_FILE_NONE@ or @code CUPS_FILE_GZIP@ */
483 cupsFileCompression(cups_file_t
*fp
) /* I - CUPS file */
485 return (fp
? fp
->compressed
: CUPS_FILE_NONE
);
490 * 'cupsFileEOF()' - Return the end-of-file status.
492 * @since CUPS 1.2/macOS 10.5@
495 int /* O - 1 on end of file, 0 otherwise */
496 cupsFileEOF(cups_file_t
*fp
) /* I - CUPS file */
498 return (fp
? fp
->eof
: 1);
503 * 'cupsFileFind()' - Find a file using the specified path.
505 * This function allows the paths in the path string to be separated by
506 * colons (UNIX standard) or semicolons (Windows standard) and stores the
507 * result in the buffer supplied. If the file cannot be found in any of
508 * the supplied paths, @code NULL@ is returned. A @code NULL@ path only
509 * matches the current directory.
511 * @since CUPS 1.2/macOS 10.5@
514 const char * /* O - Full path to file or @code NULL@ if not found */
515 cupsFileFind(const char *filename
, /* I - File to find */
516 const char *path
, /* I - Colon/semicolon-separated path */
517 int executable
, /* I - 1 = executable files, 0 = any file/dir */
518 char *buffer
, /* I - Filename buffer */
519 int bufsize
) /* I - Size of filename buffer */
521 char *bufptr
, /* Current position in buffer */
522 *bufend
; /* End of buffer */
526 * Range check input...
529 DEBUG_printf(("cupsFileFind(filename=\"%s\", path=\"%s\", executable=%d, buffer=%p, bufsize=%d)", filename
, path
, executable
, (void *)buffer
, bufsize
));
531 if (!filename
|| !buffer
|| bufsize
< 2)
537 * No path, so check current directory...
540 if (!access(filename
, 0))
542 strlcpy(buffer
, filename
, (size_t)bufsize
);
550 * Now check each path and return the first match...
553 bufend
= buffer
+ bufsize
- 1;
559 if (*path
== ';' || (*path
== ':' && ((bufptr
- buffer
) > 1 || !isalpha(buffer
[0] & 255))))
561 if (*path
== ';' || *path
== ':')
564 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
567 strlcpy(bufptr
, filename
, (size_t)(bufend
- bufptr
));
570 if (!access(buffer
, 0))
572 if (!access(buffer
, executable
? X_OK
: 0))
575 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
581 else if (bufptr
< bufend
)
588 * Check the last path...
591 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
594 strlcpy(bufptr
, filename
, (size_t)(bufend
- bufptr
));
596 if (!access(buffer
, 0))
598 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
603 DEBUG_puts("1cupsFileFind: Returning NULL");
610 * 'cupsFileFlush()' - Flush pending output.
612 * @since CUPS 1.2/macOS 10.5@
615 int /* O - 0 on success, -1 on error */
616 cupsFileFlush(cups_file_t
*fp
) /* I - CUPS file */
618 ssize_t bytes
; /* Bytes to write */
621 DEBUG_printf(("cupsFileFlush(fp=%p)", (void *)fp
));
624 * Range check input...
627 if (!fp
|| fp
->mode
!= 'w')
629 DEBUG_puts("1cupsFileFlush: Attempt to flush a read-only file...");
633 bytes
= (ssize_t
)(fp
->ptr
- fp
->buf
);
635 DEBUG_printf(("2cupsFileFlush: Flushing " CUPS_LLFMT
" bytes...",
642 bytes
= cups_compress(fp
, fp
->buf
, (size_t)bytes
);
644 #endif /* HAVE_LIBZ */
645 bytes
= cups_write(fp
, fp
->buf
, (size_t)bytes
);
658 * 'cupsFileGetChar()' - Get a single character from a file.
660 * @since CUPS 1.2/macOS 10.5@
663 int /* O - Character or -1 on end of file */
664 cupsFileGetChar(cups_file_t
*fp
) /* I - CUPS file */
667 * Range check input...
670 DEBUG_printf(("4cupsFileGetChar(fp=%p)", (void *)fp
));
672 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
674 DEBUG_puts("5cupsFileGetChar: Bad arguments!");
680 DEBUG_puts("5cupsFileGetChar: End-of-file!");
685 * If the input buffer is empty, try to read more data...
688 DEBUG_printf(("5cupsFileGetChar: fp->eof=%d, fp->ptr=%p, fp->end=%p", fp
->eof
, (void *)fp
->ptr
, (void *)fp
->end
));
690 if (fp
->ptr
>= fp
->end
)
691 if (cups_fill(fp
) <= 0)
693 DEBUG_puts("5cupsFileGetChar: Unable to fill buffer!");
698 * Return the next character in the buffer...
701 DEBUG_printf(("5cupsFileGetChar: Returning %d...", *(fp
->ptr
) & 255));
705 DEBUG_printf(("6cupsFileGetChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
707 return (*(fp
->ptr
)++ & 255);
712 * 'cupsFileGetConf()' - Get a line from a configuration file.
714 * @since CUPS 1.2/macOS 10.5@
717 char * /* O - Line read or @code NULL@ on end of file or error */
718 cupsFileGetConf(cups_file_t
*fp
, /* I - CUPS file */
719 char *buf
, /* O - String buffer */
720 size_t buflen
, /* I - Size of string buffer */
721 char **value
, /* O - Pointer to value */
722 int *linenum
) /* IO - Current line number */
724 char *ptr
; /* Pointer into line */
728 * Range check input...
731 DEBUG_printf(("2cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT
732 ", value=%p, linenum=%p)", (void *)fp
, (void *)buf
, CUPS_LLCAST buflen
, (void *)value
, (void *)linenum
));
734 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') ||
735 !buf
|| buflen
< 2 || !value
)
744 * Read the next non-comment line...
749 while (cupsFileGets(fp
, buf
, buflen
))
754 * Strip any comments...
757 if ((ptr
= strchr(buf
, '#')) != NULL
)
759 if (ptr
> buf
&& ptr
[-1] == '\\')
762 _cups_strcpy(ptr
- 1, ptr
);
766 // Strip the comment and any trailing whitespace...
769 if (!_cups_isspace(ptr
[-1]))
780 * Strip leading whitespace...
783 for (ptr
= buf
; _cups_isspace(*ptr
); ptr
++);
786 _cups_strcpy(buf
, ptr
);
789 * See if there is anything left...
795 * Yes, grab any value and return...
798 for (ptr
= buf
; *ptr
; ptr
++)
799 if (_cups_isspace(*ptr
))
805 * Have a value, skip any other spaces...
808 while (_cups_isspace(*ptr
))
815 * Strip trailing whitespace and > for lines that begin with <...
818 ptr
+= strlen(ptr
) - 1;
820 if (buf
[0] == '<' && *ptr
== '>')
822 else if (buf
[0] == '<' && *ptr
!= '>')
832 while (ptr
> *value
&& _cups_isspace(*ptr
))
849 * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may
850 * contain binary data.
852 * This function differs from @link cupsFileGets@ in that the trailing CR
853 * and LF are preserved, as is any binary data on the line. The buffer is
854 * nul-terminated, however you should use the returned length to determine
855 * the number of bytes on the line.
857 * @since CUPS 1.2/macOS 10.5@
860 size_t /* O - Number of bytes on line or 0 on end of file */
861 cupsFileGetLine(cups_file_t
*fp
, /* I - File to read from */
862 char *buf
, /* I - Buffer */
863 size_t buflen
) /* I - Size of buffer */
865 int ch
; /* Character from file */
866 char *ptr
, /* Current position in line buffer */
867 *end
; /* End of line buffer */
871 * Range check input...
874 DEBUG_printf(("2cupsFileGetLine(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST buflen
));
876 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 3)
880 * Now loop until we have a valid line...
883 for (ptr
= buf
, end
= buf
+ buflen
- 2; ptr
< end
;)
885 if (fp
->ptr
>= fp
->end
)
886 if (cups_fill(fp
) <= 0)
889 *ptr
++ = ch
= *(fp
->ptr
)++;
898 if (fp
->ptr
>= fp
->end
)
899 if (cups_fill(fp
) <= 0)
902 if (*(fp
->ptr
) == '\n')
904 *ptr
++ = *(fp
->ptr
)++;
913 * Line feed ends a line...
922 DEBUG_printf(("4cupsFileGetLine: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
924 return ((size_t)(ptr
- buf
));
929 * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
931 * @since CUPS 1.2/macOS 10.5@
934 char * /* O - Line read or @code NULL@ on end of file or error */
935 cupsFileGets(cups_file_t
*fp
, /* I - CUPS file */
936 char *buf
, /* O - String buffer */
937 size_t buflen
) /* I - Size of string buffer */
939 int ch
; /* Character from file */
940 char *ptr
, /* Current position in line buffer */
941 *end
; /* End of line buffer */
945 * Range check input...
948 DEBUG_printf(("2cupsFileGets(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST buflen
));
950 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 2)
954 * Now loop until we have a valid line...
957 for (ptr
= buf
, end
= buf
+ buflen
- 1; ptr
< end
;)
959 if (fp
->ptr
>= fp
->end
)
960 if (cups_fill(fp
) <= 0)
977 if (fp
->ptr
>= fp
->end
)
978 if (cups_fill(fp
) <= 0)
981 if (*(fp
->ptr
) == '\n')
992 * Line feed ends a line...
1003 DEBUG_printf(("4cupsFileGets: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1010 * 'cupsFileLock()' - Temporarily lock access to a file.
1012 * @since CUPS 1.2/macOS 10.5@
1015 int /* O - 0 on success, -1 on error */
1016 cupsFileLock(cups_file_t
*fp
, /* I - CUPS file */
1017 int block
) /* I - 1 to wait for the lock, 0 to fail right away */
1023 if (!fp
|| fp
->mode
== 's')
1031 return (_locking(fp
->fd
, block
? _LK_LOCK
: _LK_NBLCK
, 0));
1033 return (lockf(fp
->fd
, block
? F_LOCK
: F_TLOCK
, 0));
1039 * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
1041 * @since CUPS 1.2/macOS 10.5@
1044 int /* O - File descriptor */
1045 cupsFileNumber(cups_file_t
*fp
) /* I - CUPS file */
1055 * 'cupsFileOpen()' - Open a CUPS file.
1057 * The "mode" parameter can be "r" to read, "w" to write, overwriting any
1058 * existing file, "a" to append to an existing file or create a new file,
1059 * or "s" to open a socket connection.
1061 * When opening for writing ("w"), an optional number from 1 to 9 can be
1062 * supplied which enables Flate compression of the file. Compression is
1063 * not supported for the "a" (append) mode.
1065 * When opening a socket connection, the filename is a string of the form
1066 * "address:port" or "hostname:port". The socket will make an IPv4 or IPv6
1067 * connection as needed, generally preferring IPv6 connections when there is
1070 * @since CUPS 1.2/macOS 10.5@
1073 cups_file_t
* /* O - CUPS file or @code NULL@ if the file or socket cannot be opened */
1074 cupsFileOpen(const char *filename
, /* I - Name of file */
1075 const char *mode
) /* I - Open mode */
1077 cups_file_t
*fp
; /* New CUPS file */
1078 int fd
; /* File descriptor */
1079 char hostname
[1024], /* Hostname */
1080 *portname
; /* Port "name" (number or service) */
1081 http_addrlist_t
*addrlist
; /* Host address list */
1084 DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")", filename
,
1088 * Range check input...
1091 if (!filename
|| !mode
||
1092 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1093 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1102 case 'a' : /* Append file */
1103 fd
= cups_open(filename
,
1104 O_RDWR
| O_CREAT
| O_APPEND
| O_LARGEFILE
| O_BINARY
);
1107 case 'r' : /* Read file */
1108 fd
= open(filename
, O_RDONLY
| O_LARGEFILE
| O_BINARY
, 0);
1111 case 'w' : /* Write file */
1112 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1113 if (fd
< 0 && errno
== ENOENT
)
1115 fd
= cups_open(filename
,
1116 O_WRONLY
| O_CREAT
| O_EXCL
| O_LARGEFILE
| O_BINARY
);
1117 if (fd
< 0 && errno
== EEXIST
)
1118 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1129 case 's' : /* Read/write socket */
1130 strlcpy(hostname
, filename
, sizeof(hostname
));
1131 if ((portname
= strrchr(hostname
, ':')) != NULL
)
1137 * Lookup the hostname and service...
1140 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
1144 * Connect to the server...
1147 if (!httpAddrConnect(addrlist
, &fd
))
1149 httpAddrFreeList(addrlist
);
1153 httpAddrFreeList(addrlist
);
1156 default : /* Remove bogus compiler warning... */
1164 * Create the CUPS file structure...
1167 if ((fp
= cupsFileOpenFd(fd
, mode
)) == NULL
)
1170 httpAddrClose(NULL
, fd
);
1183 * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
1185 * The "mode" parameter can be "r" to read, "w" to write, "a" to append,
1186 * or "s" to treat the file descriptor as a bidirectional socket connection.
1188 * When opening for writing ("w"), an optional number from 1 to 9 can be
1189 * supplied which enables Flate compression of the file. Compression is
1190 * not supported for the "a" (append) mode.
1192 * @since CUPS 1.2/macOS 10.5@
1195 cups_file_t
* /* O - CUPS file or @code NULL@ if the file could not be opened */
1196 cupsFileOpenFd(int fd
, /* I - File descriptor */
1197 const char *mode
) /* I - Open mode */
1199 cups_file_t
*fp
; /* New CUPS file */
1202 DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")", fd
, mode
));
1205 * Range check input...
1208 if (fd
< 0 || !mode
||
1209 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1210 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1214 * Allocate memory...
1217 if ((fp
= calloc(1, sizeof(cups_file_t
))) == NULL
)
1229 fp
->pos
= lseek(fd
, 0, SEEK_END
);
1234 fp
->end
= fp
->buf
+ sizeof(fp
->buf
);
1237 if (mode
[1] >= '1' && mode
[1] <= '9')
1240 * Open a compressed stream, so write the standard gzip file
1244 unsigned char header
[10]; /* gzip file header */
1245 time_t curtime
; /* Current time */
1248 curtime
= time(NULL
);
1251 header
[2] = Z_DEFLATED
;
1253 header
[4] = (unsigned char)curtime
;
1254 header
[5] = (unsigned char)(curtime
>> 8);
1255 header
[6] = (unsigned char)(curtime
>> 16);
1256 header
[7] = (unsigned char)(curtime
>> 24);
1260 cups_write(fp
, (char *)header
, 10);
1263 * Initialize the compressor...
1266 deflateInit2(&(fp
->stream
), mode
[1] - '0', Z_DEFLATED
, -15, 8,
1267 Z_DEFAULT_STRATEGY
);
1269 fp
->stream
.next_out
= fp
->cbuf
;
1270 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
1272 fp
->crc
= crc32(0L, Z_NULL
, 0);
1274 #endif /* HAVE_LIBZ */
1285 default : /* Remove bogus compiler warning... */
1290 * Don't pass this file to child processes...
1294 fcntl(fp
->fd
, F_SETFD
, fcntl(fp
->fd
, F_GETFD
) | FD_CLOEXEC
);
1295 #endif /* !_WIN32 */
1302 * '_cupsFilePeekAhead()' - See if the requested character is buffered up.
1305 int /* O - 1 if present, 0 otherwise */
1306 _cupsFilePeekAhead(cups_file_t
*fp
, /* I - CUPS file */
1307 int ch
) /* I - Character */
1309 return (fp
&& fp
->ptr
&& memchr(fp
->ptr
, ch
, (size_t)(fp
->end
- fp
->ptr
)));
1314 * 'cupsFilePeekChar()' - Peek at the next character from a file.
1316 * @since CUPS 1.2/macOS 10.5@
1319 int /* O - Character or -1 on end of file */
1320 cupsFilePeekChar(cups_file_t
*fp
) /* I - CUPS file */
1323 * Range check input...
1326 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1330 * If the input buffer is empty, try to read more data...
1333 if (fp
->ptr
>= fp
->end
)
1334 if (cups_fill(fp
) <= 0)
1338 * Return the next character in the buffer...
1341 return (*(fp
->ptr
) & 255);
1346 * 'cupsFilePrintf()' - Write a formatted string.
1348 * @since CUPS 1.2/macOS 10.5@
1351 int /* O - Number of bytes written or -1 on error */
1352 cupsFilePrintf(cups_file_t
*fp
, /* I - CUPS file */
1353 const char *format
, /* I - Printf-style format string */
1354 ...) /* I - Additional args as necessary */
1356 va_list ap
; /* Argument list */
1357 ssize_t bytes
; /* Formatted size */
1360 DEBUG_printf(("2cupsFilePrintf(fp=%p, format=\"%s\", ...)", (void *)fp
, format
));
1362 if (!fp
|| !format
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1365 if (!fp
->printf_buffer
)
1368 * Start with an 1k printf buffer...
1371 if ((fp
->printf_buffer
= malloc(1024)) == NULL
)
1374 fp
->printf_size
= 1024;
1377 va_start(ap
, format
);
1378 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1381 if (bytes
>= (ssize_t
)fp
->printf_size
)
1384 * Expand the printf buffer...
1387 char *temp
; /* Temporary buffer pointer */
1393 if ((temp
= realloc(fp
->printf_buffer
, (size_t)(bytes
+ 1))) == NULL
)
1396 fp
->printf_buffer
= temp
;
1397 fp
->printf_size
= (size_t)(bytes
+ 1);
1399 va_start(ap
, format
);
1400 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1404 if (fp
->mode
== 's')
1406 if (cups_write(fp
, fp
->printf_buffer
, (size_t)bytes
) < 0)
1411 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1413 return ((int)bytes
);
1416 if ((fp
->ptr
+ bytes
) > fp
->end
)
1417 if (cupsFileFlush(fp
))
1422 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1424 if ((size_t)bytes
> sizeof(fp
->buf
))
1428 return ((int)cups_compress(fp
, fp
->printf_buffer
, (size_t)bytes
));
1430 #endif /* HAVE_LIBZ */
1431 return ((int)cups_write(fp
, fp
->printf_buffer
, (size_t)bytes
));
1435 memcpy(fp
->ptr
, fp
->printf_buffer
, (size_t)bytes
);
1438 if (fp
->is_stdio
&& cupsFileFlush(fp
))
1441 return ((int)bytes
);
1447 * 'cupsFilePutChar()' - Write a character.
1449 * @since CUPS 1.2/macOS 10.5@
1452 int /* O - 0 on success, -1 on error */
1453 cupsFilePutChar(cups_file_t
*fp
, /* I - CUPS file */
1454 int c
) /* I - Character to write */
1457 * Range check input...
1460 if (!fp
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1463 if (fp
->mode
== 's')
1466 * Send character immediately over socket...
1469 char ch
; /* Output character */
1474 if (send(fp
->fd
, &ch
, 1, 0) < 1)
1483 if (fp
->ptr
>= fp
->end
)
1484 if (cupsFileFlush(fp
))
1487 *(fp
->ptr
) ++ = (char)c
;
1492 DEBUG_printf(("4cupsFilePutChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1499 * 'cupsFilePutConf()' - Write a configuration line.
1501 * This function handles any comment escaping of the value.
1503 * @since CUPS 1.4/macOS 10.6@
1506 ssize_t
/* O - Number of bytes written or -1 on error */
1507 cupsFilePutConf(cups_file_t
*fp
, /* I - CUPS file */
1508 const char *directive
, /* I - Directive */
1509 const char *value
) /* I - Value */
1511 ssize_t bytes
, /* Number of bytes written */
1512 temp
; /* Temporary byte count */
1513 const char *ptr
; /* Pointer into value */
1516 if (!fp
|| !directive
|| !*directive
)
1519 if ((bytes
= cupsFilePuts(fp
, directive
)) < 0)
1522 if (cupsFilePutChar(fp
, ' ') < 0)
1526 if (value
&& *value
)
1528 if ((ptr
= strchr(value
, '#')) != NULL
)
1531 * Need to quote the first # in the info string...
1534 if ((temp
= cupsFileWrite(fp
, value
, (size_t)(ptr
- value
))) < 0)
1538 if (cupsFilePutChar(fp
, '\\') < 0)
1542 if ((temp
= cupsFilePuts(fp
, ptr
)) < 0)
1546 else if ((temp
= cupsFilePuts(fp
, value
)) < 0)
1552 if (cupsFilePutChar(fp
, '\n') < 0)
1560 * 'cupsFilePuts()' - Write a string.
1562 * Like the @code fputs@ function, no newline is appended to the string.
1564 * @since CUPS 1.2/macOS 10.5@
1567 int /* O - Number of bytes written or -1 on error */
1568 cupsFilePuts(cups_file_t
*fp
, /* I - CUPS file */
1569 const char *s
) /* I - String to write */
1571 ssize_t bytes
; /* Bytes to write */
1575 * Range check input...
1578 if (!fp
|| !s
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1582 * Write the string...
1585 bytes
= (ssize_t
)strlen(s
);
1587 if (fp
->mode
== 's')
1589 if (cups_write(fp
, s
, (size_t)bytes
) < 0)
1594 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1596 return ((int)bytes
);
1599 if ((fp
->ptr
+ bytes
) > fp
->end
)
1600 if (cupsFileFlush(fp
))
1605 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1607 if ((size_t)bytes
> sizeof(fp
->buf
))
1611 return ((int)cups_compress(fp
, s
, (size_t)bytes
));
1613 #endif /* HAVE_LIBZ */
1614 return ((int)cups_write(fp
, s
, (size_t)bytes
));
1618 memcpy(fp
->ptr
, s
, (size_t)bytes
);
1621 if (fp
->is_stdio
&& cupsFileFlush(fp
))
1624 return ((int)bytes
);
1630 * 'cupsFileRead()' - Read from a file.
1632 * @since CUPS 1.2/macOS 10.5@
1635 ssize_t
/* O - Number of bytes read or -1 on error */
1636 cupsFileRead(cups_file_t
*fp
, /* I - CUPS file */
1637 char *buf
, /* O - Buffer */
1638 size_t bytes
) /* I - Number of bytes to read */
1640 size_t total
; /* Total bytes read */
1641 ssize_t count
; /* Bytes read */
1644 DEBUG_printf(("2cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
1647 * Range check input...
1650 if (!fp
|| !buf
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1658 DEBUG_puts("5cupsFileRead: End-of-file!");
1663 * Loop until all bytes are read...
1669 if (fp
->ptr
>= fp
->end
)
1670 if (cups_fill(fp
) <= 0)
1672 DEBUG_printf(("4cupsFileRead: cups_fill() returned -1, total="
1673 CUPS_LLFMT
, CUPS_LLCAST total
));
1676 return ((ssize_t
)total
);
1681 count
= (ssize_t
)(fp
->end
- fp
->ptr
);
1682 if (count
> (ssize_t
)bytes
)
1683 count
= (ssize_t
)bytes
;
1685 memcpy(buf
, fp
->ptr
,(size_t) count
);
1689 DEBUG_printf(("4cupsFileRead: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1692 * Update the counts for the last read...
1695 bytes
-= (size_t)count
;
1696 total
+= (size_t)count
;
1701 * Return the total number of bytes read...
1704 DEBUG_printf(("3cupsFileRead: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
1706 return ((ssize_t
)total
);
1711 * 'cupsFileRewind()' - Set the current file position to the beginning of the
1714 * @since CUPS 1.2/macOS 10.5@
1717 off_t
/* O - New file position or -1 on error */
1718 cupsFileRewind(cups_file_t
*fp
) /* I - CUPS file */
1721 * Range check input...
1724 DEBUG_printf(("cupsFileRewind(fp=%p)", (void *)fp
));
1725 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1727 if (!fp
|| fp
->mode
!= 'r')
1731 * Handle special cases...
1734 if (fp
->bufpos
== 0)
1737 * No seeking necessary...
1748 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1754 * Otherwise, seek in the file and cleanup any compression buffers...
1760 inflateEnd(&fp
->stream
);
1763 #endif /* HAVE_LIBZ */
1765 if (lseek(fp
->fd
, 0, SEEK_SET
))
1767 DEBUG_printf(("1cupsFileRewind: lseek failed: %s", strerror(errno
)));
1777 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1784 * 'cupsFileSeek()' - Seek in a file.
1786 * @since CUPS 1.2/macOS 10.5@
1789 off_t
/* O - New file position or -1 on error */
1790 cupsFileSeek(cups_file_t
*fp
, /* I - CUPS file */
1791 off_t pos
) /* I - Position in file */
1793 ssize_t bytes
; /* Number bytes in buffer */
1796 DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT
")", (void *)fp
, CUPS_LLCAST pos
));
1797 DEBUG_printf(("2cupsFileSeek: fp->pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1798 DEBUG_printf(("2cupsFileSeek: fp->ptr=%p, fp->end=%p", (void *)fp
->ptr
, (void *)fp
->end
));
1801 * Range check input...
1804 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1808 * Handle special cases...
1812 return (cupsFileRewind(fp
));
1816 bytes
= (ssize_t
)(fp
->end
- fp
->buf
);
1818 DEBUG_printf(("2cupsFileSeek: bytes=" CUPS_LLFMT
, CUPS_LLCAST bytes
));
1820 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1823 * No seeking necessary...
1827 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1835 if (!fp
->compressed
&& !fp
->ptr
)
1838 * Preload a buffer to determine whether the file is compressed...
1841 if (cups_fill(fp
) <= 0)
1844 #endif /* HAVE_LIBZ */
1847 * Seek forwards or backwards...
1852 if (pos
< fp
->bufpos
)
1855 * Need to seek backwards...
1858 DEBUG_puts("2cupsFileSeek: SEEK BACKWARDS");
1863 inflateEnd(&fp
->stream
);
1865 lseek(fp
->fd
, 0, SEEK_SET
);
1871 while ((bytes
= cups_fill(fp
)) > 0)
1872 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1878 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1882 #endif /* HAVE_LIBZ */
1884 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1885 fp
->pos
= fp
->bufpos
;
1889 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1890 CUPS_LLCAST fp
->pos
));
1896 * Need to seek forwards...
1899 DEBUG_puts("2cupsFileSeek: SEEK FORWARDS");
1904 while ((bytes
= cups_fill(fp
)) > 0)
1906 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1913 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1917 #endif /* HAVE_LIBZ */
1919 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1920 fp
->pos
= fp
->bufpos
;
1924 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1925 CUPS_LLCAST fp
->pos
));
1929 DEBUG_printf(("2cupsFileSeek: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1936 * 'cupsFileStderr()' - Return a CUPS file associated with stderr.
1938 * @since CUPS 1.2/macOS 10.5@
1941 cups_file_t
* /* O - CUPS file */
1942 cupsFileStderr(void)
1944 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1948 * Open file descriptor 2 as needed...
1951 if (!cg
->stdio_files
[2])
1954 * Flush any pending output on the stdio file...
1960 * Open file descriptor 2...
1963 if ((cg
->stdio_files
[2] = cupsFileOpenFd(2, "w")) != NULL
)
1964 cg
->stdio_files
[2]->is_stdio
= 1;
1967 return (cg
->stdio_files
[2]);
1972 * 'cupsFileStdin()' - Return a CUPS file associated with stdin.
1974 * @since CUPS 1.2/macOS 10.5@
1977 cups_file_t
* /* O - CUPS file */
1980 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1984 * Open file descriptor 0 as needed...
1987 if (!cg
->stdio_files
[0])
1990 * Open file descriptor 0...
1993 if ((cg
->stdio_files
[0] = cupsFileOpenFd(0, "r")) != NULL
)
1994 cg
->stdio_files
[0]->is_stdio
= 1;
1997 return (cg
->stdio_files
[0]);
2002 * 'cupsFileStdout()' - Return a CUPS file associated with stdout.
2004 * @since CUPS 1.2/macOS 10.5@
2007 cups_file_t
* /* O - CUPS file */
2008 cupsFileStdout(void)
2010 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
2014 * Open file descriptor 1 as needed...
2017 if (!cg
->stdio_files
[1])
2020 * Flush any pending output on the stdio file...
2026 * Open file descriptor 1...
2029 if ((cg
->stdio_files
[1] = cupsFileOpenFd(1, "w")) != NULL
)
2030 cg
->stdio_files
[1]->is_stdio
= 1;
2033 return (cg
->stdio_files
[1]);
2038 * 'cupsFileTell()' - Return the current file position.
2040 * @since CUPS 1.2/macOS 10.5@
2043 off_t
/* O - File position */
2044 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
2046 DEBUG_printf(("2cupsFileTell(fp=%p)", (void *)fp
));
2047 DEBUG_printf(("3cupsFileTell: pos=" CUPS_LLFMT
, CUPS_LLCAST (fp
? fp
->pos
: -1)));
2049 return (fp
? fp
->pos
: 0);
2054 * 'cupsFileUnlock()' - Unlock access to a file.
2056 * @since CUPS 1.2/macOS 10.5@
2059 int /* O - 0 on success, -1 on error */
2060 cupsFileUnlock(cups_file_t
*fp
) /* I - CUPS file */
2066 DEBUG_printf(("cupsFileUnlock(fp=%p)", (void *)fp
));
2068 if (!fp
|| fp
->mode
== 's')
2076 return (_locking(fp
->fd
, _LK_UNLCK
, 0));
2078 return (lockf(fp
->fd
, F_ULOCK
, 0));
2084 * 'cupsFileWrite()' - Write to a file.
2086 * @since CUPS 1.2/macOS 10.5@
2089 ssize_t
/* O - Number of bytes written or -1 on error */
2090 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
2091 const char *buf
, /* I - Buffer */
2092 size_t bytes
) /* I - Number of bytes to write */
2095 * Range check input...
2098 DEBUG_printf(("2cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2100 if (!fp
|| !buf
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
2107 * Write the buffer...
2110 if (fp
->mode
== 's')
2112 if (cups_write(fp
, buf
, bytes
) < 0)
2115 fp
->pos
+= (off_t
)bytes
;
2117 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2119 return ((ssize_t
)bytes
);
2122 if ((fp
->ptr
+ bytes
) > fp
->end
)
2123 if (cupsFileFlush(fp
))
2126 fp
->pos
+= (off_t
)bytes
;
2128 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2130 if (bytes
> sizeof(fp
->buf
))
2134 return (cups_compress(fp
, buf
, bytes
));
2136 #endif /* HAVE_LIBZ */
2137 return (cups_write(fp
, buf
, bytes
));
2141 memcpy(fp
->ptr
, buf
, bytes
);
2143 return ((ssize_t
)bytes
);
2150 * 'cups_compress()' - Compress a buffer of data.
2153 static ssize_t
/* O - Number of bytes written or -1 */
2154 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
2155 const char *buf
, /* I - Buffer */
2156 size_t bytes
) /* I - Number bytes */
2158 DEBUG_printf(("7cups_compress(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2164 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, (uInt
)bytes
);
2167 * Deflate the bytes...
2170 fp
->stream
.next_in
= (Bytef
*)buf
;
2171 fp
->stream
.avail_in
= (uInt
)bytes
;
2173 while (fp
->stream
.avail_in
> 0)
2176 * Flush the current buffer...
2179 DEBUG_printf(("9cups_compress: avail_in=%d, avail_out=%d",
2180 fp
->stream
.avail_in
, fp
->stream
.avail_out
));
2182 if (fp
->stream
.avail_out
< (uInt
)(sizeof(fp
->cbuf
) / 8))
2184 if (cups_write(fp
, (char *)fp
->cbuf
, (size_t)(fp
->stream
.next_out
- fp
->cbuf
)) < 0)
2187 fp
->stream
.next_out
= fp
->cbuf
;
2188 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
2191 deflate(&(fp
->stream
), Z_NO_FLUSH
);
2194 return ((ssize_t
)bytes
);
2196 #endif /* HAVE_LIBZ */
2200 * 'cups_fill()' - Fill the input buffer.
2203 static ssize_t
/* O - Number of bytes or -1 */
2204 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
2206 ssize_t bytes
; /* Number of bytes read */
2208 int status
; /* Decompression status */
2209 const unsigned char *ptr
, /* Pointer into buffer */
2210 *end
; /* End of buffer */
2211 #endif /* HAVE_LIBZ */
2214 DEBUG_printf(("7cups_fill(fp=%p)", (void *)fp
));
2215 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
));
2217 if (fp
->ptr
&& fp
->end
)
2218 fp
->bufpos
+= fp
->end
- fp
->buf
;
2221 DEBUG_printf(("9cups_fill: fp->compressed=%d", fp
->compressed
));
2223 while (!fp
->ptr
|| fp
->compressed
)
2226 * Check to see if we have read any data yet; if not, see if we have a
2227 * compressed file...
2233 * Reset the file position in case we are seeking...
2239 * Read the first bytes in the file to determine if we have a gzip'd
2243 if ((bytes
= cups_read(fp
, (char *)fp
->buf
, sizeof(fp
->buf
))) < 0)
2246 * Can't read from file!
2249 DEBUG_printf(("9cups_fill: cups_read() returned " CUPS_LLFMT
,
2250 CUPS_LLCAST bytes
));
2257 if (bytes
< 10 || fp
->buf
[0] != 0x1f ||
2258 (fp
->buf
[1] & 255) != 0x8b ||
2259 fp
->buf
[2] != 8 || (fp
->buf
[3] & 0xe0) != 0)
2262 * Not a gzip'd file!
2266 fp
->end
= fp
->buf
+ bytes
;
2268 DEBUG_printf(("9cups_fill: Returning " CUPS_LLFMT
,
2269 CUPS_LLCAST bytes
));
2275 * Parse header junk: extra data, original name, and comment...
2278 ptr
= (unsigned char *)fp
->buf
+ 10;
2279 end
= (unsigned char *)fp
->buf
+ bytes
;
2281 if (fp
->buf
[3] & 0x04)
2284 * Skip extra data...
2287 if ((ptr
+ 2) > end
)
2290 * Can't read from file!
2293 DEBUG_puts("9cups_fill: Extra gzip header data missing, returning -1.");
2301 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
2307 * Can't read from file!
2310 DEBUG_puts("9cups_fill: Extra gzip header data does not fit in initial buffer, returning -1.");
2319 if (fp
->buf
[3] & 0x08)
2322 * Skip original name data...
2325 while (ptr
< end
&& *ptr
)
2333 * Can't read from file!
2336 DEBUG_puts("9cups_fill: Original filename in gzip header data does not fit in initial buffer, returning -1.");
2345 if (fp
->buf
[3] & 0x10)
2348 * Skip comment data...
2351 while (ptr
< end
&& *ptr
)
2359 * Can't read from file!
2362 DEBUG_puts("9cups_fill: Comment in gzip header data does not fit in initial buffer, returning -1.");
2371 if (fp
->buf
[3] & 0x02)
2374 * Skip header CRC data...
2382 * Can't read from file!
2385 DEBUG_puts("9cups_fill: Header CRC in gzip header data does not fit in initial buffer, returning -1.");
2395 * Copy the flate-compressed data to the compression buffer...
2398 if ((bytes
= end
- ptr
) > 0)
2399 memcpy(fp
->cbuf
, ptr
, (size_t)bytes
);
2402 * Setup the decompressor data...
2405 fp
->stream
.zalloc
= (alloc_func
)0;
2406 fp
->stream
.zfree
= (free_func
)0;
2407 fp
->stream
.opaque
= (voidpf
)0;
2408 fp
->stream
.next_in
= (Bytef
*)fp
->cbuf
;
2409 fp
->stream
.next_out
= NULL
;
2410 fp
->stream
.avail_in
= (uInt
)bytes
;
2411 fp
->stream
.avail_out
= 0;
2412 fp
->crc
= crc32(0L, Z_NULL
, 0);
2414 if ((status
= inflateInit2(&(fp
->stream
), -15)) != Z_OK
)
2416 DEBUG_printf(("9cups_fill: inflateInit2 returned %d, returning -1.", status
));
2430 * If we have reached end-of-file, return immediately...
2435 DEBUG_puts("9cups_fill: EOF, returning 0.");
2441 * Fill the decompression buffer as needed...
2444 if (fp
->stream
.avail_in
== 0)
2446 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
2448 DEBUG_printf(("9cups_fill: cups_read error, returning %d.", (int)bytes
));
2455 fp
->stream
.next_in
= fp
->cbuf
;
2456 fp
->stream
.avail_in
= (uInt
)bytes
;
2460 * Decompress data from the buffer...
2463 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
2464 fp
->stream
.avail_out
= sizeof(fp
->buf
);
2466 status
= inflate(&(fp
->stream
), Z_NO_FLUSH
);
2468 if (fp
->stream
.next_out
> (Bytef
*)fp
->buf
)
2469 fp
->crc
= crc32(fp
->crc
, (Bytef
*)fp
->buf
,
2470 (uInt
)(fp
->stream
.next_out
- (Bytef
*)fp
->buf
));
2472 if (status
== Z_STREAM_END
)
2475 * Read the CRC and length...
2478 unsigned char trailer
[8]; /* Trailer bytes */
2479 uLong tcrc
; /* Trailer CRC */
2480 ssize_t tbytes
= 0; /* Number of bytes */
2482 if (fp
->stream
.avail_in
> 0)
2484 if (fp
->stream
.avail_in
> sizeof(trailer
))
2485 tbytes
= (ssize_t
)sizeof(trailer
);
2487 tbytes
= (ssize_t
)fp
->stream
.avail_in
;
2489 memcpy(trailer
, fp
->stream
.next_in
, (size_t)tbytes
);
2490 fp
->stream
.next_in
+= tbytes
;
2491 fp
->stream
.avail_in
-= (size_t)tbytes
;
2494 if (tbytes
< (ssize_t
)sizeof(trailer
))
2496 if (read(fp
->fd
, trailer
+ tbytes
, sizeof(trailer
) - (size_t)tbytes
) < ((ssize_t
)sizeof(trailer
) - tbytes
))
2499 * Can't get it, so mark end-of-file...
2502 DEBUG_puts("9cups_fill: Unable to read gzip CRC trailer, returning -1.");
2511 tcrc
= ((((((uLong
)trailer
[3] << 8) | (uLong
)trailer
[2]) << 8) |
2512 (uLong
)trailer
[1]) << 8) | (uLong
)trailer
[0];
2514 if (tcrc
!= fp
->crc
)
2517 * Bad CRC, mark end-of-file...
2520 DEBUG_printf(("9cups_fill: tcrc=%08x != fp->crc=%08x, returning -1.", (unsigned int)tcrc
, (unsigned int)fp
->crc
));
2529 * Otherwise, reset the compressed flag so that we re-read the
2533 inflateEnd(&fp
->stream
);
2537 else if (status
< Z_OK
)
2539 DEBUG_printf(("9cups_fill: inflate returned %d, returning -1.", status
));
2547 bytes
= (ssize_t
)sizeof(fp
->buf
) - (ssize_t
)fp
->stream
.avail_out
;
2550 * Return the decompressed data...
2554 fp
->end
= fp
->buf
+ bytes
;
2558 DEBUG_printf(("9cups_fill: Returning %d.", (int)bytes
));
2563 #endif /* HAVE_LIBZ */
2566 * Read a buffer's full of data...
2569 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
2572 * Can't read from file!
2582 * Return the bytes we read...
2587 fp
->end
= fp
->buf
+ bytes
;
2590 DEBUG_printf(("9cups_fill: Not gzip, returning %d.", (int)bytes
));
2597 * 'cups_open()' - Safely open a file for writing.
2599 * We don't allow appending to directories or files that are hard-linked or
2603 static int /* O - File descriptor or -1 otherwise */
2604 cups_open(const char *filename
, /* I - Filename */
2605 int mode
) /* I - Open mode */
2607 int fd
; /* File descriptor */
2608 struct stat fileinfo
; /* File information */
2610 struct stat linkinfo
; /* Link information */
2611 #endif /* !_WIN32 */
2618 if ((fd
= open(filename
, mode
, 0666)) < 0)
2622 * Then verify that the file descriptor doesn't point to a directory or hard-
2626 if (fstat(fd
, &fileinfo
))
2632 if (fileinfo
.st_nlink
!= 1)
2640 if (fileinfo
.st_mode
& _S_IFDIR
)
2642 if (S_ISDIR(fileinfo
.st_mode
))
2652 * Then use lstat to determine whether the filename is a symlink...
2655 if (lstat(filename
, &linkinfo
))
2661 if (S_ISLNK(linkinfo
.st_mode
) ||
2662 fileinfo
.st_dev
!= linkinfo
.st_dev
||
2663 fileinfo
.st_ino
!= linkinfo
.st_ino
||
2665 fileinfo
.st_gen
!= linkinfo
.st_gen
||
2666 #endif /* HAVE_ST_GEN */
2667 fileinfo
.st_nlink
!= linkinfo
.st_nlink
||
2668 fileinfo
.st_mode
!= linkinfo
.st_mode
)
2678 #endif /* !_WIN32 */
2685 * 'cups_read()' - Read from a file descriptor.
2688 static ssize_t
/* O - Number of bytes read or -1 */
2689 cups_read(cups_file_t
*fp
, /* I - CUPS file */
2690 char *buf
, /* I - Buffer */
2691 size_t bytes
) /* I - Number bytes */
2693 ssize_t total
; /* Total bytes read */
2696 DEBUG_printf(("7cups_read(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2699 * Loop until we read at least 0 bytes...
2705 if (fp
->mode
== 's')
2706 total
= (ssize_t
)recv(fp
->fd
, buf
, (unsigned)bytes
, 0);
2708 total
= (ssize_t
)read(fp
->fd
, buf
, (unsigned)bytes
);
2710 if (fp
->mode
== 's')
2711 total
= recv(fp
->fd
, buf
, bytes
, 0);
2713 total
= read(fp
->fd
, buf
, bytes
);
2716 DEBUG_printf(("9cups_read: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
2722 * Reads can be interrupted by signals and unavailable resources...
2725 if (errno
== EAGAIN
|| errno
== EINTR
)
2732 * Return the total number of bytes read...
2740 * 'cups_write()' - Write to a file descriptor.
2743 static ssize_t
/* O - Number of bytes written or -1 */
2744 cups_write(cups_file_t
*fp
, /* I - CUPS file */
2745 const char *buf
, /* I - Buffer */
2746 size_t bytes
) /* I - Number bytes */
2748 size_t total
; /* Total bytes written */
2749 ssize_t count
; /* Count this time */
2752 DEBUG_printf(("7cups_write(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2755 * Loop until all bytes are written...
2762 if (fp
->mode
== 's')
2763 count
= (ssize_t
)send(fp
->fd
, buf
, (unsigned)bytes
, 0);
2765 count
= (ssize_t
)write(fp
->fd
, buf
, (unsigned)bytes
);
2767 if (fp
->mode
== 's')
2768 count
= send(fp
->fd
, buf
, bytes
, 0);
2770 count
= write(fp
->fd
, buf
, bytes
);
2773 DEBUG_printf(("9cups_write: count=" CUPS_LLFMT
, CUPS_LLCAST count
));
2778 * Writes can be interrupted by signals and unavailable resources...
2781 if (errno
== EAGAIN
|| errno
== EINTR
)
2788 * Update the counts for the last write call...
2791 bytes
-= (size_t)count
;
2792 total
+= (size_t)count
;
2797 * Return the total number of bytes written...
2800 return ((ssize_t
)total
);