]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/file.c
2 * "$Id: file.c 4844 2005-11-21 17:37:57Z 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 * cupsFileFlush() - Flush pending output.
35 * cupsFileGetChar() - Get a single character from a file.
36 * cupsFileGetConf() - Get a line from a configuration file...
37 * cupsFileGets() - Get a CR and/or LF-terminated line.
38 * cupsFileLock() - Temporarily lock access to a file.
39 * cupsFileNumber() - Return the file descriptor associated with a CUPS file.
40 * cupsFileOpen() - Open a CUPS file.
41 * cupsFileOpenFd() - Open a CUPS file using a file descriptor.
42 * cupsFilePeekChar() - Peek at the next character from a file.
43 * cupsFilePrintf() - Write a formatted string.
44 * cupsFilePutChar() - Write a character.
45 * cupsFilePuts() - Write a string.
46 * cupsFileRead() - Read from a file.
47 * cupsFileRewind() - Rewind a file.
48 * cupsFileSeek() - Seek in a file.
49 * cupsFileTell() - Return the current file position.
50 * cupsFileUnlock() - Unlock access to a file.
51 * cupsFileWrite() - Write to a file.
52 * cups_compress() - Compress a buffer of data...
53 * cups_fill() - Fill the input buffer...
54 * cups_read() - Read from a file descriptor.
55 * cups_write() - Write to a file descriptor.
59 * Include necessary headers...
65 #include "http-private.h"
68 #include <cups/debug.h>
69 #include <sys/types.h>
75 #endif /* HAVE_LIBZ */
78 # include <sys/locking.h>
83 * Some operating systems support large files via open flag O_LARGEFILE...
87 # define O_LARGEFILE 0
88 #endif /* !O_LARGEFILE */
92 * Types and structures...
95 struct _cups_file_s
/**** CUPS file structure... ****/
98 int fd
; /* File descriptor */
99 char mode
, /* Mode ('r' or 'w') */
100 compressed
, /* Compression used? */
101 eof
, /* End of file? */
102 buf
[2048], /* Buffer */
103 *ptr
, /* Pointer into buffer */
104 *end
; /* End of buffer data */
105 off_t pos
; /* File position for start of buffer */
108 z_stream stream
; /* (De)compression stream */
109 Bytef cbuf
[1024]; /* (De)compression buffer */
110 uLong crc
; /* (De)compression CRC */
111 #endif /* HAVE_LIBZ */
120 static ssize_t
cups_compress(cups_file_t
*fp
, const char *buf
, size_t bytes
);
121 #endif /* HAVE_LIBZ */
122 static ssize_t
cups_fill(cups_file_t
*fp
);
123 static ssize_t
cups_read(cups_file_t
*fp
, char *buf
, size_t bytes
);
124 static ssize_t
cups_write(cups_file_t
*fp
, const char *buf
, size_t bytes
);
128 * 'cupsFileClose()' - Close a CUPS file.
131 int /* O - 0 on success, -1 on error */
132 cupsFileClose(cups_file_t
*fp
) /* I - CUPS file */
134 int fd
; /* File descriptor */
135 char mode
; /* Open mode */
136 int status
; /* Return status */
139 DEBUG_printf(("cupsFileClose(fp=%p)\n", fp
));
149 * Flush pending write data...
153 status
= cupsFileFlush(fp
);
158 if (fp
->compressed
&& status
>= 0)
163 * Free decompression data...
166 inflateEnd(&fp
->stream
);
171 * Flush any remaining compressed data...
174 unsigned char trailer
[8]; /* Trailer CRC and length */
175 int done
; /* Done writing... */
178 fp
->stream
.avail_in
= 0;
182 if (fp
->stream
.next_out
> fp
->cbuf
)
184 if (cups_write(fp
, (char *)fp
->cbuf
,
185 fp
->stream
.next_out
- fp
->cbuf
) < 0)
188 fp
->stream
.next_out
= fp
->cbuf
;
189 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
192 if (done
|| status
< 0)
195 done
= deflate(&fp
->stream
, Z_FINISH
) == Z_STREAM_END
&&
196 fp
->stream
.next_out
== fp
->cbuf
;
200 * Write the CRC and length...
203 trailer
[0] = fp
->crc
;
204 trailer
[1] = fp
->crc
>> 8;
205 trailer
[2] = fp
->crc
>> 16;
206 trailer
[3] = fp
->crc
>> 24;
207 trailer
[4] = fp
->pos
;
208 trailer
[5] = fp
->pos
>> 8;
209 trailer
[6] = fp
->pos
>> 16;
210 trailer
[7] = fp
->pos
>> 24;
212 if (cups_write(fp
, (char *)trailer
, 8) < 0)
216 * Free all memory used by the compression stream...
219 deflateEnd(&(fp
->stream
));
222 #endif /* HAVE_LIBZ */
225 * Save the file descriptor we used and free memory...
234 * Close the file, returning the close status...
239 if (closesocket(fd
) < 0)
253 * 'cupsFileCompression()' - Return whether a file is compressed.
256 int /* O - CUPS_FILE_NONE or CUPS_FILE_GZIP */
257 cupsFileCompression(cups_file_t
*fp
) /* I - CUPS file */
259 return (fp
->compressed
);
264 * 'cupsFileEOF()' - Return the end-of-file status.
267 int /* O - 1 on EOF, 0 otherwise */
268 cupsFileEOF(cups_file_t
*fp
) /* I - CUPS file */
275 * 'cupsFileFlush()' - Flush pending output.
278 int /* O - 0 on success, -1 on error */
279 cupsFileFlush(cups_file_t
*fp
) /* I - CUPS file */
281 size_t bytes
; /* Bytes to write */
284 DEBUG_printf(("cupsFileFlush(fp=%p)\n", fp
));
287 * Range check input...
290 if (!fp
|| fp
->mode
!= 'w')
292 DEBUG_puts(" Attempt to flush a read-only file...");
296 bytes
= fp
->ptr
- fp
->buf
;
302 bytes
= cups_compress(fp
, fp
->buf
, bytes
);
304 #endif /* HAVE_LIBZ */
305 bytes
= cups_write(fp
, fp
->buf
, bytes
);
318 * 'cupsFileGetChar()' - Get a single character from a file.
321 int /* O - Character or -1 on EOF */
322 cupsFileGetChar(cups_file_t
*fp
) /* I - CUPS file */
325 * Range check input...
328 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
332 * If the input buffer is empty, try to read more data...
335 if (fp
->ptr
>= fp
->end
)
336 if (cups_fill(fp
) < 0)
340 * Return the next character in the buffer...
343 return (*(fp
->ptr
)++ & 255);
348 * 'cupsFileGetConf()' - Get a line from a configuration file...
351 char * /* O - Line read of NULL on eof/error */
352 cupsFileGetConf(cups_file_t
*fp
, /* I - CUPS file */
353 char *buf
, /* O - String buffer */
354 size_t buflen
, /* I - Size of string buffer */
355 char **value
, /* O - Pointer to value */
356 int *linenum
) /* IO - Current line number */
358 char *ptr
; /* Pointer into line */
362 * Range check input...
365 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') ||
366 !buf
|| buflen
< 2 || !value
)
375 * Read the next non-comment line...
380 while (cupsFileGets(fp
, buf
, buflen
))
385 * Strip any comments...
388 if ((ptr
= strchr(buf
, '#')) != NULL
)
392 if (!isspace(ptr
[-1] & 255))
402 * Strip leading whitespace...
405 for (ptr
= buf
; isspace(*ptr
& 255); ptr
++);
408 _cups_strcpy(buf
, ptr
);
411 * See if there is anything left...
417 * Yes, grab any value and return...
420 for (ptr
= buf
; *ptr
; ptr
++)
421 if (isspace(*ptr
& 255))
427 * Have a value, skip any other spaces...
430 while (isspace(*ptr
& 255))
437 * Strip trailing whitespace and > for lines that begin with <...
440 ptr
+= strlen(ptr
) - 1;
442 if (buf
[0] == '<' && *ptr
== '>')
444 else if (buf
[0] == '<' && *ptr
!= '>')
454 while (ptr
> *value
&& isspace(*ptr
& 255))
471 * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
474 char * /* O - Line read or NULL on eof/error */
475 cupsFileGets(cups_file_t
*fp
, /* I - CUPS file */
476 char *buf
, /* O - String buffer */
477 size_t buflen
) /* I - Size of string buffer */
479 int ch
; /* Character from file */
480 char *ptr
, /* Current position in line buffer */
481 *end
; /* End of line buffer */
485 * Range check input...
488 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 2)
492 * Now loop until we have a valid line...
495 for (ptr
= buf
, end
= buf
+ buflen
- 1; ptr
< end
;)
497 if (fp
->ptr
>= fp
->end
)
498 if (cups_fill(fp
) <= 0)
514 if (fp
->ptr
>= fp
->end
)
515 if (cups_fill(fp
) <= 0)
518 if (*(fp
->ptr
) == '\n')
526 * Line feed ends a line...
542 * 'cupsFileLock()' - Temporarily lock access to a file.
545 int /* O - 0 on success, -1 on error */
546 cupsFileLock(cups_file_t
*fp
, /* I - File to lock */
547 int block
) /* I - 1 to wait for the lock, 0 to fail right away */
553 if (!fp
|| fp
->mode
== 's')
561 return (locking(fp
->fd
, block
? _LK_LOCK
: _LK_NBLCK
, 0));
563 return (lockf(fp
->fd
, block
? F_LOCK
: F_TLOCK
, 0));
569 * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
572 int /* O - File descriptor */
573 cupsFileNumber(cups_file_t
*fp
) /* I - CUPS file */
580 * 'cupsFileOpen()' - Open a CUPS file.
583 cups_file_t
* /* O - CUPS file or NULL */
584 cupsFileOpen(const char *filename
, /* I - Name of file */
585 const char *mode
) /* I - Open mode */
587 cups_file_t
*fp
; /* New CUPS file */
588 int fd
; /* File descriptor */
589 char hostname
[1024], /* Hostname */
590 *portname
; /* Port "name" (number or service) */
591 http_addrlist_t
*addrlist
; /* Host address list */
595 * Range check input...
598 if (!filename
|| !mode
||
599 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's'))
608 case 'a' : /* Append file */
609 fd
= open(filename
, O_RDWR
| O_CREAT
| O_APPEND
| O_LARGEFILE
, 0666);
612 case 'r' : /* Read file */
613 fd
= open(filename
, O_RDONLY
| O_LARGEFILE
, 0);
616 case 'w' : /* Write file */
617 fd
= open(filename
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_LARGEFILE
, 0666);
620 case 's' : /* Read/write socket */
621 strlcpy(hostname
, filename
, sizeof(hostname
));
622 if ((portname
= strrchr(hostname
, ':')) != NULL
)
628 * Lookup the hostname and service...
631 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
635 * Connect to the server...
638 if (!httpAddrConnect(addrlist
, &fd
))
640 httpAddrFreeList(addrlist
);
644 httpAddrFreeList(addrlist
);
647 default : /* Remove bogus compiler warning... */
655 * Create the CUPS file structure...
658 if ((fp
= cupsFileOpenFd(fd
, mode
)) == NULL
)
674 * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
677 cups_file_t
* /* O - CUPS file or NULL */
678 cupsFileOpenFd(int fd
, /* I - File descriptor */
679 const char *mode
) /* I - Open mode */
681 cups_file_t
*fp
; /* New CUPS file */
685 * Range check input...
688 if (fd
< 0 || !mode
||
689 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's'))
696 if ((fp
= calloc(1, sizeof(cups_file_t
))) == NULL
)
711 fp
->end
= fp
->buf
+ sizeof(fp
->buf
);
714 if (mode
[1] >= '1' && mode
[1] <= '9')
717 * Open a compressed stream, so write the standard gzip file
721 unsigned char header
[10]; /* gzip file header */
722 time_t curtime
; /* Current time */
725 curtime
= time(NULL
);
728 header
[2] = Z_DEFLATED
;
731 header
[5] = curtime
>> 8;
732 header
[6] = curtime
>> 16;
733 header
[7] = curtime
>> 24;
737 cups_write(fp
, (char *)header
, 10);
740 * Initialize the compressor...
743 deflateInit2(&(fp
->stream
), mode
[1] - '0', Z_DEFLATED
, -15, 8,
746 fp
->stream
.next_out
= fp
->cbuf
;
747 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
749 fp
->crc
= crc32(0L, Z_NULL
, 0);
751 #endif /* HAVE_LIBZ */
762 default : /* Remove bogus compiler warning... */
767 * Don't pass this file to child processes...
771 fcntl(fp
->fd
, F_SETFD
, fcntl(fp
->fd
, F_GETFD
) | FD_CLOEXEC
);
779 * 'cupsFilePeekChar()' - Peek at the next character from a file.
782 int /* O - Character or -1 on EOF */
783 cupsFilePeekChar(cups_file_t
*fp
) /* I - CUPS file */
786 * Range check input...
789 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
793 * If the input buffer is empty, try to read more data...
796 if (fp
->ptr
>= fp
->end
)
797 if (cups_fill(fp
) < 0)
801 * Return the next character in the buffer...
804 return (*(fp
->ptr
) & 255);
809 * 'cupsFilePrintf()' - Write a formatted string.
812 int /* O - Number of bytes written or -1 */
813 cupsFilePrintf(cups_file_t
*fp
, /* I - CUPS file */
814 const char *format
, /* I - Printf-style format string */
815 ...) /* I - Additional args as necessary */
817 va_list ap
; /* Argument list */
818 size_t bytes
; /* Formatted size */
819 char buf
[2048]; /* Formatted text */
822 if (!fp
|| !format
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
825 va_start(ap
, format
);
826 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
830 return (cups_write(fp
, buf
, bytes
));
832 if ((fp
->ptr
+ bytes
) > fp
->end
)
833 if (cupsFileFlush(fp
))
838 if (bytes
> sizeof(fp
->buf
))
842 return (cups_compress(fp
, buf
, bytes
));
844 #endif /* HAVE_LIBZ */
845 return (cups_write(fp
, buf
, bytes
));
849 memcpy(fp
->ptr
, buf
, bytes
);
857 * 'cupsFilePutChar()' - Write a character.
860 int /* O - 0 on success, -1 on error */
861 cupsFilePutChar(cups_file_t
*fp
, /* I - CUPS file */
862 int c
) /* I - Character to write */
865 * Range check input...
868 if (!fp
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
874 * Send character immediately over socket...
877 char ch
; /* Output character */
882 if (send(fp
->fd
, &ch
, 1, 0) < 1)
891 if (fp
->ptr
>= fp
->end
)
892 if (cupsFileFlush(fp
))
905 * 'cupsFilePuts()' - Write a string.
908 int /* O - Number of bytes written or -1 */
909 cupsFilePuts(cups_file_t
*fp
, /* I - CUPS file */
910 const char *s
) /* I - String to write */
912 size_t bytes
; /* Bytes to write */
916 * Range check input...
919 if (!fp
|| !s
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
923 * Write the string...
930 if (cups_write(fp
, s
, bytes
) < 0)
938 if ((fp
->ptr
+ bytes
) > fp
->end
)
939 if (cupsFileFlush(fp
))
944 if (bytes
> sizeof(fp
->buf
))
948 return (cups_compress(fp
, s
, bytes
));
950 #endif /* HAVE_LIBZ */
951 return (cups_write(fp
, s
, bytes
));
955 memcpy(fp
->ptr
, s
, bytes
);
963 * 'cupsFileRead()' - Read from a file.
966 ssize_t
/* O - Number of bytes read or -1 */
967 cupsFileRead(cups_file_t
*fp
, /* I - CUPS file */
968 char *buf
, /* O - Buffer */
969 size_t bytes
) /* I - Number of bytes to read */
971 size_t total
, /* Total bytes read */
972 count
; /* Bytes read */
975 DEBUG_printf(("cupsFileRead(fp=%p, buf=%p, bytes=%ld)\n", fp
, buf
,
979 * Range check input...
982 if (!fp
|| !buf
|| bytes
< 0 || (fp
->mode
!= 'r' && fp
->mode
!= 's'))
989 * Loop until all bytes are read...
995 if (fp
->ptr
>= fp
->end
)
996 if (cups_fill(fp
) <= 0)
998 DEBUG_printf((" cups_fill() returned -1, total=%d\n", total
));
1006 count
= fp
->end
- fp
->ptr
;
1010 memcpy(buf
, fp
->ptr
, count
);
1014 * Update the counts for the last read...
1023 * Return the total number of bytes read...
1026 DEBUG_printf((" total=%d\n", total
));
1033 * 'cupsFileRewind()' - Rewind a file.
1036 off_t
/* O - New file position or -1 */
1037 cupsFileRewind(cups_file_t
*fp
) /* I - CUPS file */
1039 return (cupsFileSeek(fp
, 0L));
1044 * 'cupsFileSeek()' - Seek in a file.
1047 off_t
/* O - New file position or -1 */
1048 cupsFileSeek(cups_file_t
*fp
, /* I - CUPS file */
1049 off_t pos
) /* I - Position in file */
1051 size_t bytes
; /* Number bytes in buffer */
1054 DEBUG_printf(("cupsFileSeek(fp=%p, pos=%ld)\n", fp
, (long)pos
));
1055 DEBUG_printf((" fp->pos=%ld\n", (long)fp
->pos
));
1058 * Range check input...
1061 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1065 * Figure out the number of bytes in the current buffer, and then
1066 * see if we are outside of it...
1069 bytes
= fp
->end
- fp
->buf
;
1075 * Need to seek backwards...
1081 inflateEnd(&fp
->stream
);
1083 lseek(fp
->fd
, 0, SEEK_SET
);
1088 while ((bytes
= cups_fill(fp
)) > 0)
1089 if (pos
>= fp
->pos
&& pos
< (fp
->pos
+ bytes
))
1096 #endif /* HAVE_LIBZ */
1098 fp
->pos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1099 DEBUG_printf((" lseek() returned %ld...\n", (long)fp
->pos
));
1104 else if (pos
>= (fp
->pos
+ bytes
))
1107 * Need to seek forwards...
1113 while ((bytes
= cups_fill(fp
)) > 0)
1114 if (pos
>= fp
->pos
&& pos
< (fp
->pos
+ bytes
))
1121 #endif /* HAVE_LIBZ */
1123 fp
->pos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1124 DEBUG_printf((" lseek() returned %ld...\n", (long)fp
->pos
));
1132 * Just reposition the current pointer, since we have the right
1136 fp
->ptr
= fp
->buf
+ pos
- fp
->pos
;
1137 DEBUG_puts((" seek inside buffer..."));
1145 * 'cupsFileTell()' - Return the current file position.
1148 off_t
/* O - File position */
1149 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
1156 * 'cupsFileUnlock()' - Unlock access to a file.
1159 int /* O - 0 on success, -1 on error */
1160 cupsFileUnlock(cups_file_t
*fp
) /* I - File to lock */
1166 if (!fp
|| fp
->mode
== 's')
1174 return (locking(fp
->fd
, _LK_UNLCK
, 0));
1176 return (lockf(fp
->fd
, F_ULOCK
, 0));
1182 * 'cupsFileWrite()' - Write to a file.
1185 ssize_t
/* O - Number of bytes written */
1186 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
1187 const char *buf
, /* I - Buffer */
1188 size_t bytes
) /* I - Number of bytes to write */
1191 * Range check input...
1194 if (!fp
|| !buf
|| bytes
< 0 || (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1201 * Write the buffer...
1204 if (fp
->mode
== 's')
1206 if (cups_write(fp
, buf
, bytes
) < 0)
1214 if ((fp
->ptr
+ bytes
) > fp
->end
)
1215 if (cupsFileFlush(fp
))
1220 if (bytes
> sizeof(fp
->buf
))
1224 return (cups_compress(fp
, buf
, bytes
));
1226 #endif /* HAVE_LIBZ */
1227 return (cups_write(fp
, buf
, bytes
));
1231 memcpy(fp
->ptr
, buf
, bytes
);
1240 * 'cups_compress()' - Compress a buffer of data...
1243 static ssize_t
/* O - Number of bytes written or -1 */
1244 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
1245 const char *buf
, /* I - Buffer */
1246 size_t bytes
) /* I - Number bytes */
1252 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, bytes
);
1255 * Deflate the bytes...
1258 fp
->stream
.next_in
= (Bytef
*)buf
;
1259 fp
->stream
.avail_in
= bytes
;
1261 while (fp
->stream
.avail_in
> 0)
1264 * Flush the current buffer...
1267 if (fp
->stream
.avail_out
< (int)(sizeof(fp
->cbuf
) / 8))
1269 if (cups_write(fp
, (char *)fp
->cbuf
, fp
->stream
.next_out
- fp
->cbuf
) < 0)
1273 deflate(&(fp
->stream
), Z_NO_FLUSH
);
1278 #endif /* HAVE_LIBZ */
1282 * 'cups_fill()' - Fill the input buffer...
1285 static ssize_t
/* O - Number of bytes or -1 */
1286 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
1288 ssize_t bytes
; /* Number of bytes read */
1290 const unsigned char *ptr
, /* Pointer into buffer */
1291 *end
; /* End of buffer */
1292 #endif /* HAVE_LIBZ */
1295 DEBUG_printf(("cups_fill(fp=%p)\n", fp
));
1296 DEBUG_printf((" fp->ptr=%p, fp->end=%p, fp->buf=%p, fp->pos=%ld\n",
1297 fp
->ptr
, fp
->end
, fp
->buf
, (long)fp
->pos
));
1300 * Update the "pos" element as needed...
1303 if (fp
->ptr
&& fp
->end
)
1304 fp
->pos
+= fp
->end
- fp
->buf
;
1307 while (!fp
->ptr
|| fp
->compressed
)
1310 * Check to see if we have read any data yet; if not, see if we have a
1311 * compressed file...
1317 * Reset the file position in case we are seeking...
1324 * Read the first bytes in the file to determine if we have a gzip'd
1328 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) < 0)
1331 * Can't read from file!
1337 if (bytes
< 10 || fp
->cbuf
[0] != 0x1f || fp
->cbuf
[1] != 0x8b ||
1338 fp
->cbuf
[2] != 8 || (fp
->cbuf
[3] & 0xe0) != 0)
1341 * Not a gzip'd file!
1344 memcpy(fp
->buf
, fp
->cbuf
, bytes
);
1347 fp
->end
= fp
->buf
+ bytes
;
1353 * Parse header junk: extra data, original name, and comment...
1356 ptr
= (unsigned char *)fp
->cbuf
+ 10;
1357 end
= (unsigned char *)fp
->cbuf
+ bytes
;
1359 if (fp
->cbuf
[3] & 0x04)
1362 * Skip extra data...
1365 if ((ptr
+ 2) > end
)
1368 * Can't read from file!
1374 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
1380 * Can't read from file!
1387 if (fp
->cbuf
[3] & 0x08)
1390 * Skip original name data...
1393 while (ptr
< end
&& *ptr
)
1401 * Can't read from file!
1408 if (fp
->cbuf
[3] & 0x10)
1411 * Skip comment data...
1414 while (ptr
< end
&& *ptr
)
1422 * Can't read from file!
1429 if (fp
->cbuf
[3] & 0x02)
1432 * Skip header CRC data...
1440 * Can't read from file!
1448 * Setup the decompressor data...
1451 fp
->stream
.zalloc
= (alloc_func
)0;
1452 fp
->stream
.zfree
= (free_func
)0;
1453 fp
->stream
.opaque
= (voidpf
)0;
1454 fp
->stream
.next_in
= (Bytef
*)ptr
;
1455 fp
->stream
.next_out
= NULL
;
1456 fp
->stream
.avail_in
= end
- ptr
;
1457 fp
->stream
.avail_out
= 0;
1458 fp
->crc
= crc32(0L, Z_NULL
, 0);
1460 if (inflateInit2(&(fp
->stream
), -15) != Z_OK
)
1469 * If we have reached end-of-file, return immediately...
1476 * Fill the decompression buffer as needed...
1479 if (fp
->stream
.avail_in
== 0)
1481 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
1484 fp
->stream
.next_in
= fp
->cbuf
;
1485 fp
->stream
.avail_in
= bytes
;
1489 * Decompress data from the buffer...
1492 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
1493 fp
->stream
.avail_out
= sizeof(fp
->buf
);
1495 if (inflate(&(fp
->stream
), Z_NO_FLUSH
) == Z_STREAM_END
)
1498 * Read the CRC and length...
1501 unsigned char trailer
[8]; /* Trailer bytes */
1502 uLong tcrc
; /* Trailer CRC */
1505 if (read(fp
->fd
, trailer
, sizeof(trailer
)) < sizeof(trailer
))
1508 * Can't get it, so mark end-of-file...
1515 tcrc
= (((((trailer
[3] << 8) | trailer
[2]) << 8) | trailer
[1]) << 8) |
1518 if (tcrc
!= fp
->crc
)
1521 * Bad CRC, mark end-of-file...
1529 * Otherwise, reset the current pointer so that we re-read the
1537 bytes
= sizeof(fp
->buf
) - fp
->stream
.avail_out
;
1540 * Return the decompressed data...
1544 fp
->end
= fp
->buf
+ bytes
;
1549 #endif /* HAVE_LIBZ */
1552 * Read a buffer's full of data...
1555 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
1558 * Can't read from file!
1569 * Return the bytes we read...
1574 fp
->end
= fp
->buf
+ bytes
;
1581 * 'cups_read()' - Read from a file descriptor.
1584 static ssize_t
/* O - Number of bytes read or -1 */
1585 cups_read(cups_file_t
*fp
, /* I - CUPS file */
1586 char *buf
, /* I - Buffer */
1587 size_t bytes
) /* I - Number bytes */
1589 ssize_t total
; /* Total bytes read */
1593 * Loop until we read at least 0 bytes...
1598 if (fp
->mode
== 's')
1599 total
= recv(fp
->fd
, buf
, bytes
, 0);
1601 total
= read(fp
->fd
, buf
, bytes
);
1607 * Reads can be interrupted by signals and unavailable resources...
1610 if (errno
== EAGAIN
|| errno
== EINTR
)
1617 * Return the total number of bytes read...
1625 * 'cups_write()' - Write to a file descriptor.
1628 static ssize_t
/* O - Number of bytes written or -1 */
1629 cups_write(cups_file_t
*fp
, /* I - CUPS file */
1630 const char *buf
, /* I - Buffer */
1631 size_t bytes
) /* I - Number bytes */
1633 size_t total
, /* Total bytes written */
1634 count
; /* Count this time */
1638 * Loop until all bytes are written...
1644 if (fp
->mode
== 's')
1645 count
= send(fp
->fd
, buf
, bytes
, 0);
1647 count
= write(fp
->fd
, buf
, bytes
);
1652 * Writes can be interrupted by signals and unavailable resources...
1655 if (errno
== EAGAIN
|| errno
== EINTR
)
1662 * Update the counts for the last write call...
1671 * Return the total number of bytes written...
1679 * End of "$Id: file.c 4844 2005-11-21 17:37:57Z mike $".