aboutsummaryrefslogtreecommitdiff
path: root/gzwrite.c
diff options
context:
space:
mode:
authorMark Adler <madler@alumni.caltech.edu>2011-09-09 23:26:58 -0700
committerMark Adler <madler@alumni.caltech.edu>2011-09-09 23:26:58 -0700
commitdc5a43ebfadb6b775f6e64bfeb5a461c66acb394 (patch)
tree00e122ba59ad883db198dc6adf24d020cac924d3 /gzwrite.c
parentd004b047838a7e803818b4973a2e39e0ff8c1fa2 (diff)
downloadzlib-dc5a43ebfadb6b775f6e64bfeb5a461c66acb394.tar.gz
zlib-dc5a43ebfadb6b775f6e64bfeb5a461c66acb394.tar.bz2
zlib-dc5a43ebfadb6b775f6e64bfeb5a461c66acb394.zip
zlib 1.2.3.6v1.2.3.6
Diffstat (limited to 'gzwrite.c')
-rw-r--r--gzwrite.c36
1 files changed, 17 insertions, 19 deletions
diff --git a/gzwrite.c b/gzwrite.c
index 65a13c3..c7c033a 100644
--- a/gzwrite.c
+++ b/gzwrite.c
@@ -20,15 +20,6 @@ local int gz_init(state)
20 int ret; 20 int ret;
21 z_streamp strm = &(state->strm); 21 z_streamp strm = &(state->strm);
22 22
23 /* check version of zlib -- need 1.2.1 or later for gzip deflate() */
24#ifdef ZLIB_VERNUM
25 if (ZLIB_VERNUM < 0x1210)
26#endif
27 {
28 gz_error(state, Z_VERSION_ERROR, "need zlib 1.2.1 or later");
29 return -1;
30 }
31
32 /* allocate input and output buffers */ 23 /* allocate input and output buffers */
33 state->in = malloc(state->want); 24 state->in = malloc(state->want);
34 state->out = malloc(state->want); 25 state->out = malloc(state->want);
@@ -169,6 +160,17 @@ int ZEXPORT gzwrite(file, buf, len)
169 if (state->mode != GZ_WRITE || state->err != Z_OK) 160 if (state->mode != GZ_WRITE || state->err != Z_OK)
170 return -1; 161 return -1;
171 162
163 /* since an int is returned, make sure len fits in one, otherwise return
164 with an error (this avoids the flaw in the interface) */
165 if ((int)len < 0) {
166 gz_error(state, Z_BUF_ERROR, "requested length does not fit in int");
167 return -1;
168 }
169
170 /* if len is zero, avoid unnecessary operations */
171 if (len == 0)
172 return 0;
173
172 /* allocate memory if this is the first time through */ 174 /* allocate memory if this is the first time through */
173 if (state->size == 0 && gz_init(state) == -1) 175 if (state->size == 0 && gz_init(state) == -1)
174 return -1; 176 return -1;
@@ -183,7 +185,7 @@ int ZEXPORT gzwrite(file, buf, len)
183 /* for small len, copy to input buffer, otherwise compress directly */ 185 /* for small len, copy to input buffer, otherwise compress directly */
184 if (len < state->size) { 186 if (len < state->size) {
185 /* copy to input buffer, compress when full */ 187 /* copy to input buffer, compress when full */
186 while (len) { 188 do {
187 if (strm->avail_in == 0) 189 if (strm->avail_in == 0)
188 strm->next_in = state->in; 190 strm->next_in = state->in;
189 n = state->size - strm->avail_in; 191 n = state->size - strm->avail_in;
@@ -192,11 +194,11 @@ int ZEXPORT gzwrite(file, buf, len)
192 memcpy(strm->next_in + strm->avail_in, buf, n); 194 memcpy(strm->next_in + strm->avail_in, buf, n);
193 strm->avail_in += n; 195 strm->avail_in += n;
194 state->pos += n; 196 state->pos += n;
195 buf += n; 197 buf = (char *)buf + n;
196 len -= n; 198 len -= n;
197 if (len && gz_comp(state, Z_NO_FLUSH) == -1) 199 if (len && gz_comp(state, Z_NO_FLUSH) == -1)
198 return -1; 200 return -1;
199 } 201 } while (len);
200 } 202 }
201 else { 203 else {
202 /* consume whatever's left in the input buffer */ 204 /* consume whatever's left in the input buffer */
@@ -211,7 +213,7 @@ int ZEXPORT gzwrite(file, buf, len)
211 return -1; 213 return -1;
212 } 214 }
213 215
214 /* input was all buffered or compressed */ 216 /* input was all buffered or compressed (put will fit in int) */
215 return (int)put; 217 return (int)put;
216} 218}
217 219
@@ -332,12 +334,10 @@ int ZEXPORTVA gzprintf (gzFile file, const char *format, ...)
332 if (len <= 0 || len >= (int)size || state->in[size - 1] != 0) 334 if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
333 return 0; 335 return 0;
334 336
335 /* write out result of printf() */ 337 /* update buffer and position, defer compression until needed */
336 strm->avail_in = (unsigned)len; 338 strm->avail_in = (unsigned)len;
337 strm->next_in = state->in; 339 strm->next_in = state->in;
338 state->pos += len; 340 state->pos += len;
339 if (gz_comp(state, Z_NO_FLUSH) == -1)
340 return 0;
341 return len; 341 return len;
342} 342}
343 343
@@ -408,12 +408,10 @@ int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
408 if (len <= 0 || len >= (int)size || state->in[size - 1] != 0) 408 if (len <= 0 || len >= (int)size || state->in[size - 1] != 0)
409 return 0; 409 return 0;
410 410
411 /* write out result of printf() */ 411 /* update buffer and position, defer compression until needed */
412 strm->avail_in = (unsigned)len; 412 strm->avail_in = (unsigned)len;
413 strm->next_in = state->in; 413 strm->next_in = state->in;
414 state->pos += len; 414 state->pos += len;
415 if (gz_comp(state, Z_NO_FLUSH) == -1)
416 return 0;
417 return len; 415 return len;
418} 416}
419 417