Home | History | Annotate | Line # | Download | only in zlib
      1  1.1  christos /* gzwrite.c -- zlib functions for writing gzip files
      2  1.3  christos  * Copyright (C) 2004-2019 Mark Adler
      3  1.1  christos  * For conditions of distribution and use, see copyright notice in zlib.h
      4  1.1  christos  */
      5  1.1  christos 
      6  1.1  christos #include "gzguts.h"
      7  1.1  christos 
      8  1.1  christos /* Initialize state for writing a gzip file.  Mark initialization by setting
      9  1.1  christos    state->size to non-zero.  Return -1 on a memory allocation failure, or 0 on
     10  1.1  christos    success. */
     11  1.4  christos local int gz_init(gz_statep state) {
     12  1.1  christos     int ret;
     13  1.1  christos     z_streamp strm = &(state->strm);
     14  1.1  christos 
     15  1.1  christos     /* allocate input buffer (double size for gzprintf) */
     16  1.1  christos     state->in = (unsigned char *)malloc(state->want << 1);
     17  1.1  christos     if (state->in == NULL) {
     18  1.1  christos         gz_error(state, Z_MEM_ERROR, "out of memory");
     19  1.1  christos         return -1;
     20  1.1  christos     }
     21  1.1  christos 
     22  1.1  christos     /* only need output buffer and deflate state if compressing */
     23  1.1  christos     if (!state->direct) {
     24  1.1  christos         /* allocate output buffer */
     25  1.1  christos         state->out = (unsigned char *)malloc(state->want);
     26  1.1  christos         if (state->out == NULL) {
     27  1.1  christos             free(state->in);
     28  1.1  christos             gz_error(state, Z_MEM_ERROR, "out of memory");
     29  1.1  christos             return -1;
     30  1.1  christos         }
     31  1.1  christos 
     32  1.1  christos         /* allocate deflate memory, set up for gzip compression */
     33  1.1  christos         strm->zalloc = Z_NULL;
     34  1.1  christos         strm->zfree = Z_NULL;
     35  1.1  christos         strm->opaque = Z_NULL;
     36  1.1  christos         ret = deflateInit2(strm, state->level, Z_DEFLATED,
     37  1.1  christos                            MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
     38  1.1  christos         if (ret != Z_OK) {
     39  1.1  christos             free(state->out);
     40  1.1  christos             free(state->in);
     41  1.1  christos             gz_error(state, Z_MEM_ERROR, "out of memory");
     42  1.1  christos             return -1;
     43  1.1  christos         }
     44  1.1  christos         strm->next_in = NULL;
     45  1.1  christos     }
     46  1.1  christos 
     47  1.1  christos     /* mark state as initialized */
     48  1.1  christos     state->size = state->want;
     49  1.1  christos 
     50  1.1  christos     /* initialize write buffer if compressing */
     51  1.1  christos     if (!state->direct) {
     52  1.1  christos         strm->avail_out = state->size;
     53  1.1  christos         strm->next_out = state->out;
     54  1.1  christos         state->x.next = strm->next_out;
     55  1.1  christos     }
     56  1.1  christos     return 0;
     57  1.1  christos }
     58  1.1  christos 
     59  1.1  christos /* Compress whatever is at avail_in and next_in and write to the output file.
     60  1.1  christos    Return -1 if there is an error writing to the output file or if gz_init()
     61  1.1  christos    fails to allocate memory, otherwise 0.  flush is assumed to be a valid
     62  1.1  christos    deflate() flush value.  If flush is Z_FINISH, then the deflate() state is
     63  1.1  christos    reset to start a new gzip stream.  If gz->direct is true, then simply write
     64  1.1  christos    to the output file without compressing, and ignore flush. */
     65  1.4  christos local int gz_comp(gz_statep state, int flush) {
     66  1.2  christos     int ret;
     67  1.2  christos     ssize_t writ;
     68  1.2  christos     size_t put;
     69  1.2  christos     unsigned have, max = ((unsigned)-1 >> 2) + 1;
     70  1.1  christos     z_streamp strm = &(state->strm);
     71  1.1  christos 
     72  1.1  christos     /* allocate memory if this is the first time through */
     73  1.1  christos     if (state->size == 0 && gz_init(state) == -1)
     74  1.1  christos         return -1;
     75  1.1  christos 
     76  1.1  christos     /* write directly if requested */
     77  1.1  christos     if (state->direct) {
     78  1.1  christos         while (strm->avail_in) {
     79  1.1  christos             put = strm->avail_in > max ? max : strm->avail_in;
     80  1.1  christos             writ = write(state->fd, strm->next_in, put);
     81  1.1  christos             if (writ < 0) {
     82  1.1  christos                 gz_error(state, Z_ERRNO, zstrerror());
     83  1.1  christos                 return -1;
     84  1.1  christos             }
     85  1.1  christos             strm->avail_in -= (unsigned)writ;
     86  1.1  christos             strm->next_in += writ;
     87  1.1  christos         }
     88  1.1  christos         return 0;
     89  1.1  christos     }
     90  1.1  christos 
     91  1.3  christos     /* check for a pending reset */
     92  1.3  christos     if (state->reset) {
     93  1.3  christos         /* don't start a new gzip member unless there is data to write */
     94  1.3  christos         if (strm->avail_in == 0)
     95  1.3  christos             return 0;
     96  1.3  christos         deflateReset(strm);
     97  1.3  christos         state->reset = 0;
     98  1.3  christos     }
     99  1.3  christos 
    100  1.1  christos     /* run deflate() on provided input until it produces no more output */
    101  1.1  christos     ret = Z_OK;
    102  1.1  christos     do {
    103  1.1  christos         /* write out current buffer contents if full, or if flushing, but if
    104  1.1  christos            doing Z_FINISH then don't write until we get to Z_STREAM_END */
    105  1.1  christos         if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
    106  1.1  christos             (flush != Z_FINISH || ret == Z_STREAM_END))) {
    107  1.1  christos             while (strm->next_out > state->x.next) {
    108  1.1  christos                 put = strm->next_out - state->x.next > (int)max ? max :
    109  1.1  christos                       (unsigned)(strm->next_out - state->x.next);
    110  1.1  christos                 writ = write(state->fd, state->x.next, put);
    111  1.1  christos                 if (writ < 0) {
    112  1.1  christos                     gz_error(state, Z_ERRNO, zstrerror());
    113  1.1  christos                     return -1;
    114  1.1  christos                 }
    115  1.1  christos                 state->x.next += writ;
    116  1.1  christos             }
    117  1.1  christos             if (strm->avail_out == 0) {
    118  1.1  christos                 strm->avail_out = state->size;
    119  1.1  christos                 strm->next_out = state->out;
    120  1.1  christos                 state->x.next = state->out;
    121  1.1  christos             }
    122  1.1  christos         }
    123  1.1  christos 
    124  1.1  christos         /* compress */
    125  1.1  christos         have = strm->avail_out;
    126  1.1  christos         ret = deflate(strm, flush);
    127  1.1  christos         if (ret == Z_STREAM_ERROR) {
    128  1.1  christos             gz_error(state, Z_STREAM_ERROR,
    129  1.1  christos                       "internal error: deflate stream corrupt");
    130  1.1  christos             return -1;
    131  1.1  christos         }
    132  1.1  christos         have -= strm->avail_out;
    133  1.1  christos     } while (have);
    134  1.1  christos 
    135  1.1  christos     /* if that completed a deflate stream, allow another to start */
    136  1.1  christos     if (flush == Z_FINISH)
    137  1.3  christos         state->reset = 1;
    138  1.1  christos 
    139  1.1  christos     /* all done, no errors */
    140  1.1  christos     return 0;
    141  1.1  christos }
    142  1.1  christos 
    143  1.1  christos /* Compress len zeros to output.  Return -1 on a write error or memory
    144  1.1  christos    allocation failure by gz_comp(), or 0 on success. */
    145  1.4  christos local int gz_zero(gz_statep state, z_off64_t len) {
    146  1.1  christos     int first;
    147  1.1  christos     unsigned n;
    148  1.1  christos     z_streamp strm = &(state->strm);
    149  1.1  christos 
    150  1.1  christos     /* consume whatever's left in the input buffer */
    151  1.1  christos     if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
    152  1.1  christos         return -1;
    153  1.1  christos 
    154  1.1  christos     /* compress len zeros (len guaranteed > 0) */
    155  1.1  christos     first = 1;
    156  1.1  christos     while (len) {
    157  1.1  christos         n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
    158  1.1  christos             (unsigned)len : state->size;
    159  1.1  christos         if (first) {
    160  1.1  christos             memset(state->in, 0, n);
    161  1.1  christos             first = 0;
    162  1.1  christos         }
    163  1.1  christos         strm->avail_in = n;
    164  1.1  christos         strm->next_in = state->in;
    165  1.1  christos         state->x.pos += n;
    166  1.1  christos         if (gz_comp(state, Z_NO_FLUSH) == -1)
    167  1.1  christos             return -1;
    168  1.1  christos         len -= n;
    169  1.1  christos     }
    170  1.1  christos     return 0;
    171  1.1  christos }
    172  1.1  christos 
    173  1.1  christos /* Write len bytes from buf to file.  Return the number of bytes written.  If
    174  1.1  christos    the returned value is less than len, then there was an error. */
    175  1.4  christos local z_size_t gz_write(gz_statep state, voidpc buf, z_size_t len) {
    176  1.1  christos     z_size_t put = len;
    177  1.1  christos 
    178  1.1  christos     /* if len is zero, avoid unnecessary operations */
    179  1.1  christos     if (len == 0)
    180  1.1  christos         return 0;
    181  1.1  christos 
    182  1.1  christos     /* allocate memory if this is the first time through */
    183  1.1  christos     if (state->size == 0 && gz_init(state) == -1)
    184  1.1  christos         return 0;
    185  1.1  christos 
    186  1.1  christos     /* check for seek request */
    187  1.1  christos     if (state->seek) {
    188  1.1  christos         state->seek = 0;
    189  1.1  christos         if (gz_zero(state, state->skip) == -1)
    190  1.1  christos             return 0;
    191  1.1  christos     }
    192  1.1  christos 
    193  1.1  christos     /* for small len, copy to input buffer, otherwise compress directly */
    194  1.1  christos     if (len < state->size) {
    195  1.1  christos         /* copy to input buffer, compress when full */
    196  1.1  christos         do {
    197  1.1  christos             unsigned have, copy;
    198  1.1  christos 
    199  1.1  christos             if (state->strm.avail_in == 0)
    200  1.1  christos                 state->strm.next_in = state->in;
    201  1.1  christos             have = (unsigned)((state->strm.next_in + state->strm.avail_in) -
    202  1.1  christos                               state->in);
    203  1.1  christos             copy = state->size - have;
    204  1.1  christos             if (copy > len)
    205  1.3  christos                 copy = (unsigned)len;
    206  1.1  christos             memcpy(state->in + have, buf, copy);
    207  1.1  christos             state->strm.avail_in += copy;
    208  1.1  christos             state->x.pos += copy;
    209  1.1  christos             buf = (const char *)buf + copy;
    210  1.1  christos             len -= copy;
    211  1.1  christos             if (len && gz_comp(state, Z_NO_FLUSH) == -1)
    212  1.1  christos                 return 0;
    213  1.1  christos         } while (len);
    214  1.1  christos     }
    215  1.1  christos     else {
    216  1.1  christos         /* consume whatever's left in the input buffer */
    217  1.1  christos         if (state->strm.avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
    218  1.1  christos             return 0;
    219  1.1  christos 
    220  1.1  christos         /* directly compress user buffer to file */
    221  1.2  christos         state->strm.next_in = __UNCONST(buf);
    222  1.1  christos         do {
    223  1.1  christos             unsigned n = (unsigned)-1;
    224  1.1  christos             if (n > len)
    225  1.3  christos                 n = (unsigned)len;
    226  1.1  christos             state->strm.avail_in = n;
    227  1.1  christos             state->x.pos += n;
    228  1.1  christos             if (gz_comp(state, Z_NO_FLUSH) == -1)
    229  1.1  christos                 return 0;
    230  1.1  christos             len -= n;
    231  1.1  christos         } while (len);
    232  1.1  christos     }
    233  1.1  christos 
    234  1.1  christos     /* input was all buffered or compressed */
    235  1.1  christos     return put;
    236  1.1  christos }
    237  1.1  christos 
    238  1.1  christos /* -- see zlib.h -- */
    239  1.4  christos int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len) {
    240  1.1  christos     gz_statep state;
    241  1.1  christos 
    242  1.1  christos     /* get internal structure */
    243  1.1  christos     if (file == NULL)
    244  1.1  christos         return 0;
    245  1.1  christos     state = (gz_statep)file;
    246  1.1  christos 
    247  1.1  christos     /* check that we're writing and that there's no error */
    248  1.1  christos     if (state->mode != GZ_WRITE || state->err != Z_OK)
    249  1.1  christos         return 0;
    250  1.1  christos 
    251  1.1  christos     /* since an int is returned, make sure len fits in one, otherwise return
    252  1.1  christos        with an error (this avoids a flaw in the interface) */
    253  1.1  christos     if ((int)len < 0) {
    254  1.1  christos         gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
    255  1.1  christos         return 0;
    256  1.1  christos     }
    257  1.1  christos 
    258  1.1  christos     /* write len bytes from buf (the return value will fit in an int) */
    259  1.1  christos     return (int)gz_write(state, buf, len);
    260  1.1  christos }
    261  1.1  christos 
    262  1.1  christos /* -- see zlib.h -- */
    263  1.4  christos z_size_t ZEXPORT gzfwrite(voidpc buf, z_size_t size, z_size_t nitems,
    264  1.4  christos                           gzFile file) {
    265  1.1  christos     z_size_t len;
    266  1.1  christos     gz_statep state;
    267  1.1  christos 
    268  1.1  christos     /* get internal structure */
    269  1.1  christos     if (file == NULL)
    270  1.1  christos         return 0;
    271  1.1  christos     state = (gz_statep)file;
    272  1.1  christos 
    273  1.1  christos     /* check that we're writing and that there's no error */
    274  1.1  christos     if (state->mode != GZ_WRITE || state->err != Z_OK)
    275  1.1  christos         return 0;
    276  1.1  christos 
    277  1.1  christos     /* compute bytes to read -- error on overflow */
    278  1.1  christos     len = nitems * size;
    279  1.1  christos     if (size && len / size != nitems) {
    280  1.1  christos         gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
    281  1.1  christos         return 0;
    282  1.1  christos     }
    283  1.1  christos 
    284  1.1  christos     /* write len bytes to buf, return the number of full items written */
    285  1.1  christos     return len ? gz_write(state, buf, len) / size : 0;
    286  1.1  christos }
    287  1.1  christos 
    288  1.1  christos /* -- see zlib.h -- */
    289  1.4  christos int ZEXPORT gzputc(gzFile file, int c) {
    290  1.1  christos     unsigned have;
    291  1.1  christos     unsigned char buf[1];
    292  1.1  christos     gz_statep state;
    293  1.1  christos     z_streamp strm;
    294  1.1  christos 
    295  1.1  christos     /* get internal structure */
    296  1.1  christos     if (file == NULL)
    297  1.1  christos         return -1;
    298  1.1  christos     state = (gz_statep)file;
    299  1.1  christos     strm = &(state->strm);
    300  1.1  christos 
    301  1.1  christos     /* check that we're writing and that there's no error */
    302  1.1  christos     if (state->mode != GZ_WRITE || state->err != Z_OK)
    303  1.1  christos         return -1;
    304  1.1  christos 
    305  1.1  christos     /* check for seek request */
    306  1.1  christos     if (state->seek) {
    307  1.1  christos         state->seek = 0;
    308  1.1  christos         if (gz_zero(state, state->skip) == -1)
    309  1.1  christos             return -1;
    310  1.1  christos     }
    311  1.1  christos 
    312  1.1  christos     /* try writing to input buffer for speed (state->size == 0 if buffer not
    313  1.1  christos        initialized) */
    314  1.1  christos     if (state->size) {
    315  1.1  christos         if (strm->avail_in == 0)
    316  1.1  christos             strm->next_in = state->in;
    317  1.1  christos         have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
    318  1.1  christos         if (have < state->size) {
    319  1.1  christos             state->in[have] = (unsigned char)c;
    320  1.1  christos             strm->avail_in++;
    321  1.1  christos             state->x.pos++;
    322  1.1  christos             return c & 0xff;
    323  1.1  christos         }
    324  1.1  christos     }
    325  1.1  christos 
    326  1.1  christos     /* no room in buffer or not initialized, use gz_write() */
    327  1.1  christos     buf[0] = (unsigned char)c;
    328  1.1  christos     if (gz_write(state, buf, 1) != 1)
    329  1.1  christos         return -1;
    330  1.1  christos     return c & 0xff;
    331  1.1  christos }
    332  1.1  christos 
    333  1.1  christos /* -- see zlib.h -- */
    334  1.4  christos int ZEXPORT gzputs(gzFile file, const char *s) {
    335  1.3  christos     z_size_t len, put;
    336  1.1  christos     gz_statep state;
    337  1.1  christos 
    338  1.1  christos     /* get internal structure */
    339  1.1  christos     if (file == NULL)
    340  1.1  christos         return -1;
    341  1.1  christos     state = (gz_statep)file;
    342  1.1  christos 
    343  1.1  christos     /* check that we're writing and that there's no error */
    344  1.1  christos     if (state->mode != GZ_WRITE || state->err != Z_OK)
    345  1.1  christos         return -1;
    346  1.1  christos 
    347  1.1  christos     /* write string */
    348  1.3  christos     len = strlen(s);
    349  1.3  christos     if ((int)len < 0 || (unsigned)len != len) {
    350  1.3  christos         gz_error(state, Z_STREAM_ERROR, "string length does not fit in int");
    351  1.3  christos         return -1;
    352  1.3  christos     }
    353  1.3  christos     put = gz_write(state, s, len);
    354  1.3  christos     return put < len ? -1 : (int)len;
    355  1.1  christos }
    356  1.1  christos 
    357  1.1  christos #if defined(STDC) || defined(Z_HAVE_STDARG_H)
    358  1.1  christos #include <stdarg.h>
    359  1.1  christos 
    360  1.1  christos /* -- see zlib.h -- */
    361  1.4  christos int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) {
    362  1.1  christos     int len;
    363  1.1  christos     unsigned left;
    364  1.1  christos     char *next;
    365  1.1  christos     gz_statep state;
    366  1.1  christos     z_streamp strm;
    367  1.1  christos 
    368  1.1  christos     /* get internal structure */
    369  1.1  christos     if (file == NULL)
    370  1.1  christos         return Z_STREAM_ERROR;
    371  1.1  christos     state = (gz_statep)file;
    372  1.1  christos     strm = &(state->strm);
    373  1.1  christos 
    374  1.1  christos     /* check that we're writing and that there's no error */
    375  1.1  christos     if (state->mode != GZ_WRITE || state->err != Z_OK)
    376  1.1  christos         return Z_STREAM_ERROR;
    377  1.1  christos 
    378  1.1  christos     /* make sure we have some buffer space */
    379  1.1  christos     if (state->size == 0 && gz_init(state) == -1)
    380  1.1  christos         return state->err;
    381  1.1  christos 
    382  1.1  christos     /* check for seek request */
    383  1.1  christos     if (state->seek) {
    384  1.1  christos         state->seek = 0;
    385  1.1  christos         if (gz_zero(state, state->skip) == -1)
    386  1.1  christos             return state->err;
    387  1.1  christos     }
    388  1.1  christos 
    389  1.1  christos     /* do the printf() into the input buffer, put length in len -- the input
    390  1.1  christos        buffer is double-sized just for this function, so there is guaranteed to
    391  1.1  christos        be state->size bytes available after the current contents */
    392  1.1  christos     if (strm->avail_in == 0)
    393  1.1  christos         strm->next_in = state->in;
    394  1.1  christos     next = (char *)(state->in + (strm->next_in - state->in) + strm->avail_in);
    395  1.1  christos     next[state->size - 1] = 0;
    396  1.1  christos #ifdef NO_vsnprintf
    397  1.1  christos #  ifdef HAS_vsprintf_void
    398  1.1  christos     (void)vsprintf(next, format, va);
    399  1.1  christos     for (len = 0; len < state->size; len++)
    400  1.1  christos         if (next[len] == 0) break;
    401  1.1  christos #  else
    402  1.1  christos     len = vsprintf(next, format, va);
    403  1.1  christos #  endif
    404  1.1  christos #else
    405  1.1  christos #  ifdef HAS_vsnprintf_void
    406  1.1  christos     (void)vsnprintf(next, state->size, format, va);
    407  1.1  christos     len = strlen(next);
    408  1.1  christos #  else
    409  1.1  christos     len = vsnprintf(next, state->size, format, va);
    410  1.1  christos #  endif
    411  1.1  christos #endif
    412  1.1  christos 
    413  1.1  christos     /* check that printf() results fit in buffer */
    414  1.1  christos     if (len == 0 || (unsigned)len >= state->size || next[state->size - 1] != 0)
    415  1.1  christos         return 0;
    416  1.1  christos 
    417  1.1  christos     /* update buffer and position, compress first half if past that */
    418  1.1  christos     strm->avail_in += (unsigned)len;
    419  1.1  christos     state->x.pos += len;
    420  1.1  christos     if (strm->avail_in >= state->size) {
    421  1.1  christos         left = strm->avail_in - state->size;
    422  1.1  christos         strm->avail_in = state->size;
    423  1.1  christos         if (gz_comp(state, Z_NO_FLUSH) == -1)
    424  1.1  christos             return state->err;
    425  1.3  christos         memmove(state->in, state->in + state->size, left);
    426  1.1  christos         strm->next_in = state->in;
    427  1.1  christos         strm->avail_in = left;
    428  1.1  christos     }
    429  1.1  christos     return len;
    430  1.1  christos }
    431  1.1  christos 
    432  1.4  christos int ZEXPORTVA gzprintf(gzFile file, const char *format, ...) {
    433  1.1  christos     va_list va;
    434  1.1  christos     int ret;
    435  1.1  christos 
    436  1.1  christos     va_start(va, format);
    437  1.1  christos     ret = gzvprintf(file, format, va);
    438  1.1  christos     va_end(va);
    439  1.1  christos     return ret;
    440  1.1  christos }
    441  1.1  christos 
    442  1.1  christos #else /* !STDC && !Z_HAVE_STDARG_H */
    443  1.1  christos 
    444  1.1  christos /* -- see zlib.h -- */
    445  1.4  christos int ZEXPORTVA gzprintf(gzFile file, const char *format, int a1, int a2, int a3,
    446  1.4  christos                        int a4, int a5, int a6, int a7, int a8, int a9, int a10,
    447  1.4  christos                        int a11, int a12, int a13, int a14, int a15, int a16,
    448  1.4  christos                        int a17, int a18, int a19, int a20) {
    449  1.1  christos     unsigned len, left;
    450  1.1  christos     char *next;
    451  1.1  christos     gz_statep state;
    452  1.1  christos     z_streamp strm;
    453  1.1  christos 
    454  1.1  christos     /* get internal structure */
    455  1.1  christos     if (file == NULL)
    456  1.1  christos         return Z_STREAM_ERROR;
    457  1.1  christos     state = (gz_statep)file;
    458  1.1  christos     strm = &(state->strm);
    459  1.1  christos 
    460  1.1  christos     /* check that can really pass pointer in ints */
    461  1.1  christos     if (sizeof(int) != sizeof(void *))
    462  1.1  christos         return Z_STREAM_ERROR;
    463  1.1  christos 
    464  1.1  christos     /* check that we're writing and that there's no error */
    465  1.1  christos     if (state->mode != GZ_WRITE || state->err != Z_OK)
    466  1.1  christos         return Z_STREAM_ERROR;
    467  1.1  christos 
    468  1.1  christos     /* make sure we have some buffer space */
    469  1.1  christos     if (state->size == 0 && gz_init(state) == -1)
    470  1.1  christos         return state->error;
    471  1.1  christos 
    472  1.1  christos     /* check for seek request */
    473  1.1  christos     if (state->seek) {
    474  1.1  christos         state->seek = 0;
    475  1.1  christos         if (gz_zero(state, state->skip) == -1)
    476  1.1  christos             return state->error;
    477  1.1  christos     }
    478  1.1  christos 
    479  1.1  christos     /* do the printf() into the input buffer, put length in len -- the input
    480  1.1  christos        buffer is double-sized just for this function, so there is guaranteed to
    481  1.1  christos        be state->size bytes available after the current contents */
    482  1.1  christos     if (strm->avail_in == 0)
    483  1.1  christos         strm->next_in = state->in;
    484  1.1  christos     next = (char *)(strm->next_in + strm->avail_in);
    485  1.1  christos     next[state->size - 1] = 0;
    486  1.1  christos #ifdef NO_snprintf
    487  1.1  christos #  ifdef HAS_sprintf_void
    488  1.1  christos     sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12,
    489  1.1  christos             a13, a14, a15, a16, a17, a18, a19, a20);
    490  1.1  christos     for (len = 0; len < size; len++)
    491  1.1  christos         if (next[len] == 0)
    492  1.1  christos             break;
    493  1.1  christos #  else
    494  1.1  christos     len = sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11,
    495  1.1  christos                   a12, a13, a14, a15, a16, a17, a18, a19, a20);
    496  1.1  christos #  endif
    497  1.1  christos #else
    498  1.1  christos #  ifdef HAS_snprintf_void
    499  1.1  christos     snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8, a9,
    500  1.1  christos              a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
    501  1.1  christos     len = strlen(next);
    502  1.1  christos #  else
    503  1.1  christos     len = snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8,
    504  1.1  christos                    a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
    505  1.1  christos #  endif
    506  1.1  christos #endif
    507  1.1  christos 
    508  1.1  christos     /* check that printf() results fit in buffer */
    509  1.1  christos     if (len == 0 || len >= state->size || next[state->size - 1] != 0)
    510  1.1  christos         return 0;
    511  1.1  christos 
    512  1.1  christos     /* update buffer and position, compress first half if past that */
    513  1.1  christos     strm->avail_in += len;
    514  1.1  christos     state->x.pos += len;
    515  1.1  christos     if (strm->avail_in >= state->size) {
    516  1.1  christos         left = strm->avail_in - state->size;
    517  1.1  christos         strm->avail_in = state->size;
    518  1.1  christos         if (gz_comp(state, Z_NO_FLUSH) == -1)
    519  1.1  christos             return state->err;
    520  1.3  christos         memmove(state->in, state->in + state->size, left);
    521  1.1  christos         strm->next_in = state->in;
    522  1.1  christos         strm->avail_in = left;
    523  1.1  christos     }
    524  1.1  christos     return (int)len;
    525  1.1  christos }
    526  1.1  christos 
    527  1.1  christos #endif
    528  1.1  christos 
    529  1.1  christos /* -- see zlib.h -- */
    530  1.4  christos int ZEXPORT gzflush(gzFile file, int flush) {
    531  1.1  christos     gz_statep state;
    532  1.1  christos 
    533  1.1  christos     /* get internal structure */
    534  1.1  christos     if (file == NULL)
    535  1.1  christos         return Z_STREAM_ERROR;
    536  1.1  christos     state = (gz_statep)file;
    537  1.1  christos 
    538  1.1  christos     /* check that we're writing and that there's no error */
    539  1.1  christos     if (state->mode != GZ_WRITE || state->err != Z_OK)
    540  1.1  christos         return Z_STREAM_ERROR;
    541  1.1  christos 
    542  1.1  christos     /* check flush parameter */
    543  1.1  christos     if (flush < 0 || flush > Z_FINISH)
    544  1.1  christos         return Z_STREAM_ERROR;
    545  1.1  christos 
    546  1.1  christos     /* check for seek request */
    547  1.1  christos     if (state->seek) {
    548  1.1  christos         state->seek = 0;
    549  1.1  christos         if (gz_zero(state, state->skip) == -1)
    550  1.1  christos             return state->err;
    551  1.1  christos     }
    552  1.1  christos 
    553  1.1  christos     /* compress remaining data with requested flush */
    554  1.1  christos     (void)gz_comp(state, flush);
    555  1.1  christos     return state->err;
    556  1.1  christos }
    557  1.1  christos 
    558  1.1  christos /* -- see zlib.h -- */
    559  1.4  christos int ZEXPORT gzsetparams(gzFile file, int level, int strategy) {
    560  1.1  christos     gz_statep state;
    561  1.1  christos     z_streamp strm;
    562  1.1  christos 
    563  1.1  christos     /* get internal structure */
    564  1.1  christos     if (file == NULL)
    565  1.1  christos         return Z_STREAM_ERROR;
    566  1.1  christos     state = (gz_statep)file;
    567  1.1  christos     strm = &(state->strm);
    568  1.1  christos 
    569  1.1  christos     /* check that we're writing and that there's no error */
    570  1.4  christos     if (state->mode != GZ_WRITE || state->err != Z_OK || state->direct)
    571  1.1  christos         return Z_STREAM_ERROR;
    572  1.1  christos 
    573  1.1  christos     /* if no change is requested, then do nothing */
    574  1.1  christos     if (level == state->level && strategy == state->strategy)
    575  1.1  christos         return Z_OK;
    576  1.1  christos 
    577  1.1  christos     /* check for seek request */
    578  1.1  christos     if (state->seek) {
    579  1.1  christos         state->seek = 0;
    580  1.1  christos         if (gz_zero(state, state->skip) == -1)
    581  1.1  christos             return state->err;
    582  1.1  christos     }
    583  1.1  christos 
    584  1.1  christos     /* change compression parameters for subsequent input */
    585  1.1  christos     if (state->size) {
    586  1.1  christos         /* flush previous input with previous parameters before changing */
    587  1.1  christos         if (strm->avail_in && gz_comp(state, Z_BLOCK) == -1)
    588  1.1  christos             return state->err;
    589  1.1  christos         deflateParams(strm, level, strategy);
    590  1.1  christos     }
    591  1.1  christos     state->level = level;
    592  1.1  christos     state->strategy = strategy;
    593  1.1  christos     return Z_OK;
    594  1.1  christos }
    595  1.1  christos 
    596  1.1  christos /* -- see zlib.h -- */
    597  1.4  christos int ZEXPORT gzclose_w(gzFile file) {
    598  1.1  christos     int ret = Z_OK;
    599  1.1  christos     gz_statep state;
    600  1.1  christos 
    601  1.1  christos     /* get internal structure */
    602  1.1  christos     if (file == NULL)
    603  1.1  christos         return Z_STREAM_ERROR;
    604  1.1  christos     state = (gz_statep)file;
    605  1.1  christos 
    606  1.1  christos     /* check that we're writing */
    607  1.1  christos     if (state->mode != GZ_WRITE)
    608  1.1  christos         return Z_STREAM_ERROR;
    609  1.1  christos 
    610  1.1  christos     /* check for seek request */
    611  1.1  christos     if (state->seek) {
    612  1.1  christos         state->seek = 0;
    613  1.1  christos         if (gz_zero(state, state->skip) == -1)
    614  1.1  christos             ret = state->err;
    615  1.1  christos     }
    616  1.1  christos 
    617  1.1  christos     /* flush, free memory, and close file */
    618  1.1  christos     if (gz_comp(state, Z_FINISH) == -1)
    619  1.1  christos         ret = state->err;
    620  1.1  christos     if (state->size) {
    621  1.1  christos         if (!state->direct) {
    622  1.1  christos             (void)deflateEnd(&(state->strm));
    623  1.1  christos             free(state->out);
    624  1.1  christos         }
    625  1.1  christos         free(state->in);
    626  1.1  christos     }
    627  1.1  christos     gz_error(state, Z_OK, NULL);
    628  1.1  christos     free(state->path);
    629  1.1  christos     if (close(state->fd) == -1)
    630  1.1  christos         ret = Z_ERRNO;
    631  1.1  christos     free(state);
    632  1.1  christos     return ret;
    633  1.1  christos }
    634