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