1 /* gzwrite.c contains minimal changes required to be compiled with zlibWrapper:
2 * - gz_statep was converted to union to work with -Wstrict-aliasing=1 */
4 /* gzwrite.c -- zlib functions for writing gzip files
5 * Copyright (C) 2004-2017 Mark Adler
6 * For conditions of distribution and use, see https://www.zlib.net/zlib_license.html
14 local int gz_init _Z_OF((gz_statep));
15 local int gz_comp _Z_OF((gz_statep, int));
16 local int gz_zero _Z_OF((gz_statep, z_off64_t));
17 local z_size_t gz_write _Z_OF((gz_statep, voidpc, z_size_t));
19 /* Initialize state for writing a gzip file. Mark initialization by setting
20 state.state->size to non-zero. Return -1 on a memory allocation failure, or 0 on
22 local int gz_init(gz_statep state) {
24 z_streamp strm = &(state.state->strm);
26 /* allocate input buffer (double size for gzprintf) */
27 state.state->in = (unsigned char*)malloc(state.state->want << 1);
28 if (state.state->in == NULL) {
29 gz_error(state, Z_MEM_ERROR, "out of memory");
33 /* only need output buffer and deflate state if compressing */
34 if (!state.state->direct) {
35 /* allocate output buffer */
36 state.state->out = (unsigned char*)malloc(state.state->want);
37 if (state.state->out == NULL) {
38 free(state.state->in);
39 gz_error(state, Z_MEM_ERROR, "out of memory");
43 /* allocate deflate memory, set up for gzip compression */
44 strm->zalloc = Z_NULL;
46 strm->opaque = Z_NULL;
47 ret = deflateInit2(strm, state.state->level, Z_DEFLATED,
48 MAX_WBITS + 16, DEF_MEM_LEVEL, state.state->strategy);
50 free(state.state->out);
51 free(state.state->in);
52 gz_error(state, Z_MEM_ERROR, "out of memory");
58 /* mark state as initialized */
59 state.state->size = state.state->want;
61 /* initialize write buffer if compressing */
62 if (!state.state->direct) {
63 strm->avail_out = state.state->size;
64 strm->next_out = state.state->out;
65 state.state->x.next = strm->next_out;
70 /* Compress whatever is at avail_in and next_in and write to the output file.
71 Return -1 if there is an error writing to the output file or if gz_init()
72 fails to allocate memory, otherwise 0. flush is assumed to be a valid
73 deflate() flush value. If flush is Z_FINISH, then the deflate() state is
74 reset to start a new gzip stream. If gz->direct is true, then simply write
75 to the output file without compressing, and ignore flush. */
76 local int gz_comp(gz_statep state, int flush) {
78 unsigned have, put, max = ((unsigned)-1 >> 2) + 1;
79 z_streamp strm = &(state.state->strm);
81 /* allocate memory if this is the first time through */
82 if (state.state->size == 0 && gz_init(state) == -1)
85 /* write directly if requested */
86 if (state.state->direct) {
87 while (strm->avail_in) {
88 put = strm->avail_in > max ? max : strm->avail_in;
89 writ = (int)write(state.state->fd, strm->next_in, put);
91 gz_error(state, Z_ERRNO, zstrerror());
94 strm->avail_in -= (unsigned)writ;
95 strm->next_in += writ;
100 /* run deflate() on provided input until it produces no more output */
103 /* write out current buffer contents if full, or if flushing, but if
104 doing Z_FINISH then don't write until we get to Z_STREAM_END */
105 if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
106 (flush != Z_FINISH || ret == Z_STREAM_END))) {
107 while (strm->next_out > state.state->x.next) {
108 put = strm->next_out - state.state->x.next > (int)max ? max :
109 (unsigned)(strm->next_out - state.state->x.next);
110 writ = (int)write(state.state->fd, state.state->x.next, put);
112 gz_error(state, Z_ERRNO, zstrerror());
115 state.state->x.next += writ;
117 if (strm->avail_out == 0) {
118 strm->avail_out = state.state->size;
119 strm->next_out = state.state->out;
120 state.state->x.next = state.state->out;
125 have = strm->avail_out;
126 ret = deflate(strm, flush);
127 if (ret == Z_STREAM_ERROR) {
128 gz_error(state, Z_STREAM_ERROR,
129 "internal error: deflate stream corrupt");
132 have -= strm->avail_out;
135 /* if that completed a deflate stream, allow another to start */
136 if (flush == Z_FINISH)
139 /* all done, no errors */
143 /* Compress len zeros to output. Return -1 on a write error or memory
144 allocation failure by gz_comp(), or 0 on success. */
145 local int gz_zero(gz_statep state, z_off64_t len) {
148 z_streamp strm = &(state.state->strm);
150 /* consume whatever's left in the input buffer */
151 if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
154 /* compress len zeros (len guaranteed > 0) */
157 n = GT_OFF(state.state->size) || (z_off64_t)state.state->size > len ?
158 (unsigned)len : state.state->size;
160 memset(state.state->in, 0, n);
164 strm->next_in = state.state->in;
165 state.state->x.pos += n;
166 if (gz_comp(state, Z_NO_FLUSH) == -1)
173 /* Write len bytes from buf to file. Return the number of bytes written. If
174 the returned value is less than len, then there was an error. */
175 local z_size_t gz_write(gz_statep state, voidpc buf, z_size_t len) {
178 /* if len is zero, avoid unnecessary operations */
182 /* allocate memory if this is the first time through */
183 if (state.state->size == 0 && gz_init(state) == -1)
186 /* check for seek request */
187 if (state.state->seek) {
188 state.state->seek = 0;
189 if (gz_zero(state, state.state->skip) == -1)
193 /* for small len, copy to input buffer, otherwise compress directly */
194 if (len < state.state->size) {
195 /* copy to input buffer, compress when full */
199 if (state.state->strm.avail_in == 0)
200 state.state->strm.next_in = state.state->in;
201 have = (unsigned)((state.state->strm.next_in + state.state->strm.avail_in) -
203 copy = state.state->size - have;
206 memcpy(state.state->in + have, buf, copy);
207 state.state->strm.avail_in += copy;
208 state.state->x.pos += copy;
209 buf = (const char *)buf + copy;
211 if (len && gz_comp(state, Z_NO_FLUSH) == -1)
216 /* consume whatever's left in the input buffer */
217 if (state.state->strm.avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
220 /* directly compress user buffer to file */
221 state.state->strm.next_in = (z_const Bytef *)buf;
223 z_size_t n = (unsigned)-1;
226 state.state->strm.avail_in = (z_uInt)n;
227 state.state->x.pos += n;
228 if (gz_comp(state, Z_NO_FLUSH) == -1)
234 /* input was all buffered or compressed */
238 /* -- see zlib.h -- */
239 int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len) {
242 /* get internal structure */
247 /* check that we're writing and that there's no error */
248 if (state.state->mode != GZ_WRITE || state.state->err != Z_OK)
251 /* since an int is returned, make sure len fits in one, otherwise return
252 with an error (this avoids a flaw in the interface) */
254 gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
258 /* write len bytes from buf (the return value will fit in an int) */
259 return (int)gz_write(state, buf, len);
262 /* -- see zlib.h -- */
263 z_size_t ZEXPORT gzfwrite(voidpc buf, z_size_t size, z_size_t nitems,
268 /* get internal structure */
274 /* check that we're writing and that there's no error */
275 if (state.state->mode != GZ_WRITE || state.state->err != Z_OK)
278 /* compute bytes to read -- error on overflow */
280 if (size && (len / size != nitems)) {
281 gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
285 /* write len bytes to buf, return the number of full items written */
286 return len ? gz_write(state, buf, len) / size : 0;
289 /* -- see zlib.h -- */
290 int ZEXPORT gzputc(gzFile file, int c) {
292 unsigned char buf[1];
296 /* get internal structure */
300 strm = &(state.state->strm);
302 /* check that we're writing and that there's no error */
303 if (state.state->mode != GZ_WRITE || state.state->err != Z_OK)
306 /* check for seek request */
307 if (state.state->seek) {
308 state.state->seek = 0;
309 if (gz_zero(state, state.state->skip) == -1)
313 /* try writing to input buffer for speed (state.state->size == 0 if buffer not
315 if (state.state->size) {
316 if (strm->avail_in == 0)
317 strm->next_in = state.state->in;
318 have = (unsigned)((strm->next_in + strm->avail_in) - state.state->in);
319 if (have < state.state->size) {
320 state.state->in[have] = (unsigned char)c;
322 state.state->x.pos++;
327 /* no room in buffer or not initialized, use gz_write() */
328 buf[0] = (unsigned char)c;
329 if (gz_write(state, buf, 1) != 1)
334 /* -- see zlib.h -- */
335 int ZEXPORT gzputs(gzFile file, const char *str) {
340 /* get internal structure */
345 /* check that we're writing and that there's no error */
346 if (state.state->mode != GZ_WRITE || state.state->err != Z_OK)
351 ret = (int)gz_write(state, str, len);
352 return ret == 0 && len != 0 ? -1 : ret;
355 #if defined(STDC) || defined(Z_HAVE_STDARG_H)
358 /* -- see zlib.h -- */
359 int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) {
366 /* get internal structure */
368 return Z_STREAM_ERROR;
370 strm = &(state.state->strm);
372 /* check that we're writing and that there's no error */
373 if (state.state->mode != GZ_WRITE || state.state->err != Z_OK)
374 return Z_STREAM_ERROR;
376 /* make sure we have some buffer space */
377 if (state.state->size == 0 && gz_init(state) == -1)
378 return state.state->err;
380 /* check for seek request */
381 if (state.state->seek) {
382 state.state->seek = 0;
383 if (gz_zero(state, state.state->skip) == -1)
384 return state.state->err;
387 /* do the printf() into the input buffer, put length in len -- the input
388 buffer is double-sized just for this function, so there is guaranteed to
389 be state.state->size bytes available after the current contents */
390 if (strm->avail_in == 0)
391 strm->next_in = state.state->in;
392 next = (char *)(state.state->in + (strm->next_in - state.state->in) + strm->avail_in);
393 next[state.state->size - 1] = 0;
395 # ifdef HAS_vsprintf_void
396 (void)vsprintf(next, format, va);
397 for (len = 0; len < state.state->size; len++)
398 if (next[len] == 0) break;
400 len = vsprintf(next, format, va);
403 # ifdef HAS_vsnprintf_void
404 (void)vsnprintf(next, state.state->size, format, va);
407 len = vsnprintf(next, state.state->size, format, va);
411 /* check that printf() results fit in buffer */
412 if (len == 0 || (unsigned)len >= state.state->size || next[state.state->size - 1] != 0)
415 /* update buffer and position, compress first half if past that */
416 strm->avail_in += (unsigned)len;
417 state.state->x.pos += len;
418 if (strm->avail_in >= state.state->size) {
419 left = strm->avail_in - state.state->size;
420 strm->avail_in = state.state->size;
421 if (gz_comp(state, Z_NO_FLUSH) == -1)
422 return state.state->err;
423 memcpy(state.state->in, state.state->in + state.state->size, left);
424 strm->next_in = state.state->in;
425 strm->avail_in = left;
430 int ZEXPORTVA gzprintf(gzFile file, const char *format, ...) {
434 va_start(va, format);
435 ret = gzvprintf(file, format, va);
440 #else /* !STDC && !Z_HAVE_STDARG_H */
442 /* -- see zlib.h -- */
443 int ZEXPORTVA gzprintf(gzFile file, const char *format, int a1, int a2, int a3,
444 int a4, int a5, int a6, int a7, int a8, int a9, int a10,
445 int a11, int a12, int a13, int a14, int a15, int a16,
446 int a17, int a18, int a19, int a20) {
452 /* get internal structure */
454 return Z_STREAM_ERROR;
455 state = (gz_statep)file;
456 strm = &(state.state->strm);
458 /* check that can really pass pointer in ints */
459 if (sizeof(int) != sizeof(void *))
460 return Z_STREAM_ERROR;
462 /* check that we're writing and that there's no error */
463 if (state.state->mode != GZ_WRITE || state.state->err != Z_OK)
464 return Z_STREAM_ERROR;
466 /* make sure we have some buffer space */
467 if (state.state->size == 0 && gz_init(state) == -1)
468 return state.state->error;
470 /* check for seek request */
471 if (state.state->seek) {
472 state.state->seek = 0;
473 if (gz_zero(state, state.state->skip) == -1)
474 return state.state->error;
477 /* do the printf() into the input buffer, put length in len -- the input
478 buffer is double-sized just for this function, so there is guaranteed to
479 be state.state->size bytes available after the current contents */
480 if (strm->avail_in == 0)
481 strm->next_in = state.state->in;
482 next = (char *)(strm->next_in + strm->avail_in);
483 next[state.state->size - 1] = 0;
485 # ifdef HAS_sprintf_void
486 sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12,
487 a13, a14, a15, a16, a17, a18, a19, a20);
488 for (len = 0; len < size; len++)
492 len = sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11,
493 a12, a13, a14, a15, a16, a17, a18, a19, a20);
496 # ifdef HAS_snprintf_void
497 snprintf(next, state.state->size, format, a1, a2, a3, a4, a5, a6, a7, a8, a9,
498 a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
501 len = snprintf(next, state.state->size, format, a1, a2, a3, a4, a5, a6, a7, a8,
502 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
506 /* check that printf() results fit in buffer */
507 if (len == 0 || len >= state.state->size || next[state.state->size - 1] != 0)
510 /* update buffer and position, compress first half if past that */
511 strm->avail_in += len;
512 state.state->x.pos += len;
513 if (strm->avail_in >= state.state->size) {
514 left = strm->avail_in - state.state->size;
515 strm->avail_in = state.state->size;
516 if (gz_comp(state, Z_NO_FLUSH) == -1)
517 return state.state->err;
518 memcpy(state.state->in, state.state->in + state.state->size, left);
519 strm->next_in = state.state->in;
520 strm->avail_in = left;
527 /* -- see zlib.h -- */
528 int ZEXPORT gzflush(gzFile file, int flush) {
531 /* get internal structure */
533 return Z_STREAM_ERROR;
536 /* check that we're writing and that there's no error */
537 if (state.state->mode != GZ_WRITE || state.state->err != Z_OK)
538 return Z_STREAM_ERROR;
540 /* check flush parameter */
541 if (flush < 0 || flush > Z_FINISH)
542 return Z_STREAM_ERROR;
544 /* check for seek request */
545 if (state.state->seek) {
546 state.state->seek = 0;
547 if (gz_zero(state, state.state->skip) == -1)
548 return state.state->err;
551 /* compress remaining data with requested flush */
552 (void)gz_comp(state, flush);
553 return state.state->err;
556 /* -- see zlib.h -- */
557 int ZEXPORT gzsetparams(gzFile file, int level, int strategy) {
561 /* get internal structure */
563 return Z_STREAM_ERROR;
565 strm = &(state.state->strm);
567 /* check that we're writing and that there's no error */
568 if (state.state->mode != GZ_WRITE || state.state->err != Z_OK)
569 return Z_STREAM_ERROR;
571 /* if no change is requested, then do nothing */
572 if (level == state.state->level && strategy == state.state->strategy)
575 /* check for seek request */
576 if (state.state->seek) {
577 state.state->seek = 0;
578 if (gz_zero(state, state.state->skip) == -1)
579 return state.state->err;
582 /* change compression parameters for subsequent input */
583 if (state.state->size) {
584 /* flush previous input with previous parameters before changing */
585 if (strm->avail_in && gz_comp(state, Z_BLOCK) == -1)
586 return state.state->err;
587 deflateParams(strm, level, strategy);
589 state.state->level = level;
590 state.state->strategy = strategy;
594 /* -- see zlib.h -- */
595 int ZEXPORT gzclose_w(gzFile file) {
599 /* get internal structure */
601 return Z_STREAM_ERROR;
604 /* check that we're writing */
605 if (state.state->mode != GZ_WRITE)
606 return Z_STREAM_ERROR;
608 /* check for seek request */
609 if (state.state->seek) {
610 state.state->seek = 0;
611 if (gz_zero(state, state.state->skip) == -1)
612 ret = state.state->err;
615 /* flush, free memory, and close file */
616 if (gz_comp(state, Z_FINISH) == -1)
617 ret = state.state->err;
618 if (state.state->size) {
619 if (!state.state->direct) {
620 (void)deflateEnd(&(state.state->strm));
621 free(state.state->out);
623 free(state.state->in);
625 gz_error(state, Z_OK, NULL);
626 free(state.state->path);
627 if (close(state.state->fd) == -1)