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