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