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