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_bzip2.c 201091 2009-12-28 02:22:41Z kientzle $");
29 
30 #ifdef HAVE_ERRNO_H
31 #include <errno.h>
32 #endif
33 #include <stdio.h>
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #ifdef HAVE_STRING_H
38 #include <string.h>
39 #endif
40 #ifdef HAVE_BZLIB_H
41 #include <bzlib.h>
42 #endif
43 
44 #include "archive.h"
45 #include "archive_private.h"
46 #include "archive_write_private.h"
47 
48 #if ARCHIVE_VERSION_NUMBER < 4000000
49 int
50 archive_write_set_compression_bzip2(struct archive *a)
51 {
52 	__archive_write_filters_free(a);
53 	return (archive_write_add_filter_bzip2(a));
54 }
55 #endif
56 
57 #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR)
58 int
59 archive_write_add_filter_bzip2(struct archive *a)
60 {
61 	archive_set_error(a, ARCHIVE_ERRNO_MISC,
62 	    "bzip2 compression not supported on this platform");
63 	return (ARCHIVE_FATAL);
64 }
65 #else
66 /* Don't compile this if we don't have bzlib. */
67 
68 struct private_data {
69 	int		 compression_level;
70 	bz_stream	 stream;
71 	int64_t		 total_in;
72 	char		*compressed;
73 	size_t		 compressed_buffer_size;
74 };
75 
76 /*
77  * Yuck.  bzlib.h is not const-correct, so I need this one bit
78  * of ugly hackery to convert a const * pointer to a non-const pointer.
79  */
80 #define	SET_NEXT_IN(st,src)					\
81 	(st)->stream.next_in = (char *)(uintptr_t)(const void *)(src)
82 
83 static int archive_compressor_bzip2_close(struct archive_write_filter *);
84 static int archive_compressor_bzip2_free(struct archive_write_filter *);
85 static int archive_compressor_bzip2_open(struct archive_write_filter *);
86 static int archive_compressor_bzip2_options(struct archive_write_filter *,
87 		    const char *, const char *);
88 static int archive_compressor_bzip2_write(struct archive_write_filter *,
89 		    const void *, size_t);
90 static int drive_compressor(struct archive_write_filter *,
91 		    struct private_data *, int finishing);
92 
93 /*
94  * Add a bzip2 compression filter to this write handle.
95  */
96 int
97 archive_write_add_filter_bzip2(struct archive *_a)
98 {
99 	struct archive_write *a = (struct archive_write *)_a;
100 	struct archive_write_filter *f = __archive_write_allocate_filter(_a);
101 	struct private_data *data;
102 
103 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
104 	    ARCHIVE_STATE_NEW, "archive_write_add_filter_bzip2");
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 	data->compression_level = 9; /* default */
112 
113 	f->data = data;
114 	f->options = &archive_compressor_bzip2_options;
115 	f->close = &archive_compressor_bzip2_close;
116 	f->free = &archive_compressor_bzip2_free;
117 	f->open = &archive_compressor_bzip2_open;
118 	f->code = ARCHIVE_COMPRESSION_BZIP2;
119 	f->name = "bzip2";
120 	return (ARCHIVE_OK);
121 }
122 
123 /*
124  * Setup callback.
125  */
126 static int
127 archive_compressor_bzip2_open(struct archive_write_filter *f)
128 {
129 	struct private_data *data = (struct private_data *)f->data;
130 	int ret;
131 
132 	ret = __archive_write_open_filter(f->next_filter);
133 	if (ret != 0)
134 		return (ret);
135 
136 	if (data->compressed == NULL) {
137 		size_t bs = 65536, bpb;
138 		if (f->archive->magic == ARCHIVE_WRITE_MAGIC) {
139 			/* Buffer size should be a multiple number of the of bytes
140 			 * per block for performance. */
141 			bpb = archive_write_get_bytes_per_block(f->archive);
142 			if (bpb > bs)
143 				bs = bpb;
144 			else if (bpb != 0)
145 				bs -= bs % bpb;
146 		}
147 		data->compressed_buffer_size = bs;
148 		data->compressed
149 		    = (char *)malloc(data->compressed_buffer_size);
150 		if (data->compressed == NULL) {
151 			archive_set_error(f->archive, ENOMEM,
152 			    "Can't allocate data for compression buffer");
153 			return (ARCHIVE_FATAL);
154 		}
155 	}
156 
157 	memset(&data->stream, 0, sizeof(data->stream));
158 	data->stream.next_out = data->compressed;
159 	data->stream.avail_out = data->compressed_buffer_size;
160 	f->write = archive_compressor_bzip2_write;
161 
162 	/* Initialize compression library */
163 	ret = BZ2_bzCompressInit(&(data->stream),
164 	    data->compression_level, 0, 30);
165 	if (ret == BZ_OK) {
166 		f->data = data;
167 		return (ARCHIVE_OK);
168 	}
169 
170 	/* Library setup failed: clean up. */
171 	archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
172 	    "Internal error initializing compression library");
173 
174 	/* Override the error message if we know what really went wrong. */
175 	switch (ret) {
176 	case BZ_PARAM_ERROR:
177 		archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
178 		    "Internal error initializing compression library: "
179 		    "invalid setup parameter");
180 		break;
181 	case BZ_MEM_ERROR:
182 		archive_set_error(f->archive, ENOMEM,
183 		    "Internal error initializing compression library: "
184 		    "out of memory");
185 		break;
186 	case BZ_CONFIG_ERROR:
187 		archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
188 		    "Internal error initializing compression library: "
189 		    "mis-compiled library");
190 		break;
191 	}
192 
193 	return (ARCHIVE_FATAL);
194 
195 }
196 
197 /*
198  * Set write options.
199  */
200 static int
201 archive_compressor_bzip2_options(struct archive_write_filter *f,
202     const char *key, const char *value)
203 {
204 	struct private_data *data = (struct private_data *)f->data;
205 
206 	if (strcmp(key, "compression-level") == 0) {
207 		if (value == NULL || !(value[0] >= '0' && value[0] <= '9') ||
208 		    value[1] != '\0')
209 			return (ARCHIVE_WARN);
210 		data->compression_level = value[0] - '0';
211 		/* Make '0' be a synonym for '1'. */
212 		/* This way, bzip2 compressor supports the same 0..9
213 		 * range of levels as gzip. */
214 		if (data->compression_level < 1)
215 			data->compression_level = 1;
216 		return (ARCHIVE_OK);
217 	}
218 
219 	/* Note: The "warn" return is just to inform the options
220 	 * supervisor that we didn't handle it.  It will generate
221 	 * a suitable error if no one used this option. */
222 	return (ARCHIVE_WARN);
223 }
224 
225 /*
226  * Write data to the compressed stream.
227  *
228  * Returns ARCHIVE_OK if all data written, error otherwise.
229  */
230 static int
231 archive_compressor_bzip2_write(struct archive_write_filter *f,
232     const void *buff, size_t length)
233 {
234 	struct private_data *data = (struct private_data *)f->data;
235 
236 	/* Update statistics */
237 	data->total_in += length;
238 
239 	/* Compress input data to output buffer */
240 	SET_NEXT_IN(data, buff);
241 	data->stream.avail_in = length;
242 	if (drive_compressor(f, data, 0))
243 		return (ARCHIVE_FATAL);
244 	return (ARCHIVE_OK);
245 }
246 
247 
248 /*
249  * Finish the compression.
250  */
251 static int
252 archive_compressor_bzip2_close(struct archive_write_filter *f)
253 {
254 	struct private_data *data = (struct private_data *)f->data;
255 	int ret, r1;
256 
257 	/* Finish compression cycle. */
258 	ret = drive_compressor(f, data, 1);
259 	if (ret == ARCHIVE_OK) {
260 		/* Write the last block */
261 		ret = __archive_write_filter(f->next_filter,
262 		    data->compressed,
263 		    data->compressed_buffer_size - data->stream.avail_out);
264 	}
265 
266 	switch (BZ2_bzCompressEnd(&(data->stream))) {
267 	case BZ_OK:
268 		break;
269 	default:
270 		archive_set_error(f->archive, ARCHIVE_ERRNO_PROGRAMMER,
271 		    "Failed to clean up compressor");
272 		ret = ARCHIVE_FATAL;
273 	}
274 
275 	r1 = __archive_write_close_filter(f->next_filter);
276 	return (r1 < ret ? r1 : ret);
277 }
278 
279 static int
280 archive_compressor_bzip2_free(struct archive_write_filter *f)
281 {
282 	struct private_data *data = (struct private_data *)f->data;
283 	free(data->compressed);
284 	free(data);
285 	f->data = NULL;
286 	return (ARCHIVE_OK);
287 }
288 
289 /*
290  * Utility function to push input data through compressor, writing
291  * full output blocks as necessary.
292  *
293  * Note that this handles both the regular write case (finishing ==
294  * false) and the end-of-archive case (finishing == true).
295  */
296 static int
297 drive_compressor(struct archive_write_filter *f,
298     struct private_data *data, int finishing)
299 {
300 	int ret;
301 
302 	for (;;) {
303 		if (data->stream.avail_out == 0) {
304 			ret = __archive_write_filter(f->next_filter,
305 			    data->compressed,
306 			    data->compressed_buffer_size);
307 			if (ret != ARCHIVE_OK) {
308 				/* TODO: Handle this write failure */
309 				return (ARCHIVE_FATAL);
310 			}
311 			data->stream.next_out = data->compressed;
312 			data->stream.avail_out = data->compressed_buffer_size;
313 		}
314 
315 		/* If there's nothing to do, we're done. */
316 		if (!finishing && data->stream.avail_in == 0)
317 			return (ARCHIVE_OK);
318 
319 		ret = BZ2_bzCompress(&(data->stream),
320 		    finishing ? BZ_FINISH : BZ_RUN);
321 
322 		switch (ret) {
323 		case BZ_RUN_OK:
324 			/* In non-finishing case, did compressor
325 			 * consume everything? */
326 			if (!finishing && data->stream.avail_in == 0)
327 				return (ARCHIVE_OK);
328 			break;
329 		case BZ_FINISH_OK:  /* Finishing: There's more work to do */
330 			break;
331 		case BZ_STREAM_END: /* Finishing: all done */
332 			/* Only occurs in finishing case */
333 			return (ARCHIVE_OK);
334 		default:
335 			/* Any other return value indicates an error */
336 			archive_set_error(f->archive,
337 			    ARCHIVE_ERRNO_PROGRAMMER,
338 			    "Bzip2 compression failed;"
339 			    " BZ2_bzCompress() returned %d",
340 			    ret);
341 			return (ARCHIVE_FATAL);
342 		}
343 	}
344 }
345 
346 #endif /* HAVE_BZLIB_H && BZ_CONFIG_ERROR */
347