gzwrite.c 18.6 KB
Newer Older
M
Mark Adler 已提交
1
/* gzwrite.c -- zlib functions for writing gzip files
M
Mark Adler 已提交
2
 * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler
M
Mark Adler 已提交
3 4 5 6 7 8 9 10
 * For conditions of distribution and use, see copyright notice in zlib.h
 */

#include "gzguts.h"

/* Local functions */
local int gz_init OF((gz_statep));
local int gz_comp OF((gz_statep, int));
M
Mark Adler 已提交
11
local int gz_zero OF((gz_statep, z_off64_t));
12
local z_size_t gz_write OF((gz_statep, voidpc, z_size_t));
M
Mark Adler 已提交
13 14

/* Initialize state for writing a gzip file.  Mark initialization by setting
M
Mark Adler 已提交
15 16
   state->size to non-zero.  Return -1 on a memory allocation failure, or 0 on
   success. */
M
Mark Adler 已提交
17 18 19 20 21 22
local int gz_init(state)
    gz_statep state;
{
    int ret;
    z_streamp strm = &(state->strm);

23 24
    /* allocate input buffer (double size for gzprintf) */
    state->in = (unsigned char *)malloc(state->want << 1);
25
    if (state->in == NULL) {
M
Mark Adler 已提交
26 27 28 29
        gz_error(state, Z_MEM_ERROR, "out of memory");
        return -1;
    }

30 31 32
    /* only need output buffer and deflate state if compressing */
    if (!state->direct) {
        /* allocate output buffer */
33
        state->out = (unsigned char *)malloc(state->want);
34 35 36 37 38 39 40 41 42 43 44
        if (state->out == NULL) {
            free(state->in);
            gz_error(state, Z_MEM_ERROR, "out of memory");
            return -1;
        }

        /* allocate deflate memory, set up for gzip compression */
        strm->zalloc = Z_NULL;
        strm->zfree = Z_NULL;
        strm->opaque = Z_NULL;
        ret = deflateInit2(strm, state->level, Z_DEFLATED,
45
                           MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
46 47 48 49 50 51
        if (ret != Z_OK) {
            free(state->out);
            free(state->in);
            gz_error(state, Z_MEM_ERROR, "out of memory");
            return -1;
        }
52
        strm->next_in = NULL;
M
Mark Adler 已提交
53 54 55 56 57
    }

    /* mark state as initialized */
    state->size = state->want;

58 59 60 61 62 63
    /* initialize write buffer if compressing */
    if (!state->direct) {
        strm->avail_out = state->size;
        strm->next_out = state->out;
        state->x.next = strm->next_out;
    }
M
Mark Adler 已提交
64 65 66 67
    return 0;
}

/* Compress whatever is at avail_in and next_in and write to the output file.
M
Mark Adler 已提交
68 69 70 71 72
   Return -1 if there is an error writing to the output file or if gz_init()
   fails to allocate memory, otherwise 0.  flush is assumed to be a valid
   deflate() flush value.  If flush is Z_FINISH, then the deflate() state is
   reset to start a new gzip stream.  If gz->direct is true, then simply write
   to the output file without compressing, and ignore flush. */
M
Mark Adler 已提交
73 74 75 76
local int gz_comp(state, flush)
    gz_statep state;
    int flush;
{
M
Mark Adler 已提交
77
    int ret;
M
Mark Adler 已提交
78
    z_ssize_t got;
M
Mark Adler 已提交
79 80 81 82 83 84 85
    unsigned have;
    z_streamp strm = &(state->strm);

    /* allocate memory if this is the first time through */
    if (state->size == 0 && gz_init(state) == -1)
        return -1;

86 87
    /* write directly if requested */
    if (state->direct) {
88 89 90 91 92 93
        while (strm->avail_in) {
            got = write(state->fd, strm->next_in, strm->avail_in);
            if (got < 0) {
                gz_error(state, Z_ERRNO, zstrerror());
                return -1;
            }
M
Mark Adler 已提交
94
            strm->avail_in -= (unsigned)got;
95
            strm->next_in += got;
96 97 98 99
        }
        return 0;
    }

M
Mark Adler 已提交
100 101 102 103 104 105 106
    /* run deflate() on provided input until it produces no more output */
    ret = Z_OK;
    do {
        /* write out current buffer contents if full, or if flushing, but if
           doing Z_FINISH then don't write until we get to Z_STREAM_END */
        if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
            (flush != Z_FINISH || ret == Z_STREAM_END))) {
107 108
            while (strm->next_out > state->x.next) {
                got = write(state->fd, state->x.next,
M
Mark Adler 已提交
109
                            (unsigned long)(strm->next_out - state->x.next));
110 111 112 113 114
                if (got < 0) {
                    gz_error(state, Z_ERRNO, zstrerror());
                    return -1;
                }
                state->x.next += got;
M
Mark Adler 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
            }
            if (strm->avail_out == 0) {
                strm->avail_out = state->size;
                strm->next_out = state->out;
            }
        }

        /* compress */
        have = strm->avail_out;
        ret = deflate(strm, flush);
        if (ret == Z_STREAM_ERROR) {
            gz_error(state, Z_STREAM_ERROR,
                      "internal error: deflate stream corrupt");
            return -1;
        }
        have -= strm->avail_out;
    } while (have);

    /* if that completed a deflate stream, allow another to start */
    if (flush == Z_FINISH)
        deflateReset(strm);

    /* all done, no errors */
    return 0;
}

