1 /* gzio.c -- IO on .gz files
2 * Copyright (C) 1995-2005 Jean-loup Gailly.
3 * For conditions of distribution and use, see copyright notice in zlib.h
5 * Compile this file with -DNO_GZCOMPRESS to avoid the compression code.
8 /* @(#) $Id: ./src/external/zlib-1.2.3/gzio.c, 2011/09/08 dcid Exp $
15 #ifdef NO_DEFLATE /* for compatibility with old definition */
16 # define NO_GZCOMPRESS
20 struct internal_state {int dummy;}; /* for buggy compilers */
25 # define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
27 # define Z_BUFSIZE 16384
30 #ifndef Z_PRINTF_BUFSIZE
31 # define Z_PRINTF_BUFSIZE 4096
35 # pragma map (fdopen , "\174\174FDOPEN")
36 FILE *fdopen(int, const char *);
40 extern voidp malloc OF((uInt size));
41 extern void free OF((voidpf ptr));
44 #define ALLOC(size) malloc(size)
45 #define TRYFREE(p) {if (p) free(p);}
47 static int const gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
50 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
51 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
52 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
53 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
54 #define COMMENT 0x10 /* bit 4 set: file comment present */
55 #define RESERVED 0xE0 /* bits 5..7: reserved */
57 typedef struct gz_stream {
59 int z_err; /* error code for last stream operation */
60 int z_eof; /* set if end of input file */
61 FILE *file; /* .gz file */
62 Byte *inbuf; /* input buffer */
63 Byte *outbuf; /* output buffer */
64 uLong crc; /* crc32 of uncompressed data */
65 char *msg; /* error message */
66 char *path; /* path name for debugging only */
67 int transparent; /* 1 if input file is not a .gz file */
68 char mode; /* 'w' or 'r' */
69 z_off_t start; /* start of compressed data in file (header skipped) */
70 z_off_t in; /* bytes into deflate or inflate */
71 z_off_t out; /* bytes out of deflate or inflate */
72 int back; /* one character push-back */
73 int last; /* true if push-back is last character */
77 local gzFile gz_open OF((const char *path, const char *mode, int fd));
78 local int do_flush OF((gzFile file, int flush));
79 local int get_byte OF((gz_stream *s));
80 local void check_header OF((gz_stream *s));
81 local int destroy OF((gz_stream *s));
82 local void putLong OF((FILE *file, uLong x));
83 local uLong getLong OF((gz_stream *s));
85 /* ===========================================================================
86 Opens a gzip (.gz) file for reading or writing. The mode parameter
87 is as in fopen ("rb" or "wb"). The file is given either by file descriptor
88 or path name (if fd == -1).
89 gz_open returns NULL if the file could not be opened or if there was
90 insufficient memory to allocate the (de)compression state; errno
91 can be checked to distinguish the two cases (if errno is zero, the
92 zlib error is Z_MEM_ERROR).
94 local gzFile gz_open (path, mode, fd)
101 int level = Z_DEFAULT_COMPRESSION; /* compression level */
102 int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */
103 char *p = (char*)mode;
105 char fmode[80]; /* copy of mode, without the compression level */
108 if (!path || !mode) return Z_NULL;
110 s = (gz_stream *)ALLOC(sizeof(gz_stream));
111 if (!s) return Z_NULL;
113 s->stream.zalloc = (alloc_func)0;
114 s->stream.zfree = (free_func)0;
115 s->stream.opaque = (voidpf)0;
116 s->stream.next_in = s->inbuf = Z_NULL;
117 s->stream.next_out = s->outbuf = Z_NULL;
118 s->stream.avail_in = s->stream.avail_out = 0;
125 s->crc = crc32(0L, Z_NULL, 0);
129 path_size = strlen(path) +1;
131 s->path = (char*)ALLOC(path_size +1);
132 if (s->path == NULL) {
133 return destroy(s), (gzFile)Z_NULL;
135 s->path[path_size] = '\0';
136 strncpy(s->path, path, path_size); /* do this early for debugging */
140 if (*p == 'r') s->mode = 'r';
141 if (*p == 'w' || *p == 'a') s->mode = 'w';
142 if (*p >= '0' && *p <= '9') {
144 } else if (*p == 'f') {
145 strategy = Z_FILTERED;
146 } else if (*p == 'h') {
147 strategy = Z_HUFFMAN_ONLY;
148 } else if (*p == 'R') {
151 *m++ = *p; /* copy the mode */
153 } while (*p++ && m != fmode + sizeof(fmode));
154 if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
156 if (s->mode == 'w') {
158 err = Z_STREAM_ERROR;
160 err = deflateInit2(&(s->stream), level,
161 Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy);
162 /* windowBits is passed < 0 to suppress zlib header */
164 s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
166 if (err != Z_OK || s->outbuf == Z_NULL) {
167 return destroy(s), (gzFile)Z_NULL;
170 s->stream.next_in = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
172 err = inflateInit2(&(s->stream), -MAX_WBITS);
173 /* windowBits is passed < 0 to tell that there is no zlib header.
174 * Note that in this case inflate *requires* an extra "dummy" byte
175 * after the compressed stream in order to complete decompression and
176 * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
177 * present after the compressed stream.
179 if (err != Z_OK || s->inbuf == Z_NULL) {
180 return destroy(s), (gzFile)Z_NULL;
183 s->stream.avail_out = Z_BUFSIZE;
186 s->file = fd < 0 ? F_OPEN(path, fmode) : (FILE*)fdopen(fd, fmode);
188 if (s->file == NULL) {
189 return destroy(s), (gzFile)Z_NULL;
191 if (s->mode == 'w') {
192 /* Write a very simple .gz header:
194 fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
195 Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
197 /* We use 10L instead of ftell(s->file) to because ftell causes an
198 * fflush on some systems. This version of the library doesn't use
199 * start anyway in write mode, so this initialization is not
203 check_header(s); /* skip the .gz header */
204 s->start = ftell(s->file) - s->stream.avail_in;
210 /* ===========================================================================
211 Opens a gzip (.gz) file for reading or writing.
213 gzFile ZEXPORT gzopen (path, mode)
217 return gz_open (path, mode, -1);
220 /* ===========================================================================
221 Associate a gzFile with the file descriptor fd. fd is not dup'ed here
222 to mimic the behavio(u)r of fdopen.
224 gzFile ZEXPORT gzdopen (fd, mode)
228 char name[46]; /* allow for up to 128-bit integers */
230 if (fd < 0) return (gzFile)Z_NULL;
232 snprintf(name, 45, "<fd:%d>", fd); /* for debugging */
234 return gz_open (name, mode, fd);
237 /* ===========================================================================
238 * Update the compression level and strategy
240 int ZEXPORT gzsetparams (file, level, strategy)
245 gz_stream *s = (gz_stream*)file;
247 if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
249 /* Make room to allow flushing */
250 if (s->stream.avail_out == 0) {
252 s->stream.next_out = s->outbuf;
253 if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
256 s->stream.avail_out = Z_BUFSIZE;
259 return deflateParams (&(s->stream), level, strategy);
262 /* ===========================================================================
263 Read a byte from a gz_stream; update next_in and avail_in. Return EOF
265 IN assertion: the stream s has been sucessfully opened for reading.
267 local int get_byte(s)
270 if (s->z_eof) return EOF;
271 if (s->stream.avail_in == 0) {
273 s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
274 if (s->stream.avail_in == 0) {
276 if (ferror(s->file)) s->z_err = Z_ERRNO;
279 s->stream.next_in = s->inbuf;
281 s->stream.avail_in--;
282 return *(s->stream.next_in)++;
285 /* ===========================================================================
286 Check the gzip header of a gz_stream opened for reading. Set the stream
287 mode to transparent if the gzip magic header is not present; set s->err
288 to Z_DATA_ERROR if the magic header is present but the rest of the header
290 IN assertion: the stream s has already been created sucessfully;
291 s->stream.avail_in is zero for the first time, but may be non-zero
292 for concatenated .gz files.
294 local void check_header(s)
297 int method; /* method byte */
298 int flags; /* flags byte */
302 /* Assure two bytes in the buffer so we can peek ahead -- handle case
303 where first byte of header is at the end of the buffer after the last
305 len = s->stream.avail_in;
307 if (len) s->inbuf[0] = s->stream.next_in[0];
309 len = (uInt)fread(s->inbuf + len, 1, Z_BUFSIZE >> len, s->file);
310 if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO;
311 s->stream.avail_in += len;
312 s->stream.next_in = s->inbuf;
313 if (s->stream.avail_in < 2) {
314 s->transparent = s->stream.avail_in;
319 /* Peek ahead to check the gzip magic header */
320 if (s->stream.next_in[0] != gz_magic[0] ||
321 s->stream.next_in[1] != gz_magic[1]) {
325 s->stream.avail_in -= 2;
326 s->stream.next_in += 2;
328 /* Check the rest of the gzip header */
329 method = get_byte(s);
331 if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
332 s->z_err = Z_DATA_ERROR;
336 /* Discard time, xflags and OS code: */
337 for (len = 0; len < 6; len++) (void)get_byte(s);
339 if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
340 len = (uInt)get_byte(s);
341 len += ((uInt)get_byte(s))<<8;
342 /* len is garbage if EOF but the loop below will quit anyway */
343 while (len-- != 0 && get_byte(s) != EOF) ;
345 if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
346 while ((c = get_byte(s)) != 0 && c != EOF) ;
348 if ((flags & COMMENT) != 0) { /* skip the .gz file comment */
349 while ((c = get_byte(s)) != 0 && c != EOF) ;
351 if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
352 for (len = 0; len < 2; len++) (void)get_byte(s);
354 s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
357 /* ===========================================================================
358 * Cleanup then free the given gz_stream. Return a zlib error code.
359 Try freeing in the reverse order of allocations.
361 local int destroy (s)
366 if (!s) return Z_STREAM_ERROR;
370 if (s->stream.state != NULL) {
371 if (s->mode == 'w') {
373 err = Z_STREAM_ERROR;
375 err = deflateEnd(&(s->stream));
377 } else if (s->mode == 'r') {
378 err = inflateEnd(&(s->stream));
381 if (s->file != NULL && fclose(s->file)) {
383 if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */
387 if (s->z_err < 0) err = s->z_err;
396 /* ===========================================================================
397 Reads the given number of uncompressed bytes from the compressed file.
398 gzread returns the number of bytes actually read (0 for end of file).
400 int ZEXPORT gzread (file, buf, len)
405 gz_stream *s = (gz_stream*)file;
406 Bytef *start = (Bytef*)buf; /* starting point for crc computation */
407 Byte *next_out; /* == stream.next_out but not forced far (for MSDOS) */
409 if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
411 if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
412 if (s->z_err == Z_STREAM_END) return 0; /* EOF */
414 next_out = (Byte*)buf;
415 s->stream.next_out = (Bytef*)buf;
416 s->stream.avail_out = len;
418 if (s->stream.avail_out && s->back != EOF) {
419 *next_out++ = s->back;
420 s->stream.next_out++;
421 s->stream.avail_out--;
426 s->z_err = Z_STREAM_END;
431 while (s->stream.avail_out != 0) {
433 if (s->transparent) {
434 /* Copy first the lookahead bytes: */
435 uInt n = s->stream.avail_in;
436 if (n > s->stream.avail_out) n = s->stream.avail_out;
438 zmemcpy(s->stream.next_out, s->stream.next_in, n);
440 s->stream.next_out = next_out;
441 s->stream.next_in += n;
442 s->stream.avail_out -= n;
443 s->stream.avail_in -= n;
445 if (s->stream.avail_out > 0) {
446 s->stream.avail_out -=
447 (uInt)fread(next_out, 1, s->stream.avail_out, s->file);
449 len -= s->stream.avail_out;
452 if (len == 0) s->z_eof = 1;
455 if (s->stream.avail_in == 0 && !s->z_eof) {
458 s->stream.avail_in = (uInt)fread(s->inbuf, 1, Z_BUFSIZE, s->file);
459 if (s->stream.avail_in == 0) {
461 if (ferror(s->file)) {
466 s->stream.next_in = s->inbuf;
468 s->in += s->stream.avail_in;
469 s->out += s->stream.avail_out;
470 s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
471 s->in -= s->stream.avail_in;
472 s->out -= s->stream.avail_out;
474 if (s->z_err == Z_STREAM_END) {
475 /* Check CRC and original size */
476 s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
477 start = s->stream.next_out;
479 if (getLong(s) != s->crc) {
480 s->z_err = Z_DATA_ERROR;
483 /* The uncompressed length returned by above getlong() may be
484 * different from s->out in case of concatenated .gz files.
485 * Check for such files:
488 if (s->z_err == Z_OK) {
489 inflateReset(&(s->stream));
490 s->crc = crc32(0L, Z_NULL, 0);
494 if (s->z_err != Z_OK || s->z_eof) break;
496 s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
498 if (len == s->stream.avail_out &&
499 (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO))
501 return (int)(len - s->stream.avail_out);
505 /* ===========================================================================
506 Reads one byte from the compressed file. gzgetc returns this byte
507 or -1 in case of end of file or error.
509 int ZEXPORT gzgetc(file)
514 return gzread(file, &c, 1) == 1 ? c : -1;
518 /* ===========================================================================
519 Push one byte back onto the stream.
521 int ZEXPORT gzungetc(c, file)
525 gz_stream *s = (gz_stream*)file;
527 if (s == NULL || s->mode != 'r' || c == EOF || s->back != EOF) return EOF;
530 s->last = (s->z_err == Z_STREAM_END);
531 if (s->last) s->z_err = Z_OK;
537 /* ===========================================================================
538 Reads bytes from the compressed file until len-1 characters are
539 read, or a newline character is read and transferred to buf, or an
540 end-of-file condition is encountered. The string is then terminated
541 with a null character.
542 gzgets returns buf, or Z_NULL in case of error.
544 The current implementation is not optimized at all.
546 char * ZEXPORT gzgets(file, buf, len)
552 if (buf == Z_NULL || len <= 0) return Z_NULL;
554 while (--len > 0 && gzread(file, buf, 1) == 1 && *buf++ != '\n') ;
556 return b == buf && len > 0 ? Z_NULL : b;
560 #ifndef NO_GZCOMPRESS
561 /* ===========================================================================
562 Writes the given number of uncompressed bytes into the compressed file.
563 gzwrite returns the number of bytes actually written (0 in case of error).
565 int ZEXPORT gzwrite (file, buf, len)
570 gz_stream *s = (gz_stream*)file;
572 if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
574 s->stream.next_in = (Bytef*)buf;
575 s->stream.avail_in = len;
577 while (s->stream.avail_in != 0) {
579 if (s->stream.avail_out == 0) {
581 s->stream.next_out = s->outbuf;
582 if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
586 s->stream.avail_out = Z_BUFSIZE;
588 s->in += s->stream.avail_in;
589 s->out += s->stream.avail_out;
590 s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
591 s->in -= s->stream.avail_in;
592 s->out -= s->stream.avail_out;
593 if (s->z_err != Z_OK) break;
595 s->crc = crc32(s->crc, (const Bytef *)buf, len);
597 return (int)(len - s->stream.avail_in);
601 /* ===========================================================================
602 Converts, formats, and writes the args to the compressed file under
603 control of the format string, as in fprintf. gzprintf returns the number of
604 uncompressed bytes actually written (0 in case of error).
609 int ZEXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...)
611 char buf[Z_PRINTF_BUFSIZE];
615 buf[sizeof(buf) - 1] = 0;
616 va_start(va, format);
618 # ifdef HAS_vsprintf_void
619 (void)vsprintf(buf, format, va);
621 for (len = 0; len < sizeof(buf); len++)
622 if (buf[len] == 0) break;
624 len = vsprintf(buf, format, va);
628 # ifdef HAS_vsnprintf_void
629 (void)vsnprintf(buf, sizeof(buf), format, va);
633 len = vsnprintf(buf, sizeof(buf), format, va);
637 if (len <= 0 || len >= (int)sizeof(buf) || buf[sizeof(buf) - 1] != 0)
639 return gzwrite(file, buf, (unsigned)len);
641 #else /* not ANSI C */
643 int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
644 a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
647 int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
648 a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
650 char buf[Z_PRINTF_BUFSIZE];
653 buf[sizeof(buf) - 1] = 0;
655 # ifdef HAS_sprintf_void
656 sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
657 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
658 for (len = 0; len < sizeof(buf); len++)
659 if (buf[len] == 0) break;
661 len = sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
662 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
665 # ifdef HAS_snprintf_void
666 snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
667 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
670 len = snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
671 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
674 if (len <= 0 || len >= sizeof(buf) || buf[sizeof(buf) - 1] != 0)
676 return gzwrite(file, buf, len);
680 /* ===========================================================================
681 Writes c, converted to an unsigned char, into the compressed file.
682 gzputc returns the value that was written, or -1 in case of error.
684 int ZEXPORT gzputc(file, c)
688 unsigned char cc = (unsigned char) c; /* required for big endian systems */
690 return gzwrite(file, &cc, 1) == 1 ? (int)cc : -1;
694 /* ===========================================================================
695 Writes the given null-terminated string to the compressed file, excluding
696 the terminating null character.
697 gzputs returns the number of characters written, or -1 in case of error.
699 int ZEXPORT gzputs(file, s)
703 return gzwrite(file, (char*)s, (unsigned)strlen(s));
707 /* ===========================================================================
708 Flushes all pending output into the compressed file. The parameter
709 flush is as in the deflate() function.
711 local int do_flush (file, flush)
717 gz_stream *s = (gz_stream*)file;
719 if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
721 s->stream.avail_in = 0; /* should be zero already anyway */
724 len = Z_BUFSIZE - s->stream.avail_out;
727 if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) {
731 s->stream.next_out = s->outbuf;
732 s->stream.avail_out = Z_BUFSIZE;
735 s->out += s->stream.avail_out;
736 s->z_err = deflate(&(s->stream), flush);
737 s->out -= s->stream.avail_out;
739 /* Ignore the second of two consecutive flushes: */
740 if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK;
742 /* deflate has finished flushing only when it hasn't used up
743 * all the available space in the output buffer:
745 done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END);
747 if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
749 return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
752 int ZEXPORT gzflush (file, flush)
756 gz_stream *s = (gz_stream*)file;
757 int err = do_flush (file, flush);
761 return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
763 #endif /* NO_GZCOMPRESS */
765 /* ===========================================================================
766 Sets the starting position for the next gzread or gzwrite on the given
767 compressed file. The offset represents a number of bytes in the
768 gzseek returns the resulting offset location as measured in bytes from
769 the beginning of the uncompressed stream, or -1 in case of error.
770 SEEK_END is not implemented, returns error.
771 In this version of the library, gzseek can be extremely slow.
773 z_off_t ZEXPORT gzseek (file, offset, whence)
778 gz_stream *s = (gz_stream*)file;
780 if (s == NULL || whence == SEEK_END ||
781 s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) {
785 if (s->mode == 'w') {
789 if (whence == SEEK_SET) {
792 if (offset < 0) return -1L;
794 /* At this point, offset is the number of zero bytes to write. */
795 if (s->inbuf == Z_NULL) {
796 s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */
797 if (s->inbuf == Z_NULL) return -1L;
798 zmemzero(s->inbuf, Z_BUFSIZE);
801 uInt size = Z_BUFSIZE;
802 if (offset < Z_BUFSIZE) size = (uInt)offset;
804 size = gzwrite(file, s->inbuf, size);
805 if (size == 0) return -1L;
812 /* Rest of function is for reading only */
814 /* compute absolute position */
815 if (whence == SEEK_CUR) {
818 if (offset < 0) return -1L;
820 if (s->transparent) {
823 s->stream.avail_in = 0;
824 s->stream.next_in = s->inbuf;
825 if (fseek(s->file, offset, SEEK_SET) < 0) return -1L;
827 s->in = s->out = offset;
831 /* For a negative seek, rewind and use positive seek */
832 if (offset >= s->out) {
834 } else if (gzrewind(file) < 0) {
837 /* offset is now the number of bytes to skip. */
839 if (offset != 0 && s->outbuf == Z_NULL) {
840 s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
841 if (s->outbuf == Z_NULL) return -1L;
843 if (offset && s->back != EOF) {
847 if (s->last) s->z_err = Z_STREAM_END;
850 int size = Z_BUFSIZE;
851 if (offset < Z_BUFSIZE) size = (int)offset;
853 size = gzread(file, s->outbuf, (uInt)size);
854 if (size <= 0) return -1L;
860 /* ===========================================================================
863 int ZEXPORT gzrewind (file)
866 gz_stream *s = (gz_stream*)file;
868 if (s == NULL || s->mode != 'r') return -1;
873 s->stream.avail_in = 0;
874 s->stream.next_in = s->inbuf;
875 s->crc = crc32(0L, Z_NULL, 0);
876 if (!s->transparent) (void)inflateReset(&s->stream);
879 return fseek(s->file, s->start, SEEK_SET);
882 /* ===========================================================================
883 Returns the starting position for the next gzread or gzwrite on the
884 given compressed file. This position represents a number of bytes in the
885 uncompressed data stream.
887 z_off_t ZEXPORT gztell (file)
890 return gzseek(file, 0L, SEEK_CUR);
893 /* ===========================================================================
894 Returns 1 when EOF has previously been detected reading the given
895 input stream, otherwise zero.
897 int ZEXPORT gzeof (file)
900 gz_stream *s = (gz_stream*)file;
902 /* With concatenated compressed files that can have embedded
903 * crc trailers, z_eof is no longer the only/best indicator of EOF
904 * on a gz_stream. Handle end-of-stream error explicitly here.
906 if (s == NULL || s->mode != 'r') return 0;
907 if (s->z_eof) return 1;
908 return s->z_err == Z_STREAM_END;
911 /* ===========================================================================
912 Returns 1 if reading and doing so transparently, otherwise zero.
914 int ZEXPORT gzdirect (file)
917 gz_stream *s = (gz_stream*)file;
919 if (s == NULL || s->mode != 'r') return 0;
920 return s->transparent;
923 /* ===========================================================================
924 Outputs a long in LSB order to the given file
926 local void putLong (file, x)
931 for (n = 0; n < 4; n++) {
932 fputc((int)(x & 0xff), file);
937 /* ===========================================================================
938 Reads a long in LSB order from the given gz_stream. Sets z_err in case
941 local uLong getLong (s)
944 uLong x = (uLong)get_byte(s);
947 x += ((uLong)get_byte(s))<<8;
948 x += ((uLong)get_byte(s))<<16;
950 if (c == EOF) s->z_err = Z_DATA_ERROR;
955 /* ===========================================================================
956 Flushes all pending output if necessary, closes the compressed file
957 and deallocates all the (de)compression state.
959 int ZEXPORT gzclose (file)
962 gz_stream *s = (gz_stream*)file;
964 if (s == NULL) return Z_STREAM_ERROR;
966 if (s->mode == 'w') {
968 return Z_STREAM_ERROR;
970 if (do_flush (file, Z_FINISH) != Z_OK)
971 return destroy((gz_stream*)file);
973 putLong (s->file, s->crc);
974 putLong (s->file, (uLong)(s->in & 0xffffffff));
977 return destroy((gz_stream*)file);
981 # define zstrerror(errnum) strerror(errnum)
983 # define zstrerror(errnum) ""
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.
993 const char * ZEXPORT gzerror (file, errnum)
999 gz_stream *s = (gz_stream*)file;
1002 *errnum = Z_STREAM_ERROR;
1003 return (const char*)ERR_MSG(Z_STREAM_ERROR);
1006 if (*errnum == Z_OK) return (const char*)"";
1008 m = (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg);
1010 if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err);
1013 msg_size = strlen(s->path) + strlen(m) + 4;
1015 s->msg = (char*)ALLOC(msg_size +1);
1016 if (s->msg == Z_NULL) return (const char*)ERR_MSG(Z_MEM_ERROR);
1018 s->msg[msg_size] = '\0';
1019 snprintf(s->msg, msg_size, "%s: %s", s->path, m);
1020 return (const char*)s->msg;
1023 /* ===========================================================================
1024 Clear the error and end-of-file flags, and do the same for the real file.
1026 void ZEXPORT gzclearerr (file)
1029 gz_stream *s = (gz_stream*)file;
1031 if (s == NULL) return;
1032 if (s->z_err != Z_STREAM_END) s->z_err = Z_OK;