]> git.lizzy.rs Git - zlib.git/blob - gzwrite.c
zlib 1.2.9
[zlib.git] / gzwrite.c
1 /* gzwrite.c -- zlib functions for writing gzip files
2  * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013, 2016 Mark Adler
3  * For conditions of distribution and use, see copyright notice in zlib.h
4  */
5
6 #include "gzguts.h"
7
8 /* Local functions */
9 local int gz_init OF((gz_statep));
10 local int gz_comp OF((gz_statep, int));
11 local int gz_zero OF((gz_statep, z_off64_t));
12 local z_size_t gz_write OF((gz_statep, voidpc, z_size_t));
13
14 /* Initialize state for writing a gzip file.  Mark initialization by setting
15    state->size to non-zero.  Return -1 on a memory allocation failure, or 0 on
16    success. */
17 local int gz_init(state)
18     gz_statep state;
19 {
20     int ret;
21     z_streamp strm = &(state->strm);
22
23     /* allocate input buffer (double size for gzprintf) */
24     state->in = (unsigned char *)malloc(state->want << 1);
25     if (state->in == NULL) {
26         gz_error(state, Z_MEM_ERROR, "out of memory");
27         return -1;
28     }
29
30     /* only need output buffer and deflate state if compressing */
31     if (!state->direct) {
32         /* allocate output buffer */
33         state->out = (unsigned char *)malloc(state->want);
34         if (state->out == NULL) {
35             free(state->in);
36             gz_error(state, Z_MEM_ERROR, "out of memory");
37             return -1;
38         }
39
40         /* allocate deflate memory, set up for gzip compression */
41         strm->zalloc = Z_NULL;
42         strm->zfree = Z_NULL;
43         strm->opaque = Z_NULL;
44         ret = deflateInit2(strm, state->level, Z_DEFLATED,
45                            MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
46         if (ret != Z_OK) {
47             free(state->out);
48             free(state->in);
49             gz_error(state, Z_MEM_ERROR, "out of memory");
50             return -1;
51         }
52         strm->next_in = NULL;
53     }
54
55     /* mark state as initialized */
56     state->size = state->want;
57
58     /* initialize write buffer if compressing */
59     if (!state->direct) {
60         strm->avail_out = state->size;
61         strm->next_out = state->out;
62         state->x.next = strm->next_out;
63     }
64     return 0;
65 }
66
67 /* Compress whatever is at avail_in and next_in and write to the output file.
68    Return -1 if there is an error writing to the output file or if gz_init()
69    fails to allocate memory, otherwise 0.  flush is assumed to be a valid
70    deflate() flush value.  If flush is Z_FINISH, then the deflate() state is
71    reset to start a new gzip stream.  If gz->direct is true, then simply write
72    to the output file without compressing, and ignore flush. */
73 local int gz_comp(state, flush)
74     gz_statep state;
75     int flush;
76 {
77     int ret, writ;
78     unsigned have, put, max = ((unsigned)-1 >> 2) + 1;
79     z_streamp strm = &(state->strm);
80
81     /* allocate memory if this is the first time through */
82     if (state->size == 0 && gz_init(state) == -1)
83         return -1;
84
85     /* write directly if requested */
86     if (state->direct) {
87         while (strm->avail_in) {
88             put = strm->avail_in > max ? max : strm->avail_in;
89             writ = write(state->fd, strm->next_in, put);
90             if (writ < 0) {
91                 gz_error(state, Z_ERRNO, zstrerror());
92                 return -1;
93             }
94             strm->avail_in -= (unsigned)writ;
95             strm->next_in += writ;
96         }
97         return 0;
98     }
99
100     /* run deflate() on provided input until it produces no more output */
101     ret = Z_OK;
102     do {
103         /* write out current buffer contents if full, or if flushing, but if
104            doing Z_FINISH then don't write until we get to Z_STREAM_END */
105         if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
106             (flush != Z_FINISH || ret == Z_STREAM_END))) {
107             while (strm->next_out > state->x.next) {
108                 put = strm->next_out - state->x.next > (int)max ? max :
109                       (unsigned)(strm->next_out - state->x.next);
110                 writ = write(state->fd, state->x.next, put);
111                 if (writ < 0) {
112                     gz_error(state, Z_ERRNO, zstrerror());
113                     return -1;
114                 }
115                 state->x.next += writ;
116             }
117             if (strm->avail_out == 0) {
118                 strm->avail_out = state->size;
119                 strm->next_out = state->out;
120             }
121         }
122
123         /* compress */
124         have = strm->avail_out;
125         ret = deflate(strm, flush);
126         if (ret == Z_STREAM_ERROR) {
127             gz_error(state, Z_STREAM_ERROR,
128                       "internal error: deflate stream corrupt");
129             return -1;
130         }
131         have -= strm->avail_out;
132     } while (have);
133
134     /* if that completed a deflate stream, allow another to start */
135     if (flush == Z_FINISH)
136         deflateReset(strm);
137
138     /* all done, no errors */
139     return 0;
140 }
141
142 /* Compress len zeros to output.  Return -1 on a write error or memory
143    allocation failure by gz_comp(), or 0 on success. */
144 local int gz_zero(state, len)
145     gz_statep state;
146     z_off64_t len;
147 {
148     int first;
149     unsigned n;
150     z_streamp strm = &(state->strm);
151
152     /* consume whatever's left in the input buffer */
153     if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
154         return -1;
155
156     /* compress len zeros (len guaranteed > 0) */
157     first = 1;
158     while (len) {
159         n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
160             (unsigned)len : state->size;
161         if (first) {
162             memset(state->in, 0, n);
163             first = 0;
164         }
165         strm->avail_in = n;
166         strm->next_in = state->in;
167         state->x.pos += n;
168         if (gz_comp(state, Z_NO_FLUSH) == -1)
169             return -1;
170         len -= n;
171     }
172     return 0;
173 }
174
175 /* Write len bytes from buf to file.  Return the number of bytes written.  If
176    the returned value is less than len, then there was an error. */
177 local z_size_t gz_write(state, buf, len)
178     gz_statep state;
179     voidpc buf;
180     z_size_t len;
181 {
182     z_size_t put = len;
183
184     /* if len is zero, avoid unnecessary operations */
185     if (len == 0)
186         return 0;
187
188     /* allocate memory if this is the first time through */
189     if (state->size == 0 && gz_init(state) == -1)
190         return 0;
191
192     /* check for seek request */
193     if (state->seek) {
194         state->seek = 0;
195         if (gz_zero(state, state->skip) == -1)
196             return 0;
197     }
198
199     /* for small len, copy to input buffer, otherwise compress directly */
200     if (len < state->size) {
201         /* copy to input buffer, compress when full */
202         do {
203             unsigned have, copy;
204
205             if (state->strm.avail_in == 0)
206                 state->strm.next_in = state->in;
207             have = (unsigned)((state->strm.next_in + state->strm.avail_in) -
208                               state->in);
209             copy = state->size - have;
210             if (copy > len)
211                 copy = len;
212             memcpy(state->in + have, buf, copy);
213             state->strm.avail_in += copy;
214             state->x.pos += copy;
215             buf = (const char *)buf + copy;
216             len -= copy;
217             if (len && gz_comp(state, Z_NO_FLUSH) == -1)
218                 return 0;
219         } while (len);
220     }
221     else {
222         /* consume whatever's left in the input buffer */
223         if (state->strm.avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
224             return 0;
225
226         /* directly compress user buffer to file */
227         state->strm.next_in = (z_const Bytef *)buf;
228         do {
229             unsigned n = (unsigned)-1;
230             if (n > len)
231                 n = len;
232             state->strm.avail_in = n;
233             state->x.pos += n;
234             if (gz_comp(state, Z_NO_FLUSH) == -1)
235                 return 0;
236             len -= n;
237         } while (len);
238     }
239
240     /* input was all buffered or compressed */
241     return put;
242 }
243
244 /* -- see zlib.h -- */
245 int ZEXPORT gzwrite(file, buf, len)
246     gzFile file;
247     voidpc buf;
248     unsigned len;
249 {
250     gz_statep state;
251
252     /* get internal structure */
253     if (file == NULL)
254         return 0;
255     state = (gz_statep)file;
256
257     /* check that we're writing and that there's no error */
258     if (state->mode != GZ_WRITE || state->err != Z_OK)
259         return 0;
260
261     /* since an int is returned, make sure len fits in one, otherwise return
262        with an error (this avoids a flaw in the interface) */
263     if ((int)len < 0) {
264         gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
265         return 0;
266     }
267
268     /* write len bytes from buf (the return value will fit in an int) */
269     return (int)gz_write(state, buf, len);
270 }
271
272 /* -- see zlib.h -- */
273 z_size_t ZEXPORT gzfwrite(buf, size, nitems, file)
274     voidpc buf;
275     z_size_t size;
276     z_size_t nitems;
277     gzFile file;
278 {
279     z_size_t len;
280     gz_statep state;
281
282     /* get internal structure */
283     if (file == NULL)
284         return 0;
285     state = (gz_statep)file;
286
287     /* check that we're writing and that there's no error */
288     if (state->mode != GZ_WRITE || state->err != Z_OK)
289         return 0;
290
291     /* compute bytes to read -- error on overflow */
292     len = nitems * size;
293     if (size && len / size != nitems) {
294         gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
295         return 0;
296     }
297
298     /* write len bytes to buf, return the number of full items written */
299     return len ? gz_write(state, buf, len) / size : 0;
300 }
301
302 /* -- see zlib.h -- */
303 int ZEXPORT gzputc(file, c)
304     gzFile file;
305     int c;
306 {
307     unsigned have;
308     unsigned char buf[1];
309     gz_statep state;
310     z_streamp strm;
311
312     /* get internal structure */
313     if (file == NULL)
314         return -1;
315     state = (gz_statep)file;
316     strm = &(state->strm);
317
318     /* check that we're writing and that there's no error */
319     if (state->mode != GZ_WRITE || state->err != Z_OK)
320         return -1;
321
322     /* check for seek request */
323     if (state->seek) {
324         state->seek = 0;
325         if (gz_zero(state, state->skip) == -1)
326             return -1;
327     }
328
329     /* try writing to input buffer for speed (state->size == 0 if buffer not
330        initialized) */
331     if (state->size) {
332         if (strm->avail_in == 0)
333             strm->next_in = state->in;
334         have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
335         if (have < state->size) {
336             state->in[have] = (unsigned char)c;
337             strm->avail_in++;
338             state->x.pos++;
339             return c & 0xff;
340         }
341     }
342
343     /* no room in buffer or not initialized, use gz_write() */
344     buf[0] = (unsigned char)c;
345     if (gz_write(state, buf, 1) != 1)
346         return -1;
347     return c & 0xff;
348 }
349
350 /* -- see zlib.h -- */
351 int ZEXPORT gzputs(file, str)
352     gzFile file;
353     const char *str;
354 {
355     int ret;
356     z_size_t len;
357     gz_statep state;
358
359     /* get internal structure */
360     if (file == NULL)
361         return -1;
362     state = (gz_statep)file;
363
364     /* check that we're writing and that there's no error */
365     if (state->mode != GZ_WRITE || state->err != Z_OK)
366         return -1;
367
368     /* write string */
369     len = strlen(str);
370     ret = gz_write(state, str, len);
371     return ret == 0 && len != 0 ? -1 : ret;
372 }
373
374 #if defined(STDC) || defined(Z_HAVE_STDARG_H)
375 #include <stdarg.h>
376
377 /* -- see zlib.h -- */
378 int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
379 {
380     int len;
381     unsigned left;
382     char *next;
383     gz_statep state;
384     z_streamp strm;
385
386     /* get internal structure */
387     if (file == NULL)
388         return Z_STREAM_ERROR;
389     state = (gz_statep)file;
390     strm = &(state->strm);
391
392     /* check that we're writing and that there's no error */
393     if (state->mode != GZ_WRITE || state->err != Z_OK)
394         return Z_STREAM_ERROR;
395
396     /* make sure we have some buffer space */
397     if (state->size == 0 && gz_init(state) == -1)
398         return state->err;
399
400     /* check for seek request */
401     if (state->seek) {
402         state->seek = 0;
403         if (gz_zero(state, state->skip) == -1)
404             return state->err;
405     }
406
407     /* do the printf() into the input buffer, put length in len -- the input
408        buffer is double-sized just for this function, so there is guaranteed to
409        be state->size bytes available after the current contents */
410     if (strm->avail_in == 0)
411         strm->next_in = state->in;
412     next = (char *)(state->in + (strm->next_in - state->in) + strm->avail_in);
413     next[state->size - 1] = 0;
414 #ifdef NO_vsnprintf
415 #  ifdef HAS_vsprintf_void
416     (void)vsprintf(next, format, va);
417     for (len = 0; len < state->size; len++)
418         if (next[len] == 0) break;
419 #  else
420     len = vsprintf(next, format, va);
421 #  endif
422 #else
423 #  ifdef HAS_vsnprintf_void
424     (void)vsnprintf(next, state->size, format, va);
425     len = strlen(next);
426 #  else
427     len = vsnprintf(next, state->size, format, va);
428 #  endif
429 #endif
430
431     /* check that printf() results fit in buffer */
432     if (len == 0 || (unsigned)len >= state->size || next[state->size - 1] != 0)
433         return 0;
434
435     /* update buffer and position, compress first half if past that */
436     strm->avail_in += (unsigned)len;
437     state->x.pos += len;
438     if (strm->avail_in >= state->size) {
439         left = strm->avail_in - state->size;
440         strm->avail_in = state->size;
441         if (gz_comp(state, Z_NO_FLUSH) == -1)
442             return state->err;
443         memcpy(state->in, state->in + state->size, left);
444         strm->next_in = state->in;
445         strm->avail_in = left;
446     }
447     return len;
448 }
449
450 int ZEXPORTVA gzprintf(gzFile file, const char *format, ...)
451 {
452     va_list va;
453     int ret;
454
455     va_start(va, format);
456     ret = gzvprintf(file, format, va);
457     va_end(va);
458     return ret;
459 }
460
461 #else /* !STDC && !Z_HAVE_STDARG_H */
462
463 /* -- see zlib.h -- */
464 int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
465                        a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
466     gzFile file;
467     const char *format;
468     int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
469         a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
470 {
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 */
491     if (state->size == 0 && gz_init(state) == -1)
492         return state->error;
493
494     /* check for seek request */
495     if (state->seek) {
496         state->seek = 0;
497         if (gz_zero(state, state->skip) == -1)
498             return state->error;
499     }
500
501     /* do the printf() into the input buffer, put length in len -- the input
502        buffer is double-sized just for this function, so there is guaranteed to
503        be state->size bytes available after the current contents */
504     if (strm->avail_in == 0)
505         strm->next_in = state->in;
506     next = (char *)(strm->next_in + strm->avail_in);
507     next[state->size - 1] = 0;
508 #ifdef NO_snprintf
509 #  ifdef HAS_sprintf_void
510     sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12,
511             a13, a14, a15, a16, a17, a18, a19, a20);
512     for (len = 0; len < size; len++)
513         if (next[len] == 0)
514             break;
515 #  else
516     len = sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11,
517                   a12, a13, a14, a15, a16, a17, a18, a19, a20);
518 #  endif
519 #else
520 #  ifdef HAS_snprintf_void
521     snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8, a9,
522              a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
523     len = strlen(next);
524 #  else
525     len = snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8,
526                    a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
527 #  endif
528 #endif
529
530     /* check that printf() results fit in buffer */
531     if (len == 0 || len >= state->size || next[state->size - 1] != 0)
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         memcpy(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(file, flush)
553     gzFile file;
554     int flush;
555 {
556     gz_statep state;
557
558     /* get internal structure */
559     if (file == NULL)
560         return Z_STREAM_ERROR;
561     state = (gz_statep)file;
562
563     /* check that we're writing and that there's no error */
564     if (state->mode != GZ_WRITE || state->err != Z_OK)
565         return Z_STREAM_ERROR;
566
567     /* check flush parameter */
568     if (flush < 0 || flush > Z_FINISH)
569         return Z_STREAM_ERROR;
570
571     /* check for seek request */
572     if (state->seek) {
573         state->seek = 0;
574         if (gz_zero(state, state->skip) == -1)
575             return state->err;
576     }
577
578     /* compress remaining data with requested flush */
579     (void)gz_comp(state, flush);
580     return state->err;
581 }
582
583 /* -- see zlib.h -- */
584 int ZEXPORT gzsetparams(file, level, strategy)
585     gzFile file;
586     int level;
587     int strategy;
588 {
589     gz_statep state;
590     z_streamp strm;
591
592     /* get internal structure */
593     if (file == NULL)
594         return Z_STREAM_ERROR;
595     state = (gz_statep)file;
596     strm = &(state->strm);
597
598     /* check that we're writing and that there's no error */
599     if (state->mode != GZ_WRITE || state->err != Z_OK)
600         return Z_STREAM_ERROR;
601
602     /* if no change is requested, then do nothing */
603     if (level == state->level && strategy == state->strategy)
604         return Z_OK;
605
606     /* check for seek request */
607     if (state->seek) {
608         state->seek = 0;
609         if (gz_zero(state, state->skip) == -1)
610             return state->err;
611     }
612
613     /* change compression parameters for subsequent input */
614     if (state->size) {
615         /* flush previous input with previous parameters before changing */
616         if (strm->avail_in && gz_comp(state, Z_BLOCK) == -1)
617             return state->err;
618         deflateParams(strm, level, strategy);
619     }
620     state->level = level;
621     state->strategy = strategy;
622     return Z_OK;
623 }
624
625 /* -- see zlib.h -- */
626 int ZEXPORT gzclose_w(file)
627     gzFile file;
628 {
629     int ret = Z_OK;
630     gz_statep state;
631
632     /* get internal structure */
633     if (file == NULL)
634         return Z_STREAM_ERROR;
635     state = (gz_statep)file;
636
637     /* check that we're writing */
638     if (state->mode != GZ_WRITE)
639         return Z_STREAM_ERROR;
640
641     /* check for seek request */
642     if (state->seek) {
643         state->seek = 0;
644         if (gz_zero(state, state->skip) == -1)
645             ret = state->err;
646     }
647
648     /* flush, free memory, and close file */
649     if (gz_comp(state, Z_FINISH) == -1)
650         ret = state->err;
651     if (state->size) {
652         if (!state->direct) {
653             (void)deflateEnd(&(state->strm));
654             free(state->out);
655         }
656         free(state->in);
657     }
658     gz_error(state, Z_OK, NULL);
659     free(state->path);
660     if (close(state->fd) == -1)
661         ret = Z_ERRNO;
662     free(state);
663     return ret;
664 }