M
Mark Adler 已提交
141 142
/* Compress len zeros to output.  Return -1 on a write error or memory
   allocation failure by gz_comp(), or 0 on success. */
M
Mark Adler 已提交
143 144
local int gz_zero(state, len)
    gz_statep state;
M
Mark Adler 已提交
145
    z_off64_t len;
M
Mark Adler 已提交
146 147 148 149 150 151 152 153 154
{
    int first;
    unsigned n;
    z_streamp strm = &(state->strm);

    /* consume whatever's left in the input buffer */
    if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
        return -1;

M
Mark Adler 已提交
155
    /* compress len zeros (len guaranteed > 0) */
M
Mark Adler 已提交
156 157
    first = 1;
    while (len) {
M
Mark Adler 已提交
158 159
        n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
            (unsigned)len : state->size;
M
Mark Adler 已提交
160 161 162 163 164 165
        if (first) {
            memset(state->in, 0, n);
            first = 0;
        }
        strm->avail_in = n;
        strm->next_in = state->in;
166
        state->x.pos += n;
M
Mark Adler 已提交
167 168 169 170 171 172 173
        if (gz_comp(state, Z_NO_FLUSH) == -1)
            return -1;
        len -= n;
    }
    return 0;
}

174 175 176 177
/* Write len bytes from buf to file.  Return the number of bytes written.  If
   the returned value is less than len, then there was an error. */
local z_size_t gz_write(state, buf, len)
    gz_statep state;
M
Mark Adler 已提交
178
    voidpc buf;
179
    z_size_t len;
