< prev index next >

src/java.base/share/native/libzip/zlib/gzwrite.c

Print this page

 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  */
 24 
 25 /* gzwrite.c -- zlib functions for writing gzip files
 26  * Copyright (C) 2004-2019 Mark Adler
 27  * For conditions of distribution and use, see copyright notice in zlib.h
 28  */
 29 
 30 #include "gzguts.h"
 31 
 32 /* Local functions */
 33 local int gz_init OF((gz_statep));
 34 local int gz_comp OF((gz_statep, int));
 35 local int gz_zero OF((gz_statep, z_off64_t));
 36 local z_size_t gz_write OF((gz_statep, voidpc, z_size_t));
 37 
 38 /* Initialize state for writing a gzip file.  Mark initialization by setting
 39    state->size to non-zero.  Return -1 on a memory allocation failure, or 0 on
 40    success. */
 41 local int gz_init(state)
 42     gz_statep state;
 43 {
 44     int ret;
 45     z_streamp strm = &(state->strm);
 46 
 47     /* allocate input buffer (double size for gzprintf) */
 48     state->in = (unsigned char *)malloc(state->want << 1);
 49     if (state->in == NULL) {
 50         gz_error(state, Z_MEM_ERROR, "out of memory");
 51         return -1;
 52     }
 53 
 54     /* only need output buffer and deflate state if compressing */
 55     if (!state->direct) {
 56         /* allocate output buffer */
 57         state->out = (unsigned char *)malloc(state->want);
 58         if (state->out == NULL) {
 59             free(state->in);
 60             gz_error(state, Z_MEM_ERROR, "out of memory");
 61             return -1;
 62         }
 63 

 77     }
 78 
 79     /* mark state as initialized */
 80     state->size = state->want;
 81 
 82     /* initialize write buffer if compressing */
 83     if (!state->direct) {
 84         strm->avail_out = state->size;
 85         strm->next_out = state->out;
 86         state->x.next = strm->next_out;
 87     }
 88     return 0;
 89 }
 90 
 91 /* Compress whatever is at avail_in and next_in and write to the output file.
 92    Return -1 if there is an error writing to the output file or if gz_init()
 93    fails to allocate memory, otherwise 0.  flush is assumed to be a valid
 94    deflate() flush value.  If flush is Z_FINISH, then the deflate() state is
 95    reset to start a new gzip stream.  If gz->direct is true, then simply write
 96    to the output file without compressing, and ignore flush. */
 97 local int gz_comp(state, flush)
 98     gz_statep state;
 99     int flush;
