gzwrite.c (muscle7.61) | : | gzwrite.c (muscle7.62) | ||
---|---|---|---|---|
/* gzwrite.c -- zlib functions for writing gzip files | /* gzwrite.c -- zlib functions for writing gzip files | |||
* Copyright (C) 2004, 2005, 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" | |||
#ifndef _WIN32 | ||||
# include <unistd.h> | ||||
#endif | ||||
/* Local functions */ | /* Local functions */ | |||
local int gz_init OF((gz_statep)); | local int gz_init OF((gz_statep)); | |||
local int gz_comp OF((gz_statep, int)); | local int gz_comp OF((gz_statep, int)); | |||
local int gz_zero OF((gz_statep, z_off64_t)); | local int gz_zero OF((gz_statep, z_off64_t)); | |||
local z_size_t gz_write OF((gz_statep, voidpc, z_size_t)); | ||||
/* Initialize state for writing a gzip file. Mark initialization by setting | /* Initialize state for writing a gzip file. Mark initialization by setting | |||
state->size to non-zero. Return -1 on failure or 0 on success. */ | state->size to non-zero. Return -1 on a memory allocation failure, or 0 on | |||
success. */ | ||||
local int gz_init(state) | local int gz_init(state) | |||
gz_statep state; | gz_statep state; | |||
{ | { | |||
int ret; | int ret; | |||
z_streamp strm = &(state->strm); | z_streamp strm = &(state->strm); | |||
/* allocate input buffer */ | /* allocate input buffer (double size for gzprintf) */ | |||
state->in = (unsigned char *)malloc(state->want); | state->in = (unsigned char *)malloc(state->want << 1); | |||
if (state->in == NULL) { | if (state->in == NULL) { | |||
gz_error(state, Z_MEM_ERROR, "out of memory"); | gz_error(state, Z_MEM_ERROR, "out of memory"); | |||
return -1; | return -1; | |||
} | } | |||
/* only need output buffer and deflate state if compressing */ | /* only need output buffer and deflate state if compressing */ | |||
if (!state->direct) { | if (!state->direct) { | |||
/* allocate output buffer */ | /* allocate output buffer */ | |||
state->out = (unsigned char *)malloc(state->want); | state->out = (unsigned char *)malloc(state->want); | |||
if (state->out == NULL) { | if (state->out == NULL) { | |||
skipping to change at line 54 | skipping to change at line 56 | |||
strm->zfree = Z_NULL; | strm->zfree = Z_NULL; | |||
strm->opaque = Z_NULL; | strm->opaque = Z_NULL; | |||
ret = deflateInit2(strm, state->level, Z_DEFLATED, | ret = deflateInit2(strm, state->level, Z_DEFLATED, | |||
MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy); | MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy); | |||
if (ret != Z_OK) { | if (ret != Z_OK) { | |||
free(state->out); | free(state->out); | |||
free(state->in); | free(state->in); | |||
gz_error(state, Z_MEM_ERROR, "out of memory"); | gz_error(state, Z_MEM_ERROR, "out of memory"); | |||
return -1; | return -1; | |||
} | } | |||
strm->next_in = NULL; | ||||
} | } | |||
/* mark state as initialized */ | /* mark state as initialized */ | |||
state->size = state->want; | state->size = state->want; | |||
/* initialize write buffer if compressing */ | /* initialize write buffer if compressing */ | |||
if (!state->direct) { | if (!state->direct) { | |||
strm->avail_out = state->size; | strm->avail_out = state->size; | |||
strm->next_out = state->out; | strm->next_out = state->out; | |||
state->x.next = strm->next_out; | state->x.next = strm->next_out; | |||
} | } | |||
return 0; | return 0; | |||
} | } | |||
/* Compress whatever is at avail_in and next_in and write to the output file. | /* Compress whatever is at avail_in and next_in and write to the output file. | |||
Return -1 if there is an error writing to the output file, otherwise 0. | Return -1 if there is an error writing to the output file or if gz_init() | |||
flush is assumed to be a valid deflate() flush value. If flush is Z_FINISH, | fails to allocate memory, otherwise 0. flush is assumed to be a valid | |||
then the deflate() state is reset to start a new gzip stream. If gz->direct | deflate() flush value. If flush is Z_FINISH, then the deflate() state is | |||
is true, then simply write to the output file without compressing, and | reset to start a new gzip stream. If gz->direct is true, then simply write | |||
ignore flush. */ | to the output file without compressing, and ignore flush. */ | |||
local int gz_comp(state, flush) | local int gz_comp(state, flush) | |||
gz_statep state; | gz_statep state; | |||
int flush; | int flush; | |||
{ | { | |||
int ret, got; | int ret, writ; | |||
unsigned have; | unsigned have, put, max = ((unsigned)-1 >> 2) + 1; | |||
z_streamp strm = &(state->strm); | z_streamp strm = &(state->strm); | |||
/* allocate memory if this is the first time through */ | /* allocate memory if this is the first time through */ | |||
if (state->size == 0 && gz_init(state) == -1) | if (state->size == 0 && gz_init(state) == -1) | |||
return -1; | return -1; | |||
/* write directly if requested */ | /* write directly if requested */ | |||
if (state->direct) { | if (state->direct) { | |||
got = write(state->fd, strm->next_in, strm->avail_in); | while (strm->avail_in) { | |||
if (got < 0 || (unsigned)got != strm->avail_in) { | put = strm->avail_in > max ? max : strm->avail_in; | |||
#if __STDC_WANT_SECURE_LIB__ | writ = write(state->fd, strm->next_in, put); | |||
char errbuf[255]; | if (writ < 0) { | |||
(void)zstrerror(errbuf, 255); | gz_error(state, Z_ERRNO, zstrerror()); | |||
gz_error(state, Z_ERRNO, errbuf); | return -1; | |||
#else | } | |||
gz_error(state, Z_ERRNO, zstrerror()); | strm->avail_in -= (unsigned)writ; | |||
#endif | strm->next_in += writ; | |||
return -1; | ||||
} | } | |||
strm->avail_in = 0; | ||||
return 0; | return 0; | |||
} | } | |||
/* run deflate() on provided input until it produces no more output */ | /* run deflate() on provided input until it produces no more output */ | |||
ret = Z_OK; | ret = Z_OK; | |||
do { | do { | |||
/* write out current buffer contents if full, or if flushing, but if | /* 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 */ | doing Z_FINISH then don't write until we get to Z_STREAM_END */ | |||
if (strm->avail_out == 0 || (flush != Z_NO_FLUSH && | if (strm->avail_out == 0 || (flush != Z_NO_FLUSH && | |||
(flush != Z_FINISH || ret == Z_STREAM_END))) { | (flush != Z_FINISH || ret == Z_STREAM_END))) { | |||
have = (unsigned)(strm->next_out - state->x.next); | while (strm->next_out > state->x.next) { | |||
if (have && ((got = write(state->fd, state->x.next, have)) < 0 || | put = strm->next_out - state->x.next > (int)max ? max : | |||
(unsigned)got != have)) { | (unsigned)(strm->next_out - state->x.next); | |||
#if __STDC_WANT_SECURE_LIB__ | writ = write(state->fd, state->x.next, put); | |||
char errbuf[255]; | if (writ < 0) { | |||
(void)zstrerror(errbuf, 255); | gz_error(state, Z_ERRNO, zstrerror()); | |||
gz_error(state, Z_ERRNO, errbuf); | return -1; | |||
#else | } | |||
gz_error(state, Z_ERRNO, zstrerror()); | state->x.next += writ; | |||
#endif | ||||
return -1; | ||||
} | } | |||
if (strm->avail_out == 0) { | if (strm->avail_out == 0) { | |||
strm->avail_out = state->size; | strm->avail_out = state->size; | |||
strm->next_out = state->out; | strm->next_out = state->out; | |||
state->x.next = state->out; | ||||
} | } | |||
state->x.next = strm->next_out; | ||||
} | } | |||
/* compress */ | /* compress */ | |||
have = strm->avail_out; | have = strm->avail_out; | |||
ret = deflate(strm, flush); | ret = deflate(strm, flush); | |||
if (ret == Z_STREAM_ERROR) { | if (ret == Z_STREAM_ERROR) { | |||
gz_error(state, Z_STREAM_ERROR, | gz_error(state, Z_STREAM_ERROR, | |||
"internal error: deflate stream corrupt"); | "internal error: deflate stream corrupt"); | |||
return -1; | return -1; | |||
} | } | |||
skipping to change at line 148 | skipping to change at line 147 | |||
} while (have); | } while (have); | |||
/* if that completed a deflate stream, allow another to start */ | /* if that completed a deflate stream, allow another to start */ | |||
if (flush == Z_FINISH) | if (flush == Z_FINISH) | |||
deflateReset(strm); | deflateReset(strm); | |||
/* all done, no errors */ | /* all done, no errors */ | |||
return 0; | return 0; | |||
} | } | |||
/* Compress len zeros to output. Return -1 on error, 0 on success. */ | /* 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(state, len) | local int gz_zero(state, len) | |||
gz_statep state; | gz_statep state; | |||
z_off64_t len; | z_off64_t len; | |||
{ | { | |||
int first; | int first; | |||
unsigned n; | unsigned n; | |||
z_streamp strm = &(state->strm); | z_streamp strm = &(state->strm); | |||
/* consume whatever's left in the input buffer */ | /* consume whatever's left in the input buffer */ | |||
if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) | if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) | |||
skipping to change at line 180 | skipping to change at line 180 | |||
strm->avail_in = n; | strm->avail_in = n; | |||
strm->next_in = state->in; | strm->next_in = state->in; | |||
state->x.pos += n; | state->x.pos += n; | |||
if (gz_comp(state, Z_NO_FLUSH) == -1) | if (gz_comp(state, Z_NO_FLUSH) == -1) | |||
return -1; | return -1; | |||
len -= n; | len -= n; | |||
} | } | |||
return 0; | return 0; | |||
} | } | |||
/* -- see zlib.h -- */ | /* Write len bytes from buf to file. Return the number of bytes written. If | |||
int ZEXPORT gzwrite(file, buf, len) | the returned value is less than len, then there was an error. */ | |||
gzFile file; | local z_size_t gz_write(state, buf, len) | |||
gz_statep state; | ||||
voidpc buf; | voidpc buf; | |||
unsigned len; | z_size_t len; | |||
{ | { | |||
unsigned put = len; | z_size_t put = len; | |||
gz_statep state; | ||||
z_streamp strm; | ||||
/* get internal structure */ | ||||
if (file == NULL) | ||||
return 0; | ||||
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; | ||||
/* 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) { | ||||
gz_error(state, Z_DATA_ERROR, "requested length does not fit in int"); | ||||
return 0; | ||||
} | ||||
/* if len is zero, avoid unnecessary operations */ | /* if len is zero, avoid unnecessary operations */ | |||
if (len == 0) | if (len == 0) | |||
return 0; | return 0; | |||
/* allocate memory if this is the first time through */ | /* allocate memory if this is the first time through */ | |||
if (state->size == 0 && gz_init(state) == -1) | if (state->size == 0 && gz_init(state) == -1) | |||
return 0; | return 0; | |||
/* check for seek request */ | /* check for seek request */ | |||
skipping to change at line 228 | skipping to change at line 210 | |||
if (gz_zero(state, state->skip) == -1) | if (gz_zero(state, state->skip) == -1) | |||
return 0; | return 0; | |||
} | } | |||
/* for small len, copy to input buffer, otherwise compress directly */ | /* for small len, copy to input buffer, otherwise compress directly */ | |||
if (len < state->size) { | if (len < state->size) { | |||
/* copy to input buffer, compress when full */ | /* copy to input buffer, compress when full */ | |||
do { | do { | |||
unsigned have, copy; | unsigned have, copy; | |||
if (strm->avail_in == 0) | if (state->strm.avail_in == 0) | |||
strm->next_in = state->in; | state->strm.next_in = state->in; | |||
have = (unsigned)((strm->next_in + strm->avail_in) - state->in); | have = (unsigned)((state->strm.next_in + state->strm.avail_in) - | |||
state->in); | ||||
copy = state->size - have; | copy = state->size - have; | |||
if (copy > len) | if (copy > len) | |||
copy = len; | copy = len; | |||
memcpy(state->in + have, buf, copy); | memcpy(state->in + have, buf, copy); | |||
strm->avail_in += copy; | state->strm.avail_in += copy; | |||
state->x.pos += copy; | state->x.pos += copy; | |||
buf = (const char *)buf + copy; | buf = (const char *)buf + copy; | |||
len -= copy; | len -= copy; | |||
if (len && gz_comp(state, Z_NO_FLUSH) == -1) | if (len && gz_comp(state, Z_NO_FLUSH) == -1) | |||
return 0; | return 0; | |||
} while (len); | } while (len); | |||
} | } | |||
else { | else { | |||
/* consume whatever's left in the input buffer */ | /* consume whatever's left in the input buffer */ | |||
if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) | if (state->strm.avail_in && gz_comp(state, Z_NO_FLUSH) == -1) | |||
return 0; | return 0; | |||
/* directly compress user buffer to file */ | /* directly compress user buffer to file */ | |||
strm->avail_in = len; | state->strm.next_in = (z_const Bytef *)buf; | |||
strm->next_in = (z_const Bytef *)buf; | do { | |||
state->x.pos += len; | unsigned n = (unsigned)-1; | |||
if (gz_comp(state, Z_NO_FLUSH) == -1) | if (n > len) | |||
return 0; | n = len; | |||
state->strm.avail_in = n; | ||||
state->x.pos += n; | ||||
if (gz_comp(state, Z_NO_FLUSH) == -1) | ||||
return 0; | ||||
len -= n; | ||||
} while (len); | ||||
} | ||||
/* input was all buffered or compressed */ | ||||
return put; | ||||
} | ||||
/* -- see zlib.h -- */ | ||||
int ZEXPORT gzwrite(file, buf, len) | ||||
gzFile file; | ||||
voidpc buf; | ||||
unsigned len; | ||||
{ | ||||
gz_statep state; | ||||
/* get internal structure */ | ||||
if (file == NULL) | ||||
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) | ||||
return 0; | ||||
/* since an int is returned, make sure len fits in one, otherwise return | ||||
with an error (this avoids a flaw in the interface) */ | ||||
if ((int)len < 0) { | ||||
gz_error(state, Z_DATA_ERROR, "requested length does not fit in int"); | ||||
return 0; | ||||
} | ||||
/* write len bytes from buf (the return value will fit in an int) */ | ||||
return (int)gz_write(state, buf, len); | ||||
} | ||||
/* -- see zlib.h -- */ | ||||
z_size_t ZEXPORT gzfwrite(buf, size, nitems, file) | ||||
voidpc buf; | ||||
z_size_t size; | ||||
z_size_t nitems; | ||||
gzFile file; | ||||
{ | ||||
z_size_t len; | ||||
gz_statep state; | ||||
/* get internal structure */ | ||||
if (file == NULL) | ||||
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) | ||||
return 0; | ||||
/* compute bytes to read -- error on overflow */ | ||||
len = nitems * size; | ||||
if (size && len / size != nitems) { | ||||
gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t"); | ||||
return 0; | ||||
} | } | |||
/* input was all buffered or compressed (put will fit in int) */ | /* write len bytes to buf, return the number of full items written */ | |||
return (int)put; | return len ? gz_write(state, buf, len) / size : 0; | |||
} | } | |||
/* -- see zlib.h -- */ | /* -- see zlib.h -- */ | |||
int ZEXPORT gzputc(file, c) | int ZEXPORT gzputc(file, c) | |||
gzFile file; | gzFile file; | |||
int c; | int c; | |||
{ | { | |||
unsigned have; | unsigned have; | |||
unsigned char buf[1]; | unsigned char buf[1]; | |||
gz_statep state; | gz_statep state; | |||
skipping to change at line 294 | skipping to change at line 341 | |||
return -1; | return -1; | |||
} | } | |||
/* try writing to input buffer for speed (state->size == 0 if buffer not | /* try writing to input buffer for speed (state->size == 0 if buffer not | |||
initialized) */ | initialized) */ | |||
if (state->size) { | if (state->size) { | |||
if (strm->avail_in == 0) | if (strm->avail_in == 0) | |||
strm->next_in = state->in; | strm->next_in = state->in; | |||
have = (unsigned)((strm->next_in + strm->avail_in) - state->in); | have = (unsigned)((strm->next_in + strm->avail_in) - state->in); | |||
if (have < state->size) { | if (have < state->size) { | |||
state->in[have] = c; | state->in[have] = (unsigned char)c; | |||
strm->avail_in++; | strm->avail_in++; | |||
state->x.pos++; | state->x.pos++; | |||
return c & 0xff; | return c & 0xff; | |||
} | } | |||
} | } | |||
/* no room in buffer or not initialized, use gz_write() */ | /* no room in buffer or not initialized, use gz_write() */ | |||
buf[0] = c; | buf[0] = (unsigned char)c; | |||
if (gzwrite(file, buf, 1) != 1) | if (gz_write(state, buf, 1) != 1) | |||
return -1; | return -1; | |||
return c & 0xff; | return c & 0xff; | |||
} | } | |||
/* -- see zlib.h -- */ | /* -- see zlib.h -- */ | |||
int ZEXPORT gzputs(file, str) | int ZEXPORT gzputs(file, str) | |||
gzFile file; | gzFile file; | |||
const char *str; | const char *str; | |||
{ | { | |||
int ret; | int ret; | |||
unsigned len; | z_size_t len; | |||
gz_statep state; | ||||
/* 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 -1; | ||||
/* write string */ | /* write string */ | |||
len = (unsigned)strlen(str); | len = strlen(str); | |||
ret = gzwrite(file, str, len); | ret = gz_write(state, str, len); | |||
return ret == 0 && len != 0 ? -1 : ret; | return ret == 0 && len != 0 ? -1 : ret; | |||
} | } | |||
#if defined(STDC) || defined(Z_HAVE_STDARG_H) | #if defined(STDC) || defined(Z_HAVE_STDARG_H) | |||
#include <stdarg.h> | #include <stdarg.h> | |||
/* -- see zlib.h -- */ | /* -- see zlib.h -- */ | |||
int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) | int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) | |||
{ | { | |||
int size, len; | int len; | |||
unsigned left; | ||||
char *next; | ||||
gz_statep state; | gz_statep state; | |||
z_streamp strm; | z_streamp strm; | |||
/* get internal structure */ | /* get internal structure */ | |||
if (file == NULL) | if (file == NULL) | |||
return -1; | return Z_STREAM_ERROR; | |||
state = (gz_statep)file; | state = (gz_statep)file; | |||
strm = &(state->strm); | strm = &(state->strm); | |||
/* check that we're writing and that there's no error */ | /* 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; | return Z_STREAM_ERROR; | |||
/* make sure we have some buffer space */ | /* make sure we have some buffer space */ | |||
if (state->size == 0 && gz_init(state) == -1) | if (state->size == 0 && gz_init(state) == -1) | |||
return 0; | return state->err; | |||
/* check for seek request */ | /* check for seek request */ | |||
if (state->seek) { | if (state->seek) { | |||
state->seek = 0; | state->seek = 0; | |||
if (gz_zero(state, state->skip) == -1) | if (gz_zero(state, state->skip) == -1) | |||
return 0; | return state->err; | |||
} | } | |||
/* consume whatever's left in the input buffer */ | /* do the printf() into the input buffer, put length in len -- the input | |||
if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) | buffer is double-sized just for this function, so there is guaranteed to | |||
return 0; | be state->size bytes available after the current contents */ | |||
if (strm->avail_in == 0) | ||||
/* do the printf() into the input buffer, put length in len */ | strm->next_in = state->in; | |||
size = (int)(state->size); | next = (char *)(state->in + (strm->next_in - state->in) + strm->avail_in); | |||
state->in[size - 1] = 0; | next[state->size - 1] = 0; | |||
#ifdef NO_vsnprintf | #ifdef NO_vsnprintf | |||
# ifdef HAS_vsprintf_void | # ifdef HAS_vsprintf_void | |||
(void)vsprintf((char *)(state->in), format, va); | (void)vsprintf(next, format, va); | |||
for (len = 0; len < size; len++) | for (len = 0; len < state->size; len++) | |||
if (state->in[len] == 0) break; | if (next[len] == 0) break; | |||
# else | # else | |||
len = vsprintf((char *)(state->in), format, va); | len = vsprintf(next, format, va); | |||
# endif | # endif | |||
#else | #else | |||
# ifdef HAS_vsnprintf_void | # ifdef HAS_vsnprintf_void | |||
(void)vsnprintf((char *)(state->in), size, format, va); | (void)vsnprintf(next, state->size, format, va); | |||
len = strlen((char *)(state->in)); | len = strlen(next); | |||
# elif __STDC_WANT_SECURE_LIB__ | ||||
len = vsnprintf_s((char *)(state->in), size, _TRUNCATE, format, va); | ||||
# else | # else | |||
len = vsnprintf((char *)(state->in), size, format, va); | len = vsnprintf(next, state->size, format, va); | |||
# endif | # endif | |||
#endif | #endif | |||
/* check that printf() results fit in buffer */ | /* check that printf() results fit in buffer */ | |||
if (len <= 0 || len >= (int)size || state->in[size - 1] != 0) | if (len == 0 || (unsigned)len >= state->size || next[state->size - 1] != 0) | |||
return 0; | return 0; | |||
/* update buffer and position, defer compression until needed */ | /* update buffer and position, compress first half if past that */ | |||
strm->avail_in = (unsigned)len; | strm->avail_in += (unsigned)len; | |||
strm->next_in = state->in; | ||||
state->x.pos += 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; | ||||
memcpy(state->in, state->in + state->size, left); | ||||
strm->next_in = state->in; | ||||
strm->avail_in = left; | ||||
} | ||||
return len; | return len; | |||
} | } | |||
int ZEXPORTVA gzprintf(gzFile file, const char *format, ...) | int ZEXPORTVA gzprintf(gzFile file, const char *format, ...) | |||
{ | { | |||
va_list va; | va_list va; | |||
int ret; | int ret; | |||
va_start(va, format); | va_start(va, format); | |||
ret = gzvprintf(file, format, va); | ret = gzvprintf(file, format, va); | |||
skipping to change at line 411 | skipping to change at line 476 | |||
#else /* !STDC && !Z_HAVE_STDARG_H */ | #else /* !STDC && !Z_HAVE_STDARG_H */ | |||
/* -- see zlib.h -- */ | /* -- see zlib.h -- */ | |||
int ZEXPORTVA 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) | a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) | |||
gzFile file; | gzFile file; | |||
const char *format; | const char *format; | |||
int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, | int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, | |||
a11, a12, a13, a14, a15, a16, a17, a18, a19, a20; | a11, a12, a13, a14, a15, a16, a17, a18, a19, a20; | |||
{ | { | |||
int size, len; | unsigned len, left; | |||
char *next; | ||||
gz_statep state; | gz_statep state; | |||
z_streamp strm; | z_streamp strm; | |||
/* get internal structure */ | /* get internal structure */ | |||
if (file == NULL) | if (file == NULL) | |||
return -1; | return Z_STREAM_ERROR; | |||
state = (gz_statep)file; | state = (gz_statep)file; | |||
strm = &(state->strm); | strm = &(state->strm); | |||
/* check that can really pass pointer in ints */ | /* check that can really pass pointer in ints */ | |||
if (sizeof(int) != sizeof(void *)) | if (sizeof(int) != sizeof(void *)) | |||
return 0; | return Z_STREAM_ERROR; | |||
/* check that we're writing and that there's no error */ | /* 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; | return Z_STREAM_ERROR; | |||
/* make sure we have some buffer space */ | /* make sure we have some buffer space */ | |||
if (state->size == 0 && gz_init(state) == -1) | if (state->size == 0 && gz_init(state) == -1) | |||
return 0; | return state->error; | |||
/* check for seek request */ | /* check for seek request */ | |||
if (state->seek) { | if (state->seek) { | |||
state->seek = 0; | state->seek = 0; | |||
if (gz_zero(state, state->skip) == -1) | if (gz_zero(state, state->skip) == -1) | |||
return 0; | return state->error; | |||
} | } | |||
/* consume whatever's left in the input buffer */ | /* do the printf() into the input buffer, put length in len -- the input | |||
if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) | buffer is double-sized just for this function, so there is guaranteed to | |||
return 0; | be state->size bytes available after the current contents */ | |||
if (strm->avail_in == 0) | ||||
/* do the printf() into the input buffer, put length in len */ | strm->next_in = state->in; | |||
size = (int)(state->size); | next = (char *)(strm->next_in + strm->avail_in); | |||
state->in[size - 1] = 0; | next[state->size - 1] = 0; | |||
#ifdef NO_snprintf | #ifdef NO_snprintf | |||
# ifdef HAS_sprintf_void | # ifdef HAS_sprintf_void | |||
sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8, | sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, | |||
a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); | a13, a14, a15, a16, a17, a18, a19, a20); | |||
for (len = 0; len < size; len++) | for (len = 0; len < size; len++) | |||
if (state->in[len] == 0) break; | if (next[len] == 0) | |||
break; | ||||
# else | # else | |||
len = sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8, | len = sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, | |||
a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); | a12, a13, a14, a15, a16, a17, a18, a19, a20); | |||
# endif | # endif | |||
#else | #else | |||
# ifdef HAS_snprintf_void | # ifdef HAS_snprintf_void | |||
snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6, a7, a8, | snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, | |||
a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); | a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); | |||
len = strlen((char *)(state->in)); | len = strlen(next); | |||
# else | # else | |||
len = snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6, | len = snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8, | |||
a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, | a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); | |||
a19, a20); | ||||
# endif | # endif | |||
#endif | #endif | |||
/* check that printf() results fit in buffer */ | /* check that printf() results fit in buffer */ | |||
if (len <= 0 || len >= (int)size || state->in[size - 1] != 0) | if (len == 0 || len >= state->size || next[state->size - 1] != 0) | |||
return 0; | return 0; | |||
/* update buffer and position, defer compression until needed */ | /* update buffer and position, compress first half if past that */ | |||
strm->avail_in = (unsigned)len; | strm->avail_in += len; | |||
strm->next_in = state->in; | ||||
state->x.pos += len; | state->x.pos += len; | |||
return 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; | ||||
memcpy(state->in, state->in + state->size, left); | ||||
strm->next_in = state->in; | ||||
strm->avail_in = left; | ||||
} | ||||
return (int)len; | ||||
} | } | |||
#endif | #endif | |||
/* -- see zlib.h -- */ | /* -- see zlib.h -- */ | |||
int ZEXPORT gzflush(file, flush) | int ZEXPORT gzflush(file, flush) | |||
gzFile file; | gzFile file; | |||
int flush; | int flush; | |||
{ | { | |||
gz_statep state; | gz_statep state; | |||
/* get internal structure */ | /* get internal structure */ | |||
if (file == NULL) | if (file == NULL) | |||
return -1; | return Z_STREAM_ERROR; | |||
state = (gz_statep)file; | state = (gz_statep)file; | |||
/* check that we're writing and that there's no error */ | /* 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 Z_STREAM_ERROR; | return Z_STREAM_ERROR; | |||
/* check flush parameter */ | /* check flush parameter */ | |||
if (flush < 0 || flush > Z_FINISH) | if (flush < 0 || flush > Z_FINISH) | |||
return Z_STREAM_ERROR; | return Z_STREAM_ERROR; | |||
/* check for seek request */ | /* check for seek request */ | |||
if (state->seek) { | if (state->seek) { | |||
state->seek = 0; | state->seek = 0; | |||
if (gz_zero(state, state->skip) == -1) | if (gz_zero(state, state->skip) == -1) | |||
return -1; | return state->err; | |||
} | } | |||
/* compress remaining data with requested flush */ | /* compress remaining data with requested flush */ | |||
gz_comp(state, flush); | (void)gz_comp(state, flush); | |||
return state->err; | return state->err; | |||
} | } | |||
/* -- see zlib.h -- */ | /* -- see zlib.h -- */ | |||
int ZEXPORT gzsetparams(file, level, strategy) | int ZEXPORT gzsetparams(file, level, strategy) | |||
gzFile file; | gzFile file; | |||
int level; | int level; | |||
int strategy; | int strategy; | |||
{ | { | |||
gz_statep state; | gz_statep state; | |||
skipping to change at line 541 | skipping to change at line 615 | |||
return Z_STREAM_ERROR; | return Z_STREAM_ERROR; | |||
/* if no change is requested, then do nothing */ | /* if no change is requested, then do nothing */ | |||
if (level == state->level && strategy == state->strategy) | if (level == state->level && strategy == state->strategy) | |||
return Z_OK; | return Z_OK; | |||
/* check for seek request */ | /* check for seek request */ | |||
if (state->seek) { | if (state->seek) { | |||
state->seek = 0; | state->seek = 0; | |||
if (gz_zero(state, state->skip) == -1) | if (gz_zero(state, state->skip) == -1) | |||
return -1; | return state->err; | |||
} | } | |||
/* change compression parameters for subsequent input */ | /* change compression parameters for subsequent input */ | |||
if (state->size) { | if (state->size) { | |||
/* flush previous input with previous parameters before changing */ | /* flush previous input with previous parameters before changing */ | |||
if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1) | if (strm->avail_in && gz_comp(state, Z_BLOCK) == -1) | |||
return state->err; | return state->err; | |||
deflateParams(strm, level, strategy); | deflateParams(strm, level, strategy); | |||
} | } | |||
state->level = level; | state->level = level; | |||
state->strategy = strategy; | state->strategy = strategy; | |||
return Z_OK; | return Z_OK; | |||
} | } | |||
/* -- see zlib.h -- */ | /* -- see zlib.h -- */ | |||
int ZEXPORT gzclose_w(file) | int ZEXPORT gzclose_w(file) | |||
End of changes. 60 change blocks. | ||||
140 lines changed or deleted | 214 lines changed or added |