xref: /freebsd/contrib/libarchive/libarchive/archive_write_add_filter_bzip2.c (revision eb5165bb491138f60d9004bc4c781490016d9288)
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
archive_write_set_compression_bzip2(struct archive * a)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
archive_write_add_filter_bzip2(struct archive * _a)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
archive_compressor_bzip2_options(struct archive_write_filter * f,const char * key,const char * value)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 			archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
132 			    "compression-level invalid");
133 			return (ARCHIVE_FAILED);
134 		}
135 		data->compression_level = value[0] - '0';
136 		/* Make '0' be a synonym for '1'. */
137 		/* This way, bzip2 compressor supports the same 0..9
138 		 * range of levels as gzip. */
139 		if (data->compression_level < 1)
140 			data->compression_level = 1;
141 		return (ARCHIVE_OK);
142 	}
143 
144 	/* Note: The "warn" return is just to inform the options
145 	 * supervisor that we didn't handle it.  It will generate
146 	 * a suitable error if no one used this option. */
147 	return (ARCHIVE_WARN);
148 }
149 
150 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
151 /* Don't compile this if we don't have bzlib. */
152 
153 /*
154  * Yuck.  bzlib.h is not const-correct, so I need this one bit
155  * of ugly hackery to convert a const * pointer to a non-const pointer.
156  */
157 #define	SET_NEXT_IN(st,src)					\
158 	(st)->stream.next_in = (char *)(uintptr_t)(const void *)(src)
159 static int drive_compressor(struct archive_write_filter *,
160 		    struct private_data *, int finishing);
161 
162 /*
163  * Setup callback.
164  */
165 static int
archive_compressor_bzip2_open(struct archive_write_filter * f)166 archive_compressor_bzip2_open(struct archive_write_filter *f)
167 {
168 	struct private_data *data = (struct private_data *)f->data;
169 	int ret;
170 
171 	if (data->compressed == NULL) {
172 		size_t bs = 65536, bpb;
173 		if (f->archive->magic == ARCHIVE_WRITE_MAGIC) {
174 			/* Buffer size should be a multiple number of the bytes
175 			 * per block for performance. */
176 			bpb = archive_write_get_bytes_per_block(f->archive);
177 			if (bpb > bs)
178 				bs = bpb;
179 			else if (bpb != 0)
180 				bs -= bs % bpb;
181 		}
182 		data->compressed_buffer_size = bs;
183 		data->compressed = malloc(data->compressed_buffer_size);
184 		if (data->compressed == NULL) {
185 			archive_set_error(f->archive, ENOMEM,
186 			    "Can't allocate data for compression buffer");
187 			return (ARCHIVE_FATAL);
188 		}
189 	}
190 
191 	memset(&data->stream, 0, sizeof(data->stream));
192 	data->stream.next_out = data->compressed;
193 	data->stream.avail_out = (uint32_t)data->compressed_buffer_size;
194 	f->write = archive_compressor_bzip2_write;
195 
196 	/* Initialize compression library */
197 	ret = BZ2_bzCompressInit(&(data->stream),
198 	    data->compression_level, 0, 30);
199 	if (ret == BZ_OK) {
200 		f->data = data;
201 		return (ARCHIVE_OK);
202 	}
203 
204 	/* Library setup failed: clean up. */
205 	archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
206 	    "Internal error initializing compression library");
207 
208 	/* Override the error message if we know what really went wrong. */
209 	switch (ret) {
210 	case BZ_PARAM_ERROR:
211 		archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
212 		    "Internal error initializing compression library: "
213 		    "invalid setup parameter");
214 		break;
215 	case BZ_MEM_ERROR:
216 		archive_set_error(f->archive, ENOMEM,
217 		    "Internal error initializing compression library: "
218 		    "out of memory");
219 		break;
220 	case BZ_CONFIG_ERROR:
221 		archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
222 		    "Internal error initializing compression library: "
223 		    "mis-compiled library");
224 		break;
225 	}
226 
227 	return (ARCHIVE_FATAL);
228 
229 }
230 
231 /*
232  * Write data to the compressed stream.
233  *
234  * Returns ARCHIVE_OK if all data written, error otherwise.
235  */
236 static int
archive_compressor_bzip2_write(struct archive_write_filter * f,const void * buff,size_t length)237 archive_compressor_bzip2_write(struct archive_write_filter *f,
238     const void *buff, size_t length)
239 {
240 	struct private_data *data = (struct private_data *)f->data;
241 
242 	/* Update statistics */
243 	data->total_in += length;
244 
245 	/* Compress input data to output buffer */
246 	SET_NEXT_IN(data, buff);
247 	data->stream.avail_in = (uint32_t)length;
248 	if (drive_compressor(f, data, 0))
249 		return (ARCHIVE_FATAL);
250 	return (ARCHIVE_OK);
251 }
252 
253 
254 /*
255  * Finish the compression.
256  */
257 static int
archive_compressor_bzip2_close(struct archive_write_filter * f)258 archive_compressor_bzip2_close(struct archive_write_filter *f)
259 {
260 	struct private_data *data = (struct private_data *)f->data;
261 	int ret;
262 
263 	/* Finish compression cycle. */
264 	ret = drive_compressor(f, data, 1);
265 	if (ret == ARCHIVE_OK) {
266 		/* Write the last block */
267 		ret = __archive_write_filter(f->next_filter,
268 		    data->compressed,
269 		    data->compressed_buffer_size - data->stream.avail_out);
270 	}
271 
272 	switch (BZ2_bzCompressEnd(&(data->stream))) {
273 	case BZ_OK:
274 		break;
275 	default:
276 		archive_set_error(f->archive, ARCHIVE_ERRNO_PROGRAMMER,
277 		    "Failed to clean up compressor");
278 		ret = ARCHIVE_FATAL;
279 	}
280 	return ret;
281 }
282 
283 static int
archive_compressor_bzip2_free(struct archive_write_filter * f)284 archive_compressor_bzip2_free(struct archive_write_filter *f)
285 {
286 	struct private_data *data = (struct private_data *)f->data;
287 
288 	/* May already have been called, but not necessarily. */
289 	(void)BZ2_bzCompressEnd(&(data->stream));
290 
291 	free(data->compressed);
292 	free(data);
293 	f->data = NULL;
294 	return (ARCHIVE_OK);
295 }
296 
297 /*
298  * Utility function to push input data through compressor, writing
299  * full output blocks as necessary.
300  *
301  * Note that this handles both the regular write case (finishing ==
302  * false) and the end-of-archive case (finishing == true).
303  */
304 static int
drive_compressor(struct archive_write_filter * f,struct private_data * data,int finishing)305 drive_compressor(struct archive_write_filter *f,
306     struct private_data *data, int finishing)
307 {
308 	int ret;
309 
310 	for (;;) {
311 		if (data->stream.avail_out == 0) {
312 			ret = __archive_write_filter(f->next_filter,
313 			    data->compressed,
314 			    data->compressed_buffer_size);
315 			if (ret != ARCHIVE_OK) {
316 				/* TODO: Handle this write failure */
317 				return (ARCHIVE_FATAL);
318 			}
319 			data->stream.next_out = data->compressed;
320 			data->stream.avail_out = (uint32_t)data->compressed_buffer_size;
321 		}
322 
323 		/* If there's nothing to do, we're done. */
324 		if (!finishing && data->stream.avail_in == 0)
325 			return (ARCHIVE_OK);
326 
327 		ret = BZ2_bzCompress(&(data->stream),
328 		    finishing ? BZ_FINISH : BZ_RUN);
329 
330 		switch (ret) {
331 		case BZ_RUN_OK:
332 			/* In non-finishing case, did compressor
333 			 * consume everything? */
334 			if (!finishing && data->stream.avail_in == 0)
335 				return (ARCHIVE_OK);
336 			break;
337 		case BZ_FINISH_OK:  /* Finishing: There's more work to do */
338 			break;
339 		case BZ_STREAM_END: /* Finishing: all done */
340 			/* Only occurs in finishing case */
341 			return (ARCHIVE_OK);
342 		default:
343 			/* Any other return value indicates an error */
344 			archive_set_error(f->archive,
345 			    ARCHIVE_ERRNO_PROGRAMMER,
346 			    "Bzip2 compression failed;"
347 			    " BZ2_bzCompress() returned %d",
348 			    ret);
349 			return (ARCHIVE_FATAL);
350 		}
351 	}
352 }
353 
354 #else /* HAVE_BZLIB_H && BZ_CONFIG_ERROR */
355 
356 static int
archive_compressor_bzip2_open(struct archive_write_filter * f)357 archive_compressor_bzip2_open(struct archive_write_filter *f)
358 {
359 	struct private_data *data = (struct private_data *)f->data;
360 	struct archive_string as;
361 	int r;
362 
363 	archive_string_init(&as);
364 	archive_strcpy(&as, "bzip2");
365 
366 	/* Specify compression level. */
367 	if (data->compression_level > 0) {
368 		archive_strcat(&as, " -");
369 		archive_strappend_char(&as, '0' + data->compression_level);
370 	}
371 	f->write = archive_compressor_bzip2_write;
372 
373 	r = __archive_write_program_open(f, data->pdata, as.s);
374 	archive_string_free(&as);
375 	return (r);
376 }
377 
378 static int
archive_compressor_bzip2_write(struct archive_write_filter * f,const void * buff,size_t length)379 archive_compressor_bzip2_write(struct archive_write_filter *f, const void *buff,
380     size_t length)
381 {
382 	struct private_data *data = (struct private_data *)f->data;
383 
384 	return __archive_write_program_write(f, data->pdata, buff, length);
385 }
386 
387 static int
archive_compressor_bzip2_close(struct archive_write_filter * f)388 archive_compressor_bzip2_close(struct archive_write_filter *f)
389 {
390 	struct private_data *data = (struct private_data *)f->data;
391 
392 	return __archive_write_program_close(f, data->pdata);
393 }
394 
395 static int
archive_compressor_bzip2_free(struct archive_write_filter * f)396 archive_compressor_bzip2_free(struct archive_write_filter *f)
397 {
398 	struct private_data *data = (struct private_data *)f->data;
399 
400 	__archive_write_program_free(data->pdata);
401 	free(data);
402 	return (ARCHIVE_OK);
403 }
404 
405 #endif /* HAVE_BZLIB_H && BZ_CONFIG_ERROR */
406