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-2017 by Apple Inc.
10 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
12 * These coded instructions, statements, and computer programs are the
13 * property of Apple Inc. and are protected by Federal copyright
14 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
15 * which should have been included with this file. If this file is
16 * missing or damaged, see the license at "http://www.cups.org/".
18 * This file is subject to the Apple OS-Developed Software exception.
22 * Include necessary headers...
25 #include "file-private.h"
27 #include <sys/types.h>
35 static ssize_t
cups_compress(cups_file_t
*fp
, const char *buf
, size_t bytes
);
36 #endif /* HAVE_LIBZ */
37 static ssize_t
cups_fill(cups_file_t
*fp
);
38 static int cups_open(const char *filename
, int mode
);
39 static ssize_t
cups_read(cups_file_t
*fp
, char *buf
, size_t bytes
);
40 static ssize_t
cups_write(cups_file_t
*fp
, const char *buf
, size_t bytes
);
45 * '_cupsFileCheck()' - Check the permissions of the given filename.
48 _cups_fc_result_t
/* O - Check result */
50 const char *filename
, /* I - Filename to check */
51 _cups_fc_filetype_t filetype
, /* I - Type of file checks? */
52 int dorootchecks
, /* I - Check for root permissions? */
53 _cups_fc_func_t cb
, /* I - Callback function */
54 void *context
) /* I - Context pointer for callback */
57 struct stat fileinfo
; /* File information */
58 char message
[1024], /* Message string */
59 temp
[1024], /* Parent directory filename */
60 *ptr
; /* Pointer into parent directory */
61 _cups_fc_result_t result
; /* Check result */
65 * Does the filename contain a relative path ("../")?
68 if (strstr(filename
, "../"))
74 result
= _CUPS_FILE_CHECK_RELATIVE_PATH
;
79 * Does the program even exist and is it accessible?
82 if (stat(filename
, &fileinfo
))
88 result
= _CUPS_FILE_CHECK_MISSING
;
93 * Check the execute bit...
96 result
= _CUPS_FILE_CHECK_OK
;
100 case _CUPS_FILE_CHECK_DIRECTORY
:
101 if (!S_ISDIR(fileinfo
.st_mode
))
102 result
= _CUPS_FILE_CHECK_WRONG_TYPE
;
106 if (!S_ISREG(fileinfo
.st_mode
))
107 result
= _CUPS_FILE_CHECK_WRONG_TYPE
;
115 * Are we doing root checks?
121 * Nope, so anything (else) goes...
128 * Verify permission of the file itself:
130 * 1. Must be owned by root
131 * 2. Must not be writable by group
132 * 3. Must not be setuid
133 * 4. Must not be writable by others
136 if (fileinfo
.st_uid
|| /* 1. Must be owned by root */
137 (fileinfo
.st_mode
& S_IWGRP
) || /* 2. Must not be writable by group */
138 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
139 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
141 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
145 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
||
146 filetype
== _CUPS_FILE_CHECK_FILE_ONLY
)
150 * Now check the containing directory...
153 strlcpy(temp
, filename
, sizeof(temp
));
154 if ((ptr
= strrchr(temp
, '/')) != NULL
)
162 if (stat(temp
, &fileinfo
))
168 result
= _CUPS_FILE_CHECK_MISSING
;
169 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
175 if (fileinfo
.st_uid
|| /* 1. Must be owned by root */
176 (fileinfo
.st_mode
& S_IWGRP
) || /* 2. Must not be writable by group */
177 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
178 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
180 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
181 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
186 * Common return point...
193 cups_lang_t
*lang
= cupsLangDefault();
194 /* Localization information */
198 case _CUPS_FILE_CHECK_OK
:
199 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
200 snprintf(message
, sizeof(message
),
201 _cupsLangString(lang
, _("Directory \"%s\" permissions OK "
202 "(0%o/uid=%d/gid=%d).")),
203 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
204 (int)fileinfo
.st_gid
);
206 snprintf(message
, sizeof(message
),
207 _cupsLangString(lang
, _("File \"%s\" permissions OK "
208 "(0%o/uid=%d/gid=%d).")),
209 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
210 (int)fileinfo
.st_gid
);
213 case _CUPS_FILE_CHECK_MISSING
:
214 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
215 snprintf(message
, sizeof(message
),
216 _cupsLangString(lang
, _("Directory \"%s\" not available: "
218 filename
, strerror(errno
));
220 snprintf(message
, sizeof(message
),
221 _cupsLangString(lang
, _("File \"%s\" not available: %s")),
222 filename
, strerror(errno
));
225 case _CUPS_FILE_CHECK_PERMISSIONS
:
226 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
227 snprintf(message
, sizeof(message
),
228 _cupsLangString(lang
, _("Directory \"%s\" has insecure "
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\" has insecure "
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_WRONG_TYPE
:
243 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
244 snprintf(message
, sizeof(message
),
245 _cupsLangString(lang
, _("Directory \"%s\" is a file.")),
248 snprintf(message
, sizeof(message
),
249 _cupsLangString(lang
, _("File \"%s\" is a directory.")),
253 case _CUPS_FILE_CHECK_RELATIVE_PATH
:
254 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
255 snprintf(message
, sizeof(message
),
256 _cupsLangString(lang
, _("Directory \"%s\" contains a "
257 "relative path.")), filename
);
259 snprintf(message
, sizeof(message
),
260 _cupsLangString(lang
, _("File \"%s\" contains a relative "
261 "path.")), filename
);
265 (*cb
)(context
, result
, message
);
273 * '_cupsFileCheckFilter()' - Report file check results as CUPS filter messages.
277 _cupsFileCheckFilter(
278 void *context
, /* I - Context pointer (unused) */
279 _cups_fc_result_t result
, /* I - Result code */
280 const char *message
) /* I - Message text */
282 const char *prefix
; /* Messaging prefix */
290 case _CUPS_FILE_CHECK_OK
:
294 case _CUPS_FILE_CHECK_MISSING
:
295 case _CUPS_FILE_CHECK_WRONG_TYPE
:
297 fputs("STATE: +cups-missing-filter-warning\n", stderr
);
300 case _CUPS_FILE_CHECK_PERMISSIONS
:
301 case _CUPS_FILE_CHECK_RELATIVE_PATH
:
303 fputs("STATE: +cups-insecure-filter-warning\n", stderr
);
307 fprintf(stderr
, "%s: %s\n", prefix
, message
);
313 * 'cupsFileClose()' - Close a CUPS file.
315 * @since CUPS 1.2/macOS 10.5@
318 int /* O - 0 on success, -1 on error */
319 cupsFileClose(cups_file_t
*fp
) /* I - CUPS file */
321 int fd
; /* File descriptor */
322 char mode
; /* Open mode */
323 int status
; /* Return status */
326 DEBUG_printf(("cupsFileClose(fp=%p)", (void *)fp
));
336 * Flush pending write data...
340 status
= cupsFileFlush(fp
);
345 if (fp
->compressed
&& status
>= 0)
350 * Free decompression data...
353 inflateEnd(&fp
->stream
);
358 * Flush any remaining compressed data...
361 unsigned char trailer
[8]; /* Trailer CRC and length */
362 int done
; /* Done writing... */
365 fp
->stream
.avail_in
= 0;
369 if (fp
->stream
.next_out
> fp
->cbuf
)
371 if (cups_write(fp
, (char *)fp
->cbuf
,
372 (size_t)(fp
->stream
.next_out
- fp
->cbuf
)) < 0)
375 fp
->stream
.next_out
= fp
->cbuf
;
376 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
379 if (done
|| status
< 0)
382 done
= deflate(&fp
->stream
, Z_FINISH
) == Z_STREAM_END
&&
383 fp
->stream
.next_out
== fp
->cbuf
;
387 * Write the CRC and length...
390 trailer
[0] = (unsigned char)fp
->crc
;
391 trailer
[1] = (unsigned char)(fp
->crc
>> 8);
392 trailer
[2] = (unsigned char)(fp
->crc
>> 16);
393 trailer
[3] = (unsigned char)(fp
->crc
>> 24);
394 trailer
[4] = (unsigned char)fp
->pos
;
395 trailer
[5] = (unsigned char)(fp
->pos
>> 8);
396 trailer
[6] = (unsigned char)(fp
->pos
>> 16);
397 trailer
[7] = (unsigned char)(fp
->pos
>> 24);
399 if (cups_write(fp
, (char *)trailer
, 8) < 0)
403 * Free all memory used by the compression stream...
406 deflateEnd(&(fp
->stream
));
409 #endif /* HAVE_LIBZ */
412 * If this is one of the cupsFileStdin/out/err files, return now and don't
413 * actually free memory or close (these last the life of the process...)
420 * Save the file descriptor we used and free memory...
426 if (fp
->printf_buffer
)
427 free(fp
->printf_buffer
);
432 * Close the file, returning the close status...
437 if (httpAddrClose(NULL
, fd
) < 0)
440 else if (close(fd
) < 0)
448 * 'cupsFileCompression()' - Return whether a file is compressed.
450 * @since CUPS 1.2/macOS 10.5@
453 int /* O - @code CUPS_FILE_NONE@ or @code CUPS_FILE_GZIP@ */
454 cupsFileCompression(cups_file_t
*fp
) /* I - CUPS file */
456 return (fp
? fp
->compressed
: CUPS_FILE_NONE
);
461 * 'cupsFileEOF()' - Return the end-of-file status.
463 * @since CUPS 1.2/macOS 10.5@
466 int /* O - 1 on end of file, 0 otherwise */
467 cupsFileEOF(cups_file_t
*fp
) /* I - CUPS file */
469 return (fp
? fp
->eof
: 1);
474 * 'cupsFileFind()' - Find a file using the specified path.
476 * This function allows the paths in the path string to be separated by
477 * colons (UNIX standard) or semicolons (Windows standard) and stores the
478 * result in the buffer supplied. If the file cannot be found in any of
479 * the supplied paths, @code NULL@ is returned. A @code NULL@ path only
480 * matches the current directory.
482 * @since CUPS 1.2/macOS 10.5@
485 const char * /* O - Full path to file or @code NULL@ if not found */
486 cupsFileFind(const char *filename
, /* I - File to find */
487 const char *path
, /* I - Colon/semicolon-separated path */
488 int executable
, /* I - 1 = executable files, 0 = any file/dir */
489 char *buffer
, /* I - Filename buffer */
490 int bufsize
) /* I - Size of filename buffer */
492 char *bufptr
, /* Current position in buffer */
493 *bufend
; /* End of buffer */
497 * Range check input...
500 DEBUG_printf(("cupsFileFind(filename=\"%s\", path=\"%s\", executable=%d, buffer=%p, bufsize=%d)", filename
, path
, executable
, (void *)buffer
, bufsize
));
502 if (!filename
|| !buffer
|| bufsize
< 2)
508 * No path, so check current directory...
511 if (!access(filename
, 0))
513 strlcpy(buffer
, filename
, (size_t)bufsize
);
521 * Now check each path and return the first match...
524 bufend
= buffer
+ bufsize
- 1;
530 if (*path
== ';' || (*path
== ':' && ((bufptr
- buffer
) > 1 || !isalpha(buffer
[0] & 255))))
532 if (*path
== ';' || *path
== ':')
535 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
538 strlcpy(bufptr
, filename
, (size_t)(bufend
- bufptr
));
541 if (!access(buffer
, 0))
543 if (!access(buffer
, executable
? X_OK
: 0))
546 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
552 else if (bufptr
< bufend
)
559 * Check the last path...
562 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
565 strlcpy(bufptr
, filename
, (size_t)(bufend
- bufptr
));
567 if (!access(buffer
, 0))
569 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
574 DEBUG_puts("1cupsFileFind: Returning NULL");
581 * 'cupsFileFlush()' - Flush pending output.
583 * @since CUPS 1.2/macOS 10.5@
586 int /* O - 0 on success, -1 on error */
587 cupsFileFlush(cups_file_t
*fp
) /* I - CUPS file */
589 ssize_t bytes
; /* Bytes to write */
592 DEBUG_printf(("cupsFileFlush(fp=%p)", (void *)fp
));
595 * Range check input...
598 if (!fp
|| fp
->mode
!= 'w')
600 DEBUG_puts("1cupsFileFlush: Attempt to flush a read-only file...");
604 bytes
= (ssize_t
)(fp
->ptr
- fp
->buf
);
606 DEBUG_printf(("2cupsFileFlush: Flushing " CUPS_LLFMT
" bytes...",
613 bytes
= cups_compress(fp
, fp
->buf
, (size_t)bytes
);
615 #endif /* HAVE_LIBZ */
616 bytes
= cups_write(fp
, fp
->buf
, (size_t)bytes
);
629 * 'cupsFileGetChar()' - Get a single character from a file.
631 * @since CUPS 1.2/macOS 10.5@
634 int /* O - Character or -1 on end of file */
635 cupsFileGetChar(cups_file_t
*fp
) /* I - CUPS file */
638 * Range check input...
641 DEBUG_printf(("4cupsFileGetChar(fp=%p)", (void *)fp
));
643 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
645 DEBUG_puts("5cupsFileGetChar: Bad arguments!");
650 * If the input buffer is empty, try to read more data...
653 DEBUG_printf(("5cupsFileGetChar: fp->eof=%d, fp->ptr=%p, fp->end=%p", fp
->eof
, (void *)fp
->ptr
, (void *)fp
->end
));
655 if (fp
->ptr
>= fp
->end
)
656 if (cups_fill(fp
) <= 0)
658 DEBUG_puts("5cupsFileGetChar: Unable to fill buffer!");
663 * Return the next character in the buffer...
666 DEBUG_printf(("5cupsFileGetChar: Returning %d...", *(fp
->ptr
) & 255));
670 DEBUG_printf(("6cupsFileGetChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
672 return (*(fp
->ptr
)++ & 255);
677 * 'cupsFileGetConf()' - Get a line from a configuration file.
679 * @since CUPS 1.2/macOS 10.5@
682 char * /* O - Line read or @code NULL@ on end of file or error */
683 cupsFileGetConf(cups_file_t
*fp
, /* I - CUPS file */
684 char *buf
, /* O - String buffer */
685 size_t buflen
, /* I - Size of string buffer */
686 char **value
, /* O - Pointer to value */
687 int *linenum
) /* IO - Current line number */
689 char *ptr
; /* Pointer into line */
693 * Range check input...
696 DEBUG_printf(("2cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT
697 ", value=%p, linenum=%p)", (void *)fp
, (void *)buf
, CUPS_LLCAST buflen
, (void *)value
, (void *)linenum
));
699 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') ||
700 !buf
|| buflen
< 2 || !value
)
709 * Read the next non-comment line...
714 while (cupsFileGets(fp
, buf
, buflen
))
719 * Strip any comments...
722 if ((ptr
= strchr(buf
, '#')) != NULL
)
724 if (ptr
> buf
&& ptr
[-1] == '\\')
727 _cups_strcpy(ptr
- 1, ptr
);
731 // Strip the comment and any trailing whitespace...
734 if (!_cups_isspace(ptr
[-1]))
745 * Strip leading whitespace...
748 for (ptr
= buf
; _cups_isspace(*ptr
); ptr
++);
751 _cups_strcpy(buf
, ptr
);
754 * See if there is anything left...
760 * Yes, grab any value and return...
763 for (ptr
= buf
; *ptr
; ptr
++)
764 if (_cups_isspace(*ptr
))
770 * Have a value, skip any other spaces...
773 while (_cups_isspace(*ptr
))
780 * Strip trailing whitespace and > for lines that begin with <...
783 ptr
+= strlen(ptr
) - 1;
785 if (buf
[0] == '<' && *ptr
== '>')
787 else if (buf
[0] == '<' && *ptr
!= '>')
797 while (ptr
> *value
&& _cups_isspace(*ptr
))
814 * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may
815 * contain binary data.
817 * This function differs from @link cupsFileGets@ in that the trailing CR
818 * and LF are preserved, as is any binary data on the line. The buffer is
819 * nul-terminated, however you should use the returned length to determine
820 * the number of bytes on the line.
822 * @since CUPS 1.2/macOS 10.5@
825 size_t /* O - Number of bytes on line or 0 on end of file */
826 cupsFileGetLine(cups_file_t
*fp
, /* I - File to read from */
827 char *buf
, /* I - Buffer */
828 size_t buflen
) /* I - Size of buffer */
830 int ch
; /* Character from file */
831 char *ptr
, /* Current position in line buffer */
832 *end
; /* End of line buffer */
836 * Range check input...
839 DEBUG_printf(("2cupsFileGetLine(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST buflen
));
841 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 3)
845 * Now loop until we have a valid line...
848 for (ptr
= buf
, end
= buf
+ buflen
- 2; ptr
< end
;)
850 if (fp
->ptr
>= fp
->end
)
851 if (cups_fill(fp
) <= 0)
854 *ptr
++ = ch
= *(fp
->ptr
)++;
863 if (fp
->ptr
>= fp
->end
)
864 if (cups_fill(fp
) <= 0)
867 if (*(fp
->ptr
) == '\n')
869 *ptr
++ = *(fp
->ptr
)++;
878 * Line feed ends a line...
887 DEBUG_printf(("4cupsFileGetLine: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
889 return ((size_t)(ptr
- buf
));
894 * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
896 * @since CUPS 1.2/macOS 10.5@
899 char * /* O - Line read or @code NULL@ on end of file or error */
900 cupsFileGets(cups_file_t
*fp
, /* I - CUPS file */
901 char *buf
, /* O - String buffer */
902 size_t buflen
) /* I - Size of string buffer */
904 int ch
; /* Character from file */
905 char *ptr
, /* Current position in line buffer */
906 *end
; /* End of line buffer */
910 * Range check input...
913 DEBUG_printf(("2cupsFileGets(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST buflen
));
915 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 2)
919 * Now loop until we have a valid line...
922 for (ptr
= buf
, end
= buf
+ buflen
- 1; ptr
< end
;)
924 if (fp
->ptr
>= fp
->end
)
925 if (cups_fill(fp
) <= 0)
942 if (fp
->ptr
>= fp
->end
)
943 if (cups_fill(fp
) <= 0)
946 if (*(fp
->ptr
) == '\n')
957 * Line feed ends a line...
968 DEBUG_printf(("4cupsFileGets: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
975 * 'cupsFileLock()' - Temporarily lock access to a file.
977 * @since CUPS 1.2/macOS 10.5@
980 int /* O - 0 on success, -1 on error */
981 cupsFileLock(cups_file_t
*fp
, /* I - CUPS file */
982 int block
) /* I - 1 to wait for the lock, 0 to fail right away */
988 if (!fp
|| fp
->mode
== 's')
996 return (_locking(fp
->fd
, block
? _LK_LOCK
: _LK_NBLCK
, 0));
998 return (lockf(fp
->fd
, block
? F_LOCK
: F_TLOCK
, 0));
1004 * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
1006 * @since CUPS 1.2/macOS 10.5@
1009 int /* O - File descriptor */
1010 cupsFileNumber(cups_file_t
*fp
) /* I - CUPS file */
1020 * 'cupsFileOpen()' - Open a CUPS file.
1022 * The "mode" parameter can be "r" to read, "w" to write, overwriting any
1023 * existing file, "a" to append to an existing file or create a new file,
1024 * or "s" to open a socket connection.
1026 * When opening for writing ("w"), an optional number from 1 to 9 can be
1027 * supplied which enables Flate compression of the file. Compression is
1028 * not supported for the "a" (append) mode.
1030 * When opening a socket connection, the filename is a string of the form
1031 * "address:port" or "hostname:port". The socket will make an IPv4 or IPv6
1032 * connection as needed, generally preferring IPv6 connections when there is
1035 * @since CUPS 1.2/macOS 10.5@
1038 cups_file_t
* /* O - CUPS file or @code NULL@ if the file or socket cannot be opened */
1039 cupsFileOpen(const char *filename
, /* I - Name of file */
1040 const char *mode
) /* I - Open mode */
1042 cups_file_t
*fp
; /* New CUPS file */
1043 int fd
; /* File descriptor */
1044 char hostname
[1024], /* Hostname */
1045 *portname
; /* Port "name" (number or service) */
1046 http_addrlist_t
*addrlist
; /* Host address list */
1049 DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")", filename
,
1053 * Range check input...
1056 if (!filename
|| !mode
||
1057 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1058 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1067 case 'a' : /* Append file */
1068 fd
= cups_open(filename
,
1069 O_RDWR
| O_CREAT
| O_APPEND
| O_LARGEFILE
| O_BINARY
);
1072 case 'r' : /* Read file */
1073 fd
= open(filename
, O_RDONLY
| O_LARGEFILE
| O_BINARY
, 0);
1076 case 'w' : /* Write file */
1077 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1078 if (fd
< 0 && errno
== ENOENT
)
1080 fd
= cups_open(filename
,
1081 O_WRONLY
| O_CREAT
| O_EXCL
| O_LARGEFILE
| O_BINARY
);
1082 if (fd
< 0 && errno
== EEXIST
)
1083 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1094 case 's' : /* Read/write socket */
1095 strlcpy(hostname
, filename
, sizeof(hostname
));
1096 if ((portname
= strrchr(hostname
, ':')) != NULL
)
1102 * Lookup the hostname and service...
1105 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
1109 * Connect to the server...
1112 if (!httpAddrConnect(addrlist
, &fd
))
1114 httpAddrFreeList(addrlist
);
1118 httpAddrFreeList(addrlist
);
1121 default : /* Remove bogus compiler warning... */
1129 * Create the CUPS file structure...
1132 if ((fp
= cupsFileOpenFd(fd
, mode
)) == NULL
)
1135 httpAddrClose(NULL
, fd
);
1148 * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
1150 * The "mode" parameter can be "r" to read, "w" to write, "a" to append,
1151 * or "s" to treat the file descriptor as a bidirectional socket connection.
1153 * When opening for writing ("w"), an optional number from 1 to 9 can be
1154 * supplied which enables Flate compression of the file. Compression is
1155 * not supported for the "a" (append) mode.
1157 * @since CUPS 1.2/macOS 10.5@
1160 cups_file_t
* /* O - CUPS file or @code NULL@ if the file could not be opened */
1161 cupsFileOpenFd(int fd
, /* I - File descriptor */
1162 const char *mode
) /* I - Open mode */
1164 cups_file_t
*fp
; /* New CUPS file */
1167 DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")", fd
, mode
));
1170 * Range check input...
1173 if (fd
< 0 || !mode
||
1174 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1175 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1179 * Allocate memory...
1182 if ((fp
= calloc(1, sizeof(cups_file_t
))) == NULL
)
1194 fp
->pos
= lseek(fd
, 0, SEEK_END
);
1199 fp
->end
= fp
->buf
+ sizeof(fp
->buf
);
1202 if (mode
[1] >= '1' && mode
[1] <= '9')
1205 * Open a compressed stream, so write the standard gzip file
1209 unsigned char header
[10]; /* gzip file header */
1210 time_t curtime
; /* Current time */
1213 curtime
= time(NULL
);
1216 header
[2] = Z_DEFLATED
;
1218 header
[4] = (unsigned char)curtime
;
1219 header
[5] = (unsigned char)(curtime
>> 8);
1220 header
[6] = (unsigned char)(curtime
>> 16);
1221 header
[7] = (unsigned char)(curtime
>> 24);
1225 cups_write(fp
, (char *)header
, 10);
1228 * Initialize the compressor...
1231 deflateInit2(&(fp
->stream
), mode
[1] - '0', Z_DEFLATED
, -15, 8,
1232 Z_DEFAULT_STRATEGY
);
1234 fp
->stream
.next_out
= fp
->cbuf
;
1235 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
1237 fp
->crc
= crc32(0L, Z_NULL
, 0);
1239 #endif /* HAVE_LIBZ */
1250 default : /* Remove bogus compiler warning... */
1255 * Don't pass this file to child processes...
1259 fcntl(fp
->fd
, F_SETFD
, fcntl(fp
->fd
, F_GETFD
) | FD_CLOEXEC
);
1267 * 'cupsFilePeekChar()' - Peek at the next character from a file.
1269 * @since CUPS 1.2/macOS 10.5@
1272 int /* O - Character or -1 on end of file */
1273 cupsFilePeekChar(cups_file_t
*fp
) /* I - CUPS file */
1276 * Range check input...
1279 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1283 * If the input buffer is empty, try to read more data...
1286 if (fp
->ptr
>= fp
->end
)
1287 if (cups_fill(fp
) <= 0)
1291 * Return the next character in the buffer...
1294 return (*(fp
->ptr
) & 255);
1299 * 'cupsFilePrintf()' - Write a formatted string.
1301 * @since CUPS 1.2/macOS 10.5@
1304 int /* O - Number of bytes written or -1 on error */
1305 cupsFilePrintf(cups_file_t
*fp
, /* I - CUPS file */
1306 const char *format
, /* I - Printf-style format string */
1307 ...) /* I - Additional args as necessary */
1309 va_list ap
; /* Argument list */
1310 ssize_t bytes
; /* Formatted size */
1313 DEBUG_printf(("2cupsFilePrintf(fp=%p, format=\"%s\", ...)", (void *)fp
, format
));
1315 if (!fp
|| !format
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1318 if (!fp
->printf_buffer
)
1321 * Start with an 1k printf buffer...
1324 if ((fp
->printf_buffer
= malloc(1024)) == NULL
)
1327 fp
->printf_size
= 1024;
1330 va_start(ap
, format
);
1331 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1334 if (bytes
>= (ssize_t
)fp
->printf_size
)
1337 * Expand the printf buffer...
1340 char *temp
; /* Temporary buffer pointer */
1346 if ((temp
= realloc(fp
->printf_buffer
, (size_t)(bytes
+ 1))) == NULL
)
1349 fp
->printf_buffer
= temp
;
1350 fp
->printf_size
= (size_t)(bytes
+ 1);
1352 va_start(ap
, format
);
1353 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1357 if (fp
->mode
== 's')
1359 if (cups_write(fp
, fp
->printf_buffer
, (size_t)bytes
) < 0)
1364 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1366 return ((int)bytes
);
1369 if ((fp
->ptr
+ bytes
) > fp
->end
)
1370 if (cupsFileFlush(fp
))
1375 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1377 if ((size_t)bytes
> sizeof(fp
->buf
))
1381 return ((int)cups_compress(fp
, fp
->printf_buffer
, (size_t)bytes
));
1383 #endif /* HAVE_LIBZ */
1384 return ((int)cups_write(fp
, fp
->printf_buffer
, (size_t)bytes
));
1388 memcpy(fp
->ptr
, fp
->printf_buffer
, (size_t)bytes
);
1390 return ((int)bytes
);
1396 * 'cupsFilePutChar()' - Write a character.
1398 * @since CUPS 1.2/macOS 10.5@
1401 int /* O - 0 on success, -1 on error */
1402 cupsFilePutChar(cups_file_t
*fp
, /* I - CUPS file */
1403 int c
) /* I - Character to write */
1406 * Range check input...
1409 if (!fp
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1412 if (fp
->mode
== 's')
1415 * Send character immediately over socket...
1418 char ch
; /* Output character */
1423 if (send(fp
->fd
, &ch
, 1, 0) < 1)
1432 if (fp
->ptr
>= fp
->end
)
1433 if (cupsFileFlush(fp
))
1436 *(fp
->ptr
) ++ = (char)c
;
1441 DEBUG_printf(("4cupsFilePutChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1448 * 'cupsFilePutConf()' - Write a configuration line.
1450 * This function handles any comment escaping of the value.
1452 * @since CUPS 1.4/macOS 10.6@
1455 ssize_t
/* O - Number of bytes written or -1 on error */
1456 cupsFilePutConf(cups_file_t
*fp
, /* I - CUPS file */
1457 const char *directive
, /* I - Directive */
1458 const char *value
) /* I - Value */
1460 ssize_t bytes
, /* Number of bytes written */
1461 temp
; /* Temporary byte count */
1462 const char *ptr
; /* Pointer into value */
1465 if (!fp
|| !directive
|| !*directive
)
1468 if ((bytes
= cupsFilePuts(fp
, directive
)) < 0)
1471 if (cupsFilePutChar(fp
, ' ') < 0)
1475 if (value
&& *value
)
1477 if ((ptr
= strchr(value
, '#')) != NULL
)
1480 * Need to quote the first # in the info string...
1483 if ((temp
= cupsFileWrite(fp
, value
, (size_t)(ptr
- value
))) < 0)
1487 if (cupsFilePutChar(fp
, '\\') < 0)
1491 if ((temp
= cupsFilePuts(fp
, ptr
)) < 0)
1495 else if ((temp
= cupsFilePuts(fp
, value
)) < 0)
1501 if (cupsFilePutChar(fp
, '\n') < 0)
1509 * 'cupsFilePuts()' - Write a string.
1511 * Like the @code fputs@ function, no newline is appended to the string.
1513 * @since CUPS 1.2/macOS 10.5@
1516 int /* O - Number of bytes written or -1 on error */
1517 cupsFilePuts(cups_file_t
*fp
, /* I - CUPS file */
1518 const char *s
) /* I - String to write */
1520 ssize_t bytes
; /* Bytes to write */
1524 * Range check input...
1527 if (!fp
|| !s
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1531 * Write the string...
1534 bytes
= (ssize_t
)strlen(s
);
1536 if (fp
->mode
== 's')
1538 if (cups_write(fp
, s
, (size_t)bytes
) < 0)
1543 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1545 return ((int)bytes
);
1548 if ((fp
->ptr
+ bytes
) > fp
->end
)
1549 if (cupsFileFlush(fp
))
1554 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1556 if ((size_t)bytes
> sizeof(fp
->buf
))
1560 return ((int)cups_compress(fp
, s
, (size_t)bytes
));
1562 #endif /* HAVE_LIBZ */
1563 return ((int)cups_write(fp
, s
, (size_t)bytes
));
1567 memcpy(fp
->ptr
, s
, (size_t)bytes
);
1569 return ((int)bytes
);
1575 * 'cupsFileRead()' - Read from a file.
1577 * @since CUPS 1.2/macOS 10.5@
1580 ssize_t
/* O - Number of bytes read or -1 on error */
1581 cupsFileRead(cups_file_t
*fp
, /* I - CUPS file */
1582 char *buf
, /* O - Buffer */
1583 size_t bytes
) /* I - Number of bytes to read */
1585 size_t total
; /* Total bytes read */
1586 ssize_t count
; /* Bytes read */
1589 DEBUG_printf(("2cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
1592 * Range check input...
1595 if (!fp
|| !buf
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1602 * Loop until all bytes are read...
1608 if (fp
->ptr
>= fp
->end
)
1609 if (cups_fill(fp
) <= 0)
1611 DEBUG_printf(("4cupsFileRead: cups_fill() returned -1, total="
1612 CUPS_LLFMT
, CUPS_LLCAST total
));
1615 return ((ssize_t
)total
);
1620 count
= (ssize_t
)(fp
->end
- fp
->ptr
);
1621 if (count
> (ssize_t
)bytes
)
1622 count
= (ssize_t
)bytes
;
1624 memcpy(buf
, fp
->ptr
,(size_t) count
);
1628 DEBUG_printf(("4cupsFileRead: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1631 * Update the counts for the last read...
1634 bytes
-= (size_t)count
;
1635 total
+= (size_t)count
;
1640 * Return the total number of bytes read...
1643 DEBUG_printf(("3cupsFileRead: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
1645 return ((ssize_t
)total
);
1650 * 'cupsFileRewind()' - Set the current file position to the beginning of the
1653 * @since CUPS 1.2/macOS 10.5@
1656 off_t
/* O - New file position or -1 on error */
1657 cupsFileRewind(cups_file_t
*fp
) /* I - CUPS file */
1660 * Range check input...
1663 DEBUG_printf(("cupsFileRewind(fp=%p)", (void *)fp
));
1664 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1666 if (!fp
|| fp
->mode
!= 'r')
1670 * Handle special cases...
1673 if (fp
->bufpos
== 0)
1676 * No seeking necessary...
1687 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1693 * Otherwise, seek in the file and cleanup any compression buffers...
1699 inflateEnd(&fp
->stream
);
1702 #endif /* HAVE_LIBZ */
1704 if (lseek(fp
->fd
, 0, SEEK_SET
))
1706 DEBUG_printf(("1cupsFileRewind: lseek failed: %s", strerror(errno
)));
1716 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1723 * 'cupsFileSeek()' - Seek in a file.
1725 * @since CUPS 1.2/macOS 10.5@
1728 off_t
/* O - New file position or -1 on error */
1729 cupsFileSeek(cups_file_t
*fp
, /* I - CUPS file */
1730 off_t pos
) /* I - Position in file */
1732 ssize_t bytes
; /* Number bytes in buffer */
1735 DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT
")", (void *)fp
, CUPS_LLCAST pos
));
1736 DEBUG_printf(("2cupsFileSeek: fp->pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1737 DEBUG_printf(("2cupsFileSeek: fp->ptr=%p, fp->end=%p", (void *)fp
->ptr
, (void *)fp
->end
));
1740 * Range check input...
1743 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1747 * Handle special cases...
1751 return (cupsFileRewind(fp
));
1755 bytes
= (ssize_t
)(fp
->end
- fp
->buf
);
1757 DEBUG_printf(("2cupsFileSeek: bytes=" CUPS_LLFMT
, CUPS_LLCAST bytes
));
1759 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1762 * No seeking necessary...
1766 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1774 if (!fp
->compressed
&& !fp
->ptr
)
1777 * Preload a buffer to determine whether the file is compressed...
1780 if (cups_fill(fp
) <= 0)
1783 #endif /* HAVE_LIBZ */
1786 * Seek forwards or backwards...
1791 if (pos
< fp
->bufpos
)
1794 * Need to seek backwards...
1797 DEBUG_puts("2cupsFileSeek: SEEK BACKWARDS");
1802 inflateEnd(&fp
->stream
);
1804 lseek(fp
->fd
, 0, SEEK_SET
);
1810 while ((bytes
= cups_fill(fp
)) > 0)
1811 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1817 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1821 #endif /* HAVE_LIBZ */
1823 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1824 fp
->pos
= fp
->bufpos
;
1828 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1829 CUPS_LLCAST fp
->pos
));
1835 * Need to seek forwards...
1838 DEBUG_puts("2cupsFileSeek: SEEK FORWARDS");
1843 while ((bytes
= cups_fill(fp
)) > 0)
1845 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1852 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1856 #endif /* HAVE_LIBZ */
1858 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1859 fp
->pos
= fp
->bufpos
;
1863 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1864 CUPS_LLCAST fp
->pos
));
1868 DEBUG_printf(("2cupsFileSeek: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1875 * 'cupsFileStderr()' - Return a CUPS file associated with stderr.
1877 * @since CUPS 1.2/macOS 10.5@
1880 cups_file_t
* /* O - CUPS file */
1881 cupsFileStderr(void)
1883 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1887 * Open file descriptor 2 as needed...
1890 if (!cg
->stdio_files
[2])
1893 * Flush any pending output on the stdio file...
1899 * Open file descriptor 2...
1902 if ((cg
->stdio_files
[2] = cupsFileOpenFd(2, "w")) != NULL
)
1903 cg
->stdio_files
[2]->is_stdio
= 1;
1906 return (cg
->stdio_files
[2]);
1911 * 'cupsFileStdin()' - Return a CUPS file associated with stdin.
1913 * @since CUPS 1.2/macOS 10.5@
1916 cups_file_t
* /* O - CUPS file */
1919 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1923 * Open file descriptor 0 as needed...
1926 if (!cg
->stdio_files
[0])
1929 * Open file descriptor 0...
1932 if ((cg
->stdio_files
[0] = cupsFileOpenFd(0, "r")) != NULL
)
1933 cg
->stdio_files
[0]->is_stdio
= 1;
1936 return (cg
->stdio_files
[0]);
1941 * 'cupsFileStdout()' - Return a CUPS file associated with stdout.
1943 * @since CUPS 1.2/macOS 10.5@
1946 cups_file_t
* /* O - CUPS file */
1947 cupsFileStdout(void)
1949 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1953 * Open file descriptor 1 as needed...
1956 if (!cg
->stdio_files
[1])
1959 * Flush any pending output on the stdio file...
1965 * Open file descriptor 1...
1968 if ((cg
->stdio_files
[1] = cupsFileOpenFd(1, "w")) != NULL
)
1969 cg
->stdio_files
[1]->is_stdio
= 1;
1972 return (cg
->stdio_files
[1]);
1977 * 'cupsFileTell()' - Return the current file position.
1979 * @since CUPS 1.2/macOS 10.5@
1982 off_t
/* O - File position */
1983 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
1985 DEBUG_printf(("2cupsFileTell(fp=%p)", (void *)fp
));
1986 DEBUG_printf(("3cupsFileTell: pos=" CUPS_LLFMT
, CUPS_LLCAST (fp
? fp
->pos
: -1)));
1988 return (fp
? fp
->pos
: 0);
1993 * 'cupsFileUnlock()' - Unlock access to a file.
1995 * @since CUPS 1.2/macOS 10.5@
1998 int /* O - 0 on success, -1 on error */
1999 cupsFileUnlock(cups_file_t
*fp
) /* I - CUPS file */
2005 DEBUG_printf(("cupsFileUnlock(fp=%p)", (void *)fp
));
2007 if (!fp
|| fp
->mode
== 's')
2015 return (_locking(fp
->fd
, _LK_UNLCK
, 0));
2017 return (lockf(fp
->fd
, F_ULOCK
, 0));
2023 * 'cupsFileWrite()' - Write to a file.
2025 * @since CUPS 1.2/macOS 10.5@
2028 ssize_t
/* O - Number of bytes written or -1 on error */
2029 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
2030 const char *buf
, /* I - Buffer */
2031 size_t bytes
) /* I - Number of bytes to write */
2034 * Range check input...
2037 DEBUG_printf(("2cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2039 if (!fp
|| !buf
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
2046 * Write the buffer...
2049 if (fp
->mode
== 's')
2051 if (cups_write(fp
, buf
, bytes
) < 0)
2054 fp
->pos
+= (off_t
)bytes
;
2056 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2058 return ((ssize_t
)bytes
);
2061 if ((fp
->ptr
+ bytes
) > fp
->end
)
2062 if (cupsFileFlush(fp
))
2065 fp
->pos
+= (off_t
)bytes
;
2067 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2069 if (bytes
> sizeof(fp
->buf
))
2073 return (cups_compress(fp
, buf
, bytes
));
2075 #endif /* HAVE_LIBZ */
2076 return (cups_write(fp
, buf
, bytes
));
2080 memcpy(fp
->ptr
, buf
, bytes
);
2082 return ((ssize_t
)bytes
);
2089 * 'cups_compress()' - Compress a buffer of data.
2092 static ssize_t
/* O - Number of bytes written or -1 */
2093 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
2094 const char *buf
, /* I - Buffer */
2095 size_t bytes
) /* I - Number bytes */
2097 DEBUG_printf(("7cups_compress(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2103 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, (uInt
)bytes
);
2106 * Deflate the bytes...
2109 fp
->stream
.next_in
= (Bytef
*)buf
;
2110 fp
->stream
.avail_in
= (uInt
)bytes
;
2112 while (fp
->stream
.avail_in
> 0)
2115 * Flush the current buffer...
2118 DEBUG_printf(("9cups_compress: avail_in=%d, avail_out=%d",
2119 fp
->stream
.avail_in
, fp
->stream
.avail_out
));
2121 if (fp
->stream
.avail_out
< (uInt
)(sizeof(fp
->cbuf
) / 8))
2123 if (cups_write(fp
, (char *)fp
->cbuf
, (size_t)(fp
->stream
.next_out
- fp
->cbuf
)) < 0)
2126 fp
->stream
.next_out
= fp
->cbuf
;
2127 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
2130 deflate(&(fp
->stream
), Z_NO_FLUSH
);
2133 return ((ssize_t
)bytes
);
2135 #endif /* HAVE_LIBZ */
2139 * 'cups_fill()' - Fill the input buffer.
2142 static ssize_t
/* O - Number of bytes or -1 */
2143 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
2145 ssize_t bytes
; /* Number of bytes read */
2147 int status
; /* Decompression status */
2148 const unsigned char *ptr
, /* Pointer into buffer */
2149 *end
; /* End of buffer */
2150 #endif /* HAVE_LIBZ */
2153 DEBUG_printf(("7cups_fill(fp=%p)", (void *)fp
));
2154 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
));
2156 if (fp
->ptr
&& fp
->end
)
2157 fp
->bufpos
+= fp
->end
- fp
->buf
;
2160 DEBUG_printf(("9cups_fill: fp->compressed=%d", fp
->compressed
));
2162 while (!fp
->ptr
|| fp
->compressed
)
2165 * Check to see if we have read any data yet; if not, see if we have a
2166 * compressed file...
2172 * Reset the file position in case we are seeking...
2178 * Read the first bytes in the file to determine if we have a gzip'd
2182 if ((bytes
= cups_read(fp
, (char *)fp
->buf
, sizeof(fp
->buf
))) < 0)
2185 * Can't read from file!
2188 DEBUG_printf(("9cups_fill: cups_read() returned " CUPS_LLFMT
,
2189 CUPS_LLCAST bytes
));
2196 if (bytes
< 10 || fp
->buf
[0] != 0x1f ||
2197 (fp
->buf
[1] & 255) != 0x8b ||
2198 fp
->buf
[2] != 8 || (fp
->buf
[3] & 0xe0) != 0)
2201 * Not a gzip'd file!
2205 fp
->end
= fp
->buf
+ bytes
;
2207 DEBUG_printf(("9cups_fill: Returning " CUPS_LLFMT
,
2208 CUPS_LLCAST bytes
));
2214 * Parse header junk: extra data, original name, and comment...
2217 ptr
= (unsigned char *)fp
->buf
+ 10;
2218 end
= (unsigned char *)fp
->buf
+ bytes
;
2220 if (fp
->buf
[3] & 0x04)
2223 * Skip extra data...
2226 if ((ptr
+ 2) > end
)
2229 * Can't read from file!
2232 DEBUG_puts("9cups_fill: Extra gzip header data missing, returning -1.");
2240 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
2246 * Can't read from file!
2249 DEBUG_puts("9cups_fill: Extra gzip header data does not fit in initial buffer, returning -1.");
2258 if (fp
->buf
[3] & 0x08)
2261 * Skip original name data...
2264 while (ptr
< end
&& *ptr
)
2272 * Can't read from file!
2275 DEBUG_puts("9cups_fill: Original filename in gzip header data does not fit in initial buffer, returning -1.");
2284 if (fp
->buf
[3] & 0x10)
2287 * Skip comment data...
2290 while (ptr
< end
&& *ptr
)
2298 * Can't read from file!
2301 DEBUG_puts("9cups_fill: Comment in gzip header data does not fit in initial buffer, returning -1.");
2310 if (fp
->buf
[3] & 0x02)
2313 * Skip header CRC data...
2321 * Can't read from file!
2324 DEBUG_puts("9cups_fill: Header CRC in gzip header data does not fit in initial buffer, returning -1.");
2334 * Copy the flate-compressed data to the compression buffer...
2337 if ((bytes
= end
- ptr
) > 0)
2338 memcpy(fp
->cbuf
, ptr
, (size_t)bytes
);
2341 * Setup the decompressor data...
2344 fp
->stream
.zalloc
= (alloc_func
)0;
2345 fp
->stream
.zfree
= (free_func
)0;
2346 fp
->stream
.opaque
= (voidpf
)0;
2347 fp
->stream
.next_in
= (Bytef
*)fp
->cbuf
;
2348 fp
->stream
.next_out
= NULL
;
2349 fp
->stream
.avail_in
= (uInt
)bytes
;
2350 fp
->stream
.avail_out
= 0;
2351 fp
->crc
= crc32(0L, Z_NULL
, 0);
2353 if ((status
= inflateInit2(&(fp
->stream
), -15)) != Z_OK
)
2355 DEBUG_printf(("9cups_fill: inflateInit2 returned %d, returning -1.", status
));
2369 * If we have reached end-of-file, return immediately...
2374 DEBUG_puts("9cups_fill: EOF, returning 0.");
2380 * Fill the decompression buffer as needed...
2383 if (fp
->stream
.avail_in
== 0)
2385 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
2387 DEBUG_printf(("9cups_fill: cups_read error, returning %d.", (int)bytes
));
2394 fp
->stream
.next_in
= fp
->cbuf
;
2395 fp
->stream
.avail_in
= (uInt
)bytes
;
2399 * Decompress data from the buffer...
2402 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
2403 fp
->stream
.avail_out
= sizeof(fp
->buf
);
2405 status
= inflate(&(fp
->stream
), Z_NO_FLUSH
);
2407 if (fp
->stream
.next_out
> (Bytef
*)fp
->buf
)
2408 fp
->crc
= crc32(fp
->crc
, (Bytef
*)fp
->buf
,
2409 (uInt
)(fp
->stream
.next_out
- (Bytef
*)fp
->buf
));
2411 if (status
== Z_STREAM_END
)
2414 * Read the CRC and length...
2417 unsigned char trailer
[8]; /* Trailer bytes */
2418 uLong tcrc
; /* Trailer CRC */
2419 ssize_t tbytes
= 0; /* Number of bytes */
2421 if (fp
->stream
.avail_in
> 0)
2423 if (fp
->stream
.avail_in
> sizeof(trailer
))
2424 tbytes
= (ssize_t
)sizeof(trailer
);
2426 tbytes
= (ssize_t
)fp
->stream
.avail_in
;
2428 memcpy(trailer
, fp
->stream
.next_in
, (size_t)tbytes
);
2429 fp
->stream
.next_in
+= tbytes
;
2430 fp
->stream
.avail_in
-= (size_t)tbytes
;
2433 if (tbytes
< (ssize_t
)sizeof(trailer
))
2435 if (read(fp
->fd
, trailer
+ tbytes
, sizeof(trailer
) - (size_t)tbytes
) < ((ssize_t
)sizeof(trailer
) - tbytes
))
2438 * Can't get it, so mark end-of-file...
2441 DEBUG_puts("9cups_fill: Unable to read gzip CRC trailer, returning -1.");
2450 tcrc
= ((((((uLong
)trailer
[3] << 8) | (uLong
)trailer
[2]) << 8) |
2451 (uLong
)trailer
[1]) << 8) | (uLong
)trailer
[0];
2453 if (tcrc
!= fp
->crc
)
2456 * Bad CRC, mark end-of-file...
2459 DEBUG_printf(("9cups_fill: tcrc=%08x != fp->crc=%08x, returning -1.", (unsigned int)tcrc
, (unsigned int)fp
->crc
));
2468 * Otherwise, reset the compressed flag so that we re-read the
2474 else if (status
< Z_OK
)
2476 DEBUG_printf(("9cups_fill: inflate returned %d, returning -1.", status
));
2484 bytes
= (ssize_t
)sizeof(fp
->buf
) - (ssize_t
)fp
->stream
.avail_out
;
2487 * Return the decompressed data...
2491 fp
->end
= fp
->buf
+ bytes
;
2495 DEBUG_printf(("9cups_fill: Returning %d.", (int)bytes
));
2500 #endif /* HAVE_LIBZ */
2503 * Read a buffer's full of data...
2506 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
2509 * Can't read from file!
2519 * Return the bytes we read...
2524 fp
->end
= fp
->buf
+ bytes
;
2527 DEBUG_printf(("9cups_fill: Not gzip, returning %d.", (int)bytes
));
2534 * 'cups_open()' - Safely open a file for writing.
2536 * We don't allow appending to directories or files that are hard-linked or
2540 static int /* O - File descriptor or -1 otherwise */
2541 cups_open(const char *filename
, /* I - Filename */
2542 int mode
) /* I - Open mode */
2544 int fd
; /* File descriptor */
2545 struct stat fileinfo
; /* File information */
2547 struct stat linkinfo
; /* Link information */
2555 if ((fd
= open(filename
, mode
, 0666)) < 0)
2559 * Then verify that the file descriptor doesn't point to a directory or hard-
2563 if (fstat(fd
, &fileinfo
))
2569 if (fileinfo
.st_nlink
!= 1)
2577 if (fileinfo
.st_mode
& _S_IFDIR
)
2579 if (S_ISDIR(fileinfo
.st_mode
))
2589 * Then use lstat to determine whether the filename is a symlink...
2592 if (lstat(filename
, &linkinfo
))
2598 if (S_ISLNK(linkinfo
.st_mode
) ||
2599 fileinfo
.st_dev
!= linkinfo
.st_dev
||
2600 fileinfo
.st_ino
!= linkinfo
.st_ino
||
2602 fileinfo
.st_gen
!= linkinfo
.st_gen
||
2603 #endif /* HAVE_ST_GEN */
2604 fileinfo
.st_nlink
!= linkinfo
.st_nlink
||
2605 fileinfo
.st_mode
!= linkinfo
.st_mode
)
2622 * 'cups_read()' - Read from a file descriptor.
2625 static ssize_t
/* O - Number of bytes read or -1 */
2626 cups_read(cups_file_t
*fp
, /* I - CUPS file */
2627 char *buf
, /* I - Buffer */
2628 size_t bytes
) /* I - Number bytes */
2630 ssize_t total
; /* Total bytes read */
2633 DEBUG_printf(("7cups_read(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2636 * Loop until we read at least 0 bytes...
2642 if (fp
->mode
== 's')
2643 total
= (ssize_t
)recv(fp
->fd
, buf
, (unsigned)bytes
, 0);
2645 total
= (ssize_t
)read(fp
->fd
, buf
, (unsigned)bytes
);
2647 if (fp
->mode
== 's')
2648 total
= recv(fp
->fd
, buf
, bytes
, 0);
2650 total
= read(fp
->fd
, buf
, bytes
);
2653 DEBUG_printf(("9cups_read: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
2659 * Reads can be interrupted by signals and unavailable resources...
2662 if (errno
== EAGAIN
|| errno
== EINTR
)
2669 * Return the total number of bytes read...
2677 * 'cups_write()' - Write to a file descriptor.
2680 static ssize_t
/* O - Number of bytes written or -1 */
2681 cups_write(cups_file_t
*fp
, /* I - CUPS file */
2682 const char *buf
, /* I - Buffer */
2683 size_t bytes
) /* I - Number bytes */
2685 size_t total
; /* Total bytes written */
2686 ssize_t count
; /* Count this time */
2689 DEBUG_printf(("7cups_write(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2692 * Loop until all bytes are written...
2699 if (fp
->mode
== 's')
2700 count
= (ssize_t
)send(fp
->fd
, buf
, (unsigned)bytes
, 0);
2702 count
= (ssize_t
)write(fp
->fd
, buf
, (unsigned)bytes
);
2704 if (fp
->mode
== 's')
2705 count
= send(fp
->fd
, buf
, bytes
, 0);
2707 count
= write(fp
->fd
, buf
, bytes
);
2710 DEBUG_printf(("9cups_write: count=" CUPS_LLFMT
, CUPS_LLCAST count
));
2715 * Writes can be interrupted by signals and unavailable resources...
2718 if (errno
== EAGAIN
|| errno
== EINTR
)
2725 * Update the counts for the last write call...
2728 bytes
-= (size_t)count
;
2729 total
+= (size_t)count
;
2734 * Return the total number of bytes written...
2737 return ((ssize_t
)total
);