]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
2e4ff8af | 2 | * "$Id: file.c 6962 2007-09-17 20:35:47Z mike $" |
ef416fc2 | 3 | * |
4 | * File functions for the Common UNIX Printing System (CUPS). | |
5 | * | |
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. | |
10 | * | |
5a738aea | 11 | * Copyright 2007-2008 by Apple Inc. |
b86bc4cf | 12 | * Copyright 1997-2007 by Easy Software Products, all rights reserved. |
ef416fc2 | 13 | * |
14 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 15 | * property of Apple Inc. and are protected by Federal copyright |
16 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
17 | * which should have been included with this file. If this file is | |
18 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ef416fc2 | 19 | * |
20 | * Contents: | |
21 | * | |
22 | * cupsFileClose() - Close a CUPS file. | |
23 | * cupsFileCompression() - Return whether a file is compressed. | |
24 | * cupsFileEOF() - Return the end-of-file status. | |
fa73b229 | 25 | * cupsFileFind() - Find a file using the specified path. |
ef416fc2 | 26 | * cupsFileFlush() - Flush pending output. |
27 | * cupsFileGetChar() - Get a single character from a file. | |
28 | * cupsFileGetConf() - Get a line from a configuration file... | |
5a738aea MS |
29 | * cupsFileGetLine() - Get a CR and/or LF-terminated line that may contain |
30 | * binary data. | |
ef416fc2 | 31 | * cupsFileGets() - Get a CR and/or LF-terminated line. |
32 | * cupsFileLock() - Temporarily lock access to a file. | |
80ca4592 | 33 | * cupsFileNumber() - Return the file descriptor associated with a CUPS |
34 | * file. | |
ef416fc2 | 35 | * cupsFileOpen() - Open a CUPS file. |
36 | * cupsFileOpenFd() - Open a CUPS file using a file descriptor. | |
37 | * cupsFilePeekChar() - Peek at the next character from a file. | |
38 | * cupsFilePrintf() - Write a formatted string. | |
39 | * cupsFilePutChar() - Write a character. | |
40 | * cupsFilePuts() - Write a string. | |
41 | * cupsFileRead() - Read from a file. | |
5a738aea MS |
42 | * cupsFileRewind() - Set the current file position to the beginning of |
43 | * the file. | |
ef416fc2 | 44 | * cupsFileSeek() - Seek in a file. |
80ca4592 | 45 | * cupsFileStderr() - Return a CUPS file associated with stderr. |
46 | * cupsFileStdin() - Return a CUPS file associated with stdin. | |
47 | * cupsFileStdout() - Return a CUPS file associated with stdout. | |
ef416fc2 | 48 | * cupsFileTell() - Return the current file position. |
49 | * cupsFileUnlock() - Unlock access to a file. | |
50 | * cupsFileWrite() - Write to a file. | |
51 | * cups_compress() - Compress a buffer of data... | |
52 | * cups_fill() - Fill the input buffer... | |
53 | * cups_read() - Read from a file descriptor. | |
54 | * cups_write() - Write to a file descriptor. | |
55 | */ | |
56 | ||
57 | /* | |
58 | * Include necessary headers... | |
59 | */ | |
60 | ||
61 | #include <stdio.h> | |
62 | #include <stdlib.h> | |
63 | #include <stdarg.h> | |
ef416fc2 | 64 | #include <errno.h> |
ef416fc2 | 65 | #include <sys/types.h> |
66 | #include <fcntl.h> | |
80ca4592 | 67 | #include "http-private.h" |
68 | #include "globals.h" | |
69 | #include "debug.h" | |
ef416fc2 | 70 | |
ef416fc2 | 71 | #ifdef HAVE_LIBZ |
72 | # include <zlib.h> | |
73 | #endif /* HAVE_LIBZ */ | |
74 | #ifdef WIN32 | |
75 | # include <io.h> | |
76 | # include <sys/locking.h> | |
77 | #endif /* WIN32 */ | |
78 | ||
79 | ||
80 | /* | |
81 | * Some operating systems support large files via open flag O_LARGEFILE... | |
82 | */ | |
83 | ||
84 | #ifndef O_LARGEFILE | |
85 | # define O_LARGEFILE 0 | |
86 | #endif /* !O_LARGEFILE */ | |
87 | ||
88 | ||
b86bc4cf | 89 | /* |
90 | * Some operating systems don't define O_BINARY, which is used by Microsoft | |
91 | * and IBM to flag binary files... | |
92 | */ | |
93 | ||
94 | #ifndef O_BINARY | |
95 | # define O_BINARY 0 | |
96 | #endif /* !O_BINARY */ | |
97 | ||
98 | ||
ef416fc2 | 99 | /* |
100 | * Types and structures... | |
101 | */ | |
102 | ||
103 | struct _cups_file_s /**** CUPS file structure... ****/ | |
104 | ||
105 | { | |
106 | int fd; /* File descriptor */ | |
107 | char mode, /* Mode ('r' or 'w') */ | |
108 | compressed, /* Compression used? */ | |
80ca4592 | 109 | is_stdio, /* stdin/out/err? */ |
ef416fc2 | 110 | eof, /* End of file? */ |
fa73b229 | 111 | buf[4096], /* Buffer */ |
ef416fc2 | 112 | *ptr, /* Pointer into buffer */ |
113 | *end; /* End of buffer data */ | |
634763e8 MS |
114 | off_t pos, /* Position in file */ |
115 | bufpos; /* File position for start of buffer */ | |
ef416fc2 | 116 | |
117 | #ifdef HAVE_LIBZ | |
118 | z_stream stream; /* (De)compression stream */ | |
fa73b229 | 119 | Bytef cbuf[4096]; /* (De)compression buffer */ |
ef416fc2 | 120 | uLong crc; /* (De)compression CRC */ |
121 | #endif /* HAVE_LIBZ */ | |
122 | }; | |
123 | ||
124 | ||
125 | /* | |
126 | * Local functions... | |
127 | */ | |
128 | ||
129 | #ifdef HAVE_LIBZ | |
130 | static ssize_t cups_compress(cups_file_t *fp, const char *buf, size_t bytes); | |
131 | #endif /* HAVE_LIBZ */ | |
132 | static ssize_t cups_fill(cups_file_t *fp); | |
133 | static ssize_t cups_read(cups_file_t *fp, char *buf, size_t bytes); | |
134 | static ssize_t cups_write(cups_file_t *fp, const char *buf, size_t bytes); | |
135 | ||
136 | ||
137 | /* | |
138 | * 'cupsFileClose()' - Close a CUPS file. | |
5a738aea MS |
139 | * |
140 | * @since CUPS 1.2@ | |
ef416fc2 | 141 | */ |
142 | ||
143 | int /* O - 0 on success, -1 on error */ | |
144 | cupsFileClose(cups_file_t *fp) /* I - CUPS file */ | |
145 | { | |
146 | int fd; /* File descriptor */ | |
147 | char mode; /* Open mode */ | |
148 | int status; /* Return status */ | |
80ca4592 | 149 | int is_stdio; /* Is a stdio file? */ |
ef416fc2 | 150 | |
151 | ||
152 | DEBUG_printf(("cupsFileClose(fp=%p)\n", fp)); | |
153 | ||
154 | /* | |
155 | * Range check... | |
156 | */ | |
157 | ||
158 | if (!fp) | |
159 | return (-1); | |
160 | ||
161 | /* | |
162 | * Flush pending write data... | |
163 | */ | |
164 | ||
165 | if (fp->mode == 'w') | |
166 | status = cupsFileFlush(fp); | |
167 | else | |
168 | status = 0; | |
169 | ||
170 | #ifdef HAVE_LIBZ | |
171 | if (fp->compressed && status >= 0) | |
172 | { | |
173 | if (fp->mode == 'r') | |
174 | { | |
175 | /* | |
176 | * Free decompression data... | |
177 | */ | |
178 | ||
179 | inflateEnd(&fp->stream); | |
180 | } | |
181 | else | |
182 | { | |
183 | /* | |
184 | * Flush any remaining compressed data... | |
185 | */ | |
186 | ||
187 | unsigned char trailer[8]; /* Trailer CRC and length */ | |
188 | int done; /* Done writing... */ | |
189 | ||
190 | ||
191 | fp->stream.avail_in = 0; | |
192 | ||
193 | for (done = 0;;) | |
194 | { | |
195 | if (fp->stream.next_out > fp->cbuf) | |
196 | { | |
197 | if (cups_write(fp, (char *)fp->cbuf, | |
198 | fp->stream.next_out - fp->cbuf) < 0) | |
199 | status = -1; | |
200 | ||
201 | fp->stream.next_out = fp->cbuf; | |
202 | fp->stream.avail_out = sizeof(fp->cbuf); | |
203 | } | |
204 | ||
205 | if (done || status < 0) | |
206 | break; | |
207 | ||
208 | done = deflate(&fp->stream, Z_FINISH) == Z_STREAM_END && | |
209 | fp->stream.next_out == fp->cbuf; | |
210 | } | |
211 | ||
212 | /* | |
213 | * Write the CRC and length... | |
214 | */ | |
215 | ||
216 | trailer[0] = fp->crc; | |
217 | trailer[1] = fp->crc >> 8; | |
218 | trailer[2] = fp->crc >> 16; | |
219 | trailer[3] = fp->crc >> 24; | |
220 | trailer[4] = fp->pos; | |
221 | trailer[5] = fp->pos >> 8; | |
222 | trailer[6] = fp->pos >> 16; | |
223 | trailer[7] = fp->pos >> 24; | |
224 | ||
225 | if (cups_write(fp, (char *)trailer, 8) < 0) | |
226 | status = -1; | |
227 | ||
228 | /* | |
229 | * Free all memory used by the compression stream... | |
230 | */ | |
231 | ||
232 | deflateEnd(&(fp->stream)); | |
233 | } | |
234 | } | |
235 | #endif /* HAVE_LIBZ */ | |
236 | ||
237 | /* | |
238 | * Save the file descriptor we used and free memory... | |
239 | */ | |
240 | ||
80ca4592 | 241 | fd = fp->fd; |
242 | mode = fp->mode; | |
243 | is_stdio = fp->is_stdio; | |
ef416fc2 | 244 | |
245 | free(fp); | |
246 | ||
247 | /* | |
248 | * Close the file, returning the close status... | |
249 | */ | |
250 | ||
251 | if (mode == 's') | |
252 | { | |
253 | if (closesocket(fd) < 0) | |
254 | status = -1; | |
255 | } | |
80ca4592 | 256 | else if (!is_stdio) |
ef416fc2 | 257 | { |
258 | if (close(fd) < 0) | |
259 | status = -1; | |
260 | } | |
261 | ||
262 | return (status); | |
263 | } | |
264 | ||
265 | ||
266 | /* | |
267 | * 'cupsFileCompression()' - Return whether a file is compressed. | |
5a738aea MS |
268 | * |
269 | * @since CUPS 1.2@ | |
ef416fc2 | 270 | */ |
271 | ||
5a738aea | 272 | int /* O - @code CUPS_FILE_NONE@ or @code CUPS_FILE_GZIP@ */ |
ef416fc2 | 273 | cupsFileCompression(cups_file_t *fp) /* I - CUPS file */ |
274 | { | |
80ca4592 | 275 | return (fp ? fp->compressed : CUPS_FILE_NONE); |
ef416fc2 | 276 | } |
277 | ||
278 | ||
279 | /* | |
280 | * 'cupsFileEOF()' - Return the end-of-file status. | |
5a738aea MS |
281 | * |
282 | * @since CUPS 1.2@ | |
ef416fc2 | 283 | */ |
284 | ||
5a738aea | 285 | int /* O - 1 on end of file, 0 otherwise */ |
ef416fc2 | 286 | cupsFileEOF(cups_file_t *fp) /* I - CUPS file */ |
287 | { | |
80ca4592 | 288 | return (fp ? fp->eof : 1); |
ef416fc2 | 289 | } |
290 | ||
291 | ||
fa73b229 | 292 | /* |
293 | * 'cupsFileFind()' - Find a file using the specified path. | |
294 | * | |
295 | * This function allows the paths in the path string to be separated by | |
296 | * colons (UNIX standard) or semicolons (Windows standard) and stores the | |
297 | * result in the buffer supplied. If the file cannot be found in any of | |
5a738aea MS |
298 | * the supplied paths, @code NULL@ is returned. A @code NULL@ path only |
299 | * matches the current directory. | |
300 | * | |
301 | * @since CUPS 1.2@ | |
fa73b229 | 302 | */ |
303 | ||
5a738aea | 304 | const char * /* O - Full path to file or @code NULL@ if not found */ |
fa73b229 | 305 | cupsFileFind(const char *filename, /* I - File to find */ |
306 | const char *path, /* I - Colon/semicolon-separated path */ | |
4400e98d | 307 | int executable, /* I - 1 = executable files, 0 = any file/dir */ |
308 | char *buffer, /* I - Filename buffer */ | |
fa73b229 | 309 | int bufsize) /* I - Size of filename buffer */ |
310 | { | |
311 | char *bufptr, /* Current position in buffer */ | |
312 | *bufend; /* End of buffer */ | |
313 | ||
314 | ||
315 | /* | |
316 | * Range check input... | |
317 | */ | |
318 | ||
319 | if (!filename || !buffer || bufsize < 2) | |
320 | return (NULL); | |
321 | ||
322 | if (!path) | |
323 | { | |
324 | /* | |
325 | * No path, so check current directory... | |
326 | */ | |
327 | ||
328 | if (!access(filename, 0)) | |
329 | { | |
330 | strlcpy(buffer, filename, bufsize); | |
331 | return (buffer); | |
332 | } | |
333 | else | |
334 | return (NULL); | |
335 | } | |
336 | ||
337 | /* | |
338 | * Now check each path and return the first match... | |
339 | */ | |
340 | ||
341 | bufend = buffer + bufsize - 1; | |
342 | bufptr = buffer; | |
343 | ||
344 | while (*path) | |
345 | { | |
b86bc4cf | 346 | #ifdef WIN32 |
347 | if (*path == ';' || (*path == ':' && ((bufptr - buffer) > 1 || !isalpha(buffer[0] & 255)))) | |
348 | #else | |
fa73b229 | 349 | if (*path == ';' || *path == ':') |
b86bc4cf | 350 | #endif /* WIN32 */ |
fa73b229 | 351 | { |
352 | if (bufptr > buffer && bufptr[-1] != '/' && bufptr < bufend) | |
353 | *bufptr++ = '/'; | |
354 | ||
355 | strlcpy(bufptr, filename, bufend - bufptr); | |
356 | ||
4400e98d | 357 | #ifdef WIN32 |
fa73b229 | 358 | if (!access(buffer, 0)) |
4400e98d | 359 | #else |
360 | if (!access(buffer, executable ? X_OK : 0)) | |
361 | #endif /* WIN32 */ | |
b86bc4cf | 362 | { |
363 | DEBUG_printf(("cupsFileFind: Returning \"%s\"\n", buffer)); | |
fa73b229 | 364 | return (buffer); |
b86bc4cf | 365 | } |
fa73b229 | 366 | |
367 | bufptr = buffer; | |
368 | } | |
369 | else if (bufptr < bufend) | |
370 | *bufptr++ = *path; | |
371 | ||
372 | path ++; | |
373 | } | |
374 | ||
375 | /* | |
376 | * Check the last path... | |
377 | */ | |
378 | ||
379 | if (bufptr > buffer && bufptr[-1] != '/' && bufptr < bufend) | |
380 | *bufptr++ = '/'; | |
381 | ||
382 | strlcpy(bufptr, filename, bufend - bufptr); | |
383 | ||
384 | if (!access(buffer, 0)) | |
b86bc4cf | 385 | { |
386 | DEBUG_printf(("cupsFileFind: Returning \"%s\"\n", buffer)); | |
fa73b229 | 387 | return (buffer); |
b86bc4cf | 388 | } |
fa73b229 | 389 | else |
b86bc4cf | 390 | { |
391 | DEBUG_puts("cupsFileFind: Returning NULL"); | |
fa73b229 | 392 | return (NULL); |
b86bc4cf | 393 | } |
fa73b229 | 394 | } |
395 | ||
396 | ||
ef416fc2 | 397 | /* |
398 | * 'cupsFileFlush()' - Flush pending output. | |
5a738aea MS |
399 | * |
400 | * @since CUPS 1.2@ | |
ef416fc2 | 401 | */ |
402 | ||
403 | int /* O - 0 on success, -1 on error */ | |
404 | cupsFileFlush(cups_file_t *fp) /* I - CUPS file */ | |
405 | { | |
2abf387c | 406 | ssize_t bytes; /* Bytes to write */ |
ef416fc2 | 407 | |
408 | ||
409 | DEBUG_printf(("cupsFileFlush(fp=%p)\n", fp)); | |
410 | ||
411 | /* | |
412 | * Range check input... | |
413 | */ | |
414 | ||
415 | if (!fp || fp->mode != 'w') | |
416 | { | |
634763e8 | 417 | DEBUG_puts("cupsFileFlush: Attempt to flush a read-only file..."); |
ef416fc2 | 418 | return (-1); |
419 | } | |
420 | ||
b86bc4cf | 421 | bytes = (ssize_t)(fp->ptr - fp->buf); |
ef416fc2 | 422 | |
634763e8 MS |
423 | DEBUG_printf(("cupsFileFlush: Flushing " CUPS_LLFMT " bytes...\n", |
424 | CUPS_LLCAST bytes)); | |
ecdc0628 | 425 | |
ef416fc2 | 426 | if (bytes > 0) |
427 | { | |
428 | #ifdef HAVE_LIBZ | |
429 | if (fp->compressed) | |
430 | bytes = cups_compress(fp, fp->buf, bytes); | |
431 | else | |
432 | #endif /* HAVE_LIBZ */ | |
433 | bytes = cups_write(fp, fp->buf, bytes); | |
434 | ||
435 | if (bytes < 0) | |
436 | return (-1); | |
437 | ||
438 | fp->ptr = fp->buf; | |
439 | } | |
440 | ||
441 | return (0); | |
442 | } | |
443 | ||
444 | ||
445 | /* | |
446 | * 'cupsFileGetChar()' - Get a single character from a file. | |
5a738aea MS |
447 | * |
448 | * @since CUPS 1.2@ | |
ef416fc2 | 449 | */ |
450 | ||
5a738aea | 451 | int /* O - Character or -1 on end of file */ |
ef416fc2 | 452 | cupsFileGetChar(cups_file_t *fp) /* I - CUPS file */ |
453 | { | |
454 | /* | |
455 | * Range check input... | |
456 | */ | |
457 | ||
634763e8 MS |
458 | DEBUG_printf(("cupsFileGetChar(fp=%p)\n", fp)); |
459 | ||
ef416fc2 | 460 | if (!fp || (fp->mode != 'r' && fp->mode != 's')) |
b86bc4cf | 461 | { |
462 | DEBUG_puts("cupsFileGetChar: Bad arguments!"); | |
ef416fc2 | 463 | return (-1); |
b86bc4cf | 464 | } |
ef416fc2 | 465 | |
466 | /* | |
467 | * If the input buffer is empty, try to read more data... | |
468 | */ | |
469 | ||
470 | if (fp->ptr >= fp->end) | |
471 | if (cups_fill(fp) < 0) | |
b86bc4cf | 472 | { |
473 | DEBUG_puts("cupsFileGetChar: Unable to fill buffer!"); | |
ef416fc2 | 474 | return (-1); |
b86bc4cf | 475 | } |
ef416fc2 | 476 | |
477 | /* | |
478 | * Return the next character in the buffer... | |
479 | */ | |
480 | ||
b86bc4cf | 481 | DEBUG_printf(("cupsFileGetChar: Returning %d...\n", *(fp->ptr) & 255)); |
482 | ||
634763e8 MS |
483 | fp->pos ++; |
484 | ||
485 | DEBUG_printf(("cupsFileGetChar: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); | |
486 | ||
ef416fc2 | 487 | return (*(fp->ptr)++ & 255); |
488 | } | |
489 | ||
490 | ||
491 | /* | |
492 | * 'cupsFileGetConf()' - Get a line from a configuration file... | |
5a738aea MS |
493 | * |
494 | * @since CUPS 1.2@ | |
ef416fc2 | 495 | */ |
496 | ||
5a738aea | 497 | char * /* O - Line read or @code NULL@ on end of file or error */ |
ef416fc2 | 498 | cupsFileGetConf(cups_file_t *fp, /* I - CUPS file */ |
499 | char *buf, /* O - String buffer */ | |
500 | size_t buflen, /* I - Size of string buffer */ | |
501 | char **value, /* O - Pointer to value */ | |
502 | int *linenum) /* IO - Current line number */ | |
503 | { | |
504 | char *ptr; /* Pointer into line */ | |
505 | ||
506 | ||
507 | /* | |
508 | * Range check input... | |
509 | */ | |
510 | ||
634763e8 MS |
511 | DEBUG_printf(("cupsFileGetConf(fp=%p, buf=%p, buflen=" CUPS_LLFMT |
512 | ", value=%p, linenum=%p)\n", fp, buf, CUPS_LLCAST buflen, | |
513 | value, linenum)); | |
514 | ||
ef416fc2 | 515 | if (!fp || (fp->mode != 'r' && fp->mode != 's') || |
516 | !buf || buflen < 2 || !value) | |
517 | { | |
518 | if (value) | |
519 | *value = NULL; | |
520 | ||
521 | return (NULL); | |
522 | } | |
523 | ||
524 | /* | |
525 | * Read the next non-comment line... | |
526 | */ | |
527 | ||
528 | *value = NULL; | |
f7deaa1a | 529 | |
ef416fc2 | 530 | while (cupsFileGets(fp, buf, buflen)) |
531 | { | |
532 | (*linenum) ++; | |
533 | ||
534 | /* | |
535 | * Strip any comments... | |
536 | */ | |
537 | ||
538 | if ((ptr = strchr(buf, '#')) != NULL) | |
539 | { | |
f7deaa1a | 540 | if (ptr > buf && ptr[-1] == '\\') |
ef416fc2 | 541 | { |
f7deaa1a | 542 | // Unquote the #... |
543 | _cups_strcpy(ptr - 1, ptr); | |
ef416fc2 | 544 | } |
f7deaa1a | 545 | else |
546 | { | |
547 | // Strip the comment and any trailing whitespace... | |
548 | while (ptr > buf) | |
549 | { | |
550 | if (!isspace(ptr[-1] & 255)) | |
551 | break; | |
552 | ||
553 | ptr --; | |
554 | } | |
ef416fc2 | 555 | |
f7deaa1a | 556 | *ptr = '\0'; |
557 | } | |
ef416fc2 | 558 | } |
559 | ||
560 | /* | |
561 | * Strip leading whitespace... | |
562 | */ | |
563 | ||
564 | for (ptr = buf; isspace(*ptr & 255); ptr ++); | |
565 | ||
566 | if (ptr > buf) | |
567 | _cups_strcpy(buf, ptr); | |
568 | ||
569 | /* | |
570 | * See if there is anything left... | |
571 | */ | |
572 | ||
573 | if (buf[0]) | |
574 | { | |
575 | /* | |
576 | * Yes, grab any value and return... | |
577 | */ | |
578 | ||
579 | for (ptr = buf; *ptr; ptr ++) | |
580 | if (isspace(*ptr & 255)) | |
581 | break; | |
582 | ||
583 | if (*ptr) | |
584 | { | |
585 | /* | |
586 | * Have a value, skip any other spaces... | |
587 | */ | |
588 | ||
589 | while (isspace(*ptr & 255)) | |
590 | *ptr++ = '\0'; | |
591 | ||
592 | if (*ptr) | |
593 | *value = ptr; | |
594 | ||
595 | /* | |
596 | * Strip trailing whitespace and > for lines that begin with <... | |
597 | */ | |
598 | ||
599 | ptr += strlen(ptr) - 1; | |
600 | ||
601 | if (buf[0] == '<' && *ptr == '>') | |
602 | *ptr-- = '\0'; | |
603 | else if (buf[0] == '<' && *ptr != '>') | |
604 | { | |
605 | /* | |
606 | * Syntax error... | |
607 | */ | |
608 | ||
609 | *value = NULL; | |
610 | return (buf); | |
611 | } | |
612 | ||
613 | while (ptr > *value && isspace(*ptr & 255)) | |
614 | *ptr-- = '\0'; | |
615 | } | |
616 | ||
617 | /* | |
618 | * Return the line... | |
619 | */ | |
620 | ||
621 | return (buf); | |
622 | } | |
623 | } | |
624 | ||
625 | return (NULL); | |
626 | } | |
627 | ||
628 | ||
80ca4592 | 629 | /* |
630 | * 'cupsFileGetLine()' - Get a CR and/or LF-terminated line that may | |
631 | * contain binary data. | |
632 | * | |
5a738aea MS |
633 | * This function differs from @link cupsFileGets@ in that the trailing CR |
634 | * and LF are preserved, as is any binary data on the line. The buffer is | |
635 | * nul-terminated, however you should use the returned length to determine | |
80ca4592 | 636 | * the number of bytes on the line. |
5a738aea MS |
637 | * |
638 | * @since CUPS 1.2@ | |
80ca4592 | 639 | */ |
640 | ||
5a738aea | 641 | size_t /* O - Number of bytes on line or 0 on end of file */ |
80ca4592 | 642 | cupsFileGetLine(cups_file_t *fp, /* I - File to read from */ |
643 | char *buf, /* I - Buffer */ | |
644 | size_t buflen) /* I - Size of buffer */ | |
645 | { | |
646 | int ch; /* Character from file */ | |
647 | char *ptr, /* Current position in line buffer */ | |
648 | *end; /* End of line buffer */ | |
649 | ||
650 | ||
651 | /* | |
652 | * Range check input... | |
653 | */ | |
654 | ||
634763e8 MS |
655 | DEBUG_printf(("cupsFileGetLine(fp=%p, buf=%p, buflen=" CUPS_LLFMT ")\n", |
656 | fp, buf, CUPS_LLCAST buflen)); | |
657 | ||
80ca4592 | 658 | if (!fp || (fp->mode != 'r' && fp->mode != 's') || !buf || buflen < 3) |
659 | return (0); | |
660 | ||
661 | /* | |
662 | * Now loop until we have a valid line... | |
663 | */ | |
664 | ||
665 | for (ptr = buf, end = buf + buflen - 2; ptr < end ;) | |
666 | { | |
667 | if (fp->ptr >= fp->end) | |
668 | if (cups_fill(fp) <= 0) | |
669 | break; | |
670 | ||
671 | *ptr++ = ch = *(fp->ptr)++; | |
634763e8 | 672 | fp->pos ++; |
80ca4592 | 673 | |
674 | if (ch == '\r') | |
675 | { | |
676 | /* | |
677 | * Check for CR LF... | |
678 | */ | |
679 | ||
680 | if (fp->ptr >= fp->end) | |
681 | if (cups_fill(fp) <= 0) | |
682 | break; | |
683 | ||
684 | if (*(fp->ptr) == '\n') | |
634763e8 | 685 | { |
80ca4592 | 686 | *ptr++ = *(fp->ptr)++; |
634763e8 MS |
687 | fp->pos ++; |
688 | } | |
80ca4592 | 689 | |
690 | break; | |
691 | } | |
692 | else if (ch == '\n') | |
693 | { | |
694 | /* | |
695 | * Line feed ends a line... | |
696 | */ | |
697 | ||
698 | break; | |
699 | } | |
700 | } | |
701 | ||
702 | *ptr = '\0'; | |
703 | ||
634763e8 MS |
704 | DEBUG_printf(("cupsFileGetLine: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); |
705 | ||
80ca4592 | 706 | return (ptr - buf); |
707 | } | |
708 | ||
709 | ||
ef416fc2 | 710 | /* |
711 | * 'cupsFileGets()' - Get a CR and/or LF-terminated line. | |
5a738aea MS |
712 | * |
713 | * @since CUPS 1.2@ | |
ef416fc2 | 714 | */ |
715 | ||
5a738aea | 716 | char * /* O - Line read or @code NULL@ on end of file or error */ |
ef416fc2 | 717 | cupsFileGets(cups_file_t *fp, /* I - CUPS file */ |
718 | char *buf, /* O - String buffer */ | |
719 | size_t buflen) /* I - Size of string buffer */ | |
720 | { | |
721 | int ch; /* Character from file */ | |
722 | char *ptr, /* Current position in line buffer */ | |
723 | *end; /* End of line buffer */ | |
724 | ||
725 | ||
726 | /* | |
727 | * Range check input... | |
728 | */ | |
729 | ||
634763e8 MS |
730 | DEBUG_printf(("cupsFileGets(fp=%p, buf=%p, buflen=" CUPS_LLFMT ")\n", fp, buf, |
731 | CUPS_LLCAST buflen)); | |
732 | ||
ef416fc2 | 733 | if (!fp || (fp->mode != 'r' && fp->mode != 's') || !buf || buflen < 2) |
734 | return (NULL); | |
735 | ||
736 | /* | |
737 | * Now loop until we have a valid line... | |
738 | */ | |
739 | ||
740 | for (ptr = buf, end = buf + buflen - 1; ptr < end ;) | |
741 | { | |
742 | if (fp->ptr >= fp->end) | |
743 | if (cups_fill(fp) <= 0) | |
744 | { | |
745 | if (ptr == buf) | |
746 | return (NULL); | |
747 | else | |
748 | break; | |
749 | } | |
750 | ||
751 | ch = *(fp->ptr)++; | |
634763e8 | 752 | fp->pos ++; |
ef416fc2 | 753 | |
754 | if (ch == '\r') | |
755 | { | |
756 | /* | |
757 | * Check for CR LF... | |
758 | */ | |
759 | ||
760 | if (fp->ptr >= fp->end) | |
761 | if (cups_fill(fp) <= 0) | |
762 | break; | |
763 | ||
764 | if (*(fp->ptr) == '\n') | |
634763e8 MS |
765 | { |
766 | fp->ptr ++; | |
767 | fp->pos ++; | |
768 | } | |
ef416fc2 | 769 | |
770 | break; | |
771 | } | |
772 | else if (ch == '\n') | |
773 | { | |
774 | /* | |
775 | * Line feed ends a line... | |
776 | */ | |
777 | ||
778 | break; | |
779 | } | |
780 | else | |
781 | *ptr++ = ch; | |
782 | } | |
783 | ||
784 | *ptr = '\0'; | |
785 | ||
634763e8 MS |
786 | DEBUG_printf(("cupsFileGets: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); |
787 | ||
ef416fc2 | 788 | return (buf); |
789 | } | |
790 | ||
791 | ||
792 | /* | |
793 | * 'cupsFileLock()' - Temporarily lock access to a file. | |
5a738aea MS |
794 | * |
795 | * @since CUPS 1.2@ | |
ef416fc2 | 796 | */ |
797 | ||
798 | int /* O - 0 on success, -1 on error */ | |
5a738aea | 799 | cupsFileLock(cups_file_t *fp, /* I - CUPS file */ |
ef416fc2 | 800 | int block) /* I - 1 to wait for the lock, 0 to fail right away */ |
801 | { | |
802 | /* | |
803 | * Range check... | |
804 | */ | |
805 | ||
806 | if (!fp || fp->mode == 's') | |
807 | return (-1); | |
808 | ||
809 | /* | |
810 | * Try the lock... | |
811 | */ | |
812 | ||
813 | #ifdef WIN32 | |
814 | return (locking(fp->fd, block ? _LK_LOCK : _LK_NBLCK, 0)); | |
815 | #else | |
816 | return (lockf(fp->fd, block ? F_LOCK : F_TLOCK, 0)); | |
817 | #endif /* WIN32 */ | |
818 | } | |
819 | ||
820 | ||
821 | /* | |
822 | * 'cupsFileNumber()' - Return the file descriptor associated with a CUPS file. | |
5a738aea MS |
823 | * |
824 | * @since CUPS 1.2@ | |
ef416fc2 | 825 | */ |
826 | ||
827 | int /* O - File descriptor */ | |
828 | cupsFileNumber(cups_file_t *fp) /* I - CUPS file */ | |
829 | { | |
5a738aea MS |
830 | if (fp) |
831 | return (fp->fd); | |
832 | else | |
833 | return (-1); | |
ef416fc2 | 834 | } |
835 | ||
836 | ||
837 | /* | |
838 | * 'cupsFileOpen()' - Open a CUPS file. | |
5a738aea MS |
839 | * |
840 | * The "mode" parameter can be "r" to read, "w" to write, overwriting any | |
841 | * existing file, "a" to append to an existing file or create a new file, | |
842 | * or "s" to open a socket connection. | |
843 | * | |
634763e8 MS |
844 | * When opening for writing ("w"), an optional number from 1 to 9 can be |
845 | * supplied which enables Flate compression of the file. Compression is | |
846 | * not supported for the "a" (append) mode. | |
5a738aea MS |
847 | * |
848 | * When opening a socket connection, the filename is a string of the form | |
849 | * "address:port" or "hostname:port". The socket will make an IPv4 or IPv6 | |
850 | * connection as needed, generally preferring IPv6 connections when there is | |
851 | * a choice. | |
852 | * | |
853 | * @since CUPS 1.2@ | |
ef416fc2 | 854 | */ |
855 | ||
5a738aea | 856 | cups_file_t * /* O - CUPS file or @code NULL@ if the file or socket cannot be opened */ |
ef416fc2 | 857 | cupsFileOpen(const char *filename, /* I - Name of file */ |
858 | const char *mode) /* I - Open mode */ | |
859 | { | |
860 | cups_file_t *fp; /* New CUPS file */ | |
861 | int fd; /* File descriptor */ | |
862 | char hostname[1024], /* Hostname */ | |
863 | *portname; /* Port "name" (number or service) */ | |
864 | http_addrlist_t *addrlist; /* Host address list */ | |
865 | ||
866 | ||
b423cd4c | 867 | DEBUG_printf(("cupsFileOpen(filename=\"%s\", mode=\"%s\")\n", filename, |
868 | mode)); | |
869 | ||
ef416fc2 | 870 | /* |
871 | * Range check input... | |
872 | */ | |
873 | ||
874 | if (!filename || !mode || | |
634763e8 MS |
875 | (*mode != 'r' && *mode != 'w' && *mode != 'a' && *mode != 's') || |
876 | (*mode == 'a' && isdigit(mode[1] & 255))) | |
ef416fc2 | 877 | return (NULL); |
878 | ||
879 | /* | |
880 | * Open the file... | |
881 | */ | |
882 | ||
883 | switch (*mode) | |
884 | { | |
885 | case 'a' : /* Append file */ | |
b86bc4cf | 886 | fd = open(filename, O_RDWR | O_CREAT | O_APPEND | O_LARGEFILE | O_BINARY, 0666); |
ef416fc2 | 887 | break; |
888 | ||
889 | case 'r' : /* Read file */ | |
b86bc4cf | 890 | fd = open(filename, O_RDONLY | O_LARGEFILE | O_BINARY, 0); |
ef416fc2 | 891 | break; |
892 | ||
893 | case 'w' : /* Write file */ | |
b86bc4cf | 894 | fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT | O_LARGEFILE | O_BINARY, 0666); |
ef416fc2 | 895 | break; |
896 | ||
897 | case 's' : /* Read/write socket */ | |
898 | strlcpy(hostname, filename, sizeof(hostname)); | |
899 | if ((portname = strrchr(hostname, ':')) != NULL) | |
900 | *portname++ = '\0'; | |
901 | else | |
902 | return (NULL); | |
903 | ||
904 | /* | |
905 | * Lookup the hostname and service... | |
906 | */ | |
907 | ||
908 | if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, portname)) == NULL) | |
909 | return (NULL); | |
910 | ||
911 | /* | |
912 | * Connect to the server... | |
913 | */ | |
914 | ||
915 | if (!httpAddrConnect(addrlist, &fd)) | |
916 | { | |
917 | httpAddrFreeList(addrlist); | |
918 | return (NULL); | |
919 | } | |
920 | ||
921 | httpAddrFreeList(addrlist); | |
922 | break; | |
923 | ||
924 | default : /* Remove bogus compiler warning... */ | |
925 | return (NULL); | |
926 | } | |
927 | ||
928 | if (fd < 0) | |
929 | return (NULL); | |
930 | ||
931 | /* | |
932 | * Create the CUPS file structure... | |
933 | */ | |
934 | ||
935 | if ((fp = cupsFileOpenFd(fd, mode)) == NULL) | |
936 | { | |
937 | if (*mode == 's') | |
938 | closesocket(fd); | |
939 | else | |
940 | close(fd); | |
941 | } | |
942 | ||
943 | /* | |
944 | * Return it... | |
945 | */ | |
946 | ||
947 | return (fp); | |
948 | } | |
949 | ||
950 | /* | |
951 | * 'cupsFileOpenFd()' - Open a CUPS file using a file descriptor. | |
5a738aea | 952 | * |
634763e8 MS |
953 | * The "mode" parameter can be "r" to read, "w" to write, "a" to append, |
954 | * or "s" to treat the file descriptor as a bidirectional socket connection. | |
5a738aea | 955 | * |
634763e8 MS |
956 | * When opening for writing ("w"), an optional number from 1 to 9 can be |
957 | * supplied which enables Flate compression of the file. Compression is | |
958 | * not supported for the "a" (append) mode. | |
5a738aea MS |
959 | * |
960 | * @since CUPS 1.2@ | |
ef416fc2 | 961 | */ |
962 | ||
5a738aea | 963 | cups_file_t * /* O - CUPS file or @code NULL@ if the file could not be opened */ |
ef416fc2 | 964 | cupsFileOpenFd(int fd, /* I - File descriptor */ |
965 | const char *mode) /* I - Open mode */ | |
966 | { | |
967 | cups_file_t *fp; /* New CUPS file */ | |
968 | ||
969 | ||
b423cd4c | 970 | DEBUG_printf(("cupsFileOpenFd(fd=%d, mode=\"%s\")\n", fd, mode)); |
971 | ||
ef416fc2 | 972 | /* |
973 | * Range check input... | |
974 | */ | |
975 | ||
976 | if (fd < 0 || !mode || | |
634763e8 MS |
977 | (*mode != 'r' && *mode != 'w' && *mode != 'a' && *mode != 's') || |
978 | (*mode == 'a' && isdigit(mode[1] & 255))) | |
ef416fc2 | 979 | return (NULL); |
980 | ||
981 | /* | |
982 | * Allocate memory... | |
983 | */ | |
984 | ||
985 | if ((fp = calloc(1, sizeof(cups_file_t))) == NULL) | |
986 | return (NULL); | |
987 | ||
988 | /* | |
989 | * Open the file... | |
990 | */ | |
991 | ||
992 | fp->fd = fd; | |
993 | ||
994 | switch (*mode) | |
995 | { | |
ef416fc2 | 996 | case 'a' : |
634763e8 MS |
997 | fp->pos = lseek(fd, 0, SEEK_END); |
998 | ||
999 | case 'w' : | |
ef416fc2 | 1000 | fp->mode = 'w'; |
1001 | fp->ptr = fp->buf; | |
1002 | fp->end = fp->buf + sizeof(fp->buf); | |
1003 | ||
1004 | #ifdef HAVE_LIBZ | |
1005 | if (mode[1] >= '1' && mode[1] <= '9') | |
1006 | { | |
1007 | /* | |
1008 | * Open a compressed stream, so write the standard gzip file | |
1009 | * header... | |
1010 | */ | |
1011 | ||
1012 | unsigned char header[10]; /* gzip file header */ | |
1013 | time_t curtime; /* Current time */ | |
1014 | ||
1015 | ||
1016 | curtime = time(NULL); | |
1017 | header[0] = 0x1f; | |
1018 | header[1] = 0x8b; | |
1019 | header[2] = Z_DEFLATED; | |
1020 | header[3] = 0; | |
1021 | header[4] = curtime; | |
1022 | header[5] = curtime >> 8; | |
1023 | header[6] = curtime >> 16; | |
1024 | header[7] = curtime >> 24; | |
1025 | header[8] = 0; | |
1026 | header[9] = 0x03; | |
1027 | ||
1028 | cups_write(fp, (char *)header, 10); | |
1029 | ||
1030 | /* | |
1031 | * Initialize the compressor... | |
1032 | */ | |
1033 | ||
1034 | deflateInit2(&(fp->stream), mode[1] - '0', Z_DEFLATED, -15, 8, | |
1035 | Z_DEFAULT_STRATEGY); | |
1036 | ||
1037 | fp->stream.next_out = fp->cbuf; | |
1038 | fp->stream.avail_out = sizeof(fp->cbuf); | |
1039 | fp->compressed = 1; | |
1040 | fp->crc = crc32(0L, Z_NULL, 0); | |
1041 | } | |
1042 | #endif /* HAVE_LIBZ */ | |
1043 | break; | |
1044 | ||
1045 | case 'r' : | |
1046 | fp->mode = 'r'; | |
1047 | break; | |
1048 | ||
1049 | case 's' : | |
1050 | fp->mode = 's'; | |
1051 | break; | |
1052 | ||
1053 | default : /* Remove bogus compiler warning... */ | |
1054 | return (NULL); | |
1055 | } | |
1056 | ||
1057 | /* | |
1058 | * Don't pass this file to child processes... | |
1059 | */ | |
1060 | ||
1061 | #ifndef WIN32 | |
1062 | fcntl(fp->fd, F_SETFD, fcntl(fp->fd, F_GETFD) | FD_CLOEXEC); | |
1063 | #endif /* !WIN32 */ | |
1064 | ||
1065 | return (fp); | |
1066 | } | |
1067 | ||
1068 | ||
1069 | /* | |
1070 | * 'cupsFilePeekChar()' - Peek at the next character from a file. | |
5a738aea MS |
1071 | * |
1072 | * @since CUPS 1.2@ | |
ef416fc2 | 1073 | */ |
1074 | ||
5a738aea | 1075 | int /* O - Character or -1 on end of file */ |
ef416fc2 | 1076 | cupsFilePeekChar(cups_file_t *fp) /* I - CUPS file */ |
1077 | { | |
1078 | /* | |
1079 | * Range check input... | |
1080 | */ | |
1081 | ||
1082 | if (!fp || (fp->mode != 'r' && fp->mode != 's')) | |
1083 | return (-1); | |
1084 | ||
1085 | /* | |
1086 | * If the input buffer is empty, try to read more data... | |
1087 | */ | |
1088 | ||
1089 | if (fp->ptr >= fp->end) | |
1090 | if (cups_fill(fp) < 0) | |
1091 | return (-1); | |
1092 | ||
1093 | /* | |
1094 | * Return the next character in the buffer... | |
1095 | */ | |
1096 | ||
1097 | return (*(fp->ptr) & 255); | |
1098 | } | |
1099 | ||
1100 | ||
1101 | /* | |
1102 | * 'cupsFilePrintf()' - Write a formatted string. | |
5a738aea MS |
1103 | * |
1104 | * @since CUPS 1.2@ | |
ef416fc2 | 1105 | */ |
1106 | ||
5a738aea | 1107 | int /* O - Number of bytes written or -1 on error */ |
ef416fc2 | 1108 | cupsFilePrintf(cups_file_t *fp, /* I - CUPS file */ |
1109 | const char *format, /* I - Printf-style format string */ | |
1110 | ...) /* I - Additional args as necessary */ | |
1111 | { | |
1112 | va_list ap; /* Argument list */ | |
2abf387c | 1113 | ssize_t bytes; /* Formatted size */ |
ecdc0628 | 1114 | char buf[8192]; /* Formatted text */ |
1115 | ||
ef416fc2 | 1116 | |
ecdc0628 | 1117 | DEBUG_printf(("cupsFilePrintf(fp=%p, format=\"%s\", ...)\n", fp, format)); |
ef416fc2 | 1118 | |
1119 | if (!fp || !format || (fp->mode != 'w' && fp->mode != 's')) | |
1120 | return (-1); | |
1121 | ||
1122 | va_start(ap, format); | |
1123 | bytes = vsnprintf(buf, sizeof(buf), format, ap); | |
1124 | va_end(ap); | |
1125 | ||
ecdc0628 | 1126 | if (bytes >= sizeof(buf)) |
1127 | return (-1); | |
1128 | ||
ef416fc2 | 1129 | if (fp->mode == 's') |
634763e8 MS |
1130 | { |
1131 | if (cups_write(fp, buf, bytes) < 0) | |
1132 | return (-1); | |
1133 | ||
1134 | fp->pos += bytes; | |
1135 | ||
1136 | DEBUG_printf(("cupsFilePrintf: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); | |
1137 | ||
1138 | return (bytes); | |
1139 | } | |
ef416fc2 | 1140 | |
1141 | if ((fp->ptr + bytes) > fp->end) | |
1142 | if (cupsFileFlush(fp)) | |
1143 | return (-1); | |
1144 | ||
1145 | fp->pos += bytes; | |
1146 | ||
634763e8 MS |
1147 | DEBUG_printf(("cupsFilePrintf: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); |
1148 | ||
ef416fc2 | 1149 | if (bytes > sizeof(fp->buf)) |
1150 | { | |
1151 | #ifdef HAVE_LIBZ | |
1152 | if (fp->compressed) | |
1153 | return (cups_compress(fp, buf, bytes)); | |
1154 | else | |
1155 | #endif /* HAVE_LIBZ */ | |
1156 | return (cups_write(fp, buf, bytes)); | |
1157 | } | |
1158 | else | |
1159 | { | |
1160 | memcpy(fp->ptr, buf, bytes); | |
1161 | fp->ptr += bytes; | |
1162 | return (bytes); | |
1163 | } | |
1164 | } | |
1165 | ||
1166 | ||
1167 | /* | |
1168 | * 'cupsFilePutChar()' - Write a character. | |
5a738aea MS |
1169 | * |
1170 | * @since CUPS 1.2@ | |
ef416fc2 | 1171 | */ |
1172 | ||
1173 | int /* O - 0 on success, -1 on error */ | |
1174 | cupsFilePutChar(cups_file_t *fp, /* I - CUPS file */ | |
1175 | int c) /* I - Character to write */ | |
1176 | { | |
1177 | /* | |
1178 | * Range check input... | |
1179 | */ | |
1180 | ||
1181 | if (!fp || (fp->mode != 'w' && fp->mode != 's')) | |
1182 | return (-1); | |
1183 | ||
1184 | if (fp->mode == 's') | |
1185 | { | |
1186 | /* | |
1187 | * Send character immediately over socket... | |
1188 | */ | |
1189 | ||
1190 | char ch; /* Output character */ | |
1191 | ||
1192 | ||
1193 | ch = c; | |
1194 | ||
1195 | if (send(fp->fd, &ch, 1, 0) < 1) | |
1196 | return (-1); | |
1197 | } | |
1198 | else | |
1199 | { | |
1200 | /* | |
1201 | * Buffer it up... | |
1202 | */ | |
1203 | ||
1204 | if (fp->ptr >= fp->end) | |
1205 | if (cupsFileFlush(fp)) | |
1206 | return (-1); | |
1207 | ||
1208 | *(fp->ptr) ++ = c; | |
1209 | } | |
1210 | ||
1211 | fp->pos ++; | |
1212 | ||
634763e8 MS |
1213 | DEBUG_printf(("cupsFilePutChar: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); |
1214 | ||
ef416fc2 | 1215 | return (0); |
1216 | } | |
1217 | ||
1218 | ||
1219 | /* | |
1220 | * 'cupsFilePuts()' - Write a string. | |
5a738aea MS |
1221 | * |
1222 | * Like the @code fputs@ function, no newline is appended to the string. | |
1223 | * | |
1224 | * @since CUPS 1.2@ | |
ef416fc2 | 1225 | */ |
1226 | ||
5a738aea | 1227 | int /* O - Number of bytes written or -1 on error */ |
ef416fc2 | 1228 | cupsFilePuts(cups_file_t *fp, /* I - CUPS file */ |
1229 | const char *s) /* I - String to write */ | |
1230 | { | |
2abf387c | 1231 | ssize_t bytes; /* Bytes to write */ |
ef416fc2 | 1232 | |
1233 | ||
1234 | /* | |
1235 | * Range check input... | |
1236 | */ | |
1237 | ||
1238 | if (!fp || !s || (fp->mode != 'w' && fp->mode != 's')) | |
1239 | return (-1); | |
1240 | ||
1241 | /* | |
1242 | * Write the string... | |
1243 | */ | |
1244 | ||
b86bc4cf | 1245 | bytes = (int)strlen(s); |
ef416fc2 | 1246 | |
1247 | if (fp->mode == 's') | |
1248 | { | |
1249 | if (cups_write(fp, s, bytes) < 0) | |
1250 | return (-1); | |
1251 | ||
1252 | fp->pos += bytes; | |
1253 | ||
634763e8 MS |
1254 | DEBUG_printf(("cupsFilePuts: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); |
1255 | ||
ef416fc2 | 1256 | return (bytes); |
1257 | } | |
1258 | ||
1259 | if ((fp->ptr + bytes) > fp->end) | |
1260 | if (cupsFileFlush(fp)) | |
1261 | return (-1); | |
1262 | ||
1263 | fp->pos += bytes; | |
1264 | ||
634763e8 MS |
1265 | DEBUG_printf(("cupsFilePuts: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); |
1266 | ||
ef416fc2 | 1267 | if (bytes > sizeof(fp->buf)) |
1268 | { | |
1269 | #ifdef HAVE_LIBZ | |
1270 | if (fp->compressed) | |
1271 | return (cups_compress(fp, s, bytes)); | |
1272 | else | |
1273 | #endif /* HAVE_LIBZ */ | |
1274 | return (cups_write(fp, s, bytes)); | |
1275 | } | |
1276 | else | |
1277 | { | |
1278 | memcpy(fp->ptr, s, bytes); | |
1279 | fp->ptr += bytes; | |
1280 | return (bytes); | |
1281 | } | |
1282 | } | |
1283 | ||
1284 | ||
1285 | /* | |
1286 | * 'cupsFileRead()' - Read from a file. | |
5a738aea MS |
1287 | * |
1288 | * @since CUPS 1.2@ | |
ef416fc2 | 1289 | */ |
1290 | ||
5a738aea | 1291 | ssize_t /* O - Number of bytes read or -1 on error */ |
ef416fc2 | 1292 | cupsFileRead(cups_file_t *fp, /* I - CUPS file */ |
1293 | char *buf, /* O - Buffer */ | |
1294 | size_t bytes) /* I - Number of bytes to read */ | |
1295 | { | |
2abf387c | 1296 | size_t total; /* Total bytes read */ |
1297 | ssize_t count; /* Bytes read */ | |
ef416fc2 | 1298 | |
1299 | ||
634763e8 MS |
1300 | DEBUG_printf(("cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")\n", fp, buf, |
1301 | CUPS_LLCAST bytes)); | |
ef416fc2 | 1302 | |
1303 | /* | |
1304 | * Range check input... | |
1305 | */ | |
1306 | ||
1307 | if (!fp || !buf || bytes < 0 || (fp->mode != 'r' && fp->mode != 's')) | |
1308 | return (-1); | |
1309 | ||
1310 | if (bytes == 0) | |
1311 | return (0); | |
1312 | ||
1313 | /* | |
1314 | * Loop until all bytes are read... | |
1315 | */ | |
1316 | ||
1317 | total = 0; | |
1318 | while (bytes > 0) | |
1319 | { | |
1320 | if (fp->ptr >= fp->end) | |
1321 | if (cups_fill(fp) <= 0) | |
1322 | { | |
634763e8 MS |
1323 | DEBUG_printf(("cupsFileRead: cups_fill() returned -1, total=" CUPS_LLFMT "\n", |
1324 | CUPS_LLCAST total)); | |
ef416fc2 | 1325 | |
1326 | if (total > 0) | |
b86bc4cf | 1327 | return ((ssize_t)total); |
ef416fc2 | 1328 | else |
1329 | return (-1); | |
1330 | } | |
1331 | ||
b86bc4cf | 1332 | count = (ssize_t)(fp->end - fp->ptr); |
1333 | if (count > (ssize_t)bytes) | |
1334 | count = (ssize_t)bytes; | |
ef416fc2 | 1335 | |
1336 | memcpy(buf, fp->ptr, count); | |
1337 | fp->ptr += count; | |
634763e8 MS |
1338 | fp->pos += count; |
1339 | ||
1340 | DEBUG_printf(("cupsFileRead: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); | |
ef416fc2 | 1341 | |
1342 | /* | |
1343 | * Update the counts for the last read... | |
1344 | */ | |
1345 | ||
1346 | bytes -= count; | |
1347 | total += count; | |
1348 | buf += count; | |
1349 | } | |
1350 | ||
1351 | /* | |
1352 | * Return the total number of bytes read... | |
1353 | */ | |
1354 | ||
634763e8 | 1355 | DEBUG_printf(("cupsFileRead: total=%d\n", (int)total)); |
ef416fc2 | 1356 | |
b86bc4cf | 1357 | return ((ssize_t)total); |
ef416fc2 | 1358 | } |
1359 | ||
1360 | ||
1361 | /* | |
5a738aea MS |
1362 | * 'cupsFileRewind()' - Set the current file position to the beginning of the |
1363 | * file. | |
1364 | * | |
1365 | * @since CUPS 1.2@ | |
ef416fc2 | 1366 | */ |
1367 | ||
5a738aea | 1368 | off_t /* O - New file position or -1 on error */ |
ef416fc2 | 1369 | cupsFileRewind(cups_file_t *fp) /* I - CUPS file */ |
1370 | { | |
80ca4592 | 1371 | /* |
1372 | * Range check input... | |
1373 | */ | |
1374 | ||
634763e8 MS |
1375 | DEBUG_printf(("cupsFileRewind(fp=%p)\n", fp)); |
1376 | DEBUG_printf(("cupsFileRewind: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); | |
1377 | ||
80ca4592 | 1378 | if (!fp || fp->mode != 'r') |
1379 | return (-1); | |
1380 | ||
1381 | /* | |
1382 | * Handle special cases... | |
1383 | */ | |
1384 | ||
634763e8 | 1385 | if (fp->bufpos == 0) |
80ca4592 | 1386 | { |
1387 | /* | |
1388 | * No seeking necessary... | |
1389 | */ | |
1390 | ||
634763e8 MS |
1391 | fp->pos = 0; |
1392 | ||
80ca4592 | 1393 | if (fp->ptr) |
1394 | { | |
1395 | fp->ptr = fp->buf; | |
1396 | fp->eof = 0; | |
1397 | } | |
1398 | ||
634763e8 MS |
1399 | DEBUG_printf(("cupsFileRewind: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); |
1400 | ||
80ca4592 | 1401 | return (0); |
1402 | } | |
1403 | ||
1404 | /* | |
1405 | * Otherwise, seek in the file and cleanup any compression buffers... | |
1406 | */ | |
1407 | ||
1408 | #ifdef HAVE_LIBZ | |
1409 | if (fp->compressed) | |
1410 | { | |
1411 | inflateEnd(&fp->stream); | |
1412 | fp->compressed = 0; | |
1413 | } | |
1414 | #endif /* HAVE_LIBZ */ | |
1415 | ||
1416 | lseek(fp->fd, 0, SEEK_SET); | |
1417 | ||
634763e8 MS |
1418 | fp->bufpos = 0; |
1419 | fp->pos = 0; | |
1420 | fp->ptr = NULL; | |
1421 | fp->end = NULL; | |
1422 | fp->eof = 0; | |
1423 | ||
1424 | DEBUG_printf(("cupsFileRewind: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); | |
80ca4592 | 1425 | |
1426 | return (0); | |
ef416fc2 | 1427 | } |
1428 | ||
1429 | ||
1430 | /* | |
1431 | * 'cupsFileSeek()' - Seek in a file. | |
5a738aea MS |
1432 | * |
1433 | * @since CUPS 1.2@ | |
ef416fc2 | 1434 | */ |
1435 | ||
5a738aea | 1436 | off_t /* O - New file position or -1 on error */ |
ef416fc2 | 1437 | cupsFileSeek(cups_file_t *fp, /* I - CUPS file */ |
1438 | off_t pos) /* I - Position in file */ | |
1439 | { | |
2abf387c | 1440 | ssize_t bytes; /* Number bytes in buffer */ |
ef416fc2 | 1441 | |
1442 | ||
634763e8 MS |
1443 | DEBUG_printf(("cupsFileSeek(fp=%p, pos=" CUPS_LLFMT ")\n", fp, |
1444 | CUPS_LLCAST pos)); | |
1445 | DEBUG_printf(("cupsFileSeek: fp->pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); | |
1446 | DEBUG_printf(("cupsFileSeek: fp->ptr=%p, fp->end=%p\n", fp->ptr, fp->end)); | |
ef416fc2 | 1447 | |
1448 | /* | |
1449 | * Range check input... | |
1450 | */ | |
1451 | ||
1452 | if (!fp || pos < 0 || fp->mode != 'r') | |
1453 | return (-1); | |
1454 | ||
80ca4592 | 1455 | /* |
1456 | * Handle special cases... | |
1457 | */ | |
1458 | ||
1459 | if (pos == 0) | |
1460 | return (cupsFileRewind(fp)); | |
1461 | ||
634763e8 | 1462 | if (fp->ptr) |
b423cd4c | 1463 | { |
634763e8 | 1464 | bytes = (ssize_t)(fp->end - fp->buf); |
b423cd4c | 1465 | |
634763e8 | 1466 | if (pos >= fp->bufpos && pos < (fp->bufpos + bytes)) |
b423cd4c | 1467 | { |
634763e8 MS |
1468 | /* |
1469 | * No seeking necessary... | |
1470 | */ | |
1471 | ||
1472 | fp->pos = pos; | |
1473 | fp->ptr = fp->buf + pos - fp->bufpos; | |
b423cd4c | 1474 | fp->eof = 0; |
b423cd4c | 1475 | |
634763e8 MS |
1476 | return (pos); |
1477 | } | |
b423cd4c | 1478 | } |
1479 | ||
80ca4592 | 1480 | #ifdef HAVE_LIBZ |
1481 | if (!fp->compressed && !fp->ptr) | |
1482 | { | |
1483 | /* | |
1484 | * Preload a buffer to determine whether the file is compressed... | |
1485 | */ | |
1486 | ||
1487 | if (cups_fill(fp) < 0) | |
1488 | return (-1); | |
1489 | } | |
1490 | #endif /* HAVE_LIBZ */ | |
1491 | ||
ef416fc2 | 1492 | /* |
634763e8 | 1493 | * Seek forwards or backwards... |
ef416fc2 | 1494 | */ |
1495 | ||
ef416fc2 | 1496 | fp->eof = 0; |
1497 | ||
634763e8 | 1498 | DEBUG_printf(("cupsFileSeek: bytes=" CUPS_LLFMT "\n", CUPS_LLCAST bytes)); |
80ca4592 | 1499 | |
634763e8 | 1500 | if (pos < fp->bufpos) |
ef416fc2 | 1501 | { |
1502 | /* | |
1503 | * Need to seek backwards... | |
1504 | */ | |
1505 | ||
634763e8 | 1506 | DEBUG_puts("cupsFileSeek: SEEK BACKWARDS"); |
80ca4592 | 1507 | |
ef416fc2 | 1508 | #ifdef HAVE_LIBZ |
1509 | if (fp->compressed) | |
1510 | { | |
1511 | inflateEnd(&fp->stream); | |
1512 | ||
1513 | lseek(fp->fd, 0, SEEK_SET); | |
634763e8 MS |
1514 | fp->bufpos = 0; |
1515 | fp->pos = 0; | |
1516 | fp->ptr = NULL; | |
1517 | fp->end = NULL; | |
ef416fc2 | 1518 | |
1519 | while ((bytes = cups_fill(fp)) > 0) | |
634763e8 | 1520 | if (pos >= fp->bufpos && pos < (fp->bufpos + bytes)) |
ef416fc2 | 1521 | break; |
1522 | ||
1523 | if (bytes <= 0) | |
1524 | return (-1); | |
80ca4592 | 1525 | |
634763e8 MS |
1526 | fp->ptr = fp->buf + pos - fp->bufpos; |
1527 | fp->pos = pos; | |
ef416fc2 | 1528 | } |
1529 | else | |
1530 | #endif /* HAVE_LIBZ */ | |
1531 | { | |
634763e8 MS |
1532 | fp->bufpos = lseek(fp->fd, pos, SEEK_SET); |
1533 | fp->pos = fp->bufpos; | |
1534 | fp->ptr = NULL; | |
1535 | fp->end = NULL; | |
80ca4592 | 1536 | |
634763e8 MS |
1537 | DEBUG_printf(("cupsFileSeek: lseek() returned " CUPS_LLFMT "...\n", |
1538 | CUPS_LLCAST fp->pos)); | |
ef416fc2 | 1539 | } |
1540 | } | |
634763e8 | 1541 | else |
ef416fc2 | 1542 | { |
1543 | /* | |
1544 | * Need to seek forwards... | |
1545 | */ | |
1546 | ||
634763e8 | 1547 | DEBUG_puts("cupsFileSeek: SEEK FORWARDS"); |
80ca4592 | 1548 | |
ef416fc2 | 1549 | #ifdef HAVE_LIBZ |
80ca4592 | 1550 | if (fp->compressed) |
ef416fc2 | 1551 | { |
1552 | while ((bytes = cups_fill(fp)) > 0) | |
80ca4592 | 1553 | { |
634763e8 | 1554 | if (pos >= fp->bufpos && pos < (fp->bufpos + bytes)) |
ef416fc2 | 1555 | break; |
80ca4592 | 1556 | } |
ef416fc2 | 1557 | |
1558 | if (bytes <= 0) | |
1559 | return (-1); | |
80ca4592 | 1560 | |
634763e8 MS |
1561 | fp->ptr = fp->buf + pos - fp->bufpos; |
1562 | fp->pos = pos; | |
ef416fc2 | 1563 | } |
1564 | else | |
1565 | #endif /* HAVE_LIBZ */ | |
1566 | { | |
634763e8 MS |
1567 | fp->bufpos = lseek(fp->fd, pos, SEEK_SET); |
1568 | fp->pos = fp->bufpos; | |
1569 | fp->ptr = NULL; | |
1570 | fp->end = NULL; | |
80ca4592 | 1571 | |
634763e8 MS |
1572 | DEBUG_printf(("cupsFileSeek: lseek() returned " CUPS_LLFMT "...\n", |
1573 | CUPS_LLCAST fp->pos)); | |
ef416fc2 | 1574 | } |
1575 | } | |
ef416fc2 | 1576 | |
634763e8 | 1577 | DEBUG_printf(("cupsFileSeek: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); |
ef416fc2 | 1578 | |
1579 | return (fp->pos); | |
1580 | } | |
1581 | ||
1582 | ||
80ca4592 | 1583 | /* |
1584 | * 'cupsFileStderr()' - Return a CUPS file associated with stderr. | |
5a738aea MS |
1585 | * |
1586 | * @since CUPS 1.2@ | |
80ca4592 | 1587 | */ |
1588 | ||
5a738aea | 1589 | cups_file_t * /* O - CUPS file */ |
80ca4592 | 1590 | cupsFileStderr(void) |
1591 | { | |
1592 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals... */ | |
1593 | ||
1594 | ||
1595 | /* | |
1596 | * Open file descriptor 2 as needed... | |
1597 | */ | |
1598 | ||
1599 | if (!cg->stdio_files[2]) | |
1600 | { | |
1601 | /* | |
1602 | * Flush any pending output on the stdio file... | |
1603 | */ | |
1604 | ||
1605 | fflush(stderr); | |
1606 | ||
1607 | /* | |
1608 | * Open file descriptor 2... | |
1609 | */ | |
1610 | ||
1611 | if ((cg->stdio_files[2] = cupsFileOpenFd(2, "w")) != NULL) | |
1612 | cg->stdio_files[2]->is_stdio = 1; | |
1613 | } | |
1614 | ||
1615 | return (cg->stdio_files[2]); | |
1616 | } | |
1617 | ||
1618 | ||
1619 | /* | |
1620 | * 'cupsFileStdin()' - Return a CUPS file associated with stdin. | |
5a738aea MS |
1621 | * |
1622 | * @since CUPS 1.2@ | |
80ca4592 | 1623 | */ |
1624 | ||
5a738aea | 1625 | cups_file_t * /* O - CUPS file */ |
80ca4592 | 1626 | cupsFileStdin(void) |
1627 | { | |
1628 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals... */ | |
1629 | ||
1630 | ||
1631 | /* | |
1632 | * Open file descriptor 0 as needed... | |
1633 | */ | |
1634 | ||
1635 | if (!cg->stdio_files[0]) | |
1636 | { | |
1637 | /* | |
1638 | * Open file descriptor 0... | |
1639 | */ | |
1640 | ||
1641 | if ((cg->stdio_files[0] = cupsFileOpenFd(0, "r")) != NULL) | |
1642 | cg->stdio_files[0]->is_stdio = 1; | |
1643 | } | |
1644 | ||
1645 | return (cg->stdio_files[0]); | |
1646 | } | |
1647 | ||
1648 | ||
1649 | /* | |
1650 | * 'cupsFileStdout()' - Return a CUPS file associated with stdout. | |
5a738aea MS |
1651 | * |
1652 | * @since CUPS 1.2@ | |
80ca4592 | 1653 | */ |
1654 | ||
5a738aea | 1655 | cups_file_t * /* O - CUPS file */ |
80ca4592 | 1656 | cupsFileStdout(void) |
1657 | { | |
1658 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals... */ | |
1659 | ||
1660 | ||
1661 | /* | |
1662 | * Open file descriptor 1 as needed... | |
1663 | */ | |
1664 | ||
1665 | if (!cg->stdio_files[1]) | |
1666 | { | |
1667 | /* | |
1668 | * Flush any pending output on the stdio file... | |
1669 | */ | |
1670 | ||
1671 | fflush(stdout); | |
1672 | ||
1673 | /* | |
1674 | * Open file descriptor 1... | |
1675 | */ | |
1676 | ||
1677 | if ((cg->stdio_files[1] = cupsFileOpenFd(1, "w")) != NULL) | |
1678 | cg->stdio_files[1]->is_stdio = 1; | |
1679 | } | |
1680 | ||
1681 | return (cg->stdio_files[1]); | |
1682 | } | |
1683 | ||
1684 | ||
ef416fc2 | 1685 | /* |
1686 | * 'cupsFileTell()' - Return the current file position. | |
5a738aea MS |
1687 | * |
1688 | * @since CUPS 1.2@ | |
ef416fc2 | 1689 | */ |
1690 | ||
1691 | off_t /* O - File position */ | |
1692 | cupsFileTell(cups_file_t *fp) /* I - CUPS file */ | |
1693 | { | |
634763e8 MS |
1694 | DEBUG_printf(("cupsFileTell(fp=%p)\n", fp)); |
1695 | DEBUG_printf(("cupsFileTell: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); | |
1696 | ||
80ca4592 | 1697 | return (fp ? fp->pos : 0); |
ef416fc2 | 1698 | } |
1699 | ||
1700 | ||
1701 | /* | |
1702 | * 'cupsFileUnlock()' - Unlock access to a file. | |
5a738aea MS |
1703 | * |
1704 | * @since CUPS 1.2@ | |
ef416fc2 | 1705 | */ |
1706 | ||
1707 | int /* O - 0 on success, -1 on error */ | |
5a738aea | 1708 | cupsFileUnlock(cups_file_t *fp) /* I - CUPS file */ |
ef416fc2 | 1709 | { |
1710 | /* | |
1711 | * Range check... | |
1712 | */ | |
1713 | ||
634763e8 MS |
1714 | DEBUG_printf(("cupsFileUnlock(fp=%p)\n", fp)); |
1715 | ||
ef416fc2 | 1716 | if (!fp || fp->mode == 's') |
1717 | return (-1); | |
1718 | ||
1719 | /* | |
1720 | * Unlock... | |
1721 | */ | |
1722 | ||
1723 | #ifdef WIN32 | |
1724 | return (locking(fp->fd, _LK_UNLCK, 0)); | |
1725 | #else | |
1726 | return (lockf(fp->fd, F_ULOCK, 0)); | |
1727 | #endif /* WIN32 */ | |
1728 | } | |
1729 | ||
1730 | ||
1731 | /* | |
1732 | * 'cupsFileWrite()' - Write to a file. | |
5a738aea MS |
1733 | * |
1734 | * @since CUPS 1.2@ | |
ef416fc2 | 1735 | */ |
1736 | ||
5a738aea | 1737 | ssize_t /* O - Number of bytes written or -1 on error */ |
ef416fc2 | 1738 | cupsFileWrite(cups_file_t *fp, /* I - CUPS file */ |
1739 | const char *buf, /* I - Buffer */ | |
1740 | size_t bytes) /* I - Number of bytes to write */ | |
1741 | { | |
1742 | /* | |
1743 | * Range check input... | |
1744 | */ | |
1745 | ||
634763e8 MS |
1746 | DEBUG_printf(("cupsFileWrite(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")\n", |
1747 | fp, buf, CUPS_LLCAST bytes)); | |
1748 | ||
ef416fc2 | 1749 | if (!fp || !buf || bytes < 0 || (fp->mode != 'w' && fp->mode != 's')) |
1750 | return (-1); | |
1751 | ||
1752 | if (bytes == 0) | |
1753 | return (0); | |
1754 | ||
1755 | /* | |
1756 | * Write the buffer... | |
1757 | */ | |
1758 | ||
1759 | if (fp->mode == 's') | |
1760 | { | |
1761 | if (cups_write(fp, buf, bytes) < 0) | |
1762 | return (-1); | |
1763 | ||
b86bc4cf | 1764 | fp->pos += (off_t)bytes; |
ef416fc2 | 1765 | |
634763e8 MS |
1766 | DEBUG_printf(("cupsFileWrite: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); |
1767 | ||
b86bc4cf | 1768 | return ((ssize_t)bytes); |
ef416fc2 | 1769 | } |
1770 | ||
1771 | if ((fp->ptr + bytes) > fp->end) | |
1772 | if (cupsFileFlush(fp)) | |
1773 | return (-1); | |
1774 | ||
b86bc4cf | 1775 | fp->pos += (off_t)bytes; |
ef416fc2 | 1776 | |
634763e8 MS |
1777 | DEBUG_printf(("cupsFileWrite: pos=" CUPS_LLFMT "\n", CUPS_LLCAST fp->pos)); |
1778 | ||
ef416fc2 | 1779 | if (bytes > sizeof(fp->buf)) |
1780 | { | |
1781 | #ifdef HAVE_LIBZ | |
1782 | if (fp->compressed) | |
1783 | return (cups_compress(fp, buf, bytes)); | |
1784 | else | |
1785 | #endif /* HAVE_LIBZ */ | |
1786 | return (cups_write(fp, buf, bytes)); | |
1787 | } | |
1788 | else | |
1789 | { | |
1790 | memcpy(fp->ptr, buf, bytes); | |
1791 | fp->ptr += bytes; | |
b86bc4cf | 1792 | return ((ssize_t)bytes); |
ef416fc2 | 1793 | } |
1794 | } | |
1795 | ||
1796 | ||
1797 | #ifdef HAVE_LIBZ | |
1798 | /* | |
1799 | * 'cups_compress()' - Compress a buffer of data... | |
1800 | */ | |
1801 | ||
1802 | static ssize_t /* O - Number of bytes written or -1 */ | |
1803 | cups_compress(cups_file_t *fp, /* I - CUPS file */ | |
1804 | const char *buf, /* I - Buffer */ | |
1805 | size_t bytes) /* I - Number bytes */ | |
1806 | { | |
634763e8 MS |
1807 | DEBUG_printf(("cups_compress(fp=%p, buf=%p, bytes=" CUPS_LLFMT "\n", fp, buf, |
1808 | CUPS_LLCAST bytes)); | |
ecdc0628 | 1809 | |
ef416fc2 | 1810 | /* |
1811 | * Update the CRC... | |
1812 | */ | |
1813 | ||
1814 | fp->crc = crc32(fp->crc, (const Bytef *)buf, bytes); | |
1815 | ||
1816 | /* | |
1817 | * Deflate the bytes... | |
1818 | */ | |
1819 | ||
1820 | fp->stream.next_in = (Bytef *)buf; | |
1821 | fp->stream.avail_in = bytes; | |
1822 | ||
1823 | while (fp->stream.avail_in > 0) | |
1824 | { | |
1825 | /* | |
1826 | * Flush the current buffer... | |
1827 | */ | |
1828 | ||
634763e8 MS |
1829 | DEBUG_printf(("cups_compress: avail_in=%d, avail_out=%d\n", |
1830 | fp->stream.avail_in, fp->stream.avail_out)); | |
ecdc0628 | 1831 | |
ef416fc2 | 1832 | if (fp->stream.avail_out < (int)(sizeof(fp->cbuf) / 8)) |
1833 | { | |
1834 | if (cups_write(fp, (char *)fp->cbuf, fp->stream.next_out - fp->cbuf) < 0) | |
1835 | return (-1); | |
ecdc0628 | 1836 | |
1837 | fp->stream.next_out = fp->cbuf; | |
1838 | fp->stream.avail_out = sizeof(fp->cbuf); | |
ef416fc2 | 1839 | } |
1840 | ||
1841 | deflate(&(fp->stream), Z_NO_FLUSH); | |
1842 | } | |
1843 | ||
1844 | return (bytes); | |
1845 | } | |
1846 | #endif /* HAVE_LIBZ */ | |
1847 | ||
1848 | ||
1849 | /* | |
1850 | * 'cups_fill()' - Fill the input buffer... | |
1851 | */ | |
1852 | ||
1853 | static ssize_t /* O - Number of bytes or -1 */ | |
1854 | cups_fill(cups_file_t *fp) /* I - CUPS file */ | |
1855 | { | |
1856 | ssize_t bytes; /* Number of bytes read */ | |
1857 | #ifdef HAVE_LIBZ | |
c277e2f8 | 1858 | int status; /* Decompression status */ |
ef416fc2 | 1859 | const unsigned char *ptr, /* Pointer into buffer */ |
1860 | *end; /* End of buffer */ | |
1861 | #endif /* HAVE_LIBZ */ | |
1862 | ||
1863 | ||
1864 | DEBUG_printf(("cups_fill(fp=%p)\n", fp)); | |
634763e8 MS |
1865 | DEBUG_printf(("cups_fill: fp->ptr=%p, fp->end=%p, fp->buf=%p, " |
1866 | "fp->bufpos=" CUPS_LLFMT ", fp->eof=%d\n", | |
1867 | fp->ptr, fp->end, fp->buf, CUPS_LLCAST fp->bufpos, fp->eof)); | |
ef416fc2 | 1868 | |
1869 | if (fp->ptr && fp->end) | |
634763e8 | 1870 | fp->bufpos += fp->end - fp->ptr; |
ef416fc2 | 1871 | |
1872 | #ifdef HAVE_LIBZ | |
634763e8 | 1873 | DEBUG_printf(("cups_fill: fp->compressed=%d\n", fp->compressed)); |
b423cd4c | 1874 | |
ef416fc2 | 1875 | while (!fp->ptr || fp->compressed) |
1876 | { | |
1877 | /* | |
1878 | * Check to see if we have read any data yet; if not, see if we have a | |
1879 | * compressed file... | |
1880 | */ | |
1881 | ||
1882 | if (!fp->ptr) | |
1883 | { | |
1884 | /* | |
1885 | * Reset the file position in case we are seeking... | |
1886 | */ | |
1887 | ||
1888 | fp->compressed = 0; | |
ef416fc2 | 1889 | |
1890 | /* | |
1891 | * Read the first bytes in the file to determine if we have a gzip'd | |
1892 | * file... | |
1893 | */ | |
1894 | ||
fa73b229 | 1895 | if ((bytes = cups_read(fp, (char *)fp->buf, sizeof(fp->buf))) < 0) |
ef416fc2 | 1896 | { |
1897 | /* | |
1898 | * Can't read from file! | |
1899 | */ | |
1900 | ||
634763e8 | 1901 | DEBUG_printf(("cups_fill: cups_read() returned " CUPS_LLFMT "!\n", |
b423cd4c | 1902 | CUPS_LLCAST bytes)); |
1903 | ||
ef416fc2 | 1904 | return (-1); |
1905 | } | |
1906 | ||
fa73b229 | 1907 | if (bytes < 10 || fp->buf[0] != 0x1f || |
e00b005a | 1908 | (fp->buf[1] & 255) != 0x8b || |
fa73b229 | 1909 | fp->buf[2] != 8 || (fp->buf[3] & 0xe0) != 0) |
ef416fc2 | 1910 | { |
1911 | /* | |
1912 | * Not a gzip'd file! | |
1913 | */ | |
1914 | ||
ef416fc2 | 1915 | fp->ptr = fp->buf; |
1916 | fp->end = fp->buf + bytes; | |
1917 | ||
b423cd4c | 1918 | DEBUG_printf((" returning " CUPS_LLFMT "!\n", CUPS_LLCAST bytes)); |
1919 | ||
ef416fc2 | 1920 | return (bytes); |
1921 | } | |
1922 | ||
1923 | /* | |
1924 | * Parse header junk: extra data, original name, and comment... | |
1925 | */ | |
1926 | ||
fa73b229 | 1927 | ptr = (unsigned char *)fp->buf + 10; |
1928 | end = (unsigned char *)fp->buf + bytes; | |
ef416fc2 | 1929 | |
fa73b229 | 1930 | if (fp->buf[3] & 0x04) |
ef416fc2 | 1931 | { |
1932 | /* | |
1933 | * Skip extra data... | |
1934 | */ | |
1935 | ||
1936 | if ((ptr + 2) > end) | |
1937 | { | |
1938 | /* | |
1939 | * Can't read from file! | |
1940 | */ | |
1941 | ||
1942 | return (-1); | |
1943 | } | |
1944 | ||
1945 | bytes = ((unsigned char)ptr[1] << 8) | (unsigned char)ptr[0]; | |
1946 | ptr += 2 + bytes; | |
1947 | ||
1948 | if (ptr > end) | |
1949 | { | |
1950 | /* | |
1951 | * Can't read from file! | |
1952 | */ | |
1953 | ||
1954 | return (-1); | |
1955 | } | |
1956 | } | |
1957 | ||
fa73b229 | 1958 | if (fp->buf[3] & 0x08) |
ef416fc2 | 1959 | { |
1960 | /* | |
1961 | * Skip original name data... | |
1962 | */ | |
1963 | ||
1964 | while (ptr < end && *ptr) | |
1965 | ptr ++; | |
1966 | ||
1967 | if (ptr < end) | |
1968 | ptr ++; | |
1969 | else | |
1970 | { | |
1971 | /* | |
1972 | * Can't read from file! | |
1973 | */ | |
1974 | ||
1975 | return (-1); | |
1976 | } | |
1977 | } | |
1978 | ||
fa73b229 | 1979 | if (fp->buf[3] & 0x10) |
ef416fc2 | 1980 | { |
1981 | /* | |
1982 | * Skip comment data... | |
1983 | */ | |
1984 | ||
1985 | while (ptr < end && *ptr) | |
1986 | ptr ++; | |
1987 | ||
1988 | if (ptr < end) | |
1989 | ptr ++; | |
1990 | else | |
1991 | { | |
1992 | /* | |
1993 | * Can't read from file! | |
1994 | */ | |
1995 | ||
1996 | return (-1); | |
1997 | } | |
1998 | } | |
1999 | ||
fa73b229 | 2000 | if (fp->buf[3] & 0x02) |
ef416fc2 | 2001 | { |
2002 | /* | |
2003 | * Skip header CRC data... | |
2004 | */ | |
2005 | ||
2006 | ptr += 2; | |
2007 | ||
2008 | if (ptr > end) | |
2009 | { | |
2010 | /* | |
2011 | * Can't read from file! | |
2012 | */ | |
2013 | ||
2014 | return (-1); | |
2015 | } | |
2016 | } | |
2017 | ||
fa73b229 | 2018 | /* |
2019 | * Copy the flate-compressed data to the compression buffer... | |
2020 | */ | |
2021 | ||
2022 | if ((bytes = end - ptr) > 0) | |
2023 | memcpy(fp->cbuf, ptr, bytes); | |
2024 | ||
ef416fc2 | 2025 | /* |
2026 | * Setup the decompressor data... | |
2027 | */ | |
2028 | ||
2029 | fp->stream.zalloc = (alloc_func)0; | |
2030 | fp->stream.zfree = (free_func)0; | |
2031 | fp->stream.opaque = (voidpf)0; | |
fa73b229 | 2032 | fp->stream.next_in = (Bytef *)fp->cbuf; |
ef416fc2 | 2033 | fp->stream.next_out = NULL; |
fa73b229 | 2034 | fp->stream.avail_in = bytes; |
ef416fc2 | 2035 | fp->stream.avail_out = 0; |
2036 | fp->crc = crc32(0L, Z_NULL, 0); | |
2037 | ||
2038 | if (inflateInit2(&(fp->stream), -15) != Z_OK) | |
2039 | return (-1); | |
2040 | ||
2041 | fp->compressed = 1; | |
2042 | } | |
2043 | ||
2044 | if (fp->compressed) | |
2045 | { | |
2046 | /* | |
2047 | * If we have reached end-of-file, return immediately... | |
2048 | */ | |
2049 | ||
2050 | if (fp->eof) | |
2051 | return (-1); | |
2052 | ||
2053 | /* | |
2054 | * Fill the decompression buffer as needed... | |
2055 | */ | |
2056 | ||
2057 | if (fp->stream.avail_in == 0) | |
2058 | { | |
2059 | if ((bytes = cups_read(fp, (char *)fp->cbuf, sizeof(fp->cbuf))) <= 0) | |
2060 | return (-1); | |
2061 | ||
2062 | fp->stream.next_in = fp->cbuf; | |
2063 | fp->stream.avail_in = bytes; | |
2064 | } | |
2065 | ||
2066 | /* | |
2067 | * Decompress data from the buffer... | |
2068 | */ | |
2069 | ||
2070 | fp->stream.next_out = (Bytef *)fp->buf; | |
2071 | fp->stream.avail_out = sizeof(fp->buf); | |
2072 | ||
c277e2f8 MS |
2073 | status = inflate(&(fp->stream), Z_NO_FLUSH); |
2074 | ||
2075 | if (fp->stream.next_out > (Bytef *)fp->buf) | |
2076 | fp->crc = crc32(fp->crc, (Bytef *)fp->buf, | |
2077 | fp->stream.next_out - (Bytef *)fp->buf); | |
2078 | ||
2079 | if (status == Z_STREAM_END) | |
ef416fc2 | 2080 | { |
2081 | /* | |
2082 | * Read the CRC and length... | |
2083 | */ | |
2084 | ||
2085 | unsigned char trailer[8]; /* Trailer bytes */ | |
2086 | uLong tcrc; /* Trailer CRC */ | |
2087 | ||
2088 | ||
2089 | if (read(fp->fd, trailer, sizeof(trailer)) < sizeof(trailer)) | |
2090 | { | |
2091 | /* | |
2092 | * Can't get it, so mark end-of-file... | |
2093 | */ | |
2094 | ||
2095 | fp->eof = 1; | |
ef416fc2 | 2096 | } |
fa73b229 | 2097 | else |
2098 | { | |
2099 | tcrc = (((((trailer[3] << 8) | trailer[2]) << 8) | trailer[1]) << 8) | | |
2100 | trailer[0]; | |
ef416fc2 | 2101 | |
fa73b229 | 2102 | if (tcrc != fp->crc) |
2103 | { | |
2104 | /* | |
2105 | * Bad CRC, mark end-of-file... | |
2106 | */ | |
2107 | ||
c277e2f8 MS |
2108 | DEBUG_printf(("cups_fill: tcrc=%08x, fp->crc=%08x\n", |
2109 | (unsigned int)tcrc, (unsigned int)fp->crc)); | |
2110 | ||
fa73b229 | 2111 | fp->eof = 1; |
2112 | ||
2113 | return (-1); | |
2114 | } | |
ef416fc2 | 2115 | |
ef416fc2 | 2116 | /* |
fa73b229 | 2117 | * Otherwise, reset the compressed flag so that we re-read the |
2118 | * file header... | |
ef416fc2 | 2119 | */ |
ef416fc2 | 2120 | |
fa73b229 | 2121 | fp->compressed = 0; |
ef416fc2 | 2122 | } |
ef416fc2 | 2123 | } |
2124 | ||
2125 | bytes = sizeof(fp->buf) - fp->stream.avail_out; | |
2126 | ||
2127 | /* | |
2128 | * Return the decompressed data... | |
2129 | */ | |
2130 | ||
2131 | fp->ptr = fp->buf; | |
2132 | fp->end = fp->buf + bytes; | |
2133 | ||
fa73b229 | 2134 | if (bytes) |
2135 | return (bytes); | |
ef416fc2 | 2136 | } |
2137 | } | |
2138 | #endif /* HAVE_LIBZ */ | |
2139 | ||
2140 | /* | |
2141 | * Read a buffer's full of data... | |
2142 | */ | |
2143 | ||
2144 | if ((bytes = cups_read(fp, fp->buf, sizeof(fp->buf))) <= 0) | |
2145 | { | |
2146 | /* | |
2147 | * Can't read from file! | |
2148 | */ | |
2149 | ||
2150 | fp->eof = 1; | |
2151 | fp->ptr = fp->buf; | |
2152 | fp->end = fp->buf; | |
2153 | ||
2154 | return (-1); | |
2155 | } | |
2156 | ||
2157 | /* | |
2158 | * Return the bytes we read... | |
2159 | */ | |
2160 | ||
2161 | fp->eof = 0; | |
2162 | fp->ptr = fp->buf; | |
2163 | fp->end = fp->buf + bytes; | |
2164 | ||
2165 | return (bytes); | |
2166 | } | |
2167 | ||
2168 | ||
2169 | /* | |
2170 | * 'cups_read()' - Read from a file descriptor. | |
2171 | */ | |
2172 | ||
2173 | static ssize_t /* O - Number of bytes read or -1 */ | |
2174 | cups_read(cups_file_t *fp, /* I - CUPS file */ | |
2175 | char *buf, /* I - Buffer */ | |
2176 | size_t bytes) /* I - Number bytes */ | |
2177 | { | |
2178 | ssize_t total; /* Total bytes read */ | |
2179 | ||
2180 | ||
634763e8 MS |
2181 | DEBUG_printf(("cups_read(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")\n", fp, buf, |
2182 | CUPS_LLCAST bytes)); | |
2183 | ||
ef416fc2 | 2184 | /* |
2185 | * Loop until we read at least 0 bytes... | |
2186 | */ | |
2187 | ||
2188 | for (;;) | |
2189 | { | |
b86bc4cf | 2190 | #ifdef WIN32 |
2191 | if (fp->mode == 's') | |
2192 | total = (ssize_t)recv(fp->fd, buf, (unsigned)bytes, 0); | |
2193 | else | |
2194 | total = (ssize_t)read(fp->fd, buf, (unsigned)bytes); | |
2195 | #else | |
ef416fc2 | 2196 | if (fp->mode == 's') |
2197 | total = recv(fp->fd, buf, bytes, 0); | |
2198 | else | |
2199 | total = read(fp->fd, buf, bytes); | |
b86bc4cf | 2200 | #endif /* WIN32 */ |
ef416fc2 | 2201 | |
634763e8 MS |
2202 | DEBUG_printf(("cups_read: total=" CUPS_LLFMT "\n", CUPS_LLCAST total)); |
2203 | ||
ef416fc2 | 2204 | if (total >= 0) |
2205 | break; | |
2206 | ||
2207 | /* | |
2208 | * Reads can be interrupted by signals and unavailable resources... | |
2209 | */ | |
2210 | ||
2211 | if (errno == EAGAIN || errno == EINTR) | |
2212 | continue; | |
2213 | else | |
2214 | return (-1); | |
2215 | } | |
2216 | ||
2217 | /* | |
2218 | * Return the total number of bytes read... | |
2219 | */ | |
2220 | ||
2221 | return (total); | |
2222 | } | |
2223 | ||
2224 | ||
2225 | /* | |
2226 | * 'cups_write()' - Write to a file descriptor. | |
2227 | */ | |
2228 | ||
2229 | static ssize_t /* O - Number of bytes written or -1 */ | |
2230 | cups_write(cups_file_t *fp, /* I - CUPS file */ | |
2231 | const char *buf, /* I - Buffer */ | |
2232 | size_t bytes) /* I - Number bytes */ | |
2233 | { | |
2abf387c | 2234 | size_t total; /* Total bytes written */ |
2235 | ssize_t count; /* Count this time */ | |
ef416fc2 | 2236 | |
2237 | ||
634763e8 MS |
2238 | DEBUG_printf(("cups_write(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")\n", fp, buf, |
2239 | CUPS_LLCAST bytes)); | |
ecdc0628 | 2240 | |
ef416fc2 | 2241 | /* |
2242 | * Loop until all bytes are written... | |
2243 | */ | |
2244 | ||
2245 | total = 0; | |
2246 | while (bytes > 0) | |
2247 | { | |
b86bc4cf | 2248 | #ifdef WIN32 |
2249 | if (fp->mode == 's') | |
2250 | count = (ssize_t)send(fp->fd, buf, (unsigned)bytes, 0); | |
2251 | else | |
2252 | count = (ssize_t)write(fp->fd, buf, (unsigned)bytes); | |
2253 | #else | |
ef416fc2 | 2254 | if (fp->mode == 's') |
2255 | count = send(fp->fd, buf, bytes, 0); | |
2256 | else | |
2257 | count = write(fp->fd, buf, bytes); | |
b86bc4cf | 2258 | #endif /* WIN32 */ |
ef416fc2 | 2259 | |
634763e8 MS |
2260 | DEBUG_printf(("cups_write: count=" CUPS_LLFMT "\n", CUPS_LLCAST count)); |
2261 | ||
ef416fc2 | 2262 | if (count < 0) |
2263 | { | |
2264 | /* | |
2265 | * Writes can be interrupted by signals and unavailable resources... | |
2266 | */ | |
2267 | ||
2268 | if (errno == EAGAIN || errno == EINTR) | |
2269 | continue; | |
2270 | else | |
2271 | return (-1); | |
2272 | } | |
2273 | ||
2274 | /* | |
2275 | * Update the counts for the last write call... | |
2276 | */ | |
2277 | ||
2278 | bytes -= count; | |
2279 | total += count; | |
2280 | buf += count; | |
2281 | } | |
2282 | ||
2283 | /* | |
2284 | * Return the total number of bytes written... | |
2285 | */ | |
2286 | ||
b86bc4cf | 2287 | return ((ssize_t)total); |
ef416fc2 | 2288 | } |
2289 | ||
2290 | ||
2291 | /* | |
2e4ff8af | 2292 | * End of "$Id: file.c 6962 2007-09-17 20:35:47Z mike $". |
ef416fc2 | 2293 | */ |