Initialize state for writing a gzip file. Mark initialization by setting state->size to non-zero. Return -1 on failure or 0 on success. */
(state)
| 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 = (unsigned char *)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 = (unsigned char *)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. |