(deps/libz) Cleanups

This commit is contained in:
twinaphex 2020-07-02 20:48:54 +02:00
parent 1f86ea1c4c
commit 60c0470a88
8 changed files with 552 additions and 475 deletions

10
deps/libz/adler32.c vendored
View File

@ -48,17 +48,19 @@
/* ========================================================================= */
uint32_t adler32(uint32_t adler, const uint8_t *buf, size_t len)
{
int k;
uint32_t s1 = adler & 0xffff;
uint32_t s2 = (adler >> 16) & 0xffff;
int k;
if (buf == NULL)
return 1L;
while (len > 0) {
k = len < NMAX ? (int)len : NMAX;
while (len > 0)
{
k = len < NMAX ? (int)len : NMAX;
len -= k;
while (k >= 16) {
while (k >= 16)
{
DO16(buf);
buf += 16;
k -= 16;

34
deps/libz/compress.c vendored
View File

@ -21,34 +21,38 @@
*/
int compress2 (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen, int level)
{
z_stream stream;
int err;
z_stream stream;
stream.next_in = (Bytef *)source;
stream.next_in = (Bytef *)source;
stream.avail_in = (uInt)sourceLen;
#ifdef MAXSEG_64K
/* Check for source > 64K on 16-bit machine: */
if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
if ((uLong)stream.avail_in != sourceLen)
return Z_BUF_ERROR;
#endif
stream.next_out = dest;
stream.next_out = dest;
stream.avail_out = (uInt)*destLen;
if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
if ((uLong)stream.avail_out != *destLen)
return Z_BUF_ERROR;
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.opaque = (voidpf)0;
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.opaque = (voidpf)0;
err = deflateInit(&stream, level);
if (err != Z_OK) return err;
err = deflateInit(&stream, level);
if (err != Z_OK)
return err;
err = deflate(&stream, Z_FINISH);
if (err != Z_STREAM_END) {
err = deflate(&stream, Z_FINISH);
if (err != Z_STREAM_END)
{
deflateEnd(&stream);
return err == Z_OK ? Z_BUF_ERROR : err;
}
*destLen = stream.total_out;
err = deflateEnd(&stream);
err = deflateEnd(&stream);
return err;
}
@ -65,6 +69,8 @@ int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen
*/
uLong compressBound (uLong sourceLen)
{
return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
return sourceLen +
(sourceLen >> 12) +
(sourceLen >> 14) +
(sourceLen >> 25) + 13;
}

74
deps/libz/deflate.c vendored
View File

@ -182,8 +182,9 @@ int deflateResetKeep (z_streamp strm)
{
deflate_state *s;
if (strm == Z_NULL || strm->state == Z_NULL ||
strm->zalloc == Z_NULL || strm->zfree == Z_NULL)
if ( strm == Z_NULL
|| strm->state == Z_NULL
)
return Z_STREAM_ERROR;
strm->total_in = strm->total_out = 0;
@ -251,22 +252,10 @@ int deflateInit2_(z_streamp strm, int level, int method, int windowBits, int mem
if (strm == Z_NULL)
return Z_STREAM_ERROR;
strm->msg = Z_NULL;
if (strm->zalloc == (alloc_func)0)
{
#ifdef Z_SOLO
return Z_STREAM_ERROR;
#else
strm->zalloc = zcalloc;
strm->opaque = (voidpf)0;
#endif
}
if (strm->zfree == NULL)
#ifdef Z_SOLO
return Z_STREAM_ERROR;
#else
strm->zfree = zcfree;
#endif
strm->msg = Z_NULL;
strm->zalloc = zcalloc;
strm->opaque = (voidpf)0;
strm->zfree = zcfree;
#ifdef FASTEST
if (level != 0)
@ -295,7 +284,7 @@ int deflateInit2_(z_streamp strm, int level, int method, int windowBits, int mem
if (windowBits == 8)
windowBits = 9; /* until 256-byte window bug fixed */
s = (deflate_state *)ZALLOC(strm, 1, sizeof(deflate_state));
s = (deflate_state *)calloc(1, sizeof(deflate_state));
if (s == Z_NULL)
return Z_MEM_ERROR;
@ -314,15 +303,15 @@ int deflateInit2_(z_streamp strm, int level, int method, int windowBits, int mem
s->hash_mask = s->hash_size - 1;
s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
s->window = (Bytef*) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
s->prev = (Posf*) ZALLOC(strm, s->w_size, sizeof(Pos));
s->head = (Posf*) ZALLOC(strm, s->hash_size, sizeof(Pos));
s->window = (Bytef*)calloc(s->w_size, 2*sizeof(Byte));
s->prev = (Posf*) calloc(s->w_size, sizeof(Pos));
s->head = (Posf*) calloc(s->hash_size, sizeof(Pos));
s->high_water = 0; /* nothing written to s->window yet */
s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
overlay = (ushf *)calloc(s->lit_bufsize, sizeof(ush)+2);
s->pending_buf = (uchf *) overlay;
s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
@ -364,7 +353,7 @@ static int read_buf(z_streamp strm, Bytef *buf, unsigned size)
strm->avail_in -= len;
zmemcpy(buf, strm->next_in, len);
memcpy(buf, strm->next_in, len);
if (state->wrap == 1)
strm->adler = adler32(strm->adler, buf, len);
#ifdef GZIP
@ -418,7 +407,7 @@ static void fill_window(deflate_state *s)
*/
if (s->strstart >= wsize+MAX_DIST(s)) {
zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
memcpy(s->window, s->window+wsize, (unsigned)wsize);
s->match_start -= wsize;
s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
s->block_start -= (long) wsize;
@ -845,7 +834,7 @@ static void flush_pending(z_streamp strm)
if (len == 0)
return;
zmemcpy(strm->next_out, s->pending_out, len);
memcpy(strm->next_out, s->pending_out, len);
strm->next_out += len;
s->pending_out += len;
strm->total_out += len;
@ -1332,12 +1321,12 @@ int deflateEnd (z_streamp strm)
return Z_STREAM_ERROR;
/* Deallocate in reverse order of allocations: */
TRY_FREE(strm, state->pending_buf);
TRY_FREE(strm, state->head);
TRY_FREE(strm, state->prev);
TRY_FREE(strm, state->window);
free(state->pending_buf);
free(state->head);
free(state->prev);
free(state->window);
ZFREE(strm, state);
free(state);
state = Z_NULL;
return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
@ -1362,19 +1351,19 @@ int deflateCopy (z_streamp dest, z_streamp source)
ss = (deflate_state*)source->state;
zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
memcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
ds = (deflate_state *)calloc(1, sizeof(deflate_state));
if (ds == Z_NULL)
return Z_MEM_ERROR;
dest->state = (struct internal_state FAR *) ds;
zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state));
memcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state));
ds->strm = dest;
ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
ds->window = (Bytef *)calloc(ds->w_size, 2*sizeof(Byte));
ds->prev = (Posf *) calloc(ds->w_size, sizeof(Pos));
ds->head = (Posf *) calloc(ds->hash_size, sizeof(Pos));
overlay = (ushf *) calloc(ds->lit_bufsize, sizeof(ush)+2);
ds->pending_buf = (uchf *) overlay;
if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
@ -1384,11 +1373,10 @@ int deflateCopy (z_streamp dest, z_streamp source)
return Z_MEM_ERROR;
}
/* following zmemcpy do not work for 16-bit MSDOS */
zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos));
zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos));
zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
memcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
memcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos));
memcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos));
memcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);

