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