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
| 63 | reset to start a new gzip stream. If gz->direct is true, then simply write |
| 64 | to the output file without compressing, and ignore flush. */ |
| 65 | local int gz_comp(gz_statep state, int flush) { |
| 66 | int ret, writ; |
| 67 | unsigned have, put, max = ((unsigned)-1 >> 2) + 1; |
| 68 | z_streamp strm = &(state->strm); |
| 69 | |
| 70 | /* allocate memory if this is the first time through */ |
| 71 | if (state->size == 0 && gz_init(state) == -1) |
| 72 | return -1; |
| 73 | |
| 74 | /* write directly if requested */ |
| 75 | if (state->direct) { |
| 76 | while (strm->avail_in) { |
| 77 | errno = 0; |
| 78 | state->again = 0; |
| 79 | put = strm->avail_in > max ? max : strm->avail_in; |
| 80 | writ = (int)write(state->fd, strm->next_in, put); |
| 81 | if (writ < 0) { |
| 82 | if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 83 | state->again = 1; |
| 84 | gz_error(state, Z_ERRNO, zstrerror()); |
| 85 | return -1; |
| 86 | } |
| 87 | strm->avail_in -= (unsigned)writ; |
| 88 | strm->next_in += writ; |
| 89 | } |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | /* check for a pending reset */ |
| 94 | if (state->reset) { |
| 95 | /* don't start a new gzip member unless there is data to write and |
| 96 | we're not flushing */ |
| 97 | if (strm->avail_in == 0 && flush == Z_NO_FLUSH) |
| 98 | return 0; |
| 99 | deflateReset(strm); |
| 100 | state->reset = 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 | errno = 0; |
| 112 | state->again = 0; |
| 113 | put = strm->next_out - state->x.next > (int)max ? max : |
| 114 | (unsigned)(strm->next_out - state->x.next); |
| 115 | writ = (int)write(state->fd, state->x.next, put); |
| 116 | if (writ < 0) { |
| 117 | if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 118 | state->again = 1; |
| 119 | gz_error(state, Z_ERRNO, zstrerror()); |
| 120 | return -1; |
| 121 | } |
| 122 | state->x.next += writ; |
no test coverage detected