]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/file.c
2 * "$Id: file.c 5186 2006-02-26 18:56:05Z 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-2006 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
;
389 DEBUG_printf((" Flushing %ld bytes...\n", (long)bytes
));
395 bytes
= cups_compress(fp
, fp
->buf
, bytes
);
397 #endif /* HAVE_LIBZ */
398 bytes
= cups_write(fp
, fp
->buf
, bytes
);
411 * 'cupsFileGetChar()' - Get a single character from a file.
414 int /* O - Character or -1 on EOF */
415 cupsFileGetChar(cups_file_t
*fp
) /* I - CUPS file */
418 * Range check input...
421 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
425 * If the input buffer is empty, try to read more data...
428 if (fp
->ptr
>= fp
->end
)
429 if (cups_fill(fp
) < 0)
433 * Return the next character in the buffer...
436 return (*(fp
->ptr
)++ & 255);
441 * 'cupsFileGetConf()' - Get a line from a configuration file...
444 char * /* O - Line read or NULL on eof/error */
445 cupsFileGetConf(cups_file_t
*fp
, /* I - CUPS file */
446 char *buf
, /* O - String buffer */
447 size_t buflen
, /* I - Size of string buffer */
448 char **value
, /* O - Pointer to value */
449 int *linenum
) /* IO - Current line number */
451 char *ptr
; /* Pointer into line */
455 * Range check input...
458 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') ||
459 !buf
|| buflen
< 2 || !value
)
468 * Read the next non-comment line...
473 while (cupsFileGets(fp
, buf
, buflen
))
478 * Strip any comments...
481 if ((ptr
= strchr(buf
, '#')) != NULL
)
485 if (!isspace(ptr
[-1] & 255))
495 * Strip leading whitespace...
498 for (ptr
= buf
; isspace(*ptr
& 255); ptr
++);
501 _cups_strcpy(buf
, ptr
);
504 * See if there is anything left...
510 * Yes, grab any value and return...
513 for (ptr
= buf
; *ptr
; ptr
++)
514 if (isspace(*ptr
& 255))
520 * Have a value, skip any other spaces...
523 while (isspace(*ptr
& 255))
530 * Strip trailing whitespace and > for lines that begin with <...
533 ptr
+= strlen(ptr
) - 1;
535 if (buf
[0] == '<' && *ptr
== '>')
537 else if (buf
[0] == '<' && *ptr
!= '>')
547 while (ptr
> *value
&& isspace(*ptr
& 255))
564 * 'cupsFileGets()' - Get a CR and/or LF-terminated line.
567 char * /* O - Line read or NULL on eof/error */
568 cupsFileGets(cups_file_t
*fp
, /* I - CUPS file */
569 char *buf
, /* O - String buffer */
570 size_t buflen
) /* I - Size of string buffer */
572 int ch
; /* Character from file */
573 char *ptr
, /* Current position in line buffer */
574 *end
; /* End of line buffer */
578 * Range check input...
581 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's') || !buf
|| buflen
< 2)
585 * Now loop until we have a valid line...
588 for (ptr
= buf
, end
= buf
+ buflen
- 1; ptr
< end
;)
590 if (fp
->ptr
>= fp
->end
)
591 if (cups_fill(fp
) <= 0)
607 if (fp
->ptr
>= fp
->end
)
608 if (cups_fill(fp
) <= 0)
611 if (*(fp
->ptr
) == '\n')
619 * Line feed ends a line...
635 * 'cupsFileLock()' - Temporarily lock access to a file.
638 int /* O - 0 on success, -1 on error */
639 cupsFileLock(cups_file_t
*fp
, /* I - File to lock */
640 int block
) /* I - 1 to wait for the lock, 0 to fail right away */
646 if (!fp
|| fp
->mode
== 's')
654 return (locking(fp
->fd
, block
? _LK_LOCK
: _LK_NBLCK
, 0));
656 return (lockf(fp
->fd
, block
? F_LOCK
: F_TLOCK
, 0));
662 * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file.
665 int /* O - File descriptor */
666 cupsFileNumber(cups_file_t
*fp
) /* I - CUPS file */
673 * 'cupsFileOpen()' - Open a CUPS file.
676 cups_file_t
* /* O - CUPS file or NULL */
677 cupsFileOpen(const char *filename
, /* I - Name of file */
678 const char *mode
) /* I - Open mode */
680 cups_file_t
*fp
; /* New CUPS file */
681 int fd
; /* File descriptor */
682 char hostname
[1024], /* Hostname */
683 *portname
; /* Port "name" (number or service) */
684 http_addrlist_t
*addrlist
; /* Host address list */
687 DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")\n", filename
,
691 * Range check input...
694 if (!filename
|| !mode
||
695 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's'))
704 case 'a' : /* Append file */
705 fd
= open(filename
, O_RDWR
| O_CREAT
| O_APPEND
| O_LARGEFILE
, 0666);
708 case 'r' : /* Read file */
709 fd
= open(filename
, O_RDONLY
| O_LARGEFILE
, 0);
712 case 'w' : /* Write file */
713 fd
= open(filename
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_LARGEFILE
, 0666);
716 case 's' : /* Read/write socket */
717 strlcpy(hostname
, filename
, sizeof(hostname
));
718 if ((portname
= strrchr(hostname
, ':')) != NULL
)
724 * Lookup the hostname and service...
727 if ((addrlist
= httpAddrGetList(hostname
, AF_UNSPEC
, portname
)) == NULL
)
731 * Connect to the server...
734 if (!httpAddrConnect(addrlist
, &fd
))
736 httpAddrFreeList(addrlist
);
740 httpAddrFreeList(addrlist
);
743 default : /* Remove bogus compiler warning... */
751 * Create the CUPS file structure...
754 if ((fp
= cupsFileOpenFd(fd
, mode
)) == NULL
)
770 * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor.
773 cups_file_t
* /* O - CUPS file or NULL */
774 cupsFileOpenFd(int fd
, /* I - File descriptor */
775 const char *mode
) /* I - Open mode */
777 cups_file_t
*fp
; /* New CUPS file */
780 DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")\n", fd
, mode
));
783 * Range check input...
786 if (fd
< 0 || !mode
||
787 (*mode
!= 'r' && *mode
!= 'w' && *mode
!= 'a' && *mode
!= 's'))
794 if ((fp
= calloc(1, sizeof(cups_file_t
))) == NULL
)
809 fp
->end
= fp
->buf
+ sizeof(fp
->buf
);
812 if (mode
[1] >= '1' && mode
[1] <= '9')
815 * Open a compressed stream, so write the standard gzip file
819 unsigned char header
[10]; /* gzip file header */
820 time_t curtime
; /* Current time */
823 curtime
= time(NULL
);
826 header
[2] = Z_DEFLATED
;
829 header
[5] = curtime
>> 8;
830 header
[6] = curtime
>> 16;
831 header
[7] = curtime
>> 24;
835 cups_write(fp
, (char *)header
, 10);
838 * Initialize the compressor...
841 deflateInit2(&(fp
->stream
), mode
[1] - '0', Z_DEFLATED
, -15, 8,
844 fp
->stream
.next_out
= fp
->cbuf
;
845 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
847 fp
->crc
= crc32(0L, Z_NULL
, 0);
849 #endif /* HAVE_LIBZ */
860 default : /* Remove bogus compiler warning... */
865 * Don't pass this file to child processes...
869 fcntl(fp
->fd
, F_SETFD
, fcntl(fp
->fd
, F_GETFD
) | FD_CLOEXEC
);
877 * 'cupsFilePeekChar()' - Peek at the next character from a file.
880 int /* O - Character or -1 on EOF */
881 cupsFilePeekChar(cups_file_t
*fp
) /* I - CUPS file */
884 * Range check input...
887 if (!fp
|| (fp
->mode
!= 'r' && fp
->mode
!= 's'))
891 * If the input buffer is empty, try to read more data...
894 if (fp
->ptr
>= fp
->end
)
895 if (cups_fill(fp
) < 0)
899 * Return the next character in the buffer...
902 return (*(fp
->ptr
) & 255);
907 * 'cupsFilePrintf()' - Write a formatted string.
910 int /* O - Number of bytes written or -1 */
911 cupsFilePrintf(cups_file_t
*fp
, /* I - CUPS file */
912 const char *format
, /* I - Printf-style format string */
913 ...) /* I - Additional args as necessary */
915 va_list ap
; /* Argument list */
916 size_t bytes
; /* Formatted size */
917 char buf
[8192]; /* Formatted text */
920 DEBUG_printf(("cupsFilePrintf(fp=%p, format=\"%s\", ...)\n", fp
, format
));
922 if (!fp
|| !format
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
925 va_start(ap
, format
);
926 bytes
= vsnprintf(buf
, sizeof(buf
), format
, ap
);
929 if (bytes
>= sizeof(buf
))
933 return (cups_write(fp
, buf
, bytes
));
935 if ((fp
->ptr
+ bytes
) > fp
->end
)
936 if (cupsFileFlush(fp
))
941 if (bytes
> sizeof(fp
->buf
))
945 return (cups_compress(fp
, buf
, bytes
));
947 #endif /* HAVE_LIBZ */
948 return (cups_write(fp
, buf
, bytes
));
952 memcpy(fp
->ptr
, buf
, bytes
);
960 * 'cupsFilePutChar()' - Write a character.
963 int /* O - 0 on success, -1 on error */
964 cupsFilePutChar(cups_file_t
*fp
, /* I - CUPS file */
965 int c
) /* I - Character to write */
968 * Range check input...
971 if (!fp
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
977 * Send character immediately over socket...
980 char ch
; /* Output character */
985 if (send(fp
->fd
, &ch
, 1, 0) < 1)
994 if (fp
->ptr
>= fp
->end
)
995 if (cupsFileFlush(fp
))
1008 * 'cupsFilePuts()' - Write a string.
1011 int /* O - Number of bytes written or -1 */
1012 cupsFilePuts(cups_file_t
*fp
, /* I - CUPS file */
1013 const char *s
) /* I - String to write */
1015 size_t bytes
; /* Bytes to write */
1019 * Range check input...
1022 if (!fp
|| !s
|| (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1026 * Write the string...
1031 if (fp
->mode
== 's')
1033 if (cups_write(fp
, s
, bytes
) < 0)
1041 if ((fp
->ptr
+ bytes
) > fp
->end
)
1042 if (cupsFileFlush(fp
))
1047 if (bytes
> sizeof(fp
->buf
))
1051 return (cups_compress(fp
, s
, bytes
));
1053 #endif /* HAVE_LIBZ */
1054 return (cups_write(fp
, s
, bytes
));
1058 memcpy(fp
->ptr
, s
, bytes
);
1066 * 'cupsFileRead()' - Read from a file.
1069 ssize_t
/* O - Number of bytes read or -1 */
1070 cupsFileRead(cups_file_t
*fp
, /* I - CUPS file */
1071 char *buf
, /* O - Buffer */
1072 size_t bytes
) /* I - Number of bytes to read */
1074 size_t total
, /* Total bytes read */
1075 count
; /* Bytes read */
1078 DEBUG_printf(("cupsFileRead(fp=%p, buf=%p, bytes=%ld)\n", fp
, buf
,
1082 * Range check input...
1085 if (!fp
|| !buf
|| bytes
< 0 || (fp
->mode
!= 'r' && fp
->mode
!= 's'))
1092 * Loop until all bytes are read...
1098 if (fp
->ptr
>= fp
->end
)
1099 if (cups_fill(fp
) <= 0)
1101 DEBUG_printf((" cups_fill() returned -1, total=%d\n", total
));
1109 count
= fp
->end
- fp
->ptr
;
1113 memcpy(buf
, fp
->ptr
, count
);
1117 * Update the counts for the last read...
1126 * Return the total number of bytes read...
1129 DEBUG_printf((" total=%d\n", total
));
1136 * 'cupsFileRewind()' - Rewind a file.
1139 off_t
/* O - New file position or -1 */
1140 cupsFileRewind(cups_file_t
*fp
) /* I - CUPS file */
1142 return (cupsFileSeek(fp
, 0L));
1147 * 'cupsFileSeek()' - Seek in a file.
1150 off_t
/* O - New file position or -1 */
1151 cupsFileSeek(cups_file_t
*fp
, /* I - CUPS file */
1152 off_t pos
) /* I - Position in file */
1154 size_t bytes
; /* Number bytes in buffer */
1157 DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT
")\n", fp
, pos
));
1158 DEBUG_printf((" fp->pos=" CUPS_LLFMT
"\n", fp
->pos
));
1159 DEBUG_printf((" fp->ptr=%p, fp->end=%p\n", fp
->ptr
, fp
->end
));
1162 * Range check input...
1165 if (!fp
|| pos
< 0 || fp
->mode
!= 'r')
1171 * No seeking necessary...
1184 * Figure out the number of bytes in the current buffer, and then
1185 * see if we are outside of it...
1188 bytes
= fp
->end
- fp
->buf
;
1194 * Need to seek backwards...
1200 inflateEnd(&fp
->stream
);
1202 lseek(fp
->fd
, 0, SEEK_SET
);
1207 while ((bytes
= cups_fill(fp
)) > 0)
1208 if (pos
>= fp
->pos
&& pos
< (fp
->pos
+ bytes
))
1215 #endif /* HAVE_LIBZ */
1217 fp
->pos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1218 DEBUG_printf((" lseek() returned %ld...\n", (long)fp
->pos
));
1223 else if (pos
>= (fp
->pos
+ bytes
))
1226 * Need to seek forwards...
1230 if (fp
->compressed
|| !fp
->ptr
)
1232 while ((bytes
= cups_fill(fp
)) > 0)
1233 if (pos
>= fp
->pos
&& pos
< (fp
->pos
+ bytes
))
1240 #endif /* HAVE_LIBZ */
1242 fp
->pos
= lseek(fp
->fd
, pos
, SEEK_SET
);
1243 DEBUG_printf((" lseek() returned " CUPS_LLFMT
"...\n", fp
->pos
));
1251 * Just reposition the current pointer, since we have the right
1255 fp
->ptr
= fp
->buf
+ pos
- fp
->pos
;
1256 DEBUG_puts((" seek inside buffer..."));
1264 * 'cupsFileTell()' - Return the current file position.
1267 off_t
/* O - File position */
1268 cupsFileTell(cups_file_t
*fp
) /* I - CUPS file */
1275 * 'cupsFileUnlock()' - Unlock access to a file.
1278 int /* O - 0 on success, -1 on error */
1279 cupsFileUnlock(cups_file_t
*fp
) /* I - File to lock */
1285 if (!fp
|| fp
->mode
== 's')
1293 return (locking(fp
->fd
, _LK_UNLCK
, 0));
1295 return (lockf(fp
->fd
, F_ULOCK
, 0));
1301 * 'cupsFileWrite()' - Write to a file.
1304 ssize_t
/* O - Number of bytes written */
1305 cupsFileWrite(cups_file_t
*fp
, /* I - CUPS file */
1306 const char *buf
, /* I - Buffer */
1307 size_t bytes
) /* I - Number of bytes to write */
1310 * Range check input...
1313 if (!fp
|| !buf
|| bytes
< 0 || (fp
->mode
!= 'w' && fp
->mode
!= 's'))
1320 * Write the buffer...
1323 if (fp
->mode
== 's')
1325 if (cups_write(fp
, buf
, bytes
) < 0)
1333 if ((fp
->ptr
+ bytes
) > fp
->end
)
1334 if (cupsFileFlush(fp
))
1339 if (bytes
> sizeof(fp
->buf
))
1343 return (cups_compress(fp
, buf
, bytes
));
1345 #endif /* HAVE_LIBZ */
1346 return (cups_write(fp
, buf
, bytes
));
1350 memcpy(fp
->ptr
, buf
, bytes
);
1359 * 'cups_compress()' - Compress a buffer of data...
1362 static ssize_t
/* O - Number of bytes written or -1 */
1363 cups_compress(cups_file_t
*fp
, /* I - CUPS file */
1364 const char *buf
, /* I - Buffer */
1365 size_t bytes
) /* I - Number bytes */
1367 DEBUG_printf(("cups_compress(fp=%p, buf=%p, bytes=%ld)\n", fp
, buf
,
1374 fp
->crc
= crc32(fp
->crc
, (const Bytef
*)buf
, bytes
);
1377 * Deflate the bytes...
1380 fp
->stream
.next_in
= (Bytef
*)buf
;
1381 fp
->stream
.avail_in
= bytes
;
1383 while (fp
->stream
.avail_in
> 0)
1386 * Flush the current buffer...
1389 DEBUG_printf((" avail_in=%d, avail_out=%d\n", fp
->stream
.avail_in
,
1390 fp
->stream
.avail_out
));
1392 if (fp
->stream
.avail_out
< (int)(sizeof(fp
->cbuf
) / 8))
1394 if (cups_write(fp
, (char *)fp
->cbuf
, fp
->stream
.next_out
- fp
->cbuf
) < 0)
1397 fp
->stream
.next_out
= fp
->cbuf
;
1398 fp
->stream
.avail_out
= sizeof(fp
->cbuf
);
1401 deflate(&(fp
->stream
), Z_NO_FLUSH
);
1406 #endif /* HAVE_LIBZ */
1410 * 'cups_fill()' - Fill the input buffer...
1413 static ssize_t
/* O - Number of bytes or -1 */
1414 cups_fill(cups_file_t
*fp
) /* I - CUPS file */
1416 ssize_t bytes
; /* Number of bytes read */
1418 const unsigned char *ptr
, /* Pointer into buffer */
1419 *end
; /* End of buffer */
1420 #endif /* HAVE_LIBZ */
1423 DEBUG_printf(("cups_fill(fp=%p)\n", fp
));
1424 DEBUG_printf((" fp->ptr=%p, fp->end=%p, fp->buf=%p, "
1425 "fp->pos=" CUPS_LLFMT
", fp->eof=%d\n",
1426 fp
->ptr
, fp
->end
, fp
->buf
, fp
->pos
, fp
->eof
));
1429 * Update the "pos" element as needed...
1432 if (fp
->ptr
&& fp
->end
)
1433 fp
->pos
+= fp
->end
- fp
->buf
;
1436 DEBUG_printf((" fp->compressed=%d\n", fp
->compressed
));
1438 while (!fp
->ptr
|| fp
->compressed
)
1441 * Check to see if we have read any data yet; if not, see if we have a
1442 * compressed file...
1448 * Reset the file position in case we are seeking...
1454 * Read the first bytes in the file to determine if we have a gzip'd
1458 if ((bytes
= cups_read(fp
, (char *)fp
->buf
, sizeof(fp
->buf
))) < 0)
1461 * Can't read from file!
1464 DEBUG_printf((" cups_read() returned " CUPS_LLFMT
"!\n",
1465 CUPS_LLCAST bytes
));
1470 if (bytes
< 10 || fp
->buf
[0] != 0x1f ||
1471 (fp
->buf
[1] & 255) != 0x8b ||
1472 fp
->buf
[2] != 8 || (fp
->buf
[3] & 0xe0) != 0)
1475 * Not a gzip'd file!
1479 fp
->end
= fp
->buf
+ bytes
;
1481 DEBUG_printf((" returning " CUPS_LLFMT
"!\n", CUPS_LLCAST bytes
));
1487 * Parse header junk: extra data, original name, and comment...
1490 ptr
= (unsigned char *)fp
->buf
+ 10;
1491 end
= (unsigned char *)fp
->buf
+ bytes
;
1493 if (fp
->buf
[3] & 0x04)
1496 * Skip extra data...
1499 if ((ptr
+ 2) > end
)
1502 * Can't read from file!
1508 bytes
= ((unsigned char)ptr
[1] << 8) | (unsigned char)ptr
[0];
1514 * Can't read from file!
1521 if (fp
->buf
[3] & 0x08)
1524 * Skip original name data...
1527 while (ptr
< end
&& *ptr
)
1535 * Can't read from file!
1542 if (fp
->buf
[3] & 0x10)
1545 * Skip comment data...
1548 while (ptr
< end
&& *ptr
)
1556 * Can't read from file!
1563 if (fp
->buf
[3] & 0x02)
1566 * Skip header CRC data...
1574 * Can't read from file!
1582 * Copy the flate-compressed data to the compression buffer...
1585 if ((bytes
= end
- ptr
) > 0)
1586 memcpy(fp
->cbuf
, ptr
, bytes
);
1589 * Setup the decompressor data...
1592 fp
->stream
.zalloc
= (alloc_func
)0;
1593 fp
->stream
.zfree
= (free_func
)0;
1594 fp
->stream
.opaque
= (voidpf
)0;
1595 fp
->stream
.next_in
= (Bytef
*)fp
->cbuf
;
1596 fp
->stream
.next_out
= NULL
;
1597 fp
->stream
.avail_in
= bytes
;
1598 fp
->stream
.avail_out
= 0;
1599 fp
->crc
= crc32(0L, Z_NULL
, 0);
1601 if (inflateInit2(&(fp
->stream
), -15) != Z_OK
)
1610 * If we have reached end-of-file, return immediately...
1617 * Fill the decompression buffer as needed...
1620 if (fp
->stream
.avail_in
== 0)
1622 if ((bytes
= cups_read(fp
, (char *)fp
->cbuf
, sizeof(fp
->cbuf
))) <= 0)
1625 fp
->stream
.next_in
= fp
->cbuf
;
1626 fp
->stream
.avail_in
= bytes
;
1630 * Decompress data from the buffer...
1633 fp
->stream
.next_out
= (Bytef
*)fp
->buf
;
1634 fp
->stream
.avail_out
= sizeof(fp
->buf
);
1636 if (inflate(&(fp
->stream
), Z_NO_FLUSH
) == Z_STREAM_END
)
1639 * Read the CRC and length...
1642 unsigned char trailer
[8]; /* Trailer bytes */
1643 uLong tcrc
; /* Trailer CRC */
1646 if (read(fp
->fd
, trailer
, sizeof(trailer
)) < sizeof(trailer
))
1649 * Can't get it, so mark end-of-file...
1656 tcrc
= (((((trailer
[3] << 8) | trailer
[2]) << 8) | trailer
[1]) << 8) |
1659 if (tcrc
!= fp
->crc
)
1662 * Bad CRC, mark end-of-file...
1671 * Otherwise, reset the compressed flag so that we re-read the
1679 bytes
= sizeof(fp
->buf
) - fp
->stream
.avail_out
;
1682 * Return the decompressed data...
1686 fp
->end
= fp
->buf
+ bytes
;
1692 #endif /* HAVE_LIBZ */
1695 * Read a buffer's full of data...
1698 if ((bytes
= cups_read(fp
, fp
->buf
, sizeof(fp
->buf
))) <= 0)
1701 * Can't read from file!
1712 * Return the bytes we read...
1717 fp
->end
= fp
->buf
+ bytes
;
1724 * 'cups_read()' - Read from a file descriptor.
1727 static ssize_t
/* O - Number of bytes read or -1 */
1728 cups_read(cups_file_t
*fp
, /* I - CUPS file */
1729 char *buf
, /* I - Buffer */
1730 size_t bytes
) /* I - Number bytes */
1732 ssize_t total
; /* Total bytes read */
1736 * Loop until we read at least 0 bytes...
1741 if (fp
->mode
== 's')
1742 total
= recv(fp
->fd
, buf
, bytes
, 0);
1744 total
= read(fp
->fd
, buf
, bytes
);
1750 * Reads can be interrupted by signals and unavailable resources...
1753 if (errno
== EAGAIN
|| errno
== EINTR
)
1760 * Return the total number of bytes read...
1768 * 'cups_write()' - Write to a file descriptor.
1771 static ssize_t
/* O - Number of bytes written or -1 */
1772 cups_write(cups_file_t
*fp
, /* I - CUPS file */
1773 const char *buf
, /* I - Buffer */
1774 size_t bytes
) /* I - Number bytes */
1776 size_t total
, /* Total bytes written */
1777 count
; /* Count this time */
1780 DEBUG_printf(("cups_write(fp=%p, buf=%p, bytes=%ld)\n", fp
, buf
,
1784 * Loop until all bytes are written...
1790 if (fp
->mode
== 's')
1791 count
= send(fp
->fd
, buf
, bytes
, 0);
1793 count
= write(fp
->fd
, buf
, bytes
);
1798 * Writes can be interrupted by signals and unavailable resources...
1801 if (errno
== EAGAIN
|| errno
== EINTR
)
1807 DEBUG_printf((" count=%ld\n", (long)count
));
1810 * Update the counts for the last write call...
1819 * Return the total number of bytes written...
1827 * End of "$Id: file.c 5186 2006-02-26 18:56:05Z mike $".