2011-09-10 06:26:49 +00:00
|
|
|
/* gzwrite.c -- zlib functions for writing gzip files
|
2013-03-25 05:46:40 +00:00
|
|
|
* Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler
|
2011-09-10 06:26:49 +00:00
|
|
|
* For conditions of distribution and use, see copyright notice in zlib.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gzguts.h"
|
|
|
|
|
|
|
|
/* Local functions */
|
|
|
|
local int gz_init OF((gz_statep));
|
|
|
|
local int gz_comp OF((gz_statep, int));
|
2011-09-10 06:27:17 +00:00
|
|
|
local int gz_zero OF((gz_statep, z_off64_t));
|
2016-12-05 02:29:43 +00:00
|
|
|
local z_size_t gz_write OF((gz_statep, voidpc, z_size_t));
|
2011-09-10 06:26:49 +00:00
|
|
|
|
|
|
|
/* Initialize state for writing a gzip file. Mark initialization by setting
|
2016-12-03 16:18:56 +00:00
|
|
|
state->size to non-zero. Return -1 on a memory allocation failure, or 0 on
|
|
|
|
success. */
|
2011-09-10 06:26:49 +00:00
|
|
|
local int gz_init(state)
|
|
|
|
gz_statep state;
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
z_streamp strm = &(state->strm);
|
|
|
|
|
2015-07-06 01:14:53 +00:00
|
|
|
/* allocate input buffer (double size for gzprintf) */
|
|
|
|
state->in = (unsigned char *)malloc(state->want << 1);
|
2011-10-02 20:24:43 +00:00
|
|
|
if (state->in == NULL) {
|
2011-09-10 06:26:49 +00:00
|
|
|
gz_error(state, Z_MEM_ERROR, "out of memory");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-10-02 20:24:43 +00:00
|
|
|
/* only need output buffer and deflate state if compressing */
|
|
|
|
if (!state->direct) {
|
|
|
|
/* allocate output buffer */
|
2013-03-24 22:18:02 +00:00
|
|
|
state->out = (unsigned char *)malloc(state->want);
|
2011-10-02 20:24:43 +00:00
|
|
|
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,
|
2011-12-08 18:16:31 +00:00
|
|
|
MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
|
2011-10-02 20:24:43 +00:00
|
|
|
if (ret != Z_OK) {
|
|
|
|
free(state->out);
|
|
|
|
free(state->in);
|
|
|
|
gz_error(state, Z_MEM_ERROR, "out of memory");
|
|
|
|
return -1;
|
|
|
|
}
|
2015-07-29 06:13:53 +00:00
|
|
|
strm->next_in = NULL;
|
2011-09-10 06:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* mark state as initialized */
|
|
|
|
state->size = state->want;
|
|
|
|
|
2011-10-02 20:24:43 +00:00
|
|
|
/* initialize write buffer if compressing */
|
|
|
|
if (!state->direct) {
|
|
|
|
strm->avail_out = state->size;
|
|
|
|
strm->next_out = state->out;
|
|
|
|
state->x.next = strm->next_out;
|
|
|
|
}
|
2011-09-10 06:26:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compress whatever is at avail_in and next_in and write to the output file.
|
2016-12-03 16:18:56 +00:00
|
|
|
Return -1 if there is an error writing to the output file or if gz_init()
|
|
|
|
fails to allocate memory, otherwise 0. flush is assumed to be a valid
|
|
|
|
deflate() flush value. If flush is Z_FINISH, then the deflate() state is
|
|
|
|
reset to start a new gzip stream. If gz->direct is true, then simply write
|
|
|
|
to the output file without compressing, and ignore flush. */
|
2011-09-10 06:26:49 +00:00
|
|
|
local int gz_comp(state, flush)
|
|
|
|
gz_statep state;
|
|
|
|
int flush;
|
|
|
|
{
|
2016-12-31 18:03:09 +00:00
|
|
|
int ret, writ;
|
|
|
|
unsigned have, put, max = ((unsigned)-1 >> 2) + 1;
|
2011-09-10 06:26:49 +00:00
|
|
|
z_streamp strm = &(state->strm);
|
|
|
|
|
|
|
|
/* allocate memory if this is the first time through */
|
|
|
|
if (state->size == 0 && gz_init(state) == -1)
|
|
|
|
return -1;
|
|
|
|
|
2011-10-02 20:24:43 +00:00
|
|
|
/* write directly if requested */
|
|
|
|
if (state->direct) {
|
2016-04-05 10:09:59 +00:00
|
|
|
while (strm->avail_in) {
|
2016-12-31 18:03:09 +00:00
|
|
|
put = strm->avail_in > max ? max : strm->avail_in;
|
|
|
|
writ = write(state->fd, strm->next_in, put);
|
|
|
|
if (writ < 0) {
|
2016-04-05 10:09:59 +00:00
|
|
|
gz_error(state, Z_ERRNO, zstrerror());
|
|
|
|
return -1;
|
|
|
|
}
|
2016-12-31 18:03:09 +00:00
|
|
|
strm->avail_in -= (unsigned)writ;
|
|
|
|
strm->next_in += writ;
|
2011-10-02 20:24:43 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-10 06:26:49 +00:00
|
|
|
/* run deflate() on provided input until it produces no more output */
|
|
|
|
ret = Z_OK;
|
|
|
|
do {
|
|
|
|
/* 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))) {
|
2016-04-05 10:09:59 +00:00
|
|
|
while (strm->next_out > state->x.next) {
|
2016-12-31 18:03:09 +00:00
|
|
|
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);
|
|
|
|
if (writ < 0) {
|
2016-04-05 10:09:59 +00:00
|
|
|
gz_error(state, Z_ERRNO, zstrerror());
|
|
|
|
return -1;
|
|
|
|
}
|
2016-12-31 18:03:09 +00:00
|
|
|
state->x.next += writ;
|
2011-09-10 06:26:49 +00:00
|
|
|
}
|
|
|
|
if (strm->avail_out == 0) {
|
|
|
|
strm->avail_out = state->size;
|
|
|
|
strm->next_out = state->out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compress */
|
|
|
|
have = strm->avail_out;
|
|
|
|
ret = deflate(strm, flush);
|
|
|
|
if (ret == Z_STREAM_ERROR) {
|
|
|
|
gz_error(state, Z_STREAM_ERROR,
|
|
|
|
"internal error: deflate stream corrupt");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
have -= strm->avail_out;
|
|
|
|
} while (have);
|
|
|
|
|
|
|
|
/* if that completed a deflate stream, allow another to start */
|
|
|
|
if (flush == Z_FINISH)
|
|
|
|
deflateReset(strm);
|
|
|
|
|
|
|
|
/* all done, no errors */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-03 16:18:56 +00:00
|
|
|
/* Compress len zeros to output. Return -1 on a write error or memory
|
|
|
|
allocation failure by gz_comp(), or 0 on success. */
|
2011-09-10 06:26:49 +00:00
|
|
|
local int gz_zero(state, len)
|
|
|
|
gz_statep state;
|
2011-09-10 06:27:17 +00:00
|
|
|
z_off64_t len;
|
2011-09-10 06:26:49 +00:00
|
|
|
{
|
|
|
|
int first;
|
|
|
|
unsigned n;
|
|
|
|
z_streamp strm = &(state->strm);
|
|
|
|
|
|
|
|
/* consume whatever's left in the input buffer */
|
|
|
|
if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
|
|
|
|
return -1;
|
|
|
|
|
2011-09-10 06:27:17 +00:00
|
|
|
/* compress len zeros (len guaranteed > 0) */
|
2011-09-10 06:26:49 +00:00
|
|
|
first = 1;
|
|
|
|
while (len) {
|
2011-09-10 06:27:17 +00:00
|
|
|
n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
|
|
|
|
(unsigned)len : state->size;
|
2011-09-10 06:26:49 +00:00
|
|
|
if (first) {
|
|
|
|
memset(state->in, 0, n);
|
|
|
|
first = 0;
|
|
|
|
}
|
|
|
|
strm->avail_in = n;
|
|
|
|
strm->next_in = state->in;
|
2011-09-27 05:50:28 +00:00
|
|
|
state->x.pos += n;
|
2011-09-10 06:26:49 +00:00
|
|
|
if (gz_comp(state, Z_NO_FLUSH) == -1)
|
|
|
|
return -1;
|
|
|
|
len -= n;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-05 02:29:43 +00:00
|
|
|
/* 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. */
|
|
|
|
local z_size_t gz_write(state, buf, len)
|
|
|
|
gz_statep state;
|
2011-09-10 06:26:49 +00:00
|
|
|
voidpc buf;
|
2016-12-05 02:29:43 +00:00
|
|
|
z_size_t len;
|
2011-09-10 06:26:49 +00:00
|
|
|
{
|
2016-12-05 02:29:43 +00:00
|
|
|
z_size_t put = len;
|
2011-09-10 06:26:58 +00:00
|
|
|
|
|
|
|
/* if len is zero, avoid unnecessary operations */
|
|
|
|
if (len == 0)
|
|
|
|
return 0;
|
|
|
|
|
2011-09-10 06:26:49 +00:00
|
|
|
/* allocate memory if this is the first time through */
|
|
|
|
if (state->size == 0 && gz_init(state) == -1)
|
2011-09-10 06:27:08 +00:00
|
|
|
return 0;
|
2011-09-10 06:26:49 +00:00
|
|
|
|
|
|
|
/* check for seek request */
|
|
|
|
if (state->seek) {
|
|
|
|
state->seek = 0;
|
|
|
|
if (gz_zero(state, state->skip) == -1)
|
2011-09-10 06:27:08 +00:00
|
|
|
return 0;
|
2011-09-10 06:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* for small len, copy to input buffer, otherwise compress directly */
|
|
|
|
if (len < state->size) {
|
|
|
|
/* copy to input buffer, compress when full */
|
2011-09-10 06:26:58 +00:00
|
|
|
do {
|
2012-08-13 01:08:52 +00:00
|
|
|
unsigned have, copy;
|
|
|
|
|
2016-12-05 02:29:43 +00:00
|
|
|
if (state->strm.avail_in == 0)
|
|
|
|
state->strm.next_in = state->in;
|
|
|
|
have = (unsigned)((state->strm.next_in + state->strm.avail_in) -
|
|
|
|
state->in);
|
2012-08-13 01:08:52 +00:00
|
|
|
copy = state->size - have;
|
|
|
|
if (copy > len)
|
|
|
|
copy = len;
|
|
|
|
memcpy(state->in + have, buf, copy);
|
2016-12-05 02:29:43 +00:00
|
|
|
state->strm.avail_in += copy;
|
2012-08-13 01:08:52 +00:00
|
|
|
state->x.pos += copy;
|
|
|
|
buf = (const char *)buf + copy;
|
|
|
|
len -= copy;
|
2011-09-10 06:26:49 +00:00
|
|
|
if (len && gz_comp(state, Z_NO_FLUSH) == -1)
|
2011-09-10 06:27:08 +00:00
|
|
|
return 0;
|
2011-09-10 06:26:58 +00:00
|
|
|
} while (len);
|
2011-09-10 06:26:49 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* consume whatever's left in the input buffer */
|
2016-12-05 02:29:43 +00:00
|
|
|
if (state->strm.avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
|
2011-09-10 06:27:08 +00:00
|
|
|
return 0;
|
2011-09-10 06:26:49 +00:00
|
|
|
|
|
|
|
/* directly compress user buffer to file */
|
2016-12-05 02:29:43 +00:00
|
|
|
state->strm.next_in = (z_const Bytef *)buf;
|
|
|
|
do {
|
2016-12-31 06:05:05 +00:00
|
|
|
unsigned n = (unsigned)-1;
|
2016-12-05 02:29:43 +00:00
|
|
|
if (n > len)
|
|
|
|
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;
|
2011-09-10 06:26:49 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 02:29:43 +00:00
|
|
|
/* write len bytes to buf, return the number of full items written */
|
|
|
|
return len ? gz_write(state, buf, len) / size : 0;
|
2011-09-10 06:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -- see zlib.h -- */
|
|
|
|
int ZEXPORT gzputc(file, c)
|
|
|
|
gzFile file;
|
|
|
|
int c;
|
|
|
|
{
|
2012-08-13 01:08:52 +00:00
|
|
|
unsigned have;
|
2011-09-10 06:26:49 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* try writing to input buffer for speed (state->size == 0 if buffer not
|
|
|
|
initialized) */
|
2012-08-24 22:02:28 +00:00
|
|
|
if (state->size) {
|
|
|
|
if (strm->avail_in == 0)
|
|
|
|
strm->next_in = state->in;
|
2013-04-14 01:04:06 +00:00
|
|
|
have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
|
2012-08-24 22:02:28 +00:00
|
|
|
if (have < state->size) {
|
2016-10-12 05:15:50 +00:00
|
|
|
state->in[have] = (unsigned char)c;
|
2012-08-24 22:02:28 +00:00
|
|
|
strm->avail_in++;
|
|
|
|
state->x.pos++;
|
|
|
|
return c & 0xff;
|
|
|
|
}
|
2011-09-10 06:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* no room in buffer or not initialized, use gz_write() */
|
2016-10-12 05:15:50 +00:00
|
|
|
buf[0] = (unsigned char)c;
|
2016-12-05 02:29:43 +00:00
|
|
|
if (gz_write(state, buf, 1) != 1)
|
2011-09-10 06:26:49 +00:00
|
|
|
return -1;
|
2012-01-29 17:12:08 +00:00
|
|
|
return c & 0xff;
|
2011-09-10 06:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -- see zlib.h -- */
|
|
|
|
int ZEXPORT gzputs(file, str)
|
|
|
|
gzFile file;
|
|
|
|
const char *str;
|
|
|
|
{
|
2011-09-10 06:27:08 +00:00
|
|
|
int ret;
|
2016-12-05 02:29:43 +00:00
|
|
|
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;
|
2011-09-10 06:27:08 +00:00
|
|
|
|
2011-09-10 06:26:49 +00:00
|
|
|
/* write string */
|
2016-12-05 02:29:43 +00:00
|
|
|
len = strlen(str);
|
|
|
|
ret = gz_write(state, str, len);
|
2011-09-10 06:27:08 +00:00
|
|
|
return ret == 0 && len != 0 ? -1 : ret;
|
2011-09-10 06:26:49 +00:00
|
|
|
}
|
|
|
|
|
2011-09-11 18:04:49 +00:00
|
|
|
#if defined(STDC) || defined(Z_HAVE_STDARG_H)
|
2011-09-10 06:26:49 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
/* -- see zlib.h -- */
|
2013-03-23 01:32:37 +00:00
|
|
|
int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
|
2011-09-10 06:26:49 +00:00
|
|
|
{
|
2016-10-12 05:15:50 +00:00
|
|
|
int len;
|
|
|
|
unsigned left;
|
2015-07-06 01:14:53 +00:00
|
|
|
char *next;
|
2011-09-10 06:26:49 +00:00
|
|
|
gz_statep state;
|
|
|
|
z_streamp strm;
|
|
|
|
|
|
|
|
/* get internal structure */
|
|
|
|
if (file == NULL)
|
2016-12-03 16:18:56 +00:00
|
|
|
return Z_STREAM_ERROR;
|
2011-09-10 06:26:49 +00:00
|
|
|
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)
|
2016-12-03 16:18:56 +00:00
|
|
|
return Z_STREAM_ERROR;
|
2011-09-10 06:26:49 +00:00
|
|
|
|
|
|
|
/* make sure we have some buffer space */
|
|
|
|
if (state->size == 0 && gz_init(state) == -1)
|
2016-12-03 16:18:56 +00:00
|
|
|
return state->err;
|
2011-09-10 06:26:49 +00:00
|
|
|
|
|
|
|
/* check for seek request */
|
|
|
|
if (state->seek) {
|
|
|
|
state->seek = 0;
|
|
|
|
if (gz_zero(state, state->skip) == -1)
|
2016-12-03 16:18:56 +00:00
|
|
|
return state->err;
|
2011-09-10 06:26:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 01:14:53 +00:00
|
|
|
/* 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 */
|
|
|
|
if (strm->avail_in == 0)
|
|
|
|
strm->next_in = state->in;
|
2016-10-12 05:15:50 +00:00
|
|
|
next = (char *)(state->in + (strm->next_in - state->in) + strm->avail_in);
|
2015-07-06 01:14:53 +00:00
|
|
|
next[state->size - 1] = 0;
|
2011-09-10 06:26:49 +00:00
|
|
|
#ifdef NO_vsnprintf
|
|
|
|
# ifdef HAS_vsprintf_void
|
2015-07-06 01:14:53 +00:00
|
|
|
(void)vsprintf(next, format, va);
|
|
|
|
for (len = 0; len < state->size; len++)
|
|
|
|
if (next[len] == 0) break;
|
2011-09-10 06:26:49 +00:00
|
|
|
# else
|
2015-07-06 01:14:53 +00:00
|
|
|
len = vsprintf(next, format, va);
|
2011-09-10 06:26:49 +00:00
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# ifdef HAS_vsnprintf_void
|
2015-07-06 01:14:53 +00:00
|
|
|
(void)vsnprintf(next, state->size, format, va);
|
|
|
|
len = strlen(next);
|
2011-09-10 06:26:49 +00:00
|
|
|
# else
|
2015-07-06 01:14:53 +00:00
|
|
|
len = vsnprintf(next, state->size, format, va);
|
2011-09-10 06:26:49 +00:00
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* check that printf() results fit in buffer */
|
2016-10-12 05:15:50 +00:00
|
|
|
if (len == 0 || (unsigned)len >= state->size || next[state->size - 1] != 0)
|
2011-09-10 06:26:49 +00:00
|
|
|
return 0;
|
|
|
|
|
2015-07-06 01:14:53 +00:00
|
|
|
/* update buffer and position, compress first half if past that */
|
2016-10-12 05:15:50 +00:00
|
|
|
strm->avail_in += (unsigned)len;
|
2011-09-27 05:50:28 +00:00
|
|
|
state->x.pos += len;
|
2015-07-06 01:14:53 +00:00
|
|
|
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)
|
2016-12-03 16:18:56 +00:00
|
|
|
return state->err;
|
2015-07-06 01:14:53 +00:00
|
|
|
memcpy(state->in, state->in + state->size, left);
|
|
|
|
strm->next_in = state->in;
|
|
|
|
strm->avail_in = left;
|
|
|
|
}
|
2016-10-12 05:15:50 +00:00
|
|
|
return len;
|
2011-09-10 06:26:49 +00:00
|
|
|
}
|
|
|
|
|
2013-03-23 01:32:37 +00:00
|
|
|
int ZEXPORTVA gzprintf(gzFile file, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_start(va, format);
|
|
|
|
ret = gzvprintf(file, format, va);
|
|
|
|
va_end(va);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-09-11 18:04:49 +00:00
|
|
|
#else /* !STDC && !Z_HAVE_STDARG_H */
|
2011-09-10 06:26:49 +00:00
|
|
|
|
|
|
|
/* -- see zlib.h -- */
|
|
|
|
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)
|
|
|
|
gzFile file;
|
|
|
|
const char *format;
|
|
|
|
int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
|
|
|
|
a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
|
|
|
|
{
|
2015-07-06 01:14:53 +00:00
|
|
|
unsigned len, left;
|
|
|
|
char *next;
|
2011-09-10 06:26:49 +00:00
|
|
|
gz_statep state;
|
|
|
|
z_streamp strm;
|
|
|
|
|
|
|
|
/* get internal structure */
|
|
|
|
if (file == NULL)
|
2016-12-03 16:18:56 +00:00
|
|
|
return Z_STREAM_ERROR;
|
2011-09-10 06:26:49 +00:00
|
|
|
state = (gz_statep)file;
|
|
|
|
strm = &(state->strm);
|
|
|
|
|
2011-09-11 18:04:49 +00:00
|
|
|
/* check that can really pass pointer in ints */
|
|
|
|
if (sizeof(int) != sizeof(void *))
|
2016-12-03 16:18:56 +00:00
|
|
|
return Z_STREAM_ERROR;
|
2011-09-11 18:04:49 +00:00
|
|
|
|
2011-09-10 06:26:49 +00:00
|
|
|
/* check that we're writing and that there's no error */
|
|
|
|
if (state->mode != GZ_WRITE || state->err != Z_OK)
|
2016-12-03 16:18:56 +00:00
|
|
|
return Z_STREAM_ERROR;
|
2011-09-10 06:26:49 +00:00
|
|
|
|
|
|
|
/* make sure we have some buffer space */
|
|
|
|
if (state->size == 0 && gz_init(state) == -1)
|
2016-12-03 16:18:56 +00:00
|
|
|
return state->error;
|
2011-09-10 06:26:49 +00:00
|
|
|
|
|
|
|
/* check for seek request */
|
|
|
|
if (state->seek) {
|
|
|
|
state->seek = 0;
|
|
|
|
if (gz_zero(state, state->skip) == -1)
|
2016-12-03 16:18:56 +00:00
|
|
|
return state->error;
|
2011-09-10 06:26:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 01:14:53 +00:00
|
|
|
/* 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 */
|
|
|
|
if (strm->avail_in == 0)
|
|
|
|
strm->next_in = state->in;
|
|
|
|
next = (char *)(strm->next_in + strm->avail_in);
|
|
|
|
next[state->size - 1] = 0;
|
2011-09-10 06:26:49 +00:00
|
|
|
#ifdef NO_snprintf
|
|
|
|
# ifdef HAS_sprintf_void
|
2015-07-06 01:14:53 +00:00
|
|
|
sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12,
|
|
|
|
a13, a14, a15, a16, a17, a18, a19, a20);
|
2011-09-10 06:26:49 +00:00
|
|
|
for (len = 0; len < size; len++)
|
2015-07-06 01:14:53 +00:00
|
|
|
if (next[len] == 0)
|
|
|
|
break;
|
2011-09-10 06:26:49 +00:00
|
|
|
# else
|
2015-07-06 01:14:53 +00:00
|
|
|
len = sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11,
|
|
|
|
a12, a13, a14, a15, a16, a17, a18, a19, a20);
|
2011-09-10 06:26:49 +00:00
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# ifdef HAS_snprintf_void
|
2015-07-06 01:14:53 +00:00
|
|
|
snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8, a9,
|
|
|
|
a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
|
|
|
|
len = strlen(next);
|
2011-09-10 06:26:49 +00:00
|
|
|
# else
|
2015-07-06 01:14:53 +00:00
|
|
|
len = snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8,
|
|
|
|
a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
|
2011-09-10 06:26:49 +00:00
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* check that printf() results fit in buffer */
|
2015-07-06 01:14:53 +00:00
|
|
|
if (len == 0 || len >= state->size || next[state->size - 1] != 0)
|
2011-09-10 06:26:49 +00:00
|
|
|
return 0;
|
|
|
|
|
2015-07-06 01:14:53 +00:00
|
|
|
/* update buffer and position, compress first half if past that */
|
|
|
|
strm->avail_in += len;
|
2011-09-27 05:50:28 +00:00
|
|
|
state->x.pos += len;
|
2015-07-06 01:14:53 +00:00
|
|
|
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)
|
2016-12-03 16:18:56 +00:00
|
|
|
return state->err;
|
2015-07-06 01:14:53 +00:00
|
|
|
memcpy(state->in, state->in + state->size, left);
|
|
|
|
strm->next_in = state->in;
|
|
|
|
strm->avail_in = left;
|
|
|
|
}
|
|
|
|
return (int)len;
|
2011-09-10 06:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* -- see zlib.h -- */
|
|
|
|
int ZEXPORT gzflush(file, flush)
|
|
|
|
gzFile file;
|
|
|
|
int flush;
|
|
|
|
{
|
|
|
|
gz_statep state;
|
|
|
|
|
|
|
|
/* get internal structure */
|
|
|
|
if (file == NULL)
|
2016-12-03 16:18:56 +00:00
|
|
|
return Z_STREAM_ERROR;
|
2011-09-10 06:26:49 +00:00
|
|
|
state = (gz_statep)file;
|
|
|
|
|
|
|
|
/* check that we're writing and that there's no error */
|
2011-09-10 06:27:17 +00:00
|
|
|
if (state->mode != GZ_WRITE || state->err != Z_OK)
|
|
|
|
return Z_STREAM_ERROR;
|
2011-09-10 06:26:49 +00:00
|
|
|
|
|
|
|
/* 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)
|
2016-12-03 16:18:56 +00:00
|
|
|
return state->err;
|
2011-09-10 06:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* compress remaining data with requested flush */
|
2016-11-22 20:02:29 +00:00
|
|
|
(void)gz_comp(state, flush);
|
2011-09-10 06:26:49 +00:00
|
|
|
return state->err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -- see zlib.h -- */
|
|
|
|
int ZEXPORT gzsetparams(file, level, strategy)
|
|
|
|
gzFile file;
|
|
|
|
int level;
|
|
|
|
int strategy;
|
|
|
|
{
|
|
|
|
gz_statep state;
|
|
|
|
z_streamp 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;
|
|
|
|
|
|
|
|
/* 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)
|
2016-12-03 16:18:56 +00:00
|
|
|
return state->err;
|
2011-09-10 06:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* change compression parameters for subsequent input */
|
|
|
|
if (state->size) {
|
|
|
|
/* flush previous input with previous parameters before changing */
|
2016-12-03 16:29:57 +00:00
|
|
|
if (strm->avail_in && gz_comp(state, Z_BLOCK) == -1)
|
2011-09-10 06:26:49 +00:00
|
|
|
return state->err;
|
|
|
|
deflateParams(strm, level, strategy);
|
|
|
|
}
|
|
|
|
state->level = level;
|
|
|
|
state->strategy = strategy;
|
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -- see zlib.h -- */
|
|
|
|
int ZEXPORT gzclose_w(file)
|
|
|
|
gzFile file;
|
|
|
|
{
|
2011-10-01 05:19:12 +00:00
|
|
|
int ret = Z_OK;
|
2011-09-10 06:26:49 +00:00
|
|
|
gz_statep state;
|
|
|
|
|
|
|
|
/* 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 for seek request */
|
|
|
|
if (state->seek) {
|
|
|
|
state->seek = 0;
|
2011-10-01 05:19:12 +00:00
|
|
|
if (gz_zero(state, state->skip) == -1)
|
|
|
|
ret = state->err;
|
2011-09-10 06:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* flush, free memory, and close file */
|
2012-09-30 05:23:47 +00:00
|
|
|
if (gz_comp(state, Z_FINISH) == -1)
|
|
|
|
ret = state->err;
|
2012-10-02 05:42:35 +00:00
|
|
|
if (state->size) {
|
|
|
|
if (!state->direct) {
|
|
|
|
(void)deflateEnd(&(state->strm));
|
|
|
|
free(state->out);
|
|
|
|
}
|
2012-09-30 05:23:47 +00:00
|
|
|
free(state->in);
|
2012-10-02 05:42:35 +00:00
|
|
|
}
|
2011-09-10 06:26:49 +00:00
|
|
|
gz_error(state, Z_OK, NULL);
|
2011-09-10 06:32:36 +00:00
|
|
|
free(state->path);
|
2011-10-01 05:19:12 +00:00
|
|
|
if (close(state->fd) == -1)
|
|
|
|
ret = Z_ERRNO;
|
2011-09-10 06:26:49 +00:00
|
|
|
free(state);
|
2011-10-01 05:19:12 +00:00
|
|
|
return ret;
|
2011-09-10 06:26:49 +00:00
|
|
|
}
|