gzwrite.c 15.8 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 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 14 15 16 17 18 19 20

/* Initialize state for writing a gzip file.  Mark initialization by setting
   state->size to non-zero.  Return -1 on failure or 0 on success. */
local int gz_init(state)
    gz_statep state;
{
    int ret;
    z_streamp strm = &(state->strm);

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

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

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

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

/* Compress whatever is at avail_in and next_in and write to the output file.
   Return -1 if there is an error writing to the output file, otherwise 0.
   flush is assumed to be a valid deflate() flush value.  If flush is Z_FINISH,
67 68 69
   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 已提交
70 71 72 73
local int gz_comp(state, flush)
    gz_statep state;
    int flush;
{
M
Mark Adler 已提交
74
    int ret, got;
M
Mark Adler 已提交
75 76 77 78 79 80 81
    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;

82 83 84 85 86 87 88 89 90 91 92
    /* write directly if requested */
    if (state->direct) {
        got = write(state->fd, strm->next_in, strm->avail_in);
        if (got < 0 || (unsigned)got != strm->avail_in) {
            gz_error(state, Z_ERRNO, zstrerror());
            return -1;
        }
        strm->avail_in = 0;
        return 0;
    }

M
Mark Adler 已提交
93 94 95 96 97 98 99
    /* 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))) {
100 101
            have = (unsigned)(strm->next_out - state->x.next);
            if (have && ((got = write(state->fd, state->x.next, have)) < 0 ||
M
Mark Adler 已提交
102
                         (unsigned)got != have)) {
M
Mark Adler 已提交
103 104 105 106 107 108 109
                gz_error(state, Z_ERRNO, zstrerror());
                return -1;
            }
            if (strm->avail_out == 0) {
                strm->avail_out = state->size;
                strm->next_out = state->out;
            }
110
            state->x.next = strm->next_out;
M
Mark Adler 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
        }

        /* 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;
}

/* Compress len zeros to output.  Return -1 on error, 0 on success. */
local int gz_zero(state, len)
    gz_statep state;
M
Mark Adler 已提交
135
    z_off64_t len;
M
Mark Adler 已提交
136 137 138 139 140 141 142 143 144
{
    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 已提交
145
    /* compress len zeros (len guaranteed > 0) */
M
Mark Adler 已提交
146 147
    first = 1;
    while (len) {
M
Mark Adler 已提交
148 149
        n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
            (unsigned)len : state->size;
M
Mark Adler 已提交
150 151 152 153 154 155
        if (first) {
            memset(state->in, 0, n);
            first = 0;
        }
        strm->avail_in = n;
        strm->next_in = state->in;
156
        state->x.pos += n;
M
Mark Adler 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
        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;
    unsigned n;
    gz_statep state;
    z_streamp strm;

    /* get internal structure */
    if (file == NULL)
M
Mark Adler 已提交
177
        return 0;
M
Mark Adler 已提交
178 179 180 181 182
    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 已提交
183
        return 0;
M
Mark Adler 已提交
184

M
Mark Adler 已提交
185 186 187 188
    /* 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) {
        gz_error(state, Z_BUF_ERROR, "requested length does not fit in int");
M
Mark Adler 已提交
189
        return 0;
M
Mark Adler 已提交
190 191 192 193 194 195
    }

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

M
Mark Adler 已提交
196 197
    /* allocate memory if this is the first time through */
    if (state->size == 0 && gz_init(state) == -1)
M
Mark Adler 已提交
198
        return 0;
M
Mark Adler 已提交
199 200 201 202 203

    /* check for seek request */
    if (state->seek) {
        state->seek = 0;
        if (gz_zero(state, state->skip) == -1)
M
Mark Adler 已提交
204
            return 0;
M
Mark Adler 已提交
205 206 207 208 209
    }

    /* for small len, copy to input buffer, otherwise compress directly */
    if (len < state->size) {
        /* copy to input buffer, compress when full */
M
Mark Adler 已提交
210
        do {
M
Mark Adler 已提交
211 212 213 214 215 216 217
            if (strm->avail_in == 0)
                strm->next_in = state->in;
            n = state->size - strm->avail_in;
            if (n > len)
                n = len;
            memcpy(strm->next_in + strm->avail_in, buf, n);
            strm->avail_in += n;
218
            state->x.pos += n;
M
Mark Adler 已提交
219
            buf = (char *)buf + n;
M
Mark Adler 已提交
220 221
            len -= n;
            if (len && gz_comp(state, Z_NO_FLUSH) == -1)
M
Mark Adler 已提交
222
                return 0;
M
Mark Adler 已提交
223
        } while (len);
M
Mark Adler 已提交
224 225 226 227
    }
    else {
        /* consume whatever's left in the input buffer */
        if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
M
Mark Adler 已提交
228
            return 0;
M
Mark Adler 已提交
229 230 231 232

        /* directly compress user buffer to file */
        strm->avail_in = len;
        strm->next_in = (voidp)buf;
233
        state->x.pos += len;
M
Mark Adler 已提交
234
        if (gz_comp(state, Z_NO_FLUSH) == -1)
M
Mark Adler 已提交
235
            return 0;
M
Mark Adler 已提交
236 237
    }

M
Mark Adler 已提交
238
    /* input was all buffered or compressed (put will fit in int) */
M
Mark Adler 已提交
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
    return (int)put;
}

/* -- see zlib.h -- */
int ZEXPORT gzputc(file, c)
    gzFile file;
    int c;
{
    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) */
    if (strm->avail_in < state->size) {
        if (strm->avail_in == 0)
            strm->next_in = state->in;
        strm->next_in[strm->avail_in++] = c;
274
        state->x.pos++;
M
Mark Adler 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
        return c;
    }

    /* no room in buffer or not initialized, use gz_write() */
    buf[0] = c;
    if (gzwrite(file, buf, 1) != 1)
        return -1;
    return c;
}

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

M
Mark Adler 已提交
293
    /* write string */
M
Mark Adler 已提交
294
    len = (unsigned)strlen(str);
M
Mark Adler 已提交
295 296
    ret = gzwrite(file, str, len);
    return ret == 0 && len != 0 ? -1 : ret;
M
Mark Adler 已提交
297 298
}

