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