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