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