aboutsummaryrefslogtreecommitdiff
path: root/gzwrite.c
diff options
context:
space:
mode:
authorMark Adler <madler@alumni.caltech.edu>2011-09-09 23:27:08 -0700
committerMark Adler <madler@alumni.caltech.edu>2011-09-09 23:27:08 -0700
commit7df877eccdd826e94df53215f65dee639428e83f (patch)
tree11ed5070798961e28a4c69d9272ecaada500abc3 /gzwrite.c
parentdc5a43ebfadb6b775f6e64bfeb5a461c66acb394 (diff)
downloadzlib-7df877eccdd826e94df53215f65dee639428e83f.tar.gz
zlib-7df877eccdd826e94df53215f65dee639428e83f.tar.bz2
zlib-7df877eccdd826e94df53215f65dee639428e83f.zip
zlib 1.2.3.7v1.2.3.7
Diffstat (limited to 'gzwrite.c')
-rw-r--r--gzwrite.c32
1 files changed, 18 insertions, 14 deletions
diff --git a/gzwrite.c b/gzwrite.c
index c7c033a..f4a0a80 100644
--- a/gzwrite.c
+++ b/gzwrite.c
@@ -152,19 +152,19 @@ int ZEXPORT gzwrite(file, buf, len)
152 152
153 /* get internal structure */ 153 /* get internal structure */
154 if (file == NULL) 154 if (file == NULL)
155 return -1; 155 return 0;
156 state = (gz_statep)file; 156 state = (gz_statep)file;
157 strm = &(state->strm); 157 strm = &(state->strm);
158 158
159 /* check that we're writing and that there's no error */ 159 /* check that we're writing and that there's no error */
160 if (state->mode != GZ_WRITE || state->err != Z_OK) 160 if (state->mode != GZ_WRITE || state->err != Z_OK)
161 return -1; 161 return 0;
162 162
163 /* since an int is returned, make sure len fits in one, otherwise return 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) */ 164 with an error (this avoids the flaw in the interface) */
165 if ((int)len < 0) { 165 if ((int)len < 0) {
166 gz_error(state, Z_BUF_ERROR, "requested length does not fit in int"); 166 gz_error(state, Z_BUF_ERROR, "requested length does not fit in int");
167 return -1; 167 return 0;
168 } 168 }
169 169
170 /* if len is zero, avoid unnecessary operations */ 170 /* if len is zero, avoid unnecessary operations */
@@ -173,13 +173,13 @@ int ZEXPORT gzwrite(file, buf, len)
173 173
174 /* allocate memory if this is the first time through */ 174 /* allocate memory if this is the first time through */
175 if (state->size == 0 && gz_init(state) == -1) 175 if (state->size == 0 && gz_init(state) == -1)
176 return -1; 176 return 0;
177 177
178 /* check for seek request */ 178 /* check for seek request */
179 if (state->seek) { 179 if (state->seek) {
180 state->seek = 0; 180 state->seek = 0;
181 if (gz_zero(state, state->skip) == -1) 181 if (gz_zero(state, state->skip) == -1)
182 return -1; 182 return 0;
183 } 183 }
184 184
185 /* for small len, copy to input buffer, otherwise compress directly */ 185 /* for small len, copy to input buffer, otherwise compress directly */
@@ -197,20 +197,20 @@ int ZEXPORT gzwrite(file, buf, len)
197 buf = (char *)buf + n; 197 buf = (char *)buf + n;
198 len -= n; 198 len -= n;
199 if (len && gz_comp(state, Z_NO_FLUSH) == -1) 199 if (len && gz_comp(state, Z_NO_FLUSH) == -1)
200 return -1; 200 return 0;
201 } while (len); 201 } while (len);
202 } 202 }
203 else { 203 else {
204 /* consume whatever's left in the input buffer */ 204 /* consume whatever's left in the input buffer */
205 if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) 205 if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
206 return -1; 206 return 0;
207 207
208 /* directly compress user buffer to file */ 208 /* directly compress user buffer to file */
209 strm->avail_in = len; 209 strm->avail_in = len;
210 strm->next_in = (voidp)buf; 210 strm->next_in = (voidp)buf;
211 state->pos += len; 211 state->pos += len;
212 if (gz_comp(state, Z_NO_FLUSH) == -1) 212 if (gz_comp(state, Z_NO_FLUSH) == -1)
213 return -1; 213 return 0;
214 } 214 }
215 215
216 /* input was all buffered or compressed (put will fit in int) */ 216 /* input was all buffered or compressed (put will fit in int) */
@@ -265,8 +265,13 @@ int ZEXPORT gzputs(file, str)
265 gzFile file; 265 gzFile file;
266 const char *str; 266 const char *str;
267{ 267{
268 int ret;
269 unsigned len;
270
268 /* write string */ 271 /* write string */
269 return gzwrite(file, str, strlen(str)); 272 len = strlen(str);
273 ret = gzwrite(file, str, len);
274 return ret == 0 && len != 0 ? -1 : ret;
270} 275}
271 276
272#ifdef STDC 277#ifdef STDC
@@ -494,7 +499,7 @@ int ZEXPORT gzsetparams(file, level, strategy)
494int ZEXPORT gzclose_w(file) 499int ZEXPORT gzclose_w(file)
495 gzFile file; 500 gzFile file;
496{ 501{
497 int ret; 502 int ret = 0;
498 gz_statep state; 503 gz_statep state;
499 504
500 /* get internal structure */ 505 /* get internal structure */
@@ -509,13 +514,12 @@ int ZEXPORT gzclose_w(file)
509 /* check for seek request */ 514 /* check for seek request */
510 if (state->seek) { 515 if (state->seek) {
511 state->seek = 0; 516 state->seek = 0;
512 if (gz_zero(state, state->skip) == -1) 517 ret += gz_zero(state, state->skip);
513 return -1;
514 } 518 }
515 519
516 /* flush, free memory, and close file */ 520 /* flush, free memory, and close file */
517 ret = gz_comp(state, Z_FINISH); 521 ret += gz_comp(state, Z_FINISH);
518 deflateEnd(&(state->strm)); 522 (void)deflateEnd(&(state->strm));
519 free(state->out); 523 free(state->out);
520 free(state->in); 524 free(state->in);
521 ret += close(state->fd); 525 ret += close(state->fd);