xref: /freebsd-src/contrib/libarchive/libarchive/archive_write_add_filter_bzip2.c (revision bd66c1b43e33540205dbc1187c2f2a15c58b57ba)
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * Copyright (c) 2012 Michihiro NAKAJIMA
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "archive_platform.h"
28 
29 #ifdef HAVE_ERRNO_H
30 #include <errno.h>
31 #endif
32 #include <stdio.h>
33 #ifdef HAVE_STDLIB_H
34 #include <stdlib.h>
35 #endif
36 #ifdef HAVE_STRING_H
37 #include <string.h>
38 #endif
39 #ifdef HAVE_BZLIB_H
40 #include <bzlib.h>
41 #endif
42 
43 #include "archive.h"
44 #include "archive_private.h"
45 #include "archive_write_private.h"
46 
47 #if ARCHIVE_VERSION_NUMBER < 4000000
48 int
49 archive_write_set_compression_bzip2(struct archive *a)
50 {
51 	__archive_write_filters_free(a);
52 	return (archive_write_add_filter_bzip2(a));
53 }
54 #endif
55 
56 struct private_data {
57 	int		 compression_level;
58 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
59 	bz_stream	 stream;
60 	int64_t		 total_in;
61 	char		*compressed;
62 	size_t		 compressed_buffer_size;
63 #else
64 	struct archive_write_program_data *pdata;
65 #endif
66 };
67 
68 static int archive_compressor_bzip2_close(struct archive_write_filter *);
69 static int archive_compressor_bzip2_free(struct archive_write_filter *);
70 static int archive_compressor_bzip2_open(struct archive_write_filter *);
71 static int archive_compressor_bzip2_options(struct archive_write_filter *,
72 		    const char *, const char *);
73 static int archive_compressor_bzip2_write(struct archive_write_filter *,
74 		    const void *, size_t);
75 
76 /*
77  * Add a bzip2 compression filter to this write handle.
78  */
79 int
80 archive_write_add_filter_bzip2(struct archive *_a)
81 {
82 	struct archive_write *a = (struct archive_write *)_a;
83 	struct archive_write_filter *f = __archive_write_allocate_filter(_a);
84 	struct private_data *data;
85 
86 	archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
87 	    ARCHIVE_STATE_NEW, "archive_write_add_filter_bzip2");
88 
89 	data = calloc(1, sizeof(*data));
90 	if (data == NULL) {
91 		archive_set_error(&a->archive, ENOMEM, "Out of memory");
92 		return (ARCHIVE_FATAL);
93 	}
94 	data->compression_level = 9; /* default */
95 
96 	f->data = data;
97 	f->options = &archive_compressor_bzip2_options;
98 	f->close = &archive_compressor_bzip2_close;
99 	f->free = &archive_compressor_bzip2_free;
100 	f->open = &archive_compressor_bzip2_open;
101 	f->code = ARCHIVE_FILTER_BZIP2;
102 	f->name = "bzip2";
103 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
104 	return (ARCHIVE_OK);
105 #else
106 	data->pdata = __archive_write_program_allocate("bzip2");
107 	if (data->pdata == NULL) {
108 		free(data);
109 		archive_set_error(&a->archive, ENOMEM, "Out of memory");
110 		return (ARCHIVE_FATAL);
111 	}
112 	data->compression_level = 0;
113 	archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
114 	    "Using external bzip2 program");
115 	return (ARCHIVE_WARN);
116 #endif
117 }
118 
119 /*
120  * Set write options.
121  */
122 static int
123 archive_compressor_bzip2_options(struct archive_write_filter *f,
124     const char *key, const char *value)
125 {
126 	struct private_data *data = (struct private_data *)f->data;
127 
128 	if (strcmp(key, "compression-level") == 0) {
129 		if (value == NULL || !(value[0] >= '0' && value[0] <= '9') ||
130 		    value[1] != '\0')
131 			return (ARCHIVE_WARN);
132 		data->compression_level = value[0] - '0';
133 		/* Make '0' be a synonym for '1'. */
134 		/* This way, bzip2 compressor supports the same 0..9
135 		 * range of levels as gzip. */
136 		if (data->compression_level < 1)
137 			data->compression_level = 1;
138 		return (ARCHIVE_OK);
139 	}
140 
141 	/* Note: The "warn" return is just to inform the options
142 	 * supervisor that we didn't handle it.  It will generate
143 	 * a suitable error if no one used this option. */
144 	return (ARCHIVE_WARN);
145 }
146 
147 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
148 /* Don't compile this if we don't have bzlib. */
149 
150 /*
151  * Yuck.  bzlib.h is not const-correct, so I need this one bit
152  * of ugly hackery to convert a const * pointer to a non-const pointer.
153  */
154 #define	SET_NEXT_IN(st,src)					\
155 	(st)->stream.next_in = (char *)(uintptr_t)(const void *)(src)
156 static int drive_compressor(struct archive_write_filter *,
157 		    struct private_data *, int finishing);
158 
159 /*
160  * Setup callback.
161  */
162 static int
163 archive_compressor_bzip2_open(struct archive_write_filter *f)
164 {
165 	struct private_data *data = (struct private_data *)f->data;
166 	int ret;
167 
168 	if (data->compressed == NULL) {
169 		size_t bs = 65536, bpb;
170 		if (f->archive->magic == ARCHIVE_WRITE_MAGIC) {
171 			/* Buffer size should be a multiple number of the bytes
172 			 * per block for performance. */
173 			bpb = archive_write_get_bytes_per_block(f->archive);
174 			if (bpb > bs)
175 				bs = bpb;
176 			else if (bpb != 0)
177 				bs -= bs % bpb;
178 		}
179 		data->compressed_buffer_size = bs;
180 		data->compressed = malloc(data->compressed_buffer_size);
181 		if (data->compressed == NULL) {
182 			archive_set_error(f->archive, ENOMEM,
183 			    "Can't allocate data for compression buffer");
184 			return (ARCHIVE_FATAL);
185 		}
186 	}
187 
188 	memset(&data->stream, 0, sizeof(data->stream));
189 	data->stream.next_out = data->compressed;
190 	data->stream.avail_out = (uint32_t)data->compressed_buffer_size;
191 	f->write = archive_compressor_bzip2_write;
192 
193 	/* Initialize compression library */
194 	ret = BZ2_bzCompressInit(&(data->stream),
195 	    data->compression_level, 0, 30);
196 	if (ret == BZ_OK) {
197 		f->data = data;
198 		return (ARCHIVE_OK);
199 	}
200 
201 	/* Library setup failed: clean up. */
202 	archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
203 	    "Internal error initializing compression library");
204 
205 	/* Override the error message if we know what really went wrong. */
206 	switch (ret) {
207 	case BZ_PARAM_ERROR:
208 		archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
209 		    "Internal error initializing compression library: "
210 		    "invalid setup parameter");
211 		break;
212 	case BZ_MEM_ERROR:
213 		archive_set_error(f->archive, ENOMEM,
214 		    "Internal error initializing compression library: "
215 		    "out of memory");
216 		break;
217 	case BZ_CONFIG_ERROR:
218 		archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
219 		    "Internal error initializing compression library: "
220 		    "mis-compiled library");
221 		break;
222 	}
223 
224 	return (ARCHIVE_FATAL);
225 
226 }
227 
228 /*
229  * Write data to the compressed stream.
230  *
231  * Returns ARCHIVE_OK if all data written, error otherwise.
232  */
233 static int
234 archive_compressor_bzip2_write(struct archive_write_filter *f,
235     const void *buff, size_t length)
236 {
237 	struct private_data *data = (struct private_data *)f->data;
238 
239 	/* Update statistics */
240 	data->total_in += length;
241 
242 	/* Compress input data to output buffer */
243 	SET_NEXT_IN(data, buff);
244 	data->stream.avail_in = (uint32_t)length;
245 	if (drive_compressor(f, data, 0))
246 		return (ARCHIVE_FATAL);
247 	return (ARCHIVE_OK);
248 }
249 
250 
251 /*
252  * Finish the compression.
253  */
254 static int
255 archive_compressor_bzip2_close(struct archive_write_filter *f)
256 {
257 	struct private_data *data = (struct private_data *)f->data;
258 	int ret;
259 
260 	/* Finish compression cycle. */
261 	ret = drive_compressor(f, data, 1);
262 	if (ret == ARCHIVE_OK) {
263 		/* Write the last block */
264 		ret = __archive_write_filter(f->next_filter,
265 		    data->compressed,
266 		    data->compressed_buffer_size - data->stream.avail_out);
267 	}
268 
269 	switch (BZ2_bzCompressEnd(&(data->stream))) {
270 	case BZ_OK:
271 		break;
272 	default:
273 		archive_set_error(f->archive, ARCHIVE_ERRNO_PROGRAMMER,
274 		    "Failed to clean up compressor");
275 		ret = ARCHIVE_FATAL;
276 	}
277 	return ret;
278 }
279 
280 static int
281 archive_compressor_bzip2_free(struct archive_write_filter *f)
282 {
283 	struct private_data *data = (struct private_data *)f->data;
284 	free(data->compressed);
285 	free(data);
286 	f->data = NULL;
287 	return (ARCHIVE_OK);
288 }
289 
290 /*
291  * Utility function to push input data through compressor, writing
292  * full output blocks as necessary.
293  *
294  * Note that this handles both the regular write case (finishing ==
295  * false) and the end-of-archive case (finishing == true).
296  */
297 static int
298 drive_compressor(struct archive_write_filter *f,
299     struct private_data *data, int finishing)
300 {
301 	int ret;
302 
303 	for (;;) {
304 		if (data->stream.avail_out == 0) {
305 			ret = __archive_write_filter(f->next_filter,
306 			    data->compressed,
307 			    data->compressed_buffer_size);
308 			if (ret != ARCHIVE_OK) {
309 				/* TODO: Handle this write failure */
310 				return (ARCHIVE_FATAL);
311 			}
312 			data->stream.next_out = data->compressed;
313 			data->stream.avail_out = (uint32_t)data->compressed_buffer_size;
314 		}
315 
316 		/* If there's nothing to do, we're done. */
317 		if (!finishing && data->stream.avail_in == 0)
318 			return (ARCHIVE_OK);
319 
320 		ret = BZ2_bzCompress(&(data->stream),
321 		    finishing ? BZ_FINISH : BZ_RUN);
322 
323 		switch (ret) {
324 		case BZ_RUN_OK:
325 			/* In non-finishing case, did compressor
326 			 * consume everything? */
327 			if (!finishing && data->stream.avail_in == 0)
328 				return (ARCHIVE_OK);
329 			break;
330 		case BZ_FINISH_OK:  /* Finishing: There's more work to do */
331 			break;
332 		case BZ_STREAM_END: /* Finishing: all done */
333 			/* Only occurs in finishing case */
334 			return (ARCHIVE_OK);
335 		default:
336 			/* Any other return value indicates an error */
337 			archive_set_error(f->archive,
338 			    ARCHIVE_ERRNO_PROGRAMMER,
339 			    "Bzip2 compression failed;"
340 			    " BZ2_bzCompress() returned %d",
341 			    ret);
342 			return (ARCHIVE_FATAL);
343 		}
344 	}
345 }
346 
347 #else /* HAVE_BZLIB_H && BZ_CONFIG_ERROR */
348 
349 static int
350 archive_compressor_bzip2_open(struct archive_write_filter *f)
351 {
352 	struct private_data *data = (struct private_data *)f->data;
353 	struct archive_string as;
354 	int r;
355 
356 	archive_string_init(&as);
357 	archive_strcpy(&as, "bzip2");
358 
359 	/* Specify compression level. */
360 	if (data->compression_level > 0) {
361 		archive_strcat(&as, " -");
362 		archive_strappend_char(&as, '0' + data->compression_level);
363 	}
364 	f->write = archive_compressor_bzip2_write;
365 
366 	r = __archive_write_program_open(f, data->pdata, as.s);
367 	archive_string_free(&as);
368 	return (r);
369 }
370 
371 static int
372 archive_compressor_bzip2_write(struct archive_write_filter *f, const void *buff,
373     size_t length)
374 {
375 	struct private_data *data = (struct private_data *)f->data;
376 
377 	return __archive_write_program_write(f, data->pdata, buff, length);
378 }
379 
380 static int
381 archive_compressor_bzip2_close(struct archive_write_filter *f)
382 {
383 	struct private_data *data = (struct private_data *)f->data;
384 
385 	return __archive_write_program_close(f, data->pdata);
386 }
387 
388 static int
389 archive_compressor_bzip2_free(struct archive_write_filter *f)
390 {
391 	struct private_data *data = (struct private_data *)f->data;
392 
393 	__archive_write_program_free(data->pdata);
394 	free(data);
395 	return (ARCHIVE_OK);
396 }
397 
398 #endif /* HAVE_BZLIB_H && BZ_CONFIG_ERROR */
399