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