From b8c9ecb0765fc39423c07613d909c5193378bdfd Mon Sep 17 00:00:00 2001 From: Mark Adler Date: Fri, 9 Sep 2011 23:19:21 -0700 Subject: zlib 1.0.9 --- gzio.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 66 insertions(+), 26 deletions(-) (limited to 'gzio.c') diff --git a/gzio.c b/gzio.c index a9aa830..b816494 100644 --- a/gzio.c +++ b/gzio.c @@ -167,7 +167,7 @@ local gzFile gz_open (path, mode, fd) /* =========================================================================== Opens a gzip (.gz) file for reading or writing. */ -gzFile EXPORT gzopen (path, mode) +gzFile ZEXPORT gzopen (path, mode) const char *path; const char *mode; { @@ -178,7 +178,7 @@ gzFile EXPORT gzopen (path, mode) Associate a gzFile with the file descriptor fd. fd is not dup'ed here to mimic the behavio(u)r of fdopen. */ -gzFile EXPORT gzdopen (fd, mode) +gzFile ZEXPORT gzdopen (fd, mode) int fd; const char *mode; { @@ -193,7 +193,7 @@ gzFile EXPORT gzdopen (fd, mode) /* =========================================================================== * Update the compression level and strategy */ -int EXPORT gzsetparams (file, level, strategy) +int ZEXPORT gzsetparams (file, level, strategy) gzFile file; int level; int strategy; @@ -339,7 +339,7 @@ local int destroy (s) Reads the given number of uncompressed bytes from the compressed file. gzread returns the number of bytes actually read (0 for end of file). */ -int EXPORT gzread (file, buf, len) +int ZEXPORT gzread (file, buf, len) gzFile file; voidp buf; unsigned len; @@ -375,7 +375,10 @@ int EXPORT gzread (file, buf, len) s->stream.avail_out -= fread(next_out, 1, s->stream.avail_out, s->file); } - return (int)(len - s->stream.avail_out); + len -= s->stream.avail_out; + s->stream.total_in += (uLong)len; + s->stream.total_out += (uLong)len; + return (int)len; } if (s->stream.avail_in == 0 && !s->z_eof) { @@ -425,7 +428,7 @@ int EXPORT gzread (file, buf, len) Reads one byte from the compressed file. gzgetc returns this byte or -1 in case of end of file or error. */ -int EXPORT gzgetc(file) +int ZEXPORT gzgetc(file) gzFile file; { unsigned char c; @@ -434,12 +437,35 @@ int EXPORT gzgetc(file) } +/* =========================================================================== + Reads bytes from the compressed file until len-1 characters are + read, or a newline character is read and transferred to buf, or an + end-of-file condition is encountered. The string is then terminated + with a null character. + gzgets returns buf, or Z_NULL in case of error. + + The current implementation is not optimized at all. +*/ +char * ZEXPORT gzgets(file, buf, len) + gzFile file; + char *buf; + int len; +{ + char *b = buf; + if (buf == Z_NULL || len <= 0) return Z_NULL; + + while (--len > 0 && gzread(file, buf, 1) == 1 && *buf++ != '\n') ; + *buf = '\0'; + return b == buf && len > 0 ? Z_NULL : b; +} + + #ifndef NO_DEFLATE /* =========================================================================== Writes the given number of uncompressed bytes into the compressed file. gzwrite returns the number of bytes actually written (0 in case of error). */ -int EXPORT gzwrite (file, buf, len) +int ZEXPORT gzwrite (file, buf, len) gzFile file; const voidp buf; unsigned len; @@ -478,7 +504,7 @@ int EXPORT gzwrite (file, buf, len) #ifdef STDC #include -int EXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...) +int ZEXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...) { char buf[Z_BUFSIZE]; va_list va; @@ -486,18 +512,19 @@ int EXPORTVA gzprintf (gzFile file, const char *format, /* args */ ...) va_start(va, format); #ifdef HAS_vsnprintf - len = vsnprintf(buf, sizeof(buf), format, va); + (void)vsnprintf(buf, sizeof(buf), format, va); #else - len = vsprintf(buf, format, va); + (void)vsprintf(buf, format, va); #endif va_end(va); + len = strlen(buf); /* some *sprintf don't return the nb of bytes written */ if (len <= 0) return 0; return gzwrite(file, buf, (unsigned)len); } #else /* not ANSI C */ -int EXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, +int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) gzFile file; const char *format; @@ -525,7 +552,7 @@ int EXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, Writes c, converted to an unsigned char, into the compressed file. gzputc returns the value that was written, or -1 in case of error. */ -int EXPORT gzputc(file, c) +int ZEXPORT gzputc(file, c) gzFile file; int c; { @@ -535,6 +562,19 @@ int EXPORT gzputc(file, c) } +/* =========================================================================== + Writes the given null-terminated string to the compressed file, excluding + the terminating null character. + gzputs returns the number of characters written, or -1 in case of error. +*/ +int ZEXPORT gzputs(file, s) + gzFile file; + const char *s; +{ + return gzwrite(file, (const voidp)s, (unsigned)strlen(s)); +} + + /* =========================================================================== Flushes all pending output into the compressed file. The parameter flush is as in the deflate() function. @@ -578,7 +618,7 @@ local int do_flush (file, flush) return s->z_err == Z_STREAM_END ? Z_OK : s->z_err; } -int EXPORT gzflush (file, flush) +int ZEXPORT gzflush (file, flush) gzFile file; int flush; { @@ -599,17 +639,17 @@ int EXPORT gzflush (file, flush) SEEK_END is not implemented, returns error. In this version of the library, gzseek can be extremely slow. */ -z_off_t EXPORT gzseek (file, offset, whence) +z_off_t ZEXPORT gzseek (file, offset, whence) gzFile file; z_off_t offset; int whence; { gz_stream *s = (gz_stream*)file; - if (s == NULL || whence == SEEK_END || s->z_err == Z_ERRNO) return -1L; - - s->z_err = Z_OK; - s->z_eof = 0; + if (s == NULL || whence == SEEK_END || + s->z_err == Z_ERRNO || s->z_err == Z_DATA_ERROR) { + return -1L; + } if (s->mode == 'w') { #ifdef NO_DEFLATE @@ -639,8 +679,6 @@ z_off_t EXPORT gzseek (file, offset, whence) } /* Rest of function is for reading only */ - if (s->z_err == Z_DATA_ERROR) return -1L; - /* compute absolute position */ if (whence == SEEK_CUR) { offset += s->stream.total_out; @@ -651,7 +689,9 @@ z_off_t EXPORT gzseek (file, offset, whence) /* map to fseek */ s->stream.avail_in = 0; s->stream.next_in = s->inbuf; - if (fseek(s->file, offset, SEEK_SET) < 0) return -1L; + if (fseek(s->file, offset, SEEK_SET) < 0) return -1L; + + s->stream.total_in = s->stream.total_out = (uLong)offset; return offset; } @@ -680,7 +720,7 @@ z_off_t EXPORT gzseek (file, offset, whence) /* =========================================================================== Rewinds input file. */ -int EXPORT gzrewind (file) +int ZEXPORT gzrewind (file) gzFile file; { gz_stream *s = (gz_stream*)file; @@ -706,7 +746,7 @@ int EXPORT gzrewind (file) given compressed file. This position represents a number of bytes in the uncompressed data stream. */ -z_off_t EXPORT gztell (file) +z_off_t ZEXPORT gztell (file) gzFile file; { return gzseek(file, 0L, SEEK_CUR); @@ -716,7 +756,7 @@ z_off_t EXPORT gztell (file) Returns 1 when EOF has previously been detected reading the given input stream, otherwise zero. */ -int EXPORT gzeof (file) +int ZEXPORT gzeof (file) gzFile file; { gz_stream *s = (gz_stream*)file; @@ -759,7 +799,7 @@ local uLong getLong (s) Flushes all pending output if necessary, closes the compressed file and deallocates all the (de)compression state. */ -int EXPORT gzclose (file) +int ZEXPORT gzclose (file) gzFile file; { int err; @@ -788,7 +828,7 @@ int EXPORT gzclose (file) errnum is set to Z_ERRNO and the application may consult errno to get the exact error code. */ -const char* EXPORT gzerror (file, errnum) +const char* ZEXPORT gzerror (file, errnum) gzFile file; int *errnum; { -- cgit v1.2.3-55-g6feb