]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/file.c
2 * "$Id: file.c 5057 2006-02-02 20:38:29Z 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 int executable
, /* I - 1 = executable files, 0 = any file/dir */
289 char *buffer
, /* I - Filename buffer */
290 int bufsize
) /* I - Size of filename buffer */
292 char *bufptr
, /* Current position in buffer */
293 *bufend
; /* End of buffer */
297 * Range check input...
300 if (!filename
|| !buffer
|| bufsize
< 2)
306 * No path, so check current directory...
309 if (!access(filename
, 0))
311 strlcpy(buffer
, filename
, bufsize
);
319 * Now check each path and return the first match...
322 bufend
= buffer
+ bufsize
- 1;
327 if (*path
== ';' || *path
== ':')
329 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
332 strlcpy(bufptr
, filename
, bufend
- bufptr
);
335 if (!access(buffer
, 0))
337 if (!access(buffer
, executable
? X_OK
: 0))
343 else if (bufptr
< bufend
)
350 * Check the last path...
353 if (bufptr
> buffer
&& bufptr
[-1] != '/' && bufptr
< bufend
)
356 strlcpy(bufptr
, filename
, bufend
- bufptr
);
358 if (!access(buffer
, 0))
366 * 'cupsFileFlush()' - Flush pending output.
369 int /* O - 0 on success, -1 on error */
370 cupsFileFlush(cups_file_t
*fp
) /* I - CUPS file */
372 size_t bytes
; /* Bytes to write */
375 DEBUG_printf(("cupsFileFlush(fp=%p)\n", fp
));
378 * Range check input...
381 if (!fp
|| fp
->mode
!= 'w')
383 DEBUG_puts(" Attempt to flush a read-only file...");
387 bytes
= fp
->ptr
- fp
->buf
;
393 bytes
= cups_compress(fp
, fp
->buf
, bytes
);
395 #endif /* HAVE_LIBZ */
396 bytes
= cups_write(fp
, fp
->buf
, bytes
);
409 * 'cupsFileGetChar()' - Get a single character from a file.
412 int /* O - Character or -1 on EOF */
413 cupsFileGetChar(cups_file_t
*fp
) /* I - CUPS file */
416 * Range check input...
419 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
423 * If the input buffer is empty, try to read more data...
426 if (fp
->ptr
>= fp
->end
)
427 if (cups_fill(fp
) < 0)
431 * Return the next character in the buffer...
434 return (*(fp
->ptr
)++ & 255);
439 * 'cupsFileGetConf()' - Get a line from a configuration file...
442 char * /* O - Line read of NULL on eof/error */
443 cupsFileGetConf(cups_file_t
*fp
, /* I - CUPS file */
444 char *buf
, /* O - String buffer */
445 size_t buflen
, /* I - Size of string buffer */
446 char **value
, /* O - Pointer to value */
447 int *linenum
) /* IO - Current line number */
449 char *ptr
; /* Pointer into line */
453 * Range check input...
456 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') ||
457 !buf
|| buflen
< 2 || !value
)
466 * Read the next non-comment line...
471 while (cupsFileGets(fp
, buf
, buflen
))
476 * Strip any comments...
479 if ((ptr
= strchr(buf
, '#')) != NULL
)
483 if (!isspace(ptr
[-1] & 255))
493 * Strip leading whitespace...
496 for (ptr
= buf
; isspace(*ptr
& 255); ptr
++);
499 _cups_strcpy(buf
, ptr
);
502 * See if there is anything left...
508 * Yes, grab any value and return...
511 for (ptr
= buf
; *ptr
; ptr
++)
512 if (isspace(*ptr
& 255))
518 * Have a value, skip any other spaces...
521 while (isspace(*ptr
& 255))
528 * Strip trailing whitespace and > for lines that begin with <...
531 ptr
+= strlen(ptr
) - 1;
533 if (buf
[0] == '<' && *ptr
== '>')
535 else if (buf
[0] == '<' && *ptr
!= '>')
545 while (ptr
> *value
&& isspace(*ptr
& 255))
562 * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
565 char * /* O - Line read or NULL on eof/error */
566 cupsFileGets(cups_file_t
*fp
, /* I - CUPS file */
567 char *buf
, /* O - String buffer */
568 size_t buflen
) /* I - Size of string buffer */
570 int ch
; /* Character from file */
571 char *ptr
, /* Current position in line buffer */
572 *end
; /* End of line buffer */
576 * Range check input...
579 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 2)
583 * Now loop until we have a valid line...
586 for (ptr
= buf
, end
= buf
+ buflen
- 1; ptr
< end
;)
588 if (fp
->ptr
>= fp
->end
)
589 if (cups_fill(fp
) <= 0)
605 if (fp
->ptr
>= fp
->end
)
606 if (cups_fill(fp
) <= 0)
609 if (*(fp
->ptr
) == '\n')
617 * Line feed ends a line...
633 * 'cupsFileLock()' - Temporarily lock access to a file.
636 int /* O - 0 on success, -1 on error */
637 cupsFileLock(cups_file_t
*fp
, /* I - File to lock */
638 int block
) /* I - 1 to wait for the lock, 0 to fail right away */
644 if (!fp
|| fp
->mode
== 's')
652 return (locking(fp
->fd
, block
? _LK_LOCK
: _LK_NBLCK
, 0));
654 return (lockf(fp
->fd
, block
? F_LOCK
: F_TLOCK
, 0));
660 * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
663 int /* O - File descriptor */
664 cupsFileNumber(cups_file_t
*fp
) /* I - CUPS file */
671 * 'cupsFileOpen()' - Open a CUPS file.
674 cups_file_t
* /* O - CUPS file or NULL */
675 cupsFileOpen(const char *filename
, /* I - Name of file */
676 const char *mode
) /* I - Open mode */
678 cups_file_t
*fp
; /* New CUPS file */
679 int fd
; /* File descriptor */
680 char hostname
[1024], /* Hostname */
681 *portname
; /* Port "name" (number or service) */
682 http_addrlist_t
*addrlist
; /* Host address list */
686 * Range check input...
689 if (!filename
|| !mode
||
690 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's'))
699 case 'a' : /* Append file */
700 fd
= open(filename
, O_RDWR
| O_CREAT
| O_APPEND
| O_LARGEFILE
, 0666);
703 case 'r' : /* Read file */
704 fd
= open(filename
, O_RDONLY
| O_LARGEFILE
, 0);
707 case 'w' : /* Write file */
708 fd
= open(filename
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_LARGEFILE
, 0666);
711 case 's' : /* Read/write socket */
712 strlcpy(hostname
, filename
, sizeof(hostname
));
713 if ((portname
= strrchr(hostname
, ':')) != NULL
)
719 * Lookup the hostname and service...
722 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
726 * Connect to the server...
729 if (!httpAddrConnect(addrlist
, &fd
))
731 httpAddrFreeList(addrlist
);
735 httpAddrFreeList(addrlist
);
738 default : /* Remove bogus compiler warning... */
746 * Create the CUPS file structure...
749 if ((fp
= cupsFileOpenFd(fd
, mode
)) == NULL
)
765 * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
768 cups_file_t
* /* O - CUPS file or NULL */
769 cupsFileOpenFd(int fd
, /* I - File descriptor */
770 const char *mode
) /* I - Open mode */
772 cups_file_t
*fp
; /* New CUPS file */
776 * Range check input...
779 if (fd
< 0 || !mode
||
780 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's'))
787 if ((fp
= calloc(1, sizeof(cups_file_t
))) == NULL
)
802 fp
->end
= fp
->buf
+ sizeof(fp
->buf
);
805 if (mode
[1] >= '1' && mode
[1] <= '9')
808 * Open a compressed stream, so write the standard gzip file
812 unsigned char header
[10]; /* gzip file header */
813 time_t curtime
; /* Current time */
816 curtime
= time(NULL
);
819 header
[2] = Z_DEFLATED
;
822 header
[5] = curtime
>> 8;
823 header
[6] = curtime
>> 16;
824 header
[7] = curtime
>> 24;
828 cups_write(fp
, (char *)header
, 10);
831 * Initialize the compressor...
834 deflateInit2(&(fp
->stream
), mode
[1] - '0', Z_DEFLATED
, -15, 8,
837 fp
->stream
.next_out
= fp
->cbuf
;
838 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
840 fp
->crc
= crc32(0L, Z_NULL
, 0);
842 #endif /* HAVE_LIBZ */
853 default : /* Remove bogus compiler warning... */
858 * Don't pass this file to child processes...
862 fcntl(fp
->fd
, F_SETFD
, fcntl(fp
->fd
, F_GETFD
) | FD_CLOEXEC
);
870 * 'cupsFilePeekChar()' - Peek at the next character from a file.
873 int /* O - Character or -1 on EOF */
874 cupsFilePeekChar(cups_file_t
*fp
) /* I - CUPS file */
877 * Range check input...
880 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
884 * If the input buffer is empty, try to read more data...
887 if (fp
->ptr
>= fp
->end
)
888 if (cups_fill(fp
) < 0)
892 * Return the next character in the buffer...
895 return (*(fp
->ptr
) & 255);
900 * 'cupsFilePrintf()' - Write a formatted string.
903 int /* O - Number of bytes written or -1 */
904 cupsFilePrintf(cups_file_t
*fp
, /* I - CUPS file */
905 const char *format
, /* I - Printf-style format string */
906 ...) /* I - Additional args as necessary */
908 va_list ap
; /* Argument list */
909 size_t bytes
; /* Formatted size */
910 char buf
[2048]; /* Formatted text */
913 if (!fp
|| !format
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
916 va_start(ap
, format
);
917 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
921 return (cups_write(fp
, buf
, bytes
));
923 if ((fp
->ptr
+ bytes
) > fp
->end
)
924 if (cupsFileFlush(fp
))
929 if (bytes
> sizeof(fp
->buf
))
933 return (cups_compress(fp
, buf
, bytes
));
935 #endif /* HAVE_LIBZ */
936 return (cups_write(fp
, buf
, bytes
));
940 memcpy(fp
->ptr
, buf
, bytes
);
948 * 'cupsFilePutChar()' - Write a character.
951 int /* O - 0 on success, -1 on error */
952 cupsFilePutChar(cups_file_t
*fp
, /* I - CUPS file */
953 int c
) /* I - Character to write */
956 * Range check input...
959 if (!fp
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
965 * Send character immediately over socket...
968 char ch
; /* Output character */
973 if (send(fp
->fd
, &ch
, 1, 0) < 1)
982 if (fp
->ptr
>= fp
->end
)
983 if (cupsFileFlush(fp
))
996 * 'cupsFilePuts()' - Write a string.
999 int /* O - Number of bytes written or -1 */
1000 cupsFilePuts(cups_file_t
*fp
, /* I - CUPS file */
1001 const char *s
) /* I - String to write */
1003 size_t bytes
; /* Bytes to write */
1007 * Range check input...
1010 if (!fp
|| !s
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1014 * Write the string...
1019 if (fp
->mode
== 's')
1021 if (cups_write(fp
, s
, bytes
) < 0)
1029 if ((fp
->ptr
+ bytes
) > fp
->end
)
1030 if (cupsFileFlush(fp
))
1035 if (bytes
> sizeof(fp
->buf
))
1039 return (cups_compress(fp
, s
, bytes
));
1041 #endif /* HAVE_LIBZ */
1042 return (cups_write(fp
, s
, bytes
));
1046 memcpy(fp
->ptr
, s
, bytes
);
1054 * 'cupsFileRead()' - Read from a file.
1057 ssize_t
/* O - Number of bytes read or -1 */
1058 cupsFileRead(cups_file_t
*fp
, /* I - CUPS file */
1059 char *buf
, /* O - Buffer */
1060 size_t bytes
) /* I - Number of bytes to read */
1062 size_t total
, /* Total bytes read */
1063 count
; /* Bytes read */
1066 DEBUG_printf(("cupsFileRead(fp=%p, buf=%p, bytes=%ld)\n", fp
, buf
,
1070 * Range check input...
1073 if (!fp
|| !buf
|| bytes
< 0 || (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1080 * Loop until all bytes are read...
1086 if (fp
->ptr
>= fp
->end
)
1087 if (cups_fill(fp
) <= 0)
1089 DEBUG_printf((" cups_fill() returned -1, total=%d\n", total
));
1097 count
= fp
->end
- fp
->ptr
;
1101 memcpy(buf
, fp
->ptr
, count
);
1105 * Update the counts for the last read...
1114 * Return the total number of bytes read...
1117 DEBUG_printf((" total=%d\n", total
));
1124 * 'cupsFileRewind()' - Rewind a file.
1127 off_t
/* O - New file position or -1 */
1128 cupsFileRewind(cups_file_t
*fp
) /* I - CUPS file */
1130 return (cupsFileSeek(fp
, 0L));
1135 * 'cupsFileSeek()' - Seek in a file.
1138 off_t
/* O - New file position or -1 */
1139 cupsFileSeek(cups_file_t
*fp
, /* I - CUPS file */
1140 off_t pos
) /* I - Position in file */
1142 size_t bytes
; /* Number bytes in buffer */
1145 DEBUG_printf(("cupsFileSeek(fp=%p, pos=%ld)\n", fp
, (long)pos
));
1146 DEBUG_printf((" fp->pos=%ld\n", (long)fp
->pos
));
1149 * Range check input...
1152 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1156 * Figure out the number of bytes in the current buffer, and then
1157 * see if we are outside of it...
1160 bytes
= fp
->end
- fp
->buf
;
1166 * Need to seek backwards...
1172 inflateEnd(&fp
->stream
);
1174 lseek(fp
->fd
, 0, SEEK_SET
);
1179 while ((bytes
= cups_fill(fp
)) > 0)
1180 if (pos
>= fp
->pos
&& pos
< (fp
->pos
+ bytes
))
1187 #endif /* HAVE_LIBZ */
1189 fp
->pos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1190 DEBUG_printf((" lseek() returned %ld...\n", (long)fp
->pos
));
1195 else if (pos
>= (fp
->pos
+ bytes
))
1198 * Need to seek forwards...
1204 while ((bytes
= cups_fill(fp
)) > 0)
1205 if (pos
>= fp
->pos
&& pos
< (fp
->pos
+ bytes
))
1212 #endif /* HAVE_LIBZ */
1214 fp
->pos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1215 DEBUG_printf((" lseek() returned %ld...\n", (long)fp
->pos
));
1223 * Just reposition the current pointer, since we have the right
1227 fp
->ptr
= fp
->buf
+ pos
- fp
->pos
;
1228 DEBUG_puts((" seek inside buffer..."));
1236 * 'cupsFileTell()' - Return the current file position.
1239 off_t
/* O - File position */
1240 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
1247 * 'cupsFileUnlock()' - Unlock access to a file.
1250 int /* O - 0 on success, -1 on error */
1251 cupsFileUnlock(cups_file_t
*fp
) /* I - File to lock */
1257 if (!fp
|| fp
->mode
== 's')
1265 return (locking(fp
->fd
, _LK_UNLCK
, 0));
1267 return (lockf(fp
->fd
, F_ULOCK
, 0));
1273 * 'cupsFileWrite()' - Write to a file.
1276 ssize_t
/* O - Number of bytes written */
1277 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
1278 const char *buf
, /* I - Buffer */
1279 size_t bytes
) /* I - Number of bytes to write */
1282 * Range check input...
1285 if (!fp
|| !buf
|| bytes
< 0 || (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1292 * Write the buffer...
1295 if (fp
->mode
== 's')
1297 if (cups_write(fp
, buf
, bytes
) < 0)
1305 if ((fp
->ptr
+ bytes
) > fp
->end
)
1306 if (cupsFileFlush(fp
))
1311 if (bytes
> sizeof(fp
->buf
))
1315 return (cups_compress(fp
, buf
, bytes
));
1317 #endif /* HAVE_LIBZ */
1318 return (cups_write(fp
, buf
, bytes
));
1322 memcpy(fp
->ptr
, buf
, bytes
);
1331 * 'cups_compress()' - Compress a buffer of data...
1334 static ssize_t
/* O - Number of bytes written or -1 */
1335 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
1336 const char *buf
, /* I - Buffer */
1337 size_t bytes
) /* I - Number bytes */
1343 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, bytes
);
1346 * Deflate the bytes...
1349 fp
->stream
.next_in
= (Bytef
*)buf
;
1350 fp
->stream
.avail_in
= bytes
;
1352 while (fp
->stream
.avail_in
> 0)
1355 * Flush the current buffer...
1358 if (fp
->stream
.avail_out
< (int)(sizeof(fp
->cbuf
) / 8))
1360 if (cups_write(fp
, (char *)fp
->cbuf
, fp
->stream
.next_out
- fp
->cbuf
) < 0)
1364 deflate(&(fp
->stream
), Z_NO_FLUSH
);
1369 #endif /* HAVE_LIBZ */
1373 * 'cups_fill()' - Fill the input buffer...
1376 static ssize_t
/* O - Number of bytes or -1 */
1377 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
1379 ssize_t bytes
; /* Number of bytes read */
1381 const unsigned char *ptr
, /* Pointer into buffer */
1382 *end
; /* End of buffer */
1383 #endif /* HAVE_LIBZ */
1386 DEBUG_printf(("cups_fill(fp=%p)\n", fp
));
1387 DEBUG_printf((" fp->ptr=%p, fp->end=%p, fp->buf=%p, fp->pos=%ld\n",
1388 fp
->ptr
, fp
->end
, fp
->buf
, (long)fp
->pos
));
1391 * Update the "pos" element as needed...
1394 if (fp
->ptr
&& fp
->end
)
1395 fp
->pos
+= fp
->end
- fp
->buf
;
1398 while (!fp
->ptr
|| fp
->compressed
)
1401 * Check to see if we have read any data yet; if not, see if we have a
1402 * compressed file...
1408 * Reset the file position in case we are seeking...
1415 * Read the first bytes in the file to determine if we have a gzip'd
1419 if ((bytes
= cups_read(fp
, (char *)fp
->buf
, sizeof(fp
->buf
))) < 0)
1422 * Can't read from file!
1428 if (bytes
< 10 || fp
->buf
[0] != 0x1f ||
1429 (fp
->buf
[1] & 255) != 0x8b ||
1430 fp
->buf
[2] != 8 || (fp
->buf
[3] & 0xe0) != 0)
1433 * Not a gzip'd file!
1437 fp
->end
= fp
->buf
+ bytes
;
1443 * Parse header junk: extra data, original name, and comment...
1446 ptr
= (unsigned char *)fp
->buf
+ 10;
1447 end
= (unsigned char *)fp
->buf
+ bytes
;
1449 if (fp
->buf
[3] & 0x04)
1452 * Skip extra data...
1455 if ((ptr
+ 2) > end
)
1458 * Can't read from file!
1464 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
1470 * Can't read from file!
1477 if (fp
->buf
[3] & 0x08)
1480 * Skip original name data...
1483 while (ptr
< end
&& *ptr
)
1491 * Can't read from file!
1498 if (fp
->buf
[3] & 0x10)
1501 * Skip comment data...
1504 while (ptr
< end
&& *ptr
)
1512 * Can't read from file!
1519 if (fp
->buf
[3] & 0x02)
1522 * Skip header CRC data...
1530 * Can't read from file!
1538 * Copy the flate-compressed data to the compression buffer...
1541 if ((bytes
= end
- ptr
) > 0)
1542 memcpy(fp
->cbuf
, ptr
, bytes
);
1545 * Setup the decompressor data...
1548 fp
->stream
.zalloc
= (alloc_func
)0;
1549 fp
->stream
.zfree
= (free_func
)0;
1550 fp
->stream
.opaque
= (voidpf
)0;
1551 fp
->stream
.next_in
= (Bytef
*)fp
->cbuf
;
1552 fp
->stream
.next_out
= NULL
;
1553 fp
->stream
.avail_in
= bytes
;
1554 fp
->stream
.avail_out
= 0;
1555 fp
->crc
= crc32(0L, Z_NULL
, 0);
1557 if (inflateInit2(&(fp
->stream
), -15) != Z_OK
)
1566 * If we have reached end-of-file, return immediately...
1573 * Fill the decompression buffer as needed...
1576 if (fp
->stream
.avail_in
== 0)
1578 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
1581 fp
->stream
.next_in
= fp
->cbuf
;
1582 fp
->stream
.avail_in
= bytes
;
1586 * Decompress data from the buffer...
1589 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
1590 fp
->stream
.avail_out
= sizeof(fp
->buf
);
1592 if (inflate(&(fp
->stream
), Z_NO_FLUSH
) == Z_STREAM_END
)
1595 * Read the CRC and length...
1598 unsigned char trailer
[8]; /* Trailer bytes */
1599 uLong tcrc
; /* Trailer CRC */
1602 if (read(fp
->fd
, trailer
, sizeof(trailer
)) < sizeof(trailer
))
1605 * Can't get it, so mark end-of-file...
1612 tcrc
= (((((trailer
[3] << 8) | trailer
[2]) << 8) | trailer
[1]) << 8) |
1615 if (tcrc
!= fp
->crc
)
1618 * Bad CRC, mark end-of-file...
1627 * Otherwise, reset the compressed flag so that we re-read the
1635 bytes
= sizeof(fp
->buf
) - fp
->stream
.avail_out
;
1638 * Return the decompressed data...
1642 fp
->end
= fp
->buf
+ bytes
;
1648 #endif /* HAVE_LIBZ */
1651 * Read a buffer's full of data...
1654 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
1657 * Can't read from file!
1668 * Return the bytes we read...
1673 fp
->end
= fp
->buf
+ bytes
;
1680 * 'cups_read()' - Read from a file descriptor.
1683 static ssize_t
/* O - Number of bytes read or -1 */
1684 cups_read(cups_file_t
*fp
, /* I - CUPS file */
1685 char *buf
, /* I - Buffer */
1686 size_t bytes
) /* I - Number bytes */
1688 ssize_t total
; /* Total bytes read */
1692 * Loop until we read at least 0 bytes...
1697 if (fp
->mode
== 's')
1698 total
= recv(fp
->fd
, buf
, bytes
, 0);
1700 total
= read(fp
->fd
, buf
, bytes
);
1706 * Reads can be interrupted by signals and unavailable resources...
1709 if (errno
== EAGAIN
|| errno
== EINTR
)
1716 * Return the total number of bytes read...
1724 * 'cups_write()' - Write to a file descriptor.
1727 static ssize_t
/* O - Number of bytes written or -1 */
1728 cups_write(cups_file_t
*fp
, /* I - CUPS file */
1729 const char *buf
, /* I - Buffer */
1730 size_t bytes
) /* I - Number bytes */
1732 size_t total
, /* Total bytes written */
1733 count
; /* Count this time */
1737 * Loop until all bytes are written...
1743 if (fp
->mode
== 's')
1744 count
= send(fp
->fd
, buf
, bytes
, 0);
1746 count
= write(fp
->fd
, buf
, bytes
);
1751 * Writes can be interrupted by signals and unavailable resources...
1754 if (errno
== EAGAIN
|| errno
== EINTR
)
1761 * Update the counts for the last write call...
1770 * Return the total number of bytes written...
1778 * End of "$Id: file.c 5057 2006-02-02 20:38:29Z mike $".