M
Mark Adler 已提交
299
#if defined(STDC) || defined(Z_HAVE_STDARG_H)
M
Mark Adler 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
#include <stdarg.h>

/* -- see zlib.h -- */
int ZEXPORTVA gzprintf (gzFile file, const char *format, ...)
{
    int size, len;
    gz_statep state;
    z_streamp strm;
    va_list va;

    /* 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 0;

    /* make sure we have some buffer space */
    if (state->size == 0 && gz_init(state) == -1)
        return 0;

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

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

    /* do the printf() into the input buffer, put length in len */
    size = (int)(state->size);
    state->in[size - 1] = 0;
    va_start(va, format);
#ifdef NO_vsnprintf
#  ifdef HAS_vsprintf_void
    (void)vsprintf(state->in, format, va);
    va_end(va);
M
Mark Adler 已提交
343
    for (len = 0; len < size; len++)
M
Mark Adler 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
        if (state->in[len] == 0) break;
#  else
    len = vsprintf(state->in, format, va);
    va_end(va);
#  endif
#else
#  ifdef HAS_vsnprintf_void
    (void)vsnprintf(state->in, size, format, va);
    va_end(va);
    len = strlen(state->in);
#  else
    len = vsnprintf((char *)(state->in), size, format, va);
    va_end(va);
#  endif
#endif

    /* check that printf() results fit in buffer */
    if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
        return 0;

M
Mark Adler 已提交
364
    /* update buffer and position, defer compression until needed */
M
Mark Adler 已提交
365 366
    strm->avail_in = (unsigned)len;
    strm->next_in = state->in;
367
    state->x.pos += len;
M
Mark Adler 已提交
368 369 370
    return len;
}

