gzwrite.c (7648bc9fee8dec6cb3c4941e0165a930fbe8dcb0) gzwrite.c (cd8822075a38d0734e74b1735e4b5dbef9789170)
1/* gzwrite.c -- zlib functions for writing gzip files
1/* gzwrite.c -- zlib functions for writing gzip files
2 * Copyright (C) 2004-2017 Mark Adler
2 * Copyright (C) 2004-2019 Mark Adler
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/* $FreeBSD$ */
7
8#include "gzguts.h"
9#include <unistd.h>
10

--- 84 unchanged lines hidden (view full) ---

95 return -1;
96 }
97 strm->avail_in -= (unsigned)writ;
98 strm->next_in += writ;
99 }
100 return 0;
101 }
102
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6/* $FreeBSD$ */
7
8#include "gzguts.h"
9#include <unistd.h>
10

--- 84 unchanged lines hidden (view full) ---

95 return -1;
96 }
97 strm->avail_in -= (unsigned)writ;
98 strm->next_in += writ;
99 }
100 return 0;
101 }
102
103 /* check for a pending reset */
104 if (state->reset) {
105 /* don't start a new gzip member unless there is data to write */
106 if (strm->avail_in == 0)
107 return 0;
108 deflateReset(strm);
109 state->reset = 0;
110 }
111
103 /* run deflate() on provided input until it produces no more output */
104 ret = Z_OK;
105 do {
106 /* write out current buffer contents if full, or if flushing, but if
107 doing Z_FINISH then don't write until we get to Z_STREAM_END */
108 if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
109 (flush != Z_FINISH || ret == Z_STREAM_END))) {
110 while (strm->next_out > state->x.next) {

--- 21 unchanged lines hidden (view full) ---

132 "internal error: deflate stream corrupt");
133 return -1;
134 }
135 have -= strm->avail_out;
136 } while (have);
137
138 /* if that completed a deflate stream, allow another to start */
139 if (flush == Z_FINISH)
112 /* run deflate() on provided input until it produces no more output */
113 ret = Z_OK;
114 do {
115 /* write out current buffer contents if full, or if flushing, but if
116 doing Z_FINISH then don't write until we get to Z_STREAM_END */
117 if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
118 (flush != Z_FINISH || ret == Z_STREAM_END))) {
119 while (strm->next_out > state->x.next) {

--- 21 unchanged lines hidden (view full) ---

141 "internal error: deflate stream corrupt");
142 return -1;
143 }
144 have -= strm->avail_out;
145 } while (have);
146
147 /* if that completed a deflate stream, allow another to start */
148 if (flush == Z_FINISH)
140 deflateReset(strm);
149 state->reset = 1;
141
142 /* all done, no errors */
143 return 0;
144}
145
146/* Compress len zeros to output. Return -1 on a write error or memory
147 allocation failure by gz_comp(), or 0 on success. */
148local int gz_zero(state, len)

--- 58 unchanged lines hidden (view full) ---

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)
150
151 /* all done, no errors */
152 return 0;
153}
154
155/* Compress len zeros to output. Return -1 on a write error or memory
156 allocation failure by gz_comp(), or 0 on success. */
157local int gz_zero(state, len)

--- 58 unchanged lines hidden (view full) ---

216 unsigned have, copy;
217
218 if (state->strm.avail_in == 0)
219 state->strm.next_in = state->in;
220 have = (unsigned)((state->strm.next_in + state->strm.avail_in) -
221 state->in);
222 copy = state->size - have;
223 if (copy > len)
215 copy = len;
224 copy = (unsigned)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)
225 memcpy(state->in + have, buf, copy);
226 state->strm.avail_in += copy;
227 state->x.pos += copy;
228 buf = (const char *)buf + copy;
229 len -= copy;
230 if (len && gz_comp(state, Z_NO_FLUSH) == -1)
231 return 0;
232 } while (len);
233 }
234 else {
235 /* consume whatever's left in the input buffer */
236 if (state->strm.avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
237 return 0;
238
239 /* directly compress user buffer to file */
240 state->strm.next_in = (z_const Bytef *)buf;
241 do {
242 unsigned n = (unsigned)-1;
243 if (n > len)
235 n = len;
244 n = (unsigned)len;
236 state->strm.avail_in = n;
237 state->x.pos += n;
238 if (gz_comp(state, Z_NO_FLUSH) == -1)
239 return 0;
240 len -= n;
241 } while (len);
242 }
243

--- 103 unchanged lines hidden (view full) ---

347 /* no room in buffer or not initialized, use gz_write() */
348 buf[0] = (unsigned char)c;
349 if (gz_write(state, buf, 1) != 1)
350 return -1;
351 return c & 0xff;
352}
353
354/* -- see zlib.h -- */
245 state->strm.avail_in = n;
246 state->x.pos += n;
247 if (gz_comp(state, Z_NO_FLUSH) == -1)
248 return 0;
249 len -= n;
250 } while (len);
251 }
252

