-- see zlib.h -- */
(file, buf, len)
| 163 | |
| 164 | /* -- see zlib.h -- */ |
| 165 | int ZEXPORT gzwrite(file, buf, len) |
| 166 | gzFile file; |
| 167 | voidpc buf; |
| 168 | unsigned len; |
| 169 | { |
| 170 | unsigned put = len; |
| 171 | gz_statep state; |
| 172 | z_streamp strm; |
| 173 | |
| 174 | /* get internal structure */ |
| 175 | if (file == NULL) |
| 176 | return 0; |
| 177 | state = (gz_statep)file; |
| 178 | strm = &(state->strm); |
| 179 | |
| 180 | /* check that we're writing and that there's no error */ |
| 181 | if (state->mode != GZ_WRITE || state->err != Z_OK) |
| 182 | return 0; |
| 183 | |
| 184 | /* since an int is returned, make sure len fits in one, otherwise return |
| 185 | with an error (this avoids the flaw in the interface) */ |
| 186 | if ((int)len < 0) { |
| 187 | gz_error(state, Z_DATA_ERROR, "requested length does not fit in int"); |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | /* if len is zero, avoid unnecessary operations */ |
| 192 | if (len == 0) |
| 193 | return 0; |
| 194 | |
| 195 | /* allocate memory if this is the first time through */ |
| 196 | if (state->size == 0 && gz_init(state) == -1) |
| 197 | return 0; |
| 198 | |
| 199 | /* check for seek request */ |
| 200 | if (state->seek) { |
| 201 | state->seek = 0; |
| 202 | if (gz_zero(state, state->skip) == -1) |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | /* for small len, copy to input buffer, otherwise compress directly */ |
| 207 | if (len < state->size) { |
| 208 | /* copy to input buffer, compress when full */ |
| 209 | do { |
| 210 | unsigned have, copy; |
| 211 | |
| 212 | if (strm->avail_in == 0) |
| 213 | strm->next_in = state->in; |
| 214 | have = (unsigned)((strm->next_in + strm->avail_in) - state->in); |
| 215 | copy = state->size - have; |
| 216 | if (copy > len) |
| 217 | copy = len; |
| 218 | memcpy(state->in + have, buf, copy); |
| 219 | strm->avail_in += copy; |
| 220 | state->x.pos += copy; |
| 221 | buf = (const char *)buf + copy; |
| 222 | len -= copy; |