Initialize state for writing a gzip file. Mark initialization by setting state.state->size to non-zero. Return -1 on a memory allocation failure, or 0 on success. */
(state)
| 20 | state.state->size to non-zero. Return -1 on a memory allocation failure, or 0 on |
| 21 | success. */ |
| 22 | local int gz_init(state) |
| 23 | gz_statep state; |
| 24 | { |
| 25 | int ret; |
| 26 | z_streamp strm = &(state.state->strm); |
| 27 | |
| 28 | /* allocate input buffer (double size for gzprintf) */ |
| 29 | state.state->in = (unsigned char*)malloc(state.state->want << 1); |
| 30 | if (state.state->in == NULL) { |
| 31 | gz_error(state, Z_MEM_ERROR, "out of memory"); |
| 32 | return -1; |
| 33 | } |
| 34 | |
| 35 | /* only need output buffer and deflate state if compressing */ |
| 36 | if (!state.state->direct) { |
| 37 | /* allocate output buffer */ |
| 38 | state.state->out = (unsigned char*)malloc(state.state->want); |
| 39 | if (state.state->out == NULL) { |
| 40 | free(state.state->in); |
| 41 | gz_error(state, Z_MEM_ERROR, "out of memory"); |
| 42 | return -1; |
| 43 | } |
| 44 | |
| 45 | /* allocate deflate memory, set up for gzip compression */ |
| 46 | strm->zalloc = Z_NULL; |
| 47 | strm->zfree = Z_NULL; |
| 48 | strm->opaque = Z_NULL; |
| 49 | ret = deflateInit2(strm, state.state->level, Z_DEFLATED, |
| 50 | MAX_WBITS + 16, DEF_MEM_LEVEL, state.state->strategy); |
| 51 | if (ret != Z_OK) { |
| 52 | free(state.state->out); |
| 53 | free(state.state->in); |
| 54 | gz_error(state, Z_MEM_ERROR, "out of memory"); |
| 55 | return -1; |
| 56 | } |
| 57 | strm->next_in = NULL; |
| 58 | } |
| 59 | |
| 60 | /* mark state as initialized */ |
| 61 | state.state->size = state.state->want; |
| 62 | |
| 63 | /* initialize write buffer if compressing */ |
| 64 | if (!state.state->direct) { |
| 65 | strm->avail_out = state.state->size; |
| 66 | strm->next_out = state.state->out; |
| 67 | state.state->x.next = strm->next_out; |
| 68 | } |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | /* Compress whatever is at avail_in and next_in and write to the output file. |
| 73 | Return -1 if there is an error writing to the output file or if gz_init() |