blob: 6f3c9658b595bc34032c695220fabb604528927e [file] [log] [blame]
Przemyslaw Skibinskif0d7da72016-11-29 18:02:34 +01001/* gzwrite.c contains minimal changes required to be compiled with zlibWrapper:
2 * - gz_statep was converted to union to work with -Wstrict-aliasing=1 */
3
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +01004/* gzwrite.c -- zlib functions for writing gzip files
5 * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler
Przemyslaw Skibinski622d7412016-12-13 19:44:07 +01006 * For conditions of distribution and use, see http://www.zlib.net/zlib_license.html
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +01007 */
8
9#include "gzguts.h"
10
11/* Local functions */
12local int gz_init OF((gz_statep));
13local int gz_comp OF((gz_statep, int));
14local int gz_zero OF((gz_statep, z_off64_t));
15
16/* Initialize state for writing a gzip file. Mark initialization by setting
17 state->size to non-zero. Return -1 on failure or 0 on success. */
18local int gz_init(state)
19 gz_statep state;
20{
21 int ret;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010022 z_streamp strm = &(state.state->strm);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010023
24 /* allocate input buffer */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010025 state.state->in = (unsigned char *)malloc(state.state->want);
26 if (state.state->in == NULL) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010027 gz_error(state, Z_MEM_ERROR, "out of memory");
28 return -1;
29 }
30
31 /* only need output buffer and deflate state if compressing */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010032 if (!state.state->direct) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010033 /* allocate output buffer */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010034 state.state->out = (unsigned char *)malloc(state.state->want);
35 if (state.state->out == NULL) {
36 free(state.state->in);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010037 gz_error(state, Z_MEM_ERROR, "out of memory");
38 return -1;
39 }
40
41 /* allocate deflate memory, set up for gzip compression */
42 strm->zalloc = Z_NULL;
43 strm->zfree = Z_NULL;
44 strm->opaque = Z_NULL;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010045 ret = deflateInit2(strm, state.state->level, Z_DEFLATED,
46 MAX_WBITS + 16, DEF_MEM_LEVEL, state.state->strategy);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010047 if (ret != Z_OK) {
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010048 free(state.state->out);
49 free(state.state->in);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010050 gz_error(state, Z_MEM_ERROR, "out of memory");
51 return -1;
52 }
53 }
54
55 /* mark state as initialized */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010056 state.state->size = state.state->want;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010057
58 /* initialize write buffer if compressing */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010059 if (!state.state->direct) {
60 strm->avail_out = state.state->size;
61 strm->next_out = state.state->out;
62 state.state->x.next = strm->next_out;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010063 }
64 return 0;
65}
66
67/* Compress whatever is at avail_in and next_in and write to the output file.
68 Return -1 if there is an error writing to the output file, otherwise 0.
69 flush is assumed to be a valid deflate() flush value. If flush is Z_FINISH,
70 then the deflate() state is reset to start a new gzip stream. If gz->direct
71 is true, then simply write to the output file without compressing, and
72 ignore flush. */
73local int gz_comp(state, flush)
74 gz_statep state;
75 int flush;
76{
77 int ret, got;
78 unsigned have;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010079 z_streamp strm = &(state.state->strm);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010080
81 /* allocate memory if this is the first time through */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010082 if (state.state->size == 0 && gz_init(state) == -1)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010083 return -1;
84
85 /* write directly if requested */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +010086 if (state.state->direct) {
Yann Collet9ceb49e2016-12-22 15:26:33 +010087 got = (int)write(state.state->fd, strm->next_in, strm->avail_in);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +010088 if (got < 0 || (unsigned)got != strm->avail_in) {
89 gz_error(state, Z_ERRNO, zstrerror());
90 return -1;
91 }
92 strm->avail_in = 0;
93 return 0;
94 }
95
96 /* run deflate() on provided input until it produces no more output */
97 ret = Z_OK;
98 do {
99 /* write out current buffer contents if full, or if flushing, but if
100 doing Z_FINISH then don't write until we get to Z_STREAM_END */
101 if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
102 (flush != Z_FINISH || ret == Z_STREAM_END))) {
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100103 have = (unsigned)(strm->next_out - state.state->x.next);
Yann Collet9ceb49e2016-12-22 15:26:33 +0100104 if (have && ((got = (int)write(state.state->fd, state.state->x.next, have)) < 0 ||
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100105 (unsigned)got != have)) {
106 gz_error(state, Z_ERRNO, zstrerror());
107 return -1;
108 }
109 if (strm->avail_out == 0) {
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100110 strm->avail_out = state.state->size;
111 strm->next_out = state.state->out;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100112 }
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100113 state.state->x.next = strm->next_out;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100114 }
115
116 /* compress */
117 have = strm->avail_out;
118 ret = deflate(strm, flush);
119 if (ret == Z_STREAM_ERROR) {
120 gz_error(state, Z_STREAM_ERROR,
121 "internal error: deflate stream corrupt");
122 return -1;
123 }
124 have -= strm->avail_out;
125 } while (have);
126
127 /* if that completed a deflate stream, allow another to start */
128 if (flush == Z_FINISH)
129 deflateReset(strm);
130
131 /* all done, no errors */
132 return 0;
133}
134
135/* Compress len zeros to output. Return -1 on error, 0 on success. */
136local int gz_zero(state, len)
137 gz_statep state;
138 z_off64_t len;
139{
140 int first;
141 unsigned n;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100142 z_streamp strm = &(state.state->strm);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100143
144 /* consume whatever's left in the input buffer */
145 if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
146 return -1;
147
148 /* compress len zeros (len guaranteed > 0) */
149 first = 1;
150 while (len) {
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100151 n = GT_OFF(state.state->size) || (z_off64_t)state.state->size > len ?
152 (unsigned)len : state.state->size;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100153 if (first) {
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100154 memset(state.state->in, 0, n);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100155 first = 0;
156 }
157 strm->avail_in = n;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100158 strm->next_in = state.state->in;
159 state.state->x.pos += n;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100160 if (gz_comp(state, Z_NO_FLUSH) == -1)
161 return -1;
162 len -= n;
163 }
164 return 0;
165}
166
167/* -- see zlib.h -- */
168int ZEXPORT gzwrite(file, buf, len)
169 gzFile file;
170 voidpc buf;
171 unsigned len;
172{
173 unsigned put = len;
174 gz_statep state;
175 z_streamp strm;
176
177 /* get internal structure */
178 if (file == NULL)
179 return 0;
180 state = (gz_statep)file;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100181 strm = &(state.state->strm);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100182
183 /* check that we're writing and that there's no error */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100184 if (state.state->mode != GZ_WRITE || state.state->err != Z_OK)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100185 return 0;
186
187 /* since an int is returned, make sure len fits in one, otherwise return
188 with an error (this avoids the flaw in the interface) */
189 if ((int)len < 0) {
190 gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
191 return 0;
192 }
193
194 /* if len is zero, avoid unnecessary operations */
195 if (len == 0)
196 return 0;
197
198 /* allocate memory if this is the first time through */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100199 if (state.state->size == 0 && gz_init(state) == -1)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100200 return 0;
201
202 /* check for seek request */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100203 if (state.state->seek) {
204 state.state->seek = 0;
205 if (gz_zero(state, state.state->skip) == -1)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100206 return 0;
207 }
208
209 /* for small len, copy to input buffer, otherwise compress directly */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100210 if (len < state.state->size) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100211 /* copy to input buffer, compress when full */
212 do {
213 unsigned have, copy;
214
215 if (strm->avail_in == 0)
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100216 strm->next_in = state.state->in;
217 have = (unsigned)((strm->next_in + strm->avail_in) - state.state->in);
218 copy = state.state->size - have;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100219 if (copy > len)
220 copy = len;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100221 memcpy(state.state->in + have, buf, copy);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100222 strm->avail_in += copy;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100223 state.state->x.pos += copy;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100224 buf = (const char *)buf + copy;
225 len -= copy;
226 if (len && gz_comp(state, Z_NO_FLUSH) == -1)
227 return 0;
228 } while (len);
229 }
230 else {
231 /* consume whatever's left in the input buffer */
232 if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
233 return 0;
234
235 /* directly compress user buffer to file */
236 strm->avail_in = len;
237 strm->next_in = (z_const Bytef *)buf;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100238 state.state->x.pos += len;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100239 if (gz_comp(state, Z_NO_FLUSH) == -1)
240 return 0;
241 }
242
243 /* input was all buffered or compressed (put will fit in int) */
244 return (int)put;
245}
246
247/* -- see zlib.h -- */
248int ZEXPORT gzputc(file, c)
249 gzFile file;
250 int c;
251{
252 unsigned have;
253 unsigned char buf[1];
254 gz_statep state;
255 z_streamp strm;
256
257 /* get internal structure */
258 if (file == NULL)
259 return -1;
260 state = (gz_statep)file;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100261 strm = &(state.state->strm);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100262
263 /* check that we're writing and that there's no error */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100264 if (state.state->mode != GZ_WRITE || state.state->err != Z_OK)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100265 return -1;
266
267 /* check for seek request */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100268 if (state.state->seek) {
269 state.state->seek = 0;
270 if (gz_zero(state, state.state->skip) == -1)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100271 return -1;
272 }
273
274 /* try writing to input buffer for speed (state->size == 0 if buffer not
275 initialized) */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100276 if (state.state->size) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100277 if (strm->avail_in == 0)
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100278 strm->next_in = state.state->in;
279 have = (unsigned)((strm->next_in + strm->avail_in) - state.state->in);
280 if (have < state.state->size) {
Yann Collet9ceb49e2016-12-22 15:26:33 +0100281 state.state->in[have] = (unsigned char)c;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100282 strm->avail_in++;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100283 state.state->x.pos++;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100284 return c & 0xff;
285 }
286 }
287
288 /* no room in buffer or not initialized, use gz_write() */
Yann Collet9ceb49e2016-12-22 15:26:33 +0100289 buf[0] = (unsigned char)c;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100290 if (gzwrite(file, buf, 1) != 1)
291 return -1;
292 return c & 0xff;
293}
294
295/* -- see zlib.h -- */
296int ZEXPORT gzputs(file, str)
297 gzFile file;
298 const char *str;
299{
300 int ret;
301 unsigned len;
302
303 /* write string */
304 len = (unsigned)strlen(str);
305 ret = gzwrite(file, str, len);
306 return ret == 0 && len != 0 ? -1 : ret;
307}
308
309#if defined(STDC) || defined(Z_HAVE_STDARG_H)
310#include <stdarg.h>
311
312/* -- see zlib.h -- */
313int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
314{
315 int size, len;
316 gz_statep state;
317 z_streamp strm;
318
319 /* get internal structure */
320 if (file == NULL)
321 return -1;
322 state = (gz_statep)file;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100323 strm = &(state.state->strm);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100324
325 /* check that we're writing and that there's no error */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100326 if (state.state->mode != GZ_WRITE || state.state->err != Z_OK)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100327 return 0;
328
329 /* make sure we have some buffer space */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100330 if (state.state->size == 0 && gz_init(state) == -1)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100331 return 0;
332
333 /* check for seek request */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100334 if (state.state->seek) {
335 state.state->seek = 0;
336 if (gz_zero(state, state.state->skip) == -1)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100337 return 0;
338 }
339
340 /* consume whatever's left in the input buffer */
341 if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
342 return 0;
343
344 /* do the printf() into the input buffer, put length in len */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100345 size = (int)(state.state->size);
346 state.state->in[size - 1] = 0;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100347#ifdef NO_vsnprintf
348# ifdef HAS_vsprintf_void
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100349 (void)vsprintf((char *)(state.state->in), format, va);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100350 for (len = 0; len < size; len++)
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100351 if (state.state->in[len] == 0) break;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100352# else
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100353 len = vsprintf((char *)(state.state->in), format, va);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100354# endif
355#else
356# ifdef HAS_vsnprintf_void
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100357 (void)vsnprintf((char *)(state.state->in), size, format, va);
358 len = strlen((char *)(state.state->in));
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100359# else
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100360 len = vsnprintf((char *)(state.state->in), size, format, va);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100361# endif
362#endif
363
364 /* check that printf() results fit in buffer */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100365 if (len <= 0 || len >= (int)size || state.state->in[size - 1] != 0)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100366 return 0;
367
368 /* update buffer and position, defer compression until needed */
369 strm->avail_in = (unsigned)len;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100370 strm->next_in = state.state->in;
371 state.state->x.pos += len;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100372 return len;
373}
374
375int ZEXPORTVA gzprintf(gzFile file, const char *format, ...)
376{
377 va_list va;
378 int ret;
379
380 va_start(va, format);
381 ret = gzvprintf(file, format, va);
382 va_end(va);
383 return ret;
384}
385
386#else /* !STDC && !Z_HAVE_STDARG_H */
387
388/* -- see zlib.h -- */
389int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
390 a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
391 gzFile file;
392 const char *format;
393 int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
394 a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
395{
396 int size, len;
397 gz_statep state;
398 z_streamp strm;
399
400 /* get internal structure */
401 if (file == NULL)
402 return -1;
403 state = (gz_statep)file;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100404 strm = &(state.state->strm);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100405
406 /* check that can really pass pointer in ints */
407 if (sizeof(int) != sizeof(void *))
408 return 0;
409
410 /* check that we're writing and that there's no error */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100411 if (state.state->mode != GZ_WRITE || state.state->err != Z_OK)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100412 return 0;
413
414 /* make sure we have some buffer space */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100415 if (state.state->size == 0 && gz_init(state) == -1)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100416 return 0;
417
418 /* check for seek request */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100419 if (state.state->seek) {
420 state.state->seek = 0;
421 if (gz_zero(state, state.state->skip) == -1)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100422 return 0;
423 }
424
425 /* consume whatever's left in the input buffer */
426 if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
427 return 0;
428
429 /* do the printf() into the input buffer, put length in len */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100430 size = (int)(state.state->size);
431 state.state->in[size - 1] = 0;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100432#ifdef NO_snprintf
433# ifdef HAS_sprintf_void
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100434 sprintf((char *)(state.state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100435 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
436 for (len = 0; len < size; len++)
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100437 if (state.state->in[len] == 0) break;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100438# else
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100439 len = sprintf((char *)(state.state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100440 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
441# endif
442#else
443# ifdef HAS_snprintf_void
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100444 snprintf((char *)(state.state->in), size, format, a1, a2, a3, a4, a5, a6, a7, a8,
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100445 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100446 len = strlen((char *)(state.state->in));
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100447# else
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100448 len = snprintf((char *)(state.state->in), size, format, a1, a2, a3, a4, a5, a6,
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100449 a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18,
450 a19, a20);
451# endif
452#endif
453
454 /* check that printf() results fit in buffer */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100455 if (len <= 0 || len >= (int)size || state.state->in[size - 1] != 0)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100456 return 0;
457
458 /* update buffer and position, defer compression until needed */
459 strm->avail_in = (unsigned)len;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100460 strm->next_in = state.state->in;
461 state.state->x.pos += len;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100462 return len;
463}
464
465#endif
466
467/* -- see zlib.h -- */
468int ZEXPORT gzflush(file, flush)
469 gzFile file;
470 int flush;
471{
472 gz_statep state;
473
474 /* get internal structure */
475 if (file == NULL)
476 return -1;
477 state = (gz_statep)file;
478
479 /* check that we're writing and that there's no error */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100480 if (state.state->mode != GZ_WRITE || state.state->err != Z_OK)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100481 return Z_STREAM_ERROR;
482
483 /* check flush parameter */
484 if (flush < 0 || flush > Z_FINISH)
485 return Z_STREAM_ERROR;
486
487 /* check for seek request */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100488 if (state.state->seek) {
489 state.state->seek = 0;
490 if (gz_zero(state, state.state->skip) == -1)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100491 return -1;
492 }
493
494 /* compress remaining data with requested flush */
495 gz_comp(state, flush);
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100496 return state.state->err;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100497}
498
499/* -- see zlib.h -- */
500int ZEXPORT gzsetparams(file, level, strategy)
501 gzFile file;
502 int level;
503 int strategy;
504{
505 gz_statep state;
506 z_streamp strm;
507
508 /* get internal structure */
509 if (file == NULL)
510 return Z_STREAM_ERROR;
511 state = (gz_statep)file;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100512 strm = &(state.state->strm);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100513
514 /* check that we're writing and that there's no error */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100515 if (state.state->mode != GZ_WRITE || state.state->err != Z_OK)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100516 return Z_STREAM_ERROR;
517
518 /* if no change is requested, then do nothing */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100519 if (level == state.state->level && strategy == state.state->strategy)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100520 return Z_OK;
521
522 /* check for seek request */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100523 if (state.state->seek) {
524 state.state->seek = 0;
525 if (gz_zero(state, state.state->skip) == -1)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100526 return -1;
527 }
528
529 /* change compression parameters for subsequent input */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100530 if (state.state->size) {
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100531 /* flush previous input with previous parameters before changing */
532 if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1)
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100533 return state.state->err;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100534 deflateParams(strm, level, strategy);
535 }
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100536 state.state->level = level;
537 state.state->strategy = strategy;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100538 return Z_OK;
539}
540
541/* -- see zlib.h -- */
542int ZEXPORT gzclose_w(file)
543 gzFile file;
544{
545 int ret = Z_OK;
546 gz_statep state;
547
548 /* get internal structure */
549 if (file == NULL)
550 return Z_STREAM_ERROR;
551 state = (gz_statep)file;
552
553 /* check that we're writing */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100554 if (state.state->mode != GZ_WRITE)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100555 return Z_STREAM_ERROR;
556
557 /* check for seek request */
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100558 if (state.state->seek) {
559 state.state->seek = 0;
560 if (gz_zero(state, state.state->skip) == -1)
561 ret = state.state->err;
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100562 }
563
564 /* flush, free memory, and close file */
565 if (gz_comp(state, Z_FINISH) == -1)
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100566 ret = state.state->err;
567 if (state.state->size) {
568 if (!state.state->direct) {
569 (void)deflateEnd(&(state.state->strm));
570 free(state.state->out);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100571 }
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100572 free(state.state->in);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100573 }
574 gz_error(state, Z_OK, NULL);
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100575 free(state.state->path);
576 if (close(state.state->fd) == -1)
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100577 ret = Z_ERRNO;
Przemyslaw Skibinski0feb24a2016-11-29 16:05:52 +0100578 free(state.state);
Przemyslaw Skibinski3bf9a722016-11-24 18:26:30 +0100579 return ret;
580}