summaryrefslogtreecommitdiff
path: root/gzio.c
diff options
context:
space:
mode:
Diffstat (limited to 'gzio.c')
-rw-r--r--gzio.c298
1 files changed, 150 insertions, 148 deletions
diff --git a/gzio.c b/gzio.c
index 3337353..1f561ac 100644
--- a/gzio.c
+++ b/gzio.c
@@ -117,17 +117,19 @@ local gzFile gz_open (path, mode, fd)
117 if (*p == 'r') s->mode = 'r'; 117 if (*p == 'r') s->mode = 'r';
118 if (*p == 'w' || *p == 'a') s->mode = 'w'; 118 if (*p == 'w' || *p == 'a') s->mode = 'w';
119 if (*p >= '0' && *p <= '9') { 119 if (*p >= '0' && *p <= '9') {
120 level = *p - '0'; 120 level = *p - '0';
121 } else if (*p == 'f') { 121 } else if (*p == 'f') {
122 strategy = Z_FILTERED; 122 strategy = Z_FILTERED;
123 } else if (*p == 'h') { 123 } else if (*p == 'h') {
124 strategy = Z_HUFFMAN_ONLY; 124 strategy = Z_HUFFMAN_ONLY;
125 } else { 125 } else if (*p == 'R') {
126 *m++ = *p; /* copy the mode */ 126 strategy = Z_RLE;
127 } 127 } else {
128 *m++ = *p; /* copy the mode */
129 }
128 } while (*p++ && m != fmode + sizeof(fmode)); 130 } while (*p++ && m != fmode + sizeof(fmode));
129 if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL; 131 if (s->mode == '\0') return destroy(s), (gzFile)Z_NULL;
130 132
131 if (s->mode == 'w') { 133 if (s->mode == 'w') {
132#ifdef NO_DEFLATE 134#ifdef NO_DEFLATE
133 err = Z_STREAM_ERROR; 135 err = Z_STREAM_ERROR;
@@ -168,17 +170,17 @@ local gzFile gz_open (path, mode, fd)
168 */ 170 */
169 fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1], 171 fprintf(s->file, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1],
170 Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE); 172 Z_DEFLATED, 0 /*flags*/, 0,0,0,0 /*time*/, 0 /*xflags*/, OS_CODE);
171 s->startpos = 10L; 173 s->startpos = 10L;
172 /* We use 10L instead of ftell(s->file) to because ftell causes an 174 /* We use 10L instead of ftell(s->file) to because ftell causes an
173 * fflush on some systems. This version of the library doesn't use 175 * fflush on some systems. This version of the library doesn't use
174 * startpos anyway in write mode, so this initialization is not 176 * startpos anyway in write mode, so this initialization is not
175 * necessary. 177 * necessary.
176 */ 178 */
177 } else { 179 } else {
178 check_header(s); /* skip the .gz header */ 180 check_header(s); /* skip the .gz header */
179 s->startpos = (ftell(s->file) - s->stream.avail_in); 181 s->startpos = (ftell(s->file) - s->stream.avail_in);
180 } 182 }
181 183
182 return (gzFile)s; 184 return (gzFile)s;
183} 185}
184 186
@@ -223,11 +225,11 @@ int ZEXPORT gzsetparams (file, level, strategy)
223 /* Make room to allow flushing */ 225 /* Make room to allow flushing */
224 if (s->stream.avail_out == 0) { 226 if (s->stream.avail_out == 0) {
225 227
226 s->stream.next_out = s->outbuf; 228 s->stream.next_out = s->outbuf;
227 if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) { 229 if (fwrite(s->outbuf, 1, Z_BUFSIZE, s->file) != Z_BUFSIZE) {
228 s->z_err = Z_ERRNO; 230 s->z_err = Z_ERRNO;
229 } 231 }
230 s->stream.avail_out = Z_BUFSIZE; 232 s->stream.avail_out = Z_BUFSIZE;
231 } 233 }
232 234
233 return deflateParams (&(s->stream), level, strategy); 235 return deflateParams (&(s->stream), level, strategy);
@@ -243,14 +245,14 @@ local int get_byte(s)
243{ 245{
244 if (s->z_eof) return EOF; 246 if (s->z_eof) return EOF;
245 if (s->stream.avail_in == 0) { 247 if (s->stream.avail_in == 0) {
246 errno = 0; 248 errno = 0;
247 s->stream.avail_in = fread(s->inbuf, 1, Z_BUFSIZE, s->file); 249 s->stream.avail_in = fread(s->inbuf, 1, Z_BUFSIZE, s->file);
248 if (s->stream.avail_in == 0) { 250 if (s->stream.avail_in == 0) {
249 s->z_eof = 1; 251 s->z_eof = 1;
250 if (ferror(s->file)) s->z_err = Z_ERRNO; 252 if (ferror(s->file)) s->z_err = Z_ERRNO;
251 return EOF; 253 return EOF;
252 } 254 }
253 s->stream.next_in = s->inbuf; 255 s->stream.next_in = s->inbuf;
254 } 256 }
255 s->stream.avail_in--; 257 s->stream.avail_in--;
256 return *(s->stream.next_in)++; 258 return *(s->stream.next_in)++;
@@ -292,7 +294,7 @@ local void check_header(s)
292 294
293 /* Peek ahead to check the gzip magic header */ 295 /* Peek ahead to check the gzip magic header */
294 if (s->stream.next_in[0] != gz_magic[0] || 296 if (s->stream.next_in[0] != gz_magic[0] ||
295 s->stream.next_in[1] != gz_magic[1]) { 297 s->stream.next_in[1] != gz_magic[1]) {
296 s->transparent = 1; 298 s->transparent = 1;
297 return; 299 return;
298 } 300 }
@@ -303,27 +305,27 @@ local void check_header(s)
303 method = get_byte(s); 305 method = get_byte(s);
304 flags = get_byte(s); 306 flags = get_byte(s);
305 if (method != Z_DEFLATED || (flags & RESERVED) != 0) { 307 if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
306 s->z_err = Z_DATA_ERROR; 308 s->z_err = Z_DATA_ERROR;
307 return; 309 return;
308 } 310 }
309 311
310 /* Discard time, xflags and OS code: */ 312 /* Discard time, xflags and OS code: */
311 for (len = 0; len < 6; len++) (void)get_byte(s); 313 for (len = 0; len < 6; len++) (void)get_byte(s);
312 314
313 if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */ 315 if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
314 len = (uInt)get_byte(s); 316 len = (uInt)get_byte(s);
315 len += ((uInt)get_byte(s))<<8; 317 len += ((uInt)get_byte(s))<<8;
316 /* len is garbage if EOF but the loop below will quit anyway */ 318 /* len is garbage if EOF but the loop below will quit anyway */
317 while (len-- != 0 && get_byte(s) != EOF) ; 319 while (len-- != 0 && get_byte(s) != EOF) ;
318 } 320 }
319 if ((flags & ORIG_NAME) != 0) { /* skip the original file name */ 321 if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
320 while ((c = get_byte(s)) != 0 && c != EOF) ; 322 while ((c = get_byte(s)) != 0 && c != EOF) ;
321 } 323 }
322 if ((flags & COMMENT) != 0) { /* skip the .gz file comment */ 324 if ((flags & COMMENT) != 0) { /* skip the .gz file comment */
323 while ((c = get_byte(s)) != 0 && c != EOF) ; 325 while ((c = get_byte(s)) != 0 && c != EOF) ;
324 } 326 }
325 if ((flags & HEAD_CRC) != 0) { /* skip the header crc */ 327 if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
326 for (len = 0; len < 2; len++) (void)get_byte(s); 328 for (len = 0; len < 2; len++) (void)get_byte(s);
327 } 329 }
328 s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK; 330 s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
329} 331}
@@ -342,21 +344,21 @@ local int destroy (s)
342 TRYFREE(s->msg); 344 TRYFREE(s->msg);
343 345
344 if (s->stream.state != NULL) { 346 if (s->stream.state != NULL) {
345 if (s->mode == 'w') { 347 if (s->mode == 'w') {
346#ifdef NO_DEFLATE 348#ifdef NO_DEFLATE
347 err = Z_STREAM_ERROR; 349 err = Z_STREAM_ERROR;
348#else 350#else
349 err = deflateEnd(&(s->stream)); 351 err = deflateEnd(&(s->stream));
350#endif 352#endif
351 } else if (s->mode == 'r') { 353 } else if (s->mode == 'r') {
352 err = inflateEnd(&(s->stream)); 354 err = inflateEnd(&(s->stream));
353 } 355 }
354 } 356 }
355 if (s->file != NULL && fclose(s->file)) { 357 if (s->file != NULL && fclose(s->file)) {
356#ifdef ESPIPE 358#ifdef ESPIPE
357 if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */ 359 if (errno != ESPIPE) /* fclose is broken for pipes in HP/UX */
358#endif 360#endif
359 err = Z_ERRNO; 361 err = Z_ERRNO;
360 } 362 }
361 if (s->z_err < 0) err = s->z_err; 363 if (s->z_err < 0) err = s->z_err;
362 364
@@ -391,69 +393,69 @@ int ZEXPORT gzread (file, buf, len)
391 393
392 while (s->stream.avail_out != 0) { 394 while (s->stream.avail_out != 0) {
393 395
394 if (s->transparent) { 396 if (s->transparent) {
395 /* Copy first the lookahead bytes: */ 397 /* Copy first the lookahead bytes: */
396 uInt n = s->stream.avail_in; 398 uInt n = s->stream.avail_in;
397 if (n > s->stream.avail_out) n = s->stream.avail_out; 399 if (n > s->stream.avail_out) n = s->stream.avail_out;
398 if (n > 0) { 400 if (n > 0) {
399 zmemcpy(s->stream.next_out, s->stream.next_in, n); 401 zmemcpy(s->stream.next_out, s->stream.next_in, n);
400 next_out += n; 402 next_out += n;
401 s->stream.next_out = next_out; 403 s->stream.next_out = next_out;
402 s->stream.next_in += n; 404 s->stream.next_in += n;
403 s->stream.avail_out -= n; 405 s->stream.avail_out -= n;
404 s->stream.avail_in -= n; 406 s->stream.avail_in -= n;
405 } 407 }
406 if (s->stream.avail_out > 0) { 408 if (s->stream.avail_out > 0) {
407 s->stream.avail_out -= fread(next_out, 1, s->stream.avail_out, 409 s->stream.avail_out -= fread(next_out, 1, s->stream.avail_out,
408 s->file); 410 s->file);
409 } 411 }
410 len -= s->stream.avail_out; 412 len -= s->stream.avail_out;
411 s->stream.total_in += (uLong)len; 413 s->stream.total_in += (uLong)len;
412 s->stream.total_out += (uLong)len; 414 s->stream.total_out += (uLong)len;
413 if (len == 0) s->z_eof = 1; 415 if (len == 0) s->z_eof = 1;
414 return (int)len; 416 return (int)len;
415 } 417 }
416 if (s->stream.avail_in == 0 && !s->z_eof) { 418 if (s->stream.avail_in == 0 && !s->z_eof) {
417 419
418 errno = 0; 420 errno = 0;
419 s->stream.avail_in = fread(s->inbuf, 1, Z_BUFSIZE, s->file); 421 s->stream.avail_in = fread(s->inbuf, 1, Z_BUFSIZE, s->file);
420 if (s->stream.avail_in == 0) { 422 if (s->stream.avail_in == 0) {
421 s->z_eof = 1; 423 s->z_eof = 1;
422 if (ferror(s->file)) { 424 if (ferror(s->file)) {
423 s->z_err = Z_ERRNO; 425 s->z_err = Z_ERRNO;
424 break; 426 break;
425 } 427 }
426 } 428 }
427 s->stream.next_in = s->inbuf; 429 s->stream.next_in = s->inbuf;
428 } 430 }
429 s->z_err = inflate(&(s->stream), Z_NO_FLUSH); 431 s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
430 432
431 if (s->z_err == Z_STREAM_END) { 433 if (s->z_err == Z_STREAM_END) {
432 /* Check CRC and original size */ 434 /* Check CRC and original size */
433 s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start)); 435 s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
434 start = s->stream.next_out; 436 start = s->stream.next_out;
435 437
436 if (getLong(s) != s->crc) { 438 if (getLong(s) != s->crc) {
437 s->z_err = Z_DATA_ERROR; 439 s->z_err = Z_DATA_ERROR;
438 } else { 440 } else {
439 (void)getLong(s); 441 (void)getLong(s);
440 /* The uncompressed length returned by above getlong() may 442 /* The uncompressed length returned by above getlong() may
441 * be different from s->stream.total_out) in case of 443 * be different from s->stream.total_out) in case of
442 * concatenated .gz files. Check for such files: 444 * concatenated .gz files. Check for such files:
443 */ 445 */
444 check_header(s); 446 check_header(s);
445 if (s->z_err == Z_OK) { 447 if (s->z_err == Z_OK) {
446 uLong total_in = s->stream.total_in; 448 uLong total_in = s->stream.total_in;
447 uLong total_out = s->stream.total_out; 449 uLong total_out = s->stream.total_out;
448 450
449 inflateReset(&(s->stream)); 451 inflateReset(&(s->stream));
450 s->stream.total_in = total_in; 452 s->stream.total_in = total_in;
451 s->stream.total_out = total_out; 453 s->stream.total_out = total_out;
452 s->crc = crc32(0L, Z_NULL, 0); 454 s->crc = crc32(0L, Z_NULL, 0);
453 } 455 }
454 } 456 }
455 } 457 }
456 if (s->z_err != Z_OK || s->z_eof) break; 458 if (s->z_err != Z_OK || s->z_eof) break;
457 } 459 }
458 s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start)); 460 s->crc = crc32(s->crc, start, (uInt)(s->stream.next_out - start));
459 461
@@ -580,11 +582,11 @@ int ZEXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...)
580#else /* not ANSI C */ 582#else /* not ANSI C */
581 583
582int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, 584int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
583 a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) 585 a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
584 gzFile file; 586 gzFile file;
585 const char *format; 587 const char *format;
586 int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, 588 int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
587 a11, a12, a13, a14, a15, a16, a17, a18, a19, a20; 589 a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
588{ 590{
589 char buf[Z_PRINTF_BUFSIZE]; 591 char buf[Z_PRINTF_BUFSIZE];
590 int len; 592 int len;
@@ -592,25 +594,25 @@ int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
592#ifdef NO_snprintf 594#ifdef NO_snprintf
593# ifdef HAS_sprintf_void 595# ifdef HAS_sprintf_void
594 sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8, 596 sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
595 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); 597 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
596 len = strlen(buf); /* old sprintf doesn't return the nb of bytes written */ 598 len = strlen(buf); /* old sprintf doesn't return the nb of bytes written */
597 if (len <= 0) return 0; 599 if (len <= 0) return 0;
598# else 600# else
599 len = sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8, 601 len = sprintf(buf, format, a1, a2, a3, a4, a5, a6, a7, a8,
600 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); 602 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
601 if (len <= 0 || len >= sizeof(buf)) 603 if (len <= 0 || len >= sizeof(buf))
602 return 0; 604 return 0;
603# endif 605# endif
604#else 606#else
605# ifdef HAS_snprintf_void 607# ifdef HAS_snprintf_void
606 snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8, 608 snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
607 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); 609 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
608 len = strlen(buf); 610 len = strlen(buf);
609 if (len <= 0) 611 if (len <= 0)
610 return 0; 612 return 0;
611# else 613# else
612 len = snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8, 614 len = snprintf(buf, sizeof(buf), format, a1, a2, a3, a4, a5, a6, a7, a8,
613 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); 615 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
614 if (len <= 0 || len >= sizeof(buf)) 616 if (len <= 0 || len >= sizeof(buf))
615 return 0; 617 return 0;
616# endif 618# endif
@@ -677,14 +679,14 @@ local int do_flush (file, flush)
677 if (done) break; 679 if (done) break;
678 s->z_err = deflate(&(s->stream), flush); 680 s->z_err = deflate(&(s->stream), flush);
679 681
680 /* Ignore the second of two consecutive flushes: */ 682 /* Ignore the second of two consecutive flushes: */
681 if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK; 683 if (len == 0 && s->z_err == Z_BUF_ERROR) s->z_err = Z_OK;
682 684
683 /* deflate has finished flushing only when it hasn't used up 685 /* deflate has finished flushing only when it hasn't used up
684 * all the available space in the output buffer: 686 * all the available space in the output buffer:
685 */ 687 */
686 done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END); 688 done = (s->stream.avail_out != 0 || s->z_err == Z_STREAM_END);
687 689
688 if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break; 690 if (s->z_err != Z_OK && s->z_err != Z_STREAM_END) break;
689 } 691 }
690 return s->z_err == Z_STREAM_END ? Z_OK : s->z_err; 692 return s->z_err == Z_STREAM_END ? Z_OK : s->z_err;
@@ -719,86 +721,86 @@ z_off_t ZEXPORT gzseek (file, offset, whence)
719 gz_stream *s = (gz_stream*)file; 721 gz_stream *s = (gz_stream*)file;
720 722
721 if (s == NULL || whence == SEEK_END || 723 if (s == NULL || whence == SEEK_END ||
722 s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) { 724 s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) {
723 return -1L; 725 return -1L;
724 } 726 }
725 727
726 if (s->mode == 'w') { 728 if (s->mode == 'w') {
727#ifdef NO_DEFLATE 729#ifdef NO_DEFLATE
728 return -1L; 730 return -1L;
729#else 731#else
730 if (whence == SEEK_SET) { 732 if (whence == SEEK_SET) {
731 offset -= s->stream.total_in; 733 offset -= s->stream.total_in;
732 } 734 }
733 if (offset < 0) return -1L; 735 if (offset < 0) return -1L;
734 736
735 /* At this point, offset is the number of zero bytes to write. */ 737 /* At this point, offset is the number of zero bytes to write. */
736 if (s->inbuf == Z_NULL) { 738 if (s->inbuf == Z_NULL) {
737 s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */ 739 s->inbuf = (Byte*)ALLOC(Z_BUFSIZE); /* for seeking */
738 if (s->inbuf == Z_NULL) return -1L; 740 if (s->inbuf == Z_NULL) return -1L;
739 zmemzero(s->inbuf, Z_BUFSIZE); 741 zmemzero(s->inbuf, Z_BUFSIZE);
740 } 742 }
741 while (offset > 0) { 743 while (offset > 0) {
742 uInt size = Z_BUFSIZE; 744 uInt size = Z_BUFSIZE;
743 if (offset < Z_BUFSIZE) size = (uInt)offset; 745 if (offset < Z_BUFSIZE) size = (uInt)offset;
744 746
745 size = gzwrite(file, s->inbuf, size); 747 size = gzwrite(file, s->inbuf, size);
746 if (size == 0) return -1L; 748 if (size == 0) return -1L;
747 749
748 offset -= size; 750 offset -= size;
749 } 751 }
750 return (z_off_t)s->stream.total_in; 752 return (z_off_t)s->stream.total_in;
751#endif 753#endif
752 } 754 }
753 /* Rest of function is for reading only */ 755 /* Rest of function is for reading only */
754 756
755 /* compute absolute position */ 757 /* compute absolute position */
756 if (whence == SEEK_CUR) { 758 if (whence == SEEK_CUR) {
757 offset += s->stream.total_out; 759 offset += s->stream.total_out;
758 } 760 }
759 if (offset < 0) return -1L; 761 if (offset < 0) return -1L;
760 762
761 if (s->transparent) { 763 if (s->transparent) {
762 /* map to fseek */ 764 /* map to fseek */
763 s->stream.avail_in = 0; 765 s->stream.avail_in = 0;
764 s->stream.next_in = s->inbuf; 766 s->stream.next_in = s->inbuf;
765 if (fseek(s->file, offset, SEEK_SET) < 0) return -1L; 767 if (fseek(s->file, offset, SEEK_SET) < 0) return -1L;
766 768
767 s->stream.total_in = s->stream.total_out = (uLong)offset; 769 s->stream.total_in = s->stream.total_out = (uLong)offset;
768 return offset; 770 return offset;
769 } 771 }
770 772
771 /* For a negative seek, rewind and use positive seek */ 773 /* For a negative seek, rewind and use positive seek */
772 if ((uLong)offset >= s->stream.total_out) { 774 if ((uLong)offset >= s->stream.total_out) {
773 offset -= s->stream.total_out; 775 offset -= s->stream.total_out;
774 } else if (gzrewind(file) < 0) { 776 } else if (gzrewind(file) < 0) {
775 return -1L; 777 return -1L;
776 } 778 }
777 /* offset is now the number of bytes to skip. */ 779 /* offset is now the number of bytes to skip. */
778 780
779 if (offset != 0 && s->outbuf == Z_NULL) { 781 if (offset != 0 && s->outbuf == Z_NULL) {
780 s->outbuf = (Byte*)ALLOC(Z_BUFSIZE); 782 s->outbuf = (Byte*)ALLOC(Z_BUFSIZE);
781 if (s->outbuf == Z_NULL) return -1L; 783 if (s->outbuf == Z_NULL) return -1L;
782 } 784 }
783 while (offset > 0) { 785 while (offset > 0) {
784 int size = Z_BUFSIZE; 786 int size = Z_BUFSIZE;
785 if (offset < Z_BUFSIZE) size = (int)offset; 787 if (offset < Z_BUFSIZE) size = (int)offset;
786 788
787 size = gzread(file, s->outbuf, (uInt)size); 789 size = gzread(file, s->outbuf, (uInt)size);
788 if (size <= 0) return -1L; 790 if (size <= 0) return -1L;
789 offset -= size; 791 offset -= size;
790 } 792 }
791 return (z_off_t)s->stream.total_out; 793 return (z_off_t)s->stream.total_out;
792} 794}
793 795
794/* =========================================================================== 796/* ===========================================================================
795 Rewinds input file. 797 Rewinds input file.
796*/ 798*/
797int ZEXPORT gzrewind (file) 799int ZEXPORT gzrewind (file)
798 gzFile file; 800 gzFile file;
799{ 801{
800 gz_stream *s = (gz_stream*)file; 802 gz_stream *s = (gz_stream*)file;
801 803
802 if (s == NULL || s->mode != 'r') return -1; 804 if (s == NULL || s->mode != 'r') return -1;
803 805
804 s->z_err = Z_OK; 806 s->z_err = Z_OK;
@@ -806,10 +808,10 @@ int ZEXPORT gzrewind (file)
806 s->stream.avail_in = 0; 808 s->stream.avail_in = 0;
807 s->stream.next_in = s->inbuf; 809 s->stream.next_in = s->inbuf;
808 s->crc = crc32(0L, Z_NULL, 0); 810 s->crc = crc32(0L, Z_NULL, 0);
809 811
810 if (s->startpos == 0) { /* not a compressed file */ 812 if (s->startpos == 0) { /* not a compressed file */
811 rewind(s->file); 813 rewind(s->file);
812 return 0; 814 return 0;
813 } 815 }
814 816
815 (void) inflateReset(&s->stream); 817 (void) inflateReset(&s->stream);
@@ -835,7 +837,7 @@ int ZEXPORT gzeof (file)
835 gzFile file; 837 gzFile file;
836{ 838{
837 gz_stream *s = (gz_stream*)file; 839 gz_stream *s = (gz_stream*)file;
838 840
839 return (s == NULL || s->mode != 'r') ? 0 : s->z_eof; 841 return (s == NULL || s->mode != 'r') ? 0 : s->z_eof;
840} 842}
841 843
@@ -885,7 +887,7 @@ int ZEXPORT gzclose (file)
885 887
886 if (s->mode == 'w') { 888 if (s->mode == 'w') {
887#ifdef NO_DEFLATE 889#ifdef NO_DEFLATE
888 return Z_STREAM_ERROR; 890 return Z_STREAM_ERROR;
889#else 891#else
890 err = do_flush (file, Z_FINISH); 892 err = do_flush (file, Z_FINISH);
891 if (err != Z_OK) return destroy((gz_stream*)file); 893 if (err != Z_OK) return destroy((gz_stream*)file);