2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "archive_platform.h"
28 __FBSDID("$FreeBSD$");
45 #include "archive_private.h"
46 #include "archive_write_private.h"
50 archive_write_set_compression_gzip(struct archive *_a)
52 /* Unsupported gzip compression, we don't have zlib */
53 return (ARCHIVE_FATAL);
56 /* Don't compile this if we don't have zlib. */
61 unsigned char *compressed;
62 size_t compressed_buffer_size;
65 int compression_level;
70 * Yuck. zlib.h is not const-correct, so I need this one bit
71 * of ugly hackery to convert a const * pointer to a non-const pointer.
73 #define SET_NEXT_IN(st,src) \
74 (st)->stream.next_in = (Bytef *)(uintptr_t)(const void *)(src)
76 static int archive_compressor_gzip_finish(struct archive_write *);
77 static int archive_compressor_gzip_init(struct archive_write *);
78 static int archive_compressor_gzip_options(struct archive_write *,
79 const char *, const char *);
80 static int archive_compressor_gzip_write(struct archive_write *,
81 const void *, size_t);
82 static int drive_compressor(struct archive_write *, struct private_data *,
87 * Allocate, initialize and return a archive object.
90 archive_write_set_compression_gzip(struct archive *_a)
92 struct archive_write *a = (struct archive_write *)_a;
93 __archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
94 ARCHIVE_STATE_NEW, "archive_write_set_compression_gzip");
95 a->compressor.init = &archive_compressor_gzip_init;
96 a->archive.compression_code = ARCHIVE_COMPRESSION_GZIP;
97 a->archive.compression_name = "gzip";
105 archive_compressor_gzip_init(struct archive_write *a)
108 struct private_data *state;
111 a->archive.compression_code = ARCHIVE_COMPRESSION_GZIP;
112 a->archive.compression_name = "gzip";
114 if (a->client_opener != NULL) {
115 ret = (a->client_opener)(&a->archive, a->client_data);
116 if (ret != ARCHIVE_OK)
121 * The next check is a temporary workaround until the gzip
122 * code can be overhauled some. The code should not require
123 * that compressed_buffer_size == bytes_per_block. Removing
124 * this assumption will allow us to compress larger chunks at
125 * a time, which should improve overall performance
126 * marginally. As a minor side-effect, such a cleanup would
127 * allow us to support truly arbitrary block sizes.
129 if (a->bytes_per_block < 10) {
130 archive_set_error(&a->archive, EINVAL,
131 "GZip compressor requires a minimum 10 byte block size");
132 return (ARCHIVE_FATAL);
135 state = (struct private_data *)malloc(sizeof(*state));
137 archive_set_error(&a->archive, ENOMEM,
138 "Can't allocate data for compression");
139 return (ARCHIVE_FATAL);
141 memset(state, 0, sizeof(*state));
144 * See comment above. We should set compressed_buffer_size to
145 * max(bytes_per_block, 65536), but the code can't handle that yet.
147 state->compressed_buffer_size = a->bytes_per_block;
148 state->compressed = (unsigned char *)malloc(state->compressed_buffer_size);
149 state->crc = crc32(0L, NULL, 0);
150 state->compression_level = Z_DEFAULT_COMPRESSION;
152 if (state->compressed == NULL) {
153 archive_set_error(&a->archive, ENOMEM,
154 "Can't allocate data for compression buffer");
156 return (ARCHIVE_FATAL);
159 state->stream.next_out = state->compressed;
160 state->stream.avail_out = state->compressed_buffer_size;
162 /* Prime output buffer with a gzip header. */
164 state->compressed[0] = 0x1f; /* GZip signature bytes */
165 state->compressed[1] = 0x8b;
166 state->compressed[2] = 0x08; /* "Deflate" compression */
167 state->compressed[3] = 0; /* No options */
168 state->compressed[4] = (t)&0xff; /* Timestamp */
169 state->compressed[5] = (t>>8)&0xff;
170 state->compressed[6] = (t>>16)&0xff;
171 state->compressed[7] = (t>>24)&0xff;
172 state->compressed[8] = 0; /* No deflate options */
173 state->compressed[9] = 3; /* OS=Unix */
174 state->stream.next_out += 10;
175 state->stream.avail_out -= 10;
177 a->compressor.options = archive_compressor_gzip_options;
178 a->compressor.write = archive_compressor_gzip_write;
179 a->compressor.finish = archive_compressor_gzip_finish;
181 /* Initialize compression library. */
182 ret = deflateInit2(&(state->stream),
183 state->compression_level,
185 -15 /* < 0 to suppress zlib header */,
190 a->compressor.data = state;
194 /* Library setup failed: clean up. */
195 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Internal error "
196 "initializing compression library");
197 free(state->compressed);
200 /* Override the error message if we know what really went wrong. */
203 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
204 "Internal error initializing "
205 "compression library: invalid setup parameter");
208 archive_set_error(&a->archive, ENOMEM, "Internal error initializing "
209 "compression library");
211 case Z_VERSION_ERROR:
212 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
213 "Internal error initializing "
214 "compression library: invalid library version");
218 return (ARCHIVE_FATAL);
225 archive_compressor_gzip_options(struct archive_write *a, const char *key,
228 struct private_data *state;
231 state = (struct private_data *)a->compressor.data;
232 if (strcmp(key, "compression-level") == 0) {
235 if (value == NULL || !(value[0] >= '0' && value[0] <= '9') ||
237 return (ARCHIVE_WARN);
238 level = value[0] - '0';
239 if (level == state->compression_level)
242 ret = deflateParams(&(state->stream), level,
245 state->compression_level = level;
250 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
251 "Internal error updating params "
252 "compression library: state was inconsistent "
253 "or parameter was invalid");
256 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
257 "Internal error updating params "
258 "compression library: out buffer was zero");
261 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
262 "Internal error updatng params "
263 "compression library");
266 return (ARCHIVE_FATAL);
269 return (ARCHIVE_WARN);
273 * Write data to the compressed stream.
276 archive_compressor_gzip_write(struct archive_write *a, const void *buff,
279 struct private_data *state;
282 state = (struct private_data *)a->compressor.data;
283 if (a->client_writer == NULL) {
284 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
285 "No write callback is registered? "
286 "This is probably an internal programming error.");
287 return (ARCHIVE_FATAL);
290 /* Update statistics */
291 state->crc = crc32(state->crc, (const Bytef *)buff, length);
292 state->total_in += length;
294 /* Compress input data to output buffer */
295 SET_NEXT_IN(state, buff);
296 state->stream.avail_in = length;
297 if ((ret = drive_compressor(a, state, 0)) != ARCHIVE_OK)
300 a->archive.file_position += length;
306 * Finish the compression...
309 archive_compressor_gzip_finish(struct archive_write *a)
311 ssize_t block_length, target_block_length, bytes_written;
313 struct private_data *state;
315 unsigned char trailer[8];
317 state = (struct private_data *)a->compressor.data;
319 if (a->client_writer == NULL) {
320 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
321 "No write callback is registered? "
322 "This is probably an internal programming error.");
327 /* By default, always pad the uncompressed data. */
328 if (a->pad_uncompressed) {
329 tocopy = a->bytes_per_block -
330 (state->total_in % a->bytes_per_block);
331 while (tocopy > 0 && tocopy < (unsigned)a->bytes_per_block) {
332 SET_NEXT_IN(state, a->nulls);
333 state->stream.avail_in = tocopy < a->null_length ?
334 tocopy : a->null_length;
335 state->crc = crc32(state->crc, a->nulls,
336 state->stream.avail_in);
337 state->total_in += state->stream.avail_in;
338 tocopy -= state->stream.avail_in;
339 ret = drive_compressor(a, state, 0);
340 if (ret != ARCHIVE_OK)
345 /* Finish compression cycle */
346 if (((ret = drive_compressor(a, state, 1))) != ARCHIVE_OK)
349 /* Build trailer: 4-byte CRC and 4-byte length. */
350 trailer[0] = (state->crc)&0xff;
351 trailer[1] = (state->crc >> 8)&0xff;
352 trailer[2] = (state->crc >> 16)&0xff;
353 trailer[3] = (state->crc >> 24)&0xff;
354 trailer[4] = (state->total_in)&0xff;
355 trailer[5] = (state->total_in >> 8)&0xff;
356 trailer[6] = (state->total_in >> 16)&0xff;
357 trailer[7] = (state->total_in >> 24)&0xff;
359 /* Add trailer to current block. */
361 if (tocopy > state->stream.avail_out)
362 tocopy = state->stream.avail_out;
363 memcpy(state->stream.next_out, trailer, tocopy);
364 state->stream.next_out += tocopy;
365 state->stream.avail_out -= tocopy;
367 /* If it overflowed, flush and start a new block. */
369 bytes_written = (a->client_writer)(&a->archive, a->client_data,
370 state->compressed, state->compressed_buffer_size);
371 if (bytes_written <= 0) {
375 a->archive.raw_position += bytes_written;
376 state->stream.next_out = state->compressed;
377 state->stream.avail_out = state->compressed_buffer_size;
378 memcpy(state->stream.next_out, trailer + tocopy, 8-tocopy);
379 state->stream.next_out += 8-tocopy;
380 state->stream.avail_out -= 8-tocopy;
383 /* Optionally, pad the final compressed block. */
384 block_length = state->stream.next_out - state->compressed;
387 /* Tricky calculation to determine size of last block. */
388 target_block_length = block_length;
389 if (a->bytes_in_last_block <= 0)
390 /* Default or Zero: pad to full block */
391 target_block_length = a->bytes_per_block;
393 /* Round length to next multiple of bytes_in_last_block. */
394 target_block_length = a->bytes_in_last_block *
395 ( (block_length + a->bytes_in_last_block - 1) /
396 a->bytes_in_last_block);
397 if (target_block_length > a->bytes_per_block)
398 target_block_length = a->bytes_per_block;
399 if (block_length < target_block_length) {
400 memset(state->stream.next_out, 0,
401 target_block_length - block_length);
402 block_length = target_block_length;
405 /* Write the last block */
406 bytes_written = (a->client_writer)(&a->archive, a->client_data,
407 state->compressed, block_length);
408 if (bytes_written <= 0) {
412 a->archive.raw_position += bytes_written;
414 /* Cleanup: shut down compressor, release memory, etc. */
416 switch (deflateEnd(&(state->stream))) {
420 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
421 "Failed to clean up compressor");
424 free(state->compressed);
430 * Utility function to push input data through compressor,
431 * writing full output blocks as necessary.
433 * Note that this handles both the regular write case (finishing ==
434 * false) and the end-of-archive case (finishing == true).
437 drive_compressor(struct archive_write *a, struct private_data *state, int finishing)
439 ssize_t bytes_written;
443 if (state->stream.avail_out == 0) {
444 bytes_written = (a->client_writer)(&a->archive,
445 a->client_data, state->compressed,
446 state->compressed_buffer_size);
447 if (bytes_written <= 0) {
448 /* TODO: Handle this write failure */
449 return (ARCHIVE_FATAL);
450 } else if ((size_t)bytes_written < state->compressed_buffer_size) {
451 /* Short write: Move remaining to
452 * front of block and keep filling */
453 memmove(state->compressed,
454 state->compressed + bytes_written,
455 state->compressed_buffer_size - bytes_written);
457 a->archive.raw_position += bytes_written;
458 state->stream.next_out
459 = state->compressed +
460 state->compressed_buffer_size - bytes_written;
461 state->stream.avail_out = bytes_written;
464 /* If there's nothing to do, we're done. */
465 if (!finishing && state->stream.avail_in == 0)
468 ret = deflate(&(state->stream),
469 finishing ? Z_FINISH : Z_NO_FLUSH );
473 /* In non-finishing case, check if compressor
474 * consumed everything */
475 if (!finishing && state->stream.avail_in == 0)
477 /* In finishing case, this return always means
478 * there's more work */
481 /* This return can only occur in finishing case. */
484 /* Any other return value indicates an error. */
485 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
486 "GZip compression failed:"
487 " deflate() call returned status %d",
489 return (ARCHIVE_FATAL);
494 #endif /* HAVE_ZLIB_H */