M
Mark Adler 已提交
180
{
181
    z_size_t put = len;
M
Mark Adler 已提交
182 183 184 185 186

    /* if len is zero, avoid unnecessary operations */
    if (len == 0)
        return 0;

M
Mark Adler 已提交
187 188
    /* allocate memory if this is the first time through */
    if (state->size == 0 && gz_init(state) == -1)
M
Mark Adler 已提交
189
        return 0;
M
Mark Adler 已提交
190 191 192 193 194

    /* check for seek request */
    if (state->seek) {
        state->seek = 0;
        if (gz_zero(state, state->skip) == -1)
M
Mark Adler 已提交
195
            return 0;
M
Mark Adler 已提交
196 197 198 199 200
    }

    /* for small len, copy to input buffer, otherwise compress directly */
    if (len < state->size) {
        /* copy to input buffer, compress when full */
M
Mark Adler 已提交
201
        do {
202 203
            unsigned have, copy;

204 205 206 207
            if (state->strm.avail_in == 0)
                state->strm.next_in = state->in;
            have = (unsigned)((state->strm.next_in + state->strm.avail_in) -
                              state->in);
208 209 210 211
            copy = state->size - have;
            if (copy > len)
                copy = len;
            memcpy(state->in + have, buf, copy);
212
            state->strm.avail_in += copy;
213 214 215
            state->x.pos += copy;
            buf = (const char *)buf + copy;
            len -= copy;
M
Mark Adler 已提交
216
            if (len && gz_comp(state, Z_NO_FLUSH) == -1)
M
Mark Adler 已提交
217
                return 0;
M
Mark Adler 已提交
218
        } while (len);
M
Mark Adler 已提交
219 220 221
    }
    else {
        /* consume whatever's left in the input buffer */
222
        if (state->strm.avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
M
Mark Adler 已提交
223
            return 0;
M
Mark Adler 已提交
224 225

        /* directly compress user buffer to file */
226 227
        state->strm.next_in = (z_const Bytef *)buf;
        do {
228
            unsigned n = (unsigned)-1;
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
            if (n > len)
                n = len;
            state->strm.avail_in = n;
            state->x.pos += n;
            if (gz_comp(state, Z_NO_FLUSH) == -1)
                return 0;
            len -= n;
        } while (len);
    }

    /* input was all buffered or compressed */
    return put;
}

/* -- see zlib.h -- */
int ZEXPORT gzwrite(file, buf, len)
    gzFile file;
    voidpc buf;
    unsigned len;
{
    gz_statep state;

    /* get internal structure */
    if (file == NULL)
        return 0;
    state = (gz_statep)file;

    /* check that we're writing and that there's no error */
    if (state->mode != GZ_WRITE || state->err != Z_OK)
        return 0;

    /* since an int is returned, make sure len fits in one, otherwise return
       with an error (this avoids a flaw in the interface) */
    if ((int)len < 0) {
        gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
        return 0;
    }

    /* write len bytes from buf (the return value will fit in an int) */
    return (int)gz_write(state, buf, len);
}

/* -- see zlib.h -- */
z_size_t ZEXPORT gzfwrite(buf, size, nitems, file)
    voidpc buf;
    z_size_t size;
    z_size_t nitems;
    gzFile file;
{
    z_size_t len;
    gz_statep state;

    /* get internal structure */
    if (file == NULL)
        return 0;
    state = (gz_statep)file;

    /* check that we're writing and that there's no error */
    if (state->mode != GZ_WRITE || state->err != Z_OK)
        return 0;

    /* compute bytes to read -- error on overflow */
    len = nitems * size;
    if (size && len / size != nitems) {
        gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
        return 0;
M
Mark Adler 已提交
295 296
    }

297 298
    /* write len bytes to buf, return the number of full items written */
    return len ? gz_write(state, buf, len) / size : 0;
M
Mark Adler 已提交
299 300 301 302 303 304 305
}

/* -- see zlib.h -- */
int ZEXPORT gzputc(file, c)
    gzFile file;
    int c;
{
306
    unsigned have;
M
Mark Adler 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
    unsigned char buf[1];
    gz_statep state;
    z_streamp strm;

    /* get internal structure */
    if (file == NULL)
        return -1;
    state = (gz_statep)file;
    strm = &(state->strm);

    /* check that we're writing and that there's no error */
    if (state->mode != GZ_WRITE || state->err != Z_OK)
        return -1;

    /* check for seek request */
    if (state->seek) {
        state->seek = 0;
        if (gz_zero(state, state->skip) == -1)
            return -1;
    }

    /* try writing to input buffer for speed (state->size == 0 if buffer not
       initialized) */
330 331 332
    if (state->size) {
        if (strm->avail_in == 0)
            strm->next_in = state->in;
333
        have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
334
        if (have < state->size) {
M
Mark Adler 已提交
335
            state->in[have] = (unsigned char)c;
336 337 338 339
            strm->avail_in++;
            state->x.pos++;
            return c & 0xff;
        }
M
Mark Adler 已提交
340 341 342
    }

    /* no room in buffer or not initialized, use gz_write() */
M
Mark Adler 已提交
343
    buf[0] = (unsigned char)c;
344
    if (gz_write(state, buf, 1) != 1)
M
Mark Adler 已提交
345
        return -1;
346
    return c & 0xff;
M
Mark Adler 已提交
347 348 349 350 351 352 353
}

/* -- see zlib.h -- */
int ZEXPORT gzputs(file, str)
    gzFile file;
    const char *str;
{
M
Mark Adler 已提交
354
    int ret;
355 356 357 358 359 360 361 362 363 364 365
    z_size_t len;
    gz_statep state;

    /* get internal structure */
    if (file == NULL)
        return -1;
    state = (gz_statep)file;

    /* check that we're writing and that there's no error */
    if (state->mode != GZ_WRITE || state->err != Z_OK)
        return -1;
M
Mark Adler 已提交
366

M
Mark Adler 已提交
367
    /* write string */
368 369
    len = strlen(str);
    ret = gz_write(state, str, len);
M
Mark Adler 已提交
370
    return ret == 0 && len != 0 ? -1 : ret;
M
Mark Adler 已提交
371 372
}

M
Mark Adler 已提交
373
#if defined(STDC) || defined(Z_HAVE_STDARG_H)
M
Mark Adler 已提交
374 375 376
#include <stdarg.h>

/* -- see zlib.h -- */
377
int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
M
Mark Adler 已提交
378
{
M
Mark Adler 已提交
379 380
    int len;
    unsigned left;
381
    char *next;
M
Mark Adler 已提交
382 383 384 385 386
    gz_statep state;
    z_streamp strm;

    /* get internal structure */
    if (file == NULL)
M
Mark Adler 已提交
387
        return Z_STREAM_ERROR;
M
Mark Adler 已提交
388 389 390 391 392
    state = (gz_statep)file;
    strm = &(state->strm);

    /* check that we're writing and that there's no error */
    if (state->mode != GZ_WRITE || state->err != Z_OK)
M
Mark Adler 已提交
393
        return Z_STREAM_ERROR;
M
Mark Adler 已提交
394 395 396

    /* make sure we have some buffer space */
    if (state->size == 0 && gz_init(state) == -1)
M
Mark Adler 已提交
397
        return state->err;
M
Mark Adler 已提交
398 399 400 401 402

    /* check for seek request */
    if (state->seek) {
        state->seek = 0;
        if (gz_zero(state, state->skip) == -1)
M
Mark Adler 已提交
403
            return state->err;
M
Mark Adler 已提交
404 405
    }

406 407 408 409 410
    /* do the printf() into the input buffer, put length in len -- the input
       buffer is double-sized just for this function, so there is guaranteed to
       be state->size bytes available after the current contents */
    if (strm->avail_in == 0)
        strm->next_in = state->in;
M
Mark Adler 已提交
411
    next = (char *)(state->in + (strm->next_in - state->in) + strm->avail_in);
412
    next[state->size - 1] = 0;
M
Mark Adler 已提交
413 414
#ifdef NO_vsnprintf
#  ifdef HAS_vsprintf_void
415 416 417
    (void)vsprintf(next, format, va);
    for (len = 0; len < state->size; len++)
        if (next[len] == 0) break;
M
Mark Adler 已提交
418
#  else
419
    len = vsprintf(next, format, va);
M
Mark Adler 已提交
420 421 422
#  endif
#else
#  ifdef HAS_vsnprintf_void
423 424
    (void)vsnprintf(next, state->size, format, va);
    len = strlen(next);
M
Mark Adler 已提交
425
#  else
426
    len = vsnprintf(next, state->size, format, va);
M
Mark Adler 已提交
427 428 429 430
#  endif
#endif

    /* check that printf() results fit in buffer */
M
Mark Adler 已提交
431
    if (len == 0 || (unsigned)len >= state->size || next[state->size - 1] != 0)
M
Mark Adler 已提交
432 433
        return 0;

434
    /* update buffer and position, compress first half if past that */
M
Mark Adler 已提交
435
    strm->avail_in += (unsigned)len;
436
    state->x.pos += len;
437 438 439 440
    if (strm->avail_in >= state->size) {
        left = strm->avail_in - state->size;
        strm->avail_in = state->size;
        if (gz_comp(state, Z_NO_FLUSH) == -1)
M
Mark Adler 已提交
441
            return state->err;
442 443 444 445
        memcpy(state->in, state->in + state->size, left);
        strm->next_in = state->in;
        strm->avail_in = left;
    }
M
Mark Adler 已提交
446
    return len;
M
Mark Adler 已提交
447 448
}

449 450 451 452 453 454 455 456 457 458 459
int ZEXPORTVA gzprintf(gzFile file, const char *format, ...)
{
    va_list va;
    int ret;

    va_start(va, format);
    ret = gzvprintf(file, format, va);
    va_end(va);
    return ret;
}

M
Mark Adler 已提交
460
#else /* !STDC && !Z_HAVE_STDARG_H */
M
Mark Adler 已提交
461 462 463 464 465 466 467 468 469

/* -- see zlib.h -- */
int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
                       a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
    gzFile file;
    const char *format;
    int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
        a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
{
470 471
    unsigned len, left;
    char *next;
M
Mark Adler 已提交
472 473 474 475 476
    gz_statep state;
    z_streamp strm;

    /* get internal structure */
    if (file == NULL)
M
Mark Adler 已提交
477
        return Z_STREAM_ERROR;
M
Mark Adler 已提交
478 479 480
    state = (gz_statep)file;
    strm = &(state->strm);

M
Mark Adler 已提交
481 482
    /* check that can really pass pointer in ints */
    if (sizeof(int) != sizeof(void *))
M
Mark Adler 已提交
483
        return Z_STREAM_ERROR;
M
Mark Adler 已提交
484

M
Mark Adler 已提交
485 486
    /* check that we're writing and that there's no error */
    if (state->mode != GZ_WRITE || state->err != Z_OK)
M
Mark Adler 已提交
487
        return Z_STREAM_ERROR;
M
Mark Adler 已提交
488 489 490

    /* make sure we have some buffer space */
    if (state->size == 0 && gz_init(state) == -1)
M
Mark Adler 已提交
491
        return state->error;
M
Mark Adler 已提交
492 493 494 495 496

    /* check for seek request */
    if (state->seek) {
        state->seek = 0;
        if (gz_zero(state, state->skip) == -1)
M
Mark Adler 已提交
497
            return state->error;
M
Mark Adler 已提交
498 499
    }

500 501 502 503 504 505 506
    /* do the printf() into the input buffer, put length in len -- the input
       buffer is double-sized just for this function, so there is guaranteed to
       be state->size bytes available after the current contents */
    if (strm->avail_in == 0)
        strm->next_in = state->in;
    next = (char *)(strm->next_in + strm->avail_in);
    next[state->size - 1] = 0;
M
Mark Adler 已提交
507 508
#ifdef NO_snprintf
#  ifdef HAS_sprintf_void
509 510
    sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12,
            a13, a14, a15, a16, a17, a18, a19, a20);
M
Mark Adler 已提交
511
    for (len = 0; len < size; len++)
512 513
        if (next[len] == 0)
            break;
M
Mark Adler 已提交
514
#  else
515 516
    len = sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11,
                  a12, a13, a14, a15, a16, a17, a18, a19, a20);
