gzwrite.c 17.1 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));
M
Mark Adler 已提交
12 13

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

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

29 30 31
    /* only need output buffer and deflate state if compressing */
    if (!state->direct) {
        /* allocate output buffer */
32
        state->out = (unsigned char *)malloc(state->want);
33 34 35 36 37 38 39 40 41 42 43
        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,
44
                           MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
45 46 47 48 49 50
        if (ret != Z_OK) {
            free(state->out);
            free(state->in);
            gz_error(state, Z_MEM_ERROR, "out of memory");
            return -1;
        }
51
        strm->next_in = NULL;
M
Mark Adler 已提交
52 53 54 55 56
    }

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

57 58 59 60 61 62
    /* 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 已提交
63 64 65 66
    return 0;
}

/* Compress whatever is at avail_in and next_in and write to the output file.
M
Mark Adler 已提交
67 68 69 70 71
   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 已提交
72 73 74 75
local int gz_comp(state, flush)
    gz_statep state;
    int flush;
{
M
Mark Adler 已提交
76 77
    int ret;
    ssize_t got;
M
Mark Adler 已提交
78 79 80 81 82 83 84
    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;

85 86
    /* write directly if requested */
    if (state->direct) {
87 88 89 90 91 92
        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 已提交
93
            strm->avail_in -= (unsigned)got;
94
            strm->next_in += got;
95 96 97 98
        }
        return 0;
    }

M
Mark Adler 已提交
99 100 101 102 103 104 105
    /* 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))) {
106 107
            while (strm->next_out > state->x.next) {
                got = write(state->fd, state->x.next,
M
Mark Adler 已提交
108
                            (unsigned long)(strm->next_out - state->x.next));
109 110 111 112 113
                if (got < 0) {
                    gz_error(state, Z_ERRNO, zstrerror());
                    return -1;
                }
                state->x.next += got;
M
Mark Adler 已提交
114 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
            }
            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 已提交
140 141
/* 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 已提交
142 143
local int gz_zero(state, len)
    gz_statep state;
M
Mark Adler 已提交
144
    z_off64_t len;
M
Mark Adler 已提交
145 146 147 148 149 150 151 152 153
{
    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 已提交
154
    /* compress len zeros (len guaranteed > 0) */
M
Mark Adler 已提交
155 156
    first = 1;
    while (len) {
M
Mark Adler 已提交
157 158
        n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
            (unsigned)len : state->size;
M
Mark Adler 已提交
159 160 161 162 163 164
        if (first) {
            memset(state->in, 0, n);
            first = 0;
        }
        strm->avail_in = n;
        strm->next_in = state->in;
165
        state->x.pos += n;
M
Mark Adler 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
        if (gz_comp(state, Z_NO_FLUSH) == -1)
            return -1;
        len -= n;
    }
    return 0;
}

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

    /* get internal structure */
    if (file == NULL)
M
Mark Adler 已提交
185
        return 0;
M
Mark Adler 已提交
186 187 188 189 190
    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 已提交
191
        return 0;
M
Mark Adler 已提交
192

M
Mark Adler 已提交
193 194 195
    /* since an int is returned, make sure len fits in one, otherwise return
       with an error (this avoids the flaw in the interface) */
    if ((int)len < 0) {
196
        gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
M
Mark Adler 已提交
197
        return 0;
M
Mark Adler 已提交
198 199 200 201 202 203
    }

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

M
Mark Adler 已提交
204 205
    /* allocate memory if this is the first time through */
    if (state->size == 0 && gz_init(state) == -1)
M
Mark Adler 已提交
206
        return 0;
M
Mark Adler 已提交
207 208 209 210 211

    /* check for seek request */
    if (state->seek) {
        state->seek = 0;
        if (gz_zero(state, state->skip) == -1)
M
Mark Adler 已提交
212
            return 0;
M
Mark Adler 已提交
213 214 215 216 217
    }

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

M
Mark Adler 已提交
221 222
            if (strm->avail_in == 0)
                strm->next_in = state->in;
223
            have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
224 225 226 227 228 229 230 231
            copy = state->size - have;
            if (copy > len)
                copy = len;
            memcpy(state->in + have, buf, copy);
            strm->avail_in += copy;
            state->x.pos += copy;
            buf = (const char *)buf + copy;
            len -= copy;
M
Mark Adler 已提交
232
            if (len && gz_comp(state, Z_NO_FLUSH) == -1)
M
Mark Adler 已提交
233
                return 0;
