archive_write_add_filter_gzip.c revision 337351
1/*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
24 */
25
26#include "archive_platform.h"
27
28__FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_compression_gzip.c 201081 2009-12-28 02:04:42Z kientzle $");
29
30#ifdef HAVE_ERRNO_H
31#include <errno.h>
32#endif
33#ifdef HAVE_STDLIB_H
34#include <stdlib.h>
35#endif
36#ifdef HAVE_STRING_H
37#include <string.h>
38#endif
39#include <time.h>
40#ifdef HAVE_ZLIB_H
41#include <zlib.h>
42#endif
43
44#include "archive.h"
45#include "archive_private.h"
46#include "archive_string.h"
47#include "archive_write_private.h"
48
49#if ARCHIVE_VERSION_NUMBER < 4000000
50int
51archive_write_set_compression_gzip(struct archive *a)
52{
53	__archive_write_filters_free(a);
54	return (archive_write_add_filter_gzip(a));
55}
56#endif
57
58/* Don't compile this if we don't have zlib. */
59
60struct private_data {
61	int		 compression_level;
62	int		 timestamp;
63#ifdef HAVE_ZLIB_H
64	z_stream	 stream;
65	int64_t		 total_in;
66	unsigned char	*compressed;
67	size_t		 compressed_buffer_size;
68	unsigned long	 crc;
69#else
70	struct archive_write_program_data *pdata;
71#endif
72};
73
74/*
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.
77 */
78#define	SET_NEXT_IN(st,src)					\
79	(st)->stream.next_in = (Bytef *)(uintptr_t)(const void *)(src)
80
81static int archive_compressor_gzip_options(struct archive_write_filter *,
82		    const char *, const char *);
83static int archive_compressor_gzip_open(struct archive_write_filter *);
84static int archive_compressor_gzip_write(struct archive_write_filter *,
85		    const void *, size_t);
86static int archive_compressor_gzip_close(struct archive_write_filter *);
87static int archive_compressor_gzip_free(struct archive_write_filter *);
88#ifdef HAVE_ZLIB_H
89static int drive_compressor(struct archive_write_filter *,
90		    struct private_data *, int finishing);
91#endif
92
93
94/*
95 * Add a gzip compression filter to this write handle.
96 */
97int
98archive_write_add_filter_gzip(struct archive *_a)
99{
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");
105
106	data = calloc(1, sizeof(*data));
107	if (data == NULL) {
108		archive_set_error(&a->archive, ENOMEM, "Out of memory");
109		return (ARCHIVE_FATAL);
110	}
111	f->data = data;
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;
117	f->name = "gzip";
118#ifdef HAVE_ZLIB_H
119	data->compression_level = Z_DEFAULT_COMPRESSION;
120	return (ARCHIVE_OK);
121#else
122	data->pdata = __archive_write_program_allocate("gzip");
123	if (data->pdata == NULL) {
124		free(data);
125		archive_set_error(&a->archive, ENOMEM, "Out of memory");
126		return (ARCHIVE_FATAL);
127	}
128	data->compression_level = 0;
129	archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
130	    "Using external gzip program");
131	return (ARCHIVE_WARN);
132#endif
133}
134
135static int
136archive_compressor_gzip_free(struct archive_write_filter *f)
137{
138	struct private_data *data = (struct private_data *)f->data;
139
140#ifdef HAVE_ZLIB_H
141	free(data->compressed);
142#else
143	__archive_write_program_free(data->pdata);
144#endif
145	free(data);
146	f->data = NULL;
147	return (ARCHIVE_OK);
148}
149
150/*
151 * Set write options.
152 */
153static int
154archive_compressor_gzip_options(struct archive_write_filter *f, const char *key,
155    const char *value)
156{
157	struct private_data *data = (struct private_data *)f->data;
158
159	if (strcmp(key, "compression-level") == 0) {
160		if (value == NULL || !(value[0] >= '0' && value[0] <= '9') ||
161		    value[1] != '\0')
162			return (ARCHIVE_WARN);
163		data->compression_level = value[0] - '0';
164		return (ARCHIVE_OK);
165	}
166	if (strcmp(key, "timestamp") == 0) {
167		data->timestamp = (value == NULL)?-1:1;
168		return (ARCHIVE_OK);
169	}
170
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);
175}
176
177#ifdef HAVE_ZLIB_H
178/*
179 * Setup callback.
180 */
181static int
182archive_compressor_gzip_open(struct archive_write_filter *f)
183{
184	struct private_data *data = (struct private_data *)f->data;
185	int ret;
186
187	ret = __archive_write_open_filter(f->next_filter);
188	if (ret != ARCHIVE_OK)
189		return (ret);
190
191	if (data->compressed == NULL) {
192		size_t bs = 65536, bpb;
193		if (f->archive->magic == ARCHIVE_WRITE_MAGIC) {
194			/* Buffer size should be a multiple number of
195			 * the of bytes per block for performance. */
196			bpb = archive_write_get_bytes_per_block(f->archive);
197			if (bpb > bs)
198				bs = bpb;
199			else if (bpb != 0)
200				bs -= bs % bpb;
201		}
202		data->compressed_buffer_size = bs;
203		data->compressed
204		    = (unsigned char *)malloc(data->compressed_buffer_size);
205		if (data->compressed == NULL) {
206			archive_set_error(f->archive, ENOMEM,
207			    "Can't allocate data for compression buffer");
208			return (ARCHIVE_FATAL);
209		}
210	}
211
212	data->crc = crc32(0L, NULL, 0);
213	data->stream.next_out = data->compressed;
214	data->stream.avail_out = (uInt)data->compressed_buffer_size;
215
216	/* Prime output buffer with a gzip header. */
217	data->compressed[0] = 0x1f; /* GZip signature bytes */
218	data->compressed[1] = 0x8b;
219	data->compressed[2] = 0x08; /* "Deflate" compression */
220	data->compressed[3] = 0; /* No options */
221	if (data->timestamp >= 0) {
222		time_t t = time(NULL);
223		data->compressed[4] = (uint8_t)(t)&0xff;  /* Timestamp */
224		data->compressed[5] = (uint8_t)(t>>8)&0xff;
225		data->compressed[6] = (uint8_t)(t>>16)&0xff;
226		data->compressed[7] = (uint8_t)(t>>24)&0xff;
227	} else
228		memset(&data->compressed[4], 0, 4);
229    if (data->compression_level == 9)
230	    data->compressed[8] = 2;
231    else if(data->compression_level == 1)
232	    data->compressed[8] = 4;
233    else
234	    data->compressed[8] = 0;
235	data->compressed[9] = 3; /* OS=Unix */
236	data->stream.next_out += 10;
237	data->stream.avail_out -= 10;
238
239	f->write = archive_compressor_gzip_write;
240
241	/* Initialize compression library. */
242	ret = deflateInit2(&(data->stream),
243	    data->compression_level,
244	    Z_DEFLATED,
245	    -15 /* < 0 to suppress zlib header */,
246	    8,
247	    Z_DEFAULT_STRATEGY);
248
249	if (ret == Z_OK) {
250		f->data = data;
251		return (ARCHIVE_OK);
252	}
253
254	/* Library setup failed: clean up. */
255	archive_set_error(f->archive, ARCHIVE_ERRNO_MISC, "Internal error "
256	    "initializing compression library");
257
258	/* Override the error message if we know what really went wrong. */
259	switch (ret) {
260	case Z_STREAM_ERROR:
261		archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
262		    "Internal error initializing "
263		    "compression library: invalid setup parameter");
264		break;
265	case Z_MEM_ERROR:
266		archive_set_error(f->archive, ENOMEM,
267		    "Internal error initializing compression library");
268		break;
269	case Z_VERSION_ERROR:
270		archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
271		    "Internal error initializing "
272		    "compression library: invalid library version");
273		break;
274	}
275
276	return (ARCHIVE_FATAL);
277}
278
279/*
280 * Write data to the compressed stream.
281 */
282static int
283archive_compressor_gzip_write(struct archive_write_filter *f, const void *buff,
284    size_t length)
285{
286	struct private_data *data = (struct private_data *)f->data;
287	int ret;
288
289	/* Update statistics */
290	data->crc = crc32(data->crc, (const Bytef *)buff, (uInt)length);
291	data->total_in += length;
292
293	/* Compress input data to output buffer */
294	SET_NEXT_IN(data, buff);
295	data->stream.avail_in = (uInt)length;
296	if ((ret = drive_compressor(f, data, 0)) != ARCHIVE_OK)
297		return (ret);
298
299	return (ARCHIVE_OK);
300}
301
302/*
303 * Finish the compression...
304 */
305static int
306archive_compressor_gzip_close(struct archive_write_filter *f)
307{
308	unsigned char trailer[8];
309	struct private_data *data = (struct private_data *)f->data;
310	int ret, r1;
311
312	/* Finish compression cycle */
313	ret = drive_compressor(f, data, 1);
314	if (ret == ARCHIVE_OK) {
315		/* Write the last compressed data. */
316		ret = __archive_write_filter(f->next_filter,
317		    data->compressed,
318		    data->compressed_buffer_size - data->stream.avail_out);
319	}
320	if (ret == ARCHIVE_OK) {
321		/* Build and write out 8-byte trailer. */
322		trailer[0] = (uint8_t)(data->crc)&0xff;
323		trailer[1] = (uint8_t)(data->crc >> 8)&0xff;
324		trailer[2] = (uint8_t)(data->crc >> 16)&0xff;
325		trailer[3] = (uint8_t)(data->crc >> 24)&0xff;
326		trailer[4] = (uint8_t)(data->total_in)&0xff;
327		trailer[5] = (uint8_t)(data->total_in >> 8)&0xff;
328		trailer[6] = (uint8_t)(data->total_in >> 16)&0xff;
329		trailer[7] = (uint8_t)(data->total_in >> 24)&0xff;
330		ret = __archive_write_filter(f->next_filter, trailer, 8);
331	}
332
333	switch (deflateEnd(&(data->stream))) {
334	case Z_OK:
335		break;
336	default:
337		archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
338		    "Failed to clean up compressor");
339		ret = ARCHIVE_FATAL;
340	}
341	r1 = __archive_write_close_filter(f->next_filter);
342	return (r1 < ret ? r1 : ret);
343}
344
345/*
346 * Utility function to push input data through compressor,
347 * writing full output blocks as necessary.
348 *
349 * Note that this handles both the regular write case (finishing ==
350 * false) and the end-of-archive case (finishing == true).
351 */
352static int
353drive_compressor(struct archive_write_filter *f,
354    struct private_data *data, int finishing)
355{
356	int ret;
357
358	for (;;) {
359		if (data->stream.avail_out == 0) {
360			ret = __archive_write_filter(f->next_filter,
361			    data->compressed,
362			    data->compressed_buffer_size);
363			if (ret != ARCHIVE_OK)
364				return (ARCHIVE_FATAL);
365			data->stream.next_out = data->compressed;
366			data->stream.avail_out =
367			    (uInt)data->compressed_buffer_size;
368		}
369
370		/* If there's nothing to do, we're done. */
371		if (!finishing && data->stream.avail_in == 0)
372			return (ARCHIVE_OK);
373
374		ret = deflate(&(data->stream),
375		    finishing ? Z_FINISH : Z_NO_FLUSH );
376
377		switch (ret) {
378		case Z_OK:
379			/* In non-finishing case, check if compressor
380			 * consumed everything */
381			if (!finishing && data->stream.avail_in == 0)
382				return (ARCHIVE_OK);
383			/* In finishing case, this return always means
384			 * there's more work */
385			break;
386		case Z_STREAM_END:
387			/* This return can only occur in finishing case. */
388			return (ARCHIVE_OK);
389		default:
390			/* Any other return value indicates an error. */
391			archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
392			    "GZip compression failed:"
393			    " deflate() call returned status %d",
394			    ret);
395			return (ARCHIVE_FATAL);
396		}
397	}
398}
399
400#else /* HAVE_ZLIB_H */
401
402static int
403archive_compressor_gzip_open(struct archive_write_filter *f)
404{
405	struct private_data *data = (struct private_data *)f->data;
406	struct archive_string as;
407	int r;
408
409	archive_string_init(&as);
410	archive_strcpy(&as, "gzip");
411
412	/* Specify compression level. */
413	if (data->compression_level > 0) {
414		archive_strcat(&as, " -");
415		archive_strappend_char(&as, '0' + data->compression_level);
416	}
417	if (data->timestamp < 0)
418		/* Do not save timestamp. */
419		archive_strcat(&as, " -n");
420	else if (data->timestamp > 0)
421		/* Save timestamp. */
422		archive_strcat(&as, " -N");
423
424	f->write = archive_compressor_gzip_write;
425	r = __archive_write_program_open(f, data->pdata, as.s);
426	archive_string_free(&as);
427	return (r);
428}
429
430static int
431archive_compressor_gzip_write(struct archive_write_filter *f, const void *buff,
432    size_t length)
433{
434	struct private_data *data = (struct private_data *)f->data;
435
436	return __archive_write_program_write(f, data->pdata, buff, length);
437}
438
439static int
440archive_compressor_gzip_close(struct archive_write_filter *f)
441{
442	struct private_data *data = (struct private_data *)f->data;
443
444	return __archive_write_program_close(f, data->pdata);
445}
446
447#endif /* HAVE_ZLIB_H */
448