--- 103 unchanged lines hidden (view full) ---

356 /* no room in buffer or not initialized, use gz_write() */
357 buf[0] = (unsigned char)c;
358 if (gz_write(state, buf, 1) != 1)
359 return -1;
360 return c & 0xff;
361}
362
363/* -- see zlib.h -- */
355int ZEXPORT gzputs(file, str)
364int ZEXPORT gzputs(file, s)
356 gzFile file;
365 gzFile file;
357 const char *str;
366 const char *s;
358{
367{
359 int ret;
360 z_size_t len;
368 z_size_t len, put;
361 gz_statep state;
362
363 /* get internal structure */
364 if (file == NULL)
365 return -1;
366 state = (gz_statep)file;
367
368 /* check that we're writing and that there's no error */
369 if (state->mode != GZ_WRITE || state->err != Z_OK)
370 return -1;
371
372 /* write string */
369 gz_statep state;
370
371 /* get internal structure */
372 if (file == NULL)
373 return -1;
374 state = (gz_statep)file;
375
376 /* check that we're writing and that there's no error */
377 if (state->mode != GZ_WRITE || state->err != Z_OK)
378 return -1;
379
380 /* write string */
373 len = strlen(str);
374 ret = gz_write(state, str, len);
375 return ret == 0 && len != 0 ? -1 : ret;
381 len = strlen(s);
382 if ((int)len < 0 || (unsigned)len != len) {
383 gz_error(state, Z_STREAM_ERROR, "string length does not fit in int");
384 return -1;
385 }
386 put = gz_write(state, s, len);
387 return put < len ? -1 : (int)len;
376}
377
378#if defined(STDC) || defined(Z_HAVE_STDARG_H)
379#include <stdarg.h>
380
381/* -- see zlib.h -- */
382int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
383{

--- 55 unchanged lines hidden (view full) ---

439 /* update buffer and position, compress first half if past that */
440 strm->avail_in += (unsigned)len;
441 state->x.pos += len;
442 if (strm->avail_in >= state->size) {
443 left = strm->avail_in - state->size;
444 strm->avail_in = state->size;
445 if (gz_comp(state, Z_NO_FLUSH) == -1)
446 return state->err;
388}
389
390#if defined(STDC) || defined(Z_HAVE_STDARG_H)
391#include <stdarg.h>
392
393/* -- see zlib.h -- */
394int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
395{

--- 55 unchanged lines hidden (view full) ---

451 /* update buffer and position, compress first half if past that */
452 strm->avail_in += (unsigned)len;
453 state->x.pos += len;
454 if (strm->avail_in >= state->size) {
455 left = strm->avail_in - state->size;
456 strm->avail_in = state->size;
457 if (gz_comp(state, Z_NO_FLUSH) == -1)
458 return state->err;
447 memcpy(state->in, state->in + state->size, left);
459 memmove(state->in, state->in + state->size, left);
448 strm->next_in = state->in;
449 strm->avail_in = left;
450 }
451 return len;
452}
453
454int ZEXPORTVA gzprintf(gzFile file, const char *format, ...)
455{

--- 82 unchanged lines hidden (view full) ---

538 /* update buffer and position, compress first half if past that */
539 strm->avail_in += len;
540 state->x.pos += len;
541 if (strm->avail_in >= state->size) {
542 left = strm->avail_in - state->size;
543 strm->avail_in = state->size;
544 if (gz_comp(state, Z_NO_FLUSH) == -1)
545 return state->err;
460 strm->next_in = state->in;
461 strm->avail_in = left;
462 }
463 return len;
464}
465
466int ZEXPORTVA gzprintf(gzFile file, const char *format, ...)
467{

--- 82 unchanged lines hidden (view full) ---

550 /* update buffer and position, compress first half if past that */
551 strm->avail_in += len;
552 state->x.pos += len;
553 if (strm->avail_in >= state->size) {
554 left = strm->avail_in - state->size;
555 strm->avail_in = state->size;
556 if (gz_comp(state, Z_NO_FLUSH) == -1)
557 return state->err;
546 memcpy(state->in, state->in + state->size, left);
558 memmove(state->in, state->in + state->size, left);
547 strm->next_in = state->in;
548 strm->avail_in = left;
549 }
550 return (int)len;
551}
552
553#endif
554

--- 114 unchanged lines hidden ---
559 strm->next_in = state->in;
560 strm->avail_in = left;
561 }
562 return (int)len;
563}
564
565#endif
566

--- 114 unchanged lines hidden ---