]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/file.c
2 * "$Id: file.c 5026 2006-01-30 21:59:02Z mike $"
4 * File functions for the Common UNIX Printing System (CUPS).
6 * Since stdio files max out at 256 files on many systems, we have to
7 * write similar functions without this limit. At the same time, using
8 * our own file functions allows us to provide transparent support of
9 * gzip'd print files, PPD files, etc.
11 * Copyright 1997-2005 by Easy Software Products, all rights reserved.
13 * These coded instructions, statements, and computer programs are the
14 * property of Easy Software Products and are protected by Federal
15 * copyright law. Distribution and use rights are outlined in the file
16 * "LICENSE.txt" which should have been included with this file. If this
17 * file is missing or damaged please contact Easy Software Products
20 * Attn: CUPS Licensing Information
21 * Easy Software Products
22 * 44141 Airport View Drive, Suite 204
23 * Hollywood, Maryland 20636 USA
25 * Voice: (301) 373-9600
26 * EMail: cups-info@cups.org
27 * WWW: http://www.cups.org
31 * cupsFileClose() - Close a CUPS file.
32 * cupsFileCompression() - Return whether a file is compressed.
33 * cupsFileEOF() - Return the end-of-file status.
34 * cupsFileFind() - Find a file using the specified path.
35 * cupsFileFlush() - Flush pending output.
36 * cupsFileGetChar() - Get a single character from a file.
37 * cupsFileGetConf() - Get a line from a configuration file...
38 * cupsFileGets() - Get a CR and/or LF-terminated line.
39 * cupsFileLock() - Temporarily lock access to a file.
40 * cupsFileNumber() - Return the file descriptor associated with a CUPS file.
41 * cupsFileOpen() - Open a CUPS file.
42 * cupsFileOpenFd() - Open a CUPS file using a file descriptor.
43 * cupsFilePeekChar() - Peek at the next character from a file.
44 * cupsFilePrintf() - Write a formatted string.
45 * cupsFilePutChar() - Write a character.
46 * cupsFilePuts() - Write a string.
47 * cupsFileRead() - Read from a file.
48 * cupsFileRewind() - Rewind a file.
49 * cupsFileSeek() - Seek in a file.
50 * cupsFileTell() - Return the current file position.
51 * cupsFileUnlock() - Unlock access to a file.
52 * cupsFileWrite() - Write to a file.
53 * cups_compress() - Compress a buffer of data...
54 * cups_fill() - Fill the input buffer...
55 * cups_read() - Read from a file descriptor.
56 * cups_write() - Write to a file descriptor.
60 * Include necessary headers...
66 #include "http-private.h"
69 #include <cups/debug.h>
70 #include <sys/types.h>
76 #endif /* HAVE_LIBZ */
79 # include <sys/locking.h>
84 * Some operating systems support large files via open flag O_LARGEFILE...
88 # define O_LARGEFILE 0
89 #endif /* !O_LARGEFILE */
93 * Types and structures...
96 struct _cups_file_s
/**** CUPS file structure... ****/
99 int fd
; /* File descriptor */
100 char mode
, /* Mode ('r' or 'w') */
101 compressed
, /* Compression used? */
102 eof
, /* End of file? */
103 buf
[4096], /* Buffer */
104 *ptr
, /* Pointer into buffer */
105 *end
; /* End of buffer data */
106 off_t pos
; /* File position for start of buffer */
109 z_stream stream
; /* (De)compression stream */
110 Bytef cbuf
[4096]; /* (De)compression buffer */
111 uLong crc
; /* (De)compression CRC */
112 #endif /* HAVE_LIBZ */
121 static ssize_t
cups_compress(cups_file_t
*fp
, const char *buf
, size_t bytes
);
122 #endif /* HAVE_LIBZ */
123 static ssize_t
cups_fill(cups_file_t
*fp
);
124 static ssize_t
cups_read(cups_file_t
*fp
, char *buf
, size_t bytes
);
125 static ssize_t
cups_write(cups_file_t
*fp
, const char *buf
, size_t bytes
);
129 * 'cupsFileClose()' - Close a CUPS file.
132 int /* O - 0 on success, -1 on error */
133 cupsFileClose(cups_file_t
*fp
) /* I - CUPS file */
135 int fd
; /* File descriptor */
136 char mode
; /* Open mode */
137 int status
; /* Return status */
140 DEBUG_printf(("cupsFileClose(fp=%p)\n", fp
));
150 * Flush pending write data...
154 status
= cupsFileFlush(fp
);
159 if (fp
->compressed
&& status
>= 0)
164 * Free decompression data...
167 inflateEnd(&fp
->stream
);
172 * Flush any remaining compressed data...
175 unsigned char trailer
[8]; /* Trailer CRC and length */
176 int done
; /* Done writing... */
179 fp
->stream
.avail_in
= 0;
183 if (fp
->stream
.next_out
> fp
->cbuf
)
185 if (cups_write(fp
, (char *)fp
->cbuf
,
186 fp
->stream
.next_out
- fp
->cbuf
) < 0)
189 fp
->stream
.next_out
= fp
->cbuf
;
190 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
193 if (done
|| status
< 0)
196 done
= deflate(&fp
->stream
, Z_FINISH
) == Z_STREAM_END
&&
197 fp
->stream
.next_out
== fp
->cbuf
;
201 * Write the CRC and length...
204 trailer
[0] = fp
->crc
;
205 trailer
[1] = fp
->crc
>> 8;
206 trailer
[2] = fp
->crc
>> 16;
207 trailer
[3] = fp
->crc
>> 24;
208 trailer
[4] = fp
->pos
;
209 trailer
[5] = fp
->pos
>> 8;
210 trailer
[6] = fp
->pos
>> 16;
211 trailer
[7] = fp
->pos
>> 24;
213 if (cups_write(fp
, (char *)trailer
, 8) < 0)
217 * Free all memory used by the compression stream...
220 deflateEnd(&(fp
->stream
));
223 #endif /* HAVE_LIBZ */
226 * Save the file descriptor we used and free memory...
235 * Close the file, returning the close status...
240 if (closesocket(fd
) < 0)
254 * 'cupsFileCompression()' - Return whether a file is compressed.
257 int /* O - CUPS_FILE_NONE or CUPS_FILE_GZIP */
258 cupsFileCompression(cups_file_t
*fp
) /* I - CUPS file */
260 return (fp
->compressed
);
265 * 'cupsFileEOF()' - Return the end-of-file status.
268 int /* O - 1 on EOF, 0 otherwise */
269 cupsFileEOF(cups_file_t
*fp
) /* I - CUPS file */
276 * 'cupsFileFind()' - Find a file using the specified path.
278 * This function allows the paths in the path string to be separated by
279 * colons (UNIX standard) or semicolons (Windows standard) and stores the
280 * result in the buffer supplied. If the file cannot be found in any of
281 * the supplied paths, NULL is returned. A NULL path only matches the
285 const char * /* O - Full path to file or NULL */
286 cupsFileFind(const char *filename
, /* I - File to find */
287 const char *path
, /* I - Colon/semicolon-separated path */
288 char *buffer
, /* I - Filename buffer */
289 int bufsize
) /* I - Size of filename buffer */
291 char *bufptr
, /* Current position in buffer */
292 *bufend
; /* End of buffer */
296 * Range check input...
299 if (!filename
|| !buffer
|| bufsize
< 2)
305 * No path, so check current directory...
308 if (!access(filename
, 0))
310 strlcpy(buffer
, filename
, bufsize
);
318 * Now check each path and return the first match...
321 bufend
= buffer
+ bufsize
- 1;
326 if (*path
== ';' || *path
== ':')
328 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
331 strlcpy(bufptr
, filename
, bufend
- bufptr
);
333 if (!access(buffer
, 0))
338 else if (bufptr
< bufend
)
345 * Check the last path...
348 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
351 strlcpy(bufptr
, filename
, bufend
- bufptr
);
353 if (!access(buffer
, 0))
361 * 'cupsFileFlush()' - Flush pending output.
364 int /* O - 0 on success, -1 on error */
365 cupsFileFlush(cups_file_t
*fp
) /* I - CUPS file */
367 size_t bytes
; /* Bytes to write */
370 DEBUG_printf(("cupsFileFlush(fp=%p)\n", fp
));
373 * Range check input...
376 if (!fp
|| fp
->mode
!= 'w')
378 DEBUG_puts(" Attempt to flush a read-only file...");
382 bytes
= fp
->ptr
- fp
->buf
;
388 bytes
= cups_compress(fp
, fp
->buf
, bytes
);
390 #endif /* HAVE_LIBZ */
391 bytes
= cups_write(fp
, fp
->buf
, bytes
);
404 * 'cupsFileGetChar()' - Get a single character from a file.
407 int /* O - Character or -1 on EOF */
408 cupsFileGetChar(cups_file_t
*fp
) /* I - CUPS file */
411 * Range check input...
414 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
418 * If the input buffer is empty, try to read more data...
421 if (fp
->ptr
>= fp
->end
)
422 if (cups_fill(fp
) < 0)
426 * Return the next character in the buffer...
429 return (*(fp
->ptr
)++ & 255);
434 * 'cupsFileGetConf()' - Get a line from a configuration file...
437 char * /* O - Line read of NULL on eof/error */
438 cupsFileGetConf(cups_file_t
*fp
, /* I - CUPS file */
439 char *buf
, /* O - String buffer */
440 size_t buflen
, /* I - Size of string buffer */
441 char **value
, /* O - Pointer to value */
442 int *linenum
) /* IO - Current line number */
444 char *ptr
; /* Pointer into line */
448 * Range check input...
451 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') ||
452 !buf
|| buflen
< 2 || !value
)
461 * Read the next non-comment line...
466 while (cupsFileGets(fp
, buf
, buflen
))
471 * Strip any comments...
474 if ((ptr
= strchr(buf
, '#')) != NULL
)
478 if (!isspace(ptr
[-1] & 255))
488 * Strip leading whitespace...
491 for (ptr
= buf
; isspace(*ptr
& 255); ptr
++);
494 _cups_strcpy(buf
, ptr
);
497 * See if there is anything left...
503 * Yes, grab any value and return...
506 for (ptr
= buf
; *ptr
; ptr
++)
507 if (isspace(*ptr
& 255))
513 * Have a value, skip any other spaces...
516 while (isspace(*ptr
& 255))
523 * Strip trailing whitespace and > for lines that begin with <...
526 ptr
+= strlen(ptr
) - 1;
528 if (buf
[0] == '<' && *ptr
== '>')
530 else if (buf
[0] == '<' && *ptr
!= '>')
540 while (ptr
> *value
&& isspace(*ptr
& 255))
557 * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
560 char * /* O - Line read or NULL on eof/error */
561 cupsFileGets(cups_file_t
*fp
, /* I - CUPS file */
562 char *buf
, /* O - String buffer */
563 size_t buflen
) /* I - Size of string buffer */
565 int ch
; /* Character from file */
566 char *ptr
, /* Current position in line buffer */
567 *end
; /* End of line buffer */
571 * Range check input...
574 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 2)
578 * Now loop until we have a valid line...
581 for (ptr
= buf
, end
= buf
+ buflen
- 1; ptr
< end
;)
583 if (fp
->ptr
>= fp
->end
)
584 if (cups_fill(fp
) <= 0)
600 if (fp
->ptr
>= fp
->end
)
601 if (cups_fill(fp
) <= 0)
604 if (*(fp
->ptr
) == '\n')
612 * Line feed ends a line...
628 * 'cupsFileLock()' - Temporarily lock access to a file.
631 int /* O - 0 on success, -1 on error */
632 cupsFileLock(cups_file_t
*fp
, /* I - File to lock */
633 int block
) /* I - 1 to wait for the lock, 0 to fail right away */
639 if (!fp
|| fp
->mode
== 's')
647 return (locking(fp
->fd
, block
? _LK_LOCK
: _LK_NBLCK
, 0));
649 return (lockf(fp
->fd
, block
? F_LOCK
: F_TLOCK
, 0));
655 * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
658 int /* O - File descriptor */
659 cupsFileNumber(cups_file_t
*fp
) /* I - CUPS file */
666 * 'cupsFileOpen()' - Open a CUPS file.
669 cups_file_t
* /* O - CUPS file or NULL */
670 cupsFileOpen(const char *filename
, /* I - Name of file */
671 const char *mode
) /* I - Open mode */
673 cups_file_t
*fp
; /* New CUPS file */
674 int fd
; /* File descriptor */
675 char hostname
[1024], /* Hostname */
676 *portname
; /* Port "name" (number or service) */
677 http_addrlist_t
*addrlist
; /* Host address list */
681 * Range check input...
684 if (!filename
|| !mode
||
685 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's'))
694 case 'a' : /* Append file */
695 fd
= open(filename
, O_RDWR
| O_CREAT
| O_APPEND
| O_LARGEFILE
, 0666);
698 case 'r' : /* Read file */
699 fd
= open(filename
, O_RDONLY
| O_LARGEFILE
, 0);
702 case 'w' : /* Write file */
703 fd
= open(filename
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_LARGEFILE
, 0666);
706 case 's' : /* Read/write socket */
707 strlcpy(hostname
, filename
, sizeof(hostname
));
708 if ((portname
= strrchr(hostname
, ':')) != NULL
)
714 * Lookup the hostname and service...
717 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
721 * Connect to the server...
724 if (!httpAddrConnect(addrlist
, &fd
))
726 httpAddrFreeList(addrlist
);
730 httpAddrFreeList(addrlist
);
733 default : /* Remove bogus compiler warning... */
741 * Create the CUPS file structure...
744 if ((fp
= cupsFileOpenFd(fd
, mode
)) == NULL
)
760 * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
763 cups_file_t
* /* O - CUPS file or NULL */
764 cupsFileOpenFd(int fd
, /* I - File descriptor */
765 const char *mode
) /* I - Open mode */
767 cups_file_t
*fp
; /* New CUPS file */
771 * Range check input...
774 if (fd
< 0 || !mode
||
775 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's'))
782 if ((fp
= calloc(1, sizeof(cups_file_t
))) == NULL
)
797 fp
->end
= fp
->buf
+ sizeof(fp
->buf
);
800 if (mode
[1] >= '1' && mode
[1] <= '9')
803 * Open a compressed stream, so write the standard gzip file
807 unsigned char header
[10]; /* gzip file header */
808 time_t curtime
; /* Current time */
811 curtime
= time(NULL
);
814 header
[2] = Z_DEFLATED
;
817 header
[5] = curtime
>> 8;
818 header
[6] = curtime
>> 16;
819 header
[7] = curtime
>> 24;
823 cups_write(fp
, (char *)header
, 10);
826 * Initialize the compressor...
829 deflateInit2(&(fp
->stream
), mode
[1] - '0', Z_DEFLATED
, -15, 8,
832 fp
->stream
.next_out
= fp
->cbuf
;
833 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
835 fp
->crc
= crc32(0L, Z_NULL
, 0);
837 #endif /* HAVE_LIBZ */
848 default : /* Remove bogus compiler warning... */
853 * Don't pass this file to child processes...
857 fcntl(fp
->fd
, F_SETFD
, fcntl(fp
->fd
, F_GETFD
) | FD_CLOEXEC
);
865 * 'cupsFilePeekChar()' - Peek at the next character from a file.
868 int /* O - Character or -1 on EOF */
869 cupsFilePeekChar(cups_file_t
*fp
) /* I - CUPS file */
872 * Range check input...
875 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
879 * If the input buffer is empty, try to read more data...
882 if (fp
->ptr
>= fp
->end
)
883 if (cups_fill(fp
) < 0)
887 * Return the next character in the buffer...
890 return (*(fp
->ptr
) & 255);
895 * 'cupsFilePrintf()' - Write a formatted string.
898 int /* O - Number of bytes written or -1 */
899 cupsFilePrintf(cups_file_t
*fp
, /* I - CUPS file */
900 const char *format
, /* I - Printf-style format string */
901 ...) /* I - Additional args as necessary */
903 va_list ap
; /* Argument list */
904 size_t bytes
; /* Formatted size */
905 char buf
[2048]; /* Formatted text */
908 if (!fp
|| !format
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
911 va_start(ap
, format
);
912 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
916 return (cups_write(fp
, buf
, bytes
));
918 if ((fp
->ptr
+ bytes
) > fp
->end
)
919 if (cupsFileFlush(fp
))
924 if (bytes
> sizeof(fp
->buf
))
928 return (cups_compress(fp
, buf
, bytes
));
930 #endif /* HAVE_LIBZ */
931 return (cups_write(fp
, buf
, bytes
));
935 memcpy(fp
->ptr
, buf
, bytes
);
943 * 'cupsFilePutChar()' - Write a character.
946 int /* O - 0 on success, -1 on error */
947 cupsFilePutChar(cups_file_t
*fp
, /* I - CUPS file */
948 int c
) /* I - Character to write */
951 * Range check input...
954 if (!fp
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
960 * Send character immediately over socket...
963 char ch
; /* Output character */
968 if (send(fp
->fd
, &ch
, 1, 0) < 1)
977 if (fp
->ptr
>= fp
->end
)
978 if (cupsFileFlush(fp
))
991 * 'cupsFilePuts()' - Write a string.
994 int /* O - Number of bytes written or -1 */
995 cupsFilePuts(cups_file_t
*fp
, /* I - CUPS file */
996 const char *s
) /* I - String to write */
998 size_t bytes
; /* Bytes to write */
1002 * Range check input...
1005 if (!fp
|| !s
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1009 * Write the string...
1014 if (fp
->mode
== 's')
1016 if (cups_write(fp
, s
, bytes
) < 0)
1024 if ((fp
->ptr
+ bytes
) > fp
->end
)
1025 if (cupsFileFlush(fp
))
1030 if (bytes
> sizeof(fp
->buf
))
1034 return (cups_compress(fp
, s
, bytes
));
1036 #endif /* HAVE_LIBZ */
1037 return (cups_write(fp
, s
, bytes
));
1041 memcpy(fp
->ptr
, s
, bytes
);
1049 * 'cupsFileRead()' - Read from a file.
1052 ssize_t
/* O - Number of bytes read or -1 */
1053 cupsFileRead(cups_file_t
*fp
, /* I - CUPS file */
1054 char *buf
, /* O - Buffer */
1055 size_t bytes
) /* I - Number of bytes to read */
1057 size_t total
, /* Total bytes read */
1058 count
; /* Bytes read */
1061 DEBUG_printf(("cupsFileRead(fp=%p, buf=%p, bytes=%ld)\n", fp
, buf
,
1065 * Range check input...
1068 if (!fp
|| !buf
|| bytes
< 0 || (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1075 * Loop until all bytes are read...
1081 if (fp
->ptr
>= fp
->end
)
1082 if (cups_fill(fp
) <= 0)
1084 DEBUG_printf((" cups_fill() returned -1, total=%d\n", total
));
1092 count
= fp
->end
- fp
->ptr
;
1096 memcpy(buf
, fp
->ptr
, count
);
1100 * Update the counts for the last read...
1109 * Return the total number of bytes read...
1112 DEBUG_printf((" total=%d\n", total
));
1119 * 'cupsFileRewind()' - Rewind a file.
1122 off_t
/* O - New file position or -1 */
1123 cupsFileRewind(cups_file_t
*fp
) /* I - CUPS file */
1125 return (cupsFileSeek(fp
, 0L));
1130 * 'cupsFileSeek()' - Seek in a file.
1133 off_t
/* O - New file position or -1 */
1134 cupsFileSeek(cups_file_t
*fp
, /* I - CUPS file */
1135 off_t pos
) /* I - Position in file */
1137 size_t bytes
; /* Number bytes in buffer */
1140 DEBUG_printf(("cupsFileSeek(fp=%p, pos=%ld)\n", fp
, (long)pos
));
1141 DEBUG_printf((" fp->pos=%ld\n", (long)fp
->pos
));
1144 * Range check input...
1147 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1151 * Figure out the number of bytes in the current buffer, and then
1152 * see if we are outside of it...
1155 bytes
= fp
->end
- fp
->buf
;
1161 * Need to seek backwards...
1167 inflateEnd(&fp
->stream
);
1169 lseek(fp
->fd
, 0, SEEK_SET
);
1174 while ((bytes
= cups_fill(fp
)) > 0)
1175 if (pos
>= fp
->pos
&& pos
< (fp
->pos
+ bytes
))
1182 #endif /* HAVE_LIBZ */
1184 fp
->pos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1185 DEBUG_printf((" lseek() returned %ld...\n", (long)fp
->pos
));
1190 else if (pos
>= (fp
->pos
+ bytes
))
1193 * Need to seek forwards...
1199 while ((bytes
= cups_fill(fp
)) > 0)
1200 if (pos
>= fp
->pos
&& pos
< (fp
->pos
+ bytes
))
1207 #endif /* HAVE_LIBZ */
1209 fp
->pos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1210 DEBUG_printf((" lseek() returned %ld...\n", (long)fp
->pos
));
1218 * Just reposition the current pointer, since we have the right
1222 fp
->ptr
= fp
->buf
+ pos
- fp
->pos
;
1223 DEBUG_puts((" seek inside buffer..."));
1231 * 'cupsFileTell()' - Return the current file position.
1234 off_t
/* O - File position */
1235 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
1242 * 'cupsFileUnlock()' - Unlock access to a file.
1245 int /* O - 0 on success, -1 on error */
1246 cupsFileUnlock(cups_file_t
*fp
) /* I - File to lock */
1252 if (!fp
|| fp
->mode
== 's')
1260 return (locking(fp
->fd
, _LK_UNLCK
, 0));
1262 return (lockf(fp
->fd
, F_ULOCK
, 0));
1268 * 'cupsFileWrite()' - Write to a file.
1271 ssize_t
/* O - Number of bytes written */
1272 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
1273 const char *buf
, /* I - Buffer */
1274 size_t bytes
) /* I - Number of bytes to write */
1277 * Range check input...
1280 if (!fp
|| !buf
|| bytes
< 0 || (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1287 * Write the buffer...
1290 if (fp
->mode
== 's')
1292 if (cups_write(fp
, buf
, bytes
) < 0)
1300 if ((fp
->ptr
+ bytes
) > fp
->end
)
1301 if (cupsFileFlush(fp
))
1306 if (bytes
> sizeof(fp
->buf
))
1310 return (cups_compress(fp
, buf
, bytes
));
1312 #endif /* HAVE_LIBZ */
1313 return (cups_write(fp
, buf
, bytes
));
1317 memcpy(fp
->ptr
, buf
, bytes
);
1326 * 'cups_compress()' - Compress a buffer of data...
1329 static ssize_t
/* O - Number of bytes written or -1 */
1330 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
1331 const char *buf
, /* I - Buffer */
1332 size_t bytes
) /* I - Number bytes */
1338 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, bytes
);
1341 * Deflate the bytes...
1344 fp
->stream
.next_in
= (Bytef
*)buf
;
1345 fp
->stream
.avail_in
= bytes
;
1347 while (fp
->stream
.avail_in
> 0)
1350 * Flush the current buffer...
1353 if (fp
->stream
.avail_out
< (int)(sizeof(fp
->cbuf
) / 8))
1355 if (cups_write(fp
, (char *)fp
->cbuf
, fp
->stream
.next_out
- fp
->cbuf
) < 0)
1359 deflate(&(fp
->stream
), Z_NO_FLUSH
);
1364 #endif /* HAVE_LIBZ */
1368 * 'cups_fill()' - Fill the input buffer...
1371 static ssize_t
/* O - Number of bytes or -1 */
1372 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
1374 ssize_t bytes
; /* Number of bytes read */
1376 const unsigned char *ptr
, /* Pointer into buffer */
1377 *end
; /* End of buffer */
1378 #endif /* HAVE_LIBZ */
1381 DEBUG_printf(("cups_fill(fp=%p)\n", fp
));
1382 DEBUG_printf((" fp->ptr=%p, fp->end=%p, fp->buf=%p, fp->pos=%ld\n",
1383 fp
->ptr
, fp
->end
, fp
->buf
, (long)fp
->pos
));
1386 * Update the "pos" element as needed...
1389 if (fp
->ptr
&& fp
->end
)
1390 fp
->pos
+= fp
->end
- fp
->buf
;
1393 while (!fp
->ptr
|| fp
->compressed
)
1396 * Check to see if we have read any data yet; if not, see if we have a
1397 * compressed file...
1403 * Reset the file position in case we are seeking...
1410 * Read the first bytes in the file to determine if we have a gzip'd
1414 if ((bytes
= cups_read(fp
, (char *)fp
->buf
, sizeof(fp
->buf
))) < 0)
1417 * Can't read from file!
1423 if (bytes
< 10 || fp
->buf
[0] != 0x1f ||
1424 (fp
->buf
[1] & 255) != 0x8b ||
1425 fp
->buf
[2] != 8 || (fp
->buf
[3] & 0xe0) != 0)
1428 * Not a gzip'd file!
1432 fp
->end
= fp
->buf
+ bytes
;
1438 * Parse header junk: extra data, original name, and comment...
1441 ptr
= (unsigned char *)fp
->buf
+ 10;
1442 end
= (unsigned char *)fp
->buf
+ bytes
;
1444 if (fp
->buf
[3] & 0x04)
1447 * Skip extra data...
1450 if ((ptr
+ 2) > end
)
1453 * Can't read from file!
1459 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
1465 * Can't read from file!
1472 if (fp
->buf
[3] & 0x08)
1475 * Skip original name data...
1478 while (ptr
< end
&& *ptr
)
1486 * Can't read from file!
1493 if (fp
->buf
[3] & 0x10)
1496 * Skip comment data...
1499 while (ptr
< end
&& *ptr
)
1507 * Can't read from file!
1514 if (fp
->buf
[3] & 0x02)
1517 * Skip header CRC data...
1525 * Can't read from file!
1533 * Copy the flate-compressed data to the compression buffer...
1536 if ((bytes
= end
- ptr
) > 0)
1537 memcpy(fp
->cbuf
, ptr
, bytes
);
1540 * Setup the decompressor data...
1543 fp
->stream
.zalloc
= (alloc_func
)0;
1544 fp
->stream
.zfree
= (free_func
)0;
1545 fp
->stream
.opaque
= (voidpf
)0;
1546 fp
->stream
.next_in
= (Bytef
*)fp
->cbuf
;
1547 fp
->stream
.next_out
= NULL
;
1548 fp
->stream
.avail_in
= bytes
;
1549 fp
->stream
.avail_out
= 0;
1550 fp
->crc
= crc32(0L, Z_NULL
, 0);
1552 if (inflateInit2(&(fp
->stream
), -15) != Z_OK
)
1561 * If we have reached end-of-file, return immediately...
1568 * Fill the decompression buffer as needed...
1571 if (fp
->stream
.avail_in
== 0)
1573 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
1576 fp
->stream
.next_in
= fp
->cbuf
;
1577 fp
->stream
.avail_in
= bytes
;
1581 * Decompress data from the buffer...
1584 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
1585 fp
->stream
.avail_out
= sizeof(fp
->buf
);
1587 if (inflate(&(fp
->stream
), Z_NO_FLUSH
) == Z_STREAM_END
)
1590 * Read the CRC and length...
1593 unsigned char trailer
[8]; /* Trailer bytes */
1594 uLong tcrc
; /* Trailer CRC */
1597 if (read(fp
->fd
, trailer
, sizeof(trailer
)) < sizeof(trailer
))
1600 * Can't get it, so mark end-of-file...
1607 tcrc
= (((((trailer
[3] << 8) | trailer
[2]) << 8) | trailer
[1]) << 8) |
1610 if (tcrc
!= fp
->crc
)
1613 * Bad CRC, mark end-of-file...
1622 * Otherwise, reset the compressed flag so that we re-read the
1630 bytes
= sizeof(fp
->buf
) - fp
->stream
.avail_out
;
1633 * Return the decompressed data...
1637 fp
->end
= fp
->buf
+ bytes
;
1643 #endif /* HAVE_LIBZ */
1646 * Read a buffer's full of data...
1649 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
1652 * Can't read from file!
1663 * Return the bytes we read...
1668 fp
->end
= fp
->buf
+ bytes
;
1675 * 'cups_read()' - Read from a file descriptor.
1678 static ssize_t
/* O - Number of bytes read or -1 */
1679 cups_read(cups_file_t
*fp
, /* I - CUPS file */
1680 char *buf
, /* I - Buffer */
1681 size_t bytes
) /* I - Number bytes */
1683 ssize_t total
; /* Total bytes read */
1687 * Loop until we read at least 0 bytes...
1692 if (fp
->mode
== 's')
1693 total
= recv(fp
->fd
, buf
, bytes
, 0);
1695 total
= read(fp
->fd
, buf
, bytes
);
1701 * Reads can be interrupted by signals and unavailable resources...
1704 if (errno
== EAGAIN
|| errno
== EINTR
)
1711 * Return the total number of bytes read...
1719 * 'cups_write()' - Write to a file descriptor.
1722 static ssize_t
/* O - Number of bytes written or -1 */
1723 cups_write(cups_file_t
*fp
, /* I - CUPS file */
1724 const char *buf
, /* I - Buffer */
1725 size_t bytes
) /* I - Number bytes */
1727 size_t total
, /* Total bytes written */
1728 count
; /* Count this time */
1732 * Loop until all bytes are written...
1738 if (fp
->mode
== 's')
1739 count
= send(fp
->fd
, buf
, bytes
, 0);
1741 count
= write(fp
->fd
, buf
, bytes
);
1746 * Writes can be interrupted by signals and unavailable resources...
1749 if (errno
== EAGAIN
|| errno
== EINTR
)
1756 * Update the counts for the last write call...
1765 * Return the total number of bytes written...
1773 * End of "$Id: file.c 5026 2006-01-30 21:59:02Z mike $".