mirror of
https://github.com/veracrypt/VeraCrypt.git
synced 2026-06-17 18:16:07 -05:00
Update bundled zlib to 1.3.2
This commit is contained in:
+176
-99
@@ -1,5 +1,5 @@
|
||||
/* gzwrite.c -- zlib functions for writing gzip files
|
||||
* Copyright (C) 2004-2019 Mark Adler
|
||||
* Copyright (C) 2004-2026 Mark Adler
|
||||
* For conditions of distribution and use, see copyright notice in zlib.h
|
||||
*/
|
||||
|
||||
@@ -74,9 +74,17 @@ local int gz_comp(gz_statep state, int flush) {
|
||||
/* write directly if requested */
|
||||
if (state->direct) {
|
||||
while (strm->avail_in) {
|
||||
errno = 0;
|
||||
state->again = 0;
|
||||
put = strm->avail_in > max ? max : strm->avail_in;
|
||||
writ = write(state->fd, strm->next_in, put);
|
||||
writ = (int)write(state->fd, strm->next_in, put);
|
||||
if (writ < 0) {
|
||||
if (errno == EAGAIN
|
||||
#if !defined(_MSC_VER) || _MSC_VER >= 1600 // VS2010 or later
|
||||
|| errno == EWOULDBLOCK
|
||||
#endif
|
||||
)
|
||||
state->again = 1;
|
||||
gz_error(state, Z_ERRNO, zstrerror());
|
||||
return -1;
|
||||
}
|
||||
@@ -88,8 +96,9 @@ local int gz_comp(gz_statep state, int flush) {
|
||||
|
||||
/* check for a pending reset */
|
||||
if (state->reset) {
|
||||
/* don't start a new gzip member unless there is data to write */
|
||||
if (strm->avail_in == 0)
|
||||
/* don't start a new gzip member unless there is data to write and
|
||||
we're not flushing */
|
||||
if (strm->avail_in == 0 && flush == Z_NO_FLUSH)
|
||||
return 0;
|
||||
deflateReset(strm);
|
||||
state->reset = 0;
|
||||
@@ -103,10 +112,18 @@ local int gz_comp(gz_statep state, int flush) {
|
||||
if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
|
||||
(flush != Z_FINISH || ret == Z_STREAM_END))) {
|
||||
while (strm->next_out > state->x.next) {
|
||||
errno = 0;
|
||||
state->again = 0;
|
||||
put = strm->next_out - state->x.next > (int)max ? max :
|
||||
(unsigned)(strm->next_out - state->x.next);
|
||||
writ = write(state->fd, state->x.next, put);
|
||||
writ = (int)write(state->fd, state->x.next, put);
|
||||
if (writ < 0) {
|
||||
if (errno == EAGAIN
|
||||
#if !defined(_MSC_VER) || _MSC_VER >= 1600 // VS2010 or later
|
||||
|| errno == EWOULDBLOCK
|
||||
#endif
|
||||
)
|
||||
state->again = 1;
|
||||
gz_error(state, Z_ERRNO, zstrerror());
|
||||
return -1;
|
||||
}
|
||||
@@ -138,10 +155,12 @@ local int gz_comp(gz_statep state, int flush) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Compress len zeros to output. Return -1 on a write error or memory
|
||||
allocation failure by gz_comp(), or 0 on success. */
|
||||
local int gz_zero(gz_statep state, z_off64_t len) {
|
||||
int first;
|
||||
/* Compress state->skip (> 0) zeros to output. Return -1 on a write error or
|
||||
memory allocation failure by gz_comp(), or 0 on success. state->skip is
|
||||
updated with the number of successfully written zeros, in case there is a
|
||||
stall on a non-blocking write destination. */
|
||||
local int gz_zero(gz_statep state) {
|
||||
int first, ret;
|
||||
unsigned n;
|
||||
z_streamp strm = &(state->strm);
|
||||
|
||||
@@ -149,29 +168,34 @@ local int gz_zero(gz_statep state, z_off64_t len) {
|
||||
if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
|
||||
return -1;
|
||||
|
||||
/* compress len zeros (len guaranteed > 0) */
|
||||
/* compress state->skip zeros */
|
||||
first = 1;
|
||||
while (len) {
|
||||
n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
|
||||
(unsigned)len : state->size;
|
||||
do {
|
||||
n = GT_OFF(state->size) || (z_off64_t)state->size > state->skip ?
|
||||
(unsigned)state->skip : state->size;
|
||||
if (first) {
|
||||
memset(state->in, 0, n);
|
||||
first = 0;
|
||||
}
|
||||
strm->avail_in = n;
|
||||
strm->next_in = state->in;
|
||||
ret = gz_comp(state, Z_NO_FLUSH);
|
||||
n -= strm->avail_in;
|
||||
state->x.pos += n;
|
||||
if (gz_comp(state, Z_NO_FLUSH) == -1)
|
||||
state->skip -= n;
|
||||
if (ret == -1)
|
||||
return -1;
|
||||
len -= n;
|
||||
}
|
||||
} while (state->skip);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Write len bytes from buf to file. Return the number of bytes written. If
|
||||
the returned value is less than len, then there was an error. */
|
||||
the returned value is less than len, then there was an error. If the error
|
||||
was a non-blocking stall, then the number of bytes consumed is returned.
|
||||
For any other error, 0 is returned. */
|
||||
local z_size_t gz_write(gz_statep state, voidpc buf, z_size_t len) {
|
||||
z_size_t put = len;
|
||||
int ret;
|
||||
|
||||
/* if len is zero, avoid unnecessary operations */
|
||||
if (len == 0)
|
||||
@@ -182,16 +206,13 @@ local z_size_t gz_write(gz_statep state, voidpc buf, z_size_t len) {
|
||||
return 0;
|
||||
|
||||
/* check for seek request */
|
||||
if (state->seek) {
|
||||
state->seek = 0;
|
||||
if (gz_zero(state, state->skip) == -1)
|
||||
return 0;
|
||||
}
|
||||
if (state->skip && gz_zero(state) == -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 {
|
||||
for (;;) {
|
||||
unsigned have, copy;
|
||||
|
||||
if (state->strm.avail_in == 0)
|
||||
@@ -206,9 +227,11 @@ local z_size_t gz_write(gz_statep state, voidpc buf, z_size_t len) {
|
||||
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 (len == 0)
|
||||
break;
|
||||
if (gz_comp(state, Z_NO_FLUSH) == -1)
|
||||
return state->again ? put - len : 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* consume whatever's left in the input buffer */
|
||||
@@ -219,13 +242,16 @@ local z_size_t gz_write(gz_statep state, voidpc buf, z_size_t len) {
|
||||
state->strm.next_in = (z_const Bytef *)buf;
|
||||
do {
|
||||
unsigned n = (unsigned)-1;
|
||||
|
||||
if (n > len)
|
||||
n = (unsigned)len;
|
||||
state->strm.avail_in = n;
|
||||
ret = gz_comp(state, Z_NO_FLUSH);
|
||||
n -= state->strm.avail_in;
|
||||
state->x.pos += n;
|
||||
if (gz_comp(state, Z_NO_FLUSH) == -1)
|
||||
return 0;
|
||||
len -= n;
|
||||
if (ret == -1)
|
||||
return state->again ? put - len : 0;
|
||||
} while (len);
|
||||
}
|
||||
|
||||
@@ -242,9 +268,10 @@ int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len) {
|
||||
return 0;
|
||||
state = (gz_statep)file;
|
||||
|
||||
/* check that we're writing and that there's no error */
|
||||
if (state->mode != GZ_WRITE || state->err != Z_OK)
|
||||
/* check that we're writing and that there's no (serious) error */
|
||||
if (state->mode != GZ_WRITE || (state->err != Z_OK && !state->again))
|
||||
return 0;
|
||||
gz_error(state, Z_OK, NULL);
|
||||
|
||||
/* since an int is returned, make sure len fits in one, otherwise return
|
||||
with an error (this avoids a flaw in the interface) */
|
||||
@@ -268,9 +295,10 @@ z_size_t ZEXPORT gzfwrite(voidpc buf, z_size_t size, z_size_t nitems,
|
||||
return 0;
|
||||
state = (gz_statep)file;
|
||||
|
||||
/* check that we're writing and that there's no error */
|
||||
if (state->mode != GZ_WRITE || state->err != Z_OK)
|
||||
/* check that we're writing and that there's no (serious) error */
|
||||
if (state->mode != GZ_WRITE || (state->err != Z_OK && !state->again))
|
||||
return 0;
|
||||
gz_error(state, Z_OK, NULL);
|
||||
|
||||
/* compute bytes to read -- error on overflow */
|
||||
len = nitems * size;
|
||||
@@ -296,16 +324,14 @@ int ZEXPORT gzputc(gzFile file, int c) {
|
||||
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)
|
||||
/* check that we're writing and that there's no (serious) error */
|
||||
if (state->mode != GZ_WRITE || (state->err != Z_OK && !state->again))
|
||||
return -1;
|
||||
gz_error(state, Z_OK, NULL);
|
||||
|
||||
/* check for seek request */
|
||||
if (state->seek) {
|
||||
state->seek = 0;
|
||||
if (gz_zero(state, state->skip) == -1)
|
||||
return -1;
|
||||
}
|
||||
if (state->skip && gz_zero(state) == -1)
|
||||
return -1;
|
||||
|
||||
/* try writing to input buffer for speed (state->size == 0 if buffer not
|
||||
initialized) */
|
||||
@@ -338,9 +364,10 @@ int ZEXPORT gzputs(gzFile file, const char *s) {
|
||||
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)
|
||||
/* check that we're writing and that there's no (serious) error */
|
||||
if (state->mode != GZ_WRITE || (state->err != Z_OK && !state->again))
|
||||
return -1;
|
||||
gz_error(state, Z_OK, NULL);
|
||||
|
||||
/* write string */
|
||||
len = strlen(s);
|
||||
@@ -349,16 +376,47 @@ int ZEXPORT gzputs(gzFile file, const char *s) {
|
||||
return -1;
|
||||
}
|
||||
put = gz_write(state, s, len);
|
||||
return put < len ? -1 : (int)len;
|
||||
return len && put == 0 ? -1 : (int)put;
|
||||
}
|
||||
|
||||
#if (((!defined(STDC) && !defined(Z_HAVE_STDARG_H)) || !defined(NO_vsnprintf)) && \
|
||||
(defined(STDC) || defined(Z_HAVE_STDARG_H) || !defined(NO_snprintf))) || \
|
||||
defined(ZLIB_INSECURE)
|
||||
/* If the second half of the input buffer is occupied, write out the contents.
|
||||
If there is any input remaining due to a non-blocking stall on write, move
|
||||
it to the start of the buffer. Return true if this did not open up the
|
||||
second half of the buffer. state->err should be checked after this to
|
||||
handle a gz_comp() error. */
|
||||
local int gz_vacate(gz_statep state) {
|
||||
z_streamp strm;
|
||||
|
||||
strm = &(state->strm);
|
||||
if (strm->next_in + strm->avail_in <= state->in + state->size)
|
||||
return 0;
|
||||
(void)gz_comp(state, Z_NO_FLUSH);
|
||||
if (strm->avail_in == 0) {
|
||||
strm->next_in = state->in;
|
||||
return 0;
|
||||
}
|
||||
memmove(state->in, strm->next_in, strm->avail_in);
|
||||
strm->next_in = state->in;
|
||||
return strm->avail_in > state->size;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(STDC) || defined(Z_HAVE_STDARG_H)
|
||||
#include <stdarg.h>
|
||||
|
||||
/* -- see zlib.h -- */
|
||||
int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) {
|
||||
int len;
|
||||
unsigned left;
|
||||
#if defined(NO_vsnprintf) && !defined(ZLIB_INSECURE)
|
||||
#warning "vsnprintf() not available -- gzprintf() stub returns Z_STREAM_ERROR"
|
||||
#warning "you can recompile with ZLIB_INSECURE defined to use vsprintf()"
|
||||
/* prevent use of insecure vsprintf(), unless purposefully requested */
|
||||
(void)file, (void)format, (void)va;
|
||||
return Z_STREAM_ERROR;
|
||||
#else
|
||||
int len, ret;
|
||||
char *next;
|
||||
gz_statep state;
|
||||
z_streamp strm;
|
||||
@@ -369,24 +427,34 @@ int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) {
|
||||
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)
|
||||
/* check that we're writing and that there's no (serious) error */
|
||||
if (state->mode != GZ_WRITE || (state->err != Z_OK && !state->again))
|
||||
return Z_STREAM_ERROR;
|
||||
gz_error(state, Z_OK, NULL);
|
||||
|
||||
/* make sure we have some buffer space */
|
||||
if (state->size == 0 && gz_init(state) == -1)
|
||||
return state->err;
|
||||
|
||||
/* check for seek request */
|
||||
if (state->seek) {
|
||||
state->seek = 0;
|
||||
if (gz_zero(state, state->skip) == -1)
|
||||
return state->err;
|
||||
}
|
||||
if (state->skip && gz_zero(state) == -1)
|
||||
return state->err;
|
||||
|
||||
/* do the printf() into the input buffer, put length in len -- the input
|
||||
buffer is double-sized just for this function, so there is guaranteed to
|
||||
be state->size bytes available after the current contents */
|
||||
buffer is double-sized just for this function, so there should be
|
||||
state->size bytes available after the current contents */
|
||||
ret = gz_vacate(state);
|
||||
if (state->err) {
|
||||
if (ret && state->again) {
|
||||
/* There was a non-blocking stall on write, resulting in the part
|
||||
of the second half of the output buffer being occupied. Return
|
||||
a Z_BUF_ERROR to let the application know that this gzprintf()
|
||||
needs to be retried. */
|
||||
gz_error(state, Z_BUF_ERROR, "stalled write on gzprintf");
|
||||
}
|
||||
if (!state->again)
|
||||
return state->err;
|
||||
}
|
||||
if (strm->avail_in == 0)
|
||||
strm->next_in = state->in;
|
||||
next = (char *)(state->in + (strm->next_in - state->in) + strm->avail_in);
|
||||
@@ -412,19 +480,16 @@ int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) {
|
||||
if (len == 0 || (unsigned)len >= state->size || next[state->size - 1] != 0)
|
||||
return 0;
|
||||
|
||||
/* update buffer and position, compress first half if past that */
|
||||
/* update buffer and position */
|
||||
strm->avail_in += (unsigned)len;
|
||||
state->x.pos += len;
|
||||
if (strm->avail_in >= state->size) {
|
||||
left = strm->avail_in - state->size;
|
||||
strm->avail_in = state->size;
|
||||
if (gz_comp(state, Z_NO_FLUSH) == -1)
|
||||
return state->err;
|
||||
memmove(state->in, state->in + state->size, left);
|
||||
strm->next_in = state->in;
|
||||
strm->avail_in = left;
|
||||
}
|
||||
|
||||
/* write out buffer if more than half is occupied */
|
||||
ret = gz_vacate(state);
|
||||
if (state->err && !state->again)
|
||||
return state->err;
|
||||
return len;
|
||||
#endif
|
||||
}
|
||||
|
||||
int ZEXPORTVA gzprintf(gzFile file, const char *format, ...) {
|
||||
@@ -444,6 +509,17 @@ int ZEXPORTVA gzprintf(gzFile file, const char *format, int a1, int a2, int a3,
|
||||
int a4, int a5, int a6, int a7, int a8, int a9, int a10,
|
||||
int a11, int a12, int a13, int a14, int a15, int a16,
|
||||
int a17, int a18, int a19, int a20) {
|
||||
#if defined(NO_snprintf) && !defined(ZLIB_INSECURE)
|
||||
#warning "snprintf() not available -- gzprintf() stub returns Z_STREAM_ERROR"
|
||||
#warning "you can recompile with ZLIB_INSECURE defined to use sprintf()"
|
||||
/* prevent use of insecure sprintf(), unless purposefully requested */
|
||||
(void)file, (void)format, (void)a1, (void)a2, (void)a3, (void)a4, (void)a5,
|
||||
(void)a6, (void)a7, (void)a8, (void)a9, (void)a10, (void)a11, (void)a12,
|
||||
(void)a13, (void)a14, (void)a15, (void)a16, (void)a17, (void)a18,
|
||||
(void)a19, (void)a20;
|
||||
return Z_STREAM_ERROR;
|
||||
#else
|
||||
int ret;
|
||||
unsigned len, left;
|
||||
char *next;
|
||||
gz_statep state;
|
||||
@@ -459,24 +535,34 @@ int ZEXPORTVA gzprintf(gzFile file, const char *format, int a1, int a2, int a3,
|
||||
if (sizeof(int) != sizeof(void *))
|
||||
return Z_STREAM_ERROR;
|
||||
|
||||
/* check that we're writing and that there's no error */
|
||||
if (state->mode != GZ_WRITE || state->err != Z_OK)
|
||||
/* check that we're writing and that there's no (serious) error */
|
||||
if (state->mode != GZ_WRITE || (state->err != Z_OK && !state->again))
|
||||
return Z_STREAM_ERROR;
|
||||
gz_error(state, Z_OK, NULL);
|
||||
|
||||
/* make sure we have some buffer space */
|
||||
if (state->size == 0 && gz_init(state) == -1)
|
||||
return state->error;
|
||||
return state->err;
|
||||
|
||||
/* check for seek request */
|
||||
if (state->seek) {
|
||||
state->seek = 0;
|
||||
if (gz_zero(state, state->skip) == -1)
|
||||
return state->error;
|
||||
}
|
||||
if (state->skip && gz_zero(state) == -1)
|
||||
return state->err;
|
||||
|
||||
/* do the printf() into the input buffer, put length in len -- the input
|
||||
buffer is double-sized just for this function, so there is guaranteed to
|
||||
be state->size bytes available after the current contents */
|
||||
ret = gz_vacate(state);
|
||||
if (state->err) {
|
||||
if (ret && state->again) {
|
||||
/* There was a non-blocking stall on write, resulting in the part
|
||||
of the second half of the output buffer being occupied. Return
|
||||
a Z_BUF_ERROR to let the application know that this gzprintf()
|
||||
needs to be retried. */
|
||||
gz_error(state, Z_BUF_ERROR, "stalled write on gzprintf");
|
||||
}
|
||||
if (!state->again)
|
||||
return state->err;
|
||||
}
|
||||
if (strm->avail_in == 0)
|
||||
strm->next_in = state->in;
|
||||
next = (char *)(strm->next_in + strm->avail_in);
|
||||
@@ -510,16 +596,13 @@ int ZEXPORTVA gzprintf(gzFile file, const char *format, int a1, int a2, int a3,
|
||||
/* update buffer and position, compress first half if past that */
|
||||
strm->avail_in += len;
|
||||
state->x.pos += len;
|
||||
if (strm->avail_in >= state->size) {
|
||||
left = strm->avail_in - state->size;
|
||||
strm->avail_in = state->size;
|
||||
if (gz_comp(state, Z_NO_FLUSH) == -1)
|
||||
return state->err;
|
||||
memmove(state->in, state->in + state->size, left);
|
||||
strm->next_in = state->in;
|
||||
strm->avail_in = left;
|
||||
}
|
||||
|
||||
/* write out buffer if more than half is occupied */
|
||||
ret = gz_vacate(state);
|
||||
if (state->err && !state->again)
|
||||
return state->err;
|
||||
return (int)len;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -533,20 +616,18 @@ int ZEXPORT gzflush(gzFile file, int flush) {
|
||||
return Z_STREAM_ERROR;
|
||||
state = (gz_statep)file;
|
||||
|
||||
/* check that we're writing and that there's no error */
|
||||
if (state->mode != GZ_WRITE || state->err != Z_OK)
|
||||
/* check that we're writing and that there's no (serious) error */
|
||||
if (state->mode != GZ_WRITE || (state->err != Z_OK && !state->again))
|
||||
return Z_STREAM_ERROR;
|
||||
gz_error(state, Z_OK, NULL);
|
||||
|
||||
/* 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 state->err;
|
||||
}
|
||||
if (state->skip && gz_zero(state) == -1)
|
||||
return state->err;
|
||||
|
||||
/* compress remaining data with requested flush */
|
||||
(void)gz_comp(state, flush);
|
||||
@@ -564,20 +645,19 @@ int ZEXPORT gzsetparams(gzFile file, int level, int strategy) {
|
||||
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 || state->direct)
|
||||
/* check that we're compressing and that there's no (serious) error */
|
||||
if (state->mode != GZ_WRITE || (state->err != Z_OK && !state->again) ||
|
||||
state->direct)
|
||||
return Z_STREAM_ERROR;
|
||||
gz_error(state, Z_OK, NULL);
|
||||
|
||||
/* 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 state->err;
|
||||
}
|
||||
if (state->skip && gz_zero(state) == -1)
|
||||
return state->err;
|
||||
|
||||
/* change compression parameters for subsequent input */
|
||||
if (state->size) {
|
||||
@@ -606,11 +686,8 @@ int ZEXPORT gzclose_w(gzFile file) {
|
||||
return Z_STREAM_ERROR;
|
||||
|
||||
/* check for seek request */
|
||||
if (state->seek) {
|
||||
state->seek = 0;
|
||||
if (gz_zero(state, state->skip) == -1)
|
||||
ret = state->err;
|
||||
}
|
||||
if (state->skip && gz_zero(state) == -1)
|
||||
ret = state->err;
|
||||
|
||||
/* flush, free memory, and close file */
|
||||
if (gz_comp(state, Z_FINISH) == -1)
|
||||
|
||||
Reference in New Issue
Block a user