gzlib.c updated to zlib 1.2.11

This commit is contained in:
Przemyslaw Skibinski
2017-01-18 12:08:08 +01:00
parent 69f7990fc5
commit 7f82aad187
+21 -18
View File
@@ -2,13 +2,13 @@
* - gz_statep was converted to union to work with -Wstrict-aliasing=1 */ * - gz_statep was converted to union to work with -Wstrict-aliasing=1 */
/* gzlib.c -- zlib functions common to reading and writing gzip files /* gzlib.c -- zlib functions common to reading and writing gzip files
* Copyright (C) 2004, 2010, 2011, 2012, 2013 Mark Adler * Copyright (C) 2004-2017 Mark Adler
* For conditions of distribution and use, see http://www.zlib.net/zlib_license.html * For conditions of distribution and use, see copyright notice in zlib.h
*/ */
#include "gzguts.h" #include "gzguts.h"
#if defined(_WIN32) && !defined(__BORLANDC__) #if defined(_WIN32) && !defined(__BORLANDC__) && !defined(__MINGW32__)
# define LSEEK _lseeki64 # define LSEEK _lseeki64
#else #else
#if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0 #if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
@@ -97,7 +97,7 @@ local gzFile gz_open(path, fd, mode)
const char *mode; const char *mode;
{ {
gz_statep state; gz_statep state;
size_t len; z_size_t len;
int oflag; int oflag;
#ifdef O_CLOEXEC #ifdef O_CLOEXEC
int cloexec = 0; int cloexec = 0;
@@ -191,10 +191,10 @@ local gzFile gz_open(path, fd, mode)
} }
/* save the path name for error messages */ /* save the path name for error messages */
#ifdef _WIN32 #ifdef WIDECHAR
if (fd == -2) { if (fd == -2) {
len = wcstombs(NULL, path, 0); len = wcstombs(NULL, path, 0);
if (len == (size_t)-1) if (len == (z_size_t)-1)
len = 0; len = 0;
} }
else else
@@ -205,7 +205,7 @@ local gzFile gz_open(path, fd, mode)
free(state.state); free(state.state);
return NULL; return NULL;
} }
#ifdef _WIN32 #ifdef WIDECHAR
if (fd == -2) if (fd == -2)
if (len) if (len)
wcstombs(state.state->path, path, len + 1); wcstombs(state.state->path, path, len + 1);
@@ -214,7 +214,7 @@ local gzFile gz_open(path, fd, mode)
else else
#endif #endif
#if !defined(NO_snprintf) && !defined(NO_vsnprintf) #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
snprintf(state.state->path, len + 1, "%s", (const char *)path); (void)snprintf(state.state->path, len + 1, "%s", (const char *)path);
#else #else
strcpy(state.state->path, path); strcpy(state.state->path, path);
#endif #endif
@@ -242,7 +242,7 @@ local gzFile gz_open(path, fd, mode)
/* open the file with the appropriate flags (or just use fd) */ /* open the file with the appropriate flags (or just use fd) */
state.state->fd = fd > -1 ? fd : ( state.state->fd = fd > -1 ? fd : (
#ifdef _WIN32 #ifdef WIDECHAR
fd == -2 ? _wopen(path, oflag, 0666) : fd == -2 ? _wopen(path, oflag, 0666) :
#endif #endif
open((const char *)path, oflag, 0666)); open((const char *)path, oflag, 0666));
@@ -251,8 +251,10 @@ local gzFile gz_open(path, fd, mode)
free(state.state); free(state.state);
return NULL; return NULL;
} }
if (state.state->mode == GZ_APPEND) if (state.state->mode == GZ_APPEND) {
LSEEK(state.state->fd, 0, SEEK_END); /* so gzoffset() is correct */
state.state->mode = GZ_WRITE; /* simplify later checks */ state.state->mode = GZ_WRITE; /* simplify later checks */
}
/* save the current position for rewinding (only if reading) */ /* save the current position for rewinding (only if reading) */
if (state.state->mode == GZ_READ) { if (state.state->mode == GZ_READ) {
@@ -294,7 +296,7 @@ gzFile ZEXPORT gzdopen(fd, mode)
if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL) if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL)
return NULL; return NULL;
#if !defined(NO_snprintf) && !defined(NO_vsnprintf) #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
snprintf(path, 7 + 3 * sizeof(int), "<fd:%d>", fd); /* for debugging */ (void)snprintf(path, 7 + 3 * sizeof(int), "<fd:%d>", fd);
#else #else
sprintf(path, "<fd:%d>", fd); /* for debugging */ sprintf(path, "<fd:%d>", fd); /* for debugging */
#endif #endif
@@ -304,7 +306,7 @@ gzFile ZEXPORT gzdopen(fd, mode)
} }
/* -- see zlib.h -- */ /* -- see zlib.h -- */
#ifdef _WIN32 #ifdef WIDECHAR
gzFile ZEXPORT gzopen_w(path, mode) gzFile ZEXPORT gzopen_w(path, mode)
const wchar_t *path; const wchar_t *path;
const char *mode; const char *mode;
@@ -332,6 +334,8 @@ int ZEXPORT gzbuffer(file, size)
return -1; return -1;
/* check and set requested size */ /* check and set requested size */
if ((size << 1) < size)
return -1; /* need to be able to double it */
if (size < 2) if (size < 2)
size = 2; /* need two bytes to check magic header */ size = 2; /* need two bytes to check magic header */
state.state->want = size; state.state->want = size;
@@ -569,8 +573,8 @@ void ZEXPORT gzclearerr(file)
gz_error(state, Z_OK, NULL); gz_error(state, Z_OK, NULL);
} }
/* Create an error message in allocated memory and set state->err and /* Create an error message in allocated memory and set state.state->err and
state->msg accordingly. Free any previous error message already there. Do state.state->msg accordingly. Free any previous error message already there. Do
not try to free or allocate space if the error is Z_MEM_ERROR (out of not try to free or allocate space if the error is Z_MEM_ERROR (out of
memory). Simply save the error message as a static string. If there is an memory). Simply save the error message as a static string. If there is an
allocation failure constructing the error message, then convert the error to allocation failure constructing the error message, then convert the error to
@@ -587,7 +591,7 @@ void ZLIB_INTERNAL gz_error(state, err, msg)
state.state->msg = NULL; state.state->msg = NULL;
} }
/* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */ /* if fatal, set state.state->x.have to 0 so that the gzgetc() macro fails */
if (err != Z_OK && err != Z_BUF_ERROR) if (err != Z_OK && err != Z_BUF_ERROR)
state.state->x.have = 0; state.state->x.have = 0;
@@ -607,14 +611,13 @@ void ZLIB_INTERNAL gz_error(state, err, msg)
return; return;
} }
#if !defined(NO_snprintf) && !defined(NO_vsnprintf) #if !defined(NO_snprintf) && !defined(NO_vsnprintf)
snprintf(state.state->msg, strlen(state.state->path) + strlen(msg) + 3, (void)snprintf(state.state->msg, strlen(state.state->path) + strlen(msg) + 3,
"%s%s%s", state.state->path, ": ", msg); "%s%s%s", state.state->path, ": ", msg);
#else #else
strcpy(state.state->msg, state.state->path); strcpy(state.state->msg, state.state->path);
strcat(state.state->msg, ": "); strcat(state.state->msg, ": ");
strcat(state.state->msg, msg); strcat(state.state->msg, msg);
#endif #endif
return;
} }
#ifndef INT_MAX #ifndef INT_MAX