gzlib.c (muscle7.61) | : | gzlib.c (muscle7.62) | ||
---|---|---|---|---|
/* 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 copyright notice in zlib.h | * For conditions of distribution and use, see copyright notice in zlib.h | |||
*/ | */ | |||
#if defined(__APPLE__) || defined(__linux__) || defined(__EMSCRIPTEN__) | ||||
# include <unistd.h> // just to avoid implicit-declaration warnings --jaf | ||||
#endif | ||||
#include "gzguts.h" | #include "gzguts.h" | |||
#if defined(_WIN32) && !defined(__BORLANDC__) | #ifndef _WIN32 | |||
# include <unistd.h> | ||||
#endif | ||||
#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 | |||
# define LSEEK lseek64 | # define LSEEK lseek64 | |||
#else | #else | |||
# define LSEEK lseek | # define LSEEK lseek | |||
#endif | #endif | |||
#endif | #endif | |||
/* Local functions */ | /* Local functions */ | |||
skipping to change at line 101 | skipping to change at line 101 | |||
state->strm.avail_in = 0; /* no input data yet */ | state->strm.avail_in = 0; /* no input data yet */ | |||
} | } | |||
/* Open a gzip file either by name or file descriptor. */ | /* Open a gzip file either by name or file descriptor. */ | |||
local gzFile gz_open(path, fd, mode) | local gzFile gz_open(path, fd, mode) | |||
const void *path; | const void *path; | |||
int fd; | int fd; | |||
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; | |||
#endif | #endif | |||
#ifdef O_EXCL | #ifdef O_EXCL | |||
int exclusive = 0; | int exclusive = 0; | |||
#endif | #endif | |||
#if __STDC_WANT_SECURE_LIB__ | ||||
int tempfd = -1; | ||||
#endif | ||||
/* check input */ | /* check input */ | |||
if (path == NULL) | if (path == NULL) | |||
return NULL; | return NULL; | |||
/* allocate gzFile structure to return */ | /* allocate gzFile structure to return */ | |||
state = (gz_statep)malloc(sizeof(gz_state)); | state = (gz_statep)malloc(sizeof(gz_state)); | |||
if (state == NULL) | if (state == NULL) | |||
return NULL; | return NULL; | |||
state->size = 0; /* no buffers allocated yet */ | state->size = 0; /* no buffers allocated yet */ | |||
skipping to change at line 198 | skipping to change at line 195 | |||
/* can't force transparent read */ | /* can't force transparent read */ | |||
if (state->mode == GZ_READ) { | if (state->mode == GZ_READ) { | |||
if (state->direct) { | if (state->direct) { | |||
free(state); | free(state); | |||
return NULL; | return NULL; | |||
} | } | |||
state->direct = 1; /* for empty file */ | state->direct = 1; /* for empty file */ | |||
} | } | |||
/* save the path name for error messages */ | /* save the path name for error messages */ | |||
#ifdef _WIN32 | #ifdef WIDECHAR | |||
if (fd == -2) { | if (fd == -2) { | |||
# if __STDC_WANT_SECURE_LIB__ | ||||
(void) wcstombs_s(&len, NULL, 0, path, 0); | ||||
# else | ||||
len = wcstombs(NULL, path, 0); | len = wcstombs(NULL, path, 0); | |||
# endif | if (len == (z_size_t)-1) | |||
if (len == (size_t)-1) | ||||
len = 0; | len = 0; | |||
} | } | |||
else | else | |||
#endif | #endif | |||
len = strlen((const char *)path); | len = strlen((const char *)path); | |||
state->path = (char *)malloc(len + 1); | state->path = (char *)malloc(len + 1); | |||
if (state->path == NULL) { | if (state->path == NULL) { | |||
free(state); | free(state); | |||
return NULL; | return NULL; | |||
} | } | |||
#ifdef _WIN32 | #ifdef WIDECHAR | |||
if (fd == -2) | if (fd == -2) | |||
if (len) | if (len) | |||
# if __STDC_WANT_SECURE_LIB__ | ||||
{ | ||||
size_t bytes; | ||||
(void) wcstombs_s(&bytes, state->path, len + 1, path, len + 1); | ||||
} | ||||
# else | ||||
wcstombs(state->path, path, len + 1); | wcstombs(state->path, path, len + 1); | |||
# endif | ||||
else | else | |||
*(state->path) = 0; | *(state->path) = 0; | |||
else | else | |||
#endif | #endif | |||
#if !defined(NO_snprintf) && !defined(NO_vsnprintf) | #if !defined(NO_snprintf) && !defined(NO_vsnprintf) | |||
# if __STDC_WANT_SECURE_LIB__ | (void)snprintf(state->path, len + 1, "%s", (const char *)path); | |||
_snprintf_s(state->path, len + 1, _TRUNCATE, "%s", (const char *)path); | ||||
# else | ||||
snprintf(state->path, len + 1, "%s", (const char *)path); | ||||
# endif | ||||
#else | #else | |||
strcpy(state->path, path); | strcpy(state->path, path); | |||
#endif | #endif | |||
/* compute the flags for open() */ | /* compute the flags for open() */ | |||
oflag = | oflag = | |||
#ifdef O_LARGEFILE | #ifdef O_LARGEFILE | |||
O_LARGEFILE | | O_LARGEFILE | | |||
#endif | #endif | |||
#ifdef O_BINARY | #ifdef O_BINARY | |||
skipping to change at line 264 | skipping to change at line 246 | |||
(O_WRONLY | O_CREAT | | (O_WRONLY | O_CREAT | | |||
#ifdef O_EXCL | #ifdef O_EXCL | |||
(exclusive ? O_EXCL : 0) | | (exclusive ? O_EXCL : 0) | | |||
#endif | #endif | |||
(state->mode == GZ_WRITE ? | (state->mode == GZ_WRITE ? | |||
O_TRUNC : | O_TRUNC : | |||
O_APPEND))); | O_APPEND))); | |||
/* open the file with the appropriate flags (or just use fd) */ | /* open the file with the appropriate flags (or just use fd) */ | |||
state->fd = fd > -1 ? fd : ( | state->fd = fd > -1 ? fd : ( | |||
#ifdef _WIN32 | #ifdef WIDECHAR | |||
# if __STDC_WANT_SECURE_LIB__ | ||||
fd == -2 ? (_wsopen_s(&tempfd, path, oflag, (state->mode == GZ_WRITE || | ||||
state->mode == GZ_APPEND) ? _SH_DENYWR : _SH_DENYNO, 0666) == 0 ? tempfd : -1) : | ||||
# else | ||||
fd == -2 ? _wopen(path, oflag, 0666) : | fd == -2 ? _wopen(path, oflag, 0666) : | |||
# endif | ||||
#endif | #endif | |||
#if __STDC_WANT_SECURE_LIB__ | ||||
(_sopen_s(&tempfd, (const char *)path, oflag, (state->mode == GZ_WRITE | | ||||
| state->mode == GZ_APPEND) ? _SH_DENYWR : _SH_DENYNO, 0666) == 0 ? tempfd : -1) | ||||
); | ||||
#else | ||||
open((const char *)path, oflag, 0666)); | open((const char *)path, oflag, 0666)); | |||
#endif | ||||
if (state->fd == -1) { | if (state->fd == -1) { | |||
free(state->path); | free(state->path); | |||
free(state); | free(state); | |||
return NULL; | return NULL; | |||
} | } | |||
if (state->mode == GZ_APPEND) | if (state->mode == GZ_APPEND) { | |||
LSEEK(state->fd, 0, SEEK_END); /* so gzoffset() is correct */ | ||||
state->mode = GZ_WRITE; /* simplify later checks */ | 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->mode == GZ_READ) { | if (state->mode == GZ_READ) { | |||
state->start = LSEEK(state->fd, 0, SEEK_CUR); | state->start = LSEEK(state->fd, 0, SEEK_CUR); | |||
if (state->start == -1) state->start = 0; | if (state->start == -1) state->start = 0; | |||
} | } | |||
/* initialize stream */ | /* initialize stream */ | |||
gz_reset(state); | gz_reset(state); | |||
skipping to change at line 324 | skipping to change at line 300 | |||
gzFile ZEXPORT gzdopen(fd, mode) | gzFile ZEXPORT gzdopen(fd, mode) | |||
int fd; | int fd; | |||
const char *mode; | const char *mode; | |||
{ | { | |||
char *path; /* identifier for error messages */ | char *path; /* identifier for error messages */ | |||
gzFile gz; | gzFile gz; | |||
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) | |||
# if __STDC_WANT_SECURE_LIB__ | (void)snprintf(path, 7 + 3 * sizeof(int), "<fd:%d>", fd); | |||
_snprintf_s(path, 7 + 3 * sizeof(int), _TRUNCATE, "<fd:%d>", fd); /* for deb | ||||
ugging */ | ||||
# else | ||||
snprintf(path, 7 + 3 * sizeof(int), "<fd:%d>", fd); /* for debugging */ | ||||
# endif | ||||
#else | #else | |||
sprintf(path, "<fd:%d>", fd); /* for debugging */ | sprintf(path, "<fd:%d>", fd); /* for debugging */ | |||
#endif | #endif | |||
gz = gz_open(path, fd, mode); | gz = gz_open(path, fd, mode); | |||
free(path); | free(path); | |||
return gz; | return gz; | |||
} | } | |||
/* -- 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; | |||
{ | { | |||
return gz_open(path, -2, mode); | return gz_open(path, -2, mode); | |||
} | } | |||
#endif | #endif | |||
/* -- see zlib.h -- */ | /* -- see zlib.h -- */ | |||
int ZEXPORT gzbuffer(file, size) | int ZEXPORT gzbuffer(file, size) | |||
skipping to change at line 366 | skipping to change at line 338 | |||
return -1; | return -1; | |||
state = (gz_statep)file; | state = (gz_statep)file; | |||
if (state->mode != GZ_READ && state->mode != GZ_WRITE) | if (state->mode != GZ_READ && state->mode != GZ_WRITE) | |||
return -1; | return -1; | |||
/* make sure we haven't already allocated memory */ | /* make sure we haven't already allocated memory */ | |||
if (state->size != 0) | if (state->size != 0) | |||
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->want = size; | state->want = size; | |||
return 0; | return 0; | |||
} | } | |||
/* -- see zlib.h -- */ | /* -- see zlib.h -- */ | |||
int ZEXPORT gzrewind(file) | int ZEXPORT gzrewind(file) | |||
gzFile file; | gzFile file; | |||
{ | { | |||
skipping to change at line 641 | skipping to change at line 615 | |||
if (err == Z_MEM_ERROR) | if (err == Z_MEM_ERROR) | |||
return; | return; | |||
/* construct error message with path */ | /* construct error message with path */ | |||
if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) == | if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) == | |||
NULL) { | NULL) { | |||
state->err = Z_MEM_ERROR; | state->err = Z_MEM_ERROR; | |||
return; | return; | |||
} | } | |||
#if !defined(NO_snprintf) && !defined(NO_vsnprintf) | #if !defined(NO_snprintf) && !defined(NO_vsnprintf) | |||
# if __STDC_WANT_SECURE_LIB__ | (void)snprintf(state->msg, strlen(state->path) + strlen(msg) + 3, | |||
_snprintf_s(state->msg, strlen(state->path) + strlen(msg) + 3, _TRUNCATE, | "%s%s%s", state->path, ": ", msg); | |||
# else | ||||
snprintf(state->msg, strlen(state->path) + strlen(msg) + 3, | ||||
# endif | ||||
"%s%s%s", state->path, ": ", msg); | ||||
#else | #else | |||
strcpy(state->msg, state->path); | strcpy(state->msg, state->path); | |||
strcat(state->msg, ": "); | strcat(state->msg, ": "); | |||
strcat(state->msg, msg); | strcat(state->msg, msg); | |||
#endif | #endif | |||
return; | ||||
} | } | |||
#ifndef INT_MAX | #ifndef INT_MAX | |||
/* portably return maximum value for an int (when limits.h presumed not | /* portably return maximum value for an int (when limits.h presumed not | |||
available) -- we need to do this to cover cases where 2's complement not | available) -- we need to do this to cover cases where 2's complement not | |||
used, since C standard permits 1's complement and sign-bit representations, | used, since C standard permits 1's complement and sign-bit representations, | |||
otherwise we could just use ((unsigned)-1) >> 1 */ | otherwise we could just use ((unsigned)-1) >> 1 */ | |||
unsigned ZLIB_INTERNAL gz_intmax() | unsigned ZLIB_INTERNAL gz_intmax() | |||
{ | { | |||
unsigned p, q; | unsigned p, q; | |||
End of changes. 23 change blocks. | ||||
56 lines changed or deleted | 21 lines changed or added |