M
Mark Adler 已提交
371
#else /* !STDC && !Z_HAVE_STDARG_H */
M
Mark Adler 已提交
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390

/* -- 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;
{
    int size, len;
    gz_statep state;
    z_streamp strm;

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

M
Mark Adler 已提交
391 392 393 394
    /* check that can really pass pointer in ints */
    if (sizeof(int) != sizeof(void *))
        return 0;

M
Mark Adler 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
    /* check that we're writing and that there's no error */
    if (state->mode != GZ_WRITE || state->err != Z_OK)
        return 0;

    /* make sure we have some buffer space */
    if (state->size == 0 && gz_init(state) == -1)
        return 0;

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

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

    /* do the printf() into the input buffer, put length in len */
    size = (int)(state->size);
    state->in[size - 1] = 0;
#ifdef NO_snprintf
#  ifdef HAS_sprintf_void
    sprintf(state->in, format, a1, a2, a3, a4, a5, a6, a7, a8,
            a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
    for (len = 0; len < size; len++)
        if (state->in[len] == 0) break;
#  else
    len = sprintf(state->in, format, a1, a2, a3, a4, a5, a6, a7, a8,
                a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
#  endif
#else
#  ifdef HAS_snprintf_void
    snprintf(state->in, size, format, a1, a2, a3, a4, a5, a6, a7, a8,
             a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
    len = strlen(state->in);
#  else
    len = snprintf(state->in, size, format, a1, a2, a3, a4, a5, a6, a7, a8,
                 a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
#  endif
#endif

    /* check that printf() results fit in buffer */
    if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
        return 0;

M
Mark Adler 已提交
442
    /* update buffer and position, defer compression until needed */
M
Mark Adler 已提交
443 444
    strm->avail_in = (unsigned)len;
    strm->next_in = state->in;
445
    state->x.pos += len;
M
Mark Adler 已提交
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
    return len;
}

#endif

/* -- see zlib.h -- */
int ZEXPORT gzflush(file, flush)
    gzFile file;
    int flush;
{
    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 */
M
Mark Adler 已提交
464 465
    if (state->mode != GZ_WRITE || state->err != Z_OK)
        return Z_STREAM_ERROR;
M
Mark Adler 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528

    /* 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)
            return -1;
    }

    /* compress remaining data with requested flush */
    gz_comp(state, flush);
    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)
            return -1;
    }

    /* 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;
{
529
    int ret = Z_OK;
M
Mark Adler 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543
    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;
544 545
        if (gz_zero(state, state->skip) == -1)
            ret = state->err;
M
Mark Adler 已提交
546 547 548
    }

    /* flush, free memory, and close file */
549 550
    if (gz_comp(state, Z_FINISH) == -1)
        ret = state->err;
551 552 553 554
    if (!state->direct) {
        (void)deflateEnd(&(state->strm));
        free(state->out);
    }
M
Mark Adler 已提交
555 556
    free(state->in);
    gz_error(state, Z_OK, NULL);
M
Mark Adler 已提交
557
    free(state->path);
558 559
    if (close(state->fd) == -1)
        ret = Z_ERRNO;
M
Mark Adler 已提交
560
    free(state);
561
    return ret;
M
Mark Adler 已提交
562
}
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593

/* used by zlibVersion() to get the vsnprintf story from the horse's mouth */
unsigned long ZEXPORT gzflags()
{
    unsigned long flags = 0;
#if defined(STDC) || defined(Z_HAVE_STDARG_H)
#  ifdef NO_vsnprintf
    flags += 1L << 25;
#    ifdef HAS_vsprintf_void
    flags += 1L << 26;
#    endif
#  else
#    ifdef HAS_vsnprintf_void
    flags += 1L << 26;
#    endif
#  endif
#else
    flags += 1L << 24;
#  ifdef NO_snprintf
    flags += 1L << 25;
#    ifdef HAS_sprintf_void
    flags += 1L << 26;
#    endif
#  else
#    ifdef HAS_snprintf_void
    flags += 1L << 26;
#    endif
#  endif
#endif
    return flags;
}