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