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