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