M
Mark Adler 已提交
234
        } while (len);
M
Mark Adler 已提交
235 236 237 238
    }
    else {
        /* consume whatever's left in the input buffer */
        if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
M
Mark Adler 已提交
239
            return 0;
M
Mark Adler 已提交
240 241 242

        /* directly compress user buffer to file */
        strm->avail_in = len;
243
        strm->next_in = (z_const Bytef *)buf;
244
        state->x.pos += len;
M
Mark Adler 已提交
245
        if (gz_comp(state, Z_NO_FLUSH) == -1)
M
Mark Adler 已提交
246
            return 0;
M
Mark Adler 已提交
247 248
    }

M
Mark Adler 已提交
249
    /* input was all buffered or compressed (put will fit in int) */
M
Mark Adler 已提交
250 251 252 253 254 255 256 257
    return (int)put;
}

/* -- see zlib.h -- */
int ZEXPORT gzputc(file, c)
    gzFile file;
    int c;
{
258
    unsigned have;
M
Mark Adler 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    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) */
282 283 284
    if (state->size) {
        if (strm->avail_in == 0)
            strm->next_in = state->in;
285
        have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
286
        if (have < state->size) {
M
Mark Adler 已提交
287
            state->in[have] = (unsigned char)c;
288 289 290 291
            strm->avail_in++;
            state->x.pos++;
            return c & 0xff;
        }
M
Mark Adler 已提交
292 293 294
    }

    /* no room in buffer or not initialized, use gz_write() */
M
Mark Adler 已提交
295
    buf[0] = (unsigned char)c;
M
Mark Adler 已提交
296 297
    if (gzwrite(file, buf, 1) != 1)
        return -1;
298
    return c & 0xff;
M
Mark Adler 已提交
299 300 301 302 303 304 305
}

/* -- see zlib.h -- */
int ZEXPORT gzputs(file, str)
    gzFile file;
    const char *str;
{
M
Mark Adler 已提交
306 307 308
    int ret;
    unsigned len;

M
Mark Adler 已提交
309
    /* write string */
M
Mark Adler 已提交
310
    len = (unsigned)strlen(str);
M
Mark Adler 已提交
311 312
    ret = gzwrite(file, str, len);
    return ret == 0 && len != 0 ? -1 : ret;
M
Mark Adler 已提交
313 314
}

M
Mark Adler 已提交
315
#if defined(STDC) || defined(Z_HAVE_STDARG_H)
M
Mark Adler 已提交
316 317 318
#include <stdarg.h>

/* -- see zlib.h -- */
319
int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
M
Mark Adler 已提交
320
{
M
Mark Adler 已提交
321 322
    int len;
    unsigned left;
323
    char *next;
M
Mark Adler 已提交
324 325 326 327 328
    gz_statep state;
    z_streamp strm;

    /* get internal structure */
    if (file == NULL)
M
Mark Adler 已提交
329
        return Z_STREAM_ERROR;
M
Mark Adler 已提交
330 331 332 333 334
    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 已提交
335
        return Z_STREAM_ERROR;
M
Mark Adler 已提交
336 337 338

    /* make sure we have some buffer space */
    if (state->size == 0 && gz_init(state) == -1)
M
Mark Adler 已提交
339
        return state->err;
M
Mark Adler 已提交
340 341 342 343 344

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

348 349 350 351 352
    /* 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 已提交
353
    next = (char *)(state->in + (strm->next_in - state->in) + strm->avail_in);
354
    next[state->size - 1] = 0;
M
Mark Adler 已提交
355 356
#ifdef NO_vsnprintf
#  ifdef HAS_vsprintf_void
357 358 359
    (void)vsprintf(next, format, va);
    for (len = 0; len < state->size; len++)
        if (next[len] == 0) break;
M
Mark Adler 已提交
360
#  else
361
    len = vsprintf(next, format, va);
M
Mark Adler 已提交
362 363 364
#  endif
#else
#  ifdef HAS_vsnprintf_void
365 366
    (void)vsnprintf(next, state->size, format, va);
    len = strlen(next);
M
Mark Adler 已提交
367
#  else
368
    len = vsnprintf(next, state->size, format, va);
M
Mark Adler 已提交
369 370 371 372
#  endif
#endif

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

376
    /* update buffer and position, compress first half if past that */
M
Mark Adler 已提交
377
    strm->avail_in += (unsigned)len;
378
    state->x.pos += len;
379 380 381 382
    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 已提交
383
            return state->err;
384 385 386 387
        memcpy(state->in, state->in + state->size, left);
        strm->next_in = state->in;
        strm->avail_in = left;
    }