126
deps/libz/gzlib.c vendored
View File

@ -69,24 +69,25 @@ char ZLIB_INTERNAL *gz_strwinerror (DWORD error)
/* Reset gzip file state */
static void gz_reset(gz_statep state)
{
state->x.have = 0; /* no output data available */
if (state->mode == GZ_READ) { /* for reading ... */
state->eof = 0; /* not at end of file */
state->past = 0; /* have not read past end yet */
state->how = LOOK; /* look for gzip header */
state->x.have = 0; /* no output data available */
if (state->mode == GZ_READ) /* for reading ... */
{
state->eof = 0; /* not at end of file */
state->past = 0; /* have not read past end yet */
state->how = LOOK; /* look for gzip header */
}
state->seek = 0; /* no seek request pending */
state->seek = 0; /* no seek request pending */
gz_error(state, Z_OK, NULL); /* clear error */
state->x.pos = 0; /* no uncompressed data yet */
state->x.pos = 0; /* no uncompressed data yet */
state->strm.avail_in = 0; /* no input data yet */
}
/* Open a gzip file either by name or file descriptor. */
static gzFile gz_open(const void *path, int fd, const char *mode)
{
int oflag;
gz_statep state;
size_t len;
int oflag;
#ifdef O_CLOEXEC
int cloexec = 0;
#endif
@ -99,23 +100,25 @@ static gzFile gz_open(const void *path, int fd, const char *mode)
return NULL;
/* allocate gzFile structure to return */
state = (gz_statep)malloc(sizeof(gz_state));
state = (gz_statep)malloc(sizeof(gz_state));
if (state == NULL)
return NULL;
state->size = 0; /* no buffers allocated yet */
state->want = GZBUFSIZE; /* requested buffer size */
state->msg = NULL; /* no error message yet */
state->msg = NULL; /* no error message yet */
/* interpret mode */
state->mode = GZ_NONE;
state->level = Z_DEFAULT_COMPRESSION;
state->mode = GZ_NONE;
state->level = Z_DEFAULT_COMPRESSION;
state->strategy = Z_DEFAULT_STRATEGY;
state->direct = 0;
while (*mode) {
state->direct = 0;
while (*mode)
{
if (*mode >= '0' && *mode <= '9')
state->level = *mode - '0';
else
switch (*mode) {
switch (*mode)
{
case 'r':
state->mode = GZ_READ;
break;
@ -164,14 +167,17 @@ static gzFile gz_open(const void *path, int fd, const char *mode)
}
/* must provide an "r", "w", or "a" */
if (state->mode == GZ_NONE) {
if (state->mode == GZ_NONE)
{
free(state);
return NULL;
}
/* can't force transparent read */
if (state->mode == GZ_READ) {
if (state->direct) {
if (state->mode == GZ_READ)
{
if (state->direct)
{
free(state);
return NULL;
}
@ -180,7 +186,8 @@ static gzFile gz_open(const void *path, int fd, const char *mode)
/* save the path name for error messages */
#ifdef _WIN32
if (fd == -2) {
if (fd == -2)
{
len = wcstombs(NULL, (const wchar_t*)path, 0);
if (len == (size_t)-1)
len = 0;
@ -189,7 +196,8 @@ static gzFile gz_open(const void *path, int fd, const char *mode)
#endif
len = strlen((const char *)path);
state->path = (char *)malloc(len + 1);
if (state->path == NULL) {
if (state->path == NULL)
{
free(state);
return NULL;
}
@ -243,7 +251,8 @@ static gzFile gz_open(const void *path, int fd, const char *mode)
state->mode = GZ_WRITE; /* simplify later checks */
/* 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);
if (state->start == -1) state->start = 0;
}
@ -321,8 +330,10 @@ int gzrewind(gzFile file)
state = (gz_statep)file;
/* check that we're reading and that there's no error */
if (state->mode != GZ_READ ||
(state->err != Z_OK && state->err != Z_BUF_ERROR))
if (
state->mode != GZ_READ ||
( state->err != Z_OK
&& state->err != Z_BUF_ERROR))
return -1;
/* back up and start over */
@ -335,22 +346,24 @@ int gzrewind(gzFile file)
z_off64_t gzseek64(gzFile file, z_off64_t offset, int whence)
{
unsigned n;
z_off64_t ret;
gz_statep state;
/* get internal structure and check integrity */
if (file == NULL)
return -1;
state = (gz_statep)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
if ( state->mode != GZ_READ &&
state->mode != GZ_WRITE)
return -1;
/* check that there's no error */
if (state->err != Z_OK && state->err != Z_BUF_ERROR)
if ( state->err != Z_OK &&
state->err != Z_BUF_ERROR)
return -1;
/* can only seek from start or relative to current position */
if (whence != SEEK_SET && whence != SEEK_CUR)
if ( whence != SEEK_SET &&
whence != SEEK_CUR)
return -1;
/* normalize offset to a SEEK_CUR specification */
@ -361,23 +374,28 @@ z_off64_t gzseek64(gzFile file, z_off64_t offset, int whence)
state->seek = 0;
/* if within raw area while reading, just go there */
if (state->mode == GZ_READ && state->how == MODE_COPY &&
state->x.pos + offset >= 0) {
ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR);
if ( state->mode == GZ_READ &&
state->how == MODE_COPY &&
state->x.pos + offset >= 0)
{
z_off64_t ret = LSEEK(
state->fd, offset - state->x.have, SEEK_CUR);
if (ret == -1)
return -1;
state->x.have = 0;
state->eof = 0;
state->past = 0;
state->seek = 0;
state->x.have = 0;
state->eof = 0;
state->past = 0;
state->seek = 0;
gz_error(state, Z_OK, NULL);
state->strm.avail_in = 0;
state->x.pos += offset;
state->strm.avail_in = 0;
state->x.pos += offset;
return state->x.pos;
}
/* calculate skip amount, rewinding if needed for back seek when reading */
if (offset < 0) {
/* calculate skip amount, rewinding if
* needed for back seek when reading */
if (offset < 0)
{
if (state->mode != GZ_READ) /* writing -- can't go backwards */
return -1;
offset += state->x.pos;
@ -388,17 +406,19 @@ z_off64_t gzseek64(gzFile file, z_off64_t offset, int whence)
}
/* if reading, skip what's in output buffer (one less gzgetc() check) */
if (state->mode == GZ_READ) {
if (state->mode == GZ_READ)
{
n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ?
(unsigned)offset : state->x.have;
state->x.have -= n;
state->x.next += n;
state->x.pos += n;
offset -= n;
state->x.pos += n;
offset -= n;
}
/* request skip (if not zero) */
if (offset) {
if (offset)
{
state->seek = 1;
state->skip = offset;
}
@ -407,9 +427,7 @@ z_off64_t gzseek64(gzFile file, z_off64_t offset, int whence)
z_off_t gzseek(gzFile file, z_off_t offset, int whence)
{
z_off64_t ret;
ret = gzseek64(file, (z_off64_t)offset, whence);
z_off64_t ret = gzseek64(file, (z_off64_t)offset, whence);
return ret == (z_off_t)ret ? (z_off_t)ret : -1;
}
@ -421,7 +439,8 @@ z_off64_t gztell64(gzFile file)
if (file == NULL)
return -1;
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 position */
@ -430,9 +449,7 @@ z_off64_t gztell64(gzFile file)
z_off_t gztell(gzFile file)
{
z_off64_t ret;
ret = gztell64(file);
z_off64_t ret = gztell64(file);
return ret == (z_off_t)ret ? (z_off_t)ret : -1;
}
@ -471,7 +488,8 @@ int gzeof(gzFile file)
if (file == NULL)
return 0;
state = (gz_statep)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
if ( state->mode != GZ_READ &&
state->mode != GZ_WRITE)
return 0;
/* return end-of-file state */
@ -486,7 +504,9 @@ const char * gzerror(gzFile file, int *errnum)
if (file == NULL)
return NULL;
state = (gz_statep)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
if ( state->mode != GZ_READ &&
state->mode != GZ_WRITE)
return NULL;
/* return error information */
@ -504,7 +524,9 @@ void gzclearerr(gzFile file)
if (file == NULL)
return;
state = (gz_statep)file;
if (state->mode != GZ_READ && state->mode != GZ_WRITE)
if ( state->mode != GZ_READ &&
state->mode != GZ_WRITE)
return;
/* clear error and end-of-file */

188
deps/libz/gzread.c vendored
View File

@ -20,7 +20,8 @@ static int gz_load(gz_statep state, unsigned char *buf, unsigned len, unsigned *
break;
*have += ret;
} while (*have < len);
if (ret < 0) {
if (ret < 0)
{
gz_error(state, Z_ERRNO, zstrerror());
return -1;
}
@ -43,8 +44,10 @@ static int gz_avail(gz_statep state)
if (state->err != Z_OK && state->err != Z_BUF_ERROR)
return -1;
if (state->eof == 0) {
if (strm->avail_in) { /* copy what's there to the start */
if (state->eof == 0)
{
if (strm->avail_in) /* copy what's there to the start */
{
unsigned char *p = state->in;
unsigned const char *q = strm->next_in;
unsigned n = strm->avail_in;
@ -75,11 +78,13 @@ static int gz_look(gz_statep state)
z_streamp strm = &(state->strm);
/* allocate read buffers and inflate memory */
if (state->size == 0) {
if (state->size == 0)
{
/* allocate buffers */
state->in = (unsigned char *)malloc(state->want);
state->out = (unsigned char *)malloc(state->want << 1);
if (state->in == NULL || state->out == NULL) {
if (state->in == NULL || state->out == NULL)
{
if (state->out != NULL)
free(state->out);
if (state->in != NULL)
@ -95,7 +100,8 @@ static int gz_look(gz_statep state)
state->strm.opaque = Z_NULL;
state->strm.avail_in = 0;
state->strm.next_in = Z_NULL;
if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) { /* gunzip */
if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) /* gunzip */
{
free(state->out);
free(state->in);
state->size = 0;
@ -105,7 +111,8 @@ static int gz_look(gz_statep state)
}
/* get at least the magic bytes in the input buffer */
if (strm->avail_in < 2) {
if (strm->avail_in < 2)
{
if (gz_avail(state) == -1)
return -1;
if (strm->avail_in == 0)
@ -119,8 +126,10 @@ static int gz_look(gz_statep state)
file -- for here we assume that if a gzip file is being written, then
the header will be written in a single operation, so that reading a
single byte is sufficient indication that it is not a gzip file) */
if (strm->avail_in > 1 &&
strm->next_in[0] == 31 && strm->next_in[1] == 139) {
if ( strm->avail_in > 1 &&
strm->next_in[0] == 31 &&
strm->next_in[1] == 139)
{
inflateReset(strm);
state->how = MODE_GZIP;
state->direct = 0;
@ -129,10 +138,11 @@ static int gz_look(gz_statep state)
/* no gzip header -- if we were decoding gzip before, then this is trailing
garbage. Ignore the trailing garbage and finish. */
if (state->direct == 0) {
if (state->direct == 0)
{
strm->avail_in = 0;
state->eof = 1;
state->x.have = 0;
state->eof = 1;
state->x.have = 0;
return 0;
}
@ -140,12 +150,13 @@ static int gz_look(gz_statep state)
the output buffer is larger than the input buffer, which also assures
space for gzungetc() */
state->x.next = state->out;
if (strm->avail_in) {
if (strm->avail_in)
{
memcpy(state->x.next, strm->next_in, strm->avail_in);
state->x.have = strm->avail_in;
strm->avail_in = 0;
}
state->how = MODE_COPY;
state->how = MODE_COPY;
state->direct = 1;
return 0;
}
@ -158,32 +169,36 @@ static int gz_look(gz_statep state)
static int gz_decomp(gz_statep state)
{
int ret = Z_OK;
unsigned had;
z_streamp strm = &(state->strm);
/* fill output buffer up to end of deflate stream */
had = strm->avail_out;
do {
unsigned had = strm->avail_out;
do
{
/* get more input for inflate() */
if (strm->avail_in == 0 && gz_avail(state) == -1)
return -1;
if (strm->avail_in == 0) {
if (strm->avail_in == 0)
{
gz_error(state, Z_BUF_ERROR, "unexpected end of file");
break;
}
/* decompress and handle errors */
ret = inflate(strm, Z_NO_FLUSH);
if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) {
if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT)
{
gz_error(state, Z_STREAM_ERROR,
"internal error: inflate stream corrupt");
return -1;
}
if (ret == Z_MEM_ERROR) {
if (ret == Z_MEM_ERROR)
{
gz_error(state, Z_MEM_ERROR, "out of memory");
return -1;
}
if (ret == Z_DATA_ERROR) { /* deflate stream invalid */
if (ret == Z_DATA_ERROR) /* deflate stream invalid */
{
gz_error(state, Z_DATA_ERROR,
strm->msg == NULL ? "compressed data error" : strm->msg);
return -1;
@ -191,7 +206,7 @@ static int gz_decomp(gz_statep state)
} while (strm->avail_out && ret != Z_STREAM_END);
/* update available output */
state->x.have = had - strm->avail_out;
state->x.have = had - strm->avail_out;
state->x.next = strm->next_out - state->x.have;
/* if the gzip stream completed successfully, look for another */
@ -212,8 +227,10 @@ static int gz_fetch(gz_statep state)
{
z_streamp strm = &(state->strm);
do {
switch(state->how) {
do
{
switch(state->how)
{
case LOOK: /* -> LOOK, MODE_COPY (only if never GZIP), or MODE_GZIP */
if (gz_look(state) == -1)
return -1;
@ -244,7 +261,8 @@ static int gz_skip(gz_statep state, z_off64_t len)
/* skip over len bytes or reach end-of-file, whichever comes first */
while (len)
/* skip over whatever is in output buffer */
if (state->x.have) {
if (state->x.have)
{
n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > len ?
(unsigned)len : state->x.have;
state->x.have -= n;
@ -258,7 +276,8 @@ static int gz_skip(gz_statep state, z_off64_t len)
break;
/* need more data to skip -- load up output buffer */
else {
else
{
/* get more output, looking for header if required */
if (gz_fetch(state) == -1)
return -1;
@ -276,16 +295,19 @@ int gzread(gzFile file, voidp buf, unsigned len)
if (file == NULL)
return -1;
state = (gz_statep)file;
strm = &(state->strm);
strm = &(state->strm);
/* check that we're reading and that there's no (serious) error */
if (state->mode != GZ_READ ||
(state->err != Z_OK && state->err != Z_BUF_ERROR))
if (
state->mode != GZ_READ ||
( state->err != Z_OK &&
state->err != Z_BUF_ERROR))
return -1;
/* since an int is returned, make sure len fits in one, otherwise return
with an error (this avoids the flaw in the interface) */
if ((int)len < 0) {
if ((int)len < 0)
{
gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
return -1;
}
@ -295,7 +317,8 @@ int gzread(gzFile file, voidp buf, unsigned len)
return 0;
/* process a skip request */
if (state->seek) {
if (state->seek)
{
state->seek = 0;
if (gz_skip(state, state->skip) == -1)
return -1;
@ -303,10 +326,12 @@ int gzread(gzFile file, voidp buf, unsigned len)
/* get len bytes to buf, or less than len if at the end */
got = 0;
n = 0;
do {
n = 0;
do
{
/* first just try copying data from the output buffer */
if (state->x.have) {
if (state->x.have)
{
n = state->x.have > len ? len : state->x.have;
memcpy(buf, state->x.next, n);
state->x.next += n;
@ -314,14 +339,16 @@ int gzread(gzFile file, voidp buf, unsigned len)
}
/* output buffer empty -- return if we're at the end of the input */
else if (state->eof && strm->avail_in == 0) {
else if (state->eof && strm->avail_in == 0)
{
state->past = 1; /* tried to read past end */
break;
}
/* need output data -- for small len or new stream load up our output
buffer */
else if (state->how == LOOK || len < (state->size << 1)) {
else if (state->how == LOOK || len < (state->size << 1))
{
/* get more output, looking for header if required */
if (gz_fetch(state) == -1)
return -1;
@ -331,26 +358,28 @@ int gzread(gzFile file, voidp buf, unsigned len)
}
/* large len -- read directly into user buffer */
else if (state->how == MODE_COPY) { /* read directly */
else if (state->how == MODE_COPY) /* read directly */
{
if (gz_load(state, (unsigned char *)buf, len, &n) == -1)
return -1;
}
/* large len -- decompress directly into user buffer */
else { /* state->how == GZIP */
else /* state->how == GZIP */
{
strm->avail_out = len;
strm->next_out = (unsigned char *)buf;
strm->next_out = (unsigned char *)buf;
if (gz_decomp(state) == -1)
return -1;
n = state->x.have;
state->x.have = 0;
n = state->x.have;
state->x.have = 0;
}
/* update progress */
len -= n;
buf = (char *)buf + n;
got += n;
state->x.pos += n;
len -= n;
buf = (char *)buf + n;
got += n;
state->x.pos += n;
} while (len);
/* return number of bytes read into user buffer (will fit in int) */
@ -374,12 +403,15 @@ int gzgetc(gzFile file)
state = (gz_statep)file;
/* check that we're reading and that there's no (serious) error */
if (state->mode != GZ_READ ||
(state->err != Z_OK && state->err != Z_BUF_ERROR))
if (
state->mode != GZ_READ ||
(state->err != Z_OK &&
state->err != Z_BUF_ERROR))
return -1;
/* try output buffer (no need to check for skip request) */
if (state->x.have) {
if (state->x.have)
{
state->x.have--;
state->x.pos++;
return *(state->x.next)++;
@ -405,12 +437,15 @@ int gzungetc(int c, gzFile file)
state = (gz_statep)file;
/* check that we're reading and that there's no (serious) error */
if (state->mode != GZ_READ ||
(state->err != Z_OK && state->err != Z_BUF_ERROR))
if (
state->mode != GZ_READ ||
(state->err != Z_OK &&
state->err != Z_BUF_ERROR))
return -1;
/* process a skip request */
if (state->seek) {
if (state->seek)
{
state->seek = 0;
if (gz_skip(state, state->skip) == -1)
return -1;
@ -421,34 +456,37 @@ int gzungetc(int c, gzFile file)
return -1;
/* if output buffer empty, put byte at end (allows more pushing) */
if (state->x.have == 0) {
state->x.have = 1;
state->x.next = state->out + (state->size << 1) - 1;
if (state->x.have == 0)
{
state->x.have = 1;
state->x.next = state->out + (state->size << 1) - 1;
state->x.next[0] = c;
state->x.pos--;
state->past = 0;
state->past = 0;
return c;
}
/* if no room, give up (must have already done a gzungetc()) */
if (state->x.have == (state->size << 1)) {
if (state->x.have == (state->size << 1))
{
gz_error(state, Z_DATA_ERROR, "out of room to push characters");
return -1;
}
/* slide output data if needed and insert byte before existing data */
if (state->x.next == state->out) {
unsigned char *src = state->out + state->x.have;
if (state->x.next == state->out)
{
unsigned char *src = state->out + state->x.have;
unsigned char *dest = state->out + (state->size << 1);
while (src > state->out)
*--dest = *--src;
state->x.next = dest;
state->x.next = dest;
}
state->x.have++;
state->x.next--;
state->x.next[0] = c;
state->x.pos--;
state->past = 0;
state->past = 0;
return c;
}
@ -465,12 +503,15 @@ char * gzgets(gzFile file, char *buf, int len)
state = (gz_statep)file;
/* check that we're reading and that there's no (serious) error */
if (state->mode != GZ_READ ||
(state->err != Z_OK && state->err != Z_BUF_ERROR))
if (
state->mode != GZ_READ ||
(state->err != Z_OK &&
state->err != Z_BUF_ERROR))
return NULL;
/* process a skip request */
if (state->seek) {
if (state->seek)
{
state->seek = 0;
if (gz_skip(state, state->skip) == -1)
return NULL;
@ -485,13 +526,15 @@ char * gzgets(gzFile file, char *buf, int len)
/* assure that something is in the output buffer */
if (state->x.have == 0 && gz_fetch(state) == -1)
return NULL; /* error */
if (state->x.have == 0) { /* end of file */
if (state->x.have == 0) /* end of file */
{
state->past = 1; /* read past end */
break; /* return what we have */
}
/* look for end-of-line in current output buffer */
n = state->x.have > left ? left : state->x.have;
n = state->x.have > left ? left : state->x.have;
eol = (unsigned char *)memchr(state->x.next, '\n', n);
if (eol != NULL)
n = (unsigned)(eol - state->x.next) + 1;
@ -500,9 +543,9 @@ char * gzgets(gzFile file, char *buf, int len)
memcpy(buf, state->x.next, n);
state->x.have -= n;
state->x.next += n;
state->x.pos += n;
left -= n;
buf += n;
state->x.pos += n;
left -= n;
buf += n;
} while (left && eol == NULL);
/* return terminated string, or if nothing, end of file */
@ -523,7 +566,9 @@ int gzdirect(gzFile file)
/* if the state is not known, but we can find out, then do so (this is
mainly for right after a gzopen() or gzdopen()) */
if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0)
if ( state->mode == GZ_READ &&
state->how == LOOK &&
state->x.have == 0)
(void)gz_look(state);
/* return 1 if transparent, 0 if processing a gzip stream */
@ -545,7 +590,8 @@ int gzclose_r(gzFile file)
return Z_STREAM_ERROR;
/* free memory and close file */
if (state->size) {
if (state->size)
{
inflateEnd(&(state->strm));
free(state->out);
free(state->in);

528
deps/libz/gzwrite.c vendored
View File

@ -16,43 +16,48 @@ static int gz_init(gz_statep state)
/* allocate input buffer */
state->in = (unsigned char *)malloc(state->want);
if (state->in == NULL) {
if (state->in == NULL)
{
gz_error(state, Z_MEM_ERROR, "out of memory");
return -1;
}
/* only need output buffer and deflate state if compressing */
if (!state->direct) {
/* allocate output buffer */
state->out = (unsigned char *)malloc(state->want);
if (state->out == NULL) {
free(state->in);
gz_error(state, Z_MEM_ERROR, "out of memory");
return -1;
}
if (!state->direct)
{
/* allocate output buffer */
state->out = (unsigned char *)malloc(state->want);
if (state->out == NULL)
{
free(state->in);
gz_error(state, Z_MEM_ERROR, "out of memory");
return -1;
}
/* allocate deflate memory, set up for gzip compression */
strm->zalloc = Z_NULL;
strm->zfree = Z_NULL;
strm->opaque = Z_NULL;
ret = deflateInit2(strm, state->level, Z_DEFLATED,
MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
if (ret != Z_OK) {
free(state->out);
free(state->in);
gz_error(state, Z_MEM_ERROR, "out of memory");
return -1;
}
/* allocate deflate memory, set up for gzip compression */
strm->zalloc = Z_NULL;
strm->zfree = Z_NULL;
strm->opaque = Z_NULL;
ret = deflateInit2(strm, state->level, Z_DEFLATED,
MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
if (ret != Z_OK)
{
free(state->out);
free(state->in);
gz_error(state, Z_MEM_ERROR, "out of memory");
return -1;
}
}
/* mark state as initialized */
state->size = state->want;
state->size = state->want;
/* initialize write buffer if compressing */
if (!state->direct) {
strm->avail_out = state->size;
strm->next_out = state->out;
state->x.next = strm->next_out;
if (!state->direct)
{
strm->avail_out = state->size;
strm->next_out = state->out;
state->x.next = strm->next_out;
}
return 0;
}
@ -74,9 +79,11 @@ static int gz_comp(gz_statep state, int flush)
return -1;
/* write directly if requested */
if (state->direct) {
if (state->direct)
{
got = write(state->fd, strm->next_in, strm->avail_in);
if (got < 0 || (unsigned)got != strm->avail_in) {
if (got < 0 || (unsigned)got != strm->avail_in)
{
gz_error(state, Z_ERRNO, zstrerror());
return -1;
}
@ -90,24 +97,28 @@ static int gz_comp(gz_statep state, int flush)
/* write out current buffer contents if full, or if flushing, but if
doing Z_FINISH then don't write until we get to Z_STREAM_END */
if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
(flush != Z_FINISH || ret == Z_STREAM_END))) {
have = (unsigned)(strm->next_out - state->x.next);
if (have && ((got = write(state->fd, state->x.next, have)) < 0 ||
(unsigned)got != have)) {
gz_error(state, Z_ERRNO, zstrerror());
return -1;
}
if (strm->avail_out == 0) {
strm->avail_out = state->size;
strm->next_out = state->out;
}
state->x.next = strm->next_out;
(flush != Z_FINISH || ret == Z_STREAM_END)))
{
have = (unsigned)(strm->next_out - state->x.next);
if (have && ((got = write(state->fd, state->x.next, have)) < 0 ||
(unsigned)got != have))
{
gz_error(state, Z_ERRNO, zstrerror());
return -1;
}
if (strm->avail_out == 0)
{
strm->avail_out = state->size;
strm->next_out = state->out;
}
state->x.next = strm->next_out;
}
/* compress */
have = strm->avail_out;
ret = deflate(strm, flush);
if (ret == Z_STREAM_ERROR) {
ret = deflate(strm, flush);
if (ret == Z_STREAM_ERROR)
{
gz_error(state, Z_STREAM_ERROR,
"internal error: deflate stream corrupt");
return -1;
@ -127,7 +138,6 @@ static int gz_comp(gz_statep state, int flush)
static int gz_zero(gz_statep state, z_off64_t len)
{
int first;
unsigned n;
z_streamp strm = &(state->strm);
/* consume whatever's left in the input buffer */
@ -136,16 +146,18 @@ static int gz_zero(gz_statep state, z_off64_t len)
/* compress len zeros (len guaranteed > 0) */
first = 1;
while (len) {
n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
while (len)
{
unsigned n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
(unsigned)len : state->size;
if (first) {
if (first)
{
memset(state->in, 0, n);
first = 0;
}
strm->avail_in = n;
strm->next_in = state->in;
state->x.pos += n;
strm->next_in = state->in;
state->x.pos += n;
if (gz_comp(state, Z_NO_FLUSH) == -1)
return -1;
len -= n;
@ -155,23 +167,25 @@ static int gz_zero(gz_statep state, z_off64_t len)
int gzwrite(gzFile file, voidpc buf, unsigned len)
{
unsigned put = len;
gz_statep state;
z_streamp strm;
unsigned put = len;
/* get internal structure */
if (file == NULL)
return 0;
state = (gz_statep)file;
strm = &(state->strm);
strm = &(state->strm);
/* check that we're writing and that there's no error */
if (state->mode != GZ_WRITE || state->err != Z_OK)
if ( state->mode != GZ_WRITE ||
state->err != Z_OK)
return 0;
/* since an int is returned, make sure len fits in one, otherwise return
with an error (this avoids the flaw in the interface) */
if ((int)len < 0) {
if ((int)len < 0)
{
gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
return 0;
}
@ -185,44 +199,47 @@ int gzwrite(gzFile file, voidpc buf, unsigned len)
return 0;
/* check for seek request */
if (state->seek) {
if (state->seek)
{
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return 0;
}
/* for small len, copy to input buffer, otherwise compress directly */
if (len < state->size) {
/* copy to input buffer, compress when full */
do {
unsigned have, copy;
if (len < state->size)
{
/* copy to input buffer, compress when full */
do {
unsigned have, copy;
if (strm->avail_in == 0)
strm->next_in = state->in;
have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
copy = state->size - have;
if (copy > len)
copy = len;
memcpy(state->in + have, buf, copy);
strm->avail_in += copy;
state->x.pos += copy;
buf = (const char *)buf + copy;
len -= copy;
if (len && gz_comp(state, Z_NO_FLUSH) == -1)
return 0;
} while (len);
if (strm->avail_in == 0)
strm->next_in = state->in;
have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
copy = state->size - have;
if (copy > len)
copy = len;
memcpy(state->in + have, buf, copy);
strm->avail_in += copy;
state->x.pos += copy;
buf = (const char *)buf + copy;
len -= copy;
if (len && gz_comp(state, Z_NO_FLUSH) == -1)
return 0;
} while (len);
}
else {
/* consume whatever's left in the input buffer */
if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
return 0;
else
{
/* consume whatever's left in the input buffer */
if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
return 0;
/* directly compress user buffer to file */
strm->avail_in = len;
strm->next_in = (Bytef *)buf;
state->x.pos += len;
if (gz_comp(state, Z_NO_FLUSH) == -1)
return 0;
/* directly compress user buffer to file */
strm->avail_in = len;
strm->next_in = (Bytef *)buf;
state->x.pos += len;
if (gz_comp(state, Z_NO_FLUSH) == -1)
return 0;
}
/* input was all buffered or compressed (put will fit in int) */
@ -231,235 +248,244 @@ int gzwrite(gzFile file, voidpc buf, unsigned len)
int gzputc(gzFile file, int c)
{
unsigned have;
unsigned char buf[1];
gz_statep state;
z_streamp strm;
unsigned have;
unsigned char buf[1];
gz_statep state;
z_streamp strm;
/* get internal structure */
if (file == NULL)
return -1;
state = (gz_statep)file;
strm = &(state->strm);
/* get internal structure */
if (file == NULL)
return -1;
state = (gz_statep)file;
strm = &(state->strm);
/* check that we're writing and that there's no error */
if (state->mode != GZ_WRITE || state->err != Z_OK)
return -1;
/* check that we're writing and that there's no error */
if ( state->mode != GZ_WRITE ||
state->err != Z_OK)
return -1;
/* check for seek request */
if (state->seek) {
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return -1;
}
/* check for seek request */
if (state->seek)
{
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return -1;
}
/* try writing to input buffer for speed (state->size == 0 if buffer not
initialized) */
if (state->size) {
if (strm->avail_in == 0)
strm->next_in = state->in;
have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
if (have < state->size) {
state->in[have] = c;
strm->avail_in++;
state->x.pos++;
return c & 0xff;
}
}
/* try writing to input buffer for speed (state->size == 0 if buffer not
initialized) */
if (state->size)
{
if (strm->avail_in == 0)
strm->next_in = state->in;
have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
if (have < state->size)
{
state->in[have] = c;
strm->avail_in++;
state->x.pos++;
return c & 0xff;
}
}
/* no room in buffer or not initialized, use gz_write() */
buf[0] = c;
if (gzwrite(file, buf, 1) != 1)
return -1;
return c & 0xff;
/* no room in buffer or not initialized, use gz_write() */
buf[0] = c;
if (gzwrite(file, buf, 1) != 1)
return -1;
return c & 0xff;
}
int gzputs(gzFile file, const char *str)
{
int ret;
unsigned len;
/* write string */
len = (unsigned)strlen(str);
ret = gzwrite(file, str, len);
return ret == 0 && len != 0 ? -1 : ret;
/* write string */
unsigned len = (unsigned)strlen(str);
int ret = gzwrite(file, str, len);
return ret == 0 && len != 0 ? -1 : ret;
}
int gzvprintf(gzFile file, const char *format, va_list va)
{
int size, len;
gz_statep state;
z_streamp strm;
int size, len;
gz_statep state;
z_streamp strm;
/* get internal structure */
if (file == NULL)
return -1;
state = (gz_statep)file;
strm = &(state->strm);
/* get internal structure */
if (file == NULL)
return -1;
state = (gz_statep)file;
strm = &(state->strm);
/* check that we're writing and that there's no error */
if (state->mode != GZ_WRITE || state->err != Z_OK)
return 0;
/* check that we're writing and that there's no error */
if (state->mode != GZ_WRITE || state->err != Z_OK)
return 0;
/* make sure we have some buffer space */
if (state->size == 0 && gz_init(state) == -1)
return 0;
/* make sure we have some buffer space */
if (state->size == 0 && gz_init(state) == -1)
return 0;
/* check for seek request */
if (state->seek) {
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return 0;
}
/* check for seek request */
if (state->seek) {
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return 0;
}
/* consume whatever's left in the input buffer */
if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
return 0;
/* consume whatever's left in the input buffer */
if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
return 0;
/* do the printf() into the input buffer, put length in len */
size = (int)(state->size);
state->in[size - 1] = 0;
/* do the printf() into the input buffer, put length in len */
size = (int)(state->size);
state->in[size - 1] = 0;
#ifdef NO_vsnprintf
# ifdef HAS_vsprintf_void
(void)vsprintf((char *)(state->in), format, va);
for (len = 0; len < size; len++)
if (state->in[len] == 0) break;
(void)vsprintf((char *)(state->in), format, va);
for (len = 0; len < size; len++)
if (state->in[len] == 0) break;
# else
len = vsprintf((char *)(state->in), format, va);
len = vsprintf((char *)(state->in), format, va);
# endif
#else
# ifdef HAS_vsnprintf_void
(void)vsnprintf((char *)(state->in), size, format, va);
len = strlen((char *)(state->in));
(void)vsnprintf((char *)(state->in), size, format, va);
len = strlen((char *)(state->in));
# else
len = vsnprintf((char *)(state->in), size, format, va);
len = vsnprintf((char *)(state->in), size, format, va);
# endif
#endif
/* check that printf() results fit in buffer */
if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
return 0;
/* check that printf() results fit in buffer */
if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
return 0;
/* update buffer and position, defer compression until needed */
strm->avail_in = (unsigned)len;
strm->next_in = state->in;
state->x.pos += len;
return len;
/* update buffer and position, defer compression until needed */
strm->avail_in = (unsigned)len;
strm->next_in = state->in;
state->x.pos += len;
return len;
}
int gzprintf(gzFile file, const char *format, ...)
{
va_list va;
int ret;
va_list va;
int ret;
va_start(va, format);
ret = gzvprintf(file, format, va);
va_end(va);
return ret;
va_start(va, format);
ret = gzvprintf(file, format, va);
va_end(va);
return ret;
}
int gzflush(gzFile file, int flush)
{
gz_statep state;
gz_statep state;
/* get internal structure */
if (file == NULL)
return -1;
state = (gz_statep)file;
/* get internal structure */
if (file == NULL)
return -1;
state = (gz_statep)file;
/* check that we're writing and that there's no error */
if (state->mode != GZ_WRITE || state->err != Z_OK)
return Z_STREAM_ERROR;
/* check that we're writing and that there's no error */
if (state->mode != GZ_WRITE || state->err != Z_OK)
return Z_STREAM_ERROR;
/* check flush parameter */
if (flush < 0 || flush > Z_FINISH)
return Z_STREAM_ERROR;
/* check flush parameter */
if (flush < 0 || flush > Z_FINISH)
return Z_STREAM_ERROR;
/* check for seek request */
if (state->seek) {
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return -1;
}
/* check for seek request */
if (state->seek)
{
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return -1;
}
/* compress remaining data with requested flush */
gz_comp(state, flush);
return state->err;
/* compress remaining data with requested flush */
gz_comp(state, flush);
return state->err;
}
int gzsetparams(gzFile file, int level, int strategy)
{
gz_statep state;
z_streamp strm;
gz_statep state;
z_streamp strm;
/* get internal structure */
if (file == NULL)
return Z_STREAM_ERROR;
state = (gz_statep)file;
strm = &(state->strm);
/* get internal structure */
if (file == NULL)
return Z_STREAM_ERROR;
state = (gz_statep)file;
strm = &(state->strm);
/* check that we're writing and that there's no error */
if (state->mode != GZ_WRITE || state->err != Z_OK)
return Z_STREAM_ERROR;
/* check that we're writing and that there's no error */
if ( state->mode != GZ_WRITE ||
state->err != Z_OK)
return Z_STREAM_ERROR;
/* if no change is requested, then do nothing */
if (level == state->level && strategy == state->strategy)
return Z_OK;
/* if no change is requested, then do nothing */
if ( level == state->level &&
strategy == state->strategy)
return Z_OK;
/* check for seek request */
if (state->seek) {
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return -1;
}
/* check for seek request */
if (state->seek)
{
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
return -1;
}
/* change compression parameters for subsequent input */
if (state->size) {
/* flush previous input with previous parameters before changing */
if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1)
return state->err;
deflateParams(strm, level, strategy);
}
state->level = level;
state->strategy = strategy;
return Z_OK;
/* change compression parameters for subsequent input */
if (state->size)
{
/* flush previous input with previous parameters before changing */
if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1)
return state->err;
deflateParams(strm, level, strategy);
}
state->level = level;
state->strategy = strategy;
return Z_OK;
}
int gzclose_w(gzFile file)
{
int ret = Z_OK;
gz_statep state;
int ret = Z_OK;
gz_statep state;
/* get internal structure */
if (file == NULL)
return Z_STREAM_ERROR;
state = (gz_statep)file;
/* get internal structure */
if (file == NULL)
return Z_STREAM_ERROR;
state = (gz_statep)file;
/* check that we're writing */
if (state->mode != GZ_WRITE)
return Z_STREAM_ERROR;
/* check that we're writing */
if (state->mode != GZ_WRITE)
return Z_STREAM_ERROR;
/* check for seek request */
if (state->seek) {
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
ret = state->err;
}
/* check for seek request */
if (state->seek)
{
state->seek = 0;
if (gz_zero(state, state->skip) == -1)
ret = state->err;
}
/* flush, free memory, and close file */
if (gz_comp(state, Z_FINISH) == -1)
ret = state->err;
if (state->size) {
if (!state->direct) {
(void)deflateEnd(&(state->strm));
free(state->out);
}
free(state->in);
}
gz_error(state, Z_OK, NULL);
free(state->path);
if (close(state->fd) == -1)
ret = Z_ERRNO;
free(state);
return ret;
/* flush, free memory, and close file */
if (gz_comp(state, Z_FINISH) == -1)
ret = state->err;
if (state->size)
{
if (!state->direct)
{
(void)deflateEnd(&(state->strm));
free(state->out);
}
free(state->in);
}
gz_error(state, Z_OK, NULL);
free(state->path);
if (close(state->fd) == -1)
ret = Z_ERRNO;
free(state);
return ret;
}

65
deps/libz/inflate.c vendored
View File

@ -171,7 +171,7 @@ int inflateReset2(z_streamp strm, int windowBits)
if ( state->window != Z_NULL &&
state->wbits != (unsigned)windowBits)
{
ZFREE(strm, state->window);
free(state->window);
state->window = Z_NULL;
}
@ -193,24 +193,12 @@ int inflateInit2_(z_streamp strm, int windowBits,
return Z_VERSION_ERROR;
if (strm == Z_NULL)
return Z_STREAM_ERROR;
strm->msg = Z_NULL; /* in case we return an error */
if (strm->zalloc == (alloc_func)0)
{
#ifdef Z_SOLO
return Z_STREAM_ERROR;
#else
strm->zalloc = zcalloc;
strm->opaque = (voidpf)0;
#endif
}
if (strm->zfree == Z_NULL)
#ifdef Z_SOLO
return Z_STREAM_ERROR;
#else
strm->msg = Z_NULL; /* in case we return an error */
strm->zalloc = zcalloc;
strm->opaque = (voidpf)0;
strm->zfree = zcfree;
#endif
state = (struct inflate_state FAR *)
ZALLOC(strm, 1, sizeof(struct inflate_state));
calloc(1, sizeof(struct inflate_state));
if (state == Z_NULL)
return Z_MEM_ERROR;
strm->state = (struct internal_state FAR *)state;
@ -218,7 +206,7 @@ int inflateInit2_(z_streamp strm, int windowBits,
ret = inflateReset2(strm, windowBits);
if (ret != Z_OK)
{
ZFREE(strm, state);
free(state);
strm->state = Z_NULL;
}
return ret;
@ -395,7 +383,7 @@ static int updatewindow(z_streamp strm, const Bytef *end, unsigned copy)
if (state->window == Z_NULL)
{
state->window = (unsigned char FAR *)
ZALLOC(strm, 1U << state->wbits,
calloc(1U << state->wbits,
sizeof(unsigned char));
if (state->window == Z_NULL)
return 1;
@ -412,7 +400,7 @@ static int updatewindow(z_streamp strm, const Bytef *end, unsigned copy)
/* copy state->wsize or less output bytes into the circular window */
if (copy >= state->wsize)
{
zmemcpy(state->window, end - state->wsize, state->wsize);
memcpy(state->window, end - state->wsize, state->wsize);
state->wnext = 0;
state->whave = state->wsize;
}
@ -420,11 +408,11 @@ static int updatewindow(z_streamp strm, const Bytef *end, unsigned copy)
{
dist = state->wsize - state->wnext;
if (dist > copy) dist = copy;
zmemcpy(state->window + state->wnext, end - copy, dist);
memcpy(state->window + state->wnext, end - copy, dist);
copy -= dist;
if (copy)
{
zmemcpy(state->window, end - copy, copy);
memcpy(state->window, end - copy, copy);
state->wnext = copy;
state->whave = state->wsize;
}
@ -666,7 +654,7 @@ int inflate(z_streamp strm, int flush)
if (state->head != Z_NULL &&
state->head->extra != Z_NULL) {
len = state->head->extra_len - state->length;
zmemcpy(state->head->extra + len, next,
memcpy(state->head->extra + len, next,
len + copy > state->head->extra_max ?
state->head->extra_max - len : copy);
}
@ -803,7 +791,7 @@ int inflate(z_streamp strm, int flush)
if (copy > have) copy = have;
if (copy > left) copy = left;
if (copy == 0) goto inf_leave;
zmemcpy(put, next, copy);
memcpy(put, next, copy);
have -= copy;
next += copy;
left -= copy;
@ -1144,13 +1132,13 @@ int inflateEnd(z_streamp strm)
struct inflate_state FAR *state;
if (
strm == Z_NULL ||
strm->state == Z_NULL ||
strm->zfree == Z_NULL)
strm->state == Z_NULL
)
return Z_STREAM_ERROR;
state = (struct inflate_state FAR *)strm->state;
if (state->window != Z_NULL)
ZFREE(strm, state->window);
ZFREE(strm, strm->state);
free(state->window);
free(strm->state);
strm->state = Z_NULL;
return Z_OK;
}
@ -1169,9 +1157,9 @@ int inflateGetDictionary(z_streamp strm, Bytef *dictionary, uInt *dictLength)
/* copy dictionary */
if (state->whave && dictionary != Z_NULL)
{
zmemcpy(dictionary, state->window + state->wnext,
memcpy(dictionary, state->window + state->wnext,
state->whave - state->wnext);
zmemcpy(dictionary + state->whave - state->wnext,
memcpy(dictionary + state->whave - state->wnext,
state->window, state->wnext);
}
@ -1351,33 +1339,32 @@ int inflateCopy(z_streamp dest, z_streamp source)
/* check input */
if ( dest == Z_NULL ||
source == Z_NULL ||
source->state == Z_NULL ||
source->zalloc == Z_NULL ||
source->zfree == Z_NULL)
source->state == Z_NULL
)
return Z_STREAM_ERROR;
state = (struct inflate_state FAR *)source->state;
/* allocate space */
copy = (struct inflate_state FAR *)
ZALLOC(source, 1, sizeof(struct inflate_state));
calloc(1, sizeof(struct inflate_state));
if (copy == Z_NULL)
return Z_MEM_ERROR;
window = Z_NULL;
if (state->window != Z_NULL)
{
window = (unsigned char FAR *)
ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
calloc(1U << state->wbits, sizeof(unsigned char));
if (window == Z_NULL)
{
ZFREE(source, copy);
free(copy);
return Z_MEM_ERROR;
}
}
/* copy state */
zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
memcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
memcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
if ( state->lencode >= state->codes &&
state->lencode <= state->codes + ENOUGH - 1)
@ -1390,7 +1377,7 @@ int inflateCopy(z_streamp dest, z_streamp source)
if (window != Z_NULL)
{
wsize = 1U << state->wbits;
zmemcpy(window, state->window, wsize);
memcpy(window, state->window, wsize);
}
copy->window = window;
dest->state = (struct internal_state FAR *)copy;

2
deps/libz/uncompr.c vendored
View File

@ -59,6 +59,6 @@ int uncompress (
}
*destLen = stream.total_out;
err = inflateEnd(&stream);
err = inflateEnd(&stream);
return err;
}