18 z_streamp strm = &(state->strm);
21 state->in = (
unsigned char *)malloc(state->want);
22 if (state->in == NULL) {
23 gz_error(state, Z_MEM_ERROR,
"out of memory");
30 state->out = (
unsigned char *)malloc(state->want);
31 if (state->out == NULL) {
33 gz_error(state, Z_MEM_ERROR,
"out of memory");
38 strm->zalloc = Z_NULL;
40 strm->opaque = Z_NULL;
41 ret = deflateInit2(strm, state->level, Z_DEFLATED,
42 MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
46 gz_error(state, Z_MEM_ERROR,
"out of memory");
52 state->size = state->want;
56 strm->avail_out = state->size;
57 strm->next_out = state->out;
58 state->x.next = strm->next_out;
73 z_streamp strm = &(state->strm);
76 if (state->size == 0 &&
gz_init(state) == -1)
81 got = write(state->fd, strm->next_in, strm->avail_in);
82 if (got < 0 || (
unsigned)got != strm->avail_in) {
83 gz_error(state, Z_ERRNO, zstrerror());
95 if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
96 (flush != Z_FINISH || ret == Z_STREAM_END))) {
97 have = (unsigned)(strm->next_out - state->x.next);
98 if (have && ((got = write(state->fd, state->x.next, have)) < 0 ||
99 (
unsigned)got != have)) {
100 gz_error(state, Z_ERRNO, zstrerror());
103 if (strm->avail_out == 0) {
104 strm->avail_out = state->size;
105 strm->next_out = state->out;
107 state->x.next = strm->next_out;
111 have = strm->avail_out;
113 if (ret == Z_STREAM_ERROR) {
115 "internal error: deflate stream corrupt");
118 have -= strm->avail_out;
122 if (flush == Z_FINISH)
134 z_streamp strm = &(state->strm);
137 if (strm->avail_in &&
gz_comp(state, Z_NO_FLUSH) == -1)
143 n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
144 (
unsigned)len : state->size;
146 memset(state->in, 0, n);
150 strm->next_in = state->in;
152 if (
gz_comp(state, Z_NO_FLUSH) == -1)
160 int ZEXPORT
gzwrite(gzFile file, voidpc buf,
unsigned len)
169 state = (gz_statep)file;
170 strm = &(state->strm);
173 if (state->mode != GZ_WRITE || state->err != Z_OK)
179 gz_error(state, Z_DATA_ERROR,
"requested length does not fit in int");
188 if (state->size == 0 &&
gz_init(state) == -1)
194 if (
gz_zero(state, state->skip) == -1)
199 if (len < state->size) {
204 if (strm->avail_in == 0)
205 strm->next_in = state->in;
206 have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
207 copy = state->size - have;
210 memcpy(state->in + have, buf, copy);
211 strm->avail_in += copy;
212 state->x.pos += copy;
213 buf = (
const char *)buf + copy;
215 if (len &&
gz_comp(state, Z_NO_FLUSH) == -1)
221 if (strm->avail_in &&
gz_comp(state, Z_NO_FLUSH) == -1)
225 strm->avail_in = len;
226 strm->next_in = (z_const Bytef *)buf;
228 if (
gz_comp(state, Z_NO_FLUSH) == -1)
240 unsigned char buf[1];
247 state = (gz_statep)file;
248 strm = &(state->strm);
251 if (state->mode != GZ_WRITE || state->err != Z_OK)
257 if (
gz_zero(state, state->skip) == -1)
264 if (strm->avail_in == 0)
265 strm->next_in = state->in;
266 have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
267 if (have < state->size) {
277 if (
gzwrite(file, buf, 1) != 1)
283 int ZEXPORT
gzputs(gzFile file,
const char *str)
289 len = (unsigned)strlen(str);
291 return ret == 0 && len != 0 ? -1 : ret;
294 #if defined(STDC) || defined(Z_HAVE_STDARG_H)
298 int ZEXPORTVA gzvprintf(gzFile file,
const char *format, va_list va)
307 state = (gz_statep)file;
308 strm = &(state->strm);
311 if (state->mode != GZ_WRITE || state->err != Z_OK)
315 if (state->size == 0 &&
gz_init(state) == -1)
321 if (
gz_zero(state, state->skip) == -1)
326 if (strm->avail_in &&
gz_comp(state, Z_NO_FLUSH) == -1)
330 size = (int)(state->size);
331 state->in[size - 1] = 0;
333 # ifdef HAS_vsprintf_void
334 (void)vsprintf((
char *)(state->in), format, va);
336 for (len = 0; len < size; len++)
337 if (state->in[len] == 0)
break;
339 len = vsprintf((
char *)(state->in), format, va);
343 # ifdef HAS_vsnprintf_void
344 (void)vsnprintf((
char *)(state->in), size, format, va);
346 len = strlen((
char *)(state->in));
348 len = vsnprintf((
char *)(state->in), size, format, va);
354 if (len <= 0 || len >= (
int)size || state->in[size - 1] != 0)
358 strm->avail_in = (unsigned)len;
359 strm->next_in = state->in;
364 int ZEXPORTVA
gzprintf(gzFile file,
const char *format, ...)
369 va_start(va, format);
370 ret = gzvprintf(file, format, va);
378 int ZEXPORTVA
gzprintf (gzFile file,
const char *format,
379 int a1,
int a2,
int a3,
int a4,
int a5,
int a6,
int a7,
int a8,
int a9,
int a10,
380 int a11,
int a12,
int a13,
int a14,
int a15,
int a16,
int a17,
int a18,
int a19,
int a20)
389 state = (gz_statep)file;
390 strm = &(state->strm);
393 if (
sizeof(
int) !=
sizeof(
void *))
397 if (state->mode != GZ_WRITE || state->err != Z_OK)
401 if (state->size == 0 &&
gz_init(state) == -1)
407 if (
gz_zero(state, state->skip) == -1)
412 if (strm->avail_in &&
gz_comp(state, Z_NO_FLUSH) == -1)
416 size = (int)(state->size);
417 state->in[size - 1] = 0;
419 # ifdef HAS_sprintf_void
420 sprintf((
char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
421 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
422 for (len = 0; len < size; len++)
423 if (state->in[len] == 0)
break;
425 len = sprintf((
char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8,
426 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
429 # ifdef HAS_snprintf_void
430 snprintf((
char *)(state->in), size, format, a1, a2, a3, a4, a5, a6, a7, a8,
431 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
432 len = strlen((
char *)(state->in));
434 len = snprintf((
char *)(state->in), size, format, a1, a2, a3, a4, a5, a6,
435 a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18,
441 if (len <= 0 || len >= (
int)size || state->in[size - 1] != 0)
445 strm->avail_in = (unsigned)len;
446 strm->next_in = state->in;
461 state = (gz_statep)file;
464 if (state->mode != GZ_WRITE || state->err != Z_OK)
465 return Z_STREAM_ERROR;
468 if (flush < 0 || flush > Z_FINISH)
469 return Z_STREAM_ERROR;
474 if (
gz_zero(state, state->skip) == -1)
491 return Z_STREAM_ERROR;
492 state = (gz_statep)file;
493 strm = &(state->strm);
496 if (state->mode != GZ_WRITE || state->err != Z_OK)
497 return Z_STREAM_ERROR;
500 if (level == state->level && strategy == state->strategy)
506 if (
gz_zero(state, state->skip) == -1)
513 if (strm->avail_in &&
gz_comp(state, Z_PARTIAL_FLUSH) == -1)
517 state->level = level;
518 state->strategy = strategy;
530 return Z_STREAM_ERROR;
531 state = (gz_statep)file;
534 if (state->mode != GZ_WRITE)
535 return Z_STREAM_ERROR;
540 if (
gz_zero(state, state->skip) == -1)
545 if (
gz_comp(state, Z_FINISH) == -1)
548 if (!state->direct) {
556 if (close(state->fd) == -1)
int ZEXPORT deflateEnd(z_streamp strm)
void ZLIB_INTERNAL gz_error(gz_statep state, int err, const char *msg)
int ZEXPORTVA gzprintf(gzFile file, const char *format, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10, int a11, int a12, int a13, int a14, int a15, int a16, int a17, int a18, int a19, int a20)
int ZEXPORT gzsetparams(gzFile file, int level, int strategy)
int ZEXPORT gzputs(gzFile file, const char *str)
local int gz_init OF((gz_statep))
int ZEXPORT deflateParams(z_streamp strm, int level, int strategy)
local int gz_comp(gz_statep state, int flush)
int ZEXPORT gzputc(gzFile file, int c)
int ZEXPORT gzclose_w(gzFile file)
int ZEXPORT deflate(z_streamp strm, int flush)
int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len)
local int gz_init(gz_statep state)
int ZEXPORT deflateReset(z_streamp strm)
local int gz_zero(gz_statep state, z_off64_t len)
int ZEXPORT gzflush(gzFile file, int flush)