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