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: head/lib/libarchive/archive_write_set_compression_gzip.c 201081 2009-12-28 02:04:42Z kientzle $");
45 #include "archive_private.h"
46 #include "archive_string.h"
47 #include "archive_write_private.h"
49 #if ARCHIVE_VERSION_NUMBER < 4000000
51 archive_write_set_compression_gzip(struct archive *a)
53 __archive_write_filters_free(a);
54 return (archive_write_add_filter_gzip(a));
58 /* Don't compile this if we don't have zlib. */
61 int compression_level;
66 unsigned char *compressed;
67 size_t compressed_buffer_size;
70 struct archive_write_program_data *pdata;
75 * Yuck. zlib.h is not const-correct, so I need this one bit
76 * of ugly hackery to convert a const * pointer to a non-const pointer.
78 #define SET_NEXT_IN(st,src) \
79 (st)->stream.next_in = (Bytef *)(uintptr_t)(const void *)(src)
81 static int archive_compressor_gzip_options(struct archive_write_filter *,
82 const char *, const char *);
83 static int archive_compressor_gzip_open(struct archive_write_filter *);
84 static int archive_compressor_gzip_write(struct archive_write_filter *,
85 const void *, size_t);
86 static int archive_compressor_gzip_close(struct archive_write_filter *);
87 static int archive_compressor_gzip_free(struct archive_write_filter *);
89 static int drive_compressor(struct archive_write_filter *,
90 struct private_data *, int finishing);
95 * Add a gzip compression filter to this write handle.
98 archive_write_add_filter_gzip(struct archive *_a)
100 struct archive_write *a = (struct archive_write *)_a;
101 struct archive_write_filter *f = __archive_write_allocate_filter(_a);
102 struct private_data *data;
103 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
104 ARCHIVE_STATE_NEW, "archive_write_add_filter_gzip");
106 data = calloc(1, sizeof(*data));
108 archive_set_error(&a->archive, ENOMEM, "Out of memory");
109 return (ARCHIVE_FATAL);
112 f->open = &archive_compressor_gzip_open;
113 f->options = &archive_compressor_gzip_options;
114 f->close = &archive_compressor_gzip_close;
115 f->free = &archive_compressor_gzip_free;
116 f->code = ARCHIVE_FILTER_GZIP;
119 data->compression_level = Z_DEFAULT_COMPRESSION;
122 data->pdata = __archive_write_program_allocate("gzip");
123 if (data->pdata == NULL) {
125 archive_set_error(&a->archive, ENOMEM, "Out of memory");
126 return (ARCHIVE_FATAL);
128 data->compression_level = 0;
129 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
130 "Using external gzip program");
131 return (ARCHIVE_WARN);
136 archive_compressor_gzip_free(struct archive_write_filter *f)
138 struct private_data *data = (struct private_data *)f->data;
141 free(data->compressed);
143 __archive_write_program_free(data->pdata);
154 archive_compressor_gzip_options(struct archive_write_filter *f, const char *key,
157 struct private_data *data = (struct private_data *)f->data;
159 if (strcmp(key, "compression-level") == 0) {
160 if (value == NULL || !(value[0] >= '0' && value[0] <= '9') ||
162 return (ARCHIVE_WARN);
163 data->compression_level = value[0] - '0';
166 if (strcmp(key, "timestamp") == 0) {
167 data->timestamp = (value == NULL)?-1:1;
171 /* Note: The "warn" return is just to inform the options
172 * supervisor that we didn't handle it. It will generate
173 * a suitable error if no one used this option. */
174 return (ARCHIVE_WARN);
182 archive_compressor_gzip_open(struct archive_write_filter *f)
184 struct private_data *data = (struct private_data *)f->data;
187 if (data->compressed == NULL) {
188 size_t bs = 65536, bpb;
189 if (f->archive->magic == ARCHIVE_WRITE_MAGIC) {
190 /* Buffer size should be a multiple number of
191 * the of bytes per block for performance. */
192 bpb = archive_write_get_bytes_per_block(f->archive);
198 data->compressed_buffer_size = bs;
200 = (unsigned char *)malloc(data->compressed_buffer_size);
201 if (data->compressed == NULL) {
202 archive_set_error(f->archive, ENOMEM,
203 "Can't allocate data for compression buffer");
204 return (ARCHIVE_FATAL);
208 data->crc = crc32(0L, NULL, 0);
209 data->stream.next_out = data->compressed;
210 data->stream.avail_out = (uInt)data->compressed_buffer_size;
212 /* Prime output buffer with a gzip header. */
213 data->compressed[0] = 0x1f; /* GZip signature bytes */
214 data->compressed[1] = 0x8b;
215 data->compressed[2] = 0x08; /* "Deflate" compression */
216 data->compressed[3] = 0; /* No options */
217 if (data->timestamp >= 0) {
218 time_t t = time(NULL);
219 data->compressed[4] = (uint8_t)(t)&0xff; /* Timestamp */
220 data->compressed[5] = (uint8_t)(t>>8)&0xff;
221 data->compressed[6] = (uint8_t)(t>>16)&0xff;
222 data->compressed[7] = (uint8_t)(t>>24)&0xff;
224 memset(&data->compressed[4], 0, 4);
225 if (data->compression_level == 9)
226 data->compressed[8] = 2;
227 else if(data->compression_level == 1)
228 data->compressed[8] = 4;
230 data->compressed[8] = 0;
231 data->compressed[9] = 3; /* OS=Unix */
232 data->stream.next_out += 10;
233 data->stream.avail_out -= 10;
235 f->write = archive_compressor_gzip_write;
237 /* Initialize compression library. */
238 ret = deflateInit2(&(data->stream),
239 data->compression_level,
241 -15 /* < 0 to suppress zlib header */,
250 /* Library setup failed: clean up. */
251 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC, "Internal error "
252 "initializing compression library");
254 /* Override the error message if we know what really went wrong. */
257 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
258 "Internal error initializing "
259 "compression library: invalid setup parameter");
262 archive_set_error(f->archive, ENOMEM,
263 "Internal error initializing compression library");
265 case Z_VERSION_ERROR:
266 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
267 "Internal error initializing "
268 "compression library: invalid library version");
272 return (ARCHIVE_FATAL);
276 * Write data to the compressed stream.
279 archive_compressor_gzip_write(struct archive_write_filter *f, const void *buff,
282 struct private_data *data = (struct private_data *)f->data;
285 /* Update statistics */
286 data->crc = crc32(data->crc, (const Bytef *)buff, (uInt)length);
287 data->total_in += length;
289 /* Compress input data to output buffer */
290 SET_NEXT_IN(data, buff);
291 data->stream.avail_in = (uInt)length;
292 if ((ret = drive_compressor(f, data, 0)) != ARCHIVE_OK)
299 * Finish the compression...
302 archive_compressor_gzip_close(struct archive_write_filter *f)
304 unsigned char trailer[8];
305 struct private_data *data = (struct private_data *)f->data;
308 /* Finish compression cycle */
309 ret = drive_compressor(f, data, 1);
310 if (ret == ARCHIVE_OK) {
311 /* Write the last compressed data. */
312 ret = __archive_write_filter(f->next_filter,
314 data->compressed_buffer_size - data->stream.avail_out);
316 if (ret == ARCHIVE_OK) {
317 /* Build and write out 8-byte trailer. */
318 trailer[0] = (uint8_t)(data->crc)&0xff;
319 trailer[1] = (uint8_t)(data->crc >> 8)&0xff;
320 trailer[2] = (uint8_t)(data->crc >> 16)&0xff;
321 trailer[3] = (uint8_t)(data->crc >> 24)&0xff;
322 trailer[4] = (uint8_t)(data->total_in)&0xff;
323 trailer[5] = (uint8_t)(data->total_in >> 8)&0xff;
324 trailer[6] = (uint8_t)(data->total_in >> 16)&0xff;
325 trailer[7] = (uint8_t)(data->total_in >> 24)&0xff;
326 ret = __archive_write_filter(f->next_filter, trailer, 8);
329 switch (deflateEnd(&(data->stream))) {
333 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
334 "Failed to clean up compressor");
341 * Utility function to push input data through compressor,
342 * writing full output blocks as necessary.
344 * Note that this handles both the regular write case (finishing ==
345 * false) and the end-of-archive case (finishing == true).
348 drive_compressor(struct archive_write_filter *f,
349 struct private_data *data, int finishing)
354 if (data->stream.avail_out == 0) {
355 ret = __archive_write_filter(f->next_filter,
357 data->compressed_buffer_size);
358 if (ret != ARCHIVE_OK)
359 return (ARCHIVE_FATAL);
360 data->stream.next_out = data->compressed;
361 data->stream.avail_out =
362 (uInt)data->compressed_buffer_size;
365 /* If there's nothing to do, we're done. */
366 if (!finishing && data->stream.avail_in == 0)
369 ret = deflate(&(data->stream),
370 finishing ? Z_FINISH : Z_NO_FLUSH );
374 /* In non-finishing case, check if compressor
375 * consumed everything */
376 if (!finishing && data->stream.avail_in == 0)
378 /* In finishing case, this return always means
379 * there's more work */
382 /* This return can only occur in finishing case. */
385 /* Any other return value indicates an error. */
386 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
387 "GZip compression failed:"
388 " deflate() call returned status %d",
390 return (ARCHIVE_FATAL);
395 #else /* HAVE_ZLIB_H */
398 archive_compressor_gzip_open(struct archive_write_filter *f)
400 struct private_data *data = (struct private_data *)f->data;
401 struct archive_string as;
404 archive_string_init(&as);
405 archive_strcpy(&as, "gzip");
407 /* Specify compression level. */
408 if (data->compression_level > 0) {
409 archive_strcat(&as, " -");
410 archive_strappend_char(&as, '0' + data->compression_level);
412 if (data->timestamp < 0)
413 /* Do not save timestamp. */
414 archive_strcat(&as, " -n");
415 else if (data->timestamp > 0)
416 /* Save timestamp. */
417 archive_strcat(&as, " -N");
419 f->write = archive_compressor_gzip_write;
420 r = __archive_write_program_open(f, data->pdata, as.s);
421 archive_string_free(&as);
426 archive_compressor_gzip_write(struct archive_write_filter *f, const void *buff,
429 struct private_data *data = (struct private_data *)f->data;
431 return __archive_write_program_write(f, data->pdata, buff, length);
435 archive_compressor_gzip_close(struct archive_write_filter *f)
437 struct private_data *data = (struct private_data *)f->data;
439 return __archive_write_program_close(f, data->pdata);
442 #endif /* HAVE_ZLIB_H */