175fd0b74Schristos /* gzwrite.c -- zlib functions for writing gzip files
2*e992f068Schristos * Copyright (C) 2004-2019 Mark Adler
375fd0b74Schristos * For conditions of distribution and use, see copyright notice in zlib.h
475fd0b74Schristos */
575fd0b74Schristos
675fd0b74Schristos #include "gzguts.h"
775fd0b74Schristos
875fd0b74Schristos /* Local functions */
975fd0b74Schristos local int gz_init OF((gz_statep));
1075fd0b74Schristos local int gz_comp OF((gz_statep, int));
1175fd0b74Schristos local int gz_zero OF((gz_statep, z_off64_t));
12ede78133Schristos local z_size_t gz_write OF((gz_statep, voidpc, z_size_t));
1375fd0b74Schristos
1475fd0b74Schristos /* Initialize state for writing a gzip file. Mark initialization by setting
15ede78133Schristos state->size to non-zero. Return -1 on a memory allocation failure, or 0 on
16ede78133Schristos success. */
gz_init(state)1775fd0b74Schristos local int gz_init(state)
1875fd0b74Schristos gz_statep state;
1975fd0b74Schristos {
2075fd0b74Schristos int ret;
2175fd0b74Schristos z_streamp strm = &(state->strm);
2275fd0b74Schristos
23ede78133Schristos /* allocate input buffer (double size for gzprintf) */
24ede78133Schristos state->in = (unsigned char *)malloc(state->want << 1);
2575fd0b74Schristos if (state->in == NULL) {
2675fd0b74Schristos gz_error(state, Z_MEM_ERROR, "out of memory");
2775fd0b74Schristos return -1;
2875fd0b74Schristos }
2975fd0b74Schristos
3075fd0b74Schristos /* only need output buffer and deflate state if compressing */
3175fd0b74Schristos if (!state->direct) {
3275fd0b74Schristos /* allocate output buffer */
3375fd0b74Schristos state->out = (unsigned char *)malloc(state->want);
3475fd0b74Schristos if (state->out == NULL) {
3575fd0b74Schristos free(state->in);
3675fd0b74Schristos gz_error(state, Z_MEM_ERROR, "out of memory");
3775fd0b74Schristos return -1;
3875fd0b74Schristos }
3975fd0b74Schristos
4075fd0b74Schristos /* allocate deflate memory, set up for gzip compression */
4175fd0b74Schristos strm->zalloc = Z_NULL;
4275fd0b74Schristos strm->zfree = Z_NULL;
4375fd0b74Schristos strm->opaque = Z_NULL;
4475fd0b74Schristos ret = deflateInit2(strm, state->level, Z_DEFLATED,
4575fd0b74Schristos MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
4675fd0b74Schristos if (ret != Z_OK) {
4775fd0b74Schristos free(state->out);
4875fd0b74Schristos free(state->in);
4975fd0b74Schristos gz_error(state, Z_MEM_ERROR, "out of memory");
5075fd0b74Schristos return -1;
5175fd0b74Schristos }
52ede78133Schristos strm->next_in = NULL;
5375fd0b74Schristos }
5475fd0b74Schristos
5575fd0b74Schristos /* mark state as initialized */
5675fd0b74Schristos state->size = state->want;
5775fd0b74Schristos
5875fd0b74Schristos /* initialize write buffer if compressing */
5975fd0b74Schristos if (!state->direct) {
6075fd0b74Schristos strm->avail_out = state->size;
6175fd0b74Schristos strm->next_out = state->out;
6275fd0b74Schristos state->x.next = strm->next_out;
6375fd0b74Schristos }
6475fd0b74Schristos return 0;
6575fd0b74Schristos }
6675fd0b74Schristos
6775fd0b74Schristos /* Compress whatever is at avail_in and next_in and write to the output file.
68ede78133Schristos Return -1 if there is an error writing to the output file or if gz_init()
69ede78133Schristos fails to allocate memory, otherwise 0. flush is assumed to be a valid
70ede78133Schristos deflate() flush value. If flush is Z_FINISH, then the deflate() state is
71ede78133Schristos reset to start a new gzip stream. If gz->direct is true, then simply write
72ede78133Schristos to the output file without compressing, and ignore flush. */
gz_comp(state,flush)7375fd0b74Schristos local int gz_comp(state, flush)
7475fd0b74Schristos gz_statep state;
7575fd0b74Schristos int flush;
7675fd0b74Schristos {
77ede78133Schristos int ret, writ;
78ede78133Schristos unsigned have, put, max = ((unsigned)-1 >> 2) + 1;
7975fd0b74Schristos z_streamp strm = &(state->strm);
8075fd0b74Schristos
8175fd0b74Schristos /* allocate memory if this is the first time through */
8275fd0b74Schristos if (state->size == 0 && gz_init(state) == -1)
8375fd0b74Schristos return -1;
8475fd0b74Schristos
8575fd0b74Schristos /* write directly if requested */
8675fd0b74Schristos if (state->direct) {
87ede78133Schristos while (strm->avail_in) {
88ede78133Schristos put = strm->avail_in > max ? max : strm->avail_in;
89ede78133Schristos writ = write(state->fd, strm->next_in, put);
90ede78133Schristos if (writ < 0) {
9175fd0b74Schristos gz_error(state, Z_ERRNO, zstrerror());
9275fd0b74Schristos return -1;
9375fd0b74Schristos }
94ede78133Schristos strm->avail_in -= (unsigned)writ;
95ede78133Schristos strm->next_in += writ;
96ede78133Schristos }
9775fd0b74Schristos return 0;
9875fd0b74Schristos }
9975fd0b74Schristos
100*e992f068Schristos /* check for a pending reset */
101*e992f068Schristos if (state->reset) {
102*e992f068Schristos /* don't start a new gzip member unless there is data to write */
103*e992f068Schristos if (strm->avail_in == 0)
104*e992f068Schristos return 0;
105*e992f068Schristos deflateReset(strm);
106*e992f068Schristos state->reset = 0;
107*e992f068Schristos }
108*e992f068Schristos
10975fd0b74Schristos /* run deflate() on provided input until it produces no more output */
11075fd0b74Schristos ret = Z_OK;
11175fd0b74Schristos do {
11275fd0b74Schristos /* write out current buffer contents if full, or if flushing, but if
11375fd0b74Schristos doing Z_FINISH then don't write until we get to Z_STREAM_END */
11475fd0b74Schristos if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
11575fd0b74Schristos (flush != Z_FINISH || ret == Z_STREAM_END))) {
116ede78133Schristos while (strm->next_out > state->x.next) {
117ede78133Schristos put = strm->next_out - state->x.next > (int)max ? max :
118ede78133Schristos (unsigned)(strm->next_out - state->x.next);
119ede78133Schristos writ = write(state->fd, state->x.next, put);
120ede78133Schristos if (writ < 0) {
12175fd0b74Schristos gz_error(state, Z_ERRNO, zstrerror());
12275fd0b74Schristos return -1;
12375fd0b74Schristos }
124ede78133Schristos state->x.next += writ;
125ede78133Schristos }
12675fd0b74Schristos if (strm->avail_out == 0) {
12775fd0b74Schristos strm->avail_out = state->size;
12875fd0b74Schristos strm->next_out = state->out;
129ede78133Schristos state->x.next = state->out;
13075fd0b74Schristos }
13175fd0b74Schristos }
13275fd0b74Schristos
13375fd0b74Schristos /* compress */
13475fd0b74Schristos have = strm->avail_out;
13575fd0b74Schristos ret = deflate(strm, flush);
13675fd0b74Schristos if (ret == Z_STREAM_ERROR) {
13775fd0b74Schristos gz_error(state, Z_STREAM_ERROR,
13875fd0b74Schristos "internal error: deflate stream corrupt");
13975fd0b74Schristos return -1;
14075fd0b74Schristos }
14175fd0b74Schristos have -= strm->avail_out;
14275fd0b74Schristos } while (have);
14375fd0b74Schristos
14475fd0b74Schristos /* if that completed a deflate stream, allow another to start */
14575fd0b74Schristos if (flush == Z_FINISH)
146*e992f068Schristos state->reset = 1;
14775fd0b74Schristos
14875fd0b74Schristos /* all done, no errors */
14975fd0b74Schristos return 0;
15075fd0b74Schristos }
15175fd0b74Schristos
152ede78133Schristos /* Compress len zeros to output. Return -1 on a write error or memory
153ede78133Schristos allocation failure by gz_comp(), or 0 on success. */
gz_zero(state,len)15475fd0b74Schristos local int gz_zero(state, len)
15575fd0b74Schristos gz_statep state;
15675fd0b74Schristos z_off64_t len;
15775fd0b74Schristos {
15875fd0b74Schristos int first;
15975fd0b74Schristos unsigned n;
16075fd0b74Schristos z_streamp strm = &(state->strm);
16175fd0b74Schristos
16275fd0b74Schristos /* consume whatever's left in the input buffer */
16375fd0b74Schristos if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
16475fd0b74Schristos return -1;
16575fd0b74Schristos
16675fd0b74Schristos /* compress len zeros (len guaranteed > 0) */
16775fd0b74Schristos first = 1;
16875fd0b74Schristos while (len) {
16975fd0b74Schristos n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
17075fd0b74Schristos (unsigned)len : state->size;
17175fd0b74Schristos if (first) {
17275fd0b74Schristos memset(state->in, 0, n);
17375fd0b74Schristos first = 0;
17475fd0b74Schristos }
17575fd0b74Schristos strm->avail_in = n;
17675fd0b74Schristos strm->next_in = state->in;
17775fd0b74Schristos state->x.pos += n;
17875fd0b74Schristos if (gz_comp(state, Z_NO_FLUSH) == -1)
17975fd0b74Schristos return -1;
18075fd0b74Schristos len -= n;
18175fd0b74Schristos }
18275fd0b74Schristos return 0;
18375fd0b74Schristos }
18475fd0b74Schristos
185ede78133Schristos /* Write len bytes from buf to file. Return the number of bytes written. If
186ede78133Schristos the returned value is less than len, then there was an error. */
gz_write(state,buf,len)187ede78133Schristos local z_size_t gz_write(state, buf, len)
18875fd0b74Schristos gz_statep state;
189ede78133Schristos voidpc buf;
190ede78133Schristos z_size_t len;
191ede78133Schristos {
192ede78133Schristos z_size_t put = len;
19375fd0b74Schristos
19475fd0b74Schristos /* if len is zero, avoid unnecessary operations */
19575fd0b74Schristos if (len == 0)
19675fd0b74Schristos return 0;
19775fd0b74Schristos
19875fd0b74Schristos /* allocate memory if this is the first time through */
19975fd0b74Schristos if (state->size == 0 && gz_init(state) == -1)
20075fd0b74Schristos return 0;
20175fd0b74Schristos
20275fd0b74Schristos /* check for seek request */
20375fd0b74Schristos if (state->seek) {
20475fd0b74Schristos state->seek = 0;
20575fd0b74Schristos if (gz_zero(state, state->skip) == -1)
20675fd0b74Schristos return 0;
20775fd0b74Schristos }
20875fd0b74Schristos
20975fd0b74Schristos /* for small len, copy to input buffer, otherwise compress directly */
21075fd0b74Schristos if (len < state->size) {
21175fd0b74Schristos /* copy to input buffer, compress when full */
21275fd0b74Schristos do {
21375fd0b74Schristos unsigned have, copy;
21475fd0b74Schristos
215ede78133Schristos if (state->strm.avail_in == 0)
216ede78133Schristos state->strm.next_in = state->in;
217ede78133Schristos have = (unsigned)((state->strm.next_in + state->strm.avail_in) -
218ede78133Schristos state->in);
21975fd0b74Schristos copy = state->size - have;
22075fd0b74Schristos if (copy > len)
221*e992f068Schristos copy = (unsigned)len;
22275fd0b74Schristos memcpy(state->in + have, buf, copy);
223ede78133Schristos state->strm.avail_in += copy;
22475fd0b74Schristos state->x.pos += copy;
22575fd0b74Schristos buf = (const char *)buf + copy;
22675fd0b74Schristos len -= copy;
22775fd0b74Schristos if (len && gz_comp(state, Z_NO_FLUSH) == -1)
22875fd0b74Schristos return 0;
22975fd0b74Schristos } while (len);
23075fd0b74Schristos }
23175fd0b74Schristos else {
23275fd0b74Schristos /* consume whatever's left in the input buffer */
233ede78133Schristos if (state->strm.avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
23475fd0b74Schristos return 0;
23575fd0b74Schristos
23675fd0b74Schristos /* directly compress user buffer to file */
237ede78133Schristos state->strm.next_in = (z_const Bytef *)buf;
238ede78133Schristos do {
239ede78133Schristos unsigned n = (unsigned)-1;
240ede78133Schristos if (n > len)
241*e992f068Schristos n = (unsigned)len;
242ede78133Schristos state->strm.avail_in = n;
243ede78133Schristos state->x.pos += n;
24475fd0b74Schristos if (gz_comp(state, Z_NO_FLUSH) == -1)
24575fd0b74Schristos return 0;
246ede78133Schristos len -= n;
247ede78133Schristos } while (len);
248ede78133Schristos }
249ede78133Schristos
250ede78133Schristos /* input was all buffered or compressed */
251ede78133Schristos return put;
252ede78133Schristos }
253ede78133Schristos
254ede78133Schristos /* -- see zlib.h -- */
gzwrite(file,buf,len)255ede78133Schristos int ZEXPORT gzwrite(file, buf, len)
256ede78133Schristos gzFile file;
257ede78133Schristos voidpc buf;
258ede78133Schristos unsigned len;
259ede78133Schristos {
260ede78133Schristos gz_statep state;
261ede78133Schristos
262ede78133Schristos /* get internal structure */
263ede78133Schristos if (file == NULL)
264ede78133Schristos return 0;
265ede78133Schristos state = (gz_statep)file;
266ede78133Schristos
267ede78133Schristos /* check that we're writing and that there's no error */
268ede78133Schristos if (state->mode != GZ_WRITE || state->err != Z_OK)
269ede78133Schristos return 0;
270ede78133Schristos
271ede78133Schristos /* since an int is returned, make sure len fits in one, otherwise return
272ede78133Schristos with an error (this avoids a flaw in the interface) */
273ede78133Schristos if ((int)len < 0) {
274ede78133Schristos gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
275ede78133Schristos return 0;
27675fd0b74Schristos }
27775fd0b74Schristos
278ede78133Schristos /* write len bytes from buf (the return value will fit in an int) */
279ede78133Schristos return (int)gz_write(state, buf, len);
280ede78133Schristos }
281ede78133Schristos
282ede78133Schristos /* -- see zlib.h -- */
gzfwrite(buf,size,nitems,file)283ede78133Schristos z_size_t ZEXPORT gzfwrite(buf, size, nitems, file)
284ede78133Schristos voidpc buf;
285ede78133Schristos z_size_t size;
286ede78133Schristos z_size_t nitems;
287ede78133Schristos gzFile file;
288ede78133Schristos {
289ede78133Schristos z_size_t len;
290ede78133Schristos gz_statep state;
291ede78133Schristos
292ede78133Schristos /* get internal structure */
293ede78133Schristos if (file == NULL)
294ede78133Schristos return 0;
295ede78133Schristos state = (gz_statep)file;
296ede78133Schristos
297ede78133Schristos /* check that we're writing and that there's no error */
298ede78133Schristos if (state->mode != GZ_WRITE || state->err != Z_OK)
299ede78133Schristos return 0;
300ede78133Schristos
301ede78133Schristos /* compute bytes to read -- error on overflow */
302ede78133Schristos len = nitems * size;
303ede78133Schristos if (size && len / size != nitems) {
304ede78133Schristos gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
305ede78133Schristos return 0;
306ede78133Schristos }
307ede78133Schristos
308ede78133Schristos /* write len bytes to buf, return the number of full items written */
309ede78133Schristos return len ? gz_write(state, buf, len) / size : 0;
31075fd0b74Schristos }
31175fd0b74Schristos
31275fd0b74Schristos /* -- see zlib.h -- */
gzputc(file,c)31375fd0b74Schristos int ZEXPORT gzputc(file, c)
31475fd0b74Schristos gzFile file;
31575fd0b74Schristos int c;
31675fd0b74Schristos {
31775fd0b74Schristos unsigned have;
31875fd0b74Schristos unsigned char buf[1];
31975fd0b74Schristos gz_statep state;
32075fd0b74Schristos z_streamp strm;
32175fd0b74Schristos
32275fd0b74Schristos /* get internal structure */
32375fd0b74Schristos if (file == NULL)
32475fd0b74Schristos return -1;
32575fd0b74Schristos state = (gz_statep)file;
32675fd0b74Schristos strm = &(state->strm);
32775fd0b74Schristos
32875fd0b74Schristos /* check that we're writing and that there's no error */
32975fd0b74Schristos if (state->mode != GZ_WRITE || state->err != Z_OK)
33075fd0b74Schristos return -1;
33175fd0b74Schristos
33275fd0b74Schristos /* check for seek request */
33375fd0b74Schristos if (state->seek) {
33475fd0b74Schristos state->seek = 0;
33575fd0b74Schristos if (gz_zero(state, state->skip) == -1)
33675fd0b74Schristos return -1;
33775fd0b74Schristos }
33875fd0b74Schristos
33975fd0b74Schristos /* try writing to input buffer for speed (state->size == 0 if buffer not
34075fd0b74Schristos initialized) */
34175fd0b74Schristos if (state->size) {
34275fd0b74Schristos if (strm->avail_in == 0)
34375fd0b74Schristos strm->next_in = state->in;
34475fd0b74Schristos have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
34575fd0b74Schristos if (have < state->size) {
346ede78133Schristos state->in[have] = (unsigned char)c;
34775fd0b74Schristos strm->avail_in++;
34875fd0b74Schristos state->x.pos++;
34975fd0b74Schristos return c & 0xff;
35075fd0b74Schristos }
35175fd0b74Schristos }
35275fd0b74Schristos
35375fd0b74Schristos /* no room in buffer or not initialized, use gz_write() */
354ede78133Schristos buf[0] = (unsigned char)c;
355ede78133Schristos if (gz_write(state, buf, 1) != 1)
35675fd0b74Schristos return -1;
35775fd0b74Schristos return c & 0xff;
35875fd0b74Schristos }
35975fd0b74Schristos
36075fd0b74Schristos /* -- see zlib.h -- */
gzputs(file,s)361*e992f068Schristos int ZEXPORT gzputs(file, s)
36275fd0b74Schristos gzFile file;
363*e992f068Schristos const char *s;
36475fd0b74Schristos {
365*e992f068Schristos z_size_t len, put;
366ede78133Schristos gz_statep state;
367ede78133Schristos
368ede78133Schristos /* get internal structure */
369ede78133Schristos if (file == NULL)
370ede78133Schristos return -1;
371ede78133Schristos state = (gz_statep)file;
372ede78133Schristos
373ede78133Schristos /* check that we're writing and that there's no error */
374ede78133Schristos if (state->mode != GZ_WRITE || state->err != Z_OK)
375ede78133Schristos return -1;
37675fd0b74Schristos
37775fd0b74Schristos /* write string */
378*e992f068Schristos len = strlen(s);
379*e992f068Schristos if ((int)len < 0 || (unsigned)len != len) {
380*e992f068Schristos gz_error(state, Z_STREAM_ERROR, "string length does not fit in int");
381*e992f068Schristos return -1;
382*e992f068Schristos }
383*e992f068Schristos put = gz_write(state, s, len);
384*e992f068Schristos return put < len ? -1 : (int)len;
38575fd0b74Schristos }
38675fd0b74Schristos
38775fd0b74Schristos #if defined(STDC) || defined(Z_HAVE_STDARG_H)
38875fd0b74Schristos #include <stdarg.h>
38975fd0b74Schristos
39075fd0b74Schristos /* -- see zlib.h -- */
gzvprintf(gzFile file,const char * format,va_list va)39175fd0b74Schristos int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
39275fd0b74Schristos {
393ede78133Schristos int len;
394ede78133Schristos unsigned left;
395ede78133Schristos char *next;
39675fd0b74Schristos gz_statep state;
39775fd0b74Schristos z_streamp strm;
39875fd0b74Schristos
39975fd0b74Schristos /* get internal structure */
40075fd0b74Schristos if (file == NULL)
401ede78133Schristos return Z_STREAM_ERROR;
40275fd0b74Schristos state = (gz_statep)file;
40375fd0b74Schristos strm = &(state->strm);
40475fd0b74Schristos
40575fd0b74Schristos /* check that we're writing and that there's no error */
40675fd0b74Schristos if (state->mode != GZ_WRITE || state->err != Z_OK)
407ede78133Schristos return Z_STREAM_ERROR;
40875fd0b74Schristos
40975fd0b74Schristos /* make sure we have some buffer space */
41075fd0b74Schristos if (state->size == 0 && gz_init(state) == -1)
411ede78133Schristos return state->err;
41275fd0b74Schristos
41375fd0b74Schristos /* check for seek request */
41475fd0b74Schristos if (state->seek) {
41575fd0b74Schristos state->seek = 0;
41675fd0b74Schristos if (gz_zero(state, state->skip) == -1)
417ede78133Schristos return state->err;
41875fd0b74Schristos }
41975fd0b74Schristos
420ede78133Schristos /* do the printf() into the input buffer, put length in len -- the input
421ede78133Schristos buffer is double-sized just for this function, so there is guaranteed to
422ede78133Schristos be state->size bytes available after the current contents */
423ede78133Schristos if (strm->avail_in == 0)
424ede78133Schristos strm->next_in = state->in;
425ede78133Schristos next = (char *)(state->in + (strm->next_in - state->in) + strm->avail_in);
426ede78133Schristos next[state->size - 1] = 0;
42775fd0b74Schristos #ifdef NO_vsnprintf
42875fd0b74Schristos # ifdef HAS_vsprintf_void
429ede78133Schristos (void)vsprintf(next, format, va);
430ede78133Schristos for (len = 0; len < state->size; len++)
431ede78133Schristos if (next[len] == 0) break;
43275fd0b74Schristos # else
433ede78133Schristos len = vsprintf(next, format, va);
43475fd0b74Schristos # endif
43575fd0b74Schristos #else
43675fd0b74Schristos # ifdef HAS_vsnprintf_void
437ede78133Schristos (void)vsnprintf(next, state->size, format, va);
438ede78133Schristos len = strlen(next);
43975fd0b74Schristos # else
440ede78133Schristos len = vsnprintf(next, state->size, format, va);
44175fd0b74Schristos # endif
44275fd0b74Schristos #endif
44375fd0b74Schristos
44475fd0b74Schristos /* check that printf() results fit in buffer */
445ede78133Schristos if (len == 0 || (unsigned)len >= state->size || next[state->size - 1] != 0)
44675fd0b74Schristos return 0;
44775fd0b74Schristos
448ede78133Schristos /* update buffer and position, compress first half if past that */
449ede78133Schristos strm->avail_in += (unsigned)len;
45075fd0b74Schristos state->x.pos += len;
451ede78133Schristos if (strm->avail_in >= state->size) {
452ede78133Schristos left = strm->avail_in - state->size;
453ede78133Schristos strm->avail_in = state->size;
454ede78133Schristos if (gz_comp(state, Z_NO_FLUSH) == -1)
455ede78133Schristos return state->err;
456*e992f068Schristos memmove(state->in, state->in + state->size, left);
457ede78133Schristos strm->next_in = state->in;
458ede78133Schristos strm->avail_in = left;
459ede78133Schristos }
46075fd0b74Schristos return len;
46175fd0b74Schristos }
46275fd0b74Schristos
gzprintf(gzFile file,const char * format,...)46375fd0b74Schristos int ZEXPORTVA gzprintf(gzFile file, const char *format, ...)
46475fd0b74Schristos {
46575fd0b74Schristos va_list va;
46675fd0b74Schristos int ret;
46775fd0b74Schristos
46875fd0b74Schristos va_start(va, format);
46975fd0b74Schristos ret = gzvprintf(file, format, va);
47075fd0b74Schristos va_end(va);
47175fd0b74Schristos return ret;
47275fd0b74Schristos }
47375fd0b74Schristos
47475fd0b74Schristos #else /* !STDC && !Z_HAVE_STDARG_H */
47575fd0b74Schristos
47675fd0b74Schristos /* -- see zlib.h -- */
gzprintf(file,format,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20)47775fd0b74Schristos int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
47875fd0b74Schristos a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
47975fd0b74Schristos gzFile file;
48075fd0b74Schristos const char *format;
48175fd0b74Schristos int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
48275fd0b74Schristos a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
48375fd0b74Schristos {
484ede78133Schristos unsigned len, left;
485ede78133Schristos char *next;
48675fd0b74Schristos gz_statep state;
48775fd0b74Schristos z_streamp strm;
48875fd0b74Schristos
48975fd0b74Schristos /* get internal structure */
49075fd0b74Schristos if (file == NULL)
491ede78133Schristos return Z_STREAM_ERROR;
49275fd0b74Schristos state = (gz_statep)file;
49375fd0b74Schristos strm = &(state->strm);
49475fd0b74Schristos
49575fd0b74Schristos /* check that can really pass pointer in ints */
49675fd0b74Schristos if (sizeof(int) != sizeof(void *))
497ede78133Schristos return Z_STREAM_ERROR;
49875fd0b74Schristos
49975fd0b74Schristos /* check that we're writing and that there's no error */
50075fd0b74Schristos if (state->mode != GZ_WRITE || state->err != Z_OK)
501ede78133Schristos return Z_STREAM_ERROR;
50275fd0b74Schristos
50375fd0b74Schristos /* make sure we have some buffer space */
50475fd0b74Schristos if (state->size == 0 && gz_init(state) == -1)
505ede78133Schristos return state->error;
50675fd0b74Schristos
50775fd0b74Schristos /* check for seek request */
50875fd0b74Schristos if (state->seek) {
50975fd0b74Schristos state->seek = 0;
51075fd0b74Schristos if (gz_zero(state, state->skip) == -1)
511ede78133Schristos return state->error;
51275fd0b74Schristos }
51375fd0b74Schristos
514ede78133Schristos /* do the printf() into the input buffer, put length in len -- the input
515ede78133Schristos buffer is double-sized just for this function, so there is guaranteed to
516ede78133Schristos be state->size bytes available after the current contents */
517ede78133Schristos if (strm->avail_in == 0)
518ede78133Schristos strm->next_in = state->in;
519ede78133Schristos next = (char *)(strm->next_in + strm->avail_in);
520ede78133Schristos next[state->size - 1] = 0;
52175fd0b74Schristos #ifdef NO_snprintf
52275fd0b74Schristos # ifdef HAS_sprintf_void
523ede78133Schristos sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12,
524ede78133Schristos a13, a14, a15, a16, a17, a18, a19, a20);
52575fd0b74Schristos for (len = 0; len < size; len++)
526ede78133Schristos if (next[len] == 0)
527ede78133Schristos break;
52875fd0b74Schristos # else
529ede78133Schristos len = sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11,
530ede78133Schristos a12, a13, a14, a15, a16, a17, a18, a19, a20);
53175fd0b74Schristos # endif
53275fd0b74Schristos #else
53375fd0b74Schristos # ifdef HAS_snprintf_void
534ede78133Schristos snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8, a9,
535ede78133Schristos a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
536ede78133Schristos len = strlen(next);
53775fd0b74Schristos # else
538ede78133Schristos len = snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8,
539ede78133Schristos a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
54075fd0b74Schristos # endif
54175fd0b74Schristos #endif
54275fd0b74Schristos
54375fd0b74Schristos /* check that printf() results fit in buffer */
544ede78133Schristos if (len == 0 || len >= state->size || next[state->size - 1] != 0)
54575fd0b74Schristos return 0;
54675fd0b74Schristos
547ede78133Schristos /* update buffer and position, compress first half if past that */
548ede78133Schristos strm->avail_in += len;
54975fd0b74Schristos state->x.pos += len;
550ede78133Schristos if (strm->avail_in >= state->size) {
551ede78133Schristos left = strm->avail_in - state->size;
552ede78133Schristos strm->avail_in = state->size;
553ede78133Schristos if (gz_comp(state, Z_NO_FLUSH) == -1)
554ede78133Schristos return state->err;
555*e992f068Schristos memmove(state->in, state->in + state->size, left);
556ede78133Schristos strm->next_in = state->in;
557ede78133Schristos strm->avail_in = left;
558ede78133Schristos }
559ede78133Schristos return (int)len;
56075fd0b74Schristos }
56175fd0b74Schristos
56275fd0b74Schristos #endif
56375fd0b74Schristos
56475fd0b74Schristos /* -- see zlib.h -- */
gzflush(file,flush)56575fd0b74Schristos int ZEXPORT gzflush(file, flush)
56675fd0b74Schristos gzFile file;
56775fd0b74Schristos int flush;
56875fd0b74Schristos {
56975fd0b74Schristos gz_statep state;
57075fd0b74Schristos
57175fd0b74Schristos /* get internal structure */
57275fd0b74Schristos if (file == NULL)
573ede78133Schristos return Z_STREAM_ERROR;
57475fd0b74Schristos state = (gz_statep)file;
57575fd0b74Schristos
57675fd0b74Schristos /* check that we're writing and that there's no error */
57775fd0b74Schristos if (state->mode != GZ_WRITE || state->err != Z_OK)
57875fd0b74Schristos return Z_STREAM_ERROR;
57975fd0b74Schristos
58075fd0b74Schristos /* check flush parameter */
58175fd0b74Schristos if (flush < 0 || flush > Z_FINISH)
58275fd0b74Schristos return Z_STREAM_ERROR;
58375fd0b74Schristos
58475fd0b74Schristos /* check for seek request */
58575fd0b74Schristos if (state->seek) {
58675fd0b74Schristos state->seek = 0;
58775fd0b74Schristos if (gz_zero(state, state->skip) == -1)
588ede78133Schristos return state->err;
58975fd0b74Schristos }
59075fd0b74Schristos
59175fd0b74Schristos /* compress remaining data with requested flush */
592ede78133Schristos (void)gz_comp(state, flush);
59375fd0b74Schristos return state->err;
59475fd0b74Schristos }
59575fd0b74Schristos
59675fd0b74Schristos /* -- see zlib.h -- */
gzsetparams(file,level,strategy)59775fd0b74Schristos int ZEXPORT gzsetparams(file, level, strategy)
59875fd0b74Schristos gzFile file;
59975fd0b74Schristos int level;
60075fd0b74Schristos int strategy;
60175fd0b74Schristos {
60275fd0b74Schristos gz_statep state;
60375fd0b74Schristos z_streamp strm;
60475fd0b74Schristos
60575fd0b74Schristos /* get internal structure */
60675fd0b74Schristos if (file == NULL)
60775fd0b74Schristos return Z_STREAM_ERROR;
60875fd0b74Schristos state = (gz_statep)file;
60975fd0b74Schristos strm = &(state->strm);
61075fd0b74Schristos
61175fd0b74Schristos /* check that we're writing and that there's no error */
61275fd0b74Schristos if (state->mode != GZ_WRITE || state->err != Z_OK)
61375fd0b74Schristos return Z_STREAM_ERROR;
61475fd0b74Schristos
61575fd0b74Schristos /* if no change is requested, then do nothing */
61675fd0b74Schristos if (level == state->level && strategy == state->strategy)
61775fd0b74Schristos return Z_OK;
61875fd0b74Schristos
61975fd0b74Schristos /* check for seek request */
62075fd0b74Schristos if (state->seek) {
62175fd0b74Schristos state->seek = 0;
62275fd0b74Schristos if (gz_zero(state, state->skip) == -1)
623ede78133Schristos return state->err;
62475fd0b74Schristos }
62575fd0b74Schristos
62675fd0b74Schristos /* change compression parameters for subsequent input */
62775fd0b74Schristos if (state->size) {
62875fd0b74Schristos /* flush previous input with previous parameters before changing */
629ede78133Schristos if (strm->avail_in && gz_comp(state, Z_BLOCK) == -1)
63075fd0b74Schristos return state->err;
63175fd0b74Schristos deflateParams(strm, level, strategy);
63275fd0b74Schristos }
63375fd0b74Schristos state->level = level;
63475fd0b74Schristos state->strategy = strategy;
63575fd0b74Schristos return Z_OK;
63675fd0b74Schristos }
63775fd0b74Schristos
63875fd0b74Schristos /* -- see zlib.h -- */
gzclose_w(file)63975fd0b74Schristos int ZEXPORT gzclose_w(file)
64075fd0b74Schristos gzFile file;
64175fd0b74Schristos {
64275fd0b74Schristos int ret = Z_OK;
64375fd0b74Schristos gz_statep state;
64475fd0b74Schristos
64575fd0b74Schristos /* get internal structure */
64675fd0b74Schristos if (file == NULL)
64775fd0b74Schristos return Z_STREAM_ERROR;
64875fd0b74Schristos state = (gz_statep)file;
64975fd0b74Schristos
65075fd0b74Schristos /* check that we're writing */
65175fd0b74Schristos if (state->mode != GZ_WRITE)
65275fd0b74Schristos return Z_STREAM_ERROR;
65375fd0b74Schristos
65475fd0b74Schristos /* check for seek request */
65575fd0b74Schristos if (state->seek) {
65675fd0b74Schristos state->seek = 0;
65775fd0b74Schristos if (gz_zero(state, state->skip) == -1)
65875fd0b74Schristos ret = state->err;
65975fd0b74Schristos }
66075fd0b74Schristos
66175fd0b74Schristos /* flush, free memory, and close file */
66275fd0b74Schristos if (gz_comp(state, Z_FINISH) == -1)
66375fd0b74Schristos ret = state->err;
66475fd0b74Schristos if (state->size) {
66575fd0b74Schristos if (!state->direct) {
66675fd0b74Schristos (void)deflateEnd(&(state->strm));
66775fd0b74Schristos free(state->out);
66875fd0b74Schristos }
66975fd0b74Schristos free(state->in);
67075fd0b74Schristos }
67175fd0b74Schristos gz_error(state, Z_OK, NULL);
67275fd0b74Schristos free(state->path);
67375fd0b74Schristos if (close(state->fd) == -1)
67475fd0b74Schristos ret = Z_ERRNO;
67575fd0b74Schristos free(state);
67675fd0b74Schristos return ret;
67775fd0b74Schristos }
678