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