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 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
16 * Include necessary headers...
19 #include "file-private.h"
21 #include <sys/types.h>
29 static ssize_t
cups_compress(cups_file_t
*fp
, const char *buf
, size_t bytes
);
30 #endif /* HAVE_LIBZ */
31 static ssize_t
cups_fill(cups_file_t
*fp
);
32 static int cups_open(const char *filename
, int mode
);
33 static ssize_t
cups_read(cups_file_t
*fp
, char *buf
, size_t bytes
);
34 static ssize_t
cups_write(cups_file_t
*fp
, const char *buf
, size_t bytes
);
39 * '_cupsFileCheck()' - Check the permissions of the given filename.
42 _cups_fc_result_t
/* O - Check result */
44 const char *filename
, /* I - Filename to check */
45 _cups_fc_filetype_t filetype
, /* I - Type of file checks? */
46 int dorootchecks
, /* I - Check for root permissions? */
47 _cups_fc_func_t cb
, /* I - Callback function */
48 void *context
) /* I - Context pointer for callback */
51 struct stat fileinfo
; /* File information */
52 char message
[1024], /* Message string */
53 temp
[1024], /* Parent directory filename */
54 *ptr
; /* Pointer into parent directory */
55 _cups_fc_result_t result
; /* Check result */
59 * Does the filename contain a relative path ("../")?
62 if (strstr(filename
, "../"))
68 result
= _CUPS_FILE_CHECK_RELATIVE_PATH
;
73 * Does the program even exist and is it accessible?
76 if (stat(filename
, &fileinfo
))
82 result
= _CUPS_FILE_CHECK_MISSING
;
87 * Check the execute bit...
90 result
= _CUPS_FILE_CHECK_OK
;
94 case _CUPS_FILE_CHECK_DIRECTORY
:
95 if (!S_ISDIR(fileinfo
.st_mode
))
96 result
= _CUPS_FILE_CHECK_WRONG_TYPE
;
100 if (!S_ISREG(fileinfo
.st_mode
))
101 result
= _CUPS_FILE_CHECK_WRONG_TYPE
;
109 * Are we doing root checks?
115 * Nope, so anything (else) goes...
122 * Verify permission of the file itself:
124 * 1. Must be owned by root
125 * 2. Must not be writable by group
126 * 3. Must not be setuid
127 * 4. Must not be writable by others
130 if (fileinfo
.st_uid
|| /* 1. Must be owned by root */
131 (fileinfo
.st_mode
& S_IWGRP
) || /* 2. Must not be writable by group */
132 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
133 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
135 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
139 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
||
140 filetype
== _CUPS_FILE_CHECK_FILE_ONLY
)
144 * Now check the containing directory...
147 strlcpy(temp
, filename
, sizeof(temp
));
148 if ((ptr
= strrchr(temp
, '/')) != NULL
)
156 if (stat(temp
, &fileinfo
))
162 result
= _CUPS_FILE_CHECK_MISSING
;
163 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
169 if (fileinfo
.st_uid
|| /* 1. Must be owned by root */
170 (fileinfo
.st_mode
& S_IWGRP
) || /* 2. Must not be writable by group */
171 (fileinfo
.st_mode
& S_ISUID
) || /* 3. Must not be setuid */
172 (fileinfo
.st_mode
& S_IWOTH
)) /* 4. Must not be writable by others */
174 result
= _CUPS_FILE_CHECK_PERMISSIONS
;
175 filetype
= _CUPS_FILE_CHECK_DIRECTORY
;
180 * Common return point...
187 cups_lang_t
*lang
= cupsLangDefault();
188 /* Localization information */
192 case _CUPS_FILE_CHECK_OK
:
193 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
194 snprintf(message
, sizeof(message
),
195 _cupsLangString(lang
, _("Directory \"%s\" permissions OK "
196 "(0%o/uid=%d/gid=%d).")),
197 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
198 (int)fileinfo
.st_gid
);
200 snprintf(message
, sizeof(message
),
201 _cupsLangString(lang
, _("File \"%s\" permissions OK "
202 "(0%o/uid=%d/gid=%d).")),
203 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
204 (int)fileinfo
.st_gid
);
207 case _CUPS_FILE_CHECK_MISSING
:
208 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
209 snprintf(message
, sizeof(message
),
210 _cupsLangString(lang
, _("Directory \"%s\" not available: "
212 filename
, strerror(errno
));
214 snprintf(message
, sizeof(message
),
215 _cupsLangString(lang
, _("File \"%s\" not available: %s")),
216 filename
, strerror(errno
));
219 case _CUPS_FILE_CHECK_PERMISSIONS
:
220 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
221 snprintf(message
, sizeof(message
),
222 _cupsLangString(lang
, _("Directory \"%s\" has insecure "
224 "(0%o/uid=%d/gid=%d).")),
225 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
226 (int)fileinfo
.st_gid
);
228 snprintf(message
, sizeof(message
),
229 _cupsLangString(lang
, _("File \"%s\" has insecure "
231 "(0%o/uid=%d/gid=%d).")),
232 filename
, fileinfo
.st_mode
, (int)fileinfo
.st_uid
,
233 (int)fileinfo
.st_gid
);
236 case _CUPS_FILE_CHECK_WRONG_TYPE
:
237 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
238 snprintf(message
, sizeof(message
),
239 _cupsLangString(lang
, _("Directory \"%s\" is a file.")),
242 snprintf(message
, sizeof(message
),
243 _cupsLangString(lang
, _("File \"%s\" is a directory.")),
247 case _CUPS_FILE_CHECK_RELATIVE_PATH
:
248 if (filetype
== _CUPS_FILE_CHECK_DIRECTORY
)
249 snprintf(message
, sizeof(message
),
250 _cupsLangString(lang
, _("Directory \"%s\" contains a "
251 "relative path.")), filename
);
253 snprintf(message
, sizeof(message
),
254 _cupsLangString(lang
, _("File \"%s\" contains a relative "
255 "path.")), filename
);
259 (*cb
)(context
, result
, message
);
267 * '_cupsFileCheckFilter()' - Report file check results as CUPS filter messages.
271 _cupsFileCheckFilter(
272 void *context
, /* I - Context pointer (unused) */
273 _cups_fc_result_t result
, /* I - Result code */
274 const char *message
) /* I - Message text */
276 const char *prefix
; /* Messaging prefix */
284 case _CUPS_FILE_CHECK_OK
:
288 case _CUPS_FILE_CHECK_MISSING
:
289 case _CUPS_FILE_CHECK_WRONG_TYPE
:
291 fputs("STATE: +cups-missing-filter-warning\n", stderr
);
294 case _CUPS_FILE_CHECK_PERMISSIONS
:
295 case _CUPS_FILE_CHECK_RELATIVE_PATH
:
297 fputs("STATE: +cups-insecure-filter-warning\n", stderr
);
301 fprintf(stderr
, "%s: %s\n", prefix
, message
);
307 * 'cupsFileClose()' - Close a CUPS file.
309 * @since CUPS 1.2/macOS 10.5@
312 int /* O - 0 on success, -1 on error */
313 cupsFileClose(cups_file_t
*fp
) /* I - CUPS file */
315 int fd
; /* File descriptor */
316 char mode
; /* Open mode */
317 int status
; /* Return status */
320 DEBUG_printf(("cupsFileClose(fp=%p)", (void *)fp
));
330 * Flush pending write data...
334 status
= cupsFileFlush(fp
);
339 if (fp
->compressed
&& status
>= 0)
344 * Free decompression data...
347 inflateEnd(&fp
->stream
);
352 * Flush any remaining compressed data...
355 unsigned char trailer
[8]; /* Trailer CRC and length */
356 int done
; /* Done writing... */
359 fp
->stream
.avail_in
= 0;
363 if (fp
->stream
.next_out
> fp
->cbuf
)
365 if (cups_write(fp
, (char *)fp
->cbuf
,
366 (size_t)(fp
->stream
.next_out
- fp
->cbuf
)) < 0)
369 fp
->stream
.next_out
= fp
->cbuf
;
370 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
373 if (done
|| status
< 0)
376 done
= deflate(&fp
->stream
, Z_FINISH
) == Z_STREAM_END
&&
377 fp
->stream
.next_out
== fp
->cbuf
;
381 * Write the CRC and length...
384 trailer
[0] = (unsigned char)fp
->crc
;
385 trailer
[1] = (unsigned char)(fp
->crc
>> 8);
386 trailer
[2] = (unsigned char)(fp
->crc
>> 16);
387 trailer
[3] = (unsigned char)(fp
->crc
>> 24);
388 trailer
[4] = (unsigned char)fp
->pos
;
389 trailer
[5] = (unsigned char)(fp
->pos
>> 8);
390 trailer
[6] = (unsigned char)(fp
->pos
>> 16);
391 trailer
[7] = (unsigned char)(fp
->pos
>> 24);
393 if (cups_write(fp
, (char *)trailer
, 8) < 0)
397 * Free all memory used by the compression stream...
400 deflateEnd(&(fp
->stream
));
403 #endif /* HAVE_LIBZ */
406 * If this is one of the cupsFileStdin/out/err files, return now and don't
407 * actually free memory or close (these last the life of the process...)
414 * Save the file descriptor we used and free memory...
420 if (fp
->printf_buffer
)
421 free(fp
->printf_buffer
);
426 * Close the file, returning the close status...
431 if (httpAddrClose(NULL
, fd
) < 0)
434 else if (close(fd
) < 0)
442 * 'cupsFileCompression()' - Return whether a file is compressed.
444 * @since CUPS 1.2/macOS 10.5@
447 int /* O - @code CUPS_FILE_NONE@ or @code CUPS_FILE_GZIP@ */
448 cupsFileCompression(cups_file_t
*fp
) /* I - CUPS file */
450 return (fp
? fp
->compressed
: CUPS_FILE_NONE
);
455 * 'cupsFileEOF()' - Return the end-of-file status.
457 * @since CUPS 1.2/macOS 10.5@
460 int /* O - 1 on end of file, 0 otherwise */
461 cupsFileEOF(cups_file_t
*fp
) /* I - CUPS file */
463 return (fp
? fp
->eof
: 1);
468 * 'cupsFileFind()' - Find a file using the specified path.
470 * This function allows the paths in the path string to be separated by
471 * colons (UNIX standard) or semicolons (Windows standard) and stores the
472 * result in the buffer supplied. If the file cannot be found in any of
473 * the supplied paths, @code NULL@ is returned. A @code NULL@ path only
474 * matches the current directory.
476 * @since CUPS 1.2/macOS 10.5@
479 const char * /* O - Full path to file or @code NULL@ if not found */
480 cupsFileFind(const char *filename
, /* I - File to find */
481 const char *path
, /* I - Colon/semicolon-separated path */
482 int executable
, /* I - 1 = executable files, 0 = any file/dir */
483 char *buffer
, /* I - Filename buffer */
484 int bufsize
) /* I - Size of filename buffer */
486 char *bufptr
, /* Current position in buffer */
487 *bufend
; /* End of buffer */
491 * Range check input...
494 DEBUG_printf(("cupsFileFind(filename=\"%s\", path=\"%s\", executable=%d, buffer=%p, bufsize=%d)", filename
, path
, executable
, (void *)buffer
, bufsize
));
496 if (!filename
|| !buffer
|| bufsize
< 2)
502 * No path, so check current directory...
505 if (!access(filename
, 0))
507 strlcpy(buffer
, filename
, (size_t)bufsize
);
515 * Now check each path and return the first match...
518 bufend
= buffer
+ bufsize
- 1;
524 if (*path
== ';' || (*path
== ':' && ((bufptr
- buffer
) > 1 || !isalpha(buffer
[0] & 255))))
526 if (*path
== ';' || *path
== ':')
529 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
532 strlcpy(bufptr
, filename
, (size_t)(bufend
- bufptr
));
535 if (!access(buffer
, 0))
537 if (!access(buffer
, executable
? X_OK
: 0))
540 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
546 else if (bufptr
< bufend
)
553 * Check the last path...
556 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
559 strlcpy(bufptr
, filename
, (size_t)(bufend
- bufptr
));
561 if (!access(buffer
, 0))
563 DEBUG_printf(("1cupsFileFind: Returning \"%s\"", buffer
));
568 DEBUG_puts("1cupsFileFind: Returning NULL");
575 * 'cupsFileFlush()' - Flush pending output.
577 * @since CUPS 1.2/macOS 10.5@
580 int /* O - 0 on success, -1 on error */
581 cupsFileFlush(cups_file_t
*fp
) /* I - CUPS file */
583 ssize_t bytes
; /* Bytes to write */
586 DEBUG_printf(("cupsFileFlush(fp=%p)", (void *)fp
));
589 * Range check input...
592 if (!fp
|| fp
->mode
!= 'w')
594 DEBUG_puts("1cupsFileFlush: Attempt to flush a read-only file...");
598 bytes
= (ssize_t
)(fp
->ptr
- fp
->buf
);
600 DEBUG_printf(("2cupsFileFlush: Flushing " CUPS_LLFMT
" bytes...",
607 bytes
= cups_compress(fp
, fp
->buf
, (size_t)bytes
);
609 #endif /* HAVE_LIBZ */
610 bytes
= cups_write(fp
, fp
->buf
, (size_t)bytes
);
623 * 'cupsFileGetChar()' - Get a single character from a file.
625 * @since CUPS 1.2/macOS 10.5@
628 int /* O - Character or -1 on end of file */
629 cupsFileGetChar(cups_file_t
*fp
) /* I - CUPS file */
632 * Range check input...
635 DEBUG_printf(("4cupsFileGetChar(fp=%p)", (void *)fp
));
637 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
639 DEBUG_puts("5cupsFileGetChar: Bad arguments!");
644 * If the input buffer is empty, try to read more data...
647 DEBUG_printf(("5cupsFileGetChar: fp->eof=%d, fp->ptr=%p, fp->end=%p", fp
->eof
, (void *)fp
->ptr
, (void *)fp
->end
));
649 if (fp
->ptr
>= fp
->end
)
650 if (cups_fill(fp
) <= 0)
652 DEBUG_puts("5cupsFileGetChar: Unable to fill buffer!");
657 * Return the next character in the buffer...
660 DEBUG_printf(("5cupsFileGetChar: Returning %d...", *(fp
->ptr
) & 255));
664 DEBUG_printf(("6cupsFileGetChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
666 return (*(fp
->ptr
)++ & 255);
671 * 'cupsFileGetConf()' - Get a line from a configuration file.
673 * @since CUPS 1.2/macOS 10.5@
676 char * /* O - Line read or @code NULL@ on end of file or error */
677 cupsFileGetConf(cups_file_t
*fp
, /* I - CUPS file */
678 char *buf
, /* O - String buffer */
679 size_t buflen
, /* I - Size of string buffer */
680 char **value
, /* O - Pointer to value */
681 int *linenum
) /* IO - Current line number */
683 char *ptr
; /* Pointer into line */
687 * Range check input...
690 DEBUG_printf(("2cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT
691 ", value=%p, linenum=%p)", (void *)fp
, (void *)buf
, CUPS_LLCAST buflen
, (void *)value
, (void *)linenum
));
693 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') ||
694 !buf
|| buflen
< 2 || !value
)
703 * Read the next non-comment line...
708 while (cupsFileGets(fp
, buf
, buflen
))
713 * Strip any comments...
716 if ((ptr
= strchr(buf
, '#')) != NULL
)
718 if (ptr
> buf
&& ptr
[-1] == '\\')
721 _cups_strcpy(ptr
- 1, ptr
);
725 // Strip the comment and any trailing whitespace...
728 if (!_cups_isspace(ptr
[-1]))
739 * Strip leading whitespace...
742 for (ptr
= buf
; _cups_isspace(*ptr
); ptr
++);
745 _cups_strcpy(buf
, ptr
);
748 * See if there is anything left...
754 * Yes, grab any value and return...
757 for (ptr
= buf
; *ptr
; ptr
++)
758 if (_cups_isspace(*ptr
))
764 * Have a value, skip any other spaces...
767 while (_cups_isspace(*ptr
))
774 * Strip trailing whitespace and > for lines that begin with <...
777 ptr
+= strlen(ptr
) - 1;
779 if (buf
[0] == '<' && *ptr
== '>')
781 else if (buf
[0] == '<' && *ptr
!= '>')
791 while (ptr
> *value
&& _cups_isspace(*ptr
))
808 * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may
809 * contain binary data.
811 * This function differs from @link cupsFileGets@ in that the trailing CR
812 * and LF are preserved, as is any binary data on the line. The buffer is
813 * nul-terminated, however you should use the returned length to determine
814 * the number of bytes on the line.
816 * @since CUPS 1.2/macOS 10.5@
819 size_t /* O - Number of bytes on line or 0 on end of file */
820 cupsFileGetLine(cups_file_t
*fp
, /* I - File to read from */
821 char *buf
, /* I - Buffer */
822 size_t buflen
) /* I - Size of buffer */
824 int ch
; /* Character from file */
825 char *ptr
, /* Current position in line buffer */
826 *end
; /* End of line buffer */
830 * Range check input...
833 DEBUG_printf(("2cupsFileGetLine(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST buflen
));
835 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 3)
839 * Now loop until we have a valid line...
842 for (ptr
= buf
, end
= buf
+ buflen
- 2; ptr
< end
;)
844 if (fp
->ptr
>= fp
->end
)
845 if (cups_fill(fp
) <= 0)
848 *ptr
++ = ch
= *(fp
->ptr
)++;
857 if (fp
->ptr
>= fp
->end
)
858 if (cups_fill(fp
) <= 0)
861 if (*(fp
->ptr
) == '\n')
863 *ptr
++ = *(fp
->ptr
)++;
872 * Line feed ends a line...
881 DEBUG_printf(("4cupsFileGetLine: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
883 return ((size_t)(ptr
- buf
));
888 * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
890 * @since CUPS 1.2/macOS 10.5@
893 char * /* O - Line read or @code NULL@ on end of file or error */
894 cupsFileGets(cups_file_t
*fp
, /* I - CUPS file */
895 char *buf
, /* O - String buffer */
896 size_t buflen
) /* I - Size of string buffer */
898 int ch
; /* Character from file */
899 char *ptr
, /* Current position in line buffer */
900 *end
; /* End of line buffer */
904 * Range check input...
907 DEBUG_printf(("2cupsFileGets(fp=%p, buf=%p, buflen=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST buflen
));
909 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 2)
913 * Now loop until we have a valid line...
916 for (ptr
= buf
, end
= buf
+ buflen
- 1; ptr
< end
;)
918 if (fp
->ptr
>= fp
->end
)
919 if (cups_fill(fp
) <= 0)
936 if (fp
->ptr
>= fp
->end
)
937 if (cups_fill(fp
) <= 0)
940 if (*(fp
->ptr
) == '\n')
951 * Line feed ends a line...
962 DEBUG_printf(("4cupsFileGets: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
969 * 'cupsFileLock()' - Temporarily lock access to a file.
971 * @since CUPS 1.2/macOS 10.5@
974 int /* O - 0 on success, -1 on error */
975 cupsFileLock(cups_file_t
*fp
, /* I - CUPS file */
976 int block
) /* I - 1 to wait for the lock, 0 to fail right away */
982 if (!fp
|| fp
->mode
== 's')
990 return (_locking(fp
->fd
, block
? _LK_LOCK
: _LK_NBLCK
, 0));
992 return (lockf(fp
->fd
, block
? F_LOCK
: F_TLOCK
, 0));
998 * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
1000 * @since CUPS 1.2/macOS 10.5@
1003 int /* O - File descriptor */
1004 cupsFileNumber(cups_file_t
*fp
) /* I - CUPS file */
1014 * 'cupsFileOpen()' - Open a CUPS file.
1016 * The "mode" parameter can be "r" to read, "w" to write, overwriting any
1017 * existing file, "a" to append to an existing file or create a new file,
1018 * or "s" to open a socket connection.
1020 * When opening for writing ("w"), an optional number from 1 to 9 can be
1021 * supplied which enables Flate compression of the file. Compression is
1022 * not supported for the "a" (append) mode.
1024 * When opening a socket connection, the filename is a string of the form
1025 * "address:port" or "hostname:port". The socket will make an IPv4 or IPv6
1026 * connection as needed, generally preferring IPv6 connections when there is
1029 * @since CUPS 1.2/macOS 10.5@
1032 cups_file_t
* /* O - CUPS file or @code NULL@ if the file or socket cannot be opened */
1033 cupsFileOpen(const char *filename
, /* I - Name of file */
1034 const char *mode
) /* I - Open mode */
1036 cups_file_t
*fp
; /* New CUPS file */
1037 int fd
; /* File descriptor */
1038 char hostname
[1024], /* Hostname */
1039 *portname
; /* Port "name" (number or service) */
1040 http_addrlist_t
*addrlist
; /* Host address list */
1043 DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")", filename
,
1047 * Range check input...
1050 if (!filename
|| !mode
||
1051 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1052 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1061 case 'a' : /* Append file */
1062 fd
= cups_open(filename
,
1063 O_RDWR
| O_CREAT
| O_APPEND
| O_LARGEFILE
| O_BINARY
);
1066 case 'r' : /* Read file */
1067 fd
= open(filename
, O_RDONLY
| O_LARGEFILE
| O_BINARY
, 0);
1070 case 'w' : /* Write file */
1071 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1072 if (fd
< 0 && errno
== ENOENT
)
1074 fd
= cups_open(filename
,
1075 O_WRONLY
| O_CREAT
| O_EXCL
| O_LARGEFILE
| O_BINARY
);
1076 if (fd
< 0 && errno
== EEXIST
)
1077 fd
= cups_open(filename
, O_WRONLY
| O_LARGEFILE
| O_BINARY
);
1088 case 's' : /* Read/write socket */
1089 strlcpy(hostname
, filename
, sizeof(hostname
));
1090 if ((portname
= strrchr(hostname
, ':')) != NULL
)
1096 * Lookup the hostname and service...
1099 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
1103 * Connect to the server...
1106 if (!httpAddrConnect(addrlist
, &fd
))
1108 httpAddrFreeList(addrlist
);
1112 httpAddrFreeList(addrlist
);
1115 default : /* Remove bogus compiler warning... */
1123 * Create the CUPS file structure...
1126 if ((fp
= cupsFileOpenFd(fd
, mode
)) == NULL
)
1129 httpAddrClose(NULL
, fd
);
1142 * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
1144 * The "mode" parameter can be "r" to read, "w" to write, "a" to append,
1145 * or "s" to treat the file descriptor as a bidirectional socket connection.
1147 * When opening for writing ("w"), an optional number from 1 to 9 can be
1148 * supplied which enables Flate compression of the file. Compression is
1149 * not supported for the "a" (append) mode.
1151 * @since CUPS 1.2/macOS 10.5@
1154 cups_file_t
* /* O - CUPS file or @code NULL@ if the file could not be opened */
1155 cupsFileOpenFd(int fd
, /* I - File descriptor */
1156 const char *mode
) /* I - Open mode */
1158 cups_file_t
*fp
; /* New CUPS file */
1161 DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")", fd
, mode
));
1164 * Range check input...
1167 if (fd
< 0 || !mode
||
1168 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's') ||
1169 (*mode
== 'a' && isdigit(mode
[1] & 255)))
1173 * Allocate memory...
1176 if ((fp
= calloc(1, sizeof(cups_file_t
))) == NULL
)
1188 fp
->pos
= lseek(fd
, 0, SEEK_END
);
1193 fp
->end
= fp
->buf
+ sizeof(fp
->buf
);
1196 if (mode
[1] >= '1' && mode
[1] <= '9')
1199 * Open a compressed stream, so write the standard gzip file
1203 unsigned char header
[10]; /* gzip file header */
1204 time_t curtime
; /* Current time */
1207 curtime
= time(NULL
);
1210 header
[2] = Z_DEFLATED
;
1212 header
[4] = (unsigned char)curtime
;
1213 header
[5] = (unsigned char)(curtime
>> 8);
1214 header
[6] = (unsigned char)(curtime
>> 16);
1215 header
[7] = (unsigned char)(curtime
>> 24);
1219 cups_write(fp
, (char *)header
, 10);
1222 * Initialize the compressor...
1225 deflateInit2(&(fp
->stream
), mode
[1] - '0', Z_DEFLATED
, -15, 8,
1226 Z_DEFAULT_STRATEGY
);
1228 fp
->stream
.next_out
= fp
->cbuf
;
1229 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
1231 fp
->crc
= crc32(0L, Z_NULL
, 0);
1233 #endif /* HAVE_LIBZ */
1244 default : /* Remove bogus compiler warning... */
1249 * Don't pass this file to child processes...
1253 fcntl(fp
->fd
, F_SETFD
, fcntl(fp
->fd
, F_GETFD
) | FD_CLOEXEC
);
1261 * 'cupsFilePeekChar()' - Peek at the next character from a file.
1263 * @since CUPS 1.2/macOS 10.5@
1266 int /* O - Character or -1 on end of file */
1267 cupsFilePeekChar(cups_file_t
*fp
) /* I - CUPS file */
1270 * Range check input...
1273 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1277 * If the input buffer is empty, try to read more data...
1280 if (fp
->ptr
>= fp
->end
)
1281 if (cups_fill(fp
) <= 0)
1285 * Return the next character in the buffer...
1288 return (*(fp
->ptr
) & 255);
1293 * 'cupsFilePrintf()' - Write a formatted string.
1295 * @since CUPS 1.2/macOS 10.5@
1298 int /* O - Number of bytes written or -1 on error */
1299 cupsFilePrintf(cups_file_t
*fp
, /* I - CUPS file */
1300 const char *format
, /* I - Printf-style format string */
1301 ...) /* I - Additional args as necessary */
1303 va_list ap
; /* Argument list */
1304 ssize_t bytes
; /* Formatted size */
1307 DEBUG_printf(("2cupsFilePrintf(fp=%p, format=\"%s\", ...)", (void *)fp
, format
));
1309 if (!fp
|| !format
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1312 if (!fp
->printf_buffer
)
1315 * Start with an 1k printf buffer...
1318 if ((fp
->printf_buffer
= malloc(1024)) == NULL
)
1321 fp
->printf_size
= 1024;
1324 va_start(ap
, format
);
1325 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1328 if (bytes
>= (ssize_t
)fp
->printf_size
)
1331 * Expand the printf buffer...
1334 char *temp
; /* Temporary buffer pointer */
1340 if ((temp
= realloc(fp
->printf_buffer
, (size_t)(bytes
+ 1))) == NULL
)
1343 fp
->printf_buffer
= temp
;
1344 fp
->printf_size
= (size_t)(bytes
+ 1);
1346 va_start(ap
, format
);
1347 bytes
= vsnprintf(fp
->printf_buffer
, fp
->printf_size
, format
, ap
);
1351 if (fp
->mode
== 's')
1353 if (cups_write(fp
, fp
->printf_buffer
, (size_t)bytes
) < 0)
1358 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1360 return ((int)bytes
);
1363 if ((fp
->ptr
+ bytes
) > fp
->end
)
1364 if (cupsFileFlush(fp
))
1369 DEBUG_printf(("4cupsFilePrintf: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1371 if ((size_t)bytes
> sizeof(fp
->buf
))
1375 return ((int)cups_compress(fp
, fp
->printf_buffer
, (size_t)bytes
));
1377 #endif /* HAVE_LIBZ */
1378 return ((int)cups_write(fp
, fp
->printf_buffer
, (size_t)bytes
));
1382 memcpy(fp
->ptr
, fp
->printf_buffer
, (size_t)bytes
);
1385 if (fp
->is_stdio
&& cupsFileFlush(fp
))
1388 return ((int)bytes
);
1394 * 'cupsFilePutChar()' - Write a character.
1396 * @since CUPS 1.2/macOS 10.5@
1399 int /* O - 0 on success, -1 on error */
1400 cupsFilePutChar(cups_file_t
*fp
, /* I - CUPS file */
1401 int c
) /* I - Character to write */
1404 * Range check input...
1407 if (!fp
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1410 if (fp
->mode
== 's')
1413 * Send character immediately over socket...
1416 char ch
; /* Output character */
1421 if (send(fp
->fd
, &ch
, 1, 0) < 1)
1430 if (fp
->ptr
>= fp
->end
)
1431 if (cupsFileFlush(fp
))
1434 *(fp
->ptr
) ++ = (char)c
;
1439 DEBUG_printf(("4cupsFilePutChar: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1446 * 'cupsFilePutConf()' - Write a configuration line.
1448 * This function handles any comment escaping of the value.
1450 * @since CUPS 1.4/macOS 10.6@
1453 ssize_t
/* O - Number of bytes written or -1 on error */
1454 cupsFilePutConf(cups_file_t
*fp
, /* I - CUPS file */
1455 const char *directive
, /* I - Directive */
1456 const char *value
) /* I - Value */
1458 ssize_t bytes
, /* Number of bytes written */
1459 temp
; /* Temporary byte count */
1460 const char *ptr
; /* Pointer into value */
1463 if (!fp
|| !directive
|| !*directive
)
1466 if ((bytes
= cupsFilePuts(fp
, directive
)) < 0)
1469 if (cupsFilePutChar(fp
, ' ') < 0)
1473 if (value
&& *value
)
1475 if ((ptr
= strchr(value
, '#')) != NULL
)
1478 * Need to quote the first # in the info string...
1481 if ((temp
= cupsFileWrite(fp
, value
, (size_t)(ptr
- value
))) < 0)
1485 if (cupsFilePutChar(fp
, '\\') < 0)
1489 if ((temp
= cupsFilePuts(fp
, ptr
)) < 0)
1493 else if ((temp
= cupsFilePuts(fp
, value
)) < 0)
1499 if (cupsFilePutChar(fp
, '\n') < 0)
1507 * 'cupsFilePuts()' - Write a string.
1509 * Like the @code fputs@ function, no newline is appended to the string.
1511 * @since CUPS 1.2/macOS 10.5@
1514 int /* O - Number of bytes written or -1 on error */
1515 cupsFilePuts(cups_file_t
*fp
, /* I - CUPS file */
1516 const char *s
) /* I - String to write */
1518 ssize_t bytes
; /* Bytes to write */
1522 * Range check input...
1525 if (!fp
|| !s
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1529 * Write the string...
1532 bytes
= (ssize_t
)strlen(s
);
1534 if (fp
->mode
== 's')
1536 if (cups_write(fp
, s
, (size_t)bytes
) < 0)
1541 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1543 return ((int)bytes
);
1546 if ((fp
->ptr
+ bytes
) > fp
->end
)
1547 if (cupsFileFlush(fp
))
1552 DEBUG_printf(("4cupsFilePuts: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1554 if ((size_t)bytes
> sizeof(fp
->buf
))
1558 return ((int)cups_compress(fp
, s
, (size_t)bytes
));
1560 #endif /* HAVE_LIBZ */
1561 return ((int)cups_write(fp
, s
, (size_t)bytes
));
1565 memcpy(fp
->ptr
, s
, (size_t)bytes
);
1568 if (fp
->is_stdio
&& cupsFileFlush(fp
))
1571 return ((int)bytes
);
1577 * 'cupsFileRead()' - Read from a file.
1579 * @since CUPS 1.2/macOS 10.5@
1582 ssize_t
/* O - Number of bytes read or -1 on error */
1583 cupsFileRead(cups_file_t
*fp
, /* I - CUPS file */
1584 char *buf
, /* O - Buffer */
1585 size_t bytes
) /* I - Number of bytes to read */
1587 size_t total
; /* Total bytes read */
1588 ssize_t count
; /* Bytes read */
1591 DEBUG_printf(("2cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
1594 * Range check input...
1597 if (!fp
|| !buf
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1604 * Loop until all bytes are read...
1610 if (fp
->ptr
>= fp
->end
)
1611 if (cups_fill(fp
) <= 0)
1613 DEBUG_printf(("4cupsFileRead: cups_fill() returned -1, total="
1614 CUPS_LLFMT
, CUPS_LLCAST total
));
1617 return ((ssize_t
)total
);
1622 count
= (ssize_t
)(fp
->end
- fp
->ptr
);
1623 if (count
> (ssize_t
)bytes
)
1624 count
= (ssize_t
)bytes
;
1626 memcpy(buf
, fp
->ptr
,(size_t) count
);
1630 DEBUG_printf(("4cupsFileRead: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1633 * Update the counts for the last read...
1636 bytes
-= (size_t)count
;
1637 total
+= (size_t)count
;
1642 * Return the total number of bytes read...
1645 DEBUG_printf(("3cupsFileRead: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
1647 return ((ssize_t
)total
);
1652 * 'cupsFileRewind()' - Set the current file position to the beginning of the
1655 * @since CUPS 1.2/macOS 10.5@
1658 off_t
/* O - New file position or -1 on error */
1659 cupsFileRewind(cups_file_t
*fp
) /* I - CUPS file */
1662 * Range check input...
1665 DEBUG_printf(("cupsFileRewind(fp=%p)", (void *)fp
));
1666 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1668 if (!fp
|| fp
->mode
!= 'r')
1672 * Handle special cases...
1675 if (fp
->bufpos
== 0)
1678 * No seeking necessary...
1689 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1695 * Otherwise, seek in the file and cleanup any compression buffers...
1701 inflateEnd(&fp
->stream
);
1704 #endif /* HAVE_LIBZ */
1706 if (lseek(fp
->fd
, 0, SEEK_SET
))
1708 DEBUG_printf(("1cupsFileRewind: lseek failed: %s", strerror(errno
)));
1718 DEBUG_printf(("2cupsFileRewind: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1725 * 'cupsFileSeek()' - Seek in a file.
1727 * @since CUPS 1.2/macOS 10.5@
1730 off_t
/* O - New file position or -1 on error */
1731 cupsFileSeek(cups_file_t
*fp
, /* I - CUPS file */
1732 off_t pos
) /* I - Position in file */
1734 ssize_t bytes
; /* Number bytes in buffer */
1737 DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT
")", (void *)fp
, CUPS_LLCAST pos
));
1738 DEBUG_printf(("2cupsFileSeek: fp->pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1739 DEBUG_printf(("2cupsFileSeek: fp->ptr=%p, fp->end=%p", (void *)fp
->ptr
, (void *)fp
->end
));
1742 * Range check input...
1745 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1749 * Handle special cases...
1753 return (cupsFileRewind(fp
));
1757 bytes
= (ssize_t
)(fp
->end
- fp
->buf
);
1759 DEBUG_printf(("2cupsFileSeek: bytes=" CUPS_LLFMT
, CUPS_LLCAST bytes
));
1761 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1764 * No seeking necessary...
1768 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1776 if (!fp
->compressed
&& !fp
->ptr
)
1779 * Preload a buffer to determine whether the file is compressed...
1782 if (cups_fill(fp
) <= 0)
1785 #endif /* HAVE_LIBZ */
1788 * Seek forwards or backwards...
1793 if (pos
< fp
->bufpos
)
1796 * Need to seek backwards...
1799 DEBUG_puts("2cupsFileSeek: SEEK BACKWARDS");
1804 inflateEnd(&fp
->stream
);
1806 lseek(fp
->fd
, 0, SEEK_SET
);
1812 while ((bytes
= cups_fill(fp
)) > 0)
1813 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1819 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1823 #endif /* HAVE_LIBZ */
1825 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1826 fp
->pos
= fp
->bufpos
;
1830 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1831 CUPS_LLCAST fp
->pos
));
1837 * Need to seek forwards...
1840 DEBUG_puts("2cupsFileSeek: SEEK FORWARDS");
1845 while ((bytes
= cups_fill(fp
)) > 0)
1847 if (pos
>= fp
->bufpos
&& pos
< (fp
->bufpos
+ bytes
))
1854 fp
->ptr
= fp
->buf
+ pos
- fp
->bufpos
;
1858 #endif /* HAVE_LIBZ */
1860 fp
->bufpos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1861 fp
->pos
= fp
->bufpos
;
1865 DEBUG_printf(("2cupsFileSeek: lseek() returned " CUPS_LLFMT
,
1866 CUPS_LLCAST fp
->pos
));
1870 DEBUG_printf(("2cupsFileSeek: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
1877 * 'cupsFileStderr()' - Return a CUPS file associated with stderr.
1879 * @since CUPS 1.2/macOS 10.5@
1882 cups_file_t
* /* O - CUPS file */
1883 cupsFileStderr(void)
1885 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1889 * Open file descriptor 2 as needed...
1892 if (!cg
->stdio_files
[2])
1895 * Flush any pending output on the stdio file...
1901 * Open file descriptor 2...
1904 if ((cg
->stdio_files
[2] = cupsFileOpenFd(2, "w")) != NULL
)
1905 cg
->stdio_files
[2]->is_stdio
= 1;
1908 return (cg
->stdio_files
[2]);
1913 * 'cupsFileStdin()' - Return a CUPS file associated with stdin.
1915 * @since CUPS 1.2/macOS 10.5@
1918 cups_file_t
* /* O - CUPS file */
1921 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1925 * Open file descriptor 0 as needed...
1928 if (!cg
->stdio_files
[0])
1931 * Open file descriptor 0...
1934 if ((cg
->stdio_files
[0] = cupsFileOpenFd(0, "r")) != NULL
)
1935 cg
->stdio_files
[0]->is_stdio
= 1;
1938 return (cg
->stdio_files
[0]);
1943 * 'cupsFileStdout()' - Return a CUPS file associated with stdout.
1945 * @since CUPS 1.2/macOS 10.5@
1948 cups_file_t
* /* O - CUPS file */
1949 cupsFileStdout(void)
1951 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals... */
1955 * Open file descriptor 1 as needed...
1958 if (!cg
->stdio_files
[1])
1961 * Flush any pending output on the stdio file...
1967 * Open file descriptor 1...
1970 if ((cg
->stdio_files
[1] = cupsFileOpenFd(1, "w")) != NULL
)
1971 cg
->stdio_files
[1]->is_stdio
= 1;
1974 return (cg
->stdio_files
[1]);
1979 * 'cupsFileTell()' - Return the current file position.
1981 * @since CUPS 1.2/macOS 10.5@
1984 off_t
/* O - File position */
1985 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
1987 DEBUG_printf(("2cupsFileTell(fp=%p)", (void *)fp
));
1988 DEBUG_printf(("3cupsFileTell: pos=" CUPS_LLFMT
, CUPS_LLCAST (fp
? fp
->pos
: -1)));
1990 return (fp
? fp
->pos
: 0);
1995 * 'cupsFileUnlock()' - Unlock access to a file.
1997 * @since CUPS 1.2/macOS 10.5@
2000 int /* O - 0 on success, -1 on error */
2001 cupsFileUnlock(cups_file_t
*fp
) /* I - CUPS file */
2007 DEBUG_printf(("cupsFileUnlock(fp=%p)", (void *)fp
));
2009 if (!fp
|| fp
->mode
== 's')
2017 return (_locking(fp
->fd
, _LK_UNLCK
, 0));
2019 return (lockf(fp
->fd
, F_ULOCK
, 0));
2025 * 'cupsFileWrite()' - Write to a file.
2027 * @since CUPS 1.2/macOS 10.5@
2030 ssize_t
/* O - Number of bytes written or -1 on error */
2031 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
2032 const char *buf
, /* I - Buffer */
2033 size_t bytes
) /* I - Number of bytes to write */
2036 * Range check input...
2039 DEBUG_printf(("2cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2041 if (!fp
|| !buf
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
2048 * Write the buffer...
2051 if (fp
->mode
== 's')
2053 if (cups_write(fp
, buf
, bytes
) < 0)
2056 fp
->pos
+= (off_t
)bytes
;
2058 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2060 return ((ssize_t
)bytes
);
2063 if ((fp
->ptr
+ bytes
) > fp
->end
)
2064 if (cupsFileFlush(fp
))
2067 fp
->pos
+= (off_t
)bytes
;
2069 DEBUG_printf(("4cupsFileWrite: pos=" CUPS_LLFMT
, CUPS_LLCAST fp
->pos
));
2071 if (bytes
> sizeof(fp
->buf
))
2075 return (cups_compress(fp
, buf
, bytes
));
2077 #endif /* HAVE_LIBZ */
2078 return (cups_write(fp
, buf
, bytes
));
2082 memcpy(fp
->ptr
, buf
, bytes
);
2084 return ((ssize_t
)bytes
);
2091 * 'cups_compress()' - Compress a buffer of data.
2094 static ssize_t
/* O - Number of bytes written or -1 */
2095 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
2096 const char *buf
, /* I - Buffer */
2097 size_t bytes
) /* I - Number bytes */
2099 DEBUG_printf(("7cups_compress(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2105 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, (uInt
)bytes
);
2108 * Deflate the bytes...
2111 fp
->stream
.next_in
= (Bytef
*)buf
;
2112 fp
->stream
.avail_in
= (uInt
)bytes
;
2114 while (fp
->stream
.avail_in
> 0)
2117 * Flush the current buffer...
2120 DEBUG_printf(("9cups_compress: avail_in=%d, avail_out=%d",
2121 fp
->stream
.avail_in
, fp
->stream
.avail_out
));
2123 if (fp
->stream
.avail_out
< (uInt
)(sizeof(fp
->cbuf
) / 8))
2125 if (cups_write(fp
, (char *)fp
->cbuf
, (size_t)(fp
->stream
.next_out
- fp
->cbuf
)) < 0)
2128 fp
->stream
.next_out
= fp
->cbuf
;
2129 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
2132 deflate(&(fp
->stream
), Z_NO_FLUSH
);
2135 return ((ssize_t
)bytes
);
2137 #endif /* HAVE_LIBZ */
2141 * 'cups_fill()' - Fill the input buffer.
2144 static ssize_t
/* O - Number of bytes or -1 */
2145 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
2147 ssize_t bytes
; /* Number of bytes read */
2149 int status
; /* Decompression status */
2150 const unsigned char *ptr
, /* Pointer into buffer */
2151 *end
; /* End of buffer */
2152 #endif /* HAVE_LIBZ */
2155 DEBUG_printf(("7cups_fill(fp=%p)", (void *)fp
));
2156 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
));
2158 if (fp
->ptr
&& fp
->end
)
2159 fp
->bufpos
+= fp
->end
- fp
->buf
;
2162 DEBUG_printf(("9cups_fill: fp->compressed=%d", fp
->compressed
));
2164 while (!fp
->ptr
|| fp
->compressed
)
2167 * Check to see if we have read any data yet; if not, see if we have a
2168 * compressed file...
2174 * Reset the file position in case we are seeking...
2180 * Read the first bytes in the file to determine if we have a gzip'd
2184 if ((bytes
= cups_read(fp
, (char *)fp
->buf
, sizeof(fp
->buf
))) < 0)
2187 * Can't read from file!
2190 DEBUG_printf(("9cups_fill: cups_read() returned " CUPS_LLFMT
,
2191 CUPS_LLCAST bytes
));
2198 if (bytes
< 10 || fp
->buf
[0] != 0x1f ||
2199 (fp
->buf
[1] & 255) != 0x8b ||
2200 fp
->buf
[2] != 8 || (fp
->buf
[3] & 0xe0) != 0)
2203 * Not a gzip'd file!
2207 fp
->end
= fp
->buf
+ bytes
;
2209 DEBUG_printf(("9cups_fill: Returning " CUPS_LLFMT
,
2210 CUPS_LLCAST bytes
));
2216 * Parse header junk: extra data, original name, and comment...
2219 ptr
= (unsigned char *)fp
->buf
+ 10;
2220 end
= (unsigned char *)fp
->buf
+ bytes
;
2222 if (fp
->buf
[3] & 0x04)
2225 * Skip extra data...
2228 if ((ptr
+ 2) > end
)
2231 * Can't read from file!
2234 DEBUG_puts("9cups_fill: Extra gzip header data missing, returning -1.");
2242 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
2248 * Can't read from file!
2251 DEBUG_puts("9cups_fill: Extra gzip header data does not fit in initial buffer, returning -1.");
2260 if (fp
->buf
[3] & 0x08)
2263 * Skip original name data...
2266 while (ptr
< end
&& *ptr
)
2274 * Can't read from file!
2277 DEBUG_puts("9cups_fill: Original filename in gzip header data does not fit in initial buffer, returning -1.");
2286 if (fp
->buf
[3] & 0x10)
2289 * Skip comment data...
2292 while (ptr
< end
&& *ptr
)
2300 * Can't read from file!
2303 DEBUG_puts("9cups_fill: Comment in gzip header data does not fit in initial buffer, returning -1.");
2312 if (fp
->buf
[3] & 0x02)
2315 * Skip header CRC data...
2323 * Can't read from file!
2326 DEBUG_puts("9cups_fill: Header CRC in gzip header data does not fit in initial buffer, returning -1.");
2336 * Copy the flate-compressed data to the compression buffer...
2339 if ((bytes
= end
- ptr
) > 0)
2340 memcpy(fp
->cbuf
, ptr
, (size_t)bytes
);
2343 * Setup the decompressor data...
2346 fp
->stream
.zalloc
= (alloc_func
)0;
2347 fp
->stream
.zfree
= (free_func
)0;
2348 fp
->stream
.opaque
= (voidpf
)0;
2349 fp
->stream
.next_in
= (Bytef
*)fp
->cbuf
;
2350 fp
->stream
.next_out
= NULL
;
2351 fp
->stream
.avail_in
= (uInt
)bytes
;
2352 fp
->stream
.avail_out
= 0;
2353 fp
->crc
= crc32(0L, Z_NULL
, 0);
2355 if ((status
= inflateInit2(&(fp
->stream
), -15)) != Z_OK
)
2357 DEBUG_printf(("9cups_fill: inflateInit2 returned %d, returning -1.", status
));
2371 * If we have reached end-of-file, return immediately...
2376 DEBUG_puts("9cups_fill: EOF, returning 0.");
2382 * Fill the decompression buffer as needed...
2385 if (fp
->stream
.avail_in
== 0)
2387 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
2389 DEBUG_printf(("9cups_fill: cups_read error, returning %d.", (int)bytes
));
2396 fp
->stream
.next_in
= fp
->cbuf
;
2397 fp
->stream
.avail_in
= (uInt
)bytes
;
2401 * Decompress data from the buffer...
2404 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
2405 fp
->stream
.avail_out
= sizeof(fp
->buf
);
2407 status
= inflate(&(fp
->stream
), Z_NO_FLUSH
);
2409 if (fp
->stream
.next_out
> (Bytef
*)fp
->buf
)
2410 fp
->crc
= crc32(fp
->crc
, (Bytef
*)fp
->buf
,
2411 (uInt
)(fp
->stream
.next_out
- (Bytef
*)fp
->buf
));
2413 if (status
== Z_STREAM_END
)
2416 * Read the CRC and length...
2419 unsigned char trailer
[8]; /* Trailer bytes */
2420 uLong tcrc
; /* Trailer CRC */
2421 ssize_t tbytes
= 0; /* Number of bytes */
2423 if (fp
->stream
.avail_in
> 0)
2425 if (fp
->stream
.avail_in
> sizeof(trailer
))
2426 tbytes
= (ssize_t
)sizeof(trailer
);
2428 tbytes
= (ssize_t
)fp
->stream
.avail_in
;
2430 memcpy(trailer
, fp
->stream
.next_in
, (size_t)tbytes
);
2431 fp
->stream
.next_in
+= tbytes
;
2432 fp
->stream
.avail_in
-= (size_t)tbytes
;
2435 if (tbytes
< (ssize_t
)sizeof(trailer
))
2437 if (read(fp
->fd
, trailer
+ tbytes
, sizeof(trailer
) - (size_t)tbytes
) < ((ssize_t
)sizeof(trailer
) - tbytes
))
2440 * Can't get it, so mark end-of-file...
2443 DEBUG_puts("9cups_fill: Unable to read gzip CRC trailer, returning -1.");
2452 tcrc
= ((((((uLong
)trailer
[3] << 8) | (uLong
)trailer
[2]) << 8) |
2453 (uLong
)trailer
[1]) << 8) | (uLong
)trailer
[0];
2455 if (tcrc
!= fp
->crc
)
2458 * Bad CRC, mark end-of-file...
2461 DEBUG_printf(("9cups_fill: tcrc=%08x != fp->crc=%08x, returning -1.", (unsigned int)tcrc
, (unsigned int)fp
->crc
));
2470 * Otherwise, reset the compressed flag so that we re-read the
2474 inflateEnd(&fp
->stream
);
2478 else if (status
< Z_OK
)
2480 DEBUG_printf(("9cups_fill: inflate returned %d, returning -1.", status
));
2488 bytes
= (ssize_t
)sizeof(fp
->buf
) - (ssize_t
)fp
->stream
.avail_out
;
2491 * Return the decompressed data...
2495 fp
->end
= fp
->buf
+ bytes
;
2499 DEBUG_printf(("9cups_fill: Returning %d.", (int)bytes
));
2504 #endif /* HAVE_LIBZ */
2507 * Read a buffer's full of data...
2510 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
2513 * Can't read from file!
2523 * Return the bytes we read...
2528 fp
->end
= fp
->buf
+ bytes
;
2531 DEBUG_printf(("9cups_fill: Not gzip, returning %d.", (int)bytes
));
2538 * 'cups_open()' - Safely open a file for writing.
2540 * We don't allow appending to directories or files that are hard-linked or
2544 static int /* O - File descriptor or -1 otherwise */
2545 cups_open(const char *filename
, /* I - Filename */
2546 int mode
) /* I - Open mode */
2548 int fd
; /* File descriptor */
2549 struct stat fileinfo
; /* File information */
2551 struct stat linkinfo
; /* Link information */
2559 if ((fd
= open(filename
, mode
, 0666)) < 0)
2563 * Then verify that the file descriptor doesn't point to a directory or hard-
2567 if (fstat(fd
, &fileinfo
))
2573 if (fileinfo
.st_nlink
!= 1)
2581 if (fileinfo
.st_mode
& _S_IFDIR
)
2583 if (S_ISDIR(fileinfo
.st_mode
))
2593 * Then use lstat to determine whether the filename is a symlink...
2596 if (lstat(filename
, &linkinfo
))
2602 if (S_ISLNK(linkinfo
.st_mode
) ||
2603 fileinfo
.st_dev
!= linkinfo
.st_dev
||
2604 fileinfo
.st_ino
!= linkinfo
.st_ino
||
2606 fileinfo
.st_gen
!= linkinfo
.st_gen
||
2607 #endif /* HAVE_ST_GEN */
2608 fileinfo
.st_nlink
!= linkinfo
.st_nlink
||
2609 fileinfo
.st_mode
!= linkinfo
.st_mode
)
2626 * 'cups_read()' - Read from a file descriptor.
2629 static ssize_t
/* O - Number of bytes read or -1 */
2630 cups_read(cups_file_t
*fp
, /* I - CUPS file */
2631 char *buf
, /* I - Buffer */
2632 size_t bytes
) /* I - Number bytes */
2634 ssize_t total
; /* Total bytes read */
2637 DEBUG_printf(("7cups_read(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2640 * Loop until we read at least 0 bytes...
2646 if (fp
->mode
== 's')
2647 total
= (ssize_t
)recv(fp
->fd
, buf
, (unsigned)bytes
, 0);
2649 total
= (ssize_t
)read(fp
->fd
, buf
, (unsigned)bytes
);
2651 if (fp
->mode
== 's')
2652 total
= recv(fp
->fd
, buf
, bytes
, 0);
2654 total
= read(fp
->fd
, buf
, bytes
);
2657 DEBUG_printf(("9cups_read: total=" CUPS_LLFMT
, CUPS_LLCAST total
));
2663 * Reads can be interrupted by signals and unavailable resources...
2666 if (errno
== EAGAIN
|| errno
== EINTR
)
2673 * Return the total number of bytes read...
2681 * 'cups_write()' - Write to a file descriptor.
2684 static ssize_t
/* O - Number of bytes written or -1 */
2685 cups_write(cups_file_t
*fp
, /* I - CUPS file */
2686 const char *buf
, /* I - Buffer */
2687 size_t bytes
) /* I - Number bytes */
2689 size_t total
; /* Total bytes written */
2690 ssize_t count
; /* Count this time */
2693 DEBUG_printf(("7cups_write(fp=%p, buf=%p, bytes=" CUPS_LLFMT
")", (void *)fp
, (void *)buf
, CUPS_LLCAST bytes
));
2696 * Loop until all bytes are written...
2703 if (fp
->mode
== 's')
2704 count
= (ssize_t
)send(fp
->fd
, buf
, (unsigned)bytes
, 0);
2706 count
= (ssize_t
)write(fp
->fd
, buf
, (unsigned)bytes
);
2708 if (fp
->mode
== 's')
2709 count
= send(fp
->fd
, buf
, bytes
, 0);
2711 count
= write(fp
->fd
, buf
, bytes
);
2714 DEBUG_printf(("9cups_write: count=" CUPS_LLFMT
, CUPS_LLCAST count
));
2719 * Writes can be interrupted by signals and unavailable resources...
2722 if (errno
== EAGAIN
|| errno
== EINTR
)
2729 * Update the counts for the last write call...
2732 bytes
-= (size_t)count
;
2733 total
+= (size_t)count
;
2738 * Return the total number of bytes written...
2741 return ((ssize_t
)total
);