]> git.saurik.com Git - apple/xnu.git/blob - libkern/zlib/gzio.c
xnu-3789.70.16.tar.gz
[apple/xnu.git] / libkern / zlib / gzio.c
1 /*
2 * Copyright (c) 2008-2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* gzio.c -- IO on .gz files
29 * Copyright (C) 1995-2005 Jean-loup Gailly.
30 * For conditions of distribution and use, see copyright notice in zlib.h
31 *
32 * Compile this file with -DNO_GZCOMPRESS to avoid the compression code.
33 */
34
35 /* @(#) $Id$ */
36
37 #include <stdio.h>
38
39 #include "zutil.h"
40
41 #ifdef NO_DEFLATE /* for compatibility with old definition */
42 # define NO_GZCOMPRESS
43 #endif
44
45 #ifndef NO_DUMMY_DECL
46 struct internal_state {int dummy;}; /* for buggy compilers */
47 #endif
48
49 #ifndef Z_BUFSIZE
50 # ifdef MAXSEG_64K
51 # define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
52 # else
53 # define Z_BUFSIZE 16384
54 # endif
55 #endif
56 #ifndef Z_PRINTF_BUFSIZE
57 # define Z_PRINTF_BUFSIZE 4096
58 #endif
59
60 #ifdef __MVS__
61 # pragma map (fdopen , "\174\174FDOPEN")
62 FILE *fdopen(int, const char *);
63 #endif
64
65 #ifndef STDC
66 extern voidp malloc OF((uInt size));
67 extern void free OF((voidpf ptr));
68 #endif
69
70 #define ALLOC(size) malloc(size)
71 #define TRYFREE(p) {if (p) free(p);}
72
73 static int const gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
74
75 /* gzip flag byte */
76 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
77 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
78 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
79 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
80 #define COMMENT 0x10 /* bit 4 set: file comment present */
81 #define RESERVED 0xE0 /* bits 5..7: reserved */
82
83 typedef struct gz_stream {
84 z_stream stream;
85 int z_err; /* error code for last stream operation */
86 int z_eof; /* set if end of input file */
87 FILE *file; /* .gz file */
88 Byte *inbuf; /* input buffer */
89 Byte *outbuf; /* output buffer */
90 uLong crc; /* crc32 of uncompressed data */
91 char *msg; /* error message */
92 char *path; /* path name for debugging only */
93 int transparent; /* 1 if input file is not a .gz file */
94 char mode; /* 'w' or 'r' */
95 z_off_t start; /* start of compressed data in file (header skipped) */
96 z_off_t in; /* bytes into deflate or inflate */
97 z_off_t out; /* bytes out of deflate or inflate */
98 int back; /* one character push-back */
99 int last; /* true if push-back is last character */
100 } gz_stream;
101
102
103 local gzFile gz_open OF((const char *path, const char *mode, int fd));
104 local int do_flush OF((gzFile file, int flush));
105 local int get_byte OF((gz_stream *s));
106 local void check_header OF((gz_stream *s));
107 local int destroy OF((gz_stream *s));
108 local void putLong OF((FILE *file, uLong x));
109 local uLong getLong OF((gz_stream *s));
110
111 /* ===========================================================================
112 Opens a gzip (.gz) file for reading or writing. The mode parameter
113 is as in fopen ("rb" or "wb"). The file is given either by file descriptor
114 or path name (if fd == -1).
115 gz_open returns NULL if the file could not be opened or if there was
116 insufficient memory to allocate the (de)compression state; errno
117 can be checked to distinguish the two cases (if errno is zero, the
118 zlib error is Z_MEM_ERROR).
119 */
120 local gzFile
121 gz_open(const char *path, const char *mode, int fd)
122 {
123 int err;
124 int level = Z_DEFAULT_COMPRESSION; /* compression level */
125 int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */
126 char *p = (char*)mode;
127 gz_stream *s;
128 char fmode[80]; /* copy of mode, without the compression level */
129 char *m = fmode;
130
131 if (!path || !mode) return Z_NULL;
132
133 s = (gz_stream *)ALLOC(sizeof(gz_stream));
134 if (!s) return Z_NULL;
135
136 s->stream.zalloc = (alloc_func)0;
137 s->stream.zfree = (free_func)0;
138 s->stream.opaque = (voidpf)0;
139 s->stream.next_in = s->inbuf = Z_NULL;
140 s->stream.next_out = s->outbuf = Z_NULL;
141 s->stream.avail_in = s->stream.avail_out = 0;
142 s->file = NULL;
143 s->z_err = Z_OK;
144 s->z_eof = 0;
145 s->in = 0;
146 s->out = 0;
147 s->back = EOF;
148 s->crc = z_crc32(0L, Z_NULL, 0);
149 s->msg = NULL;
150 s->transparent = 0;
151
152 s->path = (char*)ALLOC(strlen(path)+1);
153 if (s->path == NULL) {
154 return destroy(s), (gzFile)Z_NULL;
155 }
156 strcpy(s->path, path); /* do this early for debugging */
157
158 s->mode = '\0';
159 do {
160 if (*p == 'r') s->mode = 'r';
161 if (*p == 'w' || *p == 'a') s->mode = 'w';
162 if (*p >= '0' && *p <= '9') {
163 level = *p - '0';
164 } else if (*p == 'f') {
165 strategy = Z_FILTERED;
166 } else if (*p == 'h') {
167 strategy = Z_HUFFMAN_ONLY;
168 } else if (*p == 'R') {
169 strategy = Z_RLE;
170 } else {
171 *m++ = *p; /* copy the mode */
172 }
173 } while (*p++ && m != fmode + sizeof(fmode));
174 if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
175
176 if (s->mode == 'w') {
177 #ifdef NO_GZCOMPRESS
178 err = Z_STREAM_ERROR;
179 #else
180 err = deflateInit2(&(s->stream), level,
181 Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy);
182 /* windowBits is passed < 0 to suppress zlib header */
183
184 s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
185 #endif
186 if (err != Z_OK || s->outbuf == Z_NULL) {
187 return destroy(s), (gzFile)Z_NULL;
188 }
189 } else {
190 s->stream.next_in = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
191
192 err = inflateInit2(&(s->stream), -MAX_WBITS);
193 /* windowBits is passed < 0 to tell that there is no zlib header.
194 * Note that in this case inflate *requires* an extra "dummy" byte
195 * after the compressed stream in order to complete decompression and
196 * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
197 * present after the compressed stream.
198 */
199 if (err != Z_OK || s->inbuf == Z_NULL) {
200 return destroy(s), (gzFile)Z_NULL;
201 }
202 }
203 s->stream.avail_out = Z_BUFSIZE;
204
205 errno = 0;
206 s->file = fd < 0 ? F_OPEN(path, fmode) : (FILE*)fdopen(fd, fmode);
207
208 if (s->file == NULL) {
209 return destroy(s), (gzFile)Z_NULL;
210 }
211 if (s->mode == 'w') {
212 /* Write a very simple .gz header:
213 */
214 fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
215 Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
216 s->start = 10L;
217 /* We use 10L instead of ftell(s->file) to because ftell causes an
218 * fflush on some systems. This version of the library doesn't use
219 * start anyway in write mode, so this initialization is not
220 * necessary.
221 */
222 } else {
223 check_header(s); /* skip the .gz header */
224 s->start = ftell(s->file) - s->stream.avail_in;
225 }
226
227 return (gzFile)s;
228 }
229
230 /* ===========================================================================
231 Opens a gzip (.gz) file for reading or writing.
232 */
233 gzFile ZEXPORT
234 gzopen(const char *path, const char *mode)
235 {
236 return gz_open (path, mode, -1);
237 }
238
239 /* ===========================================================================
240 Associate a gzFile with the file descriptor fd. fd is not dup'ed here
241 to mimic the behavio(u)r of fdopen.
242 */
243 gzFile ZEXPORT
244 gzdopen(int fd, const char *mode)
245 {
246 char name[46]; /* allow for up to 128-bit integers */
247
248 if (fd < 0) return (gzFile)Z_NULL;
249 sprintf(name, "<fd:%d>", fd); /* for debugging */
250
251 return gz_open (name, mode, fd);
252 }
253
254 /* ===========================================================================
255 * Update the compression level and strategy
256 */
257 int ZEXPORT
258 gzsetparams(gzFile file, int level, int strategy)
259 {
260 gz_stream *s = (gz_stream*)file;
261
262 if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
263
264 /* Make room to allow flushing */
265 if (s->stream.avail_out == 0) {
266
267 s->stream.next_out = s->outbuf;
268 if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
269 s->z_err = Z_ERRNO;
270 }
271 s->stream.avail_out = Z_BUFSIZE;
272 }
273
274 return deflateParams (&(s->stream), level, strategy);
275 }
276
277 /* ===========================================================================
278 Read a byte from a gz_stream; update next_in and avail_in. Return EOF
279 for end of file.
280 IN assertion: the stream s has been sucessfully opened for reading.
281 */
282 local int
283 get_byte(gz_stream *s)
284 {
285 if (s->z_eof) return EOF;
286 if (s->stream.avail_in == 0) {
287 errno = 0;
288 s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
289 if (s->stream.avail_in == 0) {
290 s->z_eof = 1;
291 if (ferror(s->file)) s->z_err = Z_ERRNO;
292 return EOF;
293 }
294 s->stream.next_in = s->inbuf;
295 }
296 s->stream.avail_in--;
297 return *(s->stream.next_in)++;
298 }
299
300 /* ===========================================================================
301 Check the gzip header of a gz_stream opened for reading. Set the stream
302 mode to transparent if the gzip magic header is not present; set s->err
303 to Z_DATA_ERROR if the magic header is present but the rest of the header
304 is incorrect.
305 IN assertion: the stream s has already been created sucessfully;
306 s->stream.avail_in is zero for the first time, but may be non-zero
307 for concatenated .gz files.
308 */
309 local void
310 check_header(gz_stream *s)
311 {
312 int method; /* method byte */
313 int flags; /* flags byte */
314 uInt len;
315 int c;
316
317 /* Assure two bytes in the buffer so we can peek ahead -- handle case
318 where first byte of header is at the end of the buffer after the last
319 gzip segment */
320 len = s->stream.avail_in;
321 if (len < 2) {
322 if (len) s->inbuf[0] = s->stream.next_in[0];
323 errno = 0;
324 len = (uInt)fread(s->inbuf + len, 1, Z_BUFSIZE >> len, s->file);
325 if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO;
326 s->stream.avail_in += len;
327 s->stream.next_in = s->inbuf;
328 if (s->stream.avail_in < 2) {
329 s->transparent = s->stream.avail_in;
330 return;
331 }
332 }
333
334 /* Peek ahead to check the gzip magic header */
335 if (s->stream.next_in[0] != gz_magic[0] ||
336 s->stream.next_in[1] != gz_magic[1]) {
337 s->transparent = 1;
338 return;
339 }
340 s->stream.avail_in -= 2;
341 s->stream.next_in += 2;
342
343 /* Check the rest of the gzip header */
344 method = get_byte(s);
345 flags = get_byte(s);
346 if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
347 s->z_err = Z_DATA_ERROR;
348 return;
349 }
350
351 /* Discard time, xflags and OS code: */
352 for (len = 0; len < 6; len++) (void)get_byte(s);
353
354 if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
355 len = (uInt)get_byte(s);
356 len += ((uInt)get_byte(s))<<8;
357 /* len is garbage if EOF but the loop below will quit anyway */
358 while (len-- != 0 && get_byte(s) != EOF) ;
359 }
360 if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
361 while ((c = get_byte(s)) != 0 && c != EOF) ;
362 }
363 if ((flags & COMMENT) != 0) { /* skip the .gz file comment */
364 while ((c = get_byte(s)) != 0 && c != EOF) ;
365 }
366 if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
367 for (len = 0; len < 2; len++) (void)get_byte(s);
368 }
369 s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
370 }
371
372 /* ===========================================================================
373 * Cleanup then free the given gz_stream. Return a zlib error code.
374 Try freeing in the reverse order of allocations.
375 */
376 local int
377 destroy(gz_stream *s)
378 {
379 int err = Z_OK;
380
381 if (!s) return Z_STREAM_ERROR;
382
383 TRYFREE(s->msg);
384
385 if (s->stream.state != NULL) {
386 if (s->mode == 'w') {
387 #ifdef NO_GZCOMPRESS
388 err = Z_STREAM_ERROR;
389 #else
390 err = deflateEnd(&(s->stream));
391 #endif
392 } else if (s->mode == 'r') {
393 err = inflateEnd(&(s->stream));
394 }
395 }
396 if (s->file != NULL && fclose(s->file)) {
397 #ifdef ESPIPE
398 if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */
399 #endif
400 err = Z_ERRNO;
401 }
402 if (s->z_err < 0) err = s->z_err;
403
404 TRYFREE(s->inbuf);
405 TRYFREE(s->outbuf);
406 TRYFREE(s->path);
407 TRYFREE(s);
408 return err;
409 }
410
411 /* ===========================================================================
412 Reads the given number of uncompressed bytes from the compressed file.
413 gzread returns the number of bytes actually read (0 for end of file).
414 */
415 int ZEXPORT
416 gzread(gzFile file, voidp buf, unsigned len)
417 {
418 gz_stream *s = (gz_stream*)file;
419 Bytef *start = (Bytef*)buf; /* starting point for crc computation */
420 Byte *next_out; /* == stream.next_out but not forced far (for MSDOS) */
421
422 if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
423
424 if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
425 if (s->z_err == Z_STREAM_END) return 0; /* EOF */
426
427 next_out = (Byte*)buf;
428 s->stream.next_out = (Bytef*)buf;
429 s->stream.avail_out = len;
430
431 if (s->stream.avail_out && s->back != EOF) {
432 *next_out++ = s->back;
433 s->stream.next_out++;
434 s->stream.avail_out--;
435 s->back = EOF;
436 s->out++;
437 start++;
438 if (s->last) {
439 s->z_err = Z_STREAM_END;
440 return 1;
441 }
442 }
443
444 while (s->stream.avail_out != 0) {
445
446 if (s->transparent) {
447 /* Copy first the lookahead bytes: */
448 uInt n = s->stream.avail_in;
449 if (n > s->stream.avail_out) n = s->stream.avail_out;
450 if (n > 0) {
451 zmemcpy(s->stream.next_out, s->stream.next_in, n);
452 next_out += n;
453 s->stream.next_out = next_out;
454 s->stream.next_in += n;
455 s->stream.avail_out -= n;
456 s->stream.avail_in -= n;
457 }
458 if (s->stream.avail_out > 0) {
459 s->stream.avail_out -=
460 (uInt)fread(next_out, 1, s->stream.avail_out, s->file);
461 }
462 len -= s->stream.avail_out;
463 s->in += len;
464 s->out += len;
465 if (len == 0) s->z_eof = 1;
466 return (int)len;
467 }
468 if (s->stream.avail_in == 0 && !s->z_eof) {
469
470 errno = 0;
471 s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
472 if (s->stream.avail_in == 0) {
473 s->z_eof = 1;
474 if (ferror(s->file)) {
475 s->z_err = Z_ERRNO;
476 break;
477 }
478 }
479 s->stream.next_in = s->inbuf;
480 }
481 s->in += s->stream.avail_in;
482 s->out += s->stream.avail_out;
483 s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
484 s->in -= s->stream.avail_in;
485 s->out -= s->stream.avail_out;
486
487 if (s->z_err == Z_STREAM_END) {
488 /* Check CRC and original size */
489 s->crc = z_crc32(s->crc, start, (uInt)(s->stream.next_out - start));
490 start = s->stream.next_out;
491
492 if (getLong(s) != s->crc) {
493 s->z_err = Z_DATA_ERROR;
494 } else {
495 (void)getLong(s);
496 /* The uncompressed length returned by above getlong() may be
497 * different from s->out in case of concatenated .gz files.
498 * Check for such files:
499 */
500 check_header(s);
501 if (s->z_err == Z_OK) {
502 inflateReset(&(s->stream));
503 s->crc = z_crc32(0L, Z_NULL, 0);
504 }
505 }
506 }
507 if (s->z_err != Z_OK || s->z_eof) break;
508 }
509 s->crc = z_crc32(s->crc, start, (uInt)(s->stream.next_out - start));
510
511 if (len == s->stream.avail_out &&
512 (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO))
513 return -1;
514 return (int)(len - s->stream.avail_out);
515 }
516
517
518 /* ===========================================================================
519 Reads one byte from the compressed file. gzgetc returns this byte
520 or -1 in case of end of file or error.
521 */
522 int ZEXPORT
523 gzgetc(gzFile file)
524 {
525 unsigned char c;
526
527 return gzread(file, &c, 1) == 1 ? c : -1;
528 }
529
530
531 /* ===========================================================================
532 Push one byte back onto the stream.
533 */
534 int ZEXPORT
535 gzungetc(int c, gzFile file)
536 {
537 gz_stream *s = (gz_stream*)file;
538
539 if (s == NULL || s->mode != 'r' || c == EOF || s->back != EOF) return EOF;
540 s->back = c;
541 s->out--;
542 s->last = (s->z_err == Z_STREAM_END);
543 if (s->last) s->z_err = Z_OK;
544 s->z_eof = 0;
545 return c;
546 }
547
548
549 /* ===========================================================================
550 Reads bytes from the compressed file until len-1 characters are
551 read, or a newline character is read and transferred to buf, or an
552 end-of-file condition is encountered. The string is then terminated
553 with a null character.
554 gzgets returns buf, or Z_NULL in case of error.
555
556 The current implementation is not optimized at all.
557 */
558 char * ZEXPORT
559 gzgets(gzFile file, char *buf, int len)
560 {
561 char *b = buf;
562 if (buf == Z_NULL || len <= 0) return Z_NULL;
563
564 while (--len > 0 && gzread(file, buf, 1) == 1 && *buf++ != '\n') ;
565 *buf = '\0';
566 return b == buf && len > 0 ? Z_NULL : b;
567 }
568
569
570 #ifndef NO_GZCOMPRESS
571 /* ===========================================================================
572 Writes the given number of uncompressed bytes into the compressed file.
573 gzwrite returns the number of bytes actually written (0 in case of error).
574 */
575 int ZEXPORT
576 gzwrite(gzFile file, voidpc buf, unsigned len)
577 {
578 gz_stream *s = (gz_stream*)file;
579
580 if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
581
582 s->stream.next_in = (Bytef*)buf;
583 s->stream.avail_in = len;
584
585 while (s->stream.avail_in != 0) {
586
587 if (s->stream.avail_out == 0) {
588
589 s->stream.next_out = s->outbuf;
590 if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
591 s->z_err = Z_ERRNO;
592 break;
593 }
594 s->stream.avail_out = Z_BUFSIZE;
595 }
596 s->in += s->stream.avail_in;
597 s->out += s->stream.avail_out;
598 s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
599 s->in -= s->stream.avail_in;
600 s->out -= s->stream.avail_out;
601 if (s->z_err != Z_OK) break;
602 }
603 s->crc = z_crc32(s->crc, (const Bytef *)buf, len);
604
605 return (int)(len - s->stream.avail_in);
606 }
607
608
609 /* ===========================================================================
610 Converts, formats, and writes the args to the compressed file under
611 control of the format string, as in fprintf. gzprintf returns the number of
612 uncompressed bytes actually written (0 in case of error).
613 */
614 #ifdef STDC
615 #include <stdarg.h>
616
617 int ZEXPORTVA
618 gzprintf(gzFile file, const char *format, /* args */ ...)
619 {
620 char buf[Z_PRINTF_BUFSIZE];
621 va_list va;
622 int len;
623
624 buf[sizeof(buf) - 1] = 0;
625 va_start(va, format);
626 #ifdef NO_vsnprintf
627 # ifdef HAS_vsprintf_void
628 (void)vsprintf(buf, format, va);
629 va_end(va);
630 for (len = 0; len < sizeof(buf); len++)
631 if (buf[len] == 0) break;
632 # else
633 len = vsprintf(buf, format, va);
634 va_end(va);
635 # endif
636 #else
637 # ifdef HAS_vsnprintf_void
638 (void)vsnprintf(buf, sizeof(buf), format, va);
639 va_end(va);
640 len = strlen(buf);
641 # else
642 len = vsnprintf(buf, sizeof(buf), format, va);
643 va_end(va);
644 # endif
645 #endif
646 if (len <= 0 || len >= (int)sizeof(buf) || buf[sizeof(buf) - 1] != 0)
647 return 0;
648 return gzwrite(file, buf, (unsigned)len);
649 }
650 #else /* not ANSI C */
651
652 int ZEXPORTVA
653 gzprintf(gzFile file, const char *format, int a1, int a2, int a3, int a4,
654 int a5, int a6, int a7, int a8, int a9, int a10, int a11, int a12,
655 int a13, int a14, int a15, int a16, int a17, int a18, int a19, int a20)
656 {
657 char buf[Z_PRINTF_BUFSIZE];
658 int len;
659
660 buf[sizeof(buf) - 1] = 0;
661 #ifdef NO_snprintf
662 # ifdef HAS_sprintf_void
663 sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
664 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
665 for (len = 0; len < sizeof(buf); len++)
666 if (buf[len] == 0) break;
667 # else
668 len = sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
669 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
670 # endif
671 #else
672 # ifdef HAS_snprintf_void
673 snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
674 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
675 len = strlen(buf);
676 # else
677 len = snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
678 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
679 # endif
680 #endif
681 if (len <= 0 || len >= sizeof(buf) || buf[sizeof(buf) - 1] != 0)
682 return 0;
683 return gzwrite(file, buf, len);
684 }
685 #endif
686
687 /* ===========================================================================
688 Writes c, converted to an unsigned char, into the compressed file.
689 gzputc returns the value that was written, or -1 in case of error.
690 */
691 int ZEXPORT
692 gzputc(gzFile file, int c)
693 {
694 unsigned char cc = (unsigned char) c; /* required for big endian systems */
695
696 return gzwrite(file, &cc, 1) == 1 ? (int)cc : -1;
697 }
698
699
700 /* ===========================================================================
701 Writes the given null-terminated string to the compressed file, excluding
702 the terminating null character.
703 gzputs returns the number of characters written, or -1 in case of error.
704 */
705 int ZEXPORT
706 gzputs(gzFile file, const char *s)
707 {
708 return gzwrite(file, (char*)s, (unsigned)strlen(s));
709 }
710
711
712 /* ===========================================================================
713 Flushes all pending output into the compressed file. The parameter
714 flush is as in the deflate() function.
715 */
716 local int
717 do_flush(gzFile file, int flush)
718 {
719 uInt len;
720 int done = 0;
721 gz_stream *s = (gz_stream*)file;
722
723 if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
724
725 s->stream.avail_in = 0; /* should be zero already anyway */
726
727 for (;;) {
728 len = Z_BUFSIZE - s->stream.avail_out;
729
730 if (len != 0) {
731 if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) {
732 s->z_err = Z_ERRNO;
733 return Z_ERRNO;
734 }
735 s->stream.next_out = s->outbuf;
736 s->stream.avail_out = Z_BUFSIZE;
737 }
738 if (done) break;
739 s->out += s->stream.avail_out;
740 s->z_err = deflate(&(s->stream), flush);
741 s->out -= s->stream.avail_out;
742
743 /* Ignore the second of two consecutive flushes: */
744 if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK;
745
746 /* deflate has finished flushing only when it hasn't used up
747 * all the available space in the output buffer:
748 */
749 done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END);
750
751 if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
752 }
753 return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
754 }
755
756 int ZEXPORT
757 gzflush(gzFile file, int flush)
758 {
759 gz_stream *s = (gz_stream*)file;
760 int err = do_flush (file, flush);
761
762 if (err) return err;
763 fflush(s->file);
764 return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
765 }
766 #endif /* NO_GZCOMPRESS */
767
768 /* ===========================================================================
769 Sets the starting position for the next gzread or gzwrite on the given
770 compressed file. The offset represents a number of bytes in the
771 gzseek returns the resulting offset location as measured in bytes from
772 the beginning of the uncompressed stream, or -1 in case of error.
773 SEEK_END is not implemented, returns error.
774 In this version of the library, gzseek can be extremely slow.
775 */
776 z_off_t ZEXPORT
777 gzseek(gzFile file, z_off_t offset, int whence)
778 {
779 gz_stream *s = (gz_stream*)file;
780
781 if (s == NULL || whence == SEEK_END ||
782 s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) {
783 return -1L;
784 }
785
786 if (s->mode == 'w') {
787 #ifdef NO_GZCOMPRESS
788 return -1L;
789 #else
790 if (whence == SEEK_SET) {
791 offset -= s->in;
792 }
793 if (offset < 0) return -1L;
794
795 /* At this point, offset is the number of zero bytes to write. */
796 if (s->inbuf == Z_NULL) {
797 s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */
798 if (s->inbuf == Z_NULL) return -1L;
799 zmemzero(s->inbuf, Z_BUFSIZE);
800 }
801 while (offset > 0) {
802 uInt size = Z_BUFSIZE;
803 if (offset < Z_BUFSIZE) size = (uInt)offset;
804
805 size = gzwrite(file, s->inbuf, size);
806 if (size == 0) return -1L;
807
808 offset -= size;
809 }
810 return s->in;
811 #endif
812 }
813 /* Rest of function is for reading only */
814
815 /* compute absolute position */
816 if (whence == SEEK_CUR) {
817 offset += s->out;
818 }
819 if (offset < 0) return -1L;
820
821 if (s->transparent) {
822 /* map to fseek */
823 s->back = EOF;
824 s->stream.avail_in = 0;
825 s->stream.next_in = s->inbuf;
826 if (fseek(s->file, offset, SEEK_SET) < 0) return -1L;
827
828 s->in = s->out = offset;
829 return offset;
830 }
831
832 /* For a negative seek, rewind and use positive seek */
833 if (offset >= s->out) {
834 offset -= s->out;
835 } else if (gzrewind(file) < 0) {
836 return -1L;
837 }
838 /* offset is now the number of bytes to skip. */
839
840 if (offset != 0 && s->outbuf == Z_NULL) {
841 s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
842 if (s->outbuf == Z_NULL) return -1L;
843 }
844 if (offset && s->back != EOF) {
845 s->back = EOF;
846 s->out++;
847 offset--;
848 if (s->last) s->z_err = Z_STREAM_END;
849 }
850 while (offset > 0) {
851 int size = Z_BUFSIZE;
852 if (offset < Z_BUFSIZE) size = (int)offset;
853
854 size = gzread(file, s->outbuf, (uInt)size);
855 if (size <= 0) return -1L;
856 offset -= size;
857 }
858 return s->out;
859 }
860
861 /* ===========================================================================
862 Rewinds input file.
863 */
864 int ZEXPORT
865 gzrewind(gzFile file)
866 {
867 gz_stream *s = (gz_stream*)file;
868
869 if (s == NULL || s->mode != 'r') return -1;
870
871 s->z_err = Z_OK;
872 s->z_eof = 0;
873 s->back = EOF;
874 s->stream.avail_in = 0;
875 s->stream.next_in = s->inbuf;
876 s->crc = z_crc32(0L, Z_NULL, 0);
877 if (!s->transparent) (void)inflateReset(&s->stream);
878 s->in = 0;
879 s->out = 0;
880 return fseek(s->file, s->start, SEEK_SET);
881 }
882
883 /* ===========================================================================
884 Returns the starting position for the next gzread or gzwrite on the
885 given compressed file. This position represents a number of bytes in the
886 uncompressed data stream.
887 */
888 z_off_t ZEXPORT
889 gztell(gzFile file)
890 {
891 return gzseek(file, 0L, SEEK_CUR);
892 }
893
894 /* ===========================================================================
895 Returns 1 when EOF has previously been detected reading the given
896 input stream, otherwise zero.
897 */
898 int ZEXPORT
899 gzeof(gzFile file)
900 {
901 gz_stream *s = (gz_stream*)file;
902
903 /* With concatenated compressed files that can have embedded
904 * crc trailers, z_eof is no longer the only/best indicator of EOF
905 * on a gz_stream. Handle end-of-stream error explicitly here.
906 */
907 if (s == NULL || s->mode != 'r') return 0;
908 if (s->z_eof) return 1;
909 return s->z_err == Z_STREAM_END;
910 }
911
912 /* ===========================================================================
913 Returns 1 if reading and doing so transparently, otherwise zero.
914 */
915 int ZEXPORT
916 gzdirect(gzFile file)
917 {
918 gz_stream *s = (gz_stream*)file;
919
920 if (s == NULL || s->mode != 'r') return 0;
921 return s->transparent;
922 }
923
924 /* ===========================================================================
925 Outputs a long in LSB order to the given file
926 */
927 local void
928 putLong(FILE *file, uLong x)
929 {
930 int n;
931 for (n = 0; n < 4; n++) {
932 fputc((int)(x & 0xff), file);
933 x >>= 8;
934 }
935 }
936
937 /* ===========================================================================
938 Reads a long in LSB order from the given gz_stream. Sets z_err in case
939 of error.
940 */
941 local uLong
942 getLong(gz_stream *s)
943 {
944 uLong x = (uLong)get_byte(s);
945 int c;
946
947 x += ((uLong)get_byte(s))<<8;
948 x += ((uLong)get_byte(s))<<16;
949 c = get_byte(s);
950 if (c == EOF) s->z_err = Z_DATA_ERROR;
951 x += ((uLong)c)<<24;
952 return x;
953 }
954
955 /* ===========================================================================
956 Flushes all pending output if necessary, closes the compressed file
957 and deallocates all the (de)compression state.
958 */
959 int ZEXPORT
960 gzclose(gzFile file)
961 {
962 gz_stream *s = (gz_stream*)file;
963
964 if (s == NULL) return Z_STREAM_ERROR;
965
966 if (s->mode == 'w') {
967 #ifdef NO_GZCOMPRESS
968 return Z_STREAM_ERROR;
969 #else
970 if (do_flush (file, Z_FINISH) != Z_OK)
971 return destroy((gz_stream*)file);
972
973 putLong (s->file, s->crc);
974 putLong (s->file, (uLong)(s->in & 0xffffffff));
975 #endif
976 }
977 return destroy((gz_stream*)file);
978 }
979
980 #ifdef STDC
981 # define zstrerror(errnum) strerror(errnum)
982 #else
983 # define zstrerror(errnum) ""
984 #endif
985
986 /* ===========================================================================
987 Returns the error message for the last error which occurred on the
988 given compressed file. errnum is set to zlib error number. If an
989 error occurred in the file system and not in the compression library,
990 errnum is set to Z_ERRNO and the application may consult errno
991 to get the exact error code.
992 */
993 const char * ZEXPORT
994 gzerror(gzFile file, int *errnum)
995 {
996 char *m;
997 gz_stream *s = (gz_stream*)file;
998
999 if (s == NULL) {
1000 *errnum = Z_STREAM_ERROR;
1001 return (const char*)ERR_MSG(Z_STREAM_ERROR);
1002 }
1003 *errnum = s->z_err;
1004 if (*errnum == Z_OK) return (const char*)"";
1005
1006 m = (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg);
1007
1008 if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err);
1009
1010 TRYFREE(s->msg);
1011 s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3);
1012 if (s->msg == Z_NULL) return (const char*)ERR_MSG(Z_MEM_ERROR);
1013 strcpy(s->msg, s->path);
1014 strcat(s->msg, ": ");
1015 strcat(s->msg, m);
1016 return (const char*)s->msg;
1017 }
1018
1019 /* ===========================================================================
1020 Clear the error and end-of-file flags, and do the same for the real file.
1021 */
1022 void ZEXPORT
1023 gzclearerr(gzFile file)
1024 {
1025 gz_stream *s = (gz_stream*)file;
1026
1027 if (s == NULL) return;
1028 if (s->z_err != Z_STREAM_END) s->z_err = Z_OK;
1029 s->z_eof = 0;
1030 clearerr(s->file);
1031 }