aboutsummaryrefslogtreecommitdiff
path: root/gzwrite.c
diff options
context:
space:
mode:
authorMark Adler <madler@alumni.caltech.edu>2025-02-21 14:22:16 -0800
committerMark Adler <madler@alumni.caltech.edu>2025-02-25 00:31:37 -0800
commita4e4521ee1303b756a8f96c18cdf4729327b687d (patch)
tree6f2e178d078a420fe9731976944874cbaf10f438 /gzwrite.c
parentaa27ba467717707881e4ae925849e6229ceabf38 (diff)
downloadzlib-a4e4521ee1303b756a8f96c18cdf4729327b687d.tar.gz
zlib-a4e4521ee1303b756a8f96c18cdf4729327b687d.tar.bz2
zlib-a4e4521ee1303b756a8f96c18cdf4729327b687d.zip
Have gz_skip() update how far it got for later continuation.
This also simplifies seek processing in general.
Diffstat (limited to 'gzwrite.c')
-rw-r--r--gzwrite.c67
1 files changed, 23 insertions, 44 deletions
diff --git a/gzwrite.c b/gzwrite.c
index 435b462..ef9ffee 100644
--- a/gzwrite.c
+++ b/gzwrite.c
@@ -138,9 +138,9 @@ local int gz_comp(gz_statep state, int flush) {
138 return 0; 138 return 0;
139} 139}
140 140
141/* Compress len zeros to output. Return -1 on a write error or memory 141/* Compress state->skip (> 0) zeros to output. Return -1 on a write error or
142 allocation failure by gz_comp(), or 0 on success. */ 142 memory allocation failure by gz_comp(), or 0 on success. */
143local int gz_zero(gz_statep state, z_off64_t len) { 143local int gz_zero(gz_statep state) {
144 int first; 144 int first;
145 unsigned n; 145 unsigned n;
146 z_streamp strm = &(state->strm); 146 z_streamp strm = &(state->strm);
@@ -149,11 +149,11 @@ local int gz_zero(gz_statep state, z_off64_t len) {
149 if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) 149 if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
150 return -1; 150 return -1;
151 151
152 /* compress len zeros (len guaranteed > 0) */ 152 /* compress state->skip zeros */
153 first = 1; 153 first = 1;
154 while (len) { 154 do {
155 n = GT_OFF(state->size) || (z_off64_t)state->size > len ? 155 n = GT_OFF(state->size) || (z_off64_t)state->size > state->skip ?
156 (unsigned)len : state->size; 156 (unsigned)state->skip : state->size;
157 if (first) { 157 if (first) {
158 memset(state->in, 0, n); 158 memset(state->in, 0, n);
159 first = 0; 159 first = 0;
@@ -163,8 +163,8 @@ local int gz_zero(gz_statep state, z_off64_t len) {
163 state->x.pos += n; 163 state->x.pos += n;
164 if (gz_comp(state, Z_NO_FLUSH) == -1) 164 if (gz_comp(state, Z_NO_FLUSH) == -1)
165 return -1; 165 return -1;
166 len -= n; 166 state->skip -= n;
167 } 167 } while (state->skip);
168 return 0; 168 return 0;
169} 169}
170 170
@@ -182,11 +182,8 @@ local z_size_t gz_write(gz_statep state, voidpc buf, z_size_t len) {
182 return 0; 182 return 0;
183 183
184 /* check for seek request */ 184 /* check for seek request */
185 if (state->seek) { 185 if (state->skip && gz_zero(state) == -1)
186 state->seek = 0; 186 return 0;
187 if (gz_zero(state, state->skip) == -1)
188 return 0;
189 }
190 187
191 /* for small len, copy to input buffer, otherwise compress directly */ 188 /* for small len, copy to input buffer, otherwise compress directly */
192 if (len < state->size) { 189 if (len < state->size) {
@@ -301,11 +298,8 @@ int ZEXPORT gzputc(gzFile file, int c) {
301 return -1; 298 return -1;
302 299
303 /* check for seek request */ 300 /* check for seek request */
304 if (state->seek) { 301 if (state->skip && gz_zero(state) == -1)
305 state->seek = 0; 302 return -1;
306 if (gz_zero(state, state->skip) == -1)
307 return -1;
308 }
309 303
310 /* try writing to input buffer for speed (state->size == 0 if buffer not 304 /* try writing to input buffer for speed (state->size == 0 if buffer not
311 initialized) */ 305 initialized) */
@@ -378,11 +372,8 @@ int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) {
378 return state->err; 372 return state->err;
379 373
380 /* check for seek request */ 374 /* check for seek request */
381 if (state->seek) { 375 if (state->skip && gz_zero(state) == -1)
382 state->seek = 0; 376 return state->err;
383 if (gz_zero(state, state->skip) == -1)
384 return state->err;
385 }
386 377
387 /* do the printf() into the input buffer, put length in len -- the input 378 /* do the printf() into the input buffer, put length in len -- the input
388 buffer is double-sized just for this function, so there is guaranteed to 379 buffer is double-sized just for this function, so there is guaranteed to
@@ -468,11 +459,8 @@ int ZEXPORTVA gzprintf(gzFile file, const char *format, int a1, int a2, int a3,
468 return state->error; 459 return state->error;
469 460
470 /* check for seek request */ 461 /* check for seek request */
471 if (state->seek) { 462 if (state->skip && gz_zero(state) == -1)
472 state->seek = 0; 463 return state->error;
473 if (gz_zero(state, state->skip) == -1)
474 return state->error;
475 }
476 464
477 /* do the printf() into the input buffer, put length in len -- the input 465 /* do the printf() into the input buffer, put length in len -- the input
478 buffer is double-sized just for this function, so there is guaranteed to 466 buffer is double-sized just for this function, so there is guaranteed to
@@ -542,11 +530,8 @@ int ZEXPORT gzflush(gzFile file, int flush) {
542 return Z_STREAM_ERROR; 530 return Z_STREAM_ERROR;
543 531
544 /* check for seek request */ 532 /* check for seek request */
545 if (state->seek) { 533 if (state->skip && gz_zero(state) == -1)
546 state->seek = 0; 534 return state->err;
547 if (gz_zero(state, state->skip) == -1)
548 return state->err;
549 }
550 535
551 /* compress remaining data with requested flush */ 536 /* compress remaining data with requested flush */
552 (void)gz_comp(state, flush); 537 (void)gz_comp(state, flush);
@@ -573,11 +558,8 @@ int ZEXPORT gzsetparams(gzFile file, int level, int strategy) {
573 return Z_OK; 558 return Z_OK;
574 559
575 /* check for seek request */ 560 /* check for seek request */
576 if (state->seek) { 561 if (state->skip && gz_zero(state) == -1)
577 state->seek = 0; 562 return state->err;
578 if (gz_zero(state, state->skip) == -1)
579 return state->err;
580 }
581 563
582 /* change compression parameters for subsequent input */ 564 /* change compression parameters for subsequent input */
583 if (state->size) { 565 if (state->size) {
@@ -606,11 +588,8 @@ int ZEXPORT gzclose_w(gzFile file) {
606 return Z_STREAM_ERROR; 588 return Z_STREAM_ERROR;
607 589
608 /* check for seek request */ 590 /* check for seek request */
609 if (state->seek) { 591 if (state->skip && gz_zero(state) == -1)
610 state->seek = 0; 592 ret = state->err;
611 if (gz_zero(state, state->skip) == -1)
612 ret = state->err;
613 }
614 593
615 /* flush, free memory, and close file */ 594 /* flush, free memory, and close file */
616 if (gz_comp(state, Z_FINISH) == -1) 595 if (gz_comp(state, Z_FINISH) == -1)