1 | /* gzwrite.c -- zlib functions for writing gzip files
|
---|
2 | * Copyright (C) 2004-2019 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 | local z_size_t gz_write OF((gz_statep, voidpc, z_size_t));
|
---|
13 |
|
---|
14 | /* Initialize state for writing a gzip file. Mark initialization by setting
|
---|
15 | state->size to non-zero. Return -1 on a memory allocation failure, or 0 on
|
---|
16 | 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 buffer (double size for gzprintf) */
|
---|
24 | state->in = (unsigned char *)malloc(state->want << 1);
|
---|
25 | if (state->in == NULL) {
|
---|
26 | gz_error(state, Z_MEM_ERROR, "out of memory");
|
---|
27 | return -1;
|
---|
28 | }
|
---|
29 |
|
---|
30 | /* only need output buffer and deflate state if compressing */
|
---|
31 | if (!state->direct) {
|
---|
32 | /* allocate output buffer */
|
---|
33 | state->out = (unsigned char *)malloc(state->want);
|
---|
34 | if (state->out == NULL) {
|
---|
35 | free(state->in);
|
---|
36 | gz_error(state, Z_MEM_ERROR, "out of memory");
|
---|
37 | return -1;
|
---|
38 | }
|
---|
39 |
|
---|
40 | /* allocate deflate memory, set up for gzip compression */
|
---|
41 | strm->zalloc = Z_NULL;
|
---|
42 | strm->zfree = Z_NULL;
|
---|
43 | strm->opaque = Z_NULL;
|
---|
44 | ret = deflateInit2(strm, state->level, Z_DEFLATED,
|
---|
45 | MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy);
|
---|
46 | if (ret != Z_OK) {
|
---|
47 | free(state->out);
|
---|
48 | free(state->in);
|
---|
49 | gz_error(state, Z_MEM_ERROR, "out of memory");
|
---|
50 | return -1;
|
---|
51 | }
|
---|
52 | strm->next_in = NULL;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /* mark state as initialized */
|
---|
56 | state->size = state->want;
|
---|
57 |
|
---|
58 | /* initialize write buffer if compressing */
|
---|
59 | if (!state->direct) {
|
---|
60 | strm->avail_out = state->size;
|
---|
61 | strm->next_out = state->out;
|
---|
62 | state->x.next = strm->next_out;
|
---|
63 | }
|
---|
64 | return 0;
|
---|
65 | }
|
---|
66 |
|
---|
67 | #ifdef IPRT_NO_CRT /* VBox */
|
---|
68 | DECLINLINE(int) gz_write_wrap(gz_statep state, void const *pvSrc, size_t cbToWrite) /* VBox */
|
---|
69 | { /* VBox */
|
---|
70 | size_t cbWritten; /* VBox */
|
---|
71 | int ret = RTFileWrite(state->fd, pvSrc, cbToWrite, &cbWritten); /* VBox */
|
---|
72 | if (RT_SUCCESS(ret)) /* VBox */
|
---|
73 | ret = (int)cbWritten; /* VBox */
|
---|
74 | else { /* VBox */
|
---|
75 | char szDefine[80]; /* VBox */
|
---|
76 | RTErrQueryDefine(ret, szDefine, sizeof(szDefine), false); /* VBox */
|
---|
77 | gz_error(state, Z_ERRNO, szDefine); /* VBox */
|
---|
78 | ret = -1; /* VBox */
|
---|
79 | } /* VBox */
|
---|
80 | return ret; /* VBox */
|
---|
81 | } /* VBox */
|
---|
82 | #endif /* IPRT_NO_CRT */ /* VBox */
|
---|
83 |
|
---|
84 | /* Compress whatever is at avail_in and next_in and write to the output file.
|
---|
85 | Return -1 if there is an error writing to the output file or if gz_init()
|
---|
86 | fails to allocate memory, otherwise 0. flush is assumed to be a valid
|
---|
87 | deflate() flush value. If flush is Z_FINISH, then the deflate() state is
|
---|
88 | reset to start a new gzip stream. If gz->direct is true, then simply write
|
---|
89 | to the output file without compressing, and ignore flush. */
|
---|
90 | local int gz_comp(state, flush)
|
---|
91 | gz_statep state;
|
---|
92 | int flush;
|
---|
93 | {
|
---|
94 | int ret, writ;
|
---|
95 | unsigned have, put, max = ((unsigned)-1 >> 2) + 1;
|
---|
96 | z_streamp strm = &(state->strm);
|
---|
97 |
|
---|
98 | /* allocate memory if this is the first time through */
|
---|
99 | if (state->size == 0 && gz_init(state) == -1)
|
---|
100 | return -1;
|
---|
101 |
|
---|
102 | /* write directly if requested */
|
---|
103 | if (state->direct) {
|
---|
104 | while (strm->avail_in) {
|
---|
105 | put = strm->avail_in > max ? max : strm->avail_in;
|
---|
106 | #ifndef IPRT_NO_CRT /* VBox */
|
---|
107 | writ = write(state->fd, strm->next_in, put);
|
---|
108 | if (writ < 0) {
|
---|
109 | gz_error(state, Z_ERRNO, zstrerror());
|
---|
110 | return -1;
|
---|
111 | }
|
---|
112 | #else /* VBox */
|
---|
113 | writ = gz_write_wrap(state, strm->next_in, put); /* VBox */
|
---|
114 | if (writ < 0) /* VBox */
|
---|
115 | return -1; /* VBox */
|
---|
116 | #endif /* VBox */
|
---|
117 | strm->avail_in -= (unsigned)writ;
|
---|
118 | strm->next_in += writ;
|
---|
119 | }
|
---|
120 | return 0;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /* check for a pending reset */
|
---|
124 | if (state->reset) {
|
---|
125 | /* don't start a new gzip member unless there is data to write */
|
---|
126 | if (strm->avail_in == 0)
|
---|
127 | return 0;
|
---|
128 | deflateReset(strm);
|
---|
129 | state->reset = 0;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /* run deflate() on provided input until it produces no more output */
|
---|
133 | ret = Z_OK;
|
---|
134 | do {
|
---|
135 | /* write out current buffer contents if full, or if flushing, but if
|
---|
136 | doing Z_FINISH then don't write until we get to Z_STREAM_END */
|
---|
137 | if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
|
---|
138 | (flush != Z_FINISH || ret == Z_STREAM_END))) {
|
---|
139 | while (strm->next_out > state->x.next) {
|
---|
140 | put = strm->next_out - state->x.next > (int)max ? max :
|
---|
141 | (unsigned)(strm->next_out - state->x.next);
|
---|
142 | #ifndef IPRT_NO_CRT /* VBox */
|
---|
143 | writ = write(state->fd, state->x.next, put);
|
---|
144 | if (writ < 0) {
|
---|
145 | gz_error(state, Z_ERRNO, zstrerror());
|
---|
146 | return -1;
|
---|
147 | }
|
---|
148 | #else /* VBox */
|
---|
149 | writ = gz_write_wrap(state, state->x.next, put); /* VBox */
|
---|
150 | if (writ < 0) /* VBox */
|
---|
151 | return -1; /* VBox */
|
---|
152 | #endif /* VBox */
|
---|
153 | state->x.next += writ;
|
---|
154 | }
|
---|
155 | if (strm->avail_out == 0) {
|
---|
156 | strm->avail_out = state->size;
|
---|
157 | strm->next_out = state->out;
|
---|
158 | state->x.next = state->out;
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | /* compress */
|
---|
163 | have = strm->avail_out;
|
---|
164 | ret = deflate(strm, flush);
|
---|
165 | if (ret == Z_STREAM_ERROR) {
|
---|
166 | gz_error(state, Z_STREAM_ERROR,
|
---|
167 | "internal error: deflate stream corrupt");
|
---|
168 | return -1;
|
---|
169 | }
|
---|
170 | have -= strm->avail_out;
|
---|
171 | } while (have);
|
---|
172 |
|
---|
173 | /* if that completed a deflate stream, allow another to start */
|
---|
174 | if (flush == Z_FINISH)
|
---|
175 | state->reset = 1;
|
---|
176 |
|
---|
177 | /* all done, no errors */
|
---|
178 | return 0;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /* Compress len zeros to output. Return -1 on a write error or memory
|
---|
182 | allocation failure by gz_comp(), or 0 on success. */
|
---|
183 | local int gz_zero(state, len)
|
---|
184 | gz_statep state;
|
---|
185 | z_off64_t len;
|
---|
186 | {
|
---|
187 | int first;
|
---|
188 | unsigned n;
|
---|
189 | z_streamp strm = &(state->strm);
|
---|
190 |
|
---|
191 | /* consume whatever's left in the input buffer */
|
---|
192 | if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
|
---|
193 | return -1;
|
---|
194 |
|
---|
195 | /* compress len zeros (len guaranteed > 0) */
|
---|
196 | first = 1;
|
---|
197 | while (len) {
|
---|
198 | n = GT_OFF(state->size) || (z_off64_t)state->size > len ?
|
---|
199 | (unsigned)len : state->size;
|
---|
200 | if (first) {
|
---|
201 | memset(state->in, 0, n);
|
---|
202 | first = 0;
|
---|
203 | }
|
---|
204 | strm->avail_in = n;
|
---|
205 | strm->next_in = state->in;
|
---|
206 | state->x.pos += n;
|
---|
207 | if (gz_comp(state, Z_NO_FLUSH) == -1)
|
---|
208 | return -1;
|
---|
209 | len -= n;
|
---|
210 | }
|
---|
211 | return 0;
|
---|
212 | }
|
---|
213 |
|
---|
214 | /* Write len bytes from buf to file. Return the number of bytes written. If
|
---|
215 | the returned value is less than len, then there was an error. */
|
---|
216 | local z_size_t gz_write(state, buf, len)
|
---|
217 | gz_statep state;
|
---|
218 | voidpc buf;
|
---|
219 | z_size_t len;
|
---|
220 | {
|
---|
221 | z_size_t put = len;
|
---|
222 |
|
---|
223 | /* if len is zero, avoid unnecessary operations */
|
---|
224 | if (len == 0)
|
---|
225 | return 0;
|
---|
226 |
|
---|
227 | /* allocate memory if this is the first time through */
|
---|
228 | if (state->size == 0 && gz_init(state) == -1)
|
---|
229 | return 0;
|
---|
230 |
|
---|
231 | /* check for seek request */
|
---|
232 | if (state->seek) {
|
---|
233 | state->seek = 0;
|
---|
234 | if (gz_zero(state, state->skip) == -1)
|
---|
235 | return 0;
|
---|
236 | }
|
---|
237 |
|
---|
238 | /* for small len, copy to input buffer, otherwise compress directly */
|
---|
239 | if (len < state->size) {
|
---|
240 | /* copy to input buffer, compress when full */
|
---|
241 | do {
|
---|
242 | unsigned have, copy;
|
---|
243 |
|
---|
244 | if (state->strm.avail_in == 0)
|
---|
245 | state->strm.next_in = state->in;
|
---|
246 | have = (unsigned)((state->strm.next_in + state->strm.avail_in) -
|
---|
247 | state->in);
|
---|
248 | copy = state->size - have;
|
---|
249 | if (copy > len)
|
---|
250 | copy = (unsigned)len;
|
---|
251 | memcpy(state->in + have, buf, copy);
|
---|
252 | state->strm.avail_in += copy;
|
---|
253 | state->x.pos += copy;
|
---|
254 | buf = (const char *)buf + copy;
|
---|
255 | len -= copy;
|
---|
256 | if (len && gz_comp(state, Z_NO_FLUSH) == -1)
|
---|
257 | return 0;
|
---|
258 | } while (len);
|
---|
259 | }
|
---|
260 | else {
|
---|
261 | /* consume whatever's left in the input buffer */
|
---|
262 | if (state->strm.avail_in && gz_comp(state, Z_NO_FLUSH) == -1)
|
---|
263 | return 0;
|
---|
264 |
|
---|
265 | /* directly compress user buffer to file */
|
---|
266 | state->strm.next_in = (z_const Bytef *)buf;
|
---|
267 | do {
|
---|
268 | unsigned n = (unsigned)-1;
|
---|
269 | if (n > len)
|
---|
270 | n = (unsigned)len;
|
---|
271 | state->strm.avail_in = n;
|
---|
272 | state->x.pos += n;
|
---|
273 | if (gz_comp(state, Z_NO_FLUSH) == -1)
|
---|
274 | return 0;
|
---|
275 | len -= n;
|
---|
276 | } while (len);
|
---|
277 | }
|
---|
278 |
|
---|
279 | /* input was all buffered or compressed */
|
---|
280 | return put;
|
---|
281 | }
|
---|
282 |
|
---|
283 | /* -- see zlib.h -- */
|
---|
284 | int ZEXPORT gzwrite(file, buf, len)
|
---|
285 | gzFile file;
|
---|
286 | voidpc buf;
|
---|
287 | unsigned len;
|
---|
288 | {
|
---|
289 | gz_statep state;
|
---|
290 |
|
---|
291 | /* get internal structure */
|
---|
292 | if (file == NULL)
|
---|
293 | return 0;
|
---|
294 | state = (gz_statep)file;
|
---|
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 | /* since an int is returned, make sure len fits in one, otherwise return
|
---|
301 | with an error (this avoids a flaw in the interface) */
|
---|
302 | if ((int)len < 0) {
|
---|
303 | gz_error(state, Z_DATA_ERROR, "requested length does not fit in int");
|
---|
304 | return 0;
|
---|
305 | }
|
---|
306 |
|
---|
307 | /* write len bytes from buf (the return value will fit in an int) */
|
---|
308 | return (int)gz_write(state, buf, len);
|
---|
309 | }
|
---|
310 |
|
---|
311 | /* -- see zlib.h -- */
|
---|
312 | z_size_t ZEXPORT gzfwrite(buf, size, nitems, file)
|
---|
313 | voidpc buf;
|
---|
314 | z_size_t size;
|
---|
315 | z_size_t nitems;
|
---|
316 | gzFile file;
|
---|
317 | {
|
---|
318 | z_size_t len;
|
---|
319 | gz_statep state;
|
---|
320 |
|
---|
321 | /* get internal structure */
|
---|
322 | if (file == NULL)
|
---|
323 | return 0;
|
---|
324 | state = (gz_statep)file;
|
---|
325 |
|
---|
326 | /* check that we're writing and that there's no error */
|
---|
327 | if (state->mode != GZ_WRITE || state->err != Z_OK)
|
---|
328 | return 0;
|
---|
329 |
|
---|
330 | /* compute bytes to read -- error on overflow */
|
---|
331 | len = nitems * size;
|
---|
332 | if (size && len / size != nitems) {
|
---|
333 | gz_error(state, Z_STREAM_ERROR, "request does not fit in a size_t");
|
---|
334 | return 0;
|
---|
335 | }
|
---|
336 |
|
---|
337 | /* write len bytes to buf, return the number of full items written */
|
---|
338 | return len ? gz_write(state, buf, len) / size : 0;
|
---|
339 | }
|
---|
340 |
|
---|
341 | /* -- see zlib.h -- */
|
---|
342 | int ZEXPORT gzputc(file, c)
|
---|
343 | gzFile file;
|
---|
344 | int c;
|
---|
345 | {
|
---|
346 | unsigned have;
|
---|
347 | unsigned char buf[1];
|
---|
348 | gz_statep state;
|
---|
349 | z_streamp strm;
|
---|
350 |
|
---|
351 | /* get internal structure */
|
---|
352 | if (file == NULL)
|
---|
353 | return -1;
|
---|
354 | state = (gz_statep)file;
|
---|
355 | strm = &(state->strm);
|
---|
356 |
|
---|
357 | /* check that we're writing and that there's no error */
|
---|
358 | if (state->mode != GZ_WRITE || state->err != Z_OK)
|
---|
359 | return -1;
|
---|
360 |
|
---|
361 | /* check for seek request */
|
---|
362 | if (state->seek) {
|
---|
363 | state->seek = 0;
|
---|
364 | if (gz_zero(state, state->skip) == -1)
|
---|
365 | return -1;
|
---|
366 | }
|
---|
367 |
|
---|
368 | /* try writing to input buffer for speed (state->size == 0 if buffer not
|
---|
369 | initialized) */
|
---|
370 | if (state->size) {
|
---|
371 | if (strm->avail_in == 0)
|
---|
372 | strm->next_in = state->in;
|
---|
373 | have = (unsigned)((strm->next_in + strm->avail_in) - state->in);
|
---|
374 | if (have < state->size) {
|
---|
375 | state->in[have] = (unsigned char)c;
|
---|
376 | strm->avail_in++;
|
---|
377 | state->x.pos++;
|
---|
378 | return c & 0xff;
|
---|
379 | }
|
---|
380 | }
|
---|
381 |
|
---|
382 | /* no room in buffer or not initialized, use gz_write() */
|
---|
383 | buf[0] = (unsigned char)c;
|
---|
384 | if (gz_write(state, buf, 1) != 1)
|
---|
385 | return -1;
|
---|
386 | return c & 0xff;
|
---|
387 | }
|
---|
388 |
|
---|
389 | /* -- see zlib.h -- */
|
---|
390 | int ZEXPORT gzputs(file, s)
|
---|
391 | gzFile file;
|
---|
392 | const char *s;
|
---|
393 | {
|
---|
394 | z_size_t len, put;
|
---|
395 | gz_statep state;
|
---|
396 |
|
---|
397 | /* get internal structure */
|
---|
398 | if (file == NULL)
|
---|
399 | return -1;
|
---|
400 | state = (gz_statep)file;
|
---|
401 |
|
---|
402 | /* check that we're writing and that there's no error */
|
---|
403 | if (state->mode != GZ_WRITE || state->err != Z_OK)
|
---|
404 | return -1;
|
---|
405 |
|
---|
406 | /* write string */
|
---|
407 | len = strlen(s);
|
---|
408 | if ((int)len < 0 || (unsigned)len != len) {
|
---|
409 | gz_error(state, Z_STREAM_ERROR, "string length does not fit in int");
|
---|
410 | return -1;
|
---|
411 | }
|
---|
412 | put = gz_write(state, s, len);
|
---|
413 | return put < len ? -1 : (int)len;
|
---|
414 | }
|
---|
415 |
|
---|
416 | #if defined(STDC) || defined(Z_HAVE_STDARG_H)
|
---|
417 | #include <stdarg.h>
|
---|
418 |
|
---|
419 | /* -- see zlib.h -- */
|
---|
420 | int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va)
|
---|
421 | {
|
---|
422 | int len;
|
---|
423 | unsigned left;
|
---|
424 | char *next;
|
---|
425 | gz_statep state;
|
---|
426 | z_streamp strm;
|
---|
427 |
|
---|
428 | /* get internal structure */
|
---|
429 | if (file == NULL)
|
---|
430 | return Z_STREAM_ERROR;
|
---|
431 | state = (gz_statep)file;
|
---|
432 | strm = &(state->strm);
|
---|
433 |
|
---|
434 | /* check that we're writing and that there's no error */
|
---|
435 | if (state->mode != GZ_WRITE || state->err != Z_OK)
|
---|
436 | return Z_STREAM_ERROR;
|
---|
437 |
|
---|
438 | /* make sure we have some buffer space */
|
---|
439 | if (state->size == 0 && gz_init(state) == -1)
|
---|
440 | return state->err;
|
---|
441 |
|
---|
442 | /* check for seek request */
|
---|
443 | if (state->seek) {
|
---|
444 | state->seek = 0;
|
---|
445 | if (gz_zero(state, state->skip) == -1)
|
---|
446 | return state->err;
|
---|
447 | }
|
---|
448 |
|
---|
449 | /* do the printf() into the input buffer, put length in len -- the input
|
---|
450 | buffer is double-sized just for this function, so there is guaranteed to
|
---|
451 | be state->size bytes available after the current contents */
|
---|
452 | if (strm->avail_in == 0)
|
---|
453 | strm->next_in = state->in;
|
---|
454 | next = (char *)(state->in + (strm->next_in - state->in) + strm->avail_in);
|
---|
455 | next[state->size - 1] = 0;
|
---|
456 | #ifdef NO_vsnprintf
|
---|
457 | # ifdef HAS_vsprintf_void
|
---|
458 | (void)vsprintf(next, format, va);
|
---|
459 | for (len = 0; len < state->size; len++)
|
---|
460 | if (next[len] == 0) break;
|
---|
461 | # else
|
---|
462 | len = vsprintf(next, format, va);
|
---|
463 | # endif
|
---|
464 | #else
|
---|
465 | # ifdef HAS_vsnprintf_void
|
---|
466 | (void)vsnprintf(next, state->size, format, va);
|
---|
467 | len = strlen(next);
|
---|
468 | # else
|
---|
469 | len = vsnprintf(next, state->size, format, va);
|
---|
470 | # endif
|
---|
471 | #endif
|
---|
472 |
|
---|
473 | /* check that printf() results fit in buffer */
|
---|
474 | if (len == 0 || (unsigned)len >= state->size || next[state->size - 1] != 0)
|
---|
475 | return 0;
|
---|
476 |
|
---|
477 | /* update buffer and position, compress first half if past that */
|
---|
478 | strm->avail_in += (unsigned)len;
|
---|
479 | state->x.pos += len;
|
---|
480 | if (strm->avail_in >= state->size) {
|
---|
481 | left = strm->avail_in - state->size;
|
---|
482 | strm->avail_in = state->size;
|
---|
483 | if (gz_comp(state, Z_NO_FLUSH) == -1)
|
---|
484 | return state->err;
|
---|
485 | memmove(state->in, state->in + state->size, left);
|
---|
486 | strm->next_in = state->in;
|
---|
487 | strm->avail_in = left;
|
---|
488 | }
|
---|
489 | return len;
|
---|
490 | }
|
---|
491 |
|
---|
492 | int ZEXPORTVA gzprintf(gzFile file, const char *format, ...)
|
---|
493 | {
|
---|
494 | va_list va;
|
---|
495 | int ret;
|
---|
496 |
|
---|
497 | va_start(va, format);
|
---|
498 | ret = gzvprintf(file, format, va);
|
---|
499 | va_end(va);
|
---|
500 | return ret;
|
---|
501 | }
|
---|
502 |
|
---|
503 | #else /* !STDC && !Z_HAVE_STDARG_H */
|
---|
504 |
|
---|
505 | /* -- see zlib.h -- */
|
---|
506 | int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
|
---|
507 | a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
|
---|
508 | gzFile file;
|
---|
509 | const char *format;
|
---|
510 | int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10,
|
---|
511 | a11, a12, a13, a14, a15, a16, a17, a18, a19, a20;
|
---|
512 | {
|
---|
513 | unsigned len, left;
|
---|
514 | char *next;
|
---|
515 | gz_statep state;
|
---|
516 | z_streamp strm;
|
---|
517 |
|
---|
518 | /* get internal structure */
|
---|
519 | if (file == NULL)
|
---|
520 | return Z_STREAM_ERROR;
|
---|
521 | state = (gz_statep)file;
|
---|
522 | strm = &(state->strm);
|
---|
523 |
|
---|
524 | /* check that can really pass pointer in ints */
|
---|
525 | if (sizeof(int) != sizeof(void *))
|
---|
526 | return Z_STREAM_ERROR;
|
---|
527 |
|
---|
528 | /* check that we're writing and that there's no error */
|
---|
529 | if (state->mode != GZ_WRITE || state->err != Z_OK)
|
---|
530 | return Z_STREAM_ERROR;
|
---|
531 |
|
---|
532 | /* make sure we have some buffer space */
|
---|
533 | if (state->size == 0 && gz_init(state) == -1)
|
---|
534 | return state->error;
|
---|
535 |
|
---|
536 | /* check for seek request */
|
---|
537 | if (state->seek) {
|
---|
538 | state->seek = 0;
|
---|
539 | if (gz_zero(state, state->skip) == -1)
|
---|
540 | return state->error;
|
---|
541 | }
|
---|
542 |
|
---|
543 | /* do the printf() into the input buffer, put length in len -- the input
|
---|
544 | buffer is double-sized just for this function, so there is guaranteed to
|
---|
545 | be state->size bytes available after the current contents */
|
---|
546 | if (strm->avail_in == 0)
|
---|
547 | strm->next_in = state->in;
|
---|
548 | next = (char *)(strm->next_in + strm->avail_in);
|
---|
549 | next[state->size - 1] = 0;
|
---|
550 | #ifdef NO_snprintf
|
---|
551 | # ifdef HAS_sprintf_void
|
---|
552 | sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12,
|
---|
553 | a13, a14, a15, a16, a17, a18, a19, a20);
|
---|
554 | for (len = 0; len < size; len++)
|
---|
555 | if (next[len] == 0)
|
---|
556 | break;
|
---|
557 | # else
|
---|
558 | len = sprintf(next, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11,
|
---|
559 | a12, a13, a14, a15, a16, a17, a18, a19, a20);
|
---|
560 | # endif
|
---|
561 | #else
|
---|
562 | # ifdef HAS_snprintf_void
|
---|
563 | snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8, a9,
|
---|
564 | a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
|
---|
565 | len = strlen(next);
|
---|
566 | # else
|
---|
567 | len = snprintf(next, state->size, format, a1, a2, a3, a4, a5, a6, a7, a8,
|
---|
568 | a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20);
|
---|
569 | # endif
|
---|
570 | #endif
|
---|
571 |
|
---|
572 | /* check that printf() results fit in buffer */
|
---|
573 | if (len == 0 || len >= state->size || next[state->size - 1] != 0)
|
---|
574 | return 0;
|
---|
575 |
|
---|
576 | /* update buffer and position, compress first half if past that */
|
---|
577 | strm->avail_in += len;
|
---|
578 | state->x.pos += len;
|
---|
579 | if (strm->avail_in >= state->size) {
|
---|
580 | left = strm->avail_in - state->size;
|
---|
581 | strm->avail_in = state->size;
|
---|
582 | if (gz_comp(state, Z_NO_FLUSH) == -1)
|
---|
583 | return state->err;
|
---|
584 | memmove(state->in, state->in + state->size, left);
|
---|
585 | strm->next_in = state->in;
|
---|
586 | strm->avail_in = left;
|
---|
587 | }
|
---|
588 | return (int)len;
|
---|
589 | }
|
---|
590 |
|
---|
591 | #endif
|
---|
592 |
|
---|
593 | /* -- see zlib.h -- */
|
---|
594 | int ZEXPORT gzflush(file, flush)
|
---|
595 | gzFile file;
|
---|
596 | int flush;
|
---|
597 | {
|
---|
598 | gz_statep state;
|
---|
599 |
|
---|
600 | /* get internal structure */
|
---|
601 | if (file == NULL)
|
---|
602 | return Z_STREAM_ERROR;
|
---|
603 | state = (gz_statep)file;
|
---|
604 |
|
---|
605 | /* check that we're writing and that there's no error */
|
---|
606 | if (state->mode != GZ_WRITE || state->err != Z_OK)
|
---|
607 | return Z_STREAM_ERROR;
|
---|
608 |
|
---|
609 | /* check flush parameter */
|
---|
610 | if (flush < 0 || flush > Z_FINISH)
|
---|
611 | return Z_STREAM_ERROR;
|
---|
612 |
|
---|
613 | /* check for seek request */
|
---|
614 | if (state->seek) {
|
---|
615 | state->seek = 0;
|
---|
616 | if (gz_zero(state, state->skip) == -1)
|
---|
617 | return state->err;
|
---|
618 | }
|
---|
619 |
|
---|
620 | /* compress remaining data with requested flush */
|
---|
621 | (void)gz_comp(state, flush);
|
---|
622 | return state->err;
|
---|
623 | }
|
---|
624 |
|
---|
625 | /* -- see zlib.h -- */
|
---|
626 | int ZEXPORT gzsetparams(file, level, strategy)
|
---|
627 | gzFile file;
|
---|
628 | int level;
|
---|
629 | int strategy;
|
---|
630 | {
|
---|
631 | gz_statep state;
|
---|
632 | z_streamp strm;
|
---|
633 |
|
---|
634 | /* get internal structure */
|
---|
635 | if (file == NULL)
|
---|
636 | return Z_STREAM_ERROR;
|
---|
637 | state = (gz_statep)file;
|
---|
638 | strm = &(state->strm);
|
---|
639 |
|
---|
640 | /* check that we're writing and that there's no error */
|
---|
641 | if (state->mode != GZ_WRITE || state->err != Z_OK)
|
---|
642 | return Z_STREAM_ERROR;
|
---|
643 |
|
---|
644 | /* if no change is requested, then do nothing */
|
---|
645 | if (level == state->level && strategy == state->strategy)
|
---|
646 | return Z_OK;
|
---|
647 |
|
---|
648 | /* check for seek request */
|
---|
649 | if (state->seek) {
|
---|
650 | state->seek = 0;
|
---|
651 | if (gz_zero(state, state->skip) == -1)
|
---|
652 | return state->err;
|
---|
653 | }
|
---|
654 |
|
---|
655 | /* change compression parameters for subsequent input */
|
---|
656 | if (state->size) {
|
---|
657 | /* flush previous input with previous parameters before changing */
|
---|
658 | if (strm->avail_in && gz_comp(state, Z_BLOCK) == -1)
|
---|
659 | return state->err;
|
---|
660 | deflateParams(strm, level, strategy);
|
---|
661 | }
|
---|
662 | state->level = level;
|
---|
663 | state->strategy = strategy;
|
---|
664 | return Z_OK;
|
---|
665 | }
|
---|
666 |
|
---|
667 | /* -- see zlib.h -- */
|
---|
668 | int ZEXPORT gzclose_w(file)
|
---|
669 | gzFile file;
|
---|
670 | {
|
---|
671 | int ret = Z_OK;
|
---|
672 | gz_statep state;
|
---|
673 |
|
---|
674 | /* get internal structure */
|
---|
675 | if (file == NULL)
|
---|
676 | return Z_STREAM_ERROR;
|
---|
677 | state = (gz_statep)file;
|
---|
678 |
|
---|
679 | /* check that we're writing */
|
---|
680 | if (state->mode != GZ_WRITE)
|
---|
681 | return Z_STREAM_ERROR;
|
---|
682 |
|
---|
683 | /* check for seek request */
|
---|
684 | if (state->seek) {
|
---|
685 | state->seek = 0;
|
---|
686 | if (gz_zero(state, state->skip) == -1)
|
---|
687 | ret = state->err;
|
---|
688 | }
|
---|
689 |
|
---|
690 | /* flush, free memory, and close file */
|
---|
691 | if (gz_comp(state, Z_FINISH) == -1)
|
---|
692 | ret = state->err;
|
---|
693 | if (state->size) {
|
---|
694 | if (!state->direct) {
|
---|
695 | (void)deflateEnd(&(state->strm));
|
---|
696 | free(state->out);
|
---|
697 | }
|
---|
698 | free(state->in);
|
---|
699 | }
|
---|
700 | gz_error(state, Z_OK, NULL);
|
---|
701 | free(state->path);
|
---|
702 | #ifndef IPRT_NO_CRT /* VBox */
|
---|
703 | if (close(state->fd) == -1)
|
---|
704 | #else /* VBox */
|
---|
705 | if (RT_FAILURE(RTFileClose(state->fd))) /* VBox */
|
---|
706 | #endif /* VBox */
|
---|
707 | ret = Z_ERRNO;
|
---|
708 | free(state);
|
---|
709 | return ret;
|
---|
710 | }
|
---|