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