| 1 | /* gzwrite.c -- zlib functions for writing gzip files
|
|---|
| 2 | * Copyright (C) 2004, 2005, 2010, 2011, 2012 Mark Adler
|
|---|
| 3 | * For conditions of distribution and use, see copyright notice in zlib.h
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | #include "gzguts.h"
|
|---|
| 7 |
|
|---|
| 8 | /* Local functions */
|
|---|
| 9 | local int gz_init OF((gz_statep));
|
|---|
| 10 | local int gz_comp OF((gz_statep, int));
|
|---|
| 11 | local int gz_zero OF((gz_statep, z_off64_t));
|
|---|
| 12 |
|
|---|
| 13 | /* Initialize state for writing a gzip file. Mark initialization by setting
|
|---|
| 14 | state->size to non-zero. Return -1 on failure or 0 on success. */
|
|---|
| 15 | local int gz_init(state)
|
|---|
| 16 | gz_statep state;
|
|---|
| 17 | {
|
|---|
| 18 | int ret;
|
|---|
| 19 | z_streamp strm = &(state->strm);
|
|---|
| 20 |
|
|---|
| 21 | /* allocate input buffer */
|
|---|
| 22 | state->in = malloc(state->want);
|
|---|
| 23 | if (state->in == NULL) {
|
|---|
| 24 | gz_error(state, Z_MEM_ERROR, "out of memory");
|
|---|
| 25 | return -1;
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | /* only need output buffer and deflate state if compressing */
|
|---|
| 29 | if (!state->direct) {
|
|---|
| 30 | /* allocate output buffer */
|
|---|
| 31 | state->out = malloc(state->want);
|
|---|
| 32 | if (state->out == NULL) {
|
|---|
| 33 | free(state->in);
|
|---|
| 34 | gz_error(state, Z_MEM_ERROR, "out of memory");
|
|---|
| 35 | return -1;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | /* allocate deflate memory, set up for gzip compression */
|
|---|
| 39 | strm->zalloc = Z_NULL;
|
|---|
| 40 | strm->zfree = Z_NULL;
|
|---|
| 41 | strm->opaque = Z_NULL;
|
|---|
| 42 | ret = deflateInit2(strm, state->level, Z_DEFLATED,
|
|---|
| 43 | MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
|
|---|
| 44 | if (ret != Z_OK) {
|
|---|
| 45 | free(state->out);
|
|---|
| 46 | free(state->in);
|
|---|
| 47 | gz_error(state, Z_MEM_ERROR, "out of memory");
|
|---|
| 48 | return -1;
|
|---|
| 49 | }
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | /* mark state as initialized */
|
|---|
| 53 | state->size = state->want;
|
|---|
| 54 |
|
|---|
| 55 | /* initialize write buffer if compressing */
|
|---|
| 56 | if (!state->direct) {
|
|---|
| 57 | strm->avail_out = state->size;
|
|---|
| 58 | strm->next_out = state->out;
|
|---|
| 59 | state->x.next = strm->next_out;
|
|---|
| 60 | }
|
|---|
| 61 | return 0;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | /* Compress whatever is at avail_in and next_in and write to the output file.
|
|---|
| 65 | Return -1 if there is an error writing to the output file, otherwise 0.
|
|---|
| 66 | flush is assumed to be a valid deflate() flush value. If flush is Z_FINISH,
|
|---|
| 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
|
|---|
| 69 | ignore flush. */
|
|---|
| 70 | local int gz_comp(state, flush)
|
|---|
| 71 | gz_statep state;
|
|---|
| 72 | int flush;
|
|---|
| 73 | {
|
|---|
| 74 | int ret, got;
|
|---|
| 75 | unsigned have;
|
|---|
| 76 | z_streamp strm = &(state->strm);
|
|---|
| 77 |
|
|---|
| 78 | /* allocate memory if this is the first time through */
|
|---|
| 79 | if (state->size == 0 && gz_init(state) == -1)
|
|---|
| 80 | return -1;
|
|---|
| 81 |
|
|---|
| 82 | /* write directly if requested */
|
|---|
| 83 | if (state->direct) {
|
|---|
| 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());
|
|---|
| 87 | return -1;
|
|---|
| 88 | }
|
|---|
| 89 | strm->avail_in = 0;
|
|---|
| 90 | return 0;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /* run deflate() on provided input until it produces no more output */
|
|---|
| 94 | ret = Z_OK;
|
|---|
| 95 | do {
|
|---|
| 96 | /* write out current buffer contents if full, or if flushing, but if
|
|---|
| 97 | doing Z_FINISH then don't write until we get to Z_STREAM_END */
|
|---|
| 98 | if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
|
|---|
| 99 | (flush != Z_FINISH || ret == Z_STREAM_END))) {
|
|---|
| 100 | have = (unsigned)(strm->next_out - state->x.next);
|
|---|
| 101 | if (have && ((got = write(state->fd, state->x.next, have)) < 0 ||
|
|---|
| 102 | (unsigned)got != have)) {
|
|---|
| 103 | gz_error(state, Z_ERRNO, zstrerror());
|
|---|
| 104 | return -1;
|
|---|
| 105 | }
|
|---|
| 106 | if (strm->avail_out == 0) {
|
|---|
| 107 | strm->avail_out = state->size;
|
|---|
| 108 | strm->next_out = state->out;
|
|---|
| 109 | }
|
|---|
| 110 | state->x.next = strm->next_out;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /* compress */
|
|---|
| 114 | have = strm->avail_out;
|
|---|
| 115 | ret = deflate(strm, flush);
|
|---|
| 116 | if (ret == Z_STREAM_ERROR) {
|
|---|
| 117 | gz_error(state, Z_STREAM_ERROR,
|
|---|
| 118 | "internal error: deflate stream corrupt");
|
|---|
| 119 | return -1;
|
|---|
| 120 | }
|
|---|
| 121 | have -= strm->avail_out;
|
|---|
| 122 | } while (have);
|
|---|
| 123 |
|
|---|
| 124 | /* if that completed a deflate stream, allow another to start */
|
|---|
| 125 | if (flush == Z_FINISH)
|
|---|
| 126 | deflateReset(strm);
|
|---|
| 127 |
|
|---|
| 128 | /* all done, no errors */
|
|---|
|
|---|