kusano fc6ab3
/* gzwrite.c -- zlib functions for writing gzip files
kusano fc6ab3
 * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler
kusano fc6ab3
 * For conditions of distribution and use, see copyright notice in zlib.h
kusano fc6ab3
 */
kusano fc6ab3
kusano fc6ab3
#include "gzguts.h"
kusano fc6ab3
kusano fc6ab3
/* Local functions */
kusano fc6ab3
local int gz_init OF((gz_statep));
kusano fc6ab3
local int gz_comp OF((gz_statep, int));
kusano fc6ab3
local int gz_zero OF((gz_statep, z_off64_t));
kusano fc6ab3
kusano fc6ab3
/* Initialize state for writing a gzip file.  Mark initialization by setting
kusano fc6ab3
   state->size to non-zero.  Return -1 on failure or 0 on success. */
kusano fc6ab3
local int gz_init(state)
kusano fc6ab3
    gz_statep state;
kusano fc6ab3
{
kusano fc6ab3
    int ret;
kusano fc6ab3
    z_streamp strm = &(state->strm);
kusano fc6ab3
kusano fc6ab3
    /* allocate input buffer */
kusano fc6ab3
    state->in = (unsigned char *)malloc(state->want);
kusano fc6ab3
    if (state->in == NULL) {
kusano fc6ab3
        gz_error(state, Z_MEM_ERROR, "out of memory");
kusano fc6ab3
        return -1;
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    /* only need output buffer and deflate state if compressing */
kusano fc6ab3
    if (!state->direct) {
kusano fc6ab3
        /* allocate output buffer */
kusano fc6ab3
        state->out = (unsigned char *)malloc(state->want);
kusano fc6ab3
        if (state->out == NULL) {
kusano fc6ab3
            free(state->in);
kusano fc6ab3
            gz_error(state, Z_MEM_ERROR, "out of memory");
kusano fc6ab3
            return -1;
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        /* allocate deflate memory, set up for gzip compression */
kusano fc6ab3
        strm->zalloc = Z_NULL;
kusano fc6ab3
        strm->zfree = Z_NULL;
kusano fc6ab3
        strm->opaque = Z_NULL;
kusano fc6ab3
        ret = deflateInit2(strm, state->level, Z_DEFLATED,
kusano fc6ab3
                           MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
kusano fc6ab3
        if (ret != Z_OK) {
kusano fc6ab3
            free(state->out);
kusano fc6ab3
            free(state->in);
kusano fc6ab3
            gz_error(state, Z_MEM_ERROR, "out of memory");
kusano fc6ab3
            return -1;
kusano fc6ab3
        }
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    /* mark state as initialized */
kusano fc6ab3
    state->size = state->want;
kusano fc6ab3
kusano fc6ab3
    /* initialize write buffer if compressing */
kusano fc6ab3
    if (!state->direct) {
kusano fc6ab3
        strm->avail_out = state->size;
kusano fc6ab3
        strm->next_out = state->out;
kusano fc6ab3
        state->x.next = strm->next_out;
kusano fc6ab3
    }
kusano fc6ab3
    return 0;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
/* Compress whatever is at avail_in and next_in and write to the output file.
kusano fc6ab3
   Return -1 if there is an error writing to the output file, otherwise 0.
kusano fc6ab3
   flush is assumed to be a valid deflate() flush value.  If flush is Z_FINISH,
kusano fc6ab3
   then the deflate() state is reset to start a new gzip stream.  If gz->direct
kusano fc6ab3
   is true, then simply write to the output file without compressing, and
kusano fc6ab3
   ignore flush. */
kusano fc6ab3
local int gz_comp(state, flush)
kusano fc6ab3
    gz_statep state;
kusano fc6ab3
    int flush;
kusano fc6ab3
{
kusano fc6ab3
    int ret, got;
kusano fc6ab3
    unsigned have;
kusano fc6ab3
    z_streamp strm = &(state->strm);
kusano fc6ab3
kusano fc6ab3
    /* allocate memory if this is the first time through */
kusano fc6ab3
    if (state->size == 0 && gz_init(state) == -1)
kusano fc6ab3
        return -1;
kusano fc6ab3
kusano fc6ab3
    /* write directly if requested */
kusano fc6ab3
    if (state->direct) {
kusano fc6ab3
        got = write(state->fd, strm->next_in, strm->avail_in);
kusano fc6ab3
        if (got < 0 || (unsigned)got != strm->avail_in) {
kusano fc6ab3
            gz_error(state, Z_ERRNO, zstrerror());
kusano fc6ab3
            return -1;
kusano fc6ab3
        }
kusano fc6ab3
        strm->avail_in = 0;
kusano fc6ab3
        return 0;
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    /* run deflate() on provided input until it produces no more output */
kusano fc6ab3
    ret = Z_OK;
kusano fc6ab3
    do {
kusano fc6ab3
        /* write out current buffer contents if full, or if flushing, but if
kusano fc6ab3
           doing Z_FINISH then don't write until we get to Z_STREAM_END */
kusano fc6ab3
        if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
kusano fc6ab3
            (flush != Z_FINISH || ret == Z_STREAM_END))) {
kusano fc6ab3
            have = (unsigned)(strm->next_out - state->x.next);
kusano fc6ab3
            if (have && ((got = write(state->fd, state->x.next, have)) < 0 ||
kusano fc6ab3
                         (unsigned)got != have)) {
kusano fc6ab3
                gz_error(state, Z_ERRNO, zstrerror());
kusano fc6ab3
                return -1;
kusano fc6ab3
            }
kusano fc6ab3
            if (strm->avail_out == 0) {
kusano fc6ab3
                strm->avail_out = state->size;
kusano fc6ab3
                strm->next_out = state->out;
kusano fc6ab3
            }
kusano fc6ab3
            state->x.next = strm->next_out;
kusano fc6ab3
        }
kusano fc6ab3
kusano fc6ab3
        /* compress */
kusano fc6ab3
        have = strm->avail_out;
kusano fc6ab3
        ret = deflate(strm, flush);
kusano fc6ab3
        if (ret == Z_STREAM_ERROR) {
kusano fc6ab3
            gz_error(state, Z_STREAM_ERROR,
kusano fc6ab3
                      "internal error: deflate stream corrupt");
kusano fc6ab3
            return -1;
kusano fc6ab3
        }
kusano fc6ab3
        have -= strm->avail_out;
kusano fc6ab3
    } while (have);
kusano fc6ab3
kusano fc6ab3
    /* if that completed a deflate stream, allow another to start */
kusano fc6ab3
    if (flush == Z_FINISH)
kusano fc6ab3
        deflateReset(strm);
kusano fc6ab3
kusano fc6ab3
    /* all done, no errors */
kusano fc6ab3
    return 0;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
/* Compress len zeros to output.  Return -1 on error, 0 on success. */
kusano fc6ab3
local int gz_zero(state, len)
kusano fc6ab3
    gz_statep state;
kusano fc6ab3
    z_off64_t len;
kusano fc6ab3
{
kusano fc6ab3
    int first;
kusano fc6ab3
    unsigned n;
kusano fc6ab3
    z_streamp strm = &(state->strm);
kusano fc6ab3
kusano fc6ab3
    /* consume whatever's left in the input buffer */
kusano fc6ab3
    if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
kusano fc6ab3
        return -1;
kusano fc6ab3
kusano fc6ab3
    /* compress len zeros (len guaranteed > 0) */
kusano fc6ab3
    first = 1;
kusano fc6ab3
    while (len) {
kusano fc6ab3
        n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
kusano fc6ab3
            (unsigned)len : state->size;
kusano fc6ab3
        if (first) {
kusano fc6ab3
            memset(state->in, 0, n);
kusano fc6ab3
            first = 0;
kusano fc6ab3
        }
kusano fc6ab3
        strm->avail_in = n;
kusano fc6ab3
        strm->next_in = state->in;
kusano fc6ab3
        state->x.pos += n;
kusano fc6ab3
        if (gz_comp(state, Z_NO_FLUSH) == -1)
kusano fc6ab3
            return -1;
kusano fc6ab3
        len -= n;
kusano fc6ab3
    }
kusano fc6ab3
    return 0;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
/* -- see zlib.h -- */
kusano fc6ab3
int ZEXPORT gzwrite(file, buf, len)
kusano fc6ab3
    gzFile file;
kusano fc6ab3
    voidpc buf;
kusano fc6ab3
    unsigned len;
kusano fc6ab3
{
kusano fc6ab3
    unsigned put = len;
kusano fc6ab3
    gz_statep state;
kusano fc6ab3
    z_streamp strm;
kusano fc6ab3
kusano fc6ab3
    /* get internal structure */
kusano fc6ab3
    if (file == NULL)
kusano fc6ab3
        return 0;
kusano fc6ab3
    state = (gz_statep)file;
kusano fc6ab3
    strm = &(state->strm);
kusano fc6ab3
kusano fc6ab3
    /* check that we're writing and that there's no error */
kusano fc6ab3
    if (state->mode != GZ_WRITE || state->err != Z_OK)
kusano fc6ab3
        return 0;
kusano fc6ab3
kusano fc6ab3
    /* since an int is returned, make sure len fits in one, otherwise return
kusano fc6ab3
       with an error (this avoids the flaw in the interface) */
kusano fc6ab3
    if ((int)len < 0) {
kusano fc6ab3
        gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
kusano fc6ab3
        return 0;
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    /* if len is zero, avoid unnecessary operations */
kusano fc6ab3
    if (len == 0)
kusano fc6ab3
        return 0;
kusano fc6ab3
kusano fc6ab3
    /* allocate memory if this is the first time through */
kusano fc6ab3
    if (state->size == 0 && gz_init(state) == -1)
kusano fc6ab3
        return 0;
kusano fc6ab3
kusano fc6ab3
    /* check for seek request */
kusano fc6ab3
    if (state->seek) {
kusano fc6ab3
        state->seek = 0;
kusano fc6ab3
        if (gz_zero(state, state->skip) == -1)
kusano fc6ab3
            return 0;
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    /* for small len, copy to input buffer, otherwise compress directly */
kusano fc6ab3
    if (len < state->size) {
kusano fc6ab3
        /* copy to input buffer, compress when full */
kusano fc6ab3
        do {
kusano fc6ab3
            unsigned have, copy;
kusano fc6ab3
kusano fc6ab3
            if (strm->avail_in == 0)
kusano fc6ab3
                strm->next_in = state->in;
kusano fc6ab3
            have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
kusano fc6ab3
            copy = state->size - have;
kusano fc6ab3
            if (copy > len)
kusano fc6ab3
                copy = len;
kusano fc6ab3
            memcpy(state->in + have, buf, copy);
kusano fc6ab3
            strm->avail_in += copy;
kusano fc6ab3
            state->x.pos += copy;
kusano fc6ab3
            buf = (const char *)buf + copy;
kusano fc6ab3
            len -= copy;
kusano fc6ab3
            if (len && gz_comp(state, Z_NO_FLUSH) == -1)
kusano fc6ab3
                return 0;
kusano fc6ab3
        } while (len);
kusano fc6ab3
    }
kusano fc6ab3
    else {
kusano fc6ab3
        /* consume whatever's left in the input buffer */
kusano fc6ab3
        if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
kusano fc6ab3
            return 0;
kusano fc6ab3
kusano fc6ab3
        /* directly compress user buffer to file */
kusano fc6ab3
        strm->avail_in = len;
kusano fc6ab3
        strm->next_in = (z_const Bytef *)buf;
kusano fc6ab3
        state->x.pos += len;
kusano fc6ab3
        if (gz_comp(state, Z_NO_FLUSH) == -1)
kusano fc6ab3
            return 0;
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    /* input was all buffered or compressed (put will fit in int) */
kusano fc6ab3
    return (int)put;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
/* -- see zlib.h -- */
kusano fc6ab3
int ZEXPORT gzputc(file, c)
kusano fc6ab3
    gzFile file;
kusano fc6ab3
    int c;
kusano fc6ab3
{
kusano fc6ab3
    unsigned have;
kusano fc6ab3
    unsigned char buf[1];
kusano fc6ab3
    gz_statep state;
kusano fc6ab3
    z_streamp strm;
kusano fc6ab3
kusano fc6ab3
    /* get internal structure */
kusano fc6ab3
    if (file == NULL)
kusano fc6ab3
        return -1;
kusano fc6ab3
    state = (gz_statep)file;
kusano fc6ab3
    strm = &(state->strm);
kusano fc6ab3
kusano fc6ab3
    /* check that we're writing and that there's no error */
kusano fc6ab3
    if (state->mode != GZ_WRITE || state->err != Z_OK)
kusano fc6ab3
        return -1;
kusano fc6ab3
kusano fc6ab3
    /* check for seek request */
kusano fc6ab3
    if (state->seek) {
kusano fc6ab3
        state->seek = 0;
kusano fc6ab3
        if (gz_zero(state, state->skip) == -1)
kusano fc6ab3
            return -1;
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    /* try writing to input buffer for speed (state->size == 0 if buffer not
kusano fc6ab3
       initialized) */
kusano fc6ab3
    if (state->size) {
kusano fc6ab3
        if (strm->avail_in == 0)
kusano fc6ab3
            strm->next_in = state->in;
kusano fc6ab3
        have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
kusano fc6ab3
        if (have < state->size) {
kusano fc6ab3
            state->in[have] = c;
kusano fc6ab3
            strm->avail_in++;
kusano fc6ab3
            state->x.pos++;
kusano fc6ab3
            return c & 0xff;
kusano fc6ab3
        }
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    /* no room in buffer or not initialized, use gz_write() */
kusano fc6ab3
    buf[0] = c;
kusano fc6ab3
    if (gzwrite(file, buf, 1) != 1)
kusano fc6ab3
        return -1;
kusano fc6ab3
    return c & 0xff;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
/* -- see zlib.h -- */
kusano fc6ab3
int ZEXPORT gzputs(file, str)
kusano fc6ab3
    gzFile file;
kusano fc6ab3
    const char *str;
kusano fc6ab3
{
kusano fc6ab3
    int ret;
kusano fc6ab3
    unsigned len;
kusano fc6ab3
kusano fc6ab3
    /* write string */
kusano fc6ab3
    len = (unsigned)strlen(str);
kusano fc6ab3
    ret = gzwrite(file, str, len);
kusano fc6ab3
    return ret == 0 && len != 0 ? -1 : ret;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
#if defined(STDC) || defined(Z_HAVE_STDARG_H)
kusano fc6ab3
#include <stdarg.h></stdarg.h>
kusano fc6ab3
kusano fc6ab3
/* -- see zlib.h -- */
kusano fc6ab3
int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
kusano fc6ab3
{
kusano fc6ab3
    int size, len;
kusano fc6ab3
    gz_statep state;
kusano fc6ab3
    z_streamp strm;
kusano fc6ab3
kusano fc6ab3
    /* get internal structure */
kusano fc6ab3
    if (file == NULL)
kusano fc6ab3
        return -1;
kusano fc6ab3
    state = (gz_statep)file;
kusano fc6ab3
    strm = &(state->strm);
kusano fc6ab3
kusano fc6ab3
    /* check that we're writing and that there's no error */
kusano fc6ab3
    if (state->mode != GZ_WRITE || state->err != Z_OK)
kusano fc6ab3
        return 0;
kusano fc6ab3
kusano fc6ab3
    /* make sure we have some buffer space */
kusano fc6ab3
    if (state->size == 0 && gz_init(state) == -1)
kusano fc6ab3
        return 0;
kusano fc6ab3
kusano fc6ab3
    /* check for seek request */
kusano fc6ab3
    if (state->seek) {
kusano fc6ab3
        state->seek = 0;
kusano fc6ab3
        if (gz_zero(state, state->skip) == -1)
kusano fc6ab3
            return 0;
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    /* consume whatever's left in the input buffer */
kusano fc6ab3
    if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
kusano fc6ab3
        return 0;
kusano fc6ab3
kusano fc6ab3
    /* do the printf() into the input buffer, put length in len */
kusano fc6ab3
    size = (int)(state->size);
kusano fc6ab3
    state->in[size - 1] = 0;
kusano fc6ab3
#ifdef NO_vsnprintf
kusano fc6ab3
#  ifdef HAS_vsprintf_void
kusano fc6ab3
    (void)vsprintf((char *)(state->in), format, va);
kusano fc6ab3
    for (len = 0; len < size; len++)
kusano fc6ab3
        if (state->in[len] == 0) break;
kusano fc6ab3
#  else
kusano fc6ab3
    len = vsprintf((char *)(state->in), format, va);
kusano fc6ab3
#  endif
kusano fc6ab3
#else
kusano fc6ab3
#  ifdef HAS_vsnprintf_void
kusano fc6ab3
    (void)vsnprintf((char *)(state->in), size, format, va);
kusano fc6ab3
    len = strlen((char *)(state->in));
kusano fc6ab3
#  else
kusano fc6ab3
    len = vsnprintf((char *)(state->in), size, format, va);
kusano fc6ab3
#  endif
kusano fc6ab3
#endif
kusano fc6ab3
kusano fc6ab3
    /* check that printf() results fit in buffer */
kusano fc6ab3
    if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
kusano fc6ab3
        return 0;
kusano fc6ab3
kusano fc6ab3
    /* update buffer and position, defer compression until needed */
kusano fc6ab3
    strm->avail_in = (unsigned)len;
kusano fc6ab3
    strm->next_in = state->in;
kusano fc6ab3
    state->x.pos += len;
kusano fc6ab3
    return len;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
int ZEXPORTVA gzprintf(gzFile file, const char *format, ...)
kusano fc6ab3
{
kusano fc6ab3
    va_list va;
kusano fc6ab3
    int ret;
kusano fc6ab3
kusano fc6ab3
    va_start(va, format);
kusano fc6ab3
    ret = gzvprintf(file, format, va);
kusano fc6ab3
    va_end(va);
kusano fc6ab3
    return ret;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
#else /* !STDC && !Z_HAVE_STDARG_H */
kusano fc6ab3
kusano fc6ab3
/* -- see zlib.h -- */
kusano fc6ab3
int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
kusano fc6ab3
                       a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
kusano fc6ab3
    gzFile file;
kusano fc6ab3
    const char *format;
kusano fc6ab3
    int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
kusano fc6ab3
        a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
kusano fc6ab3
{
kusano fc6ab3
    int size, len;
kusano fc6ab3
    gz_statep state;
kusano fc6ab3
    z_streamp strm;
kusano fc6ab3
kusano fc6ab3
    /* get internal structure */
kusano fc6ab3
    if (file == NULL)
kusano fc6ab3
        return -1;
kusano fc6ab3
    state = (gz_statep)file;
kusano fc6ab3
    strm = &(state->strm);
kusano fc6ab3
kusano fc6ab3
    /* check that can really pass pointer in ints */
kusano fc6ab3
    if (sizeof(int) != sizeof(void *))
kusano fc6ab3
        return 0;
kusano fc6ab3
kusano fc6ab3
    /* check that we're writing and that there's no error */
kusano fc6ab3
    if (state->mode != GZ_WRITE || state->err != Z_OK)
kusano fc6ab3
        return 0;
kusano fc6ab3
kusano fc6ab3
    /* make sure we have some buffer space */
kusano fc6ab3
    if (state->size == 0 && gz_init(state) == -1)
kusano fc6ab3
        return 0;
kusano fc6ab3
kusano fc6ab3
    /* check for seek request */
kusano fc6ab3
    if (state->seek) {
kusano fc6ab3
        state->seek = 0;
kusano fc6ab3
        if (gz_zero(state, state->skip) == -1)
kusano fc6ab3
            return 0;
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    /* consume whatever's left in the input buffer */
kusano fc6ab3
    if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
kusano fc6ab3
        return 0;
kusano fc6ab3
kusano fc6ab3
    /* do the printf() into the input buffer, put length in len */
kusano fc6ab3
    size = (int)(state->size);
kusano fc6ab3
    state->in[size - 1] = 0;
kusano fc6ab3
#ifdef NO_snprintf
kusano fc6ab3
#  ifdef HAS_sprintf_void
kusano fc6ab3
    sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
kusano fc6ab3
            a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
kusano fc6ab3
    for (len = 0; len < size; len++)
kusano fc6ab3
        if (state->in[len] == 0) break;
kusano fc6ab3
#  else
kusano fc6ab3
    len = sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
kusano fc6ab3
                  a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
kusano fc6ab3
#  endif
kusano fc6ab3
#else
kusano fc6ab3
#  ifdef HAS_snprintf_void
kusano fc6ab3
    snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6, a7, a8,
kusano fc6ab3
             a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
kusano fc6ab3
    len = strlen((char *)(state->in));
kusano fc6ab3
#  else
kusano fc6ab3
    len = snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6,
kusano fc6ab3
                   a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18,
kusano fc6ab3
                   a19, a20);
kusano fc6ab3
#  endif
kusano fc6ab3
#endif
kusano fc6ab3
kusano fc6ab3
    /* check that printf() results fit in buffer */
kusano fc6ab3
    if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
kusano fc6ab3
        return 0;
kusano fc6ab3
kusano fc6ab3
    /* update buffer and position, defer compression until needed */
kusano fc6ab3
    strm->avail_in = (unsigned)len;
kusano fc6ab3
    strm->next_in = state->in;
kusano fc6ab3
    state->x.pos += len;
kusano fc6ab3
    return len;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
#endif
kusano fc6ab3
kusano fc6ab3
/* -- see zlib.h -- */
kusano fc6ab3
int ZEXPORT gzflush(file, flush)
kusano fc6ab3
    gzFile file;
kusano fc6ab3
    int flush;
kusano fc6ab3
{
kusano fc6ab3
    gz_statep state;
kusano fc6ab3
kusano fc6ab3
    /* get internal structure */
kusano fc6ab3
    if (file == NULL)
kusano fc6ab3
        return -1;
kusano fc6ab3
    state = (gz_statep)file;
kusano fc6ab3
kusano fc6ab3
    /* check that we're writing and that there's no error */
kusano fc6ab3
    if (state->mode != GZ_WRITE || state->err != Z_OK)
kusano fc6ab3
        return Z_STREAM_ERROR;
kusano fc6ab3
kusano fc6ab3
    /* check flush parameter */
kusano fc6ab3
    if (flush < 0 || flush > Z_FINISH)
kusano fc6ab3
        return Z_STREAM_ERROR;
kusano fc6ab3
kusano fc6ab3
    /* check for seek request */
kusano fc6ab3
    if (state->seek) {
kusano fc6ab3
        state->seek = 0;
kusano fc6ab3
        if (gz_zero(state, state->skip) == -1)
kusano fc6ab3
            return -1;
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    /* compress remaining data with requested flush */
kusano fc6ab3
    gz_comp(state, flush);
kusano fc6ab3
    return state->err;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
/* -- see zlib.h -- */
kusano fc6ab3
int ZEXPORT gzsetparams(file, level, strategy)
kusano fc6ab3
    gzFile file;
kusano fc6ab3
    int level;
kusano fc6ab3
    int strategy;
kusano fc6ab3
{
kusano fc6ab3
    gz_statep state;
kusano fc6ab3
    z_streamp strm;
kusano fc6ab3
kusano fc6ab3
    /* get internal structure */
kusano fc6ab3
    if (file == NULL)
kusano fc6ab3
        return Z_STREAM_ERROR;
kusano fc6ab3
    state = (gz_statep)file;
kusano fc6ab3
    strm = &(state->strm);
kusano fc6ab3
kusano fc6ab3
    /* check that we're writing and that there's no error */
kusano fc6ab3
    if (state->mode != GZ_WRITE || state->err != Z_OK)
kusano fc6ab3
        return Z_STREAM_ERROR;
kusano fc6ab3
kusano fc6ab3
    /* if no change is requested, then do nothing */
kusano fc6ab3
    if (level == state->level && strategy == state->strategy)
kusano fc6ab3
        return Z_OK;
kusano fc6ab3
kusano fc6ab3
    /* check for seek request */
kusano fc6ab3
    if (state->seek) {
kusano fc6ab3
        state->seek = 0;
kusano fc6ab3
        if (gz_zero(state, state->skip) == -1)
kusano fc6ab3
            return -1;
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    /* change compression parameters for subsequent input */
kusano fc6ab3
    if (state->size) {
kusano fc6ab3
        /* flush previous input with previous parameters before changing */
kusano fc6ab3
        if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1)
kusano fc6ab3
            return state->err;
kusano fc6ab3
        deflateParams(strm, level, strategy);
kusano fc6ab3
    }
kusano fc6ab3
    state->level = level;
kusano fc6ab3
    state->strategy = strategy;
kusano fc6ab3
    return Z_OK;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
/* -- see zlib.h -- */
kusano fc6ab3
int ZEXPORT gzclose_w(file)
kusano fc6ab3
    gzFile file;
kusano fc6ab3
{
kusano fc6ab3
    int ret = Z_OK;
kusano fc6ab3
    gz_statep state;
kusano fc6ab3
kusano fc6ab3
    /* get internal structure */
kusano fc6ab3
    if (file == NULL)
kusano fc6ab3
        return Z_STREAM_ERROR;
kusano fc6ab3
    state = (gz_statep)file;
kusano fc6ab3
kusano fc6ab3
    /* check that we're writing */
kusano fc6ab3
    if (state->mode != GZ_WRITE)
kusano fc6ab3
        return Z_STREAM_ERROR;
kusano fc6ab3
kusano fc6ab3
    /* check for seek request */
kusano fc6ab3
    if (state->seek) {
kusano fc6ab3
        state->seek = 0;
kusano fc6ab3
        if (gz_zero(state, state->skip) == -1)
kusano fc6ab3
            ret = state->err;
kusano fc6ab3
    }
kusano fc6ab3
kusano fc6ab3
    /* flush, free memory, and close file */
kusano fc6ab3
    if (gz_comp(state, Z_FINISH) == -1)
kusano fc6ab3
        ret = state->err;
kusano fc6ab3
    if (state->size) {
kusano fc6ab3
        if (!state->direct) {
kusano fc6ab3
            (void)deflateEnd(&(state->strm));
kusano fc6ab3
            free(state->out);
kusano fc6ab3
        }
kusano fc6ab3
        free(state->in);
kusano fc6ab3
    }
kusano fc6ab3
    gz_error(state, Z_OK, NULL);
kusano fc6ab3
    free(state->path);
kusano fc6ab3
    if (close(state->fd) == -1)
kusano fc6ab3
        ret = Z_ERRNO;
kusano fc6ab3
    free(state);
kusano fc6ab3
    return ret;
kusano fc6ab3
}