Compress whatever is at avail_in and next_in and write to the output file. Return -1 if there is an error writing to the output file or if gz_init() fails to allocate memory, otherwise 0. flush is assumed to be a valid deflate() flush value. If flush is Z_FINISH, then the deflate() state is reset to start a new gzip stream. If gz->direct is true, then simply write to the output f
(state, flush)
| 76 | reset to start a new gzip stream. If gz->direct is true, then simply write |
| 77 | to the output file without compressing, and ignore flush. */ |
| 78 | local int gz_comp(state, flush) |
| 79 | gz_statep state; |
| 80 | int flush; |
| 81 | { |
| 82 | int ret, writ; |
| 83 | unsigned have, put, max = ((unsigned)-1 >> 2) + 1; |
| 84 | z_streamp strm = &(state.state->strm); |
| 85 | |
| 86 | /* allocate memory if this is the first time through */ |
| 87 | if (state.state->size == 0 && gz_init(state) == -1) |
| 88 | return -1; |
| 89 | |
| 90 | /* write directly if requested */ |
| 91 | if (state.state->direct) { |
| 92 | while (strm->avail_in) { |
| 93 | put = strm->avail_in > max ? max : strm->avail_in; |
| 94 | writ = (int)write(state.state->fd, strm->next_in, put); |
| 95 | if (writ < 0) { |
| 96 | gz_error(state, Z_ERRNO, zstrerror()); |
| 97 | return -1; |
| 98 | } |
| 99 | strm->avail_in -= (unsigned)writ; |
| 100 | strm->next_in += writ; |
| 101 | } |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | /* run deflate() on provided input until it produces no more output */ |
| 106 | ret = Z_OK; |
| 107 | do { |
| 108 | /* write out current buffer contents if full, or if flushing, but if |
| 109 | doing Z_FINISH then don't write until we get to Z_STREAM_END */ |
| 110 | if (strm->avail_out == 0 || (flush != Z_NO_FLUSH && |
| 111 | (flush != Z_FINISH || ret == Z_STREAM_END))) { |
| 112 | while (strm->next_out > state.state->x.next) { |
| 113 | put = strm->next_out - state.state->x.next > (int)max ? max : |
| 114 | (unsigned)(strm->next_out - state.state->x.next); |
| 115 | writ = (int)write(state.state->fd, state.state->x.next, put); |
| 116 | if (writ < 0) { |
| 117 | gz_error(state, Z_ERRNO, zstrerror()); |
| 118 | return -1; |
| 119 | } |
| 120 | state.state->x.next += writ; |
| 121 | } |
| 122 | if (strm->avail_out == 0) { |
| 123 | strm->avail_out = state.state->size; |
| 124 | strm->next_out = state.state->out; |
| 125 | state.state->x.next = state.state->out; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /* compress */ |
| 130 | have = strm->avail_out; |
| 131 | ret = deflate(strm, flush); |
| 132 | if (ret == Z_STREAM_ERROR) { |
| 133 | gz_error(state, Z_STREAM_ERROR, |
| 134 | "internal error: deflate stream corrupt"); |
| 135 | return -1; |
no test coverage detected