M
Mark Adler 已提交
517 518 519
#  endif
#else
#  ifdef HAS_snprintf_void
520 521 522
    snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8, a9,
             a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
    len = strlen(next);
M
Mark Adler 已提交
523
#  else
524 525
    len = snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8,
                   a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
M
Mark Adler 已提交
526 527 528 529
#  endif
#endif

    /* check that printf() results fit in buffer */
530
    if (len == 0 || len >= state->size || next[state->size - 1] != 0)
M
Mark Adler 已提交
531 532
        return 0;

533 534
    /* update buffer and position, compress first half if past that */
    strm->avail_in += len;
535
    state->x.pos += len;
536 537 538 539
    if (strm->avail_in >= state->size) {
        left = strm->avail_in - state->size;
        strm->avail_in = state->size;
        if (gz_comp(state, Z_NO_FLUSH) == -1)
M
Mark Adler 已提交
540
            return state->err;
541 542 543 544 545
        memcpy(state->in, state->in + state->size, left);
        strm->next_in = state->in;
        strm->avail_in = left;
    }
    return (int)len;
M
Mark Adler 已提交
546 547 548 549 550 551 552 553 554 555 556 557 558
}

#endif

/* -- see zlib.h -- */
int ZEXPORT gzflush(file, flush)
    gzFile file;
    int flush;
{
    gz_statep state;

    /* get internal structure */
    if (file == NULL)
M
Mark Adler 已提交
559
        return Z_STREAM_ERROR;
M
Mark Adler 已提交
560 561 562
    state = (gz_statep)file;

    /* check that we're writing and that there's no error */
M
Mark Adler 已提交
563 564
    if (state->mode != GZ_WRITE || state->err != Z_OK)
        return Z_STREAM_ERROR;
M
Mark Adler 已提交
565 566 567 568 569 570 571 572 573

    /* check flush parameter */
    if (flush < 0 || flush > Z_FINISH)
        return Z_STREAM_ERROR;

    /* check for seek request */
    if (state->seek) {
        state->seek = 0;
        if (gz_zero(state, state->skip) == -1)
M
Mark Adler 已提交
574
            return state->err;
M
Mark Adler 已提交
575 576 577
    }

    /* compress remaining data with requested flush */
578
    (void)gz_comp(state, flush);
M
Mark Adler 已提交
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
    return state->err;
}

