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_bzip2.c 201091 2009-12-28 02:22:41Z kientzle $");
45 #include "archive_private.h"
46 #include "archive_write_private.h"
48 #if ARCHIVE_VERSION_NUMBER < 4000000
50 archive_write_set_compression_bzip2(struct archive *a)
52 __archive_write_filters_free(a);
53 return (archive_write_add_filter_bzip2(a));
57 #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR)
59 archive_write_add_filter_bzip2(struct archive *a)
61 archive_set_error(a, ARCHIVE_ERRNO_MISC,
62 "bzip2 compression not supported on this platform");
63 return (ARCHIVE_FATAL);
66 /* Don't compile this if we don't have bzlib. */
69 int compression_level;
73 size_t compressed_buffer_size;
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.
80 #define SET_NEXT_IN(st,src) \
81 (st)->stream.next_in = (char *)(uintptr_t)(const void *)(src)
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);
94 * Add a bzip2 compression filter to this write handle.
97 archive_write_add_filter_bzip2(struct archive *_a)
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;
103 archive_check_magic(&a->archive, ARCHIVE_WRITE_MAGIC,
104 ARCHIVE_STATE_NEW, "archive_write_add_filter_bzip2");
106 data = calloc(1, sizeof(*data));
108 archive_set_error(&a->archive, ENOMEM, "Out of memory");
109 return (ARCHIVE_FATAL);
111 data->compression_level = 9; /* default */
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;
127 archive_compressor_bzip2_open(struct archive_write_filter *f)
129 struct private_data *data = (struct private_data *)f->data;
132 ret = __archive_write_open_filter(f->next_filter);
136 /* TODO: Find a better way to size this. (Maybe look at the */
137 /* block size expected by the following filter?) */
138 if (data->compressed == NULL) {
139 data->compressed_buffer_size = 65536;
141 = (char *)malloc(data->compressed_buffer_size);
142 if (data->compressed == NULL) {
143 archive_set_error(f->archive, ENOMEM,
144 "Can't allocate data for compression buffer");
145 return (ARCHIVE_FATAL);
149 memset(&data->stream, 0, sizeof(data->stream));
150 data->stream.next_out = data->compressed;
151 data->stream.avail_out = data->compressed_buffer_size;
152 f->write = archive_compressor_bzip2_write;
154 /* Initialize compression library */
155 ret = BZ2_bzCompressInit(&(data->stream),
156 data->compression_level, 0, 30);
162 /* Library setup failed: clean up. */
163 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
164 "Internal error initializing compression library");
166 /* Override the error message if we know what really went wrong. */
169 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
170 "Internal error initializing compression library: "
171 "invalid setup parameter");
174 archive_set_error(f->archive, ENOMEM,
175 "Internal error initializing compression library: "
178 case BZ_CONFIG_ERROR:
179 archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
180 "Internal error initializing compression library: "
181 "mis-compiled library");
185 return (ARCHIVE_FATAL);
193 archive_compressor_bzip2_options(struct archive_write_filter *f,
194 const char *key, const char *value)
196 struct private_data *data = (struct private_data *)f->data;
198 if (strcmp(key, "compression-level") == 0) {
199 if (value == NULL || !(value[0] >= '0' && value[0] <= '9') ||
201 return (ARCHIVE_WARN);
202 data->compression_level = value[0] - '0';
203 /* Make '0' be a synonym for '1'. */
204 /* This way, bzip2 compressor supports the same 0..9
205 * range of levels as gzip. */
206 if (data->compression_level < 1)
207 data->compression_level = 1;
211 /* Note: The "warn" return is just to inform the options
212 * supervisor that we didn't handle it. It will generate
213 * a suitable error if no one used this option. */
214 return (ARCHIVE_WARN);
218 * Write data to the compressed stream.
220 * Returns ARCHIVE_OK if all data written, error otherwise.
223 archive_compressor_bzip2_write(struct archive_write_filter *f,
224 const void *buff, size_t length)
226 struct private_data *data = (struct private_data *)f->data;
228 /* Update statistics */
229 data->total_in += length;
231 /* Compress input data to output buffer */
232 SET_NEXT_IN(data, buff);
233 data->stream.avail_in = length;
234 if (drive_compressor(f, data, 0))
235 return (ARCHIVE_FATAL);
241 * Finish the compression.
244 archive_compressor_bzip2_close(struct archive_write_filter *f)
246 struct private_data *data = (struct private_data *)f->data;
249 /* Finish compression cycle. */
250 ret = drive_compressor(f, data, 1);
251 if (ret == ARCHIVE_OK) {
252 /* Write the last block */
253 ret = __archive_write_filter(f->next_filter,
255 data->compressed_buffer_size - data->stream.avail_out);
258 switch (BZ2_bzCompressEnd(&(data->stream))) {
262 archive_set_error(f->archive, ARCHIVE_ERRNO_PROGRAMMER,
263 "Failed to clean up compressor");
267 r1 = __archive_write_close_filter(f->next_filter);
268 return (r1 < ret ? r1 : ret);
272 archive_compressor_bzip2_free(struct archive_write_filter *f)
274 struct private_data *data = (struct private_data *)f->data;
275 free(data->compressed);
282 * Utility function to push input data through compressor, writing
283 * full output blocks as necessary.
285 * Note that this handles both the regular write case (finishing ==
286 * false) and the end-of-archive case (finishing == true).
289 drive_compressor(struct archive_write_filter *f,
290 struct private_data *data, int finishing)
295 if (data->stream.avail_out == 0) {
296 ret = __archive_write_filter(f->next_filter,
298 data->compressed_buffer_size);
299 if (ret != ARCHIVE_OK) {
300 /* TODO: Handle this write failure */
301 return (ARCHIVE_FATAL);
303 data->stream.next_out = data->compressed;
304 data->stream.avail_out = data->compressed_buffer_size;
307 /* If there's nothing to do, we're done. */
308 if (!finishing && data->stream.avail_in == 0)
311 ret = BZ2_bzCompress(&(data->stream),
312 finishing ? BZ_FINISH : BZ_RUN);
316 /* In non-finishing case, did compressor
317 * consume everything? */
318 if (!finishing && data->stream.avail_in == 0)
321 case BZ_FINISH_OK: /* Finishing: There's more work to do */
323 case BZ_STREAM_END: /* Finishing: all done */
324 /* Only occurs in finishing case */
327 /* Any other return value indicates an error */
328 archive_set_error(f->archive,
329 ARCHIVE_ERRNO_PROGRAMMER,
330 "Bzip2 compression failed;"
331 " BZ2_bzCompress() returned %d",
333 return (ARCHIVE_FATAL);
338 #endif /* HAVE_BZLIB_H && BZ_CONFIG_ERROR */