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