/* -- see zlib.h -- */
int ZEXPORT gzsetparams(file, level, strategy)
    gzFile file;
    int level;
    int strategy;
{
    gz_statep state;
    z_streamp strm;

    /* get internal structure */
    if (file == NULL)
        return Z_STREAM_ERROR;
    state = (gz_statep)file;
    strm = &(state->strm);

    /* check that we're writing and that there's no error */
    if (state->mode != GZ_WRITE || state->err != Z_OK)
        return Z_STREAM_ERROR;

    /* if no change is requested, then do nothing */
    if (level == state->level && strategy == state->strategy)
        return Z_OK;

    /* check for seek request */
    if (state->seek) {
        state->seek = 0;
        if (gz_zero(state, state->skip) == -1)
M
Mark Adler 已提交
609
            return state->err;
M
Mark Adler 已提交
610 611 612 613 614
    }

    /* change compression parameters for subsequent input */
    if (state->size) {
        /* flush previous input with previous parameters before changing */
615
        if (strm->avail_in && gz_comp(state, Z_BLOCK) == -1)
M
Mark Adler 已提交
616 617 618 619 620 621 622 623 624 625 626 627
            return state->err;
        deflateParams(strm, level, strategy);
    }
    state->level = level;
    state->strategy = strategy;
    return Z_OK;
}

