diff options
Diffstat (limited to 'gzio.c')
-rw-r--r-- | gzio.c | 92 |
1 files changed, 66 insertions, 26 deletions
@@ -167,7 +167,7 @@ local gzFile gz_open (path, mode, fd) | |||
167 | /* =========================================================================== | 167 | /* =========================================================================== |
168 | Opens a gzip (.gz) file for reading or writing. | 168 | Opens a gzip (.gz) file for reading or writing. |
169 | */ | 169 | */ |
170 | gzFile EXPORT gzopen (path, mode) | 170 | gzFile ZEXPORT gzopen (path, mode) |
171 | const char *path; | 171 | const char *path; |
172 | const char *mode; | 172 | const char *mode; |
173 | { | 173 | { |
@@ -178,7 +178,7 @@ gzFile EXPORT gzopen (path, mode) | |||
178 | Associate a gzFile with the file descriptor fd. fd is not dup'ed here | 178 | Associate a gzFile with the file descriptor fd. fd is not dup'ed here |
179 | to mimic the behavio(u)r of fdopen. | 179 | to mimic the behavio(u)r of fdopen. |
180 | */ | 180 | */ |
181 | gzFile EXPORT gzdopen (fd, mode) | 181 | gzFile ZEXPORT gzdopen (fd, mode) |
182 | int fd; | 182 | int fd; |
183 | const char *mode; | 183 | const char *mode; |
184 | { | 184 | { |
@@ -193,7 +193,7 @@ gzFile EXPORT gzdopen (fd, mode) | |||
193 | /* =========================================================================== | 193 | /* =========================================================================== |
194 | * Update the compression level and strategy | 194 | * Update the compression level and strategy |
195 | */ | 195 | */ |
196 | int EXPORT gzsetparams (file, level, strategy) | 196 | int ZEXPORT gzsetparams (file, level, strategy) |
197 | gzFile file; | 197 | gzFile file; |
198 | int level; | 198 | int level; |
199 | int strategy; | 199 | int strategy; |
@@ -339,7 +339,7 @@ local int destroy (s) | |||
339 | Reads the given number of uncompressed bytes from the compressed file. | 339 | Reads the given number of uncompressed bytes from the compressed file. |
340 | gzread returns the number of bytes actually read (0 for end of file). | 340 | gzread returns the number of bytes actually read (0 for end of file). |
341 | */ | 341 | */ |
342 | int EXPORT gzread (file, buf, len) | 342 | int ZEXPORT gzread (file, buf, len) |
343 | gzFile file; | 343 | gzFile file; |
344 | voidp buf; | 344 | voidp buf; |
345 | unsigned len; | 345 | unsigned len; |
@@ -375,7 +375,10 @@ int EXPORT gzread (file, buf, len) | |||
375 | s->stream.avail_out -= fread(next_out, 1, s->stream.avail_out, | 375 | s->stream.avail_out -= fread(next_out, 1, s->stream.avail_out, |
376 | s->file); | 376 | s->file); |
377 | } | 377 | } |
378 | return (int)(len - s->stream.avail_out); | 378 | len -= s->stream.avail_out; |
379 | s->stream.total_in += (uLong)len; | ||
380 | s->stream.total_out += (uLong)len; | ||
381 | return (int)len; | ||
379 | } | 382 | } |
380 | if (s->stream.avail_in == 0 && !s->z_eof) { | 383 | if (s->stream.avail_in == 0 && !s->z_eof) { |
381 | 384 | ||
@@ -425,7 +428,7 @@ int EXPORT gzread (file, buf, len) | |||
425 | Reads one byte from the compressed file. gzgetc returns this byte | 428 | Reads one byte from the compressed file. gzgetc returns this byte |
426 | or -1 in case of end of file or error. | 429 | or -1 in case of end of file or error. |
427 | */ | 430 | */ |
428 | int EXPORT gzgetc(file) | 431 | int ZEXPORT gzgetc(file) |
429 | gzFile file; | 432 | gzFile file; |
430 | { | 433 | { |
431 | unsigned char c; | 434 | unsigned char c; |
@@ -434,12 +437,35 @@ int EXPORT gzgetc(file) | |||
434 | } | 437 | } |
435 | 438 | ||
436 | 439 | ||
440 | /* =========================================================================== | ||
441 | Reads bytes from the compressed file until len-1 characters are | ||
442 | read, or a newline character is read and transferred to buf, or an | ||
443 | end-of-file condition is encountered. The string is then terminated | ||
444 | with a null character. | ||
445 | gzgets returns buf, or Z_NULL in case of error. | ||
446 | |||
447 | The current implementation is not optimized at all. | ||
448 | */ | ||
449 | char * ZEXPORT gzgets(file, buf, len) | ||
450 | gzFile file; | ||
451 | char *buf; | ||
452 | int len; | ||
453 | { | ||
454 | char *b = buf; | ||
455 | if (buf == Z_NULL || len <= 0) return Z_NULL; | ||
456 | |||
457 | while (--len > 0 && gzread(file, buf, 1) == 1 && *buf++ != '\n') ; | ||
458 | *buf = '\0'; | ||
459 | return b == buf && len > 0 ? Z_NULL : b; | ||
460 | } | ||
461 | |||
462 | |||
437 | #ifndef NO_DEFLATE | 463 | #ifndef NO_DEFLATE |
438 | /* =========================================================================== | 464 | /* =========================================================================== |
439 | Writes the given number of uncompressed bytes into the compressed file. | 465 | Writes the given number of uncompressed bytes into the compressed file. |
440 | gzwrite returns the number of bytes actually written (0 in case of error). | 466 | gzwrite returns the number of bytes actually written (0 in case of error). |
441 | */ | 467 | */ |
442 | int EXPORT gzwrite (file, buf, len) | 468 | int ZEXPORT gzwrite (file, buf, len) |
443 | gzFile file; | 469 | gzFile file; |
444 | const voidp buf; | 470 | const voidp buf; |
445 | unsigned len; | 471 | unsigned len; |
@@ -478,7 +504,7 @@ int EXPORT gzwrite (file, buf, len) | |||
478 | #ifdef STDC | 504 | #ifdef STDC |
479 | #include <stdarg.h> | 505 | #include <stdarg.h> |
480 | 506 | ||
481 | int EXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...) | 507 | int ZEXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...) |
482 | { | 508 | { |
483 | char buf[Z_BUFSIZE]; | 509 | char buf[Z_BUFSIZE]; |
484 | va_list va; | 510 | va_list va; |
@@ -486,18 +512,19 @@ int EXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...) | |||
486 | 512 | ||
487 | va_start(va, format); | 513 | va_start(va, format); |
488 | #ifdef HAS_vsnprintf | 514 | #ifdef HAS_vsnprintf |
489 | len = vsnprintf(buf, sizeof(buf), format, va); | 515 | (void)vsnprintf(buf, sizeof(buf), format, va); |
490 | #else | 516 | #else |
491 | len = vsprintf(buf, format, va); | 517 | (void)vsprintf(buf, format, va); |
492 | #endif | 518 | #endif |
493 | va_end(va); | 519 | va_end(va); |
520 | len = strlen(buf); /* some *sprintf don't return the nb of bytes written */ | ||
494 | if (len <= 0) return 0; | 521 | if (len <= 0) return 0; |
495 | 522 | ||
496 | return gzwrite(file, buf, (unsigned)len); | 523 | return gzwrite(file, buf, (unsigned)len); |
497 | } | 524 | } |
498 | #else /* not ANSI C */ | 525 | #else /* not ANSI C */ |
499 | 526 | ||
500 | int EXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, | 527 | int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, |
501 | a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) | 528 | a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) |
502 | gzFile file; | 529 | gzFile file; |
503 | const char *format; | 530 | const char *format; |
@@ -525,7 +552,7 @@ int EXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, | |||
525 | Writes c, converted to an unsigned char, into the compressed file. | 552 | Writes c, converted to an unsigned char, into the compressed file. |
526 | gzputc returns the value that was written, or -1 in case of error. | 553 | gzputc returns the value that was written, or -1 in case of error. |
527 | */ | 554 | */ |
528 | int EXPORT gzputc(file, c) | 555 | int ZEXPORT gzputc(file, c) |
529 | gzFile file; | 556 | gzFile file; |
530 | int c; | 557 | int c; |
531 | { | 558 | { |
@@ -536,6 +563,19 @@ int EXPORT gzputc(file, c) | |||
536 | 563 | ||
537 | 564 | ||
538 | /* =========================================================================== | 565 | /* =========================================================================== |
566 | Writes the given null-terminated string to the compressed file, excluding | ||
567 | the terminating null character. | ||
568 | gzputs returns the number of characters written, or -1 in case of error. | ||
569 | */ | ||
570 | int ZEXPORT gzputs(file, s) | ||
571 | gzFile file; | ||
572 | const char *s; | ||
573 | { | ||
574 | return gzwrite(file, (const voidp)s, (unsigned)strlen(s)); | ||
575 | } | ||
576 | |||
577 | |||
578 | /* =========================================================================== | ||
539 | Flushes all pending output into the compressed file. The parameter | 579 | Flushes all pending output into the compressed file. The parameter |
540 | flush is as in the deflate() function. | 580 | flush is as in the deflate() function. |
541 | */ | 581 | */ |
@@ -578,7 +618,7 @@ local int do_flush (file, flush) | |||
578 | return s->z_err == Z_STREAM_END ? Z_OK : s->z_err; | 618 | return s->z_err == Z_STREAM_END ? Z_OK : s->z_err; |
579 | } | 619 | } |
580 | 620 | ||
581 | int EXPORT gzflush (file, flush) | 621 | int ZEXPORT gzflush (file, flush) |
582 | gzFile file; | 622 | gzFile file; |
583 | int flush; | 623 | int flush; |
584 | { | 624 | { |
@@ -599,17 +639,17 @@ int EXPORT gzflush (file, flush) | |||
599 | SEEK_END is not implemented, returns error. | 639 | SEEK_END is not implemented, returns error. |
600 | In this version of the library, gzseek can be extremely slow. | 640 | In this version of the library, gzseek can be extremely slow. |
601 | */ | 641 | */ |
602 | z_off_t EXPORT gzseek (file, offset, whence) | 642 | z_off_t ZEXPORT gzseek (file, offset, whence) |
603 | gzFile file; | 643 | gzFile file; |
604 | z_off_t offset; | 644 | z_off_t offset; |
605 | int whence; | 645 | int whence; |
606 | { | 646 | { |
607 | gz_stream *s = (gz_stream*)file; | 647 | gz_stream *s = (gz_stream*)file; |
608 | 648 | ||
609 | if (s == NULL || whence == SEEK_END || s->z_err == Z_ERRNO) return -1L; | 649 | if (s == NULL || whence == SEEK_END || |
610 | 650 | s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) { | |
611 | s->z_err = Z_OK; | 651 | return -1L; |
612 | s->z_eof = 0; | 652 | } |
613 | 653 | ||
614 | if (s->mode == 'w') { | 654 | if (s->mode == 'w') { |
615 | #ifdef NO_DEFLATE | 655 | #ifdef NO_DEFLATE |
@@ -639,8 +679,6 @@ z_off_t EXPORT gzseek (file, offset, whence) | |||
639 | } | 679 | } |
640 | /* Rest of function is for reading only */ | 680 | /* Rest of function is for reading only */ |
641 | 681 | ||
642 | if (s->z_err == Z_DATA_ERROR) return -1L; | ||
643 | |||
644 | /* compute absolute position */ | 682 | /* compute absolute position */ |
645 | if (whence == SEEK_CUR) { | 683 | if (whence == SEEK_CUR) { |
646 | offset += s->stream.total_out; | 684 | offset += s->stream.total_out; |
@@ -651,7 +689,9 @@ z_off_t EXPORT gzseek (file, offset, whence) | |||
651 | /* map to fseek */ | 689 | /* map to fseek */ |
652 | s->stream.avail_in = 0; | 690 | s->stream.avail_in = 0; |
653 | s->stream.next_in = s->inbuf; | 691 | s->stream.next_in = s->inbuf; |
654 | if (fseek(s->file, offset, SEEK_SET) < 0) return -1L; | 692 | if (fseek(s->file, offset, SEEK_SET) < 0) return -1L; |
693 | |||
694 | s->stream.total_in = s->stream.total_out = (uLong)offset; | ||
655 | return offset; | 695 | return offset; |
656 | } | 696 | } |
657 | 697 | ||
@@ -680,7 +720,7 @@ z_off_t EXPORT gzseek (file, offset, whence) | |||
680 | /* =========================================================================== | 720 | /* =========================================================================== |
681 | Rewinds input file. | 721 | Rewinds input file. |
682 | */ | 722 | */ |
683 | int EXPORT gzrewind (file) | 723 | int ZEXPORT gzrewind (file) |
684 | gzFile file; | 724 | gzFile file; |
685 | { | 725 | { |
686 | gz_stream *s = (gz_stream*)file; | 726 | gz_stream *s = (gz_stream*)file; |
@@ -706,7 +746,7 @@ int EXPORT gzrewind (file) | |||
706 | given compressed file. This position represents a number of bytes in the | 746 | given compressed file. This position represents a number of bytes in the |
707 | uncompressed data stream. | 747 | uncompressed data stream. |
708 | */ | 748 | */ |
709 | z_off_t EXPORT gztell (file) | 749 | z_off_t ZEXPORT gztell (file) |
710 | gzFile file; | 750 | gzFile file; |
711 | { | 751 | { |
712 | return gzseek(file, 0L, SEEK_CUR); | 752 | return gzseek(file, 0L, SEEK_CUR); |
@@ -716,7 +756,7 @@ z_off_t EXPORT gztell (file) | |||
716 | Returns 1 when EOF has previously been detected reading the given | 756 | Returns 1 when EOF has previously been detected reading the given |
717 | input stream, otherwise zero. | 757 | input stream, otherwise zero. |
718 | */ | 758 | */ |
719 | int EXPORT gzeof (file) | 759 | int ZEXPORT gzeof (file) |
720 | gzFile file; | 760 | gzFile file; |
721 | { | 761 | { |
722 | gz_stream *s = (gz_stream*)file; | 762 | gz_stream *s = (gz_stream*)file; |
@@ -759,7 +799,7 @@ local uLong getLong (s) | |||
759 | Flushes all pending output if necessary, closes the compressed file | 799 | Flushes all pending output if necessary, closes the compressed file |
760 | and deallocates all the (de)compression state. | 800 | and deallocates all the (de)compression state. |
761 | */ | 801 | */ |
762 | int EXPORT gzclose (file) | 802 | int ZEXPORT gzclose (file) |
763 | gzFile file; | 803 | gzFile file; |
764 | { | 804 | { |
765 | int err; | 805 | int err; |
@@ -788,7 +828,7 @@ int EXPORT gzclose (file) | |||
788 | errnum is set to Z_ERRNO and the application may consult errno | 828 | errnum is set to Z_ERRNO and the application may consult errno |
789 | to get the exact error code. | 829 | to get the exact error code. |
790 | */ | 830 | */ |
791 | const char* EXPORT gzerror (file, errnum) | 831 | const char* ZEXPORT gzerror (file, errnum) |
792 | gzFile file; | 832 | gzFile file; |
793 | int *errnum; | 833 | int *errnum; |
794 | { | 834 | { |