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