100 {
101     int ret, writ;
102     unsigned have, put, max = ((unsigned)-1 >> 2) + 1;
103     z_streamp strm = &(state->strm);
104 
105     /* allocate memory if this is the first time through */
106     if (state->size == 0 && gz_init(state) == -1)
107         return -1;
108 
109     /* write directly if requested */
110     if (state->direct) {
111         while (strm->avail_in) {
112             put = strm->avail_in > max ? max : strm->avail_in;
113             writ = write(state->fd, strm->next_in, put);
114             if (writ < 0) {
115                 gz_error(state, Z_ERRNO, zstrerror());
116                 return -1;
117             }
118             strm->avail_in -= (unsigned)writ;
119             strm->next_in += writ;
120         }

158         have = strm->avail_out;
159         ret = deflate(strm, flush);
160         if (ret == Z_STREAM_ERROR) {
161             gz_error(state, Z_STREAM_ERROR,
162                       "internal error: deflate stream corrupt");
163             return -1;
164         }
165         have -= strm->avail_out;
166     } while (have);
167 
168     /* if that completed a deflate stream, allow another to start */
169     if (flush == Z_FINISH)
170         state->reset = 1;
171 
172     /* all done, no errors */
173     return 0;
174 }
175 
176 /* Compress len zeros to output.  Return -1 on a write error or memory
177    allocation failure by gz_comp(), or 0 on success. */
178 local int gz_zero(state, len)
179     gz_statep state;
180     z_off64_t len;
181 {
182     int first;
183     unsigned n;
184     z_streamp strm = &(state->strm);
185 
186     /* consume whatever's left in the input buffer */
187     if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
188         return -1;
189 
190     /* compress len zeros (len guaranteed > 0) */
191     first = 1;
192     while (len) {
193         n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
194             (unsigned)len : state->size;
195         if (first) {
196             memset(state->in, 0, n);
197             first = 0;
198         }
199         strm->avail_in = n;
200         strm->next_in = state->in;
201         state->x.pos += n;
202         if (gz_comp(state, Z_NO_FLUSH) == -1)
203             return -1;
204         len -= n;
205     }
206     return 0;
207 }
208 
209 /* Write len bytes from buf to file.  Return the number of bytes written.  If
210    the returned value is less than len, then there was an error. */
211 local z_size_t gz_write(state, buf, len)
212     gz_statep state;
213     voidpc buf;
214     z_size_t len;
215 {
216     z_size_t put = len;
217 
218     /* if len is zero, avoid unnecessary operations */
219     if (len == 0)
220         return 0;
221 
222     /* allocate memory if this is the first time through */
223     if (state->size == 0 && gz_init(state) == -1)
224         return 0;
225 
226     /* check for seek request */
227     if (state->seek) {
228         state->seek = 0;
229         if (gz_zero(state, state->skip) == -1)
230             return 0;
231     }
232 
233     /* for small len, copy to input buffer, otherwise compress directly */
234     if (len < state->size) {
235         /* copy to input buffer, compress when full */

259 
260         /* directly compress user buffer to file */
261         state->strm.next_in = (z_const Bytef *)buf;
262         do {
263             unsigned n = (unsigned)-1;
264             if (n > len)
265                 n = (unsigned)len;
266             state->strm.avail_in = n;
267             state->x.pos += n;
268             if (gz_comp(state, Z_NO_FLUSH) == -1)
269                 return 0;
270             len -= n;
271         } while (len);
272     }
273 
274     /* input was all buffered or compressed */
275     return put;
276 }
277 
278 /* -- see zlib.h -- */
279 int ZEXPORT gzwrite(file, buf, len)
280     gzFile file;
281     voidpc buf;
282     unsigned len;
283 {
284     gz_statep state;
285 
286     /* get internal structure */
287     if (file == NULL)
288         return 0;
289     state = (gz_statep)file;
290 
291     /* check that we're writing and that there's no error */
292     if (state->mode != GZ_WRITE || state->err != Z_OK)
293         return 0;
294 
295     /* since an int is returned, make sure len fits in one, otherwise return
296        with an error (this avoids a flaw in the interface) */
297     if ((int)len < 0) {
298         gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
299         return 0;
300     }
301 
302     /* write len bytes from buf (the return value will fit in an int) */
303     return (int)gz_write(state, buf, len);
304 }
305 
306 /* -- see zlib.h -- */
307 z_size_t ZEXPORT gzfwrite(buf, size, nitems, file)
308     voidpc buf;
309     z_size_t size;
310     z_size_t nitems;
311     gzFile file;
312 {
313     z_size_t len;
314     gz_statep state;
315 
316     /* get internal structure */
317     if (file == NULL)
318         return 0;
319     state = (gz_statep)file;
320 
321     /* check that we're writing and that there's no error */
322     if (state->mode != GZ_WRITE || state->err != Z_OK)
323         return 0;
324 
325     /* compute bytes to read -- error on overflow */
326     len = nitems * size;
327     if (size && len / size != nitems) {
328         gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
329         return 0;
330     }
331 
332     /* write len bytes to buf, return the number of full items written */
333     return len ? gz_write(state, buf, len) / size : 0;
334 }
335 
336 /* -- see zlib.h -- */
337 int ZEXPORT gzputc(file, c)
338     gzFile file;
339     int c;
340 {
341     unsigned have;
342     unsigned char buf[1];
343     gz_statep state;
344     z_streamp strm;
345 
346     /* get internal structure */
347     if (file == NULL)
348         return -1;
349     state = (gz_statep)file;
350     strm = &(state->strm);
351 
352     /* check that we're writing and that there's no error */
353     if (state->mode != GZ_WRITE || state->err != Z_OK)
354         return -1;
355 
356     /* check for seek request */
357     if (state->seek) {
358         state->seek = 0;
359         if (gz_zero(state, state->skip) == -1)
360             return -1;

365     if (state->size) {
366         if (strm->avail_in == 0)
367             strm->next_in = state->in;
368         have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
369         if (have < state->size) {
370             state->in[have] = (unsigned char)c;
371             strm->avail_in++;
372             state->x.pos++;
373             return c & 0xff;
374         }
375     }
376 
377     /* no room in buffer or not initialized, use gz_write() */
378     buf[0] = (unsigned char)c;
379     if (gz_write(state, buf, 1) != 1)
380         return -1;
381     return c & 0xff;
382 }
383 
384 /* -- see zlib.h -- */
385 int ZEXPORT gzputs(file, s)
386     gzFile file;
387     const char *s;
388 {
389     z_size_t len, put;
390     gz_statep state;
391 
392     /* get internal structure */
393     if (file == NULL)
394         return -1;
395     state = (gz_statep)file;
396 
397     /* check that we're writing and that there's no error */
398     if (state->mode != GZ_WRITE || state->err != Z_OK)
399         return -1;
400 
401     /* write string */
402     len = strlen(s);
403     if ((int)len < 0 || (unsigned)len != len) {
404         gz_error(state, Z_STREAM_ERROR, "string length does not fit in int");
405         return -1;
406     }
407     put = gz_write(state, s, len);
408     return put < len ? -1 : (int)len;
409 }
410 
411 #if defined(STDC) || defined(Z_HAVE_STDARG_H)
412 #include <stdarg.h>
413 
414 /* -- see zlib.h -- */
415 int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
416 {
417     int len;
418     unsigned left;
419     char *next;
420     gz_statep state;
421     z_streamp strm;
422 
423     /* get internal structure */
424     if (file == NULL)
425         return Z_STREAM_ERROR;
426     state = (gz_statep)file;
427     strm = &(state->strm);
428 
429     /* check that we're writing and that there's no error */
430     if (state->mode != GZ_WRITE || state->err != Z_OK)
431         return Z_STREAM_ERROR;
432 
433     /* make sure we have some buffer space */
434     if (state->size == 0 && gz_init(state) == -1)
435         return state->err;
436 

467 
468     /* check that printf() results fit in buffer */
469     if (len == 0 || (unsigned)len >= state->size || next[state->size - 1] != 0)
470         return 0;
471 
472     /* update buffer and position, compress first half if past that */
473     strm->avail_in += (unsigned)len;
474     state->x.pos += len;
475     if (strm->avail_in >= state->size) {
476         left = strm->avail_in - state->size;
477         strm->avail_in = state->size;
478         if (gz_comp(state, Z_NO_FLUSH) == -1)
479             return state->err;
480         memmove(state->in, state->in + state->size, left);
481         strm->next_in = state->in;
482         strm->avail_in = left;
483     }
484     return len;
485 }
486 
487 int ZEXPORTVA gzprintf(gzFile file, const char *format, ...)
488 {
489     va_list va;
490     int ret;
491 
492     va_start(va, format);
493     ret = gzvprintf(file, format, va);
494     va_end(va);
495     return ret;
496 }
497 
498 #else /* !STDC && !Z_HAVE_STDARG_H */
499 
500 /* -- see zlib.h -- */
501 int ZEXPORTVA gzprintf(file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
502                        a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
503     gzFile file;
504     const char *format;
505     int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
506         a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
507 {
508     unsigned len, left;
509     char *next;
510     gz_statep state;
511     z_streamp strm;
512 
513     /* get internal structure */
514     if (file == NULL)
515         return Z_STREAM_ERROR;
516     state = (gz_statep)file;
517     strm = &(state->strm);
518 
519     /* check that can really pass pointer in ints */
520     if (sizeof(int) != sizeof(void *))
521         return Z_STREAM_ERROR;
522 
523     /* check that we're writing and that there's no error */
524     if (state->mode != GZ_WRITE || state->err != Z_OK)
525         return Z_STREAM_ERROR;
526 
527     /* make sure we have some buffer space */

569         return 0;
570 
571     /* update buffer and position, compress first half if past that */
572     strm->avail_in += len;
573     state->x.pos += len;
574     if (strm->avail_in >= state->size) {
575         left = strm->avail_in - state->size;
576         strm->avail_in = state->size;
577         if (gz_comp(state, Z_NO_FLUSH) == -1)
578             return state->err;
579         memmove(state->in, state->in + state->size, left);
580         strm->next_in = state->in;
581         strm->avail_in = left;
582     }
583     return (int)len;
584 }
585 
586 #endif
587 
588 /* -- see zlib.h -- */
589 int ZEXPORT gzflush(file, flush)
590     gzFile file;
591     int flush;
592 {
593     gz_statep state;
594 
595     /* get internal structure */
596     if (file == NULL)
597         return Z_STREAM_ERROR;
598     state = (gz_statep)file;
599 
600     /* check that we're writing and that there's no error */
601     if (state->mode != GZ_WRITE || state->err != Z_OK)
602         return Z_STREAM_ERROR;
603 
604     /* check flush parameter */
605     if (flush < 0 || flush > Z_FINISH)
606         return Z_STREAM_ERROR;
607 
608     /* check for seek request */
609     if (state->seek) {
610         state->seek = 0;
611         if (gz_zero(state, state->skip) == -1)
612             return state->err;
613     }
614 
615     /* compress remaining data with requested flush */
616     (void)gz_comp(state, flush);
617     return state->err;
618 }
619 
620 /* -- see zlib.h -- */
621 int ZEXPORT gzsetparams(file, level, strategy)
622     gzFile file;
623     int level;
624     int strategy;
625 {
626     gz_statep state;
627     z_streamp strm;
628 
629     /* get internal structure */
630     if (file == NULL)
631         return Z_STREAM_ERROR;
632     state = (gz_statep)file;
633     strm = &(state->strm);
634 
635     /* check that we're writing and that there's no error */
636     if (state->mode != GZ_WRITE || state->err != Z_OK)
637         return Z_STREAM_ERROR;
638 
639     /* if no change is requested, then do nothing */
640     if (level == state->level && strategy == state->strategy)
641         return Z_OK;
642 
643     /* check for seek request */
644     if (state->seek) {
645         state->seek = 0;
646         if (gz_zero(state, state->skip) == -1)
647             return state->err;
648     }
649 
650     /* change compression parameters for subsequent input */
651     if (state->size) {
652         /* flush previous input with previous parameters before changing */
653         if (strm->avail_in && gz_comp(state, Z_BLOCK) == -1)
654             return state->err;
655         deflateParams(strm, level, strategy);
656     }
657     state->level = level;
658     state->strategy = strategy;
659     return Z_OK;
660 }
661 
662 /* -- see zlib.h -- */
663 int ZEXPORT gzclose_w(file)
664     gzFile file;
665 {
666     int ret = Z_OK;
667     gz_statep state;
668 
669     /* get internal structure */
670     if (file == NULL)
671         return Z_STREAM_ERROR;
672     state = (gz_statep)file;
673 
674     /* check that we're writing */
675     if (state->mode != GZ_WRITE)
676         return Z_STREAM_ERROR;
677 
678     /* check for seek request */
679     if (state->seek) {
680         state->seek = 0;
681         if (gz_zero(state, state->skip) == -1)
682             ret = state->err;
683     }
684 
685     /* flush, free memory, and close file */

 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  */
 24 
 25 /* gzwrite.c -- zlib functions for writing gzip files
 26  * Copyright (C) 2004-2019 Mark Adler
 27  * For conditions of distribution and use, see copyright notice in zlib.h
 28  */
 29 
 30 #include "gzguts.h"
 31 






 32 /* Initialize state for writing a gzip file.  Mark initialization by setting
 33    state->size to non-zero.  Return -1 on a memory allocation failure, or 0 on
 34    success. */
 35 local int gz_init(gz_statep state) {


 36     int ret;
 37     z_streamp strm = &(state->strm);
 38 
 39     /* allocate input buffer (double size for gzprintf) */
 40     state->in = (unsigned char *)malloc(state->want << 1);
 41     if (state->in == NULL) {
 42         gz_error(state, Z_MEM_ERROR, "out of memory");
 43         return -1;
 44     }
 45 
 46     /* only need output buffer and deflate state if compressing */
 47     if (!state->direct) {
 48         /* allocate output buffer */
 49         state->out = (unsigned char *)malloc(state->want);
 50         if (state->out == NULL) {
 51             free(state->in);
 52             gz_error(state, Z_MEM_ERROR, "out of memory");
 53             return -1;
 54         }
 55 

 69     }
 70 
 71     /* mark state as initialized */
 72     state->size = state->want;
 73 
 74     /* initialize write buffer if compressing */
 75     if (!state->direct) {
 76         strm->avail_out = state->size;
 77         strm->next_out = state->out;
 78         state->x.next = strm->next_out;
 79     }
 80     return 0;
 81 }
 82 
 83 /* Compress whatever is at avail_in and next_in and write to the output file.
 84    Return -1 if there is an error writing to the output file or if gz_init()
 85    fails to allocate memory, otherwise 0.  flush is assumed to be a valid
 86    deflate() flush value.  If flush is Z_FINISH, then the deflate() state is
 87    reset to start a new gzip stream.  If gz->direct is true, then simply write
 88    to the output file without compressing, and ignore flush. */
 89 local int gz_comp(gz_statep state, int flush) {



 90     int ret, writ;
 91     unsigned have, put, max = ((unsigned)-1 >> 2) + 1;
 92     z_streamp strm = &(state->strm);
 93 
 94     /* allocate memory if this is the first time through */
 95     if (state->size == 0 && gz_init(state) == -1)
 96         return -1;
 97 
 98     /* write directly if requested */
 99     if (state->direct) {
100         while (strm->avail_in) {
101             put = strm->avail_in > max ? max : strm->avail_in;
102             writ = write(state->fd, strm->next_in, put);
103             if (writ < 0) {
104                 gz_error(state, Z_ERRNO, zstrerror());
105                 return -1;
106             }
107             strm->avail_in -= (unsigned)writ;
108             strm->next_in += writ;
109         }

147         have = strm->avail_out;
148         ret = deflate(strm, flush);
149         if (ret == Z_STREAM_ERROR) {
150             gz_error(state, Z_STREAM_ERROR,
151                       "internal error: deflate stream corrupt");
152             return -1;
153         }
154         have -= strm->avail_out;
155     } while (have);
156 
157     /* if that completed a deflate stream, allow another to start */
158     if (flush == Z_FINISH)
159         state->reset = 1;
160 
161     /* all done, no errors */
162     return 0;
163 }
164 
165 /* Compress len zeros to output.  Return -1 on a write error or memory
166    allocation failure by gz_comp(), or 0 on success. */
167 local int gz_zero(gz_statep state, z_off64_t len) {



168     int first;
169     unsigned n;
170     z_streamp strm = &(state->strm);
171 
172     /* consume whatever's left in the input buffer */
173     if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
174         return -1;
175 
176     /* compress len zeros (len guaranteed > 0) */
177     first = 1;
178     while (len) {
179         n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
180             (unsigned)len : state->size;
181         if (first) {
182             memset(state->in, 0, n);
183             first = 0;
184         }
185         strm->avail_in = n;
186         strm->next_in = state->in;
187         state->x.pos += n;
188         if (gz_comp(state, Z_NO_FLUSH) == -1)
189             return -1;
190         len -= n;
191     }
192     return 0;
193 }
194 
195 /* Write len bytes from buf to file.  Return the number of bytes written.  If
196    the returned value is less than len, then there was an error. */
197 local z_size_t gz_write(gz_statep state, voidpc buf, z_size_t len) {




198     z_size_t put = len;
199 
200     /* if len is zero, avoid unnecessary operations */
201     if (len == 0)
202         return 0;
203 
204     /* allocate memory if this is the first time through */
205     if (state->size == 0 && gz_init(state) == -1)
206         return 0;
207 
208     /* check for seek request */
209     if (state->seek) {
210         state->seek = 0;
211         if (gz_zero(state, state->skip) == -1)
212             return 0;
213     }
214 
215     /* for small len, copy to input buffer, otherwise compress directly */
216     if (len < state->size) {
217         /* copy to input buffer, compress when full */

241 
242         /* directly compress user buffer to file */
243         state->strm.next_in = (z_const Bytef *)buf;
244         do {
245             unsigned n = (unsigned)-1;
246             if (n > len)
247                 n = (unsigned)len;
248             state->strm.avail_in = n;
249             state->x.pos += n;
250             if (gz_comp(state, Z_NO_FLUSH) == -1)
251                 return 0;
252             len -= n;
253         } while (len);
254     }
255 
256     /* input was all buffered or compressed */
257     return put;
258 }
259 
260 /* -- see zlib.h -- */
261 int ZEXPORT gzwrite(gzFile file, voidpc buf, unsigned len) {




262     gz_statep state;
263 
264     /* get internal structure */
265     if (file == NULL)
266         return 0;
267     state = (gz_statep)file;
268 
269     /* check that we're writing and that there's no error */
270     if (state->mode != GZ_WRITE || state->err != Z_OK)
271         return 0;
272 
273     /* since an int is returned, make sure len fits in one, otherwise return
274        with an error (this avoids a flaw in the interface) */
275     if ((int)len < 0) {
276         gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
277         return 0;
278     }
279 
280     /* write len bytes from buf (the return value will fit in an int) */
281     return (int)gz_write(state, buf, len);
282 }
283 
284 /* -- see zlib.h -- */
285 z_size_t ZEXPORT gzfwrite(voidpc buf, z_size_t size, z_size_t nitems,
286                           gzFile file) {




287     z_size_t len;
288     gz_statep state;
289 
290     /* get internal structure */
291     if (file == NULL)
292         return 0;
293     state = (gz_statep)file;
294 
295     /* check that we're writing and that there's no error */
296     if (state->mode != GZ_WRITE || state->err != Z_OK)
297         return 0;
298 
299     /* compute bytes to read -- error on overflow */
300     len = nitems * size;
301     if (size && len / size != nitems) {
302         gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
303         return 0;
304     }
305 
306     /* write len bytes to buf, return the number of full items written */
307     return len ? gz_write(state, buf, len) / size : 0;
308 }
309 
310 /* -- see zlib.h -- */
311 int ZEXPORT gzputc(gzFile file, int c) {



312     unsigned have;
313     unsigned char buf[1];
314     gz_statep state;
315     z_streamp strm;
316 
317     /* get internal structure */
318     if (file == NULL)
319         return -1;
320     state = (gz_statep)file;
321     strm = &(state->strm);
322 
323     /* check that we're writing and that there's no error */
324     if (state->mode != GZ_WRITE || state->err != Z_OK)
325         return -1;
326 
327     /* check for seek request */
328     if (state->seek) {
329         state->seek = 0;
330         if (gz_zero(state, state->skip) == -1)
331             return -1;

336     if (state->size) {
337         if (strm->avail_in == 0)
338             strm->next_in = state->in;
339         have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
340         if (have < state->size) {
341             state->in[have] = (unsigned char)c;
342             strm->avail_in++;
343             state->x.pos++;
344             return c & 0xff;
345         }
346     }
347 
348     /* no room in buffer or not initialized, use gz_write() */
349     buf[0] = (unsigned char)c;
350     if (gz_write(state, buf, 1) != 1)
351         return -1;
352     return c & 0xff;
353 }
354 
355 /* -- see zlib.h -- */
356 int ZEXPORT gzputs(gzFile file, const char *s) {



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

384     int len;
385     unsigned left;
386     char *next;
387     gz_statep state;
388     z_streamp strm;
389 
390     /* get internal structure */
391     if (file == NULL)
392         return Z_STREAM_ERROR;
393     state = (gz_statep)file;
394     strm = &(state->strm);
395 
396     /* check that we're writing and that there's no error */
397     if (state->mode != GZ_WRITE || state->err != Z_OK)
398         return Z_STREAM_ERROR;
399 
400     /* make sure we have some buffer space */
401     if (state->size == 0 && gz_init(state) == -1)
402         return state->err;
403 

434 
435     /* check that printf() results fit in buffer */
436     if (len == 0 || (unsigned)len >= state->size || next[state->size - 1] != 0)
437         return 0;
438 
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;
447         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 
454 int ZEXPORTVA gzprintf(gzFile file, const char *format, ...) {

455     va_list va;
456     int ret;
457 
458     va_start(va, format);
459     ret = gzvprintf(file, format, va);
460     va_end(va);
461     return ret;
462 }
463 
464 #else /* !STDC && !Z_HAVE_STDARG_H */
465 
466 /* -- see zlib.h -- */
467 int ZEXPORTVA gzprintf(gzFile file, const char *format, int a1, int a2, int a3,
468                        int a4, int a5, int a6, int a7, int a8, int a9, int a10,
469                        int a11, int a12, int a13, int a14, int a15, int a16,
470                        int a17, int a18, int a19, int a20) {



471     unsigned len, left;
472     char *next;
473     gz_statep state;
474     z_streamp strm;
475 
476     /* get internal structure */
477     if (file == NULL)
478         return Z_STREAM_ERROR;
479     state = (gz_statep)file;
480     strm = &(state->strm);
481 
482     /* check that can really pass pointer in ints */
483     if (sizeof(int) != sizeof(void *))
484         return Z_STREAM_ERROR;
485 
486     /* check that we're writing and that there's no error */
487     if (state->mode != GZ_WRITE || state->err != Z_OK)
488         return Z_STREAM_ERROR;
489 
490     /* make sure we have some buffer space */

532         return 0;
533 
534     /* update buffer and position, compress first half if past that */
535     strm->avail_in += len;
536     state->x.pos += len;
537     if (strm->avail_in >= state->size) {
538         left = strm->avail_in - state->size;
539         strm->avail_in = state->size;
540         if (gz_comp(state, Z_NO_FLUSH) == -1)
541             return state->err;
542         memmove(state->in, state->in + state->size, left);
543         strm->next_in = state->in;
544         strm->avail_in = left;
545     }
546     return (int)len;
547 }
548 
549 #endif
550 
551 /* -- see zlib.h -- */
552 int ZEXPORT gzflush(gzFile file, int flush) {



553     gz_statep state;
554 
555     /* get internal structure */
556     if (file == NULL)
557         return Z_STREAM_ERROR;
558     state = (gz_statep)file;
559 
560     /* check that we're writing and that there's no error */
561     if (state->mode != GZ_WRITE || state->err != Z_OK)
562         return Z_STREAM_ERROR;
563 
564     /* check flush parameter */
565     if (flush < 0 || flush > Z_FINISH)
566         return Z_STREAM_ERROR;
567 
568     /* check for seek request */
569     if (state->seek) {
570         state->seek = 0;
571         if (gz_zero(state, state->skip) == -1)
572             return state->err;
573     }
574 
575     /* compress remaining data with requested flush */
576     (void)gz_comp(state, flush);
577     return state->err;
578 }
579 
580 /* -- see zlib.h -- */
581 int ZEXPORT gzsetparams(gzFile file, int level, int strategy) {




582     gz_statep state;
583     z_streamp strm;
584 
585     /* get internal structure */
586     if (file == NULL)
587         return Z_STREAM_ERROR;
588     state = (gz_statep)file;
589     strm = &(state->strm);
590 
591     /* check that we're writing and that there's no error */
592     if (state->mode != GZ_WRITE || state->err != Z_OK || state->direct)
593         return Z_STREAM_ERROR;
594 
595     /* if no change is requested, then do nothing */
596     if (level == state->level && strategy == state->strategy)
597         return Z_OK;
598 
599     /* check for seek request */
600     if (state->seek) {
601         state->seek = 0;
602         if (gz_zero(state, state->skip) == -1)
603             return state->err;
604     }
605 
606     /* change compression parameters for subsequent input */
607     if (state->size) {
608         /* flush previous input with previous parameters before changing */
609         if (strm->avail_in && gz_comp(state, Z_BLOCK) == -1)
610             return state->err;
611         deflateParams(strm, level, strategy);
612     }
613     state->level = level;
614     state->strategy = strategy;
615     return Z_OK;
616 }
617 
618 /* -- see zlib.h -- */
619 int ZEXPORT gzclose_w(gzFile file) {


620     int ret = Z_OK;
621     gz_statep state;
622 
623     /* get internal structure */
624     if (file == NULL)
625         return Z_STREAM_ERROR;
626     state = (gz_statep)file;
627 
628     /* check that we're writing */
629     if (state->mode != GZ_WRITE)
630         return Z_STREAM_ERROR;
631 
632     /* check for seek request */
633     if (state->seek) {
634         state->seek = 0;
635         if (gz_zero(state, state->skip) == -1)
636             ret = state->err;
637     }
638 
639     /* flush, free memory, and close file */
< prev index next >