Write len bytes from buf to file. Return the number of bytes written. If the returned value is less than len, then there was an error. */
(state, buf, len)
| 181 | /* Write len bytes from buf to file. Return the number of bytes written. If |
| 182 | the returned value is less than len, then there was an error. */ |
| 183 | local z_size_t gz_write(state, buf, len) |
| 184 | gz_statep state; |
| 185 | voidpc buf; |
| 186 | z_size_t len; |
| 187 | { |
| 188 | z_size_t put = len; |
| 189 | |
| 190 | /* if len is zero, avoid unnecessary operations */ |
| 191 | if (len == 0) |
| 192 | return 0; |
| 193 | |
| 194 | /* allocate memory if this is the first time through */ |
| 195 | if (state.state->size == 0 && gz_init(state) == -1) |
| 196 | return 0; |
| 197 | |
| 198 | /* check for seek request */ |
| 199 | if (state.state->seek) { |
| 200 | state.state->seek = 0; |
| 201 | if (gz_zero(state, state.state->skip) == -1) |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | /* for small len, copy to input buffer, otherwise compress directly */ |
| 206 | if (len < state.state->size) { |
| 207 | /* copy to input buffer, compress when full */ |
| 208 | do { |
| 209 | z_size_t have, copy; |
| 210 | |
| 211 | if (state.state->strm.avail_in == 0) |
| 212 | state.state->strm.next_in = state.state->in; |
| 213 | have = (unsigned)((state.state->strm.next_in + state.state->strm.avail_in) - |
| 214 | state.state->in); |
| 215 | copy = state.state->size - have; |
| 216 | if (copy > len) |
| 217 | copy = len; |
| 218 | memcpy(state.state->in + have, buf, copy); |
| 219 | state.state->strm.avail_in += copy; |
| 220 | state.state->x.pos += copy; |
| 221 | buf = (const char *)buf + copy; |
| 222 | len -= copy; |
| 223 | if (len && gz_comp(state, Z_NO_FLUSH) == -1) |
| 224 | return 0; |
| 225 | } while (len); |
| 226 | } |
| 227 | else { |
| 228 | /* consume whatever's left in the input buffer */ |
| 229 | if (state.state->strm.avail_in && gz_comp(state, Z_NO_FLUSH) == -1) |
| 230 | return 0; |
| 231 | |
| 232 | /* directly compress user buffer to file */ |
| 233 | state.state->strm.next_in = (z_const Bytef *)buf; |
| 234 | do { |
| 235 | z_size_t n = (unsigned)-1; |
| 236 | if (n > len) |
| 237 | n = len; |
| 238 | state.state->strm.avail_in = (z_uInt)n; |
| 239 | state.state->x.pos += n; |
| 240 | if (gz_comp(state, Z_NO_FLUSH) == -1) |