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