M
Mark Adler 已提交
388
    return len;
M
Mark Adler 已提交
389 390
}

391 392 393 394 395 396 397 398 399 400 401
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 已提交
402
#else /* !STDC && !Z_HAVE_STDARG_H */
M
Mark Adler 已提交
403 404 405 406 407 408 409 410 411

/* -- 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;
{
412 413
    unsigned len, left;
    char *next;
M
Mark Adler 已提交
414 415 416 417 418
    gz_statep state;
    z_streamp strm;

    /* get internal structure */
    if (file == NULL)
M
Mark Adler 已提交
419
        return Z_STREAM_ERROR;
M
Mark Adler 已提交
420 421 422
    state = (gz_statep)file;
    strm = &(state->strm);

M
Mark Adler 已提交
423 424
    /* check that can really pass pointer in ints */
    if (sizeof(int) != sizeof(void *))
M
Mark Adler 已提交
425
        return Z_STREAM_ERROR;
M
Mark Adler 已提交
426

M
Mark Adler 已提交
427 428
    /* check that we're writing and that there's no error */
    if (state->mode != GZ_WRITE || state->err != Z_OK)
M
Mark Adler 已提交
429
        return Z_STREAM_ERROR;
M
Mark Adler 已提交
430 431 432

    /* make sure we have some buffer space */
    if (state->size == 0 && gz_init(state) == -1)
M
Mark Adler 已提交
433
        return state->error;
M
Mark Adler 已提交
434 435 436 437 438

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

442 443 444 445 446 447 448
    /* 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 已提交
449 450
#ifdef NO_snprintf
#  ifdef HAS_sprintf_void
451 452
    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 已提交
453
    for (len = 0; len < size; len++)
454 455
        if (next[len] == 0)
            break;
M
Mark Adler 已提交
456
#  else
457 458
    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 已提交
459 460 461
#  endif
#else
#  ifdef HAS_snprintf_void
462 463 464
    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 已提交
465
#  else
466 467
    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 已提交
468 469 470 471
#  endif
#endif

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

475 476
    /* update buffer and position, compress first half if past that */
    strm->avail_in += len;
477
    state->x.pos += len;
478 479 480 481
    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 已提交
482
            return state->err;
483 484 485 486 487
        memcpy(state->in, state->in + state->size, left);
        strm->next_in = state->in;
        strm->avail_in = left;
    }
    return (int)len;
M
Mark Adler 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500
}

#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 已提交
501
        return Z_STREAM_ERROR;
M
Mark Adler 已提交
502 503 504
    state = (gz_statep)file;

    /* check that we're writing and that there's no error */
M
Mark Adler 已提交
505 506
    if (state->mode != GZ_WRITE || state->err != Z_OK)
        return Z_STREAM_ERROR;
M
Mark Adler 已提交
507 508 509 510 511 512 513 514 515

    /* 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 已提交
516
            return state->err;
M
Mark Adler 已提交
517 518 519
    }

    /* compress remaining data with requested flush */
520
    (void)gz_comp(state, flush);
M
Mark Adler 已提交
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
    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 已提交
551
            return state->err;
M
Mark Adler 已提交
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
    }

    /* change compression parameters for subsequent input */
    if (state->size) {
        /* flush previous input with previous parameters before changing */
        if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1)
            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;
{
570
    int ret = Z_OK;
M
Mark Adler 已提交
571 572 573 574 575 576 577 578 579 580 581 582 583 584
    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;
585 586
        if (gz_zero(state, state->skip) == -1)
            ret = state->err;
M
Mark Adler 已提交
587 588 589
    }

    /* flush, free memory, and close file */
590 591
    if (gz_comp(state, Z_FINISH) == -1)
        ret = state->err;
592 593 594 595 596
    if (state->size) {
        if (!state->direct) {
            (void)deflateEnd(&(state->strm));
            free(state->out);
        }
597
        free(state->in);
598
    }
M
Mark Adler 已提交
599
    gz_error(state, Z_OK, NULL);
M
Mark Adler 已提交
600
    free(state->path);
601 602
    if (close(state->fd) == -1)
        ret = Z_ERRNO;
M
Mark Adler 已提交
603
    free(state);
604
    return ret;
M
Mark Adler 已提交
605
}