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