/* -- see zlib.h -- */
int ZEXPORT gzclose_w(file)
    gzFile file;
{
628
    int ret = Z_OK;
M
Mark Adler 已提交
629 630 631 632 633 634 635 636 637 638 639 640 641 642
    gz_statep state;

    /* get internal structure */
    if (file == NULL)
        return Z_STREAM_ERROR;
    state = (gz_statep)file;

    /* check that we're writing */
    if (state->mode != GZ_WRITE)
        return Z_STREAM_ERROR;

    /* check for seek request */
    if (state->seek) {
        state->seek = 0;
643 644
        if (gz_zero(state, state->skip) == -1)
            ret = state->err;
M
Mark Adler 已提交
645 646 647
    }

    /* flush, free memory, and close file */
648 649
    if (gz_comp(state, Z_FINISH) == -1)
        ret = state->err;
650 651 652 653 654
    if (state->size) {
        if (!state->direct) {
            (void)deflateEnd(&(state->strm));
            free(state->out);
        }
655
        free(state->in);
656
    }
M
Mark Adler 已提交
657
    gz_error(state, Z_OK, NULL);
M
Mark Adler 已提交
658
    free(state->path);
659 660
    if (close(state->fd) == -1)
        ret = Z_ERRNO;
M
Mark Adler 已提交
661
    free(state);
662
    return ret;
M
Mark Adler 已提交
663
}