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