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