1
1
/* gzwrite.c -- zlib functions for writing gzip files
2
* Copyright (C) 2004, 2005, 2010 Mark Adler
2
* Copyright (C) 2004, 2005, 2010, 2011, 2012 Mark Adler
3
3
* For conditions of distribution and use, see copyright notice in zlib.h
19
19
z_streamp strm = &(state->strm);
21
/* allocate input and output buffers */
21
/* allocate input buffer */
22
22
state->in = malloc(state->want);
23
state->out = malloc(state->want);
24
if (state->in == NULL || state->out == NULL) {
25
if (state->out != NULL)
23
if (state->in == NULL) {
24
gz_error(state, Z_MEM_ERROR, "out of memory");
28
/* only need output buffer and deflate state if compressing */
30
/* allocate output buffer */
31
state->out = malloc(state->want);
32
if (state->out == NULL) {
34
gz_error(state, Z_MEM_ERROR, "out of memory");
38
/* allocate deflate memory, set up for gzip compression */
39
strm->zalloc = Z_NULL;
41
strm->opaque = Z_NULL;
42
ret = deflateInit2(strm, state->level, Z_DEFLATED,
43
MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
27
if (state->in != NULL)
29
gz_error(state, Z_MEM_ERROR, "out of memory");
33
/* allocate deflate memory, set up for gzip compression */
34
strm->zalloc = Z_NULL;
36
strm->opaque = Z_NULL;
37
ret = deflateInit2(strm, state->level, Z_DEFLATED,
38
15 + 16, 8, state->strategy);
41
gz_error(state, Z_MEM_ERROR, "out of memory");
47
gz_error(state, Z_MEM_ERROR, "out of memory");
45
52
/* mark state as initialized */
46
53
state->size = state->want;
48
/* initialize write buffer */
49
strm->avail_out = state->size;
50
strm->next_out = state->out;
51
state->next = strm->next_out;
55
/* initialize write buffer if compressing */
57
strm->avail_out = state->size;
58
strm->next_out = state->out;
59
state->x.next = strm->next_out;
55
64
/* Compress whatever is at avail_in and next_in and write to the output file.
56
65
Return -1 if there is an error writing to the output file, otherwise 0.
57
66
flush is assumed to be a valid deflate() flush value. If flush is Z_FINISH,
58
then the deflate() state is reset to start a new gzip stream. */
67
then the deflate() state is reset to start a new gzip stream. If gz->direct
68
is true, then simply write to the output file without compressing, and
59
70
local int gz_comp(state, flush)
68
79
if (state->size == 0 && gz_init(state) == -1)
82
/* write directly if requested */
84
got = write(state->fd, strm->next_in, strm->avail_in);
85
if (got < 0 || (unsigned)got != strm->avail_in) {
86
gz_error(state, Z_ERRNO, zstrerror());
71
93
/* run deflate() on provided input until it produces no more output */
75
97
doing Z_FINISH then don't write until we get to Z_STREAM_END */
76
98
if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
77
99
(flush != Z_FINISH || ret == Z_STREAM_END))) {
78
have = (unsigned)(strm->next_out - state->next);
79
if (have && ((got = write(state->fd, state->next, have)) < 0 ||
100
have = (unsigned)(strm->next_out - state->x.next);
101
if (have && ((got = write(state->fd, state->x.next, have)) < 0 ||
80
102
(unsigned)got != have)) {
81
103
gz_error(state, Z_ERRNO, zstrerror());
163
185
/* since an int is returned, make sure len fits in one, otherwise return
164
186
with an error (this avoids the flaw in the interface) */
165
187
if ((int)len < 0) {
166
gz_error(state, Z_BUF_ERROR, "requested length does not fit in int");
188
gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
342
364
/* update buffer and position, defer compression until needed */
343
365
strm->avail_in = (unsigned)len;
344
366
strm->next_in = state->in;
371
#else /* !STDC && !Z_HAVE_STDARG_H */
351
373
/* -- see zlib.h -- */
352
374
int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
515
541
/* check for seek request */
516
542
if (state->seek) {
518
ret += gz_zero(state, state->skip);
544
if (gz_zero(state, state->skip) == -1)
521
548
/* flush, free memory, and close file */
522
ret += gz_comp(state, Z_FINISH);
523
(void)deflateEnd(&(state->strm));
549
if (gz_comp(state, Z_FINISH) == -1)
551
if (!state->direct) {
552
(void)deflateEnd(&(state->strm));
526
556
gz_error(state, Z_OK, NULL);
527
557
free(state->path);
528
ret += close(state->fd);
558
if (close(state->fd) == -1)
530
return ret ? Z_ERRNO : Z_OK;
564
/* used by zlibVersion() to get the vsnprintf story from the horse's mouth */
565
unsigned long ZEXPORT gzflags()
567
unsigned long flags = 0;
568
#if defined(STDC) || defined(Z_HAVE_STDARG_H)
571
# ifdef HAS_vsprintf_void
575
# ifdef HAS_vsnprintf_void
583
# ifdef HAS_sprintf_void
587
# ifdef HAS_snprintf_void