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