athena/extern/zlib/gzwrite.c

684 lines
16 KiB
C
Raw Permalink Normal View History

2015-04-14 03:10:10 +00:00
/* gzwrite.c -- zlib functions for writing gzip files
* Copyright (C) 2004, 2005, 2010, 2011, 2012 Mark Adler
* 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));
local int gz_zero OF((gz_statep, z_off64_t));
/* Initialize state for writing a gzip file. Mark initialization by setting
state->size to non-zero. Return -1 on failure or 0 on success. */
#ifdef WIN32
local int gz_init(gz_statep state)
#else
local int gz_init(state)
2015-05-19 03:24:56 +00:00
gz_statep state;
2015-04-14 03:10:10 +00:00
#endif
{
int ret;
z_streamp strm = &(state->strm);
/* allocate input buffer */
state->in = malloc(state->want);
2015-05-19 03:24:56 +00:00
if (state->in == NULL)
{
2015-04-14 03:10:10 +00:00
gz_error(state, Z_MEM_ERROR, "out of memory");
return -1;
}
/* only need output buffer and deflate state if compressing */
2015-05-19 03:24:56 +00:00
if (!state->direct)
{
2015-04-14 03:10:10 +00:00
/* allocate output buffer */
state->out = malloc(state->want);
2015-05-19 03:24:56 +00:00
if (state->out == NULL)
{
2015-04-14 03:10:10 +00:00
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);
2015-05-19 03:24:56 +00:00
if (ret != Z_OK)
{
2015-04-14 03:10:10 +00:00
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;
/* initialize write buffer if compressing */
2015-05-19 03:24:56 +00:00
if (!state->direct)
{
2015-04-14 03:10:10 +00:00
strm->avail_out = state->size;
strm->next_out = state->out;
state->x.next = strm->next_out;
}
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
return 0;
}
/* 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.
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. */
#ifdef WIN32
local int gz_comp(gz_statep state, int flush)
#else
local int gz_comp(state, flush)
2015-05-19 03:24:56 +00:00
gz_statep state;
int flush;
2015-04-14 03:10:10 +00:00
#endif
{
int ret, got;
unsigned have;
z_streamp strm = &(state->strm);
/* allocate memory if this is the first time through */
if (state->size == 0 && gz_init(state) == -1)
return -1;
/* write directly if requested */
2015-05-19 03:24:56 +00:00
if (state->direct)
{
2015-04-14 03:10:10 +00:00
got = write(state->fd, strm->next_in, strm->avail_in);
2015-05-19 03:24:56 +00:00
if (got < 0 || (unsigned)got != strm->avail_in)
{
2015-04-14 03:10:10 +00:00
gz_error(state, Z_ERRNO, zstrerror());
return -1;
}
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
strm->avail_in = 0;
return 0;
}
/* run deflate() on provided input until it produces no more output */
ret = Z_OK;
2015-05-19 03:24:56 +00:00
do
{
2015-04-14 03:10:10 +00:00
/* 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 &&
2015-05-19 03:24:56 +00:00
(flush != Z_FINISH || ret == Z_STREAM_END)))
{
2015-04-14 03:10:10 +00:00
have = (unsigned)(strm->next_out - state->x.next);
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (have && ((got = write(state->fd, state->x.next, have)) < 0 ||
2015-05-19 03:24:56 +00:00
(unsigned)got != have))
{
2015-04-14 03:10:10 +00:00
gz_error(state, Z_ERRNO, zstrerror());
return -1;
}
2015-05-19 03:24:56 +00:00
if (strm->avail_out == 0)
{
2015-04-14 03:10:10 +00:00
strm->avail_out = state->size;
strm->next_out = state->out;
}
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
state->x.next = strm->next_out;
}
/* compress */
have = strm->avail_out;
ret = deflate(strm, flush);
2015-05-19 03:24:56 +00:00
if (ret == Z_STREAM_ERROR)
{
2015-04-14 03:10:10 +00:00
gz_error(state, Z_STREAM_ERROR,
2015-05-19 03:24:56 +00:00
"internal error: deflate stream corrupt");
2015-04-14 03:10:10 +00:00
return -1;
}
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
have -= strm->avail_out;
2015-05-19 03:24:56 +00:00
}
while (have);
2015-04-14 03:10:10 +00:00
/* if that completed a deflate stream, allow another to start */
if (flush == Z_FINISH)
deflateReset(strm);
/* all done, no errors */
return 0;
}
/* Compress len zeros to output. Return -1 on error, 0 on success. */
#ifdef WIN32
local int gz_zero(gz_statep state, z_off64_t len)
#else
local int gz_zero(state, len)
2015-05-19 03:24:56 +00:00
gz_statep state;
z_off64_t len;
2015-04-14 03:10:10 +00:00
#endif
{
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;
/* compress len zeros (len guaranteed > 0) */
first = 1;
2015-05-19 03:24:56 +00:00
while (len)
{
2015-04-14 03:10:10 +00:00
n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
(unsigned)len : state->size;
2015-05-19 03:24:56 +00:00
if (first)
{
2015-04-14 03:10:10 +00:00
memset(state->in, 0, n);
first = 0;
}
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
strm->avail_in = n;
strm->next_in = state->in;
state->x.pos += n;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (gz_comp(state, Z_NO_FLUSH) == -1)
return -1;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
len -= n;
}
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
return 0;
}
/* -- see zlib.h -- */
#ifdef WIN32
int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len)
#else
int ZEXPORT gzwrite(file, buf, len)
2015-05-19 03:24:56 +00:00
gzFile file;
voidpc buf;
unsigned len;
2015-04-14 03:10:10 +00:00
#endif
{
unsigned put = len;
unsigned n;
gz_statep state;
z_streamp strm;
/* get internal structure */
if (file == NULL)
return 0;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +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)
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) */
2015-05-19 03:24:56 +00:00
if ((int)len < 0)
{
2015-04-14 03:10:10 +00:00
gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
return 0;
}
/* if len is zero, avoid unnecessary operations */
if (len == 0)
return 0;
/* allocate memory if this is the first time through */
if (state->size == 0 && gz_init(state) == -1)
return 0;
/* check for seek request */
2015-05-19 03:24:56 +00:00
if (state->seek)
{
2015-04-14 03:10:10 +00:00
state->seek = 0;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (gz_zero(state, state->skip) == -1)
return 0;
}
/* for small len, copy to input buffer, otherwise compress directly */
2015-05-19 03:24:56 +00:00
if (len < state->size)
{
2015-04-14 03:10:10 +00:00
/* copy to input buffer, compress when full */
2015-05-19 03:24:56 +00:00
do
{
2015-04-14 03:10:10 +00:00
if (strm->avail_in == 0)
strm->next_in = state->in;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
n = state->size - strm->avail_in;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (n > len)
n = len;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
memcpy(strm->next_in + strm->avail_in, buf, n);
strm->avail_in += n;
state->x.pos += n;
2015-05-19 03:24:56 +00:00
buf = (char*)buf + n;
2015-04-14 03:10:10 +00:00
len -= n;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (len && gz_comp(state, Z_NO_FLUSH) == -1)
return 0;
2015-05-19 03:24:56 +00:00
}
while (len);
2015-04-14 03:10:10 +00:00
}
2015-05-19 03:24:56 +00:00
else
{
2015-04-14 03:10:10 +00:00
/* 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 = (voidp)buf;
state->x.pos += len;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (gz_comp(state, Z_NO_FLUSH) == -1)
return 0;
}
/* input was all buffered or compressed (put will fit in int) */
return (int)put;
}
/* -- see zlib.h -- */
#ifdef WIN32
int ZEXPORT gzputc(gzFile file, unsigned char c)
#else
int ZEXPORT gzputc(file, c)
2015-05-19 03:24:56 +00:00
gzFile file;
int c;
2015-04-14 03:10:10 +00:00
#endif
{
unsigned char buf[1];
gz_statep state;
z_streamp strm;
/* get internal structure */
if (file == NULL)
return -1;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +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)
return -1;
/* check for seek request */
2015-05-19 03:24:56 +00:00
if (state->seek)
{
2015-04-14 03:10:10 +00:00
state->seek = 0;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (gz_zero(state, state->skip) == -1)
return -1;
}
/* try writing to input buffer for speed (state->size == 0 if buffer not
initialized) */
2015-05-19 03:24:56 +00:00
if (strm->avail_in < state->size)
{
2015-04-14 03:10:10 +00:00
if (strm->avail_in == 0)
strm->next_in = state->in;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
strm->next_in[strm->avail_in++] = c;
state->x.pos++;
return c & 0xff;
}
/* no room in buffer or not initialized, use gz_write() */
buf[0] = c;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (gzwrite(file, buf, 1) != 1)
return -1;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
return c & 0xff;
}
/* -- see zlib.h -- */
#ifdef WIN32
2015-05-19 03:24:56 +00:00
int ZEXPORT gzputs(gzFile file, const char* str)
2015-04-14 03:10:10 +00:00
#else
int ZEXPORT gzputs(file, str)
2015-05-19 03:24:56 +00:00
gzFile file;
const char* str;
2015-04-14 03:10:10 +00:00
#endif
{
int ret;
unsigned len;
/* write string */
len = (unsigned)strlen(str);
ret = gzwrite(file, str, len);
return ret == 0 && len != 0 ? -1 : ret;
}
#if defined(STDC) || defined(Z_HAVE_STDARG_H)
#include <stdarg.h>
/* -- see zlib.h -- */
2015-05-19 03:24:56 +00:00
int ZEXPORTVA gzprintf(gzFile file, const char* format, ...)
2015-04-14 03:10:10 +00:00
{
int size, len;
gz_statep state;
z_streamp strm;
va_list va;
/* get internal structure */
if (file == NULL)
return -1;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +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)
return 0;
/* make sure we have some buffer space */
if (state->size == 0 && gz_init(state) == -1)
return 0;
/* check for seek request */
2015-05-19 03:24:56 +00:00
if (state->seek)
{
2015-04-14 03:10:10 +00:00
state->seek = 0;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
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;
/* do the printf() into the input buffer, put length in len */
size = (int)(state->size);
state->in[size - 1] = 0;
va_start(va, format);
#ifdef NO_vsnprintf
# ifdef HAS_vsprintf_void
2015-05-19 03:24:56 +00:00
(void)vsprintf((char*)(state->in), format, va);
2015-04-14 03:10:10 +00:00
va_end(va);
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
for (len = 0; len < size; len++)
if (state->in[len] == 0) break;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
# else
2015-05-19 03:24:56 +00:00
len = vsprintf((char*)(state->in), format, va);
2015-04-14 03:10:10 +00:00
va_end(va);
# endif
#else
# ifdef HAS_vsnprintf_void
2015-05-19 03:24:56 +00:00
(void)vsnprintf((char*)(state->in), size, format, va);
2015-04-14 03:10:10 +00:00
va_end(va);
2015-05-19 03:24:56 +00:00
len = strlen((char*)(state->in));
2015-04-14 03:10:10 +00:00
# else
2015-05-19 03:24:56 +00:00
len = vsnprintf((char*)(state->in), size, format, va);
2015-04-14 03:10:10 +00:00
va_end(va);
# endif
#endif
/* 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;
}
#else /* !STDC && !Z_HAVE_STDARG_H */
/* -- see zlib.h -- */
2015-05-19 03:24:56 +00:00
int ZEXPORTVA gzprintf(file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
2015-04-14 03:10:10 +00:00
a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
2015-05-19 03:24:56 +00:00
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-04-14 03:10:10 +00:00
{
int size, len;
gz_statep state;
z_streamp strm;
/* get internal structure */
if (file == NULL)
return -1;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
state = (gz_statep)file;
strm = &(state->strm);
/* check that can really pass pointer in ints */
2015-05-19 03:24:56 +00:00
if (sizeof(int) != sizeof(void*))
2015-04-14 03:10:10 +00:00
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;
/* check for seek request */
2015-05-19 03:24:56 +00:00
if (state->seek)
{
2015-04-14 03:10:10 +00:00
state->seek = 0;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
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;
/* do the printf() into the input buffer, put length in len */
size = (int)(state->size);
state->in[size - 1] = 0;
#ifdef NO_snprintf
# ifdef HAS_sprintf_void
2015-05-19 03:24:56 +00:00
sprintf((char*)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
2015-04-14 03:10:10 +00:00
a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
for (len = 0; len < size; len++)
if (state->in[len] == 0) break;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
# else
2015-05-19 03:24:56 +00:00
len = sprintf((char*)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
2015-04-14 03:10:10 +00:00
a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
# endif
#else
# ifdef HAS_snprintf_void
2015-05-19 03:24:56 +00:00
snprintf((char*)(state->in), size, format, a1, a2, a3, a4, a5, a6, a7, a8,
2015-04-14 03:10:10 +00:00
a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
2015-05-19 03:24:56 +00:00
len = strlen((char*)(state->in));
2015-04-14 03:10:10 +00:00
# else
2015-05-19 03:24:56 +00:00
len = snprintf((char*)(state->in), size, format, a1, a2, a3, a4, a5, a6,
2015-04-14 03:10:10 +00:00
a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18,
a19, a20);
# endif
#endif
/* 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;
}
#endif
/* -- see zlib.h -- */
#ifdef WIN32
int ZEXPORT gzflush(gzFile file, int flush)
#else
int ZEXPORT gzflush(file, flush)
2015-05-19 03:24:56 +00:00
gzFile file;
int flush;
2015-04-14 03:10:10 +00:00
#endif
{
gz_statep state;
/* get internal structure */
if (file == NULL)
return -1;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
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 flush parameter */
if (flush < 0 || flush > Z_FINISH)
return Z_STREAM_ERROR;
/* check for seek request */
2015-05-19 03:24:56 +00:00
if (state->seek)
{
2015-04-14 03:10:10 +00:00
state->seek = 0;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (gz_zero(state, state->skip) == -1)
return -1;
}
/* compress remaining data with requested flush */
gz_comp(state, flush);
return state->err;
}
/* -- see zlib.h -- */
#ifdef WIN32
int ZEXPORT gzsetparams(gzFile file, int level, int strategy)
#else
int ZEXPORT gzsetparams(file, level, strategy)
2015-05-19 03:24:56 +00:00
gzFile file;
int level;
int strategy;
2015-04-14 03:10:10 +00:00
#endif
{
gz_statep state;
z_streamp strm;
/* get internal structure */
if (file == NULL)
return Z_STREAM_ERROR;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +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)
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 */
2015-05-19 03:24:56 +00:00
if (state->seek)
{
2015-04-14 03:10:10 +00:00
state->seek = 0;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (gz_zero(state, state->skip) == -1)
return -1;
}
/* change compression parameters for subsequent input */
2015-05-19 03:24:56 +00:00
if (state->size)
{
2015-04-14 03:10:10 +00:00
/* flush previous input with previous parameters before changing */
if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1)
return state->err;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
deflateParams(strm, level, strategy);
}
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
state->level = level;
state->strategy = strategy;
return Z_OK;
}
/* -- see zlib.h -- */
#ifdef WIN32
int ZEXPORT gzclose_w(gzFile file)
#else
int ZEXPORT gzclose_w(file)
2015-05-19 03:24:56 +00:00
gzFile file;
2015-04-14 03:10:10 +00:00
#endif
{
int ret = Z_OK;
gz_statep state;
/* get internal structure */
if (file == NULL)
return Z_STREAM_ERROR;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
state = (gz_statep)file;
/* check that we're writing */
if (state->mode != GZ_WRITE)
return Z_STREAM_ERROR;
/* check for seek request */
2015-05-19 03:24:56 +00:00
if (state->seek)
{
2015-04-14 03:10:10 +00:00
state->seek = 0;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (gz_zero(state, state->skip) == -1)
ret = state->err;
}
/* flush, free memory, and close file */
2015-05-19 03:24:56 +00:00
if (state->size)
{
2015-04-14 03:10:10 +00:00
if (gz_comp(state, Z_FINISH) == -1)
ret = state->err;
2015-05-19 03:24:56 +00:00
if (!state->direct)
{
2015-04-14 03:10:10 +00:00
(void)deflateEnd(&(state->strm));
free(state->out);
}
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
free(state->in);
}
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
gz_error(state, Z_OK, NULL);
free(state->path);
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
if (close(state->fd) == -1)
ret = Z_ERRNO;
2015-05-19 03:24:56 +00:00
2015-04-14 03:10:10 +00:00
free(state);
return ret;
}