1 /* gzio.c -- IO on .gz files
2 * Copyright (C) 1995-2003 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.
14 #ifdef NO_DEFLATE /* for compatiblity with old definition */
15 # define NO_GZCOMPRESS
19 struct internal_state {int dummy;}; /* for buggy compilers */
24 # define Z_BUFSIZE 4096 /* minimize memory usage for 16-bit DOS */
26 # define Z_BUFSIZE 16384
29 #ifndef Z_PRINTF_BUFSIZE
30 # define Z_PRINTF_BUFSIZE 4096
34 # pragma map (fdopen , "\174\174FDOPEN")
35 FILE *fdopen(int, const char *);
39 extern voidp malloc OF((uInt size));
40 extern void free OF((voidpf ptr));
43 #define ALLOC(size) malloc(size)
44 #define TRYFREE(p) {if (p) free(p);}
46 /*@unchecked@*/ /*@observer@*/
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 /*@dependent@*/ /*@relnull@*/
62 FILE *file; /* .gz file */
64 Byte *inbuf; /* input buffer */
66 Byte *outbuf; /* output buffer */
67 uLong crc; /* crc32 of uncompressed data */
69 char *msg; /* error message */
71 char *path; /* path name for debugging only */
72 int transparent; /* 1 if input file is not a .gz file */
73 char mode; /* 'w' or 'r' */
74 z_off_t start; /* start of compressed data in file (header skipped) */
75 z_off_t in; /* bytes into deflate or inflate */
76 z_off_t out; /* bytes out of deflate or inflate */
77 int back; /* one character push-back */
78 int last; /* true if push-back is last character */
83 local gzFile gz_open OF((const char *path, const char *mode, int fd))
84 /*@globals errno, fileSystem @*/
85 /*@modifies errno, fileSystem @*/;
86 local int do_flush OF((gzFile file, int flush))
87 /*@globals fileSystem @*/
88 /*@modifies fileSystem @*/;
89 local int get_byte OF((gz_stream *s))
90 /*@globals errno, fileSystem @*/
91 /*@modifies s, errno, fileSystem @*/;
92 local void check_header OF((gz_stream *s))
93 /*@globals errno, fileSystem @*/
94 /*@modifies s, errno, fileSystem @*/;
95 local int destroy OF((gz_stream *s))
96 /*@globals fileSystem @*/
97 /*@modifies s, fileSystem @*/;
98 local void putLong OF((FILE *file, uLong x))
99 /*@globals fileSystem @*/
100 /*@modifies file, fileSystem @*/;
101 local uLong getLong OF((gz_stream *s))
102 /*@globals fileSystem @*/
103 /*@modifies s, fileSystem @*/;
105 /* ===========================================================================
106 Opens a gzip (.gz) file for reading or writing. The mode parameter
107 is as in fopen ("rb" or "wb"). The file is given either by file descriptor
108 or path name (if fd == -1).
109 gz_open returns NULL if the file could not be opened or if there was
110 insufficient memory to allocate the (de)compression state; errno
111 can be checked to distinguish the two cases (if errno is zero, the
112 zlib error is Z_MEM_ERROR).
114 local gzFile gz_open (const char *path, const char *mode, int fd)
117 int level = Z_DEFAULT_COMPRESSION; /* compression level */
118 int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */
119 char *p = (char*)mode;
121 char fmode[80]; /* copy of mode, without the compression level */
124 if (!path || !mode) return Z_NULL;
126 s = (gz_stream *)ALLOC(sizeof(gz_stream));
127 if (!s) return Z_NULL;
129 s->stream.zalloc = (alloc_func)0;
130 s->stream.zfree = (free_func)0;
131 s->stream.opaque = (voidpf)0;
132 s->stream.next_in = s->inbuf = Z_NULL;
133 s->stream.next_out = s->outbuf = Z_NULL;
134 s->stream.avail_in = s->stream.avail_out = 0;
141 s->crc = crc32(0L, Z_NULL, 0);
145 s->path = (char*)ALLOC(strlen(path)+1);
146 if (s->path == NULL) {
147 return destroy(s), (gzFile)Z_NULL;
149 strcpy(s->path, path); /* do this early for debugging */
153 if (*p == 'r') s->mode = 'r';
154 if (*p == 'w' || *p == 'a') s->mode = 'w';
155 if (*p >= '0' && *p <= '9') {
157 } else if (*p == 'f') {
158 strategy = Z_FILTERED;
159 } else if (*p == 'h') {
160 strategy = Z_HUFFMAN_ONLY;
161 } else if (*p == 'R') {
164 *m++ = *p; /* copy the mode */
166 } while (*p++ && m != fmode + sizeof(fmode));
167 if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
169 if (s->mode == 'w') {
171 err = Z_STREAM_ERROR;
173 err = deflateInit2(&(s->stream), level,
174 Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, strategy);
175 /* windowBits is passed < 0 to suppress zlib header */
177 s->stream.next_out = s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
179 if (err != Z_OK || s->outbuf == Z_NULL) {
180 return destroy(s), (gzFile)Z_NULL;
183 s->stream.next_in = s->inbuf = (Byte*)ALLOC(Z_BUFSIZE);
185 err = inflateInit2(&(s->stream), -MAX_WBITS);
186 /* windowBits is passed < 0 to tell that there is no zlib header.
187 * Note that in this case inflate *requires* an extra "dummy" byte
188 * after the compressed stream in order to complete decompression and
189 * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
190 * present after the compressed stream.
192 if (err != Z_OK || s->inbuf == Z_NULL) {
193 return destroy(s), (gzFile)Z_NULL;
196 s->stream.avail_out = Z_BUFSIZE;
199 s->file = fd < 0 ? F_OPEN(path, fmode) : (FILE*)fdopen(fd, fmode);
201 if (s->file == NULL) {
202 return destroy(s), (gzFile)Z_NULL;
204 if (s->mode == 'w') {
205 /* Write a very simple .gz header:
207 fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
208 Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
210 /* We use 10L instead of ftell(s->file) to because ftell causes an
211 * fflush on some systems. This version of the library doesn't use
212 * start anyway in write mode, so this initialization is not
216 check_header(s); /* skip the .gz header */
217 s->start = ftell(s->file) - s->stream.avail_in;
223 /* ===========================================================================
224 Opens a gzip (.gz) file for reading or writing.
226 gzFile ZEXPORT gzopen (const char *path, const char *mode)
228 return gz_open (path, mode, -1);
231 /* ===========================================================================
232 Associate a gzFile with the file descriptor fd. fd is not dup'ed here
233 to mimic the behavio(u)r of fdopen.
235 gzFile ZEXPORT gzdopen (int fd, const char *mode)
239 if (fd < 0) return (gzFile)Z_NULL;
240 sprintf(name, "<fd:%d>", fd); /* for debugging */
242 return gz_open (name, mode, fd);
245 /* ===========================================================================
246 * Update the compression level and strategy
248 int ZEXPORT gzsetparams (gzFile file, int level, int strategy)
250 gz_stream *s = (gz_stream*)file;
252 if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
254 /* Make room to allow flushing */
255 if (s->stream.avail_out == 0) {
257 s->stream.next_out = s->outbuf;
258 if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
261 s->stream.avail_out = Z_BUFSIZE;
264 return deflateParams (&(s->stream), level, strategy);
267 /* ===========================================================================
268 Read a byte from a gz_stream; update next_in and avail_in. Return EOF
270 IN assertion: the stream s has been sucessfully opened for reading.
272 local int get_byte(gz_stream *s)
274 if (s->z_eof) return EOF;
275 if (s->stream.avail_in == 0) {
277 s->stream.avail_in = fread(s->inbuf, 1, Z_BUFSIZE, s->file);
278 if (s->stream.avail_in == 0) {
280 if (ferror(s->file)) s->z_err = Z_ERRNO;
283 s->stream.next_in = s->inbuf;
285 s->stream.avail_in--;
286 return *(s->stream.next_in)++;
289 /* ===========================================================================
290 Check the gzip header of a gz_stream opened for reading. Set the stream
291 mode to transparent if the gzip magic header is not present; set s->err
292 to Z_DATA_ERROR if the magic header is present but the rest of the header
294 IN assertion: the stream s has already been created sucessfully;
295 s->stream.avail_in is zero for the first time, but may be non-zero
296 for concatenated .gz files.
298 local void check_header(gz_stream *s)
300 int method; /* method byte */
301 int flags; /* flags byte */
305 /* Assure two bytes in the buffer so we can peek ahead -- handle case
306 where first byte of header is at the end of the buffer after the last
308 len = s->stream.avail_in;
310 if (len) s->inbuf[0] = s->stream.next_in[0];
312 len = fread(s->inbuf + len, 1, Z_BUFSIZE >> len, s->file);
313 if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO;
314 s->stream.avail_in += len;
315 s->stream.next_in = s->inbuf;
316 if (s->stream.avail_in < 2) {
317 s->transparent = s->stream.avail_in;
322 /* Peek ahead to check the gzip magic header */
323 if (s->stream.next_in[0] != gz_magic[0] ||
324 s->stream.next_in[1] != gz_magic[1]) {
328 s->stream.avail_in -= 2;
329 s->stream.next_in += 2;
331 /* Check the rest of the gzip header */
332 method = get_byte(s);
334 if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
335 s->z_err = Z_DATA_ERROR;
339 /* Discard time, xflags and OS code: */
340 for (len = 0; len < 6; len++) (void)get_byte(s);
342 if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
343 len = (uInt)get_byte(s);
344 len += ((uInt)get_byte(s))<<8;
345 /* len is garbage if EOF but the loop below will quit anyway */
346 while (len-- != 0 && get_byte(s) != EOF) ;
348 if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
349 while ((c = get_byte(s)) != 0 && c != EOF) ;
351 if ((flags & COMMENT) != 0) { /* skip the .gz file comment */
352 while ((c = get_byte(s)) != 0 && c != EOF) ;
354 if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
355 for (len = 0; len < 2; len++) (void)get_byte(s);
357 s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
360 /* ===========================================================================
361 * Cleanup then free the given gz_stream. Return a zlib error code.
362 Try freeing in the reverse order of allocations.
364 local int destroy (gz_stream *s)
368 if (!s) return Z_STREAM_ERROR;
372 if (s->stream.state != NULL) {
373 if (s->mode == 'w') {
375 err = Z_STREAM_ERROR;
377 err = deflateEnd(&(s->stream));
379 } else if (s->mode == 'r') {
380 err = inflateEnd(&(s->stream));
383 if (s->file != NULL && fclose(s->file)) {
385 if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */
389 if (s->z_err < 0) err = s->z_err;
398 /* ===========================================================================
399 Reads the given number of uncompressed bytes from the compressed file.
400 gzread returns the number of bytes actually read (0 for end of file).
402 int ZEXPORT gzread (gzFile file, voidp buf, unsigned len)
404 gz_stream *s = (gz_stream*)file;
405 Bytef *start = (Bytef*)buf; /* starting point for crc computation */
406 Byte *next_out; /* == stream.next_out but not forced far (for MSDOS) */
408 if (s == NULL || s->mode != 'r') return Z_STREAM_ERROR;
410 if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO) return -1;
411 if (s->z_err == Z_STREAM_END) return 0; /* EOF */
413 next_out = (Byte*)buf;
414 s->stream.next_out = (Bytef*)buf;
415 s->stream.avail_out = len;
417 if (s->stream.avail_out && s->back != EOF) {
418 *next_out++ = s->back;
419 s->stream.next_out++;
420 s->stream.avail_out--;
424 s->z_err = Z_STREAM_END;
429 while (s->stream.avail_out != 0) {
431 if (s->transparent) {
432 /* Copy first the lookahead bytes: */
433 uInt n = s->stream.avail_in;
434 if (n > s->stream.avail_out) n = s->stream.avail_out;
436 zmemcpy(s->stream.next_out, s->stream.next_in, n);
438 s->stream.next_out = next_out;
439 s->stream.next_in += n;
440 s->stream.avail_out -= n;
441 s->stream.avail_in -= n;
443 if (s->stream.avail_out > 0) {
444 s->stream.avail_out -= fread(next_out, 1, s->stream.avail_out,
447 len -= s->stream.avail_out;
450 if (len == 0) s->z_eof = 1;
453 if (s->stream.avail_in == 0 && !s->z_eof) {
456 s->stream.avail_in = fread(s->inbuf, 1, Z_BUFSIZE, s->file);
457 if (s->stream.avail_in == 0) {
459 if (ferror(s->file)) {
463 if (feof(s->file)) { /* avoid error for empty file */
464 s->z_err = Z_STREAM_END;
468 s->stream.next_in = s->inbuf;
470 s->in += s->stream.avail_in;
471 s->out += s->stream.avail_out;
472 s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
473 s->in -= s->stream.avail_in;
474 s->out -= s->stream.avail_out;
476 if (s->z_err == Z_STREAM_END) {
477 /* Check CRC and original size */
478 s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
479 start = s->stream.next_out;
481 if (getLong(s) != s->crc) {
482 s->z_err = Z_DATA_ERROR;
485 /* The uncompressed length returned by above getlong() may be
486 * different from s->out in case of concatenated .gz files.
487 * Check for such files:
490 if (s->z_err == Z_OK) {
491 inflateReset(&(s->stream));
492 s->crc = crc32(0L, Z_NULL, 0);
496 if (s->z_err != Z_OK || s->z_eof) break;
498 s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
500 return (int)(len - s->stream.avail_out);
504 /* ===========================================================================
505 Reads one byte from the compressed file. gzgetc returns this byte
506 or -1 in case of end of file or error.
508 int ZEXPORT gzgetc(gzFile file)
512 return gzread(file, &c, 1) == 1 ? c : -1;
516 /* ===========================================================================
517 Push one byte back onto the stream.
519 int ZEXPORT gzungetc(int c, gzFile file)
521 gz_stream *s = (gz_stream*)file;
523 if (s == NULL || s->mode != 'r' || c == EOF || s->back != EOF) return EOF;
526 s->last = (s->z_err == Z_STREAM_END);
527 if (s->last) s->z_err = Z_OK;
533 /* ===========================================================================
534 Reads bytes from the compressed file until len-1 characters are
535 read, or a newline character is read and transferred to buf, or an
536 end-of-file condition is encountered. The string is then terminated
537 with a null character.
538 gzgets returns buf, or Z_NULL in case of error.
540 The current implementation is not optimized at all.
542 char * ZEXPORT gzgets(gzFile file, char *buf, int len)
545 if (buf == Z_NULL || len <= 0) return Z_NULL;
547 while (--len > 0 && gzread(file, buf, 1) == 1 && *buf++ != '\n') ;
549 return b == buf && len > 0 ? Z_NULL : b;
553 #ifndef NO_GZCOMPRESS
554 /* ===========================================================================
555 Writes the given number of uncompressed bytes into the compressed file.
556 gzwrite returns the number of bytes actually written (0 in case of error).
558 int ZEXPORT gzwrite (gzFile file, voidpc buf, unsigned len)
560 gz_stream *s = (gz_stream*)file;
562 if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
564 s->stream.next_in = (Bytef*)buf;
565 s->stream.avail_in = len;
567 while (s->stream.avail_in != 0) {
569 if (s->stream.avail_out == 0) {
571 s->stream.next_out = s->outbuf;
572 if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
576 s->stream.avail_out = Z_BUFSIZE;
578 s->in += s->stream.avail_in;
579 s->out += s->stream.avail_out;
580 s->z_err = deflate(&(s->stream), Z_NO_FLUSH);
581 s->in -= s->stream.avail_in;
582 s->out -= s->stream.avail_out;
583 if (s->z_err != Z_OK) break;
585 s->crc = crc32(s->crc, (const Bytef *)buf, len);
587 return (int)(len - s->stream.avail_in);
591 /* ===========================================================================
592 Converts, formats, and writes the args to the compressed file under
593 control of the format string, as in fprintf. gzprintf returns the number of
594 uncompressed bytes actually written (0 in case of error).
599 int ZEXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...)
601 char buf[Z_PRINTF_BUFSIZE];
605 buf[sizeof(buf) - 1] = 0;
606 va_start(va, format);
608 # ifdef HAS_vsprintf_void
609 (void)vsprintf(buf, format, va);
611 for (len = 0; len < sizeof(buf); len++)
612 if (buf[len] == 0) break;
614 len = vsprintf(buf, format, va);
618 # ifdef HAS_vsnprintf_void
619 (void)vsnprintf(buf, sizeof(buf), format, va);
623 len = vsnprintf(buf, sizeof(buf), format, va);
627 if (len <= 0 || len >= (int)sizeof(buf) || buf[sizeof(buf) - 1] != 0)
629 return gzwrite(file, buf, (unsigned)len);
631 #else /* not ANSI C */
633 int ZEXPORTVA gzprintf (gzFile file, const char *format,
634 int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10,
635 int a11, int a12, int a13, int a14, int a15, int a16, int a17, int a18, int a19, int a20)
637 char buf[Z_PRINTF_BUFSIZE];
640 buf[sizeof(buf) - 1] = 0;
642 # ifdef HAS_sprintf_void
643 sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
644 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
645 for (len = 0; len < sizeof(buf); len++)
646 if (buf[len] == 0) break;
648 len = sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
649 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
652 # ifdef HAS_snprintf_void
653 snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
654 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
657 len = snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
658 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
661 if (len <= 0 || len >= sizeof(buf) || buf[sizeof(buf) - 1] != 0)
663 return gzwrite(file, buf, len);
667 /* ===========================================================================
668 Writes c, converted to an unsigned char, into the compressed file.
669 gzputc returns the value that was written, or -1 in case of error.
671 int ZEXPORT gzputc(gzFile file, int c)
673 unsigned char cc = (unsigned char) c; /* required for big endian systems */
675 return gzwrite(file, &cc, 1) == 1 ? (int)cc : -1;
679 /* ===========================================================================
680 Writes the given null-terminated string to the compressed file, excluding
681 the terminating null character.
682 gzputs returns the number of characters written, or -1 in case of error.
684 int ZEXPORT gzputs(gzFile file, const char *s)
686 return gzwrite(file, (char*)s, (unsigned)strlen(s));
690 /* ===========================================================================
691 Flushes all pending output into the compressed file. The parameter
692 flush is as in the deflate() function.
694 local int do_flush (gzFile file, int flush)
698 gz_stream *s = (gz_stream*)file;
700 if (s == NULL || s->mode != 'w') return Z_STREAM_ERROR;
702 s->stream.avail_in = 0; /* should be zero already anyway */
705 len = Z_BUFSIZE - s->stream.avail_out;
708 if ((uInt)fwrite(s->outbuf, 1, len, s->file) != len) {
712 s->stream.next_out = s->outbuf;
713 s->stream.avail_out = Z_BUFSIZE;
716 s->out += s->stream.avail_out;
717 s->z_err = deflate(&(s->stream), flush);
718 s->out -= s->stream.avail_out;
720 /* Ignore the second of two consecutive flushes: */
721 if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK;
723 /* deflate has finished flushing only when it hasn't used up
724 * all the available space in the output buffer:
726 done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END);
728 if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
730 return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
733 int ZEXPORT gzflush (gzFile file, int flush)
735 gz_stream *s = (gz_stream*)file;
736 int err = do_flush (file, flush);
740 return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
742 #endif /* NO_GZCOMPRESS */
744 /* ===========================================================================
745 Sets the starting position for the next gzread or gzwrite on the given
746 compressed file. The offset represents a number of bytes in the
747 gzseek returns the resulting offset location as measured in bytes from
748 the beginning of the uncompressed stream, or -1 in case of error.
749 SEEK_END is not implemented, returns error.
750 In this version of the library, gzseek can be extremely slow.
752 z_off_t ZEXPORT gzseek (gzFile file, z_off_t offset, int whence)
754 gz_stream *s = (gz_stream*)file;
756 if (s == NULL || whence == SEEK_END ||
757 s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) {
761 if (s->mode == 'w') {
765 if (whence == SEEK_SET) {
768 if (offset < 0) return -1L;
770 /* At this point, offset is the number of zero bytes to write. */
771 if (s->inbuf == Z_NULL) {
772 s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */
773 if (s->inbuf == Z_NULL) return -1L;
774 zmemzero(s->inbuf, Z_BUFSIZE);
777 uInt size = Z_BUFSIZE;
778 if (offset < Z_BUFSIZE) size = (uInt)offset;
780 size = gzwrite(file, s->inbuf, size);
781 if (size == 0) return -1L;
788 /* Rest of function is for reading only */
790 /* compute absolute position */
791 if (whence == SEEK_CUR) {
794 if (offset < 0) return -1L;
796 if (s->transparent) {
799 s->stream.avail_in = 0;
800 s->stream.next_in = s->inbuf;
801 if (fseek(s->file, offset, SEEK_SET) < 0) return -1L;
803 s->in = s->out = offset;
807 /* For a negative seek, rewind and use positive seek */
808 if (offset >= s->out) {
810 } else if (gzrewind(file) < 0) {
813 /* offset is now the number of bytes to skip. */
815 if (offset != 0 && s->outbuf == Z_NULL) {
816 s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
817 if (s->outbuf == Z_NULL) return -1L;
819 if (offset && s->back != EOF) {
823 if (s->last) s->z_err = Z_STREAM_END;
826 int size = Z_BUFSIZE;
827 if (offset < Z_BUFSIZE) size = (int)offset;
829 size = gzread(file, s->outbuf, (uInt)size);
830 if (size <= 0) return -1L;
836 /* ===========================================================================
839 int ZEXPORT gzrewind (gzFile file)
841 gz_stream *s = (gz_stream*)file;
843 if (s == NULL || s->mode != 'r') return -1;
848 s->stream.avail_in = 0;
849 s->stream.next_in = s->inbuf;
850 s->crc = crc32(0L, Z_NULL, 0);
851 if (!s->transparent) (void)inflateReset(&s->stream);
854 return fseek(s->file, s->start, SEEK_SET);
857 /* ===========================================================================
858 Returns the starting position for the next gzread or gzwrite on the
859 given compressed file. This position represents a number of bytes in the
860 uncompressed data stream.
862 z_off_t ZEXPORT gztell (gzFile file)
864 return gzseek(file, 0L, SEEK_CUR);
867 /* ===========================================================================
868 Returns 1 when EOF has previously been detected reading the given
869 input stream, otherwise zero.
871 int ZEXPORT gzeof (gzFile file)
873 gz_stream *s = (gz_stream*)file;
875 /* With concatenated compressed files that can have embedded
876 * crc trailers, z_eof is no longer the only/best indicator of EOF
877 * on a gz_stream. Handle end-of-stream error explicitly here.
879 if (s == NULL || s->mode != 'r') return 0;
880 if (s->z_eof) return 1;
881 return s->z_err == Z_STREAM_END;
884 /* ===========================================================================
885 Outputs a long in LSB order to the given file
887 local void putLong (FILE *file, uLong x)
890 for (n = 0; n < 4; n++) {
891 fputc((int)(x & 0xff), file);
896 /* ===========================================================================
897 Reads a long in LSB order from the given gz_stream. Sets z_err in case
900 local uLong getLong (gz_stream *s)
902 uLong x = (uLong)get_byte(s);
905 x += ((uLong)get_byte(s))<<8;
906 x += ((uLong)get_byte(s))<<16;
908 if (c == EOF) s->z_err = Z_DATA_ERROR;
913 /* ===========================================================================
914 Flushes all pending output if necessary, closes the compressed file
915 and deallocates all the (de)compression state.
917 int ZEXPORT gzclose (gzFile file)
920 gz_stream *s = (gz_stream*)file;
922 if (s == NULL) return Z_STREAM_ERROR;
924 if (s->mode == 'w') {
926 return Z_STREAM_ERROR;
928 err = do_flush (file, Z_FINISH);
929 if (err != Z_OK) return destroy((gz_stream*)file);
931 putLong (s->file, s->crc);
932 putLong (s->file, (uLong)(s->in & 0xffffffff));
935 return destroy((gz_stream*)file);
938 /* ===========================================================================
939 Returns the error message for the last error which occured on the
940 given compressed file. errnum is set to zlib error number. If an
941 error occured in the file system and not in the compression library,
942 errnum is set to Z_ERRNO and the application may consult errno
943 to get the exact error code.
945 const char * ZEXPORT gzerror (gzFile file, int *errnum)
948 gz_stream *s = (gz_stream*)file;
951 *errnum = Z_STREAM_ERROR;
952 return (const char*)ERR_MSG(Z_STREAM_ERROR);
955 if (*errnum == Z_OK) return (const char*)"";
957 m = (char*)(*errnum == Z_ERRNO ? zstrerror(errno) : s->stream.msg);
959 if (m == NULL || *m == '\0') m = (char*)ERR_MSG(s->z_err);
962 s->msg = (char*)ALLOC(strlen(s->path) + strlen(m) + 3);
963 if (s->msg == Z_NULL) return (const char*)ERR_MSG(Z_MEM_ERROR);
964 strcpy(s->msg, s->path);
965 strcat(s->msg, ": ");
967 return (const char*)s->msg;
970 /* ===========================================================================
971 Clear the error and end-of-file flags, and do the same for the real file.
973 void ZEXPORT gzclearerr (gzFile file)
975 gz_stream *s = (gz_stream*)file;
977 if (s == NULL) return;
978 if (s->z_err != Z_STREAM_END) s->z_err = Z_OK;